1 /* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
23 Java and all Java-based marks are trademarks or registered trademarks
24 of Sun Microsystems, Inc. in the United States and other countries.
25 The Free Software Foundation is independent of Sun Microsystems, Inc. */
27 /* This file parses java source code and issues a tree node image
28 suitable for code generation (byte code and targeted CPU assembly
31 The grammar conforms to the Java grammar described in "The Java(TM)
32 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
33 1996, ISBN 0-201-63451-1"
35 The following modifications were brought to the original grammar:
37 method_body: added the rule '| block SC_TK'
38 static_initializer: added the rule 'static block SC_TK'.
40 Note: All the extra rules described above should go away when the
41 empty_statement rule will work.
43 statement_nsi: 'nsi' should be read no_short_if.
45 Some rules have been modified to support JDK1.1 inner classes
46 definitions and other extensions. */
57 #include "java-tree.h"
70 #define DIR_SEPARATOR '/'
73 /* Local function prototypes */
74 static char *java_accstring_lookup PARAMS ((int));
75 static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
76 static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
77 static tree create_class PARAMS ((int, tree, tree, tree));
78 static tree create_interface PARAMS ((int, tree, tree));
79 static void end_class_declaration PARAMS ((int));
80 static tree find_field PARAMS ((tree, tree));
81 static tree lookup_field_wrapper PARAMS ((tree, tree));
82 static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
83 static void register_fields PARAMS ((int, tree, tree));
84 static tree parser_qualified_classname PARAMS ((tree));
85 static int parser_check_super PARAMS ((tree, tree, tree));
86 static int parser_check_super_interface PARAMS ((tree, tree, tree));
87 static void check_modifiers_consistency PARAMS ((int));
88 static tree lookup_cl PARAMS ((tree));
89 static tree lookup_java_method2 PARAMS ((tree, tree, int));
90 static tree method_header PARAMS ((int, tree, tree, tree));
91 static void fix_method_argument_names PARAMS ((tree ,tree));
92 static tree method_declarator PARAMS ((tree, tree));
93 static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
95 static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list))
96 ATTRIBUTE_PRINTF (2, 0);
97 static void parse_ctor_invocation_error PARAMS ((void));
98 static tree parse_jdk1_1_error PARAMS ((const char *));
99 static void complete_class_report_errors PARAMS ((jdep *));
100 static int process_imports PARAMS ((void));
101 static void read_import_dir PARAMS ((tree));
102 static int find_in_imports_on_demand PARAMS ((tree));
103 static void find_in_imports PARAMS ((tree));
104 static void check_static_final_variable_assignment_flag PARAMS ((tree));
105 static void reset_static_final_variable_assignment_flag PARAMS ((tree));
106 static void check_final_variable_local_assignment_flag PARAMS ((tree, tree));
107 static void reset_final_variable_local_assignment_flag PARAMS ((tree));
108 static int check_final_variable_indirect_assignment PARAMS ((tree));
109 static void check_final_variable_global_assignment_flag PARAMS ((tree));
110 static void check_inner_class_access PARAMS ((tree, tree, tree));
111 static int check_pkg_class_access PARAMS ((tree, tree));
112 static void register_package PARAMS ((tree));
113 static tree resolve_package PARAMS ((tree, tree *));
114 static tree lookup_package_type PARAMS ((const char *, int));
115 static tree resolve_class PARAMS ((tree, tree, tree, tree));
116 static void declare_local_variables PARAMS ((int, tree, tree));
117 static void source_start_java_method PARAMS ((tree));
118 static void source_end_java_method PARAMS ((void));
119 static tree find_name_in_single_imports PARAMS ((tree));
120 static void check_abstract_method_header PARAMS ((tree));
121 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
122 static tree resolve_expression_name PARAMS ((tree, tree *));
123 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
124 static int check_class_interface_creation PARAMS ((int, int, tree,
126 static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
128 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
129 static int in_same_package PARAMS ((tree, tree));
130 static tree resolve_and_layout PARAMS ((tree, tree));
131 static tree qualify_and_find PARAMS ((tree, tree, tree));
132 static tree resolve_no_layout PARAMS ((tree, tree));
133 static int invocation_mode PARAMS ((tree, int));
134 static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
136 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
138 static tree find_most_specific_methods_list PARAMS ((tree));
139 static int argument_types_convertible PARAMS ((tree, tree));
140 static tree patch_invoke PARAMS ((tree, tree, tree));
141 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
142 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
143 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
144 static tree obtain_incomplete_type PARAMS ((tree));
145 static tree java_complete_lhs PARAMS ((tree));
146 static tree java_complete_tree PARAMS ((tree));
147 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
148 static int analyze_clinit_body PARAMS ((tree));
149 static int maybe_yank_clinit PARAMS ((tree));
150 static void java_complete_expand_method PARAMS ((tree));
151 static int unresolved_type_p PARAMS ((tree, tree *));
152 static void create_jdep_list PARAMS ((struct parser_ctxt *));
153 static tree build_expr_block PARAMS ((tree, tree));
154 static tree enter_block PARAMS ((void));
155 static tree exit_block PARAMS ((void));
156 static tree lookup_name_in_blocks PARAMS ((tree));
157 static void maybe_absorb_scoping_blocks PARAMS ((void));
158 static tree build_method_invocation PARAMS ((tree, tree));
159 static tree build_new_invocation PARAMS ((tree, tree));
160 static tree build_assignment PARAMS ((int, int, tree, tree));
161 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
162 static int check_final_assignment PARAMS ((tree ,tree));
163 static tree patch_assignment PARAMS ((tree, tree, tree ));
164 static tree patch_binop PARAMS ((tree, tree, tree));
165 static tree build_unaryop PARAMS ((int, int, tree));
166 static tree build_incdec PARAMS ((int, int, tree, int));
167 static tree patch_unaryop PARAMS ((tree, tree));
168 static tree build_cast PARAMS ((int, tree, tree));
169 static tree build_null_of_type PARAMS ((tree));
170 static tree patch_cast PARAMS ((tree, tree));
171 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
172 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
173 static int valid_cast_to_p PARAMS ((tree, tree));
174 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
175 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
176 static tree try_reference_assignconv PARAMS ((tree, tree));
177 static tree build_unresolved_array_type PARAMS ((tree));
178 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
179 static tree build_array_ref PARAMS ((int, tree, tree));
180 static tree patch_array_ref PARAMS ((tree));
181 static tree make_qualified_name PARAMS ((tree, tree, int));
182 static tree merge_qualified_name PARAMS ((tree, tree));
183 static tree make_qualified_primary PARAMS ((tree, tree, int));
184 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
186 static void qualify_ambiguous_name PARAMS ((tree));
187 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
188 static tree build_newarray_node PARAMS ((tree, tree, int));
189 static tree patch_newarray PARAMS ((tree));
190 static tree resolve_type_during_patch PARAMS ((tree));
191 static tree build_this PARAMS ((int));
192 static tree build_wfl_wrap PARAMS ((tree, int));
193 static tree build_return PARAMS ((int, tree));
194 static tree patch_return PARAMS ((tree));
195 static tree maybe_access_field PARAMS ((tree, tree, tree));
196 static int complete_function_arguments PARAMS ((tree));
197 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
199 static int not_accessible_p PARAMS ((tree, tree, tree, int));
200 static void check_deprecation PARAMS ((tree, tree));
201 static int class_in_current_package PARAMS ((tree));
202 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
203 static tree patch_if_else_statement PARAMS ((tree));
204 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
205 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
206 static tree patch_exit_expr PARAMS ((tree));
207 static tree build_labeled_block PARAMS ((int, tree));
208 static tree finish_labeled_statement PARAMS ((tree, tree));
209 static tree build_bc_statement PARAMS ((int, int, tree));
210 static tree patch_bc_statement PARAMS ((tree));
211 static tree patch_loop_statement PARAMS ((tree));
212 static tree build_new_loop PARAMS ((tree));
213 static tree build_loop_body PARAMS ((int, tree, int));
214 static tree finish_loop_body PARAMS ((int, tree, tree, int));
215 static tree build_debugable_stmt PARAMS ((int, tree));
216 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
217 static tree patch_switch_statement PARAMS ((tree));
218 static tree string_constant_concatenation PARAMS ((tree, tree));
219 static tree build_string_concatenation PARAMS ((tree, tree));
220 static tree patch_string_cst PARAMS ((tree));
221 static tree patch_string PARAMS ((tree));
222 static tree build_try_statement PARAMS ((int, tree, tree));
223 static tree build_try_finally_statement PARAMS ((int, tree, tree));
224 static tree patch_try_statement PARAMS ((tree));
225 static tree patch_synchronized_statement PARAMS ((tree, tree));
226 static tree patch_throw_statement PARAMS ((tree, tree));
227 static void check_thrown_exceptions PARAMS ((int, tree));
228 static int check_thrown_exceptions_do PARAMS ((tree));
229 static void purge_unchecked_exceptions PARAMS ((tree));
230 static void check_throws_clauses PARAMS ((tree, tree, tree));
231 static void finish_method_declaration PARAMS ((tree));
232 static tree build_super_invocation PARAMS ((tree));
233 static int verify_constructor_circularity PARAMS ((tree, tree));
234 static char *constructor_circularity_msg PARAMS ((tree, tree));
235 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
237 static const char *get_printable_method_name PARAMS ((tree));
238 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
239 static tree generate_finit PARAMS ((tree));
240 static void add_instance_initializer PARAMS ((tree));
241 static void fix_constructors PARAMS ((tree));
242 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
244 static void craft_constructor PARAMS ((tree, tree));
245 static int verify_constructor_super PARAMS ((tree));
246 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
247 static void start_artificial_method_body PARAMS ((tree));
248 static void end_artificial_method_body PARAMS ((tree));
249 static int check_method_redefinition PARAMS ((tree, tree));
250 static int check_method_types_complete PARAMS ((tree));
251 static void java_check_regular_methods PARAMS ((tree));
252 static void java_check_abstract_methods PARAMS ((tree));
253 static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
254 static void unreachable_stmt_error PARAMS ((tree));
255 static tree find_expr_with_wfl PARAMS ((tree));
256 static void missing_return_error PARAMS ((tree));
257 static tree build_new_array_init PARAMS ((int, tree));
258 static tree patch_new_array_init PARAMS ((tree, tree));
259 static tree maybe_build_array_element_wfl PARAMS ((tree));
260 static int array_constructor_check_entry PARAMS ((tree, tree));
261 static const char *purify_type_name PARAMS ((const char *));
262 static tree fold_constant_for_init PARAMS ((tree, tree));
263 static tree strip_out_static_field_access_decl PARAMS ((tree));
264 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
265 static void static_ref_err PARAMS ((tree, tree, tree));
266 static void parser_add_interface PARAMS ((tree, tree, tree));
267 static void add_superinterfaces PARAMS ((tree, tree));
268 static tree jdep_resolve_class PARAMS ((jdep *));
269 static int note_possible_classname PARAMS ((const char *, int));
270 static void java_complete_expand_classes PARAMS ((void));
271 static void java_complete_expand_class PARAMS ((tree));
272 static void java_complete_expand_methods PARAMS ((tree));
273 static tree cut_identifier_in_qualified PARAMS ((tree));
274 static tree java_stabilize_reference PARAMS ((tree));
275 static tree do_unary_numeric_promotion PARAMS ((tree));
276 static char * operator_string PARAMS ((tree));
277 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
278 static tree merge_string_cste PARAMS ((tree, tree, int));
279 static tree java_refold PARAMS ((tree));
280 static int java_decl_equiv PARAMS ((tree, tree));
281 static int binop_compound_p PARAMS ((enum tree_code));
282 static tree search_loop PARAMS ((tree));
283 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
284 static int check_abstract_method_definitions PARAMS ((int, tree, tree));
285 static void java_check_abstract_method_definitions PARAMS ((tree));
286 static void java_debug_context_do PARAMS ((int));
287 static void java_parser_context_push_initialized_field PARAMS ((void));
288 static void java_parser_context_pop_initialized_field PARAMS ((void));
289 static tree reorder_static_initialized PARAMS ((tree));
290 static void java_parser_context_suspend PARAMS ((void));
291 static void java_parser_context_resume PARAMS ((void));
292 static int pop_current_osb PARAMS ((struct parser_ctxt *));
294 /* JDK 1.1 work. FIXME */
296 static tree maybe_make_nested_class_name PARAMS ((tree));
297 static void make_nested_class_name PARAMS ((tree));
298 static void set_nested_class_simple_name_value PARAMS ((tree, int));
299 static void link_nested_class_to_enclosing PARAMS ((void));
300 static tree find_as_inner_class PARAMS ((tree, tree, tree));
301 static tree find_as_inner_class_do PARAMS ((tree, tree));
302 static int check_inner_class_redefinition PARAMS ((tree, tree));
304 static tree build_thisn_assign PARAMS ((void));
305 static tree build_current_thisn PARAMS ((tree));
306 static tree build_access_to_thisn PARAMS ((tree, tree, int));
307 static tree maybe_build_thisn_access_method PARAMS ((tree));
309 static tree build_outer_field_access PARAMS ((tree, tree));
310 static tree build_outer_field_access_methods PARAMS ((tree));
311 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
313 static tree build_outer_method_access_method PARAMS ((tree));
314 static tree build_new_access_id PARAMS ((void));
315 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
318 static int outer_field_access_p PARAMS ((tree, tree));
319 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
321 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
322 static tree build_incomplete_class_ref PARAMS ((int, tree));
323 static tree patch_incomplete_class_ref PARAMS ((tree));
324 static tree create_anonymous_class PARAMS ((int, tree));
325 static void patch_anonymous_class PARAMS ((tree, tree, tree));
326 static void add_inner_class_fields PARAMS ((tree, tree));
328 static tree build_dot_class_method PARAMS ((tree));
329 static tree build_dot_class_method_invocation PARAMS ((tree));
330 static void create_new_parser_context PARAMS ((int));
331 static void mark_parser_ctxt PARAMS ((void *));
333 /* Number of error found so far. */
334 int java_error_count;
335 /* Number of warning found so far. */
336 int java_warning_count;
337 /* Tell when not to fold, when doing xrefs */
339 /* Cyclic inheritance report, as it can be set by layout_class */
340 const char *cyclic_inheritance_report;
342 /* Tell when we're within an instance initializer */
343 static int in_instance_initializer;
345 /* The current parser context */
346 struct parser_ctxt *ctxp;
348 /* List of things that were analyzed for which code will be generated */
349 struct parser_ctxt *ctxp_for_generation = NULL;
351 /* binop_lookup maps token to tree_code. It is used where binary
352 operations are involved and required by the parser. RDIV_EXPR
353 covers both integral/floating point division. The code is changed
354 once the type of both operator is worked out. */
356 static enum tree_code binop_lookup[19] =
358 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
359 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
360 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
361 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
362 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
364 #define BINOP_LOOKUP(VALUE) \
365 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
367 /* This is the end index for binary operators that can also be used
368 in compound assignements. */
369 #define BINOP_COMPOUND_CANDIDATES 11
371 /* The "$L" identifier we use to create labels. */
372 static tree label_id = NULL_TREE;
374 /* The "StringBuffer" identifier used for the String `+' operator. */
375 static tree wfl_string_buffer = NULL_TREE;
377 /* The "append" identifier used for String `+' operator. */
378 static tree wfl_append = NULL_TREE;
380 /* The "toString" identifier used for String `+' operator. */
381 static tree wfl_to_string = NULL_TREE;
383 /* The "java.lang" import qualified name. */
384 static tree java_lang_id = NULL_TREE;
386 /* The generated `inst$' identifier used for generated enclosing
387 instance/field access functions. */
388 static tree inst_id = NULL_TREE;
390 /* The "java.lang.Cloneable" qualified name. */
391 static tree java_lang_cloneable = NULL_TREE;
393 /* The "java.io.Serializable" qualified name. */
394 static tree java_io_serializable = NULL_TREE;
396 /* Context and flag for static blocks */
397 static tree current_static_block = NULL_TREE;
399 /* The generated `write_parm_value$' identifier. */
402 /* The list of all packages we've seen so far */
403 static tree package_list = NULL_TREE;
405 /* Hold THIS for the scope of the current public method decl. */
406 static tree current_this;
408 /* Hold a list of catch clauses list. The first element of this list is
409 the list of the catch clauses of the currently analysed try block. */
410 static tree currently_caught_type_list;
412 static tree src_parse_roots[1] = { NULL_TREE };
414 /* All classes seen from source code */
415 #define gclass_list src_parse_roots[0]
417 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
418 line and point it out. */
419 /* Should point out the one that don't fit. ASCII/unicode, going
422 #define check_modifiers(__message, __value, __mask) do { \
423 if ((__value) & ~(__mask)) \
425 int i, remainder = (__value) & ~(__mask); \
426 for (i = 0; i <= 10; i++) \
427 if ((1 << i) & remainder) \
428 parse_error_context (ctxp->modifier_ctx [i], (__message), \
429 java_accstring_lookup (1 << i)); \
451 /* Things defined here have to match the order of what's in the
452 binop_lookup table. */
454 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
455 %token LS_TK SRS_TK ZRS_TK
456 %token AND_TK XOR_TK OR_TK
457 %token BOOL_AND_TK BOOL_OR_TK
458 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
460 /* This maps to the same binop_lookup entry than the token above */
462 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
464 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
465 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
468 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
470 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
471 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
472 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
473 %token PAD_TK ABSTRACT_TK MODIFIER_TK
476 /* Keep those two in order, too */
477 %token DECR_TK INCR_TK
479 /* From now one, things can be in any order */
481 %token DEFAULT_TK IF_TK THROW_TK
482 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
483 %token THROWS_TK BREAK_TK IMPORT_TK
484 %token ELSE_TK INSTANCEOF_TK RETURN_TK
485 %token VOID_TK CATCH_TK INTERFACE_TK
486 %token CASE_TK EXTENDS_TK FINALLY_TK
487 %token SUPER_TK WHILE_TK CLASS_TK
488 %token SWITCH_TK CONST_TK TRY_TK
489 %token FOR_TK NEW_TK CONTINUE_TK
490 %token GOTO_TK PACKAGE_TK THIS_TK
492 %token BYTE_TK SHORT_TK INT_TK LONG_TK
493 %token CHAR_TK INTEGRAL_TK
495 %token FLOAT_TK DOUBLE_TK FP_TK
499 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
501 %token ASSIGN_ANY_TK ASSIGN_TK
502 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
504 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
505 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
507 %type <value> modifiers MODIFIER_TK final synchronized
509 %type <node> super ID_TK identifier
510 %type <node> name simple_name qualified_name
511 %type <node> type_declaration compilation_unit
512 field_declaration method_declaration extends_interfaces
513 interfaces interface_type_list
514 class_member_declaration
515 import_declarations package_declaration
516 type_declarations interface_body
517 interface_member_declaration constant_declaration
518 interface_member_declarations interface_type
519 abstract_method_declaration interface_type_list
520 %type <node> class_body_declaration class_member_declaration
521 static_initializer constructor_declaration block
522 %type <node> class_body_declarations constructor_header
523 %type <node> class_or_interface_type class_type class_type_list
524 constructor_declarator explicit_constructor_invocation
525 %type <node> dim_expr dim_exprs this_or_super throws
527 %type <node> variable_declarator_id variable_declarator
528 variable_declarators variable_initializer
529 variable_initializers constructor_body
532 %type <node> class_body block_end constructor_block_end
533 %type <node> statement statement_without_trailing_substatement
534 labeled_statement if_then_statement label_decl
535 if_then_else_statement while_statement for_statement
536 statement_nsi labeled_statement_nsi do_statement
537 if_then_else_statement_nsi while_statement_nsi
538 for_statement_nsi statement_expression_list for_init
539 for_update statement_expression expression_statement
540 primary_no_new_array expression primary
541 array_creation_expression array_type
542 class_instance_creation_expression field_access
543 method_invocation array_access something_dot_new
544 argument_list postfix_expression while_expression
545 post_increment_expression post_decrement_expression
546 unary_expression_not_plus_minus unary_expression
547 pre_increment_expression pre_decrement_expression
548 unary_expression_not_plus_minus cast_expression
549 multiplicative_expression additive_expression
550 shift_expression relational_expression
551 equality_expression and_expression
552 exclusive_or_expression inclusive_or_expression
553 conditional_and_expression conditional_or_expression
554 conditional_expression assignment_expression
555 left_hand_side assignment for_header for_begin
556 constant_expression do_statement_begin empty_statement
557 switch_statement synchronized_statement throw_statement
558 try_statement switch_expression switch_block
559 catches catch_clause catch_clause_parameter finally
560 anonymous_class_creation
561 %type <node> return_statement break_statement continue_statement
563 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
564 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
565 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
566 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
567 %type <operator> ASSIGN_ANY_TK assignment_operator
568 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
569 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
570 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
571 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
572 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
573 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
574 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
575 %type <operator> NEW_TK
577 %type <node> method_body
579 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
580 STRING_LIT_TK NULL_TK VOID_TK
582 %type <node> IF_TK WHILE_TK FOR_TK
584 %type <node> formal_parameter_list formal_parameter
585 method_declarator method_header
587 %type <node> primitive_type reference_type type
588 BOOLEAN_TK INTEGRAL_TK FP_TK
590 /* Added or modified JDK 1.1 rule types */
591 %type <node> type_literals
594 /* 19.2 Production from 2.3: The Syntactic Grammar */
597 /* Register static variables with the garbage
599 ggc_add_tree_root (&label_id, 1);
600 ggc_add_tree_root (&wfl_string_buffer, 1);
601 ggc_add_tree_root (&wfl_append, 1);
602 ggc_add_tree_root (&wfl_to_string, 1);
603 ggc_add_tree_root (&java_lang_id, 1);
604 ggc_add_tree_root (&inst_id, 1);
605 ggc_add_tree_root (&java_lang_cloneable, 1);
606 ggc_add_tree_root (&java_io_serializable, 1);
607 ggc_add_tree_root (¤t_static_block, 1);
608 ggc_add_tree_root (&wpv_id, 1);
609 ggc_add_tree_root (&package_list, 1);
610 ggc_add_tree_root (¤t_this, 1);
611 ggc_add_tree_root (¤tly_caught_type_list, 1);
612 ggc_add_root (&ctxp, 1,
613 sizeof (struct parser_ctxt *),
615 ggc_add_root (&ctxp_for_generation, 1,
616 sizeof (struct parser_ctxt *),
623 /* 19.3 Productions from 3: Lexical structure */
633 /* 19.4 Productions from 4: Types, Values and Variables */
646 class_or_interface_type
650 class_or_interface_type:
655 class_or_interface_type /* Default rule */
659 class_or_interface_type
665 int osb = pop_current_osb (ctxp);
666 tree t = build_java_array_type (($1), -1);
667 CLASS_LOADED_P (t) = 1;
669 t = build_unresolved_array_type (t);
674 int osb = pop_current_osb (ctxp);
677 t = build_unresolved_array_type (t);
682 /* 19.5 Productions from 6: Names */
684 simple_name /* Default rule */
685 | qualified_name /* Default rule */
689 identifier /* Default rule */
693 name DOT_TK identifier
694 { $$ = make_qualified_name ($1, $3, $2.location); }
701 /* 19.6: Production from 7: Packages */
704 | package_declaration
705 | import_declarations
707 | package_declaration import_declarations
708 | package_declaration type_declarations
709 | import_declarations type_declarations
710 | package_declaration import_declarations type_declarations
718 | import_declarations import_declaration
726 | type_declarations type_declaration
730 PACKAGE_TK name SC_TK
732 ctxp->package = EXPR_WFL_NODE ($2);
733 register_package (ctxp->package);
736 {yyerror ("Missing name"); RECOVER;}
737 | PACKAGE_TK name error
738 {yyerror ("';' expected"); RECOVER;}
742 single_type_import_declaration
743 | type_import_on_demand_declaration
746 single_type_import_declaration:
749 tree name = EXPR_WFL_NODE ($2), last_name;
750 int i = IDENTIFIER_LENGTH (name)-1;
751 const char *last = &IDENTIFIER_POINTER (name)[i];
752 while (last != IDENTIFIER_POINTER (name))
758 last_name = get_identifier (++last);
759 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
761 tree err = find_name_in_single_imports (last_name);
762 if (err && err != name)
764 ($2, "Ambiguous class: `%s' and `%s'",
765 IDENTIFIER_POINTER (name),
766 IDENTIFIER_POINTER (err));
768 REGISTER_IMPORT ($2, last_name);
771 REGISTER_IMPORT ($2, last_name);
774 {yyerror ("Missing name"); RECOVER;}
775 | IMPORT_TK name error
776 {yyerror ("';' expected"); RECOVER;}
779 type_import_on_demand_declaration:
780 IMPORT_TK name DOT_TK MULT_TK SC_TK
782 tree name = EXPR_WFL_NODE ($2);
783 /* Don't import java.lang.* twice. */
784 if (name != java_lang_id)
786 read_import_dir ($2);
787 ctxp->import_demand_list =
788 chainon (ctxp->import_demand_list,
789 build_tree_list ($2, NULL_TREE));
792 | IMPORT_TK name DOT_TK error
793 {yyerror ("'*' expected"); RECOVER;}
794 | IMPORT_TK name DOT_TK MULT_TK error
795 {yyerror ("';' expected"); RECOVER;}
800 { end_class_declaration (0); }
801 | interface_declaration
802 { end_class_declaration (0); }
807 yyerror ("Class or interface declaration expected");
811 /* 19.7 Shortened from the original:
812 modifiers: modifier | modifiers modifier
813 modifier: any of public... */
819 | modifiers MODIFIER_TK
824 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
825 java_accstring_lookup (acc));
833 /* 19.8.1 Production from $8.1: Class Declaration */
835 modifiers CLASS_TK identifier super interfaces
836 { create_class ($1, $3, $4, $5); }
838 | CLASS_TK identifier super interfaces
839 { create_class (0, $2, $3, $4); }
841 | modifiers CLASS_TK error
842 {yyerror ("Missing class name"); RECOVER;}
844 {yyerror ("Missing class name"); RECOVER;}
845 | CLASS_TK identifier error
847 if (!ctxp->class_err) yyerror ("'{' expected");
850 | modifiers CLASS_TK identifier error
851 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
856 | EXTENDS_TK class_type
858 | EXTENDS_TK class_type error
859 {yyerror ("'{' expected"); ctxp->class_err=1;}
861 {yyerror ("Missing super class name"); ctxp->class_err=1;}
866 | IMPLEMENTS_TK interface_type_list
868 | IMPLEMENTS_TK error
871 yyerror ("Missing interface name");
878 ctxp->interface_number = 1;
879 $$ = build_tree_list ($1, NULL_TREE);
881 | interface_type_list C_TK interface_type
883 ctxp->interface_number++;
884 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
886 | interface_type_list C_TK error
887 {yyerror ("Missing interface name"); RECOVER;}
893 /* Store the location of the `}' when doing xrefs */
895 DECL_END_SOURCE_LINE (GET_CPC ()) =
896 EXPR_WFL_ADD_COL ($2.location, 1);
899 | OCB_TK class_body_declarations CCB_TK
901 /* Store the location of the `}' when doing xrefs */
903 DECL_END_SOURCE_LINE (GET_CPC ()) =
904 EXPR_WFL_ADD_COL ($3.location, 1);
909 class_body_declarations:
910 class_body_declaration
911 | class_body_declarations class_body_declaration
914 class_body_declaration:
915 class_member_declaration
917 | constructor_declaration
918 | block /* Added, JDK1.1, instance initializer */
920 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
921 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
925 class_member_declaration:
928 | class_declaration /* Added, JDK1.1 inner classes */
929 { end_class_declaration (1); }
930 | interface_declaration /* Added, JDK1.1 inner interfaces */
931 { end_class_declaration (1); }
935 /* 19.8.2 Productions from 8.3: Field Declarations */
937 type variable_declarators SC_TK
938 { register_fields (0, $1, $2); }
939 | modifiers type variable_declarators SC_TK
942 ("Illegal modifier `%s' for field declaration",
943 $1, FIELD_MODIFIERS);
944 check_modifiers_consistency ($1);
945 register_fields ($1, $2, $3);
949 variable_declarators:
950 /* Should we use build_decl_list () instead ? FIXME */
951 variable_declarator /* Default rule */
952 | variable_declarators C_TK variable_declarator
953 { $$ = chainon ($1, $3); }
954 | variable_declarators C_TK error
955 {yyerror ("Missing term"); RECOVER;}
959 variable_declarator_id
960 { $$ = build_tree_list ($1, NULL_TREE); }
961 | variable_declarator_id ASSIGN_TK variable_initializer
963 if (java_error_count)
966 ($1, build_assignment ($2.token, $2.location, $1, $3));
968 | variable_declarator_id ASSIGN_TK error
970 yyerror ("Missing variable initializer");
971 $$ = build_tree_list ($1, NULL_TREE);
974 | variable_declarator_id ASSIGN_TK variable_initializer error
976 yyerror ("';' expected");
977 $$ = build_tree_list ($1, NULL_TREE);
982 variable_declarator_id:
984 | variable_declarator_id OSB_TK CSB_TK
985 { $$ = build_unresolved_array_type ($1); }
987 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
988 | variable_declarator_id OSB_TK error
990 tree node = java_lval.node;
991 if (node && (TREE_CODE (node) == INTEGER_CST
992 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
993 yyerror ("Can't specify array dimension in a declaration");
995 yyerror ("']' expected");
998 | variable_declarator_id CSB_TK error
999 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1002 variable_initializer:
1007 /* 19.8.3 Productions from 8.4: Method Declarations */
1011 current_function_decl = $1;
1012 if (current_function_decl
1013 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1014 source_start_java_method (current_function_decl);
1016 current_function_decl = NULL_TREE;
1019 { finish_method_declaration ($3); }
1020 | method_header error
1021 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1025 type method_declarator throws
1026 { $$ = method_header (0, $1, $2, $3); }
1027 | VOID_TK method_declarator throws
1028 { $$ = method_header (0, void_type_node, $2, $3); }
1029 | modifiers type method_declarator throws
1030 { $$ = method_header ($1, $2, $3, $4); }
1031 | modifiers VOID_TK method_declarator throws
1032 { $$ = method_header ($1, void_type_node, $3, $4); }
1035 yyerror ("Invalid method declaration, method name required");
1038 | modifiers type error
1041 {yyerror ("Identifier expected"); RECOVER;}
1042 | modifiers VOID_TK error
1043 {yyerror ("Identifier expected"); RECOVER;}
1046 yyerror ("Invalid method declaration, return type required");
1052 identifier OP_TK CP_TK
1054 ctxp->formal_parameter_number = 0;
1055 $$ = method_declarator ($1, NULL_TREE);
1057 | identifier OP_TK formal_parameter_list CP_TK
1058 { $$ = method_declarator ($1, $3); }
1059 | method_declarator OSB_TK CSB_TK
1061 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1063 build_unresolved_array_type (TREE_PURPOSE ($1));
1064 parse_warning_context
1066 "Discouraged form of returned type specification");
1068 | identifier OP_TK error
1069 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1070 | method_declarator OSB_TK error
1071 {yyerror ("']' expected"); RECOVER;}
1074 formal_parameter_list:
1077 ctxp->formal_parameter_number = 1;
1079 | formal_parameter_list C_TK formal_parameter
1081 ctxp->formal_parameter_number += 1;
1082 $$ = chainon ($1, $3);
1084 | formal_parameter_list C_TK error
1085 { yyerror ("Missing formal parameter term"); RECOVER; }
1089 type variable_declarator_id
1091 $$ = build_tree_list ($2, $1);
1093 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1095 $$ = build_tree_list ($3, $2);
1096 ARG_FINAL_P ($$) = 1;
1100 yyerror ("Missing identifier"); RECOVER;
1105 yyerror ("Missing identifier"); RECOVER;
1113 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1115 if ($1 != ACC_FINAL)
1116 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1122 | THROWS_TK class_type_list
1125 {yyerror ("Missing class type term"); RECOVER;}
1130 { $$ = build_tree_list ($1, $1); }
1131 | class_type_list C_TK class_type
1132 { $$ = tree_cons ($3, $3, $1); }
1133 | class_type_list C_TK error
1134 {yyerror ("Missing class type term"); RECOVER;}
1139 | SC_TK { $$ = NULL_TREE; }
1142 /* 19.8.4 Productions from 8.5: Static Initializers */
1146 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1147 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1148 current_static_block = NULL_TREE;
1152 static: /* Test lval.sub_token here */
1155 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1156 /* Can't have a static initializer in an innerclass */
1157 if ($1 | ACC_STATIC &&
1158 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1160 (MODIFIER_WFL (STATIC_TK),
1161 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1162 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1163 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1167 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1168 constructor_declaration:
1171 current_function_decl = $1;
1172 source_start_java_method (current_function_decl);
1175 { finish_method_declaration ($3); }
1179 constructor_declarator throws
1180 { $$ = method_header (0, NULL_TREE, $1, $2); }
1181 | modifiers constructor_declarator throws
1182 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1185 constructor_declarator:
1186 simple_name OP_TK CP_TK
1188 ctxp->formal_parameter_number = 0;
1189 $$ = method_declarator ($1, NULL_TREE);
1191 | simple_name OP_TK formal_parameter_list CP_TK
1192 { $$ = method_declarator ($1, $3); }
1196 /* Unlike regular method, we always need a complete (empty)
1197 body so we can safely perform all the required code
1198 addition (super invocation and field initialization) */
1199 block_begin constructor_block_end
1201 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1204 | block_begin explicit_constructor_invocation constructor_block_end
1206 | block_begin block_statements constructor_block_end
1208 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1212 constructor_block_end:
1216 /* Error recovery for that rule moved down expression_statement: rule. */
1217 explicit_constructor_invocation:
1218 this_or_super OP_TK CP_TK SC_TK
1220 $$ = build_method_invocation ($1, NULL_TREE);
1221 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1222 $$ = java_method_add_stmt (current_function_decl, $$);
1224 | this_or_super OP_TK argument_list CP_TK SC_TK
1226 $$ = build_method_invocation ($1, $3);
1227 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1228 $$ = java_method_add_stmt (current_function_decl, $$);
1230 /* Added, JDK1.1 inner classes. Modified because the rule
1231 'primary' couldn't work. */
1232 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1233 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1234 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1235 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1238 this_or_super: /* Added, simplifies error diagnostics */
1241 tree wfl = build_wfl_node (this_identifier_node);
1242 EXPR_WFL_LINECOL (wfl) = $1.location;
1247 tree wfl = build_wfl_node (super_identifier_node);
1248 EXPR_WFL_LINECOL (wfl) = $1.location;
1253 /* 19.9 Productions from 9: Interfaces */
1254 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1255 interface_declaration:
1256 INTERFACE_TK identifier
1257 { create_interface (0, $2, NULL_TREE); }
1259 | modifiers INTERFACE_TK identifier
1260 { create_interface ($1, $3, NULL_TREE); }
1262 | INTERFACE_TK identifier extends_interfaces
1263 { create_interface (0, $2, $3); }
1265 | modifiers INTERFACE_TK identifier extends_interfaces
1266 { create_interface ($1, $3, $4); }
1268 | INTERFACE_TK identifier error
1269 {yyerror ("'{' expected"); RECOVER;}
1270 | modifiers INTERFACE_TK identifier error
1271 {yyerror ("'{' expected"); RECOVER;}
1275 EXTENDS_TK interface_type
1277 ctxp->interface_number = 1;
1278 $$ = build_tree_list ($2, NULL_TREE);
1280 | extends_interfaces C_TK interface_type
1282 ctxp->interface_number++;
1283 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1286 {yyerror ("Invalid interface type"); RECOVER;}
1287 | extends_interfaces C_TK error
1288 {yyerror ("Missing term"); RECOVER;}
1294 | OCB_TK interface_member_declarations CCB_TK
1298 interface_member_declarations:
1299 interface_member_declaration
1300 | interface_member_declarations interface_member_declaration
1303 interface_member_declaration:
1304 constant_declaration
1305 | abstract_method_declaration
1306 | class_declaration /* Added, JDK1.1 inner classes */
1307 { end_class_declaration (1); }
1308 | interface_declaration /* Added, JDK1.1 inner interfaces */
1309 { end_class_declaration (1); }
1312 constant_declaration:
1316 abstract_method_declaration:
1319 check_abstract_method_header ($1);
1320 current_function_decl = NULL_TREE; /* FIXME ? */
1322 | method_header error
1323 {yyerror ("';' expected"); RECOVER;}
1326 /* 19.10 Productions from 10: Arrays */
1329 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1330 | OCB_TK variable_initializers CCB_TK
1331 { $$ = build_new_array_init ($1.location, $2); }
1332 | OCB_TK variable_initializers C_TK CCB_TK
1333 { $$ = build_new_array_init ($1.location, $2); }
1336 variable_initializers:
1337 variable_initializer
1339 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1342 | variable_initializers C_TK variable_initializer
1344 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1346 | variable_initializers C_TK error
1347 {yyerror ("Missing term"); RECOVER;}
1350 /* 19.11 Production from 14: Blocks and Statements */
1354 /* Store the location of the `}' when doing xrefs */
1355 if (current_function_decl && flag_emit_xref)
1356 DECL_END_SOURCE_LINE (current_function_decl) =
1357 EXPR_WFL_ADD_COL ($2.location, 1);
1358 $$ = empty_stmt_node;
1360 | block_begin block_statements block_end
1372 maybe_absorb_scoping_blocks ();
1373 /* Store the location of the `}' when doing xrefs */
1374 if (current_function_decl && flag_emit_xref)
1375 DECL_END_SOURCE_LINE (current_function_decl) =
1376 EXPR_WFL_ADD_COL ($1.location, 1);
1378 if (!BLOCK_SUBBLOCKS ($$))
1379 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1385 | block_statements block_statement
1389 local_variable_declaration_statement
1391 { java_method_add_stmt (current_function_decl, $1); }
1392 | class_declaration /* Added, JDK1.1 local classes */
1394 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1395 end_class_declaration (1);
1399 local_variable_declaration_statement:
1400 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1403 local_variable_declaration:
1404 type variable_declarators
1405 { declare_local_variables (0, $1, $2); }
1406 | final type variable_declarators /* Added, JDK1.1 final locals */
1407 { declare_local_variables ($1, $2, $3); }
1411 statement_without_trailing_substatement
1414 | if_then_else_statement
1417 { $$ = exit_block (); }
1421 statement_without_trailing_substatement
1422 | labeled_statement_nsi
1423 | if_then_else_statement_nsi
1424 | while_statement_nsi
1426 { $$ = exit_block (); }
1429 statement_without_trailing_substatement:
1432 | expression_statement
1436 | continue_statement
1438 | synchronized_statement
1446 if (flag_extraneous_semicolon)
1448 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1449 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1451 $$ = empty_stmt_node;
1456 identifier REL_CL_TK
1458 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1459 EXPR_WFL_NODE ($1));
1461 push_labeled_block ($$);
1462 PUSH_LABELED_BLOCK ($$);
1467 label_decl statement
1468 { $$ = finish_labeled_statement ($1, $2); }
1470 {yyerror ("':' expected"); RECOVER;}
1473 labeled_statement_nsi:
1474 label_decl statement_nsi
1475 { $$ = finish_labeled_statement ($1, $2); }
1478 /* We concentrate here a bunch of error handling rules that we couldn't write
1479 earlier, because expression_statement catches a missing ';'. */
1480 expression_statement:
1481 statement_expression SC_TK
1483 /* We have a statement. Generate a WFL around it so
1485 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1486 /* We know we have a statement, so set the debug
1487 info to be eventually generate here. */
1488 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1492 YYNOT_TWICE yyerror ("Invalid expression statement");
1493 DRECOVER (expr_stmt);
1497 YYNOT_TWICE yyerror ("Invalid expression statement");
1498 DRECOVER (expr_stmt);
1502 YYNOT_TWICE yyerror ("Invalid expression statement");
1503 DRECOVER (expr_stmt);
1505 | this_or_super OP_TK error
1506 {yyerror ("')' expected"); RECOVER;}
1507 | this_or_super OP_TK CP_TK error
1509 parse_ctor_invocation_error ();
1512 | this_or_super OP_TK argument_list error
1513 {yyerror ("')' expected"); RECOVER;}
1514 | this_or_super OP_TK argument_list CP_TK error
1516 parse_ctor_invocation_error ();
1519 | name DOT_TK SUPER_TK error
1520 {yyerror ("'(' expected"); RECOVER;}
1521 | name DOT_TK SUPER_TK OP_TK error
1522 {yyerror ("')' expected"); RECOVER;}
1523 | name DOT_TK SUPER_TK OP_TK argument_list error
1524 {yyerror ("')' expected"); RECOVER;}
1525 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1526 {yyerror ("';' expected"); RECOVER;}
1527 | name DOT_TK SUPER_TK OP_TK CP_TK error
1528 {yyerror ("';' expected"); RECOVER;}
1531 statement_expression:
1533 | pre_increment_expression
1534 | pre_decrement_expression
1535 | post_increment_expression
1536 | post_decrement_expression
1538 | class_instance_creation_expression
1542 IF_TK OP_TK expression CP_TK statement
1544 $$ = build_if_else_statement ($2.location, $3,
1548 {yyerror ("'(' expected"); RECOVER;}
1550 {yyerror ("Missing term"); RECOVER;}
1551 | IF_TK OP_TK expression error
1552 {yyerror ("')' expected"); RECOVER;}
1555 if_then_else_statement:
1556 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1557 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1560 if_then_else_statement_nsi:
1561 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1562 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1572 /* Make into "proper list" of COMPOUND_EXPRs.
1573 I.e. make the last statment also have its own
1575 maybe_absorb_scoping_blocks ();
1576 TREE_OPERAND ($1, 1) = exit_block ();
1577 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1582 SWITCH_TK OP_TK expression CP_TK
1584 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1585 EXPR_WFL_LINECOL ($$) = $2.location;
1588 {yyerror ("'(' expected"); RECOVER;}
1589 | SWITCH_TK OP_TK error
1590 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1591 | SWITCH_TK OP_TK expression CP_TK error
1592 {yyerror ("'{' expected"); RECOVER;}
1595 /* Default assignment is there to avoid type node on switch_block
1601 | OCB_TK switch_labels CCB_TK
1603 | OCB_TK switch_block_statement_groups CCB_TK
1605 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1609 switch_block_statement_groups:
1610 switch_block_statement_group
1611 | switch_block_statement_groups switch_block_statement_group
1614 switch_block_statement_group:
1615 switch_labels block_statements
1620 | switch_labels switch_label
1624 CASE_TK constant_expression REL_CL_TK
1626 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1627 EXPR_WFL_LINECOL (lab) = $1.location;
1628 java_method_add_stmt (current_function_decl, lab);
1630 | DEFAULT_TK REL_CL_TK
1632 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1633 EXPR_WFL_LINECOL (lab) = $1.location;
1634 java_method_add_stmt (current_function_decl, lab);
1637 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1638 | CASE_TK constant_expression error
1639 {yyerror ("':' expected"); RECOVER;}
1641 {yyerror ("':' expected"); RECOVER;}
1645 WHILE_TK OP_TK expression CP_TK
1647 tree body = build_loop_body ($2.location, $3, 0);
1648 $$ = build_new_loop (body);
1653 while_expression statement
1654 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1656 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1657 | WHILE_TK OP_TK error
1658 {yyerror ("Missing term and ')' expected"); RECOVER;}
1659 | WHILE_TK OP_TK expression error
1660 {yyerror ("')' expected"); RECOVER;}
1663 while_statement_nsi:
1664 while_expression statement_nsi
1665 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1671 tree body = build_loop_body (0, NULL_TREE, 1);
1672 $$ = build_new_loop (body);
1674 /* Need error handing here. FIXME */
1678 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1679 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1683 for_begin SC_TK expression SC_TK for_update CP_TK statement
1685 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1686 $3 = build_wfl_node ($3);
1687 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1689 | for_begin SC_TK SC_TK for_update CP_TK statement
1691 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1692 /* We have not condition, so we get rid of the EXIT_EXPR */
1693 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1696 | for_begin SC_TK error
1697 {yyerror ("Invalid control expression"); RECOVER;}
1698 | for_begin SC_TK expression SC_TK error
1699 {yyerror ("Invalid update expression"); RECOVER;}
1700 | for_begin SC_TK SC_TK error
1701 {yyerror ("Invalid update expression"); RECOVER;}
1705 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1706 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1707 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1709 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1710 /* We have not condition, so we get rid of the EXIT_EXPR */
1711 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1719 /* This scope defined for local variable that may be
1720 defined within the scope of the for loop */
1724 {yyerror ("'(' expected"); DRECOVER(for_1);}
1725 | FOR_TK OP_TK error
1726 {yyerror ("Invalid init statement"); RECOVER;}
1732 /* We now declare the loop body. The loop is
1733 declared as a for loop. */
1734 tree body = build_loop_body (0, NULL_TREE, 0);
1735 $$ = build_new_loop (body);
1736 FOR_LOOP_P ($$) = 1;
1737 /* The loop is added to the current block the for
1738 statement is defined within */
1739 java_method_add_stmt (current_function_decl, $$);
1742 for_init: /* Can be empty */
1743 { $$ = empty_stmt_node; }
1744 | statement_expression_list
1746 /* Init statement recorded within the previously
1747 defined block scope */
1748 $$ = java_method_add_stmt (current_function_decl, $1);
1750 | local_variable_declaration
1752 /* Local variable are recorded within the previously
1753 defined block scope */
1756 | statement_expression_list error
1757 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1760 for_update: /* Can be empty */
1761 {$$ = empty_stmt_node;}
1762 | statement_expression_list
1763 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1766 statement_expression_list:
1767 statement_expression
1768 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1769 | statement_expression_list C_TK statement_expression
1770 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1771 | statement_expression_list C_TK error
1772 {yyerror ("Missing term"); RECOVER;}
1777 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1778 | BREAK_TK identifier SC_TK
1779 { $$ = build_bc_statement ($1.location, 1, $2); }
1781 {yyerror ("Missing term"); RECOVER;}
1782 | BREAK_TK identifier error
1783 {yyerror ("';' expected"); RECOVER;}
1788 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1789 | CONTINUE_TK identifier SC_TK
1790 { $$ = build_bc_statement ($1.location, 0, $2); }
1792 {yyerror ("Missing term"); RECOVER;}
1793 | CONTINUE_TK identifier error
1794 {yyerror ("';' expected"); RECOVER;}
1799 { $$ = build_return ($1.location, NULL_TREE); }
1800 | RETURN_TK expression SC_TK
1801 { $$ = build_return ($1.location, $2); }
1803 {yyerror ("Missing term"); RECOVER;}
1804 | RETURN_TK expression error
1805 {yyerror ("';' expected"); RECOVER;}
1809 THROW_TK expression SC_TK
1811 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1812 EXPR_WFL_LINECOL ($$) = $1.location;
1815 {yyerror ("Missing term"); RECOVER;}
1816 | THROW_TK expression error
1817 {yyerror ("';' expected"); RECOVER;}
1820 synchronized_statement:
1821 synchronized OP_TK expression CP_TK block
1823 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1824 EXPR_WFL_LINECOL ($$) =
1825 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1827 | synchronized OP_TK expression CP_TK error
1828 {yyerror ("'{' expected"); RECOVER;}
1829 | synchronized error
1830 {yyerror ("'(' expected"); RECOVER;}
1831 | synchronized OP_TK error CP_TK
1832 {yyerror ("Missing term"); RECOVER;}
1833 | synchronized OP_TK error
1834 {yyerror ("Missing term"); RECOVER;}
1841 "Illegal modifier `%s'. Only `synchronized' was expected here",
1842 $1, ACC_SYNCHRONIZED);
1843 if ($1 != ACC_SYNCHRONIZED)
1844 MODIFIER_WFL (SYNCHRONIZED_TK) =
1845 build_wfl_node (NULL_TREE);
1850 TRY_TK block catches
1851 { $$ = build_try_statement ($1.location, $2, $3); }
1852 | TRY_TK block finally
1853 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1854 | TRY_TK block catches finally
1855 { $$ = build_try_finally_statement
1856 ($1.location, build_try_statement ($1.location,
1860 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1865 | catches catch_clause
1867 TREE_CHAIN ($2) = $1;
1873 catch_clause_parameter block
1875 java_method_add_stmt (current_function_decl, $2);
1880 catch_clause_parameter:
1881 CATCH_TK OP_TK formal_parameter CP_TK
1883 /* We add a block to define a scope for
1884 formal_parameter (CCBP). The formal parameter is
1885 declared initialized by the appropriate function
1887 tree ccpb = enter_block ();
1888 tree init = build_assignment (ASSIGN_TK, $2.location,
1890 soft_exceptioninfo_call_node);
1891 declare_local_variables (0, TREE_VALUE ($3),
1892 build_tree_list (TREE_PURPOSE ($3),
1894 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1895 EXPR_WFL_LINECOL ($$) = $1.location;
1898 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1899 | CATCH_TK OP_TK error
1901 yyerror ("Missing term or ')' expected");
1902 RECOVER; $$ = NULL_TREE;
1904 | CATCH_TK OP_TK error CP_TK /* That's for () */
1905 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1912 {yyerror ("'{' expected"); RECOVER; }
1915 /* 19.12 Production from 15: Expressions */
1917 primary_no_new_array
1918 | array_creation_expression
1921 primary_no_new_array:
1924 { $$ = build_this ($1.location); }
1925 | OP_TK expression CP_TK
1927 | class_instance_creation_expression
1932 /* Added, JDK1.1 inner classes. Documentation is wrong
1933 refering to a 'ClassName' (class_name) rule that doesn't
1934 exist. Used name: instead. */
1935 | name DOT_TK THIS_TK
1937 tree wfl = build_wfl_node (this_identifier_node);
1938 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1940 | OP_TK expression error
1941 {yyerror ("')' expected"); RECOVER;}
1943 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1944 | primitive_type DOT_TK error
1945 {yyerror ("'class' expected" ); RECOVER;}
1946 | VOID_TK DOT_TK error
1947 {yyerror ("'class' expected" ); RECOVER;}
1951 name DOT_TK CLASS_TK
1952 { $$ = build_incomplete_class_ref ($2.location, $1); }
1953 | array_type DOT_TK CLASS_TK
1954 { $$ = build_incomplete_class_ref ($2.location, $1); }
1955 | primitive_type DOT_TK CLASS_TK
1956 { $$ = build_class_ref ($1); }
1957 | VOID_TK DOT_TK CLASS_TK
1958 { $$ = build_class_ref (void_type_node); }
1961 class_instance_creation_expression:
1962 NEW_TK class_type OP_TK argument_list CP_TK
1963 { $$ = build_new_invocation ($2, $4); }
1964 | NEW_TK class_type OP_TK CP_TK
1965 { $$ = build_new_invocation ($2, NULL_TREE); }
1966 | anonymous_class_creation
1967 /* Added, JDK1.1 inner classes, modified to use name or
1968 primary instead of primary solely which couldn't work in
1970 | something_dot_new identifier OP_TK CP_TK
1972 tree ctor = build_new_invocation ($2, NULL_TREE);
1973 $$ = make_qualified_primary ($1, ctor,
1974 EXPR_WFL_LINECOL ($1));
1976 | something_dot_new identifier OP_TK CP_TK class_body
1977 | something_dot_new identifier OP_TK argument_list CP_TK
1979 tree ctor = build_new_invocation ($2, $4);
1980 $$ = make_qualified_primary ($1, ctor,
1981 EXPR_WFL_LINECOL ($1));
1983 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1984 | NEW_TK error SC_TK
1985 {yyerror ("'(' expected"); DRECOVER(new_1);}
1986 | NEW_TK class_type error
1987 {yyerror ("'(' expected"); RECOVER;}
1988 | NEW_TK class_type OP_TK error
1989 {yyerror ("')' or term expected"); RECOVER;}
1990 | NEW_TK class_type OP_TK argument_list error
1991 {yyerror ("')' expected"); RECOVER;}
1992 | something_dot_new error
1993 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1994 | something_dot_new identifier error
1995 {yyerror ("'(' expected"); RECOVER;}
1998 /* Created after JDK1.1 rules originally added to
1999 class_instance_creation_expression, but modified to use
2000 'class_type' instead of 'TypeName' (type_name) which is mentionned
2001 in the documentation but doesn't exist. */
2003 anonymous_class_creation:
2004 NEW_TK class_type OP_TK argument_list CP_TK
2005 { create_anonymous_class ($1.location, $2); }
2008 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2009 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2011 end_class_declaration (1);
2013 /* Now we can craft the new expression */
2014 $$ = build_new_invocation (id, $4);
2016 /* Note that we can't possibly be here if
2017 `class_type' is an interface (in which case the
2018 anonymous class extends Object and implements
2019 `class_type', hence its constructor can't have
2022 /* Otherwise, the innerclass must feature a
2023 constructor matching `argument_list'. Anonymous
2024 classes are a bit special: it's impossible to
2025 define constructor for them, hence constructors
2026 must be generated following the hints provided by
2027 the `new' expression. Whether a super constructor
2028 of that nature exists or not is to be verified
2029 later on in verify_constructor_super.
2031 It's during the expansion of a `new' statement
2032 refering to an anonymous class that a ctor will
2033 be generated for the anonymous class, with the
2037 | NEW_TK class_type OP_TK CP_TK
2038 { create_anonymous_class ($1.location, $2); }
2041 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2042 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2044 end_class_declaration (1);
2046 /* Now we can craft the new expression. The
2047 statement doesn't need to be remember so that a
2048 constructor can be generated, since its signature
2049 is already known. */
2050 $$ = build_new_invocation (id, NULL_TREE);
2054 something_dot_new: /* Added, not part of the specs. */
2057 | primary DOT_TK NEW_TK
2064 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2065 ctxp->formal_parameter_number = 1;
2067 | argument_list C_TK expression
2069 ctxp->formal_parameter_number += 1;
2070 $$ = tree_cons (NULL_TREE, $3, $1);
2072 | argument_list C_TK error
2073 {yyerror ("Missing term"); RECOVER;}
2076 array_creation_expression:
2077 NEW_TK primitive_type dim_exprs
2078 { $$ = build_newarray_node ($2, $3, 0); }
2079 | NEW_TK class_or_interface_type dim_exprs
2080 { $$ = build_newarray_node ($2, $3, 0); }
2081 | NEW_TK primitive_type dim_exprs dims
2082 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2083 | NEW_TK class_or_interface_type dim_exprs dims
2084 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2085 /* Added, JDK1.1 anonymous array. Initial documentation rule
2087 | NEW_TK class_or_interface_type dims array_initializer
2090 int osb = pop_current_osb (ctxp);
2092 obstack_1grow (&temporary_obstack, '[');
2093 sig = obstack_finish (&temporary_obstack);
2094 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2095 $2, get_identifier (sig), $4);
2097 | NEW_TK primitive_type dims array_initializer
2099 int osb = pop_current_osb (ctxp);
2102 type = build_java_array_type (type, -1);
2103 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2104 build_pointer_type (type), NULL_TREE, $4);
2106 | NEW_TK error CSB_TK
2107 {yyerror ("'[' expected"); DRECOVER ("]");}
2108 | NEW_TK error OSB_TK
2109 {yyerror ("']' expected"); RECOVER;}
2114 { $$ = build_tree_list (NULL_TREE, $1); }
2115 | dim_exprs dim_expr
2116 { $$ = tree_cons (NULL_TREE, $2, $$); }
2120 OSB_TK expression CSB_TK
2122 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2124 $2 = build_wfl_node ($2);
2125 TREE_TYPE ($2) = NULL_TREE;
2127 EXPR_WFL_LINECOL ($2) = $1.location;
2130 | OSB_TK expression error
2131 {yyerror ("']' expected"); RECOVER;}
2134 yyerror ("Missing term");
2135 yyerror ("']' expected");
2144 /* If not initialized, allocate memory for the osb
2146 if (!ctxp->osb_limit)
2148 allocate = ctxp->osb_limit = 32;
2149 ctxp->osb_depth = -1;
2151 /* If capacity overflown, reallocate a bigger chunk */
2152 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2153 allocate = ctxp->osb_limit << 1;
2157 allocate *= sizeof (int);
2158 if (ctxp->osb_number)
2159 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2162 ctxp->osb_number = (int *)xmalloc (allocate);
2165 CURRENT_OSB (ctxp) = 1;
2167 | dims OSB_TK CSB_TK
2168 { CURRENT_OSB (ctxp)++; }
2170 { yyerror ("']' expected"); RECOVER;}
2174 primary DOT_TK identifier
2175 { $$ = make_qualified_primary ($1, $3, $2.location); }
2176 /* FIXME - REWRITE TO:
2177 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2178 | SUPER_TK DOT_TK identifier
2180 tree super_wfl = build_wfl_node (super_identifier_node);
2181 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2182 $$ = make_qualified_name (super_wfl, $3, $2.location);
2185 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2190 { $$ = build_method_invocation ($1, NULL_TREE); }
2191 | name OP_TK argument_list CP_TK
2192 { $$ = build_method_invocation ($1, $3); }
2193 | primary DOT_TK identifier OP_TK CP_TK
2195 if (TREE_CODE ($1) == THIS_EXPR)
2196 $$ = build_this_super_qualified_invocation
2197 (1, $3, NULL_TREE, 0, $2.location);
2200 tree invok = build_method_invocation ($3, NULL_TREE);
2201 $$ = make_qualified_primary ($1, invok, $2.location);
2204 | primary DOT_TK identifier OP_TK argument_list CP_TK
2206 if (TREE_CODE ($1) == THIS_EXPR)
2207 $$ = build_this_super_qualified_invocation
2208 (1, $3, $5, 0, $2.location);
2211 tree invok = build_method_invocation ($3, $5);
2212 $$ = make_qualified_primary ($1, invok, $2.location);
2215 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2217 $$ = build_this_super_qualified_invocation
2218 (0, $3, NULL_TREE, $1.location, $2.location);
2220 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2222 $$ = build_this_super_qualified_invocation
2223 (0, $3, $5, $1.location, $2.location);
2225 /* Screws up thing. I let it here until I'm convinced it can
2227 | primary DOT_TK error
2228 {yyerror ("'(' expected"); DRECOVER(bad);} */
2229 | SUPER_TK DOT_TK error CP_TK
2230 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2231 | SUPER_TK DOT_TK error DOT_TK
2232 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2236 name OSB_TK expression CSB_TK
2237 { $$ = build_array_ref ($2.location, $1, $3); }
2238 | primary_no_new_array OSB_TK expression CSB_TK
2239 { $$ = build_array_ref ($2.location, $1, $3); }
2242 yyerror ("Missing term and ']' expected");
2243 DRECOVER(array_access);
2245 | name OSB_TK expression error
2247 yyerror ("']' expected");
2248 DRECOVER(array_access);
2250 | primary_no_new_array OSB_TK error
2252 yyerror ("Missing term and ']' expected");
2253 DRECOVER(array_access);
2255 | primary_no_new_array OSB_TK expression error
2257 yyerror ("']' expected");
2258 DRECOVER(array_access);
2265 | post_increment_expression
2266 | post_decrement_expression
2269 post_increment_expression:
2270 postfix_expression INCR_TK
2271 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2274 post_decrement_expression:
2275 postfix_expression DECR_TK
2276 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2280 pre_increment_expression
2281 | pre_decrement_expression
2282 | PLUS_TK unary_expression
2283 {$$ = build_unaryop ($1.token, $1.location, $2); }
2284 | MINUS_TK unary_expression
2285 {$$ = build_unaryop ($1.token, $1.location, $2); }
2286 | unary_expression_not_plus_minus
2288 {yyerror ("Missing term"); RECOVER}
2290 {yyerror ("Missing term"); RECOVER}
2293 pre_increment_expression:
2294 INCR_TK unary_expression
2295 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2297 {yyerror ("Missing term"); RECOVER}
2300 pre_decrement_expression:
2301 DECR_TK unary_expression
2302 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2304 {yyerror ("Missing term"); RECOVER}
2307 unary_expression_not_plus_minus:
2309 | NOT_TK unary_expression
2310 {$$ = build_unaryop ($1.token, $1.location, $2); }
2311 | NEG_TK unary_expression
2312 {$$ = build_unaryop ($1.token, $1.location, $2); }
2315 {yyerror ("Missing term"); RECOVER}
2317 {yyerror ("Missing term"); RECOVER}
2320 cast_expression: /* Error handling here is potentially weak */
2321 OP_TK primitive_type dims CP_TK unary_expression
2324 int osb = pop_current_osb (ctxp);
2326 type = build_java_array_type (type, -1);
2327 $$ = build_cast ($1.location, type, $5);
2329 | OP_TK primitive_type CP_TK unary_expression
2330 { $$ = build_cast ($1.location, $2, $4); }
2331 | OP_TK expression CP_TK unary_expression_not_plus_minus
2332 { $$ = build_cast ($1.location, $2, $4); }
2333 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2336 int osb = pop_current_osb (ctxp);
2338 obstack_1grow (&temporary_obstack, '[');
2339 obstack_grow0 (&temporary_obstack,
2340 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2341 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2342 ptr = obstack_finish (&temporary_obstack);
2343 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2344 $$ = build_cast ($1.location, $2, $5);
2346 | OP_TK primitive_type OSB_TK error
2347 {yyerror ("']' expected, invalid type expression");}
2350 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2353 | OP_TK primitive_type dims CP_TK error
2354 {yyerror ("Missing term"); RECOVER;}
2355 | OP_TK primitive_type CP_TK error
2356 {yyerror ("Missing term"); RECOVER;}
2357 | OP_TK name dims CP_TK error
2358 {yyerror ("Missing term"); RECOVER;}
2361 multiplicative_expression:
2363 | multiplicative_expression MULT_TK unary_expression
2365 $$ = build_binop (BINOP_LOOKUP ($2.token),
2366 $2.location, $1, $3);
2368 | multiplicative_expression DIV_TK unary_expression
2370 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2373 | multiplicative_expression REM_TK unary_expression
2375 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2378 | multiplicative_expression MULT_TK error
2379 {yyerror ("Missing term"); RECOVER;}
2380 | multiplicative_expression DIV_TK error
2381 {yyerror ("Missing term"); RECOVER;}
2382 | multiplicative_expression REM_TK error
2383 {yyerror ("Missing term"); RECOVER;}
2386 additive_expression:
2387 multiplicative_expression
2388 | additive_expression PLUS_TK multiplicative_expression
2390 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2393 | additive_expression MINUS_TK multiplicative_expression
2395 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2398 | additive_expression PLUS_TK error
2399 {yyerror ("Missing term"); RECOVER;}
2400 | additive_expression MINUS_TK error
2401 {yyerror ("Missing term"); RECOVER;}
2406 | shift_expression LS_TK additive_expression
2408 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2411 | shift_expression SRS_TK additive_expression
2413 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2416 | shift_expression ZRS_TK additive_expression
2418 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2421 | shift_expression LS_TK error
2422 {yyerror ("Missing term"); RECOVER;}
2423 | shift_expression SRS_TK error
2424 {yyerror ("Missing term"); RECOVER;}
2425 | shift_expression ZRS_TK error
2426 {yyerror ("Missing term"); RECOVER;}
2429 relational_expression:
2431 | relational_expression LT_TK shift_expression
2433 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2436 | relational_expression GT_TK shift_expression
2438 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2441 | relational_expression LTE_TK shift_expression
2443 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2446 | relational_expression GTE_TK shift_expression
2448 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2451 | relational_expression INSTANCEOF_TK reference_type
2452 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2453 | relational_expression LT_TK error
2454 {yyerror ("Missing term"); RECOVER;}
2455 | relational_expression GT_TK error
2456 {yyerror ("Missing term"); RECOVER;}
2457 | relational_expression LTE_TK error
2458 {yyerror ("Missing term"); RECOVER;}
2459 | relational_expression GTE_TK error
2460 {yyerror ("Missing term"); RECOVER;}
2461 | relational_expression INSTANCEOF_TK error
2462 {yyerror ("Invalid reference type"); RECOVER;}
2465 equality_expression:
2466 relational_expression
2467 | equality_expression EQ_TK relational_expression
2469 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2472 | equality_expression NEQ_TK relational_expression
2474 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2477 | equality_expression EQ_TK error
2478 {yyerror ("Missing term"); RECOVER;}
2479 | equality_expression NEQ_TK error
2480 {yyerror ("Missing term"); RECOVER;}
2485 | and_expression AND_TK equality_expression
2487 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2490 | and_expression AND_TK error
2491 {yyerror ("Missing term"); RECOVER;}
2494 exclusive_or_expression:
2496 | exclusive_or_expression XOR_TK and_expression
2498 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2501 | exclusive_or_expression XOR_TK error
2502 {yyerror ("Missing term"); RECOVER;}
2505 inclusive_or_expression:
2506 exclusive_or_expression
2507 | inclusive_or_expression OR_TK exclusive_or_expression
2509 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2512 | inclusive_or_expression OR_TK error
2513 {yyerror ("Missing term"); RECOVER;}
2516 conditional_and_expression:
2517 inclusive_or_expression
2518 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2520 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2523 | conditional_and_expression BOOL_AND_TK error
2524 {yyerror ("Missing term"); RECOVER;}
2527 conditional_or_expression:
2528 conditional_and_expression
2529 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2531 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2534 | conditional_or_expression BOOL_OR_TK error
2535 {yyerror ("Missing term"); RECOVER;}
2538 conditional_expression: /* Error handling here is weak */
2539 conditional_or_expression
2540 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2542 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2543 EXPR_WFL_LINECOL ($$) = $2.location;
2545 | conditional_or_expression REL_QM_TK REL_CL_TK error
2548 yyerror ("Missing term");
2551 | conditional_or_expression REL_QM_TK error
2552 {yyerror ("Missing term"); DRECOVER (2);}
2553 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2554 {yyerror ("Missing term"); DRECOVER (3);}
2557 assignment_expression:
2558 conditional_expression
2563 left_hand_side assignment_operator assignment_expression
2564 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2565 | left_hand_side assignment_operator error
2567 YYNOT_TWICE yyerror ("Missing term");
2578 assignment_operator:
2584 assignment_expression
2587 constant_expression:
2593 /* Helper function to retrieve an OSB count. Should be used when the
2594 `dims:' rule is being used. */
2597 pop_current_osb (ctxp)
2598 struct parser_ctxt *ctxp;
2602 if (ctxp->osb_depth < 0)
2605 to_return = CURRENT_OSB (ctxp);
2613 /* This section of the code deal with save/restoring parser contexts.
2614 Add mode documentation here. FIXME */
2616 /* Helper function. Create a new parser context. With
2617 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2618 context is copied, otherwise, the new context is zeroed. The newly
2619 created context becomes the current one. */
2622 create_new_parser_context (copy_from_previous)
2623 int copy_from_previous;
2625 struct parser_ctxt *new;
2627 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2628 if (copy_from_previous)
2630 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2631 new->saved_data_ctx = 1;
2634 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2640 /* Create a new parser context and make it the current one. */
2643 java_push_parser_context ()
2645 create_new_parser_context (0);
2649 java_pop_parser_context (generate)
2653 struct parser_ctxt *toFree, *next;
2662 lineno = ctxp->lineno;
2663 current_class = ctxp->class_type;
2666 /* If the old and new lexers differ, then free the old one. */
2667 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2668 java_destroy_lexer (ctxp->lexer);
2670 /* Set the single import class file flag to 0 for the current list
2671 of imported things */
2672 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2673 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2675 /* And restore those of the previous context */
2676 if ((ctxp = next)) /* Assignment is really meant here */
2677 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2678 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2680 /* If we pushed a context to parse a class intended to be generated,
2681 we keep it so we can remember the class. What we could actually
2682 do is to just update a list of class names. */
2685 toFree->next = ctxp_for_generation;
2686 ctxp_for_generation = toFree;
2692 /* Create a parser context for the use of saving some global
2696 java_parser_context_save_global ()
2700 java_push_parser_context ();
2701 ctxp->saved_data_ctx = 1;
2704 /* If this context already stores data, create a new one suitable
2705 for data storage. */
2706 else if (ctxp->saved_data)
2707 create_new_parser_context (1);
2709 ctxp->lineno = lineno;
2710 ctxp->class_type = current_class;
2711 ctxp->filename = input_filename;
2712 ctxp->function_decl = current_function_decl;
2713 ctxp->saved_data = 1;
2716 /* Restore some global variables from the previous context. Make the
2717 previous context the current one. */
2720 java_parser_context_restore_global ()
2722 lineno = ctxp->lineno;
2723 current_class = ctxp->class_type;
2724 input_filename = ctxp->filename;
2728 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2729 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2731 current_function_decl = ctxp->function_decl;
2732 ctxp->saved_data = 0;
2733 if (ctxp->saved_data_ctx)
2734 java_pop_parser_context (0);
2737 /* Suspend vital data for the current class/function being parsed so
2738 that an other class can be parsed. Used to let local/anonymous
2739 classes be parsed. */
2742 java_parser_context_suspend ()
2744 /* This makes debugging through java_debug_context easier */
2745 static const char *name = "<inner buffer context>";
2747 /* Duplicate the previous context, use it to save the globals we're
2749 create_new_parser_context (1);
2750 ctxp->function_decl = current_function_decl;
2751 ctxp->class_type = current_class;
2753 /* Then create a new context which inherits all data from the
2754 previous one. This will be the new current context */
2755 create_new_parser_context (1);
2757 /* Help debugging */
2758 ctxp->next->filename = name;
2761 /* Resume vital data for the current class/function being parsed so
2762 that an other class can be parsed. Used to let local/anonymous
2763 classes be parsed. The trick is the data storing file position
2764 informations must be restored to their current value, so parsing
2765 can resume as if no context was ever saved. */
2768 java_parser_context_resume ()
2770 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2771 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2772 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2774 /* We need to inherit the list of classes to complete/generate */
2775 restored->classd_list = old->classd_list;
2776 restored->class_list = old->class_list;
2778 /* Restore the current class and function from the saver */
2779 current_class = saver->class_type;
2780 current_function_decl = saver->function_decl;
2782 /* Retrive the restored context */
2785 /* Re-installed the data for the parsing to carry on */
2786 bcopy (&old->marker_begining, &ctxp->marker_begining,
2787 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2789 /* Buffer context can now be discarded */
2794 /* Add a new anchor node to which all statement(s) initializing static
2795 and non static initialized upon declaration field(s) will be
2799 java_parser_context_push_initialized_field ()
2803 node = build_tree_list (NULL_TREE, NULL_TREE);
2804 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2805 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2807 node = build_tree_list (NULL_TREE, NULL_TREE);
2808 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2809 CPC_INITIALIZER_LIST (ctxp) = node;
2811 node = build_tree_list (NULL_TREE, NULL_TREE);
2812 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2813 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2816 /* Pop the lists of initialized field. If this lists aren't empty,
2817 remember them so we can use it to create and populate the finit$
2818 or <clinit> functions. */
2821 java_parser_context_pop_initialized_field ()
2824 tree class_type = TREE_TYPE (GET_CPC ());
2826 if (CPC_INITIALIZER_LIST (ctxp))
2828 stmts = CPC_INITIALIZER_STMT (ctxp);
2829 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2830 if (stmts && !java_error_count)
2831 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2834 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2836 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2837 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2838 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2839 /* Keep initialization in order to enforce 8.5 */
2840 if (stmts && !java_error_count)
2841 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2844 /* JDK 1.1 instance initializers */
2845 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2847 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2848 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2849 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2850 if (stmts && !java_error_count)
2851 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2856 reorder_static_initialized (list)
2859 /* We have to keep things in order. The alias initializer have to
2860 come first, then the initialized regular field, in reverse to
2861 keep them in lexical order. */
2862 tree marker, previous = NULL_TREE;
2863 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2864 if (TREE_CODE (marker) == TREE_LIST
2865 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2868 /* No static initialized, the list is fine as is */
2870 list = TREE_CHAIN (marker);
2872 /* No marker? reverse the whole list */
2874 list = nreverse (list);
2876 /* Otherwise, reverse what's after the marker and the new reordered
2877 sublist will replace the marker. */
2880 TREE_CHAIN (previous) = NULL_TREE;
2881 list = nreverse (list);
2882 list = chainon (TREE_CHAIN (marker), list);
2887 /* Helper functions to dump the parser context stack. */
2889 #define TAB_CONTEXT(C) \
2890 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2893 java_debug_context_do (tab)
2896 struct parser_ctxt *copy = ctxp;
2900 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2902 fprintf (stderr, "filename: %s\n", copy->filename);
2904 fprintf (stderr, "lineno: %d\n", copy->lineno);
2906 fprintf (stderr, "package: %s\n",
2908 IDENTIFIER_POINTER (copy->package) : "<none>"));
2910 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2912 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2918 /* Dump the stacked up parser contexts. Intended to be called from a
2922 java_debug_context ()
2924 java_debug_context_do (0);
2929 /* Flag for the error report routine to issue the error the first time
2930 it's called (overriding the default behavior which is to drop the
2931 first invocation and honor the second one, taking advantage of a
2933 static int force_error = 0;
2935 /* Reporting an constructor invocation error. */
2937 parse_ctor_invocation_error ()
2939 if (DECL_CONSTRUCTOR_P (current_function_decl))
2940 yyerror ("Constructor invocation must be first thing in a constructor");
2942 yyerror ("Only constructors can invoke constructors");
2945 /* Reporting JDK1.1 features not implemented. */
2948 parse_jdk1_1_error (msg)
2951 sorry (": `%s' JDK1.1(TM) feature", msg);
2953 return empty_stmt_node;
2956 static int do_warning = 0;
2963 static int prev_lineno;
2964 static const char *prev_msg;
2967 char *remainder, *code_from_source;
2969 if (!force_error && prev_lineno == lineno)
2972 /* Save current error location but report latter, when the context is
2974 if (ctxp->java_error_flag == 0)
2976 ctxp->java_error_flag = 1;
2978 /* Do something to use the previous line if we're reaching the
2979 end of the file... */
2980 #ifdef VERBOSE_SKELETON
2981 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2986 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2987 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2990 ctxp->java_error_flag = 0;
2992 java_warning_count++;
2996 if (elc.col == 0 && msg && msg[1] == ';')
2998 elc.col = ctxp->p_line->char_col-1;
2999 elc.line = ctxp->p_line->lineno;
3002 save_lineno = lineno;
3003 prev_lineno = lineno = elc.line;
3006 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3007 obstack_grow0 (&temporary_obstack,
3008 code_from_source, strlen (code_from_source));
3009 remainder = obstack_finish (&temporary_obstack);
3011 warning ("%s.\n%s", msg, remainder);
3013 error ("%s.\n%s", msg, remainder);
3015 /* This allow us to cheaply avoid an extra 'Invalid expression
3016 statement' error report when errors have been already reported on
3017 the same line. This occurs when we report an error but don't have
3018 a synchronization point other than ';', which
3019 expression_statement is the only one to take care of. */
3020 ctxp->prevent_ese = lineno = save_lineno;
3024 issue_warning_error_from_context (cl, msg, ap)
3029 const char *saved, *saved_input_filename;
3031 vsprintf (buffer, msg, ap);
3034 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3035 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3036 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3038 /* We have a CL, that's a good reason for using it if it contains data */
3039 saved = ctxp->filename;
3040 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3041 ctxp->filename = EXPR_WFL_FILENAME (cl);
3042 saved_input_filename = input_filename;
3043 input_filename = ctxp->filename;
3045 java_error (buffer);
3046 ctxp->filename = saved;
3047 input_filename = saved_input_filename;
3051 /* Issue an error message at a current source line CL */
3054 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3056 #ifndef ANSI_PROTOTYPES
3063 #ifndef ANSI_PROTOTYPES
3064 cl = va_arg (ap, tree);
3065 msg = va_arg (ap, const char *);
3067 issue_warning_error_from_context (cl, msg, ap);
3071 /* Issue a warning at a current source line CL */
3074 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3076 #ifndef ANSI_PROTOTYPES
3083 #ifndef ANSI_PROTOTYPES
3084 cl = va_arg (ap, tree);
3085 msg = va_arg (ap, const char *);
3088 force_error = do_warning = 1;
3089 issue_warning_error_from_context (cl, msg, ap);
3090 do_warning = force_error = 0;
3095 find_expr_with_wfl (node)
3103 switch (TREE_CODE (node))
3106 node = BLOCK_EXPR_BODY (node);
3110 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3113 node = TREE_OPERAND (node, 1);
3117 node = TREE_OPERAND (node, 0);
3120 case LABELED_BLOCK_EXPR:
3121 node = TREE_OPERAND (node, 1);
3125 code = TREE_CODE_CLASS (TREE_CODE (node));
3126 if (((code == '1') || (code == '2') || (code == 'e'))
3127 && EXPR_WFL_LINECOL (node))
3135 /* Issue a missing return statement error. Uses METHOD to figure the
3136 last line of the method the error occurs in. */
3139 missing_return_error (method)
3142 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3143 parse_error_context (wfl_operator, "Missing return statement");
3146 /* Issue an unreachable statement error. From NODE, find the next
3147 statement to report appropriately. */
3149 unreachable_stmt_error (node)
3152 /* Browse node to find the next expression node that has a WFL. Use
3153 the location to report the error */
3154 if (TREE_CODE (node) == COMPOUND_EXPR)
3155 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3157 node = find_expr_with_wfl (node);
3161 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3162 parse_error_context (wfl_operator, "Unreachable statement");
3169 java_report_errors ()
3171 if (java_error_count)
3172 fprintf (stderr, "%d error%s",
3173 java_error_count, (java_error_count == 1 ? "" : "s"));
3174 if (java_warning_count)
3175 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3176 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3177 if (java_error_count || java_warning_count)
3178 putc ('\n', stderr);
3179 return java_error_count;
3183 java_accstring_lookup (flags)
3186 static char buffer [80];
3187 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3189 /* Access modifier looked-up first for easier report on forbidden
3191 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3192 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3193 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3194 if (flags & ACC_STATIC) COPY_RETURN ("static");
3195 if (flags & ACC_FINAL) COPY_RETURN ("final");
3196 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3197 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3198 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3199 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3200 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3201 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3208 /* Issuing error messages upon redefinition of classes, interfaces or
3212 classitf_redefinition_error (context, id, decl, cl)
3213 const char *context;
3216 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3217 context, IDENTIFIER_POINTER (id),
3218 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3219 /* Here we should point out where its redefined. It's a unicode. FIXME */
3223 variable_redefinition_error (context, name, type, line)
3224 tree context, name, type;
3227 const char *type_name;
3229 /* Figure a proper name for type. We might haven't resolved it */
3230 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3231 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3233 type_name = lang_printable_name (type, 0);
3235 parse_error_context (context,
3236 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3237 IDENTIFIER_POINTER (name),
3238 type_name, IDENTIFIER_POINTER (name), line);
3242 build_array_from_name (type, type_wfl, name, ret_name)
3243 tree type, type_wfl, name, *ret_name;
3248 /* Eventually get more dims */
3249 string = IDENTIFIER_POINTER (name);
3250 while (string [more_dims] == '[')
3253 /* If we have, then craft a new type for this variable */
3256 name = get_identifier (&string [more_dims]);
3258 /* If we have a pointer, use its type */
3259 if (TREE_CODE (type) == POINTER_TYPE)
3260 type = TREE_TYPE (type);
3262 /* Building the first dimension of a primitive type uses this
3264 if (JPRIMITIVE_TYPE_P (type))
3266 type = build_java_array_type (type, -1);
3267 CLASS_LOADED_P (type) = 1;
3270 /* Otherwise, if we have a WFL for this type, use it (the type
3271 is already an array on an unresolved type, and we just keep
3272 on adding dimensions) */
3276 /* Add all the dimensions */
3278 type = build_unresolved_array_type (type);
3280 /* The type may have been incomplete in the first place */
3282 type = obtain_incomplete_type (type);
3290 /* Build something that the type identifier resolver will identify as
3291 being an array to an unresolved type. TYPE_WFL is a WFL on a
3295 build_unresolved_array_type (type_or_wfl)
3300 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3301 just create a array type */
3302 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3304 tree type = build_java_array_type (type_or_wfl, -1);
3305 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3309 obstack_1grow (&temporary_obstack, '[');
3310 obstack_grow0 (&temporary_obstack,
3311 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3312 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3313 ptr = obstack_finish (&temporary_obstack);
3314 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3319 parser_add_interface (class_decl, interface_decl, wfl)
3320 tree class_decl, interface_decl, wfl;
3322 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3323 parse_error_context (wfl, "Interface `%s' repeated",
3324 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3327 /* Bulk of common class/interface checks. Return 1 if an error was
3328 encountered. TAG is 0 for a class, 1 for an interface. */
3331 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3332 int is_interface, flags;
3333 tree raw_name, qualified_name, decl, cl;
3336 int sca = 0; /* Static class allowed */
3337 int icaf = 0; /* Inner class allowed flags */
3338 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3341 fprintf (stderr, " %s%s %s",
3342 (CPC_INNER_P () ? "inner" : ""),
3343 (is_interface ? "interface" : "class"),
3344 IDENTIFIER_POINTER (qualified_name));
3346 /* Scope of an interface/class type name:
3347 - Can't be imported by a single type import
3348 - Can't already exists in the package */
3349 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3350 && (node = find_name_in_single_imports (raw_name))
3354 (cl, "%s name `%s' clashes with imported type `%s'",
3355 (is_interface ? "Interface" : "Class"),
3356 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3359 if (decl && CLASS_COMPLETE_P (decl))
3361 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3362 qualified_name, decl, cl);
3366 if (check_inner_class_redefinition (raw_name, cl))
3369 /* If public, file name should match class/interface name, except
3370 when dealing with an inner class */
3371 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3375 /* Contains OS dependent assumption on path separator. FIXME */
3376 for (f = &input_filename [strlen (input_filename)];
3377 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3380 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3382 if (strncmp (IDENTIFIER_POINTER (raw_name),
3383 f , IDENTIFIER_LENGTH (raw_name)) ||
3384 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3386 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3387 (is_interface ? "interface" : "class"),
3388 IDENTIFIER_POINTER (qualified_name),
3389 IDENTIFIER_POINTER (raw_name));
3392 /* Static classes can be declared only in top level classes. Note:
3393 once static, a inner class is a top level class. */
3394 if (flags & ACC_STATIC)
3396 /* Catch the specific error of declaring an class inner class
3397 with no toplevel enclosing class. Prevent check_modifiers from
3398 complaining a second time */
3399 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3401 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3402 IDENTIFIER_POINTER (qualified_name));
3405 /* Else, in the context of a top-level class declaration, let
3406 `check_modifiers' do its job, otherwise, give it a go */
3408 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3411 /* Inner classes can be declared private or protected
3412 within their enclosing classes. */
3415 /* A class which is local to a block can't be public, private,
3416 protected or static. But it is created final, so allow this
3418 if (current_function_decl)
3419 icaf = sca = uaaf = ACC_FINAL;
3422 check_modifiers_consistency (flags);
3423 icaf = ACC_PRIVATE|ACC_PROTECTED;
3430 uaaf = INTERFACE_INNER_MODIFIERS;
3432 uaaf = INTERFACE_MODIFIERS;
3434 check_modifiers ("Illegal modifier `%s' for interface declaration",
3438 check_modifiers ((current_function_decl ?
3439 "Illegal modifier `%s' for local class declaration" :
3440 "Illegal modifier `%s' for class declaration"),
3441 flags, uaaf|sca|icaf);
3446 make_nested_class_name (cpc_list)
3454 make_nested_class_name (TREE_CHAIN (cpc_list));
3456 /* Pick the qualified name when dealing with the first upmost
3458 name = (TREE_CHAIN (cpc_list) ?
3459 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3460 obstack_grow (&temporary_obstack,
3461 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3462 /* Why is NO_DOLLAR_IN_LABEL defined? */
3464 #ifdef NO_DOLLAR_IN_LABEL
3465 internal_error ("Can't use '$' as a separator for inner classes");
3468 obstack_1grow (&temporary_obstack, '$');
3471 /* Can't redefine a class already defined in an earlier scope. */
3474 check_inner_class_redefinition (raw_name, cl)
3479 for (scope_list = GET_CPC_LIST (); scope_list;
3480 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3481 if (raw_name == GET_CPC_UN_NODE (scope_list))
3484 (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",
3485 IDENTIFIER_POINTER (raw_name));
3492 find_as_inner_class (enclosing, name, cl)
3493 tree enclosing, name, cl;
3495 tree qual, to_return;
3499 name = TYPE_NAME (name);
3501 /* First search: within the scope of `enclosing', search for name */
3502 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3503 qual = EXPR_WFL_QUALIFICATION (cl);
3505 qual = build_tree_list (cl, NULL_TREE);
3507 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3509 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3512 /* We're dealing with a qualified name. Try to resolve thing until
3513 we get something that is an enclosing class. */
3514 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3516 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3518 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3519 qual = TREE_CHAIN (qual))
3521 acc = merge_qualified_name (acc,
3522 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3523 BUILD_PTR_FROM_NAME (ptr, acc);
3524 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3527 /* A NULL qual and a decl means that the search ended
3528 successfully?!? We have to do something then. FIXME */
3533 qual = EXPR_WFL_QUALIFICATION (cl);
3535 /* Otherwise, create a qual for the other part of the resolution. */
3537 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3539 return find_as_inner_class_do (qual, enclosing);
3542 /* We go inside the list of sub classes and try to find a way
3546 find_as_inner_class_do (qual, enclosing)
3547 tree qual, enclosing;
3552 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3554 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3555 tree next_enclosing = NULL_TREE;
3558 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3559 inner_list; inner_list = TREE_CHAIN (inner_list))
3561 if (TREE_VALUE (inner_list) == name_to_match)
3563 next_enclosing = TREE_PURPOSE (inner_list);
3567 enclosing = next_enclosing;
3570 return (!qual && enclosing ? enclosing : NULL_TREE);
3573 /* Reach all inner classes and tie their unqualified name to a
3577 set_nested_class_simple_name_value (outer, set)
3583 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3584 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3585 TREE_PURPOSE (l) : NULL_TREE);
3589 link_nested_class_to_enclosing ()
3591 if (GET_ENCLOSING_CPC ())
3593 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3594 DECL_INNER_CLASS_LIST (enclosing) =
3595 tree_cons (GET_CPC (), GET_CPC_UN (),
3596 DECL_INNER_CLASS_LIST (enclosing));
3597 enclosing = enclosing;
3602 maybe_make_nested_class_name (name)
3605 tree id = NULL_TREE;
3609 make_nested_class_name (GET_CPC_LIST ());
3610 obstack_grow0 (&temporary_obstack,
3611 IDENTIFIER_POINTER (name),
3612 IDENTIFIER_LENGTH (name));
3613 id = get_identifier (obstack_finish (&temporary_obstack));
3615 QUALIFIED_P (id) = 1;
3620 /* If DECL is NULL, create and push a new DECL, record the current
3621 line CL and do other maintenance things. */
3624 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3625 tree decl, raw_name, qualified_name, cl;
3628 decl = push_class (make_class (), qualified_name);
3630 /* Take care of the file and line business */
3631 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3632 /* If we're emiting xrefs, store the line/col number information */
3634 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3636 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3637 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3638 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3639 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3641 PUSH_CPC (decl, raw_name);
3642 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3644 /* Link the declaration to the already seen ones */
3645 TREE_CHAIN (decl) = ctxp->class_list;
3646 ctxp->class_list = decl;
3648 /* Create a new nodes in the global lists */
3649 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3650 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3652 /* Install a new dependency list element */
3653 create_jdep_list (ctxp);
3655 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3656 IDENTIFIER_POINTER (qualified_name)));
3661 add_superinterfaces (decl, interface_list)
3662 tree decl, interface_list;
3665 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3666 takes care of ensuring that:
3667 - This is an accessible interface type,
3668 - Circularity detection.
3669 parser_add_interface is then called. If present but not defined,
3670 the check operation is delayed until the super interface gets
3672 for (node = interface_list; node; node = TREE_CHAIN (node))
3674 tree current = TREE_PURPOSE (node);
3675 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3676 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3678 if (!parser_check_super_interface (idecl, decl, current))
3679 parser_add_interface (decl, idecl, current);
3682 register_incomplete_type (JDEP_INTERFACE,
3683 current, decl, NULL_TREE);
3687 /* Create an interface in pass1 and return its decl. Return the
3688 interface's decl in pass 2. */
3691 create_interface (flags, id, super)
3695 tree raw_name = EXPR_WFL_NODE (id);
3696 tree q_name = parser_qualified_classname (raw_name);
3697 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3699 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3701 /* Basic checks: scope, redefinition, modifiers */
3702 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3708 /* Suspend the current parsing context if we're parsing an inner
3711 java_parser_context_suspend ();
3713 /* Push a new context for (static) initialized upon declaration fields */
3714 java_parser_context_push_initialized_field ();
3716 /* Interface modifiers check
3717 - public/abstract allowed (already done at that point)
3718 - abstract is obsolete (comes first, it's a warning, or should be)
3719 - Can't use twice the same (checked in the modifier rule) */
3720 if ((flags & ACC_ABSTRACT) && flag_redundant)
3721 parse_warning_context
3722 (MODIFIER_WFL (ABSTRACT_TK),
3723 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3725 /* Create a new decl if DECL is NULL, otherwise fix it */
3726 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3728 /* Set super info and mark the class a complete */
3729 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3730 object_type_node, ctxp->interface_number);
3731 ctxp->interface_number = 0;
3732 CLASS_COMPLETE_P (decl) = 1;
3733 add_superinterfaces (decl, super);
3738 /* Anonymous class counter. Will be reset to 1 every time a non
3739 anonymous class gets created. */
3740 static int anonymous_class_counter = 1;
3742 /* Patch anonymous class CLASS, by either extending or implementing
3746 patch_anonymous_class (type_decl, class_decl, wfl)
3747 tree type_decl, class_decl, wfl;
3749 tree class = TREE_TYPE (class_decl);
3750 tree type = TREE_TYPE (type_decl);
3751 tree binfo = TYPE_BINFO (class);
3753 /* If it's an interface, implement it */
3754 if (CLASS_INTERFACE (type_decl))
3759 if (parser_check_super_interface (type_decl, class_decl, wfl))
3762 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3763 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3764 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3765 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3766 /* And add the interface */
3767 parser_add_interface (class_decl, type_decl, wfl);
3769 /* Otherwise, it's a type we want to extend */
3772 if (parser_check_super (type_decl, class_decl, wfl))
3774 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3779 create_anonymous_class (location, type_name)
3784 tree super = NULL_TREE, itf = NULL_TREE;
3785 tree id, type_decl, class;
3787 /* The unqualified name of the anonymous class. It's just a number. */
3788 sprintf (buffer, "%d", anonymous_class_counter++);
3789 id = build_wfl_node (get_identifier (buffer));
3790 EXPR_WFL_LINECOL (id) = location;
3792 /* We know about the type to extend/implement. We go ahead */
3793 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3795 /* Create a class which either implements on extends the designated
3796 class. The class bears an innacessible name. */
3797 if (CLASS_INTERFACE (type_decl))
3799 /* It's OK to modify it here. It's been already used and
3800 shouldn't be reused */
3801 ctxp->interface_number = 1;
3802 /* Interfaces should presented as a list of WFLs */
3803 itf = build_tree_list (type_name, NULL_TREE);
3809 class = create_class (ACC_FINAL, id, super, itf);
3811 /* We didn't know anything about the stuff. We register a dependence. */
3813 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3815 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3819 /* Create a class in pass1 and return its decl. Return class
3820 interface's decl in pass 2. */
3823 create_class (flags, id, super, interfaces)
3825 tree id, super, interfaces;
3827 tree raw_name = EXPR_WFL_NODE (id);
3828 tree class_id, decl;
3829 tree super_decl_type;
3831 class_id = parser_qualified_classname (raw_name);
3832 decl = IDENTIFIER_CLASS_VALUE (class_id);
3833 EXPR_WFL_NODE (id) = class_id;
3835 /* Basic check: scope, redefinition, modifiers */
3836 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3842 /* Suspend the current parsing context if we're parsing an inner
3843 class or an anonymous class. */
3845 java_parser_context_suspend ();
3846 /* Push a new context for (static) initialized upon declaration fields */
3847 java_parser_context_push_initialized_field ();
3849 /* Class modifier check:
3850 - Allowed modifier (already done at that point)
3851 - abstract AND final forbidden
3852 - Public classes defined in the correct file */
3853 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3855 (id, "Class `%s' can't be declared both abstract and final",
3856 IDENTIFIER_POINTER (raw_name));
3858 /* Create a new decl if DECL is NULL, otherwise fix it */
3859 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3861 /* If SUPER exists, use it, otherwise use Object */
3864 /* Can't extend java.lang.Object */
3865 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3867 parse_error_context (id, "Can't extend `java.lang.Object'");
3872 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3874 else if (TREE_TYPE (decl) != object_type_node)
3875 super_decl_type = object_type_node;
3876 /* We're defining java.lang.Object */
3878 super_decl_type = NULL_TREE;
3880 /* A class nested in an interface is implicitly static. */
3881 if (INNER_CLASS_DECL_P (decl)
3882 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3884 flags |= ACC_STATIC;
3887 /* Set super info and mark the class as complete. */
3888 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3889 ctxp->interface_number);
3890 ctxp->interface_number = 0;
3891 CLASS_COMPLETE_P (decl) = 1;
3892 add_superinterfaces (decl, interfaces);
3894 /* Add the private this$<n> field, Replicate final locals still in
3895 scope as private final fields mangled like val$<local_name>.
3896 This doesn't not occur for top level (static) inner classes. */
3897 if (PURE_INNER_CLASS_DECL_P (decl))
3898 add_inner_class_fields (decl, current_function_decl);
3900 /* If doing xref, store the location at which the inherited class
3901 (if any) was seen. */
3902 if (flag_emit_xref && super)
3903 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3905 /* Eventually sets the @deprecated tag flag */
3906 CHECK_DEPRECATED (decl);
3908 /* Reset the anonymous class counter when declaring non inner classes */
3909 if (!INNER_CLASS_DECL_P (decl))
3910 anonymous_class_counter = 1;
3915 /* End a class declaration: register the statements used to create
3916 finit$ and <clinit>, pop the current class and resume the prior
3917 parser context if necessary. */
3920 end_class_declaration (resume)
3923 /* If an error occured, context weren't pushed and won't need to be
3924 popped by a resume. */
3925 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3927 java_parser_context_pop_initialized_field ();
3929 if (resume && no_error_occured)
3930 java_parser_context_resume ();
3932 /* We're ending a class declaration, this is a good time to reset
3933 the interface cout. Note that might have been already done in
3934 create_interface, but if at that time an inner class was being
3935 dealt with, the interface count was reset in a context created
3936 for the sake of handling inner classes declaration. */
3937 ctxp->interface_number = 0;
3941 add_inner_class_fields (class_decl, fct_decl)
3945 tree block, marker, f;
3947 f = add_field (TREE_TYPE (class_decl),
3948 build_current_thisn (TREE_TYPE (class_decl)),
3949 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3951 FIELD_THISN (f) = 1;
3956 for (block = GET_CURRENT_BLOCK (fct_decl);
3957 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3960 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3963 tree wfl, init, list;
3965 /* Avoid non final arguments. */
3966 if (!LOCAL_FINAL_P (decl))
3969 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3970 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3971 wfl = build_wfl_node (name);
3972 init = build_wfl_node (pname);
3973 /* Build an initialization for the field: it will be
3974 initialized by a parameter added to finit$, bearing a
3975 mangled name of the field itself (param$<n>.) The
3976 parameter is provided to finit$ by the constructor
3977 invoking it (hence the constructor will also feature a
3978 hidden parameter, set to the value of the outer context
3979 local at the time the inner class is created.)
3981 Note: we take into account all possible locals that can
3982 be accessed by the inner class. It's actually not trivial
3983 to minimize these aliases down to the ones really
3984 used. One way to do that would be to expand all regular
3985 methods first, then finit$ to get a picture of what's
3986 used. It works with the exception that we would have to
3987 go back on all constructor invoked in regular methods to
3988 have their invokation reworked (to include the right amount
3989 of alias initializer parameters.)
3991 The only real way around, I think, is a first pass to
3992 identify locals really used in the inner class. We leave
3993 the flag FIELD_LOCAL_ALIAS_USED around for that future
3996 On the other hand, it only affect local inner classes,
3997 whose constructors (and finit$ call) will be featuring
3998 unecessary arguments. It's easy for a developper to keep
3999 this number of parameter down by using the `final'
4000 keyword only when necessary. For the time being, we can
4001 issue a warning on unecessary finals. FIXME */
4002 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4005 /* Register the field. The TREE_LIST holding the part
4006 initialized/initializer will be marked ARG_FINAL_P so
4007 that the created field can be marked
4008 FIELD_LOCAL_ALIAS. */
4009 list = build_tree_list (wfl, init);
4010 ARG_FINAL_P (list) = 1;
4011 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4015 if (!CPC_INITIALIZER_STMT (ctxp))
4018 /* If we ever registered an alias field, insert and marker to
4019 remeber where the list ends. The second part of the list (the one
4020 featuring initialized fields) so it can be later reversed to
4021 enforce 8.5. The marker will be removed during that operation. */
4022 marker = build_tree_list (NULL_TREE, NULL_TREE);
4023 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4024 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4027 /* Can't use lookup_field () since we don't want to load the class and
4028 can't set the CLASS_LOADED_P flag */
4031 find_field (class, name)
4036 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4038 if (DECL_NAME (decl) == name)
4044 /* Wrap around lookup_field that doesn't potentially upset the value
4048 lookup_field_wrapper (class, name)
4052 tree decl = NULL_TREE;
4053 java_parser_context_save_global ();
4055 /* Last chance: if we're within the context of an inner class, we
4056 might be trying to access a local variable defined in an outer
4057 context. We try to look for it now. */
4058 if (INNER_CLASS_TYPE_P (class))
4061 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4062 decl = lookup_field (&type, new_name);
4063 if (decl && decl != error_mark_node)
4064 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4066 if (!decl || decl == error_mark_node)
4069 decl = lookup_field (&type, name);
4072 java_parser_context_restore_global ();
4073 return decl == error_mark_node ? NULL : decl;
4076 /* Find duplicate field within the same class declarations and report
4077 the error. Returns 1 if a duplicated field was found, 0
4081 duplicate_declaration_error_p (new_field_name, new_type, cl)
4082 tree new_field_name, new_type, cl;
4084 /* This might be modified to work with method decl as well */
4085 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4088 char *t1 = xstrdup (purify_type_name
4089 ((TREE_CODE (new_type) == POINTER_TYPE
4090 && TREE_TYPE (new_type) == NULL_TREE) ?
4091 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4092 lang_printable_name (new_type, 1)));
4093 /* The type may not have been completed by the time we report
4095 char *t2 = xstrdup (purify_type_name
4096 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4097 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4098 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4099 lang_printable_name (TREE_TYPE (decl), 1)));
4101 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4102 t1, IDENTIFIER_POINTER (new_field_name),
4103 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4104 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4112 /* Field registration routine. If TYPE doesn't exist, field
4113 declarations are linked to the undefined TYPE dependency list, to
4114 be later resolved in java_complete_class () */
4117 register_fields (flags, type, variable_list)
4119 tree type, variable_list;
4121 tree current, saved_type;
4122 tree class_type = NULL_TREE;
4123 int saved_lineno = lineno;
4125 tree wfl = NULL_TREE;
4128 class_type = TREE_TYPE (GET_CPC ());
4130 if (!class_type || class_type == error_mark_node)
4133 /* If we're adding fields to interfaces, those fields are public,
4135 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4137 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4138 flags, ACC_PUBLIC, "interface field(s)");
4139 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4140 flags, ACC_STATIC, "interface field(s)");
4141 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4142 flags, ACC_FINAL, "interface field(s)");
4143 check_modifiers ("Illegal interface member modifier `%s'", flags,
4144 INTERFACE_FIELD_MODIFIERS);
4145 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4148 /* Obtain a suitable type for resolution, if necessary */
4149 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4151 /* If TYPE is fully resolved and we don't have a reference, make one */
4152 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4154 for (current = variable_list, saved_type = type; current;
4155 current = TREE_CHAIN (current), type = saved_type)
4159 tree cl = TREE_PURPOSE (current);
4160 tree init = TREE_VALUE (current);
4161 tree current_name = EXPR_WFL_NODE (cl);
4163 /* Can't declare non-final static fields in inner classes */
4164 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4165 && !(flags & ACC_FINAL))
4167 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4168 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4169 lang_printable_name (class_type, 0));
4171 /* Process NAME, as it may specify extra dimension(s) for it */
4172 type = build_array_from_name (type, wfl, current_name, ¤t_name);
4174 /* Type adjustment. We may have just readjusted TYPE because
4175 the variable specified more dimensions. Make sure we have
4176 a reference if we can and don't have one already. Also
4177 change the name if we have an init. */
4178 if (type != saved_type)
4180 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4182 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4185 real_type = GET_REAL_TYPE (type);
4186 /* Check for redeclarations */
4187 if (duplicate_declaration_error_p (current_name, real_type, cl))
4190 /* Set lineno to the line the field was found and create a
4191 declaration for it. Eventually sets the @deprecated tag flag. */
4193 lineno = EXPR_WFL_LINECOL (cl);
4195 lineno = EXPR_WFL_LINENO (cl);
4196 field_decl = add_field (class_type, current_name, real_type, flags);
4197 CHECK_DEPRECATED (field_decl);
4199 /* If the field denotes a final instance variable, then we
4200 allocate a LANG_DECL_SPECIFIC part to keep track of its
4201 initialization. We also mark whether the field was
4202 initialized upon it's declaration. We don't do that if the
4203 created field is an alias to a final local. */
4204 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4206 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4207 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4208 if ((flags & ACC_STATIC) && init)
4209 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4212 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4213 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4214 hide parameters to this inner class finit$ and
4215 constructors. It also means that the field isn't final per
4217 if (ARG_FINAL_P (current))
4219 FIELD_LOCAL_ALIAS (field_decl) = 1;
4220 FIELD_FINAL (field_decl) = 0;
4223 /* Check if we must chain. */
4225 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4227 /* If we have an initialization value tied to the field */
4230 /* The field is declared static */
4231 if (flags & ACC_STATIC)
4233 /* We include the field and its initialization part into
4234 a list used to generate <clinit>. After <clinit> is
4235 walked, field initializations will be processed and
4236 fields initialized with known constants will be taken
4237 out of <clinit> and have their DECL_INITIAL set
4239 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4240 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4241 if (TREE_OPERAND (init, 1)
4242 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4243 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4245 /* A non-static field declared with an immediate initialization is
4246 to be initialized in <init>, if any. This field is remembered
4247 to be processed at the time of the generation of <init>. */
4250 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4251 SET_CPC_INITIALIZER_STMT (ctxp, init);
4253 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4254 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4257 lineno = saved_lineno;
4260 /* Generate finit$, using the list of initialized fields to populate
4261 its body. finit$'s parameter(s) list is adjusted to include the
4262 one(s) used to initialized the field(s) caching outer context
4266 generate_finit (class_type)
4270 tree list = TYPE_FINIT_STMT_LIST (class_type);
4271 tree mdecl, current, parms;
4273 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4274 class_type, NULL_TREE,
4276 CRAFTED_PARAM_LIST_FIXUP (parms);
4277 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4278 finit_identifier_node, parms);
4279 fix_method_argument_names (parms, mdecl);
4280 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4282 DECL_FUNCTION_NAP (mdecl) = count;
4283 start_artificial_method_body (mdecl);
4285 for (current = list; current; current = TREE_CHAIN (current))
4286 java_method_add_stmt (mdecl,
4287 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4289 end_artificial_method_body (mdecl);
4294 add_instance_initializer (mdecl)
4298 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4299 tree compound = NULL_TREE;
4303 for (current = stmt_list; current; current = TREE_CHAIN (current))
4304 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4306 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4307 NULL_TREE, compound));
4311 /* Shared accros method_declarator and method_header to remember the
4312 patch stage that was reached during the declaration of the method.
4313 A method DECL is built differently is there is no patch
4314 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4315 pending on the currently defined method. */
4317 static int patch_stage;
4319 /* Check the method declaration and add the method to its current
4320 class. If the argument list is known to contain incomplete types,
4321 the method is partially added and the registration will be resume
4322 once the method arguments resolved. If TYPE is NULL, we're dealing
4323 with a constructor. */
4326 method_header (flags, type, mdecl, throws)
4328 tree type, mdecl, throws;
4330 tree type_wfl = NULL_TREE;
4331 tree meth_name = NULL_TREE;
4332 tree current, orig_arg, this_class = NULL;
4335 int constructor_ok = 0, must_chain;
4338 if (mdecl == error_mark_node)
4339 return error_mark_node;
4340 meth = TREE_VALUE (mdecl);
4341 id = TREE_PURPOSE (mdecl);
4343 check_modifiers_consistency (flags);
4346 this_class = TREE_TYPE (GET_CPC ());
4348 if (!this_class || this_class == error_mark_node)
4351 /* There are some forbidden modifiers for an abstract method and its
4352 class must be abstract as well. */
4353 if (type && (flags & ACC_ABSTRACT))
4355 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4356 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4357 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4358 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4359 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4360 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4361 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4363 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4364 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4365 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4368 /* Things to be checked when declaring a constructor */
4371 int ec = java_error_count;
4372 /* 8.6: Constructor declarations: we might be trying to define a
4373 method without specifying a return type. */
4374 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4376 (id, "Invalid method declaration, return type required");
4377 /* 8.6.3: Constructor modifiers */
4380 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4381 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4382 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4383 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4384 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4386 /* If we found error here, we don't consider it's OK to tread
4387 the method definition as a constructor, for the rest of this
4389 if (ec == java_error_count)
4393 /* Method declared within the scope of an interface are implicitly
4394 abstract and public. Conflicts with other erroneously provided
4395 modifiers are checked right after. */
4397 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4399 /* If FLAGS isn't set because of a modifier, turn the
4400 corresponding modifier WFL to NULL so we issue a warning on
4401 the obsolete use of the modifier */
4402 if (!(flags & ACC_PUBLIC))
4403 MODIFIER_WFL (PUBLIC_TK) = NULL;
4404 if (!(flags & ACC_ABSTRACT))
4405 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4406 flags |= ACC_PUBLIC;
4407 flags |= ACC_ABSTRACT;
4410 /* Inner class can't declare static methods */
4411 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4414 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4415 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4416 lang_printable_name (this_class, 0));
4419 /* Modifiers context reset moved up, so abstract method declaration
4420 modifiers can be later checked. */
4422 /* Set constructor returned type to void and method name to <init>,
4423 unless we found an error identifier the constructor (in which
4424 case we retain the original name) */
4427 type = void_type_node;
4429 meth_name = init_identifier_node;
4432 meth_name = EXPR_WFL_NODE (id);
4434 /* Do the returned type resolution and registration if necessary */
4435 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4438 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4439 EXPR_WFL_NODE (id) = meth_name;
4440 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4444 patch_stage = JDEP_METHOD_RETURN;
4445 register_incomplete_type (patch_stage, type_wfl, id, type);
4446 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4449 TREE_TYPE (meth) = type;
4451 saved_lineno = lineno;
4452 /* When defining an abstract or interface method, the curly
4453 bracket at level 1 doesn't exist because there is no function
4455 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4456 EXPR_WFL_LINENO (id));
4458 /* Remember the original argument list */
4459 orig_arg = TYPE_ARG_TYPES (meth);
4461 if (patch_stage) /* includes ret type and/or all args */
4464 meth = add_method_1 (this_class, flags, meth_name, meth);
4465 /* Patch for the return type */
4466 if (patch_stage == JDEP_METHOD_RETURN)
4468 jdep = CLASSD_LAST (ctxp->classd_list);
4469 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4471 /* This is the stop JDEP. METH allows the function's signature
4473 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4476 meth = add_method (this_class, flags, meth_name,
4477 build_java_signature (meth));
4479 /* Remember final parameters */
4480 MARK_FINAL_PARMS (meth, orig_arg);
4482 /* Fix the method argument list so we have the argument name
4484 fix_method_argument_names (orig_arg, meth);
4486 /* Register the parameter number and re-install the current line
4488 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4489 lineno = saved_lineno;
4491 /* Register exception specified by the `throws' keyword for
4492 resolution and set the method decl appropriate field to the list.
4493 Note: the grammar ensures that what we get here are class
4497 throws = nreverse (throws);
4498 for (current = throws; current; current = TREE_CHAIN (current))
4500 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4501 NULL_TREE, NULL_TREE);
4502 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4503 &TREE_VALUE (current);
4505 DECL_FUNCTION_THROWS (meth) = throws;
4508 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4509 DECL_FUNCTION_WFL (meth) = id;
4511 /* Set the flag if we correctly processed a constructor */
4514 DECL_CONSTRUCTOR_P (meth) = 1;
4515 /* Compute and store the number of artificial parameters declared
4516 for this constructor */
4517 for (count = 0, current = TYPE_FIELDS (this_class); current;
4518 current = TREE_CHAIN (current))
4519 if (FIELD_LOCAL_ALIAS (current))
4521 DECL_FUNCTION_NAP (meth) = count;
4524 /* Eventually set the @deprecated tag flag */
4525 CHECK_DEPRECATED (meth);
4527 /* If doing xref, store column and line number information instead
4528 of the line number only. */
4530 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4536 fix_method_argument_names (orig_arg, meth)
4537 tree orig_arg, meth;
4539 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4540 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4542 TREE_PURPOSE (arg) = this_identifier_node;
4543 arg = TREE_CHAIN (arg);
4545 while (orig_arg != end_params_node)
4547 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4548 orig_arg = TREE_CHAIN (orig_arg);
4549 arg = TREE_CHAIN (arg);
4553 /* Complete the method declaration with METHOD_BODY. */
4556 finish_method_declaration (method_body)
4561 if (!current_function_decl)
4564 flags = get_access_flags_from_decl (current_function_decl);
4566 /* 8.4.5 Method Body */
4567 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4569 tree name = DECL_NAME (current_function_decl);
4570 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4571 "%s method `%s' can't have a body defined",
4572 (METHOD_NATIVE (current_function_decl) ?
4573 "Native" : "Abstract"),
4574 IDENTIFIER_POINTER (name));
4575 method_body = NULL_TREE;
4577 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4579 tree name = DECL_NAME (current_function_decl);
4581 (DECL_FUNCTION_WFL (current_function_decl),
4582 "Non native and non abstract method `%s' must have a body defined",
4583 IDENTIFIER_POINTER (name));
4584 method_body = NULL_TREE;
4587 if (flag_emit_class_files && method_body
4588 && TREE_CODE (method_body) == NOP_EXPR
4589 && TREE_TYPE (current_function_decl)
4590 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4591 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4593 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4594 maybe_absorb_scoping_blocks ();
4595 /* Exit function's body */
4597 /* Merge last line of the function with first line, directly in the
4598 function decl. It will be used to emit correct debug info. */
4599 if (!flag_emit_xref)
4600 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4602 /* Since function's argument's list are shared, reset the
4603 ARG_FINAL_P parameter that might have been set on some of this
4604 function parameters. */
4605 UNMARK_FINAL_PARMS (current_function_decl);
4607 /* So we don't have an irrelevant function declaration context for
4608 the next static block we'll see. */
4609 current_function_decl = NULL_TREE;
4612 /* Build a an error message for constructor circularity errors. */
4615 constructor_circularity_msg (from, to)
4618 static char string [4096];
4619 char *t = xstrdup (lang_printable_name (from, 0));
4620 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4625 /* Verify a circular call to METH. Return 1 if an error is found, 0
4629 verify_constructor_circularity (meth, current)
4632 static tree list = NULL_TREE;
4633 static int initialized_p;
4636 /* If we haven't already registered LIST with the garbage collector,
4640 ggc_add_tree_root (&list, 1);
4644 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4646 if (TREE_VALUE (c) == meth)
4652 list = nreverse (list);
4653 for (liste = list; liste; liste = TREE_CHAIN (liste))
4656 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4657 constructor_circularity_msg
4658 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4662 t = xstrdup (lang_printable_name (meth, 0));
4663 parse_error_context (TREE_PURPOSE (c),
4664 "%s: recursive invocation of constructor `%s'",
4665 constructor_circularity_msg (current, meth), t);
4671 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4673 list = tree_cons (c, current, list);
4674 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4676 list = TREE_CHAIN (list);
4681 /* Check modifiers that can be declared but exclusively */
4684 check_modifiers_consistency (flags)
4688 tree cl = NULL_TREE;
4690 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4691 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4692 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4695 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4699 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4700 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4702 parse_error_context (cl,
4703 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4706 /* Check the methode header METH for abstract specifics features */
4709 check_abstract_method_header (meth)
4712 int flags = get_access_flags_from_decl (meth);
4714 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4715 ACC_ABSTRACT, "abstract method",
4716 IDENTIFIER_POINTER (DECL_NAME (meth)));
4717 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4718 ACC_PUBLIC, "abstract method",
4719 IDENTIFIER_POINTER (DECL_NAME (meth)));
4721 check_modifiers ("Illegal modifier `%s' for interface method",
4722 flags, INTERFACE_METHOD_MODIFIERS);
4725 /* Create a FUNCTION_TYPE node and start augmenting it with the
4726 declared function arguments. Arguments type that can't be resolved
4727 are left as they are, but the returned node is marked as containing
4728 incomplete types. */
4731 method_declarator (id, list)
4734 tree arg_types = NULL_TREE, current, node;
4735 tree meth = make_node (FUNCTION_TYPE);
4738 patch_stage = JDEP_NO_PATCH;
4740 if (GET_CPC () == error_mark_node)
4741 return error_mark_node;
4743 /* If we're dealing with an inner class constructor, we hide the
4744 this$<n> decl in the name field of its parameter declaration. We
4745 also might have to hide the outer context local alias
4746 initializers. Not done when the class is a toplevel class. */
4747 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4748 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4750 tree aliases_list, type, thisn;
4751 /* First the aliases, linked to the regular parameters */
4753 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4754 TREE_TYPE (GET_CPC ()),
4756 list = chainon (nreverse (aliases_list), list);
4759 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4760 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4761 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4765 for (current = list; current; current = TREE_CHAIN (current))
4768 tree wfl_name = TREE_PURPOSE (current);
4769 tree type = TREE_VALUE (current);
4770 tree name = EXPR_WFL_NODE (wfl_name);
4771 tree already, arg_node;
4772 tree type_wfl = NULL_TREE;
4775 /* Obtain a suitable type for resolution, if necessary */
4776 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4778 /* Process NAME, as it may specify extra dimension(s) for it */
4779 type = build_array_from_name (type, type_wfl, name, &name);
4780 EXPR_WFL_NODE (wfl_name) = name;
4782 real_type = GET_REAL_TYPE (type);
4783 if (TREE_CODE (real_type) == RECORD_TYPE)
4785 real_type = promote_type (real_type);
4786 if (TREE_CODE (type) == TREE_LIST)
4787 TREE_PURPOSE (type) = real_type;
4790 /* Check redefinition */
4791 for (already = arg_types; already; already = TREE_CHAIN (already))
4792 if (TREE_PURPOSE (already) == name)
4795 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4796 IDENTIFIER_POINTER (name),
4797 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4801 /* If we've an incomplete argument type, we know there is a location
4802 to patch when the type get resolved, later. */
4806 patch_stage = JDEP_METHOD;
4807 type = register_incomplete_type (patch_stage,
4808 type_wfl, wfl_name, type);
4809 jdep = CLASSD_LAST (ctxp->classd_list);
4810 JDEP_MISC (jdep) = id;
4813 /* The argument node: a name and a (possibly) incomplete type. */
4814 arg_node = build_tree_list (name, real_type);
4815 /* Remeber arguments declared final. */
4816 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4819 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4820 TREE_CHAIN (arg_node) = arg_types;
4821 arg_types = arg_node;
4823 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4824 node = build_tree_list (id, meth);
4829 unresolved_type_p (wfl, returned)
4834 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4838 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4839 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4840 *returned = TREE_TYPE (decl);
4841 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4842 *returned = TREE_TYPE (GET_CPC ());
4844 *returned = NULL_TREE;
4853 /* From NAME, build a qualified identifier node using the
4854 qualification from the current package definition. */
4857 parser_qualified_classname (name)
4860 tree nested_class_name;
4862 if ((nested_class_name = maybe_make_nested_class_name (name)))
4863 return nested_class_name;
4866 return merge_qualified_name (ctxp->package, name);
4871 /* Called once the type a interface extends is resolved. Returns 0 if
4872 everything is OK. */
4875 parser_check_super_interface (super_decl, this_decl, this_wfl)
4876 tree super_decl, this_decl, this_wfl;
4878 tree super_type = TREE_TYPE (super_decl);
4880 /* Has to be an interface */
4881 if (!CLASS_INTERFACE (super_decl))
4884 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4885 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4886 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4887 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4888 "interface" : "class"),
4889 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4893 /* Check top-level interface access. Inner classes are subject to member
4894 access rules (6.6.1). */
4895 if (! INNER_CLASS_P (super_type)
4896 && check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4899 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4900 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4901 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4905 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4906 0 if everthing is OK. */
4909 parser_check_super (super_decl, this_decl, wfl)
4910 tree super_decl, this_decl, wfl;
4912 tree super_type = TREE_TYPE (super_decl);
4914 /* SUPER should be a CLASS (neither an array nor an interface) */
4915 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4918 (wfl, "Class `%s' can't subclass %s `%s'",
4919 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4920 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4921 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4925 if (CLASS_FINAL (TYPE_NAME (super_type)))
4927 parse_error_context (wfl, "Can't subclass final classes: %s",
4928 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4932 /* Check top-level class scope. Inner classes are subject to member access
4934 if (! INNER_CLASS_P (super_type)
4935 && (check_pkg_class_access (DECL_NAME (super_decl), wfl)))
4938 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4939 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4940 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4944 /* Create a new dependency list and link it (in a LIFO manner) to the
4945 CTXP list of type dependency list. */
4948 create_jdep_list (ctxp)
4949 struct parser_ctxt *ctxp;
4951 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
4952 new->first = new->last = NULL;
4953 new->next = ctxp->classd_list;
4954 ctxp->classd_list = new;
4958 reverse_jdep_list (ctxp)
4959 struct parser_ctxt *ctxp;
4961 register jdeplist *prev = NULL, *current, *next;
4962 for (current = ctxp->classd_list; current; current = next)
4964 next = current->next;
4965 current->next = prev;
4971 /* Create a fake pointer based on the ID stored in
4972 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4973 registered again. */
4976 obtain_incomplete_type (type_name)
4979 tree ptr = NULL_TREE, name;
4981 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4982 name = EXPR_WFL_NODE (type_name);
4983 else if (INCOMPLETE_TYPE_P (type_name))
4984 name = TYPE_NAME (type_name);
4988 BUILD_PTR_FROM_NAME (ptr, name);
4994 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4995 non NULL instead of computing a new fake type based on WFL. The new
4996 dependency is inserted in the current type dependency list, in FIFO
5000 register_incomplete_type (kind, wfl, decl, ptr)
5002 tree wfl, decl, ptr;
5004 jdep *new = (jdep *)xmalloc (sizeof (jdep));
5006 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5007 ptr = obtain_incomplete_type (wfl);
5009 JDEP_KIND (new) = kind;
5010 JDEP_DECL (new) = decl;
5011 JDEP_TO_RESOLVE (new) = ptr;
5012 JDEP_WFL (new) = wfl;
5013 JDEP_CHAIN (new) = NULL;
5014 JDEP_MISC (new) = NULL_TREE;
5015 /* For some dependencies, set the enclosing class of the current
5016 class to be the enclosing context */
5017 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5018 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
5019 && GET_ENCLOSING_CPC ())
5020 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5022 JDEP_ENCLOSING (new) = GET_CPC ();
5023 JDEP_GET_PATCH (new) = (tree *)NULL;
5025 JDEP_INSERT (ctxp->classd_list, new);
5031 java_check_circular_reference ()
5034 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5036 tree type = TREE_TYPE (current);
5037 if (CLASS_INTERFACE (current))
5039 /* Check all interfaces this class extends */
5040 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5045 n = TREE_VEC_LENGTH (basetype_vec);
5046 for (i = 0; i < n; i++)
5048 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5049 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5050 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5051 parse_error_context (lookup_cl (current),
5052 "Cyclic interface inheritance");
5056 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5057 parse_error_context (lookup_cl (current),
5058 "Cyclic class inheritance%s",
5059 (cyclic_inheritance_report ?
5060 cyclic_inheritance_report : ""));
5064 /* Augment the parameter list PARM with parameters crafted to
5065 initialize outer context locals aliases. Through ARTIFICIAL, a
5066 count is kept of the number of crafted parameters. MODE governs
5067 what eventually gets created: something suitable for a function
5068 creation or a function invocation, either the constructor or
5072 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5074 tree class_type, parm;
5078 tree additional_parms = NULL_TREE;
5080 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5081 if (FIELD_LOCAL_ALIAS (field))
5083 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5084 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5089 case AIPL_FUNCTION_DECLARATION:
5090 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5092 purpose = build_wfl_node (mangled_id);
5093 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5094 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5096 value = TREE_TYPE (field);
5099 case AIPL_FUNCTION_CREATION:
5100 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5102 value = TREE_TYPE (field);
5105 case AIPL_FUNCTION_FINIT_INVOCATION:
5106 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5108 /* Now, this is wrong. purpose should always be the NAME
5109 of something and value its matching value (decl, type,
5110 etc...) FIXME -- but there is a lot to fix. */
5112 /* When invoked for this kind of operation, we already
5113 know whether a field is used or not. */
5114 purpose = TREE_TYPE (field);
5115 value = build_wfl_node (mangled_id);
5118 case AIPL_FUNCTION_CTOR_INVOCATION:
5119 /* There are two case: the constructor invokation happends
5120 outside the local inner, in which case, locales from the outer
5121 context are directly used.
5123 Otherwise, we fold to using the alias directly. */
5124 if (class_type == current_class)
5128 name = get_identifier (&buffer[4]);
5129 value = IDENTIFIER_LOCAL_VALUE (name);
5133 additional_parms = tree_cons (purpose, value, additional_parms);
5137 if (additional_parms)
5139 if (ANONYMOUS_CLASS_P (class_type)
5140 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5141 additional_parms = nreverse (additional_parms);
5142 parm = chainon (additional_parms, parm);
5148 /* Craft a constructor for CLASS_DECL -- what we should do when none
5149 where found. ARGS is non NULL when a special signature must be
5150 enforced. This is the case for anonymous classes. */
5153 craft_constructor (class_decl, args)
5154 tree class_decl, args;
5156 tree class_type = TREE_TYPE (class_decl);
5157 tree parm = NULL_TREE;
5158 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5160 int i = 0, artificial = 0;
5161 tree decl, ctor_name;
5164 /* The constructor name is <init> unless we're dealing with an
5165 anonymous class, in which case the name will be fixed after having
5167 if (ANONYMOUS_CLASS_P (class_type))
5168 ctor_name = DECL_NAME (class_decl);
5170 ctor_name = init_identifier_node;
5172 /* If we're dealing with an inner class constructor, we hide the
5173 this$<n> decl in the name field of its parameter declaration. */
5174 if (PURE_INNER_CLASS_TYPE_P (class_type))
5176 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5177 parm = tree_cons (build_current_thisn (class_type),
5178 build_pointer_type (type), parm);
5180 /* Some more arguments to be hidden here. The values of the local
5181 variables of the outer context that the inner class needs to see. */
5182 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5187 /* Then if there are any args to be enforced, enforce them now */
5188 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5190 sprintf (buffer, "parm%d", i++);
5191 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5194 CRAFTED_PARAM_LIST_FIXUP (parm);
5195 decl = create_artificial_method (class_type, flags, void_type_node,
5197 fix_method_argument_names (parm, decl);
5198 /* Now, mark the artificial parameters. */
5199 DECL_FUNCTION_NAP (decl) = artificial;
5200 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5204 /* Fix the constructors. This will be called right after circular
5205 references have been checked. It is necessary to fix constructors
5206 early even if no code generation will take place for that class:
5207 some generated constructor might be required by the class whose
5208 compilation triggered this one to be simply loaded. */
5211 java_fix_constructors ()
5215 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5217 tree class_type = TREE_TYPE (current);
5221 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5224 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5226 if (DECL_CONSTRUCTOR_P (decl))
5228 fix_constructors (decl);
5233 /* Anonymous class constructor can't be generated that early. */
5234 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5235 craft_constructor (current, NULL_TREE);
5239 /* safe_layout_class just makes sure that we can load a class without
5240 disrupting the current_class, input_file, lineno, etc, information
5241 about the class processed currently. */
5244 safe_layout_class (class)
5247 tree save_current_class = current_class;
5248 const char *save_input_filename = input_filename;
5249 int save_lineno = lineno;
5251 layout_class (class);
5253 current_class = save_current_class;
5254 input_filename = save_input_filename;
5255 lineno = save_lineno;
5256 CLASS_LOADED_P (class) = 1;
5260 jdep_resolve_class (dep)
5265 if (JDEP_RESOLVED_P (dep))
5266 decl = JDEP_RESOLVED_DECL (dep);
5269 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5270 JDEP_DECL (dep), JDEP_WFL (dep));
5271 JDEP_RESOLVED (dep, decl);
5275 complete_class_report_errors (dep);
5276 else if (PURE_INNER_CLASS_DECL_P (decl))
5277 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5281 /* Complete unsatisfied class declaration and their dependencies */
5284 java_complete_class ()
5291 /* Process imports */
5294 /* Rever things so we have the right order */
5295 ctxp->class_list = nreverse (ctxp->class_list);
5296 ctxp->classd_list = reverse_jdep_list (ctxp);
5298 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5300 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5303 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5306 if (!(decl = jdep_resolve_class (dep)))
5309 /* Now it's time to patch */
5310 switch (JDEP_KIND (dep))
5313 /* Simply patch super */
5314 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5316 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5317 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5322 /* We do part of the job done in add_field */
5323 tree field_decl = JDEP_DECL (dep);
5324 tree field_type = TREE_TYPE (decl);
5325 if (TREE_CODE (field_type) == RECORD_TYPE)
5326 field_type = promote_type (field_type);
5327 TREE_TYPE (field_decl) = field_type;
5328 DECL_ALIGN (field_decl) = 0;
5329 DECL_USER_ALIGN (field_decl) = 0;
5330 layout_decl (field_decl, 0);
5331 SOURCE_FRONTEND_DEBUG
5332 (("Completed field/var decl `%s' with `%s'",
5333 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5334 IDENTIFIER_POINTER (DECL_NAME (decl))));
5337 case JDEP_METHOD: /* We start patching a method */
5338 case JDEP_METHOD_RETURN:
5344 type = TREE_TYPE(decl);
5345 if (TREE_CODE (type) == RECORD_TYPE)
5346 type = promote_type (type);
5347 JDEP_APPLY_PATCH (dep, type);
5348 SOURCE_FRONTEND_DEBUG
5349 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5350 "Completing fct `%s' with ret type `%s'":
5351 "Completing arg `%s' with type `%s'"),
5352 IDENTIFIER_POINTER (EXPR_WFL_NODE
5353 (JDEP_DECL_WFL (dep))),
5354 IDENTIFIER_POINTER (DECL_NAME (decl))));
5358 dep = JDEP_CHAIN (dep);
5359 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5362 decl = jdep_resolve_class (dep);
5366 tree mdecl = JDEP_DECL (dep), signature;
5367 /* Recompute and reset the signature, check first that
5368 all types are now defined. If they're not,
5369 dont build the signature. */
5370 if (check_method_types_complete (mdecl))
5372 signature = build_java_signature (TREE_TYPE (mdecl));
5373 set_java_signature (TREE_TYPE (mdecl), signature);
5380 case JDEP_INTERFACE:
5381 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5384 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5389 type = TREE_TYPE(decl);
5390 if (TREE_CODE (type) == RECORD_TYPE)
5391 type = promote_type (type);
5392 JDEP_APPLY_PATCH (dep, type);
5396 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5397 SOURCE_FRONTEND_DEBUG
5398 (("Completing a random type dependency on a '%s' node",
5399 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5402 case JDEP_EXCEPTION:
5403 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5404 SOURCE_FRONTEND_DEBUG
5405 (("Completing `%s' `throws' argument node",
5406 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5409 case JDEP_ANONYMOUS:
5410 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5421 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5425 resolve_class (enclosing, class_type, decl, cl)
5426 tree enclosing, class_type, decl, cl;
5428 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5429 const char *base = name;
5430 tree resolved_type = TREE_TYPE (class_type);
5431 tree resolved_type_decl;
5433 if (resolved_type != NULL_TREE)
5435 tree resolved_type_decl = TYPE_NAME (resolved_type);
5436 if (resolved_type_decl == NULL_TREE
5437 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5439 resolved_type_decl = build_decl (TYPE_DECL,
5440 TYPE_NAME (class_type),
5443 return resolved_type_decl;
5446 /* 1- Check to see if we have an array. If true, find what we really
5448 while (name[0] == '[')
5452 TYPE_NAME (class_type) = get_identifier (name);
5453 WFL_STRIP_BRACKET (cl, cl);
5456 /* 2- Resolve the bare type */
5457 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5460 resolved_type = TREE_TYPE (resolved_type_decl);
5462 /* 3- If we have and array, reconstruct the array down to its nesting */
5465 while (base != name)
5467 resolved_type = build_java_array_type (resolved_type, -1);
5468 CLASS_LOADED_P (resolved_type) = 1;
5471 /* A TYPE_NAME that is a TYPE_DECL was set in
5472 build_java_array_type, return it. */
5473 resolved_type_decl = TYPE_NAME (resolved_type);
5475 TREE_TYPE (class_type) = resolved_type;
5476 return resolved_type_decl;
5479 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5480 are used to report error messages. */
5483 do_resolve_class (enclosing, class_type, decl, cl)
5484 tree enclosing, class_type, decl, cl;
5486 tree new_class_decl;
5488 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5489 it is changed by find_in_imports{_on_demand} and (but it doesn't
5490 really matter) qualify_and_find */
5492 /* 0- Search in the current class as an inner class */
5494 /* Maybe some code here should be added to load the class or
5495 something, at least if the class isn't an inner class and ended
5496 being loaded from class file. FIXME. */
5501 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5502 return new_class_decl;
5504 intermediate = enclosing;
5505 /* Explore enclosing contexts. */
5506 while (INNER_CLASS_DECL_P (intermediate))
5508 intermediate = DECL_CONTEXT (intermediate);
5509 if ((new_class_decl = find_as_inner_class (intermediate,
5511 return new_class_decl;
5514 /* Now go to the upper classes, bail out if necessary. */
5515 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5516 if (!enclosing || enclosing == object_type_node)
5519 if (TREE_CODE (enclosing) == POINTER_TYPE)
5520 enclosing = do_resolve_class (NULL, enclosing, NULL, NULL);
5522 enclosing = TYPE_NAME (enclosing);
5525 /* 1- Check for the type in single imports. This will change
5526 TYPE_NAME() if something relevant is found */
5527 find_in_imports (class_type);
5529 /* 2- And check for the type in the current compilation unit */
5530 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5532 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5533 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5534 load_class (TYPE_NAME (class_type), 0);
5535 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5538 /* 3- Search according to the current package definition */
5539 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5541 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5542 TYPE_NAME (class_type))))
5543 return new_class_decl;
5546 /* 4- Check the import on demands. Don't allow bar.baz to be
5547 imported from foo.* */
5548 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5549 if (find_in_imports_on_demand (class_type))
5552 /* If found in find_in_imports_on_demant, the type has already been
5554 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5555 return new_class_decl;
5557 /* 5- Try with a name qualified with the package name we've seen so far */
5558 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5562 /* If there is a current package (ctxp->package), it's the first
5563 element of package_list and we can skip it. */
5564 for (package = (ctxp->package ?
5565 TREE_CHAIN (package_list) : package_list);
5566 package; package = TREE_CHAIN (package))
5567 if ((new_class_decl = qualify_and_find (class_type,
5568 TREE_PURPOSE (package),
5569 TYPE_NAME (class_type))))
5570 return new_class_decl;
5573 /* 5- Check an other compilation unit that bears the name of type */
5574 load_class (TYPE_NAME (class_type), 0);
5577 cl = lookup_cl (decl);
5579 /* If we don't have a value for CL, then we're being called recursively.
5580 We can't check package access just yet, but it will be taken care of
5584 if (check_pkg_class_access (TYPE_NAME (class_type), cl))
5588 /* 6- Last call for a resolution */
5589 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5593 qualify_and_find (class_type, package, name)
5594 tree class_type, package, name;
5596 tree new_qualified = merge_qualified_name (package, name);
5597 tree new_class_decl;
5599 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5600 load_class (new_qualified, 0);
5601 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5603 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5604 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5605 load_class (new_qualified, 0);
5606 TYPE_NAME (class_type) = new_qualified;
5607 return IDENTIFIER_CLASS_VALUE (new_qualified);
5612 /* Resolve NAME and lay it out (if not done and if not the current
5613 parsed class). Return a decl node. This function is meant to be
5614 called when type resolution is necessary during the walk pass. */
5617 resolve_and_layout (something, cl)
5621 tree decl, decl_type;
5623 /* Don't do that on the current class */
5624 if (something == current_class)
5625 return TYPE_NAME (current_class);
5627 /* Don't do anything for void and other primitive types */
5628 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5631 /* Pointer types can be reall pointer types or fake pointers. When
5632 finding a real pointer, recheck for primitive types */
5633 if (TREE_CODE (something) == POINTER_TYPE)
5635 if (TREE_TYPE (something))
5637 something = TREE_TYPE (something);
5638 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5642 something = TYPE_NAME (something);
5645 /* Don't do anything for arrays of primitive types */
5646 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5647 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5650 /* Something might be a WFL */
5651 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5652 something = EXPR_WFL_NODE (something);
5654 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5655 TYPE_DECL or a real TYPE */
5656 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5657 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5658 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5660 if (!(decl = resolve_no_layout (something, cl)))
5663 /* Resolve and layout if necessary */
5664 decl_type = TREE_TYPE (decl);
5665 layout_class_methods (decl_type);
5667 if (CLASS_FROM_SOURCE_P (decl_type))
5668 java_check_methods (decl);
5669 /* Layout the type if necessary */
5670 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5671 safe_layout_class (decl_type);
5676 /* Resolve a class, returns its decl but doesn't perform any
5677 layout. The current parsing context is saved and restored */
5680 resolve_no_layout (name, cl)
5684 BUILD_PTR_FROM_NAME (ptr, name);
5685 java_parser_context_save_global ();
5686 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5687 java_parser_context_restore_global ();
5692 /* Called when reporting errors. Skip leader '[' in a complex array
5693 type description that failed to be resolved. */
5696 purify_type_name (name)
5699 while (*name && *name == '[')
5704 /* The type CURRENT refers to can't be found. We print error messages. */
5707 complete_class_report_errors (dep)
5712 if (!JDEP_WFL (dep))
5715 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5716 switch (JDEP_KIND (dep))
5720 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5721 purify_type_name (name),
5722 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5726 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5727 purify_type_name (name),
5728 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5730 case JDEP_METHOD: /* Covers arguments */
5732 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5733 purify_type_name (name),
5734 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5735 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5737 case JDEP_METHOD_RETURN: /* Covers return type */
5739 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5740 purify_type_name (name),
5741 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5743 case JDEP_INTERFACE:
5745 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5746 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5747 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5748 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5752 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5753 purify_type_name (IDENTIFIER_POINTER
5754 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5755 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5757 case JDEP_EXCEPTION: /* As specified by `throws' */
5759 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5760 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5763 /* Fix for -Wall. Just break doing nothing. The error will be
5769 /* Return a static string containing the DECL prototype string. If
5770 DECL is a constructor, use the class name instead of the form
5774 get_printable_method_name (decl)
5777 const char *to_return;
5778 tree name = NULL_TREE;
5780 if (DECL_CONSTRUCTOR_P (decl))
5782 name = DECL_NAME (decl);
5783 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
5786 to_return = lang_printable_name (decl, 0);
5787 if (DECL_CONSTRUCTOR_P (decl))
5788 DECL_NAME (decl) = name;
5793 /* Track method being redefined inside the same class. As a side
5794 effect, set DECL_NAME to an IDENTIFIER (prior entering this
5795 function it's a FWL, so we can track errors more accurately.) */
5798 check_method_redefinition (class, method)
5803 /* There's no need to verify <clinit> and finit$ */
5804 if (DECL_CLINIT_P (method) || DECL_FINIT_P (method))
5807 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
5808 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5810 if (redef == method)
5812 if (DECL_NAME (redef) == DECL_NAME (method)
5813 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
5814 && !DECL_ARTIFICIAL (method))
5817 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
5818 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5819 get_printable_method_name (redef));
5826 /* Return 1 if check went ok, 0 otherwise. */
5828 check_abstract_method_definitions (do_interface, class_decl, type)
5830 tree class_decl, type;
5832 tree class = TREE_TYPE (class_decl);
5833 tree method, end_type;
5836 end_type = (do_interface ? object_type_node : type);
5837 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5839 tree other_super, other_method, method_sig, method_name;
5841 int end_type_reached = 0;
5843 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5846 /* Now verify that somewhere in between TYPE and CLASS,
5847 abstract method METHOD gets a non abstract definition
5848 that is inherited by CLASS. */
5850 method_sig = build_java_signature (TREE_TYPE (method));
5851 method_name = DECL_NAME (method);
5852 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5853 method_name = EXPR_WFL_NODE (method_name);
5855 other_super = class;
5857 if (other_super == end_type)
5858 end_type_reached = 1;
5861 for (other_method = TYPE_METHODS (other_super); other_method;
5862 other_method = TREE_CHAIN (other_method))
5864 tree s = build_java_signature (TREE_TYPE (other_method));
5865 tree other_name = DECL_NAME (other_method);
5867 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5868 other_name = EXPR_WFL_NODE (other_name);
5869 if (!DECL_CLINIT_P (other_method)
5870 && !DECL_CONSTRUCTOR_P (other_method)
5871 && method_name == other_name
5873 && !METHOD_ABSTRACT (other_method))
5879 other_super = CLASSTYPE_SUPER (other_super);
5880 } while (!end_type_reached);
5882 /* Report that abstract METHOD didn't find an implementation
5883 that CLASS can use. */
5886 char *t = xstrdup (lang_printable_name
5887 (TREE_TYPE (TREE_TYPE (method)), 0));
5888 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5891 (lookup_cl (class_decl),
5892 "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",
5893 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5894 t, lang_printable_name (method, 0),
5895 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5896 "interface" : "class"),
5897 IDENTIFIER_POINTER (ccn),
5898 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5899 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5905 if (ok && do_interface)
5907 /* Check for implemented interfaces. */
5909 tree vector = TYPE_BINFO_BASETYPES (type);
5910 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5912 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5913 ok = check_abstract_method_definitions (1, class_decl, super);
5920 /* Check that CLASS_DECL somehow implements all inherited abstract
5924 java_check_abstract_method_definitions (class_decl)
5927 tree class = TREE_TYPE (class_decl);
5931 if (CLASS_ABSTRACT (class_decl))
5934 /* Check for inherited types */
5937 super = CLASSTYPE_SUPER (super);
5938 check_abstract_method_definitions (0, class_decl, super);
5939 } while (super != object_type_node);
5941 /* Check for implemented interfaces. */
5942 vector = TYPE_BINFO_BASETYPES (class);
5943 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5945 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5946 check_abstract_method_definitions (1, class_decl, super);
5950 /* Check all the types method DECL uses and return 1 if all of them
5951 are now complete, 0 otherwise. This is used to check whether its
5952 safe to build a method signature or not. */
5955 check_method_types_complete (decl)
5958 tree type = TREE_TYPE (decl);
5961 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5964 args = TYPE_ARG_TYPES (type);
5965 if (TREE_CODE (type) == METHOD_TYPE)
5966 args = TREE_CHAIN (args);
5967 for (; args != end_params_node; args = TREE_CHAIN (args))
5968 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5974 /* Visible interface to check methods contained in CLASS_DECL */
5977 java_check_methods (class_decl)
5980 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
5983 if (CLASS_INTERFACE (class_decl))
5984 java_check_abstract_methods (class_decl);
5986 java_check_regular_methods (class_decl);
5988 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
5991 /* Check all the methods of CLASS_DECL. Methods are first completed
5992 then checked according to regular method existance rules. If no
5993 constructor for CLASS_DECL were encountered, then build its
5997 java_check_regular_methods (class_decl)
6000 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6002 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6003 tree found = NULL_TREE;
6006 /* It is not necessary to check methods defined in java.lang.Object */
6007 if (class == object_type_node)
6010 if (!TYPE_NVIRTUALS (class))
6011 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6013 /* Should take interfaces into account. FIXME */
6014 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6017 tree method_wfl = DECL_FUNCTION_WFL (method);
6020 /* Check for redefinitions */
6021 if (check_method_redefinition (class, method))
6024 /* If we see one constructor a mark so we don't generate the
6025 default one. Also skip other verifications: constructors
6026 can't be inherited hence hiden or overriden */
6027 if (DECL_CONSTRUCTOR_P (method))
6029 saw_constructor = 1;
6033 /* We verify things thrown by the method. They must inherits from
6034 java.lang.Throwable */
6035 for (mthrows = DECL_FUNCTION_THROWS (method);
6036 mthrows; mthrows = TREE_CHAIN (mthrows))
6038 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6040 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6042 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6045 sig = build_java_argument_signature (TREE_TYPE (method));
6046 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6048 /* Inner class can't declare static methods */
6049 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6051 char *t = xstrdup (lang_printable_name (class, 0));
6053 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6054 lang_printable_name (method, 0), t);
6058 /* Nothing overrides or it's a private method. */
6061 if (METHOD_PRIVATE (found))
6067 /* If `found' is declared in an interface, make sure the
6068 modifier matches. */
6069 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6070 && clinit_identifier_node != DECL_NAME (found)
6071 && !METHOD_PUBLIC (method))
6073 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6074 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6075 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6076 lang_printable_name (method, 0),
6077 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6080 /* Can't override a method with the same name and different return
6082 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6085 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6088 "Method `%s' was defined with return type `%s' in class `%s'",
6089 lang_printable_name (found, 0), t,
6091 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6095 aflags = get_access_flags_from_decl (found);
6097 /* Can't override final. Can't override static. */
6098 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6100 /* Static *can* override static */
6101 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6105 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6106 (METHOD_FINAL (found) ? "Final" : "Static"),
6107 lang_printable_name (found, 0),
6108 (METHOD_FINAL (found) ? "final" : "static"),
6110 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6114 /* Static method can't override instance method. */
6115 if (METHOD_STATIC (method))
6119 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6120 lang_printable_name (found, 0),
6122 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6126 /* - Overriding/hiding public must be public
6127 - Overriding/hiding protected must be protected or public
6128 - If the overriden or hidden method has default (package)
6129 access, then the overriding or hiding method must not be
6130 private; otherwise, a compile-time error occurs. If
6131 `found' belongs to an interface, things have been already
6133 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6134 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6135 || (METHOD_PROTECTED (found)
6136 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6137 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6138 && METHOD_PRIVATE (method))))
6142 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6143 (METHOD_PUBLIC (method) ? "public" :
6144 (METHOD_PRIVATE (method) ? "private" : "protected")),
6145 IDENTIFIER_POINTER (DECL_NAME
6146 (TYPE_NAME (DECL_CONTEXT (found)))));
6150 /* Overriding methods must have compatible `throws' clauses on checked
6151 exceptions, if any */
6152 check_throws_clauses (method, method_wfl, found);
6154 /* Inheriting multiple methods with the same signature. FIXME */
6157 if (!TYPE_NVIRTUALS (class))
6158 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6160 /* Search for inherited abstract method not yet implemented in this
6162 java_check_abstract_method_definitions (class_decl);
6164 if (!saw_constructor)
6168 /* Return a non zero value if the `throws' clause of METHOD (if any)
6169 is incompatible with the `throws' clause of FOUND (if any). */
6172 check_throws_clauses (method, method_wfl, found)
6173 tree method, method_wfl, found;
6175 tree mthrows, fthrows;
6177 /* Can't check these things with class loaded from bytecode. FIXME */
6178 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6181 for (mthrows = DECL_FUNCTION_THROWS (method);
6182 mthrows; mthrows = TREE_CHAIN (mthrows))
6184 /* We don't verify unchecked expressions */
6185 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6187 /* Checked expression must be compatible */
6188 for (fthrows = DECL_FUNCTION_THROWS (found);
6189 fthrows; fthrows = TREE_CHAIN (fthrows))
6190 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6195 (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'",
6196 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6197 lang_printable_name (found, 0),
6199 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6204 /* Check abstract method of interface INTERFACE */
6207 java_check_abstract_methods (interface_decl)
6208 tree interface_decl;
6211 tree method, basetype_vec, found;
6212 tree interface = TREE_TYPE (interface_decl);
6214 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6216 /* 2- Check for double definition inside the defining interface */
6217 if (check_method_redefinition (interface, method))
6220 /* 3- Overriding is OK as far as we preserve the return type and
6221 the thrown exceptions (FIXME) */
6222 found = lookup_java_interface_method2 (interface, method);
6226 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6228 (DECL_FUNCTION_WFL (found),
6229 "Method `%s' was defined with return type `%s' in class `%s'",
6230 lang_printable_name (found, 0), t,
6232 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6238 /* 4- Inherited methods can't differ by their returned types */
6239 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6241 n = TREE_VEC_LENGTH (basetype_vec);
6242 for (i = 0; i < n; i++)
6244 tree sub_interface_method, sub_interface;
6245 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6248 sub_interface = BINFO_TYPE (vec_elt);
6249 for (sub_interface_method = TYPE_METHODS (sub_interface);
6250 sub_interface_method;
6251 sub_interface_method = TREE_CHAIN (sub_interface_method))
6253 found = lookup_java_interface_method2 (interface,
6254 sub_interface_method);
6255 if (found && (found != sub_interface_method))
6258 (lookup_cl (sub_interface_method),
6259 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6260 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6261 lang_printable_name (found, 0),
6263 (DECL_NAME (TYPE_NAME
6264 (DECL_CONTEXT (sub_interface_method)))),
6266 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6272 /* Lookup methods in interfaces using their name and partial
6273 signature. Return a matching method only if their types differ. */
6276 lookup_java_interface_method2 (class, method_decl)
6277 tree class, method_decl;
6280 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6285 n = TREE_VEC_LENGTH (basetype_vec);
6286 for (i = 0; i < n; i++)
6288 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6289 if ((BINFO_TYPE (vec_elt) != object_type_node)
6291 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6294 for (i = 0; i < n; i++)
6296 to_return = lookup_java_interface_method2
6297 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6305 /* Lookup method using their name and partial signature. Return a
6306 matching method only if their types differ. */
6309 lookup_java_method2 (clas, method_decl, do_interface)
6310 tree clas, method_decl;
6313 tree method, method_signature, method_name, method_type, name;
6315 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6316 name = DECL_NAME (method_decl);
6317 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6318 EXPR_WFL_NODE (name) : name);
6319 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6321 while (clas != NULL_TREE)
6323 for (method = TYPE_METHODS (clas);
6324 method != NULL_TREE; method = TREE_CHAIN (method))
6326 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6327 tree name = DECL_NAME (method);
6328 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6329 EXPR_WFL_NODE (name) : name) == method_name
6330 && method_sig == method_signature
6331 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6334 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6339 /* Return the line that matches DECL line number, and try its best to
6340 position the column number. Used during error reports. */
6346 static tree cl = NULL_TREE;
6352 if (cl == NULL_TREE)
6354 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6355 ggc_add_tree_root (&cl, 1);
6358 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6359 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6361 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
6362 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6364 found = strstr ((const char *)line,
6365 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6367 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6372 /* Look for a simple name in the single-type import list */
6375 find_name_in_single_imports (name)
6380 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6381 if (TREE_VALUE (node) == name)
6382 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6387 /* Process all single-type import. */
6395 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6397 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6398 char *original_name;
6400 obstack_grow0 (&temporary_obstack,
6401 IDENTIFIER_POINTER (to_be_found),
6402 IDENTIFIER_LENGTH (to_be_found));
6403 original_name = obstack_finish (&temporary_obstack);
6405 /* Don't load twice something already defined. */
6406 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6413 QUALIFIED_P (to_be_found) = 1;
6414 load_class (to_be_found, 0);
6416 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6418 /* We found it, we can bail out */
6419 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6422 /* We haven't found it. Maybe we're trying to access an
6423 inner class. The only way for us to know is to try again
6424 after having dropped a qualifier. If we can't break it further,
6425 we have an error. */
6426 if (breakdown_qualified (&left, NULL, to_be_found))
6431 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6433 parse_error_context (TREE_PURPOSE (import),
6434 "Class or interface `%s' not found in import",
6439 obstack_free (&temporary_obstack, original_name);
6446 /* Possibly find and mark a class imported by a single-type import
6450 find_in_imports (class_type)
6455 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6456 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6458 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6459 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6464 note_possible_classname (name, len)
6469 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6471 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6475 node = ident_subst (name, len, "", '/', '.', "");
6476 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6477 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6481 /* Read a import directory, gathering potential match for further type
6482 references. Indifferently reads a filesystem or a ZIP archive
6486 read_import_dir (wfl)
6489 tree package_id = EXPR_WFL_NODE (wfl);
6490 const char *package_name = IDENTIFIER_POINTER (package_id);
6491 int package_length = IDENTIFIER_LENGTH (package_id);
6493 JCF *saved_jcf = current_jcf;
6498 struct buffer filename[1];
6501 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6503 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6505 BUFFER_INIT (filename);
6506 buffer_grow (filename, package_length + 100);
6508 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6510 const char *entry_name = jcf_path_name (entry);
6511 int entry_length = strlen (entry_name);
6512 if (jcf_path_is_zipfile (entry))
6515 buffer_grow (filename, entry_length);
6516 memcpy (filename->data, entry_name, entry_length - 1);
6517 filename->data[entry_length-1] = '\0';
6518 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6520 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6523 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6524 BUFFER_RESET (filename);
6525 for (k = 0; k < package_length; k++)
6527 char ch = package_name[k];
6528 *filename->ptr++ = ch == '.' ? '/' : ch;
6530 *filename->ptr++ = '/';
6532 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6534 const char *current_entry = ZIPDIR_FILENAME (zipd);
6535 int current_entry_len = zipd->filename_length;
6537 if (current_entry_len >= BUFFER_LENGTH (filename)
6538 && strncmp (filename->data, current_entry,
6539 BUFFER_LENGTH (filename)) != 0)
6541 found |= note_possible_classname (current_entry,
6548 BUFFER_RESET (filename);
6549 buffer_grow (filename, entry_length + package_length + 4);
6550 strcpy (filename->data, entry_name);
6551 filename->ptr = filename->data + entry_length;
6552 for (k = 0; k < package_length; k++)
6554 char ch = package_name[k];
6555 *filename->ptr++ = ch == '.' ? '/' : ch;
6557 *filename->ptr = '\0';
6559 dirp = opendir (filename->data);
6562 *filename->ptr++ = '/';
6567 struct dirent *direntp = readdir (dirp);
6570 d_name = direntp->d_name;
6571 len = strlen (direntp->d_name);
6572 buffer_grow (filename, len+1);
6573 strcpy (filename->ptr, d_name);
6574 found |= note_possible_classname (filename->data + entry_length,
6575 package_length+len+1);
6582 free (filename->data);
6584 /* Here we should have a unified way of retrieving an entry, to be
6588 static int first = 1;
6591 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6596 parse_error_context (wfl, "Package `%s' not found in import",
6598 current_jcf = saved_jcf;
6601 current_jcf = saved_jcf;
6604 /* Possibly find a type in the import on demands specified
6605 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6606 entire list, to detected potential double definitions. */
6609 find_in_imports_on_demand (class_type)
6612 tree node, import, node_to_use = NULL_TREE;
6614 tree cl = NULL_TREE;
6616 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6618 const char *id_name;
6619 obstack_grow (&temporary_obstack,
6620 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6621 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6622 obstack_1grow (&temporary_obstack, '.');
6623 obstack_grow0 (&temporary_obstack,
6624 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6625 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6626 id_name = obstack_finish (&temporary_obstack);
6628 node = maybe_get_identifier (id_name);
6629 if (node && IS_A_CLASSFILE_NAME (node))
6633 cl = TREE_PURPOSE (import);
6641 (TREE_PURPOSE (import),
6642 "Type `%s' also potentially defined in package `%s'",
6643 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6644 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6651 /* Setup lineno so that it refers to the line of the import (in
6652 case we parse a class file and encounter errors */
6654 int saved_lineno = lineno;
6655 lineno = EXPR_WFL_LINENO (cl);
6656 TYPE_NAME (class_type) = node_to_use;
6657 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6658 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6659 /* If there is no DECL set for the class or if the class isn't
6660 loaded and not seen in source yet, the load */
6661 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6662 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6664 load_class (node_to_use, 0);
6665 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6667 lineno = saved_lineno;
6668 if (! INNER_CLASS_P (TREE_TYPE (decl)))
6669 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6671 /* 6.6.1: Inner classes are subject to member access rules. */
6675 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6678 /* Add package NAME to the list of package encountered so far. To
6679 speed up class lookup in do_resolve_class, we make sure a
6680 particular package is added only once. */
6683 register_package (name)
6686 static struct hash_table _pht, *pht = NULL;
6690 hash_table_init (&_pht, hash_newfunc,
6691 java_hash_hash_tree_node, java_hash_compare_tree_node);
6695 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6697 package_list = chainon (package_list, build_tree_list (name, NULL));
6698 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6703 resolve_package (pkg, next)
6707 tree type_name = NULL_TREE;
6708 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6710 /* The trick is to determine when the package name stops and were
6711 the name of something contained in the package starts. Then we
6712 return a fully qualified name of what we want to get. */
6714 /* Do a quick search on well known package names */
6715 if (!strncmp (name, "java.lang.reflect", 17))
6718 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6719 type_name = lookup_package_type (name, 17);
6721 else if (!strncmp (name, "java.lang", 9))
6723 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6724 type_name = lookup_package_type (name, 9);
6727 /* If we found something here, return */
6731 *next = EXPR_WFL_QUALIFICATION (pkg);
6733 /* Try to progressively construct a type name */
6734 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6735 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6736 current; current = TREE_CHAIN (current))
6738 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6739 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6742 /* resolve_package should be used in a loop, hence we
6743 point at this one to naturally process the next one at
6744 the next iteration. */
6753 lookup_package_type (name, from)
6758 const char *sub = &name[from+1];
6759 while (*sub != '.' && *sub)
6761 strncpy (subname, name, sub-name);
6762 subname [sub-name] = '\0';
6763 return get_identifier (subname);
6766 /* Check accessibility of inner classes according to member access rules.
6767 DECL is the inner class, ENCLOSING_DECL is the class from which the
6768 access is being attempted. */
6771 check_inner_class_access (decl, enclosing_decl, cl)
6772 tree decl, enclosing_decl, cl;
6775 tree enclosing_decl_type;
6777 /* We don't issue an error message when CL is null. CL can be null
6778 as a result of processing a JDEP crafted by source_start_java_method
6779 for the purpose of patching its parm decl. But the error would
6780 have been already trapped when fixing the method's signature.
6781 DECL can also be NULL in case of earlier errors. */
6785 enclosing_decl_type = TREE_TYPE (enclosing_decl);
6787 if (CLASS_PRIVATE (decl))
6789 /* Access is permitted only within the body of the top-level
6790 class in which DECL is declared. */
6791 tree top_level = decl;
6792 while (DECL_CONTEXT (top_level))
6793 top_level = DECL_CONTEXT (top_level);
6794 while (DECL_CONTEXT (enclosing_decl))
6795 enclosing_decl = DECL_CONTEXT (enclosing_decl);
6796 if (top_level == enclosing_decl)
6800 else if (CLASS_PROTECTED (decl))
6803 /* Access is permitted from within the same package... */
6804 if (in_same_package (decl, enclosing_decl))
6807 /* ... or from within the body of a subtype of the context in which
6808 DECL is declared. */
6809 decl_context = DECL_CONTEXT (decl);
6810 while (enclosing_decl)
6812 if (CLASS_INTERFACE (decl))
6814 if (interface_of_p (TREE_TYPE (decl_context),
6815 enclosing_decl_type))
6820 /* Eww. The order of the arguments is different!! */
6821 if (inherits_from_p (enclosing_decl_type,
6822 TREE_TYPE (decl_context)))
6825 enclosing_decl = DECL_CONTEXT (enclosing_decl);
6827 access = "protected";
6829 else if (! CLASS_PUBLIC (decl))
6831 /* Access is permitted only from within the same package as DECL. */
6832 if (in_same_package (decl, enclosing_decl))
6834 access = "non-public";
6837 /* Class is public. */
6840 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
6841 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6842 lang_printable_name (decl, 0), access);
6845 /* Accessibility check for top-level classes. If CLASS_NAME is in a foreign
6846 package, it must be PUBLIC. Return 0 if no access violations were found,
6850 check_pkg_class_access (class_name, cl)
6856 if (!IDENTIFIER_CLASS_VALUE (class_name))
6859 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6862 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6864 /* Access to a private class within the same package is
6867 breakdown_qualified (&l, &r, class_name);
6868 if (!QUALIFIED_P (class_name) && !ctxp->package)
6869 /* Both in the empty package. */
6871 if (l == ctxp->package)
6872 /* Both in the same package. */
6876 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
6877 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6878 IDENTIFIER_POINTER (class_name));
6884 /* Local variable declaration. */
6887 declare_local_variables (modifier, type, vlist)
6892 tree decl, current, saved_type;
6893 tree type_wfl = NULL_TREE;
6897 /* Push a new block if statements were seen between the last time we
6898 pushed a block and now. Keep a count of blocks to close */
6899 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
6901 tree b = enter_block ();
6902 BLOCK_IS_IMPLICIT (b) = 1;
6908 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
6909 if (modifier == ACC_FINAL)
6914 (ctxp->modifier_ctx [i],
6915 "Only `final' is allowed as a local variables modifier");
6920 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6921 hold the TYPE value if a new incomplete has to be created (as
6922 opposed to being found already existing and reused). */
6923 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6925 /* If TYPE is fully resolved and we don't have a reference, make one */
6926 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6928 /* Go through all the declared variables */
6929 for (current = vlist, saved_type = type; current;
6930 current = TREE_CHAIN (current), type = saved_type)
6932 tree other, real_type;
6933 tree wfl = TREE_PURPOSE (current);
6934 tree name = EXPR_WFL_NODE (wfl);
6935 tree init = TREE_VALUE (current);
6937 /* Process NAME, as it may specify extra dimension(s) for it */
6938 type = build_array_from_name (type, type_wfl, name, &name);
6940 /* Variable redefinition check */
6941 if ((other = lookup_name_in_blocks (name)))
6943 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6944 DECL_SOURCE_LINE (other));
6948 /* Type adjustment. We may have just readjusted TYPE because
6949 the variable specified more dimensions. Make sure we have
6950 a reference if we can and don't have one already. */
6951 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6953 real_type = GET_REAL_TYPE (type);
6954 /* Never layout this decl. This will be done when its scope
6956 decl = build_decl (VAR_DECL, name, real_type);
6957 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
6958 LOCAL_FINAL (decl) = final_p;
6959 BLOCK_CHAIN_DECL (decl);
6961 /* If doing xreferencing, replace the line number with the WFL
6964 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6966 /* Don't try to use an INIT statement when an error was found */
6967 if (init && java_error_count)
6970 /* Add the initialization function to the current function's code */
6973 /* Name might have been readjusted */
6974 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6975 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6976 java_method_add_stmt (current_function_decl,
6977 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6981 /* Setup dependency the type of the decl */
6985 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
6986 dep = CLASSD_LAST (ctxp->classd_list);
6987 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
6990 SOURCE_FRONTEND_DEBUG (("Defined locals"));
6993 /* Called during parsing. Build decls from argument list. */
6996 source_start_java_method (fndecl)
7006 current_function_decl = fndecl;
7008 /* New scope for the function */
7010 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7011 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7013 tree type = TREE_VALUE (tem);
7014 tree name = TREE_PURPOSE (tem);
7016 /* If type is incomplete. Create an incomplete decl and ask for
7017 the decl to be patched later */
7018 if (INCOMPLETE_TYPE_P (type))
7021 tree real_type = GET_REAL_TYPE (type);
7022 parm_decl = build_decl (PARM_DECL, name, real_type);
7023 type = obtain_incomplete_type (type);
7024 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7025 jdep = CLASSD_LAST (ctxp->classd_list);
7026 JDEP_MISC (jdep) = name;
7027 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7030 parm_decl = build_decl (PARM_DECL, name, type);
7032 /* Remember if a local variable was declared final (via its
7033 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7034 if (ARG_FINAL_P (tem))
7036 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7037 LOCAL_FINAL (parm_decl) = 1;
7040 BLOCK_CHAIN_DECL (parm_decl);
7042 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7043 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7045 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7046 DECL_MAX_LOCALS (current_function_decl) = i;
7049 /* Called during parsing. Creates an artificial method declaration. */
7052 create_artificial_method (class, flags, type, name, args)
7055 tree type, name, args;
7059 java_parser_context_save_global ();
7061 mdecl = make_node (FUNCTION_TYPE);
7062 TREE_TYPE (mdecl) = type;
7063 TYPE_ARG_TYPES (mdecl) = args;
7064 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7065 java_parser_context_restore_global ();
7066 DECL_ARTIFICIAL (mdecl) = 1;
7070 /* Starts the body if an artifical method. */
7073 start_artificial_method_body (mdecl)
7076 DECL_SOURCE_LINE (mdecl) = 1;
7077 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7078 source_start_java_method (mdecl);
7083 end_artificial_method_body (mdecl)
7086 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7087 It has to be evaluated first. (if mdecl is current_function_decl,
7088 we have an undefined behavior if no temporary variable is used.) */
7089 tree b = exit_block ();
7090 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7094 /* Terminate a function and expand its body. */
7097 source_end_java_method ()
7099 tree fndecl = current_function_decl;
7100 int flag_asynchronous_exceptions = asynchronous_exceptions;
7105 java_parser_context_save_global ();
7106 lineno = ctxp->last_ccb_indent1;
7108 /* Set EH language codes */
7109 java_set_exception_lang_code ();
7111 /* Turn function bodies with only a NOP expr null, so they don't get
7112 generated at all and we won't get warnings when using the -W
7114 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7115 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7117 /* Generate function's code */
7118 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7119 && ! flag_emit_class_files
7120 && ! flag_emit_xref)
7121 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7123 /* pop out of its parameters */
7124 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7126 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7128 /* Generate rtl for function exit. */
7129 if (! flag_emit_class_files && ! flag_emit_xref)
7131 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7132 /* Emit catch-finally clauses */
7134 expand_function_end (input_filename, lineno, 0);
7136 /* FIXME: If the current method contains any exception handlers,
7137 force asynchronous_exceptions: this is necessary because signal
7138 handlers in libjava may throw exceptions. This is far from being
7139 a perfect solution, but it's better than doing nothing at all.*/
7141 asynchronous_exceptions = 1;
7143 /* Run the optimizers and output assembler code for this function. */
7144 rest_of_compilation (fndecl);
7147 current_function_decl = NULL_TREE;
7148 java_parser_context_restore_global ();
7149 asynchronous_exceptions = flag_asynchronous_exceptions;
7152 /* Record EXPR in the current function block. Complements compound
7153 expression second operand if necessary. */
7156 java_method_add_stmt (fndecl, expr)
7159 if (!GET_CURRENT_BLOCK (fndecl))
7161 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7165 add_stmt_to_block (b, type, stmt)
7168 tree body = BLOCK_EXPR_BODY (b), c;
7170 if (java_error_count)
7173 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7176 BLOCK_EXPR_BODY (b) = c;
7177 TREE_SIDE_EFFECTS (c) = 1;
7181 /* Add STMT to EXISTING if possible, otherwise create a new
7182 COMPOUND_EXPR and add STMT to it. */
7185 add_stmt_to_compound (existing, type, stmt)
7186 tree existing, type, stmt;
7189 return build (COMPOUND_EXPR, type, existing, stmt);
7194 void java_layout_seen_class_methods ()
7196 tree previous_list = all_class_list;
7197 tree end = NULL_TREE;
7202 for (current = previous_list;
7203 current != end; current = TREE_CHAIN (current))
7204 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7206 if (previous_list != all_class_list)
7208 end = previous_list;
7209 previous_list = all_class_list;
7217 java_reorder_fields ()
7219 static tree stop_reordering = NULL_TREE;
7220 static int initialized_p;
7223 /* Register STOP_REORDERING with the garbage collector. */
7226 ggc_add_tree_root (&stop_reordering, 1);
7230 for (current = gclass_list; current; current = TREE_CHAIN (current))
7232 current_class = TREE_TYPE (TREE_VALUE (current));
7234 if (current_class == stop_reordering)
7237 /* Reverse the fields, but leave the dummy field in front.
7238 Fields are already ordered for Object and Class */
7239 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7240 && current_class != class_type_node)
7242 /* If the dummy field is there, reverse the right fields and
7243 just layout the type for proper fields offset */
7244 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7246 tree fields = TYPE_FIELDS (current_class);
7247 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7248 TYPE_SIZE (current_class) = NULL_TREE;
7250 /* We don't have a dummy field, we need to layout the class,
7251 after having reversed the fields */
7254 TYPE_FIELDS (current_class) =
7255 nreverse (TYPE_FIELDS (current_class));
7256 TYPE_SIZE (current_class) = NULL_TREE;
7260 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7263 /* Layout the methods of all classes loaded in one way or another.
7264 Check methods of source parsed classes. Then reorder the
7265 fields and layout the classes or the type of all source parsed
7269 java_layout_classes ()
7272 int save_error_count = java_error_count;
7274 /* Layout the methods of all classes seen so far */
7275 java_layout_seen_class_methods ();
7276 java_parse_abort_on_error ();
7277 all_class_list = NULL_TREE;
7279 /* Then check the methods of all parsed classes */
7280 for (current = gclass_list; current; current = TREE_CHAIN (current))
7281 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7282 java_check_methods (TREE_VALUE (current));
7283 java_parse_abort_on_error ();
7285 for (current = gclass_list; current; current = TREE_CHAIN (current))
7287 current_class = TREE_TYPE (TREE_VALUE (current));
7288 layout_class (current_class);
7290 /* From now on, the class is considered completely loaded */
7291 CLASS_LOADED_P (current_class) = 1;
7293 /* Error reported by the caller */
7294 if (java_error_count)
7298 /* We might have reloaded classes durign the process of laying out
7299 classes for code generation. We must layout the methods of those
7300 late additions, as constructor checks might use them */
7301 java_layout_seen_class_methods ();
7302 java_parse_abort_on_error ();
7305 /* Expand methods in the current set of classes rememebered for
7309 java_complete_expand_classes ()
7313 do_not_fold = flag_emit_xref;
7315 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7316 if (!INNER_CLASS_DECL_P (current))
7317 java_complete_expand_class (current);
7320 /* Expand the methods found in OUTER, starting first by OUTER's inner
7324 java_complete_expand_class (outer)
7329 set_nested_class_simple_name_value (outer, 1); /* Set */
7331 /* We need to go after all inner classes and start expanding them,
7332 starting with most nested ones. We have to do that because nested
7333 classes might add functions to outer classes */
7335 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7336 inner_list; inner_list = TREE_CHAIN (inner_list))
7337 java_complete_expand_class (TREE_PURPOSE (inner_list));
7339 java_complete_expand_methods (outer);
7340 set_nested_class_simple_name_value (outer, 0); /* Reset */
7343 /* Expand methods registered in CLASS_DECL. The general idea is that
7344 we expand regular methods first. This allows us get an estimate on
7345 how outer context local alias fields are really used so we can add
7346 to the constructor just enough code to initialize them properly (it
7347 also lets us generate finit$ correctly.) Then we expand the
7348 constructors and then <clinit>. */
7351 java_complete_expand_methods (class_decl)
7354 tree clinit, finit, decl, first_decl;
7356 current_class = TREE_TYPE (class_decl);
7358 /* Find whether the class has final variables */
7359 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7360 if (FIELD_FINAL (decl))
7362 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7366 /* Initialize a new constant pool */
7367 init_outgoing_cpool ();
7369 /* Pre-expand <clinit> to figure whether we really need it or
7370 not. If we do need it, we pre-expand the static fields so they're
7371 ready to be used somewhere else. <clinit> will be fully expanded
7372 after we processed the constructors. */
7373 first_decl = TYPE_METHODS (current_class);
7374 clinit = maybe_generate_pre_expand_clinit (current_class);
7376 /* Then generate finit$ (if we need to) because constructor will
7378 if (TYPE_FINIT_STMT_LIST (current_class))
7380 finit = generate_finit (current_class);
7381 java_complete_expand_method (finit);
7384 /* Now do the constructors */
7385 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7389 if (!DECL_CONSTRUCTOR_P (decl))
7392 no_body = !DECL_FUNCTION_BODY (decl);
7393 /* Don't generate debug info on line zero when expanding a
7394 generated constructor. */
7396 restore_line_number_status (1);
7398 /* Reset the final local variable assignment flags */
7399 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7400 reset_final_variable_local_assignment_flag (current_class);
7402 java_complete_expand_method (decl);
7404 /* Check for missed out final variable assignment */
7405 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7406 check_final_variable_local_assignment_flag (current_class, decl);
7409 restore_line_number_status (0);
7412 /* First, do the ordinary methods. */
7413 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7415 /* Skip abstract or native methods -- but do handle native
7416 methods when generating JNI stubs. */
7417 if (METHOD_ABSTRACT (decl)
7418 || (! flag_jni && METHOD_NATIVE (decl))
7419 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7422 if (METHOD_NATIVE (decl))
7424 tree body = build_jni_stub (decl);
7425 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7428 java_complete_expand_method (decl);
7431 /* If there is indeed a <clinit>, fully expand it now */
7434 /* Reset the final local variable assignment flags */
7435 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7436 reset_static_final_variable_assignment_flag (current_class);
7437 /* Prevent the use of `this' inside <clinit> */
7438 ctxp->explicit_constructor_p = 1;
7439 java_complete_expand_method (clinit);
7440 ctxp->explicit_constructor_p = 0;
7441 /* Check for missed out static final variable assignment */
7442 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7443 && !CLASS_INTERFACE (class_decl))
7444 check_static_final_variable_assignment_flag (current_class);
7447 /* We might have generated a class$ that we now want to expand */
7448 if (TYPE_DOT_CLASS (current_class))
7449 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7451 /* Now verify constructor circularity (stop after the first one we
7453 if (!CLASS_INTERFACE (class_decl))
7454 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7455 if (DECL_CONSTRUCTOR_P (decl)
7456 && verify_constructor_circularity (decl, decl))
7459 /* Final check on the initialization of final variables. */
7460 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7462 check_final_variable_global_assignment_flag (current_class);
7463 /* If we have an interface, check for uninitialized fields. */
7464 if (CLASS_INTERFACE (class_decl))
7465 check_static_final_variable_assignment_flag (current_class);
7468 /* Save the constant pool. We'll need to restore it later. */
7469 TYPE_CPOOL (current_class) = outgoing_cpool;
7472 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7473 safely used in some other methods/constructors. */
7476 maybe_generate_pre_expand_clinit (class_type)
7479 tree current, mdecl;
7481 if (!TYPE_CLINIT_STMT_LIST (class_type))
7484 /* Go through all static fields and pre expand them */
7485 for (current = TYPE_FIELDS (class_type); current;
7486 current = TREE_CHAIN (current))
7487 if (FIELD_STATIC (current))
7488 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7490 /* Then build the <clinit> method */
7491 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7492 clinit_identifier_node, end_params_node);
7493 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7495 start_artificial_method_body (mdecl);
7497 /* We process the list of assignment we produced as the result of
7498 the declaration of initialized static field and add them as
7499 statement to the <clinit> method. */
7500 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7501 current = TREE_CHAIN (current))
7503 tree stmt = current;
7504 /* We build the assignment expression that will initialize the
7505 field to its value. There are strict rules on static
7506 initializers (8.5). FIXME */
7507 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7508 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7509 java_method_add_stmt (mdecl, stmt);
7512 end_artificial_method_body (mdecl);
7514 /* Now we want to place <clinit> as the last method (because we need
7515 it at least for interface so that it doesn't interfere with the
7516 dispatch table based lookup. */
7517 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7519 current = TREE_CHAIN (TYPE_METHODS (class_type));
7520 TYPE_METHODS (class_type) = current;
7522 while (TREE_CHAIN (current))
7523 current = TREE_CHAIN (current);
7525 TREE_CHAIN (current) = mdecl;
7526 TREE_CHAIN (mdecl) = NULL_TREE;
7532 /* Analyzes a method body and look for something that isn't a
7536 analyze_clinit_body (bbody)
7540 switch (TREE_CODE (bbody))
7543 bbody = BLOCK_EXPR_BODY (bbody);
7546 case EXPR_WITH_FILE_LOCATION:
7547 bbody = EXPR_WFL_NODE (bbody);
7551 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7553 bbody = TREE_OPERAND (bbody, 1);
7568 /* See whether we could get rid of <clinit>. Criteria are: all static
7569 final fields have constant initial values and the body of <clinit>
7570 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7573 maybe_yank_clinit (mdecl)
7580 if (!DECL_CLINIT_P (mdecl))
7583 /* If the body isn't empty, then we keep <clinit>. Note that if
7584 we're emitting classfiles, this isn't enough not to rule it
7586 fbody = DECL_FUNCTION_BODY (mdecl);
7587 bbody = BLOCK_EXPR_BODY (fbody);
7588 if (bbody && bbody != error_mark_node)
7589 bbody = BLOCK_EXPR_BODY (bbody);
7592 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7595 type = DECL_CONTEXT (mdecl);
7596 current = TYPE_FIELDS (type);
7598 for (current = (current ? TREE_CHAIN (current) : current);
7599 current; current = TREE_CHAIN (current))
7603 /* We're not interested in non static field */
7604 if (!FIELD_STATIC (current))
7607 /* Anything that isn't String or a basic type is ruled out -- or
7608 if we know how to deal with it (when doing things natively) we
7609 should generated an empty <clinit> so that SUID are computed
7611 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7612 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7615 f_init = DECL_INITIAL (current);
7616 /* If we're emitting native code, we want static final fields to
7617 have constant initializers. If we don't meet these
7618 conditions, we keep <clinit> */
7619 if (!flag_emit_class_files
7620 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7622 /* If we're emitting bytecode, we want static fields to have
7623 constant initializers or no initializer. If we don't meet
7624 these conditions, we keep <clinit> */
7625 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7629 /* Now we analyze the method body and look for something that
7630 isn't a MODIFY_EXPR */
7631 if (bbody == empty_stmt_node)
7634 found = analyze_clinit_body (bbody);
7636 if (current || found)
7639 /* Get rid of <clinit> in the class' list of methods */
7640 if (TYPE_METHODS (type) == mdecl)
7641 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7643 for (current = TYPE_METHODS (type); current;
7644 current = TREE_CHAIN (current))
7645 if (TREE_CHAIN (current) == mdecl)
7647 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7655 /* Complete and expand a method. */
7658 java_complete_expand_method (mdecl)
7661 int yank_clinit = 0;
7663 current_function_decl = mdecl;
7664 /* Fix constructors before expanding them */
7665 if (DECL_CONSTRUCTOR_P (mdecl))
7666 fix_constructors (mdecl);
7668 /* Expand functions that have a body */
7669 if (DECL_FUNCTION_BODY (mdecl))
7671 tree fbody = DECL_FUNCTION_BODY (mdecl);
7672 tree block_body = BLOCK_EXPR_BODY (fbody);
7673 tree exception_copy = NULL_TREE;
7676 current_function_decl = mdecl;
7679 fprintf (stderr, " [%s.",
7680 lang_printable_name (DECL_CONTEXT (mdecl), 0));
7681 announce_function (mdecl);
7683 fprintf (stderr, "]");
7685 pushlevel (1); /* Prepare for a parameter push */
7686 ptr = &DECL_ARGUMENTS (mdecl);
7687 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7690 tree next = TREE_CHAIN (tem);
7691 tree type = TREE_TYPE (tem);
7692 if (PROMOTE_PROTOTYPES
7693 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7694 && INTEGRAL_TYPE_P (type))
7695 type = integer_type_node;
7696 DECL_ARG_TYPE (tem) = type;
7697 layout_decl (tem, 0);
7700 ptr = &TREE_CHAIN (tem);
7704 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7705 lineno = DECL_SOURCE_LINE_FIRST (mdecl);
7707 build_result_decl (mdecl);
7710 = (!METHOD_STATIC (mdecl) ?
7711 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7713 /* Purge the `throws' list of unchecked exceptions. If we're
7714 doing xref, save a copy of the list and re-install it
7717 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7719 purge_unchecked_exceptions (mdecl);
7721 /* Install exceptions thrown with `throws' */
7722 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7724 if (block_body != NULL_TREE)
7726 block_body = java_complete_tree (block_body);
7728 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7729 check_for_initialization (block_body);
7730 ctxp->explicit_constructor_p = 0;
7733 BLOCK_EXPR_BODY (fbody) = block_body;
7735 /* If we saw a return but couldn't evaluate it properly, we'll
7736 have an error_mark_node here. */
7737 if (block_body != error_mark_node
7738 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7739 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7741 missing_return_error (current_function_decl);
7743 /* Check wether we could just get rid of clinit, now the picture
7745 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7746 complete_start_java_method (mdecl);
7748 /* Don't go any further if we've found error(s) during the
7750 if (!java_error_count && !yank_clinit)
7751 source_end_java_method ();
7754 if (java_error_count)
7755 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7759 /* Pop the exceptions and sanity check */
7761 if (currently_caught_type_list)
7765 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7771 /* This section of the code deals with accessing enclosing context
7772 fields either directly by using the relevant access to this$<n> or
7773 by invoking an access method crafted for that purpose. */
7775 /* Build the necessary access from an inner class to an outer
7776 class. This routine could be optimized to cache previous result
7777 (decl, current_class and returned access). When an access method
7778 needs to be generated, it always takes the form of a read. It might
7779 be later turned into a write by calling outer_field_access_fix. */
7782 build_outer_field_access (id, decl)
7785 tree access = NULL_TREE;
7786 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7787 tree decl_ctx = DECL_CONTEXT (decl);
7789 /* If the immediate enclosing context of the current class is the
7790 field decl's class or inherits from it; build the access as
7791 `this$<n>.<field>'. Note that we will break the `private' barrier
7792 if we're not emitting bytecodes. */
7793 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
7794 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7796 tree thisn = build_current_thisn (current_class);
7797 access = make_qualified_primary (build_wfl_node (thisn),
7798 id, EXPR_WFL_LINECOL (id));
7800 /* Otherwise, generate access methods to outer this and access the
7801 field (either using an access method or by direct access.) */
7804 int lc = EXPR_WFL_LINECOL (id);
7806 /* Now we chain the required number of calls to the access$0 to
7807 get a hold to the enclosing instance we need, and then we
7808 build the field access. */
7809 access = build_access_to_thisn (current_class, decl_ctx, lc);
7811 /* If the field is private and we're generating bytecode, then
7812 we generate an access method */
7813 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7815 tree name = build_outer_field_access_methods (decl);
7816 access = build_outer_field_access_expr (lc, decl_ctx,
7817 name, access, NULL_TREE);
7819 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7820 Once again we break the `private' access rule from a foreign
7823 access = make_qualified_primary (access, id, lc);
7825 return resolve_expression_name (access, NULL);
7828 /* Return a non zero value if NODE describes an outer field inner
7832 outer_field_access_p (type, decl)
7835 if (!INNER_CLASS_TYPE_P (type)
7836 || TREE_CODE (decl) != FIELD_DECL
7837 || DECL_CONTEXT (decl) == type)
7840 /* If the inner class extends the declaration context of the field
7841 we're try to acces, then this isn't an outer field access */
7842 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7845 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7846 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7848 if (type == DECL_CONTEXT (decl))
7851 if (!DECL_CONTEXT (TYPE_NAME (type)))
7853 /* Before we give up, see whether the field is inherited from
7854 the enclosing context we're considering. */
7855 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7864 /* Return a non zero value if NODE represents an outer field inner
7865 access that was been already expanded. As a side effect, it returns
7866 the name of the field being accessed and the argument passed to the
7867 access function, suitable for a regeneration of the access method
7868 call if necessary. */
7871 outer_field_expanded_access_p (node, name, arg_type, arg)
7872 tree node, *name, *arg_type, *arg;
7876 if (TREE_CODE (node) != CALL_EXPR)
7879 /* Well, gcj generates slightly different tree nodes when compiling
7880 to native or bytecodes. It's the case for function calls. */
7882 if (flag_emit_class_files
7883 && TREE_CODE (node) == CALL_EXPR
7884 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7886 else if (!flag_emit_class_files)
7888 node = TREE_OPERAND (node, 0);
7890 if (node && TREE_OPERAND (node, 0)
7891 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7893 node = TREE_OPERAND (node, 0);
7894 if (TREE_OPERAND (node, 0)
7895 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7896 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7897 (DECL_NAME (TREE_OPERAND (node, 0)))))
7902 if (identified && name && arg_type && arg)
7904 tree argument = TREE_OPERAND (node, 1);
7905 *name = DECL_NAME (TREE_OPERAND (node, 0));
7906 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7907 *arg = TREE_VALUE (argument);
7912 /* Detect in NODE an outer field read access from an inner class and
7913 transform it into a write with RHS as an argument. This function is
7914 called from the java_complete_lhs when an assignment to a LHS can
7918 outer_field_access_fix (wfl, node, rhs)
7919 tree wfl, node, rhs;
7921 tree name, arg_type, arg;
7923 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7925 /* At any rate, check whether we're trying to assign a value to
7927 tree accessed = (JDECL_P (node) ? node :
7928 (TREE_CODE (node) == COMPONENT_REF ?
7929 TREE_OPERAND (node, 1) : node));
7930 if (check_final_assignment (accessed, wfl))
7931 return error_mark_node;
7933 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7934 arg_type, name, arg, rhs);
7935 return java_complete_tree (node);
7940 /* Construct the expression that calls an access method:
7941 <type>.access$<n>(<arg1> [, <arg2>]);
7943 ARG2 can be NULL and will be omitted in that case. It will denote a
7947 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7949 tree type, access_method_name, arg1, arg2;
7951 tree args, cn, access;
7953 args = arg1 ? arg1 :
7954 build_wfl_node (build_current_thisn (current_class));
7955 args = build_tree_list (NULL_TREE, args);
7958 args = tree_cons (NULL_TREE, arg2, args);
7960 access = build_method_invocation (build_wfl_node (access_method_name), args);
7961 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7962 return make_qualified_primary (cn, access, lc);
7966 build_new_access_id ()
7968 static int access_n_counter = 1;
7971 sprintf (buffer, "access$%d", access_n_counter++);
7972 return get_identifier (buffer);
7975 /* Create the static access functions for the outer field DECL. We define a
7977 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7981 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7982 TREE_TYPE (<field>) value$) {
7983 return inst$.field = value$;
7985 We should have a usage flags on the DECL so we can lazily turn the ones
7986 we're using for code generation. FIXME.
7990 build_outer_field_access_methods (decl)
7993 tree id, args, stmt, mdecl;
7995 if (FIELD_INNER_ACCESS_P (decl))
7996 return FIELD_INNER_ACCESS (decl);
7998 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8000 /* Create the identifier and a function named after it. */
8001 id = build_new_access_id ();
8003 /* The identifier is marked as bearing the name of a generated write
8004 access function for outer field accessed from inner classes. */
8005 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8007 /* Create the read access */
8008 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8009 TREE_CHAIN (args) = end_params_node;
8010 stmt = make_qualified_primary (build_wfl_node (inst_id),
8011 build_wfl_node (DECL_NAME (decl)), 0);
8012 stmt = build_return (0, stmt);
8013 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8014 TREE_TYPE (decl), id, args, stmt);
8015 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8017 /* Create the write access method. No write access for final variable */
8018 if (!FIELD_FINAL (decl))
8020 args = build_tree_list (inst_id,
8021 build_pointer_type (DECL_CONTEXT (decl)));
8022 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8023 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8024 stmt = make_qualified_primary (build_wfl_node (inst_id),
8025 build_wfl_node (DECL_NAME (decl)), 0);
8026 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8027 build_wfl_node (wpv_id)));
8028 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8029 TREE_TYPE (decl), id,
8032 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8034 /* Return the access name */
8035 return FIELD_INNER_ACCESS (decl) = id;
8038 /* Build an field access method NAME. */
8041 build_outer_field_access_method (class, type, name, args, body)
8042 tree class, type, name, args, body;
8044 tree saved_current_function_decl, mdecl;
8046 /* Create the method */
8047 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8048 fix_method_argument_names (args, mdecl);
8049 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8051 /* Attach the method body. */
8052 saved_current_function_decl = current_function_decl;
8053 start_artificial_method_body (mdecl);
8054 java_method_add_stmt (mdecl, body);
8055 end_artificial_method_body (mdecl);
8056 current_function_decl = saved_current_function_decl;
8062 /* This section deals with building access function necessary for
8063 certain kinds of method invocation from inner classes. */
8066 build_outer_method_access_method (decl)
8069 tree saved_current_function_decl, mdecl;
8070 tree args = NULL_TREE, call_args = NULL_TREE;
8071 tree carg, id, body, class;
8073 int parm_id_count = 0;
8075 /* Test this abort with an access to a private field */
8076 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8079 /* Check the cache first */
8080 if (DECL_FUNCTION_INNER_ACCESS (decl))
8081 return DECL_FUNCTION_INNER_ACCESS (decl);
8083 class = DECL_CONTEXT (decl);
8085 /* Obtain an access identifier and mark it */
8086 id = build_new_access_id ();
8087 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8089 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8090 /* Create the arguments, as much as the original */
8091 for (; carg && carg != end_params_node;
8092 carg = TREE_CHAIN (carg))
8094 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8095 args = chainon (args, build_tree_list (get_identifier (buffer),
8096 TREE_VALUE (carg)));
8098 args = chainon (args, end_params_node);
8100 /* Create the method */
8101 mdecl = create_artificial_method (class, ACC_STATIC,
8102 TREE_TYPE (TREE_TYPE (decl)), id, args);
8103 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8104 /* There is a potential bug here. We should be able to use
8105 fix_method_argument_names, but then arg names get mixed up and
8106 eventually a constructor will have its this$0 altered and the
8107 outer context won't be assignment properly. The test case is
8109 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8111 /* Attach the method body. */
8112 saved_current_function_decl = current_function_decl;
8113 start_artificial_method_body (mdecl);
8115 /* The actual method invocation uses the same args. When invoking a
8116 static methods that way, we don't want to skip the first
8119 if (!METHOD_STATIC (decl))
8120 carg = TREE_CHAIN (carg);
8121 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8122 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8125 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8127 if (!METHOD_STATIC (decl))
8128 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8130 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8131 body = build_return (0, body);
8132 java_method_add_stmt (mdecl,body);
8133 end_artificial_method_body (mdecl);
8134 current_function_decl = saved_current_function_decl;
8136 /* Back tag the access function so it know what it accesses */
8137 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8139 /* Tag the current method so it knows it has an access generated */
8140 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8144 /* This section of the code deals with building expressions to access
8145 the enclosing instance of an inner class. The enclosing instance is
8146 kept in a generated field called this$<n>, with <n> being the
8147 inner class nesting level (starting from 0.) */
8149 /* Build an access to a given this$<n>, always chaining access call to
8150 others. Access methods to this$<n> are build on the fly if
8151 necessary. This CAN'T be used to solely access this$<n-1> from
8152 this$<n> (which alway yield to special cases and optimization, see
8153 for example build_outer_field_access). */
8156 build_access_to_thisn (from, to, lc)
8160 tree access = NULL_TREE;
8166 access = build_current_thisn (from);
8167 access = build_wfl_node (access);
8171 tree access0_wfl, cn;
8173 maybe_build_thisn_access_method (from);
8174 access0_wfl = build_wfl_node (access0_identifier_node);
8175 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8176 EXPR_WFL_LINECOL (access0_wfl) = lc;
8177 access = build_tree_list (NULL_TREE, access);
8178 access = build_method_invocation (access0_wfl, access);
8179 access = make_qualified_primary (cn, access, lc);
8182 /* if FROM isn't an inter class, that's fine, we've done
8183 enough. What we're looking for can be accessed from there. */
8184 from = DECL_CONTEXT (TYPE_NAME (from));
8187 from = TREE_TYPE (from);
8192 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8193 is returned if nothing needs to be generated. Otherwise, the method
8194 generated and a method decl is returned.
8196 NOTE: These generated methods should be declared in a class file
8197 attribute so that they can't be referred to directly. */
8200 maybe_build_thisn_access_method (type)
8203 tree mdecl, args, stmt, rtype;
8204 tree saved_current_function_decl;
8206 /* If TYPE is a top-level class, no access method is required.
8207 If there already is such an access method, bail out. */
8208 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8211 /* We generate the method. The method looks like:
8212 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8214 args = build_tree_list (inst_id, build_pointer_type (type));
8215 TREE_CHAIN (args) = end_params_node;
8216 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8217 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8218 access0_identifier_node, args);
8219 fix_method_argument_names (args, mdecl);
8220 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8221 stmt = build_current_thisn (type);
8222 stmt = make_qualified_primary (build_wfl_node (inst_id),
8223 build_wfl_node (stmt), 0);
8224 stmt = build_return (0, stmt);
8226 saved_current_function_decl = current_function_decl;
8227 start_artificial_method_body (mdecl);
8228 java_method_add_stmt (mdecl, stmt);
8229 end_artificial_method_body (mdecl);
8230 current_function_decl = saved_current_function_decl;
8232 CLASS_ACCESS0_GENERATED_P (type) = 1;
8237 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8238 the first level of innerclassing. this$1 for the next one, etc...
8239 This function can be invoked with TYPE to NULL, available and then
8240 has to count the parser context. */
8243 build_current_thisn (type)
8246 static int saved_i = -1;
8247 static tree saved_thisn = NULL_TREE;
8248 static tree saved_type = NULL_TREE;
8249 static int saved_type_i = 0;
8250 static int initialized_p;
8255 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8258 ggc_add_tree_root (&saved_thisn, 1);
8259 ggc_add_tree_root (&saved_type, 1);
8265 if (type == saved_type)
8269 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8270 decl; decl = DECL_CONTEXT (decl), i++)
8278 i = list_length (GET_CPC_LIST ())-2;
8283 sprintf (buffer, "this$%d", i);
8285 saved_thisn = get_identifier (buffer);
8289 /* Return the assignement to the hidden enclosing context `this$<n>'
8290 by the second incoming parameter to the innerclass constructor. The
8291 form used is `this.this$<n> = this$<n>;'. */
8294 build_thisn_assign ()
8296 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8298 tree thisn = build_current_thisn (current_class);
8299 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8300 build_wfl_node (thisn), 0);
8301 tree rhs = build_wfl_node (thisn);
8302 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8303 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8309 /* Building the synthetic `class$' used to implement the `.class' 1.1
8310 extension for non primitive types. This method looks like:
8312 static Class class$(String type) throws NoClassDefFoundError
8314 try {return (java.lang.Class.forName (String));}
8315 catch (ClassNotFoundException e) {
8316 throw new NoClassDefFoundError(e.getMessage());}
8320 build_dot_class_method (class)
8323 #define BWF(S) build_wfl_node (get_identifier ((S)))
8324 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8325 tree args, tmp, saved_current_function_decl, mdecl;
8326 tree stmt, throw_stmt, catch, catch_block, try_block;
8327 tree catch_clause_param;
8328 tree class_not_found_exception, no_class_def_found_error;
8330 static tree get_message_wfl, type_parm_wfl;
8332 if (!get_message_wfl)
8334 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8335 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8336 ggc_add_tree_root (&get_message_wfl, 1);
8337 ggc_add_tree_root (&type_parm_wfl, 1);
8340 /* Build the arguments */
8341 args = build_tree_list (get_identifier ("type$"),
8342 build_pointer_type (string_type_node));
8343 TREE_CHAIN (args) = end_params_node;
8345 /* Build the qualified name java.lang.Class.forName */
8346 tmp = MQN (MQN (MQN (BWF ("java"),
8347 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8349 /* For things we have to catch and throw */
8350 class_not_found_exception =
8351 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8352 no_class_def_found_error =
8353 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8354 load_class (class_not_found_exception, 1);
8355 load_class (no_class_def_found_error, 1);
8357 /* Create the "class$" function */
8358 mdecl = create_artificial_method (class, ACC_STATIC,
8359 build_pointer_type (class_type_node),
8360 classdollar_identifier_node, args);
8361 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8362 no_class_def_found_error);
8364 /* We start by building the try block. We need to build:
8365 return (java.lang.Class.forName (type)); */
8366 stmt = build_method_invocation (tmp,
8367 build_tree_list (NULL_TREE, type_parm_wfl));
8368 stmt = build_return (0, stmt);
8369 /* Put it in a block. That's the try block */
8370 try_block = build_expr_block (stmt, NULL_TREE);
8372 /* Now onto the catch block. We start by building the expression
8373 throwing a new exception:
8374 throw new NoClassDefFoundError (_.getMessage); */
8375 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8376 get_message_wfl, 0);
8377 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8379 /* Build new NoClassDefFoundError (_.getMessage) */
8380 throw_stmt = build_new_invocation
8381 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8382 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8384 /* Build the throw, (it's too early to use BUILD_THROW) */
8385 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8387 /* Build the catch block to encapsulate all this. We begin by
8388 building an decl for the catch clause parameter and link it to
8389 newly created block, the catch block. */
8390 catch_clause_param =
8391 build_decl (VAR_DECL, wpv_id,
8392 build_pointer_type (class_not_found_exception));
8393 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8395 /* We initialize the variable with the exception handler. */
8396 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8397 soft_exceptioninfo_call_node);
8398 add_stmt_to_block (catch_block, NULL_TREE, catch);
8400 /* We add the statement throwing the new exception */
8401 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8403 /* Build a catch expression for all this */
8404 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8406 /* Build the try/catch sequence */
8407 stmt = build_try_statement (0, try_block, catch_block);
8409 fix_method_argument_names (args, mdecl);
8410 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8411 saved_current_function_decl = current_function_decl;
8412 start_artificial_method_body (mdecl);
8413 java_method_add_stmt (mdecl, stmt);
8414 end_artificial_method_body (mdecl);
8415 current_function_decl = saved_current_function_decl;
8416 TYPE_DOT_CLASS (class) = mdecl;
8422 build_dot_class_method_invocation (type)
8427 if (TYPE_ARRAY_P (type))
8428 sig_id = build_java_signature (type);
8430 sig_id = DECL_NAME (TYPE_NAME (type));
8432 s = build_string (IDENTIFIER_LENGTH (sig_id),
8433 IDENTIFIER_POINTER (sig_id));
8434 return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8435 build_tree_list (NULL_TREE, s));
8438 /* This section of the code deals with constructor. */
8440 /* Craft a body for default constructor. Patch existing constructor
8441 bodies with call to super() and field initialization statements if
8445 fix_constructors (mdecl)
8448 tree body = DECL_FUNCTION_BODY (mdecl);
8449 tree thisn_assign, compound = NULL_TREE;
8450 tree class_type = DECL_CONTEXT (mdecl);
8454 /* It is an error for the compiler to generate a default
8455 constructor if the superclass doesn't have a constructor that
8456 takes no argument, or the same args for an anonymous class */
8457 if (verify_constructor_super (mdecl))
8459 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8460 tree save = DECL_NAME (mdecl);
8461 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8462 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8464 (lookup_cl (TYPE_NAME (class_type)),
8465 "No constructor matching `%s' found in class `%s'",
8466 lang_printable_name (mdecl, 0), n);
8467 DECL_NAME (mdecl) = save;
8470 /* The constructor body must be crafted by hand. It's the
8471 constructor we defined when we realize we didn't have the
8472 CLASSNAME() constructor */
8473 start_artificial_method_body (mdecl);
8475 /* Insert an assignment to the this$<n> hidden field, if
8477 if ((thisn_assign = build_thisn_assign ()))
8478 java_method_add_stmt (mdecl, thisn_assign);
8480 /* We don't generate a super constructor invocation if we're
8481 compiling java.lang.Object. build_super_invocation takes care
8483 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8485 /* Insert the instance initializer block right here, after the
8486 super invocation. */
8487 add_instance_initializer (mdecl);
8489 end_artificial_method_body (mdecl);
8491 /* Search for an explicit constructor invocation */
8495 tree main_block = BLOCK_EXPR_BODY (body);
8498 switch (TREE_CODE (body))
8501 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8505 case EXPR_WITH_FILE_LOCATION:
8506 body = TREE_OPERAND (body, 0);
8509 body = BLOCK_EXPR_BODY (body);
8515 /* The constructor is missing an invocation of super() */
8517 compound = add_stmt_to_compound (compound, NULL_TREE,
8518 build_super_invocation (mdecl));
8520 /* Generate the assignment to this$<n>, if necessary */
8521 if ((thisn_assign = build_thisn_assign ()))
8522 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8524 /* Insert the instance initializer block right here, after the
8525 super invocation. */
8526 add_instance_initializer (mdecl);
8528 /* Fix the constructor main block if we're adding extra stmts */
8531 compound = add_stmt_to_compound (compound, NULL_TREE,
8532 BLOCK_EXPR_BODY (main_block));
8533 BLOCK_EXPR_BODY (main_block) = compound;
8538 /* Browse constructors in the super class, searching for a constructor
8539 that doesn't take any argument. Return 0 if one is found, 1
8540 otherwise. If the current class is an anonymous inner class, look
8541 for something that has the same signature. */
8544 verify_constructor_super (mdecl)
8547 tree class = CLASSTYPE_SUPER (current_class);
8548 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8554 if (ANONYMOUS_CLASS_P (current_class))
8556 tree mdecl_arg_type;
8557 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8558 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8559 if (DECL_CONSTRUCTOR_P (sdecl))
8562 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8564 arg_type = TREE_CHAIN (arg_type);
8565 for (m_arg_type = mdecl_arg_type;
8566 (arg_type != end_params_node
8567 && m_arg_type != end_params_node);
8568 arg_type = TREE_CHAIN (arg_type),
8569 m_arg_type = TREE_CHAIN (m_arg_type))
8570 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
8573 if (arg_type == end_params_node && m_arg_type == end_params_node)
8579 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8581 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8583 arg = TREE_CHAIN (arg);
8584 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8591 /* Generate code for all context remembered for code generation. */
8594 java_expand_classes ()
8596 int save_error_count = 0;
8597 static struct parser_ctxt *saved_ctxp = NULL;
8599 java_parse_abort_on_error ();
8600 if (!(ctxp = ctxp_for_generation))
8602 java_layout_classes ();
8603 java_parse_abort_on_error ();
8605 saved_ctxp = ctxp_for_generation;
8606 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8608 ctxp = ctxp_for_generation;
8609 input_filename = ctxp->filename;
8610 lang_init_source (2); /* Error msgs have method prototypes */
8611 java_complete_expand_classes (); /* Complete and expand classes */
8612 java_parse_abort_on_error ();
8614 input_filename = main_input_filename;
8616 /* Find anonymous classes and expand their constructor, now they
8618 for (ctxp_for_generation = saved_ctxp;
8619 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8622 ctxp = ctxp_for_generation;
8623 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8625 current_class = TREE_TYPE (current);
8626 if (ANONYMOUS_CLASS_P (current_class))
8629 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8631 if (DECL_CONSTRUCTOR_P (d))
8633 restore_line_number_status (1);
8634 java_complete_expand_method (d);
8635 restore_line_number_status (0);
8636 break; /* We now there are no other ones */
8643 /* If we've found error at that stage, don't try to generate
8644 anything, unless we're emitting xrefs or checking the syntax only
8645 (but not using -fsyntax-only for the purpose of generating
8647 if (java_error_count && !flag_emit_xref
8648 && (!flag_syntax_only && !flag_emit_class_files))
8651 /* Now things are stable, go for generation of the class data. */
8652 for (ctxp_for_generation = saved_ctxp;
8653 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8656 ctxp = ctxp_for_generation;
8657 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8659 current_class = TREE_TYPE (current);
8660 outgoing_cpool = TYPE_CPOOL (current_class);
8661 if (flag_emit_class_files)
8662 write_classfile (current_class);
8664 expand_xref (current_class);
8665 else if (! flag_syntax_only)
8671 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8672 a tree list node containing RIGHT. Fore coming RIGHTs will be
8673 chained to this hook. LOCATION contains the location of the
8674 separating `.' operator. */
8677 make_qualified_primary (primary, right, location)
8678 tree primary, right;
8683 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8684 wfl = build_wfl_wrap (primary, location);
8688 /* If wfl wasn't qualified, we build a first anchor */
8689 if (!EXPR_WFL_QUALIFICATION (wfl))
8690 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8693 /* And chain them */
8694 EXPR_WFL_LINECOL (right) = location;
8695 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8696 PRIMARY_P (wfl) = 1;
8700 /* Simple merge of two name separated by a `.' */
8703 merge_qualified_name (left, right)
8707 if (!left && !right)
8716 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8717 IDENTIFIER_LENGTH (left));
8718 obstack_1grow (&temporary_obstack, '.');
8719 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8720 IDENTIFIER_LENGTH (right));
8721 node = get_identifier (obstack_base (&temporary_obstack));
8722 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8723 QUALIFIED_P (node) = 1;
8727 /* Merge the two parts of a qualified name into LEFT. Set the
8728 location information of the resulting node to LOCATION, usually
8729 inherited from the location information of the `.' operator. */
8732 make_qualified_name (left, right, location)
8736 #ifdef USE_COMPONENT_REF
8737 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8738 EXPR_WFL_LINECOL (node) = location;
8741 tree left_id = EXPR_WFL_NODE (left);
8742 tree right_id = EXPR_WFL_NODE (right);
8745 merge = merge_qualified_name (left_id, right_id);
8747 /* Left wasn't qualified and is now qualified */
8748 if (!QUALIFIED_P (left_id))
8750 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8751 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8752 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8755 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8756 EXPR_WFL_LINECOL (wfl) = location;
8757 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8759 EXPR_WFL_NODE (left) = merge;
8764 /* Extract the last identifier component of the qualified in WFL. The
8765 last identifier is removed from the linked list */
8768 cut_identifier_in_qualified (wfl)
8772 tree previous = NULL_TREE;
8773 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8774 if (!TREE_CHAIN (q))
8777 /* Operating on a non qualified qualified WFL. */
8780 TREE_CHAIN (previous) = NULL_TREE;
8781 return TREE_PURPOSE (q);
8785 /* Resolve the expression name NAME. Return its decl. */
8788 resolve_expression_name (id, orig)
8792 tree name = EXPR_WFL_NODE (id);
8795 /* 6.5.5.1: Simple expression names */
8796 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8798 /* 15.13.1: NAME can appear within the scope of a local variable
8800 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8803 /* 15.13.1: NAME can appear within a class declaration */
8806 decl = lookup_field_wrapper (current_class, name);
8809 tree access = NULL_TREE;
8810 int fs = FIELD_STATIC (decl);
8812 /* If we're accessing an outer scope local alias, make
8813 sure we change the name of the field we're going to
8815 if (FIELD_LOCAL_ALIAS_USED (decl))
8816 name = DECL_NAME (decl);
8818 /* Instance variable (8.3.1.1) can't appear within
8819 static method, static initializer or initializer for
8820 a static variable. */
8821 if (!fs && METHOD_STATIC (current_function_decl))
8823 static_ref_err (id, name, current_class);
8824 return error_mark_node;
8826 /* Instance variables can't appear as an argument of
8827 an explicit constructor invocation */
8828 if (!fs && ctxp->explicit_constructor_p
8829 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
8832 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
8833 return error_mark_node;
8836 /* If we're processing an inner class and we're trying
8837 to access a field belonging to an outer class, build
8838 the access to the field */
8839 if (!fs && outer_field_access_p (current_class, decl))
8841 if (CLASS_STATIC (TYPE_NAME (current_class)))
8843 static_ref_err (id, DECL_NAME (decl), current_class);
8844 return error_mark_node;
8846 return build_outer_field_access (id, decl);
8849 /* Otherwise build what it takes to access the field */
8850 access = build_field_ref ((fs ? NULL_TREE : current_this),
8851 DECL_CONTEXT (decl), name);
8852 if (fs && !flag_emit_class_files && !flag_emit_xref)
8853 access = build_class_init (DECL_CONTEXT (access), access);
8854 /* We may be asked to save the real field access node */
8857 /* And we return what we got */
8860 /* Fall down to error report on undefined variable */
8863 /* 6.5.5.2 Qualified Expression Names */
8868 qualify_ambiguous_name (id);
8869 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8870 /* 15.10.2: Accessing Superclass Members using super */
8871 return resolve_field_access (id, orig, NULL);
8874 /* We've got an error here */
8875 parse_error_context (id, "Undefined variable `%s'",
8876 IDENTIFIER_POINTER (name));
8878 return error_mark_node;
8882 static_ref_err (wfl, field_id, class_type)
8883 tree wfl, field_id, class_type;
8887 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8888 IDENTIFIER_POINTER (field_id),
8889 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8892 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8893 We return something suitable to generate the field access. We also
8894 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8895 recipient's address can be null. */
8898 resolve_field_access (qual_wfl, field_decl, field_type)
8900 tree *field_decl, *field_type;
8904 tree decl, where_found, type_found;
8906 if (resolve_qualified_expression_name (qual_wfl, &decl,
8907 &where_found, &type_found))
8908 return error_mark_node;
8910 /* Resolve the LENGTH field of an array here */
8911 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
8912 && type_found && TYPE_ARRAY_P (type_found)
8913 && ! flag_emit_class_files && ! flag_emit_xref)
8915 tree length = build_java_array_length_access (where_found);
8917 build_java_arraynull_check (type_found, length, int_type_node);
8919 /* In case we're dealing with a static array, we need to
8920 initialize its class before the array length can be fetched.
8921 It's also a good time to create a DECL_RTL for the field if
8922 none already exists, otherwise if the field was declared in a
8923 class found in an external file and hasn't been (and won't
8924 be) accessed for its value, none will be created. */
8925 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8927 build_static_field_ref (where_found);
8928 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8931 /* We might have been trying to resolve field.method(). In which
8932 case, the resolution is over and decl is the answer */
8933 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
8935 else if (JDECL_P (decl))
8937 int static_final_found = 0;
8939 type_found = DECL_CONTEXT (decl);
8940 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
8941 if (CLASS_FINAL_VARIABLE_P (decl)
8942 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
8943 && DECL_INITIAL (decl))
8945 /* When called on a FIELD_DECL of the right (primitive)
8946 type, java_complete_tree will try to substitue the decl
8947 for it's initial value. */
8948 field_ref = java_complete_tree (decl);
8949 static_final_found = 1;
8952 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8953 NULL_TREE : where_found),
8954 type_found, DECL_NAME (decl));
8955 if (field_ref == error_mark_node)
8956 return error_mark_node;
8957 if (is_static && !static_final_found
8958 && !flag_emit_class_files && !flag_emit_xref)
8959 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
8967 *field_type = (QUAL_DECL_TYPE (decl) ?
8968 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
8972 /* If NODE is an access to f static field, strip out the class
8973 initialization part and return the field decl, otherwise, return
8977 strip_out_static_field_access_decl (node)
8980 if (TREE_CODE (node) == COMPOUND_EXPR)
8982 tree op1 = TREE_OPERAND (node, 1);
8983 if (TREE_CODE (op1) == COMPOUND_EXPR)
8985 tree call = TREE_OPERAND (op1, 0);
8986 if (TREE_CODE (call) == CALL_EXPR
8987 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8988 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8989 == soft_initclass_node)
8990 return TREE_OPERAND (op1, 1);
8992 else if (JDECL_P (op1))
8998 /* 6.5.5.2: Qualified Expression Names */
9001 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9003 tree *found_decl, *type_found, *where_found;
9005 int from_type = 0; /* Field search initiated from a type */
9006 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9007 int previous_call_static = 0;
9009 tree decl = NULL_TREE, type = NULL_TREE, q;
9010 /* For certain for of inner class instantiation */
9011 tree saved_current, saved_this;
9012 #define RESTORE_THIS_AND_CURRENT_CLASS \
9013 { current_class = saved_current; current_this = saved_this;}
9015 *type_found = *where_found = NULL_TREE;
9017 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9019 tree qual_wfl = QUAL_WFL (q);
9020 tree ret_decl; /* for EH checking */
9021 int location; /* for EH checking */
9023 /* 15.10.1 Field Access Using a Primary */
9024 switch (TREE_CODE (qual_wfl))
9027 case NEW_CLASS_EXPR:
9028 /* If the access to the function call is a non static field,
9029 build the code to access it. */
9030 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9032 decl = maybe_access_field (decl, *where_found,
9033 DECL_CONTEXT (decl));
9034 if (decl == error_mark_node)
9038 /* And code for the function call */
9039 if (complete_function_arguments (qual_wfl))
9042 /* We might have to setup a new current class and a new this
9043 for the search of an inner class, relative to the type of
9044 a expression resolved as `decl'. The current values are
9045 saved and restored shortly after */
9046 saved_current = current_class;
9047 saved_this = current_this;
9049 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9050 || from_qualified_this))
9052 /* If we still have `from_qualified_this', we have the form
9053 <T>.this.f() and we need to build <T>.this */
9054 if (from_qualified_this)
9056 decl = build_access_to_thisn (current_class, type, 0);
9057 decl = java_complete_tree (decl);
9058 type = TREE_TYPE (TREE_TYPE (decl));
9060 current_class = type;
9061 current_this = decl;
9062 from_qualified_this = 0;
9065 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9066 CALL_USING_SUPER (qual_wfl) = 1;
9067 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9068 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9069 *where_found = patch_method_invocation (qual_wfl, decl, type,
9071 &is_static, &ret_decl);
9072 if (*where_found == error_mark_node)
9074 RESTORE_THIS_AND_CURRENT_CLASS;
9077 *type_found = type = QUAL_DECL_TYPE (*where_found);
9079 /* If we're creating an inner class instance, check for that
9080 an enclosing instance is in scope */
9081 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9082 && INNER_ENCLOSING_SCOPE_CHECK (type))
9085 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9086 lang_printable_name (type, 0),
9087 (!current_this ? "" :
9088 "; an explicit one must be provided when creating this inner class"));
9089 RESTORE_THIS_AND_CURRENT_CLASS;
9093 /* In case we had to change then to resolve a inner class
9094 instantiation using a primary qualified by a `new' */
9095 RESTORE_THIS_AND_CURRENT_CLASS;
9097 /* EH check. No check on access$<n> functions */
9099 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9100 (DECL_NAME (current_function_decl)))
9101 check_thrown_exceptions (location, ret_decl);
9103 /* If the previous call was static and this one is too,
9104 build a compound expression to hold the two (because in
9105 that case, previous function calls aren't transported as
9106 forcoming function's argument. */
9107 if (previous_call_static && is_static)
9109 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9110 TREE_SIDE_EFFECTS (decl) = 1;
9114 previous_call_static = is_static;
9115 decl = *where_found;
9120 case NEW_ARRAY_EXPR:
9121 case NEW_ANONYMOUS_ARRAY_EXPR:
9122 *where_found = decl = java_complete_tree (qual_wfl);
9123 if (decl == error_mark_node)
9125 *type_found = type = QUAL_DECL_TYPE (decl);
9126 CLASS_LOADED_P (type) = 1;
9130 *where_found = decl = java_complete_tree (qual_wfl);
9131 if (decl == error_mark_node)
9133 *type_found = type = QUAL_DECL_TYPE (decl);
9137 case CONDITIONAL_EXPR:
9140 *where_found = decl = java_complete_tree (qual_wfl);
9141 if (decl == error_mark_node)
9143 *type_found = type = QUAL_DECL_TYPE (decl);
9147 /* If the access to the function call is a non static field,
9148 build the code to access it. */
9149 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9151 decl = maybe_access_field (decl, *where_found, type);
9152 if (decl == error_mark_node)
9155 /* And code for the array reference expression */
9156 decl = java_complete_tree (qual_wfl);
9157 if (decl == error_mark_node)
9159 type = QUAL_DECL_TYPE (decl);
9163 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9165 if ((type = patch_string (decl)))
9167 *where_found = QUAL_RESOLUTION (q) = decl;
9168 *type_found = type = TREE_TYPE (decl);
9172 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9174 *where_found = QUAL_RESOLUTION (q) = decl;
9175 *type_found = type = TREE_TYPE (decl);
9179 /* Fix for -Wall Just go to the next statement. Don't
9184 /* If we fall here, we weren't processing a (static) function call. */
9185 previous_call_static = 0;
9187 /* It can be the keyword THIS */
9188 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9193 (wfl, "Keyword `this' used outside allowed context");
9196 if (ctxp->explicit_constructor_p
9197 && type == current_class)
9199 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9202 /* We have to generate code for intermediate acess */
9203 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9205 *where_found = decl = current_this;
9206 *type_found = type = QUAL_DECL_TYPE (decl);
9208 /* We're trying to access the this from somewhere else. Make sure
9209 it's allowed before doing so. */
9212 if (!enclosing_context_p (type, current_class))
9214 char *p = xstrdup (lang_printable_name (type, 0));
9215 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9217 lang_printable_name (current_class, 0));
9221 from_qualified_this = 1;
9222 /* If there's nothing else after that, we need to
9223 produce something now, otherwise, the section of the
9224 code that needs to produce <T>.this will generate
9225 what is necessary. */
9226 if (!TREE_CHAIN (q))
9228 decl = build_access_to_thisn (current_class, type, 0);
9229 *where_found = decl = java_complete_tree (decl);
9230 *type_found = type = TREE_TYPE (decl);
9238 /* 15.10.2 Accessing Superclass Members using SUPER */
9239 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9242 /* Check on the restricted use of SUPER */
9243 if (METHOD_STATIC (current_function_decl)
9244 || current_class == object_type_node)
9247 (wfl, "Keyword `super' used outside allowed context");
9250 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9251 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9252 CLASSTYPE_SUPER (current_class),
9253 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9254 *where_found = decl = java_complete_tree (node);
9255 if (decl == error_mark_node)
9257 *type_found = type = QUAL_DECL_TYPE (decl);
9258 from_super = from_type = 1;
9262 /* 15.13.1: Can't search for field name in packages, so we
9263 assume a variable/class name was meant. */
9264 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9266 tree name = resolve_package (wfl, &q);
9270 *where_found = decl = resolve_no_layout (name, qual_wfl);
9271 /* We want to be absolutely sure that the class is laid
9272 out. We're going to search something inside it. */
9273 *type_found = type = TREE_TYPE (decl);
9274 layout_class (type);
9277 /* Fix them all the way down, if any are left. */
9280 list = TREE_CHAIN (q);
9283 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9284 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9285 list = TREE_CHAIN (list);
9291 if (from_super || from_cast)
9293 ((from_cast ? qual_wfl : wfl),
9294 "No variable `%s' defined in class `%s'",
9295 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9296 lang_printable_name (type, 0));
9299 (qual_wfl, "Undefined variable or class name: `%s'",
9300 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9305 /* We have a type name. It's been already resolved when the
9306 expression was qualified. */
9307 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9309 if (!(decl = QUAL_RESOLUTION (q)))
9310 return 1; /* Error reported already */
9312 /* Sneak preview. If next we see a `new', we're facing a
9313 qualification with resulted in a type being selected
9314 instead of a field. Report the error */
9316 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9318 parse_error_context (qual_wfl, "Undefined variable `%s'",
9319 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9323 if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9326 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9327 java_accstring_lookup (get_access_flags_from_decl (decl)),
9328 GET_TYPE_NAME (type),
9329 IDENTIFIER_POINTER (DECL_NAME (decl)),
9330 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9333 check_deprecation (qual_wfl, decl);
9335 type = TREE_TYPE (decl);
9338 /* We resolve and expression name */
9341 tree field_decl = NULL_TREE;
9343 /* If there exists an early resolution, use it. That occurs
9344 only once and we know that there are more things to
9345 come. Don't do that when processing something after SUPER
9346 (we need more thing to be put in place below */
9347 if (!from_super && QUAL_RESOLUTION (q))
9349 decl = QUAL_RESOLUTION (q);
9352 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9355 *where_found = current_this;
9358 static_ref_err (qual_wfl, DECL_NAME (decl),
9362 if (outer_field_access_p (current_class, decl))
9363 decl = build_outer_field_access (qual_wfl, decl);
9367 *where_found = TREE_TYPE (decl);
9368 if (TREE_CODE (*where_found) == POINTER_TYPE)
9369 *where_found = TREE_TYPE (*where_found);
9374 /* We have to search for a field, knowing the type of its
9375 container. The flag FROM_TYPE indicates that we resolved
9376 the last member of the expression as a type name, which
9377 means that for the resolution of this field, we'll look
9378 for other errors than if it was resolved as a member of
9383 tree field_decl_type; /* For layout */
9385 if (!from_type && !JREFERENCE_TYPE_P (type))
9388 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9389 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9390 lang_printable_name (type, 0),
9391 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9395 field_decl = lookup_field_wrapper (type,
9396 EXPR_WFL_NODE (qual_wfl));
9398 /* Maybe what we're trying to access to is an inner
9399 class, only if decl is a TYPE_DECL. */
9400 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9402 tree ptr, inner_decl;
9404 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9405 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9408 check_inner_class_access (inner_decl, decl, qual_wfl);
9409 type = TREE_TYPE (inner_decl);
9416 if (field_decl == NULL_TREE)
9419 (qual_wfl, "No variable `%s' defined in type `%s'",
9420 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9421 GET_TYPE_NAME (type));
9424 if (field_decl == error_mark_node)
9427 /* Layout the type of field_decl, since we may need
9428 it. Don't do primitive types or loaded classes. The
9429 situation of non primitive arrays may not handled
9430 properly here. FIXME */
9431 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9432 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9434 field_decl_type = TREE_TYPE (field_decl);
9435 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9436 && !CLASS_LOADED_P (field_decl_type)
9437 && !TYPE_ARRAY_P (field_decl_type))
9438 resolve_and_layout (field_decl_type, NULL_TREE);
9439 if (TYPE_ARRAY_P (field_decl_type))
9440 CLASS_LOADED_P (field_decl_type) = 1;
9442 /* Check on accessibility here */
9443 if (not_accessible_p (current_class, field_decl,
9444 TREE_TYPE (decl), from_super))
9448 "Can't access %s field `%s.%s' from `%s'",
9449 java_accstring_lookup
9450 (get_access_flags_from_decl (field_decl)),
9451 GET_TYPE_NAME (type),
9452 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9454 (DECL_NAME (TYPE_NAME (current_class))));
9457 check_deprecation (qual_wfl, field_decl);
9459 /* There are things to check when fields are accessed
9460 from type. There are no restrictions on a static
9461 declaration of the field when it is accessed from an
9463 is_static = FIELD_STATIC (field_decl);
9464 if (!from_super && from_type
9465 && !TYPE_INTERFACE_P (type)
9467 && (current_function_decl
9468 && METHOD_STATIC (current_function_decl)))
9470 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9473 from_cast = from_super = 0;
9475 /* It's an access from a type but it isn't static, we
9476 make it relative to `this'. */
9477 if (!is_static && from_type)
9478 decl = current_this;
9480 /* If we need to generate something to get a proper
9481 handle on what this field is accessed from, do it
9485 decl = maybe_access_field (decl, *where_found, *type_found);
9486 if (decl == error_mark_node)
9490 /* We want to keep the location were found it, and the type
9492 *where_found = decl;
9495 /* Generate the correct expression for field access from
9497 if (from_qualified_this)
9499 field_decl = build_outer_field_access (qual_wfl, field_decl);
9500 from_qualified_this = 0;
9503 /* This is the decl found and eventually the next one to
9508 type = QUAL_DECL_TYPE (decl);
9510 /* Sneak preview. If decl is qualified by a `new', report
9511 the error here to be accurate on the peculiar construct */
9513 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9514 && !JREFERENCE_TYPE_P (type))
9516 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9517 lang_printable_name (type, 0));
9521 /* `q' might have changed due to a after package resolution
9530 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9531 can't be accessed from REFERENCE (a record type). If MEMBER
9532 features a protected access, we then use WHERE which, if non null,
9533 holds the type of MEMBER's access that is checked against
9534 6.6.2.1. This function should be used when decl is a field or a
9538 not_accessible_p (reference, member, where, from_super)
9539 tree reference, member;
9543 int access_flag = get_access_flags_from_decl (member);
9545 /* Inner classes are processed by check_inner_class_access */
9546 if (INNER_CLASS_TYPE_P (reference))
9549 /* Access always granted for members declared public */
9550 if (access_flag & ACC_PUBLIC)
9553 /* Check access on protected members */
9554 if (access_flag & ACC_PROTECTED)
9556 /* Access granted if it occurs from within the package
9557 containing the class in which the protected member is
9559 if (class_in_current_package (DECL_CONTEXT (member)))
9562 /* If accessed with the form `super.member', then access is granted */
9566 /* If where is active, access was made through a
9567 qualifier. Access is granted if the type of the qualifier is
9568 or is a sublass of the type the access made from (6.6.2.1.) */
9569 if (where && !inherits_from_p (where, reference))
9572 /* Otherwise, access is granted if occuring from the class where
9573 member is declared or a subclass of it. Find the right
9574 context to perform the check */
9575 if (PURE_INNER_CLASS_TYPE_P (reference))
9577 while (INNER_CLASS_TYPE_P (reference))
9579 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9581 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9584 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9589 /* Check access on private members. Access is granted only if it
9590 occurs from within the class in which it is declared -- that does
9591 it for innerclasses too. */
9592 if (access_flag & ACC_PRIVATE)
9594 if (reference == DECL_CONTEXT (member))
9596 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9601 /* Default access are permitted only when occuring within the
9602 package in which the type (REFERENCE) is declared. In other words,
9603 REFERENCE is defined in the current package */
9605 return !class_in_current_package (reference);
9607 /* Otherwise, access is granted */
9611 /* Test deprecated decl access. */
9613 check_deprecation (wfl, decl)
9616 const char *file = DECL_SOURCE_FILE (decl);
9617 /* Complain if the field is deprecated and the file it was defined
9618 in isn't compiled at the same time the file which contains its
9620 if (DECL_DEPRECATED (decl)
9621 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9624 switch (TREE_CODE (decl))
9627 strcpy (the, "method");
9630 strcpy (the, "field");
9633 strcpy (the, "class");
9638 parse_warning_context
9639 (wfl, "The %s `%s' in class `%s' has been deprecated",
9640 the, lang_printable_name (decl, 0),
9641 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9645 /* Returns 1 if class was declared in the current package, 0 otherwise */
9648 class_in_current_package (class)
9651 static tree cache = NULL_TREE;
9658 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9660 /* If the current package is empty and the name of CLASS is
9661 qualified, class isn't in the current package. If there is a
9662 current package and the name of the CLASS is not qualified, class
9663 isn't in the current package */
9664 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9667 /* If there is not package and the name of CLASS isn't qualified,
9668 they belong to the same unnamed package */
9669 if (!ctxp->package && !qualified_flag)
9672 /* Compare the left part of the name of CLASS with the package name */
9673 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9674 if (ctxp->package == left)
9676 static int initialized_p;
9677 /* Register CACHE with the garbage collector. */
9680 ggc_add_tree_root (&cache, 1);
9690 /* This function may generate code to access DECL from WHERE. This is
9691 done only if certain conditions meet. */
9694 maybe_access_field (decl, where, type)
9695 tree decl, where, type;
9697 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9698 && !FIELD_STATIC (decl))
9699 decl = build_field_ref (where ? where : current_this,
9700 (type ? type : DECL_CONTEXT (decl)),
9705 /* Build a method invocation, by patching PATCH. If non NULL
9706 and according to the situation, PRIMARY and WHERE may be
9707 used. IS_STATIC is set to 1 if the invoked function is static. */
9710 patch_method_invocation (patch, primary, where, from_super,
9711 is_static, ret_decl)
9712 tree patch, primary, where;
9717 tree wfl = TREE_OPERAND (patch, 0);
9718 tree args = TREE_OPERAND (patch, 1);
9719 tree name = EXPR_WFL_NODE (wfl);
9721 int is_static_flag = 0;
9722 int is_super_init = 0;
9723 tree this_arg = NULL_TREE;
9724 int is_array_clone_call = 0;
9726 /* Should be overriden if everything goes well. Otherwise, if
9727 something fails, it should keep this value. It stop the
9728 evaluation of a bogus assignment. See java_complete_tree,
9729 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9730 evaluating an assignment */
9731 TREE_TYPE (patch) = error_mark_node;
9733 /* Since lookup functions are messing with line numbers, save the
9735 java_parser_context_save_global ();
9737 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9739 /* Resolution of qualified name, excluding constructors */
9740 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9742 tree identifier, identifier_wfl, type, resolved;
9743 /* Extract the last IDENTIFIER of the qualified
9744 expression. This is a wfl and we will use it's location
9745 data during error report. */
9746 identifier_wfl = cut_identifier_in_qualified (wfl);
9747 identifier = EXPR_WFL_NODE (identifier_wfl);
9749 /* Given the context, IDENTIFIER is syntactically qualified
9750 as a MethodName. We need to qualify what's before */
9751 qualify_ambiguous_name (wfl);
9752 resolved = resolve_field_access (wfl, NULL, NULL);
9754 if (resolved == error_mark_node)
9755 PATCH_METHOD_RETURN_ERROR ();
9757 type = GET_SKIP_TYPE (resolved);
9758 resolve_and_layout (type, NULL_TREE);
9760 if (JPRIMITIVE_TYPE_P (type))
9764 "Can't invoke a method on primitive type `%s'",
9765 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9766 PATCH_METHOD_RETURN_ERROR ();
9769 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9770 args = nreverse (args);
9772 /* We're resolving a call from a type */
9773 if (TREE_CODE (resolved) == TYPE_DECL)
9775 if (CLASS_INTERFACE (resolved))
9779 "Can't make static reference to method `%s' in interface `%s'",
9780 IDENTIFIER_POINTER (identifier),
9781 IDENTIFIER_POINTER (name));
9782 PATCH_METHOD_RETURN_ERROR ();
9784 if (list && !METHOD_STATIC (list))
9786 char *fct_name = xstrdup (lang_printable_name (list, 0));
9789 "Can't make static reference to method `%s %s' in class `%s'",
9790 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9791 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9793 PATCH_METHOD_RETURN_ERROR ();
9797 this_arg = primary = resolved;
9799 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
9800 is_array_clone_call = 1;
9802 /* IDENTIFIER_WFL will be used to report any problem further */
9803 wfl = identifier_wfl;
9805 /* Resolution of simple names, names generated after a primary: or
9809 tree class_to_search = NULL_TREE;
9810 int lc; /* Looking for Constructor */
9812 /* We search constructor in their target class */
9813 if (CALL_CONSTRUCTOR_P (patch))
9815 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9816 class_to_search = EXPR_WFL_NODE (wfl);
9817 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9818 this_identifier_node)
9819 class_to_search = NULL_TREE;
9820 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9821 super_identifier_node)
9824 if (CLASSTYPE_SUPER (current_class))
9826 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9829 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
9830 PATCH_METHOD_RETURN_ERROR ();
9834 /* Class to search is NULL if we're searching the current one */
9835 if (class_to_search)
9837 class_to_search = resolve_and_layout (class_to_search, wfl);
9839 if (!class_to_search)
9842 (wfl, "Class `%s' not found in type declaration",
9843 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9844 PATCH_METHOD_RETURN_ERROR ();
9847 /* Can't instantiate an abstract class, but we can
9848 invoke it's constructor. It's use within the `new'
9849 context is denied here. */
9850 if (CLASS_ABSTRACT (class_to_search)
9851 && TREE_CODE (patch) == NEW_CLASS_EXPR)
9854 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9855 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9856 PATCH_METHOD_RETURN_ERROR ();
9859 class_to_search = TREE_TYPE (class_to_search);
9862 class_to_search = current_class;
9865 /* This is a regular search in the local class, unless an
9866 alternate class is specified. */
9869 class_to_search = (where ? where : current_class);
9873 /* NAME is a simple identifier or comes from a primary. Search
9874 in the class whose declaration contain the method being
9876 resolve_and_layout (class_to_search, NULL_TREE);
9878 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
9879 /* Don't continue if no method were found, as the next statement
9880 can't be executed then. */
9882 PATCH_METHOD_RETURN_ERROR ();
9884 if (TYPE_ARRAY_P (class_to_search)
9885 && DECL_NAME (list) == get_identifier ("clone"))
9886 is_array_clone_call = 1;
9888 /* Check for static reference if non static methods */
9889 if (check_for_static_method_reference (wfl, patch, list,
9890 class_to_search, primary))
9891 PATCH_METHOD_RETURN_ERROR ();
9893 /* Check for inner classes creation from illegal contexts */
9894 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9895 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9896 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9899 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9900 lang_printable_name (class_to_search, 0),
9901 (!current_this ? "" :
9902 "; an explicit one must be provided when creating this inner class"));
9903 PATCH_METHOD_RETURN_ERROR ();
9906 /* Non static methods are called with the current object extra
9907 argument. If patch a `new TYPE()', the argument is the value
9908 returned by the object allocator. If method is resolved as a
9909 primary, use the primary otherwise use the current THIS. */
9910 args = nreverse (args);
9911 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
9913 this_arg = primary ? primary : current_this;
9915 /* If we're using an access method, things are different.
9916 There are two familly of cases:
9918 1) We're not generating bytecodes:
9920 - LIST is non static. It's invocation is transformed from
9921 x(a1,...,an) into this$<n>.x(a1,....an).
9922 - LIST is static. It's invocation is transformed from
9923 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9925 2) We're generating bytecodes:
9927 - LIST is non static. It's invocation is transformed from
9928 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9929 - LIST is static. It's invocation is transformed from
9930 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9932 Of course, this$<n> can be abitrary complex, ranging from
9933 this$0 (the immediate outer context) to
9934 access$0(access$0(...(this$0))).
9936 maybe_use_access_method returns a non zero value if the
9937 this_arg has to be moved into the (then generated) stub
9938 argument list. In the meantime, the selected function
9939 might have be replaced by a generated stub. */
9940 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9942 args = tree_cons (NULL_TREE, this_arg, args);
9943 this_arg = NULL_TREE; /* So it doesn't get chained twice */
9948 /* Merge point of all resolution schemes. If we have nothing, this
9949 is an error, already signaled */
9951 PATCH_METHOD_RETURN_ERROR ();
9953 /* Check accessibility, position the is_static flag, build and
9955 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
9956 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
9957 NULL_TREE), from_super)
9958 /* Calls to clone() on array types are permitted as a special-case. */
9959 && !is_array_clone_call)
9961 char *fct_name = (char *) IDENTIFIER_POINTER (DECL_NAME (list));
9962 char *access = java_accstring_lookup (get_access_flags_from_decl (list));
9963 char *klass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
9964 char *refklass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
9965 char *what = (char *) (DECL_CONSTRUCTOR_P (list)
9966 ? "constructor" : "method");
9967 /* FIXME: WFL yields the wrong message here but I don't know
9968 what else to use. */
9969 parse_error_context (wfl,
9970 "Can't access %s %s `%s.%s' from `%s'",
9971 access, what, klass, fct_name, refklass);
9972 PATCH_METHOD_RETURN_ERROR ();
9974 check_deprecation (wfl, list);
9976 /* If invoking a innerclass constructor, there are hidden parameters
9978 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9979 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9981 /* And make sure we add the accessed local variables to be saved
9982 in field aliases. */
9983 args = build_alias_initializer_parameter_list
9984 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9986 /* Secretly pass the current_this/primary as a second argument */
9987 if (primary || current_this)
9990 tree this_type = (current_this ?
9991 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
9992 /* Method's (list) enclosing context */
9993 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
9994 /* If we have a primary, use it. */
9996 extra_arg = primary;
9997 /* The current `this' is an inner class but isn't a direct
9998 enclosing context for the inner class we're trying to
9999 create. Build an access to the proper enclosing context
10001 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10002 && this_type != TREE_TYPE (mec))
10005 extra_arg = build_access_to_thisn (current_class,
10006 TREE_TYPE (mec), 0);
10007 extra_arg = java_complete_tree (extra_arg);
10009 /* Otherwise, just use the current `this' as an enclosing
10012 extra_arg = current_this;
10013 args = tree_cons (NULL_TREE, extra_arg, args);
10016 args = tree_cons (NULL_TREE, integer_zero_node, args);
10019 /* This handles the situation where a constructor invocation needs
10020 to have an enclosing context passed as a second parameter (the
10021 constructor is one of an inner class. We extract it from the
10022 current function. */
10023 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10025 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10028 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10030 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10031 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10035 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10037 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10038 extra_arg = java_complete_tree (extra_arg);
10040 args = tree_cons (NULL_TREE, extra_arg, args);
10043 is_static_flag = METHOD_STATIC (list);
10044 if (! is_static_flag && this_arg != NULL_TREE)
10045 args = tree_cons (NULL_TREE, this_arg, args);
10047 /* In the context of an explicit constructor invocation, we can't
10048 invoke any method relying on `this'. Exceptions are: we're
10049 invoking a static function, primary exists and is not the current
10050 this, we're creating a new object. */
10051 if (ctxp->explicit_constructor_p
10053 && (!primary || primary == current_this)
10054 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10056 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10057 PATCH_METHOD_RETURN_ERROR ();
10059 java_parser_context_restore_global ();
10061 *is_static = is_static_flag;
10062 /* Sometimes, we want the decl of the selected method. Such as for
10066 patch = patch_invoke (patch, list, args);
10067 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10069 tree finit_parms, finit_call;
10071 /* Prepare to pass hidden parameters to finit$, if any. */
10072 finit_parms = build_alias_initializer_parameter_list
10073 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10076 build_method_invocation (build_wfl_node (finit_identifier_node),
10079 /* Generate the code used to initialize fields declared with an
10080 initialization statement and build a compound statement along
10081 with the super constructor invocation. */
10082 patch = build (COMPOUND_EXPR, void_type_node, patch,
10083 java_complete_tree (finit_call));
10084 CAN_COMPLETE_NORMALLY (patch) = 1;
10089 /* Check that we're not trying to do a static reference to a method in
10090 non static method. Return 1 if it's the case, 0 otherwise. */
10093 check_for_static_method_reference (wfl, node, method, where, primary)
10094 tree wfl, node, method, where, primary;
10096 if (METHOD_STATIC (current_function_decl)
10097 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10099 char *fct_name = xstrdup (lang_printable_name (method, 0));
10100 parse_error_context
10101 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10102 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10103 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10110 /* Fix the invocation of *MDECL if necessary in the case of a
10111 invocation from an inner class. *THIS_ARG might be modified
10112 appropriately and an alternative access to *MDECL might be
10116 maybe_use_access_method (is_super_init, mdecl, this_arg)
10118 tree *mdecl, *this_arg;
10121 tree md = *mdecl, ta = *this_arg;
10123 int non_static_context = !METHOD_STATIC (md);
10126 || DECL_CONTEXT (md) == current_class
10127 || !PURE_INNER_CLASS_TYPE_P (current_class)
10128 || DECL_FINIT_P (md))
10131 /* If we're calling a method found in an enclosing class, generate
10132 what it takes to retrieve the right this. Don't do that if we're
10133 invoking a static method. Note that if MD's type is unrelated to
10134 CURRENT_CLASS, then the current this can be used. */
10136 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10138 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10139 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10141 ta = build_current_thisn (current_class);
10142 ta = build_wfl_node (ta);
10149 maybe_build_thisn_access_method (type);
10150 if (inherits_from_p (type, DECL_CONTEXT (md)))
10152 ta = build_access_to_thisn (ctx, type, 0);
10155 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10156 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10159 ta = java_complete_tree (ta);
10162 /* We might have to use an access method to get to MD. We can
10163 break the method access rule as far as we're not generating
10165 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10167 md = build_outer_method_access_method (md);
10174 /* Returnin a non zero value indicates we were doing a non static
10175 method invokation that is now a static invocation. It will have
10176 callee displace `this' to insert it in the regular argument
10178 return (non_static_context && to_return);
10181 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10185 patch_invoke (patch, method, args)
10186 tree patch, method, args;
10189 tree original_call, t, ta;
10190 tree cond = NULL_TREE;
10192 /* Last step for args: convert build-in types. If we're dealing with
10193 a new TYPE() type call, the first argument to the constructor
10194 isn't found in the incoming argument list, but delivered by
10196 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10197 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10198 t = TREE_CHAIN (t);
10199 for (ta = args; t != end_params_node && ta;
10200 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10201 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10202 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10203 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10205 /* Resolve unresolved returned type isses */
10206 t = TREE_TYPE (TREE_TYPE (method));
10207 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10208 resolve_and_layout (TREE_TYPE (t), NULL);
10210 if (flag_emit_class_files || flag_emit_xref)
10214 tree signature = build_java_signature (TREE_TYPE (method));
10215 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10217 case INVOKE_VIRTUAL:
10218 dtable = invoke_build_dtable (0, args);
10219 func = build_invokevirtual (dtable, method);
10222 case INVOKE_NONVIRTUAL:
10223 /* If the object for the method call is null, we throw an
10224 exception. We don't do this if the object is the current
10225 method's `this'. In other cases we just rely on an
10226 optimization pass to eliminate redundant checks. */
10227 if (TREE_VALUE (args) != current_this)
10229 /* We use a SAVE_EXPR here to make sure we only evaluate
10230 the new `self' expression once. */
10231 tree save_arg = save_expr (TREE_VALUE (args));
10232 TREE_VALUE (args) = save_arg;
10233 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10234 null_pointer_node);
10236 /* Fall through. */
10239 case INVOKE_STATIC:
10240 func = build_known_method_ref (method, TREE_TYPE (method),
10241 DECL_CONTEXT (method),
10245 case INVOKE_INTERFACE:
10246 dtable = invoke_build_dtable (1, args);
10247 func = build_invokeinterface (dtable, method);
10254 /* Ensure self_type is initialized, (invokestatic). FIXME */
10255 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10258 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10259 TREE_OPERAND (patch, 0) = func;
10260 TREE_OPERAND (patch, 1) = args;
10261 original_call = patch;
10263 /* We're processing a `new TYPE ()' form. New is called and its
10264 returned value is the first argument to the constructor. We build
10265 a COMPOUND_EXPR and use saved expression so that the overall NEW
10266 expression value is a pointer to a newly created and initialized
10268 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10270 tree class = DECL_CONTEXT (method);
10271 tree c1, saved_new, size, new;
10272 if (flag_emit_class_files || flag_emit_xref)
10274 TREE_TYPE (patch) = build_pointer_type (class);
10277 if (!TYPE_SIZE (class))
10278 safe_layout_class (class);
10279 size = size_in_bytes (class);
10280 new = build (CALL_EXPR, promote_type (class),
10281 build_address_of (alloc_object_node),
10282 tree_cons (NULL_TREE, build_class_ref (class),
10283 build_tree_list (NULL_TREE,
10284 size_in_bytes (class))),
10286 saved_new = save_expr (new);
10287 c1 = build_tree_list (NULL_TREE, saved_new);
10288 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10289 TREE_OPERAND (original_call, 1) = c1;
10290 TREE_SET_CODE (original_call, CALL_EXPR);
10291 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10294 /* If COND is set, then we are building a check to see if the object
10296 if (cond != NULL_TREE)
10298 /* We have to make the `then' branch a compound expression to
10299 make the types turn out right. This seems bizarre. */
10300 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10301 build (COMPOUND_EXPR, TREE_TYPE (patch),
10302 build (CALL_EXPR, void_type_node,
10303 build_address_of (soft_nullpointer_node),
10304 NULL_TREE, NULL_TREE),
10305 (FLOAT_TYPE_P (TREE_TYPE (patch))
10306 ? build_real (TREE_TYPE (patch), dconst0)
10307 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10308 integer_zero_node))),
10310 TREE_SIDE_EFFECTS (patch) = 1;
10317 invocation_mode (method, super)
10321 int access = get_access_flags_from_decl (method);
10324 return INVOKE_SUPER;
10326 if (access & ACC_STATIC)
10327 return INVOKE_STATIC;
10329 /* We have to look for a constructor before we handle nonvirtual
10330 calls; otherwise the constructor will look nonvirtual. */
10331 if (DECL_CONSTRUCTOR_P (method))
10332 return INVOKE_STATIC;
10334 if (access & ACC_FINAL || access & ACC_PRIVATE)
10335 return INVOKE_NONVIRTUAL;
10337 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10338 return INVOKE_NONVIRTUAL;
10340 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10341 return INVOKE_INTERFACE;
10343 return INVOKE_VIRTUAL;
10346 /* Retrieve a refined list of matching methods. It covers the step
10347 15.11.2 (Compile-Time Step 2) */
10350 lookup_method_invoke (lc, cl, class, name, arg_list)
10353 tree class, name, arg_list;
10355 tree atl = end_params_node; /* Arg Type List */
10356 tree method, signature, list, node;
10357 const char *candidates; /* Used for error report */
10360 /* Fix the arguments */
10361 for (node = arg_list; node; node = TREE_CHAIN (node))
10363 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10364 /* Non primitive type may have to be resolved */
10365 if (!JPRIMITIVE_TYPE_P (current_arg))
10366 resolve_and_layout (current_arg, NULL_TREE);
10368 if (TREE_CODE (current_arg) == RECORD_TYPE)
10369 current_arg = promote_type (current_arg);
10370 atl = tree_cons (NULL_TREE, current_arg, atl);
10373 /* Presto. If we're dealing with an anonymous class and a
10374 constructor call, generate the right constructor now, since we
10375 know the arguments' types. */
10377 if (lc && ANONYMOUS_CLASS_P (class))
10378 craft_constructor (TYPE_NAME (class), atl);
10380 /* Find all candidates and then refine the list, searching for the
10381 most specific method. */
10382 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10383 list = find_most_specific_methods_list (list);
10384 if (list && !TREE_CHAIN (list))
10385 return TREE_VALUE (list);
10387 /* Issue an error. List candidates if any. Candidates are listed
10388 only if accessible (non accessible methods may end-up here for
10389 the sake of a better error report). */
10394 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10395 for (current = list; current; current = TREE_CHAIN (current))
10397 tree cm = TREE_VALUE (current);
10398 char string [4096];
10399 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10402 (string, " `%s' in `%s'%s",
10403 get_printable_method_name (cm),
10404 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10405 (TREE_CHAIN (current) ? "\n" : ""));
10406 obstack_grow (&temporary_obstack, string, strlen (string));
10408 obstack_1grow (&temporary_obstack, '\0');
10409 candidates = obstack_finish (&temporary_obstack);
10411 /* Issue the error message */
10412 method = make_node (FUNCTION_TYPE);
10413 TYPE_ARG_TYPES (method) = atl;
10414 signature = build_java_argument_signature (method);
10415 dup = xstrdup (lang_printable_name (class, 0));
10416 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10417 (lc ? "constructor" : "method"),
10418 (lc ? dup : IDENTIFIER_POINTER (name)),
10419 IDENTIFIER_POINTER (signature), dup,
10420 (candidates ? candidates : ""));
10425 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10426 when we're looking for a constructor. */
10429 find_applicable_accessible_methods_list (lc, class, name, arglist)
10431 tree class, name, arglist;
10433 static struct hash_table t, *searched_classes = NULL;
10434 static int search_not_done = 0;
10435 tree list = NULL_TREE, all_list = NULL_TREE;
10437 /* Check the hash table to determine if this class has been searched
10439 if (searched_classes)
10441 if (hash_lookup (searched_classes,
10442 (const hash_table_key) class, FALSE, NULL))
10447 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10448 java_hash_compare_tree_node);
10449 searched_classes = &t;
10453 hash_lookup (searched_classes,
10454 (const hash_table_key) class, TRUE, NULL);
10456 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10458 load_class (class, 1);
10459 safe_layout_class (class);
10462 /* Search interfaces */
10463 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10464 && CLASS_INTERFACE (TYPE_NAME (class)))
10467 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10468 search_applicable_methods_list (lc, TYPE_METHODS (class),
10469 name, arglist, &list, &all_list);
10470 n = TREE_VEC_LENGTH (basetype_vec);
10471 for (i = 1; i < n; i++)
10473 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10476 rlist = find_applicable_accessible_methods_list (lc, t, name,
10478 list = chainon (rlist, list);
10481 /* Search classes */
10485 int seen_inner_class = 0;
10486 search_applicable_methods_list (lc, TYPE_METHODS (class),
10487 name, arglist, &list, &all_list);
10489 /* When looking finit$ or class$, we turn LC to 1 so that we
10490 only search in class. Note that we should have found
10491 something at this point. */
10492 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name))
10499 /* We must search all interfaces of this class */
10502 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10503 int n = TREE_VEC_LENGTH (basetype_vec), i;
10504 for (i = 1; i < n; i++)
10506 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10507 if (t != object_type_node)
10510 = find_applicable_accessible_methods_list (lc, t,
10512 list = chainon (rlist, list);
10517 /* Search enclosing context of inner classes before looking
10519 while (!lc && INNER_CLASS_TYPE_P (class))
10522 seen_inner_class = 1;
10523 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
10524 rlist = find_applicable_accessible_methods_list (lc, class,
10526 list = chainon (rlist, list);
10529 if (!lc && seen_inner_class
10530 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10531 class = CLASSTYPE_SUPER (sc);
10535 /* Search superclass */
10536 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10539 class = CLASSTYPE_SUPER (class);
10540 rlist = find_applicable_accessible_methods_list (lc, class,
10542 list = chainon (rlist, list);
10548 /* We're done. Reset the searched classes list and finally search
10549 java.lang.Object if it wasn't searched already. */
10550 if (!search_not_done)
10553 && TYPE_METHODS (object_type_node)
10554 && !hash_lookup (searched_classes,
10555 (const hash_table_key) object_type_node,
10558 search_applicable_methods_list (lc,
10559 TYPE_METHODS (object_type_node),
10560 name, arglist, &list, &all_list);
10562 hash_table_free (searched_classes);
10563 searched_classes = NULL;
10566 /* Either return the list obtained or all selected (but
10567 inaccessible) methods for better error report. */
10568 return (!list ? all_list : list);
10571 /* Effectively search for the appropriate method in method */
10574 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10576 tree method, name, arglist;
10577 tree *list, *all_list;
10579 for (; method; method = TREE_CHAIN (method))
10581 /* When dealing with constructor, stop here, otherwise search
10583 if (lc && !DECL_CONSTRUCTOR_P (method))
10585 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10586 || (DECL_NAME (method) != name)))
10589 if (argument_types_convertible (method, arglist))
10591 /* Retain accessible methods only */
10592 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10593 method, NULL_TREE, 0))
10594 *list = tree_cons (NULL_TREE, method, *list);
10596 /* Also retain all selected method here */
10597 *all_list = tree_cons (NULL_TREE, method, *list);
10602 /* 15.11.2.2 Choose the Most Specific Method */
10605 find_most_specific_methods_list (list)
10609 int abstract, candidates;
10610 tree current, new_list = NULL_TREE;
10611 for (current = list; current; current = TREE_CHAIN (current))
10614 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10616 for (method = list; method; method = TREE_CHAIN (method))
10618 tree method_v, current_v;
10619 /* Don't test a method against itself */
10620 if (method == current)
10623 method_v = TREE_VALUE (method);
10624 current_v = TREE_VALUE (current);
10626 /* Compare arguments and location where methods where declared */
10627 if (argument_types_convertible (method_v, current_v))
10629 if (valid_method_invocation_conversion_p
10630 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10631 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10632 && enclosing_context_p (DECL_CONTEXT (method_v),
10633 DECL_CONTEXT (current_v))))
10635 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10636 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10637 max = (v > max ? v : max);
10643 /* Review the list and select the maximally specific methods */
10644 for (current = list, abstract = -1, candidates = -1;
10645 current; current = TREE_CHAIN (current))
10646 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10648 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10649 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10653 /* If we have several and they're all abstract, just pick the
10655 if (candidates > 0 && (candidates == abstract))
10657 new_list = nreverse (new_list);
10658 TREE_CHAIN (new_list) = NULL_TREE;
10661 /* We have several (we couldn't find a most specific), all but one
10662 are abstract, we pick the only non abstract one. */
10663 if (candidates > 0 && (candidates == abstract+1))
10665 for (current = new_list; current; current = TREE_CHAIN (current))
10666 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10668 TREE_CHAIN (current) = NULL_TREE;
10669 new_list = current;
10673 /* If we can't find one, lower expectations and try to gather multiple
10674 maximally specific methods */
10675 while (!new_list && max)
10679 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10680 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10687 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10688 converted by method invocation conversion (5.3) to the type of the
10689 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10690 to change less often than M1. */
10693 argument_types_convertible (m1, m2_or_arglist)
10694 tree m1, m2_or_arglist;
10696 static tree m2_arg_value = NULL_TREE;
10697 static tree m2_arg_cache = NULL_TREE;
10698 static int initialized_p;
10700 register tree m1_arg, m2_arg;
10702 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10704 if (!initialized_p)
10706 ggc_add_tree_root (&m2_arg_value, 1);
10707 ggc_add_tree_root (&m2_arg_cache, 1);
10711 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10713 if (m2_arg_value == m2_or_arglist)
10714 m2_arg = m2_arg_cache;
10717 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10719 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10721 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10722 if (!METHOD_STATIC (m2_or_arglist))
10723 m2_arg = TREE_CHAIN (m2_arg);
10726 m2_arg = m2_or_arglist;
10728 m2_arg_value = m2_or_arglist;
10729 m2_arg_cache = m2_arg;
10732 while (m1_arg != end_params_node && m2_arg != end_params_node)
10734 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
10735 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10736 TREE_VALUE (m2_arg)))
10738 m1_arg = TREE_CHAIN (m1_arg);
10739 m2_arg = TREE_CHAIN (m2_arg);
10741 return m1_arg == end_params_node && m2_arg == end_params_node;
10744 /* Qualification routines */
10747 qualify_ambiguous_name (id)
10750 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10751 saved_current_class;
10752 int again, super_found = 0, this_found = 0, new_array_found = 0;
10755 /* We first qualify the first element, then derive qualification of
10756 others based on the first one. If the first element is qualified
10757 by a resolution (field or type), this resolution is stored in the
10758 QUAL_RESOLUTION of the qual element being examined. We need to
10759 save the current_class since the use of SUPER might change the
10761 saved_current_class = current_class;
10762 qual = EXPR_WFL_QUALIFICATION (id);
10765 /* Simple qualified expression feature a qual_wfl that is a
10766 WFL. Expression derived from a primary feature more complicated
10767 things like a CALL_EXPR. Expression from primary need to be
10768 worked out to extract the part on which the qualification will
10770 qual_wfl = QUAL_WFL (qual);
10771 switch (TREE_CODE (qual_wfl))
10774 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10775 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10777 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10778 qual_wfl = QUAL_WFL (qual);
10781 case NEW_ARRAY_EXPR:
10782 case NEW_ANONYMOUS_ARRAY_EXPR:
10783 qual = TREE_CHAIN (qual);
10784 again = new_array_found = 1;
10788 case NEW_CLASS_EXPR:
10789 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10792 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10793 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10796 qual = TREE_CHAIN (qual);
10797 qual_wfl = QUAL_WFL (qual);
10799 case CLASS_LITERAL:
10800 qual = TREE_CHAIN (qual);
10801 qual_wfl = QUAL_WFL (qual);
10804 /* Fix for -Wall. Just break doing nothing */
10808 ptr_type = current_class;
10810 code = TREE_CODE (qual_wfl);
10812 /* Pos evaluation: non WFL leading expression nodes */
10813 if (code == CONVERT_EXPR
10814 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10815 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10817 else if (code == INTEGER_CST)
10820 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
10821 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10822 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10824 else if (code == TREE_LIST)
10825 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10827 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10828 || code == PLUS_EXPR)
10830 qual = TREE_CHAIN (qual);
10831 qual_wfl = QUAL_WFL (qual);
10836 name = EXPR_WFL_NODE (qual_wfl);
10839 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10844 /* If we have a THIS (from a primary), we set the context accordingly */
10845 if (name == this_identifier_node)
10847 /* This isn't really elegant. One more added irregularity
10848 before I start using COMPONENT_REF (hopefully very soon.) */
10849 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10850 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10851 EXPR_WITH_FILE_LOCATION
10852 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10853 this_identifier_node)
10855 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10856 qual = EXPR_WFL_QUALIFICATION (qual);
10858 qual = TREE_CHAIN (qual);
10859 qual_wfl = QUAL_WFL (qual);
10860 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10863 name = EXPR_WFL_NODE (qual_wfl);
10866 /* If we have a SUPER, we set the context accordingly */
10867 if (name == super_identifier_node)
10869 current_class = CLASSTYPE_SUPER (ptr_type);
10870 /* Check that there is such a thing as a super class. If not,
10871 return. The error will be caught later on, during the
10873 if (!current_class)
10875 current_class = saved_current_class;
10878 qual = TREE_CHAIN (qual);
10879 /* Do one more interation to set things up */
10880 super_found = again = 1;
10884 /* If name appears within the scope of a local variable declaration
10885 or parameter declaration, then it is an expression name. We don't
10886 carry this test out if we're in the context of the use of SUPER
10888 if (!this_found && !super_found
10889 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10890 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
10892 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10893 QUAL_RESOLUTION (qual) = decl;
10896 /* If within the class/interface NAME was found to be used there
10897 exists a (possibly inherited) field named NAME, then this is an
10898 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10899 address length, it is OK. */
10900 else if ((decl = lookup_field_wrapper (ptr_type, name))
10901 || (new_array_found && name == length_identifier_node))
10903 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10904 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
10907 /* We reclassify NAME as yielding to a type name resolution if:
10908 - NAME is a class/interface declared within the compilation
10909 unit containing NAME,
10910 - NAME is imported via a single-type-import declaration,
10911 - NAME is declared in an another compilation unit of the package
10912 of the compilation unit containing NAME,
10913 - NAME is declared by exactly on type-import-on-demand declaration
10914 of the compilation unit containing NAME.
10915 - NAME is actually a STRING_CST. */
10916 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10917 || (decl = resolve_and_layout (name, NULL_TREE)))
10919 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10920 QUAL_RESOLUTION (qual) = decl;
10923 /* Method call, array references and cast are expression name */
10924 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
10925 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10926 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
10927 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10929 /* Check here that NAME isn't declared by more than one
10930 type-import-on-demand declaration of the compilation unit
10931 containing NAME. FIXME */
10933 /* Otherwise, NAME is reclassified as a package name */
10935 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10937 /* Propagate the qualification accross other components of the
10939 for (qual = TREE_CHAIN (qual); qual;
10940 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10942 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10943 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10945 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10948 /* Store the global qualification for the ambiguous part of ID back
10950 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10951 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10952 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10953 RESOLVE_TYPE_NAME_P (id) = 1;
10954 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10955 RESOLVE_PACKAGE_NAME_P (id) = 1;
10957 /* Restore the current class */
10958 current_class = saved_current_class;
10962 breakdown_qualified (left, right, source)
10963 tree *left, *right, source;
10966 int l = IDENTIFIER_LENGTH (source);
10968 base = alloca (l + 1);
10969 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10971 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10973 while (*p != '.' && p != base)
10976 /* We didn't find a '.'. Return an error */
10982 *right = get_identifier (p+1);
10983 *left = get_identifier (base);
10988 /* Return TRUE if two classes are from the same package. */
10991 in_same_package (name1, name2)
10998 if (TREE_CODE (name1) == TYPE_DECL)
10999 name1 = DECL_NAME (name1);
11000 if (TREE_CODE (name2) == TYPE_DECL)
11001 name2 = DECL_NAME (name2);
11003 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11004 /* One in empty package. */
11007 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11008 /* Both in empty package. */
11011 breakdown_qualified (&pkg1, &tmp, name1);
11012 breakdown_qualified (&pkg2, &tmp, name2);
11014 return (pkg1 == pkg2);
11017 /* Patch tree nodes in a function body. When a BLOCK is found, push
11018 local variable decls if present.
11019 Same as java_complete_lhs, but does resolve static finals to values. */
11022 java_complete_tree (node)
11025 node = java_complete_lhs (node);
11026 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11027 && DECL_INITIAL (node) != NULL_TREE
11028 && !flag_emit_xref)
11030 tree value = DECL_INITIAL (node);
11031 DECL_INITIAL (node) = NULL_TREE;
11032 value = fold_constant_for_init (value, node);
11033 DECL_INITIAL (node) = value;
11034 if (value != NULL_TREE)
11036 /* fold_constant_for_init sometimes widen the original type
11037 of the constant (i.e. byte to int.) It's not desirable,
11038 especially if NODE is a function argument. */
11039 if (TREE_CODE (value) == INTEGER_CST
11040 && TREE_TYPE (node) != TREE_TYPE (value))
11041 return convert (TREE_TYPE (node), value);
11046 DECL_FIELD_FINAL_IUD (node) = 0;
11052 java_stabilize_reference (node)
11055 if (TREE_CODE (node) == COMPOUND_EXPR)
11057 tree op0 = TREE_OPERAND (node, 0);
11058 tree op1 = TREE_OPERAND (node, 1);
11059 TREE_OPERAND (node, 0) = save_expr (op0);
11060 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11063 return stabilize_reference (node);
11066 /* Patch tree nodes in a function body. When a BLOCK is found, push
11067 local variable decls if present.
11068 Same as java_complete_tree, but does not resolve static finals to values. */
11071 java_complete_lhs (node)
11074 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11077 /* CONVERT_EXPR always has its type set, even though it needs to be
11079 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11082 /* The switch block implements cases processing container nodes
11083 first. Contained nodes are always written back. Leaves come
11084 next and return a value. */
11085 switch (TREE_CODE (node))
11089 /* 1- Block section.
11090 Set the local values on decl names so we can identify them
11091 faster when they're referenced. At that stage, identifiers
11092 are legal so we don't check for declaration errors. */
11093 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11095 DECL_CONTEXT (cn) = current_function_decl;
11096 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11098 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11099 CAN_COMPLETE_NORMALLY (node) = 1;
11102 tree stmt = BLOCK_EXPR_BODY (node);
11104 int error_seen = 0;
11105 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11107 /* Re-order from (((A; B); C); ...; Z) to
11108 (A; (B; (C ; (...; Z)))).
11109 This makes it easier to scan the statements left-to-right
11110 without using recursion (which might overflow the stack
11111 if the block has many statements. */
11114 tree left = TREE_OPERAND (stmt, 0);
11115 if (TREE_CODE (left) != COMPOUND_EXPR)
11117 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11118 TREE_OPERAND (left, 1) = stmt;
11121 BLOCK_EXPR_BODY (node) = stmt;
11124 /* Now do the actual complete, without deep recursion for
11126 ptr = &BLOCK_EXPR_BODY (node);
11127 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11128 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11130 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11131 tree *next = &TREE_OPERAND (*ptr, 1);
11132 TREE_OPERAND (*ptr, 0) = cur;
11133 if (cur == empty_stmt_node)
11135 /* Optimization; makes it easier to detect empty bodies.
11136 Most useful for <clinit> with all-constant initializer. */
11140 if (TREE_CODE (cur) == ERROR_MARK)
11142 else if (! CAN_COMPLETE_NORMALLY (cur))
11147 if (TREE_CODE (wfl_op2) == BLOCK)
11148 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11149 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11150 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11154 if (TREE_CODE (wfl_op2) != CASE_EXPR
11155 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11156 unreachable_stmt_error (*ptr);
11160 *ptr = java_complete_tree (*ptr);
11162 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11163 return error_mark_node;
11164 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11166 /* Turn local bindings to null */
11167 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11168 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11170 TREE_TYPE (node) = void_type_node;
11173 /* 2- They are expressions but ultimately deal with statements */
11176 wfl_op1 = TREE_OPERAND (node, 0);
11177 COMPLETE_CHECK_OP_0 (node);
11178 /* 14.19 A throw statement cannot complete normally. */
11179 CAN_COMPLETE_NORMALLY (node) = 0;
11180 return patch_throw_statement (node, wfl_op1);
11182 case SYNCHRONIZED_EXPR:
11183 wfl_op1 = TREE_OPERAND (node, 0);
11184 return patch_synchronized_statement (node, wfl_op1);
11187 return patch_try_statement (node);
11189 case TRY_FINALLY_EXPR:
11190 COMPLETE_CHECK_OP_0 (node);
11191 COMPLETE_CHECK_OP_1 (node);
11192 CAN_COMPLETE_NORMALLY (node)
11193 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11194 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11195 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11198 case CLEANUP_POINT_EXPR:
11199 COMPLETE_CHECK_OP_0 (node);
11200 TREE_TYPE (node) = void_type_node;
11201 CAN_COMPLETE_NORMALLY (node) =
11202 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11205 case WITH_CLEANUP_EXPR:
11206 COMPLETE_CHECK_OP_0 (node);
11207 COMPLETE_CHECK_OP_2 (node);
11208 CAN_COMPLETE_NORMALLY (node) =
11209 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11210 TREE_TYPE (node) = void_type_node;
11213 case LABELED_BLOCK_EXPR:
11214 PUSH_LABELED_BLOCK (node);
11215 if (LABELED_BLOCK_BODY (node))
11216 COMPLETE_CHECK_OP_1 (node);
11217 TREE_TYPE (node) = void_type_node;
11218 POP_LABELED_BLOCK ();
11220 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11222 LABELED_BLOCK_BODY (node) = NULL_TREE;
11223 CAN_COMPLETE_NORMALLY (node) = 1;
11225 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11226 CAN_COMPLETE_NORMALLY (node) = 1;
11229 case EXIT_BLOCK_EXPR:
11230 /* We don't complete operand 1, because it's the return value of
11231 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11232 return patch_bc_statement (node);
11235 cn = java_complete_tree (TREE_OPERAND (node, 0));
11236 if (cn == error_mark_node)
11239 /* First, the case expression must be constant. Values of final
11240 fields are accepted. */
11242 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11243 && JDECL_P (TREE_OPERAND (cn, 1))
11244 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11245 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11247 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11248 TREE_OPERAND (cn, 1));
11251 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11253 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11254 parse_error_context (node, "Constant expression required");
11255 return error_mark_node;
11258 nn = ctxp->current_loop;
11260 /* It must be assignable to the type of the switch expression. */
11261 if (!try_builtin_assignconv (NULL_TREE,
11262 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11264 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11265 parse_error_context
11267 "Incompatible type for case. Can't convert `%s' to `int'",
11268 lang_printable_name (TREE_TYPE (cn), 0));
11269 return error_mark_node;
11272 cn = fold (convert (int_type_node, cn));
11274 /* Multiple instance of a case label bearing the same
11275 value is checked during code generation. The case
11276 expression is allright so far. */
11277 if (TREE_CODE (cn) == VAR_DECL)
11278 cn = DECL_INITIAL (cn);
11279 TREE_OPERAND (node, 0) = cn;
11280 TREE_TYPE (node) = void_type_node;
11281 CAN_COMPLETE_NORMALLY (node) = 1;
11282 TREE_SIDE_EFFECTS (node) = 1;
11286 nn = ctxp->current_loop;
11287 /* Only one default label is allowed per switch statement */
11288 if (SWITCH_HAS_DEFAULT (nn))
11290 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11291 parse_error_context (wfl_operator,
11292 "Duplicate case label: `default'");
11293 return error_mark_node;
11296 SWITCH_HAS_DEFAULT (nn) = 1;
11297 TREE_TYPE (node) = void_type_node;
11298 TREE_SIDE_EFFECTS (node) = 1;
11299 CAN_COMPLETE_NORMALLY (node) = 1;
11305 /* Check whether the loop was enclosed in a labeled
11306 statement. If not, create one, insert the loop in it and
11308 nn = patch_loop_statement (node);
11310 /* Anyways, walk the body of the loop */
11311 if (TREE_CODE (node) == LOOP_EXPR)
11312 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11313 /* Switch statement: walk the switch expression and the cases */
11315 node = patch_switch_statement (node);
11317 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11318 nn = error_mark_node;
11321 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11322 /* If we returned something different, that's because we
11323 inserted a label. Pop the label too. */
11326 if (CAN_COMPLETE_NORMALLY (node))
11327 CAN_COMPLETE_NORMALLY (nn) = 1;
11328 POP_LABELED_BLOCK ();
11335 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11336 return patch_exit_expr (node);
11340 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11341 if (TREE_OPERAND (node, 0) == error_mark_node)
11342 return error_mark_node;
11343 /* then-else branches */
11344 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11345 if (TREE_OPERAND (node, 1) == error_mark_node)
11346 return error_mark_node;
11347 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11348 if (TREE_OPERAND (node, 2) == error_mark_node)
11349 return error_mark_node;
11350 return patch_if_else_statement (node);
11353 case CONDITIONAL_EXPR:
11355 wfl_op1 = TREE_OPERAND (node, 0);
11356 COMPLETE_CHECK_OP_0 (node);
11357 wfl_op2 = TREE_OPERAND (node, 1);
11358 COMPLETE_CHECK_OP_1 (node);
11359 wfl_op3 = TREE_OPERAND (node, 2);
11360 COMPLETE_CHECK_OP_2 (node);
11361 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11363 /* 3- Expression section */
11364 case COMPOUND_EXPR:
11365 wfl_op2 = TREE_OPERAND (node, 1);
11366 TREE_OPERAND (node, 0) = nn =
11367 java_complete_tree (TREE_OPERAND (node, 0));
11368 if (wfl_op2 == empty_stmt_node)
11369 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11372 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11374 /* An unreachable condition in a do-while statement
11375 is *not* (technically) an unreachable statement. */
11377 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11378 nn = EXPR_WFL_NODE (nn);
11379 if (TREE_CODE (nn) != EXIT_EXPR)
11381 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11382 parse_error_context (wfl_operator, "Unreachable statement");
11385 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11386 if (TREE_OPERAND (node, 1) == error_mark_node)
11387 return error_mark_node;
11388 CAN_COMPLETE_NORMALLY (node)
11389 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11391 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11395 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11396 return patch_return (node);
11398 case EXPR_WITH_FILE_LOCATION:
11399 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11400 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11403 node = resolve_expression_name (node, NULL);
11404 if (node == error_mark_node)
11406 /* Keep line number information somewhere were it doesn't
11407 disrupt the completion process. */
11408 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11410 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11411 TREE_OPERAND (node, 1) = wfl;
11413 CAN_COMPLETE_NORMALLY (node) = 1;
11418 int save_lineno = lineno;
11419 lineno = EXPR_WFL_LINENO (node);
11420 body = java_complete_tree (EXPR_WFL_NODE (node));
11421 lineno = save_lineno;
11422 EXPR_WFL_NODE (node) = body;
11423 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11424 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11425 if (body == empty_stmt_node || TREE_CONSTANT (body))
11427 /* Makes it easier to constant fold, detect empty bodies. */
11430 if (body == error_mark_node)
11432 /* Its important for the evaluation of assignment that
11433 this mark on the TREE_TYPE is propagated. */
11434 TREE_TYPE (node) = error_mark_node;
11435 return error_mark_node;
11438 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11443 case NEW_ARRAY_EXPR:
11444 /* Patch all the dimensions */
11446 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11448 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11449 tree dim = convert (int_type_node,
11450 java_complete_tree (TREE_VALUE (cn)));
11451 if (dim == error_mark_node)
11458 TREE_VALUE (cn) = dim;
11459 /* Setup the location of the current dimension, for
11460 later error report. */
11461 TREE_PURPOSE (cn) =
11462 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11463 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11466 /* They complete the array creation expression, if no errors
11468 CAN_COMPLETE_NORMALLY (node) = 1;
11469 return (flag ? error_mark_node
11470 : force_evaluation_order (patch_newarray (node)));
11472 case NEW_ANONYMOUS_ARRAY_EXPR:
11473 /* Create the array type if necessary. */
11474 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11476 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11477 if (!(type = resolve_type_during_patch (type)))
11478 return error_mark_node;
11479 type = build_array_from_name (type, NULL_TREE,
11480 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11481 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11483 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11484 ANONYMOUS_ARRAY_INITIALIZER (node));
11485 if (node == error_mark_node)
11486 return error_mark_node;
11487 CAN_COMPLETE_NORMALLY (node) = 1;
11490 case NEW_CLASS_EXPR:
11492 /* Complete function's argument(s) first */
11493 if (complete_function_arguments (node))
11494 return error_mark_node;
11497 tree decl, wfl = TREE_OPERAND (node, 0);
11498 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11499 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11500 super_identifier_node);
11502 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11503 from_super, 0, &decl);
11504 if (node == error_mark_node)
11505 return error_mark_node;
11507 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11508 /* If we call this(...), register signature and positions */
11510 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11511 tree_cons (wfl, decl,
11512 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11513 CAN_COMPLETE_NORMALLY (node) = 1;
11514 return force_evaluation_order (node);
11518 /* Save potential wfls */
11519 wfl_op1 = TREE_OPERAND (node, 0);
11520 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11522 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11523 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11524 && DECL_INITIAL (nn) != NULL_TREE)
11528 value = fold_constant_for_init (nn, nn);
11530 if (value != NULL_TREE)
11532 tree type = TREE_TYPE (value);
11533 if (JPRIMITIVE_TYPE_P (type) ||
11534 (type == string_ptr_type_node && ! flag_emit_class_files))
11535 return empty_stmt_node;
11537 if (! flag_emit_class_files)
11538 DECL_INITIAL (nn) = NULL_TREE;
11539 if (CLASS_FINAL_VARIABLE_P (nn))
11540 DECL_FIELD_FINAL_IUD (nn) = 0;
11542 wfl_op2 = TREE_OPERAND (node, 1);
11544 if (TREE_OPERAND (node, 0) == error_mark_node)
11545 return error_mark_node;
11547 flag = COMPOUND_ASSIGN_P (wfl_op2);
11550 /* This might break when accessing outer field from inner
11551 class. TESTME, FIXME */
11552 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11554 /* Hand stabilize the lhs on both places */
11555 TREE_OPERAND (node, 0) = lvalue;
11556 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11557 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11559 /* 15.25.2.a: Left hand is not an array access. FIXME */
11560 /* Now complete the RHS. We write it back later on. */
11561 nn = java_complete_tree (TREE_OPERAND (node, 1));
11563 if ((cn = patch_string (nn)))
11566 /* The last part of the rewrite for E1 op= E2 is to have
11567 E1 = (T)(E1 op E2), with T being the type of E1. */
11568 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11569 TREE_TYPE (lvalue), nn));
11571 /* If the assignment is compound and has reference type,
11572 then ensure the LHS has type String and nothing else. */
11573 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11574 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11575 parse_error_context (wfl_op2,
11576 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11577 lang_printable_name (TREE_TYPE (lvalue), 0));
11579 /* 15.25.2.b: Left hand is an array access. FIXME */
11582 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11583 function to complete this RHS. Note that a NEW_ARRAY_INIT
11584 might have been already fully expanded if created as a result
11585 of processing an anonymous array initializer. We avoid doing
11586 the operation twice by testing whether the node already bears
11588 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11589 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11590 TREE_OPERAND (node, 1));
11591 /* Otherwise we simply complete the RHS */
11593 nn = java_complete_tree (TREE_OPERAND (node, 1));
11595 if (nn == error_mark_node)
11596 return error_mark_node;
11598 /* Write back the RHS as we evaluated it. */
11599 TREE_OPERAND (node, 1) = nn;
11601 /* In case we're handling = with a String as a RHS, we need to
11602 produce a String out of the RHS (it might still be a
11603 STRING_CST or a StringBuffer at this stage */
11604 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11605 TREE_OPERAND (node, 1) = nn;
11607 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11608 TREE_OPERAND (node, 1))))
11610 /* We return error_mark_node if outer_field_access_fix
11611 detects we write into a final. */
11612 if (nn == error_mark_node)
11613 return error_mark_node;
11618 node = patch_assignment (node, wfl_op1, wfl_op2);
11619 /* Reorganize the tree if necessary. */
11620 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11621 || JSTRING_P (TREE_TYPE (node))))
11622 node = java_refold (node);
11625 CAN_COMPLETE_NORMALLY (node) = 1;
11637 case TRUNC_MOD_EXPR:
11638 case TRUNC_DIV_EXPR:
11640 case TRUTH_ANDIF_EXPR:
11641 case TRUTH_ORIF_EXPR:
11648 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11649 knows how to handle those cases. */
11650 wfl_op1 = TREE_OPERAND (node, 0);
11651 wfl_op2 = TREE_OPERAND (node, 1);
11653 CAN_COMPLETE_NORMALLY (node) = 1;
11654 /* Don't complete string nodes if dealing with the PLUS operand. */
11655 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11657 nn = java_complete_tree (wfl_op1);
11658 if (nn == error_mark_node)
11659 return error_mark_node;
11661 TREE_OPERAND (node, 0) = nn;
11663 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11665 nn = java_complete_tree (wfl_op2);
11666 if (nn == error_mark_node)
11667 return error_mark_node;
11669 TREE_OPERAND (node, 1) = nn;
11671 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11673 case INSTANCEOF_EXPR:
11674 wfl_op1 = TREE_OPERAND (node, 0);
11675 COMPLETE_CHECK_OP_0 (node);
11676 if (flag_emit_xref)
11678 TREE_TYPE (node) = boolean_type_node;
11681 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11683 case UNARY_PLUS_EXPR:
11685 case TRUTH_NOT_EXPR:
11687 case PREDECREMENT_EXPR:
11688 case PREINCREMENT_EXPR:
11689 case POSTDECREMENT_EXPR:
11690 case POSTINCREMENT_EXPR:
11692 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11693 how to handle those cases. */
11694 wfl_op1 = TREE_OPERAND (node, 0);
11695 CAN_COMPLETE_NORMALLY (node) = 1;
11696 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11697 if (TREE_OPERAND (node, 0) == error_mark_node)
11698 return error_mark_node;
11699 node = patch_unaryop (node, wfl_op1);
11700 CAN_COMPLETE_NORMALLY (node) = 1;
11704 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11705 how to handle those cases. */
11706 wfl_op1 = TREE_OPERAND (node, 0);
11707 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11708 if (TREE_OPERAND (node, 0) == error_mark_node)
11709 return error_mark_node;
11710 if (!flag_emit_class_files && !flag_emit_xref)
11711 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
11712 /* The same applies to wfl_op2 */
11713 wfl_op2 = TREE_OPERAND (node, 1);
11714 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11715 if (TREE_OPERAND (node, 1) == error_mark_node)
11716 return error_mark_node;
11717 if (!flag_emit_class_files && !flag_emit_xref)
11718 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
11719 return patch_array_ref (node);
11724 case COMPONENT_REF:
11725 /* The first step in the re-write of qualified name handling. FIXME.
11726 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
11727 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11728 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11730 tree name = TREE_OPERAND (node, 1);
11731 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11732 if (field == NULL_TREE)
11734 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11735 return error_mark_node;
11737 if (! FIELD_STATIC (field))
11739 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11740 return error_mark_node;
11749 /* Can't use THIS in a static environment */
11752 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11753 parse_error_context (wfl_operator,
11754 "Keyword `this' used outside allowed context");
11755 TREE_TYPE (node) = error_mark_node;
11756 return error_mark_node;
11758 if (ctxp->explicit_constructor_p)
11760 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11761 parse_error_context
11762 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
11763 TREE_TYPE (node) = error_mark_node;
11764 return error_mark_node;
11766 return current_this;
11768 case CLASS_LITERAL:
11769 CAN_COMPLETE_NORMALLY (node) = 1;
11770 node = patch_incomplete_class_ref (node);
11771 if (node == error_mark_node)
11772 return error_mark_node;
11775 case INSTANCE_INITIALIZERS_EXPR:
11776 in_instance_initializer++;
11777 node = java_complete_tree (TREE_OPERAND (node, 0));
11778 in_instance_initializer--;
11779 if (node != error_mark_node)
11780 TREE_TYPE (node) = void_type_node;
11782 return error_mark_node;
11786 CAN_COMPLETE_NORMALLY (node) = 1;
11787 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
11788 and it's time to turn it into the appropriate String object */
11789 if ((nn = patch_string (node)))
11792 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
11797 /* Complete function call's argument. Return a non zero value is an
11798 error was found. */
11801 complete_function_arguments (node)
11807 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11808 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11810 tree wfl = TREE_VALUE (cn), parm, temp;
11811 parm = java_complete_tree (wfl);
11813 if (parm == error_mark_node)
11818 /* If have a string literal that we haven't transformed yet or a
11819 crafted string buffer, as a result of use of the the String
11820 `+' operator. Build `parm.toString()' and expand it. */
11821 if ((temp = patch_string (parm)))
11823 /* Inline PRIMTYPE.TYPE read access */
11824 parm = maybe_build_primttype_type_ref (parm, wfl);
11826 TREE_VALUE (cn) = parm;
11828 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11832 /* Sometimes (for loops and variable initialized during their
11833 declaration), we want to wrap a statement around a WFL and turn it
11837 build_debugable_stmt (location, stmt)
11841 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11843 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11844 EXPR_WFL_LINECOL (stmt) = location;
11846 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11851 build_expr_block (body, decls)
11854 tree node = make_node (BLOCK);
11855 BLOCK_EXPR_DECLS (node) = decls;
11856 BLOCK_EXPR_BODY (node) = body;
11858 TREE_TYPE (node) = TREE_TYPE (body);
11859 TREE_SIDE_EFFECTS (node) = 1;
11863 /* Create a new function block and link it approriately to current
11864 function block chain */
11869 tree b = build_expr_block (NULL_TREE, NULL_TREE);
11871 /* Link block B supercontext to the previous block. The current
11872 function DECL is used as supercontext when enter_a_block is called
11873 for the first time for a given function. The current function body
11874 (DECL_FUNCTION_BODY) is set to be block B. */
11876 tree fndecl = current_function_decl;
11879 BLOCK_SUPERCONTEXT (b) = current_static_block;
11880 current_static_block = b;
11883 else if (!DECL_FUNCTION_BODY (fndecl))
11885 BLOCK_SUPERCONTEXT (b) = fndecl;
11886 DECL_FUNCTION_BODY (fndecl) = b;
11890 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11891 DECL_FUNCTION_BODY (fndecl) = b;
11896 /* Exit a block by changing the current function body
11897 (DECL_FUNCTION_BODY) to the current block super context, only if
11898 the block being exited isn't the method's top level one. */
11904 if (current_function_decl)
11906 b = DECL_FUNCTION_BODY (current_function_decl);
11907 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11908 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11912 b = current_static_block;
11914 if (BLOCK_SUPERCONTEXT (b))
11915 current_static_block = BLOCK_SUPERCONTEXT (b);
11920 /* Lookup for NAME in the nested function's blocks, all the way up to
11921 the current toplevel one. It complies with Java's local variable
11925 lookup_name_in_blocks (name)
11928 tree b = GET_CURRENT_BLOCK (current_function_decl);
11930 while (b != current_function_decl)
11934 /* Paranoid sanity check. To be removed */
11935 if (TREE_CODE (b) != BLOCK)
11938 for (current = BLOCK_EXPR_DECLS (b); current;
11939 current = TREE_CHAIN (current))
11940 if (DECL_NAME (current) == name)
11942 b = BLOCK_SUPERCONTEXT (b);
11948 maybe_absorb_scoping_blocks ()
11950 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
11952 tree b = exit_block ();
11953 java_method_add_stmt (current_function_decl, b);
11954 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11959 /* This section of the source is reserved to build_* functions that
11960 are building incomplete tree nodes and the patch_* functions that
11961 are completing them. */
11963 /* Wrap a non WFL node around a WFL. */
11965 build_wfl_wrap (node, location)
11969 tree wfl, node_to_insert = node;
11971 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11972 with the way we're processing SUPER. A THIS from a primary as a
11973 different form than a SUPER. Turn THIS into something symbolic */
11974 if (TREE_CODE (node) == THIS_EXPR)
11975 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11977 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11979 EXPR_WFL_LINECOL (wfl) = location;
11980 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11985 /* Build a super() constructor invocation. Returns empty_stmt_node if
11986 we're currently dealing with the class java.lang.Object. */
11989 build_super_invocation (mdecl)
11992 if (DECL_CONTEXT (mdecl) == object_type_node)
11993 return empty_stmt_node;
11996 tree super_wfl = build_wfl_node (super_identifier_node);
11997 tree a = NULL_TREE, t;
11998 /* If we're dealing with an anonymous class, pass the arguments
11999 of the crafted constructor along. */
12000 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12002 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12003 for (; t != end_params_node; t = TREE_CHAIN (t))
12004 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12006 return build_method_invocation (super_wfl, a);
12010 /* Build a SUPER/THIS qualified method invocation. */
12013 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12020 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12021 EXPR_WFL_LINECOL (wfl) = lloc;
12022 invok = build_method_invocation (name, args);
12023 return make_qualified_primary (wfl, invok, rloc);
12026 /* Build an incomplete CALL_EXPR node. */
12029 build_method_invocation (name, args)
12033 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12034 TREE_SIDE_EFFECTS (call) = 1;
12035 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12039 /* Build an incomplete new xxx(...) node. */
12042 build_new_invocation (name, args)
12045 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12046 TREE_SIDE_EFFECTS (call) = 1;
12047 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12051 /* Build an incomplete assignment expression. */
12054 build_assignment (op, op_location, lhs, rhs)
12055 int op, op_location;
12059 /* Build the corresponding binop if we deal with a Compound
12060 Assignment operator. Mark the binop sub-tree as part of a
12061 Compound Assignment expression */
12062 if (op != ASSIGN_TK)
12064 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12065 COMPOUND_ASSIGN_P (rhs) = 1;
12067 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12068 TREE_SIDE_EFFECTS (assignment) = 1;
12069 EXPR_WFL_LINECOL (assignment) = op_location;
12073 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12076 print_int_node (node)
12079 static char buffer [80];
12080 if (TREE_CONSTANT_OVERFLOW (node))
12081 sprintf (buffer, "<overflow>");
12083 if (TREE_INT_CST_HIGH (node) == 0)
12084 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12085 TREE_INT_CST_LOW (node));
12086 else if (TREE_INT_CST_HIGH (node) == -1
12087 && TREE_INT_CST_LOW (node) != 0)
12090 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12091 -TREE_INT_CST_LOW (node));
12094 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12095 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12102 /* This section of the code handle assignment check with FINAL
12106 reset_static_final_variable_assignment_flag (class)
12110 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12111 if (CLASS_FINAL_VARIABLE_P (field))
12112 DECL_FIELD_FINAL_LIIC (field) = 0;
12115 /* Figure whether all final static variable have been initialized. */
12118 check_static_final_variable_assignment_flag (class)
12123 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12124 if (CLASS_FINAL_VARIABLE_P (field)
12125 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12126 parse_error_context
12127 (DECL_FIELD_FINAL_WFL (field),
12128 "Blank static final variable `%s' may not have been initialized",
12129 IDENTIFIER_POINTER (DECL_NAME (field)));
12132 /* This function marks all final variable locally unassigned. */
12135 reset_final_variable_local_assignment_flag (class)
12139 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12140 if (FINAL_VARIABLE_P (field))
12141 DECL_FIELD_FINAL_LIIC (field) = 0;
12144 /* Figure whether all final variables have beem initialized in MDECL
12145 and mark MDECL accordingly. */
12148 check_final_variable_local_assignment_flag (class, mdecl)
12153 int initialized = 0;
12154 int non_initialized = 0;
12156 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12159 /* First find out whether all final variables or no final variable
12160 are initialized in this ctor. We don't take into account final
12161 variable that have been initialized upon declaration. */
12162 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12163 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12165 if (DECL_FIELD_FINAL_LIIC (field))
12171 /* There were no non initialized variable and no initialized variable.
12172 This ctor is fine. */
12173 if (!non_initialized && !initialized)
12174 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12175 /* If no variables have been initialized, that fine. We'll check
12176 later whether this ctor calls a constructor which initializes
12177 them. We mark the ctor as not initializing all its finals. */
12178 else if (initialized == 0)
12179 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12180 /* If we have a mixed bag, then we have a problem. We need to report
12181 all the variables we're not initializing. */
12182 else if (initialized && non_initialized)
12184 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12185 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12186 if (FIELD_FINAL (field)
12187 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12189 parse_error_context
12190 (lookup_cl (mdecl),
12191 "Blank final variable `%s' may not have been initialized in this constructor",
12192 IDENTIFIER_POINTER (DECL_NAME (field)));
12193 DECL_FIELD_FINAL_IERR (field) = 1;
12196 /* Otherwise we know this ctor is initializing all its final
12197 variable. We mark it so. */
12199 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12202 /* This function recurses in a simple what through STMT and stops when
12203 it finds a constructor call. It then verifies that the called
12204 constructor initialized its final properly. Return 1 upon success,
12205 0 or -1 otherwise. */
12208 check_final_variable_indirect_assignment (stmt)
12212 switch (TREE_CODE (stmt))
12214 case EXPR_WITH_FILE_LOCATION:
12215 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12216 case COMPOUND_EXPR:
12217 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12220 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12222 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12225 tree decl = TREE_OPERAND (stmt, 0);
12228 if (TREE_CODE (decl) != FUNCTION_DECL)
12229 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12230 if (TREE_CODE (decl) != FUNCTION_DECL)
12232 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12234 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12236 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12237 if (fbody == error_mark_node)
12239 fbody = BLOCK_EXPR_BODY (fbody);
12240 return check_final_variable_indirect_assignment (fbody);
12248 /* This is the last chance to catch a final variable initialization
12249 problem. This routine will report an error if a final variable was
12250 never (globally) initialized and never reported as not having been
12251 initialized properly. */
12254 check_final_variable_global_assignment_flag (class)
12260 /* We go through all natural ctors and see whether they're
12261 initializing all their final variables or not. */
12262 current_function_decl = NULL_TREE; /* For the error report. */
12263 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12264 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12266 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12268 /* It doesn't. Maybe it calls a constructor that initializes
12270 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12271 if (fbody == error_mark_node)
12273 fbody = BLOCK_EXPR_BODY (fbody);
12274 if (check_final_variable_indirect_assignment (fbody) == 1)
12276 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12280 parse_error_context
12281 (lookup_cl (mdecl),
12282 "Final variable initialization error in this constructor");
12288 /* Finally we catch final variables that never were initialized */
12289 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12290 if (FINAL_VARIABLE_P (field)
12291 /* If the field wasn't initialized upon declaration */
12292 && !DECL_FIELD_FINAL_IUD (field)
12293 /* There wasn't natural ctor in which the field could have been
12296 /* If we never reported a problem with this field */
12297 && !DECL_FIELD_FINAL_IERR (field))
12299 current_function_decl = NULL;
12300 parse_error_context
12301 (DECL_FIELD_FINAL_WFL (field),
12302 "Final variable `%s' hasn't been initialized upon its declaration",
12303 IDENTIFIER_POINTER (DECL_NAME (field)));
12308 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12312 check_final_assignment (lvalue, wfl)
12315 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12318 if (TREE_CODE (lvalue) == COMPONENT_REF
12319 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12320 lvalue = TREE_OPERAND (lvalue, 1);
12322 if (!FIELD_FINAL (lvalue))
12325 /* Now the logic. We can modify a final VARIABLE:
12326 1) in finit$, (its declaration was followed by an initialization,)
12327 2) consistently in each natural ctor, if it wasn't initialized in
12328 finit$ or once in <clinit>. In any other cases, an error should be
12330 if (DECL_FINIT_P (current_function_decl))
12332 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12336 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12337 /* Only if it wasn't given a value upon initialization */
12338 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12339 /* If it was never assigned a value in this constructor */
12340 && !DECL_FIELD_FINAL_LIIC (lvalue))
12342 /* Turn the locally assigned flag on, it will be checked later
12343 on to point out at discrepancies. */
12344 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12345 if (DECL_CLINIT_P (current_function_decl))
12346 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12350 /* Other problems should be reported right away. */
12351 parse_error_context
12352 (wfl, "Can't %sassign a value to the final variable `%s'",
12353 (FIELD_STATIC (lvalue) ? "re" : ""),
12354 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12356 /* Note that static field can be initialized once and only once. */
12357 if (FIELD_STATIC (lvalue))
12358 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12363 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12364 read. This is needed to avoid circularities in the implementation
12365 of these fields in libjava. */
12368 maybe_build_primttype_type_ref (rhs, wfl)
12371 tree to_return = NULL_TREE;
12372 tree rhs_type = TREE_TYPE (rhs);
12373 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12375 tree n = TREE_OPERAND (rhs, 1);
12376 if (TREE_CODE (n) == VAR_DECL
12377 && DECL_NAME (n) == TYPE_identifier_node
12378 && rhs_type == class_ptr_type
12379 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
12381 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
12382 if (!strncmp (self_name, "java.lang.", 10))
12383 to_return = build_primtype_type_ref (self_name);
12386 return (to_return ? to_return : rhs );
12389 /* 15.25 Assignment operators. */
12392 patch_assignment (node, wfl_op1, wfl_op2)
12397 tree rhs = TREE_OPERAND (node, 1);
12398 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12399 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12400 int error_found = 0;
12401 int lvalue_from_array = 0;
12403 /* Can't assign to a (blank) final. */
12404 if (check_final_assignment (lvalue, wfl_op1))
12407 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12409 /* Lhs can be a named variable */
12410 if (JDECL_P (lvalue))
12412 lhs_type = TREE_TYPE (lvalue);
12414 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12415 comment on reason why */
12416 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12418 lhs_type = TREE_TYPE (lvalue);
12419 lvalue_from_array = 1;
12421 /* Or a field access */
12422 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12423 lhs_type = TREE_TYPE (lvalue);
12424 /* Or a function return slot */
12425 else if (TREE_CODE (lvalue) == RESULT_DECL)
12426 lhs_type = TREE_TYPE (lvalue);
12427 /* Otherwise, we might want to try to write into an optimized static
12428 final, this is an of a different nature, reported further on. */
12429 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12430 && resolve_expression_name (wfl_op1, &llvalue))
12432 if (!error_found && check_final_assignment (llvalue, wfl_op1))
12434 /* What we should do instead is resetting the all the flags
12435 previously set, exchange lvalue for llvalue and continue. */
12437 return error_mark_node;
12440 lhs_type = TREE_TYPE (lvalue);
12444 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12448 rhs_type = TREE_TYPE (rhs);
12449 /* 5.1 Try the assignment conversion for builtin type. */
12450 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12452 /* 5.2 If it failed, try a reference conversion */
12453 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12454 lhs_type = promote_type (rhs_type);
12456 /* 15.25.2 If we have a compound assignment, convert RHS into the
12458 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12459 new_rhs = convert (lhs_type, rhs);
12461 /* Explicit cast required. This is an error */
12464 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12465 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12467 char operation [32]; /* Max size known */
12469 /* If the assignment is part of a declaration, we use the WFL of
12470 the declared variable to point out the error and call it a
12471 declaration problem. If the assignment is a genuine =
12472 operator, we call is a operator `=' problem, otherwise we
12473 call it an assignment problem. In both of these last cases,
12474 we use the WFL of the operator to indicate the error. */
12476 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12479 strcpy (operation, "declaration");
12483 wfl = wfl_operator;
12484 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12485 strcpy (operation, "assignment");
12486 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12487 strcpy (operation, "`return'");
12489 strcpy (operation, "`='");
12492 if (!valid_cast_to_p (rhs_type, lhs_type))
12493 parse_error_context
12494 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12495 operation, t1, t2);
12497 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12498 operation, t1, t2);
12499 free (t1); free (t2);
12503 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12505 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
12508 return error_mark_node;
12510 /* 10.10: Array Store Exception runtime check */
12511 if (!flag_emit_class_files
12513 && lvalue_from_array
12514 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12517 tree base = lvalue;
12519 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12520 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12521 base = TREE_OPERAND (lvalue, 0);
12524 if (flag_bounds_check)
12525 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12527 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12530 /* Build the invocation of _Jv_CheckArrayStore */
12531 new_rhs = save_expr (new_rhs);
12532 check = build (CALL_EXPR, void_type_node,
12533 build_address_of (soft_checkarraystore_node),
12534 tree_cons (NULL_TREE, base,
12535 build_tree_list (NULL_TREE, new_rhs)),
12537 TREE_SIDE_EFFECTS (check) = 1;
12539 /* We have to decide on an insertion point */
12540 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12543 if (flag_bounds_check)
12545 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12546 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12547 build (COMPOUND_EXPR, void_type_node, t, check);
12550 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12551 check, TREE_OPERAND (lvalue, 1));
12555 /* Make sure the bound check will happen before the store check */
12556 if (flag_bounds_check)
12557 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12558 build (COMPOUND_EXPR, void_type_node,
12559 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12561 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12565 /* Final locals can be used as case values in switch
12566 statement. Prepare them for this eventuality. */
12567 if (TREE_CODE (lvalue) == VAR_DECL
12568 && LOCAL_FINAL_P (lvalue)
12569 && TREE_CONSTANT (new_rhs)
12570 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12571 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12574 TREE_CONSTANT (lvalue) = 1;
12575 DECL_INITIAL (lvalue) = new_rhs;
12578 TREE_OPERAND (node, 0) = lvalue;
12579 TREE_OPERAND (node, 1) = new_rhs;
12580 TREE_TYPE (node) = lhs_type;
12584 /* Check that type SOURCE can be cast into type DEST. If the cast
12585 can't occur at all, return 0 otherwise 1. This function is used to
12586 produce accurate error messages on the reasons why an assignment
12590 try_reference_assignconv (lhs_type, rhs)
12591 tree lhs_type, rhs;
12593 tree new_rhs = NULL_TREE;
12594 tree rhs_type = TREE_TYPE (rhs);
12596 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12598 /* `null' may be assigned to any reference type */
12599 if (rhs == null_pointer_node)
12600 new_rhs = null_pointer_node;
12601 /* Try the reference assignment conversion */
12602 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12604 /* This is a magic assignment that we process differently */
12605 else if (rhs == soft_exceptioninfo_call_node)
12611 /* Check that RHS can be converted into LHS_TYPE by the assignment
12612 conversion (5.2), for the cases of RHS being a builtin type. Return
12613 NULL_TREE if the conversion fails or if because RHS isn't of a
12614 builtin type. Return a converted RHS if the conversion is possible. */
12617 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12618 tree wfl_op1, lhs_type, rhs;
12620 tree new_rhs = NULL_TREE;
12621 tree rhs_type = TREE_TYPE (rhs);
12623 /* Handle boolean specially. */
12624 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12625 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12627 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12628 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12632 /* Zero accepted everywhere */
12633 else if (TREE_CODE (rhs) == INTEGER_CST
12634 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12635 && JPRIMITIVE_TYPE_P (rhs_type))
12636 new_rhs = convert (lhs_type, rhs);
12638 /* 5.1.1 Try Identity Conversion,
12639 5.1.2 Try Widening Primitive Conversion */
12640 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12641 new_rhs = convert (lhs_type, rhs);
12643 /* Try a narrowing primitive conversion (5.1.3):
12644 - expression is a constant expression of type int AND
12645 - variable is byte, short or char AND
12646 - The value of the expression is representable in the type of the
12648 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12649 && (lhs_type == byte_type_node || lhs_type == char_type_node
12650 || lhs_type == short_type_node))
12652 if (int_fits_type_p (rhs, lhs_type))
12653 new_rhs = convert (lhs_type, rhs);
12654 else if (wfl_op1) /* Might be called with a NULL */
12655 parse_warning_context
12656 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12657 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12658 /* Reported a warning that will turn into an error further
12659 down, so we don't return */
12665 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12666 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12667 0 is the conversion test fails. This implements parts the method
12668 invocation convertion (5.3). */
12671 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12672 tree lhs_type, rhs_type;
12674 /* 5.1.1: This is the identity conversion part. */
12675 if (lhs_type == rhs_type)
12678 /* Reject non primitive types and boolean conversions. */
12679 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12682 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12683 than a char can't be converted into a char. Short can't too, but
12684 the < test below takes care of that */
12685 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12688 /* Accept all promoted type here. Note, we can't use <= in the test
12689 below, because we still need to bounce out assignments of short
12690 to char and the likes */
12691 if (lhs_type == int_type_node
12692 && (rhs_type == promoted_byte_type_node
12693 || rhs_type == promoted_short_type_node
12694 || rhs_type == promoted_char_type_node
12695 || rhs_type == promoted_boolean_type_node))
12698 /* From here, an integral is widened if its precision is smaller
12699 than the precision of the LHS or if the LHS is a floating point
12700 type, or the RHS is a float and the RHS a double. */
12701 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12702 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12703 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12704 || (rhs_type == float_type_node && lhs_type == double_type_node))
12710 /* Check that something of SOURCE type can be assigned or cast to
12711 something of DEST type at runtime. Return 1 if the operation is
12712 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12713 were SOURCE is cast into DEST, which borrows a lot of the
12714 assignment check. */
12717 valid_ref_assignconv_cast_p (source, dest, cast)
12722 /* SOURCE or DEST might be null if not from a declared entity. */
12723 if (!source || !dest)
12725 if (JNULLP_TYPE_P (source))
12727 if (TREE_CODE (source) == POINTER_TYPE)
12728 source = TREE_TYPE (source);
12729 if (TREE_CODE (dest) == POINTER_TYPE)
12730 dest = TREE_TYPE (dest);
12732 /* If source and dest are being compiled from bytecode, they may need to
12734 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12736 load_class (source, 1);
12737 safe_layout_class (source);
12739 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12741 load_class (dest, 1);
12742 safe_layout_class (dest);
12745 /* Case where SOURCE is a class type */
12746 if (TYPE_CLASS_P (source))
12748 if (TYPE_CLASS_P (dest))
12749 return (source == dest
12750 || inherits_from_p (source, dest)
12751 || (cast && inherits_from_p (dest, source)));
12752 if (TYPE_INTERFACE_P (dest))
12754 /* If doing a cast and SOURCE is final, the operation is
12755 always correct a compile time (because even if SOURCE
12756 does not implement DEST, a subclass of SOURCE might). */
12757 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12759 /* Otherwise, SOURCE must implement DEST */
12760 return interface_of_p (dest, source);
12762 /* DEST is an array, cast permited if SOURCE is of Object type */
12763 return (cast && source == object_type_node ? 1 : 0);
12765 if (TYPE_INTERFACE_P (source))
12767 if (TYPE_CLASS_P (dest))
12769 /* If not casting, DEST must be the Object type */
12771 return dest == object_type_node;
12772 /* We're doing a cast. The cast is always valid is class
12773 DEST is not final, otherwise, DEST must implement SOURCE */
12774 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12777 return interface_of_p (source, dest);
12779 if (TYPE_INTERFACE_P (dest))
12781 /* If doing a cast, then if SOURCE and DEST contain method
12782 with the same signature but different return type, then
12783 this is a (compile time) error */
12786 tree method_source, method_dest;
12790 for (method_source = TYPE_METHODS (source); method_source;
12791 method_source = TREE_CHAIN (method_source))
12794 build_java_argument_signature (TREE_TYPE (method_source));
12795 source_type = TREE_TYPE (TREE_TYPE (method_source));
12796 source_name = DECL_NAME (method_source);
12797 for (method_dest = TYPE_METHODS (dest);
12798 method_dest; method_dest = TREE_CHAIN (method_dest))
12800 build_java_argument_signature (TREE_TYPE (method_dest))
12801 && source_name == DECL_NAME (method_dest)
12802 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12808 return source == dest || interface_of_p (dest, source);
12814 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12815 || (DECL_NAME (TYPE_NAME (source))
12816 == java_io_serializable)));
12819 if (TYPE_ARRAY_P (source))
12821 if (TYPE_CLASS_P (dest))
12822 return dest == object_type_node;
12823 /* Can't cast an array to an interface unless the interface is
12824 java.lang.Cloneable or java.io.Serializable. */
12825 if (TYPE_INTERFACE_P (dest))
12826 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12827 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
12830 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12831 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12833 /* In case of severe errors, they turn out null */
12834 if (!dest_element_type || !source_element_type)
12836 if (source_element_type == dest_element_type)
12838 return valid_ref_assignconv_cast_p (source_element_type,
12839 dest_element_type, cast);
12847 valid_cast_to_p (source, dest)
12851 if (TREE_CODE (source) == POINTER_TYPE)
12852 source = TREE_TYPE (source);
12853 if (TREE_CODE (dest) == POINTER_TYPE)
12854 dest = TREE_TYPE (dest);
12856 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12857 return valid_ref_assignconv_cast_p (source, dest, 1);
12859 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12862 else if (TREE_CODE (source) == BOOLEAN_TYPE
12863 && TREE_CODE (dest) == BOOLEAN_TYPE)
12870 do_unary_numeric_promotion (arg)
12873 tree type = TREE_TYPE (arg);
12874 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12875 || TREE_CODE (type) == CHAR_TYPE)
12876 arg = convert (int_type_node, arg);
12880 /* Return a non zero value if SOURCE can be converted into DEST using
12881 the method invocation conversion rule (5.3). */
12883 valid_method_invocation_conversion_p (dest, source)
12886 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12887 && valid_builtin_assignconv_identity_widening_p (dest, source))
12888 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12889 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12890 && valid_ref_assignconv_cast_p (source, dest, 0)));
12893 /* Build an incomplete binop expression. */
12896 build_binop (op, op_location, op1, op2)
12901 tree binop = build (op, NULL_TREE, op1, op2);
12902 TREE_SIDE_EFFECTS (binop) = 1;
12903 /* Store the location of the operator, for better error report. The
12904 string of the operator will be rebuild based on the OP value. */
12905 EXPR_WFL_LINECOL (binop) = op_location;
12909 /* Build the string of the operator retained by NODE. If NODE is part
12910 of a compound expression, add an '=' at the end of the string. This
12911 function is called when an error needs to be reported on an
12912 operator. The string is returned as a pointer to a static character
12916 operator_string (node)
12919 #define BUILD_OPERATOR_STRING(S) \
12921 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12925 static char buffer [10];
12926 switch (TREE_CODE (node))
12928 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12929 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12930 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12931 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12932 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12933 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12934 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12935 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12936 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12937 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12938 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12939 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12940 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12941 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12942 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12943 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12944 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12945 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12946 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
12947 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12948 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12949 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12950 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12951 case PREINCREMENT_EXPR: /* Fall through */
12952 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12953 case PREDECREMENT_EXPR: /* Fall through */
12954 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12956 internal_error ("unregistered operator %s",
12957 tree_code_name [TREE_CODE (node)]);
12960 #undef BUILD_OPERATOR_STRING
12963 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12966 java_decl_equiv (var_acc1, var_acc2)
12967 tree var_acc1, var_acc2;
12969 if (JDECL_P (var_acc1))
12970 return (var_acc1 == var_acc2);
12972 return (TREE_CODE (var_acc1) == COMPONENT_REF
12973 && TREE_CODE (var_acc2) == COMPONENT_REF
12974 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12975 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12976 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12979 /* Return a non zero value if CODE is one of the operators that can be
12980 used in conjunction with the `=' operator in a compound assignment. */
12983 binop_compound_p (code)
12984 enum tree_code code;
12987 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12988 if (binop_lookup [i] == code)
12991 return i < BINOP_COMPOUND_CANDIDATES;
12994 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13000 tree c, b, ns, decl;
13002 if (TREE_CODE (t) != MODIFY_EXPR)
13005 c = TREE_OPERAND (t, 1);
13006 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13007 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13008 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13011 /* Now the left branch of the binary operator. */
13012 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13013 if (! (b && TREE_CODE (b) == NOP_EXPR
13014 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13017 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13018 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13019 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13022 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13023 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13024 /* It's got to be the an equivalent decl */
13025 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13027 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13028 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13029 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13030 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13031 /* Change the right part of the BINOP_EXPR */
13032 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13038 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13039 errors but we modify NODE so that it contains the type computed
13040 according to the expression, when it's fixed. Otherwise, we write
13041 error_mark_node as the type. It allows us to further the analysis
13042 of remaining nodes and detects more errors in certain cases. */
13045 patch_binop (node, wfl_op1, wfl_op2)
13050 tree op1 = TREE_OPERAND (node, 0);
13051 tree op2 = TREE_OPERAND (node, 1);
13052 tree op1_type = TREE_TYPE (op1);
13053 tree op2_type = TREE_TYPE (op2);
13054 tree prom_type = NULL_TREE, cn;
13055 int code = TREE_CODE (node);
13057 /* If 1, tell the routine that we have to return error_mark_node
13058 after checking for the initialization of the RHS */
13059 int error_found = 0;
13061 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13063 /* If either op<n>_type are NULL, this might be early signs of an
13064 error situation, unless it's too early to tell (in case we're
13065 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13066 correctly so the error can be later on reported accurately. */
13067 if (! (code == PLUS_EXPR || code == NE_EXPR
13068 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13073 n = java_complete_tree (op1);
13074 op1_type = TREE_TYPE (n);
13078 n = java_complete_tree (op2);
13079 op2_type = TREE_TYPE (n);
13085 /* 15.16 Multiplicative operators */
13086 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13087 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13088 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13089 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13090 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13092 if (!JNUMERIC_TYPE_P (op1_type))
13093 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13094 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13095 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13096 TREE_TYPE (node) = error_mark_node;
13100 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13101 /* Change the division operator if necessary */
13102 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13103 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13105 if (TREE_CODE (prom_type) == INTEGER_TYPE
13106 && flag_use_divide_subroutine
13107 && ! flag_emit_class_files
13108 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13109 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13111 /* This one is more complicated. FLOATs are processed by a
13112 function call to soft_fmod. Duplicate the value of the
13113 COMPOUND_ASSIGN_P flag. */
13114 if (code == TRUNC_MOD_EXPR)
13116 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13117 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13118 TREE_SIDE_EFFECTS (mod)
13119 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13124 /* 15.17 Additive Operators */
13125 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13127 /* Operation is valid if either one argument is a string
13128 constant, a String object or a StringBuffer crafted for the
13129 purpose of the a previous usage of the String concatenation
13132 if (TREE_CODE (op1) == STRING_CST
13133 || TREE_CODE (op2) == STRING_CST
13134 || JSTRING_TYPE_P (op1_type)
13135 || JSTRING_TYPE_P (op2_type)
13136 || IS_CRAFTED_STRING_BUFFER_P (op1)
13137 || IS_CRAFTED_STRING_BUFFER_P (op2))
13138 return build_string_concatenation (op1, op2);
13140 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13142 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13144 if (!JNUMERIC_TYPE_P (op1_type))
13145 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13146 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13147 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13148 TREE_TYPE (node) = error_mark_node;
13152 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13155 /* 15.18 Shift Operators */
13159 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13161 if (!JINTEGRAL_TYPE_P (op1_type))
13162 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13165 if (JNUMERIC_TYPE_P (op2_type))
13166 parse_error_context (wfl_operator,
13167 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13168 operator_string (node),
13169 lang_printable_name (op2_type, 0));
13171 parse_error_context (wfl_operator,
13172 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13173 operator_string (node),
13174 lang_printable_name (op2_type, 0));
13176 TREE_TYPE (node) = error_mark_node;
13181 /* Unary numeric promotion (5.6.1) is performed on each operand
13183 op1 = do_unary_numeric_promotion (op1);
13184 op2 = do_unary_numeric_promotion (op2);
13186 /* The type of the shift expression is the type of the promoted
13187 type of the left-hand operand */
13188 prom_type = TREE_TYPE (op1);
13190 /* Shift int only up to 0x1f and long up to 0x3f */
13191 if (prom_type == int_type_node)
13192 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13193 build_int_2 (0x1f, 0)));
13195 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13196 build_int_2 (0x3f, 0)));
13198 /* The >>> operator is a >> operating on unsigned quantities */
13199 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13202 tree utype = unsigned_type (prom_type);
13203 op1 = convert (utype, op1);
13204 TREE_SET_CODE (node, RSHIFT_EXPR);
13205 TREE_OPERAND (node, 0) = op1;
13206 TREE_OPERAND (node, 1) = op2;
13207 TREE_TYPE (node) = utype;
13208 to_return = convert (prom_type, node);
13209 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13210 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13211 TREE_SIDE_EFFECTS (to_return)
13212 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13217 /* 15.19.1 Type Comparison Operator instaceof */
13218 case INSTANCEOF_EXPR:
13220 TREE_TYPE (node) = boolean_type_node;
13222 if (!(op2_type = resolve_type_during_patch (op2)))
13223 return error_mark_node;
13225 /* The first operand must be a reference type or the null type */
13226 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13227 error_found = 1; /* Error reported further below */
13229 /* The second operand must be a reference type */
13230 if (!JREFERENCE_TYPE_P (op2_type))
13232 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13233 parse_error_context
13234 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13235 lang_printable_name (op2_type, 0));
13239 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13241 /* If the first operand is null, the result is always false */
13242 if (op1 == null_pointer_node)
13243 return boolean_false_node;
13244 else if (flag_emit_class_files)
13246 TREE_OPERAND (node, 1) = op2_type;
13247 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13250 /* Otherwise we have to invoke instance of to figure it out */
13252 return build_instanceof (op1, op2_type);
13254 /* There is no way the expression operand can be an instance of
13255 the type operand. This is a compile time error. */
13258 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13259 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13260 parse_error_context
13261 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13262 t1, lang_printable_name (op2_type, 0));
13269 /* 15.21 Bitwise and Logical Operators */
13273 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13274 /* Binary numeric promotion is performed on both operand and the
13275 expression retain that type */
13276 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13278 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13279 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13280 /* The type of the bitwise operator expression is BOOLEAN */
13281 prom_type = boolean_type_node;
13284 if (!JINTEGRAL_TYPE_P (op1_type))
13285 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13286 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13287 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13288 TREE_TYPE (node) = error_mark_node;
13290 /* Insert a break here if adding thing before the switch's
13291 break for this case */
13295 /* 15.22 Conditional-And Operator */
13296 case TRUTH_ANDIF_EXPR:
13297 /* 15.23 Conditional-Or Operator */
13298 case TRUTH_ORIF_EXPR:
13299 /* Operands must be of BOOLEAN type */
13300 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13301 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13303 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13304 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13305 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13306 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13307 TREE_TYPE (node) = boolean_type_node;
13311 /* The type of the conditional operators is BOOLEAN */
13312 prom_type = boolean_type_node;
13315 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13320 /* The type of each of the operands must be a primitive numeric
13322 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13324 if (!JNUMERIC_TYPE_P (op1_type))
13325 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13326 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13327 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13328 TREE_TYPE (node) = boolean_type_node;
13332 /* Binary numeric promotion is performed on the operands */
13333 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13334 /* The type of the relation expression is always BOOLEAN */
13335 prom_type = boolean_type_node;
13338 /* 15.20 Equality Operator */
13341 /* It's time for us to patch the strings. */
13342 if ((cn = patch_string (op1)))
13345 op1_type = TREE_TYPE (op1);
13347 if ((cn = patch_string (op2)))
13350 op2_type = TREE_TYPE (op2);
13353 /* 15.20.1 Numerical Equality Operators == and != */
13354 /* Binary numeric promotion is performed on the operands */
13355 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13356 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13358 /* 15.20.2 Boolean Equality Operators == and != */
13359 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13360 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13361 ; /* Nothing to do here */
13363 /* 15.20.3 Reference Equality Operators == and != */
13364 /* Types have to be either references or the null type. If
13365 they're references, it must be possible to convert either
13366 type to the other by casting conversion. */
13367 else if (op1 == null_pointer_node || op2 == null_pointer_node
13368 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13369 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13370 || valid_ref_assignconv_cast_p (op2_type,
13372 ; /* Nothing to do here */
13374 /* Else we have an error figure what can't be converted into
13375 what and report the error */
13379 t1 = xstrdup (lang_printable_name (op1_type, 0));
13380 parse_error_context
13382 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13383 operator_string (node), t1,
13384 lang_printable_name (op2_type, 0));
13386 TREE_TYPE (node) = boolean_type_node;
13390 prom_type = boolean_type_node;
13395 return error_mark_node;
13397 TREE_OPERAND (node, 0) = op1;
13398 TREE_OPERAND (node, 1) = op2;
13399 TREE_TYPE (node) = prom_type;
13400 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13402 if (flag_emit_xref)
13405 /* fold does not respect side-effect order as required for Java but not C.
13406 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13409 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13410 : ! TREE_SIDE_EFFECTS (node))
13411 node = fold (node);
13415 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13416 zero value, the value of CSTE comes after the valude of STRING */
13419 do_merge_string_cste (cste, string, string_len, after)
13421 const char *string;
13422 int string_len, after;
13424 const char *old = TREE_STRING_POINTER (cste);
13425 int old_len = TREE_STRING_LENGTH (cste);
13426 int len = old_len + string_len;
13427 char *new = alloca (len+1);
13431 memcpy (new, string, string_len);
13432 memcpy (&new [string_len], old, old_len);
13436 memcpy (new, old, old_len);
13437 memcpy (&new [old_len], string, string_len);
13440 return build_string (len, new);
13443 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13444 new STRING_CST on success, NULL_TREE on failure */
13447 merge_string_cste (op1, op2, after)
13451 /* Handle two string constants right away */
13452 if (TREE_CODE (op2) == STRING_CST)
13453 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13454 TREE_STRING_LENGTH (op2), after);
13456 /* Reasonable integer constant can be treated right away */
13457 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13459 static const char *boolean_true = "true";
13460 static const char *boolean_false = "false";
13461 static const char *null_pointer = "null";
13463 const char *string;
13465 if (op2 == boolean_true_node)
13466 string = boolean_true;
13467 else if (op2 == boolean_false_node)
13468 string = boolean_false;
13469 else if (op2 == null_pointer_node)
13470 string = null_pointer;
13471 else if (TREE_TYPE (op2) == char_type_node)
13473 ch[0] = (char )TREE_INT_CST_LOW (op2);
13478 string = print_int_node (op2);
13480 return do_merge_string_cste (op1, string, strlen (string), after);
13485 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13486 has to be a STRING_CST and the other part must be a STRING_CST or a
13487 INTEGRAL constant. Return a new STRING_CST if the operation
13488 succeed, NULL_TREE otherwise.
13490 If the case we want to optimize for space, we might want to return
13491 NULL_TREE for each invocation of this routine. FIXME */
13494 string_constant_concatenation (op1, op2)
13497 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13502 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13503 rest = (string == op1 ? op2 : op1);
13504 invert = (string == op1 ? 0 : 1 );
13506 /* Walk REST, only if it looks reasonable */
13507 if (TREE_CODE (rest) != STRING_CST
13508 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13509 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13510 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13512 rest = java_complete_tree (rest);
13513 if (rest == error_mark_node)
13514 return error_mark_node;
13515 rest = fold (rest);
13517 return merge_string_cste (string, rest, invert);
13522 /* Implement the `+' operator. Does static optimization if possible,
13523 otherwise create (if necessary) and append elements to a
13524 StringBuffer. The StringBuffer will be carried around until it is
13525 used for a function call or an assignment. Then toString() will be
13526 called on it to turn it into a String object. */
13529 build_string_concatenation (op1, op2)
13533 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13535 if (flag_emit_xref)
13536 return build (PLUS_EXPR, string_type_node, op1, op2);
13538 /* Try to do some static optimization */
13539 if ((result = string_constant_concatenation (op1, op2)))
13542 /* Discard empty strings on either side of the expression */
13543 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13548 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13551 /* If operands are string constant, turn then into object references */
13552 if (TREE_CODE (op1) == STRING_CST)
13553 op1 = patch_string_cst (op1);
13554 if (op2 && TREE_CODE (op2) == STRING_CST)
13555 op2 = patch_string_cst (op2);
13557 /* If either one of the constant is null and the other non null
13558 operand is a String object, return it. */
13559 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13562 /* If OP1 isn't already a StringBuffer, create and
13563 initialize a new one */
13564 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13566 /* Two solutions here:
13567 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13568 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13569 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13570 op1 = BUILD_STRING_BUFFER (op1);
13573 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13574 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13580 /* OP1 is no longer the last node holding a crafted StringBuffer */
13581 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13582 /* Create a node for `{new...,xxx}.append (op2)' */
13584 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13587 /* Mark the last node holding a crafted StringBuffer */
13588 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13590 TREE_SIDE_EFFECTS (op1) = side_effects;
13594 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13595 StringBuffer. If no string were found to be patched, return
13599 patch_string (node)
13602 if (node == error_mark_node)
13603 return error_mark_node;
13604 if (TREE_CODE (node) == STRING_CST)
13605 return patch_string_cst (node);
13606 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13608 int saved = ctxp->explicit_constructor_p;
13609 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13611 /* Temporary disable forbid the use of `this'. */
13612 ctxp->explicit_constructor_p = 0;
13613 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13614 /* String concatenation arguments must be evaluated in order too. */
13615 ret = force_evaluation_order (ret);
13616 /* Restore it at its previous value */
13617 ctxp->explicit_constructor_p = saved;
13623 /* Build the internal representation of a string constant. */
13626 patch_string_cst (node)
13630 if (! flag_emit_class_files)
13632 node = get_identifier (TREE_STRING_POINTER (node));
13633 location = alloc_name_constant (CONSTANT_String, node);
13634 node = build_ref_from_constant_pool (location);
13636 TREE_TYPE (node) = string_ptr_type_node;
13637 TREE_CONSTANT (node) = 1;
13641 /* Build an incomplete unary operator expression. */
13644 build_unaryop (op_token, op_location, op1)
13645 int op_token, op_location;
13652 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13653 case MINUS_TK: op = NEGATE_EXPR; break;
13654 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13655 case NOT_TK: op = BIT_NOT_EXPR; break;
13659 unaryop = build1 (op, NULL_TREE, op1);
13660 TREE_SIDE_EFFECTS (unaryop) = 1;
13661 /* Store the location of the operator, for better error report. The
13662 string of the operator will be rebuild based on the OP value. */
13663 EXPR_WFL_LINECOL (unaryop) = op_location;
13667 /* Special case for the ++/-- operators, since they require an extra
13668 argument to build, which is set to NULL and patched
13669 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13672 build_incdec (op_token, op_location, op1, is_post_p)
13673 int op_token, op_location;
13677 static enum tree_code lookup [2][2] =
13679 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13680 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13682 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13683 NULL_TREE, op1, NULL_TREE);
13684 TREE_SIDE_EFFECTS (node) = 1;
13685 /* Store the location of the operator, for better error report. The
13686 string of the operator will be rebuild based on the OP value. */
13687 EXPR_WFL_LINECOL (node) = op_location;
13691 /* Build an incomplete cast operator, based on the use of the
13692 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13693 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13694 though its type is already set. */
13697 build_cast (location, type, exp)
13701 tree node = build1 (CONVERT_EXPR, type, exp);
13702 EXPR_WFL_LINECOL (node) = location;
13706 /* Build an incomplete class reference operator. */
13708 build_incomplete_class_ref (location, class_name)
13712 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13713 EXPR_WFL_LINECOL (node) = location;
13717 /* Complete an incomplete class reference operator. */
13719 patch_incomplete_class_ref (node)
13722 tree type = TREE_OPERAND (node, 0);
13725 if (!(ref_type = resolve_type_during_patch (type)))
13726 return error_mark_node;
13728 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13730 /* A class referenced by `foo.class' is initialized. */
13731 return build_class_init (ref_type, build_class_ref (ref_type));
13734 /* If we're emitting class files and we have to deal with non
13735 primitive types, we invoke (and consider generating) the
13736 synthetic static method `class$'. */
13737 if (!TYPE_DOT_CLASS (current_class))
13738 build_dot_class_method (current_class);
13739 ref_type = build_dot_class_method_invocation (ref_type);
13740 return java_complete_tree (ref_type);
13743 /* 15.14 Unary operators. We return error_mark_node in case of error,
13744 but preserve the type of NODE if the type is fixed. */
13747 patch_unaryop (node, wfl_op)
13751 tree op = TREE_OPERAND (node, 0);
13752 tree op_type = TREE_TYPE (op);
13753 tree prom_type = NULL_TREE, value, decl;
13754 int outer_field_flag = 0;
13755 int code = TREE_CODE (node);
13756 int error_found = 0;
13758 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13762 /* 15.13.2 Postfix Increment Operator ++ */
13763 case POSTINCREMENT_EXPR:
13764 /* 15.13.3 Postfix Increment Operator -- */
13765 case POSTDECREMENT_EXPR:
13766 /* 15.14.1 Prefix Increment Operator ++ */
13767 case PREINCREMENT_EXPR:
13768 /* 15.14.2 Prefix Decrement Operator -- */
13769 case PREDECREMENT_EXPR:
13770 op = decl = strip_out_static_field_access_decl (op);
13771 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13772 /* We might be trying to change an outer field accessed using
13774 if (outer_field_flag)
13776 /* Retrieve the decl of the field we're trying to access. We
13777 do that by first retrieving the function we would call to
13778 access the field. It has been already verified that this
13779 field isn't final */
13780 if (flag_emit_class_files)
13781 decl = TREE_OPERAND (op, 0);
13783 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13784 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13786 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13787 else if (!JDECL_P (decl)
13788 && TREE_CODE (decl) != COMPONENT_REF
13789 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13790 && TREE_CODE (decl) != INDIRECT_REF
13791 && !(TREE_CODE (decl) == COMPOUND_EXPR
13792 && TREE_OPERAND (decl, 1)
13793 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13796 /* Before screaming, check that we're not in fact trying to
13797 increment a optimized static final access, in which case
13798 we issue an different error message. */
13799 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13800 && resolve_expression_name (wfl_op, &lvalue)
13801 && check_final_assignment (lvalue, wfl_op)))
13802 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13803 operator_string (node));
13804 TREE_TYPE (node) = error_mark_node;
13808 if (check_final_assignment (op, wfl_op))
13811 /* From now on, we know that op if a variable and that it has a
13812 valid wfl. We use wfl_op to locate errors related to the
13814 else if (!JNUMERIC_TYPE_P (op_type))
13816 parse_error_context
13817 (wfl_op, "Invalid argument type `%s' to `%s'",
13818 lang_printable_name (op_type, 0), operator_string (node));
13819 TREE_TYPE (node) = error_mark_node;
13824 /* Before the addition, binary numeric promotion is performed on
13825 both operands, if really necessary */
13826 if (JINTEGRAL_TYPE_P (op_type))
13828 value = build_int_2 (1, 0);
13829 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13833 value = build_int_2 (1, 0);
13835 binary_numeric_promotion (op_type,
13836 TREE_TYPE (value), &op, &value);
13839 /* We remember we might be accessing an outer field */
13840 if (outer_field_flag)
13842 /* We re-generate an access to the field */
13843 value = build (PLUS_EXPR, TREE_TYPE (op),
13844 build_outer_field_access (wfl_op, decl), value);
13846 /* And we patch the original access$() into a write
13847 with plus_op as a rhs */
13848 return outer_field_access_fix (node, op, value);
13851 /* And write back into the node. */
13852 TREE_OPERAND (node, 0) = op;
13853 TREE_OPERAND (node, 1) = value;
13854 /* Convert the overall back into its original type, if
13855 necessary, and return */
13856 if (JINTEGRAL_TYPE_P (op_type))
13857 return fold (node);
13859 return fold (convert (op_type, node));
13863 /* 15.14.3 Unary Plus Operator + */
13864 case UNARY_PLUS_EXPR:
13865 /* 15.14.4 Unary Minus Operator - */
13867 if (!JNUMERIC_TYPE_P (op_type))
13869 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13870 TREE_TYPE (node) = error_mark_node;
13873 /* Unary numeric promotion is performed on operand */
13876 op = do_unary_numeric_promotion (op);
13877 prom_type = TREE_TYPE (op);
13878 if (code == UNARY_PLUS_EXPR)
13883 /* 15.14.5 Bitwise Complement Operator ~ */
13885 if (!JINTEGRAL_TYPE_P (op_type))
13887 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13888 TREE_TYPE (node) = error_mark_node;
13893 op = do_unary_numeric_promotion (op);
13894 prom_type = TREE_TYPE (op);
13898 /* 15.14.6 Logical Complement Operator ! */
13899 case TRUTH_NOT_EXPR:
13900 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13902 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
13903 /* But the type is known. We will report an error if further
13904 attempt of a assignment is made with this rhs */
13905 TREE_TYPE (node) = boolean_type_node;
13909 prom_type = boolean_type_node;
13912 /* 15.15 Cast Expression */
13914 value = patch_cast (node, wfl_operator);
13915 if (value == error_mark_node)
13917 /* If this cast is part of an assignment, we tell the code
13918 that deals with it not to complain about a mismatch,
13919 because things have been cast, anyways */
13920 TREE_TYPE (node) = error_mark_node;
13925 value = fold (value);
13926 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13933 return error_mark_node;
13935 /* There are cases where node has been replaced by something else
13936 and we don't end up returning here: UNARY_PLUS_EXPR,
13937 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
13938 TREE_OPERAND (node, 0) = fold (op);
13939 TREE_TYPE (node) = prom_type;
13940 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
13941 return fold (node);
13944 /* Generic type resolution that sometimes takes place during node
13945 patching. Returned the resolved type or generate an error
13946 message. Return the resolved type or NULL_TREE. */
13949 resolve_type_during_patch (type)
13952 if (unresolved_type_p (type, NULL))
13954 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
13957 parse_error_context (type,
13958 "Class `%s' not found in type declaration",
13959 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13964 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13965 return TREE_TYPE (type_decl);
13970 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
13971 found. Otherwise NODE or something meant to replace it is returned. */
13974 patch_cast (node, wfl_op)
13978 tree op = TREE_OPERAND (node, 0);
13979 tree op_type = TREE_TYPE (op);
13980 tree cast_type = TREE_TYPE (node);
13983 /* First resolve OP_TYPE if unresolved */
13984 if (!(cast_type = resolve_type_during_patch (cast_type)))
13985 return error_mark_node;
13987 /* Check on cast that are proven correct at compile time */
13988 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13991 if (cast_type == op_type)
13994 /* float and double type are converted to the original type main
13995 variant and then to the target type. */
13996 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13997 op = convert (integer_type_node, op);
13999 /* Try widening/narowwing convertion. Potentially, things need
14000 to be worked out in gcc so we implement the extreme cases
14001 correctly. fold_convert() needs to be fixed. */
14002 return convert (cast_type, op);
14005 /* It's also valid to cast a boolean into a boolean */
14006 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14009 /* null can be casted to references */
14010 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14011 return build_null_of_type (cast_type);
14013 /* The remaining legal casts involve conversion between reference
14014 types. Check for their compile time correctness. */
14015 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14016 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14018 TREE_TYPE (node) = promote_type (cast_type);
14019 /* Now, the case can be determined correct at compile time if
14020 OP_TYPE can be converted into CAST_TYPE by assignment
14021 conversion (5.2) */
14023 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14025 TREE_SET_CODE (node, NOP_EXPR);
14029 if (flag_emit_class_files)
14031 TREE_SET_CODE (node, CONVERT_EXPR);
14035 /* The cast requires a run-time check */
14036 return build (CALL_EXPR, promote_type (cast_type),
14037 build_address_of (soft_checkcast_node),
14038 tree_cons (NULL_TREE, build_class_ref (cast_type),
14039 build_tree_list (NULL_TREE, op)),
14043 /* Any other casts are proven incorrect at compile time */
14044 t1 = xstrdup (lang_printable_name (op_type, 0));
14045 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14046 t1, lang_printable_name (cast_type, 0));
14048 return error_mark_node;
14051 /* Build a null constant and give it the type TYPE. */
14054 build_null_of_type (type)
14057 tree node = build_int_2 (0, 0);
14058 TREE_TYPE (node) = promote_type (type);
14062 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14063 a list of indices. */
14065 build_array_ref (location, array, index)
14069 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14070 EXPR_WFL_LINECOL (node) = location;
14074 /* 15.12 Array Access Expression */
14077 patch_array_ref (node)
14080 tree array = TREE_OPERAND (node, 0);
14081 tree array_type = TREE_TYPE (array);
14082 tree index = TREE_OPERAND (node, 1);
14083 tree index_type = TREE_TYPE (index);
14084 int error_found = 0;
14086 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14088 if (TREE_CODE (array_type) == POINTER_TYPE)
14089 array_type = TREE_TYPE (array_type);
14091 /* The array reference must be an array */
14092 if (!TYPE_ARRAY_P (array_type))
14094 parse_error_context
14096 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14097 lang_printable_name (array_type, 0));
14098 TREE_TYPE (node) = error_mark_node;
14102 /* The array index undergoes unary numeric promotion. The promoted
14103 type must be int */
14104 index = do_unary_numeric_promotion (index);
14105 if (TREE_TYPE (index) != int_type_node)
14107 if (valid_cast_to_p (index_type, int_type_node))
14108 parse_error_context (wfl_operator,
14109 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14110 lang_printable_name (index_type, 0));
14112 parse_error_context (wfl_operator,
14113 "Incompatible type for `[]'. Can't convert `%s' to `int'",
14114 lang_printable_name (index_type, 0));
14115 TREE_TYPE (node) = error_mark_node;
14120 return error_mark_node;
14122 array_type = TYPE_ARRAY_ELEMENT (array_type);
14124 if (flag_emit_class_files || flag_emit_xref)
14126 TREE_OPERAND (node, 0) = array;
14127 TREE_OPERAND (node, 1) = index;
14131 /* The save_expr is for correct evaluation order. It would be cleaner
14132 to use force_evaluation_order (see comment there), but that is
14133 difficult when we also have to deal with bounds checking. */
14134 if (TREE_SIDE_EFFECTS (index))
14135 array = save_expr (array);
14136 node = build_java_arrayaccess (array, array_type, index);
14137 if (TREE_SIDE_EFFECTS (index))
14138 node = build (COMPOUND_EXPR, array_type, array, node);
14140 TREE_TYPE (node) = array_type;
14144 /* 15.9 Array Creation Expressions */
14147 build_newarray_node (type, dims, extra_dims)
14153 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14154 build_int_2 (extra_dims, 0));
14159 patch_newarray (node)
14162 tree type = TREE_OPERAND (node, 0);
14163 tree dims = TREE_OPERAND (node, 1);
14164 tree cdim, array_type;
14165 int error_found = 0;
14167 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14169 /* Dimension types are verified. It's better for the types to be
14170 verified in order. */
14171 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14174 tree dim = TREE_VALUE (cdim);
14176 /* Dim might have been saved during its evaluation */
14177 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14179 /* The type of each specified dimension must be an integral type. */
14180 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14183 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14184 promoted type must be int. */
14187 dim = do_unary_numeric_promotion (dim);
14188 if (TREE_TYPE (dim) != int_type_node)
14192 /* Report errors on types here */
14195 parse_error_context
14196 (TREE_PURPOSE (cdim),
14197 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14198 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14199 "Explicit cast needed to" : "Can't"),
14200 lang_printable_name (TREE_TYPE (dim), 0));
14204 TREE_PURPOSE (cdim) = NULL_TREE;
14207 /* Resolve array base type if unresolved */
14208 if (!(type = resolve_type_during_patch (type)))
14213 /* We don't want further evaluation of this bogus array creation
14215 TREE_TYPE (node) = error_mark_node;
14216 return error_mark_node;
14219 /* Set array_type to the actual (promoted) array type of the result. */
14220 if (TREE_CODE (type) == RECORD_TYPE)
14221 type = build_pointer_type (type);
14222 while (--xdims >= 0)
14224 type = promote_type (build_java_array_type (type, -1));
14226 dims = nreverse (dims);
14228 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14232 = build_java_array_type (type,
14233 TREE_CODE (cdim) == INTEGER_CST
14234 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14236 array_type = promote_type (array_type);
14238 dims = nreverse (dims);
14240 /* The node is transformed into a function call. Things are done
14241 differently according to the number of dimensions. If the number
14242 of dimension is equal to 1, then the nature of the base type
14243 (primitive or not) matters. */
14245 return build_new_array (type, TREE_VALUE (dims));
14247 /* Can't reuse what's already written in expr.c because it uses the
14248 JVM stack representation. Provide a build_multianewarray. FIXME */
14249 return build (CALL_EXPR, array_type,
14250 build_address_of (soft_multianewarray_node),
14251 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14252 tree_cons (NULL_TREE,
14253 build_int_2 (ndims, 0), dims )),
14257 /* 10.6 Array initializer. */
14259 /* Build a wfl for array element that don't have one, so we can
14260 pin-point errors. */
14263 maybe_build_array_element_wfl (node)
14266 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14267 return build_expr_wfl (NULL_TREE, ctxp->filename,
14268 ctxp->elc.line, ctxp->elc.prev_col);
14273 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14274 identification of initialized arrays easier to detect during walk
14278 build_new_array_init (location, values)
14282 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14283 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14284 EXPR_WFL_LINECOL (to_return) = location;
14288 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14289 occurred. Otherwise return NODE after having set its type
14293 patch_new_array_init (type, node)
14296 int error_seen = 0;
14297 tree current, element_type;
14298 HOST_WIDE_INT length;
14299 int all_constant = 1;
14300 tree init = TREE_OPERAND (node, 0);
14302 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14304 parse_error_context (node,
14305 "Invalid array initializer for non-array type `%s'",
14306 lang_printable_name (type, 1));
14307 return error_mark_node;
14309 type = TREE_TYPE (type);
14310 element_type = TYPE_ARRAY_ELEMENT (type);
14312 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14314 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14315 current; length++, current = TREE_CHAIN (current))
14317 tree elt = TREE_VALUE (current);
14318 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14320 error_seen |= array_constructor_check_entry (element_type, current);
14321 elt = TREE_VALUE (current);
14322 /* When compiling to native code, STRING_CST is converted to
14323 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14324 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14329 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14330 TREE_PURPOSE (current) = NULL_TREE;
14333 if (elt && TREE_CODE (elt) == TREE_LIST
14334 && TREE_VALUE (elt) == error_mark_node)
14339 return error_mark_node;
14341 /* Create a new type. We can't reuse the one we have here by
14342 patching its dimension because it originally is of dimension -1
14343 hence reused by gcc. This would prevent triangular arrays. */
14344 type = build_java_array_type (element_type, length);
14345 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14346 TREE_TYPE (node) = promote_type (type);
14347 TREE_CONSTANT (init) = all_constant;
14348 TREE_CONSTANT (node) = all_constant;
14352 /* Verify that one entry of the initializer element list can be
14353 assigned to the array base type. Report 1 if an error occurred, 0
14357 array_constructor_check_entry (type, entry)
14360 char *array_type_string = NULL; /* For error reports */
14361 tree value, type_value, new_value, wfl_value, patched;
14362 int error_seen = 0;
14364 new_value = NULL_TREE;
14365 wfl_value = TREE_VALUE (entry);
14367 value = java_complete_tree (TREE_VALUE (entry));
14368 /* patch_string return error_mark_node if arg is error_mark_node */
14369 if ((patched = patch_string (value)))
14371 if (value == error_mark_node)
14374 type_value = TREE_TYPE (value);
14376 /* At anytime, try_builtin_assignconv can report a warning on
14377 constant overflow during narrowing. */
14378 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14379 new_value = try_builtin_assignconv (wfl_operator, type, value);
14380 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14381 type_value = promote_type (type);
14383 /* Check and report errors */
14386 const char *msg = (!valid_cast_to_p (type_value, type) ?
14387 "Can't" : "Explicit cast needed to");
14388 if (!array_type_string)
14389 array_type_string = xstrdup (lang_printable_name (type, 1));
14390 parse_error_context
14391 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14392 msg, lang_printable_name (type_value, 1), array_type_string);
14398 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
14399 TREE_VALUE (entry) = new_value;
14402 if (array_type_string)
14403 free (array_type_string);
14405 TREE_PURPOSE (entry) = NULL_TREE;
14410 build_this (location)
14413 tree node = build_wfl_node (this_identifier_node);
14414 TREE_SET_CODE (node, THIS_EXPR);
14415 EXPR_WFL_LINECOL (node) = location;
14419 /* 14.15 The return statement. It builds a modify expression that
14420 assigns the returned value to the RESULT_DECL that hold the value
14424 build_return (location, op)
14428 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14429 EXPR_WFL_LINECOL (node) = location;
14430 node = build_debugable_stmt (location, node);
14435 patch_return (node)
14438 tree return_exp = TREE_OPERAND (node, 0);
14439 tree meth = current_function_decl;
14440 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14441 int error_found = 0;
14443 TREE_TYPE (node) = error_mark_node;
14444 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14446 /* It's invalid to have a return value within a function that is
14447 declared with the keyword void or that is a constructor */
14448 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14451 /* It's invalid to use a return statement in a static block */
14452 if (DECL_CLINIT_P (current_function_decl))
14455 /* It's invalid to have a no return value within a function that
14456 isn't declared with the keyword `void' */
14457 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14460 if (in_instance_initializer)
14465 if (in_instance_initializer)
14466 parse_error_context (wfl_operator,
14467 "`return' inside instance initializer");
14469 else if (DECL_CLINIT_P (current_function_decl))
14470 parse_error_context (wfl_operator,
14471 "`return' inside static initializer");
14473 else if (!DECL_CONSTRUCTOR_P (meth))
14475 char *t = xstrdup (lang_printable_name (mtype, 0));
14476 parse_error_context (wfl_operator,
14477 "`return' with%s value from `%s %s'",
14478 (error_found == 1 ? "" : "out"),
14479 t, lang_printable_name (meth, 0));
14483 parse_error_context (wfl_operator,
14484 "`return' with value from constructor `%s'",
14485 lang_printable_name (meth, 0));
14486 return error_mark_node;
14489 /* If we have a return_exp, build a modify expression and expand
14490 it. Note: at that point, the assignment is declared valid, but we
14491 may want to carry some more hacks */
14494 tree exp = java_complete_tree (return_exp);
14495 tree modify, patched;
14497 /* If the function returned value and EXP are booleans, EXP has
14498 to be converted into the type of DECL_RESULT, which is integer
14499 (see complete_start_java_method) */
14500 if (TREE_TYPE (exp) == boolean_type_node &&
14501 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14502 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14504 /* `null' can be assigned to a function returning a reference */
14505 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14506 exp == null_pointer_node)
14507 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14509 if ((patched = patch_string (exp)))
14512 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14513 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14514 modify = java_complete_tree (modify);
14516 if (modify != error_mark_node)
14518 TREE_SIDE_EFFECTS (modify) = 1;
14519 TREE_OPERAND (node, 0) = modify;
14522 return error_mark_node;
14524 TREE_TYPE (node) = void_type_node;
14525 TREE_SIDE_EFFECTS (node) = 1;
14529 /* 14.8 The if Statement */
14532 build_if_else_statement (location, expression, if_body, else_body)
14534 tree expression, if_body, else_body;
14538 else_body = empty_stmt_node;
14539 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14540 EXPR_WFL_LINECOL (node) = location;
14541 node = build_debugable_stmt (location, node);
14546 patch_if_else_statement (node)
14549 tree expression = TREE_OPERAND (node, 0);
14551 TREE_TYPE (node) = error_mark_node;
14552 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14554 /* The type of expression must be boolean */
14555 if (TREE_TYPE (expression) != boolean_type_node
14556 && TREE_TYPE (expression) != promoted_boolean_type_node)
14558 parse_error_context
14560 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14561 lang_printable_name (TREE_TYPE (expression), 0));
14562 return error_mark_node;
14565 TREE_TYPE (node) = void_type_node;
14566 TREE_SIDE_EFFECTS (node) = 1;
14567 CAN_COMPLETE_NORMALLY (node)
14568 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14569 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
14573 /* 14.6 Labeled Statements */
14575 /* Action taken when a lableled statement is parsed. a new
14576 LABELED_BLOCK_EXPR is created. No statement is attached to the
14577 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14580 build_labeled_block (location, label)
14585 tree label_decl, node;
14586 if (label == NULL_TREE || label == continue_identifier_node)
14587 label_name = label;
14590 label_name = merge_qualified_name (label_id, label);
14591 /* Issue an error if we try to reuse a label that was previously
14593 if (IDENTIFIER_LOCAL_VALUE (label_name))
14595 EXPR_WFL_LINECOL (wfl_operator) = location;
14596 parse_error_context (wfl_operator,
14597 "Declaration of `%s' shadows a previous label declaration",
14598 IDENTIFIER_POINTER (label));
14599 EXPR_WFL_LINECOL (wfl_operator) =
14600 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14601 parse_error_context (wfl_operator,
14602 "This is the location of the previous declaration of label `%s'",
14603 IDENTIFIER_POINTER (label));
14604 java_error_count--;
14608 label_decl = create_label_decl (label_name);
14609 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14610 EXPR_WFL_LINECOL (node) = location;
14611 TREE_SIDE_EFFECTS (node) = 1;
14615 /* A labeled statement LBE is attached a statement. */
14618 finish_labeled_statement (lbe, statement)
14619 tree lbe; /* Labeled block expr */
14622 /* In anyways, tie the loop to its statement */
14623 LABELED_BLOCK_BODY (lbe) = statement;
14624 pop_labeled_block ();
14625 POP_LABELED_BLOCK ();
14629 /* 14.10, 14.11, 14.12 Loop Statements */
14631 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14635 build_new_loop (loop_body)
14638 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14639 TREE_SIDE_EFFECTS (loop) = 1;
14644 /* Create a loop body according to the following structure:
14646 COMPOUND_EXPR (loop main body)
14647 EXIT_EXPR (this order is for while/for loops.
14648 LABELED_BLOCK_EXPR the order is reversed for do loops)
14649 LABEL_DECL (a continue occuring here branches at the
14650 BODY end of this labeled block)
14653 REVERSED, if non zero, tells that the loop condition expr comes
14654 after the body, like in the do-while loop.
14656 To obtain a loop, the loop body structure described above is
14657 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14660 LABEL_DECL (use this label to exit the loop)
14662 <structure described above> */
14665 build_loop_body (location, condition, reversed)
14670 tree first, second, body;
14672 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14673 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14674 condition = build_debugable_stmt (location, condition);
14675 TREE_SIDE_EFFECTS (condition) = 1;
14677 body = build_labeled_block (0, continue_identifier_node);
14678 first = (reversed ? body : condition);
14679 second = (reversed ? condition : body);
14681 build (COMPOUND_EXPR, NULL_TREE,
14682 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14685 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14686 their order) on the current loop. Unlink the current loop from the
14690 finish_loop_body (location, condition, body, reversed)
14692 tree condition, body;
14695 tree to_return = ctxp->current_loop;
14696 tree loop_body = LOOP_EXPR_BODY (to_return);
14699 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14700 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14701 The real EXIT_EXPR is one operand further. */
14702 EXPR_WFL_LINECOL (cnode) = location;
14703 /* This one is for accurate error reports */
14704 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14705 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14707 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14712 /* Tailored version of finish_loop_body for FOR loops, when FOR
14713 loops feature the condition part */
14716 finish_for_loop (location, condition, update, body)
14718 tree condition, update, body;
14720 /* Put the condition and the loop body in place */
14721 tree loop = finish_loop_body (location, condition, body, 0);
14722 /* LOOP is the current loop which has been now popped of the loop
14723 stack. Install the update block */
14724 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14728 /* Try to find the loop a block might be related to. This comprises
14729 the case where the LOOP_EXPR is found as the second operand of a
14730 COMPOUND_EXPR, because the loop happens to have an initialization
14731 part, then expressed as the first operand of the COMPOUND_EXPR. If
14732 the search finds something, 1 is returned. Otherwise, 0 is
14733 returned. The search is assumed to start from a
14734 LABELED_BLOCK_EXPR's block. */
14737 search_loop (statement)
14740 if (TREE_CODE (statement) == LOOP_EXPR)
14743 if (TREE_CODE (statement) == BLOCK)
14744 statement = BLOCK_SUBBLOCKS (statement);
14748 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14749 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14750 statement = TREE_OPERAND (statement, 1);
14752 return (TREE_CODE (statement) == LOOP_EXPR
14753 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14756 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14757 returned otherwise. */
14760 labeled_block_contains_loop_p (block, loop)
14766 if (LABELED_BLOCK_BODY (block) == loop)
14769 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14775 /* If the loop isn't surrounded by a labeled statement, create one and
14776 insert LOOP as its body. */
14779 patch_loop_statement (loop)
14784 TREE_TYPE (loop) = void_type_node;
14785 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14788 loop_label = build_labeled_block (0, NULL_TREE);
14789 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14790 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14791 LABELED_BLOCK_BODY (loop_label) = loop;
14792 PUSH_LABELED_BLOCK (loop_label);
14796 /* 14.13, 14.14: break and continue Statements */
14798 /* Build a break or a continue statement. a null NAME indicates an
14799 unlabeled break/continue statement. */
14802 build_bc_statement (location, is_break, name)
14803 int location, is_break;
14806 tree break_continue, label_block_expr = NULL_TREE;
14810 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14811 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14812 /* Null means that we don't have a target for this named
14813 break/continue. In this case, we make the target to be the
14814 label name, so that the error can be reported accuratly in
14815 patch_bc_statement. */
14816 label_block_expr = EXPR_WFL_NODE (name);
14818 /* Unlabeled break/continue will be handled during the
14819 break/continue patch operation */
14821 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14823 IS_BREAK_STMT_P (break_continue) = is_break;
14824 TREE_SIDE_EFFECTS (break_continue) = 1;
14825 EXPR_WFL_LINECOL (break_continue) = location;
14826 break_continue = build_debugable_stmt (location, break_continue);
14827 return break_continue;
14830 /* Verification of a break/continue statement. */
14833 patch_bc_statement (node)
14836 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14837 tree labeled_block = ctxp->current_labeled_block;
14838 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14840 /* Having an identifier here means that the target is unknown. */
14841 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14843 parse_error_context (wfl_operator, "No label definition found for `%s'",
14844 IDENTIFIER_POINTER (bc_label));
14845 return error_mark_node;
14847 if (! IS_BREAK_STMT_P (node))
14849 /* It's a continue statement. */
14850 for (;; labeled_block = TREE_CHAIN (labeled_block))
14852 if (labeled_block == NULL_TREE)
14854 if (bc_label == NULL_TREE)
14855 parse_error_context (wfl_operator,
14856 "`continue' must be in loop");
14858 parse_error_context
14859 (wfl_operator, "continue label `%s' does not name a loop",
14860 IDENTIFIER_POINTER (bc_label));
14861 return error_mark_node;
14863 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14864 == continue_identifier_node)
14865 && (bc_label == NULL_TREE
14866 || TREE_CHAIN (labeled_block) == bc_label))
14868 bc_label = labeled_block;
14873 else if (!bc_label)
14875 for (;; labeled_block = TREE_CHAIN (labeled_block))
14877 if (labeled_block == NULL_TREE)
14879 parse_error_context (wfl_operator,
14880 "`break' must be in loop or switch");
14881 return error_mark_node;
14883 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14884 if (TREE_CODE (target_stmt) == SWITCH_EXPR
14885 || search_loop (target_stmt))
14887 bc_label = labeled_block;
14893 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14894 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14896 /* Our break/continue don't return values. */
14897 TREE_TYPE (node) = void_type_node;
14898 /* Encapsulate the break within a compound statement so that it's
14899 expanded all the times by expand_expr (and not clobbered
14900 sometimes, like after a if statement) */
14901 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14902 TREE_SIDE_EFFECTS (node) = 1;
14906 /* Process the exit expression belonging to a loop. Its type must be
14910 patch_exit_expr (node)
14913 tree expression = TREE_OPERAND (node, 0);
14914 TREE_TYPE (node) = error_mark_node;
14915 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14917 /* The type of expression must be boolean */
14918 if (TREE_TYPE (expression) != boolean_type_node)
14920 parse_error_context
14922 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
14923 lang_printable_name (TREE_TYPE (expression), 0));
14924 return error_mark_node;
14926 /* Now we know things are allright, invert the condition, fold and
14928 TREE_OPERAND (node, 0) =
14929 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
14931 if (! integer_zerop (TREE_OPERAND (node, 0))
14932 && ctxp->current_loop != NULL_TREE
14933 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14934 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14935 if (! integer_onep (TREE_OPERAND (node, 0)))
14936 CAN_COMPLETE_NORMALLY (node) = 1;
14939 TREE_TYPE (node) = void_type_node;
14943 /* 14.9 Switch statement */
14946 patch_switch_statement (node)
14949 tree se = TREE_OPERAND (node, 0), se_type;
14951 /* Complete the switch expression */
14952 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14953 se_type = TREE_TYPE (se);
14954 /* The type of the switch expression must be char, byte, short or
14956 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
14958 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14959 parse_error_context (wfl_operator,
14960 "Incompatible type for `switch'. Can't convert `%s' to `int'",
14961 lang_printable_name (se_type, 0));
14962 /* This is what java_complete_tree will check */
14963 TREE_OPERAND (node, 0) = error_mark_node;
14964 return error_mark_node;
14967 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
14969 /* Ready to return */
14970 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
14972 TREE_TYPE (node) = error_mark_node;
14973 return error_mark_node;
14975 TREE_TYPE (node) = void_type_node;
14976 TREE_SIDE_EFFECTS (node) = 1;
14977 CAN_COMPLETE_NORMALLY (node)
14978 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14979 || ! SWITCH_HAS_DEFAULT (node);
14983 /* 14.18 The try/catch statements */
14986 build_try_statement (location, try_block, catches)
14988 tree try_block, catches;
14990 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
14991 EXPR_WFL_LINECOL (node) = location;
14996 build_try_finally_statement (location, try_block, finally)
14998 tree try_block, finally;
15000 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15001 EXPR_WFL_LINECOL (node) = location;
15006 patch_try_statement (node)
15009 int error_found = 0;
15010 tree try = TREE_OPERAND (node, 0);
15011 /* Exception handlers are considered in left to right order */
15012 tree catch = nreverse (TREE_OPERAND (node, 1));
15013 tree current, caught_type_list = NULL_TREE;
15015 /* Check catch clauses, if any. Every time we find an error, we try
15016 to process the next catch clause. We process the catch clause before
15017 the try block so that when processing the try block we can check thrown
15018 exceptions againts the caught type list. */
15019 for (current = catch; current; current = TREE_CHAIN (current))
15021 tree carg_decl, carg_type;
15022 tree sub_current, catch_block, catch_clause;
15025 /* At this point, the structure of the catch clause is
15026 CATCH_EXPR (catch node)
15027 BLOCK (with the decl of the parameter)
15029 MODIFY_EXPR (assignment of the catch parameter)
15030 BLOCK (catch clause block)
15032 catch_clause = TREE_OPERAND (current, 0);
15033 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15034 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15036 /* Catch clauses can't have more than one parameter declared,
15037 but it's already enforced by the grammar. Make sure that the
15038 only parameter of the clause statement in of class Throwable
15039 or a subclass of Throwable, but that was done earlier. The
15040 catch clause parameter type has also been resolved. */
15042 /* Just make sure that the catch clause parameter type inherits
15043 from java.lang.Throwable */
15044 if (!inherits_from_p (carg_type, throwable_type_node))
15046 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15047 parse_error_context (wfl_operator,
15048 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15049 lang_printable_name (carg_type, 0));
15054 /* Partial check for unreachable catch statement: The catch
15055 clause is reachable iff is no earlier catch block A in
15056 the try statement such that the type of the catch
15057 clause's parameter is the same as or a subclass of the
15058 type of A's parameter */
15060 for (sub_current = catch;
15061 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15063 tree sub_catch_clause, decl;
15064 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15065 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15067 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15069 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15070 parse_error_context
15072 "`catch' not reached because of the catch clause at line %d",
15073 EXPR_WFL_LINENO (sub_current));
15074 unreachable = error_found = 1;
15078 /* Complete the catch clause block */
15079 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15080 if (catch_block == error_mark_node)
15085 if (CAN_COMPLETE_NORMALLY (catch_block))
15086 CAN_COMPLETE_NORMALLY (node) = 1;
15087 TREE_OPERAND (current, 0) = catch_block;
15092 /* Things to do here: the exception must be thrown */
15094 /* Link this type to the caught type list */
15095 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15098 PUSH_EXCEPTIONS (caught_type_list);
15099 if ((try = java_complete_tree (try)) == error_mark_node)
15101 if (CAN_COMPLETE_NORMALLY (try))
15102 CAN_COMPLETE_NORMALLY (node) = 1;
15105 /* Verification ends here */
15107 return error_mark_node;
15109 TREE_OPERAND (node, 0) = try;
15110 TREE_OPERAND (node, 1) = catch;
15111 TREE_TYPE (node) = void_type_node;
15115 /* 14.17 The synchronized Statement */
15118 patch_synchronized_statement (node, wfl_op1)
15119 tree node, wfl_op1;
15121 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15122 tree block = TREE_OPERAND (node, 1);
15124 tree tmp, enter, exit, expr_decl, assignment;
15126 if (expr == error_mark_node)
15128 block = java_complete_tree (block);
15132 /* We might be trying to synchronize on a STRING_CST */
15133 if ((tmp = patch_string (expr)))
15136 /* The TYPE of expr must be a reference type */
15137 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15139 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15140 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15141 lang_printable_name (TREE_TYPE (expr), 0));
15142 return error_mark_node;
15145 if (flag_emit_xref)
15147 TREE_OPERAND (node, 0) = expr;
15148 TREE_OPERAND (node, 1) = java_complete_tree (block);
15149 CAN_COMPLETE_NORMALLY (node) = 1;
15153 /* Generate a try-finally for the synchronized statement, except
15154 that the handler that catches all throw exception calls
15155 _Jv_MonitorExit and then rethrow the exception.
15156 The synchronized statement is then implemented as:
15159 _Jv_MonitorEnter (expression)
15161 _Jv_MonitorExit (expression)
15165 e = _Jv_exception_info ();
15166 _Jv_MonitorExit (expression)
15170 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15171 BUILD_MONITOR_ENTER (enter, expr_decl);
15172 BUILD_MONITOR_EXIT (exit, expr_decl);
15173 CAN_COMPLETE_NORMALLY (enter) = 1;
15174 CAN_COMPLETE_NORMALLY (exit) = 1;
15175 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15176 TREE_SIDE_EFFECTS (assignment) = 1;
15177 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15178 build (COMPOUND_EXPR, NULL_TREE,
15179 build (WITH_CLEANUP_EXPR, NULL_TREE,
15180 build (COMPOUND_EXPR, NULL_TREE,
15181 assignment, enter),
15184 node = build_expr_block (node, expr_decl);
15186 return java_complete_tree (node);
15189 /* 14.16 The throw Statement */
15192 patch_throw_statement (node, wfl_op1)
15193 tree node, wfl_op1;
15195 tree expr = TREE_OPERAND (node, 0);
15196 tree type = TREE_TYPE (expr);
15197 int unchecked_ok = 0, tryblock_throws_ok = 0;
15199 /* Thrown expression must be assignable to java.lang.Throwable */
15200 if (!try_reference_assignconv (throwable_type_node, expr))
15202 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15203 parse_error_context (wfl_operator,
15204 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15205 lang_printable_name (type, 0));
15206 /* If the thrown expression was a reference, we further the
15207 compile-time check. */
15208 if (!JREFERENCE_TYPE_P (type))
15209 return error_mark_node;
15212 /* At least one of the following must be true */
15214 /* The type of the throw expression is a not checked exception,
15215 i.e. is a unchecked expression. */
15216 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15218 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15219 /* An instance can't throw a checked excetion unless that exception
15220 is explicitely declared in the `throws' clause of each
15221 constructor. This doesn't apply to anonymous classes, since they
15222 don't have declared constructors. */
15224 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15227 for (current = TYPE_METHODS (current_class); current;
15228 current = TREE_CHAIN (current))
15229 if (DECL_CONSTRUCTOR_P (current)
15230 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15232 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)",
15233 lang_printable_name (TREE_TYPE (expr), 0));
15234 return error_mark_node;
15238 /* Throw is contained in a try statement and at least one catch
15239 clause can receive the thrown expression or the current method is
15240 declared to throw such an exception. Or, the throw statement is
15241 contained in a method or constructor declaration and the type of
15242 the Expression is assignable to at least one type listed in the
15243 throws clause the declaration. */
15245 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15246 if (!(unchecked_ok || tryblock_throws_ok))
15248 /* If there is a surrounding try block that has no matching
15249 clatch clause, report it first. A surrounding try block exits
15250 only if there is something after the list of checked
15251 exception thrown by the current function (if any). */
15252 if (IN_TRY_BLOCK_P ())
15253 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15254 lang_printable_name (type, 0));
15255 /* If we have no surrounding try statement and the method doesn't have
15256 any throws, report it now. FIXME */
15258 /* We report that the exception can't be throw from a try block
15259 in all circumstances but when the `throw' is inside a static
15261 else if (!EXCEPTIONS_P (currently_caught_type_list)
15262 && !tryblock_throws_ok)
15264 if (DECL_CLINIT_P (current_function_decl))
15265 parse_error_context (wfl_operator,
15266 "Checked exception `%s' can't be thrown in initializer",
15267 lang_printable_name (type, 0));
15269 parse_error_context (wfl_operator,
15270 "Checked exception `%s' isn't thrown from a `try' block",
15271 lang_printable_name (type, 0));
15273 /* Otherwise, the current method doesn't have the appropriate
15274 throws declaration */
15276 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15277 lang_printable_name (type, 0));
15278 return error_mark_node;
15281 if (! flag_emit_class_files && ! flag_emit_xref)
15282 BUILD_THROW (node, expr);
15284 /* If doing xrefs, keep the location where the `throw' was seen. */
15285 if (flag_emit_xref)
15286 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15290 /* Check that exception said to be thrown by method DECL can be
15291 effectively caught from where DECL is invoked. */
15294 check_thrown_exceptions (location, decl)
15299 /* For all the unchecked exceptions thrown by DECL */
15300 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15301 throws = TREE_CHAIN (throws))
15302 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15305 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15306 if (DECL_NAME (decl) == get_identifier ("clone"))
15309 EXPR_WFL_LINECOL (wfl_operator) = location;
15310 if (DECL_FINIT_P (current_function_decl))
15311 parse_error_context
15312 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15313 lang_printable_name (TREE_VALUE (throws), 0));
15316 parse_error_context
15317 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15318 lang_printable_name (TREE_VALUE (throws), 0),
15319 (DECL_INIT_P (current_function_decl) ?
15320 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15321 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15326 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15327 try-catch blocks, OR is listed in the `throws' clause of the
15331 check_thrown_exceptions_do (exception)
15334 tree list = currently_caught_type_list;
15335 resolve_and_layout (exception, NULL_TREE);
15336 /* First, all the nested try-catch-finally at that stage. The
15337 last element contains `throws' clause exceptions, if any. */
15338 if (IS_UNCHECKED_EXCEPTION_P (exception))
15343 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15344 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15346 list = TREE_CHAIN (list);
15352 purge_unchecked_exceptions (mdecl)
15355 tree throws = DECL_FUNCTION_THROWS (mdecl);
15356 tree new = NULL_TREE;
15360 tree next = TREE_CHAIN (throws);
15361 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15363 TREE_CHAIN (throws) = new;
15368 /* List is inverted here, but it doesn't matter */
15369 DECL_FUNCTION_THROWS (mdecl) = new;
15372 /* 15.24 Conditional Operator ?: */
15375 patch_conditional_expr (node, wfl_cond, wfl_op1)
15376 tree node, wfl_cond, wfl_op1;
15378 tree cond = TREE_OPERAND (node, 0);
15379 tree op1 = TREE_OPERAND (node, 1);
15380 tree op2 = TREE_OPERAND (node, 2);
15381 tree resulting_type = NULL_TREE;
15382 tree t1, t2, patched;
15383 int error_found = 0;
15385 /* Operands of ?: might be StringBuffers crafted as a result of a
15386 string concatenation. Obtain a descent operand here. */
15387 if ((patched = patch_string (op1)))
15388 TREE_OPERAND (node, 1) = op1 = patched;
15389 if ((patched = patch_string (op2)))
15390 TREE_OPERAND (node, 2) = op2 = patched;
15392 t1 = TREE_TYPE (op1);
15393 t2 = TREE_TYPE (op2);
15395 /* The first expression must be a boolean */
15396 if (TREE_TYPE (cond) != boolean_type_node)
15398 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15399 parse_error_context (wfl_operator,
15400 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15401 lang_printable_name (TREE_TYPE (cond), 0));
15405 /* Second and third can be numeric, boolean (i.e. primitive),
15406 references or null. Anything else results in an error */
15407 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15408 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15409 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15410 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15413 /* Determine the type of the conditional expression. Same types are
15414 easy to deal with */
15416 resulting_type = t1;
15418 /* There are different rules for numeric types */
15419 else if (JNUMERIC_TYPE_P (t1))
15421 /* if byte/short found, the resulting type is short */
15422 if ((t1 == byte_type_node && t2 == short_type_node)
15423 || (t1 == short_type_node && t2 == byte_type_node))
15424 resulting_type = short_type_node;
15426 /* If t1 is a constant int and t2 is of type byte, short or char
15427 and t1's value fits in t2, then the resulting type is t2 */
15428 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15429 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15430 resulting_type = t2;
15432 /* If t2 is a constant int and t1 is of type byte, short or char
15433 and t2's value fits in t1, then the resulting type is t1 */
15434 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15435 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15436 resulting_type = t1;
15438 /* Otherwise, binary numeric promotion is applied and the
15439 resulting type is the promoted type of operand 1 and 2 */
15441 resulting_type = binary_numeric_promotion (t1, t2,
15442 &TREE_OPERAND (node, 1),
15443 &TREE_OPERAND (node, 2));
15446 /* Cases of a reference and a null type */
15447 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15448 resulting_type = t1;
15450 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15451 resulting_type = t2;
15453 /* Last case: different reference types. If a type can be converted
15454 into the other one by assignment conversion, the latter
15455 determines the type of the expression */
15456 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15457 resulting_type = promote_type (t1);
15459 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15460 resulting_type = promote_type (t2);
15462 /* If we don't have any resulting type, we're in trouble */
15463 if (!resulting_type)
15465 char *t = xstrdup (lang_printable_name (t1, 0));
15466 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15467 parse_error_context (wfl_operator,
15468 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15469 t, lang_printable_name (t2, 0));
15476 TREE_TYPE (node) = error_mark_node;
15477 return error_mark_node;
15480 TREE_TYPE (node) = resulting_type;
15481 TREE_SET_CODE (node, COND_EXPR);
15482 CAN_COMPLETE_NORMALLY (node) = 1;
15486 /* Try to constant fold NODE.
15487 If NODE is not a constant expression, return NULL_EXPR.
15488 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15491 fold_constant_for_init (node, context)
15495 tree op0, op1, val;
15496 enum tree_code code = TREE_CODE (node);
15498 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
15506 case TRUNC_MOD_EXPR:
15514 case TRUTH_ANDIF_EXPR:
15515 case TRUTH_ORIF_EXPR:
15522 op0 = TREE_OPERAND (node, 0);
15523 op1 = TREE_OPERAND (node, 1);
15524 val = fold_constant_for_init (op0, context);
15525 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15527 TREE_OPERAND (node, 0) = val;
15528 val = fold_constant_for_init (op1, context);
15529 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15531 TREE_OPERAND (node, 1) = val;
15532 return patch_binop (node, op0, op1);
15534 case UNARY_PLUS_EXPR:
15536 case TRUTH_NOT_EXPR:
15539 op0 = TREE_OPERAND (node, 0);
15540 val = fold_constant_for_init (op0, context);
15541 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15543 TREE_OPERAND (node, 0) = val;
15544 return patch_unaryop (node, op0);
15548 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15549 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15551 TREE_OPERAND (node, 0) = val;
15552 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15553 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15555 TREE_OPERAND (node, 1) = val;
15556 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15557 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15559 TREE_OPERAND (node, 2) = val;
15560 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15561 : TREE_OPERAND (node, 2);
15565 if (! FIELD_FINAL (node)
15566 || DECL_INITIAL (node) == NULL_TREE)
15568 val = DECL_INITIAL (node);
15569 /* Guard against infinite recursion. */
15570 DECL_INITIAL (node) = NULL_TREE;
15571 val = fold_constant_for_init (val, node);
15572 DECL_INITIAL (node) = val;
15573 if (!val && CLASS_FINAL_VARIABLE_P (node))
15574 DECL_FIELD_FINAL_IUD (node) = 0;
15577 case EXPR_WITH_FILE_LOCATION:
15578 /* Compare java_complete_tree and resolve_expression_name. */
15579 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15580 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15582 tree name = EXPR_WFL_NODE (node);
15584 if (PRIMARY_P (node))
15586 else if (! QUALIFIED_P (name))
15588 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15589 if (decl == NULL_TREE
15590 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15592 return fold_constant_for_init (decl, decl);
15596 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15597 qualify_ambiguous_name (node);
15598 if (resolve_field_access (node, &decl, NULL)
15599 && decl != NULL_TREE)
15600 return fold_constant_for_init (decl, decl);
15606 op0 = TREE_OPERAND (node, 0);
15607 val = fold_constant_for_init (op0, context);
15608 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15610 TREE_OPERAND (node, 0) = val;
15614 #ifdef USE_COMPONENT_REF
15616 case COMPONENT_REF:
15625 #ifdef USE_COMPONENT_REF
15626 /* Context is 'T' for TypeName, 'P' for PackageName,
15627 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15630 resolve_simple_name (name, context)
15637 resolve_qualified_name (name, context)
15644 /* Mark P, which is really a `struct parser_ctxt **' for GC. */
15647 mark_parser_ctxt (p)
15650 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15657 for (i = 0; i < 11; ++i)
15658 ggc_mark_tree (pc->modifier_ctx[i]);
15659 ggc_mark_tree (pc->class_type);
15660 ggc_mark_tree (pc->function_decl);
15661 ggc_mark_tree (pc->package);
15662 ggc_mark_tree (pc->class_list);
15663 ggc_mark_tree (pc->current_parsed_class);
15664 ggc_mark_tree (pc->current_parsed_class_un);
15665 ggc_mark_tree (pc->non_static_initialized);
15666 ggc_mark_tree (pc->static_initialized);
15667 ggc_mark_tree (pc->instance_initializers);
15668 ggc_mark_tree (pc->import_list);
15669 ggc_mark_tree (pc->import_demand_list);
15670 ggc_mark_tree (pc->current_loop);
15671 ggc_mark_tree (pc->current_labeled_block);
15672 #endif /* JC1_LITE */
15675 mark_parser_ctxt (&pc->next);
15681 /* Register roots with the garbage collector. */
15682 ggc_add_tree_root (src_parse_roots, sizeof (src_parse_roots) / sizeof(tree));