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, 2002, 2003
4 Free Software Foundation, Inc.
5 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc. */
28 /* This file parses java source code and issues a tree node image
29 suitable for code generation (byte code and targeted CPU assembly
32 The grammar conforms to the Java grammar described in "The Java(TM)
33 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
34 1996, ISBN 0-201-63451-1"
36 The following modifications were brought to the original grammar:
38 method_body: added the rule '| block SC_TK'
39 static_initializer: added the rule 'static block SC_TK'.
41 Note: All the extra rules described above should go away when the
42 empty_statement rule will work.
44 statement_nsi: 'nsi' should be read no_short_if.
46 Some rules have been modified to support JDK1.1 inner classes
47 definitions and other extensions. */
52 #include "coretypes.h"
61 #include "java-tree.h"
73 #include "tree-inline.h"
75 /* Local function prototypes */
76 static char *java_accstring_lookup (int);
77 static void classitf_redefinition_error (const char *,tree, tree, tree);
78 static void variable_redefinition_error (tree, tree, tree, int);
79 static tree create_class (int, tree, tree, tree);
80 static tree create_interface (int, tree, tree);
81 static void end_class_declaration (int);
82 static tree find_field (tree, tree);
83 static tree lookup_field_wrapper (tree, tree);
84 static int duplicate_declaration_error_p (tree, tree, tree);
85 static void register_fields (int, tree, tree);
86 static tree parser_qualified_classname (tree);
87 static int parser_check_super (tree, tree, tree);
88 static int parser_check_super_interface (tree, tree, tree);
89 static void check_modifiers_consistency (int);
90 static tree lookup_cl (tree);
91 static tree lookup_java_method2 (tree, tree, int);
92 static tree method_header (int, tree, tree, tree);
93 static void fix_method_argument_names (tree ,tree);
94 static tree method_declarator (tree, tree);
95 static void parse_warning_context (tree cl, const char *msg, ...)
97 static void issue_warning_error_from_context (tree, const char *msg, va_list)
98 ATTRIBUTE_PRINTF (2, 0);
99 static void parse_ctor_invocation_error (void);
100 static tree parse_jdk1_1_error (const char *);
101 static void complete_class_report_errors (jdep *);
102 static int process_imports (void);
103 static void read_import_dir (tree);
104 static int find_in_imports_on_demand (tree, tree);
105 static void find_in_imports (tree, tree);
106 static void check_inner_class_access (tree, tree, tree);
107 static int check_pkg_class_access (tree, tree, bool);
108 static void register_package (tree);
109 static tree resolve_package (tree, tree *, tree *);
110 static tree resolve_class (tree, tree, tree, tree);
111 static void declare_local_variables (int, tree, tree);
112 static void dump_java_tree (enum tree_dump_index, tree);
113 static void source_start_java_method (tree);
114 static void source_end_java_method (void);
115 static tree find_name_in_single_imports (tree);
116 static void check_abstract_method_header (tree);
117 static tree lookup_java_interface_method2 (tree, tree);
118 static tree resolve_expression_name (tree, tree *);
119 static tree maybe_create_class_interface_decl (tree, tree, tree, tree);
120 static int check_class_interface_creation (int, int, tree, tree, tree, tree);
121 static tree patch_method_invocation (tree, tree, tree, int, int *, tree *);
122 static int breakdown_qualified (tree *, tree *, tree);
123 static int in_same_package (tree, tree);
124 static tree resolve_and_layout (tree, tree);
125 static tree qualify_and_find (tree, tree, tree);
126 static tree resolve_no_layout (tree, tree);
127 static int invocation_mode (tree, int);
128 static tree find_applicable_accessible_methods_list (int, tree, tree, tree);
129 static void search_applicable_methods_list (int, tree, tree, tree, tree *, tree *);
130 static tree find_most_specific_methods_list (tree);
131 static int argument_types_convertible (tree, tree);
132 static tree patch_invoke (tree, tree, tree);
133 static int maybe_use_access_method (int, tree *, tree *);
134 static tree lookup_method_invoke (int, tree, tree, tree, tree);
135 static tree register_incomplete_type (int, tree, tree, tree);
136 static tree check_inner_circular_reference (tree, tree);
137 static tree check_circular_reference (tree);
138 static tree obtain_incomplete_type (tree);
139 static tree java_complete_lhs (tree);
140 static tree java_complete_tree (tree);
141 static tree maybe_generate_pre_expand_clinit (tree);
142 static int analyze_clinit_body (tree, tree);
143 static int maybe_yank_clinit (tree);
144 static void start_complete_expand_method (tree);
145 static void java_complete_expand_method (tree);
146 static void java_expand_method_bodies (tree);
147 static int unresolved_type_p (tree, tree *);
148 static void create_jdep_list (struct parser_ctxt *);
149 static tree build_expr_block (tree, tree);
150 static tree enter_block (void);
151 static tree exit_block (void);
152 static tree lookup_name_in_blocks (tree);
153 static void maybe_absorb_scoping_blocks (void);
154 static tree build_method_invocation (tree, tree);
155 static tree build_new_invocation (tree, tree);
156 static tree build_assignment (int, int, tree, tree);
157 static tree build_binop (enum tree_code, int, tree, tree);
158 static tree patch_assignment (tree, tree);
159 static tree patch_binop (tree, tree, tree);
160 static tree build_unaryop (int, int, tree);
161 static tree build_incdec (int, int, tree, int);
162 static tree patch_unaryop (tree, tree);
163 static tree build_cast (int, tree, tree);
164 static tree build_null_of_type (tree);
165 static tree patch_cast (tree, tree);
166 static int valid_ref_assignconv_cast_p (tree, tree, int);
167 static int valid_builtin_assignconv_identity_widening_p (tree, tree);
168 static int valid_cast_to_p (tree, tree);
169 static int valid_method_invocation_conversion_p (tree, tree);
170 static tree try_builtin_assignconv (tree, tree, tree);
171 static tree try_reference_assignconv (tree, tree);
172 static tree build_unresolved_array_type (tree);
173 static int build_type_name_from_array_name (tree, tree *);
174 static tree build_array_from_name (tree, tree, tree, tree *);
175 static tree build_array_ref (int, tree, tree);
176 static tree patch_array_ref (tree);
177 static tree make_qualified_name (tree, tree, int);
178 static tree merge_qualified_name (tree, tree);
179 static tree make_qualified_primary (tree, tree, int);
180 static int resolve_qualified_expression_name (tree, tree *, tree *, tree *);
181 static void qualify_ambiguous_name (tree);
182 static tree resolve_field_access (tree, tree *, tree *);
183 static tree build_newarray_node (tree, tree, int);
184 static tree patch_newarray (tree);
185 static tree resolve_type_during_patch (tree);
186 static tree build_this (int);
187 static tree build_wfl_wrap (tree, int);
188 static tree build_return (int, tree);
189 static tree patch_return (tree);
190 static tree maybe_access_field (tree, tree, tree);
191 static int complete_function_arguments (tree);
192 static int check_for_static_method_reference (tree, tree, tree, tree, tree);
193 static int not_accessible_p (tree, tree, tree, int);
194 static void check_deprecation (tree, tree);
195 static int class_in_current_package (tree);
196 static tree build_if_else_statement (int, tree, tree, tree);
197 static tree patch_if_else_statement (tree);
198 static tree add_stmt_to_compound (tree, tree, tree);
199 static tree add_stmt_to_block (tree, tree, tree);
200 static tree patch_exit_expr (tree);
201 static tree build_labeled_block (int, tree);
202 static tree finish_labeled_statement (tree, tree);
203 static tree build_bc_statement (int, int, tree);
204 static tree patch_bc_statement (tree);
205 static tree patch_loop_statement (tree);
206 static tree build_new_loop (tree);
207 static tree build_loop_body (int, tree, int);
208 static tree finish_loop_body (int, tree, tree, int);
209 static tree build_debugable_stmt (int, tree);
210 static tree finish_for_loop (int, tree, tree, tree);
211 static tree patch_switch_statement (tree);
212 static tree string_constant_concatenation (tree, tree);
213 static tree build_string_concatenation (tree, tree);
214 static tree patch_string_cst (tree);
215 static tree patch_string (tree);
216 static tree encapsulate_with_try_catch (int, tree, tree, tree);
217 static tree build_assertion (int, tree, tree);
218 static tree build_try_statement (int, tree, tree);
219 static tree build_try_finally_statement (int, tree, tree);
220 static tree patch_try_statement (tree);
221 static tree patch_synchronized_statement (tree, tree);
222 static tree patch_throw_statement (tree, tree);
223 static void check_thrown_exceptions (int, tree);
224 static int check_thrown_exceptions_do (tree);
225 static void purge_unchecked_exceptions (tree);
226 static bool ctors_unchecked_throws_clause_p (tree);
227 static void check_throws_clauses (tree, tree, tree);
228 static void finish_method_declaration (tree);
229 static tree build_super_invocation (tree);
230 static int verify_constructor_circularity (tree, tree);
231 static char *constructor_circularity_msg (tree, tree);
232 static tree build_this_super_qualified_invocation (int, tree, tree, int, int);
233 static const char *get_printable_method_name (tree);
234 static tree patch_conditional_expr (tree, tree, tree);
235 static tree generate_finit (tree);
236 static tree generate_instinit (tree);
237 static tree build_instinit_invocation (tree);
238 static void fix_constructors (tree);
239 static tree build_alias_initializer_parameter_list (int, tree, tree, int *);
240 static tree craft_constructor (tree, tree);
241 static int verify_constructor_super (tree);
242 static tree create_artificial_method (tree, int, tree, tree, tree);
243 static void start_artificial_method_body (tree);
244 static void end_artificial_method_body (tree);
245 static int check_method_redefinition (tree, tree);
246 static int check_method_types_complete (tree);
247 static void java_check_regular_methods (tree);
248 static void java_check_abstract_methods (tree);
249 static void unreachable_stmt_error (tree);
250 static tree find_expr_with_wfl (tree);
251 static void missing_return_error (tree);
252 static tree build_new_array_init (int, tree);
253 static tree patch_new_array_init (tree, tree);
254 static tree maybe_build_array_element_wfl (tree);
255 static int array_constructor_check_entry (tree, tree);
256 static const char *purify_type_name (const char *);
257 static tree fold_constant_for_init (tree, tree);
258 static tree strip_out_static_field_access_decl (tree);
259 static jdeplist *reverse_jdep_list (struct parser_ctxt *);
260 static void static_ref_err (tree, tree, tree);
261 static void parser_add_interface (tree, tree, tree);
262 static void add_superinterfaces (tree, tree);
263 static tree jdep_resolve_class (jdep *);
264 static int note_possible_classname (const char *, int);
265 static void java_complete_expand_classes (void);
266 static void java_complete_expand_class (tree);
267 static void java_complete_expand_methods (tree);
268 static tree cut_identifier_in_qualified (tree);
269 static tree java_stabilize_reference (tree);
270 static tree do_unary_numeric_promotion (tree);
271 static char * operator_string (tree);
272 static tree do_merge_string_cste (tree, const char *, int, int);
273 static tree merge_string_cste (tree, tree, int);
274 static tree java_refold (tree);
275 static int java_decl_equiv (tree, tree);
276 static int binop_compound_p (enum tree_code);
277 static tree search_loop (tree);
278 static int labeled_block_contains_loop_p (tree, tree);
279 static int check_abstract_method_definitions (int, tree, tree);
280 static void java_check_abstract_method_definitions (tree);
281 static void java_debug_context_do (int);
282 static void java_parser_context_push_initialized_field (void);
283 static void java_parser_context_pop_initialized_field (void);
284 static tree reorder_static_initialized (tree);
285 static void java_parser_context_suspend (void);
286 static void java_parser_context_resume (void);
287 static int pop_current_osb (struct parser_ctxt *);
289 /* JDK 1.1 work. FIXME */
291 static tree maybe_make_nested_class_name (tree);
292 static int make_nested_class_name (tree);
293 static void set_nested_class_simple_name_value (tree, int);
294 static void link_nested_class_to_enclosing (void);
295 static tree resolve_inner_class (htab_t, tree, tree *, tree *, tree);
296 static tree find_as_inner_class (tree, tree, tree);
297 static tree find_as_inner_class_do (tree, tree);
298 static int check_inner_class_redefinition (tree, tree);
300 static tree build_thisn_assign (void);
301 static tree build_current_thisn (tree);
302 static tree build_access_to_thisn (tree, tree, int);
303 static tree maybe_build_thisn_access_method (tree);
305 static tree build_outer_field_access (tree, tree);
306 static tree build_outer_field_access_methods (tree);
307 static tree build_outer_field_access_expr (int, tree, tree,
309 static tree build_outer_method_access_method (tree);
310 static tree build_new_access_id (void);
311 static tree build_outer_field_access_method (tree, tree, tree,
314 static int outer_field_access_p (tree, tree);
315 static int outer_field_expanded_access_p (tree, tree *,
317 static tree outer_field_access_fix (tree, tree, tree);
318 static tree build_incomplete_class_ref (int, tree);
319 static tree patch_incomplete_class_ref (tree);
320 static tree create_anonymous_class (int, tree);
321 static void patch_anonymous_class (tree, tree, tree);
322 static void add_inner_class_fields (tree, tree);
324 static tree build_dot_class_method (tree);
325 static tree build_dot_class_method_invocation (tree);
326 static void create_new_parser_context (int);
327 static tree maybe_build_class_init_for_field (tree, tree);
329 static int attach_init_test_initialization_flags (void **, void *);
330 static int emit_test_initialization (void **, void *);
332 static char *string_convert_int_cst (tree);
334 /* Number of error found so far. */
335 int java_error_count;
336 /* Number of warning found so far. */
337 int java_warning_count;
338 /* Tell when not to fold, when doing xrefs */
340 /* Cyclic inheritance report, as it can be set by layout_class */
341 const char *cyclic_inheritance_report;
343 /* The current parser context */
344 struct parser_ctxt *ctxp;
346 /* List of things that were analyzed for which code will be generated */
347 struct parser_ctxt *ctxp_for_generation = NULL;
349 /* binop_lookup maps token to tree_code. It is used where binary
350 operations are involved and required by the parser. RDIV_EXPR
351 covers both integral/floating point division. The code is changed
352 once the type of both operator is worked out. */
354 static const enum tree_code binop_lookup[19] =
356 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
357 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
358 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
359 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
360 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
362 #define BINOP_LOOKUP(VALUE) \
363 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
365 /* This is the end index for binary operators that can also be used
366 in compound assignements. */
367 #define BINOP_COMPOUND_CANDIDATES 11
369 /* The "$L" identifier we use to create labels. */
370 static GTY(()) tree label_id;
372 /* The "StringBuffer" identifier used for the String `+' operator. */
373 static GTY(()) tree wfl_string_buffer;
375 /* The "append" identifier used for String `+' operator. */
376 static GTY(()) tree wfl_append;
378 /* The "toString" identifier used for String `+' operator. */
379 static GTY(()) tree wfl_to_string;
381 /* The "java.lang" import qualified name. */
382 static GTY(()) tree java_lang_id;
384 /* The generated `inst$' identifier used for generated enclosing
385 instance/field access functions. */
386 static GTY(()) tree inst_id;
388 /* Context and flag for static blocks */
389 static GTY(()) tree current_static_block;
391 /* The generated `write_parm_value$' identifier. */
392 static GTY(()) tree wpv_id;
394 /* The list of all packages we've seen so far */
395 static GTY(()) tree package_list;
397 /* Hold THIS for the scope of the current method decl. */
398 static GTY(()) tree current_this;
400 /* Hold a list of catch clauses list. The first element of this list is
401 the list of the catch clauses of the currently analysed try block. */
402 static GTY(()) tree currently_caught_type_list;
404 /* This holds a linked list of all the case labels for the current
405 switch statement. It is only used when checking to see if there
406 are duplicate labels. FIXME: probably this should just be attached
407 to the switch itself; then it could be referenced via
408 `ctxp->current_loop'. */
409 static GTY(()) tree case_label_list;
411 /* Anonymous class counter. Will be reset to 1 every time a non
412 anonymous class gets created. */
413 static int anonymous_class_counter = 1;
415 static GTY(()) tree src_parse_roots[1];
417 /* All classes seen from source code */
418 #define gclass_list src_parse_roots[0]
420 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
421 line and point it out. */
422 /* Should point out the one that don't fit. ASCII/unicode, going
425 #define check_modifiers(__message, __value, __mask) do { \
426 if ((__value) & ~(__mask)) \
428 size_t i, remainder = (__value) & ~(__mask); \
429 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++) \
430 if ((1 << i) & remainder) \
431 parse_error_context (ctxp->modifier_ctx [i], (__message), \
432 java_accstring_lookup (1 << i)); \
454 /* Things defined here have to match the order of what's in the
455 binop_lookup table. */
457 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
458 %token LS_TK SRS_TK ZRS_TK
459 %token AND_TK XOR_TK OR_TK
460 %token BOOL_AND_TK BOOL_OR_TK
461 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
463 /* This maps to the same binop_lookup entry than the token above */
465 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
467 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
468 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
471 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
473 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
474 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
475 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
476 %token PAD_TK ABSTRACT_TK STRICT_TK
479 /* Keep those two in order, too */
480 %token DECR_TK INCR_TK
482 /* From now one, things can be in any order */
484 %token DEFAULT_TK IF_TK THROW_TK
485 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
486 %token THROWS_TK BREAK_TK IMPORT_TK
487 %token ELSE_TK INSTANCEOF_TK RETURN_TK
488 %token VOID_TK CATCH_TK INTERFACE_TK
489 %token CASE_TK EXTENDS_TK FINALLY_TK
490 %token SUPER_TK WHILE_TK CLASS_TK
491 %token SWITCH_TK CONST_TK TRY_TK
492 %token FOR_TK NEW_TK CONTINUE_TK
493 %token GOTO_TK PACKAGE_TK THIS_TK
496 %token BYTE_TK SHORT_TK INT_TK LONG_TK
497 %token CHAR_TK INTEGRAL_TK
499 %token FLOAT_TK DOUBLE_TK FP_TK
503 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
505 %token ASSIGN_ANY_TK ASSIGN_TK
506 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
508 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
509 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
511 %type <value> modifiers MODIFIER_TK final synchronized
513 %type <node> super ID_TK identifier
514 %type <node> name simple_name qualified_name
515 %type <node> type_declaration compilation_unit
516 field_declaration method_declaration extends_interfaces
517 interfaces interface_type_list
518 import_declarations package_declaration
519 type_declarations interface_body
520 interface_member_declaration constant_declaration
521 interface_member_declarations interface_type
522 abstract_method_declaration
523 %type <node> class_body_declaration class_member_declaration
524 static_initializer constructor_declaration block
525 %type <node> class_body_declarations constructor_header
526 %type <node> class_or_interface_type class_type class_type_list
527 constructor_declarator explicit_constructor_invocation
528 %type <node> dim_expr dim_exprs this_or_super throws
530 %type <node> variable_declarator_id variable_declarator
531 variable_declarators variable_initializer
532 variable_initializers constructor_body
535 %type <node> class_body block_end constructor_block_end
536 %type <node> statement statement_without_trailing_substatement
537 labeled_statement if_then_statement label_decl
538 if_then_else_statement while_statement for_statement
539 statement_nsi labeled_statement_nsi do_statement
540 if_then_else_statement_nsi while_statement_nsi
541 for_statement_nsi statement_expression_list for_init
542 for_update statement_expression expression_statement
543 primary_no_new_array expression primary
544 array_creation_expression array_type
545 class_instance_creation_expression field_access
546 method_invocation array_access something_dot_new
547 argument_list postfix_expression while_expression
548 post_increment_expression post_decrement_expression
549 unary_expression_not_plus_minus unary_expression
550 pre_increment_expression pre_decrement_expression
552 multiplicative_expression additive_expression
553 shift_expression relational_expression
554 equality_expression and_expression
555 exclusive_or_expression inclusive_or_expression
556 conditional_and_expression conditional_or_expression
557 conditional_expression assignment_expression
558 left_hand_side assignment for_header for_begin
559 constant_expression do_statement_begin empty_statement
560 switch_statement synchronized_statement throw_statement
561 try_statement assert_statement
562 switch_expression switch_block
563 catches catch_clause catch_clause_parameter finally
564 anonymous_class_creation trap_overflow_corner_case
565 %type <node> return_statement break_statement continue_statement
567 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
568 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
569 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
570 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
571 %type <operator> ASSIGN_ANY_TK assignment_operator
572 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
573 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
574 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
575 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
576 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
577 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
578 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
579 %type <operator> NEW_TK ASSERT_TK
581 %type <node> method_body
583 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
584 STRING_LIT_TK NULL_TK VOID_TK
586 %type <node> IF_TK WHILE_TK FOR_TK
588 %type <node> formal_parameter_list formal_parameter
589 method_declarator method_header
591 %type <node> primitive_type reference_type type
592 BOOLEAN_TK INTEGRAL_TK FP_TK
594 /* Added or modified JDK 1.1 rule types */
595 %type <node> type_literals
598 /* 19.2 Production from 2.3: The Syntactic Grammar */
599 goal: compilation_unit
603 /* 19.3 Productions from 3: Lexical structure */
613 /* 19.4 Productions from 4: Types, Values and Variables */
626 class_or_interface_type
630 class_or_interface_type:
635 class_or_interface_type /* Default rule */
639 class_or_interface_type
645 int osb = pop_current_osb (ctxp);
646 tree t = build_java_array_type (($1), -1);
648 t = build_unresolved_array_type (t);
653 int osb = pop_current_osb (ctxp);
656 t = build_unresolved_array_type (t);
661 /* 19.5 Productions from 6: Names */
663 simple_name /* Default rule */
664 | qualified_name /* Default rule */
668 identifier /* Default rule */
672 name DOT_TK identifier
673 { $$ = make_qualified_name ($1, $3, $2.location); }
680 /* 19.6: Production from 7: Packages */
683 | package_declaration
684 | import_declarations
686 | package_declaration import_declarations
687 | package_declaration type_declarations
688 | import_declarations type_declarations
689 | package_declaration import_declarations type_declarations
697 | import_declarations import_declaration
705 | type_declarations type_declaration
709 PACKAGE_TK name SC_TK
711 ctxp->package = EXPR_WFL_NODE ($2);
712 register_package (ctxp->package);
715 {yyerror ("Missing name"); RECOVER;}
716 | PACKAGE_TK name error
717 {yyerror ("';' expected"); RECOVER;}
721 single_type_import_declaration
722 | type_import_on_demand_declaration
725 single_type_import_declaration:
728 tree name = EXPR_WFL_NODE ($2), last_name;
729 int i = IDENTIFIER_LENGTH (name)-1;
730 const char *last = &IDENTIFIER_POINTER (name)[i];
731 while (last != IDENTIFIER_POINTER (name))
737 last_name = get_identifier (++last);
738 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
740 tree err = find_name_in_single_imports (last_name);
741 if (err && err != name)
743 ($2, "Ambiguous class: `%s' and `%s'",
744 IDENTIFIER_POINTER (name),
745 IDENTIFIER_POINTER (err));
747 REGISTER_IMPORT ($2, last_name);
750 REGISTER_IMPORT ($2, last_name);
753 {yyerror ("Missing name"); RECOVER;}
754 | IMPORT_TK name error
755 {yyerror ("';' expected"); RECOVER;}
758 type_import_on_demand_declaration:
759 IMPORT_TK name DOT_TK MULT_TK SC_TK
761 tree name = EXPR_WFL_NODE ($2);
763 /* Search for duplicates. */
764 for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
765 if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
767 /* Don't import the same thing more than once, just ignore
768 duplicates (7.5.2) */
771 read_import_dir ($2);
772 ctxp->import_demand_list =
773 chainon (ctxp->import_demand_list,
774 build_tree_list ($2, NULL_TREE));
777 | IMPORT_TK name DOT_TK error
778 {yyerror ("'*' expected"); RECOVER;}
779 | IMPORT_TK name DOT_TK MULT_TK error
780 {yyerror ("';' expected"); RECOVER;}
785 { end_class_declaration (0); }
786 | interface_declaration
787 { end_class_declaration (0); }
792 yyerror ("Class or interface declaration expected");
796 /* 19.7 Shortened from the original:
797 modifiers: modifier | modifiers modifier
798 modifier: any of public... */
804 | modifiers MODIFIER_TK
809 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
810 java_accstring_lookup (acc));
818 /* 19.8.1 Production from $8.1: Class Declaration */
820 modifiers CLASS_TK identifier super interfaces
821 { create_class ($1, $3, $4, $5); }
824 | CLASS_TK identifier super interfaces
825 { create_class (0, $2, $3, $4); }
828 | modifiers CLASS_TK error
829 { yyerror ("Missing class name"); RECOVER; }
831 { yyerror ("Missing class name"); RECOVER; }
832 | CLASS_TK identifier error
834 if (!ctxp->class_err) yyerror ("'{' expected");
837 | modifiers CLASS_TK identifier error
838 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
843 | EXTENDS_TK class_type
845 | EXTENDS_TK class_type error
846 {yyerror ("'{' expected"); ctxp->class_err=1;}
848 {yyerror ("Missing super class name"); ctxp->class_err=1;}
853 | IMPLEMENTS_TK interface_type_list
855 | IMPLEMENTS_TK error
858 yyerror ("Missing interface name");
865 ctxp->interface_number = 1;
866 $$ = build_tree_list ($1, NULL_TREE);
868 | interface_type_list C_TK interface_type
870 ctxp->interface_number++;
871 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
873 | interface_type_list C_TK error
874 {yyerror ("Missing interface name"); RECOVER;}
880 /* Store the location of the `}' when doing xrefs */
882 DECL_END_SOURCE_LINE (GET_CPC ()) =
883 EXPR_WFL_ADD_COL ($2.location, 1);
886 | OCB_TK class_body_declarations CCB_TK
888 /* Store the location of the `}' when doing xrefs */
890 DECL_END_SOURCE_LINE (GET_CPC ()) =
891 EXPR_WFL_ADD_COL ($3.location, 1);
896 class_body_declarations:
897 class_body_declaration
898 | class_body_declarations class_body_declaration
901 class_body_declaration:
902 class_member_declaration
904 | constructor_declaration
905 | block /* Added, JDK1.1, instance initializer */
907 if ($1 != empty_stmt_node)
909 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
910 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
915 class_member_declaration:
918 | class_declaration /* Added, JDK1.1 inner classes */
919 { end_class_declaration (1); }
920 | interface_declaration /* Added, JDK1.1 inner interfaces */
921 { end_class_declaration (1); }
925 /* 19.8.2 Productions from 8.3: Field Declarations */
927 type variable_declarators SC_TK
928 { register_fields (0, $1, $2); }
929 | modifiers type variable_declarators SC_TK
932 ("Illegal modifier `%s' for field declaration",
933 $1, FIELD_MODIFIERS);
934 check_modifiers_consistency ($1);
935 register_fields ($1, $2, $3);
939 variable_declarators:
940 /* Should we use build_decl_list () instead ? FIXME */
941 variable_declarator /* Default rule */
942 | variable_declarators C_TK variable_declarator
943 { $$ = chainon ($1, $3); }
944 | variable_declarators C_TK error
945 {yyerror ("Missing term"); RECOVER;}
949 variable_declarator_id
950 { $$ = build_tree_list ($1, NULL_TREE); }
951 | variable_declarator_id ASSIGN_TK variable_initializer
953 if (java_error_count)
956 ($1, build_assignment ($2.token, $2.location, $1, $3));
958 | variable_declarator_id ASSIGN_TK error
960 yyerror ("Missing variable initializer");
961 $$ = build_tree_list ($1, NULL_TREE);
964 | variable_declarator_id ASSIGN_TK variable_initializer error
966 yyerror ("';' expected");
967 $$ = build_tree_list ($1, NULL_TREE);
972 variable_declarator_id:
974 | variable_declarator_id OSB_TK CSB_TK
975 { $$ = build_unresolved_array_type ($1); }
977 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
978 | variable_declarator_id OSB_TK error
980 yyerror ("']' expected");
983 | variable_declarator_id CSB_TK error
984 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
987 variable_initializer:
992 /* 19.8.3 Productions from 8.4: Method Declarations */
996 current_function_decl = $1;
997 if (current_function_decl
998 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
999 source_start_java_method (current_function_decl);
1001 current_function_decl = NULL_TREE;
1004 { finish_method_declaration ($3); }
1005 | method_header error
1006 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1010 type method_declarator throws
1011 { $$ = method_header (0, $1, $2, $3); }
1012 | VOID_TK method_declarator throws
1013 { $$ = method_header (0, void_type_node, $2, $3); }
1014 | modifiers type method_declarator throws
1015 { $$ = method_header ($1, $2, $3, $4); }
1016 | modifiers VOID_TK method_declarator throws
1017 { $$ = method_header ($1, void_type_node, $3, $4); }
1020 yyerror ("Invalid method declaration, method name required");
1023 | modifiers type error
1025 yyerror ("Identifier expected");
1030 yyerror ("Identifier expected");
1033 | modifiers VOID_TK error
1035 yyerror ("Identifier expected");
1040 yyerror ("Invalid method declaration, return type required");
1046 identifier OP_TK CP_TK
1048 ctxp->formal_parameter_number = 0;
1049 $$ = method_declarator ($1, NULL_TREE);
1051 | identifier OP_TK formal_parameter_list CP_TK
1052 { $$ = method_declarator ($1, $3); }
1053 | method_declarator OSB_TK CSB_TK
1055 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1057 build_unresolved_array_type (TREE_PURPOSE ($1));
1058 parse_warning_context
1060 "Discouraged form of returned type specification");
1062 | identifier OP_TK error
1063 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1064 | method_declarator OSB_TK error
1065 {yyerror ("']' expected"); RECOVER;}
1068 formal_parameter_list:
1071 ctxp->formal_parameter_number = 1;
1073 | formal_parameter_list C_TK formal_parameter
1075 ctxp->formal_parameter_number += 1;
1076 $$ = chainon ($1, $3);
1078 | formal_parameter_list C_TK error
1079 { yyerror ("Missing formal parameter term"); RECOVER; }
1083 type variable_declarator_id
1085 $$ = build_tree_list ($2, $1);
1087 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1089 $$ = build_tree_list ($3, $2);
1090 ARG_FINAL_P ($$) = 1;
1094 yyerror ("Missing identifier"); RECOVER;
1099 yyerror ("Missing identifier"); RECOVER;
1107 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1109 if ($1 != ACC_FINAL)
1110 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1116 | THROWS_TK class_type_list
1119 {yyerror ("Missing class type term"); RECOVER;}
1124 { $$ = build_tree_list ($1, $1); }
1125 | class_type_list C_TK class_type
1126 { $$ = tree_cons ($3, $3, $1); }
1127 | class_type_list C_TK error
1128 {yyerror ("Missing class type term"); RECOVER;}
1133 | SC_TK { $$ = NULL_TREE; }
1136 /* 19.8.4 Productions from 8.5: Static Initializers */
1140 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1141 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1142 current_static_block = NULL_TREE;
1146 static: /* Test lval.sub_token here */
1149 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1150 /* Can't have a static initializer in an innerclass */
1151 if ($1 | ACC_STATIC &&
1152 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1154 (MODIFIER_WFL (STATIC_TK),
1155 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1156 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1157 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1161 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1162 constructor_declaration:
1165 current_function_decl = $1;
1166 source_start_java_method (current_function_decl);
1169 { finish_method_declaration ($3); }
1173 constructor_declarator throws
1174 { $$ = method_header (0, NULL_TREE, $1, $2); }
1175 | modifiers constructor_declarator throws
1176 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1179 constructor_declarator:
1180 simple_name OP_TK CP_TK
1182 ctxp->formal_parameter_number = 0;
1183 $$ = method_declarator ($1, NULL_TREE);
1185 | simple_name OP_TK formal_parameter_list CP_TK
1186 { $$ = method_declarator ($1, $3); }
1190 /* Unlike regular method, we always need a complete (empty)
1191 body so we can safely perform all the required code
1192 addition (super invocation and field initialization) */
1193 block_begin constructor_block_end
1195 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1198 | block_begin explicit_constructor_invocation constructor_block_end
1200 | block_begin block_statements constructor_block_end
1202 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1206 constructor_block_end:
1210 /* Error recovery for that rule moved down expression_statement: rule. */
1211 explicit_constructor_invocation:
1212 this_or_super OP_TK CP_TK SC_TK
1214 $$ = build_method_invocation ($1, NULL_TREE);
1215 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1216 $$ = java_method_add_stmt (current_function_decl, $$);
1218 | this_or_super OP_TK argument_list CP_TK SC_TK
1220 $$ = build_method_invocation ($1, $3);
1221 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1222 $$ = java_method_add_stmt (current_function_decl, $$);
1224 /* Added, JDK1.1 inner classes. Modified because the rule
1225 'primary' couldn't work. */
1226 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1227 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1228 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1229 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1232 this_or_super: /* Added, simplifies error diagnostics */
1235 tree wfl = build_wfl_node (this_identifier_node);
1236 EXPR_WFL_LINECOL (wfl) = $1.location;
1241 tree wfl = build_wfl_node (super_identifier_node);
1242 EXPR_WFL_LINECOL (wfl) = $1.location;
1247 /* 19.9 Productions from 9: Interfaces */
1248 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1249 interface_declaration:
1250 INTERFACE_TK identifier
1251 { create_interface (0, $2, NULL_TREE); }
1254 | modifiers INTERFACE_TK identifier
1255 { create_interface ($1, $3, NULL_TREE); }
1258 | INTERFACE_TK identifier extends_interfaces
1259 { create_interface (0, $2, $3); }
1262 | modifiers INTERFACE_TK identifier extends_interfaces
1263 { create_interface ($1, $3, $4); }
1266 | INTERFACE_TK identifier error
1267 { yyerror ("'{' expected"); RECOVER; }
1268 | modifiers INTERFACE_TK identifier error
1269 { yyerror ("'{' expected"); RECOVER; }
1273 EXTENDS_TK interface_type
1275 ctxp->interface_number = 1;
1276 $$ = build_tree_list ($2, NULL_TREE);
1278 | extends_interfaces C_TK interface_type
1280 ctxp->interface_number++;
1281 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1284 {yyerror ("Invalid interface type"); RECOVER;}
1285 | extends_interfaces C_TK error
1286 {yyerror ("Missing term"); RECOVER;}
1292 | OCB_TK interface_member_declarations CCB_TK
1296 interface_member_declarations:
1297 interface_member_declaration
1298 | interface_member_declarations interface_member_declaration
1301 interface_member_declaration:
1302 constant_declaration
1303 | abstract_method_declaration
1304 | class_declaration /* Added, JDK1.1 inner classes */
1305 { end_class_declaration (1); }
1306 | interface_declaration /* Added, JDK1.1 inner interfaces */
1307 { end_class_declaration (1); }
1310 constant_declaration:
1314 abstract_method_declaration:
1317 check_abstract_method_header ($1);
1318 current_function_decl = NULL_TREE; /* FIXME ? */
1320 | method_header error
1321 {yyerror ("';' expected"); RECOVER;}
1324 /* 19.10 Productions from 10: Arrays */
1327 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1328 | OCB_TK C_TK CCB_TK
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
1447 if (flag_extraneous_semicolon
1448 && ! current_static_block
1449 && (! current_function_decl ||
1450 /* Verify we're not in a inner class declaration */
1451 (GET_CPC () != TYPE_NAME
1452 (DECL_CONTEXT (current_function_decl)))))
1455 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1456 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1458 $$ = empty_stmt_node;
1463 identifier REL_CL_TK
1465 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1466 EXPR_WFL_NODE ($1));
1468 push_labeled_block ($$);
1469 PUSH_LABELED_BLOCK ($$);
1474 label_decl statement
1475 { $$ = finish_labeled_statement ($1, $2); }
1477 {yyerror ("':' expected"); RECOVER;}
1480 labeled_statement_nsi:
1481 label_decl statement_nsi
1482 { $$ = finish_labeled_statement ($1, $2); }
1485 /* We concentrate here a bunch of error handling rules that we couldn't write
1486 earlier, because expression_statement catches a missing ';'. */
1487 expression_statement:
1488 statement_expression SC_TK
1490 /* We have a statement. Generate a WFL around it so
1492 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1493 /* We know we have a statement, so set the debug
1494 info to be eventually generate here. */
1495 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1499 YYNOT_TWICE yyerror ("Invalid expression statement");
1500 DRECOVER (expr_stmt);
1504 YYNOT_TWICE yyerror ("Invalid expression statement");
1505 DRECOVER (expr_stmt);
1509 YYNOT_TWICE yyerror ("Invalid expression statement");
1510 DRECOVER (expr_stmt);
1512 | this_or_super OP_TK error
1513 {yyerror ("')' expected"); RECOVER;}
1514 | this_or_super OP_TK CP_TK error
1516 parse_ctor_invocation_error ();
1519 | this_or_super OP_TK argument_list error
1520 {yyerror ("')' expected"); RECOVER;}
1521 | this_or_super OP_TK argument_list CP_TK error
1523 parse_ctor_invocation_error ();
1526 | name DOT_TK SUPER_TK error
1527 {yyerror ("'(' expected"); RECOVER;}
1528 | name DOT_TK SUPER_TK OP_TK error
1529 {yyerror ("')' expected"); RECOVER;}
1530 | name DOT_TK SUPER_TK OP_TK argument_list error
1531 {yyerror ("')' expected"); RECOVER;}
1532 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1533 {yyerror ("';' expected"); RECOVER;}
1534 | name DOT_TK SUPER_TK OP_TK CP_TK error
1535 {yyerror ("';' expected"); RECOVER;}
1538 statement_expression:
1540 | pre_increment_expression
1541 | pre_decrement_expression
1542 | post_increment_expression
1543 | post_decrement_expression
1545 | class_instance_creation_expression
1549 IF_TK OP_TK expression CP_TK statement
1551 $$ = build_if_else_statement ($2.location, $3,
1555 {yyerror ("'(' expected"); RECOVER;}
1557 {yyerror ("Missing term"); RECOVER;}
1558 | IF_TK OP_TK expression error
1559 {yyerror ("')' expected"); RECOVER;}
1562 if_then_else_statement:
1563 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1564 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1567 if_then_else_statement_nsi:
1568 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1569 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1579 /* Make into "proper list" of COMPOUND_EXPRs.
1580 I.e. make the last statement also have its own
1582 maybe_absorb_scoping_blocks ();
1583 TREE_OPERAND ($1, 1) = exit_block ();
1584 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1589 SWITCH_TK OP_TK expression CP_TK
1591 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1592 EXPR_WFL_LINECOL ($$) = $2.location;
1595 {yyerror ("'(' expected"); RECOVER;}
1596 | SWITCH_TK OP_TK error
1597 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1598 | SWITCH_TK OP_TK expression CP_TK error
1599 {yyerror ("'{' expected"); RECOVER;}
1602 /* Default assignment is there to avoid type node on switch_block
1608 | OCB_TK switch_labels CCB_TK
1610 | OCB_TK switch_block_statement_groups CCB_TK
1612 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1616 switch_block_statement_groups:
1617 switch_block_statement_group
1618 | switch_block_statement_groups switch_block_statement_group
1621 switch_block_statement_group:
1622 switch_labels block_statements
1627 | switch_labels switch_label
1631 CASE_TK constant_expression REL_CL_TK
1633 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1634 EXPR_WFL_LINECOL (lab) = $1.location;
1635 java_method_add_stmt (current_function_decl, lab);
1637 | DEFAULT_TK REL_CL_TK
1639 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1640 EXPR_WFL_LINECOL (lab) = $1.location;
1641 java_method_add_stmt (current_function_decl, lab);
1644 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1645 | CASE_TK constant_expression error
1646 {yyerror ("':' expected"); RECOVER;}
1648 {yyerror ("':' expected"); RECOVER;}
1652 WHILE_TK OP_TK expression CP_TK
1654 tree body = build_loop_body ($2.location, $3, 0);
1655 $$ = build_new_loop (body);
1660 while_expression statement
1661 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1663 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1664 | WHILE_TK OP_TK error
1665 {yyerror ("Missing term and ')' expected"); RECOVER;}
1666 | WHILE_TK OP_TK expression error
1667 {yyerror ("')' expected"); RECOVER;}
1670 while_statement_nsi:
1671 while_expression statement_nsi
1672 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1678 tree body = build_loop_body (0, NULL_TREE, 1);
1679 $$ = build_new_loop (body);
1681 /* Need error handing here. FIXME */
1685 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1686 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1690 for_begin SC_TK expression SC_TK for_update CP_TK statement
1692 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1693 $3 = build_wfl_node ($3);
1694 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1696 | for_begin SC_TK SC_TK for_update CP_TK statement
1698 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1699 /* We have not condition, so we get rid of the EXIT_EXPR */
1700 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1703 | for_begin SC_TK error
1704 {yyerror ("Invalid control expression"); RECOVER;}
1705 | for_begin SC_TK expression SC_TK error
1706 {yyerror ("Invalid update expression"); RECOVER;}
1707 | for_begin SC_TK SC_TK error
1708 {yyerror ("Invalid update expression"); RECOVER;}
1712 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1713 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1714 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1716 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1717 /* We have not condition, so we get rid of the EXIT_EXPR */
1718 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1726 /* This scope defined for local variable that may be
1727 defined within the scope of the for loop */
1731 {yyerror ("'(' expected"); DRECOVER(for_1);}
1732 | FOR_TK OP_TK error
1733 {yyerror ("Invalid init statement"); RECOVER;}
1739 /* We now declare the loop body. The loop is
1740 declared as a for loop. */
1741 tree body = build_loop_body (0, NULL_TREE, 0);
1742 $$ = build_new_loop (body);
1743 FOR_LOOP_P ($$) = 1;
1744 /* The loop is added to the current block the for
1745 statement is defined within */
1746 java_method_add_stmt (current_function_decl, $$);
1749 for_init: /* Can be empty */
1750 { $$ = empty_stmt_node; }
1751 | statement_expression_list
1753 /* Init statement recorded within the previously
1754 defined block scope */
1755 $$ = java_method_add_stmt (current_function_decl, $1);
1757 | local_variable_declaration
1759 /* Local variable are recorded within the previously
1760 defined block scope */
1763 | statement_expression_list error
1764 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1767 for_update: /* Can be empty */
1768 {$$ = empty_stmt_node;}
1769 | statement_expression_list
1770 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1773 statement_expression_list:
1774 statement_expression
1775 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1776 | statement_expression_list C_TK statement_expression
1777 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1778 | statement_expression_list C_TK error
1779 {yyerror ("Missing term"); RECOVER;}
1784 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1785 | BREAK_TK identifier SC_TK
1786 { $$ = build_bc_statement ($1.location, 1, $2); }
1788 {yyerror ("Missing term"); RECOVER;}
1789 | BREAK_TK identifier error
1790 {yyerror ("';' expected"); RECOVER;}
1795 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1796 | CONTINUE_TK identifier SC_TK
1797 { $$ = build_bc_statement ($1.location, 0, $2); }
1799 {yyerror ("Missing term"); RECOVER;}
1800 | CONTINUE_TK identifier error
1801 {yyerror ("';' expected"); RECOVER;}
1806 { $$ = build_return ($1.location, NULL_TREE); }
1807 | RETURN_TK expression SC_TK
1808 { $$ = build_return ($1.location, $2); }
1810 {yyerror ("Missing term"); RECOVER;}
1811 | RETURN_TK expression error
1812 {yyerror ("';' expected"); RECOVER;}
1816 THROW_TK expression SC_TK
1818 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1819 EXPR_WFL_LINECOL ($$) = $1.location;
1822 {yyerror ("Missing term"); RECOVER;}
1823 | THROW_TK expression error
1824 {yyerror ("';' expected"); RECOVER;}
1828 ASSERT_TK expression REL_CL_TK expression SC_TK
1830 $$ = build_assertion ($1.location, $2, $4);
1832 | ASSERT_TK expression SC_TK
1834 $$ = build_assertion ($1.location, $2, NULL_TREE);
1837 {yyerror ("Missing term"); RECOVER;}
1838 | ASSERT_TK expression error
1839 {yyerror ("';' expected"); RECOVER;}
1842 synchronized_statement:
1843 synchronized OP_TK expression CP_TK block
1845 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1846 EXPR_WFL_LINECOL ($$) =
1847 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1849 | synchronized OP_TK expression CP_TK error
1850 {yyerror ("'{' expected"); RECOVER;}
1851 | synchronized error
1852 {yyerror ("'(' expected"); RECOVER;}
1853 | synchronized OP_TK error CP_TK
1854 {yyerror ("Missing term"); RECOVER;}
1855 | synchronized OP_TK error
1856 {yyerror ("Missing term"); RECOVER;}
1863 "Illegal modifier `%s'. Only `synchronized' was expected here",
1864 $1, ACC_SYNCHRONIZED);
1865 if ($1 != ACC_SYNCHRONIZED)
1866 MODIFIER_WFL (SYNCHRONIZED_TK) =
1867 build_wfl_node (NULL_TREE);
1872 TRY_TK block catches
1873 { $$ = build_try_statement ($1.location, $2, $3); }
1874 | TRY_TK block finally
1875 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1876 | TRY_TK block catches finally
1877 { $$ = build_try_finally_statement
1878 ($1.location, build_try_statement ($1.location,
1882 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1887 | catches catch_clause
1889 TREE_CHAIN ($2) = $1;
1895 catch_clause_parameter block
1897 java_method_add_stmt (current_function_decl, $2);
1903 catch_clause_parameter:
1904 CATCH_TK OP_TK formal_parameter CP_TK
1906 /* We add a block to define a scope for
1907 formal_parameter (CCBP). The formal parameter is
1908 declared initialized by the appropriate function
1910 tree ccpb = enter_block ();
1911 tree init = build_assignment
1912 (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1913 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1914 declare_local_variables (0, TREE_VALUE ($3),
1915 build_tree_list (TREE_PURPOSE ($3),
1917 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1918 EXPR_WFL_LINECOL ($$) = $1.location;
1921 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1922 | CATCH_TK OP_TK error
1924 yyerror ("Missing term or ')' expected");
1925 RECOVER; $$ = NULL_TREE;
1927 | CATCH_TK OP_TK error CP_TK /* That's for () */
1928 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1935 {yyerror ("'{' expected"); RECOVER; }
1938 /* 19.12 Production from 15: Expressions */
1940 primary_no_new_array
1941 | array_creation_expression
1944 primary_no_new_array:
1947 { $$ = build_this ($1.location); }
1948 | OP_TK expression CP_TK
1950 | class_instance_creation_expression
1955 /* Added, JDK1.1 inner classes. Documentation is wrong
1956 refering to a 'ClassName' (class_name) rule that doesn't
1957 exist. Used name: instead. */
1958 | name DOT_TK THIS_TK
1960 tree wfl = build_wfl_node (this_identifier_node);
1961 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1963 | OP_TK expression error
1964 {yyerror ("')' expected"); RECOVER;}
1966 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1967 | primitive_type DOT_TK error
1968 {yyerror ("'class' expected" ); RECOVER;}
1969 | VOID_TK DOT_TK error
1970 {yyerror ("'class' expected" ); RECOVER;}
1974 name DOT_TK CLASS_TK
1975 { $$ = build_incomplete_class_ref ($2.location, $1); }
1976 | array_type DOT_TK CLASS_TK
1977 { $$ = build_incomplete_class_ref ($2.location, $1); }
1978 | primitive_type DOT_TK CLASS_TK
1979 { $$ = build_incomplete_class_ref ($2.location, $1); }
1980 | VOID_TK DOT_TK CLASS_TK
1982 $$ = build_incomplete_class_ref ($2.location,
1987 class_instance_creation_expression:
1988 NEW_TK class_type OP_TK argument_list CP_TK
1989 { $$ = build_new_invocation ($2, $4); }
1990 | NEW_TK class_type OP_TK CP_TK
1991 { $$ = build_new_invocation ($2, NULL_TREE); }
1992 | anonymous_class_creation
1993 /* Added, JDK1.1 inner classes, modified to use name or
1994 primary instead of primary solely which couldn't work in
1996 | something_dot_new identifier OP_TK CP_TK
1998 tree ctor = build_new_invocation ($2, NULL_TREE);
1999 $$ = make_qualified_primary ($1, ctor,
2000 EXPR_WFL_LINECOL ($1));
2002 | something_dot_new identifier OP_TK CP_TK class_body
2003 | something_dot_new identifier OP_TK argument_list CP_TK
2005 tree ctor = build_new_invocation ($2, $4);
2006 $$ = make_qualified_primary ($1, ctor,
2007 EXPR_WFL_LINECOL ($1));
2009 | something_dot_new identifier OP_TK argument_list CP_TK class_body
2010 | NEW_TK error SC_TK
2011 {yyerror ("'(' expected"); DRECOVER(new_1);}
2012 | NEW_TK class_type error
2013 {yyerror ("'(' expected"); RECOVER;}
2014 | NEW_TK class_type OP_TK error
2015 {yyerror ("')' or term expected"); RECOVER;}
2016 | NEW_TK class_type OP_TK argument_list error
2017 {yyerror ("')' expected"); RECOVER;}
2018 | something_dot_new error
2019 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
2020 | something_dot_new identifier error
2021 {yyerror ("'(' expected"); RECOVER;}
2024 /* Created after JDK1.1 rules originally added to
2025 class_instance_creation_expression, but modified to use
2026 'class_type' instead of 'TypeName' (type_name) which is mentionned
2027 in the documentation but doesn't exist. */
2029 anonymous_class_creation:
2030 NEW_TK class_type OP_TK argument_list CP_TK
2031 { create_anonymous_class ($1.location, $2); }
2034 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2035 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2037 end_class_declaration (1);
2039 /* Now we can craft the new expression */
2040 $$ = build_new_invocation (id, $4);
2042 /* Note that we can't possibly be here if
2043 `class_type' is an interface (in which case the
2044 anonymous class extends Object and implements
2045 `class_type', hence its constructor can't have
2048 /* Otherwise, the innerclass must feature a
2049 constructor matching `argument_list'. Anonymous
2050 classes are a bit special: it's impossible to
2051 define constructor for them, hence constructors
2052 must be generated following the hints provided by
2053 the `new' expression. Whether a super constructor
2054 of that nature exists or not is to be verified
2055 later on in verify_constructor_super.
2057 It's during the expansion of a `new' statement
2058 refering to an anonymous class that a ctor will
2059 be generated for the anonymous class, with the
2063 | NEW_TK class_type OP_TK CP_TK
2064 { create_anonymous_class ($1.location, $2); }
2067 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2068 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2070 end_class_declaration (1);
2072 /* Now we can craft the new expression. The
2073 statement doesn't need to be remember so that a
2074 constructor can be generated, since its signature
2075 is already known. */
2076 $$ = build_new_invocation (id, NULL_TREE);
2080 something_dot_new: /* Added, not part of the specs. */
2083 | primary DOT_TK NEW_TK
2090 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2091 ctxp->formal_parameter_number = 1;
2093 | argument_list C_TK expression
2095 ctxp->formal_parameter_number += 1;
2096 $$ = tree_cons (NULL_TREE, $3, $1);
2098 | argument_list C_TK error
2099 {yyerror ("Missing term"); RECOVER;}
2102 array_creation_expression:
2103 NEW_TK primitive_type dim_exprs
2104 { $$ = build_newarray_node ($2, $3, 0); }
2105 | NEW_TK class_or_interface_type dim_exprs
2106 { $$ = build_newarray_node ($2, $3, 0); }
2107 | NEW_TK primitive_type dim_exprs dims
2108 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2109 | NEW_TK class_or_interface_type dim_exprs dims
2110 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2111 /* Added, JDK1.1 anonymous array. Initial documentation rule
2113 | NEW_TK class_or_interface_type dims array_initializer
2116 int osb = pop_current_osb (ctxp);
2118 obstack_grow (&temporary_obstack, "[]", 2);
2119 obstack_1grow (&temporary_obstack, '\0');
2120 sig = obstack_finish (&temporary_obstack);
2121 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2122 $2, get_identifier (sig), $4);
2124 | NEW_TK primitive_type dims array_initializer
2126 int osb = pop_current_osb (ctxp);
2129 type = build_java_array_type (type, -1);
2130 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2131 build_pointer_type (type), NULL_TREE, $4);
2133 | NEW_TK error CSB_TK
2134 {yyerror ("'[' expected"); DRECOVER ("]");}
2135 | NEW_TK error OSB_TK
2136 {yyerror ("']' expected"); RECOVER;}
2141 { $$ = build_tree_list (NULL_TREE, $1); }
2142 | dim_exprs dim_expr
2143 { $$ = tree_cons (NULL_TREE, $2, $$); }
2147 OSB_TK expression CSB_TK
2149 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2151 $2 = build_wfl_node ($2);
2152 TREE_TYPE ($2) = NULL_TREE;
2154 EXPR_WFL_LINECOL ($2) = $1.location;
2157 | OSB_TK expression error
2158 {yyerror ("']' expected"); RECOVER;}
2161 yyerror ("Missing term");
2162 yyerror ("']' expected");
2171 /* If not initialized, allocate memory for the osb
2173 if (!ctxp->osb_limit)
2175 allocate = ctxp->osb_limit = 32;
2176 ctxp->osb_depth = -1;
2178 /* If capacity overflown, reallocate a bigger chunk */
2179 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2180 allocate = ctxp->osb_limit << 1;
2184 allocate *= sizeof (int);
2185 if (ctxp->osb_number)
2186 ctxp->osb_number = xrealloc (ctxp->osb_number,
2189 ctxp->osb_number = xmalloc (allocate);
2192 CURRENT_OSB (ctxp) = 1;
2194 | dims OSB_TK CSB_TK
2195 { CURRENT_OSB (ctxp)++; }
2197 { yyerror ("']' expected"); RECOVER;}
2201 primary DOT_TK identifier
2202 { $$ = make_qualified_primary ($1, $3, $2.location); }
2203 /* FIXME - REWRITE TO:
2204 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2205 | SUPER_TK DOT_TK identifier
2207 tree super_wfl = build_wfl_node (super_identifier_node);
2208 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2209 $$ = make_qualified_name (super_wfl, $3, $2.location);
2212 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2217 { $$ = build_method_invocation ($1, NULL_TREE); }
2218 | name OP_TK argument_list CP_TK
2219 { $$ = build_method_invocation ($1, $3); }
2220 | primary DOT_TK identifier OP_TK CP_TK
2222 if (TREE_CODE ($1) == THIS_EXPR)
2223 $$ = build_this_super_qualified_invocation
2224 (1, $3, NULL_TREE, 0, $2.location);
2227 tree invok = build_method_invocation ($3, NULL_TREE);
2228 $$ = make_qualified_primary ($1, invok, $2.location);
2231 | primary DOT_TK identifier OP_TK argument_list CP_TK
2233 if (TREE_CODE ($1) == THIS_EXPR)
2234 $$ = build_this_super_qualified_invocation
2235 (1, $3, $5, 0, $2.location);
2238 tree invok = build_method_invocation ($3, $5);
2239 $$ = make_qualified_primary ($1, invok, $2.location);
2242 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2244 $$ = build_this_super_qualified_invocation
2245 (0, $3, NULL_TREE, $1.location, $2.location);
2247 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2249 $$ = build_this_super_qualified_invocation
2250 (0, $3, $5, $1.location, $2.location);
2252 /* Screws up thing. I let it here until I'm convinced it can
2254 | primary DOT_TK error
2255 {yyerror ("'(' expected"); DRECOVER(bad);} */
2256 | SUPER_TK DOT_TK error CP_TK
2257 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2258 | SUPER_TK DOT_TK error DOT_TK
2259 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2263 name OSB_TK expression CSB_TK
2264 { $$ = build_array_ref ($2.location, $1, $3); }
2265 | primary_no_new_array OSB_TK expression CSB_TK
2266 { $$ = build_array_ref ($2.location, $1, $3); }
2269 yyerror ("Missing term and ']' expected");
2270 DRECOVER(array_access);
2272 | name OSB_TK expression error
2274 yyerror ("']' expected");
2275 DRECOVER(array_access);
2277 | primary_no_new_array OSB_TK error
2279 yyerror ("Missing term and ']' expected");
2280 DRECOVER(array_access);
2282 | primary_no_new_array OSB_TK expression error
2284 yyerror ("']' expected");
2285 DRECOVER(array_access);
2292 | post_increment_expression
2293 | post_decrement_expression
2296 post_increment_expression:
2297 postfix_expression INCR_TK
2298 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2301 post_decrement_expression:
2302 postfix_expression DECR_TK
2303 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2306 trap_overflow_corner_case:
2307 pre_increment_expression
2308 | pre_decrement_expression
2309 | PLUS_TK unary_expression
2310 {$$ = build_unaryop ($1.token, $1.location, $2); }
2311 | unary_expression_not_plus_minus
2313 {yyerror ("Missing term"); RECOVER}
2317 trap_overflow_corner_case
2319 error_if_numeric_overflow ($1);
2322 | MINUS_TK trap_overflow_corner_case
2323 {$$ = build_unaryop ($1.token, $1.location, $2); }
2325 {yyerror ("Missing term"); RECOVER}
2328 pre_increment_expression:
2329 INCR_TK unary_expression
2330 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2332 {yyerror ("Missing term"); RECOVER}
2335 pre_decrement_expression:
2336 DECR_TK unary_expression
2337 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2339 {yyerror ("Missing term"); RECOVER}
2342 unary_expression_not_plus_minus:
2344 | NOT_TK unary_expression
2345 {$$ = build_unaryop ($1.token, $1.location, $2); }
2346 | NEG_TK unary_expression
2347 {$$ = build_unaryop ($1.token, $1.location, $2); }
2350 {yyerror ("Missing term"); RECOVER}
2352 {yyerror ("Missing term"); RECOVER}
2355 cast_expression: /* Error handling here is potentially weak */
2356 OP_TK primitive_type dims CP_TK unary_expression
2359 int osb = pop_current_osb (ctxp);
2361 type = build_java_array_type (type, -1);
2362 $$ = build_cast ($1.location, type, $5);
2364 | OP_TK primitive_type CP_TK unary_expression
2365 { $$ = build_cast ($1.location, $2, $4); }
2366 | OP_TK expression CP_TK unary_expression_not_plus_minus
2367 { $$ = build_cast ($1.location, $2, $4); }
2368 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2371 int osb = pop_current_osb (ctxp);
2372 obstack_grow (&temporary_obstack,
2373 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2374 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2376 obstack_grow (&temporary_obstack, "[]", 2);
2377 obstack_1grow (&temporary_obstack, '\0');
2378 ptr = obstack_finish (&temporary_obstack);
2379 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2380 $$ = build_cast ($1.location, $2, $5);
2382 | OP_TK primitive_type OSB_TK error
2383 {yyerror ("']' expected, invalid type expression");}
2386 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2389 | OP_TK primitive_type dims CP_TK error
2390 {yyerror ("Missing term"); RECOVER;}
2391 | OP_TK primitive_type CP_TK error
2392 {yyerror ("Missing term"); RECOVER;}
2393 | OP_TK name dims CP_TK error
2394 {yyerror ("Missing term"); RECOVER;}
2397 multiplicative_expression:
2399 | multiplicative_expression MULT_TK unary_expression
2401 $$ = build_binop (BINOP_LOOKUP ($2.token),
2402 $2.location, $1, $3);
2404 | multiplicative_expression DIV_TK unary_expression
2406 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2409 | multiplicative_expression REM_TK unary_expression
2411 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2414 | multiplicative_expression MULT_TK error
2415 {yyerror ("Missing term"); RECOVER;}
2416 | multiplicative_expression DIV_TK error
2417 {yyerror ("Missing term"); RECOVER;}
2418 | multiplicative_expression REM_TK error
2419 {yyerror ("Missing term"); RECOVER;}
2422 additive_expression:
2423 multiplicative_expression
2424 | additive_expression PLUS_TK multiplicative_expression
2426 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2429 | additive_expression MINUS_TK multiplicative_expression
2431 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2434 | additive_expression PLUS_TK error
2435 {yyerror ("Missing term"); RECOVER;}
2436 | additive_expression MINUS_TK error
2437 {yyerror ("Missing term"); RECOVER;}
2442 | shift_expression LS_TK additive_expression
2444 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2447 | shift_expression SRS_TK additive_expression
2449 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2452 | shift_expression ZRS_TK additive_expression
2454 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2457 | shift_expression LS_TK error
2458 {yyerror ("Missing term"); RECOVER;}
2459 | shift_expression SRS_TK error
2460 {yyerror ("Missing term"); RECOVER;}
2461 | shift_expression ZRS_TK error
2462 {yyerror ("Missing term"); RECOVER;}
2465 relational_expression:
2467 | relational_expression LT_TK shift_expression
2469 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2472 | relational_expression GT_TK shift_expression
2474 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2477 | relational_expression LTE_TK shift_expression
2479 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2482 | relational_expression GTE_TK shift_expression
2484 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2487 | relational_expression INSTANCEOF_TK reference_type
2488 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2489 | relational_expression LT_TK error
2490 {yyerror ("Missing term"); RECOVER;}
2491 | relational_expression GT_TK error
2492 {yyerror ("Missing term"); RECOVER;}
2493 | relational_expression LTE_TK error
2494 {yyerror ("Missing term"); RECOVER;}
2495 | relational_expression GTE_TK error
2496 {yyerror ("Missing term"); RECOVER;}
2497 | relational_expression INSTANCEOF_TK error
2498 {yyerror ("Invalid reference type"); RECOVER;}
2501 equality_expression:
2502 relational_expression
2503 | equality_expression EQ_TK relational_expression
2505 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2508 | equality_expression NEQ_TK relational_expression
2510 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2513 | equality_expression EQ_TK error
2514 {yyerror ("Missing term"); RECOVER;}
2515 | equality_expression NEQ_TK error
2516 {yyerror ("Missing term"); RECOVER;}
2521 | and_expression AND_TK equality_expression
2523 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2526 | and_expression AND_TK error
2527 {yyerror ("Missing term"); RECOVER;}
2530 exclusive_or_expression:
2532 | exclusive_or_expression XOR_TK and_expression
2534 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2537 | exclusive_or_expression XOR_TK error
2538 {yyerror ("Missing term"); RECOVER;}
2541 inclusive_or_expression:
2542 exclusive_or_expression
2543 | inclusive_or_expression OR_TK exclusive_or_expression
2545 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2548 | inclusive_or_expression OR_TK error
2549 {yyerror ("Missing term"); RECOVER;}
2552 conditional_and_expression:
2553 inclusive_or_expression
2554 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2556 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2559 | conditional_and_expression BOOL_AND_TK error
2560 {yyerror ("Missing term"); RECOVER;}
2563 conditional_or_expression:
2564 conditional_and_expression
2565 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2567 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2570 | conditional_or_expression BOOL_OR_TK error
2571 {yyerror ("Missing term"); RECOVER;}
2574 conditional_expression: /* Error handling here is weak */
2575 conditional_or_expression
2576 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2578 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2579 EXPR_WFL_LINECOL ($$) = $2.location;
2581 | conditional_or_expression REL_QM_TK REL_CL_TK error
2584 yyerror ("Missing term");
2587 | conditional_or_expression REL_QM_TK error
2588 {yyerror ("Missing term"); DRECOVER (2);}
2589 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2590 {yyerror ("Missing term"); DRECOVER (3);}
2593 assignment_expression:
2594 conditional_expression
2599 left_hand_side assignment_operator assignment_expression
2600 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2601 | left_hand_side assignment_operator error
2603 YYNOT_TWICE yyerror ("Missing term");
2614 assignment_operator:
2620 assignment_expression
2623 constant_expression:
2629 /* Helper function to retrieve an OSB count. Should be used when the
2630 `dims:' rule is being used. */
2633 pop_current_osb (struct parser_ctxt *ctxp)
2637 if (ctxp->osb_depth < 0)
2640 to_return = CURRENT_OSB (ctxp);
2648 /* This section of the code deal with save/restoring parser contexts.
2649 Add mode documentation here. FIXME */
2651 /* Helper function. Create a new parser context. With
2652 COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2653 context is copied, otherwise, the new context is zeroed. The newly
2654 created context becomes the current one. */
2657 create_new_parser_context (int copy_from_previous)
2659 struct parser_ctxt *new;
2661 new = ggc_alloc (sizeof (struct parser_ctxt));
2662 if (copy_from_previous)
2664 memcpy (new, ctxp, sizeof (struct parser_ctxt));
2665 new->saved_data_ctx = 1;
2668 memset (new, 0, sizeof (struct parser_ctxt));
2674 /* Create a new parser context and make it the current one. */
2677 java_push_parser_context (void)
2679 create_new_parser_context (0);
2683 java_pop_parser_context (int generate)
2686 struct parser_ctxt *toFree, *next;
2695 lineno = ctxp->lineno;
2696 current_class = ctxp->class_type;
2699 /* If the old and new lexers differ, then free the old one. */
2700 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2701 java_destroy_lexer (ctxp->lexer);
2703 /* Set the single import class file flag to 0 for the current list
2704 of imported things */
2705 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2706 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2708 /* And restore those of the previous context */
2709 if ((ctxp = next)) /* Assignment is really meant here */
2710 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2711 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2713 /* If we pushed a context to parse a class intended to be generated,
2714 we keep it so we can remember the class. What we could actually
2715 do is to just update a list of class names. */
2718 toFree->next = ctxp_for_generation;
2719 ctxp_for_generation = toFree;
2723 /* Create a parser context for the use of saving some global
2727 java_parser_context_save_global (void)
2731 java_push_parser_context ();
2732 ctxp->saved_data_ctx = 1;
2735 /* If this context already stores data, create a new one suitable
2736 for data storage. */
2737 else if (ctxp->saved_data)
2738 create_new_parser_context (1);
2740 ctxp->lineno = lineno;
2741 ctxp->class_type = current_class;
2742 ctxp->filename = input_filename;
2743 ctxp->function_decl = current_function_decl;
2744 ctxp->saved_data = 1;
2747 /* Restore some global variables from the previous context. Make the
2748 previous context the current one. */
2751 java_parser_context_restore_global (void)
2753 lineno = ctxp->lineno;
2754 current_class = ctxp->class_type;
2755 input_filename = ctxp->filename;
2759 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2760 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2762 current_function_decl = ctxp->function_decl;
2763 ctxp->saved_data = 0;
2764 if (ctxp->saved_data_ctx)
2765 java_pop_parser_context (0);
2768 /* Suspend vital data for the current class/function being parsed so
2769 that an other class can be parsed. Used to let local/anonymous
2770 classes be parsed. */
2773 java_parser_context_suspend (void)
2775 /* This makes debugging through java_debug_context easier */
2776 static const char *const name = "<inner buffer context>";
2778 /* Duplicate the previous context, use it to save the globals we're
2780 create_new_parser_context (1);
2781 ctxp->function_decl = current_function_decl;
2782 ctxp->class_type = current_class;
2784 /* Then create a new context which inherits all data from the
2785 previous one. This will be the new current context */
2786 create_new_parser_context (1);
2788 /* Help debugging */
2789 ctxp->next->filename = name;
2792 /* Resume vital data for the current class/function being parsed so
2793 that an other class can be parsed. Used to let local/anonymous
2794 classes be parsed. The trick is the data storing file position
2795 informations must be restored to their current value, so parsing
2796 can resume as if no context was ever saved. */
2799 java_parser_context_resume (void)
2801 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2802 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2803 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2805 /* We need to inherit the list of classes to complete/generate */
2806 restored->classd_list = old->classd_list;
2807 restored->class_list = old->class_list;
2809 /* Restore the current class and function from the saver */
2810 current_class = saver->class_type;
2811 current_function_decl = saver->function_decl;
2813 /* Retrive the restored context */
2816 /* Re-installed the data for the parsing to carry on */
2817 memcpy (&ctxp->marker_begining, &old->marker_begining,
2818 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2821 /* Add a new anchor node to which all statement(s) initializing static
2822 and non static initialized upon declaration field(s) will be
2826 java_parser_context_push_initialized_field (void)
2830 node = build_tree_list (NULL_TREE, NULL_TREE);
2831 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2832 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2834 node = build_tree_list (NULL_TREE, NULL_TREE);
2835 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2836 CPC_INITIALIZER_LIST (ctxp) = node;
2838 node = build_tree_list (NULL_TREE, NULL_TREE);
2839 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2840 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2843 /* Pop the lists of initialized field. If this lists aren't empty,
2844 remember them so we can use it to create and populate the finit$
2845 or <clinit> functions. */
2848 java_parser_context_pop_initialized_field (void)
2851 tree class_type = TREE_TYPE (GET_CPC ());
2853 if (CPC_INITIALIZER_LIST (ctxp))
2855 stmts = CPC_INITIALIZER_STMT (ctxp);
2856 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2857 if (stmts && !java_error_count)
2858 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2861 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2863 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2864 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2865 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2866 /* Keep initialization in order to enforce 8.5 */
2867 if (stmts && !java_error_count)
2868 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2871 /* JDK 1.1 instance initializers */
2872 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2874 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2875 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2876 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2877 if (stmts && !java_error_count)
2878 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2883 reorder_static_initialized (tree list)
2885 /* We have to keep things in order. The alias initializer have to
2886 come first, then the initialized regular field, in reverse to
2887 keep them in lexical order. */
2888 tree marker, previous = NULL_TREE;
2889 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2890 if (TREE_CODE (marker) == TREE_LIST
2891 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2894 /* No static initialized, the list is fine as is */
2896 list = TREE_CHAIN (marker);
2898 /* No marker? reverse the whole list */
2900 list = nreverse (list);
2902 /* Otherwise, reverse what's after the marker and the new reordered
2903 sublist will replace the marker. */
2906 TREE_CHAIN (previous) = NULL_TREE;
2907 list = nreverse (list);
2908 list = chainon (TREE_CHAIN (marker), list);
2913 /* Helper functions to dump the parser context stack. */
2915 #define TAB_CONTEXT(C) \
2916 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2919 java_debug_context_do (int tab)
2921 struct parser_ctxt *copy = ctxp;
2925 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2927 fprintf (stderr, "filename: %s\n", copy->filename);
2929 fprintf (stderr, "lineno: %d\n", copy->lineno);
2931 fprintf (stderr, "package: %s\n",
2933 IDENTIFIER_POINTER (copy->package) : "<none>"));
2935 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2937 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2943 /* Dump the stacked up parser contexts. Intended to be called from a
2947 java_debug_context (void)
2949 java_debug_context_do (0);
2954 /* Flag for the error report routine to issue the error the first time
2955 it's called (overriding the default behavior which is to drop the
2956 first invocation and honor the second one, taking advantage of a
2958 static int force_error = 0;
2960 /* Reporting an constructor invocation error. */
2962 parse_ctor_invocation_error (void)
2964 if (DECL_CONSTRUCTOR_P (current_function_decl))
2965 yyerror ("Constructor invocation must be first thing in a constructor");
2967 yyerror ("Only constructors can invoke constructors");
2970 /* Reporting JDK1.1 features not implemented. */
2973 parse_jdk1_1_error (const char *msg)
2975 sorry (": `%s' JDK1.1(TM) feature", msg);
2977 return empty_stmt_node;
2980 static int do_warning = 0;
2983 yyerror (const char *msg)
2986 static int prev_lineno;
2987 static const char *prev_msg;
2990 char *remainder, *code_from_source;
2992 if (!force_error && prev_lineno == lineno)
2995 /* Save current error location but report latter, when the context is
2997 if (ctxp->java_error_flag == 0)
2999 ctxp->java_error_flag = 1;
3001 /* Do something to use the previous line if we're reaching the
3002 end of the file... */
3003 #ifdef VERBOSE_SKELETON
3004 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
3009 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3010 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
3013 ctxp->java_error_flag = 0;
3015 java_warning_count++;
3019 if (elc.col == 0 && msg && msg[1] == ';')
3021 elc.col = ctxp->p_line->char_col-1;
3022 elc.line = ctxp->p_line->lineno;
3025 save_lineno = lineno;
3026 prev_lineno = lineno = elc.line;
3029 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3030 obstack_grow0 (&temporary_obstack,
3031 code_from_source, strlen (code_from_source));
3032 remainder = obstack_finish (&temporary_obstack);
3034 warning ("%s.\n%s", msg, remainder);
3036 error ("%s.\n%s", msg, remainder);
3038 /* This allow us to cheaply avoid an extra 'Invalid expression
3039 statement' error report when errors have been already reported on
3040 the same line. This occurs when we report an error but don't have
3041 a synchronization point other than ';', which
3042 expression_statement is the only one to take care of. */
3043 ctxp->prevent_ese = lineno = save_lineno;
3047 issue_warning_error_from_context (tree cl, const char *msg, va_list ap)
3049 const char *saved, *saved_input_filename;
3051 vsprintf (buffer, msg, ap);
3054 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3055 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3056 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3058 /* We have a CL, that's a good reason for using it if it contains data */
3059 saved = ctxp->filename;
3060 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3061 ctxp->filename = EXPR_WFL_FILENAME (cl);
3062 saved_input_filename = input_filename;
3063 input_filename = ctxp->filename;
3065 java_error (buffer);
3066 ctxp->filename = saved;
3067 input_filename = saved_input_filename;
3071 /* Issue an error message at a current source line CL */
3074 parse_error_context (tree cl, const char *msg, ...)
3078 issue_warning_error_from_context (cl, msg, ap);
3082 /* Issue a warning at a current source line CL */
3085 parse_warning_context (tree cl, const char *msg, ...)
3090 force_error = do_warning = 1;
3091 issue_warning_error_from_context (cl, msg, ap);
3092 do_warning = force_error = 0;
3097 find_expr_with_wfl (tree node)
3104 switch (TREE_CODE (node))
3107 node = BLOCK_EXPR_BODY (node);
3111 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3114 node = TREE_OPERAND (node, 1);
3118 node = TREE_OPERAND (node, 0);
3121 case LABELED_BLOCK_EXPR:
3122 node = TREE_OPERAND (node, 1);
3126 code = TREE_CODE_CLASS (TREE_CODE (node));
3127 if (((code == '1') || (code == '2') || (code == 'e'))
3128 && EXPR_WFL_LINECOL (node))
3136 /* Issue a missing return statement error. Uses METHOD to figure the
3137 last line of the method the error occurs in. */
3140 missing_return_error (tree 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 (tree node)
3151 /* Browse node to find the next expression node that has a WFL. Use
3152 the location to report the error */
3153 if (TREE_CODE (node) == COMPOUND_EXPR)
3154 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3156 node = find_expr_with_wfl (node);
3160 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3161 parse_error_context (wfl_operator, "Unreachable statement");
3168 java_report_errors (void)
3170 if (java_error_count)
3171 fprintf (stderr, "%d error%s",
3172 java_error_count, (java_error_count == 1 ? "" : "s"));
3173 if (java_warning_count)
3174 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3175 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3176 if (java_error_count || java_warning_count)
3177 putc ('\n', stderr);
3178 return java_error_count;
3182 java_accstring_lookup (int flags)
3184 static char buffer [80];
3185 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3187 /* Access modifier looked-up first for easier report on forbidden
3189 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3190 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3191 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3192 if (flags & ACC_STATIC) COPY_RETURN ("static");
3193 if (flags & ACC_FINAL) COPY_RETURN ("final");
3194 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3195 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3196 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3197 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3198 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3199 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3206 /* Issuing error messages upon redefinition of classes, interfaces or
3210 classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3212 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3213 context, IDENTIFIER_POINTER (id),
3214 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3215 /* Here we should point out where its redefined. It's a unicode. FIXME */
3219 variable_redefinition_error (tree context, tree name, tree type, int line)
3221 const char *type_name;
3223 /* Figure a proper name for type. We might haven't resolved it */
3224 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3225 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3227 type_name = lang_printable_name (type, 0);
3229 parse_error_context (context,
3230 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3231 IDENTIFIER_POINTER (name),
3232 type_name, IDENTIFIER_POINTER (name), line);
3235 /* If ANAME is terminated with `[]', it indicates an array. This
3236 function returns the number of `[]' found and if this number is
3237 greater than zero, it extracts the array type name and places it in
3238 the node pointed to by TRIMMED unless TRIMMED is null. */
3241 build_type_name_from_array_name (tree aname, tree *trimmed)
3243 const char *name = IDENTIFIER_POINTER (aname);
3244 int len = IDENTIFIER_LENGTH (aname);
3247 STRING_STRIP_BRACKETS (name, len, array_dims);
3249 if (array_dims && trimmed)
3250 *trimmed = get_identifier_with_length (name, len);
3256 build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3260 /* Eventually get more dims */
3261 more_dims = build_type_name_from_array_name (name, &name);
3263 /* If we have, then craft a new type for this variable */
3268 /* If we have a pointer, use its type */
3269 if (TREE_CODE (type) == POINTER_TYPE)
3270 type = TREE_TYPE (type);
3272 /* Building the first dimension of a primitive type uses this
3274 if (JPRIMITIVE_TYPE_P (type))
3276 type = build_java_array_type (type, -1);
3279 /* Otherwise, if we have a WFL for this type, use it (the type
3280 is already an array on an unresolved type, and we just keep
3281 on adding dimensions) */
3285 more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3289 /* Add all the dimensions */
3291 type = build_unresolved_array_type (type);
3293 /* The type may have been incomplete in the first place */
3295 type = obtain_incomplete_type (type);
3303 /* Build something that the type identifier resolver will identify as
3304 being an array to an unresolved type. TYPE_WFL is a WFL on a
3308 build_unresolved_array_type (tree type_or_wfl)
3313 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3314 just create a array type */
3315 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3316 return build_java_array_type (type_or_wfl, -1);
3318 obstack_grow (&temporary_obstack,
3319 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3320 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3321 obstack_grow0 (&temporary_obstack, "[]", 2);
3322 ptr = obstack_finish (&temporary_obstack);
3323 wfl = build_expr_wfl (get_identifier (ptr),
3324 EXPR_WFL_FILENAME (type_or_wfl),
3325 EXPR_WFL_LINENO (type_or_wfl),
3326 EXPR_WFL_COLNO (type_or_wfl));
3327 /* Re-install the existing qualifications so that the type can be
3328 resolved properly. */
3329 EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3334 parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3336 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3337 parse_error_context (wfl, "Interface `%s' repeated",
3338 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3341 /* Bulk of common class/interface checks. Return 1 if an error was
3342 encountered. TAG is 0 for a class, 1 for an interface. */
3345 check_class_interface_creation (int is_interface, int flags, tree raw_name,
3346 tree qualified_name, tree decl, tree cl)
3349 int sca = 0; /* Static class allowed */
3350 int icaf = 0; /* Inner class allowed flags */
3351 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3354 fprintf (stderr, " %s%s %s",
3355 (CPC_INNER_P () ? "inner" : ""),
3356 (is_interface ? "interface" : "class"),
3357 IDENTIFIER_POINTER (qualified_name));
3359 /* Scope of an interface/class type name:
3360 - Can't be imported by a single type import
3361 - Can't already exists in the package */
3362 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3363 && (node = find_name_in_single_imports (raw_name))
3367 (cl, "%s name `%s' clashes with imported type `%s'",
3368 (is_interface ? "Interface" : "Class"),
3369 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3372 if (decl && CLASS_COMPLETE_P (decl))
3374 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3375 qualified_name, decl, cl);
3379 if (check_inner_class_redefinition (raw_name, cl))
3382 /* If public, file name should match class/interface name, except
3383 when dealing with an inner class */
3384 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3388 for (f = &input_filename [strlen (input_filename)];
3389 f != input_filename && ! IS_DIR_SEPARATOR (f[0]);
3392 if (IS_DIR_SEPARATOR (f[0]))
3394 if (strncmp (IDENTIFIER_POINTER (raw_name),
3395 f , IDENTIFIER_LENGTH (raw_name)) ||
3396 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3398 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3399 (is_interface ? "interface" : "class"),
3400 IDENTIFIER_POINTER (qualified_name),
3401 IDENTIFIER_POINTER (raw_name));
3404 /* Static classes can be declared only in top level classes. Note:
3405 once static, a inner class is a top level class. */
3406 if (flags & ACC_STATIC)
3408 /* Catch the specific error of declaring an class inner class
3409 with no toplevel enclosing class. Prevent check_modifiers from
3410 complaining a second time */
3411 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3413 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3414 IDENTIFIER_POINTER (qualified_name));
3417 /* Else, in the context of a top-level class declaration, let
3418 `check_modifiers' do its job, otherwise, give it a go */
3420 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3423 /* Inner classes can be declared private or protected
3424 within their enclosing classes. */
3427 /* A class which is local to a block can't be public, private,
3428 protected or static. But it is created final, so allow this
3430 if (current_function_decl)
3431 icaf = sca = uaaf = ACC_FINAL;
3434 check_modifiers_consistency (flags);
3435 icaf = ACC_PROTECTED;
3436 if (! CLASS_INTERFACE (GET_CPC ()))
3437 icaf |= ACC_PRIVATE;
3444 uaaf = INTERFACE_INNER_MODIFIERS;
3446 uaaf = INTERFACE_MODIFIERS;
3448 check_modifiers ("Illegal modifier `%s' for interface declaration",
3452 check_modifiers ((current_function_decl ?
3453 "Illegal modifier `%s' for local class declaration" :
3454 "Illegal modifier `%s' for class declaration"),
3455 flags, uaaf|sca|icaf);
3459 /* Construct a nested class name. If the final component starts with
3460 a digit, return true. Otherwise return false. */
3462 make_nested_class_name (tree cpc_list)
3469 make_nested_class_name (TREE_CHAIN (cpc_list));
3471 /* Pick the qualified name when dealing with the first upmost
3473 name = (TREE_CHAIN (cpc_list)
3474 ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3475 obstack_grow (&temporary_obstack,
3476 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3477 obstack_1grow (&temporary_obstack, '$');
3479 return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3482 /* Can't redefine a class already defined in an earlier scope. */
3485 check_inner_class_redefinition (tree raw_name, tree cl)
3489 for (scope_list = GET_CPC_LIST (); scope_list;
3490 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3491 if (raw_name == GET_CPC_UN_NODE (scope_list))
3494 (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",
3495 IDENTIFIER_POINTER (raw_name));
3501 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3502 we remember ENCLOSING and SUPER. */
3505 resolve_inner_class (htab_t circularity_hash, tree cl, tree *enclosing,
3506 tree *super, tree class_type)
3508 tree local_enclosing = *enclosing;
3509 tree local_super = NULL_TREE;
3511 while (local_enclosing)
3513 tree intermediate, decl;
3515 *htab_find_slot (circularity_hash, local_enclosing, INSERT) =
3518 if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3521 intermediate = local_enclosing;
3522 /* Explore enclosing contexts. */
3523 while (INNER_CLASS_DECL_P (intermediate))
3525 intermediate = DECL_CONTEXT (intermediate);
3526 if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3530 /* Now go to the upper classes, bail out if necessary. We will
3531 analyze the returned SUPER and act accordingly (see
3532 do_resolve_class.) */
3533 local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3534 if (!local_super || local_super == object_type_node)
3537 if (TREE_CODE (local_super) == POINTER_TYPE)
3538 local_super = do_resolve_class (NULL, local_super, NULL, NULL);
3540 local_super = TYPE_NAME (local_super);
3542 /* We may not have checked for circular inheritance yet, so do so
3543 here to prevent an infinite loop. */
3544 if (htab_find (circularity_hash, local_super) != NULL)
3547 cl = lookup_cl (local_enclosing);
3550 (cl, "Cyclic inheritance involving %s",
3551 IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3552 local_enclosing = NULL_TREE;
3555 local_enclosing = local_super;
3558 /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3559 *super = local_super;
3560 *enclosing = local_enclosing;
3565 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3569 find_as_inner_class (tree enclosing, tree name, tree cl)
3571 tree qual, to_return;
3575 name = TYPE_NAME (name);
3577 /* First search: within the scope of `enclosing', search for name */
3578 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3579 qual = EXPR_WFL_QUALIFICATION (cl);
3581 qual = build_tree_list (cl, NULL_TREE);
3583 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3585 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3588 /* We're dealing with a qualified name. Try to resolve thing until
3589 we get something that is an enclosing class. */
3590 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3592 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3594 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3595 qual = TREE_CHAIN (qual))
3597 acc = merge_qualified_name (acc,
3598 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3599 BUILD_PTR_FROM_NAME (ptr, acc);
3600 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3603 /* A NULL qual and a decl means that the search ended
3604 successfully?!? We have to do something then. FIXME */
3609 qual = EXPR_WFL_QUALIFICATION (cl);
3611 /* Otherwise, create a qual for the other part of the resolution. */
3613 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3615 return find_as_inner_class_do (qual, enclosing);
3618 /* We go inside the list of sub classes and try to find a way
3622 find_as_inner_class_do (tree qual, tree enclosing)
3627 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3629 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3630 tree next_enclosing = NULL_TREE;
3633 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3634 inner_list; inner_list = TREE_CHAIN (inner_list))
3636 if (TREE_VALUE (inner_list) == name_to_match)
3638 next_enclosing = TREE_PURPOSE (inner_list);
3642 enclosing = next_enclosing;
3645 return (!qual && enclosing ? enclosing : NULL_TREE);
3648 /* Reach all inner classes and tie their unqualified name to a
3652 set_nested_class_simple_name_value (tree outer, int set)
3656 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3657 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3658 TREE_PURPOSE (l) : NULL_TREE);
3662 link_nested_class_to_enclosing (void)
3664 if (GET_ENCLOSING_CPC ())
3666 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3667 DECL_INNER_CLASS_LIST (enclosing) =
3668 tree_cons (GET_CPC (), GET_CPC_UN (),
3669 DECL_INNER_CLASS_LIST (enclosing));
3674 maybe_make_nested_class_name (tree name)
3676 tree id = NULL_TREE;
3680 /* If we're in a function, we must append a number to create the
3681 nested class name. However, we don't do this if the class we
3682 are constructing is anonymous, because in that case we'll
3683 already have a number as the class name. */
3684 if (! make_nested_class_name (GET_CPC_LIST ())
3685 && current_function_decl != NULL_TREE
3686 && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3689 sprintf (buf, "%d", anonymous_class_counter);
3690 ++anonymous_class_counter;
3691 obstack_grow (&temporary_obstack, buf, strlen (buf));
3692 obstack_1grow (&temporary_obstack, '$');
3694 obstack_grow0 (&temporary_obstack,
3695 IDENTIFIER_POINTER (name),
3696 IDENTIFIER_LENGTH (name));
3697 id = get_identifier (obstack_finish (&temporary_obstack));
3699 QUALIFIED_P (id) = 1;
3704 /* If DECL is NULL, create and push a new DECL, record the current
3705 line CL and do other maintenance things. */
3708 maybe_create_class_interface_decl (tree decl, tree raw_name,
3709 tree qualified_name, tree cl)
3712 decl = push_class (make_class (), qualified_name);
3714 /* Take care of the file and line business */
3715 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3716 /* If we're emiting xrefs, store the line/col number information */
3718 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3720 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3721 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3722 CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3723 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3724 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3726 PUSH_CPC (decl, raw_name);
3727 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3729 /* Link the declaration to the already seen ones */
3730 TREE_CHAIN (decl) = ctxp->class_list;
3731 ctxp->class_list = decl;
3733 /* Create a new nodes in the global lists */
3734 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3735 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3737 /* Install a new dependency list element */
3738 create_jdep_list (ctxp);
3740 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3741 IDENTIFIER_POINTER (qualified_name)));
3746 add_superinterfaces (tree decl, tree interface_list)
3749 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3750 takes care of ensuring that:
3751 - This is an accessible interface type,
3752 - Circularity detection.
3753 parser_add_interface is then called. If present but not defined,
3754 the check operation is delayed until the super interface gets
3756 for (node = interface_list; node; node = TREE_CHAIN (node))
3758 tree current = TREE_PURPOSE (node);
3759 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3760 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3762 if (!parser_check_super_interface (idecl, decl, current))
3763 parser_add_interface (decl, idecl, current);
3766 register_incomplete_type (JDEP_INTERFACE,
3767 current, decl, NULL_TREE);
3771 /* Create an interface in pass1 and return its decl. Return the
3772 interface's decl in pass 2. */
3775 create_interface (int flags, tree id, tree super)
3777 tree raw_name = EXPR_WFL_NODE (id);
3778 tree q_name = parser_qualified_classname (raw_name);
3779 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3781 /* Certain syntax errors are making SUPER be like ID. Avoid this
3783 if (ctxp->class_err && id == super)
3786 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3788 /* Basic checks: scope, redefinition, modifiers */
3789 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3795 /* Suspend the current parsing context if we're parsing an inner
3799 java_parser_context_suspend ();
3800 /* Interface members are public. */
3801 if (CLASS_INTERFACE (GET_CPC ()))
3802 flags |= ACC_PUBLIC;
3805 /* Push a new context for (static) initialized upon declaration fields */
3806 java_parser_context_push_initialized_field ();
3808 /* Interface modifiers check
3809 - public/abstract allowed (already done at that point)
3810 - abstract is obsolete (comes first, it's a warning, or should be)
3811 - Can't use twice the same (checked in the modifier rule) */
3812 if ((flags & ACC_ABSTRACT) && flag_redundant)
3813 parse_warning_context
3814 (MODIFIER_WFL (ABSTRACT_TK),
3815 "Redundant use of `abstract' modifier. Interface `%s' is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3817 /* Create a new decl if DECL is NULL, otherwise fix it */
3818 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3820 /* Set super info and mark the class a complete */
3821 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3822 object_type_node, ctxp->interface_number);
3823 ctxp->interface_number = 0;
3824 CLASS_COMPLETE_P (decl) = 1;
3825 add_superinterfaces (decl, super);
3830 /* Patch anonymous class CLASS, by either extending or implementing
3834 patch_anonymous_class (tree type_decl, tree class_decl, tree wfl)
3836 tree class = TREE_TYPE (class_decl);
3837 tree type = TREE_TYPE (type_decl);
3838 tree binfo = TYPE_BINFO (class);
3840 /* If it's an interface, implement it */
3841 if (CLASS_INTERFACE (type_decl))
3846 if (parser_check_super_interface (type_decl, class_decl, wfl))
3849 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3850 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3851 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3852 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3853 /* And add the interface */
3854 parser_add_interface (class_decl, type_decl, wfl);
3856 /* Otherwise, it's a type we want to extend */
3859 if (parser_check_super (type_decl, class_decl, wfl))
3861 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3866 create_anonymous_class (int location, tree type_name)
3869 tree super = NULL_TREE, itf = NULL_TREE;
3870 tree id, type_decl, class;
3872 /* The unqualified name of the anonymous class. It's just a number. */
3873 sprintf (buffer, "%d", anonymous_class_counter++);
3874 id = build_wfl_node (get_identifier (buffer));
3875 EXPR_WFL_LINECOL (id) = location;
3877 /* We know about the type to extend/implement. We go ahead */
3878 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3880 /* Create a class which either implements on extends the designated
3881 class. The class bears an innacessible name. */
3882 if (CLASS_INTERFACE (type_decl))
3884 /* It's OK to modify it here. It's been already used and
3885 shouldn't be reused */
3886 ctxp->interface_number = 1;
3887 /* Interfaces should presented as a list of WFLs */
3888 itf = build_tree_list (type_name, NULL_TREE);
3894 class = create_class (ACC_FINAL, id, super, itf);
3896 /* We didn't know anything about the stuff. We register a dependence. */
3898 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3900 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3904 /* Create a class in pass1 and return its decl. Return class
3905 interface's decl in pass 2. */
3908 create_class (int flags, tree id, tree super, tree interfaces)
3910 tree raw_name = EXPR_WFL_NODE (id);
3911 tree class_id, decl;
3912 tree super_decl_type;
3914 /* Certain syntax errors are making SUPER be like ID. Avoid this
3916 if (ctxp->class_err && id == super)
3919 class_id = parser_qualified_classname (raw_name);
3920 decl = IDENTIFIER_CLASS_VALUE (class_id);
3921 EXPR_WFL_NODE (id) = class_id;
3923 /* Basic check: scope, redefinition, modifiers */
3924 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3930 /* Suspend the current parsing context if we're parsing an inner
3931 class or an anonymous class. */
3934 java_parser_context_suspend ();
3935 /* Interface members are public. */
3936 if (CLASS_INTERFACE (GET_CPC ()))
3937 flags |= ACC_PUBLIC;
3940 /* Push a new context for (static) initialized upon declaration fields */
3941 java_parser_context_push_initialized_field ();
3943 /* Class modifier check:
3944 - Allowed modifier (already done at that point)
3945 - abstract AND final forbidden
3946 - Public classes defined in the correct file */
3947 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3949 (id, "Class `%s' can't be declared both abstract and final",
3950 IDENTIFIER_POINTER (raw_name));
3952 /* Create a new decl if DECL is NULL, otherwise fix it */
3953 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3955 /* If SUPER exists, use it, otherwise use Object */
3958 /* Can't extend java.lang.Object */
3959 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3961 parse_error_context (id, "Can't extend `java.lang.Object'");
3966 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3968 else if (TREE_TYPE (decl) != object_type_node)
3969 super_decl_type = object_type_node;
3970 /* We're defining java.lang.Object */
3972 super_decl_type = NULL_TREE;
3974 /* A class nested in an interface is implicitly static. */
3975 if (INNER_CLASS_DECL_P (decl)
3976 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3978 flags |= ACC_STATIC;
3981 /* Set super info and mark the class as complete. */
3982 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3983 ctxp->interface_number);
3984 ctxp->interface_number = 0;
3985 CLASS_COMPLETE_P (decl) = 1;
3986 add_superinterfaces (decl, interfaces);
3988 /* Add the private this$<n> field, Replicate final locals still in
3989 scope as private final fields mangled like val$<local_name>.
3990 This doesn't not occur for top level (static) inner classes. */
3991 if (PURE_INNER_CLASS_DECL_P (decl))
3992 add_inner_class_fields (decl, current_function_decl);
3994 /* If doing xref, store the location at which the inherited class
3995 (if any) was seen. */
3996 if (flag_emit_xref && super)
3997 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3999 /* Eventually sets the @deprecated tag flag */
4000 CHECK_DEPRECATED (decl);
4002 /* Reset the anonymous class counter when declaring non inner classes */
4003 if (!INNER_CLASS_DECL_P (decl))
4004 anonymous_class_counter = 1;
4009 /* End a class declaration: register the statements used to create
4010 finit$ and <clinit>, pop the current class and resume the prior
4011 parser context if necessary. */
4014 end_class_declaration (int resume)
4016 /* If an error occurred, context weren't pushed and won't need to be
4017 popped by a resume. */
4018 int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4020 if (GET_CPC () != error_mark_node)
4021 dump_java_tree (TDI_class, GET_CPC ());
4023 java_parser_context_pop_initialized_field ();
4025 if (resume && no_error_occurred)
4026 java_parser_context_resume ();
4028 /* We're ending a class declaration, this is a good time to reset
4029 the interface cout. Note that might have been already done in
4030 create_interface, but if at that time an inner class was being
4031 dealt with, the interface count was reset in a context created
4032 for the sake of handling inner classes declaration. */
4033 ctxp->interface_number = 0;
4037 add_inner_class_fields (tree class_decl, tree fct_decl)
4039 tree block, marker, f;
4041 f = add_field (TREE_TYPE (class_decl),
4042 build_current_thisn (TREE_TYPE (class_decl)),
4043 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4045 FIELD_THISN (f) = 1;
4050 for (block = GET_CURRENT_BLOCK (fct_decl);
4051 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4054 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4057 tree wfl, init, list;
4059 /* Avoid non final arguments. */
4060 if (!LOCAL_FINAL_P (decl))
4063 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4064 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4065 wfl = build_wfl_node (name);
4066 init = build_wfl_node (pname);
4067 /* Build an initialization for the field: it will be
4068 initialized by a parameter added to finit$, bearing a
4069 mangled name of the field itself (param$<n>.) The
4070 parameter is provided to finit$ by the constructor
4071 invoking it (hence the constructor will also feature a
4072 hidden parameter, set to the value of the outer context
4073 local at the time the inner class is created.)
4075 Note: we take into account all possible locals that can
4076 be accessed by the inner class. It's actually not trivial
4077 to minimize these aliases down to the ones really
4078 used. One way to do that would be to expand all regular
4079 methods first, then finit$ to get a picture of what's
4080 used. It works with the exception that we would have to
4081 go back on all constructor invoked in regular methods to
4082 have their invokation reworked (to include the right amount
4083 of alias initializer parameters.)
4085 The only real way around, I think, is a first pass to
4086 identify locals really used in the inner class. We leave
4087 the flag FIELD_LOCAL_ALIAS_USED around for that future
4090 On the other hand, it only affect local inner classes,
4091 whose constructors (and finit$ call) will be featuring
4092 unecessary arguments. It's easy for a developper to keep
4093 this number of parameter down by using the `final'
4094 keyword only when necessary. For the time being, we can
4095 issue a warning on unecessary finals. FIXME */
4096 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4099 /* Register the field. The TREE_LIST holding the part
4100 initialized/initializer will be marked ARG_FINAL_P so
4101 that the created field can be marked
4102 FIELD_LOCAL_ALIAS. */
4103 list = build_tree_list (wfl, init);
4104 ARG_FINAL_P (list) = 1;
4105 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4109 if (!CPC_INITIALIZER_STMT (ctxp))
4112 /* If we ever registered an alias field, insert and marker to
4113 remeber where the list ends. The second part of the list (the one
4114 featuring initialized fields) so it can be later reversed to
4115 enforce 8.5. The marker will be removed during that operation. */
4116 marker = build_tree_list (NULL_TREE, NULL_TREE);
4117 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4118 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4121 /* Can't use lookup_field () since we don't want to load the class and
4122 can't set the CLASS_LOADED_P flag */
4125 find_field (tree class, tree name)
4128 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4130 if (DECL_NAME (decl) == name)
4136 /* Wrap around lookup_field that doesn't potentially upset the value
4140 lookup_field_wrapper (tree class, tree name)
4143 tree decl = NULL_TREE;
4144 java_parser_context_save_global ();
4146 /* Last chance: if we're within the context of an inner class, we
4147 might be trying to access a local variable defined in an outer
4148 context. We try to look for it now. */
4149 if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4152 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4153 decl = lookup_field (&type, new_name);
4154 if (decl && decl != error_mark_node)
4155 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4157 if (!decl || decl == error_mark_node)
4160 decl = lookup_field (&type, name);
4163 /* If the field still hasn't been found, try the next enclosing context. */
4164 if (!decl && INNER_CLASS_TYPE_P (class))
4166 tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4167 decl = lookup_field_wrapper (outer_type, name);
4170 java_parser_context_restore_global ();
4171 return decl == error_mark_node ? NULL : decl;
4174 /* Find duplicate field within the same class declarations and report
4175 the error. Returns 1 if a duplicated field was found, 0
4179 duplicate_declaration_error_p (tree new_field_name, tree new_type, tree cl)
4181 /* This might be modified to work with method decl as well */
4182 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4185 char *t1 = xstrdup (purify_type_name
4186 ((TREE_CODE (new_type) == POINTER_TYPE
4187 && TREE_TYPE (new_type) == NULL_TREE) ?
4188 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4189 lang_printable_name (new_type, 1)));
4190 /* The type may not have been completed by the time we report
4192 char *t2 = xstrdup (purify_type_name
4193 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4194 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4195 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4196 lang_printable_name (TREE_TYPE (decl), 1)));
4198 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4199 t1, IDENTIFIER_POINTER (new_field_name),
4200 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4201 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4209 /* Field registration routine. If TYPE doesn't exist, field
4210 declarations are linked to the undefined TYPE dependency list, to
4211 be later resolved in java_complete_class () */
4214 register_fields (int flags, tree type, tree variable_list)
4216 tree current, saved_type;
4217 tree class_type = NULL_TREE;
4218 int saved_lineno = lineno;
4220 tree wfl = NULL_TREE;
4223 class_type = TREE_TYPE (GET_CPC ());
4225 if (!class_type || class_type == error_mark_node)
4228 /* If we're adding fields to interfaces, those fields are public,
4230 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4232 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4233 flags, ACC_PUBLIC, "interface field(s)");
4234 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4235 flags, ACC_STATIC, "interface field(s)");
4236 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4237 flags, ACC_FINAL, "interface field(s)");
4238 check_modifiers ("Illegal interface member modifier `%s'", flags,
4239 INTERFACE_FIELD_MODIFIERS);
4240 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4243 /* Obtain a suitable type for resolution, if necessary */
4244 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4246 /* If TYPE is fully resolved and we don't have a reference, make one */
4247 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4249 for (current = variable_list, saved_type = type; current;
4250 current = TREE_CHAIN (current), type = saved_type)
4254 tree cl = TREE_PURPOSE (current);
4255 tree init = TREE_VALUE (current);
4256 tree current_name = EXPR_WFL_NODE (cl);
4258 /* Can't declare non-final static fields in inner classes */
4259 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4260 && !(flags & ACC_FINAL))
4262 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4263 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4264 lang_printable_name (class_type, 0));
4266 /* Process NAME, as it may specify extra dimension(s) for it */
4267 type = build_array_from_name (type, wfl, current_name, ¤t_name);
4269 /* Type adjustment. We may have just readjusted TYPE because
4270 the variable specified more dimensions. Make sure we have
4271 a reference if we can and don't have one already. Also
4272 change the name if we have an init. */
4273 if (type != saved_type)
4275 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4277 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4280 real_type = GET_REAL_TYPE (type);
4281 /* Check for redeclarations */
4282 if (duplicate_declaration_error_p (current_name, real_type, cl))
4285 /* Set lineno to the line the field was found and create a
4286 declaration for it. Eventually sets the @deprecated tag flag. */
4288 lineno = EXPR_WFL_LINECOL (cl);
4290 lineno = EXPR_WFL_LINENO (cl);
4291 field_decl = add_field (class_type, current_name, real_type, flags);
4292 CHECK_DEPRECATED (field_decl);
4294 /* If the field denotes a final instance variable, then we
4295 allocate a LANG_DECL_SPECIFIC part to keep track of its
4296 initialization. We also mark whether the field was
4297 initialized upon its declaration. We don't do that if the
4298 created field is an alias to a final local. */
4299 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4301 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4302 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4305 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4306 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4307 hide parameters to this inner class finit$ and
4308 constructors. It also means that the field isn't final per
4310 if (ARG_FINAL_P (current))
4312 FIELD_LOCAL_ALIAS (field_decl) = 1;
4313 FIELD_FINAL (field_decl) = 0;
4316 /* Check if we must chain. */
4318 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4320 /* If we have an initialization value tied to the field */
4323 /* The field is declared static */
4324 if (flags & ACC_STATIC)
4326 /* We include the field and its initialization part into
4327 a list used to generate <clinit>. After <clinit> is
4328 walked, field initializations will be processed and
4329 fields initialized with known constants will be taken
4330 out of <clinit> and have their DECL_INITIAL set
4332 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4333 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4334 if (TREE_OPERAND (init, 1)
4335 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4336 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4338 /* A non-static field declared with an immediate initialization is
4339 to be initialized in <init>, if any. This field is remembered
4340 to be processed at the time of the generation of <init>. */
4343 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4344 SET_CPC_INITIALIZER_STMT (ctxp, init);
4346 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4347 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4350 lineno = saved_lineno;
4353 /* Generate finit$, using the list of initialized fields to populate
4354 its body. finit$'s parameter(s) list is adjusted to include the
4355 one(s) used to initialized the field(s) caching outer context
4359 generate_finit (tree class_type)
4362 tree list = TYPE_FINIT_STMT_LIST (class_type);
4363 tree mdecl, current, parms;
4365 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4366 class_type, NULL_TREE,
4368 CRAFTED_PARAM_LIST_FIXUP (parms);
4369 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4370 finit_identifier_node, parms);
4371 fix_method_argument_names (parms, mdecl);
4372 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4374 DECL_FUNCTION_NAP (mdecl) = count;
4375 start_artificial_method_body (mdecl);
4377 for (current = list; current; current = TREE_CHAIN (current))
4378 java_method_add_stmt (mdecl,
4379 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4381 end_artificial_method_body (mdecl);
4385 /* Generate a function to run the instance initialization code. The
4386 private method is called `instinit$'. Unless we're dealing with an
4387 anonymous class, we determine whether all ctors of CLASS_TYPE
4388 declare a checked exception in their `throws' clause in order to
4389 see whether it's necessary to encapsulate the instance initializer
4390 statements in a try/catch/rethrow sequence. */
4393 generate_instinit (tree class_type)
4396 tree compound = NULL_TREE;
4397 tree parms = tree_cons (this_identifier_node,
4398 build_pointer_type (class_type), end_params_node);
4399 tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4401 instinit_identifier_node, parms);
4403 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4406 /* Gather all the statements in a compound */
4407 for (current = TYPE_II_STMT_LIST (class_type);
4408 current; current = TREE_CHAIN (current))
4409 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4411 /* We need to encapsulate COMPOUND by a try/catch statement to
4412 rethrow exceptions that might occur in the instance initializer.
4413 We do that only if all ctors of CLASS_TYPE are set to catch a
4414 checked exception. This doesn't apply to anonymous classes (since
4415 they don't have declared ctors.) */
4416 if (!ANONYMOUS_CLASS_P (class_type) &&
4417 ctors_unchecked_throws_clause_p (class_type))
4419 compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4420 build1 (THROW_EXPR, NULL_TREE,
4421 build_wfl_node (wpv_id)));
4422 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4423 exception_type_node);
4426 start_artificial_method_body (mdecl);
4427 java_method_add_stmt (mdecl, compound);
4428 end_artificial_method_body (mdecl);
4435 build_instinit_invocation (tree class_type)
4437 tree to_return = NULL_TREE;
4439 if (TYPE_II_STMT_LIST (class_type))
4441 tree parm = build_tree_list (NULL_TREE,
4442 build_wfl_node (this_identifier_node));
4444 build_method_invocation (build_wfl_node (instinit_identifier_node),
4450 /* Shared accros method_declarator and method_header to remember the
4451 patch stage that was reached during the declaration of the method.
4452 A method DECL is built differently is there is no patch
4453 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4454 pending on the currently defined method. */
4456 static int patch_stage;
4458 /* Check the method declaration and add the method to its current
4459 class. If the argument list is known to contain incomplete types,
4460 the method is partially added and the registration will be resume
4461 once the method arguments resolved. If TYPE is NULL, we're dealing
4462 with a constructor. */
4465 method_header (int flags, tree type, tree mdecl, tree throws)
4467 tree type_wfl = NULL_TREE;
4468 tree meth_name = NULL_TREE;
4469 tree current, orig_arg, this_class = NULL;
4472 int constructor_ok = 0, must_chain;
4475 if (mdecl == error_mark_node)
4476 return error_mark_node;
4477 meth = TREE_VALUE (mdecl);
4478 id = TREE_PURPOSE (mdecl);
4480 check_modifiers_consistency (flags);
4483 this_class = TREE_TYPE (GET_CPC ());
4485 if (!this_class || this_class == error_mark_node)
4488 /* There are some forbidden modifiers for an abstract method and its
4489 class must be abstract as well. */
4490 if (type && (flags & ACC_ABSTRACT))
4492 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4493 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4494 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4495 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4496 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4497 ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4498 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4499 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4501 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4502 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4503 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4506 /* Things to be checked when declaring a constructor */
4509 int ec = java_error_count;
4510 /* 8.6: Constructor declarations: we might be trying to define a
4511 method without specifying a return type. */
4512 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4514 (id, "Invalid method declaration, return type required");
4515 /* 8.6.3: Constructor modifiers */
4518 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4519 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4520 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4521 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4522 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4523 JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4525 /* If we found error here, we don't consider it's OK to tread
4526 the method definition as a constructor, for the rest of this
4528 if (ec == java_error_count)
4532 /* Method declared within the scope of an interface are implicitly
4533 abstract and public. Conflicts with other erroneously provided
4534 modifiers are checked right after. */
4536 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4538 /* If FLAGS isn't set because of a modifier, turn the
4539 corresponding modifier WFL to NULL so we issue a warning on
4540 the obsolete use of the modifier */
4541 if (!(flags & ACC_PUBLIC))
4542 MODIFIER_WFL (PUBLIC_TK) = NULL;
4543 if (!(flags & ACC_ABSTRACT))
4544 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4545 flags |= ACC_PUBLIC;
4546 flags |= ACC_ABSTRACT;
4549 /* Inner class can't declare static methods */
4550 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4553 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4554 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4555 lang_printable_name (this_class, 0));
4558 /* Modifiers context reset moved up, so abstract method declaration
4559 modifiers can be later checked. */
4561 /* Set constructor returned type to void and method name to <init>,
4562 unless we found an error identifier the constructor (in which
4563 case we retain the original name) */
4566 type = void_type_node;
4568 meth_name = init_identifier_node;
4571 meth_name = EXPR_WFL_NODE (id);
4573 /* Do the returned type resolution and registration if necessary */
4574 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4577 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4578 EXPR_WFL_NODE (id) = meth_name;
4579 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4583 patch_stage = JDEP_METHOD_RETURN;
4584 register_incomplete_type (patch_stage, type_wfl, id, type);
4585 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4588 TREE_TYPE (meth) = type;
4590 saved_lineno = lineno;
4591 /* When defining an abstract or interface method, the curly
4592 bracket at level 1 doesn't exist because there is no function
4594 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4595 EXPR_WFL_LINENO (id));
4597 /* Remember the original argument list */
4598 orig_arg = TYPE_ARG_TYPES (meth);
4600 if (patch_stage) /* includes ret type and/or all args */
4603 meth = add_method_1 (this_class, flags, meth_name, meth);
4604 /* Patch for the return type */
4605 if (patch_stage == JDEP_METHOD_RETURN)
4607 jdep = CLASSD_LAST (ctxp->classd_list);
4608 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4610 /* This is the stop JDEP. METH allows the function's signature
4612 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4615 meth = add_method (this_class, flags, meth_name,
4616 build_java_signature (meth));
4618 /* Remember final parameters */
4619 MARK_FINAL_PARMS (meth, orig_arg);
4621 /* Fix the method argument list so we have the argument name
4623 fix_method_argument_names (orig_arg, meth);
4625 /* Register the parameter number and re-install the current line
4627 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4628 lineno = saved_lineno;
4630 /* Register exception specified by the `throws' keyword for
4631 resolution and set the method decl appropriate field to the list.
4632 Note: the grammar ensures that what we get here are class
4636 throws = nreverse (throws);
4637 for (current = throws; current; current = TREE_CHAIN (current))
4639 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4640 NULL_TREE, NULL_TREE);
4641 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4642 &TREE_VALUE (current);
4644 DECL_FUNCTION_THROWS (meth) = throws;
4647 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4648 DECL_FUNCTION_WFL (meth) = id;
4650 /* Set the flag if we correctly processed a constructor */
4653 DECL_CONSTRUCTOR_P (meth) = 1;
4654 /* Compute and store the number of artificial parameters declared
4655 for this constructor */
4656 for (count = 0, current = TYPE_FIELDS (this_class); current;
4657 current = TREE_CHAIN (current))
4658 if (FIELD_LOCAL_ALIAS (current))
4660 DECL_FUNCTION_NAP (meth) = count;
4663 /* Eventually set the @deprecated tag flag */
4664 CHECK_DEPRECATED (meth);
4666 /* If doing xref, store column and line number information instead
4667 of the line number only. */
4669 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4675 fix_method_argument_names (tree orig_arg, tree meth)
4677 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4678 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4680 TREE_PURPOSE (arg) = this_identifier_node;
4681 arg = TREE_CHAIN (arg);
4683 while (orig_arg != end_params_node)
4685 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4686 orig_arg = TREE_CHAIN (orig_arg);
4687 arg = TREE_CHAIN (arg);
4691 /* Complete the method declaration with METHOD_BODY. */
4694 finish_method_declaration (tree method_body)
4698 if (!current_function_decl)
4701 flags = get_access_flags_from_decl (current_function_decl);
4703 /* 8.4.5 Method Body */
4704 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4706 tree name = DECL_NAME (current_function_decl);
4707 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4708 "%s method `%s' can't have a body defined",
4709 (METHOD_NATIVE (current_function_decl) ?
4710 "Native" : "Abstract"),
4711 IDENTIFIER_POINTER (name));
4712 method_body = NULL_TREE;
4714 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4716 tree name = DECL_NAME (current_function_decl);
4718 (DECL_FUNCTION_WFL (current_function_decl),
4719 "Non native and non abstract method `%s' must have a body defined",
4720 IDENTIFIER_POINTER (name));
4721 method_body = NULL_TREE;
4724 if (flag_emit_class_files && method_body
4725 && TREE_CODE (method_body) == NOP_EXPR
4726 && TREE_TYPE (current_function_decl)
4727 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4728 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4730 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4731 maybe_absorb_scoping_blocks ();
4732 /* Exit function's body */
4734 /* Merge last line of the function with first line, directly in the
4735 function decl. It will be used to emit correct debug info. */
4736 if (!flag_emit_xref)
4737 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4739 /* Since function's argument's list are shared, reset the
4740 ARG_FINAL_P parameter that might have been set on some of this
4741 function parameters. */
4742 UNMARK_FINAL_PARMS (current_function_decl);
4744 /* So we don't have an irrelevant function declaration context for
4745 the next static block we'll see. */
4746 current_function_decl = NULL_TREE;
4749 /* Build a an error message for constructor circularity errors. */
4752 constructor_circularity_msg (tree from, tree to)
4754 static char string [4096];
4755 char *t = xstrdup (lang_printable_name (from, 0));
4756 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4761 /* Verify a circular call to METH. Return 1 if an error is found, 0
4764 static GTY(()) tree vcc_list;
4766 verify_constructor_circularity (tree meth, tree current)
4770 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4772 if (TREE_VALUE (c) == meth)
4778 vcc_list = nreverse (vcc_list);
4779 for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
4782 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4783 constructor_circularity_msg
4784 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4788 t = xstrdup (lang_printable_name (meth, 0));
4789 parse_error_context (TREE_PURPOSE (c),
4790 "%s: recursive invocation of constructor `%s'",
4791 constructor_circularity_msg (current, meth), t);
4793 vcc_list = NULL_TREE;
4797 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4799 vcc_list = tree_cons (c, current, vcc_list);
4800 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4802 vcc_list = TREE_CHAIN (vcc_list);
4807 /* Check modifiers that can be declared but exclusively */
4810 check_modifiers_consistency (int flags)
4813 tree cl = NULL_TREE;
4815 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4816 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4817 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4820 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4824 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4825 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4827 parse_error_context (cl,
4828 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4831 /* Check the methode header METH for abstract specifics features */
4834 check_abstract_method_header (tree meth)
4836 int flags = get_access_flags_from_decl (meth);
4838 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4839 ACC_ABSTRACT, "abstract method",
4840 IDENTIFIER_POINTER (DECL_NAME (meth)));
4841 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4842 ACC_PUBLIC, "abstract method",
4843 IDENTIFIER_POINTER (DECL_NAME (meth)));
4845 check_modifiers ("Illegal modifier `%s' for interface method",
4846 flags, INTERFACE_METHOD_MODIFIERS);
4849 /* Create a FUNCTION_TYPE node and start augmenting it with the
4850 declared function arguments. Arguments type that can't be resolved
4851 are left as they are, but the returned node is marked as containing
4852 incomplete types. */
4855 method_declarator (tree id, tree list)
4857 tree arg_types = NULL_TREE, current, node;
4858 tree meth = make_node (FUNCTION_TYPE);
4861 patch_stage = JDEP_NO_PATCH;
4863 if (GET_CPC () == error_mark_node)
4864 return error_mark_node;
4866 /* If we're dealing with an inner class constructor, we hide the
4867 this$<n> decl in the name field of its parameter declaration. We
4868 also might have to hide the outer context local alias
4869 initializers. Not done when the class is a toplevel class. */
4870 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4871 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4873 tree aliases_list, type, thisn;
4874 /* First the aliases, linked to the regular parameters */
4876 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4877 TREE_TYPE (GET_CPC ()),
4879 list = chainon (nreverse (aliases_list), list);
4882 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4883 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4884 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4888 for (current = list; current; current = TREE_CHAIN (current))
4891 tree wfl_name = TREE_PURPOSE (current);
4892 tree type = TREE_VALUE (current);
4893 tree name = EXPR_WFL_NODE (wfl_name);
4894 tree already, arg_node;
4895 tree type_wfl = NULL_TREE;
4898 /* Obtain a suitable type for resolution, if necessary */
4899 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4901 /* Process NAME, as it may specify extra dimension(s) for it */
4902 type = build_array_from_name (type, type_wfl, name, &name);
4903 EXPR_WFL_NODE (wfl_name) = name;
4905 real_type = GET_REAL_TYPE (type);
4906 if (TREE_CODE (real_type) == RECORD_TYPE)
4908 real_type = promote_type (real_type);
4909 if (TREE_CODE (type) == TREE_LIST)
4910 TREE_PURPOSE (type) = real_type;
4913 /* Check redefinition */
4914 for (already = arg_types; already; already = TREE_CHAIN (already))
4915 if (TREE_PURPOSE (already) == name)
4918 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4919 IDENTIFIER_POINTER (name),
4920 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4924 /* If we've an incomplete argument type, we know there is a location
4925 to patch when the type get resolved, later. */
4929 patch_stage = JDEP_METHOD;
4930 type = register_incomplete_type (patch_stage,
4931 type_wfl, wfl_name, type);
4932 jdep = CLASSD_LAST (ctxp->classd_list);
4933 JDEP_MISC (jdep) = id;
4936 /* The argument node: a name and a (possibly) incomplete type. */
4937 arg_node = build_tree_list (name, real_type);
4938 /* Remeber arguments declared final. */
4939 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4942 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4943 TREE_CHAIN (arg_node) = arg_types;
4944 arg_types = arg_node;
4946 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4947 node = build_tree_list (id, meth);
4952 unresolved_type_p (tree wfl, tree *returned)
4955 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4959 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4960 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4961 *returned = TREE_TYPE (decl);
4962 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4963 *returned = TREE_TYPE (GET_CPC ());
4965 *returned = NULL_TREE;
4974 /* From NAME, build a qualified identifier node using the
4975 qualification from the current package definition. */
4978 parser_qualified_classname (tree name)
4980 tree nested_class_name;
4982 if ((nested_class_name = maybe_make_nested_class_name (name)))
4983 return nested_class_name;
4986 return merge_qualified_name (ctxp->package, name);
4991 /* Called once the type a interface extends is resolved. Returns 0 if
4992 everything is OK. */
4995 parser_check_super_interface (tree super_decl, tree this_decl, tree this_wfl)
4997 tree super_type = TREE_TYPE (super_decl);
4999 /* Has to be an interface */
5000 if (!CLASS_INTERFACE (super_decl))
5003 (this_wfl, "%s `%s' can't implement/extend %s `%s'",
5004 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5005 "Interface" : "Class"),
5006 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5007 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5008 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5012 /* Check top-level interface access. Inner classes are subject to member
5013 access rules (6.6.1). */
5014 if (! INNER_CLASS_P (super_type)
5015 && check_pkg_class_access (DECL_NAME (super_decl),
5016 lookup_cl (this_decl), true))
5019 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5020 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5021 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5025 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5026 0 if everthing is OK. */
5029 parser_check_super (tree super_decl, tree this_decl, tree wfl)
5031 tree super_type = TREE_TYPE (super_decl);
5033 /* SUPER should be a CLASS (neither an array nor an interface) */
5034 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5037 (wfl, "Class `%s' can't subclass %s `%s'",
5038 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5039 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5040 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5044 if (CLASS_FINAL (TYPE_NAME (super_type)))
5046 parse_error_context (wfl, "Can't subclass final classes: %s",
5047 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5051 /* Check top-level class scope. Inner classes are subject to member access
5053 if (! INNER_CLASS_P (super_type)
5054 && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true)))
5057 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5058 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5059 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5063 /* Create a new dependency list and link it (in a LIFO manner) to the
5064 CTXP list of type dependency list. */
5067 create_jdep_list (struct parser_ctxt *ctxp)
5069 jdeplist *new = xmalloc (sizeof (jdeplist));
5070 new->first = new->last = NULL;
5071 new->next = ctxp->classd_list;
5072 ctxp->classd_list = new;
5076 reverse_jdep_list (struct parser_ctxt *ctxp)
5078 register jdeplist *prev = NULL, *current, *next;
5079 for (current = ctxp->classd_list; current; current = next)
5081 next = current->next;
5082 current->next = prev;
5088 /* Create a fake pointer based on the ID stored in
5089 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5090 registered again. */
5093 obtain_incomplete_type (tree type_name)
5095 tree ptr = NULL_TREE, name;
5097 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5098 name = EXPR_WFL_NODE (type_name);
5099 else if (INCOMPLETE_TYPE_P (type_name))
5100 name = TYPE_NAME (type_name);
5104 /* Workaround from build_pointer_type for incomplete types. */
5105 BUILD_PTR_FROM_NAME (ptr, name);
5106 TYPE_MODE (ptr) = ptr_mode;
5112 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5113 non NULL instead of computing a new fake type based on WFL. The new
5114 dependency is inserted in the current type dependency list, in FIFO
5118 register_incomplete_type (int kind, tree wfl, tree decl, tree ptr)
5120 jdep *new = xmalloc (sizeof (jdep));
5122 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5123 ptr = obtain_incomplete_type (wfl);
5125 JDEP_KIND (new) = kind;
5126 JDEP_DECL (new) = decl;
5127 JDEP_TO_RESOLVE (new) = ptr;
5128 JDEP_WFL (new) = wfl;
5129 JDEP_CHAIN (new) = NULL;
5130 JDEP_MISC (new) = NULL_TREE;
5131 /* For some dependencies, set the enclosing class of the current
5132 class to be the enclosing context */
5133 if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
5134 && GET_ENCLOSING_CPC ())
5135 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5136 else if (kind == JDEP_SUPER)
5137 JDEP_ENCLOSING (new) = (GET_ENCLOSING_CPC () ?
5138 TREE_VALUE (GET_ENCLOSING_CPC ()) : NULL_TREE);
5140 JDEP_ENCLOSING (new) = GET_CPC ();
5141 JDEP_GET_PATCH (new) = (tree *)NULL;
5143 JDEP_INSERT (ctxp->classd_list, new);
5148 /* This checks for circular references with innerclasses. We start
5149 from SOURCE and should never reach TARGET. Extended/implemented
5150 types in SOURCE have their enclosing context checked not to reach
5151 TARGET. When the last enclosing context of SOURCE is reached, its
5152 extended/implemented types are also checked not to reach TARGET.
5153 In case of error, WFL of the offending type is returned; NULL_TREE
5157 check_inner_circular_reference (tree source, tree target)
5159 tree basetype_vec = TYPE_BINFO_BASETYPES (source);
5166 for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5170 /* We can end up with a NULL_TREE or an incomplete type here if
5171 we encountered previous type resolution errors. It's safe to
5172 simply ignore these cases. */
5173 if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
5175 su = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
5176 if (INCOMPLETE_TYPE_P (su))
5179 if (inherits_from_p (su, target))
5180 return lookup_cl (TYPE_NAME (su));
5182 for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5184 /* An enclosing context shouldn't be TARGET */
5185 if (ctx == TYPE_NAME (target))
5186 return lookup_cl (TYPE_NAME (su));
5188 /* When we reach the enclosing last context, start a check
5189 on it, with the same target */
5190 if (! DECL_CONTEXT (ctx) &&
5191 (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5198 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5199 offending type if a circularity is detected. NULL_TREE is returned
5200 otherwise. TYPE can be an interface or a class. */
5203 check_circular_reference (tree type)
5205 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5211 if (! CLASS_INTERFACE (TYPE_NAME (type)))
5213 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5214 return lookup_cl (TYPE_NAME (type));
5218 for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5220 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5221 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5222 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5223 return lookup_cl (TYPE_NAME (BINFO_TYPE (vec_elt)));
5229 java_check_circular_reference (void)
5232 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5234 tree type = TREE_TYPE (current);
5237 cl = check_circular_reference (type);
5239 cl = check_inner_circular_reference (type, type);
5241 parse_error_context (cl, "Cyclic class inheritance%s",
5242 (cyclic_inheritance_report ?
5243 cyclic_inheritance_report : ""));
5247 /* Augment the parameter list PARM with parameters crafted to
5248 initialize outer context locals aliases. Through ARTIFICIAL, a
5249 count is kept of the number of crafted parameters. MODE governs
5250 what eventually gets created: something suitable for a function
5251 creation or a function invocation, either the constructor or
5255 build_alias_initializer_parameter_list (int mode, tree class_type, tree parm,
5259 tree additional_parms = NULL_TREE;
5261 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5262 if (FIELD_LOCAL_ALIAS (field))
5264 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5265 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5270 case AIPL_FUNCTION_DECLARATION:
5271 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5273 purpose = build_wfl_node (mangled_id);
5274 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5275 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5277 value = TREE_TYPE (field);
5280 case AIPL_FUNCTION_CREATION:
5281 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5283 value = TREE_TYPE (field);
5286 case AIPL_FUNCTION_FINIT_INVOCATION:
5287 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5289 /* Now, this is wrong. purpose should always be the NAME
5290 of something and value its matching value (decl, type,
5291 etc...) FIXME -- but there is a lot to fix. */
5293 /* When invoked for this kind of operation, we already
5294 know whether a field is used or not. */
5295 purpose = TREE_TYPE (field);
5296 value = build_wfl_node (mangled_id);
5299 case AIPL_FUNCTION_CTOR_INVOCATION:
5300 /* There are two case: the constructor invokation happends
5301 outside the local inner, in which case, locales from the outer
5302 context are directly used.
5304 Otherwise, we fold to using the alias directly. */
5305 if (class_type == current_class)
5309 name = get_identifier (&buffer[4]);
5310 value = IDENTIFIER_LOCAL_VALUE (name);
5314 additional_parms = tree_cons (purpose, value, additional_parms);
5318 if (additional_parms)
5320 if (ANONYMOUS_CLASS_P (class_type)
5321 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5322 additional_parms = nreverse (additional_parms);
5323 parm = chainon (additional_parms, parm);
5329 /* Craft a constructor for CLASS_DECL -- what we should do when none
5330 where found. ARGS is non NULL when a special signature must be
5331 enforced. This is the case for anonymous classes. */
5334 craft_constructor (tree class_decl, tree args)
5336 tree class_type = TREE_TYPE (class_decl);
5337 tree parm = NULL_TREE;
5338 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5340 int i = 0, artificial = 0;
5341 tree decl, ctor_name;
5344 /* The constructor name is <init> unless we're dealing with an
5345 anonymous class, in which case the name will be fixed after having
5347 if (ANONYMOUS_CLASS_P (class_type))
5348 ctor_name = DECL_NAME (class_decl);
5350 ctor_name = init_identifier_node;
5352 /* If we're dealing with an inner class constructor, we hide the
5353 this$<n> decl in the name field of its parameter declaration. */
5354 if (PURE_INNER_CLASS_TYPE_P (class_type))
5356 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5357 parm = tree_cons (build_current_thisn (class_type),
5358 build_pointer_type (type), parm);
5360 /* Some more arguments to be hidden here. The values of the local
5361 variables of the outer context that the inner class needs to see. */
5362 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5367 /* Then if there are any args to be enforced, enforce them now */
5368 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5370 sprintf (buffer, "parm%d", i++);
5371 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5374 CRAFTED_PARAM_LIST_FIXUP (parm);
5375 decl = create_artificial_method (class_type, flags, void_type_node,
5377 fix_method_argument_names (parm, decl);
5378 /* Now, mark the artificial parameters. */
5379 DECL_FUNCTION_NAP (decl) = artificial;
5380 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5385 /* Fix the constructors. This will be called right after circular
5386 references have been checked. It is necessary to fix constructors
5387 early even if no code generation will take place for that class:
5388 some generated constructor might be required by the class whose
5389 compilation triggered this one to be simply loaded. */
5392 java_fix_constructors (void)
5396 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5398 tree class_type = TREE_TYPE (current);
5402 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5405 current_class = class_type;
5406 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5408 if (DECL_CONSTRUCTOR_P (decl))
5410 fix_constructors (decl);
5415 /* Anonymous class constructor can't be generated that early. */
5416 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5417 craft_constructor (current, NULL_TREE);
5421 /* safe_layout_class just makes sure that we can load a class without
5422 disrupting the current_class, input_file, lineno, etc, information
5423 about the class processed currently. */
5426 safe_layout_class (tree class)
5428 tree save_current_class = current_class;
5429 const char *save_input_filename = input_filename;
5430 int save_lineno = lineno;
5432 layout_class (class);
5434 current_class = save_current_class;
5435 input_filename = save_input_filename;
5436 lineno = save_lineno;
5440 jdep_resolve_class (jdep *dep)
5444 if (JDEP_RESOLVED_P (dep))
5445 decl = JDEP_RESOLVED_DECL (dep);
5448 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5449 JDEP_DECL (dep), JDEP_WFL (dep));
5450 JDEP_RESOLVED (dep, decl);
5454 complete_class_report_errors (dep);
5455 else if (PURE_INNER_CLASS_DECL_P (decl))
5457 tree inner = TREE_TYPE (decl);
5458 if (! CLASS_LOADED_P (inner))
5460 safe_layout_class (inner);
5461 if (TYPE_SIZE (inner) == error_mark_node)
5462 TYPE_SIZE (inner) = NULL_TREE;
5464 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5469 /* Complete unsatisfied class declaration and their dependencies */
5472 java_complete_class (void)
5479 /* Process imports */
5482 /* Reverse things so we have the right order */
5483 ctxp->class_list = nreverse (ctxp->class_list);
5484 ctxp->classd_list = reverse_jdep_list (ctxp);
5486 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5488 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5492 /* We keep the compilation unit imports in the class so that
5493 they can be used later to resolve type dependencies that
5494 aren't necessary to solve now. */
5495 TYPE_IMPORT_LIST (TREE_TYPE (cclass)) = ctxp->import_list;
5496 TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (cclass)) = ctxp->import_demand_list;
5498 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5501 if (!(decl = jdep_resolve_class (dep)))
5504 /* Now it's time to patch */
5505 switch (JDEP_KIND (dep))
5508 /* Simply patch super */
5509 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5511 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5512 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5517 /* We do part of the job done in add_field */
5518 tree field_decl = JDEP_DECL (dep);
5519 tree field_type = TREE_TYPE (decl);
5520 if (TREE_CODE (field_type) == RECORD_TYPE)
5521 field_type = promote_type (field_type);
5522 TREE_TYPE (field_decl) = field_type;
5523 DECL_ALIGN (field_decl) = 0;
5524 DECL_USER_ALIGN (field_decl) = 0;
5525 layout_decl (field_decl, 0);
5526 SOURCE_FRONTEND_DEBUG
5527 (("Completed field/var decl `%s' with `%s'",
5528 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5529 IDENTIFIER_POINTER (DECL_NAME (decl))));
5532 case JDEP_METHOD: /* We start patching a method */
5533 case JDEP_METHOD_RETURN:
5539 type = TREE_TYPE(decl);
5540 if (TREE_CODE (type) == RECORD_TYPE)
5541 type = promote_type (type);
5542 JDEP_APPLY_PATCH (dep, type);
5543 SOURCE_FRONTEND_DEBUG
5544 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5545 "Completing fct `%s' with ret type `%s'":
5546 "Completing arg `%s' with type `%s'"),
5547 IDENTIFIER_POINTER (EXPR_WFL_NODE
5548 (JDEP_DECL_WFL (dep))),
5549 IDENTIFIER_POINTER (DECL_NAME (decl))));
5553 dep = JDEP_CHAIN (dep);
5554 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5557 decl = jdep_resolve_class (dep);
5561 tree mdecl = JDEP_DECL (dep), signature;
5562 /* Recompute and reset the signature, check first that
5563 all types are now defined. If they're not,
5564 don't build the signature. */
5565 if (check_method_types_complete (mdecl))
5567 signature = build_java_signature (TREE_TYPE (mdecl));
5568 set_java_signature (TREE_TYPE (mdecl), signature);
5575 case JDEP_INTERFACE:
5576 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5579 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5584 type = TREE_TYPE(decl);
5585 if (TREE_CODE (type) == RECORD_TYPE)
5586 type = promote_type (type);
5587 JDEP_APPLY_PATCH (dep, type);
5591 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5592 SOURCE_FRONTEND_DEBUG
5593 (("Completing a random type dependency on a '%s' node",
5594 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5597 case JDEP_EXCEPTION:
5598 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5599 SOURCE_FRONTEND_DEBUG
5600 (("Completing `%s' `throws' argument node",
5601 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5604 case JDEP_ANONYMOUS:
5605 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5616 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5620 resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5622 tree tname = TYPE_NAME (class_type);
5623 tree resolved_type = TREE_TYPE (class_type);
5625 tree resolved_type_decl;
5627 if (resolved_type != NULL_TREE)
5629 tree resolved_type_decl = TYPE_NAME (resolved_type);
5630 if (resolved_type_decl == NULL_TREE
5631 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5633 resolved_type_decl = build_decl (TYPE_DECL,
5634 TYPE_NAME (class_type),
5637 return resolved_type_decl;
5640 /* 1- Check to see if we have an array. If true, find what we really
5642 if ((array_dims = build_type_name_from_array_name (tname,
5643 &TYPE_NAME (class_type))))
5644 WFL_STRIP_BRACKET (cl, cl);
5646 /* 2- Resolve the bare type */
5647 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5650 resolved_type = TREE_TYPE (resolved_type_decl);
5652 /* 3- If we have an array, reconstruct the array down to its nesting */
5655 for (; array_dims; array_dims--)
5656 resolved_type = build_java_array_type (resolved_type, -1);
5657 resolved_type_decl = TYPE_NAME (resolved_type);
5659 TREE_TYPE (class_type) = resolved_type;
5660 return resolved_type_decl;
5663 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5664 are used to report error messages. Do not try to replace TYPE_NAME
5665 (class_type) by a variable, since it is changed by
5666 find_in_imports{_on_demand} and (but it doesn't really matter)
5667 qualify_and_find. */
5670 do_resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5672 tree new_class_decl = NULL_TREE, super = NULL_TREE;
5673 tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5675 htab_t circularity_hash;
5677 if (QUALIFIED_P (TYPE_NAME (class_type)))
5679 /* If the type name is of the form `Q . Id', then Q is either a
5680 package name or a class name. First we try to find Q as a
5681 class and then treat Id as a member type. If we can't find Q
5682 as a class then we fall through. */
5683 tree q, left, left_type, right;
5684 breakdown_qualified (&left, &right, TYPE_NAME (class_type));
5685 BUILD_PTR_FROM_NAME (left_type, left);
5686 q = do_resolve_class (enclosing, left_type, decl, cl);
5690 saved_enclosing_type = TREE_TYPE (q);
5691 BUILD_PTR_FROM_NAME (class_type, right);
5697 /* This hash table is used to register the classes we're going
5698 through when searching the current class as an inner class, in
5699 order to detect circular references. Remember to free it before
5700 returning the section 0- of this function. */
5701 circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
5704 /* 0- Search in the current class as an inner class.
5705 Maybe some code here should be added to load the class or
5706 something, at least if the class isn't an inner class and ended
5707 being loaded from class file. FIXME. */
5710 new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5711 &super, class_type);
5715 /* If we haven't found anything because SUPER reached Object and
5716 ENCLOSING happens to be an innerclass, try the enclosing context. */
5717 if ((!super || super == object_type_node) &&
5718 enclosing && INNER_CLASS_DECL_P (enclosing))
5719 enclosing = DECL_CONTEXT (enclosing);
5721 enclosing = NULL_TREE;
5724 htab_delete (circularity_hash);
5727 return new_class_decl;
5730 /* 1- Check for the type in single imports. This will change
5731 TYPE_NAME() if something relevant is found */
5732 find_in_imports (saved_enclosing_type, class_type);
5734 /* 2- And check for the type in the current compilation unit */
5735 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5737 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5738 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5739 load_class (TYPE_NAME (class_type), 0);
5740 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5743 /* 3- Search according to the current package definition */
5744 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5746 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5747 TYPE_NAME (class_type))))
5748 return new_class_decl;
5751 /* 4- Check the import on demands. Don't allow bar.baz to be
5752 imported from foo.* */
5753 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5754 if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5757 /* If found in find_in_imports_on_demand, the type has already been
5759 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5760 return new_class_decl;
5762 /* 5- Try with a name qualified with the package name we've seen so far */
5763 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5767 /* If there is a current package (ctxp->package), it's the first
5768 element of package_list and we can skip it. */
5769 for (package = (ctxp->package ?
5770 TREE_CHAIN (package_list) : package_list);
5771 package; package = TREE_CHAIN (package))
5772 if ((new_class_decl = qualify_and_find (class_type,
5773 TREE_PURPOSE (package),
5774 TYPE_NAME (class_type))))
5775 return new_class_decl;
5778 /* 5- Check another compilation unit that bears the name of type */
5779 load_class (TYPE_NAME (class_type), 0);
5782 cl = lookup_cl (decl);
5784 /* If we don't have a value for CL, then we're being called recursively.
5785 We can't check package access just yet, but it will be taken care of
5789 if (check_pkg_class_access (TYPE_NAME (class_type), cl, true))
5793 /* 6- Last call for a resolution */
5794 decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5796 /* The final lookup might have registered a.b.c into a.b$c If we
5797 failed at the first lookup, progressively change the name if
5798 applicable and use the matching DECL instead. */
5799 if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
5802 tree name = TYPE_NAME (class_type);
5803 char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
5805 strcpy (namebuffer, IDENTIFIER_POINTER (name));
5809 /* Reach the last '.', and if applicable, replace it by a `$' and
5810 see if this exists as a type. */
5811 if ((separator = strrchr (namebuffer, '.')))
5814 name = get_identifier (namebuffer);
5815 decl_result = IDENTIFIER_CLASS_VALUE (name);
5817 } while (!decl_result && separator);
5823 qualify_and_find (tree class_type, tree package, tree name)
5825 tree new_qualified = merge_qualified_name (package, name);
5826 tree new_class_decl;
5828 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5829 load_class (new_qualified, 0);
5830 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5832 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5833 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5834 load_class (new_qualified, 0);
5835 TYPE_NAME (class_type) = new_qualified;
5836 return IDENTIFIER_CLASS_VALUE (new_qualified);
5841 /* Resolve NAME and lay it out (if not done and if not the current
5842 parsed class). Return a decl node. This function is meant to be
5843 called when type resolution is necessary during the walk pass. */
5846 resolve_and_layout (tree something, tree cl)
5848 tree decl, decl_type;
5850 /* Don't do that on the current class */
5851 if (something == current_class)
5852 return TYPE_NAME (current_class);
5854 /* Don't do anything for void and other primitive types */
5855 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5858 /* Pointer types can be reall pointer types or fake pointers. When
5859 finding a real pointer, recheck for primitive types */
5860 if (TREE_CODE (something) == POINTER_TYPE)
5862 if (TREE_TYPE (something))
5864 something = TREE_TYPE (something);
5865 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5869 something = TYPE_NAME (something);
5872 /* Don't do anything for arrays of primitive types */
5873 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5874 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5877 /* Something might be a WFL */
5878 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5879 something = EXPR_WFL_NODE (something);
5881 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5882 TYPE_DECL or a real TYPE */
5883 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5884 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5885 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5887 if (!(decl = resolve_no_layout (something, cl)))
5890 /* Resolve and layout if necessary */
5891 decl_type = TREE_TYPE (decl);
5892 layout_class_methods (decl_type);
5894 if (CLASS_FROM_SOURCE_P (decl_type))
5895 java_check_methods (decl);
5896 /* Layout the type if necessary */
5897 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5898 safe_layout_class (decl_type);
5903 /* Resolve a class, returns its decl but doesn't perform any
5904 layout. The current parsing context is saved and restored */
5907 resolve_no_layout (tree name, tree cl)
5910 BUILD_PTR_FROM_NAME (ptr, name);
5911 java_parser_context_save_global ();
5912 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5913 java_parser_context_restore_global ();
5918 /* Called when reporting errors. Skip the '[]'s in a complex array
5919 type description that failed to be resolved. purify_type_name can't
5920 use an identifier tree. */
5923 purify_type_name (const char *name)
5925 int len = strlen (name);
5928 STRING_STRIP_BRACKETS (name, len, bracket_found);
5931 char *stripped_name = xmemdup (name, len, len+1);
5932 stripped_name [len] = '\0';
5933 return stripped_name;
5938 /* The type CURRENT refers to can't be found. We print error messages. */
5941 complete_class_report_errors (jdep *dep)
5945 if (!JDEP_WFL (dep))
5948 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5949 switch (JDEP_KIND (dep))
5953 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5954 purify_type_name (name),
5955 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5959 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5960 purify_type_name (name),
5961 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5963 case JDEP_METHOD: /* Covers arguments */
5965 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5966 purify_type_name (name),
5967 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5968 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5970 case JDEP_METHOD_RETURN: /* Covers return type */
5972 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5973 purify_type_name (name),
5974 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5976 case JDEP_INTERFACE:
5978 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5979 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5980 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5981 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5985 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5986 purify_type_name (IDENTIFIER_POINTER
5987 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5988 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5990 case JDEP_EXCEPTION: /* As specified by `throws' */
5992 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5993 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5996 /* Fix for -Wall. Just break doing nothing. The error will be
6002 /* Return a static string containing the DECL prototype string. If
6003 DECL is a constructor, use the class name instead of the form
6007 get_printable_method_name (tree decl)
6009 const char *to_return;
6010 tree name = NULL_TREE;
6012 if (DECL_CONSTRUCTOR_P (decl))
6014 name = DECL_NAME (decl);
6015 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6018 to_return = lang_printable_name (decl, 0);
6019 if (DECL_CONSTRUCTOR_P (decl))
6020 DECL_NAME (decl) = name;
6025 /* Track method being redefined inside the same class. As a side
6026 effect, set DECL_NAME to an IDENTIFIER (prior entering this
6027 function it's a FWL, so we can track errors more accurately.) */
6030 check_method_redefinition (tree class, tree method)
6034 /* There's no need to verify <clinit> and finit$ and instinit$ */
6035 if (DECL_CLINIT_P (method)
6036 || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6039 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6040 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6042 if (redef == method)
6044 if (DECL_NAME (redef) == DECL_NAME (method)
6045 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6046 && !DECL_ARTIFICIAL (method))
6049 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
6050 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6051 get_printable_method_name (redef));
6058 /* Return 1 if check went ok, 0 otherwise. */
6060 check_abstract_method_definitions (int do_interface, tree class_decl,
6063 tree class = TREE_TYPE (class_decl);
6064 tree method, end_type;
6067 end_type = (do_interface ? object_type_node : type);
6068 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6070 tree other_super, other_method, method_sig, method_name;
6072 int end_type_reached = 0;
6074 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6077 /* Now verify that somewhere in between TYPE and CLASS,
6078 abstract method METHOD gets a non abstract definition
6079 that is inherited by CLASS. */
6081 method_sig = build_java_signature (TREE_TYPE (method));
6082 method_name = DECL_NAME (method);
6083 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6084 method_name = EXPR_WFL_NODE (method_name);
6086 other_super = class;
6088 if (other_super == end_type)
6089 end_type_reached = 1;
6092 for (other_method = TYPE_METHODS (other_super); other_method;
6093 other_method = TREE_CHAIN (other_method))
6095 tree s = build_java_signature (TREE_TYPE (other_method));
6096 tree other_name = DECL_NAME (other_method);
6098 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6099 other_name = EXPR_WFL_NODE (other_name);
6100 if (!DECL_CLINIT_P (other_method)
6101 && !DECL_CONSTRUCTOR_P (other_method)
6102 && method_name == other_name
6104 && !METHOD_ABSTRACT (other_method))
6110 other_super = CLASSTYPE_SUPER (other_super);
6111 } while (!end_type_reached);
6113 /* Report that abstract METHOD didn't find an implementation
6114 that CLASS can use. */
6117 char *t = xstrdup (lang_printable_name
6118 (TREE_TYPE (TREE_TYPE (method)), 0));
6119 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6122 (lookup_cl (class_decl),
6123 "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",
6124 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6125 t, lang_printable_name (method, 0),
6126 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6127 "interface" : "class"),
6128 IDENTIFIER_POINTER (ccn),
6129 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6130 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6136 if (ok && do_interface)
6138 /* Check for implemented interfaces. */
6140 tree vector = TYPE_BINFO_BASETYPES (type);
6141 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
6143 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6144 ok = check_abstract_method_definitions (1, class_decl, super);
6151 /* Check that CLASS_DECL somehow implements all inherited abstract
6155 java_check_abstract_method_definitions (tree class_decl)
6157 tree class = TREE_TYPE (class_decl);
6161 if (CLASS_ABSTRACT (class_decl))
6164 /* Check for inherited types */
6167 super = CLASSTYPE_SUPER (super);
6168 check_abstract_method_definitions (0, class_decl, super);
6169 } while (super != object_type_node);
6171 /* Check for implemented interfaces. */
6172 vector = TYPE_BINFO_BASETYPES (class);
6173 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
6175 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6176 check_abstract_method_definitions (1, class_decl, super);
6180 /* Check all the types method DECL uses and return 1 if all of them
6181 are now complete, 0 otherwise. This is used to check whether its
6182 safe to build a method signature or not. */
6185 check_method_types_complete (tree decl)
6187 tree type = TREE_TYPE (decl);
6190 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6193 args = TYPE_ARG_TYPES (type);
6194 if (TREE_CODE (type) == METHOD_TYPE)
6195 args = TREE_CHAIN (args);
6196 for (; args != end_params_node; args = TREE_CHAIN (args))
6197 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6203 /* Visible interface to check methods contained in CLASS_DECL */
6206 java_check_methods (tree class_decl)
6208 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6211 if (CLASS_INTERFACE (class_decl))
6212 java_check_abstract_methods (class_decl);
6214 java_check_regular_methods (class_decl);
6216 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6219 /* Check all the methods of CLASS_DECL. Methods are first completed
6220 then checked according to regular method existence rules. If no
6221 constructor for CLASS_DECL were encountered, then build its
6225 java_check_regular_methods (tree class_decl)
6227 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6229 tree class = TREE_TYPE (class_decl);
6230 tree found = NULL_TREE;
6233 /* It is not necessary to check methods defined in java.lang.Object */
6234 if (class == object_type_node)
6237 if (!TYPE_NVIRTUALS (class))
6238 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6240 /* Should take interfaces into account. FIXME */
6241 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6244 tree method_wfl = DECL_FUNCTION_WFL (method);
6247 /* Check for redefinitions */
6248 if (check_method_redefinition (class, method))
6251 /* If we see one constructor a mark so we don't generate the
6252 default one. Also skip other verifications: constructors
6253 can't be inherited hence hiden or overriden */
6254 if (DECL_CONSTRUCTOR_P (method))
6256 saw_constructor = 1;
6260 /* We verify things thrown by the method. They must inherits from
6261 java.lang.Throwable */
6262 for (mthrows = DECL_FUNCTION_THROWS (method);
6263 mthrows; mthrows = TREE_CHAIN (mthrows))
6265 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6267 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6269 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6272 sig = build_java_argument_signature (TREE_TYPE (method));
6273 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6275 /* Inner class can't declare static methods */
6276 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6278 char *t = xstrdup (lang_printable_name (class, 0));
6280 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6281 lang_printable_name (method, 0), t);
6285 /* Nothing overrides or it's a private method. */
6288 if (METHOD_PRIVATE (found))
6294 /* If `found' is declared in an interface, make sure the
6295 modifier matches. */
6296 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6297 && clinit_identifier_node != DECL_NAME (found)
6298 && !METHOD_PUBLIC (method))
6300 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6301 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6302 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6303 lang_printable_name (method, 0),
6304 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6307 /* Can't override a method with the same name and different return
6309 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6312 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6315 "Method `%s' was defined with return type `%s' in class `%s'",
6316 lang_printable_name (found, 0), t,
6318 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6322 aflags = get_access_flags_from_decl (found);
6324 /* Can't override final. Can't override static. */
6325 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6327 /* Static *can* override static */
6328 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6332 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6333 (METHOD_FINAL (found) ? "Final" : "Static"),
6334 lang_printable_name (found, 0),
6335 (METHOD_FINAL (found) ? "final" : "static"),
6337 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6341 /* Static method can't override instance method. */
6342 if (METHOD_STATIC (method))
6346 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6347 lang_printable_name (found, 0),
6349 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6353 /* - Overriding/hiding public must be public
6354 - Overriding/hiding protected must be protected or public
6355 - If the overriden or hidden method has default (package)
6356 access, then the overriding or hiding method must not be
6357 private; otherwise, a compile-time error occurs. If
6358 `found' belongs to an interface, things have been already
6360 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6361 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6362 || (METHOD_PROTECTED (found)
6363 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6364 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6365 && METHOD_PRIVATE (method))))
6369 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6370 (METHOD_PUBLIC (method) ? "public" :
6371 (METHOD_PRIVATE (method) ? "private" : "protected")),
6372 IDENTIFIER_POINTER (DECL_NAME
6373 (TYPE_NAME (DECL_CONTEXT (found)))));
6377 /* Overriding methods must have compatible `throws' clauses on checked
6378 exceptions, if any */
6379 check_throws_clauses (method, method_wfl, found);
6381 /* Inheriting multiple methods with the same signature. FIXME */
6384 if (!TYPE_NVIRTUALS (class))
6385 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6387 /* Search for inherited abstract method not yet implemented in this
6389 java_check_abstract_method_definitions (class_decl);
6391 if (!saw_constructor)
6395 /* Return a nonzero value if the `throws' clause of METHOD (if any)
6396 is incompatible with the `throws' clause of FOUND (if any). */
6399 check_throws_clauses (tree method, tree method_wfl, tree found)
6401 tree mthrows, fthrows;
6403 /* Can't check these things with class loaded from bytecode. FIXME */
6404 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6407 for (mthrows = DECL_FUNCTION_THROWS (method);
6408 mthrows; mthrows = TREE_CHAIN (mthrows))
6410 /* We don't verify unchecked expressions */
6411 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6413 /* Checked expression must be compatible */
6414 for (fthrows = DECL_FUNCTION_THROWS (found);
6415 fthrows; fthrows = TREE_CHAIN (fthrows))
6416 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6421 (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'",
6422 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6423 lang_printable_name (found, 0),
6425 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6430 /* Check abstract method of interface INTERFACE */
6433 java_check_abstract_methods (tree interface_decl)
6436 tree method, basetype_vec, found;
6437 tree interface = TREE_TYPE (interface_decl);
6439 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6441 /* 2- Check for double definition inside the defining interface */
6442 if (check_method_redefinition (interface, method))
6445 /* 3- Overriding is OK as far as we preserve the return type and
6446 the thrown exceptions (FIXME) */
6447 found = lookup_java_interface_method2 (interface, method);
6451 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6453 (DECL_FUNCTION_WFL (found),
6454 "Method `%s' was defined with return type `%s' in class `%s'",
6455 lang_printable_name (found, 0), t,
6457 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6463 /* 4- Inherited methods can't differ by their returned types */
6464 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6466 n = TREE_VEC_LENGTH (basetype_vec);
6467 for (i = 0; i < n; i++)
6469 tree sub_interface_method, sub_interface;
6470 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6473 sub_interface = BINFO_TYPE (vec_elt);
6474 for (sub_interface_method = TYPE_METHODS (sub_interface);
6475 sub_interface_method;
6476 sub_interface_method = TREE_CHAIN (sub_interface_method))
6478 found = lookup_java_interface_method2 (interface,
6479 sub_interface_method);
6480 if (found && (found != sub_interface_method))
6483 (lookup_cl (sub_interface_method),
6484 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6485 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6486 lang_printable_name (found, 0),
6488 (DECL_NAME (TYPE_NAME
6489 (DECL_CONTEXT (sub_interface_method)))),
6491 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6497 /* Lookup methods in interfaces using their name and partial
6498 signature. Return a matching method only if their types differ. */
6501 lookup_java_interface_method2 (tree class, tree method_decl)
6504 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6509 n = TREE_VEC_LENGTH (basetype_vec);
6510 for (i = 0; i < n; i++)
6512 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6513 if ((BINFO_TYPE (vec_elt) != object_type_node)
6515 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6518 for (i = 0; i < n; i++)
6520 to_return = lookup_java_interface_method2
6521 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6529 /* Lookup method using their name and partial signature. Return a
6530 matching method only if their types differ. */
6533 lookup_java_method2 (tree clas, tree method_decl, int do_interface)
6535 tree method, method_signature, method_name, method_type, name;
6537 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6538 name = DECL_NAME (method_decl);
6539 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6540 EXPR_WFL_NODE (name) : name);
6541 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6543 while (clas != NULL_TREE)
6545 for (method = TYPE_METHODS (clas);
6546 method != NULL_TREE; method = TREE_CHAIN (method))
6548 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6549 tree name = DECL_NAME (method);
6550 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6551 EXPR_WFL_NODE (name) : name) == method_name
6552 && method_sig == method_signature
6553 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6556 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6561 /* Return the line that matches DECL line number, and try its best to
6562 position the column number. Used during error reports. */
6564 static GTY(()) tree cl_v;
6566 lookup_cl (tree decl)
6573 if (cl_v == NULL_TREE)
6575 cl_v = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6578 EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6579 EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE_FIRST (decl), -1);
6581 line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6582 EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6584 found = strstr ((const char *)line,
6585 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6587 EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6592 /* Look for a simple name in the single-type import list */
6595 find_name_in_single_imports (tree name)
6599 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6600 if (TREE_VALUE (node) == name)
6601 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6606 /* Process all single-type import. */
6609 process_imports (void)
6614 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6616 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6617 char *original_name;
6619 original_name = xmemdup (IDENTIFIER_POINTER (to_be_found),
6620 IDENTIFIER_LENGTH (to_be_found),
6621 IDENTIFIER_LENGTH (to_be_found) + 1);
6623 /* Don't load twice something already defined. */
6624 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6631 QUALIFIED_P (to_be_found) = 1;
6632 load_class (to_be_found, 0);
6634 check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true);
6636 /* We found it, we can bail out */
6637 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6640 /* We haven't found it. Maybe we're trying to access an
6641 inner class. The only way for us to know is to try again
6642 after having dropped a qualifier. If we can't break it further,
6643 we have an error. */
6644 if (breakdown_qualified (&left, NULL, to_be_found))
6649 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6651 parse_error_context (TREE_PURPOSE (import),
6652 "Class or interface `%s' not found in import",
6657 free (original_name);
6664 /* Possibly find and mark a class imported by a single-type import
6668 find_in_imports (tree enclosing_type, tree class_type)
6670 tree import = (enclosing_type ? TYPE_IMPORT_LIST (enclosing_type) :
6674 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6676 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6677 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6680 import = TREE_CHAIN (import);
6685 note_possible_classname (const char *name, int len)
6688 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6690 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6694 node = ident_subst (name, len, "", '/', '.', "");
6695 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6696 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6700 /* Read a import directory, gathering potential match for further type
6701 references. Indifferently reads a filesystem or a ZIP archive
6705 read_import_dir (tree wfl)
6707 tree package_id = EXPR_WFL_NODE (wfl);
6708 const char *package_name = IDENTIFIER_POINTER (package_id);
6709 int package_length = IDENTIFIER_LENGTH (package_id);
6711 JCF *saved_jcf = current_jcf;
6716 struct buffer filename[1];
6718 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6720 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6722 BUFFER_INIT (filename);
6723 buffer_grow (filename, package_length + 100);
6725 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6727 const char *entry_name = jcf_path_name (entry);
6728 int entry_length = strlen (entry_name);
6729 if (jcf_path_is_zipfile (entry))
6732 buffer_grow (filename, entry_length);
6733 memcpy (filename->data, entry_name, entry_length - 1);
6734 filename->data[entry_length-1] = '\0';
6735 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6737 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6740 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6741 BUFFER_RESET (filename);
6742 for (k = 0; k < package_length; k++)
6744 char ch = package_name[k];
6745 *filename->ptr++ = ch == '.' ? '/' : ch;
6747 *filename->ptr++ = '/';
6749 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6751 const char *current_entry = ZIPDIR_FILENAME (zipd);
6752 int current_entry_len = zipd->filename_length;
6754 if (current_entry_len >= BUFFER_LENGTH (filename)
6755 && strncmp (filename->data, current_entry,
6756 BUFFER_LENGTH (filename)) != 0)
6758 found |= note_possible_classname (current_entry,
6765 BUFFER_RESET (filename);
6766 buffer_grow (filename, entry_length + package_length + 4);
6767 strcpy (filename->data, entry_name);
6768 filename->ptr = filename->data + entry_length;
6769 for (k = 0; k < package_length; k++)
6771 char ch = package_name[k];
6772 *filename->ptr++ = ch == '.' ? '/' : ch;
6774 *filename->ptr = '\0';
6776 dirp = opendir (filename->data);
6779 *filename->ptr++ = '/';
6784 struct dirent *direntp = readdir (dirp);
6787 d_name = direntp->d_name;
6788 len = strlen (direntp->d_name);
6789 buffer_grow (filename, len+1);
6790 strcpy (filename->ptr, d_name);
6791 found |= note_possible_classname (filename->data + entry_length,
6792 package_length+len+1);
6799 free (filename->data);
6801 /* Here we should have a unified way of retrieving an entry, to be
6805 static int first = 1;
6808 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives", package_name);
6813 parse_error_context (wfl, "Package `%s' not found in import",
6815 current_jcf = saved_jcf;
6818 current_jcf = saved_jcf;
6821 /* Possibly find a type in the import on demands specified
6822 types. Returns 1 if an error occurred, 0 otherwise. Run through the
6823 entire list, to detected potential double definitions. */
6826 find_in_imports_on_demand (tree enclosing_type, tree class_type)
6828 tree class_type_name = TYPE_NAME (class_type);
6829 tree import = (enclosing_type ? TYPE_IMPORT_DEMAND_LIST (enclosing_type) :
6830 ctxp->import_demand_list);
6831 tree cl = NULL_TREE;
6832 int seen_once = -1; /* -1 when not set, 1 if seen once, >1 otherwise. */
6833 int to_return = -1; /* -1 when not set, 0 or 1 otherwise */
6836 for (; import; import = TREE_CHAIN (import))
6838 int saved_lineno = lineno;
6840 const char *id_name;
6841 tree decl, type_name_copy;
6843 obstack_grow (&temporary_obstack,
6844 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6845 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6846 obstack_1grow (&temporary_obstack, '.');
6847 obstack_grow0 (&temporary_obstack,
6848 IDENTIFIER_POINTER (class_type_name),
6849 IDENTIFIER_LENGTH (class_type_name));
6850 id_name = obstack_finish (&temporary_obstack);
6852 if (! (node = maybe_get_identifier (id_name)))
6855 /* Setup lineno so that it refers to the line of the import (in
6856 case we parse a class file and encounter errors */
6857 lineno = EXPR_WFL_LINENO (TREE_PURPOSE (import));
6859 type_name_copy = TYPE_NAME (class_type);
6860 TYPE_NAME (class_type) = node;
6861 QUALIFIED_P (node) = 1;
6862 decl = IDENTIFIER_CLASS_VALUE (node);
6864 /* If there is no DECL set for the class or if the class isn't
6865 loaded and not seen in source yet, then load */
6866 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6867 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6869 load_class (node, 0);
6870 decl = IDENTIFIER_CLASS_VALUE (node);
6872 if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
6873 access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
6876 /* 6.6.1: Inner classes are subject to member access rules. */
6879 lineno = saved_lineno;
6881 /* If the loaded class is not accessible or couldn't be loaded,
6882 we restore the original TYPE_NAME and process the next
6884 if (access_check || !decl)
6886 TYPE_NAME (class_type) = type_name_copy;
6890 /* If the loaded class is accessible, we keep a tab on it to
6891 detect and report multiple inclusions. */
6892 if (IS_A_CLASSFILE_NAME (node))
6896 cl = TREE_PURPOSE (import);
6899 else if (seen_once >= 0)
6901 tree location = (cl ? cl : TREE_PURPOSE (import));
6902 tree package = (cl ? EXPR_WFL_NODE (cl) :
6903 EXPR_WFL_NODE (TREE_PURPOSE (import)));
6907 "Type `%s' also potentially defined in package `%s'",
6908 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6909 IDENTIFIER_POINTER (package));
6912 to_return = access_check;
6918 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6921 /* Add package NAME to the list of packages encountered so far. To
6922 speed up class lookup in do_resolve_class, we make sure a
6923 particular package is added only once. */
6926 register_package (tree name)
6932 pht = htab_create (50, htab_hash_pointer, htab_eq_pointer, NULL);
6934 e = htab_find_slot (pht, name, INSERT);
6937 package_list = chainon (package_list, build_tree_list (name, NULL));
6943 resolve_package (tree pkg, tree *next, tree *type_name)
6946 tree decl = NULL_TREE;
6947 *type_name = NULL_TREE;
6949 /* The trick is to determine when the package name stops and were
6950 the name of something contained in the package starts. Then we
6951 return a fully qualified name of what we want to get. */
6953 *next = EXPR_WFL_QUALIFICATION (pkg);
6955 /* Try to progressively construct a type name */
6956 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6957 for (current = EXPR_WFL_QUALIFICATION (pkg);
6958 current; current = TREE_CHAIN (current))
6960 /* If we don't have what we're expecting, exit now. TYPE_NAME
6961 will be null and the error caught later. */
6962 if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
6965 merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
6966 if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
6968 /* resolve_package should be used in a loop, hence we
6969 point at this one to naturally process the next one at
6970 the next iteration. */
6979 /* Check accessibility of inner classes according to member access rules.
6980 DECL is the inner class, ENCLOSING_DECL is the class from which the
6981 access is being attempted. */
6984 check_inner_class_access (tree decl, tree enclosing_decl, tree cl)
6987 tree enclosing_decl_type;
6989 /* We don't issue an error message when CL is null. CL can be null
6990 as a result of processing a JDEP crafted by source_start_java_method
6991 for the purpose of patching its parm decl. But the error would
6992 have been already trapped when fixing the method's signature.
6993 DECL can also be NULL in case of earlier errors. */
6997 enclosing_decl_type = TREE_TYPE (enclosing_decl);
6999 if (CLASS_PRIVATE (decl))
7001 /* Access is permitted only within the body of the top-level
7002 class in which DECL is declared. */
7003 tree top_level = decl;
7004 while (DECL_CONTEXT (top_level))
7005 top_level = DECL_CONTEXT (top_level);
7006 while (DECL_CONTEXT (enclosing_decl))
7007 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7008 if (top_level == enclosing_decl)
7012 else if (CLASS_PROTECTED (decl))
7015 /* Access is permitted from within the same package... */
7016 if (in_same_package (decl, enclosing_decl))
7019 /* ... or from within the body of a subtype of the context in which
7020 DECL is declared. */
7021 decl_context = DECL_CONTEXT (decl);
7022 while (enclosing_decl)
7024 if (CLASS_INTERFACE (decl))
7026 if (interface_of_p (TREE_TYPE (decl_context),
7027 enclosing_decl_type))
7032 /* Eww. The order of the arguments is different!! */
7033 if (inherits_from_p (enclosing_decl_type,
7034 TREE_TYPE (decl_context)))
7037 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7039 access = "protected";
7041 else if (! CLASS_PUBLIC (decl))
7043 /* Access is permitted only from within the same package as DECL. */
7044 if (in_same_package (decl, enclosing_decl))
7046 access = "non-public";
7049 /* Class is public. */
7052 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7053 (CLASS_INTERFACE (decl) ? "interface" : "class"),
7054 lang_printable_name (decl, 0), access);
7057 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7058 foreign package, it must be PUBLIC. Return 0 if no access
7059 violations were found, 1 otherwise. If VERBOSE is true and an error
7060 was found, it is reported and accounted for. */
7063 check_pkg_class_access (tree class_name, tree cl, bool verbose)
7067 if (!IDENTIFIER_CLASS_VALUE (class_name))
7070 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7073 if (!CLASS_PUBLIC (TYPE_NAME (type)))
7075 /* Access to a private class within the same package is
7078 breakdown_qualified (&l, &r, class_name);
7079 if (!QUALIFIED_P (class_name) && !ctxp->package)
7080 /* Both in the empty package. */
7082 if (l == ctxp->package)
7083 /* Both in the same package. */
7088 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
7089 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7090 IDENTIFIER_POINTER (class_name));
7096 /* Local variable declaration. */
7099 declare_local_variables (int modifier, tree type, tree vlist)
7101 tree decl, current, saved_type;
7102 tree type_wfl = NULL_TREE;
7106 /* Push a new block if statements were seen between the last time we
7107 pushed a block and now. Keep a count of blocks to close */
7108 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7110 tree b = enter_block ();
7111 BLOCK_IS_IMPLICIT (b) = 1;
7117 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7118 if (1 << i & modifier)
7120 if (modifier == ACC_FINAL)
7125 (ctxp->modifier_ctx [i],
7126 "Only `final' is allowed as a local variables modifier");
7131 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7132 hold the TYPE value if a new incomplete has to be created (as
7133 opposed to being found already existing and reused). */
7134 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7136 /* If TYPE is fully resolved and we don't have a reference, make one */
7137 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7139 /* Go through all the declared variables */
7140 for (current = vlist, saved_type = type; current;
7141 current = TREE_CHAIN (current), type = saved_type)
7143 tree other, real_type;
7144 tree wfl = TREE_PURPOSE (current);
7145 tree name = EXPR_WFL_NODE (wfl);
7146 tree init = TREE_VALUE (current);
7148 /* Process NAME, as it may specify extra dimension(s) for it */
7149 type = build_array_from_name (type, type_wfl, name, &name);
7151 /* Variable redefinition check */
7152 if ((other = lookup_name_in_blocks (name)))
7154 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7155 DECL_SOURCE_LINE (other));
7159 /* Type adjustment. We may have just readjusted TYPE because
7160 the variable specified more dimensions. Make sure we have
7161 a reference if we can and don't have one already. */
7162 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7164 real_type = GET_REAL_TYPE (type);
7165 /* Never layout this decl. This will be done when its scope
7167 decl = build_decl (VAR_DECL, name, real_type);
7168 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7169 DECL_FINAL (decl) = final_p;
7170 BLOCK_CHAIN_DECL (decl);
7172 /* If doing xreferencing, replace the line number with the WFL
7175 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7177 /* Don't try to use an INIT statement when an error was found */
7178 if (init && java_error_count)
7181 /* Add the initialization function to the current function's code */
7184 /* Name might have been readjusted */
7185 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7186 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7187 java_method_add_stmt (current_function_decl,
7188 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7192 /* Setup dependency the type of the decl */
7196 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7197 dep = CLASSD_LAST (ctxp->classd_list);
7198 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7201 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7204 /* Called during parsing. Build decls from argument list. */
7207 source_start_java_method (tree fndecl)
7216 current_function_decl = fndecl;
7218 /* New scope for the function */
7220 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7221 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7223 tree type = TREE_VALUE (tem);
7224 tree name = TREE_PURPOSE (tem);
7226 /* If type is incomplete. Create an incomplete decl and ask for
7227 the decl to be patched later */
7228 if (INCOMPLETE_TYPE_P (type))
7231 tree real_type = GET_REAL_TYPE (type);
7232 parm_decl = build_decl (PARM_DECL, name, real_type);
7233 type = obtain_incomplete_type (type);
7234 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7235 jdep = CLASSD_LAST (ctxp->classd_list);
7236 JDEP_MISC (jdep) = name;
7237 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7240 parm_decl = build_decl (PARM_DECL, name, type);
7242 /* Remember if a local variable was declared final (via its
7243 TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7244 if (ARG_FINAL_P (tem))
7246 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7247 DECL_FINAL (parm_decl) = 1;
7250 BLOCK_CHAIN_DECL (parm_decl);
7252 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7253 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7255 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7256 DECL_MAX_LOCALS (current_function_decl) = i;
7259 /* Called during parsing. Creates an artificial method declaration. */
7262 create_artificial_method (tree class, int flags, tree type,
7263 tree name, tree args)
7267 java_parser_context_save_global ();
7269 mdecl = make_node (FUNCTION_TYPE);
7270 TREE_TYPE (mdecl) = type;
7271 TYPE_ARG_TYPES (mdecl) = args;
7272 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7273 java_parser_context_restore_global ();
7274 DECL_ARTIFICIAL (mdecl) = 1;
7278 /* Starts the body if an artificial method. */
7281 start_artificial_method_body (tree mdecl)
7283 DECL_SOURCE_LINE (mdecl) = 1;
7284 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7285 source_start_java_method (mdecl);
7290 end_artificial_method_body (tree mdecl)
7292 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7293 It has to be evaluated first. (if mdecl is current_function_decl,
7294 we have an undefined behavior if no temporary variable is used.) */
7295 tree b = exit_block ();
7296 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7300 /* Dump a tree of some kind. This is a convenience wrapper for the
7301 dump_* functions in tree-dump.c. */
7303 dump_java_tree (enum tree_dump_index phase, tree t)
7308 stream = dump_begin (phase, &flags);
7312 dump_node (t, flags, stream);
7313 dump_end (phase, stream);
7317 /* Terminate a function and expand its body. */
7320 source_end_java_method (void)
7322 tree fndecl = current_function_decl;
7327 java_parser_context_save_global ();
7328 lineno = ctxp->last_ccb_indent1;
7330 /* Turn function bodies with only a NOP expr null, so they don't get
7331 generated at all and we won't get warnings when using the -W
7333 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7334 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7336 /* We've generated all the trees for this function, and it has been
7337 patched. Dump it to a file if the user requested it. */
7338 dump_java_tree (TDI_original, fndecl);
7340 java_optimize_inline (fndecl);
7342 /* Generate function's code */
7343 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7344 && ! flag_emit_class_files
7345 && ! flag_emit_xref)
7346 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7348 /* pop out of its parameters */
7349 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7351 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7353 /* Generate rtl for function exit. */
7354 if (! flag_emit_class_files && ! flag_emit_xref)
7356 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7357 expand_function_end (input_filename, lineno, 0);
7359 DECL_SOURCE_LINE (fndecl) = DECL_SOURCE_LINE_FIRST (fndecl);
7361 /* Run the optimizers and output assembler code for this function. */
7362 rest_of_compilation (fndecl);
7365 current_function_decl = NULL_TREE;
7366 java_parser_context_restore_global ();
7369 /* Record EXPR in the current function block. Complements compound
7370 expression second operand if necessary. */
7373 java_method_add_stmt (tree fndecl, tree expr)
7375 if (!GET_CURRENT_BLOCK (fndecl))
7377 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7381 add_stmt_to_block (tree b, tree type, tree stmt)
7383 tree body = BLOCK_EXPR_BODY (b), c;
7385 if (java_error_count)
7388 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7391 BLOCK_EXPR_BODY (b) = c;
7392 TREE_SIDE_EFFECTS (c) = 1;
7396 /* Add STMT to EXISTING if possible, otherwise create a new
7397 COMPOUND_EXPR and add STMT to it. */
7400 add_stmt_to_compound (tree existing, tree type, tree stmt)
7402 /* Keep track of this for inlining. */
7403 if (current_function_decl)
7404 ++DECL_NUM_STMTS (current_function_decl);
7407 return build (COMPOUND_EXPR, type, existing, stmt);
7412 void java_layout_seen_class_methods (void)
7414 tree previous_list = all_class_list;
7415 tree end = NULL_TREE;
7420 for (current = previous_list;
7421 current != end; current = TREE_CHAIN (current))
7422 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7424 if (previous_list != all_class_list)
7426 end = previous_list;
7427 previous_list = all_class_list;
7434 static GTY(()) tree stop_reordering;
7436 java_reorder_fields (void)
7440 for (current = gclass_list; current; current = TREE_CHAIN (current))
7442 current_class = TREE_TYPE (TREE_VALUE (current));
7444 if (current_class == stop_reordering)
7447 /* Reverse the fields, but leave the dummy field in front.
7448 Fields are already ordered for Object and Class */
7449 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7450 && current_class != class_type_node)
7452 /* If the dummy field is there, reverse the right fields and
7453 just layout the type for proper fields offset */
7454 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7456 tree fields = TYPE_FIELDS (current_class);
7457 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7458 TYPE_SIZE (current_class) = NULL_TREE;
7460 /* We don't have a dummy field, we need to layout the class,
7461 after having reversed the fields */
7464 TYPE_FIELDS (current_class) =
7465 nreverse (TYPE_FIELDS (current_class));
7466 TYPE_SIZE (current_class) = NULL_TREE;
7470 /* There are cases were gclass_list will be empty. */
7472 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7475 /* Layout the methods of all classes loaded in one way or another.
7476 Check methods of source parsed classes. Then reorder the
7477 fields and layout the classes or the type of all source parsed
7481 java_layout_classes (void)
7484 int save_error_count = java_error_count;
7486 /* Layout the methods of all classes seen so far */
7487 java_layout_seen_class_methods ();
7488 java_parse_abort_on_error ();
7489 all_class_list = NULL_TREE;
7491 /* Then check the methods of all parsed classes */
7492 for (current = gclass_list; current; current = TREE_CHAIN (current))
7493 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7494 java_check_methods (TREE_VALUE (current));
7495 java_parse_abort_on_error ();
7497 for (current = gclass_list; current; current = TREE_CHAIN (current))
7499 current_class = TREE_TYPE (TREE_VALUE (current));
7500 layout_class (current_class);
7502 /* Error reported by the caller */
7503 if (java_error_count)
7507 /* We might have reloaded classes durign the process of laying out
7508 classes for code generation. We must layout the methods of those
7509 late additions, as constructor checks might use them */
7510 java_layout_seen_class_methods ();
7511 java_parse_abort_on_error ();
7514 /* Expand methods in the current set of classes rememebered for
7518 java_complete_expand_classes (void)
7522 do_not_fold = flag_emit_xref;
7524 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7525 if (!INNER_CLASS_DECL_P (current))
7526 java_complete_expand_class (current);
7529 /* Expand the methods found in OUTER, starting first by OUTER's inner
7533 java_complete_expand_class (tree outer)
7537 set_nested_class_simple_name_value (outer, 1); /* Set */
7539 /* We need to go after all inner classes and start expanding them,
7540 starting with most nested ones. We have to do that because nested
7541 classes might add functions to outer classes */
7543 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7544 inner_list; inner_list = TREE_CHAIN (inner_list))
7545 java_complete_expand_class (TREE_PURPOSE (inner_list));
7547 java_complete_expand_methods (outer);
7548 set_nested_class_simple_name_value (outer, 0); /* Reset */
7551 /* Expand methods registered in CLASS_DECL. The general idea is that
7552 we expand regular methods first. This allows us get an estimate on
7553 how outer context local alias fields are really used so we can add
7554 to the constructor just enough code to initialize them properly (it
7555 also lets us generate finit$ correctly.) Then we expand the
7556 constructors and then <clinit>. */
7559 java_complete_expand_methods (tree class_decl)
7561 tree clinit, decl, first_decl;
7563 current_class = TREE_TYPE (class_decl);
7565 /* Initialize a new constant pool */
7566 init_outgoing_cpool ();
7568 /* Pre-expand <clinit> to figure whether we really need it or
7569 not. If we do need it, we pre-expand the static fields so they're
7570 ready to be used somewhere else. <clinit> will be fully expanded
7571 after we processed the constructors. */
7572 first_decl = TYPE_METHODS (current_class);
7573 clinit = maybe_generate_pre_expand_clinit (current_class);
7575 /* Then generate finit$ (if we need to) because constructors will
7577 if (TYPE_FINIT_STMT_LIST (current_class))
7578 java_complete_expand_method (generate_finit (current_class));
7580 /* Then generate instinit$ (if we need to) because constructors will
7582 if (TYPE_II_STMT_LIST (current_class))
7583 java_complete_expand_method (generate_instinit (current_class));
7585 /* Now do the constructors */
7586 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7590 if (!DECL_CONSTRUCTOR_P (decl))
7593 no_body = !DECL_FUNCTION_BODY (decl);
7594 /* Don't generate debug info on line zero when expanding a
7595 generated constructor. */
7597 restore_line_number_status (1);
7599 java_complete_expand_method (decl);
7602 restore_line_number_status (0);
7605 /* First, do the ordinary methods. */
7606 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7608 /* Ctors aren't part of this batch. */
7609 if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7612 /* Skip abstract or native methods -- but do handle native
7613 methods when generating JNI stubs. */
7614 if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7616 DECL_FUNCTION_BODY (decl) = NULL_TREE;
7620 if (METHOD_NATIVE (decl))
7623 current_function_decl = decl;
7624 body = build_jni_stub (decl);
7625 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7628 java_complete_expand_method (decl);
7631 /* If there is indeed a <clinit>, fully expand it now */
7634 /* Prevent the use of `this' inside <clinit> */
7635 ctxp->explicit_constructor_p = 1;
7636 java_complete_expand_method (clinit);
7637 ctxp->explicit_constructor_p = 0;
7640 /* We might have generated a class$ that we now want to expand */
7641 if (TYPE_DOT_CLASS (current_class))
7642 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7644 /* Now verify constructor circularity (stop after the first one we
7646 if (!CLASS_INTERFACE (class_decl))
7647 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7648 if (DECL_CONSTRUCTOR_P (decl)
7649 && verify_constructor_circularity (decl, decl))
7652 /* Save the constant pool. We'll need to restore it later. */
7653 TYPE_CPOOL (current_class) = outgoing_cpool;
7656 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7657 safely used in some other methods/constructors. */
7660 maybe_generate_pre_expand_clinit (tree class_type)
7662 tree current, mdecl;
7664 if (!TYPE_CLINIT_STMT_LIST (class_type))
7667 /* Go through all static fields and pre expand them */
7668 for (current = TYPE_FIELDS (class_type); current;
7669 current = TREE_CHAIN (current))
7670 if (FIELD_STATIC (current))
7671 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7673 /* Then build the <clinit> method */
7674 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7675 clinit_identifier_node, end_params_node);
7676 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7678 start_artificial_method_body (mdecl);
7680 /* We process the list of assignment we produced as the result of
7681 the declaration of initialized static field and add them as
7682 statement to the <clinit> method. */
7683 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7684 current = TREE_CHAIN (current))
7686 tree stmt = current;
7687 /* We build the assignment expression that will initialize the
7688 field to its value. There are strict rules on static
7689 initializers (8.5). FIXME */
7690 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7691 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7692 java_method_add_stmt (mdecl, stmt);
7695 end_artificial_method_body (mdecl);
7697 /* Now we want to place <clinit> as the last method (because we need
7698 it at least for interface so that it doesn't interfere with the
7699 dispatch table based lookup. */
7700 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7702 current = TREE_CHAIN (TYPE_METHODS (class_type));
7703 TYPE_METHODS (class_type) = current;
7705 while (TREE_CHAIN (current))
7706 current = TREE_CHAIN (current);
7708 TREE_CHAIN (current) = mdecl;
7709 TREE_CHAIN (mdecl) = NULL_TREE;
7715 /* Analyzes a method body and look for something that isn't a
7716 MODIFY_EXPR with a constant value. */
7719 analyze_clinit_body (tree this_class, tree bbody)
7722 switch (TREE_CODE (bbody))
7725 bbody = BLOCK_EXPR_BODY (bbody);
7728 case EXPR_WITH_FILE_LOCATION:
7729 bbody = EXPR_WFL_NODE (bbody);
7733 if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
7735 bbody = TREE_OPERAND (bbody, 1);
7739 /* If we're generating to class file and we're dealing with an
7740 array initialization, we return 1 to keep <clinit> */
7741 if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
7742 && flag_emit_class_files)
7745 /* There are a few cases where we're required to keep
7747 - If this is an assignment whose operand is not constant,
7748 - If this is an assignment to a non-initialized field,
7749 - If this field is not a member of the current class.
7751 return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
7752 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
7753 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
7762 /* See whether we could get rid of <clinit>. Criteria are: all static
7763 final fields have constant initial values and the body of <clinit>
7764 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7767 maybe_yank_clinit (tree mdecl)
7772 if (!DECL_CLINIT_P (mdecl))
7775 /* If the body isn't empty, then we keep <clinit>. Note that if
7776 we're emitting classfiles, this isn't enough not to rule it
7778 fbody = DECL_FUNCTION_BODY (mdecl);
7779 bbody = BLOCK_EXPR_BODY (fbody);
7780 if (bbody && bbody != error_mark_node)
7781 bbody = BLOCK_EXPR_BODY (bbody);
7784 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7787 type = DECL_CONTEXT (mdecl);
7788 current = TYPE_FIELDS (type);
7790 for (current = (current ? TREE_CHAIN (current) : current);
7791 current; current = TREE_CHAIN (current))
7795 /* We're not interested in non-static fields. */
7796 if (!FIELD_STATIC (current))
7799 /* Nor in fields without initializers. */
7800 f_init = DECL_INITIAL (current);
7801 if (f_init == NULL_TREE)
7804 /* Anything that isn't String or a basic type is ruled out -- or
7805 if we know how to deal with it (when doing things natively) we
7806 should generated an empty <clinit> so that SUID are computed
7808 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7809 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7812 if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
7816 /* Now we analyze the method body and look for something that
7817 isn't a MODIFY_EXPR */
7818 if (bbody != empty_stmt_node && analyze_clinit_body (type, bbody))
7821 /* Get rid of <clinit> in the class' list of methods */
7822 if (TYPE_METHODS (type) == mdecl)
7823 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7825 for (current = TYPE_METHODS (type); current;
7826 current = TREE_CHAIN (current))
7827 if (TREE_CHAIN (current) == mdecl)
7829 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7836 /* Install the argument from MDECL. Suitable to completion and
7837 expansion of mdecl's body. */
7840 start_complete_expand_method (tree mdecl)
7844 pushlevel (1); /* Prepare for a parameter push */
7845 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7846 DECL_ARGUMENTS (mdecl) = tem;
7848 for (; tem; tem = TREE_CHAIN (tem))
7850 /* TREE_CHAIN (tem) will change after pushdecl. */
7851 tree next = TREE_CHAIN (tem);
7852 tree type = TREE_TYPE (tem);
7853 if (PROMOTE_PROTOTYPES
7854 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7855 && INTEGRAL_TYPE_P (type))
7856 type = integer_type_node;
7857 DECL_ARG_TYPE (tem) = type;
7858 layout_decl (tem, 0);
7860 /* Re-install the next so that the list is kept and the loop
7862 TREE_CHAIN (tem) = next;
7864 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7865 lineno = DECL_SOURCE_LINE_FIRST (mdecl);
7866 build_result_decl (mdecl);
7870 /* Complete and expand a method. */
7873 java_complete_expand_method (tree mdecl)
7875 tree fbody, block_body, exception_copy;
7877 current_function_decl = mdecl;
7878 /* Fix constructors before expanding them */
7879 if (DECL_CONSTRUCTOR_P (mdecl))
7880 fix_constructors (mdecl);
7882 /* Expand functions that have a body */
7883 if (!DECL_FUNCTION_BODY (mdecl))
7886 fbody = DECL_FUNCTION_BODY (mdecl);
7887 block_body = BLOCK_EXPR_BODY (fbody);
7888 exception_copy = NULL_TREE;
7890 current_function_decl = mdecl;
7893 fprintf (stderr, " [%s.",
7894 lang_printable_name (DECL_CONTEXT (mdecl), 0));
7895 announce_function (mdecl);
7897 fprintf (stderr, "]");
7899 /* Prepare the function for tree completion */
7900 start_complete_expand_method (mdecl);
7902 /* Install the current this */
7903 current_this = (!METHOD_STATIC (mdecl) ?
7904 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7906 /* Purge the `throws' list of unchecked exceptions (we save a copy
7907 of the list and re-install it later.) */
7908 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7909 purge_unchecked_exceptions (mdecl);
7911 /* Install exceptions thrown with `throws' */
7912 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7914 if (block_body != NULL_TREE)
7916 block_body = java_complete_tree (block_body);
7918 /* Before we check initialization, attached all class initialization
7919 variable to the block_body */
7920 htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
7921 attach_init_test_initialization_flags, block_body);
7923 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7925 check_for_initialization (block_body, mdecl);
7927 /* Go through all the flags marking the initialization of
7928 static variables and see whether they're definitively
7929 assigned, in which case the type is remembered as
7930 definitively initialized in MDECL. */
7931 if (STATIC_CLASS_INIT_OPT_P ())
7933 /* Always register the context as properly initialized in
7934 MDECL. This used with caution helps removing extra
7935 initialization of self. */
7936 if (METHOD_STATIC (mdecl))
7939 (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
7940 DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
7944 ctxp->explicit_constructor_p = 0;
7947 BLOCK_EXPR_BODY (fbody) = block_body;
7949 /* If we saw a return but couldn't evaluate it properly, we'll have
7950 an error_mark_node here. */
7951 if (block_body != error_mark_node
7952 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7953 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7955 missing_return_error (current_function_decl);
7957 /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
7958 maybe_yank_clinit (mdecl);
7960 /* Pop the current level, with special measures if we found errors. */
7961 if (java_error_count)
7962 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7965 /* Pop the exceptions and sanity check */
7967 if (currently_caught_type_list)
7970 /* Restore the copy of the list of exceptions if emitting xrefs. */
7971 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7974 /* For with each class for which there's code to generate. */
7977 java_expand_method_bodies (tree class)
7980 for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
7982 if (!DECL_FUNCTION_BODY (decl))
7985 current_function_decl = decl;
7987 /* Save the function for inlining. */
7988 if (flag_inline_trees)
7989 DECL_SAVED_TREE (decl) =
7990 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
7992 /* It's time to assign the variable flagging static class
7993 initialization based on which classes invoked static methods
7994 are definitely initializing. This should be flagged. */
7995 if (STATIC_CLASS_INIT_OPT_P ())
7997 tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
7998 for (; list != NULL_TREE; list = TREE_CHAIN (list))
8000 /* Executed for each statement calling a static function.
8001 LIST is a TREE_LIST whose PURPOSE is the called function
8002 and VALUE is a compound whose second operand can be patched
8003 with static class initialization flag assignments. */
8005 tree called_method = TREE_PURPOSE (list);
8006 tree compound = TREE_VALUE (list);
8007 tree assignment_compound_list
8008 = build_tree_list (called_method, NULL);
8010 /* For each class definitely initialized in
8011 CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8012 assignment to the class initialization flag. */
8013 htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8014 emit_test_initialization,
8015 assignment_compound_list);
8017 if (TREE_VALUE (assignment_compound_list))
8018 TREE_OPERAND (compound, 1)
8019 = TREE_VALUE (assignment_compound_list);
8023 /* Prepare the function for RTL expansion */
8024 start_complete_expand_method (decl);
8026 /* Expand function start, generate initialization flag
8027 assignment, and handle synchronized methods. */
8028 complete_start_java_method (decl);
8030 /* Expand the rest of the function body and terminate
8032 source_end_java_method ();
8038 /* This section of the code deals with accessing enclosing context
8039 fields either directly by using the relevant access to this$<n> or
8040 by invoking an access method crafted for that purpose. */
8042 /* Build the necessary access from an inner class to an outer
8043 class. This routine could be optimized to cache previous result
8044 (decl, current_class and returned access). When an access method
8045 needs to be generated, it always takes the form of a read. It might
8046 be later turned into a write by calling outer_field_access_fix. */
8049 build_outer_field_access (tree id, tree decl)
8051 tree access = NULL_TREE;
8052 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8053 tree decl_ctx = DECL_CONTEXT (decl);
8055 /* If the immediate enclosing context of the current class is the
8056 field decl's class or inherits from it; build the access as
8057 `this$<n>.<field>'. Note that we will break the `private' barrier
8058 if we're not emitting bytecodes. */
8059 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8060 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8062 tree thisn = build_current_thisn (current_class);
8063 access = make_qualified_primary (build_wfl_node (thisn),
8064 id, EXPR_WFL_LINECOL (id));
8066 /* Otherwise, generate access methods to outer this and access the
8067 field (either using an access method or by direct access.) */
8070 int lc = EXPR_WFL_LINECOL (id);
8072 /* Now we chain the required number of calls to the access$0 to
8073 get a hold to the enclosing instance we need, and then we
8074 build the field access. */
8075 access = build_access_to_thisn (current_class, decl_ctx, lc);
8077 /* If the field is private and we're generating bytecode, then
8078 we generate an access method */
8079 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8081 tree name = build_outer_field_access_methods (decl);
8082 access = build_outer_field_access_expr (lc, decl_ctx,
8083 name, access, NULL_TREE);
8085 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8086 Once again we break the `private' access rule from a foreign
8089 access = make_qualified_primary (access, id, lc);
8091 return resolve_expression_name (access, NULL);
8094 /* Return a nonzero value if NODE describes an outer field inner
8098 outer_field_access_p (tree type, tree decl)
8100 if (!INNER_CLASS_TYPE_P (type)
8101 || TREE_CODE (decl) != FIELD_DECL
8102 || DECL_CONTEXT (decl) == type)
8105 /* If the inner class extends the declaration context of the field
8106 we're try to acces, then this isn't an outer field access */
8107 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8110 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8111 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8113 if (type == DECL_CONTEXT (decl))
8116 if (!DECL_CONTEXT (TYPE_NAME (type)))
8118 /* Before we give up, see whether the field is inherited from
8119 the enclosing context we're considering. */
8120 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8129 /* Return a nonzero value if NODE represents an outer field inner
8130 access that was been already expanded. As a side effect, it returns
8131 the name of the field being accessed and the argument passed to the
8132 access function, suitable for a regeneration of the access method
8133 call if necessary. */
8136 outer_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8141 if (TREE_CODE (node) != CALL_EXPR)
8144 /* Well, gcj generates slightly different tree nodes when compiling
8145 to native or bytecodes. It's the case for function calls. */
8147 if (flag_emit_class_files
8148 && TREE_CODE (node) == CALL_EXPR
8149 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8151 else if (!flag_emit_class_files)
8153 node = TREE_OPERAND (node, 0);
8155 if (node && TREE_OPERAND (node, 0)
8156 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8158 node = TREE_OPERAND (node, 0);
8159 if (TREE_OPERAND (node, 0)
8160 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8161 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8162 (DECL_NAME (TREE_OPERAND (node, 0)))))
8167 if (identified && name && arg_type && arg)
8169 tree argument = TREE_OPERAND (node, 1);
8170 *name = DECL_NAME (TREE_OPERAND (node, 0));
8171 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8172 *arg = TREE_VALUE (argument);
8177 /* Detect in NODE an outer field read access from an inner class and
8178 transform it into a write with RHS as an argument. This function is
8179 called from the java_complete_lhs when an assignment to a LHS can
8183 outer_field_access_fix (tree wfl, tree node, tree rhs)
8185 tree name, arg_type, arg;
8187 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8189 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8190 arg_type, name, arg, rhs);
8191 return java_complete_tree (node);
8196 /* Construct the expression that calls an access method:
8197 <type>.access$<n>(<arg1> [, <arg2>]);
8199 ARG2 can be NULL and will be omitted in that case. It will denote a
8203 build_outer_field_access_expr (int lc, tree type, tree access_method_name,
8204 tree arg1, tree arg2)
8206 tree args, cn, access;
8208 args = arg1 ? arg1 :
8209 build_wfl_node (build_current_thisn (current_class));
8210 args = build_tree_list (NULL_TREE, args);
8213 args = tree_cons (NULL_TREE, arg2, args);
8215 access = build_method_invocation (build_wfl_node (access_method_name), args);
8216 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8217 return make_qualified_primary (cn, access, lc);
8221 build_new_access_id (void)
8223 static int access_n_counter = 1;
8226 sprintf (buffer, "access$%d", access_n_counter++);
8227 return get_identifier (buffer);
8230 /* Create the static access functions for the outer field DECL. We define a
8232 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8236 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8237 TREE_TYPE (<field>) value$) {
8238 return inst$.field = value$;
8240 We should have a usage flags on the DECL so we can lazily turn the ones
8241 we're using for code generation. FIXME.
8245 build_outer_field_access_methods (tree decl)
8247 tree id, args, stmt, mdecl;
8249 if (FIELD_INNER_ACCESS_P (decl))
8250 return FIELD_INNER_ACCESS (decl);
8252 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8254 /* Create the identifier and a function named after it. */
8255 id = build_new_access_id ();
8257 /* The identifier is marked as bearing the name of a generated write
8258 access function for outer field accessed from inner classes. */
8259 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8261 /* Create the read access */
8262 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8263 TREE_CHAIN (args) = end_params_node;
8264 stmt = make_qualified_primary (build_wfl_node (inst_id),
8265 build_wfl_node (DECL_NAME (decl)), 0);
8266 stmt = build_return (0, stmt);
8267 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8268 TREE_TYPE (decl), id, args, stmt);
8269 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8271 /* Create the write access method. No write access for final variable */
8272 if (!FIELD_FINAL (decl))
8274 args = build_tree_list (inst_id,
8275 build_pointer_type (DECL_CONTEXT (decl)));
8276 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8277 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8278 stmt = make_qualified_primary (build_wfl_node (inst_id),
8279 build_wfl_node (DECL_NAME (decl)), 0);
8280 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8281 build_wfl_node (wpv_id)));
8282 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8283 TREE_TYPE (decl), id,
8286 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8288 /* Return the access name */
8289 return FIELD_INNER_ACCESS (decl) = id;
8292 /* Build an field access method NAME. */
8295 build_outer_field_access_method (tree class, tree type, tree name,
8296 tree args, tree body)
8298 tree saved_current_function_decl, mdecl;
8300 /* Create the method */
8301 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8302 fix_method_argument_names (args, mdecl);
8303 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8305 /* Attach the method body. */
8306 saved_current_function_decl = current_function_decl;
8307 start_artificial_method_body (mdecl);
8308 java_method_add_stmt (mdecl, body);
8309 end_artificial_method_body (mdecl);
8310 current_function_decl = saved_current_function_decl;
8316 /* This section deals with building access function necessary for
8317 certain kinds of method invocation from inner classes. */
8320 build_outer_method_access_method (tree decl)
8322 tree saved_current_function_decl, mdecl;
8323 tree args = NULL_TREE, call_args = NULL_TREE;
8324 tree carg, id, body, class;
8326 int parm_id_count = 0;
8328 /* Test this abort with an access to a private field */
8329 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8332 /* Check the cache first */
8333 if (DECL_FUNCTION_INNER_ACCESS (decl))
8334 return DECL_FUNCTION_INNER_ACCESS (decl);
8336 class = DECL_CONTEXT (decl);
8338 /* Obtain an access identifier and mark it */
8339 id = build_new_access_id ();
8340 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8342 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8343 /* Create the arguments, as much as the original */
8344 for (; carg && carg != end_params_node;
8345 carg = TREE_CHAIN (carg))
8347 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8348 args = chainon (args, build_tree_list (get_identifier (buffer),
8349 TREE_VALUE (carg)));
8351 args = chainon (args, end_params_node);
8353 /* Create the method */
8354 mdecl = create_artificial_method (class, ACC_STATIC,
8355 TREE_TYPE (TREE_TYPE (decl)), id, args);
8356 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8357 /* There is a potential bug here. We should be able to use
8358 fix_method_argument_names, but then arg names get mixed up and
8359 eventually a constructor will have its this$0 altered and the
8360 outer context won't be assignment properly. The test case is
8362 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8364 /* Attach the method body. */
8365 saved_current_function_decl = current_function_decl;
8366 start_artificial_method_body (mdecl);
8368 /* The actual method invocation uses the same args. When invoking a
8369 static methods that way, we don't want to skip the first
8372 if (!METHOD_STATIC (decl))
8373 carg = TREE_CHAIN (carg);
8374 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8375 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8378 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8380 if (!METHOD_STATIC (decl))
8381 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8383 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8384 body = build_return (0, body);
8385 java_method_add_stmt (mdecl,body);
8386 end_artificial_method_body (mdecl);
8387 current_function_decl = saved_current_function_decl;
8389 /* Back tag the access function so it know what it accesses */
8390 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8392 /* Tag the current method so it knows it has an access generated */
8393 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8397 /* This section of the code deals with building expressions to access
8398 the enclosing instance of an inner class. The enclosing instance is
8399 kept in a generated field called this$<n>, with <n> being the
8400 inner class nesting level (starting from 0.) */
8402 /* Build an access to a given this$<n>, always chaining access call to
8403 others. Access methods to this$<n> are build on the fly if
8404 necessary. This CAN'T be used to solely access this$<n-1> from
8405 this$<n> (which alway yield to special cases and optimization, see
8406 for example build_outer_field_access). */
8409 build_access_to_thisn (tree from, tree to, int lc)
8411 tree access = NULL_TREE;
8413 while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8417 access = build_current_thisn (from);
8418 access = build_wfl_node (access);
8422 tree access0_wfl, cn;
8424 maybe_build_thisn_access_method (from);
8425 access0_wfl = build_wfl_node (access0_identifier_node);
8426 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8427 EXPR_WFL_LINECOL (access0_wfl) = lc;
8428 access = build_tree_list (NULL_TREE, access);
8429 access = build_method_invocation (access0_wfl, access);
8430 access = make_qualified_primary (cn, access, lc);
8433 /* If FROM isn't an inner class, that's fine, we've done enough.
8434 What we're looking for can be accessed from there. */
8435 from = DECL_CONTEXT (TYPE_NAME (from));
8438 from = TREE_TYPE (from);
8443 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8444 is returned if nothing needs to be generated. Otherwise, the method
8445 generated and a method decl is returned.
8447 NOTE: These generated methods should be declared in a class file
8448 attribute so that they can't be referred to directly. */
8451 maybe_build_thisn_access_method (tree type)
8453 tree mdecl, args, stmt, rtype;
8454 tree saved_current_function_decl;
8456 /* If TYPE is a top-level class, no access method is required.
8457 If there already is such an access method, bail out. */
8458 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8461 /* We generate the method. The method looks like:
8462 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8464 args = build_tree_list (inst_id, build_pointer_type (type));
8465 TREE_CHAIN (args) = end_params_node;
8466 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8467 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8468 access0_identifier_node, args);
8469 fix_method_argument_names (args, mdecl);
8470 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8471 stmt = build_current_thisn (type);
8472 stmt = make_qualified_primary (build_wfl_node (inst_id),
8473 build_wfl_node (stmt), 0);
8474 stmt = build_return (0, stmt);
8476 saved_current_function_decl = current_function_decl;
8477 start_artificial_method_body (mdecl);
8478 java_method_add_stmt (mdecl, stmt);
8479 end_artificial_method_body (mdecl);
8480 current_function_decl = saved_current_function_decl;
8482 CLASS_ACCESS0_GENERATED_P (type) = 1;
8487 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8488 the first level of innerclassing. this$1 for the next one, etc...
8489 This function can be invoked with TYPE to NULL, available and then
8490 has to count the parser context. */
8492 static GTY(()) tree saved_thisn;
8493 static GTY(()) tree saved_type;
8496 build_current_thisn (tree type)
8498 static int saved_i = -1;
8499 static int saved_type_i = 0;
8506 if (type == saved_type)
8510 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8511 decl; decl = DECL_CONTEXT (decl), i++)
8519 i = list_length (GET_CPC_LIST ())-2;
8524 sprintf (buffer, "this$%d", i);
8526 saved_thisn = get_identifier (buffer);
8530 /* Return the assignement to the hidden enclosing context `this$<n>'
8531 by the second incoming parameter to the innerclass constructor. The
8532 form used is `this.this$<n> = this$<n>;'. */
8535 build_thisn_assign (void)
8537 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8539 tree thisn = build_current_thisn (current_class);
8540 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8541 build_wfl_node (thisn), 0);
8542 tree rhs = build_wfl_node (thisn);
8543 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8544 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8550 /* Building the synthetic `class$' used to implement the `.class' 1.1
8551 extension for non primitive types. This method looks like:
8553 static Class class$(String type) throws NoClassDefFoundError
8555 try {return (java.lang.Class.forName (String));}
8556 catch (ClassNotFoundException e) {
8557 throw new NoClassDefFoundError(e.getMessage());}
8560 static GTY(()) tree get_message_wfl;
8561 static GTY(()) tree type_parm_wfl;
8564 build_dot_class_method (tree class)
8566 #define BWF(S) build_wfl_node (get_identifier ((S)))
8567 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8568 tree args, tmp, saved_current_function_decl, mdecl;
8569 tree stmt, throw_stmt;
8571 if (!get_message_wfl)
8573 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8574 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8577 /* Build the arguments */
8578 args = build_tree_list (get_identifier ("type$"),
8579 build_pointer_type (string_type_node));
8580 TREE_CHAIN (args) = end_params_node;
8582 /* Build the qualified name java.lang.Class.forName */
8583 tmp = MQN (MQN (MQN (BWF ("java"),
8584 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8585 load_class (class_not_found_type_node, 1);
8586 load_class (no_class_def_found_type_node, 1);
8588 /* Create the "class$" function */
8589 mdecl = create_artificial_method (class, ACC_STATIC,
8590 build_pointer_type (class_type_node),
8591 classdollar_identifier_node, args);
8592 DECL_FUNCTION_THROWS (mdecl) =
8593 build_tree_list (NULL_TREE, no_class_def_found_type_node);
8595 /* We start by building the try block. We need to build:
8596 return (java.lang.Class.forName (type)); */
8597 stmt = build_method_invocation (tmp,
8598 build_tree_list (NULL_TREE, type_parm_wfl));
8599 stmt = build_return (0, stmt);
8601 /* Now onto the catch block. We start by building the expression
8602 throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8603 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8604 get_message_wfl, 0);
8605 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8607 /* Build new NoClassDefFoundError (_.getMessage) */
8608 throw_stmt = build_new_invocation
8609 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8610 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8612 /* Build the throw, (it's too early to use BUILD_THROW) */
8613 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8615 /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8616 stmt = encapsulate_with_try_catch (0, class_not_found_type_node,
8619 fix_method_argument_names (args, mdecl);
8620 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8621 saved_current_function_decl = current_function_decl;
8622 start_artificial_method_body (mdecl);
8623 java_method_add_stmt (mdecl, stmt);
8624 end_artificial_method_body (mdecl);
8625 current_function_decl = saved_current_function_decl;
8626 TYPE_DOT_CLASS (class) = mdecl;
8632 build_dot_class_method_invocation (tree type)
8636 if (TYPE_ARRAY_P (type))
8637 sig_id = build_java_signature (type);
8639 sig_id = DECL_NAME (TYPE_NAME (type));
8641 /* Ensure that the proper name separator is used */
8642 sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8643 IDENTIFIER_LENGTH (sig_id));
8645 s = build_string (IDENTIFIER_LENGTH (sig_id),
8646 IDENTIFIER_POINTER (sig_id));
8647 return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8648 build_tree_list (NULL_TREE, s));
8651 /* This section of the code deals with constructor. */
8653 /* Craft a body for default constructor. Patch existing constructor
8654 bodies with call to super() and field initialization statements if
8658 fix_constructors (tree mdecl)
8660 tree iii; /* Instance Initializer Invocation */
8661 tree body = DECL_FUNCTION_BODY (mdecl);
8662 tree thisn_assign, compound = NULL_TREE;
8663 tree class_type = DECL_CONTEXT (mdecl);
8665 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8667 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8671 /* It is an error for the compiler to generate a default
8672 constructor if the superclass doesn't have a constructor that
8673 takes no argument, or the same args for an anonymous class */
8674 if (verify_constructor_super (mdecl))
8676 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8677 tree save = DECL_NAME (mdecl);
8678 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8679 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8681 (lookup_cl (TYPE_NAME (class_type)),
8682 "No constructor matching `%s' found in class `%s'",
8683 lang_printable_name (mdecl, 0), n);
8684 DECL_NAME (mdecl) = save;
8687 /* The constructor body must be crafted by hand. It's the
8688 constructor we defined when we realize we didn't have the
8689 CLASSNAME() constructor */
8690 start_artificial_method_body (mdecl);
8692 /* Insert an assignment to the this$<n> hidden field, if
8694 if ((thisn_assign = build_thisn_assign ()))
8695 java_method_add_stmt (mdecl, thisn_assign);
8697 /* We don't generate a super constructor invocation if we're
8698 compiling java.lang.Object. build_super_invocation takes care
8700 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8703 if ((iii = build_instinit_invocation (class_type)))
8704 java_method_add_stmt (mdecl, iii);
8706 end_artificial_method_body (mdecl);
8708 /* Search for an explicit constructor invocation */
8712 int invokes_this = 0;
8713 tree found_call = NULL_TREE;
8714 tree main_block = BLOCK_EXPR_BODY (body);
8717 switch (TREE_CODE (body))
8720 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8721 if (CALL_THIS_CONSTRUCTOR_P (body))
8726 case EXPR_WITH_FILE_LOCATION:
8728 body = TREE_OPERAND (body, 0);
8732 body = BLOCK_EXPR_BODY (body);
8739 /* Generate the assignment to this$<n>, if necessary */
8740 if ((thisn_assign = build_thisn_assign ()))
8741 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8743 /* The constructor is missing an invocation of super() */
8745 compound = add_stmt_to_compound (compound, NULL_TREE,
8746 build_super_invocation (mdecl));
8747 /* Explicit super() invokation should take place before the
8748 instance initializer blocks. */
8751 compound = add_stmt_to_compound (compound, NULL_TREE,
8752 TREE_OPERAND (found_call, 0));
8753 TREE_OPERAND (found_call, 0) = empty_stmt_node;
8756 DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
8758 /* Insert the instance initializer block right after. */
8759 if (!invokes_this && (iii = build_instinit_invocation (class_type)))
8760 compound = add_stmt_to_compound (compound, NULL_TREE, iii);
8762 /* Fix the constructor main block if we're adding extra stmts */
8765 compound = add_stmt_to_compound (compound, NULL_TREE,
8766 BLOCK_EXPR_BODY (main_block));
8767 BLOCK_EXPR_BODY (main_block) = compound;
8772 /* Browse constructors in the super class, searching for a constructor
8773 that doesn't take any argument. Return 0 if one is found, 1
8774 otherwise. If the current class is an anonymous inner class, look
8775 for something that has the same signature. */
8778 verify_constructor_super (tree mdecl)
8780 tree class = CLASSTYPE_SUPER (current_class);
8781 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8787 if (ANONYMOUS_CLASS_P (current_class))
8789 tree mdecl_arg_type;
8790 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8791 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8792 if (DECL_CONSTRUCTOR_P (sdecl))
8795 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8797 arg_type = TREE_CHAIN (arg_type);
8798 for (m_arg_type = mdecl_arg_type;
8799 (arg_type != end_params_node
8800 && m_arg_type != end_params_node);
8801 arg_type = TREE_CHAIN (arg_type),
8802 m_arg_type = TREE_CHAIN (m_arg_type))
8803 if (!valid_method_invocation_conversion_p
8804 (TREE_VALUE (arg_type),
8805 TREE_VALUE (m_arg_type)))
8808 if (arg_type == end_params_node && m_arg_type == end_params_node)
8814 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8816 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8818 arg = TREE_CHAIN (arg);
8819 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8826 /* Generate code for all context remembered for code generation. */
8828 static GTY(()) tree reversed_class_list;
8830 java_expand_classes (void)
8832 int save_error_count = 0;
8833 static struct parser_ctxt *cur_ctxp = NULL;
8835 java_parse_abort_on_error ();
8836 if (!(ctxp = ctxp_for_generation))
8838 java_layout_classes ();
8839 java_parse_abort_on_error ();
8841 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8844 input_filename = ctxp->filename;
8845 lang_init_source (2); /* Error msgs have method prototypes */
8846 java_complete_expand_classes (); /* Complete and expand classes */
8847 java_parse_abort_on_error ();
8849 input_filename = main_input_filename;
8852 /* Find anonymous classes and expand their constructor. This extra pass is
8853 neccessary because the constructor itself is only generated when the
8854 method in which it is defined is expanded. */
8855 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8859 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8861 current_class = TREE_TYPE (current);
8862 if (ANONYMOUS_CLASS_P (current_class))
8865 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8867 if (DECL_CONSTRUCTOR_P (d))
8869 restore_line_number_status (1);
8870 java_complete_expand_method (d);
8871 restore_line_number_status (0);
8872 break; /* There is only one constructor. */
8879 /* If we've found error at that stage, don't try to generate
8880 anything, unless we're emitting xrefs or checking the syntax only
8881 (but not using -fsyntax-only for the purpose of generating
8883 if (java_error_count && !flag_emit_xref
8884 && (!flag_syntax_only && !flag_emit_class_files))
8887 /* Now things are stable, go for generation of the class data. */
8889 /* We pessimistically marked all fields external until we knew
8890 what set of classes we were planning to compile. Now mark
8891 those that will be generated locally as not external. */
8892 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8896 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8898 tree class = TREE_TYPE (current);
8900 for (field = TYPE_FIELDS (class); field ; field = TREE_CHAIN (field))
8901 if (FIELD_STATIC (field))
8902 DECL_EXTERNAL (field) = 0;
8906 /* Compile the classes. */
8907 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8910 reversed_class_list = NULL;
8914 /* We write out the classes in reverse order. This ensures that
8915 inner classes are written before their containing classes,
8916 which is important for parallel builds. Otherwise, the
8917 class file for the outer class may be found, but the class
8918 file for the inner class may not be present. In that
8919 situation, the compiler cannot fall back to the original
8920 source, having already read the outer class, so we must
8921 prevent that situation. */
8922 for (current = ctxp->class_list;
8924 current = TREE_CHAIN (current))
8926 = tree_cons (NULL_TREE, current, reversed_class_list);
8928 for (current = reversed_class_list;
8930 current = TREE_CHAIN (current))
8932 current_class = TREE_TYPE (TREE_VALUE (current));
8933 outgoing_cpool = TYPE_CPOOL (current_class);
8934 if (flag_emit_class_files)
8935 write_classfile (current_class);
8937 expand_xref (current_class);
8938 else if (! flag_syntax_only)
8940 java_expand_method_bodies (current_class);
8947 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8948 a tree list node containing RIGHT. Fore coming RIGHTs will be
8949 chained to this hook. LOCATION contains the location of the
8950 separating `.' operator. */
8953 make_qualified_primary (tree primary, tree right, int location)
8957 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8958 wfl = build_wfl_wrap (primary, location);
8962 /* If wfl wasn't qualified, we build a first anchor */
8963 if (!EXPR_WFL_QUALIFICATION (wfl))
8964 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8967 /* And chain them */
8968 EXPR_WFL_LINECOL (right) = location;
8969 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8970 PRIMARY_P (wfl) = 1;
8974 /* Simple merge of two name separated by a `.' */
8977 merge_qualified_name (tree left, tree right)
8980 if (!left && !right)
8989 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8990 IDENTIFIER_LENGTH (left));
8991 obstack_1grow (&temporary_obstack, '.');
8992 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8993 IDENTIFIER_LENGTH (right));
8994 node = get_identifier (obstack_base (&temporary_obstack));
8995 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8996 QUALIFIED_P (node) = 1;
9000 /* Merge the two parts of a qualified name into LEFT. Set the
9001 location information of the resulting node to LOCATION, usually
9002 inherited from the location information of the `.' operator. */
9005 make_qualified_name (tree left, tree right, int location)
9007 #ifdef USE_COMPONENT_REF
9008 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
9009 EXPR_WFL_LINECOL (node) = location;
9012 tree left_id = EXPR_WFL_NODE (left);
9013 tree right_id = EXPR_WFL_NODE (right);
9016 merge = merge_qualified_name (left_id, right_id);
9018 /* Left wasn't qualified and is now qualified */
9019 if (!QUALIFIED_P (left_id))
9021 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9022 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9023 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9026 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9027 EXPR_WFL_LINECOL (wfl) = location;
9028 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9030 EXPR_WFL_NODE (left) = merge;
9035 /* Extract the last identifier component of the qualified in WFL. The
9036 last identifier is removed from the linked list */
9039 cut_identifier_in_qualified (tree wfl)
9042 tree previous = NULL_TREE;
9043 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9044 if (!TREE_CHAIN (q))
9047 /* Operating on a non qualified qualified WFL. */
9050 TREE_CHAIN (previous) = NULL_TREE;
9051 return TREE_PURPOSE (q);
9055 /* Resolve the expression name NAME. Return its decl. */
9058 resolve_expression_name (tree id, tree *orig)
9060 tree name = EXPR_WFL_NODE (id);
9063 /* 6.5.5.1: Simple expression names */
9064 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9066 /* 15.13.1: NAME can appear within the scope of a local variable
9068 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9071 /* 15.13.1: NAME can appear within a class declaration */
9074 decl = lookup_field_wrapper (current_class, name);
9077 tree access = NULL_TREE;
9078 int fs = FIELD_STATIC (decl);
9080 /* If we're accessing an outer scope local alias, make
9081 sure we change the name of the field we're going to
9083 if (FIELD_LOCAL_ALIAS_USED (decl))
9084 name = DECL_NAME (decl);
9086 /* Instance variable (8.3.1.1) can't appear within
9087 static method, static initializer or initializer for
9088 a static variable. */
9089 if (!fs && METHOD_STATIC (current_function_decl))
9091 static_ref_err (id, name, current_class);
9092 return error_mark_node;
9094 /* Instance variables can't appear as an argument of
9095 an explicit constructor invocation */
9096 if (!fs && ctxp->explicit_constructor_p
9097 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9100 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9101 return error_mark_node;
9104 /* If we're processing an inner class and we're trying
9105 to access a field belonging to an outer class, build
9106 the access to the field */
9107 if (!fs && outer_field_access_p (current_class, decl))
9109 if (CLASS_STATIC (TYPE_NAME (current_class)))
9111 static_ref_err (id, DECL_NAME (decl), current_class);
9112 return error_mark_node;
9114 access = build_outer_field_access (id, decl);
9120 /* Otherwise build what it takes to access the field */
9121 access = build_field_ref ((fs ? NULL_TREE : current_this),
9122 DECL_CONTEXT (decl), name);
9124 access = maybe_build_class_init_for_field (decl, access);
9125 /* We may be asked to save the real field access node */
9128 /* And we return what we got */
9131 /* Fall down to error report on undefined variable */
9134 /* 6.5.5.2 Qualified Expression Names */
9139 qualify_ambiguous_name (id);
9140 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9141 /* 15.10.2: Accessing Superclass Members using super */
9142 return resolve_field_access (id, orig, NULL);
9145 /* We've got an error here */
9146 if (INNER_CLASS_TYPE_P (current_class))
9147 parse_error_context (id,
9148 "Local variable `%s' can't be accessed from within the inner class `%s' unless it is declared final",
9149 IDENTIFIER_POINTER (name),
9150 IDENTIFIER_POINTER (DECL_NAME
9151 (TYPE_NAME (current_class))));
9153 parse_error_context (id, "Undefined variable `%s'",
9154 IDENTIFIER_POINTER (name));
9156 return error_mark_node;
9160 static_ref_err (tree wfl, tree field_id, tree class_type)
9164 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
9165 IDENTIFIER_POINTER (field_id),
9166 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9169 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9170 We return something suitable to generate the field access. We also
9171 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9172 recipient's address can be null. */
9175 resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9179 tree decl, where_found, type_found;
9181 if (resolve_qualified_expression_name (qual_wfl, &decl,
9182 &where_found, &type_found))
9183 return error_mark_node;
9185 /* Resolve the LENGTH field of an array here */
9186 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9187 && type_found && TYPE_ARRAY_P (type_found)
9188 && ! flag_emit_class_files && ! flag_emit_xref)
9190 tree length = build_java_array_length_access (where_found);
9193 /* In case we're dealing with a static array, we need to
9194 initialize its class before the array length can be fetched.
9195 It's also a good time to create a DECL_RTL for the field if
9196 none already exists, otherwise if the field was declared in a
9197 class found in an external file and hasn't been (and won't
9198 be) accessed for its value, none will be created. */
9199 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9201 build_static_field_ref (where_found);
9202 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9205 /* We might have been trying to resolve field.method(). In which
9206 case, the resolution is over and decl is the answer */
9207 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9209 else if (JDECL_P (decl))
9212 type_found = DECL_CONTEXT (decl);
9213 is_static = FIELD_STATIC (decl);
9214 field_ref = build_field_ref ((is_static && !flag_emit_xref?
9215 NULL_TREE : where_found),
9216 type_found, DECL_NAME (decl));
9217 if (field_ref == error_mark_node)
9218 return error_mark_node;
9220 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9228 *field_type = (QUAL_DECL_TYPE (decl) ?
9229 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9233 /* If NODE is an access to f static field, strip out the class
9234 initialization part and return the field decl, otherwise, return
9238 strip_out_static_field_access_decl (tree node)
9240 if (TREE_CODE (node) == COMPOUND_EXPR)
9242 tree op1 = TREE_OPERAND (node, 1);
9243 if (TREE_CODE (op1) == COMPOUND_EXPR)
9245 tree call = TREE_OPERAND (op1, 0);
9246 if (TREE_CODE (call) == CALL_EXPR
9247 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9248 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9249 == soft_initclass_node)
9250 return TREE_OPERAND (op1, 1);
9252 else if (JDECL_P (op1))
9258 /* 6.5.5.2: Qualified Expression Names */
9261 resolve_qualified_expression_name (tree wfl, tree *found_decl,
9262 tree *where_found, tree *type_found)
9264 int from_type = 0; /* Field search initiated from a type */
9265 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9266 int previous_call_static = 0;
9268 tree decl = NULL_TREE, type = NULL_TREE, q;
9269 /* For certain for of inner class instantiation */
9270 tree saved_current, saved_this;
9271 #define RESTORE_THIS_AND_CURRENT_CLASS \
9272 { current_class = saved_current; current_this = saved_this;}
9274 *type_found = *where_found = NULL_TREE;
9276 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9278 tree qual_wfl = QUAL_WFL (q);
9279 tree ret_decl; /* for EH checking */
9280 int location; /* for EH checking */
9282 /* 15.10.1 Field Access Using a Primary */
9283 switch (TREE_CODE (qual_wfl))
9286 case NEW_CLASS_EXPR:
9287 /* If the access to the function call is a non static field,
9288 build the code to access it. */
9289 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9291 decl = maybe_access_field (decl, *where_found,
9292 DECL_CONTEXT (decl));
9293 if (decl == error_mark_node)
9297 /* And code for the function call */
9298 if (complete_function_arguments (qual_wfl))
9301 /* We might have to setup a new current class and a new this
9302 for the search of an inner class, relative to the type of
9303 a expression resolved as `decl'. The current values are
9304 saved and restored shortly after */
9305 saved_current = current_class;
9306 saved_this = current_this;
9308 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9309 || from_qualified_this))
9311 /* If we still have `from_qualified_this', we have the form
9312 <T>.this.f() and we need to build <T>.this */
9313 if (from_qualified_this)
9315 decl = build_access_to_thisn (current_class, type, 0);
9316 decl = java_complete_tree (decl);
9317 type = TREE_TYPE (TREE_TYPE (decl));
9319 current_class = type;
9320 current_this = decl;
9321 from_qualified_this = 0;
9324 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9325 CALL_USING_SUPER (qual_wfl) = 1;
9326 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9327 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9328 *where_found = patch_method_invocation (qual_wfl, decl, type,
9330 &is_static, &ret_decl);
9332 if (*where_found == error_mark_node)
9334 RESTORE_THIS_AND_CURRENT_CLASS;
9337 *type_found = type = QUAL_DECL_TYPE (*where_found);
9339 *where_found = force_evaluation_order (*where_found);
9341 /* If we're creating an inner class instance, check for that
9342 an enclosing instance is in scope */
9343 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9344 && INNER_ENCLOSING_SCOPE_CHECK (type))
9347 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9348 lang_printable_name (type, 0),
9349 (!current_this ? "" :
9350 "; an explicit one must be provided when creating this inner class"));
9351 RESTORE_THIS_AND_CURRENT_CLASS;
9355 /* In case we had to change then to resolve a inner class
9356 instantiation using a primary qualified by a `new' */
9357 RESTORE_THIS_AND_CURRENT_CLASS;
9359 /* EH check. No check on access$<n> functions */
9361 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9362 (DECL_NAME (current_function_decl)))
9363 check_thrown_exceptions (location, ret_decl);
9365 /* If the previous call was static and this one is too,
9366 build a compound expression to hold the two (because in
9367 that case, previous function calls aren't transported as
9368 forcoming function's argument. */
9369 if (previous_call_static && is_static)
9371 decl = build (COMPOUND_EXPR, TREE_TYPE (*where_found),
9372 decl, *where_found);
9373 TREE_SIDE_EFFECTS (decl) = 1;
9377 previous_call_static = is_static;
9378 decl = *where_found;
9383 case NEW_ARRAY_EXPR:
9384 case NEW_ANONYMOUS_ARRAY_EXPR:
9385 *where_found = decl = java_complete_tree (qual_wfl);
9386 if (decl == error_mark_node)
9388 *type_found = type = QUAL_DECL_TYPE (decl);
9392 *where_found = decl = java_complete_tree (qual_wfl);
9393 if (decl == error_mark_node)
9395 *type_found = type = QUAL_DECL_TYPE (decl);
9399 case CONDITIONAL_EXPR:
9402 *where_found = decl = java_complete_tree (qual_wfl);
9403 if (decl == error_mark_node)
9405 *type_found = type = QUAL_DECL_TYPE (decl);
9409 /* If the access to the function call is a non static field,
9410 build the code to access it. */
9411 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9413 decl = maybe_access_field (decl, *where_found, type);
9414 if (decl == error_mark_node)
9417 /* And code for the array reference expression */
9418 decl = java_complete_tree (qual_wfl);
9419 if (decl == error_mark_node)
9421 type = QUAL_DECL_TYPE (decl);
9425 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9427 if ((type = patch_string (decl)))
9429 *where_found = QUAL_RESOLUTION (q) = decl;
9430 *type_found = type = TREE_TYPE (decl);
9434 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9436 *where_found = QUAL_RESOLUTION (q) = decl;
9437 *type_found = type = TREE_TYPE (decl);
9441 /* Fix for -Wall Just go to the next statement. Don't
9446 /* If we fall here, we weren't processing a (static) function call. */
9447 previous_call_static = 0;
9449 /* It can be the keyword THIS */
9450 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9451 && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9456 (wfl, "Keyword `this' used outside allowed context");
9459 if (ctxp->explicit_constructor_p
9460 && type == current_class)
9462 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9465 /* We have to generate code for intermediate access */
9466 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9468 *where_found = decl = current_this;
9469 *type_found = type = QUAL_DECL_TYPE (decl);
9471 /* We're trying to access the this from somewhere else. Make sure
9472 it's allowed before doing so. */
9475 if (!enclosing_context_p (type, current_class))
9477 char *p = xstrdup (lang_printable_name (type, 0));
9478 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9480 lang_printable_name (current_class, 0));
9484 from_qualified_this = 1;
9485 /* If there's nothing else after that, we need to
9486 produce something now, otherwise, the section of the
9487 code that needs to produce <T>.this will generate
9488 what is necessary. */
9489 if (!TREE_CHAIN (q))
9491 decl = build_access_to_thisn (current_class, type, 0);
9492 *where_found = decl = java_complete_tree (decl);
9493 *type_found = type = TREE_TYPE (decl);
9501 /* 15.10.2 Accessing Superclass Members using SUPER */
9502 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9503 && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9506 /* Check on the restricted use of SUPER */
9507 if (METHOD_STATIC (current_function_decl)
9508 || current_class == object_type_node)
9511 (wfl, "Keyword `super' used outside allowed context");
9514 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9515 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9516 CLASSTYPE_SUPER (current_class),
9517 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9518 *where_found = decl = java_complete_tree (node);
9519 if (decl == error_mark_node)
9521 *type_found = type = QUAL_DECL_TYPE (decl);
9522 from_super = from_type = 1;
9526 /* 15.13.1: Can't search for field name in packages, so we
9527 assume a variable/class name was meant. */
9528 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9531 if ((decl = resolve_package (wfl, &q, &name)))
9534 *where_found = decl;
9536 /* We want to be absolutely sure that the class is laid
9537 out. We're going to search something inside it. */
9538 *type_found = type = TREE_TYPE (decl);
9539 layout_class (type);
9542 /* Fix them all the way down, if any are left. */
9545 list = TREE_CHAIN (q);
9548 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9549 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9550 list = TREE_CHAIN (list);
9556 if (from_super || from_cast)
9558 ((from_cast ? qual_wfl : wfl),
9559 "No variable `%s' defined in class `%s'",
9560 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9561 lang_printable_name (type, 0));
9564 (qual_wfl, "Undefined variable or class name: `%s'",
9565 IDENTIFIER_POINTER (name));
9570 /* We have a type name. It's been already resolved when the
9571 expression was qualified. */
9572 else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
9574 decl = QUAL_RESOLUTION (q);
9576 /* Sneak preview. If next we see a `new', we're facing a
9577 qualification with resulted in a type being selected
9578 instead of a field. Report the error */
9580 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9582 parse_error_context (qual_wfl, "Undefined variable `%s'",
9583 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9587 if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9590 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9591 java_accstring_lookup (get_access_flags_from_decl (decl)),
9592 GET_TYPE_NAME (type),
9593 IDENTIFIER_POINTER (DECL_NAME (decl)),
9594 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9597 check_deprecation (qual_wfl, decl);
9599 type = TREE_TYPE (decl);
9602 /* We resolve an expression name */
9605 tree field_decl = NULL_TREE;
9607 /* If there exists an early resolution, use it. That occurs
9608 only once and we know that there are more things to
9609 come. Don't do that when processing something after SUPER
9610 (we need more thing to be put in place below */
9611 if (!from_super && QUAL_RESOLUTION (q))
9613 decl = QUAL_RESOLUTION (q);
9616 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9619 *where_found = current_this;
9622 static_ref_err (qual_wfl, DECL_NAME (decl),
9626 if (outer_field_access_p (current_class, decl))
9627 decl = build_outer_field_access (qual_wfl, decl);
9631 *where_found = TREE_TYPE (decl);
9632 if (TREE_CODE (*where_found) == POINTER_TYPE)
9633 *where_found = TREE_TYPE (*where_found);
9638 /* Report and error if we're using a numerical litteral as a
9639 qualifier. It can only be an INTEGER_CST. */
9640 else if (TREE_CODE (qual_wfl) == INTEGER_CST)
9643 (wfl, "Can't use type `%s' as a qualifier",
9644 lang_printable_name (TREE_TYPE (qual_wfl), 0));
9648 /* We have to search for a field, knowing the type of its
9649 container. The flag FROM_TYPE indicates that we resolved
9650 the last member of the expression as a type name, which
9651 means that for the resolution of this field, we'll look
9652 for other errors than if it was resolved as a member of
9657 tree field_decl_type; /* For layout */
9659 if (!from_type && !JREFERENCE_TYPE_P (type))
9662 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9663 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9664 lang_printable_name (type, 0),
9665 IDENTIFIER_POINTER (DECL_NAME (decl)));
9669 field_decl = lookup_field_wrapper (type,
9670 EXPR_WFL_NODE (qual_wfl));
9672 /* Maybe what we're trying to access to is an inner
9673 class, only if decl is a TYPE_DECL. */
9674 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9676 tree ptr, inner_decl;
9678 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9679 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9682 check_inner_class_access (inner_decl, decl, qual_wfl);
9683 type = TREE_TYPE (inner_decl);
9690 if (field_decl == NULL_TREE)
9693 (qual_wfl, "No variable `%s' defined in type `%s'",
9694 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9695 GET_TYPE_NAME (type));
9698 if (field_decl == error_mark_node)
9701 /* Layout the type of field_decl, since we may need
9702 it. Don't do primitive types or loaded classes. The
9703 situation of non primitive arrays may not handled
9704 properly here. FIXME */
9705 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9706 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9708 field_decl_type = TREE_TYPE (field_decl);
9709 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9710 && !CLASS_LOADED_P (field_decl_type)
9711 && !TYPE_ARRAY_P (field_decl_type))
9712 resolve_and_layout (field_decl_type, NULL_TREE);
9714 /* Check on accessibility here */
9715 if (not_accessible_p (current_class, field_decl,
9716 DECL_CONTEXT (field_decl), from_super))
9720 "Can't access %s field `%s.%s' from `%s'",
9721 java_accstring_lookup
9722 (get_access_flags_from_decl (field_decl)),
9723 GET_TYPE_NAME (type),
9724 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9726 (DECL_NAME (TYPE_NAME (current_class))));
9729 check_deprecation (qual_wfl, field_decl);
9731 /* There are things to check when fields are accessed
9732 from type. There are no restrictions on a static
9733 declaration of the field when it is accessed from an
9735 is_static = FIELD_STATIC (field_decl);
9736 if (!from_super && from_type
9737 && !TYPE_INTERFACE_P (type)
9739 && (current_function_decl
9740 && METHOD_STATIC (current_function_decl)))
9742 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9745 from_cast = from_super = 0;
9747 /* It's an access from a type but it isn't static, we
9748 make it relative to `this'. */
9749 if (!is_static && from_type)
9750 decl = current_this;
9752 /* If we need to generate something to get a proper
9753 handle on what this field is accessed from, do it
9757 decl = maybe_access_field (decl, *where_found, *type_found);
9758 if (decl == error_mark_node)
9762 /* We want to keep the location were found it, and the type
9764 *where_found = decl;
9767 /* Generate the correct expression for field access from
9769 if (from_qualified_this)
9771 field_decl = build_outer_field_access (qual_wfl, field_decl);
9772 from_qualified_this = 0;
9775 /* This is the decl found and eventually the next one to
9780 type = QUAL_DECL_TYPE (decl);
9782 /* Sneak preview. If decl is qualified by a `new', report
9783 the error here to be accurate on the peculiar construct */
9785 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9786 && !JREFERENCE_TYPE_P (type))
9788 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9789 lang_printable_name (type, 0));
9793 /* `q' might have changed due to a after package resolution
9802 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9803 can't be accessed from REFERENCE (a record type). If MEMBER
9804 features a protected access, we then use WHERE which, if non null,
9805 holds the type of MEMBER's access that is checked against
9806 6.6.2.1. This function should be used when decl is a field or a
9810 not_accessible_p (tree reference, tree member, tree where, int from_super)
9812 int access_flag = get_access_flags_from_decl (member);
9814 /* Inner classes are processed by check_inner_class_access */
9815 if (INNER_CLASS_TYPE_P (reference))
9818 /* Access always granted for members declared public */
9819 if (access_flag & ACC_PUBLIC)
9822 /* Check access on protected members */
9823 if (access_flag & ACC_PROTECTED)
9825 /* Access granted if it occurs from within the package
9826 containing the class in which the protected member is
9828 if (class_in_current_package (DECL_CONTEXT (member)))
9831 /* If accessed with the form `super.member', then access is granted */
9835 /* If where is active, access was made through a
9836 qualifier. Access is granted if the type of the qualifier is
9837 or is a sublass of the type the access made from (6.6.2.1.) */
9838 if (where && !inherits_from_p (reference, where))
9841 /* Otherwise, access is granted if occurring from the class where
9842 member is declared or a subclass of it. Find the right
9843 context to perform the check */
9844 if (PURE_INNER_CLASS_TYPE_P (reference))
9846 while (INNER_CLASS_TYPE_P (reference))
9848 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9850 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9853 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9858 /* Check access on private members. Access is granted only if it
9859 occurs from within the class in which it is declared -- that does
9860 it for innerclasses too. */
9861 if (access_flag & ACC_PRIVATE)
9863 if (reference == DECL_CONTEXT (member))
9865 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9870 /* Default access are permitted only when occurring within the
9871 package in which the type (REFERENCE) is declared. In other words,
9872 REFERENCE is defined in the current package */
9874 return !class_in_current_package (reference);
9876 /* Otherwise, access is granted */
9880 /* Test deprecated decl access. */
9882 check_deprecation (tree wfl, tree decl)
9884 const char *file = DECL_SOURCE_FILE (decl);
9885 /* Complain if the field is deprecated and the file it was defined
9886 in isn't compiled at the same time the file which contains its
9888 if (DECL_DEPRECATED (decl)
9889 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9892 switch (TREE_CODE (decl))
9895 strcpy (the, "method");
9899 strcpy (the, "field");
9902 parse_warning_context (wfl, "The class `%s' has been deprecated",
9903 IDENTIFIER_POINTER (DECL_NAME (decl)));
9908 /* Don't issue a message if the context as been deprecated as a
9910 if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
9911 parse_warning_context
9912 (wfl, "The %s `%s' in class `%s' has been deprecated",
9913 the, lang_printable_name (decl, 0),
9914 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9918 /* Returns 1 if class was declared in the current package, 0 otherwise */
9920 static GTY(()) tree cicp_cache;
9922 class_in_current_package (tree class)
9927 if (cicp_cache == class)
9930 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9932 /* If the current package is empty and the name of CLASS is
9933 qualified, class isn't in the current package. If there is a
9934 current package and the name of the CLASS is not qualified, class
9935 isn't in the current package */
9936 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9939 /* If there is not package and the name of CLASS isn't qualified,
9940 they belong to the same unnamed package */
9941 if (!ctxp->package && !qualified_flag)
9944 /* Compare the left part of the name of CLASS with the package name */
9945 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9946 if (ctxp->package == left)
9954 /* This function may generate code to access DECL from WHERE. This is
9955 done only if certain conditions meet. */
9958 maybe_access_field (tree decl, tree where, tree type)
9960 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9961 && !FIELD_STATIC (decl))
9962 decl = build_field_ref (where ? where : current_this,
9963 (type ? type : DECL_CONTEXT (decl)),
9968 /* Build a method invocation, by patching PATCH. If non NULL
9969 and according to the situation, PRIMARY and WHERE may be
9970 used. IS_STATIC is set to 1 if the invoked function is static. */
9973 patch_method_invocation (tree patch, tree primary, tree where, int from_super,
9974 int *is_static, tree *ret_decl)
9976 tree wfl = TREE_OPERAND (patch, 0);
9977 tree args = TREE_OPERAND (patch, 1);
9978 tree name = EXPR_WFL_NODE (wfl);
9980 int is_static_flag = 0;
9981 int is_super_init = 0;
9982 tree this_arg = NULL_TREE;
9983 int is_array_clone_call = 0;
9985 /* Should be overriden if everything goes well. Otherwise, if
9986 something fails, it should keep this value. It stop the
9987 evaluation of a bogus assignment. See java_complete_tree,
9988 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9989 evaluating an assignment */
9990 TREE_TYPE (patch) = error_mark_node;
9992 /* Since lookup functions are messing with line numbers, save the
9994 java_parser_context_save_global ();
9996 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9998 /* Resolution of qualified name, excluding constructors */
9999 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10001 tree identifier, identifier_wfl, type, resolved;
10002 /* Extract the last IDENTIFIER of the qualified
10003 expression. This is a wfl and we will use it's location
10004 data during error report. */
10005 identifier_wfl = cut_identifier_in_qualified (wfl);
10006 identifier = EXPR_WFL_NODE (identifier_wfl);
10008 /* Given the context, IDENTIFIER is syntactically qualified
10009 as a MethodName. We need to qualify what's before */
10010 qualify_ambiguous_name (wfl);
10011 resolved = resolve_field_access (wfl, NULL, NULL);
10013 if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10014 && FIELD_FINAL (resolved)
10015 && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10016 && !flag_emit_class_files && !flag_emit_xref)
10017 resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10019 if (resolved == error_mark_node)
10020 PATCH_METHOD_RETURN_ERROR ();
10022 type = GET_SKIP_TYPE (resolved);
10023 resolve_and_layout (type, NULL_TREE);
10025 if (JPRIMITIVE_TYPE_P (type))
10027 parse_error_context
10029 "Can't invoke a method on primitive type `%s'",
10030 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10031 PATCH_METHOD_RETURN_ERROR ();
10034 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10035 args = nreverse (args);
10037 /* We're resolving a call from a type */
10038 if (TREE_CODE (resolved) == TYPE_DECL)
10040 if (CLASS_INTERFACE (resolved))
10042 parse_error_context
10044 "Can't make static reference to method `%s' in interface `%s'",
10045 IDENTIFIER_POINTER (identifier),
10046 IDENTIFIER_POINTER (name));
10047 PATCH_METHOD_RETURN_ERROR ();
10049 if (list && !METHOD_STATIC (list))
10051 char *fct_name = xstrdup (lang_printable_name (list, 0));
10052 parse_error_context
10054 "Can't make static reference to method `%s %s' in class `%s'",
10055 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10056 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10058 PATCH_METHOD_RETURN_ERROR ();
10062 this_arg = primary = resolved;
10064 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10065 is_array_clone_call = 1;
10067 /* IDENTIFIER_WFL will be used to report any problem further */
10068 wfl = identifier_wfl;
10070 /* Resolution of simple names, names generated after a primary: or
10074 tree class_to_search = NULL_TREE;
10075 int lc; /* Looking for Constructor */
10077 /* We search constructor in their target class */
10078 if (CALL_CONSTRUCTOR_P (patch))
10080 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10081 class_to_search = EXPR_WFL_NODE (wfl);
10082 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10083 this_identifier_node)
10084 class_to_search = NULL_TREE;
10085 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10086 super_identifier_node)
10089 if (CLASSTYPE_SUPER (current_class))
10091 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10094 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10095 PATCH_METHOD_RETURN_ERROR ();
10099 /* Class to search is NULL if we're searching the current one */
10100 if (class_to_search)
10102 class_to_search = resolve_and_layout (class_to_search, wfl);
10104 if (!class_to_search)
10106 parse_error_context
10107 (wfl, "Class `%s' not found in type declaration",
10108 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10109 PATCH_METHOD_RETURN_ERROR ();
10112 /* Can't instantiate an abstract class, but we can
10113 invoke it's constructor. It's use within the `new'
10114 context is denied here. */
10115 if (CLASS_ABSTRACT (class_to_search)
10116 && TREE_CODE (patch) == NEW_CLASS_EXPR)
10118 parse_error_context
10119 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
10120 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10121 PATCH_METHOD_RETURN_ERROR ();
10124 class_to_search = TREE_TYPE (class_to_search);
10127 class_to_search = current_class;
10130 /* This is a regular search in the local class, unless an
10131 alternate class is specified. */
10134 if (where != NULL_TREE)
10135 class_to_search = where;
10136 else if (QUALIFIED_P (name))
10137 class_to_search = current_class;
10140 class_to_search = current_class;
10144 if (has_method (class_to_search, name))
10146 if (! INNER_CLASS_TYPE_P (class_to_search))
10148 parse_error_context (wfl,
10149 "No method named `%s' in scope",
10150 IDENTIFIER_POINTER (name));
10151 PATCH_METHOD_RETURN_ERROR ();
10154 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10160 /* NAME is a simple identifier or comes from a primary. Search
10161 in the class whose declaration contain the method being
10163 resolve_and_layout (class_to_search, NULL_TREE);
10165 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10166 /* Don't continue if no method were found, as the next statement
10167 can't be executed then. */
10169 PATCH_METHOD_RETURN_ERROR ();
10171 if (TYPE_ARRAY_P (class_to_search)
10172 && DECL_NAME (list) == get_identifier ("clone"))
10173 is_array_clone_call = 1;
10175 /* Check for static reference if non static methods */
10176 if (check_for_static_method_reference (wfl, patch, list,
10177 class_to_search, primary))
10178 PATCH_METHOD_RETURN_ERROR ();
10180 /* Check for inner classes creation from illegal contexts */
10181 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10182 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10183 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10184 && !DECL_INIT_P (current_function_decl))
10186 parse_error_context
10187 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
10188 lang_printable_name (class_to_search, 0),
10189 (!current_this ? "" :
10190 "; an explicit one must be provided when creating this inner class"));
10191 PATCH_METHOD_RETURN_ERROR ();
10194 /* Non static methods are called with the current object extra
10195 argument. If patch a `new TYPE()', the argument is the value
10196 returned by the object allocator. If method is resolved as a
10197 primary, use the primary otherwise use the current THIS. */
10198 args = nreverse (args);
10199 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10201 this_arg = primary ? primary : current_this;
10203 /* If we're using an access method, things are different.
10204 There are two familly of cases:
10206 1) We're not generating bytecodes:
10208 - LIST is non static. It's invocation is transformed from
10209 x(a1,...,an) into this$<n>.x(a1,....an).
10210 - LIST is static. It's invocation is transformed from
10211 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10213 2) We're generating bytecodes:
10215 - LIST is non static. It's invocation is transformed from
10216 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10217 - LIST is static. It's invocation is transformed from
10218 x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10220 Of course, this$<n> can be abitrary complex, ranging from
10221 this$0 (the immediate outer context) to
10222 access$0(access$0(...(this$0))).
10224 maybe_use_access_method returns a nonzero value if the
10225 this_arg has to be moved into the (then generated) stub
10226 argument list. In the meantime, the selected function
10227 might have be replaced by a generated stub. */
10229 maybe_use_access_method (is_super_init, &list, &this_arg))
10231 args = tree_cons (NULL_TREE, this_arg, args);
10232 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10237 /* Merge point of all resolution schemes. If we have nothing, this
10238 is an error, already signaled */
10240 PATCH_METHOD_RETURN_ERROR ();
10242 /* Check accessibility, position the is_static flag, build and
10244 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10245 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10246 NULL_TREE), from_super)
10247 /* Calls to clone() on array types are permitted as a special-case. */
10248 && !is_array_clone_call)
10250 const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10251 const char *const access =
10252 java_accstring_lookup (get_access_flags_from_decl (list));
10253 const char *const klass =
10254 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10255 const char *const refklass =
10256 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10257 const char *const what = (DECL_CONSTRUCTOR_P (list)
10258 ? "constructor" : "method");
10259 /* FIXME: WFL yields the wrong message here but I don't know
10260 what else to use. */
10261 parse_error_context (wfl,
10262 "Can't access %s %s `%s.%s' from `%s'",
10263 access, what, klass, fct_name, refklass);
10264 PATCH_METHOD_RETURN_ERROR ();
10267 /* Deprecation check: check whether the method being invoked or the
10268 instance-being-created's type are deprecated. */
10269 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10270 check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10272 check_deprecation (wfl, list);
10274 /* If invoking a innerclass constructor, there are hidden parameters
10276 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10277 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10279 /* And make sure we add the accessed local variables to be saved
10280 in field aliases. */
10281 args = build_alias_initializer_parameter_list
10282 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10284 /* Secretly pass the current_this/primary as a second argument */
10285 if (primary || current_this)
10288 tree this_type = (current_this ?
10289 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10290 /* Method's (list) enclosing context */
10291 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10292 /* If we have a primary, use it. */
10294 extra_arg = primary;
10295 /* The current `this' is an inner class but isn't a direct
10296 enclosing context for the inner class we're trying to
10297 create. Build an access to the proper enclosing context
10299 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10300 && this_type != TREE_TYPE (mec))
10303 extra_arg = build_access_to_thisn (current_class,
10304 TREE_TYPE (mec), 0);
10305 extra_arg = java_complete_tree (extra_arg);
10307 /* Otherwise, just use the current `this' as an enclosing
10310 extra_arg = current_this;
10311 args = tree_cons (NULL_TREE, extra_arg, args);
10314 args = tree_cons (NULL_TREE, integer_zero_node, args);
10317 /* This handles the situation where a constructor invocation needs
10318 to have an enclosing context passed as a second parameter (the
10319 constructor is one of an inner class). */
10320 if ((is_super_init ||
10321 (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10322 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10324 tree dest = TYPE_NAME (DECL_CONTEXT (list));
10326 build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10327 extra_arg = java_complete_tree (extra_arg);
10328 args = tree_cons (NULL_TREE, extra_arg, args);
10331 is_static_flag = METHOD_STATIC (list);
10332 if (! is_static_flag && this_arg != NULL_TREE)
10333 args = tree_cons (NULL_TREE, this_arg, args);
10335 /* In the context of an explicit constructor invocation, we can't
10336 invoke any method relying on `this'. Exceptions are: we're
10337 invoking a static function, primary exists and is not the current
10338 this, we're creating a new object. */
10339 if (ctxp->explicit_constructor_p
10341 && (!primary || primary == current_this)
10342 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10344 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10345 PATCH_METHOD_RETURN_ERROR ();
10347 java_parser_context_restore_global ();
10349 *is_static = is_static_flag;
10350 /* Sometimes, we want the decl of the selected method. Such as for
10354 patch = patch_invoke (patch, list, args);
10356 /* Now is a good time to insert the call to finit$ */
10357 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10359 tree finit_parms, finit_call;
10361 /* Prepare to pass hidden parameters to finit$, if any. */
10362 finit_parms = build_alias_initializer_parameter_list
10363 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10366 build_method_invocation (build_wfl_node (finit_identifier_node),
10369 /* Generate the code used to initialize fields declared with an
10370 initialization statement and build a compound statement along
10371 with the super constructor invocation. */
10372 CAN_COMPLETE_NORMALLY (patch) = 1;
10373 patch = build (COMPOUND_EXPR, void_type_node, patch,
10374 java_complete_tree (finit_call));
10379 /* Check that we're not trying to do a static reference to a method in
10380 non static method. Return 1 if it's the case, 0 otherwise. */
10383 check_for_static_method_reference (tree wfl, tree node, tree method,
10384 tree where, tree primary)
10386 if (METHOD_STATIC (current_function_decl)
10387 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10389 char *fct_name = xstrdup (lang_printable_name (method, 0));
10390 parse_error_context
10391 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10392 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10393 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10400 /* Fix the invocation of *MDECL if necessary in the case of a
10401 invocation from an inner class. *THIS_ARG might be modified
10402 appropriately and an alternative access to *MDECL might be
10406 maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10409 tree md = *mdecl, ta = *this_arg;
10411 int non_static_context = !METHOD_STATIC (md);
10414 || DECL_CONTEXT (md) == current_class
10415 || !PURE_INNER_CLASS_TYPE_P (current_class)
10416 || DECL_FINIT_P (md)
10417 || DECL_INSTINIT_P (md))
10420 /* If we're calling a method found in an enclosing class, generate
10421 what it takes to retrieve the right this. Don't do that if we're
10422 invoking a static method. Note that if MD's type is unrelated to
10423 CURRENT_CLASS, then the current this can be used. */
10425 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10427 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10428 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10430 ta = build_current_thisn (current_class);
10431 ta = build_wfl_node (ta);
10438 maybe_build_thisn_access_method (type);
10439 if (inherits_from_p (type, DECL_CONTEXT (md)))
10441 ta = build_access_to_thisn (ctx, type, 0);
10444 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10445 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10448 ta = java_complete_tree (ta);
10451 /* We might have to use an access method to get to MD. We can
10452 break the method access rule as far as we're not generating
10454 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10456 md = build_outer_method_access_method (md);
10463 /* Returnin a nonzero value indicates we were doing a non static
10464 method invokation that is now a static invocation. It will have
10465 callee displace `this' to insert it in the regular argument
10467 return (non_static_context && to_return);
10470 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10474 patch_invoke (tree patch, tree method, tree args)
10477 tree original_call, t, ta;
10478 tree check = NULL_TREE;
10480 /* Last step for args: convert build-in types. If we're dealing with
10481 a new TYPE() type call, the first argument to the constructor
10482 isn't found in the incoming argument list, but delivered by
10484 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10485 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10486 t = TREE_CHAIN (t);
10487 for (ta = args; t != end_params_node && ta;
10488 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10489 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10490 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10491 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10493 /* Resolve unresolved returned type isses */
10494 t = TREE_TYPE (TREE_TYPE (method));
10495 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10496 resolve_and_layout (TREE_TYPE (t), NULL);
10498 if (flag_emit_class_files || flag_emit_xref)
10502 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10504 case INVOKE_VIRTUAL:
10505 dtable = invoke_build_dtable (0, args);
10506 func = build_invokevirtual (dtable, method);
10509 case INVOKE_NONVIRTUAL:
10510 /* If the object for the method call is null, we throw an
10511 exception. We don't do this if the object is the current
10512 method's `this'. In other cases we just rely on an
10513 optimization pass to eliminate redundant checks. */
10514 if (TREE_VALUE (args) != current_this)
10516 /* We use a save_expr here to make sure we only evaluate
10517 the new `self' expression once. */
10518 tree save_arg = save_expr (TREE_VALUE (args));
10519 TREE_VALUE (args) = save_arg;
10520 check = java_check_reference (save_arg, 1);
10522 /* Fall through. */
10525 case INVOKE_STATIC:
10527 tree signature = build_java_signature (TREE_TYPE (method));
10528 func = build_known_method_ref (method, TREE_TYPE (method),
10529 DECL_CONTEXT (method),
10534 case INVOKE_INTERFACE:
10535 dtable = invoke_build_dtable (1, args);
10536 func = build_invokeinterface (dtable, method);
10543 /* Ensure self_type is initialized, (invokestatic). FIXME */
10544 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10547 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10548 TREE_OPERAND (patch, 0) = func;
10549 TREE_OPERAND (patch, 1) = args;
10550 patch = check_for_builtin (method, patch);
10551 original_call = patch;
10553 /* We're processing a `new TYPE ()' form. New is called and its
10554 returned value is the first argument to the constructor. We build
10555 a COMPOUND_EXPR and use saved expression so that the overall NEW
10556 expression value is a pointer to a newly created and initialized
10558 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10560 tree class = DECL_CONTEXT (method);
10561 tree c1, saved_new, size, new;
10564 if (flag_emit_class_files || flag_emit_xref)
10566 TREE_TYPE (patch) = build_pointer_type (class);
10569 if (!TYPE_SIZE (class))
10570 safe_layout_class (class);
10571 size = size_in_bytes (class);
10573 (class_has_finalize_method (class) ? alloc_object_node
10574 : alloc_no_finalizer_node);
10575 new = build (CALL_EXPR, promote_type (class),
10576 build_address_of (alloc_node),
10577 tree_cons (NULL_TREE, build_class_ref (class),
10578 build_tree_list (NULL_TREE,
10579 size_in_bytes (class))),
10581 saved_new = save_expr (new);
10582 c1 = build_tree_list (NULL_TREE, saved_new);
10583 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10584 TREE_OPERAND (original_call, 1) = c1;
10585 TREE_SET_CODE (original_call, CALL_EXPR);
10586 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10589 /* If CHECK is set, then we are building a check to see if the object
10591 if (check != NULL_TREE)
10593 /* We have to call force_evaluation_order now because creating a
10594 COMPOUND_EXPR wraps the arg list in a way that makes it
10595 unrecognizable by force_evaluation_order later. Yuk. */
10596 patch = build (COMPOUND_EXPR, TREE_TYPE (patch), check,
10597 force_evaluation_order (patch));
10598 TREE_SIDE_EFFECTS (patch) = 1;
10601 /* In order to be able to modify PATCH later, we SAVE_EXPR it and
10602 put it as the first expression of a COMPOUND_EXPR. The second
10603 expression being an empty statement to be later patched if
10604 necessary. We remember a TREE_LIST (the PURPOSE is the method,
10605 the VALUE is the compound) in a hashtable and return a
10606 COMPOUND_EXPR built so that the result of the evaluation of the
10607 original PATCH node is returned. */
10608 if (STATIC_CLASS_INIT_OPT_P ()
10609 && current_function_decl && METHOD_STATIC (method))
10612 tree fndecl = current_function_decl;
10613 /* We have to call force_evaluation_order now because creating a
10614 COMPOUND_EXPR wraps the arg list in a way that makes it
10615 unrecognizable by force_evaluation_order later. Yuk. */
10616 tree save = save_expr (force_evaluation_order (patch));
10617 tree type = TREE_TYPE (patch);
10619 patch = build (COMPOUND_EXPR, type, save, empty_stmt_node);
10620 list = tree_cons (method, patch,
10621 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
10623 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
10625 patch = build (COMPOUND_EXPR, type, patch, save);
10632 invocation_mode (tree method, int super)
10634 int access = get_access_flags_from_decl (method);
10637 return INVOKE_SUPER;
10639 if (access & ACC_STATIC)
10640 return INVOKE_STATIC;
10642 /* We have to look for a constructor before we handle nonvirtual
10643 calls; otherwise the constructor will look nonvirtual. */
10644 if (DECL_CONSTRUCTOR_P (method))
10645 return INVOKE_STATIC;
10647 if (access & ACC_FINAL || access & ACC_PRIVATE)
10648 return INVOKE_NONVIRTUAL;
10650 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10651 return INVOKE_NONVIRTUAL;
10653 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10654 return INVOKE_INTERFACE;
10656 return INVOKE_VIRTUAL;
10659 /* Retrieve a refined list of matching methods. It covers the step
10660 15.11.2 (Compile-Time Step 2) */
10663 lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
10665 tree atl = end_params_node; /* Arg Type List */
10666 tree method, signature, list, node;
10667 const char *candidates; /* Used for error report */
10670 /* Fix the arguments */
10671 for (node = arg_list; node; node = TREE_CHAIN (node))
10673 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10674 /* Non primitive type may have to be resolved */
10675 if (!JPRIMITIVE_TYPE_P (current_arg))
10676 resolve_and_layout (current_arg, NULL_TREE);
10678 if (TREE_CODE (current_arg) == RECORD_TYPE)
10679 current_arg = promote_type (current_arg);
10680 atl = tree_cons (NULL_TREE, current_arg, atl);
10683 /* Presto. If we're dealing with an anonymous class and a
10684 constructor call, generate the right constructor now, since we
10685 know the arguments' types. */
10687 if (lc && ANONYMOUS_CLASS_P (class))
10689 tree saved_current_class;
10690 tree mdecl = craft_constructor (TYPE_NAME (class), atl);
10691 saved_current_class = current_class;
10692 current_class = class;
10693 fix_constructors (mdecl);
10694 current_class = saved_current_class;
10697 /* Find all candidates and then refine the list, searching for the
10698 most specific method. */
10699 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10700 list = find_most_specific_methods_list (list);
10701 if (list && !TREE_CHAIN (list))
10702 return TREE_VALUE (list);
10704 /* Issue an error. List candidates if any. Candidates are listed
10705 only if accessible (non accessible methods may end-up here for
10706 the sake of a better error report). */
10711 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10712 for (current = list; current; current = TREE_CHAIN (current))
10714 tree cm = TREE_VALUE (current);
10715 char string [4096];
10716 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10719 (string, " `%s' in `%s'%s",
10720 get_printable_method_name (cm),
10721 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10722 (TREE_CHAIN (current) ? "\n" : ""));
10723 obstack_grow (&temporary_obstack, string, strlen (string));
10725 obstack_1grow (&temporary_obstack, '\0');
10726 candidates = obstack_finish (&temporary_obstack);
10728 /* Issue the error message */
10729 method = make_node (FUNCTION_TYPE);
10730 TYPE_ARG_TYPES (method) = atl;
10731 signature = build_java_argument_signature (method);
10732 dup = xstrdup (lang_printable_name (class, 0));
10733 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10734 (lc ? "constructor" : "method"),
10735 (lc ? dup : IDENTIFIER_POINTER (name)),
10736 IDENTIFIER_POINTER (signature), dup,
10737 (candidates ? candidates : ""));
10742 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10743 when we're looking for a constructor. */
10746 find_applicable_accessible_methods_list (int lc, tree class, tree name,
10749 static htab_t searched_classes;
10750 static int search_not_done = 0;
10751 tree list = NULL_TREE, all_list = NULL_TREE;
10753 /* Check the hash table to determine if this class has been searched
10755 if (searched_classes)
10757 if (htab_find (searched_classes, class) != NULL)
10762 searched_classes = htab_create (10, htab_hash_pointer,
10763 htab_eq_pointer, NULL);
10767 *htab_find_slot (searched_classes, class, INSERT) = class;
10769 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10771 load_class (class, 1);
10772 safe_layout_class (class);
10775 /* Search interfaces */
10776 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10777 && CLASS_INTERFACE (TYPE_NAME (class)))
10780 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10781 search_applicable_methods_list (lc, TYPE_METHODS (class),
10782 name, arglist, &list, &all_list);
10783 n = TREE_VEC_LENGTH (basetype_vec);
10784 for (i = 1; i < n; i++)
10786 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10789 rlist = find_applicable_accessible_methods_list (lc, t, name,
10791 list = chainon (rlist, list);
10794 /* Search classes */
10797 search_applicable_methods_list (lc, TYPE_METHODS (class),
10798 name, arglist, &list, &all_list);
10800 /* When looking finit$, class$ or instinit$, we turn LC to 1 so
10801 that we only search in class. Note that we should have found
10802 something at this point. */
10803 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
10810 /* We must search all interfaces of this class */
10813 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10814 int n = TREE_VEC_LENGTH (basetype_vec), i;
10815 for (i = 1; i < n; i++)
10817 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10818 if (t != object_type_node)
10821 = find_applicable_accessible_methods_list (lc, t,
10823 list = chainon (rlist, list);
10828 /* Search superclass */
10829 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10832 class = CLASSTYPE_SUPER (class);
10833 rlist = find_applicable_accessible_methods_list (lc, class,
10835 list = chainon (rlist, list);
10841 /* We're done. Reset the searched classes list and finally search
10842 java.lang.Object if it wasn't searched already. */
10843 if (!search_not_done)
10846 && TYPE_METHODS (object_type_node)
10847 && htab_find (searched_classes, object_type_node) == NULL)
10849 search_applicable_methods_list (lc,
10850 TYPE_METHODS (object_type_node),
10851 name, arglist, &list, &all_list);
10853 htab_delete (searched_classes);
10854 searched_classes = NULL;
10857 /* Either return the list obtained or all selected (but
10858 inaccessible) methods for better error report. */
10859 return (!list ? all_list : list);
10862 /* Effectively search for the appropriate method in method */
10865 search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
10866 tree *list, tree *all_list)
10868 for (; method; method = TREE_CHAIN (method))
10870 /* When dealing with constructor, stop here, otherwise search
10872 if (lc && !DECL_CONSTRUCTOR_P (method))
10874 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10875 || (DECL_NAME (method) != name)))
10878 if (argument_types_convertible (method, arglist))
10880 /* Retain accessible methods only */
10881 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10882 method, NULL_TREE, 0))
10883 *list = tree_cons (NULL_TREE, method, *list);
10885 /* Also retain all selected method here */
10886 *all_list = tree_cons (NULL_TREE, method, *list);
10891 /* 15.11.2.2 Choose the Most Specific Method */
10894 find_most_specific_methods_list (tree list)
10897 int abstract, candidates;
10898 tree current, new_list = NULL_TREE;
10899 for (current = list; current; current = TREE_CHAIN (current))
10902 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10904 for (method = list; method; method = TREE_CHAIN (method))
10906 tree method_v, current_v;
10907 /* Don't test a method against itself */
10908 if (method == current)
10911 method_v = TREE_VALUE (method);
10912 current_v = TREE_VALUE (current);
10914 /* Compare arguments and location where methods where declared */
10915 if (argument_types_convertible (method_v, current_v))
10917 if (valid_method_invocation_conversion_p
10918 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10919 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10920 && enclosing_context_p (DECL_CONTEXT (method_v),
10921 DECL_CONTEXT (current_v))))
10923 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10924 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10925 max = (v > max ? v : max);
10931 /* Review the list and select the maximally specific methods */
10932 for (current = list, abstract = -1, candidates = -1;
10933 current; current = TREE_CHAIN (current))
10934 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10936 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10937 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10941 /* If we have several and they're all abstract, just pick the
10943 if (candidates > 0 && (candidates == abstract))
10945 new_list = nreverse (new_list);
10946 TREE_CHAIN (new_list) = NULL_TREE;
10949 /* We have several (we couldn't find a most specific), all but one
10950 are abstract, we pick the only non abstract one. */
10951 if (candidates > 0 && (candidates == abstract+1))
10953 for (current = new_list; current; current = TREE_CHAIN (current))
10954 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10956 TREE_CHAIN (current) = NULL_TREE;
10957 new_list = current;
10961 /* If we can't find one, lower expectations and try to gather multiple
10962 maximally specific methods */
10963 while (!new_list && max)
10967 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10968 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10975 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10976 converted by method invocation conversion (5.3) to the type of the
10977 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10978 to change less often than M1. */
10980 static GTY(()) tree m2_arg_value;
10981 static GTY(()) tree m2_arg_cache;
10984 argument_types_convertible (tree m1, tree m2_or_arglist)
10986 register tree m1_arg, m2_arg;
10988 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10990 if (m2_arg_value == m2_or_arglist)
10991 m2_arg = m2_arg_cache;
10994 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10996 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10998 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10999 if (!METHOD_STATIC (m2_or_arglist))
11000 m2_arg = TREE_CHAIN (m2_arg);
11003 m2_arg = m2_or_arglist;
11005 m2_arg_value = m2_or_arglist;
11006 m2_arg_cache = m2_arg;
11009 while (m1_arg != end_params_node && m2_arg != end_params_node)
11011 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11012 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11013 TREE_VALUE (m2_arg)))
11015 m1_arg = TREE_CHAIN (m1_arg);
11016 m2_arg = TREE_CHAIN (m2_arg);
11018 return m1_arg == end_params_node && m2_arg == end_params_node;
11021 /* Qualification routines */
11024 qualify_ambiguous_name (tree id)
11026 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
11027 saved_current_class;
11028 int again, super_found = 0, this_found = 0, new_array_found = 0;
11031 /* We first qualify the first element, then derive qualification of
11032 others based on the first one. If the first element is qualified
11033 by a resolution (field or type), this resolution is stored in the
11034 QUAL_RESOLUTION of the qual element being examined. We need to
11035 save the current_class since the use of SUPER might change the
11037 saved_current_class = current_class;
11038 qual = EXPR_WFL_QUALIFICATION (id);
11041 /* Simple qualified expression feature a qual_wfl that is a
11042 WFL. Expression derived from a primary feature more complicated
11043 things like a CALL_EXPR. Expression from primary need to be
11044 worked out to extract the part on which the qualification will
11046 qual_wfl = QUAL_WFL (qual);
11047 switch (TREE_CODE (qual_wfl))
11050 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11051 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION
11052 || (EXPR_WFL_QUALIFICATION (qual_wfl)
11053 && TREE_CODE (EXPR_WFL_QUALIFICATION (qual_wfl)) == TREE_LIST))
11055 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11056 qual_wfl = QUAL_WFL (qual);
11059 case NEW_ARRAY_EXPR:
11060 case NEW_ANONYMOUS_ARRAY_EXPR:
11061 qual = TREE_CHAIN (qual);
11062 again = new_array_found = 1;
11066 case NEW_CLASS_EXPR:
11067 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11070 while (TREE_CODE (qual_wfl) == ARRAY_REF)
11071 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11074 qual = TREE_CHAIN (qual);
11075 qual_wfl = QUAL_WFL (qual);
11077 case CLASS_LITERAL:
11078 qual = TREE_CHAIN (qual);
11079 qual_wfl = QUAL_WFL (qual);
11082 /* Fix for -Wall. Just break doing nothing */
11086 ptr_type = current_class;
11088 code = TREE_CODE (qual_wfl);
11090 /* Pos evaluation: non WFL leading expression nodes */
11091 if (code == CONVERT_EXPR
11092 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
11093 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
11095 else if (code == INTEGER_CST)
11098 else if (code == CONVERT_EXPR &&
11099 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11100 name = TREE_OPERAND (qual_wfl, 0);
11102 else if (code == CONVERT_EXPR
11103 && TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == CALL_EXPR
11104 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (qual_wfl, 0), 0))
11105 == EXPR_WITH_FILE_LOCATION))
11106 name = TREE_OPERAND (TREE_OPERAND (qual_wfl, 0), 0);
11108 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
11109 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11110 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
11112 else if (code == TREE_LIST)
11113 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
11115 else if (code == STRING_CST || code == CONDITIONAL_EXPR
11116 || code == PLUS_EXPR)
11118 qual = TREE_CHAIN (qual);
11119 qual_wfl = QUAL_WFL (qual);
11124 name = EXPR_WFL_NODE (qual_wfl);
11127 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11132 /* If we have a THIS (from a primary), we set the context accordingly */
11133 if (name == this_identifier_node)
11135 /* This isn't really elegant. One more added irregularity
11136 before I start using COMPONENT_REF (hopefully very soon.) */
11137 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
11138 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11139 EXPR_WITH_FILE_LOCATION
11140 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11141 this_identifier_node)
11143 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
11144 qual = EXPR_WFL_QUALIFICATION (qual);
11146 qual = TREE_CHAIN (qual);
11147 qual_wfl = QUAL_WFL (qual);
11148 if (TREE_CODE (qual_wfl) == CALL_EXPR)
11150 else if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION)
11151 name = EXPR_WFL_NODE (qual_wfl);
11152 else if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
11153 name = TREE_OPERAND (qual_wfl, 0);
11156 /* If we have a SUPER, we set the context accordingly */
11157 if (name == super_identifier_node)
11159 current_class = CLASSTYPE_SUPER (ptr_type);
11160 /* Check that there is such a thing as a super class. If not,
11161 return. The error will be caught later on, during the
11163 if (!current_class)
11165 current_class = saved_current_class;
11168 qual = TREE_CHAIN (qual);
11169 /* Do one more interation to set things up */
11170 super_found = again = 1;
11174 /* If name appears within the scope of a local variable declaration
11175 or parameter declaration, then it is an expression name. We don't
11176 carry this test out if we're in the context of the use of SUPER
11178 if (!this_found && !super_found
11179 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
11180 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
11182 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11183 QUAL_RESOLUTION (qual) = decl;
11186 /* If within the class/interface NAME was found to be used there
11187 exists a (possibly inherited) field named NAME, then this is an
11188 expression name. If we saw a NEW_ARRAY_EXPR before and want to
11189 address length, it is OK. */
11190 else if ((decl = lookup_field_wrapper (ptr_type, name))
11191 || name == length_identifier_node)
11193 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11194 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
11197 /* We reclassify NAME as yielding to a type name resolution if:
11198 - NAME is a class/interface declared within the compilation
11199 unit containing NAME,
11200 - NAME is imported via a single-type-import declaration,
11201 - NAME is declared in an another compilation unit of the package
11202 of the compilation unit containing NAME,
11203 - NAME is declared by exactly on type-import-on-demand declaration
11204 of the compilation unit containing NAME.
11205 - NAME is actually a STRING_CST.
11206 This can't happen if the expression was qualified by `this.' */
11207 else if (! this_found &&
11208 (TREE_CODE (name) == STRING_CST ||
11209 TREE_CODE (name) == INTEGER_CST ||
11210 (decl = resolve_and_layout (name, NULL_TREE))))
11212 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11213 QUAL_RESOLUTION (qual) = decl;
11216 /* Method call, array references and cast are expression name */
11217 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
11218 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
11219 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR
11220 || TREE_CODE (QUAL_WFL (qual)) == MODIFY_EXPR)
11221 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11223 /* Check here that NAME isn't declared by more than one
11224 type-import-on-demand declaration of the compilation unit
11225 containing NAME. FIXME */
11227 /* Otherwise, NAME is reclassified as a package name */
11229 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11231 /* Propagate the qualification accross other components of the
11233 for (qual = TREE_CHAIN (qual); qual;
11234 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11236 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11237 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11239 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
11242 /* Store the global qualification for the ambiguous part of ID back
11244 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
11245 RESOLVE_EXPRESSION_NAME_P (id) = 1;
11246 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
11247 RESOLVE_TYPE_NAME_P (id) = 1;
11248 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11249 RESOLVE_PACKAGE_NAME_P (id) = 1;
11251 /* Restore the current class */
11252 current_class = saved_current_class;
11256 breakdown_qualified (tree *left, tree *right, tree source)
11259 int l = IDENTIFIER_LENGTH (source);
11261 base = alloca (l + 1);
11262 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
11264 /* Breakdown NAME into REMAINDER . IDENTIFIER. */
11266 while (*p != '.' && p != base)
11269 /* We didn't find a '.'. Return an error. */
11275 *right = get_identifier (p+1);
11276 *left = get_identifier (base);
11281 /* Return TRUE if two classes are from the same package. */
11284 in_same_package (tree name1, tree name2)
11290 if (TREE_CODE (name1) == TYPE_DECL)
11291 name1 = DECL_NAME (name1);
11292 if (TREE_CODE (name2) == TYPE_DECL)
11293 name2 = DECL_NAME (name2);
11295 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11296 /* One in empty package. */
11299 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11300 /* Both in empty package. */
11303 breakdown_qualified (&pkg1, &tmp, name1);
11304 breakdown_qualified (&pkg2, &tmp, name2);
11306 return (pkg1 == pkg2);
11309 /* Patch tree nodes in a function body. When a BLOCK is found, push
11310 local variable decls if present.
11311 Same as java_complete_lhs, but does resolve static finals to values. */
11314 java_complete_tree (tree node)
11316 node = java_complete_lhs (node);
11317 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11318 && DECL_INITIAL (node) != NULL_TREE
11319 && !flag_emit_xref)
11321 tree value = fold_constant_for_init (node, node);
11322 if (value != NULL_TREE)
11329 java_stabilize_reference (tree node)
11331 if (TREE_CODE (node) == COMPOUND_EXPR)
11333 tree op0 = TREE_OPERAND (node, 0);
11334 tree op1 = TREE_OPERAND (node, 1);
11335 TREE_OPERAND (node, 0) = save_expr (op0);
11336 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11339 return stabilize_reference (node);
11342 /* Patch tree nodes in a function body. When a BLOCK is found, push
11343 local variable decls if present.
11344 Same as java_complete_tree, but does not resolve static finals to values. */
11347 java_complete_lhs (tree node)
11349 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11352 /* CONVERT_EXPR always has its type set, even though it needs to be
11354 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11357 /* The switch block implements cases processing container nodes
11358 first. Contained nodes are always written back. Leaves come
11359 next and return a value. */
11360 switch (TREE_CODE (node))
11364 /* 1- Block section.
11365 Set the local values on decl names so we can identify them
11366 faster when they're referenced. At that stage, identifiers
11367 are legal so we don't check for declaration errors. */
11368 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11370 DECL_CONTEXT (cn) = current_function_decl;
11371 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11373 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11374 CAN_COMPLETE_NORMALLY (node) = 1;
11377 tree stmt = BLOCK_EXPR_BODY (node);
11379 int error_seen = 0;
11380 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11382 /* Re-order from (((A; B); C); ...; Z) to
11383 (A; (B; (C ; (...; Z)))).
11384 This makes it easier to scan the statements left-to-right
11385 without using recursion (which might overflow the stack
11386 if the block has many statements. */
11389 tree left = TREE_OPERAND (stmt, 0);
11390 if (TREE_CODE (left) != COMPOUND_EXPR)
11392 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11393 TREE_OPERAND (left, 1) = stmt;
11396 BLOCK_EXPR_BODY (node) = stmt;
11399 /* Now do the actual complete, without deep recursion for
11401 ptr = &BLOCK_EXPR_BODY (node);
11402 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11403 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11405 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11406 tree *next = &TREE_OPERAND (*ptr, 1);
11407 TREE_OPERAND (*ptr, 0) = cur;
11408 if (cur == empty_stmt_node)
11410 /* Optimization; makes it easier to detect empty bodies.
11411 Most useful for <clinit> with all-constant initializer. */
11415 if (TREE_CODE (cur) == ERROR_MARK)
11417 else if (! CAN_COMPLETE_NORMALLY (cur))
11422 if (TREE_CODE (wfl_op2) == BLOCK)
11423 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11424 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11425 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11429 if (TREE_CODE (wfl_op2) != CASE_EXPR
11430 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11431 unreachable_stmt_error (*ptr);
11435 *ptr = java_complete_tree (*ptr);
11437 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11438 return error_mark_node;
11439 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11441 /* Turn local bindings to null */
11442 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11443 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11445 TREE_TYPE (node) = void_type_node;
11448 /* 2- They are expressions but ultimately deal with statements */
11451 wfl_op1 = TREE_OPERAND (node, 0);
11452 COMPLETE_CHECK_OP_0 (node);
11453 /* 14.19 A throw statement cannot complete normally. */
11454 CAN_COMPLETE_NORMALLY (node) = 0;
11455 return patch_throw_statement (node, wfl_op1);
11457 case SYNCHRONIZED_EXPR:
11458 wfl_op1 = TREE_OPERAND (node, 0);
11459 return patch_synchronized_statement (node, wfl_op1);
11462 return patch_try_statement (node);
11464 case TRY_FINALLY_EXPR:
11465 COMPLETE_CHECK_OP_0 (node);
11466 COMPLETE_CHECK_OP_1 (node);
11467 if (TREE_OPERAND (node, 0) == empty_stmt_node)
11468 return TREE_OPERAND (node, 1);
11469 if (TREE_OPERAND (node, 1) == empty_stmt_node)
11470 return TREE_OPERAND (node, 0);
11471 CAN_COMPLETE_NORMALLY (node)
11472 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11473 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11474 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11477 case LABELED_BLOCK_EXPR:
11478 PUSH_LABELED_BLOCK (node);
11479 if (LABELED_BLOCK_BODY (node))
11480 COMPLETE_CHECK_OP_1 (node);
11481 TREE_TYPE (node) = void_type_node;
11482 POP_LABELED_BLOCK ();
11484 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11486 LABELED_BLOCK_BODY (node) = NULL_TREE;
11487 CAN_COMPLETE_NORMALLY (node) = 1;
11489 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11490 CAN_COMPLETE_NORMALLY (node) = 1;
11493 case EXIT_BLOCK_EXPR:
11494 /* We don't complete operand 1, because it's the return value of
11495 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11496 return patch_bc_statement (node);
11499 cn = java_complete_tree (TREE_OPERAND (node, 0));
11500 if (cn == error_mark_node)
11503 /* First, the case expression must be constant. Values of final
11504 fields are accepted. */
11506 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11507 && JDECL_P (TREE_OPERAND (cn, 1))
11508 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11509 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11511 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11512 TREE_OPERAND (cn, 1));
11514 /* Accept final locals too. */
11515 else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn))
11516 cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11518 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11520 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11521 parse_error_context (node, "Constant expression required");
11522 return error_mark_node;
11525 nn = ctxp->current_loop;
11527 /* It must be assignable to the type of the switch expression. */
11528 if (!try_builtin_assignconv (NULL_TREE,
11529 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11531 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11532 parse_error_context
11534 "Incompatible type for case. Can't convert `%s' to `int'",
11535 lang_printable_name (TREE_TYPE (cn), 0));
11536 return error_mark_node;
11539 cn = fold (convert (int_type_node, cn));
11540 TREE_CONSTANT_OVERFLOW (cn) = 0;
11541 CAN_COMPLETE_NORMALLY (cn) = 1;
11543 /* Save the label on a list so that we can later check for
11545 case_label_list = tree_cons (node, cn, case_label_list);
11547 /* Multiple instance of a case label bearing the same value is
11548 checked later. The case expression is all right so far. */
11549 if (TREE_CODE (cn) == VAR_DECL)
11550 cn = DECL_INITIAL (cn);
11551 TREE_OPERAND (node, 0) = cn;
11552 TREE_TYPE (node) = void_type_node;
11553 CAN_COMPLETE_NORMALLY (node) = 1;
11554 TREE_SIDE_EFFECTS (node) = 1;
11558 nn = ctxp->current_loop;
11559 /* Only one default label is allowed per switch statement */
11560 if (SWITCH_HAS_DEFAULT (nn))
11562 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11563 parse_error_context (wfl_operator,
11564 "Duplicate case label: `default'");
11565 return error_mark_node;
11568 SWITCH_HAS_DEFAULT (nn) = 1;
11569 TREE_TYPE (node) = void_type_node;
11570 TREE_SIDE_EFFECTS (node) = 1;
11571 CAN_COMPLETE_NORMALLY (node) = 1;
11577 /* Check whether the loop was enclosed in a labeled
11578 statement. If not, create one, insert the loop in it and
11580 nn = patch_loop_statement (node);
11582 /* Anyways, walk the body of the loop */
11583 if (TREE_CODE (node) == LOOP_EXPR)
11584 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11585 /* Switch statement: walk the switch expression and the cases */
11587 node = patch_switch_statement (node);
11589 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11590 nn = error_mark_node;
11593 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11594 /* If we returned something different, that's because we
11595 inserted a label. Pop the label too. */
11598 if (CAN_COMPLETE_NORMALLY (node))
11599 CAN_COMPLETE_NORMALLY (nn) = 1;
11600 POP_LABELED_BLOCK ();
11607 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11608 return patch_exit_expr (node);
11612 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11613 if (TREE_OPERAND (node, 0) == error_mark_node)
11614 return error_mark_node;
11615 /* then-else branches */
11616 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11617 if (TREE_OPERAND (node, 1) == error_mark_node)
11618 return error_mark_node;
11619 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11620 if (TREE_OPERAND (node, 2) == error_mark_node)
11621 return error_mark_node;
11622 return patch_if_else_statement (node);
11625 case CONDITIONAL_EXPR:
11627 wfl_op1 = TREE_OPERAND (node, 0);
11628 COMPLETE_CHECK_OP_0 (node);
11629 wfl_op2 = TREE_OPERAND (node, 1);
11630 COMPLETE_CHECK_OP_1 (node);
11631 wfl_op3 = TREE_OPERAND (node, 2);
11632 COMPLETE_CHECK_OP_2 (node);
11633 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11635 /* 3- Expression section */
11636 case COMPOUND_EXPR:
11637 wfl_op2 = TREE_OPERAND (node, 1);
11638 TREE_OPERAND (node, 0) = nn =
11639 java_complete_tree (TREE_OPERAND (node, 0));
11640 if (wfl_op2 == empty_stmt_node)
11641 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11644 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11646 /* An unreachable condition in a do-while statement
11647 is *not* (technically) an unreachable statement. */
11649 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11650 nn = EXPR_WFL_NODE (nn);
11651 if (TREE_CODE (nn) != EXIT_EXPR)
11653 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11654 if (SUPPRESS_UNREACHABLE_ERROR (nn))
11656 /* Perhaps this warning should have an
11657 associated flag. The code being compiled is
11658 pedantically correct, but useless. */
11659 parse_warning_context (wfl_operator,
11660 "Unreachable statement");
11663 parse_error_context (wfl_operator,
11664 "Unreachable statement");
11667 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11668 if (TREE_OPERAND (node, 1) == error_mark_node)
11669 return error_mark_node;
11670 /* Even though we might allow the case where the first
11671 operand doesn't return normally, we still should compute
11672 CAN_COMPLETE_NORMALLY correctly. */
11673 CAN_COMPLETE_NORMALLY (node)
11674 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11675 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11677 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11681 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11682 return patch_return (node);
11684 case EXPR_WITH_FILE_LOCATION:
11685 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11686 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11689 node = resolve_expression_name (node, NULL);
11690 if (node == error_mark_node)
11692 /* Keep line number information somewhere were it doesn't
11693 disrupt the completion process. */
11694 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11696 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11697 TREE_OPERAND (node, 1) = wfl;
11699 CAN_COMPLETE_NORMALLY (node) = 1;
11704 int save_lineno = lineno;
11705 lineno = EXPR_WFL_LINENO (node);
11706 body = java_complete_tree (EXPR_WFL_NODE (node));
11707 lineno = save_lineno;
11708 EXPR_WFL_NODE (node) = body;
11709 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11710 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11711 if (body == empty_stmt_node || TREE_CONSTANT (body))
11713 /* Makes it easier to constant fold, detect empty bodies. */
11716 if (body == error_mark_node)
11718 /* Its important for the evaluation of assignment that
11719 this mark on the TREE_TYPE is propagated. */
11720 TREE_TYPE (node) = error_mark_node;
11721 return error_mark_node;
11724 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11729 case NEW_ARRAY_EXPR:
11730 /* Patch all the dimensions */
11732 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11734 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11735 tree dim = convert (int_type_node,
11736 java_complete_tree (TREE_VALUE (cn)));
11737 if (dim == error_mark_node)
11744 TREE_VALUE (cn) = dim;
11745 /* Setup the location of the current dimension, for
11746 later error report. */
11747 TREE_PURPOSE (cn) =
11748 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11749 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11752 /* They complete the array creation expression, if no errors
11754 CAN_COMPLETE_NORMALLY (node) = 1;
11755 return (flag ? error_mark_node
11756 : force_evaluation_order (patch_newarray (node)));
11758 case NEW_ANONYMOUS_ARRAY_EXPR:
11759 /* Create the array type if necessary. */
11760 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11762 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11763 if (!(type = resolve_type_during_patch (type)))
11764 return error_mark_node;
11765 type = build_array_from_name (type, NULL_TREE,
11766 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11767 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11769 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11770 ANONYMOUS_ARRAY_INITIALIZER (node));
11771 if (node == error_mark_node)
11772 return error_mark_node;
11773 CAN_COMPLETE_NORMALLY (node) = 1;
11776 case NEW_CLASS_EXPR:
11778 /* Complete function's argument(s) first */
11779 if (complete_function_arguments (node))
11780 return error_mark_node;
11783 tree decl, wfl = TREE_OPERAND (node, 0);
11784 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11785 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11786 super_identifier_node);
11788 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11789 from_super, 0, &decl);
11790 if (node == error_mark_node)
11791 return error_mark_node;
11793 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11794 /* If we call this(...), register signature and positions */
11796 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11797 tree_cons (wfl, decl,
11798 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11799 CAN_COMPLETE_NORMALLY (node) = 1;
11800 return force_evaluation_order (node);
11804 /* Save potential wfls */
11805 wfl_op1 = TREE_OPERAND (node, 0);
11806 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11808 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11809 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11810 && DECL_INITIAL (nn) != NULL_TREE)
11814 value = fold_constant_for_init (nn, nn);
11816 /* When we have a primitype type, or a string and we're not
11817 emitting a class file, we actually don't want to generate
11818 anything for the assignment. */
11819 if (value != NULL_TREE &&
11820 (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
11821 (TREE_TYPE (value) == string_ptr_type_node &&
11822 ! flag_emit_class_files)))
11824 /* Prepare node for patch_assignment */
11825 TREE_OPERAND (node, 1) = value;
11826 /* Call patch assignment to verify the assignment */
11827 if (patch_assignment (node, wfl_op1) == error_mark_node)
11828 return error_mark_node;
11829 /* Set DECL_INITIAL properly (a conversion might have
11830 been decided by patch_assignment) and return the
11831 empty statement. */
11834 tree patched = patch_string (TREE_OPERAND (node, 1));
11836 DECL_INITIAL (nn) = patched;
11838 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
11839 DECL_FIELD_FINAL_IUD (nn) = 1;
11840 return empty_stmt_node;
11843 if (! flag_emit_class_files)
11844 DECL_INITIAL (nn) = NULL_TREE;
11846 wfl_op2 = TREE_OPERAND (node, 1);
11848 if (TREE_OPERAND (node, 0) == error_mark_node)
11849 return error_mark_node;
11851 flag = COMPOUND_ASSIGN_P (wfl_op2);
11854 /* This might break when accessing outer field from inner
11855 class. TESTME, FIXME */
11856 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11858 /* Hand stabilize the lhs on both places */
11859 TREE_OPERAND (node, 0) = lvalue;
11860 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11861 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11863 /* 15.25.2.a: Left hand is not an array access. FIXME */
11864 /* Now complete the RHS. We write it back later on. */
11865 nn = java_complete_tree (TREE_OPERAND (node, 1));
11867 if ((cn = patch_string (nn)))
11870 /* The last part of the rewrite for E1 op= E2 is to have
11871 E1 = (T)(E1 op E2), with T being the type of E1. */
11872 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11873 TREE_TYPE (lvalue), nn));
11875 /* If the assignment is compound and has reference type,
11876 then ensure the LHS has type String and nothing else. */
11877 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11878 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11879 parse_error_context (wfl_op2,
11880 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11881 lang_printable_name (TREE_TYPE (lvalue), 0));
11883 /* 15.25.2.b: Left hand is an array access. FIXME */
11886 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11887 function to complete this RHS. Note that a NEW_ARRAY_INIT
11888 might have been already fully expanded if created as a result
11889 of processing an anonymous array initializer. We avoid doing
11890 the operation twice by testing whether the node already bears
11892 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11893 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11894 TREE_OPERAND (node, 1));
11895 /* Otherwise we simply complete the RHS */
11897 nn = java_complete_tree (TREE_OPERAND (node, 1));
11899 if (nn == error_mark_node)
11900 return error_mark_node;
11902 /* Write back the RHS as we evaluated it. */
11903 TREE_OPERAND (node, 1) = nn;
11905 /* In case we're handling = with a String as a RHS, we need to
11906 produce a String out of the RHS (it might still be a
11907 STRING_CST or a StringBuffer at this stage */
11908 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11909 TREE_OPERAND (node, 1) = nn;
11911 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11912 TREE_OPERAND (node, 1))))
11914 /* We return error_mark_node if outer_field_access_fix
11915 detects we write into a final. */
11916 if (nn == error_mark_node)
11917 return error_mark_node;
11922 node = patch_assignment (node, wfl_op1);
11923 if (node == error_mark_node)
11924 return error_mark_node;
11925 /* Reorganize the tree if necessary. */
11926 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11927 || JSTRING_P (TREE_TYPE (node))))
11928 node = java_refold (node);
11931 /* Seek to set DECL_INITIAL to a proper value, since it might have
11932 undergone a conversion in patch_assignment. We do that only when
11933 it's necessary to have DECL_INITIAL properly set. */
11934 nn = TREE_OPERAND (node, 0);
11935 if (TREE_CODE (nn) == VAR_DECL
11936 && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
11937 && FIELD_STATIC (nn) && FIELD_FINAL (nn)
11938 && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
11939 || TREE_TYPE (nn) == string_ptr_type_node))
11940 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
11942 CAN_COMPLETE_NORMALLY (node) = 1;
11954 case TRUNC_MOD_EXPR:
11955 case TRUNC_DIV_EXPR:
11957 case TRUTH_ANDIF_EXPR:
11958 case TRUTH_ORIF_EXPR:
11965 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11966 knows how to handle those cases. */
11967 wfl_op1 = TREE_OPERAND (node, 0);
11968 wfl_op2 = TREE_OPERAND (node, 1);
11970 CAN_COMPLETE_NORMALLY (node) = 1;
11971 /* Don't complete string nodes if dealing with the PLUS operand. */
11972 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11974 nn = java_complete_tree (wfl_op1);
11975 if (nn == error_mark_node)
11976 return error_mark_node;
11978 TREE_OPERAND (node, 0) = nn;
11980 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11982 nn = java_complete_tree (wfl_op2);
11983 if (nn == error_mark_node)
11984 return error_mark_node;
11986 TREE_OPERAND (node, 1) = nn;
11988 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11990 case INSTANCEOF_EXPR:
11991 wfl_op1 = TREE_OPERAND (node, 0);
11992 COMPLETE_CHECK_OP_0 (node);
11993 if (flag_emit_xref)
11995 TREE_TYPE (node) = boolean_type_node;
11998 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
12000 case UNARY_PLUS_EXPR:
12002 case TRUTH_NOT_EXPR:
12004 case PREDECREMENT_EXPR:
12005 case PREINCREMENT_EXPR:
12006 case POSTDECREMENT_EXPR:
12007 case POSTINCREMENT_EXPR:
12009 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12010 how to handle those cases. */
12011 wfl_op1 = TREE_OPERAND (node, 0);
12012 CAN_COMPLETE_NORMALLY (node) = 1;
12013 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12014 if (TREE_OPERAND (node, 0) == error_mark_node)
12015 return error_mark_node;
12016 node = patch_unaryop (node, wfl_op1);
12017 CAN_COMPLETE_NORMALLY (node) = 1;
12021 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12022 how to handle those cases. */
12023 wfl_op1 = TREE_OPERAND (node, 0);
12024 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12025 if (TREE_OPERAND (node, 0) == error_mark_node)
12026 return error_mark_node;
12027 if (!flag_emit_class_files && !flag_emit_xref)
12028 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12029 /* The same applies to wfl_op2 */
12030 wfl_op2 = TREE_OPERAND (node, 1);
12031 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12032 if (TREE_OPERAND (node, 1) == error_mark_node)
12033 return error_mark_node;
12034 if (!flag_emit_class_files && !flag_emit_xref)
12035 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12036 return patch_array_ref (node);
12041 case COMPONENT_REF:
12042 /* The first step in the re-write of qualified name handling. FIXME.
12043 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12044 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12045 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12047 tree name = TREE_OPERAND (node, 1);
12048 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12049 if (field == NULL_TREE)
12051 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
12052 return error_mark_node;
12054 if (! FIELD_STATIC (field))
12056 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
12057 return error_mark_node;
12066 /* Can't use THIS in a static environment */
12069 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12070 parse_error_context (wfl_operator,
12071 "Keyword `this' used outside allowed context");
12072 TREE_TYPE (node) = error_mark_node;
12073 return error_mark_node;
12075 if (ctxp->explicit_constructor_p)
12077 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12078 parse_error_context
12079 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
12080 TREE_TYPE (node) = error_mark_node;
12081 return error_mark_node;
12083 return current_this;
12085 case CLASS_LITERAL:
12086 CAN_COMPLETE_NORMALLY (node) = 1;
12087 node = patch_incomplete_class_ref (node);
12088 if (node == error_mark_node)
12089 return error_mark_node;
12093 CAN_COMPLETE_NORMALLY (node) = 1;
12094 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12095 and it's time to turn it into the appropriate String object */
12096 if ((nn = patch_string (node)))
12099 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12104 /* Complete function call's argument. Return a nonzero value is an
12105 error was found. */
12108 complete_function_arguments (tree node)
12113 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12114 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12116 tree wfl = TREE_VALUE (cn), parm, temp;
12117 parm = java_complete_tree (wfl);
12119 if (parm == error_mark_node)
12124 /* If have a string literal that we haven't transformed yet or a
12125 crafted string buffer, as a result of use of the the String
12126 `+' operator. Build `parm.toString()' and expand it. */
12127 if ((temp = patch_string (parm)))
12130 TREE_VALUE (cn) = parm;
12132 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12136 /* Sometimes (for loops and variable initialized during their
12137 declaration), we want to wrap a statement around a WFL and turn it
12141 build_debugable_stmt (int location, tree stmt)
12143 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12145 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12146 EXPR_WFL_LINECOL (stmt) = location;
12148 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12153 build_expr_block (tree body, tree decls)
12156 tree node = make_node (BLOCK);
12157 BLOCK_EXPR_DECLS (node) = decls;
12158 BLOCK_EXPR_BODY (node) = body;
12160 TREE_TYPE (node) = TREE_TYPE (body);
12161 TREE_SIDE_EFFECTS (node) = 1;
12165 /* Create a new function block and link it appropriately to current
12166 function block chain */
12171 tree b = build_expr_block (NULL_TREE, NULL_TREE);
12173 /* Link block B supercontext to the previous block. The current
12174 function DECL is used as supercontext when enter_a_block is called
12175 for the first time for a given function. The current function body
12176 (DECL_FUNCTION_BODY) is set to be block B. */
12178 tree fndecl = current_function_decl;
12181 BLOCK_SUPERCONTEXT (b) = current_static_block;
12182 current_static_block = b;
12185 else if (!DECL_FUNCTION_BODY (fndecl))
12187 BLOCK_SUPERCONTEXT (b) = fndecl;
12188 DECL_FUNCTION_BODY (fndecl) = b;
12192 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12193 DECL_FUNCTION_BODY (fndecl) = b;
12198 /* Exit a block by changing the current function body
12199 (DECL_FUNCTION_BODY) to the current block super context, only if
12200 the block being exited isn't the method's top level one. */
12206 if (current_function_decl)
12208 b = DECL_FUNCTION_BODY (current_function_decl);
12209 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12210 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12214 b = current_static_block;
12216 if (BLOCK_SUPERCONTEXT (b))
12217 current_static_block = BLOCK_SUPERCONTEXT (b);
12222 /* Lookup for NAME in the nested function's blocks, all the way up to
12223 the current toplevel one. It complies with Java's local variable
12227 lookup_name_in_blocks (tree name)
12229 tree b = GET_CURRENT_BLOCK (current_function_decl);
12231 while (b != current_function_decl)
12235 /* Paranoid sanity check. To be removed */
12236 if (TREE_CODE (b) != BLOCK)
12239 for (current = BLOCK_EXPR_DECLS (b); current;
12240 current = TREE_CHAIN (current))
12241 if (DECL_NAME (current) == name)
12243 b = BLOCK_SUPERCONTEXT (b);
12249 maybe_absorb_scoping_blocks (void)
12251 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12253 tree b = exit_block ();
12254 java_method_add_stmt (current_function_decl, b);
12255 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
12260 /* This section of the source is reserved to build_* functions that
12261 are building incomplete tree nodes and the patch_* functions that
12262 are completing them. */
12264 /* Wrap a non WFL node around a WFL. */
12267 build_wfl_wrap (tree node, int location)
12269 tree wfl, node_to_insert = node;
12271 /* We want to process THIS . xxx symbolicaly, to keep it consistent
12272 with the way we're processing SUPER. A THIS from a primary as a
12273 different form than a SUPER. Turn THIS into something symbolic */
12274 if (TREE_CODE (node) == THIS_EXPR)
12275 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12277 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12279 EXPR_WFL_LINECOL (wfl) = location;
12280 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12284 /* Build a super() constructor invocation. Returns empty_stmt_node if
12285 we're currently dealing with the class java.lang.Object. */
12288 build_super_invocation (tree mdecl)
12290 if (DECL_CONTEXT (mdecl) == object_type_node)
12291 return empty_stmt_node;
12294 tree super_wfl = build_wfl_node (super_identifier_node);
12295 tree a = NULL_TREE, t;
12296 /* If we're dealing with an anonymous class, pass the arguments
12297 of the crafted constructor along. */
12298 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12300 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12301 for (; t != end_params_node; t = TREE_CHAIN (t))
12302 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12304 return build_method_invocation (super_wfl, a);
12308 /* Build a SUPER/THIS qualified method invocation. */
12311 build_this_super_qualified_invocation (int use_this, tree name, tree args,
12312 int lloc, int rloc)
12316 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12317 EXPR_WFL_LINECOL (wfl) = lloc;
12318 invok = build_method_invocation (name, args);
12319 return make_qualified_primary (wfl, invok, rloc);
12322 /* Build an incomplete CALL_EXPR node. */
12325 build_method_invocation (tree name, tree args)
12327 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12328 TREE_SIDE_EFFECTS (call) = 1;
12329 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12333 /* Build an incomplete new xxx(...) node. */
12336 build_new_invocation (tree name, tree args)
12338 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12339 TREE_SIDE_EFFECTS (call) = 1;
12340 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12344 /* Build an incomplete assignment expression. */
12347 build_assignment (int op, int op_location, tree lhs, tree rhs)
12350 /* Build the corresponding binop if we deal with a Compound
12351 Assignment operator. Mark the binop sub-tree as part of a
12352 Compound Assignment expression */
12353 if (op != ASSIGN_TK)
12355 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12356 COMPOUND_ASSIGN_P (rhs) = 1;
12358 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12359 TREE_SIDE_EFFECTS (assignment) = 1;
12360 EXPR_WFL_LINECOL (assignment) = op_location;
12364 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12365 the buffer. This is used only for string conversion. */
12367 string_convert_int_cst (tree node)
12369 static char buffer[80];
12371 unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12372 unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12373 char *p = buffer + sizeof (buffer) - 1;
12376 unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12377 << (HOST_BITS_PER_WIDE_INT - 1));
12381 /* If negative, note the fact and negate the value. */
12391 /* Divide by 10 until there are no bits left. */
12394 unsigned HOST_WIDE_INT acc = 0;
12395 unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12398 /* Use long division to compute the result and the remainder. */
12399 for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12401 /* Shift a bit into accumulator. */
12406 /* Shift the value. */
12412 /* Shift the correct bit into the result. */
12414 if ((outlo & hibit))
12424 /* FIXME: ASCII assumption. */
12437 /* Print an INTEGER_CST node in a static buffer, and return the
12438 buffer. This is used only for error handling. */
12440 print_int_node (tree node)
12442 static char buffer [80];
12443 if (TREE_CONSTANT_OVERFLOW (node))
12444 sprintf (buffer, "<overflow>");
12446 if (TREE_INT_CST_HIGH (node) == 0)
12447 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12448 TREE_INT_CST_LOW (node));
12449 else if (TREE_INT_CST_HIGH (node) == -1
12450 && TREE_INT_CST_LOW (node) != 0)
12453 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12454 -TREE_INT_CST_LOW (node));
12457 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12458 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12464 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12467 /* 15.25 Assignment operators. */
12470 patch_assignment (tree node, tree wfl_op1)
12472 tree rhs = TREE_OPERAND (node, 1);
12473 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12474 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12475 int error_found = 0;
12476 int lvalue_from_array = 0;
12479 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12481 /* Lhs can be a named variable */
12482 if (JDECL_P (lvalue))
12484 lhs_type = TREE_TYPE (lvalue);
12486 /* Or Lhs can be an array access. */
12487 else if (TREE_CODE (lvalue) == ARRAY_REF)
12489 lhs_type = TREE_TYPE (lvalue);
12490 lvalue_from_array = 1;
12492 /* Or a field access */
12493 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12494 lhs_type = TREE_TYPE (lvalue);
12495 /* Or a function return slot */
12496 else if (TREE_CODE (lvalue) == RESULT_DECL)
12498 /* If the return type is an integral type, then we create the
12499 RESULT_DECL with a promoted type, but we need to do these
12500 checks against the unpromoted type to ensure type safety. So
12501 here we look at the real type, not the type of the decl we
12503 lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12506 /* Otherwise, we might want to try to write into an optimized static
12507 final, this is an of a different nature, reported further on. */
12508 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12509 && resolve_expression_name (wfl_op1, &llvalue))
12511 lhs_type = TREE_TYPE (lvalue);
12515 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12519 rhs_type = TREE_TYPE (rhs);
12521 /* 5.1 Try the assignment conversion for builtin type. */
12522 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12524 /* 5.2 If it failed, try a reference conversion */
12525 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12526 lhs_type = promote_type (rhs_type);
12528 /* 15.25.2 If we have a compound assignment, convert RHS into the
12530 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12531 new_rhs = convert (lhs_type, rhs);
12533 /* Explicit cast required. This is an error */
12536 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12537 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12539 char operation [32]; /* Max size known */
12541 /* If the assignment is part of a declaration, we use the WFL of
12542 the declared variable to point out the error and call it a
12543 declaration problem. If the assignment is a genuine =
12544 operator, we call is a operator `=' problem, otherwise we
12545 call it an assignment problem. In both of these last cases,
12546 we use the WFL of the operator to indicate the error. */
12548 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12551 strcpy (operation, "declaration");
12555 wfl = wfl_operator;
12556 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12557 strcpy (operation, "assignment");
12558 else if (is_return)
12559 strcpy (operation, "`return'");
12561 strcpy (operation, "`='");
12564 if (!valid_cast_to_p (rhs_type, lhs_type))
12565 parse_error_context
12566 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12567 operation, t1, t2);
12569 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12570 operation, t1, t2);
12571 free (t1); free (t2);
12576 return error_mark_node;
12578 /* If we're processing a `return' statement, promote the actual type
12579 to the promoted type. */
12581 new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12583 /* 10.10: Array Store Exception runtime check */
12584 if (!flag_emit_class_files
12586 && lvalue_from_array
12587 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12589 tree array, store_check, base, index_expr;
12591 /* Save RHS so that it doesn't get re-evaluated by the store check. */
12592 new_rhs = save_expr (new_rhs);
12594 /* Get the INDIRECT_REF. */
12595 array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12596 /* Get the array pointer expr. */
12597 array = TREE_OPERAND (array, 0);
12598 store_check = build_java_arraystore_check (array, new_rhs);
12600 index_expr = TREE_OPERAND (lvalue, 1);
12602 if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12604 /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12605 happen before the store check, so prepare to insert the store
12606 check within the second operand of the existing COMPOUND_EXPR. */
12612 index_expr = TREE_OPERAND (base, 1);
12613 TREE_OPERAND (base, 1) = build (COMPOUND_EXPR, TREE_TYPE (index_expr),
12614 store_check, index_expr);
12617 /* Final locals can be used as case values in switch
12618 statement. Prepare them for this eventuality. */
12619 if (TREE_CODE (lvalue) == VAR_DECL
12620 && DECL_FINAL (lvalue)
12621 && TREE_CONSTANT (new_rhs)
12622 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12623 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12626 TREE_CONSTANT (lvalue) = 1;
12627 DECL_INITIAL (lvalue) = new_rhs;
12630 /* Copy the rhs if it's a reference. */
12631 if (! flag_check_references && optimize > 0)
12633 switch (TREE_CODE (new_rhs))
12637 case COMPONENT_REF:
12638 /* Transform a = foo.bar
12639 into a = { int tmp; tmp = foo.bar; tmp; ).
12640 We need to ensure that if a read from memory fails
12641 because of a NullPointerException, a destination variable
12642 will remain unchanged. An explicit temporary does what
12645 If flag_check_references is set, this is unnecessary
12646 because we'll check each reference before doing any
12647 reads. If optimize is not set the result will never be
12648 written to a stack slot that contains the LHS. */
12650 tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"),
12651 TREE_TYPE (new_rhs));
12652 tree block = build (BLOCK, TREE_TYPE (new_rhs), NULL);
12654 = build (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
12655 BLOCK_VARS (block) = tmp;
12656 BLOCK_EXPR_BODY (block)
12657 = build (COMPOUND_EXPR, TREE_TYPE (new_rhs), assignment, tmp);
12658 TREE_SIDE_EFFECTS (block) = 1;
12667 TREE_OPERAND (node, 0) = lvalue;
12668 TREE_OPERAND (node, 1) = new_rhs;
12669 TREE_TYPE (node) = lhs_type;
12673 /* Check that type SOURCE can be cast into type DEST. If the cast
12674 can't occur at all, return NULL; otherwise, return a possibly
12678 try_reference_assignconv (tree lhs_type, tree rhs)
12680 tree new_rhs = NULL_TREE;
12681 tree rhs_type = TREE_TYPE (rhs);
12683 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12685 /* `null' may be assigned to any reference type */
12686 if (rhs == null_pointer_node)
12687 new_rhs = null_pointer_node;
12688 /* Try the reference assignment conversion */
12689 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12691 /* This is a magic assignment that we process differently */
12692 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
12698 /* Check that RHS can be converted into LHS_TYPE by the assignment
12699 conversion (5.2), for the cases of RHS being a builtin type. Return
12700 NULL_TREE if the conversion fails or if because RHS isn't of a
12701 builtin type. Return a converted RHS if the conversion is possible. */
12704 try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
12706 tree new_rhs = NULL_TREE;
12707 tree rhs_type = TREE_TYPE (rhs);
12709 /* Handle boolean specially. */
12710 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12711 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12713 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12714 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12718 /* 5.1.1 Try Identity Conversion,
12719 5.1.2 Try Widening Primitive Conversion */
12720 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12721 new_rhs = convert (lhs_type, rhs);
12723 /* Try a narrowing primitive conversion (5.1.3):
12724 - expression is a constant expression of type byte, short, char,
12726 - variable is byte, short or char AND
12727 - The value of the expression is representable in the type of the
12729 else if ((rhs_type == byte_type_node || rhs_type == short_type_node
12730 || rhs_type == char_type_node || rhs_type == int_type_node)
12731 && TREE_CONSTANT (rhs)
12732 && (lhs_type == byte_type_node || lhs_type == char_type_node
12733 || lhs_type == short_type_node))
12735 if (int_fits_type_p (rhs, lhs_type))
12736 new_rhs = convert (lhs_type, rhs);
12737 else if (wfl_op1) /* Might be called with a NULL */
12738 parse_warning_context
12739 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12740 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12741 /* Reported a warning that will turn into an error further
12742 down, so we don't return */
12748 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12749 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12750 0 is the conversion test fails. This implements parts the method
12751 invocation convertion (5.3). */
12754 valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
12756 /* 5.1.1: This is the identity conversion part. */
12757 if (lhs_type == rhs_type)
12760 /* Reject non primitive types and boolean conversions. */
12761 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12764 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12765 than a char can't be converted into a char. Short can't too, but
12766 the < test below takes care of that */
12767 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12770 /* Accept all promoted type here. Note, we can't use <= in the test
12771 below, because we still need to bounce out assignments of short
12772 to char and the likes */
12773 if (lhs_type == int_type_node
12774 && (rhs_type == promoted_byte_type_node
12775 || rhs_type == promoted_short_type_node
12776 || rhs_type == promoted_char_type_node
12777 || rhs_type == promoted_boolean_type_node))
12780 /* From here, an integral is widened if its precision is smaller
12781 than the precision of the LHS or if the LHS is a floating point
12782 type, or the RHS is a float and the RHS a double. */
12783 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12784 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12785 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12786 || (rhs_type == float_type_node && lhs_type == double_type_node))
12792 /* Check that something of SOURCE type can be assigned or cast to
12793 something of DEST type at runtime. Return 1 if the operation is
12794 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12795 were SOURCE is cast into DEST, which borrows a lot of the
12796 assignment check. */
12799 valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
12801 /* SOURCE or DEST might be null if not from a declared entity. */
12802 if (!source || !dest)
12804 if (JNULLP_TYPE_P (source))
12806 if (TREE_CODE (source) == POINTER_TYPE)
12807 source = TREE_TYPE (source);
12808 if (TREE_CODE (dest) == POINTER_TYPE)
12809 dest = TREE_TYPE (dest);
12811 /* If source and dest are being compiled from bytecode, they may need to
12813 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12815 load_class (source, 1);
12816 safe_layout_class (source);
12818 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12820 load_class (dest, 1);
12821 safe_layout_class (dest);
12824 /* Case where SOURCE is a class type */
12825 if (TYPE_CLASS_P (source))
12827 if (TYPE_CLASS_P (dest))
12828 return (source == dest
12829 || inherits_from_p (source, dest)
12830 || (cast && inherits_from_p (dest, source)));
12831 if (TYPE_INTERFACE_P (dest))
12833 /* If doing a cast and SOURCE is final, the operation is
12834 always correct a compile time (because even if SOURCE
12835 does not implement DEST, a subclass of SOURCE might). */
12836 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12838 /* Otherwise, SOURCE must implement DEST */
12839 return interface_of_p (dest, source);
12841 /* DEST is an array, cast permited if SOURCE is of Object type */
12842 return (cast && source == object_type_node ? 1 : 0);
12844 if (TYPE_INTERFACE_P (source))
12846 if (TYPE_CLASS_P (dest))
12848 /* If not casting, DEST must be the Object type */
12850 return dest == object_type_node;
12851 /* We're doing a cast. The cast is always valid is class
12852 DEST is not final, otherwise, DEST must implement SOURCE */
12853 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12856 return interface_of_p (source, dest);
12858 if (TYPE_INTERFACE_P (dest))
12860 /* If doing a cast, then if SOURCE and DEST contain method
12861 with the same signature but different return type, then
12862 this is a (compile time) error */
12865 tree method_source, method_dest;
12869 for (method_source = TYPE_METHODS (source); method_source;
12870 method_source = TREE_CHAIN (method_source))
12873 build_java_argument_signature (TREE_TYPE (method_source));
12874 source_type = TREE_TYPE (TREE_TYPE (method_source));
12875 source_name = DECL_NAME (method_source);
12876 for (method_dest = TYPE_METHODS (dest);
12877 method_dest; method_dest = TREE_CHAIN (method_dest))
12879 build_java_argument_signature (TREE_TYPE (method_dest))
12880 && source_name == DECL_NAME (method_dest)
12881 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12887 return source == dest || interface_of_p (dest, source);
12893 && (DECL_NAME (TYPE_NAME (source))
12894 == java_lang_cloneable_identifier_node
12895 || (DECL_NAME (TYPE_NAME (source))
12896 == java_io_serializable_identifier_node)));
12899 if (TYPE_ARRAY_P (source))
12901 if (TYPE_CLASS_P (dest))
12902 return dest == object_type_node;
12903 /* Can't cast an array to an interface unless the interface is
12904 java.lang.Cloneable or java.io.Serializable. */
12905 if (TYPE_INTERFACE_P (dest))
12906 return (DECL_NAME (TYPE_NAME (dest))
12907 == java_lang_cloneable_identifier_node
12908 || (DECL_NAME (TYPE_NAME (dest))
12909 == java_io_serializable_identifier_node));
12912 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12913 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12915 /* In case of severe errors, they turn out null */
12916 if (!dest_element_type || !source_element_type)
12918 if (source_element_type == dest_element_type)
12920 return valid_ref_assignconv_cast_p (source_element_type,
12921 dest_element_type, cast);
12929 valid_cast_to_p (tree source, tree dest)
12931 if (TREE_CODE (source) == POINTER_TYPE)
12932 source = TREE_TYPE (source);
12933 if (TREE_CODE (dest) == POINTER_TYPE)
12934 dest = TREE_TYPE (dest);
12936 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12937 return valid_ref_assignconv_cast_p (source, dest, 1);
12939 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12942 else if (TREE_CODE (source) == BOOLEAN_TYPE
12943 && TREE_CODE (dest) == BOOLEAN_TYPE)
12950 do_unary_numeric_promotion (tree arg)
12952 tree type = TREE_TYPE (arg);
12953 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12954 || TREE_CODE (type) == CHAR_TYPE)
12955 arg = convert (int_type_node, arg);
12959 /* Return a nonzero value if SOURCE can be converted into DEST using
12960 the method invocation conversion rule (5.3). */
12962 valid_method_invocation_conversion_p (tree dest, tree source)
12964 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12965 && valid_builtin_assignconv_identity_widening_p (dest, source))
12966 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12967 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12968 && valid_ref_assignconv_cast_p (source, dest, 0)));
12971 /* Build an incomplete binop expression. */
12974 build_binop (enum tree_code op, int op_location, tree op1, tree op2)
12976 tree binop = build (op, NULL_TREE, op1, op2);
12977 TREE_SIDE_EFFECTS (binop) = 1;
12978 /* Store the location of the operator, for better error report. The
12979 string of the operator will be rebuild based on the OP value. */
12980 EXPR_WFL_LINECOL (binop) = op_location;
12984 /* Build the string of the operator retained by NODE. If NODE is part
12985 of a compound expression, add an '=' at the end of the string. This
12986 function is called when an error needs to be reported on an
12987 operator. The string is returned as a pointer to a static character
12991 operator_string (tree node)
12993 #define BUILD_OPERATOR_STRING(S) \
12995 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12999 static char buffer [10];
13000 switch (TREE_CODE (node))
13002 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13003 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13004 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13005 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13006 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13007 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13008 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13009 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13010 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13011 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13012 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13013 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13014 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13015 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13016 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13017 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13018 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13019 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13020 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13021 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13022 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13023 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13024 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13025 case PREINCREMENT_EXPR: /* Fall through */
13026 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13027 case PREDECREMENT_EXPR: /* Fall through */
13028 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13030 internal_error ("unregistered operator %s",
13031 tree_code_name [TREE_CODE (node)]);
13034 #undef BUILD_OPERATOR_STRING
13037 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13040 java_decl_equiv (tree var_acc1, tree var_acc2)
13042 if (JDECL_P (var_acc1))
13043 return (var_acc1 == var_acc2);
13045 return (TREE_CODE (var_acc1) == COMPONENT_REF
13046 && TREE_CODE (var_acc2) == COMPONENT_REF
13047 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13048 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13049 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13052 /* Return a nonzero value if CODE is one of the operators that can be
13053 used in conjunction with the `=' operator in a compound assignment. */
13056 binop_compound_p (enum tree_code code)
13059 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13060 if (binop_lookup [i] == code)
13063 return i < BINOP_COMPOUND_CANDIDATES;
13066 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13069 java_refold (tree t)
13071 tree c, b, ns, decl;
13073 if (TREE_CODE (t) != MODIFY_EXPR)
13076 c = TREE_OPERAND (t, 1);
13077 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13078 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13079 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13082 /* Now the left branch of the binary operator. */
13083 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13084 if (! (b && TREE_CODE (b) == NOP_EXPR
13085 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13088 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13089 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13090 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13093 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13094 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13095 /* It's got to be the an equivalent decl */
13096 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13098 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13099 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13100 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13101 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13102 /* Change the right part of the BINOP_EXPR */
13103 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13109 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13110 errors but we modify NODE so that it contains the type computed
13111 according to the expression, when it's fixed. Otherwise, we write
13112 error_mark_node as the type. It allows us to further the analysis
13113 of remaining nodes and detects more errors in certain cases. */
13116 patch_binop (tree node, tree wfl_op1, tree wfl_op2)
13118 tree op1 = TREE_OPERAND (node, 0);
13119 tree op2 = TREE_OPERAND (node, 1);
13120 tree op1_type = TREE_TYPE (op1);
13121 tree op2_type = TREE_TYPE (op2);
13122 tree prom_type = NULL_TREE, cn;
13123 enum tree_code code = TREE_CODE (node);
13125 /* If 1, tell the routine that we have to return error_mark_node
13126 after checking for the initialization of the RHS */
13127 int error_found = 0;
13129 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13131 /* If either op<n>_type are NULL, this might be early signs of an
13132 error situation, unless it's too early to tell (in case we're
13133 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13134 correctly so the error can be later on reported accurately. */
13135 if (! (code == PLUS_EXPR || code == NE_EXPR
13136 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13141 n = java_complete_tree (op1);
13142 op1_type = TREE_TYPE (n);
13146 n = java_complete_tree (op2);
13147 op2_type = TREE_TYPE (n);
13153 /* 15.16 Multiplicative operators */
13154 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13155 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13156 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13157 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13158 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13160 if (!JNUMERIC_TYPE_P (op1_type))
13161 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13162 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13163 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13164 TREE_TYPE (node) = error_mark_node;
13168 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13170 /* Detect integral division by zero */
13171 if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13172 && TREE_CODE (prom_type) == INTEGER_TYPE
13173 && (op2 == integer_zero_node || op2 == long_zero_node ||
13174 (TREE_CODE (op2) == INTEGER_CST &&
13175 ! TREE_INT_CST_LOW (op2) && ! TREE_INT_CST_HIGH (op2))))
13177 parse_warning_context (wfl_operator, "Evaluating this expression will result in an arithmetic exception being thrown");
13178 TREE_CONSTANT (node) = 0;
13181 /* Change the division operator if necessary */
13182 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13183 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13185 /* Before divisions as is disapear, try to simplify and bail if
13186 applicable, otherwise we won't perform even simple
13187 simplifications like (1-1)/3. We can't do that with floating
13188 point number, folds can't handle them at this stage. */
13189 if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13190 && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13192 TREE_TYPE (node) = prom_type;
13193 node = fold (node);
13194 if (TREE_CODE (node) != code)
13198 if (TREE_CODE (prom_type) == INTEGER_TYPE
13199 && flag_use_divide_subroutine
13200 && ! flag_emit_class_files
13201 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13202 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13204 /* This one is more complicated. FLOATs are processed by a
13205 function call to soft_fmod. Duplicate the value of the
13206 COMPOUND_ASSIGN_P flag. */
13207 if (code == TRUNC_MOD_EXPR)
13209 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13210 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13211 TREE_SIDE_EFFECTS (mod)
13212 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13217 /* 15.17 Additive Operators */
13218 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13220 /* Operation is valid if either one argument is a string
13221 constant, a String object or a StringBuffer crafted for the
13222 purpose of the a previous usage of the String concatenation
13225 if (TREE_CODE (op1) == STRING_CST
13226 || TREE_CODE (op2) == STRING_CST
13227 || JSTRING_TYPE_P (op1_type)
13228 || JSTRING_TYPE_P (op2_type)
13229 || IS_CRAFTED_STRING_BUFFER_P (op1)
13230 || IS_CRAFTED_STRING_BUFFER_P (op2))
13231 return build_string_concatenation (op1, op2);
13233 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13235 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13237 if (!JNUMERIC_TYPE_P (op1_type))
13238 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13239 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13240 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13241 TREE_TYPE (node) = error_mark_node;
13245 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13248 /* 15.18 Shift Operators */
13252 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13254 if (!JINTEGRAL_TYPE_P (op1_type))
13255 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13258 if (JNUMERIC_TYPE_P (op2_type))
13259 parse_error_context (wfl_operator,
13260 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13261 operator_string (node),
13262 lang_printable_name (op2_type, 0));
13264 parse_error_context (wfl_operator,
13265 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13266 operator_string (node),
13267 lang_printable_name (op2_type, 0));
13269 TREE_TYPE (node) = error_mark_node;
13274 /* Unary numeric promotion (5.6.1) is performed on each operand
13276 op1 = do_unary_numeric_promotion (op1);
13277 op2 = do_unary_numeric_promotion (op2);
13279 /* If the right hand side is of type `long', first cast it to
13281 if (TREE_TYPE (op2) == long_type_node)
13282 op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13284 /* The type of the shift expression is the type of the promoted
13285 type of the left-hand operand */
13286 prom_type = TREE_TYPE (op1);
13288 /* Shift int only up to 0x1f and long up to 0x3f */
13289 if (prom_type == int_type_node)
13290 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13291 build_int_2 (0x1f, 0)));
13293 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13294 build_int_2 (0x3f, 0)));
13296 /* The >>> operator is a >> operating on unsigned quantities */
13297 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13300 tree utype = java_unsigned_type (prom_type);
13301 op1 = convert (utype, op1);
13302 TREE_SET_CODE (node, RSHIFT_EXPR);
13303 TREE_OPERAND (node, 0) = op1;
13304 TREE_OPERAND (node, 1) = op2;
13305 TREE_TYPE (node) = utype;
13306 to_return = convert (prom_type, node);
13307 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13308 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13309 TREE_SIDE_EFFECTS (to_return)
13310 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13315 /* 15.19.1 Type Comparison Operator instaceof */
13316 case INSTANCEOF_EXPR:
13318 TREE_TYPE (node) = boolean_type_node;
13320 /* OP1_TYPE might be NULL when OP1 is a string constant. */
13321 if ((cn = patch_string (op1)))
13324 op1_type = TREE_TYPE (op1);
13326 if (op1_type == NULL_TREE)
13329 if (!(op2_type = resolve_type_during_patch (op2)))
13330 return error_mark_node;
13332 /* The first operand must be a reference type or the null type */
13333 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13334 error_found = 1; /* Error reported further below */
13336 /* The second operand must be a reference type */
13337 if (!JREFERENCE_TYPE_P (op2_type))
13339 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13340 parse_error_context
13341 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13342 lang_printable_name (op2_type, 0));
13346 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13348 /* If the first operand is null, the result is always false */
13349 if (op1 == null_pointer_node)
13350 return boolean_false_node;
13351 else if (flag_emit_class_files)
13353 TREE_OPERAND (node, 1) = op2_type;
13354 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13357 /* Otherwise we have to invoke instance of to figure it out */
13359 return build_instanceof (op1, op2_type);
13361 /* There is no way the expression operand can be an instance of
13362 the type operand. This is a compile time error. */
13365 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13366 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13367 parse_error_context
13368 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13369 t1, lang_printable_name (op2_type, 0));
13376 /* 15.21 Bitwise and Logical Operators */
13380 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13381 /* Binary numeric promotion is performed on both operand and the
13382 expression retain that type */
13383 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13385 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13386 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13387 /* The type of the bitwise operator expression is BOOLEAN */
13388 prom_type = boolean_type_node;
13391 if (!JINTEGRAL_TYPE_P (op1_type))
13392 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13393 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13394 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13395 TREE_TYPE (node) = error_mark_node;
13397 /* Insert a break here if adding thing before the switch's
13398 break for this case */
13402 /* 15.22 Conditional-And Operator */
13403 case TRUTH_ANDIF_EXPR:
13404 /* 15.23 Conditional-Or Operator */
13405 case TRUTH_ORIF_EXPR:
13406 /* Operands must be of BOOLEAN type */
13407 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13408 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13410 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13411 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13412 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13413 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13414 TREE_TYPE (node) = boolean_type_node;
13418 else if (integer_zerop (op1))
13420 return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13422 else if (integer_onep (op1))
13424 return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13426 /* The type of the conditional operators is BOOLEAN */
13427 prom_type = boolean_type_node;
13430 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13435 /* The type of each of the operands must be a primitive numeric
13437 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13439 if (!JNUMERIC_TYPE_P (op1_type))
13440 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13441 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13442 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13443 TREE_TYPE (node) = boolean_type_node;
13447 /* Binary numeric promotion is performed on the operands */
13448 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13449 /* The type of the relation expression is always BOOLEAN */
13450 prom_type = boolean_type_node;
13453 /* 15.20 Equality Operator */
13456 /* It's time for us to patch the strings. */
13457 if ((cn = patch_string (op1)))
13460 op1_type = TREE_TYPE (op1);
13462 if ((cn = patch_string (op2)))
13465 op2_type = TREE_TYPE (op2);
13468 /* 15.20.1 Numerical Equality Operators == and != */
13469 /* Binary numeric promotion is performed on the operands */
13470 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13471 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13473 /* 15.20.2 Boolean Equality Operators == and != */
13474 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13475 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13476 ; /* Nothing to do here */
13478 /* 15.20.3 Reference Equality Operators == and != */
13479 /* Types have to be either references or the null type. If
13480 they're references, it must be possible to convert either
13481 type to the other by casting conversion. */
13482 else if (op1 == null_pointer_node || op2 == null_pointer_node
13483 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13484 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13485 || valid_ref_assignconv_cast_p (op2_type,
13487 ; /* Nothing to do here */
13489 /* Else we have an error figure what can't be converted into
13490 what and report the error */
13494 t1 = xstrdup (lang_printable_name (op1_type, 0));
13495 parse_error_context
13497 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13498 operator_string (node), t1,
13499 lang_printable_name (op2_type, 0));
13501 TREE_TYPE (node) = boolean_type_node;
13505 prom_type = boolean_type_node;
13512 return error_mark_node;
13514 TREE_OPERAND (node, 0) = op1;
13515 TREE_OPERAND (node, 1) = op2;
13516 TREE_TYPE (node) = prom_type;
13517 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13519 if (flag_emit_xref)
13522 /* fold does not respect side-effect order as required for Java but not C.
13523 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13526 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13527 : ! TREE_SIDE_EFFECTS (node))
13528 node = fold (node);
13532 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13533 zero value, the value of CSTE comes after the valude of STRING */
13536 do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13538 const char *old = TREE_STRING_POINTER (cste);
13539 int old_len = TREE_STRING_LENGTH (cste);
13540 int len = old_len + string_len;
13541 char *new = alloca (len+1);
13545 memcpy (new, string, string_len);
13546 memcpy (&new [string_len], old, old_len);
13550 memcpy (new, old, old_len);
13551 memcpy (&new [old_len], string, string_len);
13554 return build_string (len, new);
13557 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13558 new STRING_CST on success, NULL_TREE on failure */
13561 merge_string_cste (tree op1, tree op2, int after)
13563 /* Handle two string constants right away */
13564 if (TREE_CODE (op2) == STRING_CST)
13565 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13566 TREE_STRING_LENGTH (op2), after);
13568 /* Reasonable integer constant can be treated right away */
13569 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13571 static const char *const boolean_true = "true";
13572 static const char *const boolean_false = "false";
13573 static const char *const null_pointer = "null";
13575 const char *string;
13577 if (op2 == boolean_true_node)
13578 string = boolean_true;
13579 else if (op2 == boolean_false_node)
13580 string = boolean_false;
13581 else if (op2 == null_pointer_node)
13582 string = null_pointer;
13583 else if (TREE_TYPE (op2) == char_type_node)
13585 /* Convert the character into UTF-8. */
13586 unsigned char c = (unsigned char) TREE_INT_CST_LOW (op2);
13587 unsigned char *p = (unsigned char *) ch;
13593 *p++ = c >> 6 | 0xc0;
13594 *p++ = (c & 0x3f) | 0x80;
13601 string = string_convert_int_cst (op2);
13603 return do_merge_string_cste (op1, string, strlen (string), after);
13608 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13609 has to be a STRING_CST and the other part must be a STRING_CST or a
13610 INTEGRAL constant. Return a new STRING_CST if the operation
13611 succeed, NULL_TREE otherwise.
13613 If the case we want to optimize for space, we might want to return
13614 NULL_TREE for each invocation of this routine. FIXME */
13617 string_constant_concatenation (tree op1, tree op2)
13619 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13624 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13625 rest = (string == op1 ? op2 : op1);
13626 invert = (string == op1 ? 0 : 1 );
13628 /* Walk REST, only if it looks reasonable */
13629 if (TREE_CODE (rest) != STRING_CST
13630 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13631 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13632 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13634 rest = java_complete_tree (rest);
13635 if (rest == error_mark_node)
13636 return error_mark_node;
13637 rest = fold (rest);
13639 return merge_string_cste (string, rest, invert);
13644 /* Implement the `+' operator. Does static optimization if possible,
13645 otherwise create (if necessary) and append elements to a
13646 StringBuffer. The StringBuffer will be carried around until it is
13647 used for a function call or an assignment. Then toString() will be
13648 called on it to turn it into a String object. */
13651 build_string_concatenation (tree op1, tree op2)
13654 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13656 if (flag_emit_xref)
13657 return build (PLUS_EXPR, string_type_node, op1, op2);
13659 /* Try to do some static optimization */
13660 if ((result = string_constant_concatenation (op1, op2)))
13663 /* Discard empty strings on either side of the expression */
13664 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13669 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13672 /* If operands are string constant, turn then into object references */
13673 if (TREE_CODE (op1) == STRING_CST)
13674 op1 = patch_string_cst (op1);
13675 if (op2 && TREE_CODE (op2) == STRING_CST)
13676 op2 = patch_string_cst (op2);
13678 /* If either one of the constant is null and the other non null
13679 operand is a String constant, return it. */
13680 if ((TREE_CODE (op1) == STRING_CST) && !op2)
13683 /* If OP1 isn't already a StringBuffer, create and
13684 initialize a new one */
13685 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13687 /* Two solutions here:
13688 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13689 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13690 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13691 op1 = BUILD_STRING_BUFFER (op1);
13694 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13695 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13701 /* OP1 is no longer the last node holding a crafted StringBuffer */
13702 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13703 /* Create a node for `{new...,xxx}.append (op2)' */
13705 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13708 /* Mark the last node holding a crafted StringBuffer */
13709 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13711 TREE_SIDE_EFFECTS (op1) = side_effects;
13715 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13716 StringBuffer. If no string were found to be patched, return
13720 patch_string (tree node)
13722 if (node == error_mark_node)
13723 return error_mark_node;
13724 if (TREE_CODE (node) == STRING_CST)
13725 return patch_string_cst (node);
13726 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13728 int saved = ctxp->explicit_constructor_p;
13729 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13731 /* Temporary disable forbid the use of `this'. */
13732 ctxp->explicit_constructor_p = 0;
13733 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13734 /* String concatenation arguments must be evaluated in order too. */
13735 ret = force_evaluation_order (ret);
13736 /* Restore it at its previous value */
13737 ctxp->explicit_constructor_p = saved;
13743 /* Build the internal representation of a string constant. */
13746 patch_string_cst (tree node)
13749 if (! flag_emit_class_files)
13751 node = get_identifier (TREE_STRING_POINTER (node));
13752 location = alloc_name_constant (CONSTANT_String, node);
13753 node = build_ref_from_constant_pool (location);
13755 TREE_TYPE (node) = string_ptr_type_node;
13756 TREE_CONSTANT (node) = 1;
13760 /* Build an incomplete unary operator expression. */
13763 build_unaryop (int op_token, int op_location, tree op1)
13769 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13770 case MINUS_TK: op = NEGATE_EXPR; break;
13771 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13772 case NOT_TK: op = BIT_NOT_EXPR; break;
13776 unaryop = build1 (op, NULL_TREE, op1);
13777 TREE_SIDE_EFFECTS (unaryop) = 1;
13778 /* Store the location of the operator, for better error report. The
13779 string of the operator will be rebuild based on the OP value. */
13780 EXPR_WFL_LINECOL (unaryop) = op_location;
13784 /* Special case for the ++/-- operators, since they require an extra
13785 argument to build, which is set to NULL and patched
13786 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13789 build_incdec (int op_token, int op_location, tree op1, int is_post_p)
13791 static const enum tree_code lookup [2][2] =
13793 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13794 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13796 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13797 NULL_TREE, op1, NULL_TREE);
13798 TREE_SIDE_EFFECTS (node) = 1;
13799 /* Store the location of the operator, for better error report. The
13800 string of the operator will be rebuild based on the OP value. */
13801 EXPR_WFL_LINECOL (node) = op_location;
13805 /* Build an incomplete cast operator, based on the use of the
13806 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13807 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13808 though its type is already set. */
13811 build_cast (int location, tree type, tree exp)
13813 tree node = build1 (CONVERT_EXPR, type, exp);
13814 EXPR_WFL_LINECOL (node) = location;
13818 /* Build an incomplete class reference operator. */
13820 build_incomplete_class_ref (int location, tree class_name)
13822 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13823 EXPR_WFL_LINECOL (node) = location;
13827 /* Complete an incomplete class reference operator. */
13829 patch_incomplete_class_ref (tree node)
13831 tree type = TREE_OPERAND (node, 0);
13834 if (!(ref_type = resolve_type_during_patch (type)))
13835 return error_mark_node;
13837 /* Generate the synthetic static method `class$'. (Previously we
13838 deferred this, causing different method tables to be emitted
13839 for native code and bytecode.) */
13840 if (!TYPE_DOT_CLASS (current_class))
13841 build_dot_class_method (current_class);
13843 /* If we're not emitting class files and we know ref_type is a
13844 compiled class, build a direct reference. */
13845 if ((! flag_emit_class_files && is_compiled_class (ref_type))
13846 || JPRIMITIVE_TYPE_P (ref_type)
13847 || TREE_CODE (ref_type) == VOID_TYPE)
13849 tree dot = build_class_ref (ref_type);
13850 /* A class referenced by `foo.class' is initialized. */
13851 if (!flag_emit_class_files)
13852 dot = build_class_init (ref_type, dot);
13853 return java_complete_tree (dot);
13856 /* If we're emitting class files and we have to deal with non
13857 primitive types, we invoke the synthetic static method `class$'. */
13858 ref_type = build_dot_class_method_invocation (ref_type);
13859 return java_complete_tree (ref_type);
13862 /* 15.14 Unary operators. We return error_mark_node in case of error,
13863 but preserve the type of NODE if the type is fixed. */
13866 patch_unaryop (tree node, tree wfl_op)
13868 tree op = TREE_OPERAND (node, 0);
13869 tree op_type = TREE_TYPE (op);
13870 tree prom_type = NULL_TREE, value, decl;
13871 int outer_field_flag = 0;
13872 int code = TREE_CODE (node);
13873 int error_found = 0;
13875 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13879 /* 15.13.2 Postfix Increment Operator ++ */
13880 case POSTINCREMENT_EXPR:
13881 /* 15.13.3 Postfix Increment Operator -- */
13882 case POSTDECREMENT_EXPR:
13883 /* 15.14.1 Prefix Increment Operator ++ */
13884 case PREINCREMENT_EXPR:
13885 /* 15.14.2 Prefix Decrement Operator -- */
13886 case PREDECREMENT_EXPR:
13887 op = decl = strip_out_static_field_access_decl (op);
13888 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13889 /* We might be trying to change an outer field accessed using
13891 if (outer_field_flag)
13893 /* Retrieve the decl of the field we're trying to access. We
13894 do that by first retrieving the function we would call to
13895 access the field. It has been already verified that this
13896 field isn't final */
13897 if (flag_emit_class_files)
13898 decl = TREE_OPERAND (op, 0);
13900 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13901 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13903 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13904 else if (!JDECL_P (decl)
13905 && TREE_CODE (decl) != COMPONENT_REF
13906 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13907 && TREE_CODE (decl) != INDIRECT_REF
13908 && !(TREE_CODE (decl) == COMPOUND_EXPR
13909 && TREE_OPERAND (decl, 1)
13910 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13912 TREE_TYPE (node) = error_mark_node;
13916 /* From now on, we know that op if a variable and that it has a
13917 valid wfl. We use wfl_op to locate errors related to the
13919 if (!JNUMERIC_TYPE_P (op_type))
13921 parse_error_context
13922 (wfl_op, "Invalid argument type `%s' to `%s'",
13923 lang_printable_name (op_type, 0), operator_string (node));
13924 TREE_TYPE (node) = error_mark_node;
13929 /* Before the addition, binary numeric promotion is performed on
13930 both operands, if really necessary */
13931 if (JINTEGRAL_TYPE_P (op_type))
13933 value = build_int_2 (1, 0);
13934 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13938 value = build_int_2 (1, 0);
13940 binary_numeric_promotion (op_type,
13941 TREE_TYPE (value), &op, &value);
13944 /* We remember we might be accessing an outer field */
13945 if (outer_field_flag)
13947 /* We re-generate an access to the field */
13948 value = build (PLUS_EXPR, TREE_TYPE (op),
13949 build_outer_field_access (wfl_op, decl), value);
13951 /* And we patch the original access$() into a write
13952 with plus_op as a rhs */
13953 return outer_field_access_fix (node, op, value);
13956 /* And write back into the node. */
13957 TREE_OPERAND (node, 0) = op;
13958 TREE_OPERAND (node, 1) = value;
13959 /* Convert the overall back into its original type, if
13960 necessary, and return */
13961 if (JINTEGRAL_TYPE_P (op_type))
13962 return fold (node);
13964 return fold (convert (op_type, node));
13968 /* 15.14.3 Unary Plus Operator + */
13969 case UNARY_PLUS_EXPR:
13970 /* 15.14.4 Unary Minus Operator - */
13972 if (!JNUMERIC_TYPE_P (op_type))
13974 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13975 TREE_TYPE (node) = error_mark_node;
13978 /* Unary numeric promotion is performed on operand */
13981 op = do_unary_numeric_promotion (op);
13982 prom_type = TREE_TYPE (op);
13983 if (code == UNARY_PLUS_EXPR)
13988 /* 15.14.5 Bitwise Complement Operator ~ */
13990 if (!JINTEGRAL_TYPE_P (op_type))
13992 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13993 TREE_TYPE (node) = error_mark_node;
13998 op = do_unary_numeric_promotion (op);
13999 prom_type = TREE_TYPE (op);
14003 /* 15.14.6 Logical Complement Operator ! */
14004 case TRUTH_NOT_EXPR:
14005 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14007 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14008 /* But the type is known. We will report an error if further
14009 attempt of a assignment is made with this rhs */
14010 TREE_TYPE (node) = boolean_type_node;
14014 prom_type = boolean_type_node;
14017 /* 15.15 Cast Expression */
14019 value = patch_cast (node, wfl_operator);
14020 if (value == error_mark_node)
14022 /* If this cast is part of an assignment, we tell the code
14023 that deals with it not to complain about a mismatch,
14024 because things have been cast, anyways */
14025 TREE_TYPE (node) = error_mark_node;
14030 value = fold (value);
14031 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
14038 return error_mark_node;
14040 /* There are cases where node has been replaced by something else
14041 and we don't end up returning here: UNARY_PLUS_EXPR,
14042 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14043 TREE_OPERAND (node, 0) = fold (op);
14044 TREE_TYPE (node) = prom_type;
14045 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14046 return fold (node);
14049 /* Generic type resolution that sometimes takes place during node
14050 patching. Returned the resolved type or generate an error
14051 message. Return the resolved type or NULL_TREE. */
14054 resolve_type_during_patch (tree type)
14056 if (unresolved_type_p (type, NULL))
14058 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14061 parse_error_context (type,
14062 "Class `%s' not found in type declaration",
14063 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14066 return TREE_TYPE (type_decl);
14070 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14071 found. Otherwise NODE or something meant to replace it is returned. */
14074 patch_cast (tree node, tree wfl_op)
14076 tree op = TREE_OPERAND (node, 0);
14077 tree cast_type = TREE_TYPE (node);
14078 tree patched, op_type;
14081 /* Some string patching might be necessary at this stage */
14082 if ((patched = patch_string (op)))
14083 TREE_OPERAND (node, 0) = op = patched;
14084 op_type = TREE_TYPE (op);
14086 /* First resolve OP_TYPE if unresolved */
14087 if (!(cast_type = resolve_type_during_patch (cast_type)))
14088 return error_mark_node;
14090 /* Check on cast that are proven correct at compile time */
14091 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14094 if (cast_type == op_type)
14097 /* float and double type are converted to the original type main
14098 variant and then to the target type. */
14099 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14100 op = convert (integer_type_node, op);
14102 /* Try widening/narowwing convertion. Potentially, things need
14103 to be worked out in gcc so we implement the extreme cases
14104 correctly. fold_convert() needs to be fixed. */
14105 return convert (cast_type, op);
14108 /* It's also valid to cast a boolean into a boolean */
14109 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14112 /* null can be casted to references */
14113 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14114 return build_null_of_type (cast_type);
14116 /* The remaining legal casts involve conversion between reference
14117 types. Check for their compile time correctness. */
14118 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14119 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14121 TREE_TYPE (node) = promote_type (cast_type);
14122 /* Now, the case can be determined correct at compile time if
14123 OP_TYPE can be converted into CAST_TYPE by assignment
14124 conversion (5.2) */
14126 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14128 TREE_SET_CODE (node, NOP_EXPR);
14132 if (flag_emit_class_files)
14134 TREE_SET_CODE (node, CONVERT_EXPR);
14138 /* The cast requires a run-time check */
14139 return build (CALL_EXPR, promote_type (cast_type),
14140 build_address_of (soft_checkcast_node),
14141 tree_cons (NULL_TREE, build_class_ref (cast_type),
14142 build_tree_list (NULL_TREE, op)),
14146 /* Any other casts are proven incorrect at compile time */
14147 t1 = xstrdup (lang_printable_name (op_type, 0));
14148 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14149 t1, lang_printable_name (cast_type, 0));
14151 return error_mark_node;
14154 /* Build a null constant and give it the type TYPE. */
14157 build_null_of_type (tree type)
14159 tree node = build_int_2 (0, 0);
14160 TREE_TYPE (node) = promote_type (type);
14164 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14165 a list of indices. */
14167 build_array_ref (int location, tree array, tree index)
14169 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14170 EXPR_WFL_LINECOL (node) = location;
14174 /* 15.12 Array Access Expression */
14177 patch_array_ref (tree node)
14179 tree array = TREE_OPERAND (node, 0);
14180 tree array_type = TREE_TYPE (array);
14181 tree index = TREE_OPERAND (node, 1);
14182 tree index_type = TREE_TYPE (index);
14183 int error_found = 0;
14185 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14187 if (TREE_CODE (array_type) == POINTER_TYPE)
14188 array_type = TREE_TYPE (array_type);
14190 /* The array reference must be an array */
14191 if (!TYPE_ARRAY_P (array_type))
14193 parse_error_context
14195 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14196 lang_printable_name (array_type, 0));
14197 TREE_TYPE (node) = error_mark_node;
14201 /* The array index undergoes unary numeric promotion. The promoted
14202 type must be int */
14203 index = do_unary_numeric_promotion (index);
14204 if (TREE_TYPE (index) != int_type_node)
14206 if (valid_cast_to_p (index_type, int_type_node))
14207 parse_error_context (wfl_operator,
14208 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14209 lang_printable_name (index_type, 0));
14211 parse_error_context (wfl_operator,
14212 "Incompatible type for `[]'. Can't convert `%s' to `int'",
14213 lang_printable_name (index_type, 0));
14214 TREE_TYPE (node) = error_mark_node;
14219 return error_mark_node;
14221 array_type = TYPE_ARRAY_ELEMENT (array_type);
14223 if (flag_emit_class_files || flag_emit_xref)
14225 TREE_OPERAND (node, 0) = array;
14226 TREE_OPERAND (node, 1) = index;
14229 node = build_java_arrayaccess (array, array_type, index);
14230 TREE_TYPE (node) = array_type;
14234 /* 15.9 Array Creation Expressions */
14237 build_newarray_node (tree type, tree dims, int extra_dims)
14240 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14241 build_int_2 (extra_dims, 0));
14246 patch_newarray (tree node)
14248 tree type = TREE_OPERAND (node, 0);
14249 tree dims = TREE_OPERAND (node, 1);
14250 tree cdim, array_type;
14251 int error_found = 0;
14253 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14255 /* Dimension types are verified. It's better for the types to be
14256 verified in order. */
14257 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14260 tree dim = TREE_VALUE (cdim);
14262 /* Dim might have been saved during its evaluation */
14263 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14265 /* The type of each specified dimension must be an integral type. */
14266 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14269 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14270 promoted type must be int. */
14273 dim = do_unary_numeric_promotion (dim);
14274 if (TREE_TYPE (dim) != int_type_node)
14278 /* Report errors on types here */
14281 parse_error_context
14282 (TREE_PURPOSE (cdim),
14283 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14284 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14285 "Explicit cast needed to" : "Can't"),
14286 lang_printable_name (TREE_TYPE (dim), 0));
14290 TREE_PURPOSE (cdim) = NULL_TREE;
14293 /* Resolve array base type if unresolved */
14294 if (!(type = resolve_type_during_patch (type)))
14299 /* We don't want further evaluation of this bogus array creation
14301 TREE_TYPE (node) = error_mark_node;
14302 return error_mark_node;
14305 /* Set array_type to the actual (promoted) array type of the result. */
14306 if (TREE_CODE (type) == RECORD_TYPE)
14307 type = build_pointer_type (type);
14308 while (--xdims >= 0)
14310 type = promote_type (build_java_array_type (type, -1));
14312 dims = nreverse (dims);
14314 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14318 = build_java_array_type (type,
14319 TREE_CODE (cdim) == INTEGER_CST
14320 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14322 array_type = promote_type (array_type);
14324 dims = nreverse (dims);
14326 /* The node is transformed into a function call. Things are done
14327 differently according to the number of dimensions. If the number
14328 of dimension is equal to 1, then the nature of the base type
14329 (primitive or not) matters. */
14331 return build_new_array (type, TREE_VALUE (dims));
14333 /* Can't reuse what's already written in expr.c because it uses the
14334 JVM stack representation. Provide a build_multianewarray. FIXME */
14335 return build (CALL_EXPR, array_type,
14336 build_address_of (soft_multianewarray_node),
14337 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14338 tree_cons (NULL_TREE,
14339 build_int_2 (ndims, 0), dims )),
14343 /* 10.6 Array initializer. */
14345 /* Build a wfl for array element that don't have one, so we can
14346 pin-point errors. */
14349 maybe_build_array_element_wfl (tree node)
14351 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14352 return build_expr_wfl (NULL_TREE, ctxp->filename,
14353 ctxp->elc.line, ctxp->elc.prev_col);
14358 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14359 identification of initialized arrays easier to detect during walk
14363 build_new_array_init (int location, tree values)
14365 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14366 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14367 EXPR_WFL_LINECOL (to_return) = location;
14371 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14372 occurred. Otherwise return NODE after having set its type
14376 patch_new_array_init (tree type, tree node)
14378 int error_seen = 0;
14379 tree current, element_type;
14380 HOST_WIDE_INT length;
14381 int all_constant = 1;
14382 tree init = TREE_OPERAND (node, 0);
14384 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14386 parse_error_context (node,
14387 "Invalid array initializer for non-array type `%s'",
14388 lang_printable_name (type, 1));
14389 return error_mark_node;
14391 type = TREE_TYPE (type);
14392 element_type = TYPE_ARRAY_ELEMENT (type);
14394 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14396 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14397 current; length++, current = TREE_CHAIN (current))
14399 tree elt = TREE_VALUE (current);
14400 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14402 error_seen |= array_constructor_check_entry (element_type, current);
14403 elt = TREE_VALUE (current);
14404 /* When compiling to native code, STRING_CST is converted to
14405 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14406 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14411 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14412 TREE_PURPOSE (current) = NULL_TREE;
14415 if (elt && TREE_CODE (elt) == TREE_LIST
14416 && TREE_VALUE (elt) == error_mark_node)
14421 return error_mark_node;
14423 /* Create a new type. We can't reuse the one we have here by
14424 patching its dimension because it originally is of dimension -1
14425 hence reused by gcc. This would prevent triangular arrays. */
14426 type = build_java_array_type (element_type, length);
14427 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14428 TREE_TYPE (node) = promote_type (type);
14429 TREE_CONSTANT (init) = all_constant;
14430 TREE_CONSTANT (node) = all_constant;
14434 /* Verify that one entry of the initializer element list can be
14435 assigned to the array base type. Report 1 if an error occurred, 0
14439 array_constructor_check_entry (tree type, tree entry)
14441 char *array_type_string = NULL; /* For error reports */
14442 tree value, type_value, new_value, wfl_value, patched;
14443 int error_seen = 0;
14445 new_value = NULL_TREE;
14446 wfl_value = TREE_VALUE (entry);
14448 value = java_complete_tree (TREE_VALUE (entry));
14449 /* patch_string return error_mark_node if arg is error_mark_node */
14450 if ((patched = patch_string (value)))
14452 if (value == error_mark_node)
14455 type_value = TREE_TYPE (value);
14457 /* At anytime, try_builtin_assignconv can report a warning on
14458 constant overflow during narrowing. */
14459 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14460 new_value = try_builtin_assignconv (wfl_operator, type, value);
14461 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14462 type_value = promote_type (type);
14464 /* Check and report errors */
14467 const char *const msg = (!valid_cast_to_p (type_value, type) ?
14468 "Can't" : "Explicit cast needed to");
14469 if (!array_type_string)
14470 array_type_string = xstrdup (lang_printable_name (type, 1));
14471 parse_error_context
14472 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14473 msg, lang_printable_name (type_value, 1), array_type_string);
14478 TREE_VALUE (entry) = new_value;
14480 if (array_type_string)
14481 free (array_type_string);
14483 TREE_PURPOSE (entry) = NULL_TREE;
14488 build_this (int location)
14490 tree node = build_wfl_node (this_identifier_node);
14491 TREE_SET_CODE (node, THIS_EXPR);
14492 EXPR_WFL_LINECOL (node) = location;
14496 /* 14.15 The return statement. It builds a modify expression that
14497 assigns the returned value to the RESULT_DECL that hold the value
14501 build_return (int location, tree op)
14503 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14504 EXPR_WFL_LINECOL (node) = location;
14505 node = build_debugable_stmt (location, node);
14510 patch_return (tree node)
14512 tree return_exp = TREE_OPERAND (node, 0);
14513 tree meth = current_function_decl;
14514 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14515 int error_found = 0;
14517 TREE_TYPE (node) = error_mark_node;
14518 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14520 /* It's invalid to have a return value within a function that is
14521 declared with the keyword void or that is a constructor */
14522 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14525 /* It's invalid to use a return statement in a static block */
14526 if (DECL_CLINIT_P (current_function_decl))
14529 /* It's invalid to have a no return value within a function that
14530 isn't declared with the keyword `void' */
14531 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14534 if (DECL_INSTINIT_P (current_function_decl))
14539 if (DECL_INSTINIT_P (current_function_decl))
14540 parse_error_context (wfl_operator,
14541 "`return' inside instance initializer");
14543 else if (DECL_CLINIT_P (current_function_decl))
14544 parse_error_context (wfl_operator,
14545 "`return' inside static initializer");
14547 else if (!DECL_CONSTRUCTOR_P (meth))
14549 char *t = xstrdup (lang_printable_name (mtype, 0));
14550 parse_error_context (wfl_operator,
14551 "`return' with%s value from `%s %s'",
14552 (error_found == 1 ? "" : "out"),
14553 t, lang_printable_name (meth, 0));
14557 parse_error_context (wfl_operator,
14558 "`return' with value from constructor `%s'",
14559 lang_printable_name (meth, 0));
14560 return error_mark_node;
14563 /* If we have a return_exp, build a modify expression and expand
14564 it. Note: at that point, the assignment is declared valid, but we
14565 may want to carry some more hacks */
14568 tree exp = java_complete_tree (return_exp);
14569 tree modify, patched;
14571 if ((patched = patch_string (exp)))
14574 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14575 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14576 modify = java_complete_tree (modify);
14578 if (modify != error_mark_node)
14580 TREE_SIDE_EFFECTS (modify) = 1;
14581 TREE_OPERAND (node, 0) = modify;
14584 return error_mark_node;
14586 TREE_TYPE (node) = void_type_node;
14587 TREE_SIDE_EFFECTS (node) = 1;
14591 /* 14.8 The if Statement */
14594 build_if_else_statement (int location, tree expression, tree if_body,
14599 else_body = empty_stmt_node;
14600 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14601 EXPR_WFL_LINECOL (node) = location;
14602 node = build_debugable_stmt (location, node);
14607 patch_if_else_statement (tree node)
14609 tree expression = TREE_OPERAND (node, 0);
14610 int can_complete_normally
14611 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14612 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
14614 TREE_TYPE (node) = error_mark_node;
14615 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14617 /* The type of expression must be boolean */
14618 if (TREE_TYPE (expression) != boolean_type_node
14619 && TREE_TYPE (expression) != promoted_boolean_type_node)
14621 parse_error_context
14623 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14624 lang_printable_name (TREE_TYPE (expression), 0));
14625 return error_mark_node;
14628 if (TREE_CODE (expression) == INTEGER_CST)
14630 if (integer_zerop (expression))
14631 node = TREE_OPERAND (node, 2);
14633 node = TREE_OPERAND (node, 1);
14634 if (CAN_COMPLETE_NORMALLY (node) != can_complete_normally)
14636 node = build (COMPOUND_EXPR, void_type_node, node, empty_stmt_node);
14637 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14641 TREE_TYPE (node) = void_type_node;
14642 TREE_SIDE_EFFECTS (node) = 1;
14643 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14647 /* 14.6 Labeled Statements */
14649 /* Action taken when a lableled statement is parsed. a new
14650 LABELED_BLOCK_EXPR is created. No statement is attached to the
14651 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14654 build_labeled_block (int location, tree label)
14657 tree label_decl, node;
14658 if (label == NULL_TREE || label == continue_identifier_node)
14659 label_name = label;
14662 label_name = merge_qualified_name (label_id, label);
14663 /* Issue an error if we try to reuse a label that was previously
14665 if (IDENTIFIER_LOCAL_VALUE (label_name))
14667 EXPR_WFL_LINECOL (wfl_operator) = location;
14668 parse_error_context (wfl_operator,
14669 "Declaration of `%s' shadows a previous label declaration",
14670 IDENTIFIER_POINTER (label));
14671 EXPR_WFL_LINECOL (wfl_operator) =
14672 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14673 parse_error_context (wfl_operator,
14674 "This is the location of the previous declaration of label `%s'",
14675 IDENTIFIER_POINTER (label));
14676 java_error_count--;
14680 label_decl = create_label_decl (label_name);
14681 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14682 EXPR_WFL_LINECOL (node) = location;
14683 TREE_SIDE_EFFECTS (node) = 1;
14687 /* A labeled statement LBE is attached a statement. */
14690 finish_labeled_statement (tree lbe, /* Labeled block expr */
14693 /* In anyways, tie the loop to its statement */
14694 LABELED_BLOCK_BODY (lbe) = statement;
14695 pop_labeled_block ();
14696 POP_LABELED_BLOCK ();
14700 /* 14.10, 14.11, 14.12 Loop Statements */
14702 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14706 build_new_loop (tree loop_body)
14708 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14709 TREE_SIDE_EFFECTS (loop) = 1;
14714 /* Create a loop body according to the following structure:
14716 COMPOUND_EXPR (loop main body)
14717 EXIT_EXPR (this order is for while/for loops.
14718 LABELED_BLOCK_EXPR the order is reversed for do loops)
14719 LABEL_DECL (a continue occurring here branches at the
14720 BODY end of this labeled block)
14723 REVERSED, if nonzero, tells that the loop condition expr comes
14724 after the body, like in the do-while loop.
14726 To obtain a loop, the loop body structure described above is
14727 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14730 LABEL_DECL (use this label to exit the loop)
14732 <structure described above> */
14735 build_loop_body (int location, tree condition, int reversed)
14737 tree first, second, body;
14739 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14740 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14741 condition = build_debugable_stmt (location, condition);
14742 TREE_SIDE_EFFECTS (condition) = 1;
14744 body = build_labeled_block (0, continue_identifier_node);
14745 first = (reversed ? body : condition);
14746 second = (reversed ? condition : body);
14748 build (COMPOUND_EXPR, NULL_TREE,
14749 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14752 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14753 their order) on the current loop. Unlink the current loop from the
14757 finish_loop_body (int location, tree condition, tree body, int reversed)
14759 tree to_return = ctxp->current_loop;
14760 tree loop_body = LOOP_EXPR_BODY (to_return);
14763 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14764 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14765 The real EXIT_EXPR is one operand further. */
14766 EXPR_WFL_LINECOL (cnode) = location;
14767 /* This one is for accurate error reports */
14768 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14769 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14771 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14776 /* Tailored version of finish_loop_body for FOR loops, when FOR
14777 loops feature the condition part */
14780 finish_for_loop (int location, tree condition, tree update, tree body)
14782 /* Put the condition and the loop body in place */
14783 tree loop = finish_loop_body (location, condition, body, 0);
14784 /* LOOP is the current loop which has been now popped of the loop
14785 stack. Mark the update block as reachable and install it. We do
14786 this because the (current interpretation of the) JLS requires
14787 that the update expression be considered reachable even if the
14788 for loop's body doesn't complete normally. */
14789 if (update != NULL_TREE && update != empty_stmt_node)
14792 if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
14793 up2 = EXPR_WFL_NODE (up2);
14794 /* Try to detect constraint violations. These would be
14795 programming errors somewhere. */
14796 if (! IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (up2)))
14797 || TREE_CODE (up2) == LOOP_EXPR)
14799 SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
14801 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14805 /* Try to find the loop a block might be related to. This comprises
14806 the case where the LOOP_EXPR is found as the second operand of a
14807 COMPOUND_EXPR, because the loop happens to have an initialization
14808 part, then expressed as the first operand of the COMPOUND_EXPR. If
14809 the search finds something, 1 is returned. Otherwise, 0 is
14810 returned. The search is assumed to start from a
14811 LABELED_BLOCK_EXPR's block. */
14814 search_loop (tree statement)
14816 if (TREE_CODE (statement) == LOOP_EXPR)
14819 if (TREE_CODE (statement) == BLOCK)
14820 statement = BLOCK_SUBBLOCKS (statement);
14824 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14825 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14826 statement = TREE_OPERAND (statement, 1);
14828 return (TREE_CODE (statement) == LOOP_EXPR
14829 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14832 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14833 returned otherwise. */
14836 labeled_block_contains_loop_p (tree block, tree loop)
14841 if (LABELED_BLOCK_BODY (block) == loop)
14844 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14850 /* If the loop isn't surrounded by a labeled statement, create one and
14851 insert LOOP as its body. */
14854 patch_loop_statement (tree loop)
14858 TREE_TYPE (loop) = void_type_node;
14859 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14862 loop_label = build_labeled_block (0, NULL_TREE);
14863 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14864 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14865 LABELED_BLOCK_BODY (loop_label) = loop;
14866 PUSH_LABELED_BLOCK (loop_label);
14870 /* 14.13, 14.14: break and continue Statements */
14872 /* Build a break or a continue statement. a null NAME indicates an
14873 unlabeled break/continue statement. */
14876 build_bc_statement (int location, int is_break, tree name)
14878 tree break_continue, label_block_expr = NULL_TREE;
14882 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14883 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14884 /* Null means that we don't have a target for this named
14885 break/continue. In this case, we make the target to be the
14886 label name, so that the error can be reported accuratly in
14887 patch_bc_statement. */
14888 label_block_expr = EXPR_WFL_NODE (name);
14890 /* Unlabeled break/continue will be handled during the
14891 break/continue patch operation */
14893 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14895 IS_BREAK_STMT_P (break_continue) = is_break;
14896 TREE_SIDE_EFFECTS (break_continue) = 1;
14897 EXPR_WFL_LINECOL (break_continue) = location;
14898 break_continue = build_debugable_stmt (location, break_continue);
14899 return break_continue;
14902 /* Verification of a break/continue statement. */
14905 patch_bc_statement (tree node)
14907 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14908 tree labeled_block = ctxp->current_labeled_block;
14909 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14911 /* Having an identifier here means that the target is unknown. */
14912 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14914 parse_error_context (wfl_operator, "No label definition found for `%s'",
14915 IDENTIFIER_POINTER (bc_label));
14916 return error_mark_node;
14918 if (! IS_BREAK_STMT_P (node))
14920 /* It's a continue statement. */
14921 for (;; labeled_block = TREE_CHAIN (labeled_block))
14923 if (labeled_block == NULL_TREE)
14925 if (bc_label == NULL_TREE)
14926 parse_error_context (wfl_operator,
14927 "`continue' must be in loop");
14929 parse_error_context
14930 (wfl_operator, "continue label `%s' does not name a loop",
14931 IDENTIFIER_POINTER (bc_label));
14932 return error_mark_node;
14934 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14935 == continue_identifier_node)
14936 && (bc_label == NULL_TREE
14937 || TREE_CHAIN (labeled_block) == bc_label))
14939 bc_label = labeled_block;
14944 else if (!bc_label)
14946 for (;; labeled_block = TREE_CHAIN (labeled_block))
14948 if (labeled_block == NULL_TREE)
14950 parse_error_context (wfl_operator,
14951 "`break' must be in loop or switch");
14952 return error_mark_node;
14954 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14955 if (TREE_CODE (target_stmt) == SWITCH_EXPR
14956 || search_loop (target_stmt))
14958 bc_label = labeled_block;
14964 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14965 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14967 /* Our break/continue don't return values. */
14968 TREE_TYPE (node) = void_type_node;
14969 /* Encapsulate the break within a compound statement so that it's
14970 expanded all the times by expand_expr (and not clobbered
14971 sometimes, like after a if statement) */
14972 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14973 TREE_SIDE_EFFECTS (node) = 1;
14977 /* Process the exit expression belonging to a loop. Its type must be
14981 patch_exit_expr (tree node)
14983 tree expression = TREE_OPERAND (node, 0);
14984 TREE_TYPE (node) = error_mark_node;
14985 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14987 /* The type of expression must be boolean */
14988 if (TREE_TYPE (expression) != boolean_type_node)
14990 parse_error_context
14992 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
14993 lang_printable_name (TREE_TYPE (expression), 0));
14994 return error_mark_node;
14996 /* Now we know things are allright, invert the condition, fold and
14998 TREE_OPERAND (node, 0) =
14999 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15001 if (! integer_zerop (TREE_OPERAND (node, 0))
15002 && ctxp->current_loop != NULL_TREE
15003 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15004 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15005 if (! integer_onep (TREE_OPERAND (node, 0)))
15006 CAN_COMPLETE_NORMALLY (node) = 1;
15009 TREE_TYPE (node) = void_type_node;
15013 /* 14.9 Switch statement */
15016 patch_switch_statement (tree node)
15018 tree se = TREE_OPERAND (node, 0), se_type;
15021 /* Complete the switch expression */
15022 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15023 se_type = TREE_TYPE (se);
15024 /* The type of the switch expression must be char, byte, short or
15026 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15028 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15029 parse_error_context (wfl_operator,
15030 "Incompatible type for `switch'. Can't convert `%s' to `int'",
15031 lang_printable_name (se_type, 0));
15032 /* This is what java_complete_tree will check */
15033 TREE_OPERAND (node, 0) = error_mark_node;
15034 return error_mark_node;
15037 /* Save and restore the outer case label list. */
15038 save = case_label_list;
15039 case_label_list = NULL_TREE;
15041 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15043 /* See if we've found a duplicate label. We can't leave this until
15044 code generation, because in `--syntax-only' and `-C' modes we
15045 don't do ordinary code generation. */
15046 for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15048 HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15050 for (subiter = TREE_CHAIN (iter);
15051 subiter != NULL_TREE;
15052 subiter = TREE_CHAIN (subiter))
15054 HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15057 EXPR_WFL_LINECOL (wfl_operator)
15058 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15059 /* The case_label_list is in reverse order, so print the
15060 outer label first. */
15061 parse_error_context (wfl_operator, "duplicate case label: `"
15062 HOST_WIDE_INT_PRINT_DEC "'", subval);
15063 EXPR_WFL_LINECOL (wfl_operator)
15064 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15065 parse_error_context (wfl_operator, "original label is here");
15072 case_label_list = save;
15074 /* Ready to return */
15075 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15077 TREE_TYPE (node) = error_mark_node;
15078 return error_mark_node;
15080 TREE_TYPE (node) = void_type_node;
15081 TREE_SIDE_EFFECTS (node) = 1;
15082 CAN_COMPLETE_NORMALLY (node)
15083 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15084 || ! SWITCH_HAS_DEFAULT (node);
15090 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15091 might be NULL_TREE. */
15093 build_assertion (int location, tree condition, tree value)
15096 tree klass = GET_CPC ();
15098 if (! CLASS_USES_ASSERTIONS (klass))
15100 tree field, classdollar, id, call;
15101 tree class_type = TREE_TYPE (klass);
15103 field = add_field (class_type,
15104 get_identifier ("$assertionsDisabled"),
15106 ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15107 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15108 FIELD_SYNTHETIC (field) = 1;
15110 if (!TYPE_DOT_CLASS (class_type))
15111 build_dot_class_method (class_type);
15112 classdollar = build_dot_class_method_invocation (class_type);
15114 /* Call CLASS.desiredAssertionStatus(). */
15115 id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15116 call = build (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15117 call = make_qualified_primary (classdollar, call, location);
15118 TREE_SIDE_EFFECTS (call) = 1;
15120 /* Invert to obtain !CLASS.desiredAssertionStatus(). This may
15121 seem odd, but we do it to generate code identical to that of
15123 call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15124 TREE_SIDE_EFFECTS (call) = 1;
15125 DECL_INITIAL (field) = call;
15127 /* Record the initializer in the initializer statement list. */
15128 call = build (MODIFY_EXPR, NULL_TREE, field, call);
15129 TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15130 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15131 MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15133 CLASS_USES_ASSERTIONS (klass) = 1;
15136 if (value != NULL_TREE)
15137 value = tree_cons (NULL_TREE, value, NULL_TREE);
15139 node = build_wfl_node (get_identifier ("java"));
15140 node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15142 node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15145 node = build (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15146 TREE_SIDE_EFFECTS (node) = 1;
15147 /* It is too early to use BUILD_THROW. */
15148 node = build1 (THROW_EXPR, NULL_TREE, node);
15149 TREE_SIDE_EFFECTS (node) = 1;
15151 /* We invert the condition; if we just put NODE as the `else' part
15152 then we generate weird-looking bytecode. */
15153 condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15154 /* Check $assertionsDisabled. */
15156 = build (TRUTH_ANDIF_EXPR, NULL_TREE,
15157 build1 (TRUTH_NOT_EXPR, NULL_TREE,
15158 build_wfl_node (get_identifier ("$assertionsDisabled"))),
15160 node = build_if_else_statement (location, condition, node, NULL_TREE);
15164 /* 14.18 The try/catch statements */
15166 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15167 catches TYPE and executes CATCH_STMTS. */
15170 encapsulate_with_try_catch (int location, tree type, tree try_stmts,
15173 tree try_block, catch_clause_param, catch_block, catch;
15175 /* First build a try block */
15176 try_block = build_expr_block (try_stmts, NULL_TREE);
15178 /* Build a catch block: we need a catch clause parameter */
15179 catch_clause_param = build_decl (VAR_DECL,
15180 wpv_id, build_pointer_type (type));
15182 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15184 /* Initialize the variable and store in the block */
15185 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15186 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15187 add_stmt_to_block (catch_block, NULL_TREE, catch);
15189 /* Add the catch statements */
15190 add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15192 /* Now we can build a CATCH_EXPR */
15193 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
15195 return build_try_statement (location, try_block, catch_block);
15199 build_try_statement (int location, tree try_block, tree catches)
15201 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
15202 EXPR_WFL_LINECOL (node) = location;
15207 build_try_finally_statement (int location, tree try_block, tree finally)
15209 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15210 EXPR_WFL_LINECOL (node) = location;
15215 patch_try_statement (tree node)
15217 int error_found = 0;
15218 tree try = TREE_OPERAND (node, 0);
15219 /* Exception handlers are considered in left to right order */
15220 tree catch = nreverse (TREE_OPERAND (node, 1));
15221 tree current, caught_type_list = NULL_TREE;
15223 /* Check catch clauses, if any. Every time we find an error, we try
15224 to process the next catch clause. We process the catch clause before
15225 the try block so that when processing the try block we can check thrown
15226 exceptions againts the caught type list. */
15227 for (current = catch; current; current = TREE_CHAIN (current))
15229 tree carg_decl, carg_type;
15230 tree sub_current, catch_block, catch_clause;
15233 /* At this point, the structure of the catch clause is
15234 CATCH_EXPR (catch node)
15235 BLOCK (with the decl of the parameter)
15237 MODIFY_EXPR (assignment of the catch parameter)
15238 BLOCK (catch clause block)
15240 catch_clause = TREE_OPERAND (current, 0);
15241 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15242 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15244 /* Catch clauses can't have more than one parameter declared,
15245 but it's already enforced by the grammar. Make sure that the
15246 only parameter of the clause statement in of class Throwable
15247 or a subclass of Throwable, but that was done earlier. The
15248 catch clause parameter type has also been resolved. */
15250 /* Just make sure that the catch clause parameter type inherits
15251 from java.lang.Throwable */
15252 if (!inherits_from_p (carg_type, throwable_type_node))
15254 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15255 parse_error_context (wfl_operator,
15256 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15257 lang_printable_name (carg_type, 0));
15262 /* Partial check for unreachable catch statement: The catch
15263 clause is reachable iff is no earlier catch block A in
15264 the try statement such that the type of the catch
15265 clause's parameter is the same as or a subclass of the
15266 type of A's parameter */
15268 for (sub_current = catch;
15269 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15271 tree sub_catch_clause, decl;
15272 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15273 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15275 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15277 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15278 parse_error_context
15280 "`catch' not reached because of the catch clause at line %d",
15281 EXPR_WFL_LINENO (sub_current));
15282 unreachable = error_found = 1;
15286 /* Complete the catch clause block */
15287 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15288 if (catch_block == error_mark_node)
15293 if (CAN_COMPLETE_NORMALLY (catch_block))
15294 CAN_COMPLETE_NORMALLY (node) = 1;
15295 TREE_OPERAND (current, 0) = catch_block;
15300 /* Things to do here: the exception must be thrown */
15302 /* Link this type to the caught type list */
15303 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15306 PUSH_EXCEPTIONS (caught_type_list);
15307 if ((try = java_complete_tree (try)) == error_mark_node)
15309 if (CAN_COMPLETE_NORMALLY (try))
15310 CAN_COMPLETE_NORMALLY (node) = 1;
15313 /* Verification ends here */
15315 return error_mark_node;
15317 TREE_OPERAND (node, 0) = try;
15318 TREE_OPERAND (node, 1) = catch;
15319 TREE_TYPE (node) = void_type_node;
15323 /* 14.17 The synchronized Statement */
15326 patch_synchronized_statement (tree node, tree wfl_op1)
15328 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15329 tree block = TREE_OPERAND (node, 1);
15331 tree tmp, enter, exit, expr_decl, assignment;
15333 if (expr == error_mark_node)
15335 block = java_complete_tree (block);
15339 /* We might be trying to synchronize on a STRING_CST */
15340 if ((tmp = patch_string (expr)))
15343 /* The TYPE of expr must be a reference type */
15344 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15346 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15347 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15348 lang_printable_name (TREE_TYPE (expr), 0));
15349 return error_mark_node;
15352 if (flag_emit_xref)
15354 TREE_OPERAND (node, 0) = expr;
15355 TREE_OPERAND (node, 1) = java_complete_tree (block);
15356 CAN_COMPLETE_NORMALLY (node) = 1;
15360 /* Generate a try-finally for the synchronized statement, except
15361 that the handler that catches all throw exception calls
15362 _Jv_MonitorExit and then rethrow the exception.
15363 The synchronized statement is then implemented as:
15366 _Jv_MonitorEnter (expression)
15368 _Jv_MonitorExit (expression)
15372 e = _Jv_exception_info ();
15373 _Jv_MonitorExit (expression)
15377 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15378 BUILD_MONITOR_ENTER (enter, expr_decl);
15379 BUILD_MONITOR_EXIT (exit, expr_decl);
15380 CAN_COMPLETE_NORMALLY (enter) = 1;
15381 CAN_COMPLETE_NORMALLY (exit) = 1;
15382 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15383 TREE_SIDE_EFFECTS (assignment) = 1;
15384 node = build (COMPOUND_EXPR, NULL_TREE,
15385 build (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15386 build (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15387 node = build_expr_block (node, expr_decl);
15389 return java_complete_tree (node);
15392 /* 14.16 The throw Statement */
15395 patch_throw_statement (tree node, tree wfl_op1)
15397 tree expr = TREE_OPERAND (node, 0);
15398 tree type = TREE_TYPE (expr);
15399 int unchecked_ok = 0, tryblock_throws_ok = 0;
15401 /* Thrown expression must be assignable to java.lang.Throwable */
15402 if (!try_reference_assignconv (throwable_type_node, expr))
15404 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15405 parse_error_context (wfl_operator,
15406 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15407 lang_printable_name (type, 0));
15408 /* If the thrown expression was a reference, we further the
15409 compile-time check. */
15410 if (!JREFERENCE_TYPE_P (type))
15411 return error_mark_node;
15414 /* At least one of the following must be true */
15416 /* The type of the throw expression is a not checked exception,
15417 i.e. is a unchecked expression. */
15418 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15420 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15421 /* An instance can't throw a checked exception unless that exception
15422 is explicitly declared in the `throws' clause of each
15423 constructor. This doesn't apply to anonymous classes, since they
15424 don't have declared constructors. */
15426 && DECL_INSTINIT_P (current_function_decl)
15427 && !ANONYMOUS_CLASS_P (current_class))
15430 for (current = TYPE_METHODS (current_class); current;
15431 current = TREE_CHAIN (current))
15432 if (DECL_CONSTRUCTOR_P (current)
15433 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15435 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)",
15436 lang_printable_name (TREE_TYPE (expr), 0));
15437 return error_mark_node;
15441 /* Throw is contained in a try statement and at least one catch
15442 clause can receive the thrown expression or the current method is
15443 declared to throw such an exception. Or, the throw statement is
15444 contained in a method or constructor declaration and the type of
15445 the Expression is assignable to at least one type listed in the
15446 throws clause the declaration. */
15448 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15449 if (!(unchecked_ok || tryblock_throws_ok))
15451 /* If there is a surrounding try block that has no matching
15452 clatch clause, report it first. A surrounding try block exits
15453 only if there is something after the list of checked
15454 exception thrown by the current function (if any). */
15455 if (IN_TRY_BLOCK_P ())
15456 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15457 lang_printable_name (type, 0));
15458 /* If we have no surrounding try statement and the method doesn't have
15459 any throws, report it now. FIXME */
15461 /* We report that the exception can't be throw from a try block
15462 in all circumstances but when the `throw' is inside a static
15464 else if (!EXCEPTIONS_P (currently_caught_type_list)
15465 && !tryblock_throws_ok)
15467 if (DECL_CLINIT_P (current_function_decl))
15468 parse_error_context (wfl_operator,
15469 "Checked exception `%s' can't be thrown in initializer",
15470 lang_printable_name (type, 0));
15472 parse_error_context (wfl_operator,
15473 "Checked exception `%s' isn't thrown from a `try' block",
15474 lang_printable_name (type, 0));
15476 /* Otherwise, the current method doesn't have the appropriate
15477 throws declaration */
15479 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15480 lang_printable_name (type, 0));
15481 return error_mark_node;
15484 if (! flag_emit_class_files && ! flag_emit_xref)
15485 BUILD_THROW (node, expr);
15487 /* If doing xrefs, keep the location where the `throw' was seen. */
15488 if (flag_emit_xref)
15489 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15493 /* Check that exception said to be thrown by method DECL can be
15494 effectively caught from where DECL is invoked. */
15497 check_thrown_exceptions (int location, tree decl)
15500 /* For all the unchecked exceptions thrown by DECL */
15501 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15502 throws = TREE_CHAIN (throws))
15503 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15506 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15507 if (DECL_NAME (decl) == get_identifier ("clone"))
15510 EXPR_WFL_LINECOL (wfl_operator) = location;
15511 if (DECL_FINIT_P (current_function_decl))
15512 parse_error_context
15513 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15514 lang_printable_name (TREE_VALUE (throws), 0));
15517 parse_error_context
15518 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15519 lang_printable_name (TREE_VALUE (throws), 0),
15520 (DECL_INIT_P (current_function_decl) ?
15521 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15522 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15527 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15528 try-catch blocks, OR is listed in the `throws' clause of the
15532 check_thrown_exceptions_do (tree exception)
15534 tree list = currently_caught_type_list;
15535 resolve_and_layout (exception, NULL_TREE);
15536 /* First, all the nested try-catch-finally at that stage. The
15537 last element contains `throws' clause exceptions, if any. */
15538 if (IS_UNCHECKED_EXCEPTION_P (exception))
15543 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15544 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15546 list = TREE_CHAIN (list);
15552 purge_unchecked_exceptions (tree mdecl)
15554 tree throws = DECL_FUNCTION_THROWS (mdecl);
15555 tree new = NULL_TREE;
15559 tree next = TREE_CHAIN (throws);
15560 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15562 TREE_CHAIN (throws) = new;
15567 /* List is inverted here, but it doesn't matter */
15568 DECL_FUNCTION_THROWS (mdecl) = new;
15571 /* This function goes over all of CLASS_TYPE ctors and checks whether
15572 each of them features at least one unchecked exception in its
15573 `throws' clause. If it's the case, it returns `true', `false'
15577 ctors_unchecked_throws_clause_p (tree class_type)
15581 for (current = TYPE_METHODS (class_type); current;
15582 current = TREE_CHAIN (current))
15584 bool ctu = false; /* Ctor Throws Unchecked */
15585 if (DECL_CONSTRUCTOR_P (current))
15588 for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
15589 throws = TREE_CHAIN (throws))
15590 if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
15593 /* We return false as we found one ctor that is unfit. */
15594 if (!ctu && DECL_CONSTRUCTOR_P (current))
15597 /* All ctors feature at least one unchecked exception in their
15598 `throws' clause. */
15602 /* 15.24 Conditional Operator ?: */
15605 patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
15607 tree cond = TREE_OPERAND (node, 0);
15608 tree op1 = TREE_OPERAND (node, 1);
15609 tree op2 = TREE_OPERAND (node, 2);
15610 tree resulting_type = NULL_TREE;
15611 tree t1, t2, patched;
15612 int error_found = 0;
15614 /* Operands of ?: might be StringBuffers crafted as a result of a
15615 string concatenation. Obtain a descent operand here. */
15616 if ((patched = patch_string (op1)))
15617 TREE_OPERAND (node, 1) = op1 = patched;
15618 if ((patched = patch_string (op2)))
15619 TREE_OPERAND (node, 2) = op2 = patched;
15621 t1 = TREE_TYPE (op1);
15622 t2 = TREE_TYPE (op2);
15624 /* The first expression must be a boolean */
15625 if (TREE_TYPE (cond) != boolean_type_node)
15627 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15628 parse_error_context (wfl_operator,
15629 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15630 lang_printable_name (TREE_TYPE (cond), 0));
15634 /* Second and third can be numeric, boolean (i.e. primitive),
15635 references or null. Anything else results in an error */
15636 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15637 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15638 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15639 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15642 /* Determine the type of the conditional expression. Same types are
15643 easy to deal with */
15645 resulting_type = t1;
15647 /* There are different rules for numeric types */
15648 else if (JNUMERIC_TYPE_P (t1))
15650 /* if byte/short found, the resulting type is short */
15651 if ((t1 == byte_type_node && t2 == short_type_node)
15652 || (t1 == short_type_node && t2 == byte_type_node))
15653 resulting_type = short_type_node;
15655 /* If t1 is a constant int and t2 is of type byte, short or char
15656 and t1's value fits in t2, then the resulting type is t2 */
15657 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15658 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15659 resulting_type = t2;
15661 /* If t2 is a constant int and t1 is of type byte, short or char
15662 and t2's value fits in t1, then the resulting type is t1 */
15663 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15664 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15665 resulting_type = t1;
15667 /* Otherwise, binary numeric promotion is applied and the
15668 resulting type is the promoted type of operand 1 and 2 */
15670 resulting_type = binary_numeric_promotion (t1, t2,
15671 &TREE_OPERAND (node, 1),
15672 &TREE_OPERAND (node, 2));
15675 /* Cases of a reference and a null type */
15676 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15677 resulting_type = t1;
15679 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15680 resulting_type = t2;
15682 /* Last case: different reference types. If a type can be converted
15683 into the other one by assignment conversion, the latter
15684 determines the type of the expression */
15685 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15686 resulting_type = promote_type (t1);
15688 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15689 resulting_type = promote_type (t2);
15691 /* If we don't have any resulting type, we're in trouble */
15692 if (!resulting_type)
15694 char *t = xstrdup (lang_printable_name (t1, 0));
15695 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15696 parse_error_context (wfl_operator,
15697 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15698 t, lang_printable_name (t2, 0));
15705 TREE_TYPE (node) = error_mark_node;
15706 return error_mark_node;
15709 TREE_TYPE (node) = resulting_type;
15710 TREE_SET_CODE (node, COND_EXPR);
15711 CAN_COMPLETE_NORMALLY (node) = 1;
15715 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
15718 maybe_build_class_init_for_field (tree decl, tree expr)
15720 tree clas = DECL_CONTEXT (decl);
15721 if (flag_emit_class_files || flag_emit_xref)
15724 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
15725 && FIELD_FINAL (decl))
15727 tree init = DECL_INITIAL (decl);
15728 if (init != NULL_TREE)
15729 init = fold_constant_for_init (init, decl);
15730 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
15734 return build_class_init (clas, expr);
15737 /* Try to constant fold NODE.
15738 If NODE is not a constant expression, return NULL_EXPR.
15739 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15742 fold_constant_for_init (tree node, tree context)
15744 tree op0, op1, val;
15745 enum tree_code code = TREE_CODE (node);
15750 if (node == null_pointer_node)
15759 case TRUNC_MOD_EXPR:
15767 case TRUTH_ANDIF_EXPR:
15768 case TRUTH_ORIF_EXPR:
15775 op0 = TREE_OPERAND (node, 0);
15776 op1 = TREE_OPERAND (node, 1);
15777 val = fold_constant_for_init (op0, context);
15778 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15780 TREE_OPERAND (node, 0) = val;
15781 val = fold_constant_for_init (op1, context);
15782 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15784 TREE_OPERAND (node, 1) = val;
15785 return patch_binop (node, op0, op1);
15787 case UNARY_PLUS_EXPR:
15789 case TRUTH_NOT_EXPR:
15792 op0 = TREE_OPERAND (node, 0);
15793 val = fold_constant_for_init (op0, context);
15794 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15796 TREE_OPERAND (node, 0) = val;
15797 return patch_unaryop (node, op0);
15801 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15802 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15804 TREE_OPERAND (node, 0) = val;
15805 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15806 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15808 TREE_OPERAND (node, 1) = val;
15809 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15810 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15812 TREE_OPERAND (node, 2) = val;
15813 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15814 : TREE_OPERAND (node, 2);
15818 if (! FIELD_FINAL (node)
15819 || DECL_INITIAL (node) == NULL_TREE)
15821 val = DECL_INITIAL (node);
15822 /* Guard against infinite recursion. */
15823 DECL_INITIAL (node) = NULL_TREE;
15824 val = fold_constant_for_init (val, node);
15825 if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
15826 val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
15827 DECL_INITIAL (node) = val;
15830 case EXPR_WITH_FILE_LOCATION:
15831 /* Compare java_complete_tree and resolve_expression_name. */
15832 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15833 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15835 tree name = EXPR_WFL_NODE (node);
15837 if (PRIMARY_P (node))
15839 else if (! QUALIFIED_P (name))
15841 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15842 if (decl == NULL_TREE
15843 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15845 return fold_constant_for_init (decl, decl);
15849 /* Install the proper context for the field resolution.
15850 The prior context is restored once the name is
15851 properly qualified. */
15852 tree saved_current_class = current_class;
15853 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15854 current_class = DECL_CONTEXT (context);
15855 qualify_ambiguous_name (node);
15856 current_class = saved_current_class;
15857 if (resolve_field_access (node, &decl, NULL)
15858 && decl != NULL_TREE)
15859 return fold_constant_for_init (decl, decl);
15865 op0 = TREE_OPERAND (node, 0);
15866 val = fold_constant_for_init (op0, context);
15867 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15869 TREE_OPERAND (node, 0) = val;
15873 #ifdef USE_COMPONENT_REF
15875 case COMPONENT_REF:
15884 #ifdef USE_COMPONENT_REF
15885 /* Context is 'T' for TypeName, 'P' for PackageName,
15886 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15889 resolve_simple_name (tree name, int context)
15894 resolve_qualified_name (tree name, int context)
15900 init_src_parse (void)
15902 /* Sanity check; we've been bit by this before. */
15903 if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
15909 /* This section deals with the functions that are called when tables
15910 recording class initialization information are traversed. */
15912 /* Attach to PTR (a block) the declaration found in ENTRY. */
15915 attach_init_test_initialization_flags (void **entry, void *ptr)
15917 tree block = (tree)ptr;
15918 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
15920 if (block != error_mark_node)
15922 TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
15923 BLOCK_EXPR_DECLS (block) = ite->value;
15928 /* This function is called for each classes that is known definitely
15929 assigned when a given static method was called. This function
15930 augments a compound expression (INFO) storing all assignment to
15931 initialized static class flags if a flag already existed, otherwise
15932 a new one is created. */
15935 emit_test_initialization (void **entry_p, void *info)
15937 tree l = (tree) info;
15939 tree key = (tree) *entry_p;
15941 htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
15943 /* If we haven't found a flag and we're dealing with self registered
15944 with current_function_decl, then don't do anything. Self is
15945 always added as definitely initialized but this information is
15946 valid only if used outside the current function. */
15947 if (current_function_decl == TREE_PURPOSE (l)
15948 && java_treetreehash_find (cf_ht, key) == NULL)
15951 ite = java_treetreehash_new (cf_ht, key);
15953 /* If we don't have a variable, create one and install it. */
15958 decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
15959 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
15960 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
15961 DECL_CONTEXT (decl) = current_function_decl;
15962 DECL_INITIAL (decl) = boolean_true_node;
15964 /* The trick is to find the right context for it. */
15965 block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
15966 TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
15967 BLOCK_EXPR_DECLS (block) = decl;
15973 /* Now simply augment the compound that holds all the assignments
15974 pertaining to this method invocation. */
15975 init = build (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
15976 TREE_SIDE_EFFECTS (init) = 1;
15977 TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
15978 TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
15983 #include "gt-java-parse.h"
15984 #include "gtype-java.h"