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, 2004, 2005
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, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, 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"
60 #include "pretty-print.h"
61 #include "diagnostic.h"
63 #include "java-tree.h"
74 #include "tree-inline.h"
78 /* Local function prototypes */
79 static char *java_accstring_lookup (int);
80 static const char *accessibility_string (int);
81 static void classitf_redefinition_error (const char *,tree, tree, tree);
82 static void variable_redefinition_error (tree, tree, tree, int);
83 static tree create_class (int, tree, tree, tree);
84 static tree create_interface (int, tree, tree);
85 static void end_class_declaration (int);
86 static tree find_field (tree, tree);
87 static tree lookup_field_wrapper (tree, tree);
88 static int duplicate_declaration_error_p (tree, tree, tree);
89 static void register_fields (int, tree, tree);
90 static tree parser_qualified_classname (tree);
91 static int parser_check_super (tree, tree, tree);
92 static int parser_check_super_interface (tree, tree, tree);
93 static void check_modifiers_consistency (int);
94 static tree lookup_cl (tree);
95 static tree lookup_java_method2 (tree, tree, int);
96 static tree method_header (int, tree, tree, tree);
97 static void fix_method_argument_names (tree ,tree);
98 static tree method_declarator (tree, tree);
99 static void parse_warning_context (tree cl, const char *gmsgid, ...) ATTRIBUTE_GCC_DIAG(2,3);
100 #ifdef USE_MAPPED_LOCATION
101 static void issue_warning_error_from_context
102 (source_location, const char *gmsgid, va_list *);
104 static void issue_warning_error_from_context
105 (tree, const char *gmsgid, va_list *);
107 static void parse_ctor_invocation_error (void);
108 static tree parse_jdk1_1_error (const char *);
109 static void complete_class_report_errors (jdep *);
110 static int process_imports (void);
111 static void read_import_dir (tree);
112 static int find_in_imports_on_demand (tree, tree);
113 static void find_in_imports (tree, tree);
114 static void check_inner_class_access (tree, tree, tree);
115 static int check_pkg_class_access (tree, tree, bool, tree);
116 static tree resolve_package (tree, tree *, tree *);
117 static tree resolve_class (tree, tree, tree, tree);
118 static void declare_local_variables (int, tree, tree);
119 static void dump_java_tree (enum tree_dump_index, tree);
120 static void source_start_java_method (tree);
121 static void source_end_java_method (void);
122 static tree find_name_in_single_imports (tree);
123 static void check_abstract_method_header (tree);
124 static tree lookup_java_interface_method2 (tree, tree);
125 static tree resolve_expression_name (tree, tree *);
126 static tree maybe_create_class_interface_decl (tree, tree, tree, tree);
127 static int check_class_interface_creation (int, int, tree, tree, tree, tree);
128 static tree patch_method_invocation (tree, tree, tree, int, int *, tree *);
129 static tree resolve_and_layout (tree, tree);
130 static tree qualify_and_find (tree, tree, tree);
131 static tree resolve_no_layout (tree, tree);
132 static int invocation_mode (tree, int);
133 static tree find_applicable_accessible_methods_list (int, tree, tree, tree);
134 static void search_applicable_methods_list (int, tree, tree, tree, tree *, tree *);
135 static tree find_most_specific_methods_list (tree);
136 static int argument_types_convertible (tree, tree);
137 static tree patch_invoke (tree, tree, tree);
138 static int maybe_use_access_method (int, tree *, tree *);
139 static tree lookup_method_invoke (int, tree, tree, tree, tree);
140 static tree register_incomplete_type (int, tree, tree, tree);
141 static tree check_inner_circular_reference (tree, tree);
142 static tree check_circular_reference (tree);
143 static tree obtain_incomplete_type (tree);
144 static tree java_complete_lhs (tree);
145 static tree java_complete_tree (tree);
146 static tree maybe_generate_pre_expand_clinit (tree);
147 static int analyze_clinit_body (tree, tree);
148 static int maybe_yank_clinit (tree);
149 static void start_complete_expand_method (tree);
150 static void java_complete_expand_method (tree);
151 static void java_expand_method_bodies (tree);
152 static int unresolved_type_p (tree, tree *);
153 static void create_jdep_list (struct parser_ctxt *);
154 static tree build_expr_block (tree, tree);
155 static tree enter_block (void);
156 static tree exit_block (void);
157 static tree lookup_name_in_blocks (tree);
158 static void maybe_absorb_scoping_blocks (void);
159 static tree build_method_invocation (tree, tree);
160 static tree build_new_invocation (tree, tree);
161 static tree build_assignment (int, int, tree, tree);
162 static tree build_binop (enum tree_code, int, tree, tree);
163 static tree patch_assignment (tree, tree);
164 static tree patch_binop (tree, tree, tree, int);
165 static tree build_unaryop (int, int, tree);
166 static tree build_incdec (int, int, tree, int);
167 static tree patch_unaryop (tree, tree);
168 static tree build_cast (int, tree, tree);
169 static tree build_null_of_type (tree);
170 static tree patch_cast (tree, tree);
171 static int valid_ref_assignconv_cast_p (tree, tree, int);
172 static int valid_builtin_assignconv_identity_widening_p (tree, tree);
173 static int valid_cast_to_p (tree, tree);
174 static int valid_method_invocation_conversion_p (tree, tree);
175 static tree try_builtin_assignconv (tree, tree, tree);
176 static tree try_reference_assignconv (tree, tree);
177 static tree build_unresolved_array_type (tree);
178 static int build_type_name_from_array_name (tree, tree *);
179 static tree build_array_from_name (tree, tree, tree, tree *);
180 static tree build_array_ref (int, tree, tree);
181 static tree patch_array_ref (tree);
182 #ifdef USE_MAPPED_LOCATION
183 static tree make_qualified_name (tree, tree, source_location);
185 static tree make_qualified_name (tree, tree, int);
187 static tree merge_qualified_name (tree, tree);
188 static tree make_qualified_primary (tree, tree, int);
189 static int resolve_qualified_expression_name (tree, tree *, tree *, tree *);
190 static void qualify_ambiguous_name (tree);
191 static tree resolve_field_access (tree, tree *, tree *);
192 static tree build_newarray_node (tree, tree, int);
193 static tree patch_newarray (tree);
194 static tree resolve_type_during_patch (tree);
195 static tree build_this (int);
196 static tree build_wfl_wrap (tree, int);
197 static tree build_return (int, tree);
198 static tree patch_return (tree);
199 static tree maybe_access_field (tree, tree, tree);
200 static int complete_function_arguments (tree);
201 static int check_for_static_method_reference (tree, tree, tree, tree, tree);
202 static int not_accessible_p (tree, tree, tree, int);
203 static void check_deprecation (tree, tree);
204 static int class_in_current_package (tree);
205 static tree build_if_else_statement (int, tree, tree, tree);
206 static tree patch_if_else_statement (tree);
207 static tree add_stmt_to_block (tree, tree, tree);
208 static tree patch_exit_expr (tree);
209 static tree build_labeled_block (int, tree);
210 static tree finish_labeled_statement (tree, tree);
211 static tree build_bc_statement (int, int, tree);
212 static tree patch_bc_statement (tree);
213 static tree patch_loop_statement (tree);
214 static tree build_new_loop (tree);
215 static tree build_loop_body (int, tree, int);
216 static tree finish_loop_body (int, tree, tree, int);
217 static tree build_debugable_stmt (int, tree);
218 static tree finish_for_loop (int, tree, tree, tree);
219 static tree patch_switch_statement (tree);
220 static tree string_constant_concatenation (tree, tree);
221 static tree build_string_concatenation (tree, tree);
222 static tree patch_string_cst (tree);
223 static tree patch_string (tree);
224 static tree encapsulate_with_try_catch (int, tree, tree, tree);
225 #ifdef USE_MAPPED_LOCATION
226 static tree build_assertion (source_location, tree, tree);
228 static tree build_assertion (int, tree, tree);
230 static tree build_try_statement (int, tree, tree);
231 static tree build_try_finally_statement (int, tree, tree);
232 static tree patch_try_statement (tree);
233 static tree patch_synchronized_statement (tree, tree);
234 static tree patch_throw_statement (tree, tree);
235 #ifdef USE_MAPPED_LOCATION
236 static void check_thrown_exceptions (source_location, tree, tree);
238 static void check_thrown_exceptions (int, tree, tree);
240 static int check_thrown_exceptions_do (tree);
241 static void purge_unchecked_exceptions (tree);
242 static bool ctors_unchecked_throws_clause_p (tree);
243 static void check_concrete_throws_clauses (tree, tree, tree, tree);
244 static void check_throws_clauses (tree, tree, tree);
245 static void finish_method_declaration (tree);
246 static tree build_super_invocation (tree);
247 static int verify_constructor_circularity (tree, tree);
248 static char *constructor_circularity_msg (tree, tree);
249 static tree build_this_super_qualified_invocation (int, tree, tree, int, int);
250 static const char *get_printable_method_name (tree);
251 static tree patch_conditional_expr (tree, tree, tree);
252 static tree generate_finit (tree);
253 static tree generate_instinit (tree);
254 static tree build_instinit_invocation (tree);
255 static void fix_constructors (tree);
256 static tree build_alias_initializer_parameter_list (int, tree, tree, int *);
257 static tree craft_constructor (tree, tree);
258 static int verify_constructor_super (tree);
259 static tree create_artificial_method (tree, int, tree, tree, tree);
260 static void start_artificial_method_body (tree);
261 static void end_artificial_method_body (tree);
262 static int check_method_redefinition (tree, tree);
263 static int check_method_types_complete (tree);
264 static bool hack_is_accessible_p (tree, tree);
265 static void java_check_regular_methods (tree);
266 static void check_interface_throws_clauses (tree, tree);
267 static void java_check_abstract_methods (tree);
268 static void unreachable_stmt_error (tree);
269 static int not_accessible_field_error (tree, tree);
270 static tree find_expr_with_wfl (tree);
271 static void missing_return_error (tree);
272 static tree build_new_array_init (int, tree);
273 static tree patch_new_array_init (tree, tree);
274 static tree maybe_build_array_element_wfl (tree);
275 static int array_constructor_check_entry (tree, tree);
276 static const char *purify_type_name (const char *);
277 static tree fold_constant_for_init (tree, tree);
278 static jdeplist *reverse_jdep_list (struct parser_ctxt *);
279 static void static_ref_err (tree, tree, tree);
280 static void parser_add_interface (tree, tree, tree);
281 static void add_superinterfaces (tree, tree);
282 static tree jdep_resolve_class (jdep *);
283 static int note_possible_classname (const char *, int);
284 static void java_complete_expand_classes (void);
285 static void java_complete_expand_class (tree);
286 static void java_complete_expand_methods (tree);
287 static tree cut_identifier_in_qualified (tree);
288 static tree java_stabilize_reference (tree);
289 static tree do_unary_numeric_promotion (tree);
290 static char * operator_string (tree);
291 static tree do_merge_string_cste (tree, const char *, int, int);
292 static tree merge_string_cste (tree, tree, int);
293 static tree java_refold (tree);
294 static int java_decl_equiv (tree, tree);
295 static int binop_compound_p (enum tree_code);
296 static tree search_loop (tree);
297 static int labeled_block_contains_loop_p (tree, tree);
298 static int check_abstract_method_definitions (int, tree, tree);
299 static void java_check_abstract_method_definitions (tree);
300 static void java_debug_context_do (int);
301 static void java_parser_context_push_initialized_field (void);
302 static void java_parser_context_pop_initialized_field (void);
303 static tree reorder_static_initialized (tree);
304 static void java_parser_context_suspend (void);
305 static void java_parser_context_resume (void);
306 static int pop_current_osb (struct parser_ctxt *);
308 /* JDK 1.1 work. FIXME */
310 static tree maybe_make_nested_class_name (tree);
311 static int make_nested_class_name (tree);
312 static void link_nested_class_to_enclosing (void);
313 static tree resolve_inner_class (htab_t, tree, tree *, tree *, tree);
314 static tree find_as_inner_class (tree, tree, tree);
315 static tree find_as_inner_class_do (tree, tree);
316 static int check_inner_class_redefinition (tree, tree);
318 static tree build_thisn_assign (void);
319 static tree build_current_thisn (tree);
320 static tree build_access_to_thisn (tree, tree, int);
321 static tree maybe_build_thisn_access_method (tree);
323 static tree build_nested_field_access (tree, tree);
324 static tree build_nested_field_access_methods (tree);
325 static tree build_nested_field_access_method (tree, tree, tree, tree, tree);
326 static tree build_nested_field_access_expr (int, tree, tree, tree, tree);
327 static tree build_outer_method_access_method (tree);
328 static tree build_new_access_id (void);
330 static int nested_field_access_p (tree, tree);
331 static int nested_field_expanded_access_p (tree, tree *, tree *, tree *);
332 static tree nested_field_access_fix (tree, tree, tree);
334 static tree build_incomplete_class_ref (int, tree);
335 static tree patch_incomplete_class_ref (tree);
336 static tree create_anonymous_class (tree);
337 static void patch_anonymous_class (tree, tree, tree);
338 static void add_inner_class_fields (tree, tree);
340 static tree build_dot_class_method (tree);
341 static tree build_dot_class_method_invocation (tree, tree);
342 static void create_new_parser_context (int);
343 static tree maybe_build_class_init_for_field (tree, tree);
345 static int emit_test_initialization (void **, void *);
347 static char *string_convert_int_cst (tree);
349 /* Number of error found so far. */
350 int java_error_count;
351 /* Number of warning found so far. */
352 int java_warning_count;
353 /* Cyclic inheritance report, as it can be set by layout_class */
354 const char *cyclic_inheritance_report;
356 /* The current parser context */
357 struct parser_ctxt *ctxp;
359 /* List of things that were analyzed for which code will be generated */
360 struct parser_ctxt *ctxp_for_generation = NULL;
361 struct parser_ctxt *ctxp_for_generation_last = NULL;
363 /* binop_lookup maps token to tree_code. It is used where binary
364 operations are involved and required by the parser. RDIV_EXPR
365 covers both integral/floating point division. The code is changed
366 once the type of both operator is worked out. */
368 static const enum tree_code binop_lookup[19] =
370 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
371 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
372 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
373 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
374 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
376 #define BINOP_LOOKUP(VALUE) \
377 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
379 /* This is the end index for binary operators that can also be used
380 in compound assignments. */
381 #define BINOP_COMPOUND_CANDIDATES 11
383 /* The "$L" identifier we use to create labels. */
384 static GTY(()) tree label_id;
386 /* The "StringBuffer" identifier used for the String `+' operator. */
387 static GTY(()) tree wfl_string_buffer;
389 /* The "append" identifier used for String `+' operator. */
390 static GTY(()) tree wfl_append;
392 /* The "toString" identifier used for String `+' operator. */
393 static GTY(()) tree wfl_to_string;
395 /* The "java.lang" import qualified name. */
396 static GTY(()) tree java_lang_id;
398 /* The generated `inst$' identifier used for generated enclosing
399 instance/field access functions. */
400 static GTY(()) tree inst_id;
402 /* Context and flag for static blocks */
403 static GTY(()) tree current_static_block;
405 /* The generated `write_parm_value$' identifier. */
406 static GTY(()) tree wpv_id;
408 /* Hold THIS for the scope of the current method decl. */
409 static GTY(()) tree current_this;
411 /* Hold a list of catch clauses list. The first element of this list is
412 the list of the catch clauses of the currently analyzed try block. */
413 static GTY(()) tree currently_caught_type_list;
415 /* This holds a linked list of all the case labels for the current
416 switch statement. It is only used when checking to see if there
417 are duplicate labels. FIXME: probably this should just be attached
418 to the switch itself; then it could be referenced via
419 `ctxp->current_loop'. */
420 static GTY(()) tree case_label_list;
422 /* Anonymous class counter. Will be reset to 1 every time a non
423 anonymous class gets created. */
424 static int anonymous_class_counter = 1;
426 static GTY(()) tree src_parse_roots[1];
428 /* All classes seen from source code */
429 #define gclass_list src_parse_roots[0]
431 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
432 line and point it out. */
433 /* Should point out the one that don't fit. ASCII/unicode, going
436 #define check_modifiers(__message, __value, __mask) do { \
437 if ((__value) & ~(__mask)) \
439 size_t i, remainder = (__value) & ~(__mask); \
440 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++) \
441 if ((1 << i) & remainder) \
442 parse_error_context (ctxp->modifier_ctx [i], (__message), \
443 java_accstring_lookup (1 << i)); \
454 #ifdef USE_MAPPED_LOCATION
455 source_location location;
464 #ifdef USE_MAPPED_LOCATION
465 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
466 SET_EXPR_LOCATION(EXPR, (TOKEN).location)
468 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
469 (EXPR_WFL_LINECOL (EXPR) = (TOKEN).location)
477 /* Things defined here have to match the order of what's in the
478 binop_lookup table. */
480 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
481 %token LS_TK SRS_TK ZRS_TK
482 %token AND_TK XOR_TK OR_TK
483 %token BOOL_AND_TK BOOL_OR_TK
484 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
486 /* This maps to the same binop_lookup entry than the token above */
488 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
490 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
491 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
494 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
496 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
497 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
498 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
499 %token PAD_TK ABSTRACT_TK STRICT_TK
502 /* Keep those two in order, too */
503 %token DECR_TK INCR_TK
505 /* From now one, things can be in any order */
507 %token DEFAULT_TK IF_TK THROW_TK
508 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
509 %token THROWS_TK BREAK_TK IMPORT_TK
510 %token ELSE_TK INSTANCEOF_TK RETURN_TK
511 %token VOID_TK CATCH_TK INTERFACE_TK
512 %token CASE_TK EXTENDS_TK FINALLY_TK
513 %token SUPER_TK WHILE_TK CLASS_TK
514 %token SWITCH_TK CONST_TK TRY_TK
515 %token FOR_TK NEW_TK CONTINUE_TK
516 %token GOTO_TK PACKAGE_TK THIS_TK
519 %token BYTE_TK SHORT_TK INT_TK LONG_TK
520 %token CHAR_TK INTEGRAL_TK
522 %token FLOAT_TK DOUBLE_TK FP_TK
526 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
528 %token ASSIGN_ANY_TK ASSIGN_TK
529 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
531 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
532 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
534 %type <value> modifiers MODIFIER_TK final synchronized
536 %type <node> super ID_TK identifier
537 %type <node> name simple_name qualified_name
538 %type <node> type_declaration compilation_unit
539 field_declaration method_declaration extends_interfaces
540 interfaces interface_type_list
541 import_declarations package_declaration
542 type_declarations interface_body
543 interface_member_declaration constant_declaration
544 interface_member_declarations interface_type
545 abstract_method_declaration
546 %type <node> class_body_declaration class_member_declaration
547 static_initializer constructor_declaration block
548 %type <node> class_body_declarations constructor_header
549 %type <node> class_or_interface_type class_type class_type_list
550 constructor_declarator explicit_constructor_invocation
551 %type <node> dim_expr dim_exprs this_or_super throws
553 %type <node> variable_declarator_id variable_declarator
554 variable_declarators variable_initializer
555 variable_initializers constructor_body
558 %type <node> class_body block_end constructor_block_end
559 %type <node> statement statement_without_trailing_substatement
560 labeled_statement if_then_statement label_decl
561 if_then_else_statement while_statement for_statement
562 statement_nsi labeled_statement_nsi do_statement
563 if_then_else_statement_nsi while_statement_nsi
564 for_statement_nsi statement_expression_list for_init
565 for_update statement_expression expression_statement
566 primary_no_new_array expression primary array_type
567 array_creation_initialized array_creation_uninitialized
568 class_instance_creation_expression field_access
569 method_invocation array_access something_dot_new
570 argument_list postfix_expression while_expression
571 post_increment_expression post_decrement_expression
572 unary_expression_not_plus_minus unary_expression
573 pre_increment_expression pre_decrement_expression
575 multiplicative_expression additive_expression
576 shift_expression relational_expression
577 equality_expression and_expression
578 exclusive_or_expression inclusive_or_expression
579 conditional_and_expression conditional_or_expression
580 conditional_expression assignment_expression
581 left_hand_side assignment for_header for_begin
582 constant_expression do_statement_begin empty_statement
583 switch_statement synchronized_statement throw_statement
584 try_statement assert_statement
585 switch_expression switch_block
586 catches catch_clause catch_clause_parameter finally
587 anonymous_class_creation trap_overflow_corner_case
588 %type <node> return_statement break_statement continue_statement
590 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
591 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
592 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
593 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
594 %type <operator> ASSIGN_ANY_TK assignment_operator
595 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
596 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
597 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
598 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
599 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
600 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
601 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
602 %type <operator> NEW_TK ASSERT_TK
604 %type <node> method_body
606 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
607 STRING_LIT_TK NULL_TK VOID_TK
609 %type <node> IF_TK WHILE_TK FOR_TK
611 %type <node> formal_parameter_list formal_parameter
612 method_declarator method_header
614 %type <node> primitive_type reference_type type
615 BOOLEAN_TK INTEGRAL_TK FP_TK
617 /* Added or modified JDK 1.1 rule types */
618 %type <node> type_literals
621 /* 19.2 Production from 2.3: The Syntactic Grammar */
622 goal: compilation_unit
626 /* 19.3 Productions from 3: Lexical structure */
636 /* 19.4 Productions from 4: Types, Values and Variables */
649 class_or_interface_type
653 class_or_interface_type:
658 class_or_interface_type /* Default rule */
662 class_or_interface_type
668 int osb = pop_current_osb (ctxp);
669 tree t = build_java_array_type (($1), -1);
671 t = build_unresolved_array_type (t);
676 int osb = pop_current_osb (ctxp);
679 t = build_unresolved_array_type (t);
684 /* 19.5 Productions from 6: Names */
686 simple_name /* Default rule */
687 | qualified_name /* Default rule */
691 identifier /* Default rule */
695 name DOT_TK identifier
696 { $$ = make_qualified_name ($1, $3, $2.location); }
703 /* 19.6: Production from 7: Packages */
706 | package_declaration
707 | import_declarations
709 | package_declaration import_declarations
710 | package_declaration type_declarations
711 | import_declarations type_declarations
712 | package_declaration import_declarations type_declarations
720 | import_declarations import_declaration
728 | type_declarations type_declaration
732 PACKAGE_TK name SC_TK
734 ctxp->package = EXPR_WFL_NODE ($2);
737 {yyerror ("Missing name"); RECOVER;}
738 | PACKAGE_TK name error
739 {yyerror ("';' expected"); RECOVER;}
743 single_type_import_declaration
744 | type_import_on_demand_declaration
747 single_type_import_declaration:
750 tree name = EXPR_WFL_NODE ($2), last_name;
751 int i = IDENTIFIER_LENGTH (name)-1;
752 const char *last = &IDENTIFIER_POINTER (name)[i];
753 while (last != IDENTIFIER_POINTER (name))
759 last_name = get_identifier (++last);
760 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
762 tree err = find_name_in_single_imports (last_name);
763 if (err && err != name)
765 ($2, "Ambiguous class: %qs and %qs",
766 IDENTIFIER_POINTER (name),
767 IDENTIFIER_POINTER (err));
769 REGISTER_IMPORT ($2, last_name);
772 REGISTER_IMPORT ($2, last_name);
775 {yyerror ("Missing name"); RECOVER;}
776 | IMPORT_TK name error
777 {yyerror ("';' expected"); RECOVER;}
780 type_import_on_demand_declaration:
781 IMPORT_TK name DOT_TK MULT_TK SC_TK
783 tree name = EXPR_WFL_NODE ($2);
785 /* Search for duplicates. */
786 for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
787 if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
789 /* Don't import the same thing more than once, just ignore
790 duplicates (7.5.2) */
793 read_import_dir ($2);
794 ctxp->import_demand_list =
795 chainon (ctxp->import_demand_list,
796 build_tree_list ($2, NULL_TREE));
799 | IMPORT_TK name DOT_TK error
800 {yyerror ("'*' expected"); RECOVER;}
801 | IMPORT_TK name DOT_TK MULT_TK error
802 {yyerror ("';' expected"); RECOVER;}
807 { end_class_declaration (0); }
808 | interface_declaration
809 { end_class_declaration (0); }
814 yyerror ("Class or interface declaration expected");
818 /* 19.7 Shortened from the original:
819 modifiers: modifier | modifiers modifier
820 modifier: any of public... */
826 | modifiers MODIFIER_TK
831 (ctxp->modifier_ctx [$2], "Modifier %qs declared twice",
832 java_accstring_lookup (acc));
840 /* 19.8.1 Production from $8.1: Class Declaration */
842 modifiers CLASS_TK identifier super interfaces
843 { create_class ($1, $3, $4, $5); }
846 | CLASS_TK identifier super interfaces
847 { create_class (0, $2, $3, $4); }
850 | modifiers CLASS_TK error
851 { yyerror ("Missing class name"); RECOVER; }
853 { yyerror ("Missing class name"); RECOVER; }
854 | CLASS_TK identifier error
856 if (!ctxp->class_err) yyerror ("'{' expected");
859 | modifiers CLASS_TK identifier error
860 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
865 | EXTENDS_TK class_type
867 | EXTENDS_TK class_type error
868 {yyerror ("'{' expected"); ctxp->class_err=1;}
870 {yyerror ("Missing super class name"); ctxp->class_err=1;}
875 | IMPLEMENTS_TK interface_type_list
877 | IMPLEMENTS_TK error
880 yyerror ("Missing interface name");
887 ctxp->interface_number = 1;
888 $$ = build_tree_list ($1, NULL_TREE);
890 | interface_type_list C_TK interface_type
892 ctxp->interface_number++;
893 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
895 | interface_type_list C_TK error
896 {yyerror ("Missing interface name"); RECOVER;}
904 | OCB_TK class_body_declarations CCB_TK
910 class_body_declarations:
911 class_body_declaration
912 | class_body_declarations class_body_declaration
915 class_body_declaration:
916 class_member_declaration
918 | constructor_declaration
919 | block /* Added, JDK1.1, instance initializer */
921 if (!IS_EMPTY_STMT ($1))
923 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
924 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
929 class_member_declaration:
932 | class_declaration /* Added, JDK1.1 inner classes */
933 { end_class_declaration (1); }
934 | interface_declaration /* Added, JDK1.1 inner interfaces */
935 { end_class_declaration (1); }
939 /* 19.8.2 Productions from 8.3: Field Declarations */
941 type variable_declarators SC_TK
942 { register_fields (0, $1, $2); }
943 | modifiers type variable_declarators SC_TK
946 ("Illegal modifier %qs for field declaration",
947 $1, FIELD_MODIFIERS);
948 check_modifiers_consistency ($1);
949 register_fields ($1, $2, $3);
953 variable_declarators:
954 /* Should we use build_decl_list () instead ? FIXME */
955 variable_declarator /* Default rule */
956 | variable_declarators C_TK variable_declarator
957 { $$ = chainon ($1, $3); }
958 | variable_declarators C_TK error
959 {yyerror ("Missing term"); RECOVER;}
963 variable_declarator_id
964 { $$ = build_tree_list ($1, NULL_TREE); }
965 | variable_declarator_id ASSIGN_TK variable_initializer
967 if (java_error_count)
970 ($1, build_assignment ($2.token, $2.location, $1, $3));
972 | variable_declarator_id ASSIGN_TK error
974 yyerror ("Missing variable initializer");
975 $$ = build_tree_list ($1, NULL_TREE);
978 | variable_declarator_id ASSIGN_TK variable_initializer error
980 yyerror ("';' expected");
981 $$ = build_tree_list ($1, NULL_TREE);
986 variable_declarator_id:
988 | variable_declarator_id OSB_TK CSB_TK
989 { $$ = build_unresolved_array_type ($1); }
991 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
992 | variable_declarator_id OSB_TK error
994 yyerror ("']' expected");
997 | variable_declarator_id CSB_TK error
998 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1001 variable_initializer:
1006 /* 19.8.3 Productions from 8.4: Method Declarations */
1010 current_function_decl = $1;
1011 if (current_function_decl
1012 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1013 source_start_java_method (current_function_decl);
1015 current_function_decl = NULL_TREE;
1018 { finish_method_declaration ($3); }
1019 | method_header error
1020 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1024 type method_declarator throws
1025 { $$ = method_header (0, $1, $2, $3); }
1026 | VOID_TK method_declarator throws
1027 { $$ = method_header (0, void_type_node, $2, $3); }
1028 | modifiers type method_declarator throws
1029 { $$ = method_header ($1, $2, $3, $4); }
1030 | modifiers VOID_TK method_declarator throws
1031 { $$ = method_header ($1, void_type_node, $3, $4); }
1034 yyerror ("Invalid method declaration, method name required");
1037 | modifiers type error
1039 yyerror ("Identifier expected");
1044 yyerror ("Identifier expected");
1047 | modifiers VOID_TK error
1049 yyerror ("Identifier expected");
1054 yyerror ("Invalid method declaration, return type required");
1060 identifier OP_TK CP_TK
1062 ctxp->formal_parameter_number = 0;
1063 $$ = method_declarator ($1, NULL_TREE);
1065 | identifier OP_TK formal_parameter_list CP_TK
1066 { $$ = method_declarator ($1, $3); }
1067 | method_declarator OSB_TK CSB_TK
1069 SET_EXPR_LOCATION_FROM_TOKEN (wfl_operator, $2);
1071 build_unresolved_array_type (TREE_PURPOSE ($1));
1072 parse_warning_context
1074 "Discouraged form of returned type specification");
1076 | identifier OP_TK error
1077 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1078 | method_declarator OSB_TK error
1079 {yyerror ("']' expected"); RECOVER;}
1082 formal_parameter_list:
1085 ctxp->formal_parameter_number = 1;
1087 | formal_parameter_list C_TK formal_parameter
1089 ctxp->formal_parameter_number += 1;
1090 $$ = chainon ($1, $3);
1092 | formal_parameter_list C_TK error
1093 { yyerror ("Missing formal parameter term"); RECOVER; }
1097 type variable_declarator_id
1099 $$ = build_tree_list ($2, $1);
1101 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1103 $$ = build_tree_list ($3, $2);
1104 ARG_FINAL_P ($$) = 1;
1108 yyerror ("Missing identifier"); RECOVER;
1113 yyerror ("Missing identifier"); RECOVER;
1121 check_modifiers ("Illegal modifier %qs. Only %<final%> was expected here",
1123 if ($1 != ACC_FINAL)
1124 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1130 | THROWS_TK class_type_list
1133 {yyerror ("Missing class type term"); RECOVER;}
1138 { $$ = build_tree_list ($1, $1); }
1139 | class_type_list C_TK class_type
1140 { $$ = tree_cons ($3, $3, $1); }
1141 | class_type_list C_TK error
1142 {yyerror ("Missing class type term"); RECOVER;}
1147 | SC_TK { $$ = NULL_TREE; }
1150 /* 19.8.4 Productions from 8.5: Static Initializers */
1154 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1155 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1156 current_static_block = NULL_TREE;
1160 static: /* Test lval.sub_token here */
1163 check_modifiers ("Illegal modifier %qs for static initializer", $1, ACC_STATIC);
1164 /* Can't have a static initializer in an innerclass */
1165 if ($1 | ACC_STATIC &&
1166 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1168 (MODIFIER_WFL (STATIC_TK),
1169 "Can't define static initializer in class %qs. Static initializer can only be defined in top-level classes",
1170 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1171 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1175 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1176 constructor_declaration:
1179 current_function_decl = $1;
1180 source_start_java_method (current_function_decl);
1183 { finish_method_declaration ($3); }
1187 constructor_declarator throws
1188 { $$ = method_header (0, NULL_TREE, $1, $2); }
1189 | modifiers constructor_declarator throws
1190 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1193 constructor_declarator:
1194 simple_name OP_TK CP_TK
1196 ctxp->formal_parameter_number = 0;
1197 $$ = method_declarator ($1, NULL_TREE);
1199 | simple_name OP_TK formal_parameter_list CP_TK
1200 { $$ = method_declarator ($1, $3); }
1204 /* Unlike regular method, we always need a complete (empty)
1205 body so we can safely perform all the required code
1206 addition (super invocation and field initialization) */
1207 block_begin constructor_block_end
1209 BLOCK_EXPR_BODY ($2) = build_java_empty_stmt ();
1212 | block_begin explicit_constructor_invocation constructor_block_end
1214 | block_begin block_statements constructor_block_end
1216 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1220 constructor_block_end:
1224 /* Error recovery for that rule moved down expression_statement: rule. */
1225 explicit_constructor_invocation:
1226 this_or_super OP_TK CP_TK SC_TK
1228 $$ = build_method_invocation ($1, NULL_TREE);
1229 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1230 $$ = java_method_add_stmt (current_function_decl, $$);
1232 | this_or_super OP_TK argument_list CP_TK SC_TK
1234 $$ = build_method_invocation ($1, $3);
1235 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1236 $$ = java_method_add_stmt (current_function_decl, $$);
1238 /* Added, JDK1.1 inner classes. Modified because the rule
1239 'primary' couldn't work. */
1240 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1241 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1242 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1243 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1246 this_or_super: /* Added, simplifies error diagnostics */
1249 tree wfl = build_wfl_node (this_identifier_node);
1250 SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1255 tree wfl = build_wfl_node (super_identifier_node);
1256 SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1261 /* 19.9 Productions from 9: Interfaces */
1262 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1263 interface_declaration:
1264 INTERFACE_TK identifier
1265 { create_interface (0, $2, NULL_TREE); }
1268 | modifiers INTERFACE_TK identifier
1269 { create_interface ($1, $3, NULL_TREE); }
1272 | INTERFACE_TK identifier extends_interfaces
1273 { create_interface (0, $2, $3); }
1276 | modifiers INTERFACE_TK identifier extends_interfaces
1277 { create_interface ($1, $3, $4); }
1280 | INTERFACE_TK identifier error
1281 { yyerror ("'{' expected"); RECOVER; }
1282 | modifiers INTERFACE_TK identifier error
1283 { yyerror ("'{' expected"); RECOVER; }
1287 EXTENDS_TK interface_type
1289 ctxp->interface_number = 1;
1290 $$ = build_tree_list ($2, NULL_TREE);
1292 | extends_interfaces C_TK interface_type
1294 ctxp->interface_number++;
1295 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1298 {yyerror ("Invalid interface type"); RECOVER;}
1299 | extends_interfaces C_TK error
1300 {yyerror ("Missing term"); RECOVER;}
1306 | OCB_TK interface_member_declarations CCB_TK
1310 interface_member_declarations:
1311 interface_member_declaration
1312 | interface_member_declarations interface_member_declaration
1315 interface_member_declaration:
1316 constant_declaration
1317 | abstract_method_declaration
1318 | class_declaration /* Added, JDK1.1 inner classes */
1319 { end_class_declaration (1); }
1320 | interface_declaration /* Added, JDK1.1 inner interfaces */
1321 { end_class_declaration (1); }
1324 constant_declaration:
1328 abstract_method_declaration:
1331 check_abstract_method_header ($1);
1332 current_function_decl = NULL_TREE; /* FIXME ? */
1334 | method_header error
1335 {yyerror ("';' expected"); RECOVER;}
1338 /* 19.10 Productions from 10: Arrays */
1341 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1342 | OCB_TK C_TK CCB_TK
1343 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1344 | OCB_TK variable_initializers CCB_TK
1345 { $$ = build_new_array_init ($1.location, $2); }
1346 | OCB_TK variable_initializers C_TK CCB_TK
1347 { $$ = build_new_array_init ($1.location, $2); }
1350 variable_initializers:
1351 variable_initializer
1353 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1356 | variable_initializers C_TK variable_initializer
1358 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1360 | variable_initializers C_TK error
1361 {yyerror ("Missing term"); RECOVER;}
1364 /* 19.11 Production from 14: Blocks and Statements */
1366 block_begin block_end
1368 | block_begin block_statements block_end
1380 maybe_absorb_scoping_blocks ();
1382 if (!BLOCK_SUBBLOCKS ($$))
1383 BLOCK_SUBBLOCKS ($$) = build_java_empty_stmt ();
1389 | block_statements block_statement
1393 local_variable_declaration_statement
1395 { java_method_add_stmt (current_function_decl, $1); }
1396 | class_declaration /* Added, JDK1.1 local classes */
1398 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1399 end_class_declaration (1);
1403 local_variable_declaration_statement:
1404 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1407 local_variable_declaration:
1408 type variable_declarators
1409 { declare_local_variables (0, $1, $2); }
1410 | final type variable_declarators /* Added, JDK1.1 final locals */
1411 { declare_local_variables ($1, $2, $3); }
1415 statement_without_trailing_substatement
1418 | if_then_else_statement
1421 { $$ = exit_block (); }
1425 statement_without_trailing_substatement
1426 | labeled_statement_nsi
1427 | if_then_else_statement_nsi
1428 | while_statement_nsi
1430 { $$ = exit_block (); }
1433 statement_without_trailing_substatement:
1436 | expression_statement
1440 | continue_statement
1442 | synchronized_statement
1451 if (flag_extraneous_semicolon
1452 && ! current_static_block
1453 && (! current_function_decl ||
1454 /* Verify we're not in a inner class declaration */
1455 (GET_CPC () != TYPE_NAME
1456 (DECL_CONTEXT (current_function_decl)))))
1459 #ifdef USE_MAPPED_LOCATION
1460 SET_EXPR_LOCATION (wfl_operator, input_location);
1462 EXPR_WFL_SET_LINECOL (wfl_operator, input_line, -1);
1464 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1466 $$ = build_java_empty_stmt ();
1471 identifier REL_CL_TK
1473 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1474 EXPR_WFL_NODE ($1));
1476 push_labeled_block ($$);
1477 PUSH_LABELED_BLOCK ($$);
1482 label_decl statement
1483 { $$ = finish_labeled_statement ($1, $2); }
1485 {yyerror ("':' expected"); RECOVER;}
1488 labeled_statement_nsi:
1489 label_decl statement_nsi
1490 { $$ = finish_labeled_statement ($1, $2); }
1493 /* We concentrate here a bunch of error handling rules that we couldn't write
1494 earlier, because expression_statement catches a missing ';'. */
1495 expression_statement:
1496 statement_expression SC_TK
1498 /* We have a statement. Generate a WFL around it so
1500 #ifdef USE_MAPPED_LOCATION
1501 $$ = expr_add_location ($1, input_location, 1);
1503 $$ = build_expr_wfl ($1, input_filename, input_line, 0);
1504 JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1506 /* We know we have a statement, so set the debug
1507 info to be eventually generate here. */
1511 YYNOT_TWICE yyerror ("Invalid expression statement");
1512 DRECOVER (expr_stmt);
1516 YYNOT_TWICE yyerror ("Invalid expression statement");
1517 DRECOVER (expr_stmt);
1521 YYNOT_TWICE yyerror ("Invalid expression statement");
1522 DRECOVER (expr_stmt);
1524 | this_or_super OP_TK error
1525 {yyerror ("')' expected"); RECOVER;}
1526 | this_or_super OP_TK CP_TK error
1528 parse_ctor_invocation_error ();
1531 | this_or_super OP_TK argument_list error
1532 {yyerror ("')' expected"); RECOVER;}
1533 | this_or_super OP_TK argument_list CP_TK error
1535 parse_ctor_invocation_error ();
1538 | name DOT_TK SUPER_TK error
1539 {yyerror ("'(' expected"); RECOVER;}
1540 | name DOT_TK SUPER_TK OP_TK error
1541 {yyerror ("')' expected"); RECOVER;}
1542 | name DOT_TK SUPER_TK OP_TK argument_list error
1543 {yyerror ("')' expected"); RECOVER;}
1544 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1545 {yyerror ("';' expected"); RECOVER;}
1546 | name DOT_TK SUPER_TK OP_TK CP_TK error
1547 {yyerror ("';' expected"); RECOVER;}
1550 statement_expression:
1552 | pre_increment_expression
1553 | pre_decrement_expression
1554 | post_increment_expression
1555 | post_decrement_expression
1557 | class_instance_creation_expression
1561 IF_TK OP_TK expression CP_TK statement
1563 $$ = build_if_else_statement ($2.location, $3,
1567 {yyerror ("'(' expected"); RECOVER;}
1569 {yyerror ("Missing term"); RECOVER;}
1570 | IF_TK OP_TK expression error
1571 {yyerror ("')' expected"); RECOVER;}
1574 if_then_else_statement:
1575 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1576 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1579 if_then_else_statement_nsi:
1580 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1581 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1591 /* Make into "proper list" of COMPOUND_EXPRs.
1592 I.e. make the last statement also have its own
1594 maybe_absorb_scoping_blocks ();
1595 TREE_OPERAND ($1, 1) = exit_block ();
1596 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1601 SWITCH_TK OP_TK expression CP_TK
1603 $$ = build3 (SWITCH_EXPR, NULL_TREE, $3,
1604 NULL_TREE, NULL_TREE);
1605 SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
1608 {yyerror ("'(' expected"); RECOVER;}
1609 | SWITCH_TK OP_TK error
1610 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1611 | SWITCH_TK OP_TK expression CP_TK error
1612 {yyerror ("'{' expected"); RECOVER;}
1615 /* Default assignment is there to avoid type node on switch_block
1621 | OCB_TK switch_labels CCB_TK
1623 | OCB_TK switch_block_statement_groups CCB_TK
1625 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1629 switch_block_statement_groups:
1630 switch_block_statement_group
1631 | switch_block_statement_groups switch_block_statement_group
1634 switch_block_statement_group:
1635 switch_labels block_statements
1640 | switch_labels switch_label
1644 CASE_TK constant_expression REL_CL_TK
1646 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1647 SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1648 java_method_add_stmt (current_function_decl, lab);
1650 | DEFAULT_TK REL_CL_TK
1652 tree lab = make_node (DEFAULT_EXPR);
1653 SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1654 java_method_add_stmt (current_function_decl, lab);
1657 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1658 | CASE_TK constant_expression error
1659 {yyerror ("':' expected"); RECOVER;}
1661 {yyerror ("':' expected"); RECOVER;}
1665 WHILE_TK OP_TK expression CP_TK
1667 tree body = build_loop_body ($2.location, $3, 0);
1668 $$ = build_new_loop (body);
1673 while_expression statement
1674 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1676 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1677 | WHILE_TK OP_TK error
1678 {yyerror ("Missing term and ')' expected"); RECOVER;}
1679 | WHILE_TK OP_TK expression error
1680 {yyerror ("')' expected"); RECOVER;}
1683 while_statement_nsi:
1684 while_expression statement_nsi
1685 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1691 tree body = build_loop_body (0, NULL_TREE, 1);
1692 $$ = build_new_loop (body);
1694 /* Need error handing here. FIXME */
1698 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1699 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1703 for_begin SC_TK expression SC_TK for_update CP_TK statement
1705 if (CONSTANT_CLASS_P ($3))
1706 $3 = build_wfl_node ($3);
1707 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1709 | for_begin SC_TK SC_TK for_update CP_TK statement
1711 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1712 /* We have not condition, so we get rid of the EXIT_EXPR */
1713 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1714 build_java_empty_stmt ();
1716 | for_begin SC_TK error
1717 {yyerror ("Invalid control expression"); RECOVER;}
1718 | for_begin SC_TK expression SC_TK error
1719 {yyerror ("Invalid update expression"); RECOVER;}
1720 | for_begin SC_TK SC_TK error
1721 {yyerror ("Invalid update expression"); RECOVER;}
1725 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1726 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1727 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1729 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1730 /* We have not condition, so we get rid of the EXIT_EXPR */
1731 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1732 build_java_empty_stmt ();
1739 /* This scope defined for local variable that may be
1740 defined within the scope of the for loop */
1744 {yyerror ("'(' expected"); DRECOVER(for_1);}
1745 | FOR_TK OP_TK error
1746 {yyerror ("Invalid init statement"); RECOVER;}
1752 /* We now declare the loop body. The loop is
1753 declared as a for loop. */
1754 tree body = build_loop_body (0, NULL_TREE, 0);
1755 $$ = build_new_loop (body);
1756 FOR_LOOP_P ($$) = 1;
1757 /* The loop is added to the current block the for
1758 statement is defined within */
1759 java_method_add_stmt (current_function_decl, $$);
1762 for_init: /* Can be empty */
1763 { $$ = build_java_empty_stmt (); }
1764 | statement_expression_list
1766 /* Init statement recorded within the previously
1767 defined block scope */
1768 $$ = java_method_add_stmt (current_function_decl, $1);
1770 | local_variable_declaration
1772 /* Local variable are recorded within the previously
1773 defined block scope */
1776 | statement_expression_list error
1777 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1780 for_update: /* Can be empty */
1781 {$$ = build_java_empty_stmt ();}
1782 | statement_expression_list
1783 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1786 statement_expression_list:
1787 statement_expression
1788 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1789 | statement_expression_list C_TK statement_expression
1790 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1791 | statement_expression_list C_TK error
1792 {yyerror ("Missing term"); RECOVER;}
1797 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1798 | BREAK_TK identifier SC_TK
1799 { $$ = build_bc_statement ($1.location, 1, $2); }
1801 {yyerror ("Missing term"); RECOVER;}
1802 | BREAK_TK identifier error
1803 {yyerror ("';' expected"); RECOVER;}
1808 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1809 | CONTINUE_TK identifier SC_TK
1810 { $$ = build_bc_statement ($1.location, 0, $2); }
1812 {yyerror ("Missing term"); RECOVER;}
1813 | CONTINUE_TK identifier error
1814 {yyerror ("';' expected"); RECOVER;}
1819 { $$ = build_return ($1.location, NULL_TREE); }
1820 | RETURN_TK expression SC_TK
1821 { $$ = build_return ($1.location, $2); }
1823 {yyerror ("Missing term"); RECOVER;}
1824 | RETURN_TK expression error
1825 {yyerror ("';' expected"); RECOVER;}
1829 THROW_TK expression SC_TK
1831 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1832 SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1835 {yyerror ("Missing term"); RECOVER;}
1836 | THROW_TK expression error
1837 {yyerror ("';' expected"); RECOVER;}
1841 ASSERT_TK expression REL_CL_TK expression SC_TK
1843 $$ = build_assertion ($1.location, $2, $4);
1845 | ASSERT_TK expression SC_TK
1847 $$ = build_assertion ($1.location, $2, NULL_TREE);
1850 {yyerror ("Missing term"); RECOVER;}
1851 | ASSERT_TK expression error
1852 {yyerror ("';' expected"); RECOVER;}
1855 synchronized_statement:
1856 synchronized OP_TK expression CP_TK block
1858 $$ = build2 (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1859 EXPR_WFL_LINECOL ($$) =
1860 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1862 | synchronized OP_TK expression CP_TK error
1863 {yyerror ("'{' expected"); RECOVER;}
1864 | synchronized error
1865 {yyerror ("'(' expected"); RECOVER;}
1866 | synchronized OP_TK error CP_TK
1867 {yyerror ("Missing term"); RECOVER;}
1868 | synchronized OP_TK error
1869 {yyerror ("Missing term"); RECOVER;}
1876 "Illegal modifier %qs. Only %<synchronized%> was expected here",
1877 $1, ACC_SYNCHRONIZED);
1878 if ($1 != ACC_SYNCHRONIZED)
1879 MODIFIER_WFL (SYNCHRONIZED_TK) =
1880 build_wfl_node (NULL_TREE);
1885 TRY_TK block catches
1886 { $$ = build_try_statement ($1.location, $2, $3); }
1887 | TRY_TK block finally
1888 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1889 | TRY_TK block catches finally
1890 { $$ = build_try_finally_statement
1891 ($1.location, build_try_statement ($1.location,
1895 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1900 | catches catch_clause
1902 TREE_CHAIN ($2) = $1;
1908 catch_clause_parameter block
1910 java_method_add_stmt (current_function_decl, $2);
1916 catch_clause_parameter:
1917 CATCH_TK OP_TK formal_parameter CP_TK
1919 /* We add a block to define a scope for
1920 formal_parameter (CCBP). The formal parameter is
1921 declared initialized by the appropriate function
1927 ccpb = enter_block ();
1928 init = build_assignment
1929 (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1930 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1931 declare_local_variables (0, TREE_VALUE ($3),
1933 (TREE_PURPOSE ($3), init));
1934 $$ = build1 (JAVA_CATCH_EXPR, NULL_TREE, ccpb);
1935 SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1939 $$ = error_mark_node;
1943 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1944 | CATCH_TK OP_TK error
1946 yyerror ("Missing term or ')' expected");
1947 RECOVER; $$ = NULL_TREE;
1949 | CATCH_TK OP_TK error CP_TK /* That's for () */
1950 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1957 {yyerror ("'{' expected"); RECOVER; }
1960 /* 19.12 Production from 15: Expressions */
1962 primary_no_new_array
1963 | array_creation_uninitialized
1964 | array_creation_initialized
1967 primary_no_new_array:
1970 { $$ = build_this ($1.location); }
1971 | OP_TK expression CP_TK
1973 | class_instance_creation_expression
1978 /* Added, JDK1.1 inner classes. Documentation is wrong
1979 referring to a 'ClassName' (class_name) rule that doesn't
1980 exist. Used name: instead. */
1981 | name DOT_TK THIS_TK
1983 tree wfl = build_wfl_node (this_identifier_node);
1984 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1986 | OP_TK expression error
1987 {yyerror ("')' expected"); RECOVER;}
1989 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1990 | primitive_type DOT_TK error
1991 {yyerror ("'class' expected" ); RECOVER;}
1992 | VOID_TK DOT_TK error
1993 {yyerror ("'class' expected" ); RECOVER;}
1997 name DOT_TK CLASS_TK
1998 { $$ = build_incomplete_class_ref ($2.location, $1); }
1999 | array_type DOT_TK CLASS_TK
2000 { $$ = build_incomplete_class_ref ($2.location, $1); }
2001 | primitive_type DOT_TK CLASS_TK
2002 { $$ = build_incomplete_class_ref ($2.location, $1); }
2003 | VOID_TK DOT_TK CLASS_TK
2005 $$ = build_incomplete_class_ref ($2.location,
2010 class_instance_creation_expression:
2011 NEW_TK class_type OP_TK argument_list CP_TK
2012 { $$ = build_new_invocation ($2, $4); }
2013 | NEW_TK class_type OP_TK CP_TK
2014 { $$ = build_new_invocation ($2, NULL_TREE); }
2015 | anonymous_class_creation
2016 /* Added, JDK1.1 inner classes, modified to use name or
2017 primary instead of primary solely which couldn't work in
2019 | something_dot_new identifier OP_TK CP_TK
2021 tree ctor = build_new_invocation ($2, NULL_TREE);
2022 $$ = make_qualified_primary ($1, ctor,
2023 EXPR_WFL_LINECOL ($1));
2025 | something_dot_new identifier OP_TK CP_TK class_body
2026 | something_dot_new identifier OP_TK argument_list CP_TK
2028 tree ctor = build_new_invocation ($2, $4);
2029 $$ = make_qualified_primary ($1, ctor,
2030 EXPR_WFL_LINECOL ($1));
2032 | something_dot_new identifier OP_TK argument_list CP_TK class_body
2033 | NEW_TK error SC_TK
2034 {$$ = NULL_TREE; yyerror ("'(' expected"); DRECOVER(new_1);}
2035 | NEW_TK class_type error
2036 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2037 | NEW_TK class_type OP_TK error
2038 {$$ = NULL_TREE; yyerror ("')' or term expected"); RECOVER;}
2039 | NEW_TK class_type OP_TK argument_list error
2040 {$$ = NULL_TREE; yyerror ("')' expected"); RECOVER;}
2041 | something_dot_new error
2045 yyerror ("Identifier expected");
2048 | something_dot_new identifier error
2049 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2052 /* Created after JDK1.1 rules originally added to
2053 class_instance_creation_expression, but modified to use
2054 'class_type' instead of 'TypeName' (type_name) which is mentioned
2055 in the documentation but doesn't exist. */
2057 anonymous_class_creation:
2058 NEW_TK class_type OP_TK argument_list CP_TK
2059 { create_anonymous_class ($2); }
2062 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2063 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2065 end_class_declaration (1);
2067 /* Now we can craft the new expression */
2068 $$ = build_new_invocation (id, $4);
2070 /* Note that we can't possibly be here if
2071 `class_type' is an interface (in which case the
2072 anonymous class extends Object and implements
2073 `class_type', hence its constructor can't have
2076 /* Otherwise, the innerclass must feature a
2077 constructor matching `argument_list'. Anonymous
2078 classes are a bit special: it's impossible to
2079 define constructor for them, hence constructors
2080 must be generated following the hints provided by
2081 the `new' expression. Whether a super constructor
2082 of that nature exists or not is to be verified
2083 later on in verify_constructor_super.
2085 It's during the expansion of a `new' statement
2086 referring to an anonymous class that a ctor will
2087 be generated for the anonymous class, with the
2091 | NEW_TK class_type OP_TK CP_TK
2092 { create_anonymous_class ($2); }
2095 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2096 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2098 end_class_declaration (1);
2100 /* Now we can craft the new expression. The
2101 statement doesn't need to be remember so that a
2102 constructor can be generated, since its signature
2103 is already known. */
2104 $$ = build_new_invocation (id, NULL_TREE);
2108 something_dot_new: /* Added, not part of the specs. */
2111 | primary DOT_TK NEW_TK
2118 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2119 ctxp->formal_parameter_number = 1;
2121 | argument_list C_TK expression
2123 ctxp->formal_parameter_number += 1;
2124 $$ = tree_cons (NULL_TREE, $3, $1);
2126 | argument_list C_TK error
2127 {yyerror ("Missing term"); RECOVER;}
2130 array_creation_uninitialized:
2131 NEW_TK primitive_type dim_exprs
2132 { $$ = build_newarray_node ($2, $3, 0); }
2133 | NEW_TK class_or_interface_type dim_exprs
2134 { $$ = build_newarray_node ($2, $3, 0); }
2135 | NEW_TK primitive_type dim_exprs dims
2136 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2137 | NEW_TK class_or_interface_type dim_exprs dims
2138 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2139 | NEW_TK error CSB_TK
2140 {yyerror ("'[' expected"); DRECOVER ("]");}
2141 | NEW_TK error OSB_TK
2142 {yyerror ("']' expected"); RECOVER;}
2145 array_creation_initialized:
2146 /* Added, JDK1.1 anonymous array. Initial documentation rule
2148 NEW_TK class_or_interface_type dims array_initializer
2151 int osb = pop_current_osb (ctxp);
2153 obstack_grow (&temporary_obstack, "[]", 2);
2154 obstack_1grow (&temporary_obstack, '\0');
2155 sig = obstack_finish (&temporary_obstack);
2156 $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2157 $2, get_identifier (sig), $4);
2159 | NEW_TK primitive_type dims array_initializer
2161 int osb = pop_current_osb (ctxp);
2164 type = build_java_array_type (type, -1);
2165 $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2166 build_pointer_type (type), NULL_TREE, $4);
2168 | NEW_TK error CSB_TK
2169 {yyerror ("'[' expected"); DRECOVER ("]");}
2170 | NEW_TK error OSB_TK
2171 {yyerror ("']' expected"); RECOVER;}
2176 { $$ = build_tree_list (NULL_TREE, $1); }
2177 | dim_exprs dim_expr
2178 { $$ = tree_cons (NULL_TREE, $2, $$); }
2182 OSB_TK expression CSB_TK
2184 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2186 $2 = build_wfl_node ($2);
2187 TREE_TYPE ($2) = NULL_TREE;
2189 EXPR_WFL_LINECOL ($2) = $1.location;
2192 | OSB_TK expression error
2193 {yyerror ("']' expected"); RECOVER;}
2196 yyerror ("Missing term");
2197 yyerror ("']' expected");
2206 /* If not initialized, allocate memory for the osb
2208 if (!ctxp->osb_limit)
2210 allocate = ctxp->osb_limit = 32;
2211 ctxp->osb_depth = -1;
2213 /* If capacity overflown, reallocate a bigger chunk */
2214 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2215 allocate = ctxp->osb_limit << 1;
2219 allocate *= sizeof (int);
2220 if (ctxp->osb_number)
2221 ctxp->osb_number = xrealloc (ctxp->osb_number,
2224 ctxp->osb_number = xmalloc (allocate);
2227 CURRENT_OSB (ctxp) = 1;
2229 | dims OSB_TK CSB_TK
2230 { CURRENT_OSB (ctxp)++; }
2232 { yyerror ("']' expected"); RECOVER;}
2236 primary DOT_TK identifier
2237 { $$ = make_qualified_primary ($1, $3, $2.location); }
2238 /* FIXME - REWRITE TO:
2239 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2240 | SUPER_TK DOT_TK identifier
2242 tree super_wfl = build_wfl_node (super_identifier_node);
2243 SET_EXPR_LOCATION_FROM_TOKEN (super_wfl, $1);
2244 $$ = make_qualified_name (super_wfl, $3, $2.location);
2247 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2252 { $$ = build_method_invocation ($1, NULL_TREE); }
2253 | name OP_TK argument_list CP_TK
2254 { $$ = build_method_invocation ($1, $3); }
2255 | primary DOT_TK identifier OP_TK CP_TK
2257 if (TREE_CODE ($1) == THIS_EXPR)
2258 $$ = build_this_super_qualified_invocation
2259 (1, $3, NULL_TREE, 0, $2.location);
2262 tree invok = build_method_invocation ($3, NULL_TREE);
2263 $$ = make_qualified_primary ($1, invok, $2.location);
2266 | primary DOT_TK identifier OP_TK argument_list CP_TK
2268 if (TREE_CODE ($1) == THIS_EXPR)
2269 $$ = build_this_super_qualified_invocation
2270 (1, $3, $5, 0, $2.location);
2273 tree invok = build_method_invocation ($3, $5);
2274 $$ = make_qualified_primary ($1, invok, $2.location);
2277 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2279 $$ = build_this_super_qualified_invocation
2280 (0, $3, NULL_TREE, $1.location, $2.location);
2282 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2284 $$ = build_this_super_qualified_invocation
2285 (0, $3, $5, $1.location, $2.location);
2287 /* Screws up thing. I let it here until I'm convinced it can
2289 | primary DOT_TK error
2290 {yyerror ("'(' expected"); DRECOVER(bad);} */
2291 | SUPER_TK DOT_TK error CP_TK
2292 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2293 | SUPER_TK DOT_TK error DOT_TK
2294 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2298 name OSB_TK expression CSB_TK
2299 { $$ = build_array_ref ($2.location, $1, $3); }
2300 | primary_no_new_array OSB_TK expression CSB_TK
2301 { $$ = build_array_ref ($2.location, $1, $3); }
2302 | array_creation_initialized OSB_TK expression CSB_TK
2303 { $$ = build_array_ref ($2.location, $1, $3); }
2306 yyerror ("Missing term and ']' expected");
2307 DRECOVER(array_access);
2309 | name OSB_TK expression error
2311 yyerror ("']' expected");
2312 DRECOVER(array_access);
2314 | primary_no_new_array OSB_TK error
2316 yyerror ("Missing term and ']' expected");
2317 DRECOVER(array_access);
2319 | primary_no_new_array OSB_TK expression error
2321 yyerror ("']' expected");
2322 DRECOVER(array_access);
2324 | array_creation_initialized OSB_TK error
2326 yyerror ("Missing term and ']' expected");
2327 DRECOVER(array_access);
2329 | array_creation_initialized OSB_TK expression error
2331 yyerror ("']' expected");
2332 DRECOVER(array_access);
2339 | post_increment_expression
2340 | post_decrement_expression
2343 post_increment_expression:
2344 postfix_expression INCR_TK
2345 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2348 post_decrement_expression:
2349 postfix_expression DECR_TK
2350 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2353 trap_overflow_corner_case:
2354 pre_increment_expression
2355 | pre_decrement_expression
2356 | PLUS_TK unary_expression
2357 {$$ = build_unaryop ($1.token, $1.location, $2); }
2358 | unary_expression_not_plus_minus
2360 {yyerror ("Missing term"); RECOVER}
2364 trap_overflow_corner_case
2367 error_if_numeric_overflow ($1);
2370 | MINUS_TK trap_overflow_corner_case
2371 {$$ = build_unaryop ($1.token, $1.location, $2); }
2373 {yyerror ("Missing term"); RECOVER}
2376 pre_increment_expression:
2377 INCR_TK unary_expression
2378 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2380 {yyerror ("Missing term"); RECOVER}
2383 pre_decrement_expression:
2384 DECR_TK unary_expression
2385 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2387 {yyerror ("Missing term"); RECOVER}
2390 unary_expression_not_plus_minus:
2392 | NOT_TK unary_expression
2393 {$$ = build_unaryop ($1.token, $1.location, $2); }
2394 | NEG_TK unary_expression
2395 {$$ = build_unaryop ($1.token, $1.location, $2); }
2398 {yyerror ("Missing term"); RECOVER}
2400 {yyerror ("Missing term"); RECOVER}
2403 cast_expression: /* Error handling here is potentially weak */
2404 OP_TK primitive_type dims CP_TK unary_expression
2407 int osb = pop_current_osb (ctxp);
2409 type = build_java_array_type (type, -1);
2410 $$ = build_cast ($1.location, type, $5);
2412 | OP_TK primitive_type CP_TK unary_expression
2413 { $$ = build_cast ($1.location, $2, $4); }
2414 | OP_TK expression CP_TK unary_expression_not_plus_minus
2415 { $$ = build_cast ($1.location, $2, $4); }
2416 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2419 int osb = pop_current_osb (ctxp);
2420 obstack_grow (&temporary_obstack,
2421 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2422 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2424 obstack_grow (&temporary_obstack, "[]", 2);
2425 obstack_1grow (&temporary_obstack, '\0');
2426 ptr = obstack_finish (&temporary_obstack);
2427 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2428 $$ = build_cast ($1.location, $2, $5);
2430 | OP_TK primitive_type OSB_TK error
2431 {yyerror ("']' expected, invalid type expression");}
2434 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2437 | OP_TK primitive_type dims CP_TK error
2438 {yyerror ("Missing term"); RECOVER;}
2439 | OP_TK primitive_type CP_TK error
2440 {yyerror ("Missing term"); RECOVER;}
2441 | OP_TK name dims CP_TK error
2442 {yyerror ("Missing term"); RECOVER;}
2445 multiplicative_expression:
2447 | multiplicative_expression MULT_TK unary_expression
2449 $$ = build_binop (BINOP_LOOKUP ($2.token),
2450 $2.location, $1, $3);
2452 | multiplicative_expression DIV_TK unary_expression
2454 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2457 | multiplicative_expression REM_TK unary_expression
2459 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2462 | multiplicative_expression MULT_TK error
2463 {yyerror ("Missing term"); RECOVER;}
2464 | multiplicative_expression DIV_TK error
2465 {yyerror ("Missing term"); RECOVER;}
2466 | multiplicative_expression REM_TK error
2467 {yyerror ("Missing term"); RECOVER;}
2470 additive_expression:
2471 multiplicative_expression
2472 | additive_expression PLUS_TK multiplicative_expression
2474 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2477 | additive_expression MINUS_TK multiplicative_expression
2479 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2482 | additive_expression PLUS_TK error
2483 {yyerror ("Missing term"); RECOVER;}
2484 | additive_expression MINUS_TK error
2485 {yyerror ("Missing term"); RECOVER;}
2490 | shift_expression LS_TK additive_expression
2492 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2495 | shift_expression SRS_TK additive_expression
2497 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2500 | shift_expression ZRS_TK additive_expression
2502 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2505 | shift_expression LS_TK error
2506 {yyerror ("Missing term"); RECOVER;}
2507 | shift_expression SRS_TK error
2508 {yyerror ("Missing term"); RECOVER;}
2509 | shift_expression ZRS_TK error
2510 {yyerror ("Missing term"); RECOVER;}
2513 relational_expression:
2515 | relational_expression LT_TK shift_expression
2517 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2520 | relational_expression GT_TK shift_expression
2522 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2525 | relational_expression LTE_TK shift_expression
2527 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2530 | relational_expression GTE_TK shift_expression
2532 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2535 | relational_expression INSTANCEOF_TK reference_type
2536 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2537 | relational_expression LT_TK error
2538 {yyerror ("Missing term"); RECOVER;}
2539 | relational_expression GT_TK error
2540 {yyerror ("Missing term"); RECOVER;}
2541 | relational_expression LTE_TK error
2542 {yyerror ("Missing term"); RECOVER;}
2543 | relational_expression GTE_TK error
2544 {yyerror ("Missing term"); RECOVER;}
2545 | relational_expression INSTANCEOF_TK error
2546 {yyerror ("Invalid reference type"); RECOVER;}
2549 equality_expression:
2550 relational_expression
2551 | equality_expression EQ_TK relational_expression
2553 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2556 | equality_expression NEQ_TK relational_expression
2558 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2561 | equality_expression EQ_TK error
2562 {yyerror ("Missing term"); RECOVER;}
2563 | equality_expression NEQ_TK error
2564 {yyerror ("Missing term"); RECOVER;}
2569 | and_expression AND_TK equality_expression
2571 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2574 | and_expression AND_TK error
2575 {yyerror ("Missing term"); RECOVER;}
2578 exclusive_or_expression:
2580 | exclusive_or_expression XOR_TK and_expression
2582 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2585 | exclusive_or_expression XOR_TK error
2586 {yyerror ("Missing term"); RECOVER;}
2589 inclusive_or_expression:
2590 exclusive_or_expression
2591 | inclusive_or_expression OR_TK exclusive_or_expression
2593 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2596 | inclusive_or_expression OR_TK error
2597 {yyerror ("Missing term"); RECOVER;}
2600 conditional_and_expression:
2601 inclusive_or_expression
2602 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2604 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2607 | conditional_and_expression BOOL_AND_TK error
2608 {yyerror ("Missing term"); RECOVER;}
2611 conditional_or_expression:
2612 conditional_and_expression
2613 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2615 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2618 | conditional_or_expression BOOL_OR_TK error
2619 {yyerror ("Missing term"); RECOVER;}
2622 conditional_expression: /* Error handling here is weak */
2623 conditional_or_expression
2624 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2626 $$ = build3 (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2627 SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
2629 | conditional_or_expression REL_QM_TK REL_CL_TK error
2632 yyerror ("Missing term");
2635 | conditional_or_expression REL_QM_TK error
2636 {yyerror ("Missing term"); DRECOVER (2);}
2637 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2638 {yyerror ("Missing term"); DRECOVER (3);}
2641 assignment_expression:
2642 conditional_expression
2647 left_hand_side assignment_operator assignment_expression
2648 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2649 | left_hand_side assignment_operator error
2651 YYNOT_TWICE yyerror ("Missing term");
2662 assignment_operator:
2668 assignment_expression
2671 constant_expression:
2677 /* Helper function to retrieve an OSB count. Should be used when the
2678 `dims:' rule is being used. */
2681 pop_current_osb (struct parser_ctxt *ctxp)
2685 if (ctxp->osb_depth < 0)
2688 to_return = CURRENT_OSB (ctxp);
2696 /* This section of the code deal with save/restoring parser contexts.
2697 Add mode documentation here. FIXME */
2699 /* Helper function. Create a new parser context. With
2700 COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2701 context is copied, otherwise, the new context is zeroed. The newly
2702 created context becomes the current one. */
2705 create_new_parser_context (int copy_from_previous)
2707 struct parser_ctxt *new;
2709 new = ggc_alloc (sizeof (struct parser_ctxt));
2710 if (copy_from_previous)
2712 memcpy (new, ctxp, sizeof (struct parser_ctxt));
2713 /* This flag, indicating the context saves global values,
2714 should only be set by java_parser_context_save_global. */
2715 new->saved_data_ctx = 0;
2718 memset (new, 0, sizeof (struct parser_ctxt));
2724 /* Create a new parser context and make it the current one. */
2727 java_push_parser_context (void)
2729 create_new_parser_context (0);
2733 java_pop_parser_context (int generate)
2736 struct parser_ctxt *next;
2744 input_location = ctxp->save_location;
2745 current_class = ctxp->class_type;
2748 /* If the old and new lexers differ, then free the old one. */
2749 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2750 java_destroy_lexer (ctxp->lexer);
2752 /* Set the single import class file flag to 0 for the current list
2753 of imported things */
2754 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2755 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2757 /* If we pushed a context to parse a class intended to be generated,
2758 we keep it so we can remember the class. What we could actually
2759 do is to just update a list of class names. */
2762 if (ctxp_for_generation_last == NULL)
2763 ctxp_for_generation = ctxp;
2765 ctxp_for_generation_last->next = ctxp;
2767 ctxp_for_generation_last = ctxp;
2770 /* And restore those of the previous context */
2771 if ((ctxp = next)) /* Assignment is really meant here */
2772 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2773 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2776 /* Create a parser context for the use of saving some global
2780 java_parser_context_save_global (void)
2784 java_push_parser_context ();
2785 ctxp->saved_data_ctx = 1;
2788 /* If this context already stores data, create a new one suitable
2789 for data storage. */
2790 else if (ctxp->saved_data)
2792 create_new_parser_context (1);
2793 ctxp->saved_data_ctx = 1;
2796 ctxp->save_location = input_location;
2797 ctxp->class_type = current_class;
2798 ctxp->function_decl = current_function_decl;
2799 ctxp->saved_data = 1;
2802 /* Restore some global variables from the previous context. Make the
2803 previous context the current one. */
2806 java_parser_context_restore_global (void)
2808 input_location = ctxp->save_location;
2809 current_class = ctxp->class_type;
2811 #ifdef USE_MAPPED_LOCATION
2812 SET_EXPR_LOCATION (wfl_operator, ctxp->save_location);
2814 EXPR_WFL_FILENAME_NODE (wfl_operator) = get_identifier (input_filename);
2816 current_function_decl = ctxp->function_decl;
2817 ctxp->saved_data = 0;
2818 if (ctxp->saved_data_ctx)
2819 java_pop_parser_context (0);
2822 /* Suspend vital data for the current class/function being parsed so
2823 that an other class can be parsed. Used to let local/anonymous
2824 classes be parsed. */
2827 java_parser_context_suspend (void)
2829 /* This makes debugging through java_debug_context easier */
2830 static const char *const name = "<inner buffer context>";
2832 /* Duplicate the previous context, use it to save the globals we're
2834 create_new_parser_context (1);
2835 ctxp->function_decl = current_function_decl;
2836 ctxp->class_type = current_class;
2838 /* Then create a new context which inherits all data from the
2839 previous one. This will be the new current context */
2840 create_new_parser_context (1);
2842 /* Help debugging */
2843 ctxp->next->filename = name;
2846 /* Resume vital data for the current class/function being parsed so
2847 that an other class can be parsed. Used to let local/anonymous
2848 classes be parsed. The trick is the data storing file position
2849 informations must be restored to their current value, so parsing
2850 can resume as if no context was ever saved. */
2853 java_parser_context_resume (void)
2855 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2856 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2857 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2859 /* We need to inherit the list of classes to complete/generate */
2860 restored->classd_list = old->classd_list;
2861 restored->class_list = old->class_list;
2863 /* Restore the current class and function from the saver */
2864 current_class = saver->class_type;
2865 current_function_decl = saver->function_decl;
2867 /* Retrieve the restored context */
2870 /* Re-installed the data for the parsing to carry on */
2871 memcpy (&ctxp->marker_begining, &old->marker_begining,
2872 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2875 /* Add a new anchor node to which all statement(s) initializing static
2876 and non static initialized upon declaration field(s) will be
2880 java_parser_context_push_initialized_field (void)
2884 node = build_tree_list (NULL_TREE, NULL_TREE);
2885 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2886 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2888 node = build_tree_list (NULL_TREE, NULL_TREE);
2889 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2890 CPC_INITIALIZER_LIST (ctxp) = node;
2892 node = build_tree_list (NULL_TREE, NULL_TREE);
2893 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2894 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2897 /* Pop the lists of initialized field. If this lists aren't empty,
2898 remember them so we can use it to create and populate the finit$
2899 or <clinit> functions. */
2902 java_parser_context_pop_initialized_field (void)
2905 tree class_type = TREE_TYPE (GET_CPC ());
2907 if (CPC_INITIALIZER_LIST (ctxp))
2909 stmts = CPC_INITIALIZER_STMT (ctxp);
2910 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2911 if (stmts && !java_error_count)
2912 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2915 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2917 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2918 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2919 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2920 /* Keep initialization in order to enforce 8.5 */
2921 if (stmts && !java_error_count)
2922 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2925 /* JDK 1.1 instance initializers */
2926 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2928 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2929 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2930 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2931 if (stmts && !java_error_count)
2932 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2937 reorder_static_initialized (tree list)
2939 /* We have to keep things in order. The alias initializer have to
2940 come first, then the initialized regular field, in reverse to
2941 keep them in lexical order. */
2942 tree marker, previous = NULL_TREE;
2943 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2944 if (TREE_CODE (marker) == TREE_LIST
2945 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2948 /* No static initialized, the list is fine as is */
2950 list = TREE_CHAIN (marker);
2952 /* No marker? reverse the whole list */
2954 list = nreverse (list);
2956 /* Otherwise, reverse what's after the marker and the new reordered
2957 sublist will replace the marker. */
2960 TREE_CHAIN (previous) = NULL_TREE;
2961 list = nreverse (list);
2962 list = chainon (TREE_CHAIN (marker), list);
2967 /* Helper functions to dump the parser context stack. */
2969 #define TAB_CONTEXT(C) \
2970 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2973 java_debug_context_do (int tab)
2975 struct parser_ctxt *copy = ctxp;
2979 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2981 fprintf (stderr, "filename: %s\n", copy->filename);
2983 fprintf (stderr, "package: %s\n",
2985 IDENTIFIER_POINTER (copy->package) : "<none>"));
2987 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2989 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2995 /* Dump the stacked up parser contexts. Intended to be called from a
2999 java_debug_context (void)
3001 java_debug_context_do (0);
3006 /* Flag for the error report routine to issue the error the first time
3007 it's called (overriding the default behavior which is to drop the
3008 first invocation and honor the second one, taking advantage of a
3010 static int force_error = 0;
3012 /* Reporting an constructor invocation error. */
3014 parse_ctor_invocation_error (void)
3016 if (DECL_CONSTRUCTOR_P (current_function_decl))
3017 yyerror ("Constructor invocation must be first thing in a constructor");
3019 yyerror ("Only constructors can invoke constructors");
3022 /* Reporting JDK1.1 features not implemented. */
3025 parse_jdk1_1_error (const char *msg)
3027 sorry (": %qs JDK1.1(TM) feature", msg);
3029 return build_java_empty_stmt ();
3032 static int do_warning = 0;
3035 yyerror (const char *msgid)
3037 #ifdef USE_MAPPED_LOCATION
3038 static source_location elc;
3039 expanded_location xloc = expand_location (input_location);
3040 int current_line = xloc.line;
3044 int current_line = input_line;
3046 static int prev_lineno;
3047 static const char *prev_msg;
3049 char *remainder, *code_from_source;
3051 if (!force_error && prev_lineno == current_line)
3053 #ifndef USE_MAPPED_LOCATION
3054 current_line = ctxp->lexer->token_start.line;
3057 /* Save current error location but report latter, when the context is
3059 if (ctxp->java_error_flag == 0)
3061 ctxp->java_error_flag = 1;
3062 #ifdef USE_MAPPED_LOCATION
3063 elc = input_location;
3065 elc = ctxp->lexer->token_start;
3067 /* Do something to use the previous line if we're reaching the
3068 end of the file... */
3069 #ifdef VERBOSE_SKELETON
3070 printf ("* Error detected (%s)\n", (msgid ? msgid : "(null)"));
3075 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3076 if (!force_error && msgid == prev_msg && prev_lineno == current_line)
3079 ctxp->java_error_flag = 0;
3081 java_warning_count++;
3086 if (elc.col == 0 && msgid && msgid[1] == ';')
3087 elc = ctxp->prev_line_end;
3092 #ifdef USE_MAPPED_LOCATION
3093 prev_lineno = current_line;
3094 code_from_source = java_get_line_col (xloc.file, current_line, xloc.column);
3096 save_lineno = input_line;
3097 prev_lineno = input_line = current_line;
3098 code_from_source = java_get_line_col (input_filename, current_line,
3099 ctxp->lexer->token_start.col);
3103 obstack_grow0 (&temporary_obstack,
3104 code_from_source, strlen (code_from_source));
3105 remainder = obstack_finish (&temporary_obstack);
3107 warning (0, "%s.\n%s", msgid, remainder);
3109 error ("%s.\n%s", msgid, remainder);
3111 /* This allow us to cheaply avoid an extra 'Invalid expression
3112 statement' error report when errors have been already reported on
3113 the same line. This occurs when we report an error but don't have
3114 a synchronization point other than ';', which
3115 expression_statement is the only one to take care of. */
3116 #ifndef USE_MAPPED_LOCATION
3117 input_line = save_lineno;
3119 ctxp->prevent_ese = input_line;
3123 issue_warning_error_from_context (
3124 #ifdef USE_MAPPED_LOCATION
3129 const char *gmsgid, va_list *ap)
3131 #ifdef USE_MAPPED_LOCATION
3132 source_location saved_location = input_location;
3133 expanded_location xloc = expand_location (cl);
3135 java_lc save_lc = ctxp->lexer->token_start;
3136 const char *saved = ctxp->filename, *saved_input_filename;
3141 text.err_no = errno;
3143 text.format_spec = gmsgid;
3144 pp_format (global_dc->printer, &text);
3145 pp_output_formatted_text (global_dc->printer);
3146 strncpy (buffer, pp_formatted_text (global_dc->printer), sizeof (buffer) - 1);
3147 buffer[sizeof (buffer) - 1] = '\0';
3148 pp_clear_output_area (global_dc->printer);
3152 #ifdef USE_MAPPED_LOCATION
3153 if (xloc.file != NULL)
3155 ctxp->filename = xloc.file;
3156 input_location = cl;
3159 ctxp->lexer->token_start.line = EXPR_WFL_LINENO (cl);
3160 ctxp->lexer->token_start.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1
3161 : EXPR_WFL_COLNO (cl) == 0xffe ? -2
3162 : EXPR_WFL_COLNO (cl));
3164 /* We have a CL, that's a good reason for using it if it contains data */
3165 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3166 ctxp->filename = EXPR_WFL_FILENAME (cl);
3167 saved_input_filename = input_filename;
3168 input_filename = ctxp->filename;
3171 java_error (buffer);
3172 #ifdef USE_MAPPED_LOCATION
3173 input_location = saved_location;
3175 ctxp->filename = saved;
3176 input_filename = saved_input_filename;
3177 ctxp->lexer->token_start = save_lc;
3182 /* Issue an error message at a current source line CL.
3183 FUTURE/FIXME: change cl to be a source_location. */
3186 parse_error_context (tree cl, const char *gmsgid, ...)
3189 va_start (ap, gmsgid);
3190 #ifdef USE_MAPPED_LOCATION
3191 issue_warning_error_from_context (EXPR_LOCATION (cl), gmsgid, &ap);
3193 issue_warning_error_from_context (cl, gmsgid, &ap);
3198 /* Issue a warning at a current source line CL.
3199 FUTURE/FIXME: change cl to be a source_location. */
3202 parse_warning_context (tree cl, const char *gmsgid, ...)
3205 va_start (ap, gmsgid);
3208 #ifdef USE_MAPPED_LOCATION
3209 issue_warning_error_from_context (EXPR_LOCATION (cl), gmsgid, &ap);
3211 issue_warning_error_from_context (cl, gmsgid, &ap);
3218 find_expr_with_wfl (tree node)
3222 enum tree_code_class code;
3225 switch (TREE_CODE (node))
3228 node = BLOCK_EXPR_BODY (node);
3232 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3235 node = TREE_OPERAND (node, 1);
3239 node = TREE_OPERAND (node, 0);
3242 case LABELED_BLOCK_EXPR:
3243 node = LABELED_BLOCK_BODY (node);
3247 code = TREE_CODE_CLASS (TREE_CODE (node));
3248 if (((code == tcc_unary) || (code == tcc_binary)
3249 || (code == tcc_expression))
3250 && EXPR_WFL_LINECOL (node))
3258 /* Issue a missing return statement error. Uses METHOD to figure the
3259 last line of the method the error occurs in. */
3262 missing_return_error (tree method)
3264 #ifdef USE_MAPPED_LOCATION
3265 SET_EXPR_LOCATION (wfl_operator, DECL_FUNCTION_LAST_LINE (method));
3267 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_FUNCTION_LAST_LINE (method), -2);
3269 parse_error_context (wfl_operator, "Missing return statement");
3272 /* Issue an unreachable statement error. From NODE, find the next
3273 statement to report appropriately. */
3275 unreachable_stmt_error (tree node)
3277 /* Browse node to find the next expression node that has a WFL. Use
3278 the location to report the error */
3279 if (TREE_CODE (node) == COMPOUND_EXPR)
3280 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3282 node = find_expr_with_wfl (node);
3286 #ifdef USE_MAPPED_LOCATION
3287 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
3289 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3291 parse_error_context (wfl_operator, "Unreachable statement");
3298 not_accessible_field_error (tree wfl, tree decl)
3301 (wfl, "Can't access %s field %<%s.%s%> from %qs",
3302 accessibility_string (get_access_flags_from_decl (decl)),
3303 GET_TYPE_NAME (DECL_CONTEXT (decl)),
3304 IDENTIFIER_POINTER (DECL_NAME (decl)),
3305 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
3310 java_report_errors (void)
3312 if (java_error_count)
3313 fprintf (stderr, "%d error%s",
3314 java_error_count, (java_error_count == 1 ? "" : "s"));
3315 if (java_warning_count)
3316 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3317 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3318 if (java_error_count || java_warning_count)
3319 putc ('\n', stderr);
3320 return java_error_count;
3324 java_accstring_lookup (int flags)
3326 static char buffer [80];
3327 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3329 /* Access modifier looked-up first for easier report on forbidden
3331 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3332 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3333 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3334 if (flags & ACC_STATIC) COPY_RETURN ("static");
3335 if (flags & ACC_FINAL) COPY_RETURN ("final");
3336 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3337 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3338 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3339 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3340 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3341 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3348 /* Returns a string denoting the accessibility of a class or a member as
3349 indicated by FLAGS. We need a separate function from
3350 java_accstring_lookup, as the latter can return spurious "static", etc.
3351 if package-private access is defined (in which case none of the
3352 relevant access control bits in FLAGS is set). */
3355 accessibility_string (int flags)
3357 if (flags & ACC_PRIVATE) return "private";
3358 if (flags & ACC_PROTECTED) return "protected";
3359 if (flags & ACC_PUBLIC) return "public";
3361 return "package-private";
3364 /* Issuing error messages upon redefinition of classes, interfaces or
3368 classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3370 parse_error_context (cl, "%s %qs already defined in %s:%d",
3371 context, IDENTIFIER_POINTER (id),
3372 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3373 /* Here we should point out where its redefined. It's a unicode. FIXME */
3377 variable_redefinition_error (tree context, tree name, tree type, int line)
3379 const char *type_name;
3381 /* Figure a proper name for type. We might haven't resolved it */
3382 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3383 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3385 type_name = lang_printable_name (type, 0);
3387 parse_error_context (context,
3388 "Variable %qs is already defined in this method and was declared %<%s %s%> at line %d",
3389 IDENTIFIER_POINTER (name),
3390 type_name, IDENTIFIER_POINTER (name), line);
3393 /* If ANAME is terminated with `[]', it indicates an array. This
3394 function returns the number of `[]' found and if this number is
3395 greater than zero, it extracts the array type name and places it in
3396 the node pointed to by TRIMMED unless TRIMMED is null. */
3399 build_type_name_from_array_name (tree aname, tree *trimmed)
3401 const char *name = IDENTIFIER_POINTER (aname);
3402 int len = IDENTIFIER_LENGTH (aname);
3405 STRING_STRIP_BRACKETS (name, len, array_dims);
3407 if (array_dims && trimmed)
3408 *trimmed = get_identifier_with_length (name, len);
3414 build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3418 /* Eventually get more dims */
3419 more_dims = build_type_name_from_array_name (name, &name);
3421 /* If we have, then craft a new type for this variable */
3426 /* If we have a pointer, use its type */
3427 if (TREE_CODE (type) == POINTER_TYPE)
3428 type = TREE_TYPE (type);
3430 /* Building the first dimension of a primitive type uses this
3432 if (JPRIMITIVE_TYPE_P (type))
3434 type = build_java_array_type (type, -1);
3437 /* Otherwise, if we have a WFL for this type, use it (the type
3438 is already an array on an unresolved type, and we just keep
3439 on adding dimensions) */
3443 more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3447 /* Add all the dimensions */
3449 type = build_unresolved_array_type (type);
3451 /* The type may have been incomplete in the first place */
3453 type = obtain_incomplete_type (type);
3461 /* Build something that the type identifier resolver will identify as
3462 being an array to an unresolved type. TYPE_WFL is a WFL on a
3466 build_unresolved_array_type (tree type_or_wfl)
3471 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3472 just create a array type */
3473 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3474 return build_java_array_type (type_or_wfl, -1);
3476 obstack_grow (&temporary_obstack,
3477 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3478 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3479 obstack_grow0 (&temporary_obstack, "[]", 2);
3480 ptr = obstack_finish (&temporary_obstack);
3481 #ifdef USE_MAPPED_LOCATION
3482 wfl = build_expr_wfl (get_identifier (ptr), EXPR_LOCATION (type_or_wfl));
3484 wfl = build_expr_wfl (get_identifier (ptr),
3485 EXPR_WFL_FILENAME (type_or_wfl),
3486 EXPR_WFL_LINENO (type_or_wfl),
3487 EXPR_WFL_COLNO (type_or_wfl));
3489 /* Re-install the existing qualifications so that the type can be
3490 resolved properly. */
3491 EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3496 parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3498 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3499 parse_error_context (wfl, "Interface %qs repeated",
3500 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3503 /* Bulk of common class/interface checks. Return 1 if an error was
3504 encountered. TAG is 0 for a class, 1 for an interface. */
3507 check_class_interface_creation (int is_interface, int flags, tree raw_name,
3508 tree qualified_name, tree decl, tree cl)
3511 int sca = 0; /* Static class allowed */
3512 int icaf = 0; /* Inner class allowed flags */
3513 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3516 fprintf (stderr, " %s%s %s",
3517 (CPC_INNER_P () ? "inner" : ""),
3518 (is_interface ? "interface" : "class"),
3519 IDENTIFIER_POINTER (qualified_name));
3521 /* Scope of an interface/class type name:
3522 - Can't be imported by a single type import
3523 - Can't already exists in the package */
3524 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3525 && (node = find_name_in_single_imports (raw_name))
3529 (cl, "%s name %qs clashes with imported type %qs",
3530 (is_interface ? "Interface" : "Class"),
3531 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3534 if (decl && CLASS_COMPLETE_P (decl))
3536 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3537 qualified_name, decl, cl);
3541 if (check_inner_class_redefinition (raw_name, cl))
3544 /* If public, file name should match class/interface name, except
3545 when dealing with an inner class */
3546 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3548 const char *fname = input_filename;
3551 for (f = fname + strlen (fname);
3552 f != fname && ! IS_DIR_SEPARATOR (*f);
3555 if (IS_DIR_SEPARATOR (*f))
3557 if (strncmp (IDENTIFIER_POINTER (raw_name),
3558 f , IDENTIFIER_LENGTH (raw_name)) ||
3559 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3561 (cl, "Public %s %qs must be defined in a file called %<%s.java%>",
3562 (is_interface ? "interface" : "class"),
3563 IDENTIFIER_POINTER (qualified_name),
3564 IDENTIFIER_POINTER (raw_name));
3567 /* Static classes can be declared only in top level classes. Note:
3568 once static, a inner class is a top level class. */
3569 if (flags & ACC_STATIC)
3571 /* Catch the specific error of declaring an class inner class
3572 with no toplevel enclosing class. Prevent check_modifiers from
3573 complaining a second time */
3574 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3576 parse_error_context (cl, "Inner class %qs can't be static. Static classes can only occur in interfaces and top-level classes",
3577 IDENTIFIER_POINTER (qualified_name));
3580 /* Else, in the context of a top-level class declaration, let
3581 `check_modifiers' do its job, otherwise, give it a go */
3583 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3586 /* Inner classes can be declared private or protected
3587 within their enclosing classes. */
3590 /* A class which is local to a block can't be public, private,
3591 protected or static. But it is created final, so allow this
3593 if (current_function_decl)
3594 icaf = sca = uaaf = ACC_FINAL;
3597 check_modifiers_consistency (flags);
3598 icaf = ACC_PROTECTED;
3599 if (! CLASS_INTERFACE (GET_CPC ()))
3600 icaf |= ACC_PRIVATE;
3607 uaaf = INTERFACE_INNER_MODIFIERS;
3609 uaaf = INTERFACE_MODIFIERS;
3611 check_modifiers ("Illegal modifier %qs for interface declaration",
3615 check_modifiers ((current_function_decl ?
3616 "Illegal modifier %qs for local class declaration" :
3617 "Illegal modifier %qs for class declaration"),
3618 flags, uaaf|sca|icaf);
3622 /* Construct a nested class name. If the final component starts with
3623 a digit, return true. Otherwise return false. */
3625 make_nested_class_name (tree cpc_list)
3632 make_nested_class_name (TREE_CHAIN (cpc_list));
3634 /* Pick the qualified name when dealing with the first upmost
3636 name = (TREE_CHAIN (cpc_list)
3637 ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3638 obstack_grow (&temporary_obstack,
3639 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3640 obstack_1grow (&temporary_obstack, '$');
3642 return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3645 /* Can't redefine a class already defined in an earlier scope. */
3648 check_inner_class_redefinition (tree raw_name, tree cl)
3652 for (scope_list = GET_CPC_LIST (); scope_list;
3653 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3654 if (raw_name == GET_CPC_UN_NODE (scope_list))
3657 (cl, "The class name %qs is already defined in this scope. An inner class may not have the same simple name as any of its enclosing classes",
3658 IDENTIFIER_POINTER (raw_name));
3664 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3665 we remember ENCLOSING and SUPER. */
3668 resolve_inner_class (htab_t circularity_hash, tree cl, tree *enclosing,
3669 tree *super, tree class_type)
3671 tree local_enclosing = *enclosing;
3672 tree local_super = NULL_TREE;
3674 while (local_enclosing)
3676 tree intermediate, decl;
3678 *htab_find_slot (circularity_hash, local_enclosing, INSERT) =
3681 if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3684 intermediate = local_enclosing;
3685 /* Explore enclosing contexts. */
3686 while (INNER_CLASS_DECL_P (intermediate))
3688 intermediate = DECL_CONTEXT (intermediate);
3689 if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3693 /* Now go to the upper classes, bail out if necessary. We will
3694 analyze the returned SUPER and act accordingly (see
3695 do_resolve_class). */
3696 if (JPRIMITIVE_TYPE_P (TREE_TYPE (local_enclosing))
3697 || TREE_TYPE (local_enclosing) == void_type_node)
3699 parse_error_context (cl, "Qualifier must be a reference");
3700 local_enclosing = NULL_TREE;
3703 local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3704 if (!local_super || local_super == object_type_node)
3707 if (TREE_CODE (local_super) == POINTER_TYPE)
3708 local_super = do_resolve_class (NULL, NULL, local_super, NULL, NULL);
3710 local_super = TYPE_NAME (local_super);
3712 /* We may not have checked for circular inheritance yet, so do so
3713 here to prevent an infinite loop. */
3714 if (htab_find (circularity_hash, local_super) != NULL)
3717 cl = lookup_cl (local_enclosing);
3720 (cl, "Cyclic inheritance involving %s",
3721 IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3722 local_enclosing = NULL_TREE;
3725 local_enclosing = local_super;
3728 /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3729 *super = local_super;
3730 *enclosing = local_enclosing;
3735 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3739 find_as_inner_class (tree enclosing, tree name, tree cl)
3741 tree qual, to_return;
3745 name = TYPE_NAME (name);
3747 /* First search: within the scope of `enclosing', search for name */
3748 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3749 qual = EXPR_WFL_QUALIFICATION (cl);
3751 qual = build_tree_list (cl, NULL_TREE);
3753 qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3755 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3758 /* We're dealing with a qualified name. Try to resolve thing until
3759 we get something that is an enclosing class. */
3760 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3762 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3764 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3765 qual = TREE_CHAIN (qual))
3767 acc = merge_qualified_name (acc,
3768 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3769 BUILD_PTR_FROM_NAME (ptr, acc);
3770 decl = do_resolve_class (NULL_TREE, NULL_TREE, ptr, NULL_TREE, cl);
3773 /* A NULL qual and a decl means that the search ended
3774 successfully?!? We have to do something then. FIXME */
3779 qual = EXPR_WFL_QUALIFICATION (cl);
3781 /* Otherwise, create a qual for the other part of the resolution. */
3783 qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3785 return find_as_inner_class_do (qual, enclosing);
3788 /* We go inside the list of sub classes and try to find a way
3792 find_as_inner_class_do (tree qual, tree enclosing)
3797 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3799 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3800 tree next_enclosing = NULL_TREE;
3803 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3804 inner_list; inner_list = TREE_CHAIN (inner_list))
3806 if (TREE_VALUE (inner_list) == name_to_match)
3808 next_enclosing = TREE_PURPOSE (inner_list);
3812 enclosing = next_enclosing;
3815 return (!qual && enclosing ? enclosing : NULL_TREE);
3819 link_nested_class_to_enclosing (void)
3821 if (GET_ENCLOSING_CPC ())
3823 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3824 DECL_INNER_CLASS_LIST (enclosing) =
3825 tree_cons (GET_CPC (), GET_CPC_UN (),
3826 DECL_INNER_CLASS_LIST (enclosing));
3831 maybe_make_nested_class_name (tree name)
3833 tree id = NULL_TREE;
3837 /* If we're in a function, we must append a number to create the
3838 nested class name. However, we don't do this if the class we
3839 are constructing is anonymous, because in that case we'll
3840 already have a number as the class name. */
3841 if (! make_nested_class_name (GET_CPC_LIST ())
3842 && current_function_decl != NULL_TREE
3843 && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3846 sprintf (buf, "%d", anonymous_class_counter);
3847 ++anonymous_class_counter;
3848 obstack_grow (&temporary_obstack, buf, strlen (buf));
3849 obstack_1grow (&temporary_obstack, '$');
3851 obstack_grow0 (&temporary_obstack,
3852 IDENTIFIER_POINTER (name),
3853 IDENTIFIER_LENGTH (name));
3854 id = get_identifier (obstack_finish (&temporary_obstack));
3856 QUALIFIED_P (id) = 1;
3861 /* If DECL is NULL, create and push a new DECL, record the current
3862 line CL and do other maintenance things. */
3865 maybe_create_class_interface_decl (tree decl, tree raw_name,
3866 tree qualified_name, tree cl)
3869 decl = push_class (make_class (), qualified_name);
3871 /* Take care of the file and line business */
3872 #ifdef USE_MAPPED_LOCATION
3873 DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (cl);
3875 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3876 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3878 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3879 CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3880 #ifdef USE_MAPPED_LOCATION
3882 tree tmp = maybe_get_identifier (EXPR_FILENAME (cl));
3883 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3884 tmp && IS_A_COMMAND_LINE_FILENAME_P (tmp);
3887 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3888 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3891 PUSH_CPC (decl, raw_name);
3892 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3894 /* Link the declaration to the already seen ones */
3895 TREE_CHAIN (decl) = ctxp->class_list;
3896 ctxp->class_list = decl;
3898 /* Create a new nodes in the global lists */
3899 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3900 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3902 /* Install a new dependency list element */
3903 create_jdep_list (ctxp);
3905 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3906 IDENTIFIER_POINTER (qualified_name)));
3911 add_superinterfaces (tree decl, tree interface_list)
3914 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3915 takes care of ensuring that:
3916 - This is an accessible interface type,
3917 - Circularity detection.
3918 parser_add_interface is then called. If present but not defined,
3919 the check operation is delayed until the super interface gets
3921 for (node = interface_list; node; node = TREE_CHAIN (node))
3923 tree current = TREE_PURPOSE (node);
3924 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3925 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3927 if (!parser_check_super_interface (idecl, decl, current))
3928 parser_add_interface (decl, idecl, current);
3931 register_incomplete_type (JDEP_INTERFACE,
3932 current, decl, NULL_TREE);
3936 /* Create an interface in pass1 and return its decl. Return the
3937 interface's decl in pass 2. */
3940 create_interface (int flags, tree id, tree super)
3942 tree raw_name = EXPR_WFL_NODE (id);
3943 tree q_name = parser_qualified_classname (raw_name);
3944 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3946 /* Certain syntax errors are making SUPER be like ID. Avoid this
3948 if (ctxp->class_err && id == super)
3951 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3953 /* Basic checks: scope, redefinition, modifiers */
3954 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3960 /* Suspend the current parsing context if we're parsing an inner
3964 java_parser_context_suspend ();
3965 /* Interface members are public. */
3966 if (CLASS_INTERFACE (GET_CPC ()))
3967 flags |= ACC_PUBLIC;
3970 /* Push a new context for (static) initialized upon declaration fields */
3971 java_parser_context_push_initialized_field ();
3973 /* Interface modifiers check
3974 - public/abstract allowed (already done at that point)
3975 - abstract is obsolete (comes first, it's a warning, or should be)
3976 - Can't use twice the same (checked in the modifier rule) */
3977 if ((flags & ACC_ABSTRACT) && flag_redundant)
3978 parse_warning_context
3979 (MODIFIER_WFL (ABSTRACT_TK),
3980 "Redundant use of %<abstract%> modifier. Interface %qs is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3982 /* Create a new decl if DECL is NULL, otherwise fix it */
3983 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3985 /* Interfaces are always abstract. */
3986 flags |= ACC_ABSTRACT;
3988 /* Inner interfaces are always static. */
3989 if (INNER_CLASS_DECL_P (decl))
3990 flags |= ACC_STATIC;
3992 /* Set super info and mark the class a complete */
3993 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3994 object_type_node, ctxp->interface_number);
3995 ctxp->interface_number = 0;
3996 CLASS_COMPLETE_P (decl) = 1;
3997 add_superinterfaces (decl, super);
3999 /* Eventually sets the @deprecated tag flag */
4000 CHECK_DEPRECATED (decl);
4005 /* Patch anonymous class CLASS, by either extending or implementing
4009 patch_anonymous_class (tree type_decl, tree class_decl, tree wfl)
4011 tree class = TREE_TYPE (class_decl);
4012 tree type = TREE_TYPE (type_decl);
4013 tree binfo = TYPE_BINFO (class);
4015 /* If it's an interface, implement it */
4016 if (CLASS_INTERFACE (type_decl))
4018 if (parser_check_super_interface (type_decl, class_decl, wfl))
4021 if (!VEC_space (tree, BINFO_BASE_BINFOS (binfo), 1))
4023 /* Extend the binfo - by reallocating and copying it. */
4028 new_binfo = make_tree_binfo ((BINFO_N_BASE_BINFOS (binfo) + 1) * 2);
4029 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4030 BINFO_BASE_APPEND (new_binfo, base_binfo);
4031 CLASS_HAS_SUPER_FLAG (new_binfo) = CLASS_HAS_SUPER_FLAG (binfo);
4032 BINFO_VTABLE (new_binfo) = BINFO_VTABLE (binfo);
4033 TYPE_BINFO (class) = new_binfo;
4036 /* And add the interface */
4037 parser_add_interface (class_decl, type_decl, wfl);
4039 /* Otherwise, it's a type we want to extend */
4042 if (parser_check_super (type_decl, class_decl, wfl))
4044 BINFO_TYPE (BINFO_BASE_BINFO (binfo, 0)) = type;
4048 /* Create an anonymous class which extends/implements TYPE_NAME, and return
4052 create_anonymous_class (tree type_name)
4055 tree super = NULL_TREE, itf = NULL_TREE;
4056 tree id, type_decl, class;
4058 /* The unqualified name of the anonymous class. It's just a number. */
4059 sprintf (buffer, "%d", anonymous_class_counter++);
4060 id = build_wfl_node (get_identifier (buffer));
4061 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL (type_name);
4063 /* We know about the type to extend/implement. We go ahead */
4064 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
4066 /* Create a class which either implements on extends the designated
4067 class. The class bears an inaccessible name. */
4068 if (CLASS_INTERFACE (type_decl))
4070 /* It's OK to modify it here. It's been already used and
4071 shouldn't be reused */
4072 ctxp->interface_number = 1;
4073 /* Interfaces should presented as a list of WFLs */
4074 itf = build_tree_list (type_name, NULL_TREE);
4080 class = create_class (ACC_FINAL, id, super, itf);
4082 /* We didn't know anything about the stuff. We register a dependence. */
4084 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
4086 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
4090 /* Create a class in pass1 and return its decl. Return class
4091 interface's decl in pass 2. */
4094 create_class (int flags, tree id, tree super, tree interfaces)
4096 tree raw_name = EXPR_WFL_NODE (id);
4097 tree class_id, decl;
4098 tree super_decl_type;
4100 /* Certain syntax errors are making SUPER be like ID. Avoid this
4102 if (ctxp->class_err && id == super)
4105 class_id = parser_qualified_classname (raw_name);
4106 decl = IDENTIFIER_CLASS_VALUE (class_id);
4107 EXPR_WFL_NODE (id) = class_id;
4109 /* Basic check: scope, redefinition, modifiers */
4110 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
4116 /* Suspend the current parsing context if we're parsing an inner
4117 class or an anonymous class. */
4120 java_parser_context_suspend ();
4121 /* Interface members are public. */
4122 if (CLASS_INTERFACE (GET_CPC ()))
4123 flags |= ACC_PUBLIC;
4126 /* Push a new context for (static) initialized upon declaration fields */
4127 java_parser_context_push_initialized_field ();
4129 /* Class modifier check:
4130 - Allowed modifier (already done at that point)
4131 - abstract AND final forbidden
4132 - Public classes defined in the correct file */
4133 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4135 (id, "Class %qs can't be declared both abstract and final",
4136 IDENTIFIER_POINTER (raw_name));
4138 /* Create a new decl if DECL is NULL, otherwise fix it */
4139 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4141 /* If SUPER exists, use it, otherwise use Object */
4144 /* java.lang.Object can't extend anything. */
4145 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4147 parse_error_context (id, "%<java.lang.Object%> can't extend anything");
4152 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4154 else if (TREE_TYPE (decl) != object_type_node)
4155 super_decl_type = object_type_node;
4156 /* We're defining java.lang.Object */
4158 super_decl_type = NULL_TREE;
4160 /* A class nested in an interface is implicitly static. */
4161 if (INNER_CLASS_DECL_P (decl)
4162 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4164 flags |= ACC_STATIC;
4167 /* Set super info and mark the class as complete. */
4168 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4169 ctxp->interface_number);
4170 ctxp->interface_number = 0;
4171 CLASS_COMPLETE_P (decl) = 1;
4172 add_superinterfaces (decl, interfaces);
4174 /* TYPE_VFIELD' is a compiler-generated field used to point to
4175 virtual function tables. In gcj, every class has a common base
4176 virtual function table in java.lang.object. */
4177 TYPE_VFIELD (TREE_TYPE (decl)) = TYPE_VFIELD (object_type_node);
4179 /* We keep the compilation unit imports in the class so that
4180 they can be used later to resolve type dependencies that
4181 aren't necessary to solve now. */
4182 TYPE_IMPORT_LIST (TREE_TYPE (decl)) = ctxp->import_list;
4183 TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (decl)) = ctxp->import_demand_list;
4185 /* Add the private this$<n> field, Replicate final locals still in
4186 scope as private final fields mangled like val$<local_name>.
4187 This does not occur for top level (static) inner classes. */
4188 if (PURE_INNER_CLASS_DECL_P (decl))
4189 add_inner_class_fields (decl, current_function_decl);
4191 /* Eventually sets the @deprecated tag flag */
4192 CHECK_DEPRECATED (decl);
4194 /* Reset the anonymous class counter when declaring non inner classes */
4195 if (!INNER_CLASS_DECL_P (decl))
4196 anonymous_class_counter = 1;
4201 /* End a class declaration: register the statements used to create
4202 finit$ and <clinit>, pop the current class and resume the prior
4203 parser context if necessary. */
4206 end_class_declaration (int resume)
4208 /* If an error occurred, context weren't pushed and won't need to be
4209 popped by a resume. */
4210 int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4212 if (GET_CPC () != error_mark_node)
4213 dump_java_tree (TDI_class, GET_CPC ());
4215 java_parser_context_pop_initialized_field ();
4217 if (resume && no_error_occurred)
4218 java_parser_context_resume ();
4220 /* We're ending a class declaration, this is a good time to reset
4221 the interface cout. Note that might have been already done in
4222 create_interface, but if at that time an inner class was being
4223 dealt with, the interface count was reset in a context created
4224 for the sake of handling inner classes declaration. */
4225 ctxp->interface_number = 0;
4229 add_inner_class_fields (tree class_decl, tree fct_decl)
4231 tree block, marker, f;
4233 f = add_field (TREE_TYPE (class_decl),
4234 build_current_thisn (TREE_TYPE (class_decl)),
4235 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4237 FIELD_THISN (f) = 1;
4242 for (block = GET_CURRENT_BLOCK (fct_decl);
4243 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4246 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4249 tree wfl, init, list;
4251 /* Avoid non final arguments. */
4252 if (!LOCAL_FINAL_P (decl))
4255 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4256 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4257 wfl = build_wfl_node (name);
4258 init = build_wfl_node (pname);
4259 /* Build an initialization for the field: it will be
4260 initialized by a parameter added to finit$, bearing a
4261 mangled name of the field itself (param$<n>.) The
4262 parameter is provided to finit$ by the constructor
4263 invoking it (hence the constructor will also feature a
4264 hidden parameter, set to the value of the outer context
4265 local at the time the inner class is created.)
4267 Note: we take into account all possible locals that can
4268 be accessed by the inner class. It's actually not trivial
4269 to minimize these aliases down to the ones really
4270 used. One way to do that would be to expand all regular
4271 methods first, then finit$ to get a picture of what's
4272 used. It works with the exception that we would have to
4273 go back on all constructor invoked in regular methods to
4274 have their invocation reworked (to include the right amount
4275 of alias initializer parameters.)
4277 The only real way around, I think, is a first pass to
4278 identify locals really used in the inner class. We leave
4279 the flag FIELD_LOCAL_ALIAS_USED around for that future
4282 On the other hand, it only affect local inner classes,
4283 whose constructors (and finit$ call) will be featuring
4284 unnecessary arguments. It's easy for a developer to keep
4285 this number of parameter down by using the `final'
4286 keyword only when necessary. For the time being, we can
4287 issue a warning on unnecessary finals. FIXME */
4288 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4291 /* Register the field. The TREE_LIST holding the part
4292 initialized/initializer will be marked ARG_FINAL_P so
4293 that the created field can be marked
4294 FIELD_LOCAL_ALIAS. */
4295 list = build_tree_list (wfl, init);
4296 ARG_FINAL_P (list) = 1;
4297 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4301 if (!CPC_INITIALIZER_STMT (ctxp))
4304 /* If we ever registered an alias field, insert and marker to
4305 remember where the list ends. The second part of the list (the one
4306 featuring initialized fields) so it can be later reversed to
4307 enforce 8.5. The marker will be removed during that operation. */
4308 marker = build_tree_list (NULL_TREE, NULL_TREE);
4309 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4310 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4313 /* Can't use lookup_field () since we don't want to load the class and
4314 can't set the CLASS_LOADED_P flag */
4317 find_field (tree class, tree name)
4320 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4322 if (DECL_NAME (decl) == name)
4328 /* Wrap around lookup_field that doesn't potentially upset the value
4332 lookup_field_wrapper (tree class, tree name)
4335 tree decl = NULL_TREE;
4336 java_parser_context_save_global ();
4338 /* Last chance: if we're within the context of an inner class, we
4339 might be trying to access a local variable defined in an outer
4340 context. We try to look for it now. */
4341 if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4344 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4345 decl = lookup_field (&type, new_name);
4346 if (decl && decl != error_mark_node)
4347 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4349 if (!decl || decl == error_mark_node)
4352 decl = lookup_field (&type, name);
4355 /* If the field still hasn't been found, try the next enclosing context. */
4356 if (!decl && INNER_CLASS_TYPE_P (class))
4358 tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4359 decl = lookup_field_wrapper (outer_type, name);
4362 java_parser_context_restore_global ();
4363 return decl == error_mark_node ? NULL : decl;
4366 /* Find duplicate field within the same class declarations and report
4367 the error. Returns 1 if a duplicated field was found, 0
4371 duplicate_declaration_error_p (tree new_field_name, tree new_type, tree cl)
4373 /* This might be modified to work with method decl as well */
4374 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4377 char *t1 = xstrdup (purify_type_name
4378 ((TREE_CODE (new_type) == POINTER_TYPE
4379 && TREE_TYPE (new_type) == NULL_TREE) ?
4380 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4381 lang_printable_name (new_type, 1)));
4382 /* The type may not have been completed by the time we report
4384 char *t2 = xstrdup (purify_type_name
4385 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4386 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4387 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4388 lang_printable_name (TREE_TYPE (decl), 1)));
4390 (cl, "Duplicate variable declaration: %<%s %s%> was %<%s %s%> (%s:%d)",
4391 t1, IDENTIFIER_POINTER (new_field_name),
4392 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4393 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4401 /* Field registration routine. If TYPE doesn't exist, field
4402 declarations are linked to the undefined TYPE dependency list, to
4403 be later resolved in java_complete_class () */
4406 register_fields (int flags, tree type, tree variable_list)
4408 tree current, saved_type;
4409 tree class_type = NULL_TREE;
4410 location_t saved_location = input_location;
4412 tree wfl = NULL_TREE;
4415 class_type = TREE_TYPE (GET_CPC ());
4417 if (!class_type || class_type == error_mark_node)
4420 /* If we're adding fields to interfaces, those fields are public,
4422 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4424 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4425 flags, ACC_PUBLIC, "interface field(s)");
4426 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4427 flags, ACC_STATIC, "interface field(s)");
4428 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4429 flags, ACC_FINAL, "interface field(s)");
4430 check_modifiers ("Illegal interface member modifier %qs", flags,
4431 INTERFACE_FIELD_MODIFIERS);
4432 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4435 /* Obtain a suitable type for resolution, if necessary */
4436 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4438 /* If TYPE is fully resolved and we don't have a reference, make one */
4439 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4441 for (current = variable_list, saved_type = type; current;
4442 current = TREE_CHAIN (current), type = saved_type)
4446 tree cl = TREE_PURPOSE (current);
4447 tree init = TREE_VALUE (current);
4448 tree current_name = EXPR_WFL_NODE (cl);
4450 /* Can't declare non-final static fields in inner classes */
4451 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4452 && !(flags & ACC_FINAL))
4454 (cl, "Field %qs can't be static in inner class %qs unless it is final",
4455 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4456 lang_printable_name (class_type, 0));
4458 /* Process NAME, as it may specify extra dimension(s) for it */
4459 type = build_array_from_name (type, wfl, current_name, ¤t_name);
4461 /* Type adjustment. We may have just readjusted TYPE because
4462 the variable specified more dimensions. Make sure we have
4463 a reference if we can and don't have one already. Also
4464 change the name if we have an init. */
4465 if (type != saved_type)
4467 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4469 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4472 real_type = GET_REAL_TYPE (type);
4473 /* Check for redeclarations */
4474 if (duplicate_declaration_error_p (current_name, real_type, cl))
4477 /* Set input_line to the line the field was found and create a
4478 declaration for it. Eventually sets the @deprecated tag flag. */
4479 #ifdef USE_MAPPED_LOCATION
4480 input_location = EXPR_LOCATION (cl);
4482 input_line = EXPR_WFL_LINENO (cl);
4484 field_decl = add_field (class_type, current_name, real_type, flags);
4485 CHECK_DEPRECATED_NO_RESET (field_decl);
4487 /* If the field denotes a final instance variable, then we
4488 allocate a LANG_DECL_SPECIFIC part to keep track of its
4489 initialization. We also mark whether the field was
4490 initialized upon its declaration. We don't do that if the
4491 created field is an alias to a final local. */
4492 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4494 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4495 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4498 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4499 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4500 hide parameters to this inner class finit$ and
4501 constructors. It also means that the field isn't final per
4503 if (ARG_FINAL_P (current))
4505 FIELD_LOCAL_ALIAS (field_decl) = 1;
4506 FIELD_FINAL (field_decl) = 0;
4509 /* Check if we must chain. */
4511 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4513 /* If we have an initialization value tied to the field */
4516 /* The field is declared static */
4517 if (flags & ACC_STATIC)
4519 /* We include the field and its initialization part into
4520 a list used to generate <clinit>. After <clinit> is
4521 walked, field initializations will be processed and
4522 fields initialized with known constants will be taken
4523 out of <clinit> and have their DECL_INITIAL set
4525 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4526 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4527 if (TREE_OPERAND (init, 1)
4528 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4529 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4531 /* A non-static field declared with an immediate initialization is
4532 to be initialized in <init>, if any. This field is remembered
4533 to be processed at the time of the generation of <init>. */
4536 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4537 SET_CPC_INITIALIZER_STMT (ctxp, init);
4539 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4540 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4545 input_location = saved_location;
4548 /* Generate finit$, using the list of initialized fields to populate
4549 its body. finit$'s parameter(s) list is adjusted to include the
4550 one(s) used to initialized the field(s) caching outer context
4554 generate_finit (tree class_type)
4557 tree list = TYPE_FINIT_STMT_LIST (class_type);
4558 tree mdecl, current, parms;
4560 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4561 class_type, NULL_TREE,
4563 CRAFTED_PARAM_LIST_FIXUP (parms);
4564 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4565 finit_identifier_node, parms);
4566 fix_method_argument_names (parms, mdecl);
4567 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4569 DECL_FUNCTION_NAP (mdecl) = count;
4570 start_artificial_method_body (mdecl);
4572 for (current = list; current; current = TREE_CHAIN (current))
4573 java_method_add_stmt (mdecl,
4574 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4576 end_artificial_method_body (mdecl);
4580 /* Generate a function to run the instance initialization code. The
4581 private method is called `instinit$'. Unless we're dealing with an
4582 anonymous class, we determine whether all ctors of CLASS_TYPE
4583 declare a checked exception in their `throws' clause in order to
4584 see whether it's necessary to encapsulate the instance initializer
4585 statements in a try/catch/rethrow sequence. */
4588 generate_instinit (tree class_type)
4591 tree compound = NULL_TREE;
4592 tree parms = tree_cons (this_identifier_node,
4593 build_pointer_type (class_type), end_params_node);
4594 tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4596 instinit_identifier_node, parms);
4598 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4601 /* Gather all the statements in a compound */
4602 for (current = TYPE_II_STMT_LIST (class_type);
4603 current; current = TREE_CHAIN (current))
4604 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4606 /* We need to encapsulate COMPOUND by a try/catch statement to
4607 rethrow exceptions that might occur in the instance initializer.
4608 We do that only if all ctors of CLASS_TYPE are set to catch a
4609 checked exception. This doesn't apply to anonymous classes (since
4610 they don't have declared ctors.) */
4611 if (!ANONYMOUS_CLASS_P (class_type) &&
4612 ctors_unchecked_throws_clause_p (class_type))
4614 compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4615 build1 (THROW_EXPR, NULL_TREE,
4616 build_wfl_node (wpv_id)));
4617 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4618 exception_type_node);
4621 start_artificial_method_body (mdecl);
4622 java_method_add_stmt (mdecl, compound);
4623 end_artificial_method_body (mdecl);
4630 build_instinit_invocation (tree class_type)
4632 tree to_return = NULL_TREE;
4634 if (TYPE_II_STMT_LIST (class_type))
4636 tree parm = build_tree_list (NULL_TREE,
4637 build_wfl_node (this_identifier_node));
4639 build_method_invocation (build_wfl_node (instinit_identifier_node),
4645 /* Shared across method_declarator and method_header to remember the
4646 patch stage that was reached during the declaration of the method.
4647 A method DECL is built differently is there is no patch
4648 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4649 pending on the currently defined method. */
4651 static int patch_stage;
4653 /* Check the method declaration and add the method to its current
4654 class. If the argument list is known to contain incomplete types,
4655 the method is partially added and the registration will be resume
4656 once the method arguments resolved. If TYPE is NULL, we're dealing
4657 with a constructor. */
4660 method_header (int flags, tree type, tree mdecl, tree throws)
4662 tree type_wfl = NULL_TREE;
4663 tree meth_name = NULL_TREE;
4664 tree current, orig_arg, this_class = NULL;
4666 location_t saved_location;
4667 int constructor_ok = 0, must_chain;
4670 if (mdecl == error_mark_node)
4671 return error_mark_node;
4672 meth = TREE_VALUE (mdecl);
4673 id = TREE_PURPOSE (mdecl);
4675 check_modifiers_consistency (flags);
4678 this_class = TREE_TYPE (GET_CPC ());
4680 if (!this_class || this_class == error_mark_node)
4683 /* There are some forbidden modifiers for an abstract method and its
4684 class must be abstract as well. */
4685 if (type && (flags & ACC_ABSTRACT))
4687 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4688 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4689 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4690 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4691 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4692 ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4693 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4694 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4697 "Class %qs must be declared abstract to define abstract method %qs",
4698 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4699 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4702 /* A native method can't be strictfp. */
4703 if ((flags & ACC_NATIVE) && (flags & ACC_STRICT))
4704 parse_error_context (id, "native method %qs can't be strictfp",
4705 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4706 /* No such thing as a transient or volatile method. */
4707 if ((flags & ACC_TRANSIENT))
4708 parse_error_context (id, "method %qs can't be transient",
4709 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4710 if ((flags & ACC_VOLATILE))
4711 parse_error_context (id, "method %qs can't be volatile",
4712 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4714 /* Things to be checked when declaring a constructor */
4717 int ec = java_error_count;
4718 /* 8.6: Constructor declarations: we might be trying to define a
4719 method without specifying a return type. */
4720 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4722 (id, "Invalid method declaration, return type required");
4723 /* 8.6.3: Constructor modifiers */
4726 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4727 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4728 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4729 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4730 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4731 JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4733 /* If we found error here, we don't consider it's OK to tread
4734 the method definition as a constructor, for the rest of this
4736 if (ec == java_error_count)
4740 /* Method declared within the scope of an interface are implicitly
4741 abstract and public. Conflicts with other erroneously provided
4742 modifiers are checked right after. */
4744 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4746 /* If FLAGS isn't set because of a modifier, turn the
4747 corresponding modifier WFL to NULL so we issue a warning on
4748 the obsolete use of the modifier */
4749 if (!(flags & ACC_PUBLIC))
4750 MODIFIER_WFL (PUBLIC_TK) = NULL;
4751 if (!(flags & ACC_ABSTRACT))
4752 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4753 flags |= ACC_PUBLIC;
4754 flags |= ACC_ABSTRACT;
4757 /* Inner class can't declare static methods */
4758 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4761 (id, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
4762 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4763 lang_printable_name (this_class, 0));
4766 /* Modifiers context reset moved up, so abstract method declaration
4767 modifiers can be later checked. */
4769 /* Set constructor returned type to void and method name to <init>,
4770 unless we found an error identifier the constructor (in which
4771 case we retain the original name) */
4774 type = void_type_node;
4776 meth_name = init_identifier_node;
4779 meth_name = EXPR_WFL_NODE (id);
4781 /* Do the returned type resolution and registration if necessary */
4782 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4785 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4786 EXPR_WFL_NODE (id) = meth_name;
4787 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4791 patch_stage = JDEP_METHOD_RETURN;
4792 register_incomplete_type (patch_stage, type_wfl, id, type);
4793 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4796 TREE_TYPE (meth) = type;
4798 saved_location = input_location;
4799 /* When defining an abstract or interface method, the curly
4800 bracket at level 1 doesn't exist because there is no function
4802 #ifdef USE_MAPPED_LOCATION
4803 input_location = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4804 EXPR_LOCATION (id));
4806 input_line = (ctxp->first_ccb_indent1 ? (int) ctxp->first_ccb_indent1 :
4807 EXPR_WFL_LINENO (id));
4810 /* Remember the original argument list */
4811 orig_arg = TYPE_ARG_TYPES (meth);
4813 if (patch_stage) /* includes ret type and/or all args */
4816 meth = add_method_1 (this_class, flags, meth_name, meth);
4817 /* Patch for the return type */
4818 if (patch_stage == JDEP_METHOD_RETURN)
4820 jdep = CLASSD_LAST (ctxp->classd_list);
4821 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4823 /* This is the stop JDEP. METH allows the function's signature
4825 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4828 meth = add_method (this_class, flags, meth_name,
4829 build_java_signature (meth));
4831 /* Remember final parameters */
4832 MARK_FINAL_PARMS (meth, orig_arg);
4834 /* Fix the method argument list so we have the argument name
4836 fix_method_argument_names (orig_arg, meth);
4838 /* Register the parameter number and re-install the current line
4840 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4841 input_location = saved_location;
4843 /* Register exception specified by the `throws' keyword for
4844 resolution and set the method decl appropriate field to the list.
4845 Note: the grammar ensures that what we get here are class
4849 throws = nreverse (throws);
4850 for (current = throws; current; current = TREE_CHAIN (current))
4852 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4853 NULL_TREE, NULL_TREE);
4854 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4855 &TREE_VALUE (current);
4857 DECL_FUNCTION_THROWS (meth) = throws;
4860 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4861 DECL_FUNCTION_WFL (meth) = id;
4863 /* Set the flag if we correctly processed a constructor */
4866 DECL_CONSTRUCTOR_P (meth) = 1;
4867 /* Compute and store the number of artificial parameters declared
4868 for this constructor */
4869 for (count = 0, current = TYPE_FIELDS (this_class); current;
4870 current = TREE_CHAIN (current))
4871 if (FIELD_LOCAL_ALIAS (current))
4873 DECL_FUNCTION_NAP (meth) = count;
4876 /* Eventually set the @deprecated tag flag */
4877 CHECK_DEPRECATED (meth);
4883 fix_method_argument_names (tree orig_arg, tree meth)
4885 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4886 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4888 TREE_PURPOSE (arg) = this_identifier_node;
4889 arg = TREE_CHAIN (arg);
4891 while (orig_arg != end_params_node)
4893 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4894 orig_arg = TREE_CHAIN (orig_arg);
4895 arg = TREE_CHAIN (arg);
4899 /* Complete the method declaration with METHOD_BODY. */
4902 finish_method_declaration (tree method_body)
4906 if (!current_function_decl)
4909 flags = get_access_flags_from_decl (current_function_decl);
4911 /* 8.4.5 Method Body */
4912 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4914 tree name = DECL_NAME (current_function_decl);
4915 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4916 "%s method %qs can't have a body defined",
4917 (METHOD_NATIVE (current_function_decl) ?
4918 "Native" : "Abstract"),
4919 IDENTIFIER_POINTER (name));
4920 method_body = NULL_TREE;
4922 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4924 tree name = DECL_NAME (current_function_decl);
4926 (DECL_FUNCTION_WFL (current_function_decl),
4927 "Non native and non abstract method %qs must have a body defined",
4928 IDENTIFIER_POINTER (name));
4929 method_body = NULL_TREE;
4932 if (flag_emit_class_files && method_body
4933 && TREE_CODE (method_body) == NOP_EXPR
4934 && TREE_TYPE (current_function_decl)
4935 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4936 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4938 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4939 maybe_absorb_scoping_blocks ();
4940 /* Exit function's body */
4942 /* Merge last line of the function with first line, directly in the
4943 function decl. It will be used to emit correct debug info. */
4944 DECL_FUNCTION_LAST_LINE (current_function_decl) = ctxp->last_ccb_indent1;
4946 /* Since function's argument's list are shared, reset the
4947 ARG_FINAL_P parameter that might have been set on some of this
4948 function parameters. */
4949 UNMARK_FINAL_PARMS (current_function_decl);
4951 /* So we don't have an irrelevant function declaration context for
4952 the next static block we'll see. */
4953 current_function_decl = NULL_TREE;
4956 /* Build a an error message for constructor circularity errors. */
4959 constructor_circularity_msg (tree from, tree to)
4961 static char string [4096];
4962 char *t = xstrdup (lang_printable_name (from, 2));
4963 sprintf (string, "'%s' invokes '%s'", t, lang_printable_name (to, 2));
4968 /* Verify a circular call to METH. Return 1 if an error is found, 0
4971 static GTY(()) tree vcc_list;
4973 verify_constructor_circularity (tree meth, tree current)
4977 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4979 if (TREE_VALUE (c) == meth)
4985 vcc_list = nreverse (vcc_list);
4986 for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
4989 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4990 constructor_circularity_msg
4991 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4995 t = xstrdup (lang_printable_name (meth, 2));
4996 parse_error_context (TREE_PURPOSE (c),
4997 "%s: recursive invocation of constructor %qs",
4998 constructor_circularity_msg (current, meth), t);
5000 vcc_list = NULL_TREE;
5004 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5006 vcc_list = tree_cons (c, current, vcc_list);
5007 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
5009 vcc_list = TREE_CHAIN (vcc_list);
5014 /* Check modifiers that can be declared but exclusively */
5017 check_modifiers_consistency (int flags)
5020 tree cl = NULL_TREE;
5022 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
5023 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
5024 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
5027 (cl, "Inconsistent member declaration. At most one of %<public%>, %<private%>, or %<protected%> may be specified");
5031 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
5032 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
5034 parse_error_context (cl,
5035 "Inconsistent member declaration. At most one of %<final%> or %<volatile%> may be specified");
5038 /* Check the methode header METH for abstract specifics features */
5041 check_abstract_method_header (tree meth)
5043 int flags = get_access_flags_from_decl (meth);
5045 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
5046 ACC_ABSTRACT, "abstract method",
5047 IDENTIFIER_POINTER (DECL_NAME (meth)));
5048 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
5049 ACC_PUBLIC, "abstract method",
5050 IDENTIFIER_POINTER (DECL_NAME (meth)));
5052 check_modifiers ("Illegal modifier %qs for interface method",
5053 flags, INTERFACE_METHOD_MODIFIERS);
5056 /* Create a FUNCTION_TYPE node and start augmenting it with the
5057 declared function arguments. Arguments type that can't be resolved
5058 are left as they are, but the returned node is marked as containing
5059 incomplete types. */
5062 method_declarator (tree id, tree list)
5064 tree arg_types = NULL_TREE, current, node;
5065 tree meth = make_node (FUNCTION_TYPE);
5068 patch_stage = JDEP_NO_PATCH;
5070 if (GET_CPC () == error_mark_node)
5071 return error_mark_node;
5073 /* If we're dealing with an inner class constructor, we hide the
5074 this$<n> decl in the name field of its parameter declaration. We
5075 also might have to hide the outer context local alias
5076 initializers. Not done when the class is a toplevel class. */
5077 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
5078 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
5080 tree aliases_list, type, thisn;
5081 /* First the aliases, linked to the regular parameters */
5083 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
5084 TREE_TYPE (GET_CPC ()),
5086 list = chainon (nreverse (aliases_list), list);
5089 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
5090 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
5091 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
5095 for (current = list; current; current = TREE_CHAIN (current))
5098 tree wfl_name = TREE_PURPOSE (current);
5099 tree type = TREE_VALUE (current);
5100 tree name = EXPR_WFL_NODE (wfl_name);
5101 tree already, arg_node;
5102 tree type_wfl = NULL_TREE;
5105 /* Obtain a suitable type for resolution, if necessary */
5106 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5108 /* Process NAME, as it may specify extra dimension(s) for it */
5109 type = build_array_from_name (type, type_wfl, name, &name);
5110 EXPR_WFL_NODE (wfl_name) = name;
5112 real_type = GET_REAL_TYPE (type);
5113 if (TREE_CODE (real_type) == RECORD_TYPE)
5115 real_type = promote_type (real_type);
5116 if (TREE_CODE (type) == TREE_LIST)
5117 TREE_PURPOSE (type) = real_type;
5120 /* Check redefinition */
5121 for (already = arg_types; already; already = TREE_CHAIN (already))
5122 if (TREE_PURPOSE (already) == name)
5125 (wfl_name, "Variable %qs is used more than once in the argument list of method %qs",
5126 IDENTIFIER_POINTER (name),
5127 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5131 /* If we've an incomplete argument type, we know there is a location
5132 to patch when the type get resolved, later. */
5136 patch_stage = JDEP_METHOD;
5137 type = register_incomplete_type (patch_stage,
5138 type_wfl, wfl_name, type);
5139 jdep = CLASSD_LAST (ctxp->classd_list);
5140 JDEP_MISC (jdep) = id;
5143 /* The argument node: a name and a (possibly) incomplete type. */
5144 arg_node = build_tree_list (name, real_type);
5145 /* Remember arguments declared final. */
5146 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5149 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5150 TREE_CHAIN (arg_node) = arg_types;
5151 arg_types = arg_node;
5153 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5154 node = build_tree_list (id, meth);
5159 unresolved_type_p (tree wfl, tree *returned)
5161 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5165 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5166 if (decl && current_class && (decl == TYPE_NAME (current_class)))
5167 *returned = TREE_TYPE (decl);
5168 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5169 *returned = TREE_TYPE (GET_CPC ());
5171 *returned = NULL_TREE;
5180 /* From NAME, build a qualified identifier node using the
5181 qualification from the current package definition. */
5184 parser_qualified_classname (tree name)
5186 tree nested_class_name;
5188 if ((nested_class_name = maybe_make_nested_class_name (name)))
5189 return nested_class_name;
5192 return merge_qualified_name (ctxp->package, name);
5197 /* Called once the type a interface extends is resolved. Returns 0 if
5198 everything is OK. */
5201 parser_check_super_interface (tree super_decl, tree this_decl, tree this_wfl)
5203 tree super_type = TREE_TYPE (super_decl);
5205 /* Has to be an interface */
5206 if (!CLASS_INTERFACE (super_decl))
5209 (this_wfl, "%s %qs can't implement/extend %s %qs",
5210 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5211 "Interface" : "Class"),
5212 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5213 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5214 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5218 /* Check top-level interface access. Inner classes are subject to member
5219 access rules (6.6.1). */
5220 if (! INNER_CLASS_P (super_type)
5221 && check_pkg_class_access (DECL_NAME (super_decl),
5222 NULL_TREE, true, this_decl))
5225 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5226 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5227 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5231 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5232 0 if everything is OK. */
5235 parser_check_super (tree super_decl, tree this_decl, tree wfl)
5237 tree super_type = TREE_TYPE (super_decl);
5239 /* SUPER should be a CLASS (neither an array nor an interface) */
5240 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5243 (wfl, "Class %qs can't subclass %s %qs",
5244 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5245 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5246 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5250 if (CLASS_FINAL (TYPE_NAME (super_type)))
5252 parse_error_context (wfl, "Can't subclass final classes: %s",
5253 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5257 /* Check top-level class scope. Inner classes are subject to member access
5259 if (! INNER_CLASS_P (super_type)
5260 && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true, NULL_TREE)))
5263 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5264 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5265 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5269 /* Create a new dependency list and link it (in a LIFO manner) to the
5270 CTXP list of type dependency list. */
5273 create_jdep_list (struct parser_ctxt *ctxp)
5275 jdeplist *new = xmalloc (sizeof (jdeplist));
5276 new->first = new->last = NULL;
5277 new->next = ctxp->classd_list;
5278 ctxp->classd_list = new;
5282 reverse_jdep_list (struct parser_ctxt *ctxp)
5284 jdeplist *prev = NULL, *current, *next;
5285 for (current = ctxp->classd_list; current; current = next)
5287 next = current->next;
5288 current->next = prev;
5294 /* Create a fake pointer based on the ID stored in
5295 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5296 registered again. */
5299 obtain_incomplete_type (tree type_name)
5301 tree ptr = NULL_TREE, name;
5303 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5304 name = EXPR_WFL_NODE (type_name);
5305 else if (INCOMPLETE_TYPE_P (type_name))
5306 name = TYPE_NAME (type_name);
5310 /* Workaround from build_pointer_type for incomplete types. */
5311 BUILD_PTR_FROM_NAME (ptr, name);
5312 TYPE_MODE (ptr) = ptr_mode;
5318 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5319 non NULL instead of computing a new fake type based on WFL. The new
5320 dependency is inserted in the current type dependency list, in FIFO
5324 register_incomplete_type (int kind, tree wfl, tree decl, tree ptr)
5326 jdep *new = xmalloc (sizeof (jdep));
5328 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5329 ptr = obtain_incomplete_type (wfl);
5331 JDEP_KIND (new) = kind;
5332 JDEP_DECL (new) = decl;
5333 JDEP_TO_RESOLVE (new) = ptr;
5334 JDEP_WFL (new) = wfl;
5335 JDEP_CHAIN (new) = NULL;
5336 JDEP_MISC (new) = NULL_TREE;
5337 /* For some dependencies, set the enclosing class of the current
5338 class to be the enclosing context */
5339 if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS || kind == JDEP_SUPER)
5340 && GET_ENCLOSING_CPC ())
5341 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5343 JDEP_ENCLOSING (new) = GET_CPC ();
5344 JDEP_GET_PATCH (new) = (tree *)NULL;
5346 JDEP_INSERT (ctxp->classd_list, new);
5351 /* This checks for circular references with innerclasses. We start
5352 from SOURCE and should never reach TARGET. Extended/implemented
5353 types in SOURCE have their enclosing context checked not to reach
5354 TARGET. When the last enclosing context of SOURCE is reached, its
5355 extended/implemented types are also checked not to reach TARGET.
5356 In case of error, WFL of the offending type is returned; NULL_TREE
5360 check_inner_circular_reference (tree source, tree target)
5366 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (source), i, base_binfo); i++)
5370 /* We can end up with a NULL_TREE or an incomplete type here if
5371 we encountered previous type resolution errors. It's safe to
5372 simply ignore these cases. */
5373 su = BINFO_TYPE (base_binfo);
5374 if (INCOMPLETE_TYPE_P (su))
5377 if (inherits_from_p (su, target))
5378 return lookup_cl (TYPE_NAME (su));
5380 for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5382 /* An enclosing context shouldn't be TARGET */
5383 if (ctx == TYPE_NAME (target))
5384 return lookup_cl (TYPE_NAME (su));
5386 /* When we reach the enclosing last context, start a check
5387 on it, with the same target */
5388 if (! DECL_CONTEXT (ctx) &&
5389 (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5396 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5397 offending type if a circularity is detected. NULL_TREE is returned
5398 otherwise. TYPE can be an interface or a class. */
5401 check_circular_reference (tree type)
5406 if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
5409 if (! CLASS_INTERFACE (TYPE_NAME (type)))
5411 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5412 return lookup_cl (TYPE_NAME (type));
5416 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo); i++)
5418 if (BINFO_TYPE (base_binfo) != object_type_node
5419 && interface_of_p (type, BINFO_TYPE (base_binfo)))
5420 return lookup_cl (TYPE_NAME (BINFO_TYPE (base_binfo)));
5426 java_check_circular_reference (void)
5429 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5431 tree type = TREE_TYPE (current);
5434 cl = check_circular_reference (type);
5436 cl = check_inner_circular_reference (type, type);
5438 parse_error_context (cl, "Cyclic class inheritance%s",
5439 (cyclic_inheritance_report ?
5440 cyclic_inheritance_report : ""));
5444 /* Augment the parameter list PARM with parameters crafted to
5445 initialize outer context locals aliases. Through ARTIFICIAL, a
5446 count is kept of the number of crafted parameters. MODE governs
5447 what eventually gets created: something suitable for a function
5448 creation or a function invocation, either the constructor or
5452 build_alias_initializer_parameter_list (int mode, tree class_type, tree parm,
5456 tree additional_parms = NULL_TREE;
5458 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5459 if (FIELD_LOCAL_ALIAS (field))
5461 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5462 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5467 case AIPL_FUNCTION_DECLARATION:
5468 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5470 purpose = build_wfl_node (mangled_id);
5471 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5472 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5474 value = TREE_TYPE (field);
5477 case AIPL_FUNCTION_CREATION:
5478 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5480 value = TREE_TYPE (field);
5483 case AIPL_FUNCTION_FINIT_INVOCATION:
5484 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5486 /* Now, this is wrong. purpose should always be the NAME
5487 of something and value its matching value (decl, type,
5488 etc...) FIXME -- but there is a lot to fix. */
5490 /* When invoked for this kind of operation, we already
5491 know whether a field is used or not. */
5492 purpose = TREE_TYPE (field);
5493 value = build_wfl_node (mangled_id);
5496 case AIPL_FUNCTION_CTOR_INVOCATION:
5497 /* There are two case: the constructor invocation happens
5498 outside the local inner, in which case, locales from the outer
5499 context are directly used.
5501 Otherwise, we fold to using the alias directly. */
5502 if (class_type == current_class)
5506 name = get_identifier (&buffer[4]);
5507 value = IDENTIFIER_LOCAL_VALUE (name);
5511 additional_parms = tree_cons (purpose, value, additional_parms);
5515 if (additional_parms)
5517 if (ANONYMOUS_CLASS_P (class_type)
5518 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5519 additional_parms = nreverse (additional_parms);
5520 parm = chainon (additional_parms, parm);
5526 /* Craft a constructor for CLASS_DECL -- what we should do when none
5527 where found. ARGS is non NULL when a special signature must be
5528 enforced. This is the case for anonymous classes. */
5531 craft_constructor (tree class_decl, tree args)
5533 tree class_type = TREE_TYPE (class_decl);
5534 tree parm = NULL_TREE;
5535 /* Inherit access flags for the constructor from its enclosing class. */
5536 int valid_ctor_flags = ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE;
5537 int flags = (get_access_flags_from_decl (class_decl) & valid_ctor_flags);
5538 int i = 0, artificial = 0;
5539 tree decl, ctor_name;
5542 ctor_name = init_identifier_node;
5544 /* If we're dealing with an inner class constructor, we hide the
5545 this$<n> decl in the name field of its parameter declaration. */
5546 if (PURE_INNER_CLASS_TYPE_P (class_type))
5548 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5549 parm = tree_cons (build_current_thisn (class_type),
5550 build_pointer_type (type), parm);
5552 /* Some more arguments to be hidden here. The values of the local
5553 variables of the outer context that the inner class needs to see. */
5554 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5559 /* Then if there are any args to be enforced, enforce them now */
5560 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5562 /* If we see a `void *', we need to change it to Object. */
5563 if (TREE_VALUE (args) == TREE_TYPE (null_pointer_node))
5564 TREE_VALUE (args) = object_ptr_type_node;
5566 sprintf (buffer, "parm%d", i++);
5567 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5570 CRAFTED_PARAM_LIST_FIXUP (parm);
5571 decl = create_artificial_method (class_type, flags, void_type_node,
5573 fix_method_argument_names (parm, decl);
5574 /* Now, mark the artificial parameters. */
5575 DECL_FUNCTION_NAP (decl) = artificial;
5576 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5577 DECL_INLINE (decl) = 1;
5582 /* Fix the constructors. This will be called right after circular
5583 references have been checked. It is necessary to fix constructors
5584 early even if no code generation will take place for that class:
5585 some generated constructor might be required by the class whose
5586 compilation triggered this one to be simply loaded. */
5589 java_fix_constructors (void)
5593 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5595 tree class_type = TREE_TYPE (current);
5599 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5602 output_class = current_class = class_type;
5603 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5605 if (DECL_CONSTRUCTOR_P (decl))
5607 fix_constructors (decl);
5612 /* Anonymous class constructor can't be generated that early. */
5613 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5614 craft_constructor (current, NULL_TREE);
5618 /* safe_layout_class just makes sure that we can load a class without
5619 disrupting the current_class, input_file, input_line, etc, information
5620 about the class processed currently. */
5623 safe_layout_class (tree class)
5625 tree save_current_class = current_class;
5626 location_t save_location = input_location;
5628 layout_class (class);
5630 current_class = save_current_class;
5631 input_location = save_location;
5635 jdep_resolve_class (jdep *dep)
5639 if (JDEP_RESOLVED_P (dep))
5640 decl = JDEP_RESOLVED_DECL (dep);
5643 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5644 JDEP_DECL (dep), JDEP_WFL (dep));
5645 JDEP_RESOLVED (dep, decl);
5646 /* If there is no WFL, that's ok. We generate this warning
5648 if (decl && JDEP_WFL (dep) != NULL_TREE)
5649 check_deprecation (JDEP_WFL (dep), decl);
5653 complete_class_report_errors (dep);
5654 else if (INNER_CLASS_DECL_P (decl))
5656 tree inner = TREE_TYPE (decl);
5657 if (! CLASS_LOADED_P (inner))
5659 safe_layout_class (inner);
5660 if (TYPE_SIZE (inner) == error_mark_node)
5661 TYPE_SIZE (inner) = NULL_TREE;
5663 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5668 /* Complete unsatisfied class declaration and their dependencies */
5671 java_complete_class (void)
5678 /* Process imports */
5681 /* Reverse things so we have the right order */
5682 ctxp->class_list = nreverse (ctxp->class_list);
5683 ctxp->classd_list = reverse_jdep_list (ctxp);
5685 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5687 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5691 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5694 if (!(decl = jdep_resolve_class (dep)))
5697 /* Now it's time to patch */
5698 switch (JDEP_KIND (dep))
5701 /* Simply patch super */
5702 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5704 BINFO_TYPE (BINFO_BASE_BINFO
5705 (TYPE_BINFO (TREE_TYPE (JDEP_DECL (dep))), 0))
5711 /* We do part of the job done in add_field */
5712 tree field_decl = JDEP_DECL (dep);
5713 tree field_type = TREE_TYPE (decl);
5714 if (TREE_CODE (field_type) == RECORD_TYPE)
5715 field_type = promote_type (field_type);
5716 TREE_TYPE (field_decl) = field_type;
5717 DECL_ALIGN (field_decl) = 0;
5718 DECL_USER_ALIGN (field_decl) = 0;
5719 layout_decl (field_decl, 0);
5720 SOURCE_FRONTEND_DEBUG
5721 (("Completed field/var decl '%s' with '%s'",
5722 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5723 IDENTIFIER_POINTER (DECL_NAME (decl))));
5726 case JDEP_METHOD: /* We start patching a method */
5727 case JDEP_METHOD_RETURN:
5733 type = TREE_TYPE(decl);
5734 if (TREE_CODE (type) == RECORD_TYPE)
5735 type = promote_type (type);
5736 JDEP_APPLY_PATCH (dep, type);
5737 SOURCE_FRONTEND_DEBUG
5738 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5739 "Completing fct '%s' with ret type '%s'":
5740 "Completing arg '%s' with type '%s'"),
5741 IDENTIFIER_POINTER (EXPR_WFL_NODE
5742 (JDEP_DECL_WFL (dep))),
5743 IDENTIFIER_POINTER (DECL_NAME (decl))));
5747 dep = JDEP_CHAIN (dep);
5748 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5751 decl = jdep_resolve_class (dep);
5755 tree mdecl = JDEP_DECL (dep), signature;
5756 /* Recompute and reset the signature, check first that
5757 all types are now defined. If they're not,
5758 don't build the signature. */
5759 if (check_method_types_complete (mdecl))
5761 signature = build_java_signature (TREE_TYPE (mdecl));
5762 set_java_signature (TREE_TYPE (mdecl), signature);
5769 case JDEP_INTERFACE:
5770 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5773 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5778 type = TREE_TYPE(decl);
5779 if (TREE_CODE (type) == RECORD_TYPE)
5780 type = promote_type (type);
5781 JDEP_APPLY_PATCH (dep, type);
5785 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5786 SOURCE_FRONTEND_DEBUG
5787 (("Completing a random type dependency on a '%s' node",
5788 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5791 case JDEP_EXCEPTION:
5792 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5793 SOURCE_FRONTEND_DEBUG
5794 (("Completing '%s' 'throws' argument node",
5795 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5798 case JDEP_ANONYMOUS:
5799 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5810 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5814 resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5816 tree tname = TYPE_NAME (class_type);
5817 tree resolved_type = TREE_TYPE (class_type);
5819 tree resolved_type_decl;
5821 if (resolved_type != NULL_TREE)
5823 tree resolved_type_decl = TYPE_NAME (resolved_type);
5824 if (resolved_type_decl == NULL_TREE
5825 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5827 resolved_type_decl = build_decl (TYPE_DECL,
5828 TYPE_NAME (class_type),
5831 return resolved_type_decl;
5834 /* 1- Check to see if we have an array. If true, find what we really
5836 if ((array_dims = build_type_name_from_array_name (tname,
5837 &TYPE_NAME (class_type))))
5838 WFL_STRIP_BRACKET (cl, cl);
5840 /* 2- Resolve the bare type */
5841 if (!(resolved_type_decl = do_resolve_class (enclosing, NULL_TREE, class_type,
5844 resolved_type = TREE_TYPE (resolved_type_decl);
5846 /* 3- If we have an array, reconstruct the array down to its nesting */
5849 for (; array_dims; array_dims--)
5850 resolved_type = build_java_array_type (resolved_type, -1);
5851 resolved_type_decl = TYPE_NAME (resolved_type);
5853 TREE_TYPE (class_type) = resolved_type;
5854 return resolved_type_decl;
5857 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5858 are used to report error messages; CL must either be NULL_TREE or a
5859 WFL wrapping a class. Do not try to replace TYPE_NAME (class_type)
5860 by a variable, since it is changed by find_in_imports{_on_demand}
5861 and (but it doesn't really matter) qualify_and_find. */
5864 do_resolve_class (tree enclosing, tree import_type, tree class_type, tree decl,
5867 tree new_class_decl = NULL_TREE, super = NULL_TREE;
5868 tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5870 htab_t circularity_hash;
5872 if (QUALIFIED_P (TYPE_NAME (class_type)))
5874 /* If the type name is of the form `Q . Id', then Q is either a
5875 package name or a class name. First we try to find Q as a
5876 class and then treat Id as a member type. If we can't find Q
5877 as a class then we fall through. */
5878 tree q, left, left_type, right;
5879 if (split_qualified_name (&left, &right, TYPE_NAME (class_type)) == 0)
5881 BUILD_PTR_FROM_NAME (left_type, left);
5882 q = do_resolve_class (enclosing, import_type, left_type, decl, cl);
5886 saved_enclosing_type = TREE_TYPE (q);
5887 BUILD_PTR_FROM_NAME (class_type, right);
5894 /* This hash table is used to register the classes we're going
5895 through when searching the current class as an inner class, in
5896 order to detect circular references. Remember to free it before
5897 returning the section 0- of this function. */
5898 circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
5901 /* 0- Search in the current class as an inner class.
5902 Maybe some code here should be added to load the class or
5903 something, at least if the class isn't an inner class and ended
5904 being loaded from class file. FIXME. */
5907 new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5908 &super, class_type);
5912 /* If we haven't found anything because SUPER reached Object and
5913 ENCLOSING happens to be an innerclass, try the enclosing context. */
5914 if ((!super || super == object_type_node) &&
5915 enclosing && INNER_CLASS_DECL_P (enclosing))
5916 enclosing = DECL_CONTEXT (enclosing);
5918 enclosing = NULL_TREE;
5921 htab_delete (circularity_hash);
5924 return new_class_decl;
5927 /* 1- Check for the type in single imports. Look at enclosing classes and,
5928 if we're laying out a superclass, at the import list for the subclass.
5929 This will change TYPE_NAME() if something relevant is found. */
5930 if (import_type && TYPE_IMPORT_LIST (import_type))
5931 find_in_imports (import_type, class_type);
5932 find_in_imports (saved_enclosing_type, class_type);
5934 /* 2- And check for the type in the current compilation unit */
5935 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5937 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
5938 load_class (TYPE_NAME (class_type), 0);
5939 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5942 /* 3- Search according to the current package definition */
5943 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5945 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5946 TYPE_NAME (class_type))))
5947 return new_class_decl;
5950 /* 4- Check the import on demands. Don't allow bar.baz to be
5951 imported from foo.* */
5952 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5955 && TYPE_IMPORT_DEMAND_LIST (import_type)
5956 && find_in_imports_on_demand (import_type, class_type))
5958 if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5962 /* If found in find_in_imports_on_demand, the type has already been
5964 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5965 return new_class_decl;
5967 /* 5- Check another compilation unit that bears the name of type */
5968 load_class (TYPE_NAME (class_type), 0);
5971 cl = lookup_cl (decl);
5973 /* If we don't have a value for CL, then we're being called recursively.
5974 We can't check package access just yet, but it will be taken care of
5978 if (check_pkg_class_access (TYPE_NAME (class_type), cl, true, NULL_TREE))
5982 /* 6- Last call for a resolution */
5983 decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5985 /* The final lookup might have registered a.b.c into a.b$c If we
5986 failed at the first lookup, progressively change the name if
5987 applicable and use the matching DECL instead. */
5988 if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
5991 tree name = TYPE_NAME (class_type);
5992 char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
5994 strcpy (namebuffer, IDENTIFIER_POINTER (name));
5998 /* Reach the last '.', and if applicable, replace it by a `$' and
5999 see if this exists as a type. */
6000 if ((separator = strrchr (namebuffer, '.')))
6003 name = get_identifier (namebuffer);
6004 decl_result = IDENTIFIER_CLASS_VALUE (name);
6006 } while (!decl_result && separator);
6012 qualify_and_find (tree class_type, tree package, tree name)
6014 tree new_qualified = merge_qualified_name (package, name);
6015 tree new_class_decl;
6017 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
6018 load_class (new_qualified, 0);
6019 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
6021 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
6022 load_class (TREE_TYPE (new_class_decl), 0);
6023 TYPE_NAME (class_type) = new_qualified;
6024 return IDENTIFIER_CLASS_VALUE (new_qualified);
6029 /* Resolve NAME and lay it out (if not done and if not the current
6030 parsed class). Return a decl node. This function is meant to be
6031 called when type resolution is necessary during the walk pass. */
6034 resolve_and_layout (tree something, tree cl)
6036 tree decl, decl_type;
6038 /* Don't do that on the current class */
6039 if (something == current_class)
6040 return TYPE_NAME (current_class);
6042 /* Don't do anything for void and other primitive types */
6043 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6046 /* Pointer types can be reall pointer types or fake pointers. When
6047 finding a real pointer, recheck for primitive types */
6048 if (TREE_CODE (something) == POINTER_TYPE)
6050 if (TREE_TYPE (something))
6052 something = TREE_TYPE (something);
6053 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6057 something = TYPE_NAME (something);
6060 /* Don't do anything for arrays of primitive types */
6061 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
6062 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
6065 /* Something might be a WFL */
6066 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
6067 something = EXPR_WFL_NODE (something);
6069 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
6070 TYPE_DECL or a real TYPE */
6071 else if (TREE_CODE (something) != IDENTIFIER_NODE)
6072 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
6073 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
6075 if (!(decl = resolve_no_layout (something, cl)))
6078 /* Resolve and layout if necessary */
6079 decl_type = TREE_TYPE (decl);
6080 layout_class_methods (decl_type);
6082 if (CLASS_FROM_SOURCE_P (decl_type))
6083 java_check_methods (decl);
6084 /* Layout the type if necessary */
6085 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6086 safe_layout_class (decl_type);
6091 /* Resolve a class, returns its decl but doesn't perform any
6092 layout. The current parsing context is saved and restored */
6095 resolve_no_layout (tree name, tree cl)
6098 BUILD_PTR_FROM_NAME (ptr, name);
6099 java_parser_context_save_global ();
6100 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6101 java_parser_context_restore_global ();
6106 /* Called when reporting errors. Skip the '[]'s in a complex array
6107 type description that failed to be resolved. purify_type_name can't
6108 use an identifier tree. */
6111 purify_type_name (const char *name)
6113 int len = strlen (name);
6116 STRING_STRIP_BRACKETS (name, len, bracket_found);
6119 char *stripped_name = xmemdup (name, len, len+1);
6120 stripped_name [len] = '\0';
6121 return stripped_name;
6126 /* The type CURRENT refers to can't be found. We print error messages. */
6129 complete_class_report_errors (jdep *dep)
6133 if (!JDEP_WFL (dep))
6136 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6137 switch (JDEP_KIND (dep))
6141 (JDEP_WFL (dep), "Superclass %qs of class %qs not found",
6142 purify_type_name (name),
6143 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6147 (JDEP_WFL (dep), "Type %qs not found in declaration of field %qs",
6148 purify_type_name (name),
6149 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6151 case JDEP_METHOD: /* Covers arguments */
6153 (JDEP_WFL (dep), "Type %qs not found in the declaration of the argument %qs of method %qs",
6154 purify_type_name (name),
6155 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6156 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6158 case JDEP_METHOD_RETURN: /* Covers return type */
6160 (JDEP_WFL (dep), "Type %qs not found in the declaration of the return type of method %qs",
6161 purify_type_name (name),
6162 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6164 case JDEP_INTERFACE:
6166 (JDEP_WFL (dep), "Superinterface %qs of %s %qs not found",
6167 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6168 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6169 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6173 (JDEP_WFL (dep), "Type %qs not found in the declaration of the local variable %qs",
6174 purify_type_name (IDENTIFIER_POINTER
6175 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6176 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6178 case JDEP_EXCEPTION: /* As specified by `throws' */
6180 (JDEP_WFL (dep), "Class %qs not found in %<throws%>",
6181 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6184 /* Fix for -Wall. Just break doing nothing. The error will be
6190 /* Return a static string containing the DECL prototype string. If
6191 DECL is a constructor, use the class name instead of the form
6195 get_printable_method_name (tree decl)
6197 const char *to_return;
6198 tree name = NULL_TREE;
6200 if (DECL_CONSTRUCTOR_P (decl))
6202 name = DECL_NAME (decl);
6203 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6206 to_return = lang_printable_name (decl, 2);
6207 if (DECL_CONSTRUCTOR_P (decl))
6208 DECL_NAME (decl) = name;
6213 /* Track method being redefined inside the same class. As a side
6214 effect, set DECL_NAME to an IDENTIFIER (prior entering this
6215 function it's a FWL, so we can track errors more accurately.) */
6218 check_method_redefinition (tree class, tree method)
6222 /* There's no need to verify <clinit> and finit$ and instinit$ */
6223 if (DECL_CLINIT_P (method)
6224 || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6227 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6228 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6230 if (redef == method)
6232 if (DECL_NAME (redef) == DECL_NAME (method)
6233 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6234 && !DECL_ARTIFICIAL (method))
6237 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration %qs",
6238 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6239 get_printable_method_name (redef));
6246 /* Return 1 if check went ok, 0 otherwise. */
6248 check_abstract_method_definitions (int do_interface, tree class_decl,
6251 tree class = TREE_TYPE (class_decl);
6252 tree method, end_type;
6255 end_type = (do_interface ? object_type_node : type);
6256 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6258 tree other_super, other_method, method_sig, method_name;
6260 int end_type_reached = 0;
6262 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6265 /* Now verify that somewhere in between TYPE and CLASS,
6266 abstract method METHOD gets a non abstract definition
6267 that is inherited by CLASS. */
6269 method_sig = build_java_signature (TREE_TYPE (method));
6270 method_name = DECL_NAME (method);
6271 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6272 method_name = EXPR_WFL_NODE (method_name);
6274 other_super = class;
6276 if (other_super == end_type)
6277 end_type_reached = 1;
6280 for (other_method = TYPE_METHODS (other_super); other_method;
6281 other_method = TREE_CHAIN (other_method))
6283 tree s = build_java_signature (TREE_TYPE (other_method));
6284 tree other_name = DECL_NAME (other_method);
6286 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6287 other_name = EXPR_WFL_NODE (other_name);
6288 if (!DECL_CLINIT_P (other_method)
6289 && !DECL_CONSTRUCTOR_P (other_method)
6290 && method_name == other_name
6292 && !METHOD_ABSTRACT (other_method))
6298 other_super = CLASSTYPE_SUPER (other_super);
6299 } while (!end_type_reached);
6301 /* Report that abstract METHOD didn't find an implementation
6302 that CLASS can use. */
6305 char *t = xstrdup (lang_printable_name
6306 (TREE_TYPE (TREE_TYPE (method)), 0));
6307 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6310 (lookup_cl (class_decl),
6311 "Class %qs doesn't define the abstract method %<%s %s%> from %s %<%s%>. This method must be defined or %s %qs must be declared abstract",
6312 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6313 t, lang_printable_name (method, 2),
6314 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6315 "interface" : "class"),
6316 IDENTIFIER_POINTER (ccn),
6317 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6318 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6324 if (ok && do_interface)
6326 /* Check for implemented interfaces. */
6331 ok && BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo);
6333 ok = check_abstract_method_definitions (1, class_decl,
6334 BINFO_TYPE (base_binfo));
6340 /* Check that CLASS_DECL somehow implements all inherited abstract
6344 java_check_abstract_method_definitions (tree class_decl)
6346 tree class = TREE_TYPE (class_decl);
6347 tree super, base_binfo;
6350 if (CLASS_ABSTRACT (class_decl))
6353 /* Check for inherited types */
6356 super = CLASSTYPE_SUPER (super);
6357 check_abstract_method_definitions (0, class_decl, super);
6358 } while (super != object_type_node);
6360 /* Check for implemented interfaces. */
6361 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6362 check_abstract_method_definitions (1, class_decl, BINFO_TYPE (base_binfo));
6365 /* Check all the types method DECL uses and return 1 if all of them
6366 are now complete, 0 otherwise. This is used to check whether its
6367 safe to build a method signature or not. */
6370 check_method_types_complete (tree decl)
6372 tree type = TREE_TYPE (decl);
6375 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6378 args = TYPE_ARG_TYPES (type);
6379 if (TREE_CODE (type) == METHOD_TYPE)
6380 args = TREE_CHAIN (args);
6381 for (; args != end_params_node; args = TREE_CHAIN (args))
6382 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6388 /* Visible interface to check methods contained in CLASS_DECL */
6391 java_check_methods (tree class_decl)
6393 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6396 if (CLASS_INTERFACE (class_decl))
6397 java_check_abstract_methods (class_decl);
6399 java_check_regular_methods (class_decl);
6401 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6404 /* Like not_accessible_p, but doesn't refer to the current class at
6407 hack_is_accessible_p (tree member, tree from_where)
6409 int flags = get_access_flags_from_decl (member);
6411 if (from_where == DECL_CONTEXT (member)
6412 || (flags & ACC_PUBLIC))
6415 if ((flags & ACC_PROTECTED))
6417 if (inherits_from_p (from_where, DECL_CONTEXT (member)))
6421 if ((flags & ACC_PRIVATE))
6424 /* Package private, or protected. */
6425 return in_same_package (TYPE_NAME (from_where),
6426 TYPE_NAME (DECL_CONTEXT (member)));
6429 /* Check all the methods of CLASS_DECL. Methods are first completed
6430 then checked according to regular method existence rules. If no
6431 constructor for CLASS_DECL were encountered, then build its
6434 java_check_regular_methods (tree class_decl)
6436 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6438 tree class = TREE_TYPE (class_decl);
6439 tree found = NULL_TREE;
6442 /* It is not necessary to check methods defined in java.lang.Object */
6443 if (class == object_type_node)
6446 if (!TYPE_NVIRTUALS (class))
6447 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6449 /* Should take interfaces into account. FIXME */
6450 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6453 tree method_wfl = DECL_FUNCTION_WFL (method);
6456 /* Check for redefinitions */
6457 if (check_method_redefinition (class, method))
6460 /* We verify things thrown by the method. They must inherit from
6461 java.lang.Throwable. */
6462 for (mthrows = DECL_FUNCTION_THROWS (method);
6463 mthrows; mthrows = TREE_CHAIN (mthrows))
6465 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6467 (TREE_PURPOSE (mthrows), "Class %qs in %<throws%> clause must be a subclass of class %<java.lang.Throwable%>",
6469 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6472 /* If we see one constructor a mark so we don't generate the
6473 default one. Also skip other verifications: constructors
6474 can't be inherited hence hidden or overridden. */
6475 if (DECL_CONSTRUCTOR_P (method))
6477 saw_constructor = 1;
6481 sig = build_java_argument_signature (TREE_TYPE (method));
6482 found = lookup_argument_method_generic (class, DECL_NAME (method), sig,
6483 SEARCH_SUPER | SEARCH_INTERFACE);
6485 /* Inner class can't declare static methods */
6486 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6488 char *t = xstrdup (lang_printable_name (class, 0));
6490 (method_wfl, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
6491 lang_printable_name (method, 2), t);
6495 /* Nothing overrides or it's a private method. */
6498 if (METHOD_PRIVATE (found))
6504 /* If `found' is declared in an interface, make sure the
6505 modifier matches. */
6506 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6507 && clinit_identifier_node != DECL_NAME (found)
6508 && !METHOD_PUBLIC (method))
6510 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6511 parse_error_context (method_wfl, "Class %qs must override %qs with a public method in order to implement interface %qs",
6512 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6513 lang_printable_name (method, 2),
6514 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6517 /* Can't override a method with the same name and different return
6519 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6522 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6525 "Method %qs was defined with return type %qs in class %qs",
6526 lang_printable_name (found, 2), t,
6528 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6532 aflags = get_access_flags_from_decl (found);
6534 /* Can't override final. Can't override static. */
6535 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6537 /* Static *can* override static */
6538 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6542 "%s methods can't be overridden. Method %qs is %s in class %qs",
6543 (METHOD_FINAL (found) ? "Final" : "Static"),
6544 lang_printable_name (found, 2),
6545 (METHOD_FINAL (found) ? "final" : "static"),
6547 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6551 /* Static method can't override instance method. */
6552 if (METHOD_STATIC (method))
6556 "Instance methods can't be overridden by a static method. Method %qs is an instance method in class %qs",
6557 lang_printable_name (found, 2),
6559 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6563 /* - Overriding/hiding public must be public
6564 - Overriding/hiding protected must be protected or public
6565 - If the overridden or hidden method has default (package)
6566 access, then the overriding or hiding method must not be
6567 private; otherwise, a compile-time error occurs. If
6568 `found' belongs to an interface, things have been already
6570 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6571 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6572 || (METHOD_PROTECTED (found)
6573 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6574 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6575 && METHOD_PRIVATE (method))))
6579 "Methods can't be overridden to be more private. Method %qs is not %s in class %qs", lang_printable_name (method, 2),
6580 (METHOD_PUBLIC (method) ? "public" :
6581 (METHOD_PRIVATE (method) ? "private" : "protected")),
6582 IDENTIFIER_POINTER (DECL_NAME
6583 (TYPE_NAME (DECL_CONTEXT (found)))));
6587 /* Check this method against all the other implementations it
6588 overrides. Here we only check the class hierarchy; the rest
6589 of the checking is done later. If this method is just a
6590 Miranda method, we can skip the check. */
6591 if (! METHOD_INVISIBLE (method))
6592 check_concrete_throws_clauses (class, method, DECL_NAME (method), sig);
6595 /* The above throws clause check only looked at superclasses. Now
6596 we must also make sure that all methods declared in interfaces
6597 have compatible throws clauses. FIXME: there are more efficient
6598 ways to organize this checking; we should implement one. */
6599 check_interface_throws_clauses (class, class);
6601 if (!TYPE_NVIRTUALS (class))
6602 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6604 /* Search for inherited abstract method not yet implemented in this
6606 java_check_abstract_method_definitions (class_decl);
6608 if (!saw_constructor)
6612 /* Check to make sure that all the methods in all the interfaces
6613 implemented by CLASS_DECL are compatible with the concrete
6614 implementations available in CHECK_CLASS_DECL. */
6616 check_interface_throws_clauses (tree check_class_decl, tree class_decl)
6618 for (; class_decl != NULL_TREE; class_decl = CLASSTYPE_SUPER (class_decl))
6622 if (! CLASS_LOADED_P (class_decl))
6624 if (CLASS_FROM_SOURCE_P (class_decl))
6625 safe_layout_class (class_decl);
6627 load_class (class_decl, 1);
6630 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (class_decl)) - 1; i > 0; --i)
6633 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (class_decl), i));
6636 for (iface_method = TYPE_METHODS (interface);
6637 iface_method != NULL_TREE;
6638 iface_method = TREE_CHAIN (iface_method))
6642 /* First look for a concrete method implemented or
6643 inherited by this class. No need to search
6644 interfaces here, since we're already looking through
6646 sig = build_java_argument_signature (TREE_TYPE (iface_method));
6648 = lookup_argument_method_generic (check_class_decl,
6649 DECL_NAME (iface_method),
6650 sig, SEARCH_VISIBLE);
6651 /* If we don't find an implementation, that is ok. Any
6652 potential errors from that are diagnosed elsewhere.
6653 Also, multiple inheritance with conflicting throws
6654 clauses is fine in the absence of a concrete
6656 if (method != NULL_TREE && !METHOD_ABSTRACT (method)
6657 && !METHOD_INVISIBLE (iface_method))
6659 tree method_wfl = DECL_FUNCTION_WFL (method);
6660 check_throws_clauses (method, method_wfl, iface_method);
6664 /* Now check superinterfaces. */
6665 check_interface_throws_clauses (check_class_decl, interface);
6670 /* Check throws clauses of a method against the clauses of all the
6671 methods it overrides. We do this by searching up the class
6672 hierarchy, examining all matching accessible methods. */
6674 check_concrete_throws_clauses (tree class, tree self_method,
6675 tree name, tree signature)
6677 tree method = lookup_argument_method_generic (class, name, signature,
6678 SEARCH_SUPER | SEARCH_VISIBLE);
6679 while (method != NULL_TREE)
6681 if (! METHOD_INVISIBLE (method) && hack_is_accessible_p (method, class))
6682 check_throws_clauses (self_method, DECL_FUNCTION_WFL (self_method),
6685 method = lookup_argument_method_generic (DECL_CONTEXT (method),
6687 SEARCH_SUPER | SEARCH_VISIBLE);
6691 /* Generate an error if the `throws' clause of METHOD (if any) is
6692 incompatible with the `throws' clause of FOUND (if any). */
6694 check_throws_clauses (tree method, tree method_wfl, tree found)
6698 for (mthrows = DECL_FUNCTION_THROWS (method);
6699 mthrows; mthrows = TREE_CHAIN (mthrows))
6703 /* We don't verify unchecked expressions */
6704 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6706 /* Checked expression must be compatible */
6707 for (fthrows = DECL_FUNCTION_THROWS (found);
6708 fthrows; fthrows = TREE_CHAIN (fthrows))
6710 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6716 (method_wfl, "Invalid checked exception class %qs in %<throws%> clause. The exception must be a subclass of an exception thrown by %qs from class %qs",
6717 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6718 lang_printable_name (found, 2),
6720 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6725 /* Check abstract method of interface INTERFACE */
6727 java_check_abstract_methods (tree interface_decl)
6731 tree interface = TREE_TYPE (interface_decl);
6734 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6736 /* 2- Check for double definition inside the defining interface */
6737 if (check_method_redefinition (interface, method))
6740 /* 3- Overriding is OK as far as we preserve the return type. */
6741 found = lookup_java_interface_method2 (interface, method);
6745 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6747 (DECL_FUNCTION_WFL (found),
6748 "Method %qs was defined with return type %qs in class %qs",
6749 lang_printable_name (found, 2), t,
6751 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6757 /* 4- Inherited methods can't differ by their returned types */
6758 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (interface), i, base_binfo); i++)
6760 tree sub_interface_method, sub_interface;
6762 sub_interface = BINFO_TYPE (base_binfo);
6763 for (sub_interface_method = TYPE_METHODS (sub_interface);
6764 sub_interface_method;
6765 sub_interface_method = TREE_CHAIN (sub_interface_method))
6767 found = lookup_java_interface_method2 (interface,
6768 sub_interface_method);
6769 if (found && (found != sub_interface_method))
6772 (lookup_cl (sub_interface_method),
6773 "Interface %qs inherits method %qs from interface %qs. This method is redefined with a different return type in interface %qs",
6774 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6775 lang_printable_name (found, 2),
6777 (DECL_NAME (TYPE_NAME
6778 (DECL_CONTEXT (sub_interface_method)))),
6780 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6786 /* Lookup methods in interfaces using their name and partial
6787 signature. Return a matching method only if their types differ. */
6790 lookup_java_interface_method2 (tree class, tree method_decl)
6796 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6798 if ((BINFO_TYPE (base_binfo) != object_type_node)
6800 lookup_java_method2 (BINFO_TYPE (base_binfo), method_decl, 1)))
6803 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6805 to_return = lookup_java_interface_method2
6806 (BINFO_TYPE (base_binfo), method_decl);
6814 /* Lookup method using their name and partial signature. Return a
6815 matching method only if their types differ. */
6818 lookup_java_method2 (tree clas, tree method_decl, int do_interface)
6820 tree method, method_signature, method_name, method_type, name;
6822 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6823 name = DECL_NAME (method_decl);
6824 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6825 EXPR_WFL_NODE (name) : name);
6826 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6828 while (clas != NULL_TREE)
6830 for (method = TYPE_METHODS (clas);
6831 method != NULL_TREE; method = TREE_CHAIN (method))
6833 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6834 tree name = DECL_NAME (method);
6835 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6836 EXPR_WFL_NODE (name) : name) == method_name
6837 && method_sig == method_signature
6838 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6841 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6846 /* Return the line that matches DECL line number, and try its best to
6847 position the column number. Used during error reports.
6848 FUTURE/FIXME: return source_location instead of node. */
6850 static GTY(()) tree cl_v;
6852 lookup_cl (tree decl)
6854 #ifndef USE_MAPPED_LOCATION
6861 if (cl_v == NULL_TREE)
6863 cl_v = build_unknown_wfl (NULL_TREE);
6866 #ifdef USE_MAPPED_LOCATION
6867 SET_EXPR_LOCATION (cl_v, DECL_SOURCE_LOCATION (decl));
6869 EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6870 EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE (decl), -1);
6872 line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6873 EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6875 found = strstr ((const char *)line,
6876 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6878 EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6884 /* Look for a simple name in the single-type import list */
6887 find_name_in_single_imports (tree name)
6891 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6892 if (TREE_VALUE (node) == name)
6893 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6898 /* Process all single-type import. */
6901 process_imports (void)
6906 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6908 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6909 char *original_name;
6911 /* Don't load twice something already defined. */
6912 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6915 original_name = xmemdup (IDENTIFIER_POINTER (to_be_found),
6916 IDENTIFIER_LENGTH (to_be_found),
6917 IDENTIFIER_LENGTH (to_be_found) + 1);
6923 QUALIFIED_P (to_be_found) = 1;
6924 load_class (to_be_found, 0);
6926 check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true, NULL_TREE);
6928 /* We found it, we can bail out */
6929 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6931 check_deprecation (TREE_PURPOSE (import),
6932 IDENTIFIER_CLASS_VALUE (to_be_found));
6936 /* We haven't found it. Maybe we're trying to access an
6937 inner class. The only way for us to know is to try again
6938 after having dropped a qualifier. If we can't break it further,
6939 we have an error. */
6940 if (split_qualified_name (&left, NULL, to_be_found))
6945 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6947 parse_error_context (TREE_PURPOSE (import),
6948 "Class or interface %qs not found in import",
6953 free (original_name);
6960 /* Possibly find and mark a class imported by a single-type import
6964 find_in_imports (tree enclosing_type, tree class_type)
6967 if (enclosing_type && TYPE_IMPORT_LIST (enclosing_type))
6968 import = TYPE_IMPORT_LIST (enclosing_type);
6970 import = ctxp->import_list;
6974 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6976 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6977 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6980 import = TREE_CHAIN (import);
6985 note_possible_classname (const char *name, int len)
6988 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6990 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6994 node = ident_subst (name, len, "", '/', '.', "");
6995 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6996 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
7000 /* Read a import directory, gathering potential match for further type
7001 references. Indifferently reads a filesystem or a ZIP archive
7005 read_import_dir (tree wfl)
7007 tree package_id = EXPR_WFL_NODE (wfl);
7008 const char *package_name = IDENTIFIER_POINTER (package_id);
7009 int package_length = IDENTIFIER_LENGTH (package_id);
7011 JCF *saved_jcf = current_jcf;
7016 struct buffer filename[1];
7018 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
7020 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
7022 BUFFER_INIT (filename);
7023 buffer_grow (filename, package_length + 100);
7025 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
7027 const char *entry_name = jcf_path_name (entry);
7028 int entry_length = strlen (entry_name);
7029 if (jcf_path_is_zipfile (entry))
7032 buffer_grow (filename, entry_length);
7033 memcpy (filename->data, entry_name, entry_length - 1);
7034 filename->data[entry_length-1] = '\0';
7035 zipf = opendir_in_zip ((const char *) filename->data, jcf_path_is_system (entry));
7037 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
7040 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
7041 BUFFER_RESET (filename);
7042 for (k = 0; k < package_length; k++)
7044 char ch = package_name[k];
7045 *filename->ptr++ = ch == '.' ? '/' : ch;
7047 *filename->ptr++ = '/';
7049 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
7051 const char *current_entry = ZIPDIR_FILENAME (zipd);
7052 int current_entry_len = zipd->filename_length;
7054 if (current_entry_len >= BUFFER_LENGTH (filename)
7055 && strncmp ((const char *) filename->data, current_entry,
7056 BUFFER_LENGTH (filename)) != 0)
7058 found |= note_possible_classname (current_entry,
7065 BUFFER_RESET (filename);
7066 buffer_grow (filename, entry_length + package_length + 4);
7067 strcpy ((char *) filename->data, entry_name);
7068 filename->ptr = filename->data + entry_length;
7069 for (k = 0; k < package_length; k++)
7071 char ch = package_name[k];
7072 *filename->ptr++ = ch == '.' ? '/' : ch;
7074 *filename->ptr = '\0';
7076 dirp = opendir ((const char *) filename->data);
7079 *filename->ptr++ = '/';
7084 struct dirent *direntp = readdir (dirp);
7087 d_name = direntp->d_name;
7088 len = strlen (direntp->d_name);
7089 buffer_grow (filename, len+1);
7090 strcpy ((char *) filename->ptr, d_name);
7091 found |= note_possible_classname ((const char *) filename->data + entry_length,
7092 package_length+len+1);
7099 free (filename->data);
7101 /* Here we should have a unified way of retrieving an entry, to be
7105 static int first = 1;
7108 error ("Can't find default package %qs. Check the CLASSPATH environment variable and the access to the archives", package_name);
7113 parse_error_context (wfl, "Package %qs not found in import",
7115 current_jcf = saved_jcf;
7118 current_jcf = saved_jcf;
7121 /* Possibly find a type in the import on demands specified
7122 types. Returns 1 if an error occurred, 0 otherwise. Run through the
7123 entire list, to detected potential double definitions. */
7126 find_in_imports_on_demand (tree enclosing_type, tree class_type)
7128 tree class_type_name = TYPE_NAME (class_type);
7129 tree cl = NULL_TREE;
7130 int seen_once = -1; /* -1 when not set, 1 if seen once, >1 otherwise. */
7131 int to_return = -1; /* -1 when not set, 0 or 1 otherwise */
7135 if (enclosing_type && TYPE_IMPORT_DEMAND_LIST (enclosing_type))
7136 import = TYPE_IMPORT_DEMAND_LIST (enclosing_type);
7138 import = ctxp->import_demand_list;
7140 for (; import; import = TREE_CHAIN (import))
7142 location_t saved_location = input_location;
7144 const char *id_name;
7145 tree decl, type_name_copy;
7147 obstack_grow (&temporary_obstack,
7148 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
7149 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
7150 obstack_1grow (&temporary_obstack, '.');
7151 obstack_grow0 (&temporary_obstack,
7152 IDENTIFIER_POINTER (class_type_name),
7153 IDENTIFIER_LENGTH (class_type_name));
7154 id_name = obstack_finish (&temporary_obstack);
7156 if (! (node = maybe_get_identifier (id_name)))
7159 /* Setup input_line so that it refers to the line of the import (in
7160 case we parse a class file and encounter errors */
7161 #ifdef USE_MAPPED_LOCATION
7162 input_location = EXPR_LOCATION (TREE_PURPOSE (import));
7164 input_line = EXPR_WFL_LINENO (TREE_PURPOSE (import));
7167 type_name_copy = TYPE_NAME (class_type);
7168 TYPE_NAME (class_type) = node;
7169 QUALIFIED_P (node) = 1;
7170 decl = IDENTIFIER_CLASS_VALUE (node);
7172 /* If there is no DECL set for the class or if the class isn't
7173 loaded and not seen in source yet, then load */
7174 if (!decl || ! CLASS_LOADED_P (TREE_TYPE (decl)))
7176 load_class (node, 0);
7177 decl = IDENTIFIER_CLASS_VALUE (node);
7179 if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7180 access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7183 /* 6.6.1: Inner classes are subject to member access rules. */
7186 input_location = saved_location;
7188 /* If the loaded class is not accessible or couldn't be loaded,
7189 we restore the original TYPE_NAME and process the next
7191 if (access_check || !decl)
7193 TYPE_NAME (class_type) = type_name_copy;
7197 /* If the loaded class is accessible, we keep a tab on it to
7198 detect and report multiple inclusions. */
7199 if (IS_A_CLASSFILE_NAME (node))
7203 cl = TREE_PURPOSE (import);
7206 else if (seen_once >= 0)
7208 tree location = (cl ? cl : TREE_PURPOSE (import));
7209 tree package = (cl ? EXPR_WFL_NODE (cl) :
7210 EXPR_WFL_NODE (TREE_PURPOSE (import)));
7214 "Type %qs also potentially defined in package %qs",
7215 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7216 IDENTIFIER_POINTER (package));
7219 to_return = access_check;
7225 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7229 resolve_package (tree pkg, tree *next, tree *type_name)
7232 tree decl = NULL_TREE;
7233 *type_name = NULL_TREE;
7235 /* The trick is to determine when the package name stops and were
7236 the name of something contained in the package starts. Then we
7237 return a fully qualified name of what we want to get. */
7239 *next = EXPR_WFL_QUALIFICATION (pkg);
7241 /* Try to progressively construct a type name */
7242 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7243 for (current = EXPR_WFL_QUALIFICATION (pkg);
7244 current; current = TREE_CHAIN (current))
7246 /* If we don't have what we're expecting, exit now. TYPE_NAME
7247 will be null and the error caught later. */
7248 if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7251 merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7252 if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7254 /* resolve_package should be used in a loop, hence we
7255 point at this one to naturally process the next one at
7256 the next iteration. */
7265 /* Check accessibility of inner classes according to member access rules.
7266 DECL is the inner class, ENCLOSING_DECL is the class from which the
7267 access is being attempted. */
7270 check_inner_class_access (tree decl, tree enclosing_decl, tree cl)
7273 tree enclosing_decl_type;
7275 /* We don't issue an error message when CL is null. CL can be null
7276 as a result of processing a JDEP crafted by source_start_java_method
7277 for the purpose of patching its parm decl. But the error would
7278 have been already trapped when fixing the method's signature.
7279 DECL can also be NULL in case of earlier errors. */
7283 enclosing_decl_type = TREE_TYPE (enclosing_decl);
7285 if (CLASS_PRIVATE (decl))
7287 /* Access is permitted only within the body of the top-level
7288 class in which DECL is declared. */
7289 tree top_level = decl;
7290 while (DECL_CONTEXT (top_level))
7291 top_level = DECL_CONTEXT (top_level);
7292 while (DECL_CONTEXT (enclosing_decl))
7293 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7294 if (top_level == enclosing_decl)
7298 else if (CLASS_PROTECTED (decl))
7301 /* Access is permitted from within the same package... */
7302 if (in_same_package (decl, enclosing_decl))
7305 /* ... or from within the body of a subtype of the context in which
7306 DECL is declared. */
7307 decl_context = DECL_CONTEXT (decl);
7308 while (enclosing_decl)
7310 if (CLASS_INTERFACE (decl))
7312 if (interface_of_p (TREE_TYPE (decl_context),
7313 enclosing_decl_type))
7318 /* Eww. The order of the arguments is different!! */
7319 if (inherits_from_p (enclosing_decl_type,
7320 TREE_TYPE (decl_context)))
7323 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7325 access = "protected";
7327 else if (! CLASS_PUBLIC (decl))
7329 /* Access is permitted only from within the same package as DECL. */
7330 if (in_same_package (decl, enclosing_decl))
7332 access = "non-public";
7335 /* Class is public. */
7338 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7339 (CLASS_INTERFACE (decl) ? "interface" : "class"),
7340 lang_printable_name (decl, 2), access);
7343 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7344 foreign package, it must be PUBLIC. Return 0 if no access
7345 violations were found, 1 otherwise. If VERBOSE is true and an error
7346 was found, it is reported and accounted for. If CL is NULL then
7347 look it up with THIS_DECL. */
7350 check_pkg_class_access (tree class_name, tree cl, bool verbose, tree this_decl)
7354 if (!IDENTIFIER_CLASS_VALUE (class_name))
7357 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7360 if (!CLASS_PUBLIC (TYPE_NAME (type)))
7362 /* Access to a private class within the same package is
7365 split_qualified_name (&l, &r, class_name);
7366 if (!QUALIFIED_P (class_name) && !ctxp->package)
7367 /* Both in the empty package. */
7369 if (l == ctxp->package)
7370 /* Both in the same package. */
7375 (cl == NULL ? lookup_cl (this_decl): cl,
7376 "Can't access %s %qs. Only public classes and interfaces in other packages can be accessed",
7377 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7378 IDENTIFIER_POINTER (class_name));
7384 /* Local variable declaration. */
7387 declare_local_variables (int modifier, tree type, tree vlist)
7389 tree decl, current, saved_type;
7390 tree type_wfl = NULL_TREE;
7394 /* Push a new block if statements were seen between the last time we
7395 pushed a block and now. Keep a count of blocks to close */
7396 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7398 tree b = enter_block ();
7399 BLOCK_IS_IMPLICIT (b) = 1;
7405 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7406 if (1 << i & modifier)
7408 if (modifier == ACC_FINAL)
7413 (ctxp->modifier_ctx [i],
7414 "Only %<final%> is allowed as a local variables modifier");
7419 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7420 hold the TYPE value if a new incomplete has to be created (as
7421 opposed to being found already existing and reused). */
7422 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7424 /* If TYPE is fully resolved and we don't have a reference, make one */
7425 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7427 /* Go through all the declared variables */
7428 for (current = vlist, saved_type = type; current;
7429 current = TREE_CHAIN (current), type = saved_type)
7431 tree other, real_type;
7432 tree wfl = TREE_PURPOSE (current);
7433 tree name = EXPR_WFL_NODE (wfl);
7434 tree init = TREE_VALUE (current);
7436 /* Process NAME, as it may specify extra dimension(s) for it */
7437 type = build_array_from_name (type, type_wfl, name, &name);
7439 /* Variable redefinition check */
7440 if ((other = lookup_name_in_blocks (name)))
7442 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7443 DECL_SOURCE_LINE (other));
7447 /* Type adjustment. We may have just readjusted TYPE because
7448 the variable specified more dimensions. Make sure we have
7449 a reference if we can and don't have one already. */
7450 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7452 real_type = GET_REAL_TYPE (type);
7453 /* Never layout this decl. This will be done when its scope
7455 decl = build_decl (VAR_DECL, name, real_type);
7456 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7457 DECL_FINAL (decl) = final_p;
7458 BLOCK_CHAIN_DECL (decl);
7460 /* Don't try to use an INIT statement when an error was found */
7461 if (init && java_error_count)
7464 /* Remember it if this is an initialized-upon-declaration final
7466 if (init && final_p)
7468 DECL_LOCAL_FINAL_IUD (decl) = 1;
7471 /* Add the initialization function to the current function's code */
7474 /* Name might have been readjusted */
7475 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7476 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7477 java_method_add_stmt (current_function_decl,
7478 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7482 /* Setup dependency the type of the decl */
7486 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7487 dep = CLASSD_LAST (ctxp->classd_list);
7488 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7491 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7494 /* Called during parsing. Build decls from argument list. */
7497 source_start_java_method (tree fndecl)
7506 current_function_decl = fndecl;
7508 /* New scope for the function */
7510 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7511 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7513 tree type = TREE_VALUE (tem);
7514 tree name = TREE_PURPOSE (tem);
7516 /* If type is incomplete. Create an incomplete decl and ask for
7517 the decl to be patched later */
7518 if (INCOMPLETE_TYPE_P (type))
7521 tree real_type = GET_REAL_TYPE (type);
7522 parm_decl = build_decl (PARM_DECL, name, real_type);
7523 type = obtain_incomplete_type (type);
7524 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7525 jdep = CLASSD_LAST (ctxp->classd_list);
7526 JDEP_MISC (jdep) = name;
7527 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7530 parm_decl = build_decl (PARM_DECL, name, type);
7532 /* Remember if a local variable was declared final (via its
7533 TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7534 if (ARG_FINAL_P (tem))
7536 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7537 DECL_FINAL (parm_decl) = 1;
7540 BLOCK_CHAIN_DECL (parm_decl);
7542 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7543 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7545 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7546 DECL_MAX_LOCALS (current_function_decl) = i;
7549 /* Called during parsing. Creates an artificial method declaration. */
7552 create_artificial_method (tree class, int flags, tree type,
7553 tree name, tree args)
7556 location_t save_location = input_location;
7558 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (class));
7559 mdecl = make_node (FUNCTION_TYPE);
7560 TREE_TYPE (mdecl) = type;
7561 TYPE_ARG_TYPES (mdecl) = args;
7562 /* We used to compute the signature of MDECL here and then use
7563 add_method(), but that failed because our caller might modify
7564 the type of the returned method, which trashes the cache in
7565 get_type_from_signature(). */
7566 mdecl = add_method_1 (class, flags, name, mdecl);
7567 input_location = save_location;
7568 DECL_ARTIFICIAL (mdecl) = 1;
7572 /* Starts the body if an artificial method. */
7575 start_artificial_method_body (tree mdecl)
7577 #ifdef USE_MAPPED_LOCATION
7578 DECL_SOURCE_LOCATION (mdecl) = ctxp->file_start_location;
7579 DECL_FUNCTION_LAST_LINE (mdecl) = ctxp->file_start_location;
7581 DECL_SOURCE_LINE (mdecl) = 1;
7582 DECL_FUNCTION_LAST_LINE (mdecl) = 1;
7584 source_start_java_method (mdecl);
7589 end_artificial_method_body (tree mdecl)
7591 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7592 It has to be evaluated first. (if mdecl is current_function_decl,
7593 we have an undefined behavior if no temporary variable is used.) */
7594 tree b = exit_block ();
7595 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7599 /* Dump a tree of some kind. This is a convenience wrapper for the
7600 dump_* functions in tree-dump.c. */
7602 dump_java_tree (enum tree_dump_index phase, tree t)
7607 stream = dump_begin (phase, &flags);
7611 dump_node (t, flags, stream);
7612 dump_end (phase, stream);
7616 /* Terminate a function and expand its body. */
7619 source_end_java_method (void)
7621 tree fndecl = current_function_decl;
7626 java_parser_context_save_global ();
7627 #ifdef USE_MAPPED_LOCATION
7628 input_location = ctxp->last_ccb_indent1;
7630 input_line = ctxp->last_ccb_indent1;
7633 /* Turn function bodies with only a NOP expr null, so they don't get
7634 generated at all and we won't get warnings when using the -W
7636 if (IS_EMPTY_STMT (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))))
7637 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7639 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7640 && ! flag_emit_class_files)
7641 finish_method (fndecl);
7643 current_function_decl = NULL_TREE;
7644 java_parser_context_restore_global ();
7645 current_function_decl = NULL_TREE;
7648 /* Record EXPR in the current function block. Complements compound
7649 expression second operand if necessary. */
7652 java_method_add_stmt (tree fndecl, tree expr)
7654 if (!GET_CURRENT_BLOCK (fndecl))
7656 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7660 add_stmt_to_block (tree b, tree type, tree stmt)
7662 tree body = BLOCK_EXPR_BODY (b), c;
7664 if (java_error_count)
7667 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7670 BLOCK_EXPR_BODY (b) = c;
7671 TREE_SIDE_EFFECTS (c) = 1;
7675 /* Lays out the methods for the classes seen so far. */
7678 java_layout_seen_class_methods (void)
7680 tree previous_list = all_class_list;
7681 tree end = NULL_TREE;
7686 for (current = previous_list;
7687 current != end; current = TREE_CHAIN (current))
7689 tree decl = TREE_VALUE (current);
7690 tree cls = TREE_TYPE (decl);
7692 input_location = DECL_SOURCE_LOCATION (decl);
7694 if (! CLASS_LOADED_P (cls))
7695 load_class (cls, 0);
7697 layout_class_methods (cls);
7700 /* Note that new classes might have been added while laying out
7701 methods, changing the value of all_class_list. */
7703 if (previous_list != all_class_list)
7705 end = previous_list;
7706 previous_list = all_class_list;
7713 static GTY(()) tree stop_reordering;
7715 java_reorder_fields (void)
7719 for (current = gclass_list; current; current = TREE_CHAIN (current))
7721 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7723 if (current_class == stop_reordering)
7726 /* Reverse the fields, but leave the dummy field in front.
7727 Fields are already ordered for Object and Class */
7728 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7729 && current_class != class_type_node)
7731 /* If the dummy field is there, reverse the right fields and
7732 just layout the type for proper fields offset */
7733 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7735 tree fields = TYPE_FIELDS (current_class);
7736 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7737 TYPE_SIZE (current_class) = NULL_TREE;
7739 /* We don't have a dummy field, we need to layout the class,
7740 after having reversed the fields */
7743 TYPE_FIELDS (current_class) =
7744 nreverse (TYPE_FIELDS (current_class));
7745 TYPE_SIZE (current_class) = NULL_TREE;
7749 /* There are cases were gclass_list will be empty. */
7751 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7754 /* Layout the methods of all classes loaded in one way or another.
7755 Check methods of source parsed classes. Then reorder the
7756 fields and layout the classes or the type of all source parsed
7760 java_layout_classes (void)
7763 int save_error_count = java_error_count;
7765 /* Layout the methods of all classes seen so far */
7766 java_layout_seen_class_methods ();
7767 java_parse_abort_on_error ();
7768 all_class_list = NULL_TREE;
7770 /* Then check the methods of all parsed classes */
7771 for (current = gclass_list; current; current = TREE_CHAIN (current))
7772 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7773 java_check_methods (TREE_VALUE (current));
7774 java_parse_abort_on_error ();
7776 for (current = gclass_list; current; current = TREE_CHAIN (current))
7778 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7779 layout_class (current_class);
7781 /* Error reported by the caller */
7782 if (java_error_count)
7786 /* We might have reloaded classes durign the process of laying out
7787 classes for code generation. We must layout the methods of those
7788 late additions, as constructor checks might use them */
7789 java_layout_seen_class_methods ();
7790 java_parse_abort_on_error ();
7793 /* Expand methods in the current set of classes remembered for
7797 java_complete_expand_classes (void)
7801 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7802 if (!INNER_CLASS_DECL_P (current))
7803 java_complete_expand_class (current);
7806 /* Expand the methods found in OUTER, starting first by OUTER's inner
7810 java_complete_expand_class (tree outer)
7814 /* We need to go after all inner classes and start expanding them,
7815 starting with most nested ones. We have to do that because nested
7816 classes might add functions to outer classes */
7818 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7819 inner_list; inner_list = TREE_CHAIN (inner_list))
7820 java_complete_expand_class (TREE_PURPOSE (inner_list));
7822 java_complete_expand_methods (outer);
7825 /* Expand methods registered in CLASS_DECL. The general idea is that
7826 we expand regular methods first. This allows us get an estimate on
7827 how outer context local alias fields are really used so we can add
7828 to the constructor just enough code to initialize them properly (it
7829 also lets us generate finit$ correctly.) Then we expand the
7830 constructors and then <clinit>. */
7833 java_complete_expand_methods (tree class_decl)
7835 tree clinit, decl, first_decl;
7837 output_class = current_class = TREE_TYPE (class_decl);
7839 /* Pre-expand <clinit> to figure whether we really need it or
7840 not. If we do need it, we pre-expand the static fields so they're
7841 ready to be used somewhere else. <clinit> will be fully expanded
7842 after we processed the constructors. */
7843 first_decl = TYPE_METHODS (current_class);
7844 clinit = maybe_generate_pre_expand_clinit (current_class);
7846 /* Then generate finit$ (if we need to) because constructors will
7848 if (TYPE_FINIT_STMT_LIST (current_class))
7849 java_complete_expand_method (generate_finit (current_class));
7851 /* Then generate instinit$ (if we need to) because constructors will
7853 if (TYPE_II_STMT_LIST (current_class))
7854 java_complete_expand_method (generate_instinit (current_class));
7856 /* Now do the constructors */
7857 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7859 if (!DECL_CONSTRUCTOR_P (decl))
7861 java_complete_expand_method (decl);
7864 /* First, do the ordinary methods. */
7865 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7867 /* Ctors aren't part of this batch. */
7868 if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7871 /* Skip abstract or native methods -- but do handle native
7872 methods when generating JNI stubs. */
7873 if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7875 DECL_FUNCTION_BODY (decl) = NULL_TREE;
7879 if (METHOD_NATIVE (decl))
7882 current_function_decl = decl;
7883 body = build_jni_stub (decl);
7884 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7887 java_complete_expand_method (decl);
7890 /* If there is indeed a <clinit>, fully expand it now */
7893 /* Prevent the use of `this' inside <clinit> */
7894 ctxp->explicit_constructor_p = 1;
7895 java_complete_expand_method (clinit);
7896 ctxp->explicit_constructor_p = 0;
7899 /* We might have generated a class$ that we now want to expand */
7900 if (TYPE_DOT_CLASS (current_class))
7901 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7903 /* Now verify constructor circularity (stop after the first one we
7905 if (!CLASS_INTERFACE (class_decl))
7906 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7907 if (DECL_CONSTRUCTOR_P (decl)
7908 && verify_constructor_circularity (decl, decl))
7912 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7913 safely used in some other methods/constructors. */
7916 maybe_generate_pre_expand_clinit (tree class_type)
7918 tree current, mdecl;
7920 if (!TYPE_CLINIT_STMT_LIST (class_type))
7923 /* Go through all static fields and pre expand them */
7924 for (current = TYPE_FIELDS (class_type); current;
7925 current = TREE_CHAIN (current))
7926 if (FIELD_STATIC (current))
7927 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7929 /* Then build the <clinit> method */
7930 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7931 clinit_identifier_node, end_params_node);
7932 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7934 start_artificial_method_body (mdecl);
7936 /* We process the list of assignment we produced as the result of
7937 the declaration of initialized static field and add them as
7938 statement to the <clinit> method. */
7939 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7940 current = TREE_CHAIN (current))
7942 tree stmt = current;
7943 /* We build the assignment expression that will initialize the
7944 field to its value. There are strict rules on static
7945 initializers (8.5). FIXME */
7946 if (TREE_CODE (stmt) != BLOCK && !IS_EMPTY_STMT (stmt))
7947 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7948 java_method_add_stmt (mdecl, stmt);
7951 end_artificial_method_body (mdecl);
7953 /* Now we want to place <clinit> as the last method (because we need
7954 it at least for interface so that it doesn't interfere with the
7955 dispatch table based lookup. */
7956 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7958 current = TREE_CHAIN (TYPE_METHODS (class_type));
7959 TYPE_METHODS (class_type) = current;
7961 while (TREE_CHAIN (current))
7962 current = TREE_CHAIN (current);
7964 TREE_CHAIN (current) = mdecl;
7965 TREE_CHAIN (mdecl) = NULL_TREE;
7971 /* Analyzes a method body and look for something that isn't a
7972 MODIFY_EXPR with a constant value. */
7975 analyze_clinit_body (tree this_class, tree bbody)
7978 switch (TREE_CODE (bbody))
7981 bbody = BLOCK_EXPR_BODY (bbody);
7984 case EXPR_WITH_FILE_LOCATION:
7985 bbody = EXPR_WFL_NODE (bbody);
7989 if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
7991 bbody = TREE_OPERAND (bbody, 1);
7995 /* If we're generating to class file and we're dealing with an
7996 array initialization, we return 1 to keep <clinit> */
7997 if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
7998 && flag_emit_class_files)
8001 /* There are a few cases where we're required to keep
8003 - If this is an assignment whose operand is not constant,
8004 - If this is an assignment to a non-initialized field,
8005 - If this field is not a member of the current class.
8007 return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
8008 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
8009 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
8018 /* See whether we could get rid of <clinit>. Criteria are: all static
8019 final fields have constant initial values and the body of <clinit>
8020 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
8023 maybe_yank_clinit (tree mdecl)
8028 if (!DECL_CLINIT_P (mdecl))
8031 /* If the body isn't empty, then we keep <clinit>. Note that if
8032 we're emitting classfiles, this isn't enough not to rule it
8034 fbody = DECL_FUNCTION_BODY (mdecl);
8035 bbody = BLOCK_EXPR_BODY (fbody);
8036 if (bbody && bbody != error_mark_node)
8037 bbody = BLOCK_EXPR_BODY (bbody);
8040 if (bbody && ! flag_emit_class_files && !IS_EMPTY_STMT (bbody))
8043 type = DECL_CONTEXT (mdecl);
8044 current = TYPE_FIELDS (type);
8046 for (current = (current ? TREE_CHAIN (current) : current);
8047 current; current = TREE_CHAIN (current))
8051 /* We're not interested in non-static fields. */
8052 if (!FIELD_STATIC (current))
8055 /* Nor in fields without initializers. */
8056 f_init = DECL_INITIAL (current);
8057 if (f_init == NULL_TREE)
8060 /* Anything that isn't String or a basic type is ruled out -- or
8061 if we know how to deal with it (when doing things natively) we
8062 should generated an empty <clinit> so that SUID are computed
8064 if (! JSTRING_TYPE_P (TREE_TYPE (current))
8065 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
8068 if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
8072 /* Now we analyze the method body and look for something that
8073 isn't a MODIFY_EXPR */
8074 if (bbody && !IS_EMPTY_STMT (bbody) && analyze_clinit_body (type, bbody))
8077 /* Get rid of <clinit> in the class' list of methods */
8078 if (TYPE_METHODS (type) == mdecl)
8079 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
8081 for (current = TYPE_METHODS (type); current;
8082 current = TREE_CHAIN (current))
8083 if (TREE_CHAIN (current) == mdecl)
8085 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
8092 /* Install the argument from MDECL. Suitable to completion and
8093 expansion of mdecl's body. */
8096 start_complete_expand_method (tree mdecl)
8100 pushlevel (1); /* Prepare for a parameter push */
8101 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
8102 DECL_ARGUMENTS (mdecl) = tem;
8104 for (; tem; tem = TREE_CHAIN (tem))
8106 /* TREE_CHAIN (tem) will change after pushdecl. */
8107 tree next = TREE_CHAIN (tem);
8108 tree type = TREE_TYPE (tem);
8109 if (targetm.calls.promote_prototypes (type)
8110 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8111 && INTEGRAL_TYPE_P (type))
8112 type = integer_type_node;
8113 DECL_ARG_TYPE (tem) = type;
8114 layout_decl (tem, 0);
8116 /* Re-install the next so that the list is kept and the loop
8118 TREE_CHAIN (tem) = next;
8120 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8121 input_location = DECL_SOURCE_LOCATION (mdecl);
8122 build_result_decl (mdecl);
8126 /* Complete and expand a method. */
8129 java_complete_expand_method (tree mdecl)
8131 tree fbody, block_body, exception_copy;
8133 current_function_decl = mdecl;
8134 /* Fix constructors before expanding them */
8135 if (DECL_CONSTRUCTOR_P (mdecl))
8136 fix_constructors (mdecl);
8138 /* Expand functions that have a body */
8139 if (!DECL_FUNCTION_BODY (mdecl))
8142 fbody = DECL_FUNCTION_BODY (mdecl);
8143 block_body = BLOCK_EXPR_BODY (fbody);
8144 exception_copy = NULL_TREE;
8146 current_function_decl = mdecl;
8149 fprintf (stderr, " [%s.",
8150 lang_printable_name (DECL_CONTEXT (mdecl), 0));
8151 announce_function (mdecl);
8153 fprintf (stderr, "]");
8155 /* Prepare the function for tree completion */
8156 start_complete_expand_method (mdecl);
8158 /* Install the current this */
8159 current_this = (!METHOD_STATIC (mdecl) ?
8160 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8162 /* Purge the `throws' list of unchecked exceptions (we save a copy
8163 of the list and re-install it later.) */
8164 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8165 purge_unchecked_exceptions (mdecl);
8167 /* Install exceptions thrown with `throws' */
8168 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8170 if (block_body != NULL_TREE)
8172 block_body = java_complete_tree (block_body);
8174 /* Before we check initialization, attached all class initialization
8175 variable to the block_body */
8176 htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8177 attach_init_test_initialization_flags, block_body);
8179 if (! METHOD_NATIVE (mdecl))
8181 check_for_initialization (block_body, mdecl);
8183 /* Go through all the flags marking the initialization of
8184 static variables and see whether they're definitively
8185 assigned, in which case the type is remembered as
8186 definitively initialized in MDECL. */
8187 if (STATIC_CLASS_INIT_OPT_P ())
8189 /* Always register the context as properly initialized in
8190 MDECL. This used with caution helps removing extra
8191 initialization of self. */
8192 if (METHOD_STATIC (mdecl))
8195 (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8196 DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8200 ctxp->explicit_constructor_p = 0;
8203 BLOCK_EXPR_BODY (fbody) = block_body;
8205 /* If we saw a return but couldn't evaluate it properly, we'll have
8206 an error_mark_node here. */
8207 if (block_body != error_mark_node
8208 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8209 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE)
8210 missing_return_error (current_function_decl);
8212 /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8213 maybe_yank_clinit (mdecl);
8215 /* Pop the current level, with special measures if we found errors. */
8216 if (java_error_count)
8217 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8220 /* Pop the exceptions and sanity check */
8222 if (currently_caught_type_list)
8225 /* Restore the copy of the list of exceptions. */
8226 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8229 /* For with each class for which there's code to generate. */
8232 java_expand_method_bodies (tree class)
8235 for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8239 if (! DECL_FUNCTION_BODY (decl))
8242 current_function_decl = decl;
8244 block = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8246 /* Save the function body for gimplify and inlining. */
8247 DECL_SAVED_TREE (decl) = block;
8249 /* It's time to assign the variable flagging static class
8250 initialization based on which classes invoked static methods
8251 are definitely initializing. This should be flagged. */
8252 if (STATIC_CLASS_INIT_OPT_P ())
8254 tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8255 for (; list != NULL_TREE; list = TREE_CHAIN (list))
8257 /* Executed for each statement calling a static function.
8258 LIST is a TREE_LIST whose PURPOSE is the called function
8259 and VALUE is a compound whose second operand can be patched
8260 with static class initialization flag assignments. */
8262 tree called_method = TREE_PURPOSE (list);
8263 tree compound = TREE_VALUE (list);
8264 tree assignment_compound_list
8265 = build_tree_list (called_method, NULL);
8267 /* For each class definitely initialized in
8268 CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8269 assignment to the class initialization flag. */
8270 htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8271 emit_test_initialization,
8272 assignment_compound_list);
8274 if (TREE_VALUE (assignment_compound_list))
8275 TREE_OPERAND (compound, 1)
8276 = TREE_VALUE (assignment_compound_list);
8280 /* Expand the function body. */
8281 source_end_java_method ();
8287 /* This section of the code deals with accessing enclosing context
8288 fields either directly by using the relevant access to this$<n> or
8289 by invoking an access method crafted for that purpose. */
8291 /* Build the necessary access across nested class boundaries.
8292 This routine could be optimized to cache previous result
8293 (decl, current_class and returned access). When an access method
8294 needs to be generated, it always takes the form of a read. It might
8295 be later turned into a write by calling nested_field_access_fix. */
8298 build_nested_field_access (tree id, tree decl)
8300 tree access = NULL_TREE;
8301 tree ctx = NULL_TREE;
8302 tree decl_ctx = DECL_CONTEXT (decl);
8303 bool is_static = FIELD_STATIC (decl);
8305 if (DECL_CONTEXT (TYPE_NAME (current_class)))
8306 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8308 /* For non-static fields, if the immediate enclosing context of the
8309 current class is the field decl's class or inherits from it,
8310 build the access as `this$<n>.<field>'. Note that we will break
8311 the `private' barrier if we're not emitting bytecodes. */
8314 && (ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8315 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files))
8317 tree thisn = build_current_thisn (current_class);
8318 access = make_qualified_primary (build_wfl_node (thisn),
8319 id, EXPR_WFL_LINECOL (id));
8321 /* Otherwise, generate and use accessor methods for the field as
8325 int lc = EXPR_WFL_LINECOL (id);
8327 /* Now we chain the required number of calls to the access$0 to
8328 get a hold to the enclosing instance we need for a non-static
8329 field, and then we build the field access. */
8331 access = build_access_to_thisn (current_class, decl_ctx, lc);
8333 /* If the field is private and we're generating bytecode, then
8334 we generate an access method. */
8335 if (FIELD_PRIVATE (decl) && flag_emit_class_files)
8337 tree name = build_nested_field_access_methods (decl);
8338 access = build_nested_field_access_expr (lc, decl_ctx,
8339 name, access, NULL_TREE);
8341 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'
8342 for non-static fields.
8343 Once again we break the `private' access rule from a foreign
8347 tree class_name = DECL_NAME (TYPE_NAME (decl_ctx));
8349 = make_qualified_primary (build_wfl_node (class_name), id, lc);
8352 access = make_qualified_primary (access, id, lc);
8355 return resolve_expression_name (access, NULL);
8358 /* Return a nonzero value if DECL describes a field access across nested
8359 class boundaries. That is, DECL is in a class that either encloses,
8360 is enclosed by or shares a common enclosing class with, the class
8364 nested_field_access_p (tree type, tree decl)
8366 bool is_static = false;
8367 tree decl_type = DECL_CONTEXT (decl);
8368 tree type_root, decl_type_root;
8370 if (decl_type == type
8371 || (TREE_CODE (decl) != FIELD_DECL && TREE_CODE (decl) != VAR_DECL))
8374 if (!INNER_CLASS_TYPE_P (type)
8375 && !(TREE_CODE (decl_type) == RECORD_TYPE
8376 && INNER_CLASS_TYPE_P (decl_type)))
8379 is_static = FIELD_STATIC (decl);
8381 /* If TYPE extends the declaration context of the non-static
8382 field we're trying to access, then this isn't a nested field
8383 access we need to worry about. */
8384 if (!is_static && inherits_from_p (type, decl_type))
8387 for (type_root = type;
8388 DECL_CONTEXT (TYPE_NAME (type_root));
8389 type_root = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type_root))))
8391 if (type_root == decl_type)
8395 if (TREE_CODE (decl_type) == RECORD_TYPE
8396 && INNER_CLASS_TYPE_P (decl_type))
8398 for (decl_type_root = decl_type;
8399 DECL_CONTEXT (TYPE_NAME (decl_type_root));
8401 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (decl_type_root))))
8403 if (decl_type_root == type)
8408 decl_type_root = decl_type;
8410 if (type_root == decl_type_root)
8413 /* Before we give up, see whether it is a non-static field
8414 inherited from the enclosing context we are considering. */
8415 if (!DECL_CONTEXT (TYPE_NAME (type_root))
8417 && inherits_from_p (type_root, decl_type))
8423 /* Return a nonzero value if NODE represents a cross-nested-class
8424 access that has already been expanded. As a side effect, it returns
8425 the name of the field being accessed and the argument passed to the
8426 access function, suitable for a regeneration of the access method
8427 call if necessary. */
8430 nested_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8435 if (TREE_CODE (node) != CALL_EXPR)
8438 /* Well, GCJ generates slightly different tree nodes when compiling
8439 to native or bytecodes. It's the case for function calls. */
8441 if (flag_emit_class_files
8442 && TREE_CODE (node) == CALL_EXPR
8443 && NESTED_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8445 else if (!flag_emit_class_files)
8447 node = TREE_OPERAND (node, 0);
8449 if (node && TREE_OPERAND (node, 0)
8450 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8452 node = TREE_OPERAND (node, 0);
8453 if (TREE_OPERAND (node, 0)
8454 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8455 && (NESTED_FIELD_ACCESS_IDENTIFIER_P
8456 (DECL_NAME (TREE_OPERAND (node, 0)))))
8461 if (identified && name && arg_type && arg)
8463 tree argument = TREE_OPERAND (node, 1);
8464 *name = DECL_NAME (TREE_OPERAND (node, 0));
8466 /* The accessors for static fields do not take in a this$<n> argument,
8467 so we take the class name from the accessor's context instead. */
8470 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8471 *arg = TREE_VALUE (argument);
8475 *arg_type = DECL_CONTEXT (TREE_OPERAND (node, 0));
8482 /* Detect in NODE cross-nested-class field read access and
8483 transform it into a write with RHS as an argument. This function
8484 is called from the java_complete_lhs when an assignment to a LHS can
8488 nested_field_access_fix (tree wfl, tree node, tree rhs)
8490 tree name, arg_type, arg;
8492 if (nested_field_expanded_access_p (node, &name, &arg_type, &arg))
8494 node = build_nested_field_access_expr (EXPR_WFL_LINECOL (wfl),
8495 arg_type, name, arg, rhs);
8496 return java_complete_tree (node);
8501 /* Construct the expression that calls an access method:
8502 <type>.access$<n>(<arg1> [, <arg2>]);
8504 ARG2 can be NULL and will be omitted in that case. It will denote a
8508 build_nested_field_access_expr (int lc, tree type, tree access_method_name,
8509 tree arg1, tree arg2)
8511 tree args, cn, access;
8514 args = build_tree_list (NULL_TREE, arg1);
8521 args = tree_cons (NULL_TREE, arg2, args);
8523 args = build_tree_list (NULL_TREE, arg2);
8527 = build_method_invocation (build_wfl_node (access_method_name), args);
8528 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8530 return make_qualified_primary (cn, access, lc);
8533 /* Build the name of a synthetic accessor used to access class members
8534 across nested class boundaries. */
8537 build_new_access_id (void)
8539 static int access_n_counter = 1;
8542 sprintf (buffer, "access$%d", access_n_counter++);
8543 return get_identifier (buffer);
8546 /* Create the static access functions for the cross-nested-class field DECL.
8548 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8552 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8553 TREE_TYPE (<field>) value$) {
8554 return inst$.field = value$;
8556 For static fields, these methods are generated without the instance
8558 We should have a usage flag on the DECL so we can lazily turn the ones
8559 we're using for code generation. FIXME.
8563 build_nested_field_access_methods (tree decl)
8565 tree id, args, stmt, mdecl, class_name = NULL_TREE;
8566 bool is_static = FIELD_STATIC (decl);
8568 if (FIELD_NESTED_ACCESS_P (decl))
8569 return FIELD_NESTED_ACCESS (decl);
8571 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8573 /* Create the identifier and a function named after it. */
8574 id = build_new_access_id ();
8576 /* The identifier is marked as bearing the name of a generated write
8577 access function for outer field accessed from inner classes. */
8578 NESTED_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8580 /* Create the read access. */
8583 args = build_tree_list (inst_id,
8584 build_pointer_type (DECL_CONTEXT (decl)));
8585 TREE_CHAIN (args) = end_params_node;
8586 stmt = make_qualified_primary (build_wfl_node (inst_id),
8587 build_wfl_node (DECL_NAME (decl)), 0);
8591 args = end_params_node;
8592 class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
8593 stmt = make_qualified_primary (build_wfl_node (class_name),
8594 build_wfl_node (DECL_NAME (decl)), 0);
8596 stmt = build_return (0, stmt);
8597 mdecl = build_nested_field_access_method (DECL_CONTEXT (decl),
8598 TREE_TYPE (decl), id, args, stmt);
8599 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8601 /* Create the write access method. No write access for final variable */
8602 if (!FIELD_FINAL (decl))
8606 args = build_tree_list (inst_id,
8607 build_pointer_type (DECL_CONTEXT (decl)));
8608 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8609 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8610 stmt = make_qualified_primary (build_wfl_node (inst_id),
8611 build_wfl_node (DECL_NAME (decl)),
8616 args = build_tree_list (wpv_id, TREE_TYPE (decl));
8617 TREE_CHAIN (args) = end_params_node;
8618 stmt = make_qualified_primary (build_wfl_node (class_name),
8619 build_wfl_node (DECL_NAME (decl)),
8622 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8623 build_wfl_node (wpv_id)));
8624 mdecl = build_nested_field_access_method (DECL_CONTEXT (decl),
8625 TREE_TYPE (decl), id,
8628 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8630 /* Return the access name */
8631 return FIELD_NESTED_ACCESS (decl) = id;
8634 /* Build a field access method NAME. */
8637 build_nested_field_access_method (tree class, tree type, tree name,
8638 tree args, tree body)
8640 tree saved_current_function_decl, mdecl;
8642 /* Create the method */
8643 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8644 fix_method_argument_names (args, mdecl);
8645 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8647 /* Attach the method body. */
8648 saved_current_function_decl = current_function_decl;
8649 start_artificial_method_body (mdecl);
8650 java_method_add_stmt (mdecl, body);
8651 end_artificial_method_body (mdecl);
8652 current_function_decl = saved_current_function_decl;
8658 /* This section deals with building access function necessary for
8659 certain kinds of method invocation from inner classes. */
8662 build_outer_method_access_method (tree decl)
8664 tree saved_current_function_decl, mdecl;
8665 tree args = NULL_TREE, call_args = NULL_TREE;
8666 tree carg, id, body, class;
8668 int parm_id_count = 0;
8670 /* Test this abort with an access to a private field */
8671 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8674 /* Check the cache first */
8675 if (DECL_FUNCTION_INNER_ACCESS (decl))
8676 return DECL_FUNCTION_INNER_ACCESS (decl);
8678 class = DECL_CONTEXT (decl);
8680 /* Obtain an access identifier and mark it */
8681 id = build_new_access_id ();
8682 NESTED_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8684 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8685 /* Create the arguments, as much as the original */
8686 for (; carg && carg != end_params_node;
8687 carg = TREE_CHAIN (carg))
8689 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8690 args = chainon (args, build_tree_list (get_identifier (buffer),
8691 TREE_VALUE (carg)));
8693 args = chainon (args, end_params_node);
8695 /* Create the method */
8696 mdecl = create_artificial_method (class, ACC_STATIC,
8697 TREE_TYPE (TREE_TYPE (decl)), id, args);
8698 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8699 /* There is a potential bug here. We should be able to use
8700 fix_method_argument_names, but then arg names get mixed up and
8701 eventually a constructor will have its this$0 altered and the
8702 outer context won't be assignment properly. The testcase is
8704 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8706 /* Attach the method body. */
8707 saved_current_function_decl = current_function_decl;
8708 start_artificial_method_body (mdecl);
8710 /* The actual method invocation uses the same args. When invoking a
8711 static methods that way, we don't want to skip the first
8714 if (!METHOD_STATIC (decl))
8715 carg = TREE_CHAIN (carg);
8716 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8717 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8720 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8722 if (!METHOD_STATIC (decl))
8723 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8725 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8726 body = build_return (0, body);
8727 java_method_add_stmt (mdecl,body);
8728 end_artificial_method_body (mdecl);
8729 current_function_decl = saved_current_function_decl;
8731 /* Back tag the access function so it know what it accesses */
8732 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8734 /* Tag the current method so it knows it has an access generated */
8735 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8739 /* This section of the code deals with building expressions to access
8740 the enclosing instance of an inner class. The enclosing instance is
8741 kept in a generated field called this$<n>, with <n> being the
8742 inner class nesting level (starting from 0.) */
8744 /* Build an access to a given this$<n>, always chaining access call to
8745 others. Access methods to this$<n> are build on the fly if
8746 necessary. This CAN'T be used to solely access this$<n-1> from
8747 this$<n> (which alway yield to special cases and optimization, see
8748 for example build_nested_field_access). */
8751 build_access_to_thisn (tree from, tree to, int lc)
8753 tree access = NULL_TREE;
8755 while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8759 access = build_current_thisn (from);
8760 access = build_wfl_node (access);
8764 tree access0_wfl, cn;
8766 maybe_build_thisn_access_method (from);
8767 access0_wfl = build_wfl_node (access0_identifier_node);
8768 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8769 EXPR_WFL_LINECOL (access0_wfl) = lc;
8770 access = build_tree_list (NULL_TREE, access);
8771 access = build_method_invocation (access0_wfl, access);
8772 access = make_qualified_primary (cn, access, lc);
8775 /* If FROM isn't an inner class, that's fine, we've done enough.
8776 What we're looking for can be accessed from there. */
8777 from = DECL_CONTEXT (TYPE_NAME (from));
8780 from = TREE_TYPE (from);
8785 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8786 is returned if nothing needs to be generated. Otherwise, the method
8787 generated and a method decl is returned.
8789 NOTE: These generated methods should be declared in a class file
8790 attribute so that they can't be referred to directly. */
8793 maybe_build_thisn_access_method (tree type)
8795 tree mdecl, args, stmt, rtype;
8796 tree saved_current_function_decl;
8798 /* If TYPE is a top-level class, no access method is required.
8799 If there already is such an access method, bail out. */
8800 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8803 /* We generate the method. The method looks like:
8804 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8806 args = build_tree_list (inst_id, build_pointer_type (type));
8807 TREE_CHAIN (args) = end_params_node;
8808 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8809 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8810 access0_identifier_node, args);
8811 fix_method_argument_names (args, mdecl);
8812 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8813 stmt = build_current_thisn (type);
8814 stmt = make_qualified_primary (build_wfl_node (inst_id),
8815 build_wfl_node (stmt), 0);
8816 stmt = build_return (0, stmt);
8818 saved_current_function_decl = current_function_decl;
8819 start_artificial_method_body (mdecl);
8820 java_method_add_stmt (mdecl, stmt);
8821 end_artificial_method_body (mdecl);
8822 current_function_decl = saved_current_function_decl;
8824 CLASS_ACCESS0_GENERATED_P (type) = 1;
8829 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8830 the first level of innerclassing. this$1 for the next one, etc...
8831 This function can be invoked with TYPE to NULL, available and then
8832 has to count the parser context. */
8834 static GTY(()) tree saved_thisn;
8835 static GTY(()) tree saved_type;
8838 build_current_thisn (tree type)
8840 static int saved_i = -1;
8841 static int saved_type_i = 0;
8848 if (type == saved_type)
8852 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8853 decl; decl = DECL_CONTEXT (decl), i++)
8861 i = list_length (GET_CPC_LIST ())-2;
8866 sprintf (buffer, "this$%d", i);
8868 saved_thisn = get_identifier (buffer);
8872 /* Return the assignment to the hidden enclosing context `this$<n>'
8873 by the second incoming parameter to the innerclass constructor. The
8874 form used is `this.this$<n> = this$<n>;'. */
8877 build_thisn_assign (void)
8879 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8881 tree thisn = build_current_thisn (current_class);
8882 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8883 build_wfl_node (thisn), 0);
8884 tree rhs = build_wfl_node (thisn);
8885 #ifdef USE_MAPPED_LOCATION
8886 SET_EXPR_LOCATION (lhs, input_location);
8888 EXPR_WFL_SET_LINECOL (lhs, input_line, 0);
8890 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8896 /* Building the synthetic `class$' used to implement the `.class' 1.1
8897 extension for non primitive types. This method looks like:
8899 static Class class$(String type) throws NoClassDefFoundError
8901 try {return (java.lang.Class.forName (String));}
8902 catch (ClassNotFoundException e) {
8903 throw new NoClassDefFoundError(e.getMessage());}
8906 static GTY(()) tree get_message_wfl;
8907 static GTY(()) tree type_parm_wfl;
8910 build_dot_class_method (tree class)
8912 #define BWF(S) build_wfl_node (get_identifier ((S)))
8913 #ifdef USE_MAPPED_LOCATION
8914 #define MQN(X,Y) make_qualified_name ((X), (Y), UNKNOWN_LOCATION)
8916 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8918 tree args, tmp, saved_current_function_decl, mdecl, qual_name;
8919 tree stmt, throw_stmt;
8921 if (!get_message_wfl)
8923 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8924 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8927 /* Build the arguments */
8928 args = build_tree_list (get_identifier ("type$"),
8929 build_pointer_type (string_type_node));
8930 TREE_CHAIN (args) = end_params_node;
8932 /* Build the qualified name java.lang.Class.forName */
8933 tmp = MQN (MQN (MQN (BWF ("java"),
8934 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8936 /* Create the "class$" function */
8937 mdecl = create_artificial_method (class, ACC_STATIC,
8938 build_pointer_type (class_type_node),
8939 classdollar_identifier_node, args);
8940 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8941 BWF ("NoClassDefFoundError"));
8942 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE, qual_name);
8943 register_incomplete_type (JDEP_EXCEPTION, qual_name, NULL_TREE, NULL_TREE);
8944 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
8945 &TREE_VALUE (DECL_FUNCTION_THROWS (mdecl));
8947 /* We start by building the try block. We need to build:
8948 return (java.lang.Class.forName (type)); */
8949 stmt = build_method_invocation (tmp,
8950 build_tree_list (NULL_TREE, type_parm_wfl));
8951 stmt = build_return (0, stmt);
8953 /* Now onto the catch block. We start by building the expression
8954 throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8955 #ifdef USE_MAPPED_LOCATION
8956 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8957 get_message_wfl, UNKNOWN_LOCATION);
8959 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8960 get_message_wfl, 0);
8962 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8964 /* Build new NoClassDefFoundError (_.getMessage) */
8965 throw_stmt = build_new_invocation
8966 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8967 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8969 /* Build the throw, (it's too early to use BUILD_THROW) */
8970 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8972 /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8973 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8974 BWF ("ClassNotFoundException"));
8975 stmt = encapsulate_with_try_catch (0, qual_name, stmt, throw_stmt);
8977 fix_method_argument_names (args, mdecl);
8978 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8979 saved_current_function_decl = current_function_decl;
8980 start_artificial_method_body (mdecl);
8981 java_method_add_stmt (mdecl, stmt);
8982 end_artificial_method_body (mdecl);
8983 current_function_decl = saved_current_function_decl;
8984 TYPE_DOT_CLASS (class) = mdecl;
8990 build_dot_class_method_invocation (tree this_class, tree type)
8992 tree dot_class_method = TYPE_DOT_CLASS (this_class);
8995 if (TYPE_ARRAY_P (type))
8996 sig_id = build_java_signature (type);
8998 sig_id = DECL_NAME (TYPE_NAME (type));
9000 /* Ensure that the proper name separator is used */
9001 sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
9002 IDENTIFIER_LENGTH (sig_id));
9004 s = build_string (IDENTIFIER_LENGTH (sig_id),
9005 IDENTIFIER_POINTER (sig_id));
9006 t = build_method_invocation (build_wfl_node (DECL_NAME (dot_class_method)),
9007 build_tree_list (NULL_TREE, s));
9008 if (DECL_CONTEXT (dot_class_method) != this_class)
9010 tree class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (dot_class_method)));
9011 t = make_qualified_primary (build_wfl_node (class_name), t, 0);
9016 /* This section of the code deals with constructor. */
9018 /* Craft a body for default constructor. Patch existing constructor
9019 bodies with call to super() and field initialization statements if
9023 fix_constructors (tree mdecl)
9025 tree iii; /* Instance Initializer Invocation */
9026 tree *bodyp = &DECL_FUNCTION_BODY (mdecl);
9027 tree thisn_assign, compound = NULL_TREE;
9028 tree class_type = DECL_CONTEXT (mdecl);
9030 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
9032 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
9036 /* It is an error for the compiler to generate a default
9037 constructor if the superclass doesn't have a constructor that
9038 takes no argument, or the same args for an anonymous class */
9039 if (verify_constructor_super (mdecl))
9041 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
9042 tree save = DECL_NAME (mdecl);
9043 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
9044 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
9046 (lookup_cl (TYPE_NAME (class_type)),
9047 "No constructor matching %qs found in class %qs",
9048 lang_printable_name (mdecl, 2), n);
9049 DECL_NAME (mdecl) = save;
9052 /* The constructor body must be crafted by hand. It's the
9053 constructor we defined when we realize we didn't have the
9054 CLASSNAME() constructor */
9055 start_artificial_method_body (mdecl);
9057 /* Insert an assignment to the this$<n> hidden field, if
9059 if ((thisn_assign = build_thisn_assign ()))
9060 java_method_add_stmt (mdecl, thisn_assign);
9062 /* We don't generate a super constructor invocation if we're
9063 compiling java.lang.Object. build_super_invocation takes care
9065 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
9068 if ((iii = build_instinit_invocation (class_type)))
9069 java_method_add_stmt (mdecl, iii);
9071 end_artificial_method_body (mdecl);
9073 /* Search for an explicit constructor invocation */
9077 int invokes_this = 0;
9078 tree main_block = BLOCK_EXPR_BODY (*bodyp);
9083 switch (TREE_CODE (body))
9086 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
9087 if (CALL_THIS_CONSTRUCTOR_P (body))
9091 case EXPR_WITH_FILE_LOCATION:
9092 bodyp = &TREE_OPERAND (body, 0);
9095 bodyp = &BLOCK_EXPR_BODY (body);
9103 /* Generate the assignment to this$<n>, if necessary */
9104 if ((thisn_assign = build_thisn_assign ()))
9105 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
9107 /* The constructor is missing an invocation of super() */
9109 compound = add_stmt_to_compound (compound, NULL_TREE,
9110 build_super_invocation (mdecl));
9111 /* Explicit super() invocation should take place before the
9112 instance initializer blocks. */
9115 compound = add_stmt_to_compound (compound, NULL_TREE, *bodyp);
9116 *bodyp = build_java_empty_stmt ();
9119 DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
9121 /* Insert the instance initializer block right after. */
9122 if (!invokes_this && (iii = build_instinit_invocation (class_type)))
9123 compound = add_stmt_to_compound (compound, NULL_TREE, iii);
9125 /* Fix the constructor main block if we're adding extra stmts */
9128 compound = add_stmt_to_compound (compound, NULL_TREE,
9129 BLOCK_EXPR_BODY (main_block));
9130 BLOCK_EXPR_BODY (main_block) = compound;
9135 /* Browse constructors in the super class, searching for a constructor
9136 that doesn't take any argument. Return 0 if one is found, 1
9137 otherwise. If the current class is an anonymous inner class, look
9138 for something that has the same signature. */
9141 verify_constructor_super (tree mdecl)
9143 tree class = CLASSTYPE_SUPER (current_class);
9144 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
9150 if (ANONYMOUS_CLASS_P (current_class))
9152 tree mdecl_arg_type;
9153 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
9154 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9155 if (DECL_CONSTRUCTOR_P (sdecl))
9158 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9160 arg_type = TREE_CHAIN (arg_type);
9161 for (m_arg_type = mdecl_arg_type;
9162 (arg_type != end_params_node
9163 && m_arg_type != end_params_node);
9164 arg_type = TREE_CHAIN (arg_type),
9165 m_arg_type = TREE_CHAIN (m_arg_type))
9166 if (!valid_method_invocation_conversion_p
9167 (TREE_VALUE (arg_type),
9168 TREE_VALUE (m_arg_type)))
9171 if (arg_type == end_params_node && m_arg_type == end_params_node)
9177 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9179 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9181 arg = TREE_CHAIN (arg);
9182 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
9189 /* Generate code for all context remembered for code generation. */
9191 static GTY(()) tree reversed_class_list;
9193 java_expand_classes (void)
9195 int save_error_count = 0;
9196 static struct parser_ctxt *cur_ctxp = NULL;
9197 location_t save_location;
9199 java_parse_abort_on_error ();
9200 if (!(ctxp = ctxp_for_generation))
9202 java_layout_classes ();
9203 java_parse_abort_on_error ();
9204 save_location = input_location;
9206 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9209 for (current = cur_ctxp->class_list;
9211 current = TREE_CHAIN (current))
9212 gen_indirect_dispatch_tables (TREE_TYPE (current));
9215 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9218 input_location = ctxp->file_start_location;
9219 lang_init_source (2); /* Error msgs have method prototypes */
9220 java_complete_expand_classes (); /* Complete and expand classes */
9221 java_parse_abort_on_error ();
9223 input_location = save_location;
9225 /* Find anonymous classes and expand their constructor. This extra pass is
9226 necessary because the constructor itself is only generated when the
9227 method in which it is defined is expanded. */
9228 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9232 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9234 output_class = current_class = TREE_TYPE (current);
9235 if (ANONYMOUS_CLASS_P (current_class))
9238 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9240 if (DECL_CONSTRUCTOR_P (d))
9242 java_complete_expand_method (d);
9243 break; /* There is only one constructor. */
9250 /* Expanding the constructors of anonymous classes generates access
9251 methods. Scan all the methods looking for null DECL_RESULTs --
9252 this will be the case if a method hasn't been expanded. */
9253 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9257 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9260 output_class = current_class = TREE_TYPE (current);
9261 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9263 if (DECL_RESULT (d) == NULL_TREE)
9264 java_complete_expand_method (d);
9269 /* ??? Instead of all this we could iterate around the list of
9270 classes until there were no more un-expanded methods. It would
9271 take a little longer -- one pass over the whole list of methods
9272 -- but it would be simpler. Like this: */
9275 int something_changed;
9279 something_changed = 0;
9280 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9284 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9287 output_class = current_class = TREE_TYPE (current);
9288 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9290 if (DECL_RESULT (d) == NULL_TREE)
9292 something_changed = 1;
9293 java_complete_expand_method (d);
9299 while (something_changed);
9303 /* If we've found error at that stage, don't try to generate
9304 anything, unless we're checking the syntax only
9305 (but not using -fsyntax-only for the purpose of generating
9307 if (java_error_count
9308 && (!flag_syntax_only && !flag_emit_class_files))
9311 /* Now things are stable, go for generation of the class data. */
9313 /* We pessimistically marked all methods and fields external until
9314 we knew what set of classes we were planning to compile. Now mark
9315 those that will be generated locally as not external. */
9316 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9320 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9321 java_mark_class_local (TREE_TYPE (current));
9324 /* Compile the classes. */
9325 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9328 reversed_class_list = NULL;
9332 /* We write out the classes in reverse order. This ensures that
9333 inner classes are written before their containing classes,
9334 which is important for parallel builds. Otherwise, the
9335 class file for the outer class may be found, but the class
9336 file for the inner class may not be present. In that
9337 situation, the compiler cannot fall back to the original
9338 source, having already read the outer class, so we must
9339 prevent that situation. */
9340 for (current = ctxp->class_list;
9342 current = TREE_CHAIN (current))
9344 = tree_cons (NULL_TREE, current, reversed_class_list);
9346 for (current = reversed_class_list;
9348 current = TREE_CHAIN (current))
9350 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
9351 if (flag_emit_class_files)
9352 write_classfile (current_class);
9353 else if (! flag_syntax_only)
9354 java_expand_method_bodies (current_class);
9360 java_finish_classes (void)
9362 static struct parser_ctxt *cur_ctxp = NULL;
9363 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9367 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9369 output_class = current_class = TREE_TYPE (current);
9375 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9376 a tree list node containing RIGHT. Fore coming RIGHTs will be
9377 chained to this hook. LOCATION contains the location of the
9378 separating `.' operator. */
9381 make_qualified_primary (tree primary, tree right, int location)
9385 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9386 wfl = build_wfl_wrap (primary, location);
9390 /* If wfl wasn't qualified, we build a first anchor */
9391 if (!EXPR_WFL_QUALIFICATION (wfl))
9392 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9395 /* And chain them */
9396 EXPR_WFL_LINECOL (right) = location;
9397 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9398 PRIMARY_P (wfl) = 1;
9402 /* Simple merge of two name separated by a `.' */
9405 merge_qualified_name (tree left, tree right)
9408 if (!left && !right)
9417 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9418 IDENTIFIER_LENGTH (left));
9419 obstack_1grow (&temporary_obstack, '.');
9420 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9421 IDENTIFIER_LENGTH (right));
9422 node = get_identifier (obstack_base (&temporary_obstack));
9423 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9424 QUALIFIED_P (node) = 1;
9428 /* Merge the two parts of a qualified name into LEFT. Set the
9429 location information of the resulting node to LOCATION, usually
9430 inherited from the location information of the `.' operator. */
9433 make_qualified_name (tree left, tree right,
9434 #ifdef USE_MAPPED_LOCATION
9435 source_location location
9441 #ifdef USE_COMPONENT_REF
9442 tree node = build3 (COMPONENT_REF, NULL_TREE, left, right, NULL_TREE);
9443 SET_EXPR_LOCATION (node, location);
9446 tree left_id = EXPR_WFL_NODE (left);
9447 tree right_id = EXPR_WFL_NODE (right);
9450 merge = merge_qualified_name (left_id, right_id);
9452 /* Left wasn't qualified and is now qualified */
9453 #ifdef USE_MAPPED_LOCATION
9454 if (!QUALIFIED_P (left_id))
9456 tree wfl = build_expr_wfl (left_id, EXPR_LOCATION (left));
9457 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9460 wfl = build_expr_wfl (right_id, location);
9462 if (!QUALIFIED_P (left_id))
9464 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9465 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9466 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9469 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9470 EXPR_WFL_LINECOL (wfl) = location;
9472 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9473 EXPR_WFL_NODE (left) = merge;
9478 /* Extract the last identifier component of the qualified in WFL. The
9479 last identifier is removed from the linked list */
9482 cut_identifier_in_qualified (tree wfl)
9485 tree previous = NULL_TREE;
9486 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9487 if (!TREE_CHAIN (q))
9490 /* Operating on a non qualified qualified WFL. */
9493 TREE_CHAIN (previous) = NULL_TREE;
9494 return TREE_PURPOSE (q);
9498 /* Resolve the expression name NAME. Return its decl. */
9501 resolve_expression_name (tree id, tree *orig)
9503 tree name = EXPR_WFL_NODE (id);
9506 /* 6.5.5.1: Simple expression names */
9507 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9509 /* 15.13.1: NAME can appear within the scope of a local variable
9511 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9514 /* 15.13.1: NAME can appear within a class declaration */
9517 decl = lookup_field_wrapper (current_class, name);
9520 tree access = NULL_TREE;
9521 int fs = FIELD_STATIC (decl);
9523 /* If we're accessing an outer scope local alias, make
9524 sure we change the name of the field we're going to
9526 if (FIELD_LOCAL_ALIAS_USED (decl))
9527 name = DECL_NAME (decl);
9529 check_deprecation (id, decl);
9531 /* Instance variable (8.3.1.1) can't appear within
9532 static method, static initializer or initializer for
9533 a static variable. */
9534 if (!fs && METHOD_STATIC (current_function_decl))
9536 static_ref_err (id, name, current_class);
9537 return error_mark_node;
9539 /* Instance variables can't appear as an argument of
9540 an explicit constructor invocation */
9541 if (!fs && ctxp->explicit_constructor_p
9542 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9545 (id, "Can't reference %qs before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9546 return error_mark_node;
9549 /* If we're processing an inner class and we're trying
9550 to access a field belonging to an outer class, build
9551 the access to the field. */
9552 if (nested_field_access_p (current_class, decl))
9554 if (!fs && CLASS_STATIC (TYPE_NAME (current_class)))
9556 static_ref_err (id, DECL_NAME (decl), current_class);
9557 return error_mark_node;
9559 access = build_nested_field_access (id, decl);
9565 /* Otherwise build what it takes to access the field */
9566 access = build_field_ref ((fs ? NULL_TREE : current_this),
9567 DECL_CONTEXT (decl), name);
9569 access = maybe_build_class_init_for_field (decl, access);
9570 /* We may be asked to save the real field access node */
9573 /* Last check: can we access the field? */
9574 if (not_accessible_p (current_class, decl, NULL_TREE, 0))
9576 not_accessible_field_error (id, decl);
9577 return error_mark_node;
9579 /* And we return what we got */
9582 /* Fall down to error report on undefined variable */
9585 /* 6.5.5.2 Qualified Expression Names */
9590 qualify_ambiguous_name (id);
9591 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9592 /* 15.10.2: Accessing Superclass Members using super */
9593 return resolve_field_access (id, orig, NULL);
9596 /* We've got an error here */
9597 if (INNER_CLASS_TYPE_P (current_class))
9598 parse_error_context (id,
9599 "Local variable %qs can't be accessed from within the inner class %qs unless it is declared final",
9600 IDENTIFIER_POINTER (name),
9601 IDENTIFIER_POINTER (DECL_NAME
9602 (TYPE_NAME (current_class))));
9604 parse_error_context (id, "Undefined variable %qs",
9605 IDENTIFIER_POINTER (name));
9607 return error_mark_node;
9611 static_ref_err (tree wfl, tree field_id, tree class_type)
9615 "Can't make a static reference to nonstatic variable %qs in class %qs",
9616 IDENTIFIER_POINTER (field_id),
9617 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9620 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9621 We return something suitable to generate the field access. We also
9622 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9623 recipient's address can be null. */
9626 resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9630 tree decl = NULL_TREE, where_found, type_found;
9632 if (resolve_qualified_expression_name (qual_wfl, &decl,
9633 &where_found, &type_found))
9634 return error_mark_node;
9636 /* Resolve the LENGTH field of an array here */
9637 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9638 && type_found && TYPE_ARRAY_P (type_found)
9639 && ! flag_emit_class_files)
9641 tree length = build_java_array_length_access (where_found);
9644 /* In case we're dealing with a static array, we need to
9645 initialize its class before the array length can be fetched. */
9646 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9648 build_static_field_ref (where_found);
9649 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9652 /* We might have been trying to resolve field.method(). In which
9653 case, the resolution is over and decl is the answer */
9654 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9656 else if (JDECL_P (decl))
9659 type_found = DECL_CONTEXT (decl);
9660 is_static = FIELD_STATIC (decl);
9661 field_ref = build_field_ref ((is_static ?
9662 NULL_TREE : where_found),
9663 type_found, DECL_NAME (decl));
9664 if (field_ref == error_mark_node)
9665 return error_mark_node;
9667 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9669 /* If we're looking at a static field, we may need to generate a
9670 class initialization for it. This can happen when the access
9671 looks like `field.ref', where `field' is a static field in an
9672 interface we implement. */
9673 if (!flag_emit_class_files
9674 && TREE_CODE (where_found) == VAR_DECL
9675 && FIELD_STATIC (where_found))
9677 build_static_field_ref (where_found);
9678 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9687 *field_type = (QUAL_DECL_TYPE (decl) ?
9688 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9692 /* If NODE is an access to a static field, strip out the class
9693 initialization part and return the field decl, otherwise, return
9697 extract_field_decl (tree node)
9699 if (TREE_CODE (node) == COMPOUND_EXPR)
9701 tree op1 = TREE_OPERAND (node, 1);
9702 if (TREE_CODE (op1) == COMPOUND_EXPR)
9704 tree call = TREE_OPERAND (op1, 0);
9705 if (TREE_CODE (call) == CALL_EXPR
9706 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9707 && (TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9708 == soft_initclass_node))
9709 return TREE_OPERAND (op1, 1);
9711 else if (JDECL_P (op1))
9717 /* 6.5.5.2: Qualified Expression Names */
9720 resolve_qualified_expression_name (tree wfl, tree *found_decl,
9721 tree *where_found, tree *type_found)
9723 int from_type = 0; /* Field search initiated from a type */
9724 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9725 int previous_call_static = 0;
9727 tree decl = NULL_TREE, type = NULL_TREE, q;
9728 /* For certain for of inner class instantiation */
9729 tree saved_current, saved_this;
9730 #define RESTORE_THIS_AND_CURRENT_CLASS \
9731 { current_class = saved_current; current_this = saved_this;}
9733 *type_found = *where_found = NULL_TREE;
9735 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9737 tree qual_wfl = QUAL_WFL (q);
9738 tree ret_decl; /* for EH checking */
9739 #ifdef USE_MAPPED_LOCATION
9740 source_location location; /* for EH checking */
9742 int location; /* for EH checking */
9745 /* 15.10.1 Field Access Using a Primary */
9746 switch (TREE_CODE (qual_wfl))
9749 case NEW_CLASS_EXPR:
9750 /* If the access to the function call is a non static field,
9751 build the code to access it. */
9752 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9754 decl = maybe_access_field (decl, *where_found,
9755 DECL_CONTEXT (decl));
9756 if (decl == error_mark_node)
9760 /* And code for the function call */
9761 if (complete_function_arguments (qual_wfl))
9764 /* We might have to setup a new current class and a new this
9765 for the search of an inner class, relative to the type of
9766 a expression resolved as `decl'. The current values are
9767 saved and restored shortly after */
9768 saved_current = current_class;
9769 saved_this = current_this;
9771 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9772 || from_qualified_this))
9774 /* If we still have `from_qualified_this', we have the form
9775 <T>.this.f() and we need to build <T>.this */
9776 if (from_qualified_this)
9778 decl = build_access_to_thisn (current_class, type, 0);
9779 decl = java_complete_tree (decl);
9780 type = TREE_TYPE (TREE_TYPE (decl));
9782 current_class = type;
9783 current_this = decl;
9784 from_qualified_this = 0;
9787 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9788 CALL_USING_SUPER (qual_wfl) = 1;
9789 #ifdef USE_MAPPED_LOCATION
9790 location = (TREE_CODE (qual_wfl) == CALL_EXPR
9791 ? EXPR_LOCATION (TREE_OPERAND (qual_wfl, 0))
9792 : UNKNOWN_LOCATION);
9794 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9795 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9797 *where_found = patch_method_invocation (qual_wfl, decl, type,
9799 &is_static, &ret_decl);
9801 if (*where_found == error_mark_node)
9803 RESTORE_THIS_AND_CURRENT_CLASS;
9806 *type_found = type = QUAL_DECL_TYPE (*where_found);
9808 *where_found = force_evaluation_order (*where_found);
9810 /* If we're creating an inner class instance, check for that
9811 an enclosing instance is in scope */
9812 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9813 && INNER_ENCLOSING_SCOPE_CHECK (type))
9816 (qual_wfl, "No enclosing instance for inner class %qs is in scope%s",
9817 lang_printable_name (type, 0),
9818 (!current_this ? "" :
9819 "; an explicit one must be provided when creating this inner class"));
9820 RESTORE_THIS_AND_CURRENT_CLASS;
9824 /* In case we had to change then to resolve a inner class
9825 instantiation using a primary qualified by a `new' */
9826 RESTORE_THIS_AND_CURRENT_CLASS;
9828 #ifdef USE_MAPPED_LOCATION
9829 if (location != UNKNOWN_LOCATION)
9834 tree arguments = NULL_TREE;
9835 if (TREE_CODE (qual_wfl) == CALL_EXPR
9836 && TREE_OPERAND (qual_wfl, 1) != NULL_TREE)
9837 arguments = TREE_VALUE (TREE_OPERAND (qual_wfl, 1));
9838 check_thrown_exceptions (location, ret_decl, arguments);
9841 /* If the previous call was static and this one is too,
9842 build a compound expression to hold the two (because in
9843 that case, previous function calls aren't transported as
9844 forcoming function's argument. */
9845 if (previous_call_static && is_static)
9847 /* We must set CAN_COMPLETE_NORMALLY for the first call
9848 since it is done nowhere else. */
9849 CAN_COMPLETE_NORMALLY (decl) = 1;
9850 decl = build2 (COMPOUND_EXPR, TREE_TYPE (*where_found),
9851 decl, *where_found);
9852 TREE_SIDE_EFFECTS (decl) = 1;
9856 previous_call_static = is_static;
9857 decl = *where_found;
9862 case NEW_ARRAY_EXPR:
9863 case NEW_ANONYMOUS_ARRAY_EXPR:
9864 *where_found = decl = java_complete_tree (qual_wfl);
9865 if (decl == error_mark_node)
9867 *type_found = type = QUAL_DECL_TYPE (decl);
9871 *where_found = decl = java_complete_tree (qual_wfl);
9872 if (decl == error_mark_node)
9874 *type_found = type = QUAL_DECL_TYPE (decl);
9878 case CONDITIONAL_EXPR:
9881 *where_found = decl = java_complete_tree (qual_wfl);
9882 if (decl == error_mark_node)
9884 *type_found = type = QUAL_DECL_TYPE (decl);
9888 /* If the access to the function call is a non static field,
9889 build the code to access it. */
9890 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9892 decl = maybe_access_field (decl, *where_found, type);
9893 if (decl == error_mark_node)
9896 /* And code for the array reference expression */
9897 decl = java_complete_tree (qual_wfl);
9898 if (decl == error_mark_node)
9900 type = QUAL_DECL_TYPE (decl);
9904 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9906 if ((type = patch_string (decl)))
9908 *where_found = QUAL_RESOLUTION (q) = decl;
9909 *type_found = type = TREE_TYPE (decl);
9913 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9915 *where_found = QUAL_RESOLUTION (q) = decl;
9916 *type_found = type = TREE_TYPE (decl);
9920 /* Fix for -Wall Just go to the next statement. Don't
9925 /* If we fall here, we weren't processing a (static) function call. */
9926 previous_call_static = 0;
9928 /* It can be the keyword THIS */
9929 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9930 && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9935 (wfl, "Keyword %<this%> used outside allowed context");
9938 if (ctxp->explicit_constructor_p
9939 && type == current_class)
9941 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
9944 /* We have to generate code for intermediate access */
9945 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9947 *where_found = decl = current_this;
9948 *type_found = type = QUAL_DECL_TYPE (decl);
9950 /* We're trying to access the this from somewhere else. Make sure
9951 it's allowed before doing so. */
9954 if (!enclosing_context_p (type, current_class))
9956 char *p = xstrdup (lang_printable_name (type, 0));
9957 parse_error_context (qual_wfl, "Can't use variable %<%s.this%>: type %qs isn't an outer type of type %qs",
9959 lang_printable_name (current_class, 0));
9963 from_qualified_this = 1;
9964 /* If there's nothing else after that, we need to
9965 produce something now, otherwise, the section of the
9966 code that needs to produce <T>.this will generate
9967 what is necessary. */
9968 if (!TREE_CHAIN (q))
9970 decl = build_access_to_thisn (current_class, type, 0);
9971 *where_found = decl = java_complete_tree (decl);
9972 *type_found = type = TREE_TYPE (decl);
9980 /* 15.10.2 Accessing Superclass Members using SUPER */
9981 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9982 && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9985 /* Check on the restricted use of SUPER */
9986 if (METHOD_STATIC (current_function_decl)
9987 || current_class == object_type_node)
9990 (wfl, "Keyword %<super%> used outside allowed context");
9993 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9994 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9995 CLASSTYPE_SUPER (current_class),
9996 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9997 *where_found = decl = java_complete_tree (node);
9998 if (decl == error_mark_node)
10000 *type_found = type = QUAL_DECL_TYPE (decl);
10001 from_super = from_type = 1;
10005 /* 15.13.1: Can't search for field name in packages, so we
10006 assume a variable/class name was meant. */
10007 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10010 if ((decl = resolve_package (wfl, &q, &name)))
10013 *where_found = decl;
10015 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10017 /* We want to be absolutely sure that the class is laid
10018 out. We're going to search something inside it. */
10019 *type_found = type = TREE_TYPE (decl);
10020 layout_class (type);
10023 /* Fix them all the way down, if any are left. */
10026 list = TREE_CHAIN (q);
10029 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
10030 list = TREE_CHAIN (list);
10036 if (from_super || from_cast)
10037 parse_error_context
10038 ((from_cast ? qual_wfl : wfl),
10039 "No variable %qs defined in class %qs",
10040 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10041 lang_printable_name (type, 0));
10043 parse_error_context
10044 (qual_wfl, "Undefined variable or class name: %qs",
10045 IDENTIFIER_POINTER (name));
10050 /* We have a type name. It's been already resolved when the
10051 expression was qualified. */
10052 else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
10054 decl = QUAL_RESOLUTION (q);
10056 /* Sneak preview. If next we see a `new', we're facing a
10057 qualification which resulted in a type being selected
10058 instead of a field. Report the error. */
10060 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
10062 parse_error_context (qual_wfl, "Undefined variable %qs",
10063 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10067 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10069 check_deprecation (qual_wfl, decl);
10071 type = TREE_TYPE (decl);
10074 /* We resolve an expression name */
10077 tree field_decl = NULL_TREE;
10079 /* If there exists an early resolution, use it. That occurs
10080 only once and we know that there are more things to
10081 come. Don't do that when processing something after SUPER
10082 (we need more thing to be put in place below */
10083 if (!from_super && QUAL_RESOLUTION (q))
10085 decl = QUAL_RESOLUTION (q);
10088 if (TREE_CODE (decl) == FIELD_DECL
10089 || TREE_CODE (decl) == VAR_DECL)
10091 if (TREE_CODE (decl) == FIELD_DECL
10092 && !FIELD_STATIC (decl))
10095 *where_found = current_this;
10098 static_ref_err (qual_wfl, DECL_NAME (decl),
10105 *where_found = TREE_TYPE (decl);
10106 if (TREE_CODE (*where_found) == POINTER_TYPE)
10107 *where_found = TREE_TYPE (*where_found);
10109 if (nested_field_access_p (current_class, decl))
10110 decl = build_nested_field_access (qual_wfl, decl);
10114 *where_found = TREE_TYPE (decl);
10115 if (TREE_CODE (*where_found) == POINTER_TYPE)
10116 *where_found = TREE_TYPE (*where_found);
10121 /* Report and error if we're using a numerical literal as a
10122 qualifier. It can only be an INTEGER_CST. */
10123 else if (TREE_CODE (qual_wfl) == INTEGER_CST)
10125 parse_error_context
10126 (wfl, "Can't use type %qs as a qualifier",
10127 lang_printable_name (TREE_TYPE (qual_wfl), 0));
10131 /* We have to search for a field, knowing the type of its
10132 container. The flag FROM_TYPE indicates that we resolved
10133 the last member of the expression as a type name, which
10134 means that for the resolution of this field, we'll look
10135 for other errors than if it was resolved as a member of
10140 tree field_decl_type; /* For layout */
10142 if (!from_type && !JREFERENCE_TYPE_P (type))
10144 parse_error_context
10145 (qual_wfl, "Attempt to reference field %qs in %<%s %s%>",
10146 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10147 lang_printable_name (type, 0),
10148 IDENTIFIER_POINTER (DECL_NAME (decl)));
10152 field_decl = lookup_field_wrapper (type,
10153 EXPR_WFL_NODE (qual_wfl));
10155 /* Maybe what we're trying to access to is an inner
10156 class, only if decl is a TYPE_DECL. */
10157 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
10159 tree ptr, inner_decl;
10161 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
10162 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
10165 check_inner_class_access (inner_decl, decl, qual_wfl);
10166 type = TREE_TYPE (inner_decl);
10173 if (field_decl == NULL_TREE)
10175 parse_error_context
10176 (qual_wfl, "No variable %qs defined in type %qs",
10177 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10178 GET_TYPE_NAME (type));
10181 if (field_decl == error_mark_node)
10184 /* Layout the type of field_decl, since we may need
10185 it. Don't do primitive types or loaded classes. The
10186 situation of non primitive arrays may not handled
10187 properly here. FIXME */
10188 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10189 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
10191 field_decl_type = TREE_TYPE (field_decl);
10192 if (!JPRIMITIVE_TYPE_P (field_decl_type)
10193 && !CLASS_LOADED_P (field_decl_type)
10194 && !TYPE_ARRAY_P (field_decl_type))
10195 resolve_and_layout (field_decl_type, NULL_TREE);
10197 /* Check on accessibility here */
10198 if (not_accessible_p (current_class, field_decl,
10199 *type_found, from_super))
10200 return not_accessible_field_error (qual_wfl,field_decl);
10201 check_deprecation (qual_wfl, field_decl);
10203 /* There are things to check when fields are accessed
10204 from type. There are no restrictions on a static
10205 declaration of the field when it is accessed from an
10207 is_static = FIELD_STATIC (field_decl);
10208 if (!from_super && from_type
10209 && !TYPE_INTERFACE_P (type)
10211 && (current_function_decl
10212 && METHOD_STATIC (current_function_decl)))
10214 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
10217 from_cast = from_super = 0;
10219 /* If it's an access from a type but isn't static, we
10220 make it relative to `this'. */
10221 if (!is_static && from_type)
10222 decl = current_this;
10224 /* If we need to generate something to get a proper
10225 handle on what this field is accessed from, do it
10229 decl = maybe_access_field (decl, *where_found, *type_found);
10230 if (decl == error_mark_node)
10234 /* We want to keep the location where we found it, and the
10236 *where_found = decl;
10237 *type_found = type;
10239 /* Generate the correct expression for field access from
10241 if (from_qualified_this)
10244 = build_nested_field_access (qual_wfl, field_decl);
10245 from_qualified_this = 0;
10248 /* If needed, generate accessors for static field access. */
10250 && FIELD_PRIVATE (field_decl)
10251 && flag_emit_class_files
10252 && nested_field_access_p (current_class, field_decl))
10253 field_decl = build_nested_field_access (qual_wfl, field_decl);
10255 /* This is the decl found and eventually the next one to
10260 type = QUAL_DECL_TYPE (decl);
10262 /* Sneak preview. If decl is qualified by a `new', report
10263 the error here to be accurate on the peculiar construct */
10265 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
10266 && !JREFERENCE_TYPE_P (type))
10268 parse_error_context (qual_wfl, "Attempt to reference field %<new%> in a %qs",
10269 lang_printable_name (type, 0));
10273 /* `q' might have changed due to a after package resolution
10274 re-qualification */
10278 *found_decl = decl;
10282 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
10283 can't be accessed from REFERENCE (a record type). If MEMBER
10284 features a protected access, we then use WHERE which, if non null,
10285 holds the type of MEMBER's access that is checked against
10286 6.6.2.1. This function should be used when decl is a field or a
10290 not_accessible_p (tree reference, tree member, tree where, int from_super)
10292 int access_flag = get_access_flags_from_decl (member);
10293 bool is_static = false;
10295 if (TREE_CODE (member) == FIELD_DECL ||
10296 TREE_CODE (member) == VAR_DECL)
10297 is_static = FIELD_STATIC (member);
10299 is_static = METHOD_STATIC (member);
10301 /* Access always granted for members declared public */
10302 if (access_flag & ACC_PUBLIC)
10305 /* Check access on protected members */
10306 if (access_flag & ACC_PROTECTED)
10308 /* Access granted if it occurs from within the package
10309 containing the class in which the protected member is
10311 if (class_in_current_package (DECL_CONTEXT (member)))
10314 /* If accessed with the form `super.member', then access is granted */
10318 /* If WHERE is active, access was made through a qualifier. For
10319 non-static members, access is granted if the type of the qualifier
10320 is or is a sublass of the type the access is made from (6.6.2.1.) */
10321 if (where && !is_static)
10325 if (inherits_from_p (where, reference))
10327 if (INNER_CLASS_TYPE_P (reference))
10328 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10335 /* Otherwise, access is granted if occurring from within the class
10336 where member is declared, or a subclass of it. */
10339 if (inherits_from_p (reference, DECL_CONTEXT (member)))
10341 if (INNER_CLASS_TYPE_P (reference))
10342 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10349 /* Check access on private members. Access is granted only if it
10350 occurs from within the class in which it is declared -- that does
10351 it for innerclasses too. */
10352 if (access_flag & ACC_PRIVATE)
10354 if (reference == DECL_CONTEXT (member) ||
10355 common_enclosing_context_p (DECL_CONTEXT (member), reference))
10360 /* Default access is permitted only when occurring from within the
10361 package in which the context (MEMBER) is declared. */
10362 return !class_in_current_package (DECL_CONTEXT (member));
10365 /* Test deprecated decl access. */
10367 check_deprecation (tree wfl, tree decl)
10372 if (! warn_deprecated)
10375 /* We want to look at the element type of arrays here, so we strip
10376 all surrounding array types. */
10377 if (TYPE_ARRAY_P (TREE_TYPE (decl)))
10379 elt = TREE_TYPE (decl);
10380 while (TYPE_ARRAY_P (elt))
10381 elt = TYPE_ARRAY_ELEMENT (elt);
10382 /* We'll end up with a pointer type, so we use TREE_TYPE to go
10384 decl = TYPE_NAME (TREE_TYPE (elt));
10386 file = DECL_SOURCE_FILE (decl);
10388 /* Complain if the field is deprecated and the file it was defined
10389 in isn't compiled at the same time the file which contains its
10391 if (DECL_DEPRECATED (decl)
10392 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10395 switch (TREE_CODE (decl))
10397 case FUNCTION_DECL:
10405 parse_warning_context (wfl, "The class %qs has been deprecated",
10406 IDENTIFIER_POINTER (DECL_NAME (decl)));
10411 /* Don't issue a message if the context as been deprecated as a
10413 if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10414 parse_warning_context
10415 (wfl, "The %s %qs in class %qs has been deprecated",
10416 the, lang_printable_name (decl, 0),
10417 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10421 /* Returns 1 if class was declared in the current package, 0 otherwise */
10423 static GTY(()) tree cicp_cache;
10425 class_in_current_package (tree class)
10427 int qualified_flag;
10430 if (cicp_cache == class)
10433 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10435 /* If the current package is empty and the name of CLASS is
10436 qualified, class isn't in the current package. If there is a
10437 current package and the name of the CLASS is not qualified, class
10438 isn't in the current package */
10439 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10442 /* If there is not package and the name of CLASS isn't qualified,
10443 they belong to the same unnamed package */
10444 if (!ctxp->package && !qualified_flag)
10447 /* Compare the left part of the name of CLASS with the package name */
10448 split_qualified_name (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10449 if (ctxp->package == left)
10451 cicp_cache = class;
10457 /* This function may generate code to access DECL from WHERE. This is
10458 done only if certain conditions meet. */
10461 maybe_access_field (tree decl, tree where, tree type)
10463 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10464 && !FIELD_STATIC (decl))
10465 decl = build_field_ref (where ? where : current_this,
10466 (type ? type : DECL_CONTEXT (decl)),
10471 /* Build a method invocation, by patching PATCH. If non NULL
10472 and according to the situation, PRIMARY and WHERE may be
10473 used. IS_STATIC is set to 1 if the invoked function is static. */
10476 patch_method_invocation (tree patch, tree primary, tree where, int from_super,
10477 int *is_static, tree *ret_decl)
10479 tree wfl = TREE_OPERAND (patch, 0);
10480 tree args = TREE_OPERAND (patch, 1);
10481 tree name = EXPR_WFL_NODE (wfl);
10483 int is_static_flag = 0;
10484 int is_super_init = 0;
10485 tree this_arg = NULL_TREE;
10486 int is_array_clone_call = 0;
10488 /* Should be overridden if everything goes well. Otherwise, if
10489 something fails, it should keep this value. It stop the
10490 evaluation of a bogus assignment. See java_complete_tree,
10491 MODIFY_EXPR: for the reasons why we sometimes want to keep on
10492 evaluating an assignment */
10493 TREE_TYPE (patch) = error_mark_node;
10495 /* Since lookup functions are messing with line numbers, save the
10497 java_parser_context_save_global ();
10499 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10501 /* Resolution of qualified name, excluding constructors */
10502 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10504 tree identifier, identifier_wfl, type, resolved;
10505 /* Extract the last IDENTIFIER of the qualified
10506 expression. This is a wfl and we will use it's location
10507 data during error report. */
10508 identifier_wfl = cut_identifier_in_qualified (wfl);
10509 identifier = EXPR_WFL_NODE (identifier_wfl);
10511 /* Given the context, IDENTIFIER is syntactically qualified
10512 as a MethodName. We need to qualify what's before */
10513 qualify_ambiguous_name (wfl);
10514 resolved = resolve_field_access (wfl, NULL, NULL);
10516 if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10517 && FIELD_FINAL (resolved)
10518 && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10519 && !flag_emit_class_files)
10520 resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10522 if (resolved == error_mark_node)
10523 PATCH_METHOD_RETURN_ERROR ();
10525 type = GET_SKIP_TYPE (resolved);
10526 resolve_and_layout (type, NULL_TREE);
10528 if (JPRIMITIVE_TYPE_P (type))
10530 parse_error_context
10532 "Can't invoke a method on primitive type %qs",
10533 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10534 PATCH_METHOD_RETURN_ERROR ();
10537 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10538 args = nreverse (args);
10540 /* We're resolving a call from a type */
10541 if (TREE_CODE (resolved) == TYPE_DECL)
10543 if (CLASS_INTERFACE (resolved))
10545 parse_error_context
10547 "Can't make static reference to method %qs in interface %qs",
10548 IDENTIFIER_POINTER (identifier),
10549 IDENTIFIER_POINTER (name));
10550 PATCH_METHOD_RETURN_ERROR ();
10552 if (list && !METHOD_STATIC (list))
10554 char *fct_name = xstrdup (lang_printable_name (list, 2));
10555 parse_error_context
10557 "Can't make static reference to method %<%s %s%> in class %qs",
10558 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10559 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10561 PATCH_METHOD_RETURN_ERROR ();
10565 this_arg = primary = resolved;
10567 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10568 is_array_clone_call = 1;
10570 /* IDENTIFIER_WFL will be used to report any problem further */
10571 wfl = identifier_wfl;
10573 /* Resolution of simple names, names generated after a primary: or
10577 tree class_to_search = NULL_TREE;
10578 int lc; /* Looking for Constructor */
10580 /* We search constructor in their target class */
10581 if (CALL_CONSTRUCTOR_P (patch))
10583 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10584 class_to_search = EXPR_WFL_NODE (wfl);
10585 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10586 this_identifier_node)
10587 class_to_search = NULL_TREE;
10588 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10589 super_identifier_node)
10592 if (CLASSTYPE_SUPER (current_class))
10594 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10597 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10598 PATCH_METHOD_RETURN_ERROR ();
10602 /* Class to search is NULL if we're searching the current one */
10603 if (class_to_search)
10605 class_to_search = resolve_and_layout (class_to_search, wfl);
10607 if (!class_to_search)
10609 parse_error_context
10610 (wfl, "Class %qs not found in type declaration",
10611 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10612 PATCH_METHOD_RETURN_ERROR ();
10615 /* Can't instantiate an abstract class, but we can
10616 invoke it's constructor. It's use within the `new'
10617 context is denied here. */
10618 if (CLASS_ABSTRACT (class_to_search)
10619 && TREE_CODE (patch) == NEW_CLASS_EXPR)
10621 parse_error_context
10622 (wfl, "Class %qs is an abstract class. It can't be instantiated",
10623 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10624 PATCH_METHOD_RETURN_ERROR ();
10627 class_to_search = TREE_TYPE (class_to_search);
10630 class_to_search = current_class;
10633 /* This is a regular search in the local class, unless an
10634 alternate class is specified. */
10637 if (where != NULL_TREE)
10638 class_to_search = where;
10639 else if (QUALIFIED_P (name))
10640 class_to_search = current_class;
10643 class_to_search = current_class;
10647 if (has_method (class_to_search, name))
10649 if (! INNER_CLASS_TYPE_P (class_to_search))
10651 parse_error_context (wfl,
10652 "No method named %qs in scope",
10653 IDENTIFIER_POINTER (name));
10654 PATCH_METHOD_RETURN_ERROR ();
10657 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10663 /* NAME is a simple identifier or comes from a primary. Search
10664 in the class whose declaration contain the method being
10666 resolve_and_layout (class_to_search, NULL_TREE);
10668 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10669 /* Don't continue if no method were found, as the next statement
10670 can't be executed then. */
10672 PATCH_METHOD_RETURN_ERROR ();
10674 if (TYPE_ARRAY_P (class_to_search)
10675 && DECL_NAME (list) == get_identifier ("clone"))
10676 is_array_clone_call = 1;
10678 /* Check for static reference if non static methods */
10679 if (check_for_static_method_reference (wfl, patch, list,
10680 class_to_search, primary))
10681 PATCH_METHOD_RETURN_ERROR ();
10683 /* Check for inner classes creation from illegal contexts */
10684 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10685 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10686 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10687 && !DECL_INIT_P (current_function_decl))
10689 parse_error_context
10690 (wfl, "No enclosing instance for inner class %qs is in scope%s",
10691 lang_printable_name (class_to_search, 0),
10692 (!current_this ? "" :
10693 "; an explicit one must be provided when creating this inner class"));
10694 PATCH_METHOD_RETURN_ERROR ();
10697 /* Non static methods are called with the current object extra
10698 argument. If patch a `new TYPE()', the argument is the value
10699 returned by the object allocator. If method is resolved as a
10700 primary, use the primary otherwise use the current THIS. */
10701 args = nreverse (args);
10702 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10704 this_arg = primary ? primary : current_this;
10706 /* If we're using an access method, things are different.
10707 There are two family of cases:
10709 1) We're not generating bytecodes:
10711 - LIST is non static. It's invocation is transformed from
10712 x(a1,...,an) into this$<n>.x(a1,....an).
10713 - LIST is static. It's invocation is transformed from
10714 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10716 2) We're generating bytecodes:
10718 - LIST is non static. It's invocation is transformed from
10719 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10720 - LIST is static. It's invocation is transformed from
10721 x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10723 Of course, this$<n> can be arbitrarily complex, ranging from
10724 this$0 (the immediate outer context) to
10725 access$0(access$0(...(this$0))).
10727 maybe_use_access_method returns a nonzero value if the
10728 this_arg has to be moved into the (then generated) stub
10729 argument list. In the meantime, the selected function
10730 might have be replaced by a generated stub. */
10732 maybe_use_access_method (is_super_init, &list, &this_arg))
10734 args = tree_cons (NULL_TREE, this_arg, args);
10735 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10740 /* Merge point of all resolution schemes. If we have nothing, this
10741 is an error, already signaled */
10743 PATCH_METHOD_RETURN_ERROR ();
10745 /* Check accessibility, position the is_static flag, build and
10747 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10748 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10749 NULL_TREE), from_super)
10750 /* Calls to clone() on array types are permitted as a special-case. */
10751 && !is_array_clone_call)
10753 const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10754 const char *const access =
10755 accessibility_string (get_access_flags_from_decl (list));
10756 const char *const klass =
10757 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10758 const char *const refklass =
10759 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10760 const char *const what = (DECL_CONSTRUCTOR_P (list)
10761 ? "constructor" : "method");
10762 parse_error_context (wfl,
10763 "Can't access %s %s %<%s.%s%> from %qs",
10764 access, what, klass, fct_name, refklass);
10765 PATCH_METHOD_RETURN_ERROR ();
10768 /* Deprecation check: check whether the method being invoked or the
10769 instance-being-created's type are deprecated. */
10770 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10771 check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10772 check_deprecation (wfl, list);
10774 /* If invoking a innerclass constructor, there are hidden parameters
10776 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10777 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10779 /* And make sure we add the accessed local variables to be saved
10780 in field aliases. */
10781 args = build_alias_initializer_parameter_list
10782 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10784 /* Secretly pass the current_this/primary as a second argument */
10785 if (primary || current_this)
10788 tree this_type = (current_this ?
10789 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10790 /* Method's (list) enclosing context */
10791 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10792 /* If we have a primary, use it. */
10794 extra_arg = primary;
10795 /* The current `this' is an inner class but isn't a direct
10796 enclosing context for the inner class we're trying to
10797 create. Build an access to the proper enclosing context
10799 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10800 && this_type != TREE_TYPE (mec))
10803 extra_arg = build_access_to_thisn (current_class,
10804 TREE_TYPE (mec), 0);
10805 extra_arg = java_complete_tree (extra_arg);
10807 /* Otherwise, just use the current `this' as an enclosing
10810 extra_arg = current_this;
10811 args = tree_cons (NULL_TREE, extra_arg, args);
10814 args = tree_cons (NULL_TREE, integer_zero_node, args);
10817 /* This handles the situation where a constructor invocation needs
10818 to have an enclosing context passed as a second parameter (the
10819 constructor is one of an inner class). */
10820 if ((is_super_init ||
10821 (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10822 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10824 tree dest = TYPE_NAME (DECL_CONTEXT (list));
10826 build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10827 extra_arg = java_complete_tree (extra_arg);
10828 args = tree_cons (NULL_TREE, extra_arg, args);
10831 is_static_flag = METHOD_STATIC (list);
10832 if (! is_static_flag && this_arg != NULL_TREE)
10833 args = tree_cons (NULL_TREE, this_arg, args);
10835 /* In the context of an explicit constructor invocation, we can't
10836 invoke any method relying on `this'. Exceptions are: we're
10837 invoking a static function, primary exists and is not the current
10838 this, we're creating a new object. */
10839 if (ctxp->explicit_constructor_p
10841 && (!primary || primary == current_this)
10842 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10844 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
10845 PATCH_METHOD_RETURN_ERROR ();
10847 java_parser_context_restore_global ();
10849 *is_static = is_static_flag;
10850 /* Sometimes, we want the decl of the selected method. Such as for
10854 patch = patch_invoke (patch, list, args);
10856 /* Now is a good time to insert the call to finit$ */
10857 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10859 tree finit_parms, finit_call;
10861 /* Prepare to pass hidden parameters to finit$, if any. */
10862 finit_parms = build_alias_initializer_parameter_list
10863 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10866 build_method_invocation (build_wfl_node (finit_identifier_node),
10869 /* Generate the code used to initialize fields declared with an
10870 initialization statement and build a compound statement along
10871 with the super constructor invocation. */
10872 CAN_COMPLETE_NORMALLY (patch) = 1;
10873 patch = build2 (COMPOUND_EXPR, void_type_node, patch,
10874 java_complete_tree (finit_call));
10879 /* Check that we're not trying to do a static reference to a method in
10880 non static method. Return 1 if it's the case, 0 otherwise. */
10883 check_for_static_method_reference (tree wfl, tree node, tree method,
10884 tree where, tree primary)
10886 if (METHOD_STATIC (current_function_decl)
10887 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10889 char *fct_name = xstrdup (lang_printable_name (method, 0));
10890 parse_error_context
10891 (wfl, "Can't make static reference to method %<%s %s%> in class %qs",
10892 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10893 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10900 /* Fix the invocation of *MDECL if necessary in the case of a
10901 invocation from an inner class. *THIS_ARG might be modified
10902 appropriately and an alternative access to *MDECL might be
10906 maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10909 tree md = *mdecl, ta = *this_arg;
10911 int non_static_context = !METHOD_STATIC (md);
10914 || DECL_CONTEXT (md) == current_class
10915 || !PURE_INNER_CLASS_TYPE_P (current_class)
10916 || DECL_FINIT_P (md)
10917 || DECL_INSTINIT_P (md))
10920 /* If we're calling a method found in an enclosing class, generate
10921 what it takes to retrieve the right this. Don't do that if we're
10922 invoking a static method. Note that if MD's type is unrelated to
10923 CURRENT_CLASS, then the current this can be used. */
10925 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10927 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10928 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10930 ta = build_current_thisn (current_class);
10931 ta = build_wfl_node (ta);
10938 maybe_build_thisn_access_method (type);
10939 if (inherits_from_p (type, DECL_CONTEXT (md)))
10941 ta = build_access_to_thisn (ctx, type, 0);
10944 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10945 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10948 ta = java_complete_tree (ta);
10951 /* We might have to use an access method to get to MD. We can
10952 break the method access rule as far as we're not generating
10954 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10956 md = build_outer_method_access_method (md);
10963 /* Returning a nonzero value indicates we were doing a non static
10964 method invocation that is now a static invocation. It will have
10965 callee displace `this' to insert it in the regular argument
10967 return (non_static_context && to_return);
10970 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10974 patch_invoke (tree patch, tree method, tree args)
10977 tree original_call, t, ta;
10978 tree check = NULL_TREE;
10980 /* Last step for args: convert build-in types. If we're dealing with
10981 a new TYPE() type call, the first argument to the constructor
10982 isn't found in the incoming argument list, but delivered by
10984 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10985 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10986 t = TREE_CHAIN (t);
10987 for (ta = args; t != end_params_node && ta;
10988 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10989 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10990 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10991 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10993 /* Resolve unresolved returned type issues */
10994 t = TREE_TYPE (TREE_TYPE (method));
10995 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10996 resolve_and_layout (TREE_TYPE (t), NULL);
10998 if (flag_emit_class_files)
11002 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
11004 case INVOKE_VIRTUAL:
11005 dtable = invoke_build_dtable (0, args);
11006 func = build_invokevirtual (dtable, method);
11009 case INVOKE_NONVIRTUAL:
11010 /* If the object for the method call is null, we throw an
11011 exception. We don't do this if the object is the current
11012 method's `this'. In other cases we just rely on an
11013 optimization pass to eliminate redundant checks. */
11014 if (TREE_VALUE (args) != current_this)
11016 /* We use a save_expr here to make sure we only evaluate
11017 the new `self' expression once. */
11018 tree save_arg = save_expr (TREE_VALUE (args));
11019 TREE_VALUE (args) = save_arg;
11020 check = java_check_reference (save_arg, 1);
11022 /* Fall through. */
11025 case INVOKE_STATIC:
11027 tree signature = build_java_signature (TREE_TYPE (method));
11028 func = build_known_method_ref (method, TREE_TYPE (method),
11029 DECL_CONTEXT (method),
11034 case INVOKE_INTERFACE:
11035 dtable = invoke_build_dtable (1, args);
11036 func = build_invokeinterface (dtable, method);
11043 /* Ensure self_type is initialized, (invokestatic). FIXME */
11044 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
11047 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
11048 TREE_OPERAND (patch, 0) = func;
11049 TREE_OPERAND (patch, 1) = args;
11050 patch = check_for_builtin (method, patch);
11051 original_call = patch;
11053 /* We're processing a `new TYPE ()' form. New is called and its
11054 returned value is the first argument to the constructor. We build
11055 a COMPOUND_EXPR and use saved expression so that the overall NEW
11056 expression value is a pointer to a newly created and initialized
11058 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
11060 tree class = DECL_CONTEXT (method);
11061 tree c1, saved_new, new;
11064 if (flag_emit_class_files)
11066 TREE_TYPE (patch) = build_pointer_type (class);
11069 if (!TYPE_SIZE (class))
11070 safe_layout_class (class);
11072 (class_has_finalize_method (class) ? alloc_object_node
11073 : alloc_no_finalizer_node);
11074 new = build3 (CALL_EXPR, promote_type (class),
11075 build_address_of (alloc_node),
11076 build_tree_list (NULL_TREE, build_class_ref (class)),
11078 saved_new = save_expr (new);
11079 c1 = build_tree_list (NULL_TREE, saved_new);
11080 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
11081 TREE_OPERAND (original_call, 1) = c1;
11082 TREE_SET_CODE (original_call, CALL_EXPR);
11083 patch = build2 (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
11086 /* If CHECK is set, then we are building a check to see if the object
11088 if (check != NULL_TREE)
11090 /* We have to call force_evaluation_order now because creating a
11091 COMPOUND_EXPR wraps the arg list in a way that makes it
11092 unrecognizable by force_evaluation_order later. Yuk. */
11093 patch = build2 (COMPOUND_EXPR, TREE_TYPE (patch), check,
11094 force_evaluation_order (patch));
11095 TREE_SIDE_EFFECTS (patch) = 1;
11098 /* In order to be able to modify PATCH later, we SAVE_EXPR it and
11099 put it as the first expression of a COMPOUND_EXPR. The second
11100 expression being an empty statement to be later patched if
11101 necessary. We remember a TREE_LIST (the PURPOSE is the method,
11102 the VALUE is the compound) in a hashtable and return a
11103 COMPOUND_EXPR built so that the result of the evaluation of the
11104 original PATCH node is returned. */
11105 if (STATIC_CLASS_INIT_OPT_P ()
11106 && current_function_decl && METHOD_STATIC (method))
11109 tree fndecl = current_function_decl;
11110 /* We have to call force_evaluation_order now because creating a
11111 COMPOUND_EXPR wraps the arg list in a way that makes it
11112 unrecognizable by force_evaluation_order later. Yuk. */
11113 tree save = force_evaluation_order (patch);
11114 tree type = TREE_TYPE (patch);
11116 patch = build2 (COMPOUND_EXPR, type, save, build_java_empty_stmt ());
11117 list = tree_cons (method, patch,
11118 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
11120 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
11122 patch = build2 (COMPOUND_EXPR, type, patch, save);
11129 invocation_mode (tree method, int super)
11131 int access = get_access_flags_from_decl (method);
11134 return INVOKE_SUPER;
11136 if (access & ACC_STATIC)
11137 return INVOKE_STATIC;
11139 /* We have to look for a constructor before we handle nonvirtual
11140 calls; otherwise the constructor will look nonvirtual. */
11141 if (DECL_CONSTRUCTOR_P (method))
11142 return INVOKE_STATIC;
11144 if (access & ACC_PRIVATE)
11145 return INVOKE_NONVIRTUAL;
11147 /* Binary compatibility: just because it's final today, that doesn't
11148 mean it'll be final tomorrow. */
11149 if (! flag_indirect_dispatch
11150 || DECL_CONTEXT (method) == object_type_node)
11152 if (access & ACC_FINAL)
11153 return INVOKE_NONVIRTUAL;
11155 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
11156 return INVOKE_NONVIRTUAL;
11159 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
11160 return INVOKE_INTERFACE;
11162 return INVOKE_VIRTUAL;
11165 /* Retrieve a refined list of matching methods. It covers the step
11166 15.11.2 (Compile-Time Step 2) */
11169 lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
11171 tree atl = end_params_node; /* Arg Type List */
11172 tree method, signature, list, node;
11173 const char *candidates; /* Used for error report */
11176 /* Fix the arguments */
11177 for (node = arg_list; node; node = TREE_CHAIN (node))
11179 tree current_arg = TREE_TYPE (TREE_VALUE (node));
11180 /* Non primitive type may have to be resolved */
11181 if (!JPRIMITIVE_TYPE_P (current_arg))
11182 resolve_and_layout (current_arg, NULL_TREE);
11184 if (TREE_CODE (current_arg) == RECORD_TYPE)
11185 current_arg = promote_type (current_arg);
11186 atl = tree_cons (NULL_TREE, current_arg, atl);
11189 /* Presto. If we're dealing with an anonymous class and a
11190 constructor call, generate the right constructor now, since we
11191 know the arguments' types. */
11193 if (lc && ANONYMOUS_CLASS_P (class))
11195 tree mdecl = craft_constructor (TYPE_NAME (class), atl);
11196 /* The anonymous class may have already been laid out, so make sure
11197 the new constructor is laid out here. */
11198 layout_class_method (class, CLASSTYPE_SUPER (class), mdecl, NULL_TREE);
11201 /* Find all candidates and then refine the list, searching for the
11202 most specific method. */
11203 list = find_applicable_accessible_methods_list (lc, class, name, atl);
11204 list = find_most_specific_methods_list (list);
11205 if (list && !TREE_CHAIN (list))
11206 return TREE_VALUE (list);
11208 /* Issue an error. List candidates if any. Candidates are listed
11209 only if accessible (non accessible methods may end-up here for
11210 the sake of a better error report). */
11215 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
11216 for (current = list; current; current = TREE_CHAIN (current))
11218 tree cm = TREE_VALUE (current);
11219 char string [4096];
11220 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
11223 (string, " '%s' in '%s'%s",
11224 get_printable_method_name (cm),
11225 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
11226 (TREE_CHAIN (current) ? "\n" : ""));
11227 obstack_grow (&temporary_obstack, string, strlen (string));
11229 obstack_1grow (&temporary_obstack, '\0');
11230 candidates = obstack_finish (&temporary_obstack);
11232 /* Issue the error message */
11233 method = make_node (FUNCTION_TYPE);
11234 TYPE_ARG_TYPES (method) = atl;
11235 signature = build_java_argument_signature (method);
11236 dup = xstrdup (lang_printable_name (class, 0));
11237 parse_error_context (cl, "Can't find %s %<%s(%s)%> in type %qs%s",
11238 (lc ? "constructor" : "method"),
11239 (lc ? dup : IDENTIFIER_POINTER (name)),
11240 IDENTIFIER_POINTER (signature), dup,
11241 (candidates ? candidates : ""));
11246 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
11247 when we're looking for a constructor. */
11250 find_applicable_accessible_methods_list (int lc, tree class, tree name,
11253 static htab_t searched_classes;
11254 static int search_not_done = 0;
11255 tree list = NULL_TREE, all_list = NULL_TREE;
11259 /* Check the hash table to determine if this class has been searched
11261 if (searched_classes)
11263 if (htab_find (searched_classes, class) != NULL)
11268 searched_classes = htab_create (10, htab_hash_pointer,
11269 htab_eq_pointer, NULL);
11273 *htab_find_slot (searched_classes, class, INSERT) = class;
11275 if (!CLASS_LOADED_P (class))
11277 load_class (class, 1);
11278 safe_layout_class (class);
11281 /* Search interfaces */
11282 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
11283 && CLASS_INTERFACE (TYPE_NAME (class)))
11285 search_applicable_methods_list (lc, TYPE_METHODS (class),
11286 name, arglist, &list, &all_list);
11287 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11289 tree t = BINFO_TYPE (base_binfo);
11292 rlist = find_applicable_accessible_methods_list (lc, t, name,
11294 list = chainon (rlist, list);
11297 /* Search classes */
11300 search_applicable_methods_list (lc, TYPE_METHODS (class),
11301 name, arglist, &list, &all_list);
11303 /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11304 that we only search in class. Note that we should have found
11305 something at this point. */
11306 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11313 /* We must search all interfaces of this class */
11317 BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11319 tree t = BINFO_TYPE (base_binfo);
11320 if (t != object_type_node)
11323 = find_applicable_accessible_methods_list (lc, t,
11325 list = chainon (rlist, list);
11330 /* Search superclass */
11331 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11334 class = CLASSTYPE_SUPER (class);
11335 rlist = find_applicable_accessible_methods_list (lc, class,
11337 list = chainon (rlist, list);
11343 /* We're done. Reset the searched classes list and finally search
11344 java.lang.Object if it wasn't searched already. */
11345 if (!search_not_done)
11348 && TYPE_METHODS (object_type_node)
11349 && htab_find (searched_classes, object_type_node) == NULL)
11351 search_applicable_methods_list (lc,
11352 TYPE_METHODS (object_type_node),
11353 name, arglist, &list, &all_list);
11355 htab_delete (searched_classes);
11356 searched_classes = NULL;
11359 /* Either return the list obtained or all selected (but
11360 inaccessible) methods for better error report. */
11361 return (!list ? all_list : list);
11364 /* Effectively search for the appropriate method in method */
11367 search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
11368 tree *list, tree *all_list)
11370 for (; method; method = TREE_CHAIN (method))
11372 /* When dealing with constructor, stop here, otherwise search
11374 if (lc && !DECL_CONSTRUCTOR_P (method))
11376 else if (!lc && (DECL_CONSTRUCTOR_P (method)
11377 || (DECL_NAME (method) != name)))
11380 if (argument_types_convertible (method, arglist))
11382 /* Retain accessible methods only */
11383 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11384 method, NULL_TREE, 0))
11385 *list = tree_cons (NULL_TREE, method, *list);
11387 /* Also retain all selected method here */
11388 *all_list = tree_cons (NULL_TREE, method, *list);
11393 /* 15.11.2.2 Choose the Most Specific Method */
11396 find_most_specific_methods_list (tree list)
11399 int abstract, candidates;
11400 tree current, new_list = NULL_TREE;
11401 for (current = list; current; current = TREE_CHAIN (current))
11404 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11406 for (method = list; method; method = TREE_CHAIN (method))
11408 tree method_v, current_v;
11409 /* Don't test a method against itself */
11410 if (method == current)
11413 method_v = TREE_VALUE (method);
11414 current_v = TREE_VALUE (current);
11416 /* Compare arguments and location where methods where declared */
11417 if (argument_types_convertible (method_v, current_v))
11419 if (valid_method_invocation_conversion_p
11420 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v)))
11422 int v = (DECL_SPECIFIC_COUNT (current_v) += 1);
11423 max = (v > max ? v : max);
11429 /* Review the list and select the maximally specific methods */
11430 for (current = list, abstract = -1, candidates = -1;
11431 current; current = TREE_CHAIN (current))
11432 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11434 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11435 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11439 /* If we have several and they're all abstract, just pick the
11441 if (candidates > 0 && candidates == abstract)
11443 /* FIXME: merge the throws clauses. There is no convenient way
11444 to do this in gcj right now, since ideally we'd like to
11445 introduce a new METHOD_DECL here, but that is really not
11447 new_list = nreverse (new_list);
11448 TREE_CHAIN (new_list) = NULL_TREE;
11452 /* We have several (we couldn't find a most specific), all but one
11453 are abstract, we pick the only non abstract one. */
11454 if (candidates > 0 && (candidates == abstract+1))
11456 for (current = new_list; current; current = TREE_CHAIN (current))
11457 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11459 TREE_CHAIN (current) = NULL_TREE;
11460 new_list = current;
11464 /* If we can't find one, lower expectations and try to gather multiple
11465 maximally specific methods */
11466 while (!new_list && max)
11470 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11471 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11478 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11479 converted by method invocation conversion (5.3) to the type of the
11480 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11481 to change less often than M1. */
11483 static GTY(()) tree m2_arg_value;
11484 static GTY(()) tree m2_arg_cache;
11487 argument_types_convertible (tree m1, tree m2_or_arglist)
11489 tree m1_arg, m2_arg;
11491 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11493 if (m2_arg_value == m2_or_arglist)
11494 m2_arg = m2_arg_cache;
11497 /* M2_OR_ARGLIST can be a function DECL or a raw list of
11499 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11501 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11502 if (!METHOD_STATIC (m2_or_arglist))
11503 m2_arg = TREE_CHAIN (m2_arg);
11506 m2_arg = m2_or_arglist;
11508 m2_arg_value = m2_or_arglist;
11509 m2_arg_cache = m2_arg;
11512 while (m1_arg != end_params_node && m2_arg != end_params_node)
11514 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11515 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11516 TREE_VALUE (m2_arg)))
11518 m1_arg = TREE_CHAIN (m1_arg);
11519 m2_arg = TREE_CHAIN (m2_arg);
11521 return m1_arg == end_params_node && m2_arg == end_params_node;
11524 /* Qualification routines */
11526 /* Given a name x.y.z, look up x locally. If it's found, save the
11527 decl. If it's not found, mark the name as RESOLVE_PACKAGE_NAME_P,
11528 so that we later try and load the appropriate classes. */
11530 qualify_ambiguous_name (tree id)
11534 /* We inspect the first item of the qualification list. As a sanity
11535 check, make sure that it is an identfier node. */
11536 tree qual = EXPR_WFL_QUALIFICATION (id);
11537 tree qual_wfl = QUAL_WFL (qual);
11539 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11542 name = EXPR_WFL_NODE (qual_wfl);
11544 /* If we don't have an identifier, or we have a 'this' or 'super',
11545 then field access processing is all we need : there is nothing
11547 if (!name || TREE_CODE (name) != IDENTIFIER_NODE ||
11548 name == this_identifier_node ||
11549 name == super_identifier_node)
11552 /* If name appears within the scope of a local variable declaration
11553 or parameter declaration, or is a field within an enclosing
11554 class, then it is an expression name. Save the decl and let
11555 resolve_field_access do it's work. */
11556 if ((decl = IDENTIFIER_LOCAL_VALUE (name)) ||
11557 (decl = lookup_field_wrapper (current_class, name)))
11559 QUAL_RESOLUTION (qual) = decl;
11563 /* If name is a known class name (either declared or imported), mark
11564 us as a type name. */
11565 if ((decl = resolve_and_layout (name, NULL_TREE)))
11567 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11568 QUAL_RESOLUTION (qual) = decl;
11571 /* Check here that NAME isn't declared by more than one
11572 type-import-on-demand declaration of the compilation unit
11573 containing NAME. FIXME */
11575 /* We couldn't find a declaration for the name. Assume for now that
11576 we have a qualified class name that needs to be loaded from an
11577 external class file. */
11579 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11581 /* Propagate the qualification across other components of the
11583 for (qual = TREE_CHAIN (qual); qual;
11584 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11586 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11587 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11590 /* Store the global qualification for the ambiguous part of ID back
11592 if (RESOLVE_TYPE_NAME_P (qual_wfl))
11593 RESOLVE_TYPE_NAME_P (id) = 1;
11594 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11595 RESOLVE_PACKAGE_NAME_P (id) = 1;
11598 /* Patch tree nodes in a function body. When a BLOCK is found, push
11599 local variable decls if present.
11600 Same as java_complete_lhs, but does resolve static finals to values. */
11603 java_complete_tree (tree node)
11605 node = java_complete_lhs (node);
11606 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11607 && DECL_INITIAL (node) != NULL_TREE)
11609 tree value = fold_constant_for_init (node, node);
11610 if (value != NULL_TREE)
11617 java_stabilize_reference (tree node)
11619 if (TREE_CODE (node) == COMPOUND_EXPR)
11621 tree op0 = TREE_OPERAND (node, 0);
11622 tree op1 = TREE_OPERAND (node, 1);
11623 TREE_OPERAND (node, 0) = save_expr (op0);
11624 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11627 return stabilize_reference (node);
11630 /* Patch tree nodes in a function body. When a BLOCK is found, push
11631 local variable decls if present.
11632 Same as java_complete_tree, but does not resolve static finals to values. */
11635 java_complete_lhs (tree node)
11637 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11640 /* CONVERT_EXPR always has its type set, even though it needs to be
11642 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11645 /* The switch block implements cases processing container nodes
11646 first. Contained nodes are always written back. Leaves come
11647 next and return a value. */
11648 switch (TREE_CODE (node))
11652 /* 1- Block section.
11653 Set the local values on decl names so we can identify them
11654 faster when they're referenced. At that stage, identifiers
11655 are legal so we don't check for declaration errors. */
11656 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11658 DECL_CONTEXT (cn) = current_function_decl;
11659 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11661 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11662 CAN_COMPLETE_NORMALLY (node) = 1;
11665 tree stmt = BLOCK_EXPR_BODY (node);
11667 int error_seen = 0;
11668 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11670 /* Re-order from (((A; B); C); ...; Z) to
11671 (A; (B; (C ; (...; Z)))).
11672 This makes it easier to scan the statements left-to-right
11673 without using recursion (which might overflow the stack
11674 if the block has many statements. */
11677 tree left = TREE_OPERAND (stmt, 0);
11678 if (TREE_CODE (left) != COMPOUND_EXPR)
11680 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11681 TREE_OPERAND (left, 1) = stmt;
11684 BLOCK_EXPR_BODY (node) = stmt;
11687 /* Now do the actual complete, without deep recursion for
11689 ptr = &BLOCK_EXPR_BODY (node);
11690 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11691 && !IS_EMPTY_STMT (TREE_OPERAND (*ptr, 1)))
11693 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11694 tree *next = &TREE_OPERAND (*ptr, 1);
11695 TREE_OPERAND (*ptr, 0) = cur;
11696 if (IS_EMPTY_STMT (cur))
11698 /* Optimization; makes it easier to detect empty bodies.
11699 Most useful for <clinit> with all-constant initializer. */
11703 if (TREE_CODE (cur) == ERROR_MARK)
11705 else if (! CAN_COMPLETE_NORMALLY (cur))
11710 if (TREE_CODE (wfl_op2) == BLOCK)
11711 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11712 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11713 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11717 if (TREE_CODE (wfl_op2) != CASE_EXPR
11718 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11719 unreachable_stmt_error (*ptr);
11721 if (TREE_TYPE (*ptr) == NULL_TREE)
11722 TREE_TYPE (*ptr) = void_type_node;
11725 *ptr = java_complete_tree (*ptr);
11727 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11728 return error_mark_node;
11729 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11731 /* Turn local bindings to null */
11732 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11733 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11735 TREE_TYPE (node) = void_type_node;
11738 /* 2- They are expressions but ultimately deal with statements */
11741 wfl_op1 = TREE_OPERAND (node, 0);
11742 COMPLETE_CHECK_OP_0 (node);
11743 /* 14.19 A throw statement cannot complete normally. */
11744 CAN_COMPLETE_NORMALLY (node) = 0;
11745 return patch_throw_statement (node, wfl_op1);
11747 case SYNCHRONIZED_EXPR:
11748 wfl_op1 = TREE_OPERAND (node, 0);
11749 return patch_synchronized_statement (node, wfl_op1);
11752 return patch_try_statement (node);
11754 case TRY_FINALLY_EXPR:
11755 COMPLETE_CHECK_OP_0 (node);
11756 COMPLETE_CHECK_OP_1 (node);
11757 if (IS_EMPTY_STMT (TREE_OPERAND (node, 0)))
11758 /* Reduce try/finally nodes with an empty try block. */
11759 return TREE_OPERAND (node, 1);
11760 if (IS_EMPTY_STMT (TREE_OPERAND (node, 1)))
11761 /* Likewise for an empty finally block. */
11762 return TREE_OPERAND (node, 0);
11763 CAN_COMPLETE_NORMALLY (node)
11764 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11765 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11766 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11769 case LABELED_BLOCK_EXPR:
11770 PUSH_LABELED_BLOCK (node);
11771 if (LABELED_BLOCK_BODY (node))
11772 COMPLETE_CHECK_OP_1 (node);
11773 TREE_TYPE (node) = void_type_node;
11774 POP_LABELED_BLOCK ();
11776 if (IS_EMPTY_STMT (LABELED_BLOCK_BODY (node)))
11778 LABELED_BLOCK_BODY (node) = NULL_TREE;
11779 CAN_COMPLETE_NORMALLY (node) = 1;
11781 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11782 CAN_COMPLETE_NORMALLY (node) = 1;
11785 case EXIT_BLOCK_EXPR:
11786 return patch_bc_statement (node);
11789 cn = java_complete_tree (TREE_OPERAND (node, 0));
11790 if (cn == error_mark_node)
11793 /* First, the case expression must be constant. Values of final
11794 fields are accepted. */
11795 nn = fold_constant_for_init (cn, NULL_TREE);
11796 if (nn != NULL_TREE)
11800 if ((TREE_CODE (cn) == COMPOUND_EXPR
11801 || TREE_CODE (cn) == COMPONENT_REF)
11802 && JDECL_P (TREE_OPERAND (cn, 1))
11803 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11804 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11806 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11807 TREE_OPERAND (cn, 1));
11809 /* Accept final locals too. */
11810 else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn)
11811 && DECL_INITIAL (cn))
11812 cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11814 if (!TREE_CONSTANT (cn))
11816 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11817 parse_error_context (node, "Constant expression required");
11818 return error_mark_node;
11821 nn = ctxp->current_loop;
11823 /* It must be assignable to the type of the switch expression. */
11824 if (!try_builtin_assignconv (NULL_TREE,
11825 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11827 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11828 parse_error_context
11830 "Incompatible type for case. Can't convert %qs to %<int%>",
11831 lang_printable_name (TREE_TYPE (cn), 0));
11832 return error_mark_node;
11835 cn = fold (convert (int_type_node, cn));
11836 TREE_CONSTANT_OVERFLOW (cn) = 0;
11837 CAN_COMPLETE_NORMALLY (cn) = 1;
11839 /* Save the label on a list so that we can later check for
11841 case_label_list = tree_cons (node, cn, case_label_list);
11843 /* Multiple instance of a case label bearing the same value is
11844 checked later. The case expression is all right so far. */
11845 if (TREE_CODE (cn) == VAR_DECL)
11846 cn = DECL_INITIAL (cn);
11847 TREE_OPERAND (node, 0) = cn;
11848 TREE_TYPE (node) = void_type_node;
11849 CAN_COMPLETE_NORMALLY (node) = 1;
11850 TREE_SIDE_EFFECTS (node) = 1;
11854 nn = ctxp->current_loop;
11855 /* Only one default label is allowed per switch statement */
11856 if (SWITCH_HAS_DEFAULT (nn))
11858 #ifdef USE_MAPPED_LOCATION
11859 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
11861 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11863 parse_error_context (wfl_operator,
11864 "Duplicate case label: %<default%>");
11865 return error_mark_node;
11868 SWITCH_HAS_DEFAULT (nn) = 1;
11869 TREE_TYPE (node) = void_type_node;
11870 TREE_SIDE_EFFECTS (node) = 1;
11871 CAN_COMPLETE_NORMALLY (node) = 1;
11877 /* Check whether the loop was enclosed in a labeled
11878 statement. If not, create one, insert the loop in it and
11880 nn = patch_loop_statement (node);
11882 /* Anyways, walk the body of the loop */
11883 if (TREE_CODE (node) == LOOP_EXPR)
11884 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11885 /* Switch statement: walk the switch expression and the cases */
11887 node = patch_switch_statement (node);
11889 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11890 nn = error_mark_node;
11893 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11894 /* If we returned something different, that's because we
11895 inserted a label. Pop the label too. */
11898 if (CAN_COMPLETE_NORMALLY (node))
11899 CAN_COMPLETE_NORMALLY (nn) = 1;
11900 POP_LABELED_BLOCK ();
11907 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11908 return patch_exit_expr (node);
11912 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11913 if (TREE_OPERAND (node, 0) == error_mark_node)
11914 return error_mark_node;
11915 /* then-else branches */
11916 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11917 if (TREE_OPERAND (node, 1) == error_mark_node)
11918 return error_mark_node;
11920 /* This is a special case due to build_assertion(). When
11921 assertions are disabled we build a COND_EXPR in which
11922 Operand 1 is the body of the assertion. If that happens to
11923 be a string concatenation we'll need to patch it here. */
11924 tree patched = patch_string (TREE_OPERAND (node, 1));
11926 TREE_OPERAND (node, 1) = patched;
11928 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11929 if (TREE_OPERAND (node, 2) == error_mark_node)
11930 return error_mark_node;
11931 return patch_if_else_statement (node);
11934 case CONDITIONAL_EXPR:
11936 wfl_op1 = TREE_OPERAND (node, 0);
11937 COMPLETE_CHECK_OP_0 (node);
11938 wfl_op2 = TREE_OPERAND (node, 1);
11939 COMPLETE_CHECK_OP_1 (node);
11940 wfl_op3 = TREE_OPERAND (node, 2);
11941 COMPLETE_CHECK_OP_2 (node);
11942 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11944 /* 3- Expression section */
11945 case COMPOUND_EXPR:
11946 wfl_op2 = TREE_OPERAND (node, 1);
11947 TREE_OPERAND (node, 0) = nn =
11948 java_complete_tree (TREE_OPERAND (node, 0));
11949 if (IS_EMPTY_STMT (wfl_op2))
11950 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11953 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11955 /* An unreachable condition in a do-while statement
11956 is *not* (technically) an unreachable statement. */
11958 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11959 nn = EXPR_WFL_NODE (nn);
11960 /* NN can be NULL_TREE exactly when UPDATE is, in
11961 finish_for_loop. */
11962 if (nn != NULL_TREE && TREE_CODE (nn) != EXIT_EXPR)
11964 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11965 if (SUPPRESS_UNREACHABLE_ERROR (nn))
11967 /* Perhaps this warning should have an
11968 associated flag. The code being compiled is
11969 pedantically correct, but useless. */
11970 parse_warning_context (wfl_operator,
11971 "Unreachable statement");
11974 parse_error_context (wfl_operator,
11975 "Unreachable statement");
11978 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11979 if (TREE_OPERAND (node, 1) == error_mark_node)
11980 return error_mark_node;
11981 /* Even though we might allow the case where the first
11982 operand doesn't return normally, we still should compute
11983 CAN_COMPLETE_NORMALLY correctly. */
11984 CAN_COMPLETE_NORMALLY (node)
11985 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11986 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11988 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11992 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11993 return patch_return (node);
11995 case EXPR_WITH_FILE_LOCATION:
11996 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11997 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11999 node = resolve_expression_name (node, NULL);
12000 if (node == error_mark_node)
12002 CAN_COMPLETE_NORMALLY (node) = 1;
12007 location_t save_location = input_location;
12008 #ifdef USE_MAPPED_LOCATION
12009 input_location = EXPR_LOCATION (node);
12010 if (input_location == UNKNOWN_LOCATION)
12011 input_location = save_location;
12013 input_line = EXPR_WFL_LINENO (node);
12015 body = java_complete_tree (EXPR_WFL_NODE (node));
12016 input_location = save_location;
12017 EXPR_WFL_NODE (node) = body;
12018 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
12019 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
12020 if (IS_EMPTY_STMT (body) || TREE_CONSTANT (body))
12022 /* Makes it easier to constant fold, detect empty bodies. */
12025 if (body == error_mark_node)
12027 /* Its important for the evaluation of assignment that
12028 this mark on the TREE_TYPE is propagated. */
12029 TREE_TYPE (node) = error_mark_node;
12030 return error_mark_node;
12033 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
12038 case NEW_ARRAY_EXPR:
12039 /* Patch all the dimensions */
12041 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12043 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
12044 tree dim = convert (int_type_node,
12045 java_complete_tree (TREE_VALUE (cn)));
12046 if (dim == error_mark_node)
12053 TREE_VALUE (cn) = dim;
12054 /* Setup the location of the current dimension, for
12055 later error report. */
12056 #ifdef USE_MAPPED_LOCATION
12057 TREE_PURPOSE (cn) = expr_add_location (NULL_TREE, location, 0);
12059 TREE_PURPOSE (cn) =
12060 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
12061 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
12065 /* They complete the array creation expression, if no errors
12067 CAN_COMPLETE_NORMALLY (node) = 1;
12068 return (flag ? error_mark_node
12069 : force_evaluation_order (patch_newarray (node)));
12071 case NEW_ANONYMOUS_ARRAY_EXPR:
12072 /* Create the array type if necessary. */
12073 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
12075 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
12076 if (!(type = resolve_type_during_patch (type)))
12077 return error_mark_node;
12078 type = build_array_from_name (type, NULL_TREE,
12079 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
12080 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
12082 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
12083 ANONYMOUS_ARRAY_INITIALIZER (node));
12084 if (node == error_mark_node)
12085 return error_mark_node;
12086 CAN_COMPLETE_NORMALLY (node) = 1;
12089 case NEW_CLASS_EXPR:
12091 /* Complete function's argument(s) first */
12092 if (complete_function_arguments (node))
12093 return error_mark_node;
12096 tree decl, wfl = TREE_OPERAND (node, 0);
12097 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
12098 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
12099 super_identifier_node);
12101 #ifdef USE_MAPPED_LOCATION
12102 source_location location = EXPR_LOCATION (node);
12104 int location = EXPR_WFL_LINECOL (node);
12107 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12108 from_super, 0, &decl);
12109 if (node == error_mark_node)
12110 return error_mark_node;
12112 if (TREE_CODE (node) == CALL_EXPR
12113 && TREE_OPERAND (node, 1) != NULL_TREE)
12114 arguments = TREE_VALUE (TREE_OPERAND (node, 1));
12116 arguments = NULL_TREE;
12117 check_thrown_exceptions (location, decl, arguments);
12118 /* If we call this(...), register signature and positions */
12120 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12121 tree_cons (wfl, decl,
12122 DECL_CONSTRUCTOR_CALLS (current_function_decl));
12123 CAN_COMPLETE_NORMALLY (node) = 1;
12124 return force_evaluation_order (node);
12128 /* Save potential wfls */
12129 wfl_op1 = TREE_OPERAND (node, 0);
12130 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12132 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12133 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12134 && DECL_INITIAL (nn) != NULL_TREE)
12138 value = fold_constant_for_init (nn, nn);
12140 /* When we have a primitype type, or a string and we're not
12141 emitting a class file, we actually don't want to generate
12142 anything for the assignment. */
12143 if (value != NULL_TREE &&
12144 (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
12145 (TREE_TYPE (value) == string_ptr_type_node &&
12146 ! flag_emit_class_files)))
12148 /* Prepare node for patch_assignment */
12149 TREE_OPERAND (node, 1) = value;
12150 /* Call patch assignment to verify the assignment */
12151 if (patch_assignment (node, wfl_op1) == error_mark_node)
12152 return error_mark_node;
12153 /* Set DECL_INITIAL properly (a conversion might have
12154 been decided by patch_assignment) and return the
12155 empty statement. */
12158 tree patched = patch_string (TREE_OPERAND (node, 1));
12160 DECL_INITIAL (nn) = patched;
12162 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12163 DECL_FIELD_FINAL_IUD (nn) = 1;
12164 return build_java_empty_stmt ();
12167 if (! flag_emit_class_files)
12168 DECL_INITIAL (nn) = NULL_TREE;
12170 wfl_op2 = TREE_OPERAND (node, 1);
12172 if (TREE_OPERAND (node, 0) == error_mark_node)
12173 return error_mark_node;
12175 flag = COMPOUND_ASSIGN_P (wfl_op2);
12178 /* This might break when accessing outer field from inner
12179 class. TESTME, FIXME */
12180 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12182 /* Hand stabilize the lhs on both places */
12183 TREE_OPERAND (node, 0) = lvalue;
12184 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12185 (flag_emit_class_files ? lvalue : save_expr (lvalue));
12187 /* 15.25.2.a: Left hand is not an array access. FIXME */
12188 /* Now complete the RHS. We write it back later on. */
12189 nn = java_complete_tree (TREE_OPERAND (node, 1));
12191 if ((cn = patch_string (nn)))
12194 /* The last part of the rewrite for E1 op= E2 is to have
12195 E1 = (T)(E1 op E2), with T being the type of E1. */
12196 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12197 TREE_TYPE (lvalue), nn));
12199 /* If the assignment is compound and has reference type,
12200 then ensure the LHS has type String and nothing else. */
12201 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12202 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12203 parse_error_context (wfl_op2,
12204 "Incompatible type for %<+=%>. Can't convert %qs to %<java.lang.String%>",
12205 lang_printable_name (TREE_TYPE (lvalue), 0));
12207 /* 15.25.2.b: Left hand is an array access. FIXME */
12210 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12211 function to complete this RHS. Note that a NEW_ARRAY_INIT
12212 might have been already fully expanded if created as a result
12213 of processing an anonymous array initializer. We avoid doing
12214 the operation twice by testing whether the node already bears
12216 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12217 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12218 TREE_OPERAND (node, 1));
12219 /* Otherwise we simply complete the RHS */
12221 nn = java_complete_tree (TREE_OPERAND (node, 1));
12223 if (nn == error_mark_node)
12224 return error_mark_node;
12226 /* Write back the RHS as we evaluated it. */
12227 TREE_OPERAND (node, 1) = nn;
12229 /* In case we're handling = with a String as a RHS, we need to
12230 produce a String out of the RHS (it might still be a
12231 STRING_CST or a StringBuffer at this stage */
12232 if ((nn = patch_string (TREE_OPERAND (node, 1))))
12233 TREE_OPERAND (node, 1) = nn;
12235 if ((nn = nested_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12236 TREE_OPERAND (node, 1))))
12238 /* We return error_mark_node if nested_field_access_fix
12239 detects we write into a final. */
12240 if (nn == error_mark_node)
12241 return error_mark_node;
12246 node = patch_assignment (node, wfl_op1);
12247 if (node == error_mark_node)
12248 return error_mark_node;
12249 /* Reorganize the tree if necessary. */
12250 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12251 || JSTRING_P (TREE_TYPE (node))))
12252 node = java_refold (node);
12255 /* Seek to set DECL_INITIAL to a proper value, since it might have
12256 undergone a conversion in patch_assignment. We do that only when
12257 it's necessary to have DECL_INITIAL properly set. */
12258 nn = TREE_OPERAND (node, 0);
12259 if (TREE_CODE (nn) == VAR_DECL
12260 && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12261 && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12262 && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12263 || TREE_TYPE (nn) == string_ptr_type_node))
12264 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12266 CAN_COMPLETE_NORMALLY (node) = 1;
12278 case TRUNC_MOD_EXPR:
12279 case TRUNC_DIV_EXPR:
12281 case TRUTH_ANDIF_EXPR:
12282 case TRUTH_ORIF_EXPR:
12289 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12290 knows how to handle those cases. */
12291 wfl_op1 = TREE_OPERAND (node, 0);
12292 wfl_op2 = TREE_OPERAND (node, 1);
12294 CAN_COMPLETE_NORMALLY (node) = 1;
12295 /* Don't complete string nodes if dealing with the PLUS operand. */
12296 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12298 nn = java_complete_tree (wfl_op1);
12299 if (nn == error_mark_node)
12300 return error_mark_node;
12302 TREE_OPERAND (node, 0) = nn;
12304 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12306 nn = java_complete_tree (wfl_op2);
12307 if (nn == error_mark_node)
12308 return error_mark_node;
12310 TREE_OPERAND (node, 1) = nn;
12312 return patch_binop (node, wfl_op1, wfl_op2, 0);
12314 case INSTANCEOF_EXPR:
12315 wfl_op1 = TREE_OPERAND (node, 0);
12316 COMPLETE_CHECK_OP_0 (node);
12317 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1), 0);
12319 case UNARY_PLUS_EXPR:
12321 case TRUTH_NOT_EXPR:
12323 case PREDECREMENT_EXPR:
12324 case PREINCREMENT_EXPR:
12325 case POSTDECREMENT_EXPR:
12326 case POSTINCREMENT_EXPR:
12328 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12329 how to handle those cases. */
12330 wfl_op1 = TREE_OPERAND (node, 0);
12331 CAN_COMPLETE_NORMALLY (node) = 1;
12332 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12333 if (TREE_OPERAND (node, 0) == error_mark_node)
12334 return error_mark_node;
12335 node = patch_unaryop (node, wfl_op1);
12336 CAN_COMPLETE_NORMALLY (node) = 1;
12340 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12341 how to handle those cases. */
12342 wfl_op1 = TREE_OPERAND (node, 0);
12343 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12344 if (TREE_OPERAND (node, 0) == error_mark_node)
12345 return error_mark_node;
12346 if (!flag_emit_class_files)
12347 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12348 /* The same applies to wfl_op2 */
12349 wfl_op2 = TREE_OPERAND (node, 1);
12350 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12351 if (TREE_OPERAND (node, 1) == error_mark_node)
12352 return error_mark_node;
12353 if (!flag_emit_class_files)
12354 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12355 return patch_array_ref (node);
12360 case COMPONENT_REF:
12361 /* The first step in the re-write of qualified name handling. FIXME.
12362 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12363 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12364 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12366 tree name = TREE_OPERAND (node, 1);
12367 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12368 if (field == NULL_TREE)
12370 error ("missing static field %qs", IDENTIFIER_POINTER (name));
12371 return error_mark_node;
12373 if (! FIELD_STATIC (field))
12375 error ("not a static field %qs", IDENTIFIER_POINTER (name));
12376 return error_mark_node;
12385 /* Can't use THIS in a static environment */
12388 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12389 parse_error_context (wfl_operator,
12390 "Keyword %<this%> used outside allowed context");
12391 TREE_TYPE (node) = error_mark_node;
12392 return error_mark_node;
12394 if (ctxp->explicit_constructor_p)
12396 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12397 parse_error_context
12398 (wfl_operator, "Can't reference %<this%> or %<super%> before the superclass constructor has been called");
12399 TREE_TYPE (node) = error_mark_node;
12400 return error_mark_node;
12402 return current_this;
12404 case CLASS_LITERAL:
12405 CAN_COMPLETE_NORMALLY (node) = 1;
12406 node = patch_incomplete_class_ref (node);
12407 if (node == error_mark_node)
12408 return error_mark_node;
12412 CAN_COMPLETE_NORMALLY (node) = 1;
12413 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12414 and it's time to turn it into the appropriate String object */
12415 if ((nn = patch_string (node)))
12418 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12423 /* Complete function call's argument. Return a nonzero value is an
12424 error was found. */
12427 complete_function_arguments (tree node)
12432 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12433 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12435 tree wfl = TREE_VALUE (cn), parm, temp;
12436 parm = java_complete_tree (wfl);
12438 if (parm == error_mark_node)
12443 /* If we have a string literal that we haven't transformed yet or a
12444 crafted string buffer, as a result of the use of the String
12445 `+' operator. Build `parm.toString()' and expand it. */
12446 if ((temp = patch_string (parm)))
12449 TREE_VALUE (cn) = parm;
12451 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12455 /* Sometimes (for loops and variable initialized during their
12456 declaration), we want to wrap a statement around a WFL and turn it
12460 build_debugable_stmt (int location, tree stmt)
12462 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12464 #ifdef USE_MAPPED_LOCATION
12465 stmt = expr_add_location (stmt, location, 1);
12467 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12468 EXPR_WFL_LINECOL (stmt) = location;
12469 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12476 build_expr_block (tree body, tree decls)
12478 tree node = make_node (BLOCK);
12479 BLOCK_EXPR_DECLS (node) = decls;
12480 BLOCK_EXPR_BODY (node) = body;
12482 TREE_TYPE (node) = TREE_TYPE (body);
12483 TREE_SIDE_EFFECTS (node) = 1;
12487 /* Create a new function block and link it appropriately to current
12488 function block chain */
12493 tree b = build_expr_block (NULL_TREE, NULL_TREE);
12495 /* Link block B supercontext to the previous block. The current
12496 function DECL is used as supercontext when enter_a_block is called
12497 for the first time for a given function. The current function body
12498 (DECL_FUNCTION_BODY) is set to be block B. */
12500 tree fndecl = current_function_decl;
12503 BLOCK_SUPERCONTEXT (b) = current_static_block;
12504 current_static_block = b;
12507 else if (!DECL_FUNCTION_BODY (fndecl))
12509 BLOCK_SUPERCONTEXT (b) = fndecl;
12510 DECL_FUNCTION_BODY (fndecl) = b;
12514 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12515 DECL_FUNCTION_BODY (fndecl) = b;
12520 /* Exit a block by changing the current function body
12521 (DECL_FUNCTION_BODY) to the current block super context, only if
12522 the block being exited isn't the method's top level one. */
12528 if (current_function_decl)
12530 b = DECL_FUNCTION_BODY (current_function_decl);
12531 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12532 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12536 b = current_static_block;
12538 if (BLOCK_SUPERCONTEXT (b))
12539 current_static_block = BLOCK_SUPERCONTEXT (b);
12544 /* Lookup for NAME in the nested function's blocks, all the way up to
12545 the current toplevel one. It complies with Java's local variable
12549 lookup_name_in_blocks (tree name)
12551 tree b = GET_CURRENT_BLOCK (current_function_decl);
12553 while (b != current_function_decl)
12557 /* Paranoid sanity check. To be removed */
12558 if (TREE_CODE (b) != BLOCK)
12561 for (current = BLOCK_EXPR_DECLS (b); current;
12562 current = TREE_CHAIN (current))
12563 if (DECL_NAME (current) == name)
12565 b = BLOCK_SUPERCONTEXT (b);
12571 maybe_absorb_scoping_blocks (void)
12573 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12575 tree b = exit_block ();
12576 java_method_add_stmt (current_function_decl, b);
12577 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", input_line));
12582 /* This section of the source is reserved to build_* functions that
12583 are building incomplete tree nodes and the patch_* functions that
12584 are completing them. */
12586 /* Wrap a non WFL node around a WFL. */
12589 build_wfl_wrap (tree node, int location)
12591 tree wfl, node_to_insert = node;
12593 /* We want to process THIS . xxx symbolically, to keep it consistent
12594 with the way we're processing SUPER. A THIS from a primary as a
12595 different form than a SUPER. Turn THIS into something symbolic */
12596 if (TREE_CODE (node) == THIS_EXPR)
12597 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12599 #ifdef USE_MAPPED_LOCATION
12600 wfl = build_unknown_wfl (NULL_TREE);
12602 SET_EXPR_LOCATION (wfl, location);
12604 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12606 EXPR_WFL_LINECOL (wfl) = location;
12608 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12612 /* Build a super() constructor invocation. Returns an empty statement if
12613 we're currently dealing with the class java.lang.Object. */
12616 build_super_invocation (tree mdecl)
12618 if (DECL_CONTEXT (mdecl) == object_type_node)
12619 return build_java_empty_stmt ();
12622 tree super_wfl = build_wfl_node (super_identifier_node);
12623 tree a = NULL_TREE, t;
12625 /* This is called after parsing is done, so the parser context
12626 won't be accurate. Set location info from current_class decl. */
12627 tree class_wfl = lookup_cl (TYPE_NAME (current_class));
12628 EXPR_WFL_LINECOL (super_wfl) = EXPR_WFL_LINECOL (class_wfl);
12630 /* If we're dealing with an anonymous class, pass the arguments
12631 of the crafted constructor along. */
12632 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12634 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12635 for (; t != end_params_node; t = TREE_CHAIN (t))
12636 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12638 return build_method_invocation (super_wfl, a);
12642 /* Build a SUPER/THIS qualified method invocation. */
12645 build_this_super_qualified_invocation (int use_this, tree name, tree args,
12646 int lloc, int rloc)
12650 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12651 EXPR_WFL_LINECOL (wfl) = lloc;
12652 invok = build_method_invocation (name, args);
12653 return make_qualified_primary (wfl, invok, rloc);
12656 /* Build an incomplete CALL_EXPR node. */
12659 build_method_invocation (tree name, tree args)
12661 tree call = build3 (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12662 TREE_SIDE_EFFECTS (call) = 1;
12663 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12667 /* Build an incomplete new xxx(...) node. */
12670 build_new_invocation (tree name, tree args)
12672 tree call = build3 (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12673 TREE_SIDE_EFFECTS (call) = 1;
12674 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12678 /* Build an incomplete assignment expression. */
12681 build_assignment (int op, int op_location, tree lhs, tree rhs)
12684 /* Build the corresponding binop if we deal with a Compound
12685 Assignment operator. Mark the binop sub-tree as part of a
12686 Compound Assignment expression */
12687 if (op != ASSIGN_TK)
12689 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12690 COMPOUND_ASSIGN_P (rhs) = 1;
12692 assignment = build2 (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12693 TREE_SIDE_EFFECTS (assignment) = 1;
12694 EXPR_WFL_LINECOL (assignment) = op_location;
12698 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12699 the buffer. This is used only for string conversion. */
12701 string_convert_int_cst (tree node)
12703 /* Long.MIN_VALUE is -9223372036854775808, 20 characters. */
12704 static char buffer[21];
12706 unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12707 unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12708 char *p = buffer + sizeof (buffer);
12711 unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12712 << (HOST_BITS_PER_WIDE_INT - 1));
12716 /* If negative, note the fact and negate the value. */
12726 /* Divide by 10 until there are no bits left. */
12729 unsigned HOST_WIDE_INT acc = 0;
12730 unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12733 /* Use long division to compute the result and the remainder. */
12734 for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12736 /* Shift a bit into accumulator. */
12741 /* Shift the value. */
12747 /* Shift the correct bit into the result. */
12749 if ((outlo & hibit))
12759 /* '0' == 060 in Java, but might not be here (think EBCDIC). */
12760 *--p = '\060' + acc;
12768 *--p = '\055'; /* '-' == 055 in Java, but might not be here. */
12773 /* Print an INTEGER_CST node in a static buffer, and return the
12774 buffer. This is used only for error handling. */
12776 print_int_node (tree node)
12778 static char buffer [80];
12779 if (TREE_CONSTANT_OVERFLOW (node))
12780 sprintf (buffer, "<overflow>");
12782 if (TREE_INT_CST_HIGH (node) == 0)
12783 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12784 TREE_INT_CST_LOW (node));
12785 else if (TREE_INT_CST_HIGH (node) == -1
12786 && TREE_INT_CST_LOW (node) != 0)
12787 sprintf (buffer, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
12788 -TREE_INT_CST_LOW (node));
12790 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12791 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12797 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12800 /* 15.25 Assignment operators. */
12803 patch_assignment (tree node, tree wfl_op1)
12805 tree rhs = TREE_OPERAND (node, 1);
12806 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12807 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12808 int error_found = 0;
12809 int lvalue_from_array = 0;
12812 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12814 /* Lhs can be a named variable */
12815 if (JDECL_P (lvalue))
12817 lhs_type = TREE_TYPE (lvalue);
12819 /* Or Lhs can be an array access. */
12820 else if (TREE_CODE (lvalue) == ARRAY_REF)
12822 lhs_type = TREE_TYPE (lvalue);
12823 lvalue_from_array = 1;
12825 /* Or a field access */
12826 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12827 lhs_type = TREE_TYPE (lvalue);
12828 /* Or a function return slot */
12829 else if (TREE_CODE (lvalue) == RESULT_DECL)
12831 /* If the return type is an integral type, then we create the
12832 RESULT_DECL with a promoted type, but we need to do these
12833 checks against the unpromoted type to ensure type safety. So
12834 here we look at the real type, not the type of the decl we
12836 lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12839 /* Otherwise, we might want to try to write into an optimized static
12840 final, this is an of a different nature, reported further on. */
12841 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12842 && resolve_expression_name (wfl_op1, &llvalue))
12844 lhs_type = TREE_TYPE (lvalue);
12848 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12852 rhs_type = TREE_TYPE (rhs);
12854 /* 5.1 Try the assignment conversion for builtin type. */
12855 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12857 /* 5.2 If it failed, try a reference conversion */
12859 new_rhs = try_reference_assignconv (lhs_type, rhs);
12861 /* 15.25.2 If we have a compound assignment, convert RHS into the
12863 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12864 new_rhs = convert (lhs_type, rhs);
12866 /* Explicit cast required. This is an error */
12869 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12870 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12872 char operation [32]; /* Max size known */
12874 /* If the assignment is part of a declaration, we use the WFL of
12875 the declared variable to point out the error and call it a
12876 declaration problem. If the assignment is a genuine =
12877 operator, we call is a operator `=' problem, otherwise we
12878 call it an assignment problem. In both of these last cases,
12879 we use the WFL of the operator to indicate the error. */
12881 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12884 strcpy (operation, "declaration");
12888 wfl = wfl_operator;
12889 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12890 strcpy (operation, "assignment");
12891 else if (is_return)
12892 strcpy (operation, "'return'");
12894 strcpy (operation, "'='");
12897 if (!valid_cast_to_p (rhs_type, lhs_type))
12898 parse_error_context
12899 (wfl, "Incompatible type for %s. Can't convert %qs to %qs",
12900 operation, t1, t2);
12902 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert %qs to %qs",
12903 operation, t1, t2);
12904 free (t1); free (t2);
12909 return error_mark_node;
12911 /* If we're processing a `return' statement, promote the actual type
12912 to the promoted type. */
12914 new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12916 /* 10.10: Array Store Exception runtime check */
12917 if (!flag_emit_class_files
12918 && lvalue_from_array
12919 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12921 tree array, store_check, base, index_expr;
12923 /* Save RHS so that it doesn't get re-evaluated by the store check. */
12924 new_rhs = save_expr (new_rhs);
12926 /* Get the INDIRECT_REF. */
12927 array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12928 /* Get the array pointer expr. */
12929 array = TREE_OPERAND (array, 0);
12930 store_check = build_java_arraystore_check (array, new_rhs);
12932 index_expr = TREE_OPERAND (lvalue, 1);
12934 if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12936 /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12937 happen before the store check, so prepare to insert the store
12938 check within the second operand of the existing COMPOUND_EXPR. */
12944 index_expr = TREE_OPERAND (base, 1);
12945 TREE_OPERAND (base, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (index_expr),
12946 store_check, index_expr);
12949 /* Final locals can be used as case values in switch
12950 statement. Prepare them for this eventuality. */
12951 if (TREE_CODE (lvalue) == VAR_DECL
12952 && DECL_FINAL (lvalue)
12953 && TREE_CONSTANT (new_rhs)
12954 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12955 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12958 TREE_CONSTANT (lvalue) = 1;
12959 TREE_INVARIANT (lvalue) = 1;
12960 DECL_INITIAL (lvalue) = new_rhs;
12963 /* Copy the rhs if it's a reference. */
12964 if (! flag_check_references && ! flag_emit_class_files && optimize > 0)
12966 switch (TREE_CODE (new_rhs))
12970 case COMPONENT_REF:
12971 /* Transform a = foo.bar
12972 into a = ({int tmp; tmp = foo.bar;}).
12973 We need to ensure that if a read from memory fails
12974 because of a NullPointerException, a destination variable
12975 will remain unchanged. An explicit temporary does what
12978 If flag_check_references is set, this is unnecessary
12979 because we'll check each reference before doing any
12980 reads. If optimize is not set the result will never be
12981 written to a stack slot that contains the LHS. */
12983 tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"),
12984 TREE_TYPE (new_rhs));
12985 tree block = make_node (BLOCK);
12987 = build2 (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
12988 DECL_CONTEXT (tmp) = current_function_decl;
12989 TREE_TYPE (block) = TREE_TYPE (new_rhs);
12990 BLOCK_VARS (block) = tmp;
12991 BLOCK_EXPR_BODY (block) = assignment;
12992 TREE_SIDE_EFFECTS (block) = 1;
13001 TREE_OPERAND (node, 0) = lvalue;
13002 TREE_OPERAND (node, 1) = new_rhs;
13003 TREE_TYPE (node) = lhs_type;
13007 /* Check that type SOURCE can be cast into type DEST. If the cast
13008 can't occur at all, return NULL; otherwise, return a possibly
13012 try_reference_assignconv (tree lhs_type, tree rhs)
13014 tree new_rhs = NULL_TREE;
13015 tree rhs_type = TREE_TYPE (rhs);
13017 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
13019 /* `null' may be assigned to any reference type */
13020 if (rhs == null_pointer_node)
13021 new_rhs = null_pointer_node;
13022 /* Try the reference assignment conversion */
13023 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
13025 /* This is a magic assignment that we process differently */
13026 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
13032 /* Check that RHS can be converted into LHS_TYPE by the assignment
13033 conversion (5.2), for the cases of RHS being a builtin type. Return
13034 NULL_TREE if the conversion fails or if because RHS isn't of a
13035 builtin type. Return a converted RHS if the conversion is possible. */
13038 try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
13040 tree new_rhs = NULL_TREE;
13041 tree rhs_type = TREE_TYPE (rhs);
13043 /* Handle boolean specially. */
13044 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13045 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13047 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13048 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13052 /* 5.1.1 Try Identity Conversion,
13053 5.1.2 Try Widening Primitive Conversion */
13054 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
13055 new_rhs = convert (lhs_type, rhs);
13057 /* Try a narrowing primitive conversion (5.1.3):
13058 - expression is a constant expression of type byte, short, char,
13060 - variable is byte, short or char AND
13061 - The value of the expression is representable in the type of the
13063 else if ((rhs_type == byte_type_node || rhs_type == short_type_node
13064 || rhs_type == char_type_node || rhs_type == int_type_node)
13065 && TREE_CONSTANT (rhs)
13066 && (lhs_type == byte_type_node || lhs_type == char_type_node
13067 || lhs_type == short_type_node))
13069 if (int_fits_type_p (rhs, lhs_type))
13070 new_rhs = convert (lhs_type, rhs);
13071 else if (wfl_op1) /* Might be called with a NULL */
13072 parse_warning_context
13074 "Constant expression %qs too wide for narrowing primitive conversion to %qs",
13075 print_int_node (rhs), lang_printable_name (lhs_type, 0));
13076 /* Reported a warning that will turn into an error further
13077 down, so we don't return */
13083 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
13084 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
13085 0 is the conversion test fails. This implements parts the method
13086 invocation conversion (5.3). */
13089 valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
13091 /* 5.1.1: This is the identity conversion part. */
13092 if (lhs_type == rhs_type)
13095 /* Reject non primitive types and boolean conversions. */
13096 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
13099 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
13100 than a char can't be converted into a char. Short can't too, but
13101 the < test below takes care of that */
13102 if (lhs_type == char_type_node && rhs_type == byte_type_node)
13105 /* Accept all promoted type here. Note, we can't use <= in the test
13106 below, because we still need to bounce out assignments of short
13107 to char and the likes */
13108 if (lhs_type == int_type_node
13109 && (rhs_type == promoted_byte_type_node
13110 || rhs_type == promoted_short_type_node
13111 || rhs_type == promoted_char_type_node
13112 || rhs_type == promoted_boolean_type_node))
13115 /* From here, an integral is widened if its precision is smaller
13116 than the precision of the LHS or if the LHS is a floating point
13117 type, or the RHS is a float and the RHS a double. */
13118 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13119 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13120 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13121 || (rhs_type == float_type_node && lhs_type == double_type_node))
13127 /* Check that something of SOURCE type can be assigned or cast to
13128 something of DEST type at runtime. Return 1 if the operation is
13129 valid, 0 otherwise. If CAST is set to 1, we're treating the case
13130 were SOURCE is cast into DEST, which borrows a lot of the
13131 assignment check. */
13134 valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
13136 /* SOURCE or DEST might be null if not from a declared entity. */
13137 if (!source || !dest)
13139 if (JNULLP_TYPE_P (source))
13141 if (TREE_CODE (source) == POINTER_TYPE)
13142 source = TREE_TYPE (source);
13143 if (TREE_CODE (dest) == POINTER_TYPE)
13144 dest = TREE_TYPE (dest);
13146 /* If source and dest are being compiled from bytecode, they may need to
13148 if (CLASS_P (source) && !CLASS_LOADED_P (source))
13150 load_class (source, 1);
13151 safe_layout_class (source);
13153 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13155 load_class (dest, 1);
13156 safe_layout_class (dest);
13159 /* Case where SOURCE is a class type */
13160 if (TYPE_CLASS_P (source))
13162 if (TYPE_CLASS_P (dest))
13163 return (source == dest
13164 || inherits_from_p (source, dest)
13165 || (cast && inherits_from_p (dest, source)));
13166 if (TYPE_INTERFACE_P (dest))
13168 /* If doing a cast and SOURCE is final, the operation is
13169 always correct a compile time (because even if SOURCE
13170 does not implement DEST, a subclass of SOURCE might). */
13171 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13173 /* Otherwise, SOURCE must implement DEST */
13174 return interface_of_p (dest, source);
13176 /* DEST is an array, cast permitted if SOURCE is of Object type */
13177 return (cast && source == object_type_node ? 1 : 0);
13179 if (TYPE_INTERFACE_P (source))
13181 if (TYPE_CLASS_P (dest))
13183 /* If not casting, DEST must be the Object type */
13185 return dest == object_type_node;
13186 /* We're doing a cast. The cast is always valid is class
13187 DEST is not final, otherwise, DEST must implement SOURCE */
13188 else if (!CLASS_FINAL (TYPE_NAME (dest)))
13191 return interface_of_p (source, dest);
13193 if (TYPE_INTERFACE_P (dest))
13195 /* If doing a cast, then if SOURCE and DEST contain method
13196 with the same signature but different return type, then
13197 this is a (compile time) error */
13200 tree method_source, method_dest;
13204 for (method_source = TYPE_METHODS (source); method_source;
13205 method_source = TREE_CHAIN (method_source))
13208 build_java_argument_signature (TREE_TYPE (method_source));
13209 source_type = TREE_TYPE (TREE_TYPE (method_source));
13210 source_name = DECL_NAME (method_source);
13211 for (method_dest = TYPE_METHODS (dest);
13212 method_dest; method_dest = TREE_CHAIN (method_dest))
13214 build_java_argument_signature (TREE_TYPE (method_dest))
13215 && source_name == DECL_NAME (method_dest)
13216 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13222 return source == dest || interface_of_p (dest, source);
13228 && (DECL_NAME (TYPE_NAME (source))
13229 == java_lang_cloneable_identifier_node
13230 || (DECL_NAME (TYPE_NAME (source))
13231 == java_io_serializable_identifier_node)));
13234 if (TYPE_ARRAY_P (source))
13236 if (TYPE_CLASS_P (dest))
13237 return dest == object_type_node;
13238 /* Can't cast an array to an interface unless the interface is
13239 java.lang.Cloneable or java.io.Serializable. */
13240 if (TYPE_INTERFACE_P (dest))
13241 return (DECL_NAME (TYPE_NAME (dest))
13242 == java_lang_cloneable_identifier_node
13243 || (DECL_NAME (TYPE_NAME (dest))
13244 == java_io_serializable_identifier_node));
13247 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13248 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13250 /* In case of severe errors, they turn out null */
13251 if (!dest_element_type || !source_element_type)
13253 if (source_element_type == dest_element_type)
13255 return valid_ref_assignconv_cast_p (source_element_type,
13256 dest_element_type, cast);
13264 valid_cast_to_p (tree source, tree dest)
13266 if (TREE_CODE (source) == POINTER_TYPE)
13267 source = TREE_TYPE (source);
13268 if (TREE_CODE (dest) == POINTER_TYPE)
13269 dest = TREE_TYPE (dest);
13271 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13272 return valid_ref_assignconv_cast_p (source, dest, 1);
13274 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13277 else if (TREE_CODE (source) == BOOLEAN_TYPE
13278 && TREE_CODE (dest) == BOOLEAN_TYPE)
13285 do_unary_numeric_promotion (tree arg)
13287 tree type = TREE_TYPE (arg);
13288 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13289 || TREE_CODE (type) == CHAR_TYPE)
13290 arg = convert (int_type_node, arg);
13294 /* Return a nonzero value if SOURCE can be converted into DEST using
13295 the method invocation conversion rule (5.3). */
13297 valid_method_invocation_conversion_p (tree dest, tree source)
13299 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13300 && valid_builtin_assignconv_identity_widening_p (dest, source))
13301 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13302 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13303 && valid_ref_assignconv_cast_p (source, dest, 0)));
13306 /* Build an incomplete binop expression. */
13309 build_binop (enum tree_code op, int op_location, tree op1, tree op2)
13311 tree binop = build2 (op, NULL_TREE, op1, op2);
13312 TREE_SIDE_EFFECTS (binop) = 1;
13313 /* Store the location of the operator, for better error report. The
13314 string of the operator will be rebuild based on the OP value. */
13315 EXPR_WFL_LINECOL (binop) = op_location;
13319 /* Build the string of the operator retained by NODE. If NODE is part
13320 of a compound expression, add an '=' at the end of the string. This
13321 function is called when an error needs to be reported on an
13322 operator. The string is returned as a pointer to a static character
13326 operator_string (tree node)
13328 #define BUILD_OPERATOR_STRING(S) \
13330 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13334 static char buffer [10];
13335 switch (TREE_CODE (node))
13337 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13338 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13339 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13340 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13341 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13342 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13343 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13344 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13345 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13346 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13347 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13348 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13349 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13350 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13351 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13352 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13353 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13354 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13355 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13356 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13357 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13358 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13359 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13360 case PREINCREMENT_EXPR: /* Fall through */
13361 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13362 case PREDECREMENT_EXPR: /* Fall through */
13363 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13365 internal_error ("unregistered operator %s",
13366 tree_code_name [TREE_CODE (node)]);
13369 #undef BUILD_OPERATOR_STRING
13372 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13375 java_decl_equiv (tree var_acc1, tree var_acc2)
13377 if (JDECL_P (var_acc1))
13378 return (var_acc1 == var_acc2);
13380 return (TREE_CODE (var_acc1) == COMPONENT_REF
13381 && TREE_CODE (var_acc2) == COMPONENT_REF
13382 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13383 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13384 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13387 /* Return a nonzero value if CODE is one of the operators that can be
13388 used in conjunction with the `=' operator in a compound assignment. */
13391 binop_compound_p (enum tree_code code)
13394 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13395 if (binop_lookup [i] == code)
13398 return i < BINOP_COMPOUND_CANDIDATES;
13401 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13404 java_refold (tree t)
13406 tree c, b, ns, decl;
13408 if (TREE_CODE (t) != MODIFY_EXPR)
13411 c = TREE_OPERAND (t, 1);
13412 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13413 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13414 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13417 /* Now the left branch of the binary operator. */
13418 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13419 if (! (b && TREE_CODE (b) == NOP_EXPR
13420 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13423 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13424 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13425 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13428 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13429 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13430 /* It's got to be the an equivalent decl */
13431 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13433 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13434 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13435 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13436 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13437 /* Change the right part of the BINOP_EXPR */
13438 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13444 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13445 errors but we modify NODE so that it contains the type computed
13446 according to the expression, when it's fixed. Otherwise, we write
13447 error_mark_node as the type. It allows us to further the analysis
13448 of remaining nodes and detects more errors in certain cases. */
13451 patch_binop (tree node, tree wfl_op1, tree wfl_op2, int folding)
13453 tree op1 = TREE_OPERAND (node, 0);
13454 tree op2 = TREE_OPERAND (node, 1);
13455 tree op1_type = TREE_TYPE (op1);
13456 tree op2_type = TREE_TYPE (op2);
13457 tree prom_type = NULL_TREE, cn;
13458 enum tree_code code = TREE_CODE (node);
13460 /* If 1, tell the routine that we have to return error_mark_node
13461 after checking for the initialization of the RHS */
13462 int error_found = 0;
13464 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13466 /* If either op<n>_type are NULL, this might be early signs of an
13467 error situation, unless it's too early to tell (in case we're
13468 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13469 correctly so the error can be later on reported accurately. */
13470 if (! (code == PLUS_EXPR || code == NE_EXPR
13471 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13476 n = java_complete_tree (op1);
13477 op1_type = TREE_TYPE (n);
13481 n = java_complete_tree (op2);
13482 op2_type = TREE_TYPE (n);
13488 /* 15.16 Multiplicative operators */
13489 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13490 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13491 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13492 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13493 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13495 if (!JNUMERIC_TYPE_P (op1_type))
13496 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13497 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13498 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13499 TREE_TYPE (node) = error_mark_node;
13503 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13505 /* Detect integral division by zero */
13506 if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13507 && TREE_CODE (prom_type) == INTEGER_TYPE
13508 && (op2 == integer_zero_node || op2 == long_zero_node ||
13509 (TREE_CODE (op2) == INTEGER_CST &&
13510 ! TREE_INT_CST_LOW (op2) && ! TREE_INT_CST_HIGH (op2))))
13512 parse_warning_context
13514 "Evaluating this expression will result in an arithmetic exception being thrown");
13515 TREE_CONSTANT (node) = 0;
13516 TREE_INVARIANT (node) = 0;
13519 /* Change the division operator if necessary */
13520 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13521 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13523 /* Before divisions as is disappear, try to simplify and bail if
13524 applicable, otherwise we won't perform even simple
13525 simplifications like (1-1)/3. We can't do that with floating
13526 point number, folds can't handle them at this stage. */
13527 if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13528 && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13530 TREE_TYPE (node) = prom_type;
13531 node = fold (node);
13532 if (TREE_CODE (node) != code)
13536 if (TREE_CODE (prom_type) == INTEGER_TYPE
13537 && flag_use_divide_subroutine
13538 && ! flag_emit_class_files
13539 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13540 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13542 /* This one is more complicated. FLOATs are processed by a
13543 function call to soft_fmod. Duplicate the value of the
13544 COMPOUND_ASSIGN_P flag. */
13545 if (code == TRUNC_MOD_EXPR)
13547 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13548 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13553 /* 15.17 Additive Operators */
13554 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13556 /* Operation is valid if either one argument is a string
13557 constant, a String object or a StringBuffer crafted for the
13558 purpose of the a previous usage of the String concatenation
13561 if (TREE_CODE (op1) == STRING_CST
13562 || TREE_CODE (op2) == STRING_CST
13563 || JSTRING_TYPE_P (op1_type)
13564 || JSTRING_TYPE_P (op2_type)
13565 || IS_CRAFTED_STRING_BUFFER_P (op1)
13566 || IS_CRAFTED_STRING_BUFFER_P (op2))
13567 return build_string_concatenation (op1, op2);
13569 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13571 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13573 if (!JNUMERIC_TYPE_P (op1_type))
13574 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13575 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13576 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13577 TREE_TYPE (node) = error_mark_node;
13581 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13584 /* 15.18 Shift Operators */
13588 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13590 if (!JINTEGRAL_TYPE_P (op1_type))
13591 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13594 if (JNUMERIC_TYPE_P (op2_type))
13595 parse_error_context (wfl_operator,
13596 "Incompatible type for %qs. Explicit cast needed to convert shift distance from %qs to integral",
13597 operator_string (node),
13598 lang_printable_name (op2_type, 0));
13600 parse_error_context (wfl_operator,
13601 "Incompatible type for %qs. Can't convert shift distance from %qs to integral",
13602 operator_string (node),
13603 lang_printable_name (op2_type, 0));
13605 TREE_TYPE (node) = error_mark_node;
13610 /* Unary numeric promotion (5.6.1) is performed on each operand
13612 op1 = do_unary_numeric_promotion (op1);
13613 op2 = do_unary_numeric_promotion (op2);
13615 /* If the right hand side is of type `long', first cast it to
13617 if (TREE_TYPE (op2) == long_type_node)
13618 op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13620 /* The type of the shift expression is the type of the promoted
13621 type of the left-hand operand */
13622 prom_type = TREE_TYPE (op1);
13624 /* Shift int only up to 0x1f and long up to 0x3f */
13625 if (prom_type == int_type_node)
13626 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13627 build_int_cst (NULL_TREE, 0x1f)));
13629 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13630 build_int_cst (NULL_TREE, 0x3f)));
13632 /* The >>> operator is a >> operating on unsigned quantities */
13633 if (code == URSHIFT_EXPR && (folding || ! flag_emit_class_files))
13636 tree utype = java_unsigned_type (prom_type);
13637 op1 = convert (utype, op1);
13639 to_return = fold_build2 (RSHIFT_EXPR, utype, op1, op2);
13640 to_return = convert (prom_type, to_return);
13641 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13642 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13643 TREE_SIDE_EFFECTS (to_return)
13644 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13649 /* 15.19.1 Type Comparison Operator instanceof */
13650 case INSTANCEOF_EXPR:
13652 TREE_TYPE (node) = boolean_type_node;
13654 /* OP1_TYPE might be NULL when OP1 is a string constant. */
13655 if ((cn = patch_string (op1)))
13658 op1_type = TREE_TYPE (op1);
13660 if (op1_type == NULL_TREE)
13663 if (!(op2_type = resolve_type_during_patch (op2)))
13664 return error_mark_node;
13666 /* The first operand must be a reference type or the null type */
13667 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13668 error_found = 1; /* Error reported further below */
13670 /* The second operand must be a reference type */
13671 if (!JREFERENCE_TYPE_P (op2_type))
13673 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13674 parse_error_context
13675 (wfl_operator, "Invalid argument %qs for %<instanceof%>",
13676 lang_printable_name (op2_type, 0));
13680 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13682 /* If the first operand is null, the result is always false */
13683 if (op1 == null_pointer_node)
13684 return boolean_false_node;
13685 else if (flag_emit_class_files)
13687 TREE_OPERAND (node, 1) = op2_type;
13688 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13691 /* Otherwise we have to invoke instance of to figure it out */
13693 return build_instanceof (op1, op2_type);
13695 /* There is no way the expression operand can be an instance of
13696 the type operand. This is a compile time error. */
13699 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13700 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13701 parse_error_context
13702 (wfl_operator, "Impossible for %qs to be instance of %qs",
13703 t1, lang_printable_name (op2_type, 0));
13710 /* 15.21 Bitwise and Logical Operators */
13714 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13715 /* Binary numeric promotion is performed on both operand and the
13716 expression retain that type */
13717 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13719 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13720 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13721 /* The type of the bitwise operator expression is BOOLEAN */
13722 prom_type = boolean_type_node;
13725 if (!JINTEGRAL_TYPE_P (op1_type))
13726 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13727 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13728 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13729 TREE_TYPE (node) = error_mark_node;
13731 /* Insert a break here if adding thing before the switch's
13732 break for this case */
13736 /* 15.22 Conditional-And Operator */
13737 case TRUTH_ANDIF_EXPR:
13738 /* 15.23 Conditional-Or Operator */
13739 case TRUTH_ORIF_EXPR:
13740 /* Operands must be of BOOLEAN type */
13741 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13742 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13744 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13745 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13746 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13747 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13748 TREE_TYPE (node) = boolean_type_node;
13752 else if (integer_zerop (op1))
13754 return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13756 else if (integer_onep (op1))
13758 return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13760 /* The type of the conditional operators is BOOLEAN */
13761 prom_type = boolean_type_node;
13764 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13769 /* The type of each of the operands must be a primitive numeric
13771 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13773 if (!JNUMERIC_TYPE_P (op1_type))
13774 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13775 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13776 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13777 TREE_TYPE (node) = boolean_type_node;
13781 /* Binary numeric promotion is performed on the operands */
13782 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13783 /* The type of the relation expression is always BOOLEAN */
13784 prom_type = boolean_type_node;
13787 /* 15.20 Equality Operator */
13790 /* It's time for us to patch the strings. */
13791 if ((cn = patch_string (op1)))
13794 op1_type = TREE_TYPE (op1);
13796 if ((cn = patch_string (op2)))
13799 op2_type = TREE_TYPE (op2);
13802 /* 15.20.1 Numerical Equality Operators == and != */
13803 /* Binary numeric promotion is performed on the operands */
13804 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13805 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13807 /* 15.20.2 Boolean Equality Operators == and != */
13808 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13809 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13810 ; /* Nothing to do here */
13812 /* 15.20.3 Reference Equality Operators == and != */
13813 /* Types have to be either references or the null type. If
13814 they're references, it must be possible to convert either
13815 type to the other by casting conversion. */
13816 else if ((op1 == null_pointer_node && op2 == null_pointer_node)
13817 || (op1 == null_pointer_node && JREFERENCE_TYPE_P (op2_type))
13818 || (JREFERENCE_TYPE_P (op1_type) && op2 == null_pointer_node)
13819 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13820 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13821 || valid_ref_assignconv_cast_p (op2_type,
13823 ; /* Nothing to do here */
13825 /* Else we have an error figure what can't be converted into
13826 what and report the error */
13830 t1 = xstrdup (lang_printable_name (op1_type, 0));
13831 parse_error_context
13833 "Incompatible type for %qs. Can't convert %qs to %qs",
13834 operator_string (node), t1,
13835 lang_printable_name (op2_type, 0));
13837 TREE_TYPE (node) = boolean_type_node;
13841 prom_type = boolean_type_node;
13848 return error_mark_node;
13850 TREE_OPERAND (node, 0) = op1;
13851 TREE_OPERAND (node, 1) = op2;
13852 TREE_TYPE (node) = prom_type;
13853 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13855 /* fold does not respect side-effect order as required for Java but not C.
13856 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13859 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13860 : ! TREE_SIDE_EFFECTS (node))
13861 node = fold (node);
13865 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13866 zero value, the value of CSTE comes after the valude of STRING */
13869 do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13871 const char *old = TREE_STRING_POINTER (cste);
13872 int old_len = TREE_STRING_LENGTH (cste);
13873 int len = old_len + string_len;
13874 char *new = alloca (len+1);
13878 memcpy (new, string, string_len);
13879 memcpy (&new [string_len], old, old_len);
13883 memcpy (new, old, old_len);
13884 memcpy (&new [old_len], string, string_len);
13887 return build_string (len, new);
13890 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13891 new STRING_CST on success, NULL_TREE on failure. */
13894 merge_string_cste (tree op1, tree op2, int after)
13896 /* Handle two string constants right away. */
13897 if (TREE_CODE (op2) == STRING_CST)
13898 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13899 TREE_STRING_LENGTH (op2), after);
13901 /* Reasonable integer constant can be treated right away. */
13902 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13904 static const char *const boolean_true = "true";
13905 static const char *const boolean_false = "false";
13906 static const char *const null_pointer = "null";
13908 const char *string;
13910 if (op2 == boolean_true_node)
13911 string = boolean_true;
13912 else if (op2 == boolean_false_node)
13913 string = boolean_false;
13914 else if (op2 == null_pointer_node
13915 || (integer_zerop (op2)
13916 && TREE_CODE (TREE_TYPE (op2)) == POINTER_TYPE))
13917 /* FIXME: null is not a compile-time constant, so it is only safe to
13918 merge if the overall expression is non-constant. However, this
13919 code always merges without checking the overall expression. */
13920 string = null_pointer;
13921 else if (TREE_TYPE (op2) == char_type_node)
13923 /* Convert the character into UTF-8. */
13924 unsigned int c = (unsigned int) TREE_INT_CST_LOW (op2);
13925 unsigned char *p = (unsigned char *) ch;
13926 if (0x01 <= c && c <= 0x7f)
13927 *p++ = (unsigned char) c;
13928 else if (c < 0x7ff)
13930 *p++ = (unsigned char) (c >> 6 | 0xc0);
13931 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13935 *p++ = (unsigned char) (c >> 12 | 0xe0);
13936 *p++ = (unsigned char) (((c >> 6) & 0x3f) | 0x80);
13937 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13944 string = string_convert_int_cst (op2);
13946 return do_merge_string_cste (op1, string, strlen (string), after);
13951 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13952 has to be a STRING_CST and the other part must be a STRING_CST or a
13953 INTEGRAL constant. Return a new STRING_CST if the operation
13954 succeed, NULL_TREE otherwise.
13956 If the case we want to optimize for space, we might want to return
13957 NULL_TREE for each invocation of this routine. FIXME */
13960 string_constant_concatenation (tree op1, tree op2)
13962 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13967 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13968 rest = (string == op1 ? op2 : op1);
13969 invert = (string == op1 ? 0 : 1 );
13971 /* Walk REST, only if it looks reasonable */
13972 if (TREE_CODE (rest) != STRING_CST
13973 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13974 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13975 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13977 rest = java_complete_tree (rest);
13978 if (rest == error_mark_node)
13979 return error_mark_node;
13980 rest = fold (rest);
13982 return merge_string_cste (string, rest, invert);
13987 /* Implement the `+' operator. Does static optimization if possible,
13988 otherwise create (if necessary) and append elements to a
13989 StringBuffer. The StringBuffer will be carried around until it is
13990 used for a function call or an assignment. Then toString() will be
13991 called on it to turn it into a String object. */
13994 build_string_concatenation (tree op1, tree op2)
13997 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13999 /* Try to do some static optimization */
14000 if ((result = string_constant_concatenation (op1, op2)))
14003 /* Discard empty strings on either side of the expression */
14004 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
14009 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
14012 /* If operands are string constant, turn then into object references */
14013 if (TREE_CODE (op1) == STRING_CST)
14014 op1 = patch_string_cst (op1);
14015 if (op2 && TREE_CODE (op2) == STRING_CST)
14016 op2 = patch_string_cst (op2);
14018 /* If either one of the constant is null and the other non null
14019 operand is a String constant, return it. */
14020 if ((TREE_CODE (op1) == STRING_CST) && !op2)
14023 /* If OP1 isn't already a StringBuffer, create and
14024 initialize a new one */
14025 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
14027 /* Two solutions here:
14028 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
14029 2) OP1 is something else, we call new StringBuffer().append(OP1). */
14030 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
14031 op1 = BUILD_STRING_BUFFER (op1);
14034 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
14035 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
14041 /* OP1 is no longer the last node holding a crafted StringBuffer */
14042 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
14043 /* Create a node for `{new...,xxx}.append (op2)' */
14044 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
14047 /* Mark the last node holding a crafted StringBuffer */
14048 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
14050 TREE_SIDE_EFFECTS (op1) = side_effects;
14054 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
14055 StringBuffer. If no string were found to be patched, return
14059 patch_string (tree node)
14061 if (node == error_mark_node)
14062 return error_mark_node;
14063 if (TREE_CODE (node) == STRING_CST)
14064 return patch_string_cst (node);
14065 else if (IS_CRAFTED_STRING_BUFFER_P (node))
14067 int saved = ctxp->explicit_constructor_p;
14068 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
14070 /* Temporary disable forbid the use of `this'. */
14071 ctxp->explicit_constructor_p = 0;
14072 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
14073 /* String concatenation arguments must be evaluated in order too. */
14074 ret = force_evaluation_order (ret);
14075 /* Restore it at its previous value */
14076 ctxp->explicit_constructor_p = saved;
14082 /* Build the internal representation of a string constant. */
14085 patch_string_cst (tree node)
14088 if (! flag_emit_class_files)
14090 node = get_identifier (TREE_STRING_POINTER (node));
14091 location = alloc_name_constant (CONSTANT_String, node);
14092 node = build_ref_from_constant_pool (location);
14094 TREE_CONSTANT (node) = 1;
14095 TREE_INVARIANT (node) = 1;
14097 /* ??? Guessing that the class file code can't handle casts. */
14098 if (! flag_emit_class_files)
14099 node = convert (string_ptr_type_node, node);
14101 TREE_TYPE (node) = string_ptr_type_node;
14106 /* Build an incomplete unary operator expression. */
14109 build_unaryop (int op_token, int op_location, tree op1)
14115 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14116 case MINUS_TK: op = NEGATE_EXPR; break;
14117 case NEG_TK: op = TRUTH_NOT_EXPR; break;
14118 case NOT_TK: op = BIT_NOT_EXPR; break;
14122 unaryop = build1 (op, NULL_TREE, op1);
14123 TREE_SIDE_EFFECTS (unaryop) = 1;
14124 /* Store the location of the operator, for better error report. The
14125 string of the operator will be rebuild based on the OP value. */
14126 EXPR_WFL_LINECOL (unaryop) = op_location;
14130 /* Special case for the ++/-- operators, since they require an extra
14131 argument to build, which is set to NULL and patched
14132 later. IS_POST_P is 1 if the operator, 0 otherwise. */
14135 build_incdec (int op_token, int op_location, tree op1, int is_post_p)
14137 static const enum tree_code lookup [2][2] =
14139 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14140 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14142 tree node = build2 (lookup [is_post_p][(op_token - DECR_TK)],
14143 NULL_TREE, op1, NULL_TREE);
14144 TREE_SIDE_EFFECTS (node) = 1;
14145 /* Store the location of the operator, for better error report. The
14146 string of the operator will be rebuild based on the OP value. */
14147 EXPR_WFL_LINECOL (node) = op_location;
14151 /* Build an incomplete cast operator, based on the use of the
14152 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14153 set. java_complete_tree is trained to walk a CONVERT_EXPR even
14154 though its type is already set. */
14157 build_cast (int location, tree type, tree exp)
14159 tree node = build1 (CONVERT_EXPR, type, exp);
14160 EXPR_WFL_LINECOL (node) = location;
14164 /* Build an incomplete class reference operator. */
14166 build_incomplete_class_ref (int location, tree class_name)
14168 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14169 tree class_decl = GET_CPC ();
14170 tree this_class = TREE_TYPE (class_decl);
14172 /* Generate the synthetic static method `class$'. (Previously we
14173 deferred this, causing different method tables to be emitted
14174 for native code and bytecode.) */
14175 if (!TYPE_DOT_CLASS (this_class)
14176 && !JPRIMITIVE_TYPE_P (class_name)
14177 && !(TREE_CODE (class_name) == VOID_TYPE))
14179 tree cpc_list = GET_CPC_LIST();
14180 tree cpc = cpc_list;
14183 /* For inner classes, add a 'class$' method to their outermost
14184 context, creating it if necessary. */
14186 while (GET_NEXT_ENCLOSING_CPC(cpc))
14187 cpc = GET_NEXT_ENCLOSING_CPC(cpc);
14188 class_decl = TREE_VALUE (cpc);
14190 target_class = TREE_TYPE (class_decl);
14192 if (CLASS_INTERFACE (TYPE_NAME (target_class)))
14194 /* For interfaces, adding a static 'class$' method directly
14195 is illegal. So create an inner class to contain the new
14196 method. Empirically this matches the behavior of javac. */
14198 /* We want the generated inner class inside the outermost class. */
14199 GET_CPC_LIST() = cpc;
14200 t = build_wfl_node (DECL_NAME (TYPE_NAME (object_type_node)));
14201 inner = create_anonymous_class (t);
14202 target_class = TREE_TYPE (inner);
14203 end_class_declaration (1);
14204 GET_CPC_LIST() = cpc_list;
14207 if (TYPE_DOT_CLASS (target_class) == NULL_TREE)
14208 build_dot_class_method (target_class);
14210 if (this_class != target_class)
14211 TYPE_DOT_CLASS (this_class) = TYPE_DOT_CLASS (target_class);
14214 EXPR_WFL_LINECOL (node) = location;
14218 /* Complete an incomplete class reference operator. */
14220 patch_incomplete_class_ref (tree node)
14222 tree type = TREE_OPERAND (node, 0);
14225 if (!(ref_type = resolve_type_during_patch (type)))
14226 return error_mark_node;
14228 /* If we're not emitting class files and we know ref_type is a
14229 compiled class, build a direct reference. */
14230 if ((! flag_emit_class_files && is_compiled_class (ref_type))
14231 || JPRIMITIVE_TYPE_P (ref_type)
14232 || TREE_CODE (ref_type) == VOID_TYPE)
14234 tree dot = build_class_ref (ref_type);
14235 /* A class referenced by `foo.class' is initialized. */
14236 if (!flag_emit_class_files)
14237 dot = build_class_init (ref_type, dot);
14238 return java_complete_tree (dot);
14241 /* If we're emitting class files and we have to deal with non
14242 primitive types, we invoke the synthetic static method `class$'. */
14243 ref_type = build_dot_class_method_invocation (current_class, ref_type);
14244 return java_complete_tree (ref_type);
14247 /* 15.14 Unary operators. We return error_mark_node in case of error,
14248 but preserve the type of NODE if the type is fixed. */
14251 patch_unaryop (tree node, tree wfl_op)
14253 tree op = TREE_OPERAND (node, 0);
14254 tree op_type = TREE_TYPE (op);
14255 tree prom_type = NULL_TREE, value, decl;
14256 int nested_field_flag = 0;
14257 int code = TREE_CODE (node);
14258 int error_found = 0;
14260 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14264 /* 15.13.2 Postfix Increment Operator ++ */
14265 case POSTINCREMENT_EXPR:
14266 /* 15.13.3 Postfix Increment Operator -- */
14267 case POSTDECREMENT_EXPR:
14268 /* 15.14.1 Prefix Increment Operator ++ */
14269 case PREINCREMENT_EXPR:
14270 /* 15.14.2 Prefix Decrement Operator -- */
14271 case PREDECREMENT_EXPR:
14272 op = decl = extract_field_decl (op);
14274 = nested_field_expanded_access_p (op, NULL, NULL, NULL);
14275 /* We might be trying to change an outer field accessed using
14277 if (nested_field_flag)
14279 /* Retrieve the decl of the field we're trying to access. We
14280 do that by first retrieving the function we would call to
14281 access the field. It has been already verified that this
14282 field isn't final */
14283 if (flag_emit_class_files)
14284 decl = TREE_OPERAND (op, 0);
14286 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14287 decl = DECL_FUNCTION_ACCESS_DECL (decl);
14289 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14290 else if (!JDECL_P (decl)
14291 && TREE_CODE (decl) != COMPONENT_REF
14292 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14293 && TREE_CODE (decl) != INDIRECT_REF
14294 && !(TREE_CODE (decl) == COMPOUND_EXPR
14295 && TREE_OPERAND (decl, 1)
14296 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14298 TREE_TYPE (node) = error_mark_node;
14302 /* From now on, we know that op if a variable and that it has a
14303 valid wfl. We use wfl_op to locate errors related to the
14305 if (!JNUMERIC_TYPE_P (op_type))
14307 parse_error_context
14308 (wfl_op, "Invalid argument type %qs to %qs",
14309 lang_printable_name (op_type, 0), operator_string (node));
14310 TREE_TYPE (node) = error_mark_node;
14315 /* Before the addition, binary numeric promotion is performed on
14316 both operands, if really necessary */
14317 if (JINTEGRAL_TYPE_P (op_type))
14319 value = build_int_cst (op_type, 1);
14320 TREE_TYPE (node) = op_type;
14324 value = build_int_cst (NULL_TREE, 1);
14326 binary_numeric_promotion (op_type,
14327 TREE_TYPE (value), &op, &value);
14330 /* We remember we might be accessing an outer field */
14331 if (nested_field_flag)
14333 /* We re-generate an access to the field */
14334 value = build2 (PLUS_EXPR, TREE_TYPE (op),
14335 build_nested_field_access (wfl_op, decl), value);
14337 /* And we patch the original access$() into a write
14338 with plus_op as a rhs */
14339 return nested_field_access_fix (node, op, value);
14342 /* And write back into the node. */
14343 TREE_OPERAND (node, 0) = op;
14344 TREE_OPERAND (node, 1) = value;
14345 /* Convert the overall back into its original type, if
14346 necessary, and return */
14347 if (JINTEGRAL_TYPE_P (op_type))
14348 return fold (node);
14350 return fold (convert (op_type, node));
14354 /* 15.14.3 Unary Plus Operator + */
14355 case UNARY_PLUS_EXPR:
14356 /* 15.14.4 Unary Minus Operator - */
14358 if (!JNUMERIC_TYPE_P (op_type))
14360 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14361 TREE_TYPE (node) = error_mark_node;
14364 /* Unary numeric promotion is performed on operand */
14367 op = do_unary_numeric_promotion (op);
14368 prom_type = TREE_TYPE (op);
14369 if (code == UNARY_PLUS_EXPR)
14374 /* 15.14.5 Bitwise Complement Operator ~ */
14376 if (!JINTEGRAL_TYPE_P (op_type))
14378 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14379 TREE_TYPE (node) = error_mark_node;
14384 op = do_unary_numeric_promotion (op);
14385 prom_type = TREE_TYPE (op);
14389 /* 15.14.6 Logical Complement Operator ! */
14390 case TRUTH_NOT_EXPR:
14391 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14393 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14394 /* But the type is known. We will report an error if further
14395 attempt of a assignment is made with this rhs */
14396 TREE_TYPE (node) = boolean_type_node;
14400 prom_type = boolean_type_node;
14403 /* 15.15 Cast Expression */
14405 value = patch_cast (node, wfl_operator);
14406 if (value == error_mark_node)
14408 /* If this cast is part of an assignment, we tell the code
14409 that deals with it not to complain about a mismatch,
14410 because things have been cast, anyways */
14411 TREE_TYPE (node) = error_mark_node;
14416 value = fold (value);
14422 /* This can only happen when the type is already known. */
14423 gcc_assert (TREE_TYPE (node) != NULL_TREE);
14424 prom_type = TREE_TYPE (node);
14429 return error_mark_node;
14431 /* There are cases where node has been replaced by something else
14432 and we don't end up returning here: UNARY_PLUS_EXPR,
14433 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14434 TREE_OPERAND (node, 0) = fold (op);
14435 TREE_TYPE (node) = prom_type;
14436 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14437 return fold (node);
14440 /* Generic type resolution that sometimes takes place during node
14441 patching. Returned the resolved type or generate an error
14442 message. Return the resolved type or NULL_TREE. */
14445 resolve_type_during_patch (tree type)
14447 if (unresolved_type_p (type, NULL))
14449 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14452 parse_error_context (type,
14453 "Class %qs not found in type declaration",
14454 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14458 check_deprecation (type, type_decl);
14460 return TREE_TYPE (type_decl);
14465 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14466 found. Otherwise NODE or something meant to replace it is returned. */
14469 patch_cast (tree node, tree wfl_op)
14471 tree op = TREE_OPERAND (node, 0);
14472 tree cast_type = TREE_TYPE (node);
14473 tree patched, op_type;
14476 /* Some string patching might be necessary at this stage */
14477 if ((patched = patch_string (op)))
14478 TREE_OPERAND (node, 0) = op = patched;
14479 op_type = TREE_TYPE (op);
14481 /* First resolve OP_TYPE if unresolved */
14482 if (!(cast_type = resolve_type_during_patch (cast_type)))
14483 return error_mark_node;
14485 /* Check on cast that are proven correct at compile time */
14486 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14489 if (cast_type == op_type)
14492 /* A narrowing conversion from a floating-point number to an
14493 integral type requires special handling (5.1.3). */
14494 if (JFLOAT_TYPE_P (op_type) && JINTEGRAL_TYPE_P (cast_type))
14495 if (cast_type != long_type_node)
14496 op = convert (integer_type_node, op);
14498 /* Try widening/narrowing conversion. Potentially, things need
14499 to be worked out in gcc so we implement the extreme cases
14500 correctly. fold_convert() needs to be fixed. */
14501 return convert (cast_type, op);
14504 /* It's also valid to cast a boolean into a boolean */
14505 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14508 /* null can be casted to references */
14509 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14510 return build_null_of_type (cast_type);
14512 /* The remaining legal casts involve conversion between reference
14513 types. Check for their compile time correctness. */
14514 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14515 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14517 TREE_TYPE (node) = promote_type (cast_type);
14518 /* Now, the case can be determined correct at compile time if
14519 OP_TYPE can be converted into CAST_TYPE by assignment
14520 conversion (5.2) */
14522 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14524 TREE_SET_CODE (node, NOP_EXPR);
14528 if (flag_emit_class_files)
14530 TREE_SET_CODE (node, CONVERT_EXPR);
14534 /* The cast requires a run-time check */
14535 return build3 (CALL_EXPR, promote_type (cast_type),
14536 build_address_of (soft_checkcast_node),
14537 tree_cons (NULL_TREE, build_class_ref (cast_type),
14538 build_tree_list (NULL_TREE, op)),
14542 /* Any other casts are proven incorrect at compile time */
14543 t1 = xstrdup (lang_printable_name (op_type, 0));
14544 parse_error_context (wfl_op, "Invalid cast from %qs to %qs",
14545 t1, lang_printable_name (cast_type, 0));
14547 return error_mark_node;
14550 /* Build a null constant and give it the type TYPE. */
14553 build_null_of_type (tree type)
14555 tree node = build_int_cst (promote_type (type), 0);
14559 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14560 a list of indices. */
14562 build_array_ref (int location, tree array, tree index)
14564 tree node = build4 (ARRAY_REF, NULL_TREE, array, index,
14565 NULL_TREE, NULL_TREE);
14566 EXPR_WFL_LINECOL (node) = location;
14570 /* 15.12 Array Access Expression */
14573 patch_array_ref (tree node)
14575 tree array = TREE_OPERAND (node, 0);
14576 tree array_type = TREE_TYPE (array);
14577 tree index = TREE_OPERAND (node, 1);
14578 tree index_type = TREE_TYPE (index);
14579 int error_found = 0;
14581 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14583 if (TREE_CODE (array_type) == POINTER_TYPE)
14584 array_type = TREE_TYPE (array_type);
14586 /* The array reference must be an array */
14587 if (!TYPE_ARRAY_P (array_type))
14589 parse_error_context
14591 "%<[]%> can only be applied to arrays. It can't be applied to %qs",
14592 lang_printable_name (array_type, 0));
14593 TREE_TYPE (node) = error_mark_node;
14597 /* The array index undergoes unary numeric promotion. The promoted
14598 type must be int */
14599 index = do_unary_numeric_promotion (index);
14600 if (TREE_TYPE (index) != int_type_node)
14602 if (valid_cast_to_p (index_type, int_type_node))
14603 parse_error_context (wfl_operator,
14604 "Incompatible type for %<[]%>. Explicit cast needed to convert %qs to %<int%>",
14605 lang_printable_name (index_type, 0));
14607 parse_error_context (wfl_operator,
14608 "Incompatible type for %<[]%>. Can't convert %qs to %<int%>",
14609 lang_printable_name (index_type, 0));
14610 TREE_TYPE (node) = error_mark_node;
14615 return error_mark_node;
14617 array_type = TYPE_ARRAY_ELEMENT (array_type);
14619 if (flag_emit_class_files)
14621 TREE_OPERAND (node, 0) = array;
14622 TREE_OPERAND (node, 1) = index;
14625 node = build_java_arrayaccess (array, array_type, index);
14626 TREE_TYPE (node) = array_type;
14630 /* 15.9 Array Creation Expressions */
14633 build_newarray_node (tree type, tree dims, int extra_dims)
14635 tree node = build3 (NEW_ARRAY_EXPR, NULL_TREE, type,
14637 build_int_cst (NULL_TREE, extra_dims));
14642 patch_newarray (tree node)
14644 tree type = TREE_OPERAND (node, 0);
14645 tree dims = TREE_OPERAND (node, 1);
14646 tree cdim, array_type;
14647 int error_found = 0;
14649 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14651 /* Dimension types are verified. It's better for the types to be
14652 verified in order. */
14653 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14656 tree dim = TREE_VALUE (cdim);
14658 /* Dim might have been saved during its evaluation */
14659 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14661 /* The type of each specified dimension must be an integral type. */
14662 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14665 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14666 promoted type must be int. */
14669 dim = do_unary_numeric_promotion (dim);
14670 if (TREE_TYPE (dim) != int_type_node)
14674 /* Report errors on types here */
14677 parse_error_context
14678 (TREE_PURPOSE (cdim),
14679 "Incompatible type for dimension in array creation expression. %s convert %qs to %<int%>",
14680 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14681 "Explicit cast needed to" : "Can't"),
14682 lang_printable_name (TREE_TYPE (dim), 0));
14686 TREE_PURPOSE (cdim) = NULL_TREE;
14689 /* Resolve array base type if unresolved */
14690 if (!(type = resolve_type_during_patch (type)))
14695 /* We don't want further evaluation of this bogus array creation
14697 TREE_TYPE (node) = error_mark_node;
14698 return error_mark_node;
14701 /* Set array_type to the actual (promoted) array type of the result. */
14702 if (TREE_CODE (type) == RECORD_TYPE)
14703 type = build_pointer_type (type);
14704 while (--xdims >= 0)
14706 type = promote_type (build_java_array_type (type, -1));
14708 dims = nreverse (dims);
14710 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14714 = build_java_array_type (type,
14715 TREE_CODE (cdim) == INTEGER_CST
14716 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14718 array_type = promote_type (array_type);
14720 dims = nreverse (dims);
14722 /* The node is transformed into a function call. Things are done
14723 differently according to the number of dimensions. If the number
14724 of dimension is equal to 1, then the nature of the base type
14725 (primitive or not) matters. */
14727 return build_new_array (type, TREE_VALUE (dims));
14729 /* Can't reuse what's already written in expr.c because it uses the
14730 JVM stack representation. Provide a build_multianewarray. FIXME */
14731 return build3 (CALL_EXPR, array_type,
14732 build_address_of (soft_multianewarray_node),
14733 tree_cons (NULL_TREE,
14734 build_class_ref (TREE_TYPE (array_type)),
14735 tree_cons (NULL_TREE,
14736 build_int_cst (NULL_TREE, ndims),
14741 /* 10.6 Array initializer. */
14743 /* Build a wfl for array element that don't have one, so we can
14744 pin-point errors. */
14747 maybe_build_array_element_wfl (tree node)
14749 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14751 /* FIXME - old code used "prev_lc.line" and "elc.prev_col */
14752 return build_expr_wfl (NULL_TREE,
14753 #ifdef USE_MAPPED_LOCATION
14757 ctxp->lexer->token_start.line,
14758 ctxp->lexer->token_start.col
14766 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14767 identification of initialized arrays easier to detect during walk
14771 build_new_array_init (int location, tree values)
14773 tree constructor = build_constructor (NULL_TREE, values);
14774 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14775 EXPR_WFL_LINECOL (to_return) = location;
14779 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14780 occurred. Otherwise return NODE after having set its type
14784 patch_new_array_init (tree type, tree node)
14786 int error_seen = 0;
14787 tree current, element_type;
14788 HOST_WIDE_INT length;
14789 int all_constant = 1;
14790 tree init = TREE_OPERAND (node, 0);
14792 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14794 parse_error_context (node,
14795 "Invalid array initializer for non-array type %qs",
14796 lang_printable_name (type, 1));
14797 return error_mark_node;
14799 type = TREE_TYPE (type);
14800 element_type = TYPE_ARRAY_ELEMENT (type);
14802 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14804 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14805 current; length++, current = TREE_CHAIN (current))
14807 tree elt = TREE_VALUE (current);
14808 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14810 error_seen |= array_constructor_check_entry (element_type, current);
14811 elt = TREE_VALUE (current);
14812 /* When compiling to native code, STRING_CST is converted to
14813 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14814 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14819 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14820 TREE_PURPOSE (current) = NULL_TREE;
14823 if (elt && TREE_CODE (elt) == TREE_LIST
14824 && TREE_VALUE (elt) == error_mark_node)
14829 return error_mark_node;
14831 /* Create a new type. We can't reuse the one we have here by
14832 patching its dimension because it originally is of dimension -1
14833 hence reused by gcc. This would prevent triangular arrays. */
14834 type = build_java_array_type (element_type, length);
14835 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14836 TREE_TYPE (node) = promote_type (type);
14837 TREE_CONSTANT (init) = all_constant;
14838 TREE_INVARIANT (init) = all_constant;
14839 TREE_CONSTANT (node) = all_constant;
14840 TREE_INVARIANT (node) = all_constant;
14844 /* Verify that one entry of the initializer element list can be
14845 assigned to the array base type. Report 1 if an error occurred, 0
14849 array_constructor_check_entry (tree type, tree entry)
14851 char *array_type_string = NULL; /* For error reports */
14852 tree value, type_value, new_value, wfl_value, patched;
14853 int error_seen = 0;
14855 new_value = NULL_TREE;
14856 wfl_value = TREE_VALUE (entry);
14858 value = java_complete_tree (TREE_VALUE (entry));
14859 /* patch_string return error_mark_node if arg is error_mark_node */
14860 if ((patched = patch_string (value)))
14862 if (value == error_mark_node)
14865 type_value = TREE_TYPE (value);
14867 /* At anytime, try_builtin_assignconv can report a warning on
14868 constant overflow during narrowing. */
14869 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14870 new_value = try_builtin_assignconv (wfl_operator, type, value);
14871 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14872 type_value = promote_type (type);
14874 /* Check and report errors */
14877 const char *const msg = (!valid_cast_to_p (type_value, type) ?
14878 "Can't" : "Explicit cast needed to");
14879 if (!array_type_string)
14880 array_type_string = xstrdup (lang_printable_name (type, 1));
14881 parse_error_context
14882 (wfl_operator, "Incompatible type for array. %s convert %qs to %qs",
14883 msg, lang_printable_name (type_value, 1), array_type_string);
14888 TREE_VALUE (entry) = new_value;
14890 if (array_type_string)
14891 free (array_type_string);
14893 TREE_PURPOSE (entry) = NULL_TREE;
14898 build_this (int location)
14900 tree node = build_wfl_node (this_identifier_node);
14901 TREE_SET_CODE (node, THIS_EXPR);
14902 EXPR_WFL_LINECOL (node) = location;
14906 /* 14.15 The return statement. It builds a modify expression that
14907 assigns the returned value to the RESULT_DECL that hold the value
14911 build_return (int location, tree op)
14913 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14914 EXPR_WFL_LINECOL (node) = location;
14915 node = build_debugable_stmt (location, node);
14920 patch_return (tree node)
14922 tree return_exp = TREE_OPERAND (node, 0);
14923 tree meth = current_function_decl;
14924 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14925 int error_found = 0;
14927 TREE_TYPE (node) = error_mark_node;
14928 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14930 /* It's invalid to have a return value within a function that is
14931 declared with the keyword void or that is a constructor */
14932 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14935 /* It's invalid to use a return statement in a static block */
14936 if (DECL_CLINIT_P (current_function_decl))
14939 /* It's invalid to have a no return value within a function that
14940 isn't declared with the keyword `void' */
14941 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14944 if (DECL_INSTINIT_P (current_function_decl))
14949 if (DECL_INSTINIT_P (current_function_decl))
14950 parse_error_context (wfl_operator,
14951 "%<return%> inside instance initializer");
14953 else if (DECL_CLINIT_P (current_function_decl))
14954 parse_error_context (wfl_operator,
14955 "%<return%> inside static initializer");
14957 else if (!DECL_CONSTRUCTOR_P (meth))
14959 char *t = xstrdup (lang_printable_name (mtype, 0));
14960 parse_error_context (wfl_operator,
14961 "%<return%> with%s value from %<%s %s%>",
14962 (error_found == 1 ? "" : "out"),
14963 t, lang_printable_name (meth, 2));
14967 parse_error_context (wfl_operator,
14968 "%<return%> with value from constructor %qs",
14969 lang_printable_name (meth, 2));
14970 return error_mark_node;
14973 /* If we have a return_exp, build a modify expression and expand
14974 it. Note: at that point, the assignment is declared valid, but we
14975 may want to carry some more hacks */
14978 tree exp = java_complete_tree (return_exp);
14979 tree modify, patched;
14981 if ((patched = patch_string (exp)))
14984 modify = build2 (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14985 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14986 modify = java_complete_tree (modify);
14988 if (modify != error_mark_node)
14990 TREE_SIDE_EFFECTS (modify) = 1;
14991 TREE_OPERAND (node, 0) = modify;
14994 return error_mark_node;
14996 TREE_TYPE (node) = void_type_node;
14997 TREE_SIDE_EFFECTS (node) = 1;
15001 /* 14.8 The if Statement */
15004 build_if_else_statement (int location, tree expression, tree if_body,
15009 else_body = build_java_empty_stmt ();
15010 node = build3 (COND_EXPR, NULL_TREE, expression, if_body, else_body);
15011 EXPR_WFL_LINECOL (node) = location;
15012 node = build_debugable_stmt (location, node);
15017 patch_if_else_statement (tree node)
15019 tree expression = TREE_OPERAND (node, 0);
15020 int can_complete_normally
15021 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15022 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
15024 TREE_TYPE (node) = error_mark_node;
15025 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15027 /* The type of expression must be boolean */
15028 if (TREE_TYPE (expression) != boolean_type_node
15029 && TREE_TYPE (expression) != promoted_boolean_type_node)
15031 parse_error_context
15033 "Incompatible type for %<if%>. Can't convert %qs to %<boolean%>",
15034 lang_printable_name (TREE_TYPE (expression), 0));
15035 return error_mark_node;
15038 TREE_TYPE (node) = void_type_node;
15039 TREE_SIDE_EFFECTS (node) = 1;
15040 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
15044 /* 14.6 Labeled Statements */
15046 /* Action taken when a labeled statement is parsed. a new
15047 LABELED_BLOCK_EXPR is created. No statement is attached to the
15048 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
15051 build_labeled_block (int location, tree label)
15054 tree label_decl, node;
15055 if (label == NULL_TREE || label == continue_identifier_node)
15056 label_name = label;
15059 label_name = merge_qualified_name (label_id, label);
15060 /* Issue an error if we try to reuse a label that was previously
15062 if (IDENTIFIER_LOCAL_VALUE (label_name))
15064 EXPR_WFL_LINECOL (wfl_operator) = location;
15065 parse_error_context (wfl_operator,
15066 "Declaration of %qs shadows a previous label declaration",
15067 IDENTIFIER_POINTER (label));
15068 EXPR_WFL_LINECOL (wfl_operator) =
15069 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
15070 parse_error_context (wfl_operator,
15071 "This is the location of the previous declaration of label %qs",
15072 IDENTIFIER_POINTER (label));
15073 java_error_count--;
15077 label_decl = create_label_decl (label_name);
15078 node = build2 (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
15079 EXPR_WFL_LINECOL (node) = location;
15080 TREE_SIDE_EFFECTS (node) = 1;
15084 /* A labeled statement LBE is attached a statement. */
15087 finish_labeled_statement (tree lbe, /* Labeled block expr */
15090 /* In anyways, tie the loop to its statement */
15091 LABELED_BLOCK_BODY (lbe) = statement;
15092 pop_labeled_block ();
15093 POP_LABELED_BLOCK ();
15097 /* 14.10, 14.11, 14.12 Loop Statements */
15099 /* Create an empty LOOP_EXPR and make it the last in the nested loop
15103 build_new_loop (tree loop_body)
15105 tree loop = build1 (LOOP_EXPR, NULL_TREE, loop_body);
15106 TREE_SIDE_EFFECTS (loop) = 1;
15111 /* Create a loop body according to the following structure:
15113 COMPOUND_EXPR (loop main body)
15114 EXIT_EXPR (this order is for while/for loops.
15115 LABELED_BLOCK_EXPR the order is reversed for do loops)
15116 LABEL_DECL (a continue occurring here branches at the
15117 BODY end of this labeled block)
15120 REVERSED, if nonzero, tells that the loop condition expr comes
15121 after the body, like in the do-while loop.
15123 To obtain a loop, the loop body structure described above is
15124 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15127 LABEL_DECL (use this label to exit the loop)
15129 <structure described above> */
15132 build_loop_body (int location, tree condition, int reversed)
15134 tree first, second, body;
15136 condition = build1 (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15137 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15138 condition = build_debugable_stmt (location, condition);
15139 TREE_SIDE_EFFECTS (condition) = 1;
15141 body = build_labeled_block (0, continue_identifier_node);
15142 first = (reversed ? body : condition);
15143 second = (reversed ? condition : body);
15144 return build2 (COMPOUND_EXPR, NULL_TREE,
15145 build2 (COMPOUND_EXPR, NULL_TREE, first, second),
15146 build_java_empty_stmt ());
15149 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15150 their order) on the current loop. Unlink the current loop from the
15154 finish_loop_body (int location, tree condition, tree body, int reversed)
15156 tree to_return = ctxp->current_loop;
15157 tree loop_body = LOOP_EXPR_BODY (to_return);
15160 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15161 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15162 The real EXIT_EXPR is one operand further. */
15163 EXPR_WFL_LINECOL (cnode) = location;
15164 if (TREE_CODE (cnode) == EXPR_WITH_FILE_LOCATION)
15166 cnode = EXPR_WFL_NODE (cnode);
15167 /* This one is for accurate error reports */
15168 EXPR_WFL_LINECOL (cnode) = location;
15170 TREE_OPERAND (cnode, 0) = condition;
15172 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15177 /* Tailored version of finish_loop_body for FOR loops, when FOR
15178 loops feature the condition part */
15181 finish_for_loop (int location, tree condition, tree update, tree body)
15183 /* Put the condition and the loop body in place */
15184 tree loop = finish_loop_body (location, condition, body, 0);
15185 /* LOOP is the current loop which has been now popped of the loop
15186 stack. Mark the update block as reachable and install it. We do
15187 this because the (current interpretation of the) JLS requires
15188 that the update expression be considered reachable even if the
15189 for loop's body doesn't complete normally. */
15190 if (update != NULL_TREE && !IS_EMPTY_STMT (update))
15193 if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15194 up2 = EXPR_WFL_NODE (up2);
15195 /* It is possible for the update expression to be an
15196 EXPR_WFL_NODE wrapping nothing. */
15197 if (up2 != NULL_TREE && !IS_EMPTY_STMT (up2))
15199 /* Try to detect constraint violations. These would be
15200 programming errors somewhere. */
15201 if (! EXPR_P (up2) || TREE_CODE (up2) == LOOP_EXPR)
15203 SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15206 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15210 /* Try to find the loop a block might be related to. This comprises
15211 the case where the LOOP_EXPR is found as the second operand of a
15212 COMPOUND_EXPR, because the loop happens to have an initialization
15213 part, then expressed as the first operand of the COMPOUND_EXPR. If
15214 the search finds something, 1 is returned. Otherwise, 0 is
15215 returned. The search is assumed to start from a
15216 LABELED_BLOCK_EXPR's block. */
15219 search_loop (tree statement)
15221 if (TREE_CODE (statement) == LOOP_EXPR)
15224 if (TREE_CODE (statement) == BLOCK)
15225 statement = BLOCK_SUBBLOCKS (statement);
15229 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15230 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15231 statement = TREE_OPERAND (statement, 1);
15233 return (TREE_CODE (statement) == LOOP_EXPR
15234 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15237 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15238 returned otherwise. */
15241 labeled_block_contains_loop_p (tree block, tree loop)
15246 if (LABELED_BLOCK_BODY (block) == loop)
15249 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15255 /* If the loop isn't surrounded by a labeled statement, create one and
15256 insert LOOP as its body. */
15259 patch_loop_statement (tree loop)
15263 TREE_TYPE (loop) = void_type_node;
15264 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15267 loop_label = build_labeled_block (0, NULL_TREE);
15268 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15269 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15270 LABELED_BLOCK_BODY (loop_label) = loop;
15271 PUSH_LABELED_BLOCK (loop_label);
15275 /* 14.13, 14.14: break and continue Statements */
15277 /* Build a break or a continue statement. a null NAME indicates an
15278 unlabeled break/continue statement. */
15281 build_bc_statement (int location, int is_break, tree name)
15283 tree break_continue, label_block_expr = NULL_TREE;
15287 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15288 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15289 /* Null means that we don't have a target for this named
15290 break/continue. In this case, we make the target to be the
15291 label name, so that the error can be reported accurately in
15292 patch_bc_statement. */
15293 label_block_expr = EXPR_WFL_NODE (name);
15295 /* Unlabeled break/continue will be handled during the
15296 break/continue patch operation */
15297 break_continue = build1 (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr);
15299 IS_BREAK_STMT_P (break_continue) = is_break;
15300 TREE_SIDE_EFFECTS (break_continue) = 1;
15301 EXPR_WFL_LINECOL (break_continue) = location;
15302 break_continue = build_debugable_stmt (location, break_continue);
15303 return break_continue;
15306 /* Verification of a break/continue statement. */
15309 patch_bc_statement (tree node)
15311 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15312 tree labeled_block = ctxp->current_labeled_block;
15313 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15315 /* Having an identifier here means that the target is unknown. */
15316 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15318 parse_error_context (wfl_operator, "No label definition found for %qs",
15319 IDENTIFIER_POINTER (bc_label));
15320 return error_mark_node;
15322 if (! IS_BREAK_STMT_P (node))
15324 /* It's a continue statement. */
15325 for (;; labeled_block = TREE_CHAIN (labeled_block))
15327 if (labeled_block == NULL_TREE)
15329 if (bc_label == NULL_TREE)
15330 parse_error_context (wfl_operator,
15331 "%<continue%> must be in loop");
15333 parse_error_context
15334 (wfl_operator, "continue label %qs does not name a loop",
15335 IDENTIFIER_POINTER (bc_label));
15336 return error_mark_node;
15338 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15339 == continue_identifier_node)
15340 && (bc_label == NULL_TREE
15341 || TREE_CHAIN (labeled_block) == bc_label))
15343 bc_label = labeled_block;
15348 else if (!bc_label)
15350 for (;; labeled_block = TREE_CHAIN (labeled_block))
15352 if (labeled_block == NULL_TREE)
15354 parse_error_context (wfl_operator,
15355 "%<break%> must be in loop or switch");
15356 return error_mark_node;
15358 target_stmt = LABELED_BLOCK_BODY (labeled_block);
15359 if (TREE_CODE (target_stmt) == SWITCH_EXPR
15360 || search_loop (target_stmt))
15362 bc_label = labeled_block;
15368 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15369 CAN_COMPLETE_NORMALLY (bc_label) = 1;
15371 /* Our break/continue don't return values. */
15372 TREE_TYPE (node) = void_type_node;
15373 /* Encapsulate the break within a compound statement so that it's
15374 expanded all the times by expand_expr (and not clobbered
15375 sometimes, like after a if statement) */
15376 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15377 TREE_SIDE_EFFECTS (node) = 1;
15381 /* Process the exit expression belonging to a loop. Its type must be
15385 patch_exit_expr (tree node)
15387 tree expression = TREE_OPERAND (node, 0);
15388 TREE_TYPE (node) = error_mark_node;
15389 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15391 /* The type of expression must be boolean */
15392 if (TREE_TYPE (expression) != boolean_type_node)
15394 parse_error_context
15396 "Incompatible type for loop conditional. Can't convert %qs to %<boolean%>",
15397 lang_printable_name (TREE_TYPE (expression), 0));
15398 return error_mark_node;
15400 /* Now we know things are allright, invert the condition, fold and
15402 TREE_OPERAND (node, 0) =
15403 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15405 if (! integer_zerop (TREE_OPERAND (node, 0))
15406 && ctxp->current_loop != NULL_TREE
15407 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15408 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15409 if (! integer_onep (TREE_OPERAND (node, 0)))
15410 CAN_COMPLETE_NORMALLY (node) = 1;
15413 TREE_TYPE (node) = void_type_node;
15417 /* 14.9 Switch statement */
15420 patch_switch_statement (tree node)
15422 tree se = TREE_OPERAND (node, 0), se_type;
15425 /* Complete the switch expression */
15426 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15427 se_type = TREE_TYPE (se);
15428 /* The type of the switch expression must be char, byte, short or
15430 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15432 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15433 parse_error_context (wfl_operator,
15434 "Incompatible type for %<switch%>. Can't convert %qs to %<int%>",
15435 lang_printable_name (se_type, 0));
15436 /* This is what java_complete_tree will check */
15437 TREE_OPERAND (node, 0) = error_mark_node;
15438 return error_mark_node;
15441 /* Save and restore the outer case label list. */
15442 save = case_label_list;
15443 case_label_list = NULL_TREE;
15445 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15447 /* See if we've found a duplicate label. We can't leave this until
15448 code generation, because in `--syntax-only' and `-C' modes we
15449 don't do ordinary code generation. */
15450 for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15452 HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15454 for (subiter = TREE_CHAIN (iter);
15455 subiter != NULL_TREE;
15456 subiter = TREE_CHAIN (subiter))
15458 HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15461 EXPR_WFL_LINECOL (wfl_operator)
15462 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15463 /* The case_label_list is in reverse order, so print the
15464 outer label first. */
15465 parse_error_context (wfl_operator, "duplicate case label: %<"
15466 HOST_WIDE_INT_PRINT_DEC "%>", subval);
15467 EXPR_WFL_LINECOL (wfl_operator)
15468 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15469 parse_error_context (wfl_operator, "original label is here");
15476 case_label_list = save;
15478 /* Ready to return */
15479 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15481 TREE_TYPE (node) = error_mark_node;
15482 return error_mark_node;
15484 TREE_TYPE (node) = void_type_node;
15485 TREE_SIDE_EFFECTS (node) = 1;
15486 CAN_COMPLETE_NORMALLY (node)
15487 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15488 || ! SWITCH_HAS_DEFAULT (node);
15494 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15495 might be NULL_TREE. */
15498 #ifdef USE_MAPPED_LOCATION
15499 source_location location,
15503 tree condition, tree value)
15506 tree klass = GET_CPC ();
15508 if (! enable_assertions (klass))
15510 condition = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15511 boolean_false_node, condition);
15512 if (value == NULL_TREE)
15513 value = build_java_empty_stmt ();
15514 return build_if_else_statement (location, condition,
15518 if (! CLASS_USES_ASSERTIONS (klass))
15520 tree field, classdollar, id, call;
15521 tree class_type = TREE_TYPE (klass);
15523 field = add_field (class_type,
15524 get_identifier ("$assertionsDisabled"),
15526 ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15527 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15528 FIELD_SYNTHETIC (field) = 1;
15530 classdollar = build_incomplete_class_ref (location, class_type);
15532 /* Call CLASS.desiredAssertionStatus(). */
15533 id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15534 call = build3 (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15535 call = make_qualified_primary (classdollar, call, location);
15536 TREE_SIDE_EFFECTS (call) = 1;
15538 /* Invert to obtain !CLASS.desiredAssertionStatus(). This may
15539 seem odd, but we do it to generate code identical to that of
15541 call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15542 TREE_SIDE_EFFECTS (call) = 1;
15543 DECL_INITIAL (field) = call;
15545 /* Record the initializer in the initializer statement list. */
15546 call = build2 (MODIFY_EXPR, NULL_TREE, field, call);
15547 TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15548 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15549 MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15551 CLASS_USES_ASSERTIONS (klass) = 1;
15554 if (value != NULL_TREE)
15555 value = tree_cons (NULL_TREE, value, NULL_TREE);
15557 node = build_wfl_node (get_identifier ("java"));
15558 node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15560 node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15563 node = build3 (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15564 TREE_SIDE_EFFECTS (node) = 1;
15565 /* It is too early to use BUILD_THROW. */
15566 node = build1 (THROW_EXPR, NULL_TREE, node);
15567 TREE_SIDE_EFFECTS (node) = 1;
15569 /* We invert the condition; if we just put NODE as the `else' part
15570 then we generate weird-looking bytecode. */
15571 condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15572 /* Check $assertionsDisabled. */
15574 = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15575 build1 (TRUTH_NOT_EXPR, NULL_TREE,
15576 build_wfl_node (get_identifier ("$assertionsDisabled"))),
15578 node = build_if_else_statement (location, condition, node, NULL_TREE);
15582 /* 14.18 The try/catch statements */
15584 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15585 catches TYPE and executes CATCH_STMTS. */
15588 encapsulate_with_try_catch (int location, tree type_or_name, tree try_stmts,
15591 tree try_block, catch_clause_param, catch_block, catch;
15593 /* First build a try block */
15594 try_block = build_expr_block (try_stmts, NULL_TREE);
15596 /* Build a catch block: we need a catch clause parameter */
15597 if (TREE_CODE (type_or_name) == EXPR_WITH_FILE_LOCATION)
15599 tree catch_type = obtain_incomplete_type (type_or_name);
15601 catch_clause_param = build_decl (VAR_DECL, wpv_id, catch_type);
15602 register_incomplete_type (JDEP_VARIABLE, type_or_name,
15603 catch_clause_param, catch_type);
15604 dep = CLASSD_LAST (ctxp->classd_list);
15605 JDEP_GET_PATCH (dep) = &TREE_TYPE (catch_clause_param);
15608 catch_clause_param = build_decl (VAR_DECL, wpv_id,
15609 build_pointer_type (type_or_name));
15612 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15614 /* Initialize the variable and store in the block */
15615 catch = build2 (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15616 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15617 add_stmt_to_block (catch_block, NULL_TREE, catch);
15619 /* Add the catch statements */
15620 add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15622 /* Now we can build a JAVA_CATCH_EXPR */
15623 catch_block = build1 (JAVA_CATCH_EXPR, NULL_TREE, catch_block);
15625 return build_try_statement (location, try_block, catch_block);
15629 build_try_statement (int location, tree try_block, tree catches)
15631 tree node = build2 (TRY_EXPR, NULL_TREE, try_block, catches);
15632 EXPR_WFL_LINECOL (node) = location;
15637 build_try_finally_statement (int location, tree try_block, tree finally)
15639 tree node = build2 (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15640 EXPR_WFL_LINECOL (node) = location;
15645 patch_try_statement (tree node)
15647 int error_found = 0;
15648 tree try = TREE_OPERAND (node, 0);
15649 /* Exception handlers are considered in left to right order */
15650 tree catch = nreverse (TREE_OPERAND (node, 1));
15651 tree current, caught_type_list = NULL_TREE;
15653 /* Check catch clauses, if any. Every time we find an error, we try
15654 to process the next catch clause. We process the catch clause before
15655 the try block so that when processing the try block we can check thrown
15656 exceptions against the caught type list. */
15657 for (current = catch; current; current = TREE_CHAIN (current))
15659 tree carg_decl, carg_type;
15660 tree sub_current, catch_block, catch_clause;
15663 /* At this point, the structure of the catch clause is
15664 JAVA_CATCH_EXPR (catch node)
15665 BLOCK (with the decl of the parameter)
15667 MODIFY_EXPR (assignment of the catch parameter)
15668 BLOCK (catch clause block)
15670 catch_clause = TREE_OPERAND (current, 0);
15671 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15672 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15674 /* Catch clauses can't have more than one parameter declared,
15675 but it's already enforced by the grammar. Make sure that the
15676 only parameter of the clause statement in of class Throwable
15677 or a subclass of Throwable, but that was done earlier. The
15678 catch clause parameter type has also been resolved. */
15680 /* Just make sure that the catch clause parameter type inherits
15681 from java.lang.Throwable */
15682 if (!inherits_from_p (carg_type, throwable_type_node))
15684 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15685 parse_error_context (wfl_operator,
15686 "Can't catch class %qs. Catch clause parameter type must be a subclass of class %<java.lang.Throwable%>",
15687 lang_printable_name (carg_type, 0));
15692 /* Partial check for unreachable catch statement: The catch
15693 clause is reachable iff is no earlier catch block A in
15694 the try statement such that the type of the catch
15695 clause's parameter is the same as or a subclass of the
15696 type of A's parameter */
15698 for (sub_current = catch;
15699 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15701 tree sub_catch_clause, decl;
15702 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15703 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15705 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15707 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15708 parse_error_context
15710 "%<catch%> not reached because of the catch clause at line %d",
15711 EXPR_WFL_LINENO (sub_current));
15712 unreachable = error_found = 1;
15716 /* Complete the catch clause block */
15717 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15718 if (catch_block == error_mark_node)
15723 if (CAN_COMPLETE_NORMALLY (catch_block))
15724 CAN_COMPLETE_NORMALLY (node) = 1;
15725 TREE_OPERAND (current, 0) = catch_block;
15730 /* Things to do here: the exception must be thrown */
15732 /* Link this type to the caught type list */
15733 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15736 PUSH_EXCEPTIONS (caught_type_list);
15737 if ((try = java_complete_tree (try)) == error_mark_node)
15739 if (CAN_COMPLETE_NORMALLY (try))
15740 CAN_COMPLETE_NORMALLY (node) = 1;
15743 /* Verification ends here */
15745 return error_mark_node;
15747 TREE_OPERAND (node, 0) = try;
15748 TREE_OPERAND (node, 1) = catch;
15749 TREE_TYPE (node) = void_type_node;
15753 /* 14.17 The synchronized Statement */
15756 patch_synchronized_statement (tree node, tree wfl_op1)
15758 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15759 tree block = TREE_OPERAND (node, 1);
15761 tree tmp, enter, exit, expr_decl, assignment;
15763 if (expr == error_mark_node)
15765 block = java_complete_tree (block);
15769 /* We might be trying to synchronize on a STRING_CST */
15770 if ((tmp = patch_string (expr)))
15773 /* The TYPE of expr must be a reference type */
15774 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15776 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15777 parse_error_context (wfl_operator, "Incompatible type for %<synchronized%>. Can't convert %qs to %<java.lang.Object%>",
15778 lang_printable_name (TREE_TYPE (expr), 0));
15779 return error_mark_node;
15782 /* Generate a try-finally for the synchronized statement, except
15783 that the handler that catches all throw exception calls
15784 _Jv_MonitorExit and then rethrow the exception.
15785 The synchronized statement is then implemented as:
15788 _Jv_MonitorEnter (expression)
15790 _Jv_MonitorExit (expression)
15794 e = _Jv_exception_info ();
15795 _Jv_MonitorExit (expression)
15799 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15800 BUILD_MONITOR_ENTER (enter, expr_decl);
15801 BUILD_MONITOR_EXIT (exit, expr_decl);
15802 CAN_COMPLETE_NORMALLY (enter) = 1;
15803 CAN_COMPLETE_NORMALLY (exit) = 1;
15804 assignment = build2 (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15805 TREE_SIDE_EFFECTS (assignment) = 1;
15806 node = build2 (COMPOUND_EXPR, NULL_TREE,
15807 build2 (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15808 build2 (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15809 node = build_expr_block (node, expr_decl);
15811 return java_complete_tree (node);
15814 /* 14.16 The throw Statement */
15817 patch_throw_statement (tree node, tree wfl_op1)
15819 tree expr = TREE_OPERAND (node, 0);
15820 tree type = TREE_TYPE (expr);
15821 int unchecked_ok = 0, tryblock_throws_ok = 0;
15823 /* Thrown expression must be assignable to java.lang.Throwable */
15824 if (!try_reference_assignconv (throwable_type_node, expr))
15826 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15827 parse_error_context (wfl_operator,
15828 "Can't throw %qs; it must be a subclass of class %<java.lang.Throwable%>",
15829 lang_printable_name (type, 0));
15830 /* If the thrown expression was a reference, we further the
15831 compile-time check. */
15832 if (!JREFERENCE_TYPE_P (type))
15833 return error_mark_node;
15836 /* At least one of the following must be true */
15838 /* The type of the throw expression is a not checked exception,
15839 i.e. is a unchecked expression. */
15840 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15842 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15843 /* An instance can't throw a checked exception unless that exception
15844 is explicitly declared in the `throws' clause of each
15845 constructor. This doesn't apply to anonymous classes, since they
15846 don't have declared constructors. */
15848 && DECL_INSTINIT_P (current_function_decl)
15849 && !ANONYMOUS_CLASS_P (current_class))
15852 for (current = TYPE_METHODS (current_class); current;
15853 current = TREE_CHAIN (current))
15854 if (DECL_CONSTRUCTOR_P (current)
15855 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15857 parse_error_context (wfl_operator, "Checked exception %qs can't be thrown in instance initializer (not all declared constructor are declaring it in their %<throws%> clause)",
15858 lang_printable_name (TREE_TYPE (expr), 0));
15859 return error_mark_node;
15863 /* Throw is contained in a try statement and at least one catch
15864 clause can receive the thrown expression or the current method is
15865 declared to throw such an exception. Or, the throw statement is
15866 contained in a method or constructor declaration and the type of
15867 the Expression is assignable to at least one type listed in the
15868 throws clause the declaration. */
15870 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15871 if (!(unchecked_ok || tryblock_throws_ok))
15873 /* If there is a surrounding try block that has no matching
15874 clatch clause, report it first. A surrounding try block exits
15875 only if there is something after the list of checked
15876 exception thrown by the current function (if any). */
15877 if (IN_TRY_BLOCK_P ())
15878 parse_error_context (wfl_operator, "Checked exception %qs can't be caught by any of the catch clause(s) of the surrounding %<try%> block",
15879 lang_printable_name (type, 0));
15880 /* If we have no surrounding try statement and the method doesn't have
15881 any throws, report it now. FIXME */
15883 /* We report that the exception can't be throw from a try block
15884 in all circumstances but when the `throw' is inside a static
15886 else if (!EXCEPTIONS_P (currently_caught_type_list)
15887 && !tryblock_throws_ok)
15889 if (DECL_CLINIT_P (current_function_decl))
15890 parse_error_context (wfl_operator,
15891 "Checked exception %qs can't be thrown in initializer",
15892 lang_printable_name (type, 0));
15894 parse_error_context (wfl_operator,
15895 "Checked exception %qs isn't thrown from a %<try%> block",
15896 lang_printable_name (type, 0));
15898 /* Otherwise, the current method doesn't have the appropriate
15899 throws declaration */
15901 parse_error_context (wfl_operator, "Checked exception %qs doesn't match any of current method's %<throws%> declaration(s)",
15902 lang_printable_name (type, 0));
15903 return error_mark_node;
15906 if (! flag_emit_class_files)
15907 BUILD_THROW (node, expr);
15912 /* Check that exception said to be thrown by method DECL can be
15913 effectively caught from where DECL is invoked. THIS_EXPR is the
15914 expression that computes `this' for the method call. */
15916 check_thrown_exceptions (
15917 #ifdef USE_MAPPED_LOCATION
15918 source_location location,
15923 tree decl, tree this_expr)
15926 int is_array_call = 0;
15928 /* Skip check within generated methods, such as access$<n>. */
15929 if (NESTED_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (current_function_decl)))
15932 if (this_expr != NULL_TREE
15933 && TREE_CODE (TREE_TYPE (this_expr)) == POINTER_TYPE
15934 && TYPE_ARRAY_P (TREE_TYPE (TREE_TYPE (this_expr))))
15937 /* For all the unchecked exceptions thrown by DECL. */
15938 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15939 throws = TREE_CHAIN (throws))
15940 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15942 /* Suppress errors about cloning arrays. */
15943 if (is_array_call && DECL_NAME (decl) == get_identifier ("clone"))
15946 #ifdef USE_MAPPED_LOCATION
15947 SET_EXPR_LOCATION (wfl_operator, location);
15949 EXPR_WFL_LINECOL (wfl_operator) = location;
15951 if (DECL_FINIT_P (current_function_decl))
15952 parse_error_context
15953 (wfl_operator, "Exception %qs can't be thrown in initializer",
15954 lang_printable_name (TREE_VALUE (throws), 0));
15957 parse_error_context
15958 (wfl_operator, "Exception %qs must be caught, or it must be declared in the %<throws%> clause of %qs",
15959 lang_printable_name (TREE_VALUE (throws), 0),
15960 (DECL_INIT_P (current_function_decl) ?
15961 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15962 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15967 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15968 try-catch blocks, OR is listed in the `throws' clause of the
15972 check_thrown_exceptions_do (tree exception)
15974 tree list = currently_caught_type_list;
15975 resolve_and_layout (exception, NULL_TREE);
15976 /* First, all the nested try-catch-finally at that stage. The
15977 last element contains `throws' clause exceptions, if any. */
15978 if (IS_UNCHECKED_EXCEPTION_P (exception))
15983 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15984 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15986 list = TREE_CHAIN (list);
15992 purge_unchecked_exceptions (tree mdecl)
15994 tree throws = DECL_FUNCTION_THROWS (mdecl);
15995 tree new = NULL_TREE;
15999 tree next = TREE_CHAIN (throws);
16000 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
16002 TREE_CHAIN (throws) = new;
16007 /* List is inverted here, but it doesn't matter */
16008 DECL_FUNCTION_THROWS (mdecl) = new;
16011 /* This function goes over all of CLASS_TYPE ctors and checks whether
16012 each of them features at least one unchecked exception in its
16013 `throws' clause. If it's the case, it returns `true', `false'
16017 ctors_unchecked_throws_clause_p (tree class_type)
16021 for (current = TYPE_METHODS (class_type); current;
16022 current = TREE_CHAIN (current))
16024 bool ctu = false; /* Ctor Throws Unchecked */
16025 if (DECL_CONSTRUCTOR_P (current))
16028 for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
16029 throws = TREE_CHAIN (throws))
16030 if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
16033 /* We return false as we found one ctor that is unfit. */
16034 if (!ctu && DECL_CONSTRUCTOR_P (current))
16037 /* All ctors feature at least one unchecked exception in their
16038 `throws' clause. */
16042 /* 15.24 Conditional Operator ?: */
16045 patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
16047 tree cond = TREE_OPERAND (node, 0);
16048 tree op1 = TREE_OPERAND (node, 1);
16049 tree op2 = TREE_OPERAND (node, 2);
16050 tree resulting_type = NULL_TREE;
16051 tree t1, t2, patched;
16052 int error_found = 0;
16054 /* The condition and operands of ?: might be StringBuffers crafted
16055 as a result of a string concatenation. Obtain decent ones here. */
16056 if ((patched = patch_string (cond)))
16057 TREE_OPERAND (node, 0) = cond = patched;
16058 if ((patched = patch_string (op1)))
16059 TREE_OPERAND (node, 1) = op1 = patched;
16060 if ((patched = patch_string (op2)))
16061 TREE_OPERAND (node, 2) = op2 = patched;
16063 t1 = TREE_TYPE (op1);
16064 t2 = TREE_TYPE (op2);
16066 /* The first expression must be a boolean */
16067 if (TREE_TYPE (cond) != boolean_type_node)
16069 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
16070 parse_error_context (wfl_operator,
16071 "Incompatible type for %<?:%>. Can't convert %qs to %<boolean%>",
16072 lang_printable_name (TREE_TYPE (cond), 0));
16076 /* Second and third can be numeric, boolean (i.e. primitive),
16077 references or null. Anything else results in an error */
16078 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
16079 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
16080 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
16081 || (t1 == boolean_type_node && t2 == boolean_type_node)))
16084 /* Determine the type of the conditional expression. Same types are
16085 easy to deal with */
16087 resulting_type = t1;
16089 /* There are different rules for numeric types */
16090 else if (JNUMERIC_TYPE_P (t1))
16092 /* if byte/short found, the resulting type is short */
16093 if ((t1 == byte_type_node && t2 == short_type_node)
16094 || (t1 == short_type_node && t2 == byte_type_node))
16095 resulting_type = short_type_node;
16097 /* If t1 is a constant int and t2 is of type byte, short or char
16098 and t1's value fits in t2, then the resulting type is t2 */
16099 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
16100 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
16101 resulting_type = t2;
16103 /* If t2 is a constant int and t1 is of type byte, short or char
16104 and t2's value fits in t1, then the resulting type is t1 */
16105 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
16106 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
16107 resulting_type = t1;
16109 /* Otherwise, binary numeric promotion is applied and the
16110 resulting type is the promoted type of operand 1 and 2 */
16112 resulting_type = binary_numeric_promotion (t1, t2,
16113 &TREE_OPERAND (node, 1),
16114 &TREE_OPERAND (node, 2));
16117 /* Cases of a reference and a null type */
16118 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
16119 resulting_type = t1;
16121 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
16122 resulting_type = t2;
16124 /* Last case: different reference types. If a type can be converted
16125 into the other one by assignment conversion, the latter
16126 determines the type of the expression */
16127 else if ((resulting_type = try_reference_assignconv (t1, op2)))
16128 resulting_type = promote_type (t1);
16130 else if ((resulting_type = try_reference_assignconv (t2, op1)))
16131 resulting_type = promote_type (t2);
16133 /* If we don't have any resulting type, we're in trouble */
16134 if (!resulting_type)
16136 char *t = xstrdup (lang_printable_name (t1, 0));
16137 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16138 parse_error_context (wfl_operator,
16139 "Incompatible type for %<?:%>. Can't convert %qs to %qs",
16140 t, lang_printable_name (t2, 0));
16147 TREE_TYPE (node) = error_mark_node;
16148 return error_mark_node;
16151 TREE_TYPE (node) = resulting_type;
16152 TREE_SET_CODE (node, COND_EXPR);
16153 CAN_COMPLETE_NORMALLY (node) = 1;
16157 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16160 maybe_build_class_init_for_field (tree decl, tree expr)
16162 tree clas = DECL_CONTEXT (decl);
16163 if (flag_emit_class_files)
16166 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16167 && FIELD_FINAL (decl))
16169 tree init = DECL_INITIAL (decl);
16170 if (init != NULL_TREE)
16171 init = fold_constant_for_init (init, decl);
16172 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16176 return build_class_init (clas, expr);
16179 /* Try to constant fold NODE.
16180 If NODE is not a constant expression, return NULL_EXPR.
16181 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16184 fold_constant_for_init (tree node, tree context)
16186 tree op0, op1, val;
16187 enum tree_code code = TREE_CODE (node);
16192 if (node == null_pointer_node)
16201 case TRUNC_MOD_EXPR:
16209 case TRUTH_ANDIF_EXPR:
16210 case TRUTH_ORIF_EXPR:
16217 op0 = TREE_OPERAND (node, 0);
16218 op1 = TREE_OPERAND (node, 1);
16219 val = fold_constant_for_init (op0, context);
16220 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16222 TREE_OPERAND (node, 0) = val;
16223 val = fold_constant_for_init (op1, context);
16224 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16226 TREE_OPERAND (node, 1) = val;
16227 return patch_binop (node, op0, op1, 1);
16229 case UNARY_PLUS_EXPR:
16231 case TRUTH_NOT_EXPR:
16235 op0 = TREE_OPERAND (node, 0);
16236 val = fold_constant_for_init (op0, context);
16237 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16239 TREE_OPERAND (node, 0) = val;
16240 val = patch_unaryop (node, op0);
16241 if (! TREE_CONSTANT (val))
16248 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16249 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16251 TREE_OPERAND (node, 0) = val;
16252 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16253 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16255 TREE_OPERAND (node, 1) = val;
16256 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16257 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16259 TREE_OPERAND (node, 2) = val;
16260 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 2)
16261 : TREE_OPERAND (node, 1);
16265 if (! FIELD_FINAL (node)
16266 || DECL_INITIAL (node) == NULL_TREE)
16268 val = DECL_INITIAL (node);
16269 /* Guard against infinite recursion. */
16270 DECL_INITIAL (node) = NULL_TREE;
16271 val = fold_constant_for_init (val, node);
16272 if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16273 val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16274 DECL_INITIAL (node) = val;
16277 case EXPR_WITH_FILE_LOCATION:
16278 /* Compare java_complete_tree and resolve_expression_name. */
16279 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16280 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16282 tree name = EXPR_WFL_NODE (node);
16284 if (PRIMARY_P (node))
16286 else if (! QUALIFIED_P (name))
16288 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16289 if (decl == NULL_TREE
16290 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16292 return fold_constant_for_init (decl, decl);
16296 tree r = NULL_TREE;
16297 /* Install the proper context for the field resolution. */
16298 tree saved_current_class = current_class;
16299 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
16300 current_class = DECL_CONTEXT (context);
16301 qualify_ambiguous_name (node);
16302 r = resolve_field_access (node, &decl, NULL);
16303 /* Restore prior context. */
16304 current_class = saved_current_class;
16305 if (r != error_mark_node && decl != NULL_TREE)
16306 return fold_constant_for_init (decl, decl);
16312 op0 = TREE_OPERAND (node, 0);
16313 val = fold_constant_for_init (op0, context);
16314 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16316 TREE_OPERAND (node, 0) = val;
16320 #ifdef USE_COMPONENT_REF
16322 case COMPONENT_REF:
16331 #ifdef USE_COMPONENT_REF
16332 /* Context is 'T' for TypeName, 'P' for PackageName,
16333 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16336 resolve_simple_name (tree name, int context)
16341 resolve_qualified_name (tree name, int context)
16347 init_src_parse (void)
16349 /* Sanity check; we've been bit by this before. */
16350 if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16356 /* This section deals with the functions that are called when tables
16357 recording class initialization information are traversed. */
16359 /* This function is called for each class that is known definitely
16360 initialized when a given static method was called. This function
16361 augments a compound expression (INFO) storing all assignment to
16362 initialized static class flags if a flag already existed, otherwise
16363 a new one is created. */
16366 emit_test_initialization (void **entry_p, void *info)
16368 tree l = (tree) info;
16370 tree key = (tree) *entry_p;
16372 htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16374 /* If we haven't found a flag and we're dealing with self registered
16375 with current_function_decl, then don't do anything. Self is
16376 always added as definitely initialized but this information is
16377 valid only if used outside the current function. */
16378 if (current_function_decl == TREE_PURPOSE (l)
16379 && java_treetreehash_find (cf_ht, key) == NULL)
16382 ite = java_treetreehash_new (cf_ht, key);
16384 /* If we don't have a variable, create one and install it. */
16389 decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16390 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16391 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16392 DECL_CONTEXT (decl) = current_function_decl;
16393 DECL_INITIAL (decl) = boolean_true_node;
16394 /* Don't emit any symbolic debugging info for this decl. */
16395 DECL_IGNORED_P (decl) = 1;
16397 /* The trick is to find the right context for it. */
16398 block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16399 TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16400 BLOCK_EXPR_DECLS (block) = decl;
16406 /* Now simply augment the compound that holds all the assignments
16407 pertaining to this method invocation. */
16408 init = build2 (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16409 TREE_SIDE_EFFECTS (init) = 1;
16410 TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16411 TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16416 #ifdef __XGETTEXT__
16417 /* Depending on the version of Bison used to compile this grammar,
16418 it may issue generic diagnostics spelled "syntax error" or
16419 "parse error". To prevent this from changing the translation
16420 template randomly, we list all the variants of this particular
16421 diagnostic here. Translators: there is no fine distinction
16422 between diagnostics with "syntax error" in them, and diagnostics
16423 with "parse error" in them. It's okay to give them both the same
16425 const char d1[] = N_("syntax error");
16426 const char d2[] = N_("parse error");
16427 const char d3[] = N_("syntax error; also virtual memory exhausted");
16428 const char d4[] = N_("parse error; also virtual memory exhausted");
16429 const char d5[] = N_("syntax error: cannot back up");
16430 const char d6[] = N_("parse error: cannot back up");
16433 #include "gt-java-parse.h"
16434 #include "gtype-java.h"