* doc/invoke.texi (i386 Options): Document x86-64 options.
[official-gcc.git] / gcc / java / parse.y
blob154fef3f699e3c90e4f90c418c349fb275465aba
1 /* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
23 Java and all Java-based marks are trademarks or registered trademarks
24 of Sun Microsystems, Inc. in the United States and other countries.
25 The Free Software Foundation is independent of Sun Microsystems, Inc. */
27 /* This file parses java source code and issues a tree node image
28 suitable for code generation (byte code and targeted CPU assembly
29 language).
31 The grammar conforms to the Java grammar described in "The Java(TM)
32 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
33 1996, ISBN 0-201-63451-1"
35 The following modifications were brought to the original grammar:
37 method_body: added the rule '| block SC_TK'
38 static_initializer: added the rule 'static block SC_TK'.
40 Note: All the extra rules described above should go away when the
41 empty_statement rule will work.
43 statement_nsi: 'nsi' should be read no_short_if.
45 Some rules have been modified to support JDK1.1 inner classes
46 definitions and other extensions. */
49 #include "config.h"
50 #include "system.h"
51 #include <dirent.h>
52 #include "tree.h"
53 #include "rtl.h"
54 #include "obstack.h"
55 #include "toplev.h"
56 #include "flags.h"
57 #include "java-tree.h"
58 #include "jcf.h"
59 #include "lex.h"
60 #include "parse.h"
61 #include "zipfile.h"
62 #include "convert.h"
63 #include "buffer.h"
64 #include "xref.h"
65 #include "function.h"
66 #include "except.h"
67 #include "ggc.h"
69 #ifndef DIR_SEPARATOR
70 #define DIR_SEPARATOR '/'
71 #endif
73 /* Local function prototypes */
74 static char *java_accstring_lookup PARAMS ((int));
75 static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
76 static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
77 static tree create_class PARAMS ((int, tree, tree, tree));
78 static tree create_interface PARAMS ((int, tree, tree));
79 static void end_class_declaration PARAMS ((int));
80 static tree find_field PARAMS ((tree, tree));
81 static tree lookup_field_wrapper PARAMS ((tree, tree));
82 static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
83 static void register_fields PARAMS ((int, tree, tree));
84 static tree parser_qualified_classname PARAMS ((tree));
85 static int parser_check_super PARAMS ((tree, tree, tree));
86 static int parser_check_super_interface PARAMS ((tree, tree, tree));
87 static void check_modifiers_consistency PARAMS ((int));
88 static tree lookup_cl PARAMS ((tree));
89 static tree lookup_java_method2 PARAMS ((tree, tree, int));
90 static tree method_header PARAMS ((int, tree, tree, tree));
91 static void fix_method_argument_names PARAMS ((tree ,tree));
92 static tree method_declarator PARAMS ((tree, tree));
93 static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
94 ATTRIBUTE_PRINTF_2;
95 static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list))
96 ATTRIBUTE_PRINTF (2, 0);
97 static void parse_ctor_invocation_error PARAMS ((void));
98 static tree parse_jdk1_1_error PARAMS ((const char *));
99 static void complete_class_report_errors PARAMS ((jdep *));
100 static int process_imports PARAMS ((void));
101 static void read_import_dir PARAMS ((tree));
102 static int find_in_imports_on_demand PARAMS ((tree, tree));
103 static void find_in_imports PARAMS ((tree, tree));
104 static void check_static_final_variable_assignment_flag PARAMS ((tree));
105 static void reset_static_final_variable_assignment_flag PARAMS ((tree));
106 static void check_final_variable_local_assignment_flag PARAMS ((tree, tree));
107 static void reset_final_variable_local_assignment_flag PARAMS ((tree));
108 static int check_final_variable_indirect_assignment PARAMS ((tree));
109 static void check_final_variable_global_assignment_flag PARAMS ((tree));
110 static void check_inner_class_access PARAMS ((tree, tree, tree));
111 static int check_pkg_class_access PARAMS ((tree, tree));
112 static void register_package PARAMS ((tree));
113 static tree resolve_package PARAMS ((tree, tree *));
114 static tree lookup_package_type PARAMS ((const char *, int));
115 static tree resolve_class PARAMS ((tree, tree, tree, tree));
116 static void declare_local_variables PARAMS ((int, tree, tree));
117 static void source_start_java_method PARAMS ((tree));
118 static void source_end_java_method PARAMS ((void));
119 static tree find_name_in_single_imports PARAMS ((tree));
120 static void check_abstract_method_header PARAMS ((tree));
121 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
122 static tree resolve_expression_name PARAMS ((tree, tree *));
123 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
124 static int check_class_interface_creation PARAMS ((int, int, tree,
125 tree, tree, tree));
126 static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
127 int *, tree *));
128 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
129 static int in_same_package PARAMS ((tree, tree));
130 static tree resolve_and_layout PARAMS ((tree, tree));
131 static tree qualify_and_find PARAMS ((tree, tree, tree));
132 static tree resolve_no_layout PARAMS ((tree, tree));
133 static int invocation_mode PARAMS ((tree, int));
134 static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
135 tree, tree));
136 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
137 tree *, tree *));
138 static tree find_most_specific_methods_list PARAMS ((tree));
139 static int argument_types_convertible PARAMS ((tree, tree));
140 static tree patch_invoke PARAMS ((tree, tree, tree));
141 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
142 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
143 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
144 static tree check_inner_circular_reference PARAMS ((tree, tree));
145 static tree check_circular_reference PARAMS ((tree));
146 static tree obtain_incomplete_type PARAMS ((tree));
147 static tree java_complete_lhs PARAMS ((tree));
148 static tree java_complete_tree PARAMS ((tree));
149 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
150 static int analyze_clinit_body PARAMS ((tree));
151 static int maybe_yank_clinit PARAMS ((tree));
152 static void start_complete_expand_method PARAMS ((tree));
153 static void java_complete_expand_method PARAMS ((tree));
154 static void java_expand_method_bodies PARAMS ((tree));
155 static int unresolved_type_p PARAMS ((tree, tree *));
156 static void create_jdep_list PARAMS ((struct parser_ctxt *));
157 static tree build_expr_block PARAMS ((tree, tree));
158 static tree enter_block PARAMS ((void));
159 static tree exit_block PARAMS ((void));
160 static tree lookup_name_in_blocks PARAMS ((tree));
161 static void maybe_absorb_scoping_blocks PARAMS ((void));
162 static tree build_method_invocation PARAMS ((tree, tree));
163 static tree build_new_invocation PARAMS ((tree, tree));
164 static tree build_assignment PARAMS ((int, int, tree, tree));
165 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
166 static int check_final_assignment PARAMS ((tree ,tree));
167 static tree patch_assignment PARAMS ((tree, tree, tree ));
168 static tree patch_binop PARAMS ((tree, tree, tree));
169 static tree build_unaryop PARAMS ((int, int, tree));
170 static tree build_incdec PARAMS ((int, int, tree, int));
171 static tree patch_unaryop PARAMS ((tree, tree));
172 static tree build_cast PARAMS ((int, tree, tree));
173 static tree build_null_of_type PARAMS ((tree));
174 static tree patch_cast PARAMS ((tree, tree));
175 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
176 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
177 static int valid_cast_to_p PARAMS ((tree, tree));
178 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
179 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
180 static tree try_reference_assignconv PARAMS ((tree, tree));
181 static tree build_unresolved_array_type PARAMS ((tree));
182 static int build_type_name_from_array_name PARAMS ((tree, tree *));
183 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
184 static tree build_array_ref PARAMS ((int, tree, tree));
185 static tree patch_array_ref PARAMS ((tree));
186 static tree make_qualified_name PARAMS ((tree, tree, int));
187 static tree merge_qualified_name PARAMS ((tree, tree));
188 static tree make_qualified_primary PARAMS ((tree, tree, int));
189 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
190 tree *, tree *));
191 static void qualify_ambiguous_name PARAMS ((tree));
192 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
193 static tree build_newarray_node PARAMS ((tree, tree, int));
194 static tree patch_newarray PARAMS ((tree));
195 static tree resolve_type_during_patch PARAMS ((tree));
196 static tree build_this PARAMS ((int));
197 static tree build_wfl_wrap PARAMS ((tree, int));
198 static tree build_return PARAMS ((int, tree));
199 static tree patch_return PARAMS ((tree));
200 static tree maybe_access_field PARAMS ((tree, tree, tree));
201 static int complete_function_arguments PARAMS ((tree));
202 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
203 tree, tree));
204 static int not_accessible_p PARAMS ((tree, tree, tree, int));
205 static void check_deprecation PARAMS ((tree, tree));
206 static int class_in_current_package PARAMS ((tree));
207 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
208 static tree patch_if_else_statement PARAMS ((tree));
209 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
210 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
211 static tree patch_exit_expr PARAMS ((tree));
212 static tree build_labeled_block PARAMS ((int, tree));
213 static tree finish_labeled_statement PARAMS ((tree, tree));
214 static tree build_bc_statement PARAMS ((int, int, tree));
215 static tree patch_bc_statement PARAMS ((tree));
216 static tree patch_loop_statement PARAMS ((tree));
217 static tree build_new_loop PARAMS ((tree));
218 static tree build_loop_body PARAMS ((int, tree, int));
219 static tree finish_loop_body PARAMS ((int, tree, tree, int));
220 static tree build_debugable_stmt PARAMS ((int, tree));
221 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
222 static tree patch_switch_statement PARAMS ((tree));
223 static tree string_constant_concatenation PARAMS ((tree, tree));
224 static tree build_string_concatenation PARAMS ((tree, tree));
225 static tree patch_string_cst PARAMS ((tree));
226 static tree patch_string PARAMS ((tree));
227 static tree encapsulate_with_try_catch PARAMS ((int, tree, tree, tree));
228 static tree build_try_statement PARAMS ((int, tree, tree));
229 static tree build_try_finally_statement PARAMS ((int, tree, tree));
230 static tree patch_try_statement PARAMS ((tree));
231 static tree patch_synchronized_statement PARAMS ((tree, tree));
232 static tree patch_throw_statement PARAMS ((tree, tree));
233 static void check_thrown_exceptions PARAMS ((int, tree));
234 static int check_thrown_exceptions_do PARAMS ((tree));
235 static void purge_unchecked_exceptions PARAMS ((tree));
236 static bool ctors_unchecked_throws_clause_p PARAMS ((tree));
237 static void check_throws_clauses PARAMS ((tree, tree, tree));
238 static void finish_method_declaration PARAMS ((tree));
239 static tree build_super_invocation PARAMS ((tree));
240 static int verify_constructor_circularity PARAMS ((tree, tree));
241 static char *constructor_circularity_msg PARAMS ((tree, tree));
242 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
243 int, int));
244 static const char *get_printable_method_name PARAMS ((tree));
245 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
246 static tree generate_finit PARAMS ((tree));
247 static tree generate_instinit PARAMS ((tree));
248 static tree build_instinit_invocation PARAMS ((tree));
249 static void fix_constructors PARAMS ((tree));
250 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
251 tree, int *));
252 static void craft_constructor PARAMS ((tree, tree));
253 static int verify_constructor_super PARAMS ((tree));
254 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
255 static void start_artificial_method_body PARAMS ((tree));
256 static void end_artificial_method_body PARAMS ((tree));
257 static int check_method_redefinition PARAMS ((tree, tree));
258 static int check_method_types_complete PARAMS ((tree));
259 static void java_check_regular_methods PARAMS ((tree));
260 static void java_check_abstract_methods PARAMS ((tree));
261 static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
262 static void unreachable_stmt_error PARAMS ((tree));
263 static tree find_expr_with_wfl PARAMS ((tree));
264 static void missing_return_error PARAMS ((tree));
265 static tree build_new_array_init PARAMS ((int, tree));
266 static tree patch_new_array_init PARAMS ((tree, tree));
267 static tree maybe_build_array_element_wfl PARAMS ((tree));
268 static int array_constructor_check_entry PARAMS ((tree, tree));
269 static const char *purify_type_name PARAMS ((const char *));
270 static tree fold_constant_for_init PARAMS ((tree, tree));
271 static tree strip_out_static_field_access_decl PARAMS ((tree));
272 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
273 static void static_ref_err PARAMS ((tree, tree, tree));
274 static void parser_add_interface PARAMS ((tree, tree, tree));
275 static void add_superinterfaces PARAMS ((tree, tree));
276 static tree jdep_resolve_class PARAMS ((jdep *));
277 static int note_possible_classname PARAMS ((const char *, int));
278 static void java_complete_expand_classes PARAMS ((void));
279 static void java_complete_expand_class PARAMS ((tree));
280 static void java_complete_expand_methods PARAMS ((tree));
281 static tree cut_identifier_in_qualified PARAMS ((tree));
282 static tree java_stabilize_reference PARAMS ((tree));
283 static tree do_unary_numeric_promotion PARAMS ((tree));
284 static char * operator_string PARAMS ((tree));
285 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
286 static tree merge_string_cste PARAMS ((tree, tree, int));
287 static tree java_refold PARAMS ((tree));
288 static int java_decl_equiv PARAMS ((tree, tree));
289 static int binop_compound_p PARAMS ((enum tree_code));
290 static tree search_loop PARAMS ((tree));
291 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
292 static int check_abstract_method_definitions PARAMS ((int, tree, tree));
293 static void java_check_abstract_method_definitions PARAMS ((tree));
294 static void java_debug_context_do PARAMS ((int));
295 static void java_parser_context_push_initialized_field PARAMS ((void));
296 static void java_parser_context_pop_initialized_field PARAMS ((void));
297 static tree reorder_static_initialized PARAMS ((tree));
298 static void java_parser_context_suspend PARAMS ((void));
299 static void java_parser_context_resume PARAMS ((void));
300 static int pop_current_osb PARAMS ((struct parser_ctxt *));
302 /* JDK 1.1 work. FIXME */
304 static tree maybe_make_nested_class_name PARAMS ((tree));
305 static void make_nested_class_name PARAMS ((tree));
306 static void set_nested_class_simple_name_value PARAMS ((tree, int));
307 static void link_nested_class_to_enclosing PARAMS ((void));
308 static tree resolve_inner_class PARAMS ((struct hash_table *, tree, tree *,
309 tree *, tree));
310 static tree find_as_inner_class PARAMS ((tree, tree, tree));
311 static tree find_as_inner_class_do PARAMS ((tree, tree));
312 static int check_inner_class_redefinition PARAMS ((tree, tree));
314 static tree build_thisn_assign PARAMS ((void));
315 static tree build_current_thisn PARAMS ((tree));
316 static tree build_access_to_thisn PARAMS ((tree, tree, int));
317 static tree maybe_build_thisn_access_method PARAMS ((tree));
319 static tree build_outer_field_access PARAMS ((tree, tree));
320 static tree build_outer_field_access_methods PARAMS ((tree));
321 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
322 tree, tree));
323 static tree build_outer_method_access_method PARAMS ((tree));
324 static tree build_new_access_id PARAMS ((void));
325 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
326 tree, tree));
328 static int outer_field_access_p PARAMS ((tree, tree));
329 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
330 tree *, tree *));
331 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
332 static tree build_incomplete_class_ref PARAMS ((int, tree));
333 static tree patch_incomplete_class_ref PARAMS ((tree));
334 static tree create_anonymous_class PARAMS ((int, tree));
335 static void patch_anonymous_class PARAMS ((tree, tree, tree));
336 static void add_inner_class_fields PARAMS ((tree, tree));
338 static tree build_dot_class_method PARAMS ((tree));
339 static tree build_dot_class_method_invocation PARAMS ((tree));
340 static void create_new_parser_context PARAMS ((int));
341 static void mark_parser_ctxt PARAMS ((void *));
342 static tree maybe_build_class_init_for_field PARAMS ((tree, tree));
344 static bool attach_init_test_initialization_flags PARAMS ((struct hash_entry *,
345 PTR));
346 static bool adjust_init_test_initialization PARAMS ((struct hash_entry *,
347 PTR));
348 static bool emit_test_initialization PARAMS ((struct hash_entry *, PTR));
350 /* Number of error found so far. */
351 int java_error_count;
352 /* Number of warning found so far. */
353 int java_warning_count;
354 /* Tell when not to fold, when doing xrefs */
355 int do_not_fold;
356 /* Cyclic inheritance report, as it can be set by layout_class */
357 const char *cyclic_inheritance_report;
359 /* The current parser context */
360 struct parser_ctxt *ctxp;
362 /* List of things that were analyzed for which code will be generated */
363 struct parser_ctxt *ctxp_for_generation = NULL;
365 /* binop_lookup maps token to tree_code. It is used where binary
366 operations are involved and required by the parser. RDIV_EXPR
367 covers both integral/floating point division. The code is changed
368 once the type of both operator is worked out. */
370 static enum tree_code binop_lookup[19] =
372 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
373 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
374 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
375 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
376 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
378 #define BINOP_LOOKUP(VALUE) \
379 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
381 /* This is the end index for binary operators that can also be used
382 in compound assignements. */
383 #define BINOP_COMPOUND_CANDIDATES 11
385 /* The "$L" identifier we use to create labels. */
386 static tree label_id = NULL_TREE;
388 /* The "StringBuffer" identifier used for the String `+' operator. */
389 static tree wfl_string_buffer = NULL_TREE;
391 /* The "append" identifier used for String `+' operator. */
392 static tree wfl_append = NULL_TREE;
394 /* The "toString" identifier used for String `+' operator. */
395 static tree wfl_to_string = NULL_TREE;
397 /* The "java.lang" import qualified name. */
398 static tree java_lang_id = NULL_TREE;
400 /* The generated `inst$' identifier used for generated enclosing
401 instance/field access functions. */
402 static tree inst_id = NULL_TREE;
404 /* The "java.lang.Cloneable" qualified name. */
405 static tree java_lang_cloneable = NULL_TREE;
407 /* The "java.io.Serializable" qualified name. */
408 static tree java_io_serializable = NULL_TREE;
410 /* Context and flag for static blocks */
411 static tree current_static_block = NULL_TREE;
413 /* The generated `write_parm_value$' identifier. */
414 static tree wpv_id;
416 /* The list of all packages we've seen so far */
417 static tree package_list = NULL_TREE;
419 /* Hold THIS for the scope of the current public method decl. */
420 static tree current_this;
422 /* Hold a list of catch clauses list. The first element of this list is
423 the list of the catch clauses of the currently analysed try block. */
424 static tree currently_caught_type_list;
426 static tree src_parse_roots[1] = { NULL_TREE };
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
434 backward. FIXME */
436 #define check_modifiers(__message, __value, __mask) do { \
437 if ((__value) & ~(__mask)) \
439 int i, remainder = (__value) & ~(__mask); \
440 for (i = 0; i <= 10; i++) \
441 if ((1 << i) & remainder) \
442 parse_error_context (ctxp->modifier_ctx [i], (__message), \
443 java_accstring_lookup (1 << i)); \
445 } while (0)
449 %union {
450 tree node;
451 int sub_token;
452 struct {
453 int token;
454 int location;
455 } operator;
456 int value;
460 #include "lex.c"
463 %pure_parser
465 /* Things defined here have to match the order of what's in the
466 binop_lookup table. */
468 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
469 %token LS_TK SRS_TK ZRS_TK
470 %token AND_TK XOR_TK OR_TK
471 %token BOOL_AND_TK BOOL_OR_TK
472 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
474 /* This maps to the same binop_lookup entry than the token above */
476 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
477 %token REM_ASSIGN_TK
478 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
479 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
482 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
484 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
485 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
486 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
487 %token PAD_TK ABSTRACT_TK MODIFIER_TK
488 %token STRICT_TK
490 /* Keep those two in order, too */
491 %token DECR_TK INCR_TK
493 /* From now one, things can be in any order */
495 %token DEFAULT_TK IF_TK THROW_TK
496 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
497 %token THROWS_TK BREAK_TK IMPORT_TK
498 %token ELSE_TK INSTANCEOF_TK RETURN_TK
499 %token VOID_TK CATCH_TK INTERFACE_TK
500 %token CASE_TK EXTENDS_TK FINALLY_TK
501 %token SUPER_TK WHILE_TK CLASS_TK
502 %token SWITCH_TK CONST_TK TRY_TK
503 %token FOR_TK NEW_TK CONTINUE_TK
504 %token GOTO_TK PACKAGE_TK THIS_TK
506 %token BYTE_TK SHORT_TK INT_TK LONG_TK
507 %token CHAR_TK INTEGRAL_TK
509 %token FLOAT_TK DOUBLE_TK FP_TK
511 %token ID_TK
513 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
515 %token ASSIGN_ANY_TK ASSIGN_TK
516 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
518 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
519 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
521 %type <value> modifiers MODIFIER_TK final synchronized
523 %type <node> super ID_TK identifier
524 %type <node> name simple_name qualified_name
525 %type <node> type_declaration compilation_unit
526 field_declaration method_declaration extends_interfaces
527 interfaces interface_type_list
528 class_member_declaration
529 import_declarations package_declaration
530 type_declarations interface_body
531 interface_member_declaration constant_declaration
532 interface_member_declarations interface_type
533 abstract_method_declaration interface_type_list
534 %type <node> class_body_declaration class_member_declaration
535 static_initializer constructor_declaration block
536 %type <node> class_body_declarations constructor_header
537 %type <node> class_or_interface_type class_type class_type_list
538 constructor_declarator explicit_constructor_invocation
539 %type <node> dim_expr dim_exprs this_or_super throws
541 %type <node> variable_declarator_id variable_declarator
542 variable_declarators variable_initializer
543 variable_initializers constructor_body
544 array_initializer
546 %type <node> class_body block_end constructor_block_end
547 %type <node> statement statement_without_trailing_substatement
548 labeled_statement if_then_statement label_decl
549 if_then_else_statement while_statement for_statement
550 statement_nsi labeled_statement_nsi do_statement
551 if_then_else_statement_nsi while_statement_nsi
552 for_statement_nsi statement_expression_list for_init
553 for_update statement_expression expression_statement
554 primary_no_new_array expression primary
555 array_creation_expression array_type
556 class_instance_creation_expression field_access
557 method_invocation array_access something_dot_new
558 argument_list postfix_expression while_expression
559 post_increment_expression post_decrement_expression
560 unary_expression_not_plus_minus unary_expression
561 pre_increment_expression pre_decrement_expression
562 unary_expression_not_plus_minus cast_expression
563 multiplicative_expression additive_expression
564 shift_expression relational_expression
565 equality_expression and_expression
566 exclusive_or_expression inclusive_or_expression
567 conditional_and_expression conditional_or_expression
568 conditional_expression assignment_expression
569 left_hand_side assignment for_header for_begin
570 constant_expression do_statement_begin empty_statement
571 switch_statement synchronized_statement throw_statement
572 try_statement switch_expression switch_block
573 catches catch_clause catch_clause_parameter finally
574 anonymous_class_creation
575 %type <node> return_statement break_statement continue_statement
577 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
578 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
579 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
580 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
581 %type <operator> ASSIGN_ANY_TK assignment_operator
582 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
583 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
584 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
585 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
586 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
587 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
588 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
589 %type <operator> NEW_TK
591 %type <node> method_body
593 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
594 STRING_LIT_TK NULL_TK VOID_TK
596 %type <node> IF_TK WHILE_TK FOR_TK
598 %type <node> formal_parameter_list formal_parameter
599 method_declarator method_header
601 %type <node> primitive_type reference_type type
602 BOOLEAN_TK INTEGRAL_TK FP_TK
604 /* Added or modified JDK 1.1 rule types */
605 %type <node> type_literals
608 /* 19.2 Production from 2.3: The Syntactic Grammar */
609 goal:
611 /* Register static variables with the garbage
612 collector. */
613 ggc_add_tree_root (&label_id, 1);
614 ggc_add_tree_root (&wfl_string_buffer, 1);
615 ggc_add_tree_root (&wfl_append, 1);
616 ggc_add_tree_root (&wfl_to_string, 1);
617 ggc_add_tree_root (&java_lang_id, 1);
618 ggc_add_tree_root (&inst_id, 1);
619 ggc_add_tree_root (&java_lang_cloneable, 1);
620 ggc_add_tree_root (&java_io_serializable, 1);
621 ggc_add_tree_root (&current_static_block, 1);
622 ggc_add_tree_root (&wpv_id, 1);
623 ggc_add_tree_root (&package_list, 1);
624 ggc_add_tree_root (&current_this, 1);
625 ggc_add_tree_root (&currently_caught_type_list, 1);
626 ggc_add_root (&ctxp, 1,
627 sizeof (struct parser_ctxt *),
628 mark_parser_ctxt);
629 ggc_add_root (&ctxp_for_generation, 1,
630 sizeof (struct parser_ctxt *),
631 mark_parser_ctxt);
633 compilation_unit
637 /* 19.3 Productions from 3: Lexical structure */
638 literal:
639 INT_LIT_TK
640 | FP_LIT_TK
641 | BOOL_LIT_TK
642 | CHAR_LIT_TK
643 | STRING_LIT_TK
644 | NULL_TK
647 /* 19.4 Productions from 4: Types, Values and Variables */
648 type:
649 primitive_type
650 | reference_type
653 primitive_type:
654 INTEGRAL_TK
655 | FP_TK
656 | BOOLEAN_TK
659 reference_type:
660 class_or_interface_type
661 | array_type
664 class_or_interface_type:
665 name
668 class_type:
669 class_or_interface_type /* Default rule */
672 interface_type:
673 class_or_interface_type
676 array_type:
677 primitive_type dims
679 int osb = pop_current_osb (ctxp);
680 tree t = build_java_array_type (($1), -1);
681 while (--osb)
682 t = build_unresolved_array_type (t);
683 $$ = t;
685 | name dims
687 int osb = pop_current_osb (ctxp);
688 tree t = $1;
689 while (osb--)
690 t = build_unresolved_array_type (t);
691 $$ = t;
695 /* 19.5 Productions from 6: Names */
696 name:
697 simple_name /* Default rule */
698 | qualified_name /* Default rule */
701 simple_name:
702 identifier /* Default rule */
705 qualified_name:
706 name DOT_TK identifier
707 { $$ = make_qualified_name ($1, $3, $2.location); }
710 identifier:
711 ID_TK
714 /* 19.6: Production from 7: Packages */
715 compilation_unit:
716 {$$ = NULL;}
717 | package_declaration
718 | import_declarations
719 | type_declarations
720 | package_declaration import_declarations
721 | package_declaration type_declarations
722 | import_declarations type_declarations
723 | package_declaration import_declarations type_declarations
726 import_declarations:
727 import_declaration
729 $$ = NULL;
731 | import_declarations import_declaration
733 $$ = NULL;
737 type_declarations:
738 type_declaration
739 | type_declarations type_declaration
742 package_declaration:
743 PACKAGE_TK name SC_TK
745 ctxp->package = EXPR_WFL_NODE ($2);
746 register_package (ctxp->package);
748 | PACKAGE_TK error
749 {yyerror ("Missing name"); RECOVER;}
750 | PACKAGE_TK name error
751 {yyerror ("';' expected"); RECOVER;}
754 import_declaration:
755 single_type_import_declaration
756 | type_import_on_demand_declaration
759 single_type_import_declaration:
760 IMPORT_TK name SC_TK
762 tree name = EXPR_WFL_NODE ($2), last_name;
763 int i = IDENTIFIER_LENGTH (name)-1;
764 const char *last = &IDENTIFIER_POINTER (name)[i];
765 while (last != IDENTIFIER_POINTER (name))
767 if (last [0] == '.')
768 break;
769 last--;
771 last_name = get_identifier (++last);
772 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
774 tree err = find_name_in_single_imports (last_name);
775 if (err && err != name)
776 parse_error_context
777 ($2, "Ambiguous class: `%s' and `%s'",
778 IDENTIFIER_POINTER (name),
779 IDENTIFIER_POINTER (err));
780 else
781 REGISTER_IMPORT ($2, last_name);
783 else
784 REGISTER_IMPORT ($2, last_name);
786 | IMPORT_TK error
787 {yyerror ("Missing name"); RECOVER;}
788 | IMPORT_TK name error
789 {yyerror ("';' expected"); RECOVER;}
792 type_import_on_demand_declaration:
793 IMPORT_TK name DOT_TK MULT_TK SC_TK
795 tree name = EXPR_WFL_NODE ($2);
796 tree it;
797 /* Search for duplicates. */
798 for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
799 if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
800 break;
801 /* Don't import the same thing more than once, just ignore
802 duplicates (7.5.2) */
803 if (! it)
805 read_import_dir ($2);
806 ctxp->import_demand_list =
807 chainon (ctxp->import_demand_list,
808 build_tree_list ($2, NULL_TREE));
811 | IMPORT_TK name DOT_TK error
812 {yyerror ("'*' expected"); RECOVER;}
813 | IMPORT_TK name DOT_TK MULT_TK error
814 {yyerror ("';' expected"); RECOVER;}
817 type_declaration:
818 class_declaration
819 { end_class_declaration (0); }
820 | interface_declaration
821 { end_class_declaration (0); }
822 | empty_statement
823 | error
825 YYERROR_NOW;
826 yyerror ("Class or interface declaration expected");
830 /* 19.7 Shortened from the original:
831 modifiers: modifier | modifiers modifier
832 modifier: any of public... */
833 modifiers:
834 MODIFIER_TK
836 $$ = (1 << $1);
838 | modifiers MODIFIER_TK
840 int acc = (1 << $2);
841 if ($$ & acc)
842 parse_error_context
843 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
844 java_accstring_lookup (acc));
845 else
847 $$ |= acc;
852 /* 19.8.1 Production from $8.1: Class Declaration */
853 class_declaration:
854 modifiers CLASS_TK identifier super interfaces
855 { create_class ($1, $3, $4, $5); }
856 class_body
857 | CLASS_TK identifier super interfaces
858 { create_class (0, $2, $3, $4); }
859 class_body
860 | modifiers CLASS_TK error
861 {yyerror ("Missing class name"); RECOVER;}
862 | CLASS_TK error
863 {yyerror ("Missing class name"); RECOVER;}
864 | CLASS_TK identifier error
866 if (!ctxp->class_err) yyerror ("'{' expected");
867 DRECOVER(class1);
869 | modifiers CLASS_TK identifier error
870 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
873 super:
874 { $$ = NULL; }
875 | EXTENDS_TK class_type
876 { $$ = $2; }
877 | EXTENDS_TK class_type error
878 {yyerror ("'{' expected"); ctxp->class_err=1;}
879 | EXTENDS_TK error
880 {yyerror ("Missing super class name"); ctxp->class_err=1;}
883 interfaces:
884 { $$ = NULL_TREE; }
885 | IMPLEMENTS_TK interface_type_list
886 { $$ = $2; }
887 | IMPLEMENTS_TK error
889 ctxp->class_err=1;
890 yyerror ("Missing interface name");
894 interface_type_list:
895 interface_type
897 ctxp->interface_number = 1;
898 $$ = build_tree_list ($1, NULL_TREE);
900 | interface_type_list C_TK interface_type
902 ctxp->interface_number++;
903 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
905 | interface_type_list C_TK error
906 {yyerror ("Missing interface name"); RECOVER;}
909 class_body:
910 OCB_TK CCB_TK
912 /* Store the location of the `}' when doing xrefs */
913 if (flag_emit_xref)
914 DECL_END_SOURCE_LINE (GET_CPC ()) =
915 EXPR_WFL_ADD_COL ($2.location, 1);
916 $$ = GET_CPC ();
918 | OCB_TK class_body_declarations CCB_TK
920 /* Store the location of the `}' when doing xrefs */
921 if (flag_emit_xref)
922 DECL_END_SOURCE_LINE (GET_CPC ()) =
923 EXPR_WFL_ADD_COL ($3.location, 1);
924 $$ = GET_CPC ();
928 class_body_declarations:
929 class_body_declaration
930 | class_body_declarations class_body_declaration
933 class_body_declaration:
934 class_member_declaration
935 | static_initializer
936 | constructor_declaration
937 | block /* Added, JDK1.1, instance initializer */
939 if ($1 != empty_stmt_node)
941 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
942 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
947 class_member_declaration:
948 field_declaration
949 | method_declaration
950 | class_declaration /* Added, JDK1.1 inner classes */
951 { end_class_declaration (1); }
952 | interface_declaration /* Added, JDK1.1 inner interfaces */
953 { end_class_declaration (1); }
954 | empty_statement
957 /* 19.8.2 Productions from 8.3: Field Declarations */
958 field_declaration:
959 type variable_declarators SC_TK
960 { register_fields (0, $1, $2); }
961 | modifiers type variable_declarators SC_TK
963 check_modifiers
964 ("Illegal modifier `%s' for field declaration",
965 $1, FIELD_MODIFIERS);
966 check_modifiers_consistency ($1);
967 register_fields ($1, $2, $3);
971 variable_declarators:
972 /* Should we use build_decl_list () instead ? FIXME */
973 variable_declarator /* Default rule */
974 | variable_declarators C_TK variable_declarator
975 { $$ = chainon ($1, $3); }
976 | variable_declarators C_TK error
977 {yyerror ("Missing term"); RECOVER;}
980 variable_declarator:
981 variable_declarator_id
982 { $$ = build_tree_list ($1, NULL_TREE); }
983 | variable_declarator_id ASSIGN_TK variable_initializer
985 if (java_error_count)
986 $3 = NULL_TREE;
987 $$ = build_tree_list
988 ($1, build_assignment ($2.token, $2.location, $1, $3));
990 | variable_declarator_id ASSIGN_TK error
992 yyerror ("Missing variable initializer");
993 $$ = build_tree_list ($1, NULL_TREE);
994 RECOVER;
996 | variable_declarator_id ASSIGN_TK variable_initializer error
998 yyerror ("';' expected");
999 $$ = build_tree_list ($1, NULL_TREE);
1000 RECOVER;
1004 variable_declarator_id:
1005 identifier
1006 | variable_declarator_id OSB_TK CSB_TK
1007 { $$ = build_unresolved_array_type ($1); }
1008 | identifier error
1009 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
1010 | variable_declarator_id OSB_TK error
1012 tree node = java_lval.node;
1013 if (node && (TREE_CODE (node) == INTEGER_CST
1014 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
1015 yyerror ("Can't specify array dimension in a declaration");
1016 else
1017 yyerror ("']' expected");
1018 DRECOVER(vdi);
1020 | variable_declarator_id CSB_TK error
1021 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1024 variable_initializer:
1025 expression
1026 | array_initializer
1029 /* 19.8.3 Productions from 8.4: Method Declarations */
1030 method_declaration:
1031 method_header
1033 current_function_decl = $1;
1034 if (current_function_decl
1035 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1036 source_start_java_method (current_function_decl);
1037 else
1038 current_function_decl = NULL_TREE;
1040 method_body
1041 { finish_method_declaration ($3); }
1042 | method_header error
1043 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1046 method_header:
1047 type method_declarator throws
1048 { $$ = method_header (0, $1, $2, $3); }
1049 | VOID_TK method_declarator throws
1050 { $$ = method_header (0, void_type_node, $2, $3); }
1051 | modifiers type method_declarator throws
1052 { $$ = method_header ($1, $2, $3, $4); }
1053 | modifiers VOID_TK method_declarator throws
1054 { $$ = method_header ($1, void_type_node, $3, $4); }
1055 | type error
1057 yyerror ("Invalid method declaration, method name required");
1058 RECOVER;
1060 | modifiers type error
1061 {RECOVER;}
1062 | VOID_TK error
1063 {yyerror ("Identifier expected"); RECOVER;}
1064 | modifiers VOID_TK error
1065 {yyerror ("Identifier expected"); RECOVER;}
1066 | modifiers error
1068 yyerror ("Invalid method declaration, return type required");
1069 RECOVER;
1073 method_declarator:
1074 identifier OP_TK CP_TK
1076 ctxp->formal_parameter_number = 0;
1077 $$ = method_declarator ($1, NULL_TREE);
1079 | identifier OP_TK formal_parameter_list CP_TK
1080 { $$ = method_declarator ($1, $3); }
1081 | method_declarator OSB_TK CSB_TK
1083 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1084 TREE_PURPOSE ($1) =
1085 build_unresolved_array_type (TREE_PURPOSE ($1));
1086 parse_warning_context
1087 (wfl_operator,
1088 "Discouraged form of returned type specification");
1090 | identifier OP_TK error
1091 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1092 | method_declarator OSB_TK error
1093 {yyerror ("']' expected"); RECOVER;}
1096 formal_parameter_list:
1097 formal_parameter
1099 ctxp->formal_parameter_number = 1;
1101 | formal_parameter_list C_TK formal_parameter
1103 ctxp->formal_parameter_number += 1;
1104 $$ = chainon ($1, $3);
1106 | formal_parameter_list C_TK error
1107 { yyerror ("Missing formal parameter term"); RECOVER; }
1110 formal_parameter:
1111 type variable_declarator_id
1113 $$ = build_tree_list ($2, $1);
1115 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1117 $$ = build_tree_list ($3, $2);
1118 ARG_FINAL_P ($$) = 1;
1120 | type error
1122 yyerror ("Missing identifier"); RECOVER;
1123 $$ = NULL_TREE;
1125 | final type error
1127 yyerror ("Missing identifier"); RECOVER;
1128 $$ = NULL_TREE;
1132 final:
1133 modifiers
1135 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1136 $1, ACC_FINAL);
1137 if ($1 != ACC_FINAL)
1138 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1142 throws:
1143 { $$ = NULL_TREE; }
1144 | THROWS_TK class_type_list
1145 { $$ = $2; }
1146 | THROWS_TK error
1147 {yyerror ("Missing class type term"); RECOVER;}
1150 class_type_list:
1151 class_type
1152 { $$ = build_tree_list ($1, $1); }
1153 | class_type_list C_TK class_type
1154 { $$ = tree_cons ($3, $3, $1); }
1155 | class_type_list C_TK error
1156 {yyerror ("Missing class type term"); RECOVER;}
1159 method_body:
1160 block
1161 | SC_TK { $$ = NULL_TREE; }
1164 /* 19.8.4 Productions from 8.5: Static Initializers */
1165 static_initializer:
1166 static block
1168 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1169 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1170 current_static_block = NULL_TREE;
1174 static: /* Test lval.sub_token here */
1175 modifiers
1177 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1178 /* Can't have a static initializer in an innerclass */
1179 if ($1 | ACC_STATIC &&
1180 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1181 parse_error_context
1182 (MODIFIER_WFL (STATIC_TK),
1183 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1184 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1185 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1189 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1190 constructor_declaration:
1191 constructor_header
1193 current_function_decl = $1;
1194 source_start_java_method (current_function_decl);
1196 constructor_body
1197 { finish_method_declaration ($3); }
1200 constructor_header:
1201 constructor_declarator throws
1202 { $$ = method_header (0, NULL_TREE, $1, $2); }
1203 | modifiers constructor_declarator throws
1204 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1207 constructor_declarator:
1208 simple_name OP_TK CP_TK
1210 ctxp->formal_parameter_number = 0;
1211 $$ = method_declarator ($1, NULL_TREE);
1213 | simple_name OP_TK formal_parameter_list CP_TK
1214 { $$ = method_declarator ($1, $3); }
1217 constructor_body:
1218 /* Unlike regular method, we always need a complete (empty)
1219 body so we can safely perform all the required code
1220 addition (super invocation and field initialization) */
1221 block_begin constructor_block_end
1223 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1224 $$ = $2;
1226 | block_begin explicit_constructor_invocation constructor_block_end
1227 { $$ = $3; }
1228 | block_begin block_statements constructor_block_end
1229 { $$ = $3; }
1230 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1231 { $$ = $4; }
1234 constructor_block_end:
1235 block_end
1238 /* Error recovery for that rule moved down expression_statement: rule. */
1239 explicit_constructor_invocation:
1240 this_or_super OP_TK CP_TK SC_TK
1242 $$ = build_method_invocation ($1, NULL_TREE);
1243 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1244 $$ = java_method_add_stmt (current_function_decl, $$);
1246 | this_or_super OP_TK argument_list CP_TK SC_TK
1248 $$ = build_method_invocation ($1, $3);
1249 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1250 $$ = java_method_add_stmt (current_function_decl, $$);
1252 /* Added, JDK1.1 inner classes. Modified because the rule
1253 'primary' couldn't work. */
1254 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1255 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1256 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1257 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1260 this_or_super: /* Added, simplifies error diagnostics */
1261 THIS_TK
1263 tree wfl = build_wfl_node (this_identifier_node);
1264 EXPR_WFL_LINECOL (wfl) = $1.location;
1265 $$ = wfl;
1267 | SUPER_TK
1269 tree wfl = build_wfl_node (super_identifier_node);
1270 EXPR_WFL_LINECOL (wfl) = $1.location;
1271 $$ = wfl;
1275 /* 19.9 Productions from 9: Interfaces */
1276 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1277 interface_declaration:
1278 INTERFACE_TK identifier
1279 { create_interface (0, $2, NULL_TREE); }
1280 interface_body
1281 | modifiers INTERFACE_TK identifier
1282 { create_interface ($1, $3, NULL_TREE); }
1283 interface_body
1284 | INTERFACE_TK identifier extends_interfaces
1285 { create_interface (0, $2, $3); }
1286 interface_body
1287 | modifiers INTERFACE_TK identifier extends_interfaces
1288 { create_interface ($1, $3, $4); }
1289 interface_body
1290 | INTERFACE_TK identifier error
1291 {yyerror ("'{' expected"); RECOVER;}
1292 | modifiers INTERFACE_TK identifier error
1293 {yyerror ("'{' expected"); RECOVER;}
1296 extends_interfaces:
1297 EXTENDS_TK interface_type
1299 ctxp->interface_number = 1;
1300 $$ = build_tree_list ($2, NULL_TREE);
1302 | extends_interfaces C_TK interface_type
1304 ctxp->interface_number++;
1305 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1307 | EXTENDS_TK error
1308 {yyerror ("Invalid interface type"); RECOVER;}
1309 | extends_interfaces C_TK error
1310 {yyerror ("Missing term"); RECOVER;}
1313 interface_body:
1314 OCB_TK CCB_TK
1315 { $$ = NULL_TREE; }
1316 | OCB_TK interface_member_declarations CCB_TK
1317 { $$ = NULL_TREE; }
1320 interface_member_declarations:
1321 interface_member_declaration
1322 | interface_member_declarations interface_member_declaration
1325 interface_member_declaration:
1326 constant_declaration
1327 | abstract_method_declaration
1328 | class_declaration /* Added, JDK1.1 inner classes */
1329 { end_class_declaration (1); }
1330 | interface_declaration /* Added, JDK1.1 inner interfaces */
1331 { end_class_declaration (1); }
1334 constant_declaration:
1335 field_declaration
1338 abstract_method_declaration:
1339 method_header SC_TK
1341 check_abstract_method_header ($1);
1342 current_function_decl = NULL_TREE; /* FIXME ? */
1344 | method_header error
1345 {yyerror ("';' expected"); RECOVER;}
1348 /* 19.10 Productions from 10: Arrays */
1349 array_initializer:
1350 OCB_TK CCB_TK
1351 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1352 | OCB_TK C_TK CCB_TK
1353 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1354 | OCB_TK variable_initializers CCB_TK
1355 { $$ = build_new_array_init ($1.location, $2); }
1356 | OCB_TK variable_initializers C_TK CCB_TK
1357 { $$ = build_new_array_init ($1.location, $2); }
1360 variable_initializers:
1361 variable_initializer
1363 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1364 $1, NULL_TREE);
1366 | variable_initializers C_TK variable_initializer
1368 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1370 | variable_initializers C_TK error
1371 {yyerror ("Missing term"); RECOVER;}
1374 /* 19.11 Production from 14: Blocks and Statements */
1375 block:
1376 OCB_TK CCB_TK
1378 /* Store the location of the `}' when doing xrefs */
1379 if (current_function_decl && flag_emit_xref)
1380 DECL_END_SOURCE_LINE (current_function_decl) =
1381 EXPR_WFL_ADD_COL ($2.location, 1);
1382 $$ = empty_stmt_node;
1384 | block_begin block_statements block_end
1385 { $$ = $3; }
1388 block_begin:
1389 OCB_TK
1390 { enter_block (); }
1393 block_end:
1394 CCB_TK
1396 maybe_absorb_scoping_blocks ();
1397 /* Store the location of the `}' when doing xrefs */
1398 if (current_function_decl && flag_emit_xref)
1399 DECL_END_SOURCE_LINE (current_function_decl) =
1400 EXPR_WFL_ADD_COL ($1.location, 1);
1401 $$ = exit_block ();
1402 if (!BLOCK_SUBBLOCKS ($$))
1403 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1407 block_statements:
1408 block_statement
1409 | block_statements block_statement
1412 block_statement:
1413 local_variable_declaration_statement
1414 | statement
1415 { java_method_add_stmt (current_function_decl, $1); }
1416 | class_declaration /* Added, JDK1.1 local classes */
1418 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1419 end_class_declaration (1);
1423 local_variable_declaration_statement:
1424 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1427 local_variable_declaration:
1428 type variable_declarators
1429 { declare_local_variables (0, $1, $2); }
1430 | final type variable_declarators /* Added, JDK1.1 final locals */
1431 { declare_local_variables ($1, $2, $3); }
1434 statement:
1435 statement_without_trailing_substatement
1436 | labeled_statement
1437 | if_then_statement
1438 | if_then_else_statement
1439 | while_statement
1440 | for_statement
1441 { $$ = exit_block (); }
1444 statement_nsi:
1445 statement_without_trailing_substatement
1446 | labeled_statement_nsi
1447 | if_then_else_statement_nsi
1448 | while_statement_nsi
1449 | for_statement_nsi
1450 { $$ = exit_block (); }
1453 statement_without_trailing_substatement:
1454 block
1455 | empty_statement
1456 | expression_statement
1457 | switch_statement
1458 | do_statement
1459 | break_statement
1460 | continue_statement
1461 | return_statement
1462 | synchronized_statement
1463 | throw_statement
1464 | try_statement
1467 empty_statement:
1468 SC_TK
1470 if (flag_extraneous_semicolon
1471 && ! current_static_block
1472 && (! current_function_decl ||
1473 /* Verify we're not in a inner class declaration */
1474 (GET_CPC () != TYPE_NAME
1475 (DECL_CONTEXT (current_function_decl)))))
1478 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1479 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1481 $$ = empty_stmt_node;
1485 label_decl:
1486 identifier REL_CL_TK
1488 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1489 EXPR_WFL_NODE ($1));
1490 pushlevel (2);
1491 push_labeled_block ($$);
1492 PUSH_LABELED_BLOCK ($$);
1496 labeled_statement:
1497 label_decl statement
1498 { $$ = finish_labeled_statement ($1, $2); }
1499 | identifier error
1500 {yyerror ("':' expected"); RECOVER;}
1503 labeled_statement_nsi:
1504 label_decl statement_nsi
1505 { $$ = finish_labeled_statement ($1, $2); }
1508 /* We concentrate here a bunch of error handling rules that we couldn't write
1509 earlier, because expression_statement catches a missing ';'. */
1510 expression_statement:
1511 statement_expression SC_TK
1513 /* We have a statement. Generate a WFL around it so
1514 we can debug it */
1515 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1516 /* We know we have a statement, so set the debug
1517 info to be eventually generate here. */
1518 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1520 | error SC_TK
1522 YYNOT_TWICE yyerror ("Invalid expression statement");
1523 DRECOVER (expr_stmt);
1525 | error OCB_TK
1527 YYNOT_TWICE yyerror ("Invalid expression statement");
1528 DRECOVER (expr_stmt);
1530 | error CCB_TK
1532 YYNOT_TWICE yyerror ("Invalid expression statement");
1533 DRECOVER (expr_stmt);
1535 | this_or_super OP_TK error
1536 {yyerror ("')' expected"); RECOVER;}
1537 | this_or_super OP_TK CP_TK error
1539 parse_ctor_invocation_error ();
1540 RECOVER;
1542 | this_or_super OP_TK argument_list error
1543 {yyerror ("')' expected"); RECOVER;}
1544 | this_or_super OP_TK argument_list CP_TK error
1546 parse_ctor_invocation_error ();
1547 RECOVER;
1549 | name DOT_TK SUPER_TK error
1550 {yyerror ("'(' expected"); RECOVER;}
1551 | name DOT_TK SUPER_TK OP_TK error
1552 {yyerror ("')' expected"); RECOVER;}
1553 | name DOT_TK SUPER_TK OP_TK argument_list error
1554 {yyerror ("')' expected"); RECOVER;}
1555 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1556 {yyerror ("';' expected"); RECOVER;}
1557 | name DOT_TK SUPER_TK OP_TK CP_TK error
1558 {yyerror ("';' expected"); RECOVER;}
1561 statement_expression:
1562 assignment
1563 | pre_increment_expression
1564 | pre_decrement_expression
1565 | post_increment_expression
1566 | post_decrement_expression
1567 | method_invocation
1568 | class_instance_creation_expression
1571 if_then_statement:
1572 IF_TK OP_TK expression CP_TK statement
1574 $$ = build_if_else_statement ($2.location, $3,
1575 $5, NULL_TREE);
1577 | IF_TK error
1578 {yyerror ("'(' expected"); RECOVER;}
1579 | IF_TK OP_TK error
1580 {yyerror ("Missing term"); RECOVER;}
1581 | IF_TK OP_TK expression error
1582 {yyerror ("')' expected"); RECOVER;}
1585 if_then_else_statement:
1586 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1587 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1590 if_then_else_statement_nsi:
1591 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1592 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1595 switch_statement:
1596 switch_expression
1598 enter_block ();
1600 switch_block
1602 /* Make into "proper list" of COMPOUND_EXPRs.
1603 I.e. make the last statment also have its own
1604 COMPOUND_EXPR. */
1605 maybe_absorb_scoping_blocks ();
1606 TREE_OPERAND ($1, 1) = exit_block ();
1607 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1611 switch_expression:
1612 SWITCH_TK OP_TK expression CP_TK
1614 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1615 EXPR_WFL_LINECOL ($$) = $2.location;
1617 | SWITCH_TK error
1618 {yyerror ("'(' expected"); RECOVER;}
1619 | SWITCH_TK OP_TK error
1620 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1621 | SWITCH_TK OP_TK expression CP_TK error
1622 {yyerror ("'{' expected"); RECOVER;}
1625 /* Default assignment is there to avoid type node on switch_block
1626 node. */
1628 switch_block:
1629 OCB_TK CCB_TK
1630 { $$ = NULL_TREE; }
1631 | OCB_TK switch_labels CCB_TK
1632 { $$ = NULL_TREE; }
1633 | OCB_TK switch_block_statement_groups CCB_TK
1634 { $$ = NULL_TREE; }
1635 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1636 { $$ = NULL_TREE; }
1639 switch_block_statement_groups:
1640 switch_block_statement_group
1641 | switch_block_statement_groups switch_block_statement_group
1644 switch_block_statement_group:
1645 switch_labels block_statements
1648 switch_labels:
1649 switch_label
1650 | switch_labels switch_label
1653 switch_label:
1654 CASE_TK constant_expression REL_CL_TK
1656 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1657 EXPR_WFL_LINECOL (lab) = $1.location;
1658 java_method_add_stmt (current_function_decl, lab);
1660 | DEFAULT_TK REL_CL_TK
1662 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1663 EXPR_WFL_LINECOL (lab) = $1.location;
1664 java_method_add_stmt (current_function_decl, lab);
1666 | CASE_TK error
1667 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1668 | CASE_TK constant_expression error
1669 {yyerror ("':' expected"); RECOVER;}
1670 | DEFAULT_TK error
1671 {yyerror ("':' expected"); RECOVER;}
1674 while_expression:
1675 WHILE_TK OP_TK expression CP_TK
1677 tree body = build_loop_body ($2.location, $3, 0);
1678 $$ = build_new_loop (body);
1682 while_statement:
1683 while_expression statement
1684 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1685 | WHILE_TK error
1686 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1687 | WHILE_TK OP_TK error
1688 {yyerror ("Missing term and ')' expected"); RECOVER;}
1689 | WHILE_TK OP_TK expression error
1690 {yyerror ("')' expected"); RECOVER;}
1693 while_statement_nsi:
1694 while_expression statement_nsi
1695 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1698 do_statement_begin:
1699 DO_TK
1701 tree body = build_loop_body (0, NULL_TREE, 1);
1702 $$ = build_new_loop (body);
1704 /* Need error handing here. FIXME */
1707 do_statement:
1708 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1709 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1712 for_statement:
1713 for_begin SC_TK expression SC_TK for_update CP_TK statement
1715 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1716 $3 = build_wfl_node ($3);
1717 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1719 | for_begin SC_TK SC_TK for_update CP_TK statement
1721 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1722 /* We have not condition, so we get rid of the EXIT_EXPR */
1723 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1724 empty_stmt_node;
1726 | for_begin SC_TK error
1727 {yyerror ("Invalid control expression"); RECOVER;}
1728 | for_begin SC_TK expression SC_TK error
1729 {yyerror ("Invalid update expression"); RECOVER;}
1730 | for_begin SC_TK SC_TK error
1731 {yyerror ("Invalid update expression"); RECOVER;}
1734 for_statement_nsi:
1735 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1736 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1737 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1739 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1740 /* We have not condition, so we get rid of the EXIT_EXPR */
1741 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1742 empty_stmt_node;
1746 for_header:
1747 FOR_TK OP_TK
1749 /* This scope defined for local variable that may be
1750 defined within the scope of the for loop */
1751 enter_block ();
1753 | FOR_TK error
1754 {yyerror ("'(' expected"); DRECOVER(for_1);}
1755 | FOR_TK OP_TK error
1756 {yyerror ("Invalid init statement"); RECOVER;}
1759 for_begin:
1760 for_header for_init
1762 /* We now declare the loop body. The loop is
1763 declared as a for loop. */
1764 tree body = build_loop_body (0, NULL_TREE, 0);
1765 $$ = build_new_loop (body);
1766 FOR_LOOP_P ($$) = 1;
1767 /* The loop is added to the current block the for
1768 statement is defined within */
1769 java_method_add_stmt (current_function_decl, $$);
1772 for_init: /* Can be empty */
1773 { $$ = empty_stmt_node; }
1774 | statement_expression_list
1776 /* Init statement recorded within the previously
1777 defined block scope */
1778 $$ = java_method_add_stmt (current_function_decl, $1);
1780 | local_variable_declaration
1782 /* Local variable are recorded within the previously
1783 defined block scope */
1784 $$ = NULL_TREE;
1786 | statement_expression_list error
1787 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1790 for_update: /* Can be empty */
1791 {$$ = empty_stmt_node;}
1792 | statement_expression_list
1793 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1796 statement_expression_list:
1797 statement_expression
1798 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1799 | statement_expression_list C_TK statement_expression
1800 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1801 | statement_expression_list C_TK error
1802 {yyerror ("Missing term"); RECOVER;}
1805 break_statement:
1806 BREAK_TK SC_TK
1807 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1808 | BREAK_TK identifier SC_TK
1809 { $$ = build_bc_statement ($1.location, 1, $2); }
1810 | BREAK_TK error
1811 {yyerror ("Missing term"); RECOVER;}
1812 | BREAK_TK identifier error
1813 {yyerror ("';' expected"); RECOVER;}
1816 continue_statement:
1817 CONTINUE_TK SC_TK
1818 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1819 | CONTINUE_TK identifier SC_TK
1820 { $$ = build_bc_statement ($1.location, 0, $2); }
1821 | CONTINUE_TK error
1822 {yyerror ("Missing term"); RECOVER;}
1823 | CONTINUE_TK identifier error
1824 {yyerror ("';' expected"); RECOVER;}
1827 return_statement:
1828 RETURN_TK SC_TK
1829 { $$ = build_return ($1.location, NULL_TREE); }
1830 | RETURN_TK expression SC_TK
1831 { $$ = build_return ($1.location, $2); }
1832 | RETURN_TK error
1833 {yyerror ("Missing term"); RECOVER;}
1834 | RETURN_TK expression error
1835 {yyerror ("';' expected"); RECOVER;}
1838 throw_statement:
1839 THROW_TK expression SC_TK
1841 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1842 EXPR_WFL_LINECOL ($$) = $1.location;
1844 | THROW_TK error
1845 {yyerror ("Missing term"); RECOVER;}
1846 | THROW_TK expression error
1847 {yyerror ("';' expected"); RECOVER;}
1850 synchronized_statement:
1851 synchronized OP_TK expression CP_TK block
1853 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1854 EXPR_WFL_LINECOL ($$) =
1855 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1857 | synchronized OP_TK expression CP_TK error
1858 {yyerror ("'{' expected"); RECOVER;}
1859 | synchronized error
1860 {yyerror ("'(' expected"); RECOVER;}
1861 | synchronized OP_TK error CP_TK
1862 {yyerror ("Missing term"); RECOVER;}
1863 | synchronized OP_TK error
1864 {yyerror ("Missing term"); RECOVER;}
1867 synchronized:
1868 modifiers
1870 check_modifiers (
1871 "Illegal modifier `%s'. Only `synchronized' was expected here",
1872 $1, ACC_SYNCHRONIZED);
1873 if ($1 != ACC_SYNCHRONIZED)
1874 MODIFIER_WFL (SYNCHRONIZED_TK) =
1875 build_wfl_node (NULL_TREE);
1879 try_statement:
1880 TRY_TK block catches
1881 { $$ = build_try_statement ($1.location, $2, $3); }
1882 | TRY_TK block finally
1883 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1884 | TRY_TK block catches finally
1885 { $$ = build_try_finally_statement
1886 ($1.location, build_try_statement ($1.location,
1887 $2, $3), $4);
1889 | TRY_TK error
1890 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1893 catches:
1894 catch_clause
1895 | catches catch_clause
1897 TREE_CHAIN ($2) = $1;
1898 $$ = $2;
1902 catch_clause:
1903 catch_clause_parameter block
1905 java_method_add_stmt (current_function_decl, $2);
1906 exit_block ();
1907 $$ = $1;
1910 catch_clause_parameter:
1911 CATCH_TK OP_TK formal_parameter CP_TK
1913 /* We add a block to define a scope for
1914 formal_parameter (CCBP). The formal parameter is
1915 declared initialized by the appropriate function
1916 call */
1917 tree ccpb = enter_block ();
1918 tree init = build_assignment
1919 (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1920 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1921 declare_local_variables (0, TREE_VALUE ($3),
1922 build_tree_list (TREE_PURPOSE ($3),
1923 init));
1924 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1925 EXPR_WFL_LINECOL ($$) = $1.location;
1927 | CATCH_TK error
1928 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1929 | CATCH_TK OP_TK error
1931 yyerror ("Missing term or ')' expected");
1932 RECOVER; $$ = NULL_TREE;
1934 | CATCH_TK OP_TK error CP_TK /* That's for () */
1935 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1938 finally:
1939 FINALLY_TK block
1940 { $$ = $2; }
1941 | FINALLY_TK error
1942 {yyerror ("'{' expected"); RECOVER; }
1945 /* 19.12 Production from 15: Expressions */
1946 primary:
1947 primary_no_new_array
1948 | array_creation_expression
1951 primary_no_new_array:
1952 literal
1953 | THIS_TK
1954 { $$ = build_this ($1.location); }
1955 | OP_TK expression CP_TK
1956 {$$ = $2;}
1957 | class_instance_creation_expression
1958 | field_access
1959 | method_invocation
1960 | array_access
1961 | type_literals
1962 /* Added, JDK1.1 inner classes. Documentation is wrong
1963 refering to a 'ClassName' (class_name) rule that doesn't
1964 exist. Used name: instead. */
1965 | name DOT_TK THIS_TK
1967 tree wfl = build_wfl_node (this_identifier_node);
1968 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1970 | OP_TK expression error
1971 {yyerror ("')' expected"); RECOVER;}
1972 | name DOT_TK error
1973 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1974 | primitive_type DOT_TK error
1975 {yyerror ("'class' expected" ); RECOVER;}
1976 | VOID_TK DOT_TK error
1977 {yyerror ("'class' expected" ); RECOVER;}
1980 type_literals:
1981 name DOT_TK CLASS_TK
1982 { $$ = build_incomplete_class_ref ($2.location, $1); }
1983 | array_type DOT_TK CLASS_TK
1984 { $$ = build_incomplete_class_ref ($2.location, $1); }
1985 | primitive_type DOT_TK CLASS_TK
1986 { $$ = build_incomplete_class_ref ($2.location, $1); }
1987 | VOID_TK DOT_TK CLASS_TK
1989 $$ = build_incomplete_class_ref ($2.location,
1990 void_type_node);
1994 class_instance_creation_expression:
1995 NEW_TK class_type OP_TK argument_list CP_TK
1996 { $$ = build_new_invocation ($2, $4); }
1997 | NEW_TK class_type OP_TK CP_TK
1998 { $$ = build_new_invocation ($2, NULL_TREE); }
1999 | anonymous_class_creation
2000 /* Added, JDK1.1 inner classes, modified to use name or
2001 primary instead of primary solely which couldn't work in
2002 all situations. */
2003 | something_dot_new identifier OP_TK CP_TK
2005 tree ctor = build_new_invocation ($2, NULL_TREE);
2006 $$ = make_qualified_primary ($1, ctor,
2007 EXPR_WFL_LINECOL ($1));
2009 | something_dot_new identifier OP_TK CP_TK class_body
2010 | something_dot_new identifier OP_TK argument_list CP_TK
2012 tree ctor = build_new_invocation ($2, $4);
2013 $$ = make_qualified_primary ($1, ctor,
2014 EXPR_WFL_LINECOL ($1));
2016 | something_dot_new identifier OP_TK argument_list CP_TK class_body
2017 | NEW_TK error SC_TK
2018 {yyerror ("'(' expected"); DRECOVER(new_1);}
2019 | NEW_TK class_type error
2020 {yyerror ("'(' expected"); RECOVER;}
2021 | NEW_TK class_type OP_TK error
2022 {yyerror ("')' or term expected"); RECOVER;}
2023 | NEW_TK class_type OP_TK argument_list error
2024 {yyerror ("')' expected"); RECOVER;}
2025 | something_dot_new error
2026 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
2027 | something_dot_new identifier error
2028 {yyerror ("'(' expected"); RECOVER;}
2031 /* Created after JDK1.1 rules originally added to
2032 class_instance_creation_expression, but modified to use
2033 'class_type' instead of 'TypeName' (type_name) which is mentionned
2034 in the documentation but doesn't exist. */
2036 anonymous_class_creation:
2037 NEW_TK class_type OP_TK argument_list CP_TK
2038 { create_anonymous_class ($1.location, $2); }
2039 class_body
2041 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2042 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2044 end_class_declaration (1);
2046 /* Now we can craft the new expression */
2047 $$ = build_new_invocation (id, $4);
2049 /* Note that we can't possibly be here if
2050 `class_type' is an interface (in which case the
2051 anonymous class extends Object and implements
2052 `class_type', hence its constructor can't have
2053 arguments.) */
2055 /* Otherwise, the innerclass must feature a
2056 constructor matching `argument_list'. Anonymous
2057 classes are a bit special: it's impossible to
2058 define constructor for them, hence constructors
2059 must be generated following the hints provided by
2060 the `new' expression. Whether a super constructor
2061 of that nature exists or not is to be verified
2062 later on in verify_constructor_super.
2064 It's during the expansion of a `new' statement
2065 refering to an anonymous class that a ctor will
2066 be generated for the anonymous class, with the
2067 right arguments. */
2070 | NEW_TK class_type OP_TK CP_TK
2071 { create_anonymous_class ($1.location, $2); }
2072 class_body
2074 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2075 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2077 end_class_declaration (1);
2079 /* Now we can craft the new expression. The
2080 statement doesn't need to be remember so that a
2081 constructor can be generated, since its signature
2082 is already known. */
2083 $$ = build_new_invocation (id, NULL_TREE);
2087 something_dot_new: /* Added, not part of the specs. */
2088 name DOT_TK NEW_TK
2089 { $$ = $1; }
2090 | primary DOT_TK NEW_TK
2091 { $$ = $1; }
2094 argument_list:
2095 expression
2097 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2098 ctxp->formal_parameter_number = 1;
2100 | argument_list C_TK expression
2102 ctxp->formal_parameter_number += 1;
2103 $$ = tree_cons (NULL_TREE, $3, $1);
2105 | argument_list C_TK error
2106 {yyerror ("Missing term"); RECOVER;}
2109 array_creation_expression:
2110 NEW_TK primitive_type dim_exprs
2111 { $$ = build_newarray_node ($2, $3, 0); }
2112 | NEW_TK class_or_interface_type dim_exprs
2113 { $$ = build_newarray_node ($2, $3, 0); }
2114 | NEW_TK primitive_type dim_exprs dims
2115 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2116 | NEW_TK class_or_interface_type dim_exprs dims
2117 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2118 /* Added, JDK1.1 anonymous array. Initial documentation rule
2119 modified */
2120 | NEW_TK class_or_interface_type dims array_initializer
2122 char *sig;
2123 int osb = pop_current_osb (ctxp);
2124 while (osb--)
2125 obstack_grow (&temporary_obstack, "[]", 2);
2126 obstack_1grow (&temporary_obstack, '\0');
2127 sig = obstack_finish (&temporary_obstack);
2128 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2129 $2, get_identifier (sig), $4);
2131 | NEW_TK primitive_type dims array_initializer
2133 int osb = pop_current_osb (ctxp);
2134 tree type = $2;
2135 while (osb--)
2136 type = build_java_array_type (type, -1);
2137 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2138 build_pointer_type (type), NULL_TREE, $4);
2140 | NEW_TK error CSB_TK
2141 {yyerror ("'[' expected"); DRECOVER ("]");}
2142 | NEW_TK error OSB_TK
2143 {yyerror ("']' expected"); RECOVER;}
2146 dim_exprs:
2147 dim_expr
2148 { $$ = build_tree_list (NULL_TREE, $1); }
2149 | dim_exprs dim_expr
2150 { $$ = tree_cons (NULL_TREE, $2, $$); }
2153 dim_expr:
2154 OSB_TK expression CSB_TK
2156 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2158 $2 = build_wfl_node ($2);
2159 TREE_TYPE ($2) = NULL_TREE;
2161 EXPR_WFL_LINECOL ($2) = $1.location;
2162 $$ = $2;
2164 | OSB_TK expression error
2165 {yyerror ("']' expected"); RECOVER;}
2166 | OSB_TK error
2168 yyerror ("Missing term");
2169 yyerror ("']' expected");
2170 RECOVER;
2174 dims:
2175 OSB_TK CSB_TK
2177 int allocate = 0;
2178 /* If not initialized, allocate memory for the osb
2179 numbers stack */
2180 if (!ctxp->osb_limit)
2182 allocate = ctxp->osb_limit = 32;
2183 ctxp->osb_depth = -1;
2185 /* If capacity overflown, reallocate a bigger chunk */
2186 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2187 allocate = ctxp->osb_limit << 1;
2189 if (allocate)
2191 allocate *= sizeof (int);
2192 if (ctxp->osb_number)
2193 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2194 allocate);
2195 else
2196 ctxp->osb_number = (int *)xmalloc (allocate);
2198 ctxp->osb_depth++;
2199 CURRENT_OSB (ctxp) = 1;
2201 | dims OSB_TK CSB_TK
2202 { CURRENT_OSB (ctxp)++; }
2203 | dims OSB_TK error
2204 { yyerror ("']' expected"); RECOVER;}
2207 field_access:
2208 primary DOT_TK identifier
2209 { $$ = make_qualified_primary ($1, $3, $2.location); }
2210 /* FIXME - REWRITE TO:
2211 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2212 | SUPER_TK DOT_TK identifier
2214 tree super_wfl = build_wfl_node (super_identifier_node);
2215 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2216 $$ = make_qualified_name (super_wfl, $3, $2.location);
2218 | SUPER_TK error
2219 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2222 method_invocation:
2223 name OP_TK CP_TK
2224 { $$ = build_method_invocation ($1, NULL_TREE); }
2225 | name OP_TK argument_list CP_TK
2226 { $$ = build_method_invocation ($1, $3); }
2227 | primary DOT_TK identifier OP_TK CP_TK
2229 if (TREE_CODE ($1) == THIS_EXPR)
2230 $$ = build_this_super_qualified_invocation
2231 (1, $3, NULL_TREE, 0, $2.location);
2232 else
2234 tree invok = build_method_invocation ($3, NULL_TREE);
2235 $$ = make_qualified_primary ($1, invok, $2.location);
2238 | primary DOT_TK identifier OP_TK argument_list CP_TK
2240 if (TREE_CODE ($1) == THIS_EXPR)
2241 $$ = build_this_super_qualified_invocation
2242 (1, $3, $5, 0, $2.location);
2243 else
2245 tree invok = build_method_invocation ($3, $5);
2246 $$ = make_qualified_primary ($1, invok, $2.location);
2249 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2251 $$ = build_this_super_qualified_invocation
2252 (0, $3, NULL_TREE, $1.location, $2.location);
2254 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2256 $$ = build_this_super_qualified_invocation
2257 (0, $3, $5, $1.location, $2.location);
2259 /* Screws up thing. I let it here until I'm convinced it can
2260 be removed. FIXME
2261 | primary DOT_TK error
2262 {yyerror ("'(' expected"); DRECOVER(bad);} */
2263 | SUPER_TK DOT_TK error CP_TK
2264 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2265 | SUPER_TK DOT_TK error DOT_TK
2266 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2269 array_access:
2270 name OSB_TK expression CSB_TK
2271 { $$ = build_array_ref ($2.location, $1, $3); }
2272 | primary_no_new_array OSB_TK expression CSB_TK
2273 { $$ = build_array_ref ($2.location, $1, $3); }
2274 | name OSB_TK error
2276 yyerror ("Missing term and ']' expected");
2277 DRECOVER(array_access);
2279 | name OSB_TK expression error
2281 yyerror ("']' expected");
2282 DRECOVER(array_access);
2284 | primary_no_new_array OSB_TK error
2286 yyerror ("Missing term and ']' expected");
2287 DRECOVER(array_access);
2289 | primary_no_new_array OSB_TK expression error
2291 yyerror ("']' expected");
2292 DRECOVER(array_access);
2296 postfix_expression:
2297 primary
2298 | name
2299 | post_increment_expression
2300 | post_decrement_expression
2303 post_increment_expression:
2304 postfix_expression INCR_TK
2305 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2308 post_decrement_expression:
2309 postfix_expression DECR_TK
2310 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2313 unary_expression:
2314 pre_increment_expression
2315 | pre_decrement_expression
2316 | PLUS_TK unary_expression
2317 {$$ = build_unaryop ($1.token, $1.location, $2); }
2318 | MINUS_TK unary_expression
2319 {$$ = build_unaryop ($1.token, $1.location, $2); }
2320 | unary_expression_not_plus_minus
2321 | PLUS_TK error
2322 {yyerror ("Missing term"); RECOVER}
2323 | MINUS_TK error
2324 {yyerror ("Missing term"); RECOVER}
2327 pre_increment_expression:
2328 INCR_TK unary_expression
2329 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2330 | INCR_TK error
2331 {yyerror ("Missing term"); RECOVER}
2334 pre_decrement_expression:
2335 DECR_TK unary_expression
2336 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2337 | DECR_TK error
2338 {yyerror ("Missing term"); RECOVER}
2341 unary_expression_not_plus_minus:
2342 postfix_expression
2343 | NOT_TK unary_expression
2344 {$$ = build_unaryop ($1.token, $1.location, $2); }
2345 | NEG_TK unary_expression
2346 {$$ = build_unaryop ($1.token, $1.location, $2); }
2347 | cast_expression
2348 | NOT_TK error
2349 {yyerror ("Missing term"); RECOVER}
2350 | NEG_TK error
2351 {yyerror ("Missing term"); RECOVER}
2354 cast_expression: /* Error handling here is potentially weak */
2355 OP_TK primitive_type dims CP_TK unary_expression
2357 tree type = $2;
2358 int osb = pop_current_osb (ctxp);
2359 while (osb--)
2360 type = build_java_array_type (type, -1);
2361 $$ = build_cast ($1.location, type, $5);
2363 | OP_TK primitive_type CP_TK unary_expression
2364 { $$ = build_cast ($1.location, $2, $4); }
2365 | OP_TK expression CP_TK unary_expression_not_plus_minus
2366 { $$ = build_cast ($1.location, $2, $4); }
2367 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2369 const char *ptr;
2370 int osb = pop_current_osb (ctxp);
2371 obstack_grow (&temporary_obstack,
2372 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2373 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2374 while (osb--)
2375 obstack_grow (&temporary_obstack, "[]", 2);
2376 obstack_1grow (&temporary_obstack, '\0');
2377 ptr = obstack_finish (&temporary_obstack);
2378 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2379 $$ = build_cast ($1.location, $2, $5);
2381 | OP_TK primitive_type OSB_TK error
2382 {yyerror ("']' expected, invalid type expression");}
2383 | OP_TK error
2385 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2386 RECOVER;
2388 | OP_TK primitive_type dims CP_TK error
2389 {yyerror ("Missing term"); RECOVER;}
2390 | OP_TK primitive_type CP_TK error
2391 {yyerror ("Missing term"); RECOVER;}
2392 | OP_TK name dims CP_TK error
2393 {yyerror ("Missing term"); RECOVER;}
2396 multiplicative_expression:
2397 unary_expression
2398 | multiplicative_expression MULT_TK unary_expression
2400 $$ = build_binop (BINOP_LOOKUP ($2.token),
2401 $2.location, $1, $3);
2403 | multiplicative_expression DIV_TK unary_expression
2405 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2406 $1, $3);
2408 | multiplicative_expression REM_TK unary_expression
2410 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2411 $1, $3);
2413 | multiplicative_expression MULT_TK error
2414 {yyerror ("Missing term"); RECOVER;}
2415 | multiplicative_expression DIV_TK error
2416 {yyerror ("Missing term"); RECOVER;}
2417 | multiplicative_expression REM_TK error
2418 {yyerror ("Missing term"); RECOVER;}
2421 additive_expression:
2422 multiplicative_expression
2423 | additive_expression PLUS_TK multiplicative_expression
2425 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2426 $1, $3);
2428 | additive_expression MINUS_TK multiplicative_expression
2430 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2431 $1, $3);
2433 | additive_expression PLUS_TK error
2434 {yyerror ("Missing term"); RECOVER;}
2435 | additive_expression MINUS_TK error
2436 {yyerror ("Missing term"); RECOVER;}
2439 shift_expression:
2440 additive_expression
2441 | shift_expression LS_TK additive_expression
2443 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2444 $1, $3);
2446 | shift_expression SRS_TK additive_expression
2448 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2449 $1, $3);
2451 | shift_expression ZRS_TK additive_expression
2453 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2454 $1, $3);
2456 | shift_expression LS_TK error
2457 {yyerror ("Missing term"); RECOVER;}
2458 | shift_expression SRS_TK error
2459 {yyerror ("Missing term"); RECOVER;}
2460 | shift_expression ZRS_TK error
2461 {yyerror ("Missing term"); RECOVER;}
2464 relational_expression:
2465 shift_expression
2466 | relational_expression LT_TK shift_expression
2468 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2469 $1, $3);
2471 | relational_expression GT_TK shift_expression
2473 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2474 $1, $3);
2476 | relational_expression LTE_TK shift_expression
2478 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2479 $1, $3);
2481 | relational_expression GTE_TK shift_expression
2483 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2484 $1, $3);
2486 | relational_expression INSTANCEOF_TK reference_type
2487 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2488 | relational_expression LT_TK error
2489 {yyerror ("Missing term"); RECOVER;}
2490 | relational_expression GT_TK error
2491 {yyerror ("Missing term"); RECOVER;}
2492 | relational_expression LTE_TK error
2493 {yyerror ("Missing term"); RECOVER;}
2494 | relational_expression GTE_TK error
2495 {yyerror ("Missing term"); RECOVER;}
2496 | relational_expression INSTANCEOF_TK error
2497 {yyerror ("Invalid reference type"); RECOVER;}
2500 equality_expression:
2501 relational_expression
2502 | equality_expression EQ_TK relational_expression
2504 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2505 $1, $3);
2507 | equality_expression NEQ_TK relational_expression
2509 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2510 $1, $3);
2512 | equality_expression EQ_TK error
2513 {yyerror ("Missing term"); RECOVER;}
2514 | equality_expression NEQ_TK error
2515 {yyerror ("Missing term"); RECOVER;}
2518 and_expression:
2519 equality_expression
2520 | and_expression AND_TK equality_expression
2522 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2523 $1, $3);
2525 | and_expression AND_TK error
2526 {yyerror ("Missing term"); RECOVER;}
2529 exclusive_or_expression:
2530 and_expression
2531 | exclusive_or_expression XOR_TK and_expression
2533 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2534 $1, $3);
2536 | exclusive_or_expression XOR_TK error
2537 {yyerror ("Missing term"); RECOVER;}
2540 inclusive_or_expression:
2541 exclusive_or_expression
2542 | inclusive_or_expression OR_TK exclusive_or_expression
2544 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2545 $1, $3);
2547 | inclusive_or_expression OR_TK error
2548 {yyerror ("Missing term"); RECOVER;}
2551 conditional_and_expression:
2552 inclusive_or_expression
2553 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2555 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2556 $1, $3);
2558 | conditional_and_expression BOOL_AND_TK error
2559 {yyerror ("Missing term"); RECOVER;}
2562 conditional_or_expression:
2563 conditional_and_expression
2564 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2566 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2567 $1, $3);
2569 | conditional_or_expression BOOL_OR_TK error
2570 {yyerror ("Missing term"); RECOVER;}
2573 conditional_expression: /* Error handling here is weak */
2574 conditional_or_expression
2575 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2577 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2578 EXPR_WFL_LINECOL ($$) = $2.location;
2580 | conditional_or_expression REL_QM_TK REL_CL_TK error
2582 YYERROR_NOW;
2583 yyerror ("Missing term");
2584 DRECOVER (1);
2586 | conditional_or_expression REL_QM_TK error
2587 {yyerror ("Missing term"); DRECOVER (2);}
2588 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2589 {yyerror ("Missing term"); DRECOVER (3);}
2592 assignment_expression:
2593 conditional_expression
2594 | assignment
2597 assignment:
2598 left_hand_side assignment_operator assignment_expression
2599 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2600 | left_hand_side assignment_operator error
2602 YYNOT_TWICE yyerror ("Missing term");
2603 DRECOVER (assign);
2607 left_hand_side:
2608 name
2609 | field_access
2610 | array_access
2613 assignment_operator:
2614 ASSIGN_ANY_TK
2615 | ASSIGN_TK
2618 expression:
2619 assignment_expression
2622 constant_expression:
2623 expression
2628 /* Helper function to retrieve an OSB count. Should be used when the
2629 `dims:' rule is being used. */
2631 static int
2632 pop_current_osb (ctxp)
2633 struct parser_ctxt *ctxp;
2635 int to_return;
2637 if (ctxp->osb_depth < 0)
2638 abort ();
2640 to_return = CURRENT_OSB (ctxp);
2641 ctxp->osb_depth--;
2643 return to_return;
2648 /* This section of the code deal with save/restoring parser contexts.
2649 Add mode documentation here. FIXME */
2651 /* Helper function. Create a new parser context. With
2652 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2653 context is copied, otherwise, the new context is zeroed. The newly
2654 created context becomes the current one. */
2656 static void
2657 create_new_parser_context (copy_from_previous)
2658 int copy_from_previous;
2660 struct parser_ctxt *new;
2662 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2663 if (copy_from_previous)
2665 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2666 new->saved_data_ctx = 1;
2668 else
2669 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2671 new->next = ctxp;
2672 ctxp = new;
2675 /* Create a new parser context and make it the current one. */
2677 void
2678 java_push_parser_context ()
2680 create_new_parser_context (0);
2683 void
2684 java_pop_parser_context (generate)
2685 int generate;
2687 tree current;
2688 struct parser_ctxt *toFree, *next;
2690 if (!ctxp)
2691 return;
2693 toFree = ctxp;
2694 next = ctxp->next;
2695 if (next)
2697 lineno = ctxp->lineno;
2698 current_class = ctxp->class_type;
2701 /* If the old and new lexers differ, then free the old one. */
2702 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2703 java_destroy_lexer (ctxp->lexer);
2705 /* Set the single import class file flag to 0 for the current list
2706 of imported things */
2707 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2708 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2710 /* And restore those of the previous context */
2711 if ((ctxp = next)) /* Assignment is really meant here */
2712 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2713 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2715 /* If we pushed a context to parse a class intended to be generated,
2716 we keep it so we can remember the class. What we could actually
2717 do is to just update a list of class names. */
2718 if (generate)
2720 toFree->next = ctxp_for_generation;
2721 ctxp_for_generation = toFree;
2723 else
2724 free (toFree);
2727 /* Create a parser context for the use of saving some global
2728 variables. */
2730 void
2731 java_parser_context_save_global ()
2733 if (!ctxp)
2735 java_push_parser_context ();
2736 ctxp->saved_data_ctx = 1;
2739 /* If this context already stores data, create a new one suitable
2740 for data storage. */
2741 else if (ctxp->saved_data)
2742 create_new_parser_context (1);
2744 ctxp->lineno = lineno;
2745 ctxp->class_type = current_class;
2746 ctxp->filename = input_filename;
2747 ctxp->function_decl = current_function_decl;
2748 ctxp->saved_data = 1;
2751 /* Restore some global variables from the previous context. Make the
2752 previous context the current one. */
2754 void
2755 java_parser_context_restore_global ()
2757 lineno = ctxp->lineno;
2758 current_class = ctxp->class_type;
2759 input_filename = ctxp->filename;
2760 if (wfl_operator)
2762 tree s;
2763 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2764 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2766 current_function_decl = ctxp->function_decl;
2767 ctxp->saved_data = 0;
2768 if (ctxp->saved_data_ctx)
2769 java_pop_parser_context (0);
2772 /* Suspend vital data for the current class/function being parsed so
2773 that an other class can be parsed. Used to let local/anonymous
2774 classes be parsed. */
2776 static void
2777 java_parser_context_suspend ()
2779 /* This makes debugging through java_debug_context easier */
2780 static const char *const name = "<inner buffer context>";
2782 /* Duplicate the previous context, use it to save the globals we're
2783 interested in */
2784 create_new_parser_context (1);
2785 ctxp->function_decl = current_function_decl;
2786 ctxp->class_type = current_class;
2788 /* Then create a new context which inherits all data from the
2789 previous one. This will be the new current context */
2790 create_new_parser_context (1);
2792 /* Help debugging */
2793 ctxp->next->filename = name;
2796 /* Resume vital data for the current class/function being parsed so
2797 that an other class can be parsed. Used to let local/anonymous
2798 classes be parsed. The trick is the data storing file position
2799 informations must be restored to their current value, so parsing
2800 can resume as if no context was ever saved. */
2802 static void
2803 java_parser_context_resume ()
2805 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2806 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2807 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2809 /* We need to inherit the list of classes to complete/generate */
2810 restored->classd_list = old->classd_list;
2811 restored->class_list = old->class_list;
2813 /* Restore the current class and function from the saver */
2814 current_class = saver->class_type;
2815 current_function_decl = saver->function_decl;
2817 /* Retrive the restored context */
2818 ctxp = restored;
2820 /* Re-installed the data for the parsing to carry on */
2821 memcpy (&ctxp->marker_begining, &old->marker_begining,
2822 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2824 /* Buffer context can now be discarded */
2825 free (saver);
2826 free (old);
2829 /* Add a new anchor node to which all statement(s) initializing static
2830 and non static initialized upon declaration field(s) will be
2831 linked. */
2833 static void
2834 java_parser_context_push_initialized_field ()
2836 tree node;
2838 node = build_tree_list (NULL_TREE, NULL_TREE);
2839 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2840 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2842 node = build_tree_list (NULL_TREE, NULL_TREE);
2843 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2844 CPC_INITIALIZER_LIST (ctxp) = node;
2846 node = build_tree_list (NULL_TREE, NULL_TREE);
2847 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2848 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2851 /* Pop the lists of initialized field. If this lists aren't empty,
2852 remember them so we can use it to create and populate the finit$
2853 or <clinit> functions. */
2855 static void
2856 java_parser_context_pop_initialized_field ()
2858 tree stmts;
2859 tree class_type = TREE_TYPE (GET_CPC ());
2861 if (CPC_INITIALIZER_LIST (ctxp))
2863 stmts = CPC_INITIALIZER_STMT (ctxp);
2864 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2865 if (stmts && !java_error_count)
2866 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2869 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2871 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2872 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2873 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2874 /* Keep initialization in order to enforce 8.5 */
2875 if (stmts && !java_error_count)
2876 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2879 /* JDK 1.1 instance initializers */
2880 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2882 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2883 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2884 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2885 if (stmts && !java_error_count)
2886 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2890 static tree
2891 reorder_static_initialized (list)
2892 tree list;
2894 /* We have to keep things in order. The alias initializer have to
2895 come first, then the initialized regular field, in reverse to
2896 keep them in lexical order. */
2897 tree marker, previous = NULL_TREE;
2898 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2899 if (TREE_CODE (marker) == TREE_LIST
2900 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2901 break;
2903 /* No static initialized, the list is fine as is */
2904 if (!previous)
2905 list = TREE_CHAIN (marker);
2907 /* No marker? reverse the whole list */
2908 else if (!marker)
2909 list = nreverse (list);
2911 /* Otherwise, reverse what's after the marker and the new reordered
2912 sublist will replace the marker. */
2913 else
2915 TREE_CHAIN (previous) = NULL_TREE;
2916 list = nreverse (list);
2917 list = chainon (TREE_CHAIN (marker), list);
2919 return list;
2922 /* Helper functions to dump the parser context stack. */
2924 #define TAB_CONTEXT(C) \
2925 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2927 static void
2928 java_debug_context_do (tab)
2929 int tab;
2931 struct parser_ctxt *copy = ctxp;
2932 while (copy)
2934 TAB_CONTEXT (tab);
2935 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2936 TAB_CONTEXT (tab);
2937 fprintf (stderr, "filename: %s\n", copy->filename);
2938 TAB_CONTEXT (tab);
2939 fprintf (stderr, "lineno: %d\n", copy->lineno);
2940 TAB_CONTEXT (tab);
2941 fprintf (stderr, "package: %s\n",
2942 (copy->package ?
2943 IDENTIFIER_POINTER (copy->package) : "<none>"));
2944 TAB_CONTEXT (tab);
2945 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2946 TAB_CONTEXT (tab);
2947 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2948 copy = copy->next;
2949 tab += 2;
2953 /* Dump the stacked up parser contexts. Intended to be called from a
2954 debugger. */
2956 void
2957 java_debug_context ()
2959 java_debug_context_do (0);
2964 /* Flag for the error report routine to issue the error the first time
2965 it's called (overriding the default behavior which is to drop the
2966 first invocation and honor the second one, taking advantage of a
2967 richer context. */
2968 static int force_error = 0;
2970 /* Reporting an constructor invocation error. */
2971 static void
2972 parse_ctor_invocation_error ()
2974 if (DECL_CONSTRUCTOR_P (current_function_decl))
2975 yyerror ("Constructor invocation must be first thing in a constructor");
2976 else
2977 yyerror ("Only constructors can invoke constructors");
2980 /* Reporting JDK1.1 features not implemented. */
2982 static tree
2983 parse_jdk1_1_error (msg)
2984 const char *msg;
2986 sorry (": `%s' JDK1.1(TM) feature", msg);
2987 java_error_count++;
2988 return empty_stmt_node;
2991 static int do_warning = 0;
2993 void
2994 yyerror (msg)
2995 const char *msg;
2997 static java_lc elc;
2998 static int prev_lineno;
2999 static const char *prev_msg;
3001 int save_lineno;
3002 char *remainder, *code_from_source;
3004 if (!force_error && prev_lineno == lineno)
3005 return;
3007 /* Save current error location but report latter, when the context is
3008 richer. */
3009 if (ctxp->java_error_flag == 0)
3011 ctxp->java_error_flag = 1;
3012 elc = ctxp->elc;
3013 /* Do something to use the previous line if we're reaching the
3014 end of the file... */
3015 #ifdef VERBOSE_SKELETON
3016 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
3017 #endif
3018 return;
3021 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3022 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
3023 return;
3025 ctxp->java_error_flag = 0;
3026 if (do_warning)
3027 java_warning_count++;
3028 else
3029 java_error_count++;
3031 if (elc.col == 0 && msg && msg[1] == ';')
3033 elc.col = ctxp->p_line->char_col-1;
3034 elc.line = ctxp->p_line->lineno;
3037 save_lineno = lineno;
3038 prev_lineno = lineno = elc.line;
3039 prev_msg = msg;
3041 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3042 obstack_grow0 (&temporary_obstack,
3043 code_from_source, strlen (code_from_source));
3044 remainder = obstack_finish (&temporary_obstack);
3045 if (do_warning)
3046 warning ("%s.\n%s", msg, remainder);
3047 else
3048 error ("%s.\n%s", msg, remainder);
3050 /* This allow us to cheaply avoid an extra 'Invalid expression
3051 statement' error report when errors have been already reported on
3052 the same line. This occurs when we report an error but don't have
3053 a synchronization point other than ';', which
3054 expression_statement is the only one to take care of. */
3055 ctxp->prevent_ese = lineno = save_lineno;
3058 static void
3059 issue_warning_error_from_context (cl, msg, ap)
3060 tree cl;
3061 const char *msg;
3062 va_list ap;
3064 const char *saved, *saved_input_filename;
3065 char buffer [4096];
3066 vsprintf (buffer, msg, ap);
3067 force_error = 1;
3069 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3070 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3071 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3073 /* We have a CL, that's a good reason for using it if it contains data */
3074 saved = ctxp->filename;
3075 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3076 ctxp->filename = EXPR_WFL_FILENAME (cl);
3077 saved_input_filename = input_filename;
3078 input_filename = ctxp->filename;
3079 java_error (NULL);
3080 java_error (buffer);
3081 ctxp->filename = saved;
3082 input_filename = saved_input_filename;
3083 force_error = 0;
3086 /* Issue an error message at a current source line CL */
3088 void
3089 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3091 VA_OPEN (ap, msg);
3092 VA_FIXEDARG (ap, tree, cl);
3093 VA_FIXEDARG (ap, const char *, msg);
3094 issue_warning_error_from_context (cl, msg, ap);
3095 VA_CLOSE (ap);
3098 /* Issue a warning at a current source line CL */
3100 static void
3101 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3103 VA_OPEN (ap, msg);
3104 VA_FIXEDARG (ap, tree, cl);
3105 VA_FIXEDARG (ap, const char *, msg);
3107 force_error = do_warning = 1;
3108 issue_warning_error_from_context (cl, msg, ap);
3109 do_warning = force_error = 0;
3110 VA_CLOSE (ap);
3113 static tree
3114 find_expr_with_wfl (node)
3115 tree node;
3117 while (node)
3119 char code;
3120 tree to_return;
3122 switch (TREE_CODE (node))
3124 case BLOCK:
3125 node = BLOCK_EXPR_BODY (node);
3126 continue;
3128 case COMPOUND_EXPR:
3129 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3130 if (to_return)
3131 return to_return;
3132 node = TREE_OPERAND (node, 1);
3133 continue;
3135 case LOOP_EXPR:
3136 node = TREE_OPERAND (node, 0);
3137 continue;
3139 case LABELED_BLOCK_EXPR:
3140 node = TREE_OPERAND (node, 1);
3141 continue;
3143 default:
3144 code = TREE_CODE_CLASS (TREE_CODE (node));
3145 if (((code == '1') || (code == '2') || (code == 'e'))
3146 && EXPR_WFL_LINECOL (node))
3147 return node;
3148 return NULL_TREE;
3151 return NULL_TREE;
3154 /* Issue a missing return statement error. Uses METHOD to figure the
3155 last line of the method the error occurs in. */
3157 static void
3158 missing_return_error (method)
3159 tree method;
3161 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3162 parse_error_context (wfl_operator, "Missing return statement");
3165 /* Issue an unreachable statement error. From NODE, find the next
3166 statement to report appropriately. */
3167 static void
3168 unreachable_stmt_error (node)
3169 tree node;
3171 /* Browse node to find the next expression node that has a WFL. Use
3172 the location to report the error */
3173 if (TREE_CODE (node) == COMPOUND_EXPR)
3174 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3175 else
3176 node = find_expr_with_wfl (node);
3178 if (node)
3180 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3181 parse_error_context (wfl_operator, "Unreachable statement");
3183 else
3184 abort ();
3188 java_report_errors ()
3190 if (java_error_count)
3191 fprintf (stderr, "%d error%s",
3192 java_error_count, (java_error_count == 1 ? "" : "s"));
3193 if (java_warning_count)
3194 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3195 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3196 if (java_error_count || java_warning_count)
3197 putc ('\n', stderr);
3198 return java_error_count;
3201 static char *
3202 java_accstring_lookup (flags)
3203 int flags;
3205 static char buffer [80];
3206 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3208 /* Access modifier looked-up first for easier report on forbidden
3209 access. */
3210 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3211 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3212 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3213 if (flags & ACC_STATIC) COPY_RETURN ("static");
3214 if (flags & ACC_FINAL) COPY_RETURN ("final");
3215 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3216 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3217 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3218 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3219 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3220 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3222 buffer [0] = '\0';
3223 return buffer;
3224 #undef COPY_RETURN
3227 /* Issuing error messages upon redefinition of classes, interfaces or
3228 variables. */
3230 static void
3231 classitf_redefinition_error (context, id, decl, cl)
3232 const char *context;
3233 tree id, decl, cl;
3235 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3236 context, IDENTIFIER_POINTER (id),
3237 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3238 /* Here we should point out where its redefined. It's a unicode. FIXME */
3241 static void
3242 variable_redefinition_error (context, name, type, line)
3243 tree context, name, type;
3244 int line;
3246 const char *type_name;
3248 /* Figure a proper name for type. We might haven't resolved it */
3249 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3250 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3251 else
3252 type_name = lang_printable_name (type, 0);
3254 parse_error_context (context,
3255 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3256 IDENTIFIER_POINTER (name),
3257 type_name, IDENTIFIER_POINTER (name), line);
3260 /* If ANAME is terminated with `[]', it indicates an array. This
3261 function returns the number of `[]' found and if this number is
3262 greater than zero, it extracts the array type name and places it in
3263 the node pointed to by TRIMMED unless TRIMMED is null. */
3265 static int
3266 build_type_name_from_array_name (aname, trimmed)
3267 tree aname;
3268 tree *trimmed;
3270 const char *name = IDENTIFIER_POINTER (aname);
3271 int len = IDENTIFIER_LENGTH (aname);
3272 int array_dims;
3274 STRING_STRIP_BRACKETS (name, len, array_dims);
3276 if (array_dims && trimmed)
3277 *trimmed = get_identifier_with_length (name, len);
3279 return array_dims;
3282 static tree
3283 build_array_from_name (type, type_wfl, name, ret_name)
3284 tree type, type_wfl, name, *ret_name;
3286 int more_dims = 0;
3288 /* Eventually get more dims */
3289 more_dims = build_type_name_from_array_name (name, &name);
3291 /* If we have, then craft a new type for this variable */
3292 if (more_dims)
3294 tree save = type;
3296 /* If we have a pointer, use its type */
3297 if (TREE_CODE (type) == POINTER_TYPE)
3298 type = TREE_TYPE (type);
3300 /* Building the first dimension of a primitive type uses this
3301 function */
3302 if (JPRIMITIVE_TYPE_P (type))
3304 type = build_java_array_type (type, -1);
3305 more_dims--;
3307 /* Otherwise, if we have a WFL for this type, use it (the type
3308 is already an array on an unresolved type, and we just keep
3309 on adding dimensions) */
3310 else if (type_wfl)
3312 type = type_wfl;
3313 more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3314 NULL);
3317 /* Add all the dimensions */
3318 while (more_dims--)
3319 type = build_unresolved_array_type (type);
3321 /* The type may have been incomplete in the first place */
3322 if (type_wfl)
3323 type = obtain_incomplete_type (type);
3326 if (ret_name)
3327 *ret_name = name;
3328 return type;
3331 /* Build something that the type identifier resolver will identify as
3332 being an array to an unresolved type. TYPE_WFL is a WFL on a
3333 identifier. */
3335 static tree
3336 build_unresolved_array_type (type_or_wfl)
3337 tree type_or_wfl;
3339 const char *ptr;
3340 tree wfl;
3342 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3343 just create a array type */
3344 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3345 return build_java_array_type (type_or_wfl, -1);
3347 obstack_grow (&temporary_obstack,
3348 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3349 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3350 obstack_grow0 (&temporary_obstack, "[]", 2);
3351 ptr = obstack_finish (&temporary_obstack);
3352 wfl = build_expr_wfl (get_identifier (ptr),
3353 EXPR_WFL_FILENAME (type_or_wfl),
3354 EXPR_WFL_LINENO (type_or_wfl),
3355 EXPR_WFL_COLNO (type_or_wfl));
3356 /* Re-install the existing qualifications so that the type can be
3357 resolved properly. */
3358 EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3359 return wfl;
3362 static void
3363 parser_add_interface (class_decl, interface_decl, wfl)
3364 tree class_decl, interface_decl, wfl;
3366 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3367 parse_error_context (wfl, "Interface `%s' repeated",
3368 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3371 /* Bulk of common class/interface checks. Return 1 if an error was
3372 encountered. TAG is 0 for a class, 1 for an interface. */
3374 static int
3375 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3376 int is_interface, flags;
3377 tree raw_name, qualified_name, decl, cl;
3379 tree node;
3380 int sca = 0; /* Static class allowed */
3381 int icaf = 0; /* Inner class allowed flags */
3382 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3384 if (!quiet_flag)
3385 fprintf (stderr, " %s%s %s",
3386 (CPC_INNER_P () ? "inner" : ""),
3387 (is_interface ? "interface" : "class"),
3388 IDENTIFIER_POINTER (qualified_name));
3390 /* Scope of an interface/class type name:
3391 - Can't be imported by a single type import
3392 - Can't already exists in the package */
3393 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3394 && (node = find_name_in_single_imports (raw_name))
3395 && !CPC_INNER_P ())
3397 parse_error_context
3398 (cl, "%s name `%s' clashes with imported type `%s'",
3399 (is_interface ? "Interface" : "Class"),
3400 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3401 return 1;
3403 if (decl && CLASS_COMPLETE_P (decl))
3405 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3406 qualified_name, decl, cl);
3407 return 1;
3410 if (check_inner_class_redefinition (raw_name, cl))
3411 return 1;
3413 /* If public, file name should match class/interface name, except
3414 when dealing with an inner class */
3415 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3417 const char *f;
3419 /* Contains OS dependent assumption on path separator. FIXME */
3420 for (f = &input_filename [strlen (input_filename)];
3421 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3422 f--)
3424 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3425 f++;
3426 if (strncmp (IDENTIFIER_POINTER (raw_name),
3427 f , IDENTIFIER_LENGTH (raw_name)) ||
3428 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3429 parse_error_context
3430 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3431 (is_interface ? "interface" : "class"),
3432 IDENTIFIER_POINTER (qualified_name),
3433 IDENTIFIER_POINTER (raw_name));
3436 /* Static classes can be declared only in top level classes. Note:
3437 once static, a inner class is a top level class. */
3438 if (flags & ACC_STATIC)
3440 /* Catch the specific error of declaring an class inner class
3441 with no toplevel enclosing class. Prevent check_modifiers from
3442 complaining a second time */
3443 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3445 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3446 IDENTIFIER_POINTER (qualified_name));
3447 sca = ACC_STATIC;
3449 /* Else, in the context of a top-level class declaration, let
3450 `check_modifiers' do its job, otherwise, give it a go */
3451 else
3452 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3455 /* Inner classes can be declared private or protected
3456 within their enclosing classes. */
3457 if (CPC_INNER_P ())
3459 /* A class which is local to a block can't be public, private,
3460 protected or static. But it is created final, so allow this
3461 one. */
3462 if (current_function_decl)
3463 icaf = sca = uaaf = ACC_FINAL;
3464 else
3466 check_modifiers_consistency (flags);
3467 icaf = ACC_PROTECTED;
3468 if (! CLASS_INTERFACE (GET_CPC ()))
3469 icaf |= ACC_PRIVATE;
3473 if (is_interface)
3475 if (CPC_INNER_P ())
3476 uaaf = INTERFACE_INNER_MODIFIERS;
3477 else
3478 uaaf = INTERFACE_MODIFIERS;
3480 check_modifiers ("Illegal modifier `%s' for interface declaration",
3481 flags, uaaf);
3483 else
3484 check_modifiers ((current_function_decl ?
3485 "Illegal modifier `%s' for local class declaration" :
3486 "Illegal modifier `%s' for class declaration"),
3487 flags, uaaf|sca|icaf);
3488 return 0;
3491 static void
3492 make_nested_class_name (cpc_list)
3493 tree cpc_list;
3495 tree name;
3497 if (!cpc_list)
3498 return;
3499 else
3500 make_nested_class_name (TREE_CHAIN (cpc_list));
3502 /* Pick the qualified name when dealing with the first upmost
3503 enclosing class */
3504 name = (TREE_CHAIN (cpc_list) ?
3505 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3506 obstack_grow (&temporary_obstack,
3507 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3508 /* Why is NO_DOLLAR_IN_LABEL defined? */
3509 #if 0
3510 #ifdef NO_DOLLAR_IN_LABEL
3511 internal_error ("Can't use '$' as a separator for inner classes");
3512 #endif
3513 #endif
3514 obstack_1grow (&temporary_obstack, '$');
3517 /* Can't redefine a class already defined in an earlier scope. */
3519 static int
3520 check_inner_class_redefinition (raw_name, cl)
3521 tree raw_name, cl;
3523 tree scope_list;
3525 for (scope_list = GET_CPC_LIST (); scope_list;
3526 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3527 if (raw_name == GET_CPC_UN_NODE (scope_list))
3529 parse_error_context
3530 (cl, "The class name `%s' is already defined in this scope. An inner class may not have the same simple name as any of its enclosing classes",
3531 IDENTIFIER_POINTER (raw_name));
3532 return 1;
3534 return 0;
3537 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3538 we remember ENCLOSING and SUPER. */
3540 static tree
3541 resolve_inner_class (circularity_hash, cl, enclosing, super, class_type)
3542 struct hash_table *circularity_hash;
3543 tree cl, *enclosing, *super, class_type;
3545 tree local_enclosing = *enclosing;
3546 tree local_super = NULL_TREE;
3548 while (local_enclosing)
3550 tree intermediate, decl;
3552 hash_lookup (circularity_hash,
3553 (const hash_table_key) local_enclosing, TRUE, NULL);
3555 if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3556 return decl;
3558 intermediate = local_enclosing;
3559 /* Explore enclosing contexts. */
3560 while (INNER_CLASS_DECL_P (intermediate))
3562 intermediate = DECL_CONTEXT (intermediate);
3563 if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3564 return decl;
3567 /* Now go to the upper classes, bail out if necessary. We will
3568 analyze the returned SUPER and act accordingly (see
3569 do_resolve_class.) */
3570 local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3571 if (!local_super || local_super == object_type_node)
3572 break;
3574 if (TREE_CODE (local_super) == POINTER_TYPE)
3575 local_super = do_resolve_class (NULL, local_super, NULL, NULL);
3576 else
3577 local_super = TYPE_NAME (local_super);
3579 /* We may not have checked for circular inheritance yet, so do so
3580 here to prevent an infinite loop. */
3581 if (hash_lookup (circularity_hash,
3582 (const hash_table_key) local_super, FALSE, NULL))
3584 if (!cl)
3585 cl = lookup_cl (local_enclosing);
3587 parse_error_context
3588 (cl, "Cyclic inheritance involving %s",
3589 IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3590 local_enclosing = NULL_TREE;
3592 else
3593 local_enclosing = local_super;
3596 /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3597 *super = local_super;
3598 *enclosing = local_enclosing;
3600 return NULL_TREE;
3603 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3604 qualified. */
3606 static tree
3607 find_as_inner_class (enclosing, name, cl)
3608 tree enclosing, name, cl;
3610 tree qual, to_return;
3611 if (!enclosing)
3612 return NULL_TREE;
3614 name = TYPE_NAME (name);
3616 /* First search: within the scope of `enclosing', search for name */
3617 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3618 qual = EXPR_WFL_QUALIFICATION (cl);
3619 else if (cl)
3620 qual = build_tree_list (cl, NULL_TREE);
3621 else
3622 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3624 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3625 return to_return;
3627 /* We're dealing with a qualified name. Try to resolve thing until
3628 we get something that is an enclosing class. */
3629 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3631 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3633 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3634 qual = TREE_CHAIN (qual))
3636 acc = merge_qualified_name (acc,
3637 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3638 BUILD_PTR_FROM_NAME (ptr, acc);
3640 /* Don't try to resolve ACC as a class name if it follows
3641 the current package name. We don't want to pick something
3642 that's accidentally there: for example `a.b.c' in package
3643 `a.b' shouldn't trigger loading `a' if it's there by
3644 itself. */
3645 if (ctxp->package
3646 && strstr (IDENTIFIER_POINTER (ctxp->package),
3647 IDENTIFIER_POINTER (acc)))
3648 decl = NULL;
3649 else
3650 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3653 /* A NULL qual and a decl means that the search ended
3654 successfully?!? We have to do something then. FIXME */
3656 if (decl)
3657 enclosing = decl;
3658 else
3659 qual = EXPR_WFL_QUALIFICATION (cl);
3661 /* Otherwise, create a qual for the other part of the resolution. */
3662 else
3663 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3665 return find_as_inner_class_do (qual, enclosing);
3668 /* We go inside the list of sub classes and try to find a way
3669 through. */
3671 static tree
3672 find_as_inner_class_do (qual, enclosing)
3673 tree qual, enclosing;
3675 if (!qual)
3676 return NULL_TREE;
3678 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3680 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3681 tree next_enclosing = NULL_TREE;
3682 tree inner_list;
3684 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3685 inner_list; inner_list = TREE_CHAIN (inner_list))
3687 if (TREE_VALUE (inner_list) == name_to_match)
3689 next_enclosing = TREE_PURPOSE (inner_list);
3690 break;
3693 enclosing = next_enclosing;
3696 return (!qual && enclosing ? enclosing : NULL_TREE);
3699 /* Reach all inner classes and tie their unqualified name to a
3700 DECL. */
3702 static void
3703 set_nested_class_simple_name_value (outer, set)
3704 tree outer;
3705 int set;
3707 tree l;
3709 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3710 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3711 TREE_PURPOSE (l) : NULL_TREE);
3714 static void
3715 link_nested_class_to_enclosing ()
3717 if (GET_ENCLOSING_CPC ())
3719 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3720 DECL_INNER_CLASS_LIST (enclosing) =
3721 tree_cons (GET_CPC (), GET_CPC_UN (),
3722 DECL_INNER_CLASS_LIST (enclosing));
3723 enclosing = enclosing;
3727 static tree
3728 maybe_make_nested_class_name (name)
3729 tree name;
3731 tree id = NULL_TREE;
3733 if (CPC_INNER_P ())
3735 make_nested_class_name (GET_CPC_LIST ());
3736 obstack_grow0 (&temporary_obstack,
3737 IDENTIFIER_POINTER (name),
3738 IDENTIFIER_LENGTH (name));
3739 id = get_identifier (obstack_finish (&temporary_obstack));
3740 if (ctxp->package)
3741 QUALIFIED_P (id) = 1;
3743 return id;
3746 /* If DECL is NULL, create and push a new DECL, record the current
3747 line CL and do other maintenance things. */
3749 static tree
3750 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3751 tree decl, raw_name, qualified_name, cl;
3753 if (!decl)
3754 decl = push_class (make_class (), qualified_name);
3756 /* Take care of the file and line business */
3757 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3758 /* If we're emiting xrefs, store the line/col number information */
3759 if (flag_emit_xref)
3760 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3761 else
3762 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3763 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3764 CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3765 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3766 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3768 PUSH_CPC (decl, raw_name);
3769 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3771 /* Link the declaration to the already seen ones */
3772 TREE_CHAIN (decl) = ctxp->class_list;
3773 ctxp->class_list = decl;
3775 /* Create a new nodes in the global lists */
3776 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3777 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3779 /* Install a new dependency list element */
3780 create_jdep_list (ctxp);
3782 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3783 IDENTIFIER_POINTER (qualified_name)));
3784 return decl;
3787 static void
3788 add_superinterfaces (decl, interface_list)
3789 tree decl, interface_list;
3791 tree node;
3792 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3793 takes care of ensuring that:
3794 - This is an accessible interface type,
3795 - Circularity detection.
3796 parser_add_interface is then called. If present but not defined,
3797 the check operation is delayed until the super interface gets
3798 defined. */
3799 for (node = interface_list; node; node = TREE_CHAIN (node))
3801 tree current = TREE_PURPOSE (node);
3802 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3803 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3805 if (!parser_check_super_interface (idecl, decl, current))
3806 parser_add_interface (decl, idecl, current);
3808 else
3809 register_incomplete_type (JDEP_INTERFACE,
3810 current, decl, NULL_TREE);
3814 /* Create an interface in pass1 and return its decl. Return the
3815 interface's decl in pass 2. */
3817 static tree
3818 create_interface (flags, id, super)
3819 int flags;
3820 tree id, super;
3822 tree raw_name = EXPR_WFL_NODE (id);
3823 tree q_name = parser_qualified_classname (raw_name);
3824 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3826 /* Certain syntax errors are making SUPER be like ID. Avoid this
3827 case. */
3828 if (ctxp->class_err && id == super)
3829 super = NULL;
3831 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3833 /* Basic checks: scope, redefinition, modifiers */
3834 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3836 PUSH_ERROR ();
3837 return NULL_TREE;
3840 /* Suspend the current parsing context if we're parsing an inner
3841 interface */
3842 if (CPC_INNER_P ())
3844 java_parser_context_suspend ();
3845 /* Interface members are public. */
3846 if (CLASS_INTERFACE (GET_CPC ()))
3847 flags |= ACC_PUBLIC;
3850 /* Push a new context for (static) initialized upon declaration fields */
3851 java_parser_context_push_initialized_field ();
3853 /* Interface modifiers check
3854 - public/abstract allowed (already done at that point)
3855 - abstract is obsolete (comes first, it's a warning, or should be)
3856 - Can't use twice the same (checked in the modifier rule) */
3857 if ((flags & ACC_ABSTRACT) && flag_redundant)
3858 parse_warning_context
3859 (MODIFIER_WFL (ABSTRACT_TK),
3860 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3862 /* Create a new decl if DECL is NULL, otherwise fix it */
3863 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3865 /* Set super info and mark the class a complete */
3866 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3867 object_type_node, ctxp->interface_number);
3868 ctxp->interface_number = 0;
3869 CLASS_COMPLETE_P (decl) = 1;
3870 add_superinterfaces (decl, super);
3872 return decl;
3875 /* Anonymous class counter. Will be reset to 1 every time a non
3876 anonymous class gets created. */
3877 static int anonymous_class_counter = 1;
3879 /* Patch anonymous class CLASS, by either extending or implementing
3880 DEP. */
3882 static void
3883 patch_anonymous_class (type_decl, class_decl, wfl)
3884 tree type_decl, class_decl, wfl;
3886 tree class = TREE_TYPE (class_decl);
3887 tree type = TREE_TYPE (type_decl);
3888 tree binfo = TYPE_BINFO (class);
3890 /* If it's an interface, implement it */
3891 if (CLASS_INTERFACE (type_decl))
3893 tree s_binfo;
3894 int length;
3896 if (parser_check_super_interface (type_decl, class_decl, wfl))
3897 return;
3899 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3900 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3901 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3902 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3903 /* And add the interface */
3904 parser_add_interface (class_decl, type_decl, wfl);
3906 /* Otherwise, it's a type we want to extend */
3907 else
3909 if (parser_check_super (type_decl, class_decl, wfl))
3910 return;
3911 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3915 static tree
3916 create_anonymous_class (location, type_name)
3917 int location;
3918 tree type_name;
3920 char buffer [80];
3921 tree super = NULL_TREE, itf = NULL_TREE;
3922 tree id, type_decl, class;
3924 /* The unqualified name of the anonymous class. It's just a number. */
3925 sprintf (buffer, "%d", anonymous_class_counter++);
3926 id = build_wfl_node (get_identifier (buffer));
3927 EXPR_WFL_LINECOL (id) = location;
3929 /* We know about the type to extend/implement. We go ahead */
3930 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3932 /* Create a class which either implements on extends the designated
3933 class. The class bears an innacessible name. */
3934 if (CLASS_INTERFACE (type_decl))
3936 /* It's OK to modify it here. It's been already used and
3937 shouldn't be reused */
3938 ctxp->interface_number = 1;
3939 /* Interfaces should presented as a list of WFLs */
3940 itf = build_tree_list (type_name, NULL_TREE);
3942 else
3943 super = type_name;
3946 class = create_class (ACC_FINAL, id, super, itf);
3948 /* We didn't know anything about the stuff. We register a dependence. */
3949 if (!type_decl)
3950 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3952 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3953 return class;
3956 /* Create a class in pass1 and return its decl. Return class
3957 interface's decl in pass 2. */
3959 static tree
3960 create_class (flags, id, super, interfaces)
3961 int flags;
3962 tree id, super, interfaces;
3964 tree raw_name = EXPR_WFL_NODE (id);
3965 tree class_id, decl;
3966 tree super_decl_type;
3968 /* Certain syntax errors are making SUPER be like ID. Avoid this
3969 case. */
3970 if (ctxp->class_err && id == super)
3971 super = NULL;
3973 class_id = parser_qualified_classname (raw_name);
3974 decl = IDENTIFIER_CLASS_VALUE (class_id);
3975 EXPR_WFL_NODE (id) = class_id;
3977 /* Basic check: scope, redefinition, modifiers */
3978 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3980 PUSH_ERROR ();
3981 return NULL_TREE;
3984 /* Suspend the current parsing context if we're parsing an inner
3985 class or an anonymous class. */
3986 if (CPC_INNER_P ())
3988 java_parser_context_suspend ();
3989 /* Interface members are public. */
3990 if (CLASS_INTERFACE (GET_CPC ()))
3991 flags |= ACC_PUBLIC;
3994 /* Push a new context for (static) initialized upon declaration fields */
3995 java_parser_context_push_initialized_field ();
3997 /* Class modifier check:
3998 - Allowed modifier (already done at that point)
3999 - abstract AND final forbidden
4000 - Public classes defined in the correct file */
4001 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4002 parse_error_context
4003 (id, "Class `%s' can't be declared both abstract and final",
4004 IDENTIFIER_POINTER (raw_name));
4006 /* Create a new decl if DECL is NULL, otherwise fix it */
4007 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4009 /* If SUPER exists, use it, otherwise use Object */
4010 if (super)
4012 /* Can't extend java.lang.Object */
4013 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4015 parse_error_context (id, "Can't extend `java.lang.Object'");
4016 return NULL_TREE;
4019 super_decl_type =
4020 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4022 else if (TREE_TYPE (decl) != object_type_node)
4023 super_decl_type = object_type_node;
4024 /* We're defining java.lang.Object */
4025 else
4026 super_decl_type = NULL_TREE;
4028 /* A class nested in an interface is implicitly static. */
4029 if (INNER_CLASS_DECL_P (decl)
4030 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4032 flags |= ACC_STATIC;
4035 /* Set super info and mark the class as complete. */
4036 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4037 ctxp->interface_number);
4038 ctxp->interface_number = 0;
4039 CLASS_COMPLETE_P (decl) = 1;
4040 add_superinterfaces (decl, interfaces);
4042 /* Add the private this$<n> field, Replicate final locals still in
4043 scope as private final fields mangled like val$<local_name>.
4044 This doesn't not occur for top level (static) inner classes. */
4045 if (PURE_INNER_CLASS_DECL_P (decl))
4046 add_inner_class_fields (decl, current_function_decl);
4048 /* If doing xref, store the location at which the inherited class
4049 (if any) was seen. */
4050 if (flag_emit_xref && super)
4051 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
4053 /* Eventually sets the @deprecated tag flag */
4054 CHECK_DEPRECATED (decl);
4056 /* Reset the anonymous class counter when declaring non inner classes */
4057 if (!INNER_CLASS_DECL_P (decl))
4058 anonymous_class_counter = 1;
4060 return decl;
4063 /* End a class declaration: register the statements used to create
4064 finit$ and <clinit>, pop the current class and resume the prior
4065 parser context if necessary. */
4067 static void
4068 end_class_declaration (resume)
4069 int resume;
4071 /* If an error occured, context weren't pushed and won't need to be
4072 popped by a resume. */
4073 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
4075 java_parser_context_pop_initialized_field ();
4076 POP_CPC ();
4077 if (resume && no_error_occured)
4078 java_parser_context_resume ();
4080 /* We're ending a class declaration, this is a good time to reset
4081 the interface cout. Note that might have been already done in
4082 create_interface, but if at that time an inner class was being
4083 dealt with, the interface count was reset in a context created
4084 for the sake of handling inner classes declaration. */
4085 ctxp->interface_number = 0;
4088 static void
4089 add_inner_class_fields (class_decl, fct_decl)
4090 tree class_decl;
4091 tree fct_decl;
4093 tree block, marker, f;
4095 f = add_field (TREE_TYPE (class_decl),
4096 build_current_thisn (TREE_TYPE (class_decl)),
4097 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4098 ACC_PRIVATE);
4099 FIELD_THISN (f) = 1;
4101 if (!fct_decl)
4102 return;
4104 for (block = GET_CURRENT_BLOCK (fct_decl);
4105 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4107 tree decl;
4108 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4110 tree name, pname;
4111 tree wfl, init, list;
4113 /* Avoid non final arguments. */
4114 if (!LOCAL_FINAL_P (decl))
4115 continue;
4117 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4118 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4119 wfl = build_wfl_node (name);
4120 init = build_wfl_node (pname);
4121 /* Build an initialization for the field: it will be
4122 initialized by a parameter added to finit$, bearing a
4123 mangled name of the field itself (param$<n>.) The
4124 parameter is provided to finit$ by the constructor
4125 invoking it (hence the constructor will also feature a
4126 hidden parameter, set to the value of the outer context
4127 local at the time the inner class is created.)
4129 Note: we take into account all possible locals that can
4130 be accessed by the inner class. It's actually not trivial
4131 to minimize these aliases down to the ones really
4132 used. One way to do that would be to expand all regular
4133 methods first, then finit$ to get a picture of what's
4134 used. It works with the exception that we would have to
4135 go back on all constructor invoked in regular methods to
4136 have their invokation reworked (to include the right amount
4137 of alias initializer parameters.)
4139 The only real way around, I think, is a first pass to
4140 identify locals really used in the inner class. We leave
4141 the flag FIELD_LOCAL_ALIAS_USED around for that future
4142 use.
4144 On the other hand, it only affect local inner classes,
4145 whose constructors (and finit$ call) will be featuring
4146 unecessary arguments. It's easy for a developper to keep
4147 this number of parameter down by using the `final'
4148 keyword only when necessary. For the time being, we can
4149 issue a warning on unecessary finals. FIXME */
4150 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4151 wfl, init);
4153 /* Register the field. The TREE_LIST holding the part
4154 initialized/initializer will be marked ARG_FINAL_P so
4155 that the created field can be marked
4156 FIELD_LOCAL_ALIAS. */
4157 list = build_tree_list (wfl, init);
4158 ARG_FINAL_P (list) = 1;
4159 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4163 if (!CPC_INITIALIZER_STMT (ctxp))
4164 return;
4166 /* If we ever registered an alias field, insert and marker to
4167 remeber where the list ends. The second part of the list (the one
4168 featuring initialized fields) so it can be later reversed to
4169 enforce 8.5. The marker will be removed during that operation. */
4170 marker = build_tree_list (NULL_TREE, NULL_TREE);
4171 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4172 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4175 /* Can't use lookup_field () since we don't want to load the class and
4176 can't set the CLASS_LOADED_P flag */
4178 static tree
4179 find_field (class, name)
4180 tree class;
4181 tree name;
4183 tree decl;
4184 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4186 if (DECL_NAME (decl) == name)
4187 return decl;
4189 return NULL_TREE;
4192 /* Wrap around lookup_field that doesn't potentially upset the value
4193 of CLASS */
4195 static tree
4196 lookup_field_wrapper (class, name)
4197 tree class, name;
4199 tree type = class;
4200 tree decl = NULL_TREE;
4201 java_parser_context_save_global ();
4203 /* Last chance: if we're within the context of an inner class, we
4204 might be trying to access a local variable defined in an outer
4205 context. We try to look for it now. */
4206 if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4208 tree new_name;
4209 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4210 decl = lookup_field (&type, new_name);
4211 if (decl && decl != error_mark_node)
4212 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4214 if (!decl || decl == error_mark_node)
4216 type = class;
4217 decl = lookup_field (&type, name);
4220 java_parser_context_restore_global ();
4221 return decl == error_mark_node ? NULL : decl;
4224 /* Find duplicate field within the same class declarations and report
4225 the error. Returns 1 if a duplicated field was found, 0
4226 otherwise. */
4228 static int
4229 duplicate_declaration_error_p (new_field_name, new_type, cl)
4230 tree new_field_name, new_type, cl;
4232 /* This might be modified to work with method decl as well */
4233 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4234 if (decl)
4236 char *t1 = xstrdup (purify_type_name
4237 ((TREE_CODE (new_type) == POINTER_TYPE
4238 && TREE_TYPE (new_type) == NULL_TREE) ?
4239 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4240 lang_printable_name (new_type, 1)));
4241 /* The type may not have been completed by the time we report
4242 the error */
4243 char *t2 = xstrdup (purify_type_name
4244 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4245 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4246 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4247 lang_printable_name (TREE_TYPE (decl), 1)));
4248 parse_error_context
4249 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4250 t1, IDENTIFIER_POINTER (new_field_name),
4251 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4252 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4253 free (t1);
4254 free (t2);
4255 return 1;
4257 return 0;
4260 /* Field registration routine. If TYPE doesn't exist, field
4261 declarations are linked to the undefined TYPE dependency list, to
4262 be later resolved in java_complete_class () */
4264 static void
4265 register_fields (flags, type, variable_list)
4266 int flags;
4267 tree type, variable_list;
4269 tree current, saved_type;
4270 tree class_type = NULL_TREE;
4271 int saved_lineno = lineno;
4272 int must_chain = 0;
4273 tree wfl = NULL_TREE;
4275 if (GET_CPC ())
4276 class_type = TREE_TYPE (GET_CPC ());
4278 if (!class_type || class_type == error_mark_node)
4279 return;
4281 /* If we're adding fields to interfaces, those fields are public,
4282 static, final */
4283 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4285 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4286 flags, ACC_PUBLIC, "interface field(s)");
4287 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4288 flags, ACC_STATIC, "interface field(s)");
4289 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4290 flags, ACC_FINAL, "interface field(s)");
4291 check_modifiers ("Illegal interface member modifier `%s'", flags,
4292 INTERFACE_FIELD_MODIFIERS);
4293 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4296 /* Obtain a suitable type for resolution, if necessary */
4297 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4299 /* If TYPE is fully resolved and we don't have a reference, make one */
4300 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4302 for (current = variable_list, saved_type = type; current;
4303 current = TREE_CHAIN (current), type = saved_type)
4305 tree real_type;
4306 tree field_decl;
4307 tree cl = TREE_PURPOSE (current);
4308 tree init = TREE_VALUE (current);
4309 tree current_name = EXPR_WFL_NODE (cl);
4311 /* Can't declare non-final static fields in inner classes */
4312 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4313 && !(flags & ACC_FINAL))
4314 parse_error_context
4315 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4316 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4317 lang_printable_name (class_type, 0));
4319 /* Process NAME, as it may specify extra dimension(s) for it */
4320 type = build_array_from_name (type, wfl, current_name, &current_name);
4322 /* Type adjustment. We may have just readjusted TYPE because
4323 the variable specified more dimensions. Make sure we have
4324 a reference if we can and don't have one already. Also
4325 change the name if we have an init. */
4326 if (type != saved_type)
4328 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4329 if (init)
4330 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4333 real_type = GET_REAL_TYPE (type);
4334 /* Check for redeclarations */
4335 if (duplicate_declaration_error_p (current_name, real_type, cl))
4336 continue;
4338 /* Set lineno to the line the field was found and create a
4339 declaration for it. Eventually sets the @deprecated tag flag. */
4340 if (flag_emit_xref)
4341 lineno = EXPR_WFL_LINECOL (cl);
4342 else
4343 lineno = EXPR_WFL_LINENO (cl);
4344 field_decl = add_field (class_type, current_name, real_type, flags);
4345 CHECK_DEPRECATED (field_decl);
4347 /* If the field denotes a final instance variable, then we
4348 allocate a LANG_DECL_SPECIFIC part to keep track of its
4349 initialization. We also mark whether the field was
4350 initialized upon it's declaration. We don't do that if the
4351 created field is an alias to a final local. */
4352 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4354 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4355 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4356 if ((flags & ACC_STATIC) && init)
4357 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4360 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4361 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4362 hide parameters to this inner class finit$ and
4363 constructors. It also means that the field isn't final per
4364 say. */
4365 if (ARG_FINAL_P (current))
4367 FIELD_LOCAL_ALIAS (field_decl) = 1;
4368 FIELD_FINAL (field_decl) = 0;
4371 /* Check if we must chain. */
4372 if (must_chain)
4373 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4375 /* If we have an initialization value tied to the field */
4376 if (init)
4378 /* The field is declared static */
4379 if (flags & ACC_STATIC)
4381 /* We include the field and its initialization part into
4382 a list used to generate <clinit>. After <clinit> is
4383 walked, field initializations will be processed and
4384 fields initialized with known constants will be taken
4385 out of <clinit> and have their DECL_INITIAL set
4386 appropriately. */
4387 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4388 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4389 if (TREE_OPERAND (init, 1)
4390 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4391 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4393 /* A non-static field declared with an immediate initialization is
4394 to be initialized in <init>, if any. This field is remembered
4395 to be processed at the time of the generation of <init>. */
4396 else
4398 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4399 SET_CPC_INITIALIZER_STMT (ctxp, init);
4401 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4402 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4405 lineno = saved_lineno;
4408 /* Generate finit$, using the list of initialized fields to populate
4409 its body. finit$'s parameter(s) list is adjusted to include the
4410 one(s) used to initialized the field(s) caching outer context
4411 local(s). */
4413 static tree
4414 generate_finit (class_type)
4415 tree class_type;
4417 int count = 0;
4418 tree list = TYPE_FINIT_STMT_LIST (class_type);
4419 tree mdecl, current, parms;
4421 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4422 class_type, NULL_TREE,
4423 &count);
4424 CRAFTED_PARAM_LIST_FIXUP (parms);
4425 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4426 finit_identifier_node, parms);
4427 fix_method_argument_names (parms, mdecl);
4428 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4429 mdecl, NULL_TREE);
4430 DECL_FUNCTION_NAP (mdecl) = count;
4431 start_artificial_method_body (mdecl);
4433 for (current = list; current; current = TREE_CHAIN (current))
4434 java_method_add_stmt (mdecl,
4435 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4436 current));
4437 end_artificial_method_body (mdecl);
4438 return mdecl;
4441 /* Generate a function to run the instance initialization code. The
4442 private method is called `instinit$'. Unless we're dealing with an
4443 anonymous class, we determine whether all ctors of CLASS_TYPE
4444 declare a checked exception in their `throws' clause in order to
4445 see whether it's necessary to encapsulate the instance initializer
4446 statements in a try/catch/rethrow sequence. */
4448 static tree
4449 generate_instinit (class_type)
4450 tree class_type;
4452 tree current;
4453 tree compound = NULL_TREE;
4454 tree parms = tree_cons (this_identifier_node,
4455 build_pointer_type (class_type), end_params_node);
4456 tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4457 void_type_node,
4458 instinit_identifier_node, parms);
4460 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4461 mdecl, NULL_TREE);
4463 /* Gather all the statements in a compound */
4464 for (current = TYPE_II_STMT_LIST (class_type);
4465 current; current = TREE_CHAIN (current))
4466 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4468 /* We need to encapsulate COMPOUND by a try/catch statement to
4469 rethrow exceptions that might occur in the instance initializer.
4470 We do that only if all ctors of CLASS_TYPE are set to catch a
4471 checked exception. This doesn't apply to anonymous classes (since
4472 they don't have declared ctors.) */
4473 if (!ANONYMOUS_CLASS_P (class_type) &&
4474 ctors_unchecked_throws_clause_p (class_type))
4476 compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4477 build1 (THROW_EXPR, NULL_TREE,
4478 build_wfl_node (wpv_id)));
4479 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4480 exception_type_node);
4483 start_artificial_method_body (mdecl);
4484 java_method_add_stmt (mdecl, compound);
4485 end_artificial_method_body (mdecl);
4487 return mdecl;
4490 /* FIXME */
4491 static tree
4492 build_instinit_invocation (class_type)
4493 tree class_type;
4495 tree to_return = NULL_TREE;
4497 if (TYPE_II_STMT_LIST (class_type))
4499 tree parm = build_tree_list (NULL_TREE,
4500 build_wfl_node (this_identifier_node));
4501 to_return =
4502 build_method_invocation (build_wfl_node (instinit_identifier_node),
4503 parm);
4505 return to_return;
4508 /* Shared accros method_declarator and method_header to remember the
4509 patch stage that was reached during the declaration of the method.
4510 A method DECL is built differently is there is no patch
4511 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4512 pending on the currently defined method. */
4514 static int patch_stage;
4516 /* Check the method declaration and add the method to its current
4517 class. If the argument list is known to contain incomplete types,
4518 the method is partially added and the registration will be resume
4519 once the method arguments resolved. If TYPE is NULL, we're dealing
4520 with a constructor. */
4522 static tree
4523 method_header (flags, type, mdecl, throws)
4524 int flags;
4525 tree type, mdecl, throws;
4527 tree type_wfl = NULL_TREE;
4528 tree meth_name = NULL_TREE;
4529 tree current, orig_arg, this_class = NULL;
4530 tree id, meth;
4531 int saved_lineno;
4532 int constructor_ok = 0, must_chain;
4533 int count;
4535 if (mdecl == error_mark_node)
4536 return error_mark_node;
4537 meth = TREE_VALUE (mdecl);
4538 id = TREE_PURPOSE (mdecl);
4540 check_modifiers_consistency (flags);
4542 if (GET_CPC ())
4543 this_class = TREE_TYPE (GET_CPC ());
4545 if (!this_class || this_class == error_mark_node)
4546 return NULL_TREE;
4548 /* There are some forbidden modifiers for an abstract method and its
4549 class must be abstract as well. */
4550 if (type && (flags & ACC_ABSTRACT))
4552 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4553 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4554 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4555 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4556 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4557 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4558 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4559 parse_error_context
4560 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4561 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4562 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4565 /* Things to be checked when declaring a constructor */
4566 if (!type)
4568 int ec = java_error_count;
4569 /* 8.6: Constructor declarations: we might be trying to define a
4570 method without specifying a return type. */
4571 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4572 parse_error_context
4573 (id, "Invalid method declaration, return type required");
4574 /* 8.6.3: Constructor modifiers */
4575 else
4577 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4578 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4579 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4580 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4581 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4583 /* If we found error here, we don't consider it's OK to tread
4584 the method definition as a constructor, for the rest of this
4585 function */
4586 if (ec == java_error_count)
4587 constructor_ok = 1;
4590 /* Method declared within the scope of an interface are implicitly
4591 abstract and public. Conflicts with other erroneously provided
4592 modifiers are checked right after. */
4594 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4596 /* If FLAGS isn't set because of a modifier, turn the
4597 corresponding modifier WFL to NULL so we issue a warning on
4598 the obsolete use of the modifier */
4599 if (!(flags & ACC_PUBLIC))
4600 MODIFIER_WFL (PUBLIC_TK) = NULL;
4601 if (!(flags & ACC_ABSTRACT))
4602 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4603 flags |= ACC_PUBLIC;
4604 flags |= ACC_ABSTRACT;
4607 /* Inner class can't declare static methods */
4608 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4610 parse_error_context
4611 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4612 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4613 lang_printable_name (this_class, 0));
4616 /* Modifiers context reset moved up, so abstract method declaration
4617 modifiers can be later checked. */
4619 /* Set constructor returned type to void and method name to <init>,
4620 unless we found an error identifier the constructor (in which
4621 case we retain the original name) */
4622 if (!type)
4624 type = void_type_node;
4625 if (constructor_ok)
4626 meth_name = init_identifier_node;
4628 else
4629 meth_name = EXPR_WFL_NODE (id);
4631 /* Do the returned type resolution and registration if necessary */
4632 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4634 if (meth_name)
4635 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4636 EXPR_WFL_NODE (id) = meth_name;
4637 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4639 if (must_chain)
4641 patch_stage = JDEP_METHOD_RETURN;
4642 register_incomplete_type (patch_stage, type_wfl, id, type);
4643 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4645 else
4646 TREE_TYPE (meth) = type;
4648 saved_lineno = lineno;
4649 /* When defining an abstract or interface method, the curly
4650 bracket at level 1 doesn't exist because there is no function
4651 body */
4652 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4653 EXPR_WFL_LINENO (id));
4655 /* Remember the original argument list */
4656 orig_arg = TYPE_ARG_TYPES (meth);
4658 if (patch_stage) /* includes ret type and/or all args */
4660 jdep *jdep;
4661 meth = add_method_1 (this_class, flags, meth_name, meth);
4662 /* Patch for the return type */
4663 if (patch_stage == JDEP_METHOD_RETURN)
4665 jdep = CLASSD_LAST (ctxp->classd_list);
4666 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4668 /* This is the stop JDEP. METH allows the function's signature
4669 to be computed. */
4670 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4672 else
4673 meth = add_method (this_class, flags, meth_name,
4674 build_java_signature (meth));
4676 /* Remember final parameters */
4677 MARK_FINAL_PARMS (meth, orig_arg);
4679 /* Fix the method argument list so we have the argument name
4680 information */
4681 fix_method_argument_names (orig_arg, meth);
4683 /* Register the parameter number and re-install the current line
4684 number */
4685 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4686 lineno = saved_lineno;
4688 /* Register exception specified by the `throws' keyword for
4689 resolution and set the method decl appropriate field to the list.
4690 Note: the grammar ensures that what we get here are class
4691 types. */
4692 if (throws)
4694 throws = nreverse (throws);
4695 for (current = throws; current; current = TREE_CHAIN (current))
4697 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4698 NULL_TREE, NULL_TREE);
4699 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4700 &TREE_VALUE (current);
4702 DECL_FUNCTION_THROWS (meth) = throws;
4705 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4706 DECL_FUNCTION_WFL (meth) = id;
4708 /* Set the flag if we correctly processed a constructor */
4709 if (constructor_ok)
4711 DECL_CONSTRUCTOR_P (meth) = 1;
4712 /* Compute and store the number of artificial parameters declared
4713 for this constructor */
4714 for (count = 0, current = TYPE_FIELDS (this_class); current;
4715 current = TREE_CHAIN (current))
4716 if (FIELD_LOCAL_ALIAS (current))
4717 count++;
4718 DECL_FUNCTION_NAP (meth) = count;
4721 /* Eventually set the @deprecated tag flag */
4722 CHECK_DEPRECATED (meth);
4724 /* If doing xref, store column and line number information instead
4725 of the line number only. */
4726 if (flag_emit_xref)
4727 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4729 return meth;
4732 static void
4733 fix_method_argument_names (orig_arg, meth)
4734 tree orig_arg, meth;
4736 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4737 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4739 TREE_PURPOSE (arg) = this_identifier_node;
4740 arg = TREE_CHAIN (arg);
4742 while (orig_arg != end_params_node)
4744 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4745 orig_arg = TREE_CHAIN (orig_arg);
4746 arg = TREE_CHAIN (arg);
4750 /* Complete the method declaration with METHOD_BODY. */
4752 static void
4753 finish_method_declaration (method_body)
4754 tree method_body;
4756 int flags;
4758 if (!current_function_decl)
4759 return;
4761 flags = get_access_flags_from_decl (current_function_decl);
4763 /* 8.4.5 Method Body */
4764 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4766 tree name = DECL_NAME (current_function_decl);
4767 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4768 "%s method `%s' can't have a body defined",
4769 (METHOD_NATIVE (current_function_decl) ?
4770 "Native" : "Abstract"),
4771 IDENTIFIER_POINTER (name));
4772 method_body = NULL_TREE;
4774 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4776 tree name = DECL_NAME (current_function_decl);
4777 parse_error_context
4778 (DECL_FUNCTION_WFL (current_function_decl),
4779 "Non native and non abstract method `%s' must have a body defined",
4780 IDENTIFIER_POINTER (name));
4781 method_body = NULL_TREE;
4784 if (flag_emit_class_files && method_body
4785 && TREE_CODE (method_body) == NOP_EXPR
4786 && TREE_TYPE (current_function_decl)
4787 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4788 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4790 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4791 maybe_absorb_scoping_blocks ();
4792 /* Exit function's body */
4793 exit_block ();
4794 /* Merge last line of the function with first line, directly in the
4795 function decl. It will be used to emit correct debug info. */
4796 if (!flag_emit_xref)
4797 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4799 /* Since function's argument's list are shared, reset the
4800 ARG_FINAL_P parameter that might have been set on some of this
4801 function parameters. */
4802 UNMARK_FINAL_PARMS (current_function_decl);
4804 /* So we don't have an irrelevant function declaration context for
4805 the next static block we'll see. */
4806 current_function_decl = NULL_TREE;
4809 /* Build a an error message for constructor circularity errors. */
4811 static char *
4812 constructor_circularity_msg (from, to)
4813 tree from, to;
4815 static char string [4096];
4816 char *t = xstrdup (lang_printable_name (from, 0));
4817 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4818 free (t);
4819 return string;
4822 /* Verify a circular call to METH. Return 1 if an error is found, 0
4823 otherwise. */
4825 static int
4826 verify_constructor_circularity (meth, current)
4827 tree meth, current;
4829 static tree list = NULL_TREE;
4830 static int initialized_p;
4831 tree c;
4833 /* If we haven't already registered LIST with the garbage collector,
4834 do so now. */
4835 if (!initialized_p)
4837 ggc_add_tree_root (&list, 1);
4838 initialized_p = 1;
4841 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4843 if (TREE_VALUE (c) == meth)
4845 char *t;
4846 if (list)
4848 tree liste;
4849 list = nreverse (list);
4850 for (liste = list; liste; liste = TREE_CHAIN (liste))
4852 parse_error_context
4853 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4854 constructor_circularity_msg
4855 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4856 java_error_count--;
4859 t = xstrdup (lang_printable_name (meth, 0));
4860 parse_error_context (TREE_PURPOSE (c),
4861 "%s: recursive invocation of constructor `%s'",
4862 constructor_circularity_msg (current, meth), t);
4863 free (t);
4864 list = NULL_TREE;
4865 return 1;
4868 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4870 list = tree_cons (c, current, list);
4871 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4872 return 1;
4873 list = TREE_CHAIN (list);
4875 return 0;
4878 /* Check modifiers that can be declared but exclusively */
4880 static void
4881 check_modifiers_consistency (flags)
4882 int flags;
4884 int acc_count = 0;
4885 tree cl = NULL_TREE;
4887 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4888 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4889 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4890 if (acc_count > 1)
4891 parse_error_context
4892 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4894 acc_count = 0;
4895 cl = NULL_TREE;
4896 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4897 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4898 if (acc_count > 1)
4899 parse_error_context (cl,
4900 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4903 /* Check the methode header METH for abstract specifics features */
4905 static void
4906 check_abstract_method_header (meth)
4907 tree meth;
4909 int flags = get_access_flags_from_decl (meth);
4911 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4912 ACC_ABSTRACT, "abstract method",
4913 IDENTIFIER_POINTER (DECL_NAME (meth)));
4914 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4915 ACC_PUBLIC, "abstract method",
4916 IDENTIFIER_POINTER (DECL_NAME (meth)));
4918 check_modifiers ("Illegal modifier `%s' for interface method",
4919 flags, INTERFACE_METHOD_MODIFIERS);
4922 /* Create a FUNCTION_TYPE node and start augmenting it with the
4923 declared function arguments. Arguments type that can't be resolved
4924 are left as they are, but the returned node is marked as containing
4925 incomplete types. */
4927 static tree
4928 method_declarator (id, list)
4929 tree id, list;
4931 tree arg_types = NULL_TREE, current, node;
4932 tree meth = make_node (FUNCTION_TYPE);
4933 jdep *jdep;
4935 patch_stage = JDEP_NO_PATCH;
4937 if (GET_CPC () == error_mark_node)
4938 return error_mark_node;
4940 /* If we're dealing with an inner class constructor, we hide the
4941 this$<n> decl in the name field of its parameter declaration. We
4942 also might have to hide the outer context local alias
4943 initializers. Not done when the class is a toplevel class. */
4944 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4945 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4947 tree aliases_list, type, thisn;
4948 /* First the aliases, linked to the regular parameters */
4949 aliases_list =
4950 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4951 TREE_TYPE (GET_CPC ()),
4952 NULL_TREE, NULL);
4953 list = chainon (nreverse (aliases_list), list);
4955 /* Then this$<n> */
4956 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4957 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4958 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4959 list);
4962 for (current = list; current; current = TREE_CHAIN (current))
4964 int must_chain = 0;
4965 tree wfl_name = TREE_PURPOSE (current);
4966 tree type = TREE_VALUE (current);
4967 tree name = EXPR_WFL_NODE (wfl_name);
4968 tree already, arg_node;
4969 tree type_wfl = NULL_TREE;
4970 tree real_type;
4972 /* Obtain a suitable type for resolution, if necessary */
4973 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4975 /* Process NAME, as it may specify extra dimension(s) for it */
4976 type = build_array_from_name (type, type_wfl, name, &name);
4977 EXPR_WFL_NODE (wfl_name) = name;
4979 real_type = GET_REAL_TYPE (type);
4980 if (TREE_CODE (real_type) == RECORD_TYPE)
4982 real_type = promote_type (real_type);
4983 if (TREE_CODE (type) == TREE_LIST)
4984 TREE_PURPOSE (type) = real_type;
4987 /* Check redefinition */
4988 for (already = arg_types; already; already = TREE_CHAIN (already))
4989 if (TREE_PURPOSE (already) == name)
4991 parse_error_context
4992 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4993 IDENTIFIER_POINTER (name),
4994 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4995 break;
4998 /* If we've an incomplete argument type, we know there is a location
4999 to patch when the type get resolved, later. */
5000 jdep = NULL;
5001 if (must_chain)
5003 patch_stage = JDEP_METHOD;
5004 type = register_incomplete_type (patch_stage,
5005 type_wfl, wfl_name, type);
5006 jdep = CLASSD_LAST (ctxp->classd_list);
5007 JDEP_MISC (jdep) = id;
5010 /* The argument node: a name and a (possibly) incomplete type. */
5011 arg_node = build_tree_list (name, real_type);
5012 /* Remeber arguments declared final. */
5013 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5015 if (jdep)
5016 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5017 TREE_CHAIN (arg_node) = arg_types;
5018 arg_types = arg_node;
5020 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5021 node = build_tree_list (id, meth);
5022 return node;
5025 static int
5026 unresolved_type_p (wfl, returned)
5027 tree wfl;
5028 tree *returned;
5031 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5033 if (returned)
5035 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5036 if (decl && current_class && (decl == TYPE_NAME (current_class)))
5037 *returned = TREE_TYPE (decl);
5038 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5039 *returned = TREE_TYPE (GET_CPC ());
5040 else
5041 *returned = NULL_TREE;
5043 return 1;
5045 if (returned)
5046 *returned = wfl;
5047 return 0;
5050 /* From NAME, build a qualified identifier node using the
5051 qualification from the current package definition. */
5053 static tree
5054 parser_qualified_classname (name)
5055 tree name;
5057 tree nested_class_name;
5059 if ((nested_class_name = maybe_make_nested_class_name (name)))
5060 return nested_class_name;
5062 if (ctxp->package)
5063 return merge_qualified_name (ctxp->package, name);
5064 else
5065 return name;
5068 /* Called once the type a interface extends is resolved. Returns 0 if
5069 everything is OK. */
5071 static int
5072 parser_check_super_interface (super_decl, this_decl, this_wfl)
5073 tree super_decl, this_decl, this_wfl;
5075 tree super_type = TREE_TYPE (super_decl);
5077 /* Has to be an interface */
5078 if (!CLASS_INTERFACE (super_decl))
5080 parse_error_context
5081 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
5082 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5083 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
5084 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5085 "interface" : "class"),
5086 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
5087 return 1;
5090 /* Check top-level interface access. Inner classes are subject to member
5091 access rules (6.6.1). */
5092 if (! INNER_CLASS_P (super_type)
5093 && check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
5094 return 1;
5096 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5097 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5098 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5099 return 0;
5102 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5103 0 if everthing is OK. */
5105 static int
5106 parser_check_super (super_decl, this_decl, wfl)
5107 tree super_decl, this_decl, wfl;
5109 tree super_type = TREE_TYPE (super_decl);
5111 /* SUPER should be a CLASS (neither an array nor an interface) */
5112 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5114 parse_error_context
5115 (wfl, "Class `%s' can't subclass %s `%s'",
5116 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5117 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5118 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5119 return 1;
5122 if (CLASS_FINAL (TYPE_NAME (super_type)))
5124 parse_error_context (wfl, "Can't subclass final classes: %s",
5125 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5126 return 1;
5129 /* Check top-level class scope. Inner classes are subject to member access
5130 rules (6.6.1). */
5131 if (! INNER_CLASS_P (super_type)
5132 && (check_pkg_class_access (DECL_NAME (super_decl), wfl)))
5133 return 1;
5135 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5136 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5137 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5138 return 0;
5141 /* Create a new dependency list and link it (in a LIFO manner) to the
5142 CTXP list of type dependency list. */
5144 static void
5145 create_jdep_list (ctxp)
5146 struct parser_ctxt *ctxp;
5148 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
5149 new->first = new->last = NULL;
5150 new->next = ctxp->classd_list;
5151 ctxp->classd_list = new;
5154 static jdeplist *
5155 reverse_jdep_list (ctxp)
5156 struct parser_ctxt *ctxp;
5158 register jdeplist *prev = NULL, *current, *next;
5159 for (current = ctxp->classd_list; current; current = next)
5161 next = current->next;
5162 current->next = prev;
5163 prev = current;
5165 return prev;
5168 /* Create a fake pointer based on the ID stored in
5169 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5170 registered again. */
5172 static tree
5173 obtain_incomplete_type (type_name)
5174 tree type_name;
5176 tree ptr = NULL_TREE, name;
5178 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5179 name = EXPR_WFL_NODE (type_name);
5180 else if (INCOMPLETE_TYPE_P (type_name))
5181 name = TYPE_NAME (type_name);
5182 else
5183 abort ();
5185 BUILD_PTR_FROM_NAME (ptr, name);
5186 layout_type (ptr);
5188 return ptr;
5191 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5192 non NULL instead of computing a new fake type based on WFL. The new
5193 dependency is inserted in the current type dependency list, in FIFO
5194 manner. */
5196 static tree
5197 register_incomplete_type (kind, wfl, decl, ptr)
5198 int kind;
5199 tree wfl, decl, ptr;
5201 jdep *new = (jdep *)xmalloc (sizeof (jdep));
5203 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5204 ptr = obtain_incomplete_type (wfl);
5206 JDEP_KIND (new) = kind;
5207 JDEP_DECL (new) = decl;
5208 JDEP_TO_RESOLVE (new) = ptr;
5209 JDEP_WFL (new) = wfl;
5210 JDEP_CHAIN (new) = NULL;
5211 JDEP_MISC (new) = NULL_TREE;
5212 /* For some dependencies, set the enclosing class of the current
5213 class to be the enclosing context */
5214 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5215 || kind == JDEP_ANONYMOUS)
5216 && GET_ENCLOSING_CPC ())
5217 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5218 else
5219 JDEP_ENCLOSING (new) = GET_CPC ();
5220 JDEP_GET_PATCH (new) = (tree *)NULL;
5222 JDEP_INSERT (ctxp->classd_list, new);
5224 return ptr;
5227 /* This checks for circular references with innerclasses. We start
5228 from SOURCE and should never reach TARGET. Extended/implemented
5229 types in SOURCE have their enclosing context checked not to reach
5230 TARGET. When the last enclosing context of SOURCE is reached, its
5231 extended/implemented types are also checked not to reach TARGET.
5232 In case of error, WFL of the offending type is returned; NULL_TREE
5233 otherwise. */
5235 static tree
5236 check_inner_circular_reference (source, target)
5237 tree source;
5238 tree target;
5240 tree basetype_vec = TYPE_BINFO_BASETYPES (source);
5241 tree ctx, cl;
5242 int i;
5244 if (!basetype_vec)
5245 return NULL_TREE;
5247 for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5249 tree su = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
5251 if (inherits_from_p (su, target))
5252 return lookup_cl (TYPE_NAME (su));
5254 for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5256 /* An enclosing context shouldn't be TARGET */
5257 if (ctx == TYPE_NAME (target))
5258 return lookup_cl (TYPE_NAME (su));
5260 /* When we reach the enclosing last context, start a check
5261 on it, with the same target */
5262 if (! DECL_CONTEXT (ctx) &&
5263 (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5264 return cl;
5267 return NULL_TREE;
5270 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5271 offending type if a circularity is detected. NULL_TREE is returned
5272 otherwise. TYPE can be an interface or a class. */
5274 static tree
5275 check_circular_reference (type)
5276 tree type;
5278 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5279 int i;
5281 if (!basetype_vec)
5282 return NULL_TREE;
5284 if (! CLASS_INTERFACE (TYPE_NAME (type)))
5286 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5287 return lookup_cl (TYPE_NAME (type));
5288 return NULL_TREE;
5291 for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5293 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5294 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5295 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5296 return lookup_cl (TYPE_NAME (BINFO_TYPE (vec_elt)));
5298 return NULL_TREE;
5301 void
5302 java_check_circular_reference ()
5304 tree current;
5305 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5307 tree type = TREE_TYPE (current);
5308 tree cl;
5310 cl = check_circular_reference (type);
5311 if (! cl)
5312 cl = check_inner_circular_reference (type, type);
5313 if (cl)
5314 parse_error_context (cl, "Cyclic class inheritance%s",
5315 (cyclic_inheritance_report ?
5316 cyclic_inheritance_report : ""));
5320 /* Augment the parameter list PARM with parameters crafted to
5321 initialize outer context locals aliases. Through ARTIFICIAL, a
5322 count is kept of the number of crafted parameters. MODE governs
5323 what eventually gets created: something suitable for a function
5324 creation or a function invocation, either the constructor or
5325 finit$. */
5327 static tree
5328 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5329 int mode;
5330 tree class_type, parm;
5331 int *artificial;
5333 tree field;
5334 tree additional_parms = NULL_TREE;
5336 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5337 if (FIELD_LOCAL_ALIAS (field))
5339 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5340 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5341 tree mangled_id;
5343 switch (mode)
5345 case AIPL_FUNCTION_DECLARATION:
5346 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5347 &buffer [4]);
5348 purpose = build_wfl_node (mangled_id);
5349 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5350 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5351 else
5352 value = TREE_TYPE (field);
5353 break;
5355 case AIPL_FUNCTION_CREATION:
5356 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5357 &buffer [4]);
5358 value = TREE_TYPE (field);
5359 break;
5361 case AIPL_FUNCTION_FINIT_INVOCATION:
5362 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5363 &buffer [4]);
5364 /* Now, this is wrong. purpose should always be the NAME
5365 of something and value its matching value (decl, type,
5366 etc...) FIXME -- but there is a lot to fix. */
5368 /* When invoked for this kind of operation, we already
5369 know whether a field is used or not. */
5370 purpose = TREE_TYPE (field);
5371 value = build_wfl_node (mangled_id);
5372 break;
5374 case AIPL_FUNCTION_CTOR_INVOCATION:
5375 /* There are two case: the constructor invokation happends
5376 outside the local inner, in which case, locales from the outer
5377 context are directly used.
5379 Otherwise, we fold to using the alias directly. */
5380 if (class_type == current_class)
5381 value = field;
5382 else
5384 name = get_identifier (&buffer[4]);
5385 value = IDENTIFIER_LOCAL_VALUE (name);
5387 break;
5389 additional_parms = tree_cons (purpose, value, additional_parms);
5390 if (artificial)
5391 *artificial +=1;
5393 if (additional_parms)
5395 if (ANONYMOUS_CLASS_P (class_type)
5396 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5397 additional_parms = nreverse (additional_parms);
5398 parm = chainon (additional_parms, parm);
5401 return parm;
5404 /* Craft a constructor for CLASS_DECL -- what we should do when none
5405 where found. ARGS is non NULL when a special signature must be
5406 enforced. This is the case for anonymous classes. */
5408 static void
5409 craft_constructor (class_decl, args)
5410 tree class_decl, args;
5412 tree class_type = TREE_TYPE (class_decl);
5413 tree parm = NULL_TREE;
5414 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5415 ACC_PUBLIC : 0);
5416 int i = 0, artificial = 0;
5417 tree decl, ctor_name;
5418 char buffer [80];
5420 /* The constructor name is <init> unless we're dealing with an
5421 anonymous class, in which case the name will be fixed after having
5422 be expanded. */
5423 if (ANONYMOUS_CLASS_P (class_type))
5424 ctor_name = DECL_NAME (class_decl);
5425 else
5426 ctor_name = init_identifier_node;
5428 /* If we're dealing with an inner class constructor, we hide the
5429 this$<n> decl in the name field of its parameter declaration. */
5430 if (PURE_INNER_CLASS_TYPE_P (class_type))
5432 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5433 parm = tree_cons (build_current_thisn (class_type),
5434 build_pointer_type (type), parm);
5436 /* Some more arguments to be hidden here. The values of the local
5437 variables of the outer context that the inner class needs to see. */
5438 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5439 class_type, parm,
5440 &artificial);
5443 /* Then if there are any args to be enforced, enforce them now */
5444 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5446 sprintf (buffer, "parm%d", i++);
5447 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5450 CRAFTED_PARAM_LIST_FIXUP (parm);
5451 decl = create_artificial_method (class_type, flags, void_type_node,
5452 ctor_name, parm);
5453 fix_method_argument_names (parm, decl);
5454 /* Now, mark the artificial parameters. */
5455 DECL_FUNCTION_NAP (decl) = artificial;
5456 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5460 /* Fix the constructors. This will be called right after circular
5461 references have been checked. It is necessary to fix constructors
5462 early even if no code generation will take place for that class:
5463 some generated constructor might be required by the class whose
5464 compilation triggered this one to be simply loaded. */
5466 void
5467 java_fix_constructors ()
5469 tree current;
5471 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5473 tree class_type = TREE_TYPE (current);
5474 int saw_ctor = 0;
5475 tree decl;
5477 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5478 continue;
5480 current_class = class_type;
5481 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5483 if (DECL_CONSTRUCTOR_P (decl))
5485 fix_constructors (decl);
5486 saw_ctor = 1;
5490 /* Anonymous class constructor can't be generated that early. */
5491 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5492 craft_constructor (current, NULL_TREE);
5496 /* safe_layout_class just makes sure that we can load a class without
5497 disrupting the current_class, input_file, lineno, etc, information
5498 about the class processed currently. */
5500 void
5501 safe_layout_class (class)
5502 tree class;
5504 tree save_current_class = current_class;
5505 const char *save_input_filename = input_filename;
5506 int save_lineno = lineno;
5508 layout_class (class);
5510 current_class = save_current_class;
5511 input_filename = save_input_filename;
5512 lineno = save_lineno;
5515 static tree
5516 jdep_resolve_class (dep)
5517 jdep *dep;
5519 tree decl;
5521 if (JDEP_RESOLVED_P (dep))
5522 decl = JDEP_RESOLVED_DECL (dep);
5523 else
5525 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5526 JDEP_DECL (dep), JDEP_WFL (dep));
5527 JDEP_RESOLVED (dep, decl);
5530 if (!decl)
5531 complete_class_report_errors (dep);
5532 else if (PURE_INNER_CLASS_DECL_P (decl))
5534 tree inner = TREE_TYPE (decl);
5535 if (! CLASS_LOADED_P (inner))
5537 safe_layout_class (inner);
5538 if (TYPE_SIZE (inner) == error_mark_node)
5539 TYPE_SIZE (inner) = NULL_TREE;
5541 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5543 return decl;
5546 /* Complete unsatisfied class declaration and their dependencies */
5548 void
5549 java_complete_class ()
5551 tree cclass;
5552 jdeplist *cclassd;
5553 int error_found;
5554 tree type;
5556 /* Process imports */
5557 process_imports ();
5559 /* Rever things so we have the right order */
5560 ctxp->class_list = nreverse (ctxp->class_list);
5561 ctxp->classd_list = reverse_jdep_list (ctxp);
5563 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5564 cclass && cclassd;
5565 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5567 jdep *dep;
5569 /* We keep the compilation unit imports in the class so that
5570 they can be used later to resolve type dependencies that
5571 aren't necessary to solve now. */
5572 TYPE_IMPORT_LIST (TREE_TYPE (cclass)) = ctxp->import_list;
5573 TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (cclass)) = ctxp->import_demand_list;
5575 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5577 tree decl;
5578 if (!(decl = jdep_resolve_class (dep)))
5579 continue;
5581 /* Now it's time to patch */
5582 switch (JDEP_KIND (dep))
5584 case JDEP_SUPER:
5585 /* Simply patch super */
5586 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5587 continue;
5588 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5589 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5590 break;
5592 case JDEP_FIELD:
5594 /* We do part of the job done in add_field */
5595 tree field_decl = JDEP_DECL (dep);
5596 tree field_type = TREE_TYPE (decl);
5597 if (TREE_CODE (field_type) == RECORD_TYPE)
5598 field_type = promote_type (field_type);
5599 TREE_TYPE (field_decl) = field_type;
5600 DECL_ALIGN (field_decl) = 0;
5601 DECL_USER_ALIGN (field_decl) = 0;
5602 layout_decl (field_decl, 0);
5603 SOURCE_FRONTEND_DEBUG
5604 (("Completed field/var decl `%s' with `%s'",
5605 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5606 IDENTIFIER_POINTER (DECL_NAME (decl))));
5607 break;
5609 case JDEP_METHOD: /* We start patching a method */
5610 case JDEP_METHOD_RETURN:
5611 error_found = 0;
5612 while (1)
5614 if (decl)
5616 type = TREE_TYPE(decl);
5617 if (TREE_CODE (type) == RECORD_TYPE)
5618 type = promote_type (type);
5619 JDEP_APPLY_PATCH (dep, type);
5620 SOURCE_FRONTEND_DEBUG
5621 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5622 "Completing fct `%s' with ret type `%s'":
5623 "Completing arg `%s' with type `%s'"),
5624 IDENTIFIER_POINTER (EXPR_WFL_NODE
5625 (JDEP_DECL_WFL (dep))),
5626 IDENTIFIER_POINTER (DECL_NAME (decl))));
5628 else
5629 error_found = 1;
5630 dep = JDEP_CHAIN (dep);
5631 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5632 break;
5633 else
5634 decl = jdep_resolve_class (dep);
5636 if (!error_found)
5638 tree mdecl = JDEP_DECL (dep), signature;
5639 /* Recompute and reset the signature, check first that
5640 all types are now defined. If they're not,
5641 dont build the signature. */
5642 if (check_method_types_complete (mdecl))
5644 signature = build_java_signature (TREE_TYPE (mdecl));
5645 set_java_signature (TREE_TYPE (mdecl), signature);
5648 else
5649 continue;
5650 break;
5652 case JDEP_INTERFACE:
5653 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5654 JDEP_WFL (dep)))
5655 continue;
5656 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5657 break;
5659 case JDEP_PARM:
5660 case JDEP_VARIABLE:
5661 type = TREE_TYPE(decl);
5662 if (TREE_CODE (type) == RECORD_TYPE)
5663 type = promote_type (type);
5664 JDEP_APPLY_PATCH (dep, type);
5665 break;
5667 case JDEP_TYPE:
5668 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5669 SOURCE_FRONTEND_DEBUG
5670 (("Completing a random type dependency on a '%s' node",
5671 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5672 break;
5674 case JDEP_EXCEPTION:
5675 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5676 SOURCE_FRONTEND_DEBUG
5677 (("Completing `%s' `throws' argument node",
5678 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5679 break;
5681 case JDEP_ANONYMOUS:
5682 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5683 break;
5685 default:
5686 abort ();
5690 return;
5693 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5694 array. */
5696 static tree
5697 resolve_class (enclosing, class_type, decl, cl)
5698 tree enclosing, class_type, decl, cl;
5700 tree tname = TYPE_NAME (class_type);
5701 tree resolved_type = TREE_TYPE (class_type);
5702 int array_dims = 0;
5703 tree resolved_type_decl;
5705 if (resolved_type != NULL_TREE)
5707 tree resolved_type_decl = TYPE_NAME (resolved_type);
5708 if (resolved_type_decl == NULL_TREE
5709 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5711 resolved_type_decl = build_decl (TYPE_DECL,
5712 TYPE_NAME (class_type),
5713 resolved_type);
5715 return resolved_type_decl;
5718 /* 1- Check to see if we have an array. If true, find what we really
5719 want to resolve */
5720 if ((array_dims = build_type_name_from_array_name (tname,
5721 &TYPE_NAME (class_type))))
5722 WFL_STRIP_BRACKET (cl, cl);
5724 /* 2- Resolve the bare type */
5725 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5726 decl, cl)))
5727 return NULL_TREE;
5728 resolved_type = TREE_TYPE (resolved_type_decl);
5730 /* 3- If we have and array, reconstruct the array down to its nesting */
5731 if (array_dims)
5733 for (; array_dims; array_dims--)
5734 resolved_type = build_java_array_type (resolved_type, -1);
5735 resolved_type_decl = TYPE_NAME (resolved_type);
5737 TREE_TYPE (class_type) = resolved_type;
5738 return resolved_type_decl;
5741 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5742 are used to report error messages. Do not try to replace TYPE_NAME
5743 (class_type) by a variable, since it is changed by
5744 find_in_imports{_on_demand} and (but it doesn't really matter)
5745 qualify_and_find. */
5747 tree
5748 do_resolve_class (enclosing, class_type, decl, cl)
5749 tree enclosing, class_type, decl, cl;
5751 tree new_class_decl = NULL_TREE, super = NULL_TREE;
5752 tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5753 struct hash_table _ht, *circularity_hash = &_ht;
5755 /* This hash table is used to register the classes we're going
5756 through when searching the current class as an inner class, in
5757 order to detect circular references. Remember to free it before
5758 returning the section 0- of this function. */
5759 hash_table_init (circularity_hash, hash_newfunc,
5760 java_hash_hash_tree_node, java_hash_compare_tree_node);
5762 /* 0- Search in the current class as an inner class.
5763 Maybe some code here should be added to load the class or
5764 something, at least if the class isn't an inner class and ended
5765 being loaded from class file. FIXME. */
5766 while (enclosing)
5768 new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5769 &super, class_type);
5770 if (new_class_decl)
5771 break;
5773 /* If we haven't found anything because SUPER reached Object and
5774 ENCLOSING happens to be an innerclass, try the enclosing context. */
5775 if ((!super || super == object_type_node) &&
5776 enclosing && INNER_CLASS_DECL_P (enclosing))
5777 enclosing = DECL_CONTEXT (enclosing);
5778 else
5779 enclosing = NULL_TREE;
5782 hash_table_free (circularity_hash);
5784 if (new_class_decl)
5785 return new_class_decl;
5787 /* 1- Check for the type in single imports. This will change
5788 TYPE_NAME() if something relevant is found */
5789 find_in_imports (saved_enclosing_type, class_type);
5791 /* 2- And check for the type in the current compilation unit */
5792 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5794 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5795 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5796 load_class (TYPE_NAME (class_type), 0);
5797 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5800 /* 3- Search according to the current package definition */
5801 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5803 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5804 TYPE_NAME (class_type))))
5805 return new_class_decl;
5808 /* 4- Check the import on demands. Don't allow bar.baz to be
5809 imported from foo.* */
5810 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5811 if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5812 return NULL_TREE;
5814 /* If found in find_in_imports_on_demant, the type has already been
5815 loaded. */
5816 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5817 return new_class_decl;
5819 /* 5- Try with a name qualified with the package name we've seen so far */
5820 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5822 tree package;
5824 /* If there is a current package (ctxp->package), it's the first
5825 element of package_list and we can skip it. */
5826 for (package = (ctxp->package ?
5827 TREE_CHAIN (package_list) : package_list);
5828 package; package = TREE_CHAIN (package))
5829 if ((new_class_decl = qualify_and_find (class_type,
5830 TREE_PURPOSE (package),
5831 TYPE_NAME (class_type))))
5832 return new_class_decl;
5835 /* 5- Check an other compilation unit that bears the name of type */
5836 load_class (TYPE_NAME (class_type), 0);
5838 if (!cl)
5839 cl = lookup_cl (decl);
5841 /* If we don't have a value for CL, then we're being called recursively.
5842 We can't check package access just yet, but it will be taken care of
5843 by the caller. */
5844 if (cl)
5846 if (check_pkg_class_access (TYPE_NAME (class_type), cl))
5847 return NULL_TREE;
5850 /* 6- Last call for a resolution */
5851 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5854 static tree
5855 qualify_and_find (class_type, package, name)
5856 tree class_type, package, name;
5858 tree new_qualified = merge_qualified_name (package, name);
5859 tree new_class_decl;
5861 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5862 load_class (new_qualified, 0);
5863 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5865 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5866 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5867 load_class (new_qualified, 0);
5868 TYPE_NAME (class_type) = new_qualified;
5869 return IDENTIFIER_CLASS_VALUE (new_qualified);
5871 return NULL_TREE;
5874 /* Resolve NAME and lay it out (if not done and if not the current
5875 parsed class). Return a decl node. This function is meant to be
5876 called when type resolution is necessary during the walk pass. */
5878 static tree
5879 resolve_and_layout (something, cl)
5880 tree something;
5881 tree cl;
5883 tree decl, decl_type;
5885 /* Don't do that on the current class */
5886 if (something == current_class)
5887 return TYPE_NAME (current_class);
5889 /* Don't do anything for void and other primitive types */
5890 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5891 return NULL_TREE;
5893 /* Pointer types can be reall pointer types or fake pointers. When
5894 finding a real pointer, recheck for primitive types */
5895 if (TREE_CODE (something) == POINTER_TYPE)
5897 if (TREE_TYPE (something))
5899 something = TREE_TYPE (something);
5900 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5901 return NULL_TREE;
5903 else
5904 something = TYPE_NAME (something);
5907 /* Don't do anything for arrays of primitive types */
5908 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5909 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5910 return NULL_TREE;
5912 /* Something might be a WFL */
5913 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5914 something = EXPR_WFL_NODE (something);
5916 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5917 TYPE_DECL or a real TYPE */
5918 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5919 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5920 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5922 if (!(decl = resolve_no_layout (something, cl)))
5923 return NULL_TREE;
5925 /* Resolve and layout if necessary */
5926 decl_type = TREE_TYPE (decl);
5927 layout_class_methods (decl_type);
5928 /* Check methods */
5929 if (CLASS_FROM_SOURCE_P (decl_type))
5930 java_check_methods (decl);
5931 /* Layout the type if necessary */
5932 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5933 safe_layout_class (decl_type);
5935 return decl;
5938 /* Resolve a class, returns its decl but doesn't perform any
5939 layout. The current parsing context is saved and restored */
5941 static tree
5942 resolve_no_layout (name, cl)
5943 tree name, cl;
5945 tree ptr, decl;
5946 BUILD_PTR_FROM_NAME (ptr, name);
5947 java_parser_context_save_global ();
5948 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5949 java_parser_context_restore_global ();
5951 return decl;
5954 /* Called when reporting errors. Skip the '[]'s in a complex array
5955 type description that failed to be resolved. purify_type_name can't
5956 use an identifier tree. */
5958 static const char *
5959 purify_type_name (name)
5960 const char *name;
5962 int len = strlen (name);
5963 int bracket_found;
5965 STRING_STRIP_BRACKETS (name, len, bracket_found);
5966 if (bracket_found)
5968 char *stripped_name = xmemdup (name, len, len+1);
5969 stripped_name [len] = '\0';
5970 return stripped_name;
5972 return name;
5975 /* The type CURRENT refers to can't be found. We print error messages. */
5977 static void
5978 complete_class_report_errors (dep)
5979 jdep *dep;
5981 const char *name;
5983 if (!JDEP_WFL (dep))
5984 return;
5986 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5987 switch (JDEP_KIND (dep))
5989 case JDEP_SUPER:
5990 parse_error_context
5991 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5992 purify_type_name (name),
5993 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5994 break;
5995 case JDEP_FIELD:
5996 parse_error_context
5997 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5998 purify_type_name (name),
5999 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6000 break;
6001 case JDEP_METHOD: /* Covers arguments */
6002 parse_error_context
6003 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
6004 purify_type_name (name),
6005 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6006 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6007 break;
6008 case JDEP_METHOD_RETURN: /* Covers return type */
6009 parse_error_context
6010 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
6011 purify_type_name (name),
6012 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6013 break;
6014 case JDEP_INTERFACE:
6015 parse_error_context
6016 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
6017 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6018 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6019 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6020 break;
6021 case JDEP_VARIABLE:
6022 parse_error_context
6023 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
6024 purify_type_name (IDENTIFIER_POINTER
6025 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6026 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6027 break;
6028 case JDEP_EXCEPTION: /* As specified by `throws' */
6029 parse_error_context
6030 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
6031 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6032 break;
6033 default:
6034 /* Fix for -Wall. Just break doing nothing. The error will be
6035 caught later */
6036 break;
6040 /* Return a static string containing the DECL prototype string. If
6041 DECL is a constructor, use the class name instead of the form
6042 <init> */
6044 static const char *
6045 get_printable_method_name (decl)
6046 tree decl;
6048 const char *to_return;
6049 tree name = NULL_TREE;
6051 if (DECL_CONSTRUCTOR_P (decl))
6053 name = DECL_NAME (decl);
6054 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6057 to_return = lang_printable_name (decl, 0);
6058 if (DECL_CONSTRUCTOR_P (decl))
6059 DECL_NAME (decl) = name;
6061 return to_return;
6064 /* Track method being redefined inside the same class. As a side
6065 effect, set DECL_NAME to an IDENTIFIER (prior entering this
6066 function it's a FWL, so we can track errors more accurately.) */
6068 static int
6069 check_method_redefinition (class, method)
6070 tree class, method;
6072 tree redef, sig;
6074 /* There's no need to verify <clinit> and finit$ and instinit$ */
6075 if (DECL_CLINIT_P (method)
6076 || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6077 return 0;
6079 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6080 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6082 if (redef == method)
6083 break;
6084 if (DECL_NAME (redef) == DECL_NAME (method)
6085 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6086 && !DECL_ARTIFICIAL (method))
6088 parse_error_context
6089 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
6090 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6091 get_printable_method_name (redef));
6092 return 1;
6095 return 0;
6098 /* Return 1 if check went ok, 0 otherwise. */
6099 static int
6100 check_abstract_method_definitions (do_interface, class_decl, type)
6101 int do_interface;
6102 tree class_decl, type;
6104 tree class = TREE_TYPE (class_decl);
6105 tree method, end_type;
6106 int ok = 1;
6108 end_type = (do_interface ? object_type_node : type);
6109 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6111 tree other_super, other_method, method_sig, method_name;
6112 int found = 0;
6113 int end_type_reached = 0;
6115 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6116 continue;
6118 /* Now verify that somewhere in between TYPE and CLASS,
6119 abstract method METHOD gets a non abstract definition
6120 that is inherited by CLASS. */
6122 method_sig = build_java_signature (TREE_TYPE (method));
6123 method_name = DECL_NAME (method);
6124 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6125 method_name = EXPR_WFL_NODE (method_name);
6127 other_super = class;
6128 do {
6129 if (other_super == end_type)
6130 end_type_reached = 1;
6132 /* Method search */
6133 for (other_method = TYPE_METHODS (other_super); other_method;
6134 other_method = TREE_CHAIN (other_method))
6136 tree s = build_java_signature (TREE_TYPE (other_method));
6137 tree other_name = DECL_NAME (other_method);
6139 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6140 other_name = EXPR_WFL_NODE (other_name);
6141 if (!DECL_CLINIT_P (other_method)
6142 && !DECL_CONSTRUCTOR_P (other_method)
6143 && method_name == other_name
6144 && method_sig == s
6145 && !METHOD_ABSTRACT (other_method))
6147 found = 1;
6148 break;
6151 other_super = CLASSTYPE_SUPER (other_super);
6152 } while (!end_type_reached);
6154 /* Report that abstract METHOD didn't find an implementation
6155 that CLASS can use. */
6156 if (!found)
6158 char *t = xstrdup (lang_printable_name
6159 (TREE_TYPE (TREE_TYPE (method)), 0));
6160 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6162 parse_error_context
6163 (lookup_cl (class_decl),
6164 "Class `%s' doesn't define the abstract method `%s %s' from %s `%s'. This method must be defined or %s `%s' must be declared abstract",
6165 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6166 t, lang_printable_name (method, 0),
6167 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6168 "interface" : "class"),
6169 IDENTIFIER_POINTER (ccn),
6170 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6171 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6172 ok = 0;
6173 free (t);
6177 if (ok && do_interface)
6179 /* Check for implemented interfaces. */
6180 int i;
6181 tree vector = TYPE_BINFO_BASETYPES (type);
6182 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
6184 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6185 ok = check_abstract_method_definitions (1, class_decl, super);
6189 return ok;
6192 /* Check that CLASS_DECL somehow implements all inherited abstract
6193 methods. */
6195 static void
6196 java_check_abstract_method_definitions (class_decl)
6197 tree class_decl;
6199 tree class = TREE_TYPE (class_decl);
6200 tree super, vector;
6201 int i;
6203 if (CLASS_ABSTRACT (class_decl))
6204 return;
6206 /* Check for inherited types */
6207 super = class;
6208 do {
6209 super = CLASSTYPE_SUPER (super);
6210 check_abstract_method_definitions (0, class_decl, super);
6211 } while (super != object_type_node);
6213 /* Check for implemented interfaces. */
6214 vector = TYPE_BINFO_BASETYPES (class);
6215 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
6217 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6218 check_abstract_method_definitions (1, class_decl, super);
6222 /* Check all the types method DECL uses and return 1 if all of them
6223 are now complete, 0 otherwise. This is used to check whether its
6224 safe to build a method signature or not. */
6226 static int
6227 check_method_types_complete (decl)
6228 tree decl;
6230 tree type = TREE_TYPE (decl);
6231 tree args;
6233 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6234 return 0;
6236 args = TYPE_ARG_TYPES (type);
6237 if (TREE_CODE (type) == METHOD_TYPE)
6238 args = TREE_CHAIN (args);
6239 for (; args != end_params_node; args = TREE_CHAIN (args))
6240 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6241 return 0;
6243 return 1;
6246 /* Visible interface to check methods contained in CLASS_DECL */
6248 void
6249 java_check_methods (class_decl)
6250 tree class_decl;
6252 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6253 return;
6255 if (CLASS_INTERFACE (class_decl))
6256 java_check_abstract_methods (class_decl);
6257 else
6258 java_check_regular_methods (class_decl);
6260 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6263 /* Check all the methods of CLASS_DECL. Methods are first completed
6264 then checked according to regular method existance rules. If no
6265 constructor for CLASS_DECL were encountered, then build its
6266 declaration. */
6268 static void
6269 java_check_regular_methods (class_decl)
6270 tree class_decl;
6272 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6273 tree method;
6274 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6275 tree found = NULL_TREE;
6276 tree mthrows;
6278 /* It is not necessary to check methods defined in java.lang.Object */
6279 if (class == object_type_node)
6280 return;
6282 if (!TYPE_NVIRTUALS (class))
6283 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6285 /* Should take interfaces into account. FIXME */
6286 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6288 tree sig;
6289 tree method_wfl = DECL_FUNCTION_WFL (method);
6290 int aflags;
6292 /* Check for redefinitions */
6293 if (check_method_redefinition (class, method))
6294 continue;
6296 /* If we see one constructor a mark so we don't generate the
6297 default one. Also skip other verifications: constructors
6298 can't be inherited hence hiden or overriden */
6299 if (DECL_CONSTRUCTOR_P (method))
6301 saw_constructor = 1;
6302 continue;
6305 /* We verify things thrown by the method. They must inherits from
6306 java.lang.Throwable */
6307 for (mthrows = DECL_FUNCTION_THROWS (method);
6308 mthrows; mthrows = TREE_CHAIN (mthrows))
6310 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6311 parse_error_context
6312 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6313 IDENTIFIER_POINTER
6314 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6317 sig = build_java_argument_signature (TREE_TYPE (method));
6318 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6320 /* Inner class can't declare static methods */
6321 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6323 char *t = xstrdup (lang_printable_name (class, 0));
6324 parse_error_context
6325 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6326 lang_printable_name (method, 0), t);
6327 free (t);
6330 /* Nothing overrides or it's a private method. */
6331 if (!found)
6332 continue;
6333 if (METHOD_PRIVATE (found))
6335 found = NULL_TREE;
6336 continue;
6339 /* If `found' is declared in an interface, make sure the
6340 modifier matches. */
6341 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6342 && clinit_identifier_node != DECL_NAME (found)
6343 && !METHOD_PUBLIC (method))
6345 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6346 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6347 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6348 lang_printable_name (method, 0),
6349 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6352 /* Can't override a method with the same name and different return
6353 types. */
6354 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6356 char *t = xstrdup
6357 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6358 parse_error_context
6359 (method_wfl,
6360 "Method `%s' was defined with return type `%s' in class `%s'",
6361 lang_printable_name (found, 0), t,
6362 IDENTIFIER_POINTER
6363 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6364 free (t);
6367 aflags = get_access_flags_from_decl (found);
6369 /* Can't override final. Can't override static. */
6370 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6372 /* Static *can* override static */
6373 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6374 continue;
6375 parse_error_context
6376 (method_wfl,
6377 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6378 (METHOD_FINAL (found) ? "Final" : "Static"),
6379 lang_printable_name (found, 0),
6380 (METHOD_FINAL (found) ? "final" : "static"),
6381 IDENTIFIER_POINTER
6382 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6383 continue;
6386 /* Static method can't override instance method. */
6387 if (METHOD_STATIC (method))
6389 parse_error_context
6390 (method_wfl,
6391 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6392 lang_printable_name (found, 0),
6393 IDENTIFIER_POINTER
6394 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6395 continue;
6398 /* - Overriding/hiding public must be public
6399 - Overriding/hiding protected must be protected or public
6400 - If the overriden or hidden method has default (package)
6401 access, then the overriding or hiding method must not be
6402 private; otherwise, a compile-time error occurs. If
6403 `found' belongs to an interface, things have been already
6404 taken care of. */
6405 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6406 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6407 || (METHOD_PROTECTED (found)
6408 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6409 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6410 && METHOD_PRIVATE (method))))
6412 parse_error_context
6413 (method_wfl,
6414 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6415 (METHOD_PUBLIC (method) ? "public" :
6416 (METHOD_PRIVATE (method) ? "private" : "protected")),
6417 IDENTIFIER_POINTER (DECL_NAME
6418 (TYPE_NAME (DECL_CONTEXT (found)))));
6419 continue;
6422 /* Overriding methods must have compatible `throws' clauses on checked
6423 exceptions, if any */
6424 check_throws_clauses (method, method_wfl, found);
6426 /* Inheriting multiple methods with the same signature. FIXME */
6429 if (!TYPE_NVIRTUALS (class))
6430 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6432 /* Search for inherited abstract method not yet implemented in this
6433 class. */
6434 java_check_abstract_method_definitions (class_decl);
6436 if (!saw_constructor)
6437 abort ();
6440 /* Return a non zero value if the `throws' clause of METHOD (if any)
6441 is incompatible with the `throws' clause of FOUND (if any). */
6443 static void
6444 check_throws_clauses (method, method_wfl, found)
6445 tree method, method_wfl, found;
6447 tree mthrows, fthrows;
6449 /* Can't check these things with class loaded from bytecode. FIXME */
6450 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6451 return;
6453 for (mthrows = DECL_FUNCTION_THROWS (method);
6454 mthrows; mthrows = TREE_CHAIN (mthrows))
6456 /* We don't verify unchecked expressions */
6457 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6458 continue;
6459 /* Checked expression must be compatible */
6460 for (fthrows = DECL_FUNCTION_THROWS (found);
6461 fthrows; fthrows = TREE_CHAIN (fthrows))
6462 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6463 break;
6464 if (!fthrows)
6466 parse_error_context
6467 (method_wfl, "Invalid checked exception class `%s' in `throws' clause. The exception must be a subclass of an exception thrown by `%s' from class `%s'",
6468 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6469 lang_printable_name (found, 0),
6470 IDENTIFIER_POINTER
6471 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6476 /* Check abstract method of interface INTERFACE */
6478 static void
6479 java_check_abstract_methods (interface_decl)
6480 tree interface_decl;
6482 int i, n;
6483 tree method, basetype_vec, found;
6484 tree interface = TREE_TYPE (interface_decl);
6486 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6488 /* 2- Check for double definition inside the defining interface */
6489 if (check_method_redefinition (interface, method))
6490 continue;
6492 /* 3- Overriding is OK as far as we preserve the return type and
6493 the thrown exceptions (FIXME) */
6494 found = lookup_java_interface_method2 (interface, method);
6495 if (found)
6497 char *t;
6498 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6499 parse_error_context
6500 (DECL_FUNCTION_WFL (found),
6501 "Method `%s' was defined with return type `%s' in class `%s'",
6502 lang_printable_name (found, 0), t,
6503 IDENTIFIER_POINTER
6504 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6505 free (t);
6506 continue;
6510 /* 4- Inherited methods can't differ by their returned types */
6511 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6512 return;
6513 n = TREE_VEC_LENGTH (basetype_vec);
6514 for (i = 0; i < n; i++)
6516 tree sub_interface_method, sub_interface;
6517 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6518 if (!vec_elt)
6519 continue;
6520 sub_interface = BINFO_TYPE (vec_elt);
6521 for (sub_interface_method = TYPE_METHODS (sub_interface);
6522 sub_interface_method;
6523 sub_interface_method = TREE_CHAIN (sub_interface_method))
6525 found = lookup_java_interface_method2 (interface,
6526 sub_interface_method);
6527 if (found && (found != sub_interface_method))
6529 parse_error_context
6530 (lookup_cl (sub_interface_method),
6531 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6532 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6533 lang_printable_name (found, 0),
6534 IDENTIFIER_POINTER
6535 (DECL_NAME (TYPE_NAME
6536 (DECL_CONTEXT (sub_interface_method)))),
6537 IDENTIFIER_POINTER
6538 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6544 /* Lookup methods in interfaces using their name and partial
6545 signature. Return a matching method only if their types differ. */
6547 static tree
6548 lookup_java_interface_method2 (class, method_decl)
6549 tree class, method_decl;
6551 int i, n;
6552 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6554 if (!basetype_vec)
6555 return NULL_TREE;
6557 n = TREE_VEC_LENGTH (basetype_vec);
6558 for (i = 0; i < n; i++)
6560 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6561 if ((BINFO_TYPE (vec_elt) != object_type_node)
6562 && (to_return =
6563 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6564 return to_return;
6566 for (i = 0; i < n; i++)
6568 to_return = lookup_java_interface_method2
6569 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6570 if (to_return)
6571 return to_return;
6574 return NULL_TREE;
6577 /* Lookup method using their name and partial signature. Return a
6578 matching method only if their types differ. */
6580 static tree
6581 lookup_java_method2 (clas, method_decl, do_interface)
6582 tree clas, method_decl;
6583 int do_interface;
6585 tree method, method_signature, method_name, method_type, name;
6587 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6588 name = DECL_NAME (method_decl);
6589 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6590 EXPR_WFL_NODE (name) : name);
6591 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6593 while (clas != NULL_TREE)
6595 for (method = TYPE_METHODS (clas);
6596 method != NULL_TREE; method = TREE_CHAIN (method))
6598 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6599 tree name = DECL_NAME (method);
6600 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6601 EXPR_WFL_NODE (name) : name) == method_name
6602 && method_sig == method_signature
6603 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6604 return method;
6606 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6608 return NULL_TREE;
6611 /* Return the line that matches DECL line number, and try its best to
6612 position the column number. Used during error reports. */
6614 static tree
6615 lookup_cl (decl)
6616 tree decl;
6618 static tree cl = NULL_TREE;
6619 char *line, *found;
6621 if (!decl)
6622 return NULL_TREE;
6624 if (cl == NULL_TREE)
6626 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6627 ggc_add_tree_root (&cl, 1);
6630 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6631 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6633 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
6634 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6636 found = strstr ((const char *)line,
6637 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6638 if (found)
6639 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6641 return cl;
6644 /* Look for a simple name in the single-type import list */
6646 static tree
6647 find_name_in_single_imports (name)
6648 tree name;
6650 tree node;
6652 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6653 if (TREE_VALUE (node) == name)
6654 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6656 return NULL_TREE;
6659 /* Process all single-type import. */
6661 static int
6662 process_imports ()
6664 tree import;
6665 int error_found;
6667 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6669 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6670 char *original_name;
6672 obstack_grow0 (&temporary_obstack,
6673 IDENTIFIER_POINTER (to_be_found),
6674 IDENTIFIER_LENGTH (to_be_found));
6675 original_name = obstack_finish (&temporary_obstack);
6677 /* Don't load twice something already defined. */
6678 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6679 continue;
6681 while (1)
6683 tree left;
6685 QUALIFIED_P (to_be_found) = 1;
6686 load_class (to_be_found, 0);
6687 error_found =
6688 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6690 /* We found it, we can bail out */
6691 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6692 break;
6694 /* We haven't found it. Maybe we're trying to access an
6695 inner class. The only way for us to know is to try again
6696 after having dropped a qualifier. If we can't break it further,
6697 we have an error. */
6698 if (breakdown_qualified (&left, NULL, to_be_found))
6699 break;
6701 to_be_found = left;
6703 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6705 parse_error_context (TREE_PURPOSE (import),
6706 "Class or interface `%s' not found in import",
6707 original_name);
6708 error_found = 1;
6711 obstack_free (&temporary_obstack, original_name);
6712 if (error_found)
6713 return 1;
6715 return 0;
6718 /* Possibly find and mark a class imported by a single-type import
6719 statement. */
6721 static void
6722 find_in_imports (enclosing_type, class_type)
6723 tree enclosing_type;
6724 tree class_type;
6726 tree import = (enclosing_type ? TYPE_IMPORT_LIST (enclosing_type) :
6727 ctxp->import_list);
6728 while (import)
6730 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6732 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6733 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6734 return;
6736 import = TREE_CHAIN (import);
6740 static int
6741 note_possible_classname (name, len)
6742 const char *name;
6743 int len;
6745 tree node;
6746 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6747 len = len - 5;
6748 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6749 len = len - 6;
6750 else
6751 return 0;
6752 node = ident_subst (name, len, "", '/', '.', "");
6753 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6754 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6755 return 1;
6758 /* Read a import directory, gathering potential match for further type
6759 references. Indifferently reads a filesystem or a ZIP archive
6760 directory. */
6762 static void
6763 read_import_dir (wfl)
6764 tree wfl;
6766 tree package_id = EXPR_WFL_NODE (wfl);
6767 const char *package_name = IDENTIFIER_POINTER (package_id);
6768 int package_length = IDENTIFIER_LENGTH (package_id);
6769 DIR *dirp = NULL;
6770 JCF *saved_jcf = current_jcf;
6772 int found = 0;
6773 int k;
6774 void *entry;
6775 struct buffer filename[1];
6778 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6779 return;
6780 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6782 BUFFER_INIT (filename);
6783 buffer_grow (filename, package_length + 100);
6785 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6787 const char *entry_name = jcf_path_name (entry);
6788 int entry_length = strlen (entry_name);
6789 if (jcf_path_is_zipfile (entry))
6791 ZipFile *zipf;
6792 buffer_grow (filename, entry_length);
6793 memcpy (filename->data, entry_name, entry_length - 1);
6794 filename->data[entry_length-1] = '\0';
6795 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6796 if (zipf == NULL)
6797 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6798 else
6800 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6801 BUFFER_RESET (filename);
6802 for (k = 0; k < package_length; k++)
6804 char ch = package_name[k];
6805 *filename->ptr++ = ch == '.' ? '/' : ch;
6807 *filename->ptr++ = '/';
6809 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6811 const char *current_entry = ZIPDIR_FILENAME (zipd);
6812 int current_entry_len = zipd->filename_length;
6814 if (current_entry_len >= BUFFER_LENGTH (filename)
6815 && strncmp (filename->data, current_entry,
6816 BUFFER_LENGTH (filename)) != 0)
6817 continue;
6818 found |= note_possible_classname (current_entry,
6819 current_entry_len);
6823 else
6825 BUFFER_RESET (filename);
6826 buffer_grow (filename, entry_length + package_length + 4);
6827 strcpy (filename->data, entry_name);
6828 filename->ptr = filename->data + entry_length;
6829 for (k = 0; k < package_length; k++)
6831 char ch = package_name[k];
6832 *filename->ptr++ = ch == '.' ? '/' : ch;
6834 *filename->ptr = '\0';
6836 dirp = opendir (filename->data);
6837 if (dirp == NULL)
6838 continue;
6839 *filename->ptr++ = '/';
6840 for (;;)
6842 int len;
6843 const char *d_name;
6844 struct dirent *direntp = readdir (dirp);
6845 if (!direntp)
6846 break;
6847 d_name = direntp->d_name;
6848 len = strlen (direntp->d_name);
6849 buffer_grow (filename, len+1);
6850 strcpy (filename->ptr, d_name);
6851 found |= note_possible_classname (filename->data + entry_length,
6852 package_length+len+1);
6854 if (dirp)
6855 closedir (dirp);
6859 free (filename->data);
6861 /* Here we should have a unified way of retrieving an entry, to be
6862 indexed. */
6863 if (!found)
6865 static int first = 1;
6866 if (first)
6868 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6869 java_error_count++;
6870 first = 0;
6872 else
6873 parse_error_context (wfl, "Package `%s' not found in import",
6874 package_name);
6875 current_jcf = saved_jcf;
6876 return;
6878 current_jcf = saved_jcf;
6881 /* Possibly find a type in the import on demands specified
6882 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6883 entire list, to detected potential double definitions. */
6885 static int
6886 find_in_imports_on_demand (enclosing_type, class_type)
6887 tree enclosing_type;
6888 tree class_type;
6890 tree import = (enclosing_type ? TYPE_IMPORT_DEMAND_LIST (enclosing_type) :
6891 ctxp->import_demand_list);
6892 tree node_to_use = NULL_TREE, cl = NULL_TREE;
6893 tree node;
6894 int seen_once = -1;
6896 while (import)
6898 const char *id_name;
6899 obstack_grow (&temporary_obstack,
6900 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6901 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6902 obstack_1grow (&temporary_obstack, '.');
6903 obstack_grow0 (&temporary_obstack,
6904 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6905 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6906 id_name = obstack_finish (&temporary_obstack);
6908 node = maybe_get_identifier (id_name);
6909 if (node && IS_A_CLASSFILE_NAME (node))
6911 if (seen_once < 0)
6913 cl = TREE_PURPOSE (import);
6914 seen_once = 1;
6915 node_to_use = node;
6917 else
6919 seen_once++;
6920 parse_error_context
6921 (TREE_PURPOSE (import),
6922 "Type `%s' also potentially defined in package `%s'",
6923 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6924 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6927 import = TREE_CHAIN (import);
6930 if (seen_once == 1)
6932 /* Setup lineno so that it refers to the line of the import (in
6933 case we parse a class file and encounter errors */
6934 tree decl;
6935 int saved_lineno = lineno;
6936 lineno = EXPR_WFL_LINENO (cl);
6937 TYPE_NAME (class_type) = node_to_use;
6938 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6939 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6940 /* If there is no DECL set for the class or if the class isn't
6941 loaded and not seen in source yet, the load */
6942 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6943 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6945 load_class (node_to_use, 0);
6946 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6948 lineno = saved_lineno;
6949 if (! INNER_CLASS_P (TREE_TYPE (decl)))
6950 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6951 else
6952 /* 6.6.1: Inner classes are subject to member access rules. */
6953 return 0;
6955 else
6956 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6959 /* Add package NAME to the list of package encountered so far. To
6960 speed up class lookup in do_resolve_class, we make sure a
6961 particular package is added only once. */
6963 static void
6964 register_package (name)
6965 tree name;
6967 static struct hash_table _pht, *pht = NULL;
6969 if (!pht)
6971 hash_table_init (&_pht, hash_newfunc,
6972 java_hash_hash_tree_node, java_hash_compare_tree_node);
6973 pht = &_pht;
6976 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6978 package_list = chainon (package_list, build_tree_list (name, NULL));
6979 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6983 static tree
6984 resolve_package (pkg, next)
6985 tree pkg, *next;
6987 tree current, acc;
6988 tree type_name = NULL_TREE;
6989 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6991 /* The trick is to determine when the package name stops and were
6992 the name of something contained in the package starts. Then we
6993 return a fully qualified name of what we want to get. */
6995 /* Do a quick search on well known package names */
6996 if (!strncmp (name, "java.lang.reflect", 17))
6998 *next =
6999 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
7000 type_name = lookup_package_type (name, 17);
7002 else if (!strncmp (name, "java.lang", 9))
7004 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
7005 type_name = lookup_package_type (name, 9);
7008 /* If we found something here, return */
7009 if (type_name)
7010 return type_name;
7012 *next = EXPR_WFL_QUALIFICATION (pkg);
7014 /* Try to progressively construct a type name */
7015 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7016 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
7017 current; current = TREE_CHAIN (current))
7019 /* If we don't have what we're expecting, exit now. TYPE_NAME
7020 will be null and the error caught later. */
7021 if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7022 break;
7023 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
7024 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
7026 type_name = acc;
7027 /* resolve_package should be used in a loop, hence we
7028 point at this one to naturally process the next one at
7029 the next iteration. */
7030 *next = current;
7031 break;
7034 return type_name;
7037 static tree
7038 lookup_package_type (name, from)
7039 const char *name;
7040 int from;
7042 char subname [128];
7043 const char *sub = &name[from+1];
7044 while (*sub != '.' && *sub)
7045 sub++;
7046 strncpy (subname, name, sub-name);
7047 subname [sub-name] = '\0';
7048 return get_identifier (subname);
7051 /* Check accessibility of inner classes according to member access rules.
7052 DECL is the inner class, ENCLOSING_DECL is the class from which the
7053 access is being attempted. */
7055 static void
7056 check_inner_class_access (decl, enclosing_decl, cl)
7057 tree decl, enclosing_decl, cl;
7059 const char *access;
7060 tree enclosing_decl_type;
7062 /* We don't issue an error message when CL is null. CL can be null
7063 as a result of processing a JDEP crafted by source_start_java_method
7064 for the purpose of patching its parm decl. But the error would
7065 have been already trapped when fixing the method's signature.
7066 DECL can also be NULL in case of earlier errors. */
7067 if (!decl || !cl)
7068 return;
7070 enclosing_decl_type = TREE_TYPE (enclosing_decl);
7072 if (CLASS_PRIVATE (decl))
7074 /* Access is permitted only within the body of the top-level
7075 class in which DECL is declared. */
7076 tree top_level = decl;
7077 while (DECL_CONTEXT (top_level))
7078 top_level = DECL_CONTEXT (top_level);
7079 while (DECL_CONTEXT (enclosing_decl))
7080 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7081 if (top_level == enclosing_decl)
7082 return;
7083 access = "private";
7085 else if (CLASS_PROTECTED (decl))
7087 tree decl_context;
7088 /* Access is permitted from within the same package... */
7089 if (in_same_package (decl, enclosing_decl))
7090 return;
7092 /* ... or from within the body of a subtype of the context in which
7093 DECL is declared. */
7094 decl_context = DECL_CONTEXT (decl);
7095 while (enclosing_decl)
7097 if (CLASS_INTERFACE (decl))
7099 if (interface_of_p (TREE_TYPE (decl_context),
7100 enclosing_decl_type))
7101 return;
7103 else
7105 /* Eww. The order of the arguments is different!! */
7106 if (inherits_from_p (enclosing_decl_type,
7107 TREE_TYPE (decl_context)))
7108 return;
7110 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7112 access = "protected";
7114 else if (! CLASS_PUBLIC (decl))
7116 /* Access is permitted only from within the same package as DECL. */
7117 if (in_same_package (decl, enclosing_decl))
7118 return;
7119 access = "non-public";
7121 else
7122 /* Class is public. */
7123 return;
7125 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7126 (CLASS_INTERFACE (decl) ? "interface" : "class"),
7127 lang_printable_name (decl, 0), access);
7130 /* Accessibility check for top-level classes. If CLASS_NAME is in a foreign
7131 package, it must be PUBLIC. Return 0 if no access violations were found,
7132 1 otherwise. */
7134 static int
7135 check_pkg_class_access (class_name, cl)
7136 tree class_name;
7137 tree cl;
7139 tree type;
7141 if (!IDENTIFIER_CLASS_VALUE (class_name))
7142 return 0;
7144 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7145 return 0;
7147 if (!CLASS_PUBLIC (TYPE_NAME (type)))
7149 /* Access to a private class within the same package is
7150 allowed. */
7151 tree l, r;
7152 breakdown_qualified (&l, &r, class_name);
7153 if (!QUALIFIED_P (class_name) && !ctxp->package)
7154 /* Both in the empty package. */
7155 return 0;
7156 if (l == ctxp->package)
7157 /* Both in the same package. */
7158 return 0;
7160 parse_error_context
7161 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
7162 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7163 IDENTIFIER_POINTER (class_name));
7164 return 1;
7166 return 0;
7169 /* Local variable declaration. */
7171 static void
7172 declare_local_variables (modifier, type, vlist)
7173 int modifier;
7174 tree type;
7175 tree vlist;
7177 tree decl, current, saved_type;
7178 tree type_wfl = NULL_TREE;
7179 int must_chain = 0;
7180 int final_p = 0;
7182 /* Push a new block if statements were seen between the last time we
7183 pushed a block and now. Keep a count of blocks to close */
7184 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7186 tree b = enter_block ();
7187 BLOCK_IS_IMPLICIT (b) = 1;
7190 if (modifier)
7192 int i;
7193 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
7194 if (modifier == ACC_FINAL)
7195 final_p = 1;
7196 else
7198 parse_error_context
7199 (ctxp->modifier_ctx [i],
7200 "Only `final' is allowed as a local variables modifier");
7201 return;
7205 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7206 hold the TYPE value if a new incomplete has to be created (as
7207 opposed to being found already existing and reused). */
7208 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7210 /* If TYPE is fully resolved and we don't have a reference, make one */
7211 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7213 /* Go through all the declared variables */
7214 for (current = vlist, saved_type = type; current;
7215 current = TREE_CHAIN (current), type = saved_type)
7217 tree other, real_type;
7218 tree wfl = TREE_PURPOSE (current);
7219 tree name = EXPR_WFL_NODE (wfl);
7220 tree init = TREE_VALUE (current);
7222 /* Process NAME, as it may specify extra dimension(s) for it */
7223 type = build_array_from_name (type, type_wfl, name, &name);
7225 /* Variable redefinition check */
7226 if ((other = lookup_name_in_blocks (name)))
7228 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7229 DECL_SOURCE_LINE (other));
7230 continue;
7233 /* Type adjustment. We may have just readjusted TYPE because
7234 the variable specified more dimensions. Make sure we have
7235 a reference if we can and don't have one already. */
7236 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7238 real_type = GET_REAL_TYPE (type);
7239 /* Never layout this decl. This will be done when its scope
7240 will be entered */
7241 decl = build_decl (VAR_DECL, name, real_type);
7242 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7243 LOCAL_FINAL (decl) = final_p;
7244 BLOCK_CHAIN_DECL (decl);
7246 /* If doing xreferencing, replace the line number with the WFL
7247 compound value */
7248 if (flag_emit_xref)
7249 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7251 /* Don't try to use an INIT statement when an error was found */
7252 if (init && java_error_count)
7253 init = NULL_TREE;
7255 /* Add the initialization function to the current function's code */
7256 if (init)
7258 /* Name might have been readjusted */
7259 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7260 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7261 java_method_add_stmt (current_function_decl,
7262 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7263 init));
7266 /* Setup dependency the type of the decl */
7267 if (must_chain)
7269 jdep *dep;
7270 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7271 dep = CLASSD_LAST (ctxp->classd_list);
7272 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7275 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7278 /* Called during parsing. Build decls from argument list. */
7280 static void
7281 source_start_java_method (fndecl)
7282 tree fndecl;
7284 tree tem;
7285 tree parm_decl;
7286 int i;
7288 if (!fndecl)
7289 return;
7291 current_function_decl = fndecl;
7293 /* New scope for the function */
7294 enter_block ();
7295 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7296 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7298 tree type = TREE_VALUE (tem);
7299 tree name = TREE_PURPOSE (tem);
7301 /* If type is incomplete. Create an incomplete decl and ask for
7302 the decl to be patched later */
7303 if (INCOMPLETE_TYPE_P (type))
7305 jdep *jdep;
7306 tree real_type = GET_REAL_TYPE (type);
7307 parm_decl = build_decl (PARM_DECL, name, real_type);
7308 type = obtain_incomplete_type (type);
7309 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7310 jdep = CLASSD_LAST (ctxp->classd_list);
7311 JDEP_MISC (jdep) = name;
7312 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7314 else
7315 parm_decl = build_decl (PARM_DECL, name, type);
7317 /* Remember if a local variable was declared final (via its
7318 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7319 if (ARG_FINAL_P (tem))
7321 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7322 LOCAL_FINAL (parm_decl) = 1;
7325 BLOCK_CHAIN_DECL (parm_decl);
7327 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7328 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7329 nreverse (tem);
7330 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7331 DECL_MAX_LOCALS (current_function_decl) = i;
7334 /* Called during parsing. Creates an artificial method declaration. */
7336 static tree
7337 create_artificial_method (class, flags, type, name, args)
7338 tree class;
7339 int flags;
7340 tree type, name, args;
7342 tree mdecl;
7344 java_parser_context_save_global ();
7345 lineno = 0;
7346 mdecl = make_node (FUNCTION_TYPE);
7347 TREE_TYPE (mdecl) = type;
7348 TYPE_ARG_TYPES (mdecl) = args;
7349 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7350 java_parser_context_restore_global ();
7351 DECL_ARTIFICIAL (mdecl) = 1;
7352 return mdecl;
7355 /* Starts the body if an artifical method. */
7357 static void
7358 start_artificial_method_body (mdecl)
7359 tree mdecl;
7361 DECL_SOURCE_LINE (mdecl) = 1;
7362 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7363 source_start_java_method (mdecl);
7364 enter_block ();
7367 static void
7368 end_artificial_method_body (mdecl)
7369 tree mdecl;
7371 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7372 It has to be evaluated first. (if mdecl is current_function_decl,
7373 we have an undefined behavior if no temporary variable is used.) */
7374 tree b = exit_block ();
7375 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7376 exit_block ();
7379 /* Terminate a function and expand its body. */
7381 static void
7382 source_end_java_method ()
7384 tree fndecl = current_function_decl;
7386 if (!fndecl)
7387 return;
7389 java_parser_context_save_global ();
7390 lineno = ctxp->last_ccb_indent1;
7392 /* Turn function bodies with only a NOP expr null, so they don't get
7393 generated at all and we won't get warnings when using the -W
7394 -Wall flags. */
7395 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7396 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7398 /* Generate function's code */
7399 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7400 && ! flag_emit_class_files
7401 && ! flag_emit_xref)
7402 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7404 /* pop out of its parameters */
7405 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7406 poplevel (1, 0, 1);
7407 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7409 /* Generate rtl for function exit. */
7410 if (! flag_emit_class_files && ! flag_emit_xref)
7412 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7413 expand_function_end (input_filename, lineno, 0);
7415 /* Run the optimizers and output assembler code for this function. */
7416 rest_of_compilation (fndecl);
7419 current_function_decl = NULL_TREE;
7420 java_parser_context_restore_global ();
7423 /* Record EXPR in the current function block. Complements compound
7424 expression second operand if necessary. */
7426 tree
7427 java_method_add_stmt (fndecl, expr)
7428 tree fndecl, expr;
7430 if (!GET_CURRENT_BLOCK (fndecl))
7431 return NULL_TREE;
7432 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7435 static tree
7436 add_stmt_to_block (b, type, stmt)
7437 tree b, type, stmt;
7439 tree body = BLOCK_EXPR_BODY (b), c;
7441 if (java_error_count)
7442 return body;
7444 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7445 return body;
7447 BLOCK_EXPR_BODY (b) = c;
7448 TREE_SIDE_EFFECTS (c) = 1;
7449 return c;
7452 /* Add STMT to EXISTING if possible, otherwise create a new
7453 COMPOUND_EXPR and add STMT to it. */
7455 static tree
7456 add_stmt_to_compound (existing, type, stmt)
7457 tree existing, type, stmt;
7459 if (existing)
7460 return build (COMPOUND_EXPR, type, existing, stmt);
7461 else
7462 return stmt;
7465 void java_layout_seen_class_methods ()
7467 tree previous_list = all_class_list;
7468 tree end = NULL_TREE;
7469 tree current;
7471 while (1)
7473 for (current = previous_list;
7474 current != end; current = TREE_CHAIN (current))
7475 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7477 if (previous_list != all_class_list)
7479 end = previous_list;
7480 previous_list = all_class_list;
7482 else
7483 break;
7487 void
7488 java_reorder_fields ()
7490 static tree stop_reordering = NULL_TREE;
7491 static int initialized_p;
7492 tree current;
7494 /* Register STOP_REORDERING with the garbage collector. */
7495 if (!initialized_p)
7497 ggc_add_tree_root (&stop_reordering, 1);
7498 initialized_p = 1;
7501 for (current = gclass_list; current; current = TREE_CHAIN (current))
7503 current_class = TREE_TYPE (TREE_VALUE (current));
7505 if (current_class == stop_reordering)
7506 break;
7508 /* Reverse the fields, but leave the dummy field in front.
7509 Fields are already ordered for Object and Class */
7510 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7511 && current_class != class_type_node)
7513 /* If the dummy field is there, reverse the right fields and
7514 just layout the type for proper fields offset */
7515 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7517 tree fields = TYPE_FIELDS (current_class);
7518 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7519 TYPE_SIZE (current_class) = NULL_TREE;
7521 /* We don't have a dummy field, we need to layout the class,
7522 after having reversed the fields */
7523 else
7525 TYPE_FIELDS (current_class) =
7526 nreverse (TYPE_FIELDS (current_class));
7527 TYPE_SIZE (current_class) = NULL_TREE;
7531 /* There are cases were gclass_list will be empty. */
7532 if (gclass_list)
7533 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7536 /* Layout the methods of all classes loaded in one way or another.
7537 Check methods of source parsed classes. Then reorder the
7538 fields and layout the classes or the type of all source parsed
7539 classes */
7541 void
7542 java_layout_classes ()
7544 tree current;
7545 int save_error_count = java_error_count;
7547 /* Layout the methods of all classes seen so far */
7548 java_layout_seen_class_methods ();
7549 java_parse_abort_on_error ();
7550 all_class_list = NULL_TREE;
7552 /* Then check the methods of all parsed classes */
7553 for (current = gclass_list; current; current = TREE_CHAIN (current))
7554 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7555 java_check_methods (TREE_VALUE (current));
7556 java_parse_abort_on_error ();
7558 for (current = gclass_list; current; current = TREE_CHAIN (current))
7560 current_class = TREE_TYPE (TREE_VALUE (current));
7561 layout_class (current_class);
7563 /* Error reported by the caller */
7564 if (java_error_count)
7565 return;
7568 /* We might have reloaded classes durign the process of laying out
7569 classes for code generation. We must layout the methods of those
7570 late additions, as constructor checks might use them */
7571 java_layout_seen_class_methods ();
7572 java_parse_abort_on_error ();
7575 /* Expand methods in the current set of classes rememebered for
7576 generation. */
7578 static void
7579 java_complete_expand_classes ()
7581 tree current;
7583 do_not_fold = flag_emit_xref;
7585 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7586 if (!INNER_CLASS_DECL_P (current))
7587 java_complete_expand_class (current);
7590 /* Expand the methods found in OUTER, starting first by OUTER's inner
7591 classes, if any. */
7593 static void
7594 java_complete_expand_class (outer)
7595 tree outer;
7597 tree inner_list;
7599 set_nested_class_simple_name_value (outer, 1); /* Set */
7601 /* We need to go after all inner classes and start expanding them,
7602 starting with most nested ones. We have to do that because nested
7603 classes might add functions to outer classes */
7605 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7606 inner_list; inner_list = TREE_CHAIN (inner_list))
7607 java_complete_expand_class (TREE_PURPOSE (inner_list));
7609 java_complete_expand_methods (outer);
7610 set_nested_class_simple_name_value (outer, 0); /* Reset */
7613 /* Expand methods registered in CLASS_DECL. The general idea is that
7614 we expand regular methods first. This allows us get an estimate on
7615 how outer context local alias fields are really used so we can add
7616 to the constructor just enough code to initialize them properly (it
7617 also lets us generate finit$ correctly.) Then we expand the
7618 constructors and then <clinit>. */
7620 static void
7621 java_complete_expand_methods (class_decl)
7622 tree class_decl;
7624 tree clinit, decl, first_decl;
7626 current_class = TREE_TYPE (class_decl);
7628 /* Find whether the class has final variables */
7629 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7630 if (FIELD_FINAL (decl))
7632 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7633 break;
7636 /* Initialize a new constant pool */
7637 init_outgoing_cpool ();
7639 /* Pre-expand <clinit> to figure whether we really need it or
7640 not. If we do need it, we pre-expand the static fields so they're
7641 ready to be used somewhere else. <clinit> will be fully expanded
7642 after we processed the constructors. */
7643 first_decl = TYPE_METHODS (current_class);
7644 clinit = maybe_generate_pre_expand_clinit (current_class);
7646 /* Then generate finit$ (if we need to) because constructors will
7647 try to use it.*/
7648 if (TYPE_FINIT_STMT_LIST (current_class))
7649 java_complete_expand_method (generate_finit (current_class));
7651 /* Then generate instinit$ (if we need to) because constructors will
7652 try to use it. */
7653 if (TYPE_II_STMT_LIST (current_class))
7654 java_complete_expand_method (generate_instinit (current_class));
7656 /* Now do the constructors */
7657 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7659 int no_body;
7661 if (!DECL_CONSTRUCTOR_P (decl))
7662 continue;
7664 no_body = !DECL_FUNCTION_BODY (decl);
7665 /* Don't generate debug info on line zero when expanding a
7666 generated constructor. */
7667 if (no_body)
7668 restore_line_number_status (1);
7670 /* Reset the final local variable assignment flags */
7671 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7672 reset_final_variable_local_assignment_flag (current_class);
7674 java_complete_expand_method (decl);
7676 /* Check for missed out final variable assignment */
7677 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7678 check_final_variable_local_assignment_flag (current_class, decl);
7680 if (no_body)
7681 restore_line_number_status (0);
7684 /* First, do the ordinary methods. */
7685 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7687 /* Ctors aren't part of this batch. */
7688 if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7689 continue;
7691 /* Skip abstract or native methods -- but do handle native
7692 methods when generating JNI stubs. */
7693 if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7695 DECL_FUNCTION_BODY (decl) = NULL_TREE;
7696 continue;
7699 if (METHOD_NATIVE (decl))
7701 tree body = build_jni_stub (decl);
7702 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7705 java_complete_expand_method (decl);
7708 /* If there is indeed a <clinit>, fully expand it now */
7709 if (clinit)
7711 /* Reset the final local variable assignment flags */
7712 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7713 reset_static_final_variable_assignment_flag (current_class);
7714 /* Prevent the use of `this' inside <clinit> */
7715 ctxp->explicit_constructor_p = 1;
7716 java_complete_expand_method (clinit);
7717 ctxp->explicit_constructor_p = 0;
7718 /* Check for missed out static final variable assignment */
7719 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7720 && !CLASS_INTERFACE (class_decl))
7721 check_static_final_variable_assignment_flag (current_class);
7724 /* We might have generated a class$ that we now want to expand */
7725 if (TYPE_DOT_CLASS (current_class))
7726 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7728 /* Now verify constructor circularity (stop after the first one we
7729 prove wrong.) */
7730 if (!CLASS_INTERFACE (class_decl))
7731 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7732 if (DECL_CONSTRUCTOR_P (decl)
7733 && verify_constructor_circularity (decl, decl))
7734 break;
7736 /* Final check on the initialization of final variables. */
7737 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7739 check_final_variable_global_assignment_flag (current_class);
7740 /* If we have an interface, check for uninitialized fields. */
7741 if (CLASS_INTERFACE (class_decl))
7742 check_static_final_variable_assignment_flag (current_class);
7745 /* Save the constant pool. We'll need to restore it later. */
7746 TYPE_CPOOL (current_class) = outgoing_cpool;
7749 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7750 safely used in some other methods/constructors. */
7752 static tree
7753 maybe_generate_pre_expand_clinit (class_type)
7754 tree class_type;
7756 tree current, mdecl;
7758 if (!TYPE_CLINIT_STMT_LIST (class_type))
7759 return NULL_TREE;
7761 /* Go through all static fields and pre expand them */
7762 for (current = TYPE_FIELDS (class_type); current;
7763 current = TREE_CHAIN (current))
7764 if (FIELD_STATIC (current))
7765 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7767 /* Then build the <clinit> method */
7768 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7769 clinit_identifier_node, end_params_node);
7770 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7771 mdecl, NULL_TREE);
7772 start_artificial_method_body (mdecl);
7774 /* We process the list of assignment we produced as the result of
7775 the declaration of initialized static field and add them as
7776 statement to the <clinit> method. */
7777 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7778 current = TREE_CHAIN (current))
7780 tree stmt = current;
7781 /* We build the assignment expression that will initialize the
7782 field to its value. There are strict rules on static
7783 initializers (8.5). FIXME */
7784 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7785 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7786 java_method_add_stmt (mdecl, stmt);
7789 end_artificial_method_body (mdecl);
7791 /* Now we want to place <clinit> as the last method (because we need
7792 it at least for interface so that it doesn't interfere with the
7793 dispatch table based lookup. */
7794 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7796 current = TREE_CHAIN (TYPE_METHODS (class_type));
7797 TYPE_METHODS (class_type) = current;
7799 while (TREE_CHAIN (current))
7800 current = TREE_CHAIN (current);
7802 TREE_CHAIN (current) = mdecl;
7803 TREE_CHAIN (mdecl) = NULL_TREE;
7806 return mdecl;
7809 /* Analyzes a method body and look for something that isn't a
7810 MODIFY_EXPR with a constant value. */
7812 static int
7813 analyze_clinit_body (bbody)
7814 tree bbody;
7816 while (bbody)
7817 switch (TREE_CODE (bbody))
7819 case BLOCK:
7820 bbody = BLOCK_EXPR_BODY (bbody);
7821 break;
7823 case EXPR_WITH_FILE_LOCATION:
7824 bbody = EXPR_WFL_NODE (bbody);
7825 break;
7827 case COMPOUND_EXPR:
7828 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7829 return 1;
7830 bbody = TREE_OPERAND (bbody, 1);
7831 break;
7833 case MODIFY_EXPR:
7834 /* If we're generating to class file and we're dealing with an
7835 array initialization, we return 1 to keep <clinit> */
7836 if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
7837 && flag_emit_class_files)
7838 return 1;
7839 /* Return 0 if the operand is constant, 1 otherwise. */
7840 return ! TREE_CONSTANT (TREE_OPERAND (bbody, 1));
7842 default:
7843 return 1;
7845 return 0;
7849 /* See whether we could get rid of <clinit>. Criteria are: all static
7850 final fields have constant initial values and the body of <clinit>
7851 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7853 static int
7854 maybe_yank_clinit (mdecl)
7855 tree mdecl;
7857 tree type, current;
7858 tree fbody, bbody;
7859 int found = 0;
7861 if (!DECL_CLINIT_P (mdecl))
7862 return 0;
7864 /* If the body isn't empty, then we keep <clinit>. Note that if
7865 we're emitting classfiles, this isn't enough not to rule it
7866 out. */
7867 fbody = DECL_FUNCTION_BODY (mdecl);
7868 bbody = BLOCK_EXPR_BODY (fbody);
7869 if (bbody && bbody != error_mark_node)
7870 bbody = BLOCK_EXPR_BODY (bbody);
7871 else
7872 return 0;
7873 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7874 return 0;
7876 type = DECL_CONTEXT (mdecl);
7877 current = TYPE_FIELDS (type);
7879 for (current = (current ? TREE_CHAIN (current) : current);
7880 current; current = TREE_CHAIN (current))
7882 tree f_init;
7884 /* We're not interested in non static field */
7885 if (!FIELD_STATIC (current))
7886 continue;
7888 /* nor in fields with initializers. */
7889 f_init = DECL_INITIAL (current);
7891 if (f_init == NULL_TREE)
7892 continue;
7894 /* Anything that isn't String or a basic type is ruled out -- or
7895 if we know how to deal with it (when doing things natively) we
7896 should generated an empty <clinit> so that SUID are computed
7897 correctly. */
7898 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7899 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7900 break;
7902 if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
7903 break;
7906 /* Now we analyze the method body and look for something that
7907 isn't a MODIFY_EXPR */
7908 if (bbody == empty_stmt_node)
7909 found = 0;
7910 else
7911 found = analyze_clinit_body (bbody);
7913 if (current || found)
7914 return 0;
7916 /* Get rid of <clinit> in the class' list of methods */
7917 if (TYPE_METHODS (type) == mdecl)
7918 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7919 else
7920 for (current = TYPE_METHODS (type); current;
7921 current = TREE_CHAIN (current))
7922 if (TREE_CHAIN (current) == mdecl)
7924 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7925 break;
7928 return 1;
7931 /* Install the argument from MDECL. Suitable to completion and
7932 expansion of mdecl's body. */
7934 static void
7935 start_complete_expand_method (mdecl)
7936 tree mdecl;
7938 tree tem, *ptr;
7940 pushlevel (1); /* Prepare for a parameter push */
7941 ptr = &DECL_ARGUMENTS (mdecl);
7942 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7944 while (tem)
7946 tree next = TREE_CHAIN (tem);
7947 tree type = TREE_TYPE (tem);
7948 if (PROMOTE_PROTOTYPES
7949 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7950 && INTEGRAL_TYPE_P (type))
7951 type = integer_type_node;
7952 DECL_ARG_TYPE (tem) = type;
7953 layout_decl (tem, 0);
7954 pushdecl (tem);
7955 *ptr = tem;
7956 ptr = &TREE_CHAIN (tem);
7957 tem = next;
7959 *ptr = NULL_TREE;
7960 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7961 lineno = DECL_SOURCE_LINE_FIRST (mdecl);
7962 build_result_decl (mdecl);
7966 /* Complete and expand a method. */
7968 static void
7969 java_complete_expand_method (mdecl)
7970 tree mdecl;
7972 tree fbody, block_body, exception_copy;
7974 current_function_decl = mdecl;
7975 /* Fix constructors before expanding them */
7976 if (DECL_CONSTRUCTOR_P (mdecl))
7977 fix_constructors (mdecl);
7979 /* Expand functions that have a body */
7980 if (!DECL_FUNCTION_BODY (mdecl))
7981 return;
7983 fbody = DECL_FUNCTION_BODY (mdecl);
7984 block_body = BLOCK_EXPR_BODY (fbody);
7985 exception_copy = NULL_TREE;
7987 current_function_decl = mdecl;
7989 if (! quiet_flag)
7990 fprintf (stderr, " [%s.",
7991 lang_printable_name (DECL_CONTEXT (mdecl), 0));
7992 announce_function (mdecl);
7993 if (! quiet_flag)
7994 fprintf (stderr, "]");
7996 /* Prepare the function for tree completion */
7997 start_complete_expand_method (mdecl);
7999 /* Install the current this */
8000 current_this = (!METHOD_STATIC (mdecl) ?
8001 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8003 /* Purge the `throws' list of unchecked exceptions (we save a copy
8004 of the list and re-install it later.) */
8005 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8006 purge_unchecked_exceptions (mdecl);
8008 /* Install exceptions thrown with `throws' */
8009 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8011 if (block_body != NULL_TREE)
8013 block_body = java_complete_tree (block_body);
8015 /* Before we check initialization, attached all class initialization
8016 variable to the block_body */
8017 hash_traverse (&DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8018 attach_init_test_initialization_flags, block_body);
8020 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
8022 unsigned int state = check_for_initialization (block_body);
8024 /* Go through all the flags marking the initialization of
8025 static variables and see whether they're definitively
8026 assigned, in which case the type is remembered as
8027 definitively initialized in MDECL. */
8028 if (STATIC_CLASS_INIT_OPT_P ())
8030 hash_traverse (&DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8031 attach_initialized_static_class, (PTR)&state);
8033 /* Always register the context as properly initialized in
8034 MDECL. This used with caution helps removing extra
8035 initialization of self. */
8036 if (METHOD_STATIC (mdecl))
8037 hash_lookup (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8038 (hash_table_key) DECL_CONTEXT (mdecl),
8039 TRUE, NULL);
8042 ctxp->explicit_constructor_p = 0;
8045 BLOCK_EXPR_BODY (fbody) = block_body;
8047 /* If we saw a return but couldn't evaluate it properly, we'll have
8048 an error_mark_node here. */
8049 if (block_body != error_mark_node
8050 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8051 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
8052 && !flag_emit_xref)
8053 missing_return_error (current_function_decl);
8055 /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8056 maybe_yank_clinit (mdecl);
8058 /* Pop the current level, with special measures if we found errors. */
8059 if (java_error_count)
8060 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8061 poplevel (1, 0, 1);
8063 /* Pop the exceptions and sanity check */
8064 POP_EXCEPTIONS();
8065 if (currently_caught_type_list)
8066 abort ();
8068 /* Restore the copy of the list of exceptions if emitting xrefs. */
8069 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8072 /* For with each class for which there's code to generate. */
8074 static void
8075 java_expand_method_bodies (class)
8076 tree class;
8078 tree decl;
8079 for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8081 if (!DECL_FUNCTION_BODY (decl))
8082 continue;
8084 current_function_decl = decl;
8086 /* It's time to assign the variable flagging static class
8087 initialization based on which classes invoked static methods
8088 are definitely initializing. This should be flagged. */
8089 if (STATIC_CLASS_INIT_OPT_P ())
8090 hash_traverse (&DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl),
8091 adjust_init_test_initialization, NULL);
8093 /* Prepare the function for RTL expansion */
8094 start_complete_expand_method (decl);
8096 /* Expand function start, generate initialization flag
8097 assignment, and handle synchronized methods. */
8098 complete_start_java_method (decl);
8100 /* Expand the rest of the function body and terminate
8101 expansion. */
8102 source_end_java_method ();
8108 /* This section of the code deals with accessing enclosing context
8109 fields either directly by using the relevant access to this$<n> or
8110 by invoking an access method crafted for that purpose. */
8112 /* Build the necessary access from an inner class to an outer
8113 class. This routine could be optimized to cache previous result
8114 (decl, current_class and returned access). When an access method
8115 needs to be generated, it always takes the form of a read. It might
8116 be later turned into a write by calling outer_field_access_fix. */
8118 static tree
8119 build_outer_field_access (id, decl)
8120 tree id, decl;
8122 tree access = NULL_TREE;
8123 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8124 tree decl_ctx = DECL_CONTEXT (decl);
8126 /* If the immediate enclosing context of the current class is the
8127 field decl's class or inherits from it; build the access as
8128 `this$<n>.<field>'. Note that we will break the `private' barrier
8129 if we're not emitting bytecodes. */
8130 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8131 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8133 tree thisn = build_current_thisn (current_class);
8134 access = make_qualified_primary (build_wfl_node (thisn),
8135 id, EXPR_WFL_LINECOL (id));
8137 /* Otherwise, generate access methods to outer this and access the
8138 field (either using an access method or by direct access.) */
8139 else
8141 int lc = EXPR_WFL_LINECOL (id);
8143 /* Now we chain the required number of calls to the access$0 to
8144 get a hold to the enclosing instance we need, and then we
8145 build the field access. */
8146 access = build_access_to_thisn (current_class, decl_ctx, lc);
8148 /* If the field is private and we're generating bytecode, then
8149 we generate an access method */
8150 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8152 tree name = build_outer_field_access_methods (decl);
8153 access = build_outer_field_access_expr (lc, decl_ctx,
8154 name, access, NULL_TREE);
8156 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8157 Once again we break the `private' access rule from a foreign
8158 class. */
8159 else
8160 access = make_qualified_primary (access, id, lc);
8162 return resolve_expression_name (access, NULL);
8165 /* Return a non zero value if NODE describes an outer field inner
8166 access. */
8168 static int
8169 outer_field_access_p (type, decl)
8170 tree type, decl;
8172 if (!INNER_CLASS_TYPE_P (type)
8173 || TREE_CODE (decl) != FIELD_DECL
8174 || DECL_CONTEXT (decl) == type)
8175 return 0;
8177 /* If the inner class extends the declaration context of the field
8178 we're try to acces, then this isn't an outer field access */
8179 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8180 return 0;
8182 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8183 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8185 if (type == DECL_CONTEXT (decl))
8186 return 1;
8188 if (!DECL_CONTEXT (TYPE_NAME (type)))
8190 /* Before we give up, see whether the field is inherited from
8191 the enclosing context we're considering. */
8192 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8193 return 1;
8194 break;
8198 return 0;
8201 /* Return a non zero value if NODE represents an outer field inner
8202 access that was been already expanded. As a side effect, it returns
8203 the name of the field being accessed and the argument passed to the
8204 access function, suitable for a regeneration of the access method
8205 call if necessary. */
8207 static int
8208 outer_field_expanded_access_p (node, name, arg_type, arg)
8209 tree node, *name, *arg_type, *arg;
8211 int identified = 0;
8213 if (TREE_CODE (node) != CALL_EXPR)
8214 return 0;
8216 /* Well, gcj generates slightly different tree nodes when compiling
8217 to native or bytecodes. It's the case for function calls. */
8219 if (flag_emit_class_files
8220 && TREE_CODE (node) == CALL_EXPR
8221 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8222 identified = 1;
8223 else if (!flag_emit_class_files)
8225 node = TREE_OPERAND (node, 0);
8227 if (node && TREE_OPERAND (node, 0)
8228 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8230 node = TREE_OPERAND (node, 0);
8231 if (TREE_OPERAND (node, 0)
8232 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8233 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8234 (DECL_NAME (TREE_OPERAND (node, 0)))))
8235 identified = 1;
8239 if (identified && name && arg_type && arg)
8241 tree argument = TREE_OPERAND (node, 1);
8242 *name = DECL_NAME (TREE_OPERAND (node, 0));
8243 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8244 *arg = TREE_VALUE (argument);
8246 return identified;
8249 /* Detect in NODE an outer field read access from an inner class and
8250 transform it into a write with RHS as an argument. This function is
8251 called from the java_complete_lhs when an assignment to a LHS can
8252 be identified. */
8254 static tree
8255 outer_field_access_fix (wfl, node, rhs)
8256 tree wfl, node, rhs;
8258 tree name, arg_type, arg;
8260 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8262 /* At any rate, check whether we're trying to assign a value to
8263 a final. */
8264 tree accessed = (JDECL_P (node) ? node :
8265 (TREE_CODE (node) == COMPONENT_REF ?
8266 TREE_OPERAND (node, 1) : node));
8267 if (check_final_assignment (accessed, wfl))
8268 return error_mark_node;
8270 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8271 arg_type, name, arg, rhs);
8272 return java_complete_tree (node);
8274 return NULL_TREE;
8277 /* Construct the expression that calls an access method:
8278 <type>.access$<n>(<arg1> [, <arg2>]);
8280 ARG2 can be NULL and will be omitted in that case. It will denote a
8281 read access. */
8283 static tree
8284 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
8285 int lc;
8286 tree type, access_method_name, arg1, arg2;
8288 tree args, cn, access;
8290 args = arg1 ? arg1 :
8291 build_wfl_node (build_current_thisn (current_class));
8292 args = build_tree_list (NULL_TREE, args);
8294 if (arg2)
8295 args = tree_cons (NULL_TREE, arg2, args);
8297 access = build_method_invocation (build_wfl_node (access_method_name), args);
8298 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8299 return make_qualified_primary (cn, access, lc);
8302 static tree
8303 build_new_access_id ()
8305 static int access_n_counter = 1;
8306 char buffer [128];
8308 sprintf (buffer, "access$%d", access_n_counter++);
8309 return get_identifier (buffer);
8312 /* Create the static access functions for the outer field DECL. We define a
8313 read:
8314 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8315 return inst$.field;
8317 and a write access:
8318 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8319 TREE_TYPE (<field>) value$) {
8320 return inst$.field = value$;
8322 We should have a usage flags on the DECL so we can lazily turn the ones
8323 we're using for code generation. FIXME.
8326 static tree
8327 build_outer_field_access_methods (decl)
8328 tree decl;
8330 tree id, args, stmt, mdecl;
8332 if (FIELD_INNER_ACCESS_P (decl))
8333 return FIELD_INNER_ACCESS (decl);
8335 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8337 /* Create the identifier and a function named after it. */
8338 id = build_new_access_id ();
8340 /* The identifier is marked as bearing the name of a generated write
8341 access function for outer field accessed from inner classes. */
8342 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8344 /* Create the read access */
8345 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8346 TREE_CHAIN (args) = end_params_node;
8347 stmt = make_qualified_primary (build_wfl_node (inst_id),
8348 build_wfl_node (DECL_NAME (decl)), 0);
8349 stmt = build_return (0, stmt);
8350 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8351 TREE_TYPE (decl), id, args, stmt);
8352 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8354 /* Create the write access method. No write access for final variable */
8355 if (!FIELD_FINAL (decl))
8357 args = build_tree_list (inst_id,
8358 build_pointer_type (DECL_CONTEXT (decl)));
8359 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8360 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8361 stmt = make_qualified_primary (build_wfl_node (inst_id),
8362 build_wfl_node (DECL_NAME (decl)), 0);
8363 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8364 build_wfl_node (wpv_id)));
8365 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8366 TREE_TYPE (decl), id,
8367 args, stmt);
8369 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8371 /* Return the access name */
8372 return FIELD_INNER_ACCESS (decl) = id;
8375 /* Build an field access method NAME. */
8377 static tree
8378 build_outer_field_access_method (class, type, name, args, body)
8379 tree class, type, name, args, body;
8381 tree saved_current_function_decl, mdecl;
8383 /* Create the method */
8384 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8385 fix_method_argument_names (args, mdecl);
8386 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8388 /* Attach the method body. */
8389 saved_current_function_decl = current_function_decl;
8390 start_artificial_method_body (mdecl);
8391 java_method_add_stmt (mdecl, body);
8392 end_artificial_method_body (mdecl);
8393 current_function_decl = saved_current_function_decl;
8395 return mdecl;
8399 /* This section deals with building access function necessary for
8400 certain kinds of method invocation from inner classes. */
8402 static tree
8403 build_outer_method_access_method (decl)
8404 tree decl;
8406 tree saved_current_function_decl, mdecl;
8407 tree args = NULL_TREE, call_args = NULL_TREE;
8408 tree carg, id, body, class;
8409 char buffer [80];
8410 int parm_id_count = 0;
8412 /* Test this abort with an access to a private field */
8413 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8414 abort ();
8416 /* Check the cache first */
8417 if (DECL_FUNCTION_INNER_ACCESS (decl))
8418 return DECL_FUNCTION_INNER_ACCESS (decl);
8420 class = DECL_CONTEXT (decl);
8422 /* Obtain an access identifier and mark it */
8423 id = build_new_access_id ();
8424 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8426 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8427 /* Create the arguments, as much as the original */
8428 for (; carg && carg != end_params_node;
8429 carg = TREE_CHAIN (carg))
8431 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8432 args = chainon (args, build_tree_list (get_identifier (buffer),
8433 TREE_VALUE (carg)));
8435 args = chainon (args, end_params_node);
8437 /* Create the method */
8438 mdecl = create_artificial_method (class, ACC_STATIC,
8439 TREE_TYPE (TREE_TYPE (decl)), id, args);
8440 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8441 /* There is a potential bug here. We should be able to use
8442 fix_method_argument_names, but then arg names get mixed up and
8443 eventually a constructor will have its this$0 altered and the
8444 outer context won't be assignment properly. The test case is
8445 stub.java FIXME */
8446 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8448 /* Attach the method body. */
8449 saved_current_function_decl = current_function_decl;
8450 start_artificial_method_body (mdecl);
8452 /* The actual method invocation uses the same args. When invoking a
8453 static methods that way, we don't want to skip the first
8454 argument. */
8455 carg = args;
8456 if (!METHOD_STATIC (decl))
8457 carg = TREE_CHAIN (carg);
8458 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8459 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8460 call_args);
8462 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8463 call_args);
8464 if (!METHOD_STATIC (decl))
8465 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8466 body, 0);
8467 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8468 body = build_return (0, body);
8469 java_method_add_stmt (mdecl,body);
8470 end_artificial_method_body (mdecl);
8471 current_function_decl = saved_current_function_decl;
8473 /* Back tag the access function so it know what it accesses */
8474 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8476 /* Tag the current method so it knows it has an access generated */
8477 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8481 /* This section of the code deals with building expressions to access
8482 the enclosing instance of an inner class. The enclosing instance is
8483 kept in a generated field called this$<n>, with <n> being the
8484 inner class nesting level (starting from 0.) */
8486 /* Build an access to a given this$<n>, always chaining access call to
8487 others. Access methods to this$<n> are build on the fly if
8488 necessary. This CAN'T be used to solely access this$<n-1> from
8489 this$<n> (which alway yield to special cases and optimization, see
8490 for example build_outer_field_access). */
8492 static tree
8493 build_access_to_thisn (from, to, lc)
8494 tree from, to;
8495 int lc;
8497 tree access = NULL_TREE;
8499 while (from != to)
8501 if (!access)
8503 access = build_current_thisn (from);
8504 access = build_wfl_node (access);
8506 else
8508 tree access0_wfl, cn;
8510 maybe_build_thisn_access_method (from);
8511 access0_wfl = build_wfl_node (access0_identifier_node);
8512 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8513 EXPR_WFL_LINECOL (access0_wfl) = lc;
8514 access = build_tree_list (NULL_TREE, access);
8515 access = build_method_invocation (access0_wfl, access);
8516 access = make_qualified_primary (cn, access, lc);
8519 /* if FROM isn't an inter class, that's fine, we've done
8520 enough. What we're looking for can be accessed from there. */
8521 from = DECL_CONTEXT (TYPE_NAME (from));
8522 if (!from)
8523 break;
8524 from = TREE_TYPE (from);
8526 return access;
8529 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8530 is returned if nothing needs to be generated. Otherwise, the method
8531 generated and a method decl is returned.
8533 NOTE: These generated methods should be declared in a class file
8534 attribute so that they can't be referred to directly. */
8536 static tree
8537 maybe_build_thisn_access_method (type)
8538 tree type;
8540 tree mdecl, args, stmt, rtype;
8541 tree saved_current_function_decl;
8543 /* If TYPE is a top-level class, no access method is required.
8544 If there already is such an access method, bail out. */
8545 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8546 return NULL_TREE;
8548 /* We generate the method. The method looks like:
8549 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8551 args = build_tree_list (inst_id, build_pointer_type (type));
8552 TREE_CHAIN (args) = end_params_node;
8553 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8554 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8555 access0_identifier_node, args);
8556 fix_method_argument_names (args, mdecl);
8557 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8558 stmt = build_current_thisn (type);
8559 stmt = make_qualified_primary (build_wfl_node (inst_id),
8560 build_wfl_node (stmt), 0);
8561 stmt = build_return (0, stmt);
8563 saved_current_function_decl = current_function_decl;
8564 start_artificial_method_body (mdecl);
8565 java_method_add_stmt (mdecl, stmt);
8566 end_artificial_method_body (mdecl);
8567 current_function_decl = saved_current_function_decl;
8569 CLASS_ACCESS0_GENERATED_P (type) = 1;
8571 return mdecl;
8574 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8575 the first level of innerclassing. this$1 for the next one, etc...
8576 This function can be invoked with TYPE to NULL, available and then
8577 has to count the parser context. */
8579 static tree
8580 build_current_thisn (type)
8581 tree type;
8583 static int saved_i = -1;
8584 static tree saved_thisn = NULL_TREE;
8585 static tree saved_type = NULL_TREE;
8586 static int saved_type_i = 0;
8587 static int initialized_p;
8588 tree decl;
8589 char buffer [80];
8590 int i = 0;
8592 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8593 if (!initialized_p)
8595 ggc_add_tree_root (&saved_thisn, 1);
8596 ggc_add_tree_root (&saved_type, 1);
8597 initialized_p = 1;
8600 if (type)
8602 if (type == saved_type)
8603 i = saved_type_i;
8604 else
8606 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8607 decl; decl = DECL_CONTEXT (decl), i++)
8610 saved_type = type;
8611 saved_type_i = i;
8614 else
8615 i = list_length (GET_CPC_LIST ())-2;
8617 if (i == saved_i)
8618 return saved_thisn;
8620 sprintf (buffer, "this$%d", i);
8621 saved_i = i;
8622 saved_thisn = get_identifier (buffer);
8623 return saved_thisn;
8626 /* Return the assignement to the hidden enclosing context `this$<n>'
8627 by the second incoming parameter to the innerclass constructor. The
8628 form used is `this.this$<n> = this$<n>;'. */
8630 static tree
8631 build_thisn_assign ()
8633 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8635 tree thisn = build_current_thisn (current_class);
8636 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8637 build_wfl_node (thisn), 0);
8638 tree rhs = build_wfl_node (thisn);
8639 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8640 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8642 return NULL_TREE;
8646 /* Building the synthetic `class$' used to implement the `.class' 1.1
8647 extension for non primitive types. This method looks like:
8649 static Class class$(String type) throws NoClassDefFoundError
8651 try {return (java.lang.Class.forName (String));}
8652 catch (ClassNotFoundException e) {
8653 throw new NoClassDefFoundError(e.getMessage());}
8654 } */
8656 static tree
8657 build_dot_class_method (class)
8658 tree class;
8660 #define BWF(S) build_wfl_node (get_identifier ((S)))
8661 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8662 tree args, tmp, saved_current_function_decl, mdecl;
8663 tree stmt, throw_stmt;
8665 static tree get_message_wfl, type_parm_wfl;
8667 if (!get_message_wfl)
8669 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8670 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8671 ggc_add_tree_root (&get_message_wfl, 1);
8672 ggc_add_tree_root (&type_parm_wfl, 1);
8675 /* Build the arguments */
8676 args = build_tree_list (get_identifier ("type$"),
8677 build_pointer_type (string_type_node));
8678 TREE_CHAIN (args) = end_params_node;
8680 /* Build the qualified name java.lang.Class.forName */
8681 tmp = MQN (MQN (MQN (BWF ("java"),
8682 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8683 load_class (class_not_found_type_node, 1);
8684 load_class (no_class_def_found_type_node, 1);
8686 /* Create the "class$" function */
8687 mdecl = create_artificial_method (class, ACC_STATIC,
8688 build_pointer_type (class_type_node),
8689 classdollar_identifier_node, args);
8690 DECL_FUNCTION_THROWS (mdecl) =
8691 build_tree_list (NULL_TREE, no_class_def_found_type_node);
8693 /* We start by building the try block. We need to build:
8694 return (java.lang.Class.forName (type)); */
8695 stmt = build_method_invocation (tmp,
8696 build_tree_list (NULL_TREE, type_parm_wfl));
8697 stmt = build_return (0, stmt);
8699 /* Now onto the catch block. We start by building the expression
8700 throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8701 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8702 get_message_wfl, 0);
8703 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8705 /* Build new NoClassDefFoundError (_.getMessage) */
8706 throw_stmt = build_new_invocation
8707 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8708 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8710 /* Build the throw, (it's too early to use BUILD_THROW) */
8711 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8713 /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8714 stmt = encapsulate_with_try_catch (0, class_not_found_type_node,
8715 stmt, throw_stmt);
8717 fix_method_argument_names (args, mdecl);
8718 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8719 saved_current_function_decl = current_function_decl;
8720 start_artificial_method_body (mdecl);
8721 java_method_add_stmt (mdecl, stmt);
8722 end_artificial_method_body (mdecl);
8723 current_function_decl = saved_current_function_decl;
8724 TYPE_DOT_CLASS (class) = mdecl;
8726 return mdecl;
8729 static tree
8730 build_dot_class_method_invocation (type)
8731 tree type;
8733 tree sig_id, s;
8735 if (TYPE_ARRAY_P (type))
8736 sig_id = build_java_signature (type);
8737 else
8738 sig_id = DECL_NAME (TYPE_NAME (type));
8740 /* Ensure that the proper name separator is used */
8741 sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8742 IDENTIFIER_LENGTH (sig_id));
8744 s = build_string (IDENTIFIER_LENGTH (sig_id),
8745 IDENTIFIER_POINTER (sig_id));
8746 return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8747 build_tree_list (NULL_TREE, s));
8750 /* This section of the code deals with constructor. */
8752 /* Craft a body for default constructor. Patch existing constructor
8753 bodies with call to super() and field initialization statements if
8754 necessary. */
8756 static void
8757 fix_constructors (mdecl)
8758 tree mdecl;
8760 tree iii; /* Instance Initializer Invocation */
8761 tree body = DECL_FUNCTION_BODY (mdecl);
8762 tree thisn_assign, compound = NULL_TREE;
8763 tree class_type = DECL_CONTEXT (mdecl);
8765 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8766 return;
8767 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8769 if (!body)
8771 /* It is an error for the compiler to generate a default
8772 constructor if the superclass doesn't have a constructor that
8773 takes no argument, or the same args for an anonymous class */
8774 if (verify_constructor_super (mdecl))
8776 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8777 tree save = DECL_NAME (mdecl);
8778 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8779 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8780 parse_error_context
8781 (lookup_cl (TYPE_NAME (class_type)),
8782 "No constructor matching `%s' found in class `%s'",
8783 lang_printable_name (mdecl, 0), n);
8784 DECL_NAME (mdecl) = save;
8787 /* The constructor body must be crafted by hand. It's the
8788 constructor we defined when we realize we didn't have the
8789 CLASSNAME() constructor */
8790 start_artificial_method_body (mdecl);
8792 /* Insert an assignment to the this$<n> hidden field, if
8793 necessary */
8794 if ((thisn_assign = build_thisn_assign ()))
8795 java_method_add_stmt (mdecl, thisn_assign);
8797 /* We don't generate a super constructor invocation if we're
8798 compiling java.lang.Object. build_super_invocation takes care
8799 of that. */
8800 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8802 /* FIXME */
8803 if ((iii = build_instinit_invocation (class_type)))
8804 java_method_add_stmt (mdecl, iii);
8806 end_artificial_method_body (mdecl);
8808 /* Search for an explicit constructor invocation */
8809 else
8811 int found = 0;
8812 int invokes_this = 0;
8813 tree found_call = NULL_TREE;
8814 tree main_block = BLOCK_EXPR_BODY (body);
8816 while (body)
8817 switch (TREE_CODE (body))
8819 case CALL_EXPR:
8820 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8821 if (CALL_THIS_CONSTRUCTOR_P (body))
8822 invokes_this = 1;
8823 body = NULL_TREE;
8824 break;
8825 case COMPOUND_EXPR:
8826 case EXPR_WITH_FILE_LOCATION:
8827 found_call = body;
8828 body = TREE_OPERAND (body, 0);
8829 break;
8830 case BLOCK:
8831 found_call = body;
8832 body = BLOCK_EXPR_BODY (body);
8833 break;
8834 default:
8835 found = 0;
8836 body = NULL_TREE;
8839 /* Generate the assignment to this$<n>, if necessary */
8840 if ((thisn_assign = build_thisn_assign ()))
8841 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8843 /* The constructor is missing an invocation of super() */
8844 if (!found)
8845 compound = add_stmt_to_compound (compound, NULL_TREE,
8846 build_super_invocation (mdecl));
8847 /* Explicit super() invokation should take place before the
8848 instance initializer blocks. */
8849 else
8851 compound = add_stmt_to_compound (compound, NULL_TREE,
8852 TREE_OPERAND (found_call, 0));
8853 TREE_OPERAND (found_call, 0) = empty_stmt_node;
8856 /* Insert the instance initializer block right after. */
8857 if (!invokes_this && (iii = build_instinit_invocation (class_type)))
8858 compound = add_stmt_to_compound (compound, NULL_TREE, iii);
8860 /* Fix the constructor main block if we're adding extra stmts */
8861 if (compound)
8863 compound = add_stmt_to_compound (compound, NULL_TREE,
8864 BLOCK_EXPR_BODY (main_block));
8865 BLOCK_EXPR_BODY (main_block) = compound;
8870 /* Browse constructors in the super class, searching for a constructor
8871 that doesn't take any argument. Return 0 if one is found, 1
8872 otherwise. If the current class is an anonymous inner class, look
8873 for something that has the same signature. */
8875 static int
8876 verify_constructor_super (mdecl)
8877 tree mdecl;
8879 tree class = CLASSTYPE_SUPER (current_class);
8880 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8881 tree sdecl;
8883 if (!class)
8884 return 0;
8886 if (ANONYMOUS_CLASS_P (current_class))
8888 tree mdecl_arg_type;
8889 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8890 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8891 if (DECL_CONSTRUCTOR_P (sdecl))
8893 tree m_arg_type;
8894 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8895 if (super_inner)
8896 arg_type = TREE_CHAIN (arg_type);
8897 for (m_arg_type = mdecl_arg_type;
8898 (arg_type != end_params_node
8899 && m_arg_type != end_params_node);
8900 arg_type = TREE_CHAIN (arg_type),
8901 m_arg_type = TREE_CHAIN (m_arg_type))
8902 if (!valid_method_invocation_conversion_p
8903 (TREE_VALUE (arg_type),
8904 TREE_VALUE (m_arg_type)))
8905 break;
8907 if (arg_type == end_params_node && m_arg_type == end_params_node)
8908 return 0;
8911 else
8913 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8915 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8916 if (super_inner)
8917 arg = TREE_CHAIN (arg);
8918 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8919 return 0;
8922 return 1;
8925 /* Generate code for all context remembered for code generation. */
8927 void
8928 java_expand_classes ()
8930 int save_error_count = 0;
8931 static struct parser_ctxt *cur_ctxp = NULL;
8933 java_parse_abort_on_error ();
8934 if (!(ctxp = ctxp_for_generation))
8935 return;
8936 java_layout_classes ();
8937 java_parse_abort_on_error ();
8939 cur_ctxp = ctxp_for_generation;
8940 for (; cur_ctxp; cur_ctxp = cur_ctxp->next)
8942 ctxp = cur_ctxp;
8943 input_filename = ctxp->filename;
8944 lang_init_source (2); /* Error msgs have method prototypes */
8945 java_complete_expand_classes (); /* Complete and expand classes */
8946 java_parse_abort_on_error ();
8948 input_filename = main_input_filename;
8950 /* Find anonymous classes and expand their constructor, now they
8951 have been fixed. */
8952 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8954 tree current;
8955 ctxp = cur_ctxp;
8956 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8958 current_class = TREE_TYPE (current);
8959 if (ANONYMOUS_CLASS_P (current_class))
8961 tree d;
8962 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8964 if (DECL_CONSTRUCTOR_P (d))
8966 restore_line_number_status (1);
8967 java_complete_expand_method (d);
8968 restore_line_number_status (0);
8969 break; /* We now there are no other ones */
8976 /* If we've found error at that stage, don't try to generate
8977 anything, unless we're emitting xrefs or checking the syntax only
8978 (but not using -fsyntax-only for the purpose of generating
8979 bytecode. */
8980 if (java_error_count && !flag_emit_xref
8981 && (!flag_syntax_only && !flag_emit_class_files))
8982 return;
8984 /* Now things are stable, go for generation of the class data. */
8985 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8987 tree current;
8988 ctxp = cur_ctxp;
8989 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8991 current_class = TREE_TYPE (current);
8992 outgoing_cpool = TYPE_CPOOL (current_class);
8993 if (flag_emit_class_files)
8994 write_classfile (current_class);
8995 if (flag_emit_xref)
8996 expand_xref (current_class);
8997 else if (! flag_syntax_only)
8999 java_expand_method_bodies (current_class);
9000 finish_class ();
9006 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9007 a tree list node containing RIGHT. Fore coming RIGHTs will be
9008 chained to this hook. LOCATION contains the location of the
9009 separating `.' operator. */
9011 static tree
9012 make_qualified_primary (primary, right, location)
9013 tree primary, right;
9014 int location;
9016 tree wfl;
9018 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9019 wfl = build_wfl_wrap (primary, location);
9020 else
9022 wfl = primary;
9023 /* If wfl wasn't qualified, we build a first anchor */
9024 if (!EXPR_WFL_QUALIFICATION (wfl))
9025 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9028 /* And chain them */
9029 EXPR_WFL_LINECOL (right) = location;
9030 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9031 PRIMARY_P (wfl) = 1;
9032 return wfl;
9035 /* Simple merge of two name separated by a `.' */
9037 static tree
9038 merge_qualified_name (left, right)
9039 tree left, right;
9041 tree node;
9042 if (!left && !right)
9043 return NULL_TREE;
9045 if (!left)
9046 return right;
9048 if (!right)
9049 return left;
9051 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9052 IDENTIFIER_LENGTH (left));
9053 obstack_1grow (&temporary_obstack, '.');
9054 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9055 IDENTIFIER_LENGTH (right));
9056 node = get_identifier (obstack_base (&temporary_obstack));
9057 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9058 QUALIFIED_P (node) = 1;
9059 return node;
9062 /* Merge the two parts of a qualified name into LEFT. Set the
9063 location information of the resulting node to LOCATION, usually
9064 inherited from the location information of the `.' operator. */
9066 static tree
9067 make_qualified_name (left, right, location)
9068 tree left, right;
9069 int location;
9071 #ifdef USE_COMPONENT_REF
9072 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
9073 EXPR_WFL_LINECOL (node) = location;
9074 return node;
9075 #else
9076 tree left_id = EXPR_WFL_NODE (left);
9077 tree right_id = EXPR_WFL_NODE (right);
9078 tree wfl, merge;
9080 merge = merge_qualified_name (left_id, right_id);
9082 /* Left wasn't qualified and is now qualified */
9083 if (!QUALIFIED_P (left_id))
9085 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9086 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9087 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9090 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9091 EXPR_WFL_LINECOL (wfl) = location;
9092 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9094 EXPR_WFL_NODE (left) = merge;
9095 return left;
9096 #endif
9099 /* Extract the last identifier component of the qualified in WFL. The
9100 last identifier is removed from the linked list */
9102 static tree
9103 cut_identifier_in_qualified (wfl)
9104 tree wfl;
9106 tree q;
9107 tree previous = NULL_TREE;
9108 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9109 if (!TREE_CHAIN (q))
9111 if (!previous)
9112 /* Operating on a non qualified qualified WFL. */
9113 abort ();
9115 TREE_CHAIN (previous) = NULL_TREE;
9116 return TREE_PURPOSE (q);
9120 /* Resolve the expression name NAME. Return its decl. */
9122 static tree
9123 resolve_expression_name (id, orig)
9124 tree id;
9125 tree *orig;
9127 tree name = EXPR_WFL_NODE (id);
9128 tree decl;
9130 /* 6.5.5.1: Simple expression names */
9131 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9133 /* 15.13.1: NAME can appear within the scope of a local variable
9134 declaration */
9135 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9136 return decl;
9138 /* 15.13.1: NAME can appear within a class declaration */
9139 else
9141 decl = lookup_field_wrapper (current_class, name);
9142 if (decl)
9144 tree access = NULL_TREE;
9145 int fs = FIELD_STATIC (decl);
9147 /* If we're accessing an outer scope local alias, make
9148 sure we change the name of the field we're going to
9149 build access to. */
9150 if (FIELD_LOCAL_ALIAS_USED (decl))
9151 name = DECL_NAME (decl);
9153 /* Instance variable (8.3.1.1) can't appear within
9154 static method, static initializer or initializer for
9155 a static variable. */
9156 if (!fs && METHOD_STATIC (current_function_decl))
9158 static_ref_err (id, name, current_class);
9159 return error_mark_node;
9161 /* Instance variables can't appear as an argument of
9162 an explicit constructor invocation */
9163 if (!fs && ctxp->explicit_constructor_p
9164 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9166 parse_error_context
9167 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9168 return error_mark_node;
9171 /* If we're processing an inner class and we're trying
9172 to access a field belonging to an outer class, build
9173 the access to the field */
9174 if (!fs && outer_field_access_p (current_class, decl))
9176 if (CLASS_STATIC (TYPE_NAME (current_class)))
9178 static_ref_err (id, DECL_NAME (decl), current_class);
9179 return error_mark_node;
9181 access = build_outer_field_access (id, decl);
9182 if (orig)
9183 *orig = access;
9184 return access;
9187 /* Otherwise build what it takes to access the field */
9188 access = build_field_ref ((fs ? NULL_TREE : current_this),
9189 DECL_CONTEXT (decl), name);
9190 if (fs)
9191 access = maybe_build_class_init_for_field (decl, access);
9192 /* We may be asked to save the real field access node */
9193 if (orig)
9194 *orig = access;
9195 /* And we return what we got */
9196 return access;
9198 /* Fall down to error report on undefined variable */
9201 /* 6.5.5.2 Qualified Expression Names */
9202 else
9204 if (orig)
9205 *orig = NULL_TREE;
9206 qualify_ambiguous_name (id);
9207 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9208 /* 15.10.2: Accessing Superclass Members using super */
9209 return resolve_field_access (id, orig, NULL);
9212 /* We've got an error here */
9213 if (INNER_CLASS_TYPE_P (current_class))
9214 parse_error_context (id,
9215 "Local variable `%s' can't be accessed from within the inner class `%s' unless it is declared final",
9216 IDENTIFIER_POINTER (name),
9217 IDENTIFIER_POINTER (DECL_NAME
9218 (TYPE_NAME (current_class))));
9219 else
9220 parse_error_context (id, "Undefined variable `%s'",
9221 IDENTIFIER_POINTER (name));
9223 return error_mark_node;
9226 static void
9227 static_ref_err (wfl, field_id, class_type)
9228 tree wfl, field_id, class_type;
9230 parse_error_context
9231 (wfl,
9232 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
9233 IDENTIFIER_POINTER (field_id),
9234 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9237 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
9238 We return something suitable to generate the field access. We also
9239 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9240 recipient's address can be null. */
9242 static tree
9243 resolve_field_access (qual_wfl, field_decl, field_type)
9244 tree qual_wfl;
9245 tree *field_decl, *field_type;
9247 int is_static = 0;
9248 tree field_ref;
9249 tree decl, where_found, type_found;
9251 if (resolve_qualified_expression_name (qual_wfl, &decl,
9252 &where_found, &type_found))
9253 return error_mark_node;
9255 /* Resolve the LENGTH field of an array here */
9256 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9257 && type_found && TYPE_ARRAY_P (type_found)
9258 && ! flag_emit_class_files && ! flag_emit_xref)
9260 tree length = build_java_array_length_access (where_found);
9261 field_ref = length;
9263 /* In case we're dealing with a static array, we need to
9264 initialize its class before the array length can be fetched.
9265 It's also a good time to create a DECL_RTL for the field if
9266 none already exists, otherwise if the field was declared in a
9267 class found in an external file and hasn't been (and won't
9268 be) accessed for its value, none will be created. */
9269 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9271 build_static_field_ref (where_found);
9272 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9275 /* We might have been trying to resolve field.method(). In which
9276 case, the resolution is over and decl is the answer */
9277 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9278 field_ref = decl;
9279 else if (JDECL_P (decl))
9281 if (!type_found)
9282 type_found = DECL_CONTEXT (decl);
9283 is_static = FIELD_STATIC (decl);
9284 field_ref = build_field_ref ((is_static && !flag_emit_xref?
9285 NULL_TREE : where_found),
9286 type_found, DECL_NAME (decl));
9287 if (field_ref == error_mark_node)
9288 return error_mark_node;
9289 if (is_static)
9290 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9292 else
9293 field_ref = decl;
9295 if (field_decl)
9296 *field_decl = decl;
9297 if (field_type)
9298 *field_type = (QUAL_DECL_TYPE (decl) ?
9299 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9300 return field_ref;
9303 /* If NODE is an access to f static field, strip out the class
9304 initialization part and return the field decl, otherwise, return
9305 NODE. */
9307 static tree
9308 strip_out_static_field_access_decl (node)
9309 tree node;
9311 if (TREE_CODE (node) == COMPOUND_EXPR)
9313 tree op1 = TREE_OPERAND (node, 1);
9314 if (TREE_CODE (op1) == COMPOUND_EXPR)
9316 tree call = TREE_OPERAND (op1, 0);
9317 if (TREE_CODE (call) == CALL_EXPR
9318 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9319 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9320 == soft_initclass_node)
9321 return TREE_OPERAND (op1, 1);
9323 else if (JDECL_P (op1))
9324 return op1;
9326 return node;
9329 /* 6.5.5.2: Qualified Expression Names */
9331 static int
9332 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9333 tree wfl;
9334 tree *found_decl, *type_found, *where_found;
9336 int from_type = 0; /* Field search initiated from a type */
9337 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9338 int previous_call_static = 0;
9339 int is_static;
9340 tree decl = NULL_TREE, type = NULL_TREE, q;
9341 /* For certain for of inner class instantiation */
9342 tree saved_current, saved_this;
9343 #define RESTORE_THIS_AND_CURRENT_CLASS \
9344 { current_class = saved_current; current_this = saved_this;}
9346 *type_found = *where_found = NULL_TREE;
9348 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9350 tree qual_wfl = QUAL_WFL (q);
9351 tree ret_decl; /* for EH checking */
9352 int location; /* for EH checking */
9354 /* 15.10.1 Field Access Using a Primary */
9355 switch (TREE_CODE (qual_wfl))
9357 case CALL_EXPR:
9358 case NEW_CLASS_EXPR:
9359 /* If the access to the function call is a non static field,
9360 build the code to access it. */
9361 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9363 decl = maybe_access_field (decl, *where_found,
9364 DECL_CONTEXT (decl));
9365 if (decl == error_mark_node)
9366 return 1;
9369 /* And code for the function call */
9370 if (complete_function_arguments (qual_wfl))
9371 return 1;
9373 /* We might have to setup a new current class and a new this
9374 for the search of an inner class, relative to the type of
9375 a expression resolved as `decl'. The current values are
9376 saved and restored shortly after */
9377 saved_current = current_class;
9378 saved_this = current_this;
9379 if (decl
9380 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9381 || from_qualified_this))
9383 /* If we still have `from_qualified_this', we have the form
9384 <T>.this.f() and we need to build <T>.this */
9385 if (from_qualified_this)
9387 decl = build_access_to_thisn (current_class, type, 0);
9388 decl = java_complete_tree (decl);
9389 type = TREE_TYPE (TREE_TYPE (decl));
9391 current_class = type;
9392 current_this = decl;
9393 from_qualified_this = 0;
9396 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9397 CALL_USING_SUPER (qual_wfl) = 1;
9398 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9399 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9400 *where_found = patch_method_invocation (qual_wfl, decl, type,
9401 from_super,
9402 &is_static, &ret_decl);
9403 if (*where_found == error_mark_node)
9405 RESTORE_THIS_AND_CURRENT_CLASS;
9406 return 1;
9408 *type_found = type = QUAL_DECL_TYPE (*where_found);
9410 /* If we're creating an inner class instance, check for that
9411 an enclosing instance is in scope */
9412 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9413 && INNER_ENCLOSING_SCOPE_CHECK (type))
9415 parse_error_context
9416 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9417 lang_printable_name (type, 0),
9418 (!current_this ? "" :
9419 "; an explicit one must be provided when creating this inner class"));
9420 RESTORE_THIS_AND_CURRENT_CLASS;
9421 return 1;
9424 /* In case we had to change then to resolve a inner class
9425 instantiation using a primary qualified by a `new' */
9426 RESTORE_THIS_AND_CURRENT_CLASS;
9428 /* EH check. No check on access$<n> functions */
9429 if (location
9430 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9431 (DECL_NAME (current_function_decl)))
9432 check_thrown_exceptions (location, ret_decl);
9434 /* If the previous call was static and this one is too,
9435 build a compound expression to hold the two (because in
9436 that case, previous function calls aren't transported as
9437 forcoming function's argument. */
9438 if (previous_call_static && is_static)
9440 decl = build (COMPOUND_EXPR, TREE_TYPE (*where_found),
9441 decl, *where_found);
9442 TREE_SIDE_EFFECTS (decl) = 1;
9444 else
9446 previous_call_static = is_static;
9447 decl = *where_found;
9449 from_type = 0;
9450 continue;
9452 case NEW_ARRAY_EXPR:
9453 case NEW_ANONYMOUS_ARRAY_EXPR:
9454 *where_found = decl = java_complete_tree (qual_wfl);
9455 if (decl == error_mark_node)
9456 return 1;
9457 *type_found = type = QUAL_DECL_TYPE (decl);
9458 continue;
9460 case CONVERT_EXPR:
9461 *where_found = decl = java_complete_tree (qual_wfl);
9462 if (decl == error_mark_node)
9463 return 1;
9464 *type_found = type = QUAL_DECL_TYPE (decl);
9465 from_cast = 1;
9466 continue;
9468 case CONDITIONAL_EXPR:
9469 case STRING_CST:
9470 case MODIFY_EXPR:
9471 *where_found = decl = java_complete_tree (qual_wfl);
9472 if (decl == error_mark_node)
9473 return 1;
9474 *type_found = type = QUAL_DECL_TYPE (decl);
9475 continue;
9477 case ARRAY_REF:
9478 /* If the access to the function call is a non static field,
9479 build the code to access it. */
9480 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9482 decl = maybe_access_field (decl, *where_found, type);
9483 if (decl == error_mark_node)
9484 return 1;
9486 /* And code for the array reference expression */
9487 decl = java_complete_tree (qual_wfl);
9488 if (decl == error_mark_node)
9489 return 1;
9490 type = QUAL_DECL_TYPE (decl);
9491 continue;
9493 case PLUS_EXPR:
9494 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9495 return 1;
9496 if ((type = patch_string (decl)))
9497 decl = type;
9498 *where_found = QUAL_RESOLUTION (q) = decl;
9499 *type_found = type = TREE_TYPE (decl);
9500 break;
9502 case CLASS_LITERAL:
9503 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9504 return 1;
9505 *where_found = QUAL_RESOLUTION (q) = decl;
9506 *type_found = type = TREE_TYPE (decl);
9507 break;
9509 default:
9510 /* Fix for -Wall Just go to the next statement. Don't
9511 continue */
9512 break;
9515 /* If we fall here, we weren't processing a (static) function call. */
9516 previous_call_static = 0;
9518 /* It can be the keyword THIS */
9519 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9520 && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9522 if (!current_this)
9524 parse_error_context
9525 (wfl, "Keyword `this' used outside allowed context");
9526 return 1;
9528 if (ctxp->explicit_constructor_p
9529 && type == current_class)
9531 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9532 return 1;
9534 /* We have to generate code for intermediate acess */
9535 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9537 *where_found = decl = current_this;
9538 *type_found = type = QUAL_DECL_TYPE (decl);
9540 /* We're trying to access the this from somewhere else. Make sure
9541 it's allowed before doing so. */
9542 else
9544 if (!enclosing_context_p (type, current_class))
9546 char *p = xstrdup (lang_printable_name (type, 0));
9547 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9548 p, p,
9549 lang_printable_name (current_class, 0));
9550 free (p);
9551 return 1;
9553 from_qualified_this = 1;
9554 /* If there's nothing else after that, we need to
9555 produce something now, otherwise, the section of the
9556 code that needs to produce <T>.this will generate
9557 what is necessary. */
9558 if (!TREE_CHAIN (q))
9560 decl = build_access_to_thisn (current_class, type, 0);
9561 *where_found = decl = java_complete_tree (decl);
9562 *type_found = type = TREE_TYPE (decl);
9566 from_type = 0;
9567 continue;
9570 /* 15.10.2 Accessing Superclass Members using SUPER */
9571 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9572 && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9574 tree node;
9575 /* Check on the restricted use of SUPER */
9576 if (METHOD_STATIC (current_function_decl)
9577 || current_class == object_type_node)
9579 parse_error_context
9580 (wfl, "Keyword `super' used outside allowed context");
9581 return 1;
9583 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9584 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9585 CLASSTYPE_SUPER (current_class),
9586 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9587 *where_found = decl = java_complete_tree (node);
9588 if (decl == error_mark_node)
9589 return 1;
9590 *type_found = type = QUAL_DECL_TYPE (decl);
9591 from_super = from_type = 1;
9592 continue;
9595 /* 15.13.1: Can't search for field name in packages, so we
9596 assume a variable/class name was meant. */
9597 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9599 tree name = resolve_package (wfl, &q);
9600 if (name)
9602 tree list;
9603 *where_found = decl = resolve_no_layout (name, qual_wfl);
9604 /* We want to be absolutely sure that the class is laid
9605 out. We're going to search something inside it. */
9606 *type_found = type = TREE_TYPE (decl);
9607 layout_class (type);
9608 from_type = 1;
9610 /* Fix them all the way down, if any are left. */
9611 if (q)
9613 list = TREE_CHAIN (q);
9614 while (list)
9616 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9617 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9618 list = TREE_CHAIN (list);
9622 else
9624 if (from_super || from_cast)
9625 parse_error_context
9626 ((from_cast ? qual_wfl : wfl),
9627 "No variable `%s' defined in class `%s'",
9628 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9629 lang_printable_name (type, 0));
9630 else
9631 parse_error_context
9632 (qual_wfl, "Undefined variable or class name: `%s'",
9633 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9634 return 1;
9638 /* We have a type name. It's been already resolved when the
9639 expression was qualified. */
9640 else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
9642 decl = QUAL_RESOLUTION (q);
9644 /* Sneak preview. If next we see a `new', we're facing a
9645 qualification with resulted in a type being selected
9646 instead of a field. Report the error */
9647 if(TREE_CHAIN (q)
9648 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9650 parse_error_context (qual_wfl, "Undefined variable `%s'",
9651 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9652 return 1;
9655 if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9657 parse_error_context
9658 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9659 java_accstring_lookup (get_access_flags_from_decl (decl)),
9660 GET_TYPE_NAME (type),
9661 IDENTIFIER_POINTER (DECL_NAME (decl)),
9662 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9663 return 1;
9665 check_deprecation (qual_wfl, decl);
9667 type = TREE_TYPE (decl);
9668 from_type = 1;
9670 /* We resolve an expression name */
9671 else
9673 tree field_decl = NULL_TREE;
9675 /* If there exists an early resolution, use it. That occurs
9676 only once and we know that there are more things to
9677 come. Don't do that when processing something after SUPER
9678 (we need more thing to be put in place below */
9679 if (!from_super && QUAL_RESOLUTION (q))
9681 decl = QUAL_RESOLUTION (q);
9682 if (!type)
9684 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9686 if (current_this)
9687 *where_found = current_this;
9688 else
9690 static_ref_err (qual_wfl, DECL_NAME (decl),
9691 current_class);
9692 return 1;
9694 if (outer_field_access_p (current_class, decl))
9695 decl = build_outer_field_access (qual_wfl, decl);
9697 else
9699 *where_found = TREE_TYPE (decl);
9700 if (TREE_CODE (*where_found) == POINTER_TYPE)
9701 *where_found = TREE_TYPE (*where_found);
9706 /* Report and error if we're using a numerical litteral as a
9707 qualifier. It can only be an INTEGER_CST. */
9708 else if (TREE_CODE (qual_wfl) == INTEGER_CST)
9710 parse_error_context
9711 (wfl, "Can't use type `%s' as a qualifier",
9712 lang_printable_name (TREE_TYPE (qual_wfl), 0));
9713 return 1;
9716 /* We have to search for a field, knowing the type of its
9717 container. The flag FROM_TYPE indicates that we resolved
9718 the last member of the expression as a type name, which
9719 means that for the resolution of this field, we'll look
9720 for other errors than if it was resolved as a member of
9721 an other field. */
9722 else
9724 int is_static;
9725 tree field_decl_type; /* For layout */
9727 if (!from_type && !JREFERENCE_TYPE_P (type))
9729 parse_error_context
9730 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9731 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9732 lang_printable_name (type, 0),
9733 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9734 return 1;
9737 field_decl = lookup_field_wrapper (type,
9738 EXPR_WFL_NODE (qual_wfl));
9740 /* Maybe what we're trying to access to is an inner
9741 class, only if decl is a TYPE_DECL. */
9742 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9744 tree ptr, inner_decl;
9746 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9747 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9748 if (inner_decl)
9750 check_inner_class_access (inner_decl, decl, qual_wfl);
9751 type = TREE_TYPE (inner_decl);
9752 decl = inner_decl;
9753 from_type = 1;
9754 continue;
9758 if (field_decl == NULL_TREE)
9760 parse_error_context
9761 (qual_wfl, "No variable `%s' defined in type `%s'",
9762 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9763 GET_TYPE_NAME (type));
9764 return 1;
9766 if (field_decl == error_mark_node)
9767 return 1;
9769 /* Layout the type of field_decl, since we may need
9770 it. Don't do primitive types or loaded classes. The
9771 situation of non primitive arrays may not handled
9772 properly here. FIXME */
9773 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9774 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9775 else
9776 field_decl_type = TREE_TYPE (field_decl);
9777 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9778 && !CLASS_LOADED_P (field_decl_type)
9779 && !TYPE_ARRAY_P (field_decl_type))
9780 resolve_and_layout (field_decl_type, NULL_TREE);
9782 /* Check on accessibility here */
9783 if (not_accessible_p (current_class, field_decl,
9784 DECL_CONTEXT (field_decl), from_super))
9786 parse_error_context
9787 (qual_wfl,
9788 "Can't access %s field `%s.%s' from `%s'",
9789 java_accstring_lookup
9790 (get_access_flags_from_decl (field_decl)),
9791 GET_TYPE_NAME (type),
9792 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9793 IDENTIFIER_POINTER
9794 (DECL_NAME (TYPE_NAME (current_class))));
9795 return 1;
9797 check_deprecation (qual_wfl, field_decl);
9799 /* There are things to check when fields are accessed
9800 from type. There are no restrictions on a static
9801 declaration of the field when it is accessed from an
9802 interface */
9803 is_static = FIELD_STATIC (field_decl);
9804 if (!from_super && from_type
9805 && !TYPE_INTERFACE_P (type)
9806 && !is_static
9807 && (current_function_decl
9808 && METHOD_STATIC (current_function_decl)))
9810 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9811 return 1;
9813 from_cast = from_super = 0;
9815 /* It's an access from a type but it isn't static, we
9816 make it relative to `this'. */
9817 if (!is_static && from_type)
9818 decl = current_this;
9820 /* If we need to generate something to get a proper
9821 handle on what this field is accessed from, do it
9822 now. */
9823 if (!is_static)
9825 decl = maybe_access_field (decl, *where_found, *type_found);
9826 if (decl == error_mark_node)
9827 return 1;
9830 /* We want to keep the location were found it, and the type
9831 we found. */
9832 *where_found = decl;
9833 *type_found = type;
9835 /* Generate the correct expression for field access from
9836 qualified this */
9837 if (from_qualified_this)
9839 field_decl = build_outer_field_access (qual_wfl, field_decl);
9840 from_qualified_this = 0;
9843 /* This is the decl found and eventually the next one to
9844 search from */
9845 decl = field_decl;
9847 from_type = 0;
9848 type = QUAL_DECL_TYPE (decl);
9850 /* Sneak preview. If decl is qualified by a `new', report
9851 the error here to be accurate on the peculiar construct */
9852 if (TREE_CHAIN (q)
9853 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9854 && !JREFERENCE_TYPE_P (type))
9856 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9857 lang_printable_name (type, 0));
9858 return 1;
9861 /* `q' might have changed due to a after package resolution
9862 re-qualification */
9863 if (!q)
9864 break;
9866 *found_decl = decl;
9867 return 0;
9870 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9871 can't be accessed from REFERENCE (a record type). If MEMBER
9872 features a protected access, we then use WHERE which, if non null,
9873 holds the type of MEMBER's access that is checked against
9874 6.6.2.1. This function should be used when decl is a field or a
9875 method. */
9877 static int
9878 not_accessible_p (reference, member, where, from_super)
9879 tree reference, member;
9880 tree where;
9881 int from_super;
9883 int access_flag = get_access_flags_from_decl (member);
9885 /* Inner classes are processed by check_inner_class_access */
9886 if (INNER_CLASS_TYPE_P (reference))
9887 return 0;
9889 /* Access always granted for members declared public */
9890 if (access_flag & ACC_PUBLIC)
9891 return 0;
9893 /* Check access on protected members */
9894 if (access_flag & ACC_PROTECTED)
9896 /* Access granted if it occurs from within the package
9897 containing the class in which the protected member is
9898 declared */
9899 if (class_in_current_package (DECL_CONTEXT (member)))
9900 return 0;
9902 /* If accessed with the form `super.member', then access is granted */
9903 if (from_super)
9904 return 0;
9906 /* If where is active, access was made through a
9907 qualifier. Access is granted if the type of the qualifier is
9908 or is a sublass of the type the access made from (6.6.2.1.) */
9909 if (where && !inherits_from_p (reference, where))
9910 return 1;
9912 /* Otherwise, access is granted if occuring from the class where
9913 member is declared or a subclass of it. Find the right
9914 context to perform the check */
9915 if (PURE_INNER_CLASS_TYPE_P (reference))
9917 while (INNER_CLASS_TYPE_P (reference))
9919 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9920 return 0;
9921 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9924 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9925 return 0;
9926 return 1;
9929 /* Check access on private members. Access is granted only if it
9930 occurs from within the class in which it is declared -- that does
9931 it for innerclasses too. */
9932 if (access_flag & ACC_PRIVATE)
9934 if (reference == DECL_CONTEXT (member))
9935 return 0;
9936 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9937 return 0;
9938 return 1;
9941 /* Default access are permitted only when occuring within the
9942 package in which the type (REFERENCE) is declared. In other words,
9943 REFERENCE is defined in the current package */
9944 if (ctxp->package)
9945 return !class_in_current_package (reference);
9947 /* Otherwise, access is granted */
9948 return 0;
9951 /* Test deprecated decl access. */
9952 static void
9953 check_deprecation (wfl, decl)
9954 tree wfl, decl;
9956 const char *file = DECL_SOURCE_FILE (decl);
9957 /* Complain if the field is deprecated and the file it was defined
9958 in isn't compiled at the same time the file which contains its
9959 use is */
9960 if (DECL_DEPRECATED (decl)
9961 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9963 char the [20];
9964 switch (TREE_CODE (decl))
9966 case FUNCTION_DECL:
9967 strcpy (the, "method");
9968 break;
9969 case FIELD_DECL:
9970 case VAR_DECL:
9971 strcpy (the, "field");
9972 break;
9973 case TYPE_DECL:
9974 parse_warning_context (wfl, "The class `%s' has been deprecated",
9975 IDENTIFIER_POINTER (DECL_NAME (decl)));
9976 return;
9977 default:
9978 abort ();
9980 /* Don't issue a message if the context as been deprecated as a
9981 whole. */
9982 if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
9983 parse_warning_context
9984 (wfl, "The %s `%s' in class `%s' has been deprecated",
9985 the, lang_printable_name (decl, 0),
9986 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9990 /* Returns 1 if class was declared in the current package, 0 otherwise */
9992 static int
9993 class_in_current_package (class)
9994 tree class;
9996 static tree cache = NULL_TREE;
9997 int qualified_flag;
9998 tree left;
10000 if (cache == class)
10001 return 1;
10003 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10005 /* If the current package is empty and the name of CLASS is
10006 qualified, class isn't in the current package. If there is a
10007 current package and the name of the CLASS is not qualified, class
10008 isn't in the current package */
10009 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10010 return 0;
10012 /* If there is not package and the name of CLASS isn't qualified,
10013 they belong to the same unnamed package */
10014 if (!ctxp->package && !qualified_flag)
10015 return 1;
10017 /* Compare the left part of the name of CLASS with the package name */
10018 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10019 if (ctxp->package == left)
10021 static int initialized_p;
10022 /* Register CACHE with the garbage collector. */
10023 if (!initialized_p)
10025 ggc_add_tree_root (&cache, 1);
10026 initialized_p = 1;
10029 cache = class;
10030 return 1;
10032 return 0;
10035 /* This function may generate code to access DECL from WHERE. This is
10036 done only if certain conditions meet. */
10038 static tree
10039 maybe_access_field (decl, where, type)
10040 tree decl, where, type;
10042 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10043 && !FIELD_STATIC (decl))
10044 decl = build_field_ref (where ? where : current_this,
10045 (type ? type : DECL_CONTEXT (decl)),
10046 DECL_NAME (decl));
10047 return decl;
10050 /* Build a method invocation, by patching PATCH. If non NULL
10051 and according to the situation, PRIMARY and WHERE may be
10052 used. IS_STATIC is set to 1 if the invoked function is static. */
10054 static tree
10055 patch_method_invocation (patch, primary, where, from_super,
10056 is_static, ret_decl)
10057 tree patch, primary, where;
10058 int from_super;
10059 int *is_static;
10060 tree *ret_decl;
10062 tree wfl = TREE_OPERAND (patch, 0);
10063 tree args = TREE_OPERAND (patch, 1);
10064 tree name = EXPR_WFL_NODE (wfl);
10065 tree list;
10066 int is_static_flag = 0;
10067 int is_super_init = 0;
10068 tree this_arg = NULL_TREE;
10069 int is_array_clone_call = 0;
10071 /* Should be overriden if everything goes well. Otherwise, if
10072 something fails, it should keep this value. It stop the
10073 evaluation of a bogus assignment. See java_complete_tree,
10074 MODIFY_EXPR: for the reasons why we sometimes want to keep on
10075 evaluating an assignment */
10076 TREE_TYPE (patch) = error_mark_node;
10078 /* Since lookup functions are messing with line numbers, save the
10079 context now. */
10080 java_parser_context_save_global ();
10082 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10084 /* Resolution of qualified name, excluding constructors */
10085 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10087 tree identifier, identifier_wfl, type, resolved;
10088 /* Extract the last IDENTIFIER of the qualified
10089 expression. This is a wfl and we will use it's location
10090 data during error report. */
10091 identifier_wfl = cut_identifier_in_qualified (wfl);
10092 identifier = EXPR_WFL_NODE (identifier_wfl);
10094 /* Given the context, IDENTIFIER is syntactically qualified
10095 as a MethodName. We need to qualify what's before */
10096 qualify_ambiguous_name (wfl);
10097 resolved = resolve_field_access (wfl, NULL, NULL);
10099 if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10100 && FIELD_FINAL (resolved)
10101 && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10102 && !flag_emit_class_files && !flag_emit_xref)
10103 resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10105 if (resolved == error_mark_node)
10106 PATCH_METHOD_RETURN_ERROR ();
10108 type = GET_SKIP_TYPE (resolved);
10109 resolve_and_layout (type, NULL_TREE);
10111 if (JPRIMITIVE_TYPE_P (type))
10113 parse_error_context
10114 (identifier_wfl,
10115 "Can't invoke a method on primitive type `%s'",
10116 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10117 PATCH_METHOD_RETURN_ERROR ();
10120 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10121 args = nreverse (args);
10123 /* We're resolving a call from a type */
10124 if (TREE_CODE (resolved) == TYPE_DECL)
10126 if (CLASS_INTERFACE (resolved))
10128 parse_error_context
10129 (identifier_wfl,
10130 "Can't make static reference to method `%s' in interface `%s'",
10131 IDENTIFIER_POINTER (identifier),
10132 IDENTIFIER_POINTER (name));
10133 PATCH_METHOD_RETURN_ERROR ();
10135 if (list && !METHOD_STATIC (list))
10137 char *fct_name = xstrdup (lang_printable_name (list, 0));
10138 parse_error_context
10139 (identifier_wfl,
10140 "Can't make static reference to method `%s %s' in class `%s'",
10141 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10142 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10143 free (fct_name);
10144 PATCH_METHOD_RETURN_ERROR ();
10147 else
10148 this_arg = primary = resolved;
10150 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10151 is_array_clone_call = 1;
10153 /* IDENTIFIER_WFL will be used to report any problem further */
10154 wfl = identifier_wfl;
10156 /* Resolution of simple names, names generated after a primary: or
10157 constructors */
10158 else
10160 tree class_to_search = NULL_TREE;
10161 int lc; /* Looking for Constructor */
10163 /* We search constructor in their target class */
10164 if (CALL_CONSTRUCTOR_P (patch))
10166 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10167 class_to_search = EXPR_WFL_NODE (wfl);
10168 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10169 this_identifier_node)
10170 class_to_search = NULL_TREE;
10171 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10172 super_identifier_node)
10174 is_super_init = 1;
10175 if (CLASSTYPE_SUPER (current_class))
10176 class_to_search =
10177 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10178 else
10180 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10181 PATCH_METHOD_RETURN_ERROR ();
10185 /* Class to search is NULL if we're searching the current one */
10186 if (class_to_search)
10188 class_to_search = resolve_and_layout (class_to_search, wfl);
10190 if (!class_to_search)
10192 parse_error_context
10193 (wfl, "Class `%s' not found in type declaration",
10194 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10195 PATCH_METHOD_RETURN_ERROR ();
10198 /* Can't instantiate an abstract class, but we can
10199 invoke it's constructor. It's use within the `new'
10200 context is denied here. */
10201 if (CLASS_ABSTRACT (class_to_search)
10202 && TREE_CODE (patch) == NEW_CLASS_EXPR)
10204 parse_error_context
10205 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
10206 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10207 PATCH_METHOD_RETURN_ERROR ();
10210 class_to_search = TREE_TYPE (class_to_search);
10212 else
10213 class_to_search = current_class;
10214 lc = 1;
10216 /* This is a regular search in the local class, unless an
10217 alternate class is specified. */
10218 else
10220 if (where != NULL_TREE)
10221 class_to_search = where;
10222 else if (QUALIFIED_P (name))
10223 class_to_search = current_class;
10224 else
10226 class_to_search = current_class;
10228 for (;;)
10230 if (has_method (class_to_search, name))
10231 break;
10232 if (! INNER_CLASS_TYPE_P (class_to_search))
10234 parse_error_context (wfl,
10235 "No method named `%s' in scope",
10236 IDENTIFIER_POINTER (name));
10237 PATCH_METHOD_RETURN_ERROR ();
10239 class_to_search
10240 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10243 lc = 0;
10246 /* NAME is a simple identifier or comes from a primary. Search
10247 in the class whose declaration contain the method being
10248 invoked. */
10249 resolve_and_layout (class_to_search, NULL_TREE);
10251 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10252 /* Don't continue if no method were found, as the next statement
10253 can't be executed then. */
10254 if (!list)
10255 PATCH_METHOD_RETURN_ERROR ();
10257 if (TYPE_ARRAY_P (class_to_search)
10258 && DECL_NAME (list) == get_identifier ("clone"))
10259 is_array_clone_call = 1;
10261 /* Check for static reference if non static methods */
10262 if (check_for_static_method_reference (wfl, patch, list,
10263 class_to_search, primary))
10264 PATCH_METHOD_RETURN_ERROR ();
10266 /* Check for inner classes creation from illegal contexts */
10267 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10268 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10269 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
10271 parse_error_context
10272 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
10273 lang_printable_name (class_to_search, 0),
10274 (!current_this ? "" :
10275 "; an explicit one must be provided when creating this inner class"));
10276 PATCH_METHOD_RETURN_ERROR ();
10279 /* Non static methods are called with the current object extra
10280 argument. If patch a `new TYPE()', the argument is the value
10281 returned by the object allocator. If method is resolved as a
10282 primary, use the primary otherwise use the current THIS. */
10283 args = nreverse (args);
10284 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10286 this_arg = primary ? primary : current_this;
10288 /* If we're using an access method, things are different.
10289 There are two familly of cases:
10291 1) We're not generating bytecodes:
10293 - LIST is non static. It's invocation is transformed from
10294 x(a1,...,an) into this$<n>.x(a1,....an).
10295 - LIST is static. It's invocation is transformed from
10296 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10298 2) We're generating bytecodes:
10300 - LIST is non static. It's invocation is transformed from
10301 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10302 - LIST is static. It's invocation is transformed from
10303 x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10305 Of course, this$<n> can be abitrary complex, ranging from
10306 this$0 (the immediate outer context) to
10307 access$0(access$0(...(this$0))).
10309 maybe_use_access_method returns a non zero value if the
10310 this_arg has to be moved into the (then generated) stub
10311 argument list. In the meantime, the selected function
10312 might have be replaced by a generated stub. */
10313 if (!primary &&
10314 maybe_use_access_method (is_super_init, &list, &this_arg))
10316 args = tree_cons (NULL_TREE, this_arg, args);
10317 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10322 /* Merge point of all resolution schemes. If we have nothing, this
10323 is an error, already signaled */
10324 if (!list)
10325 PATCH_METHOD_RETURN_ERROR ();
10327 /* Check accessibility, position the is_static flag, build and
10328 return the call */
10329 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10330 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10331 NULL_TREE), from_super)
10332 /* Calls to clone() on array types are permitted as a special-case. */
10333 && !is_array_clone_call)
10335 const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10336 const char *const access =
10337 java_accstring_lookup (get_access_flags_from_decl (list));
10338 const char *const klass =
10339 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10340 const char *const refklass =
10341 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10342 const char *const what = (DECL_CONSTRUCTOR_P (list)
10343 ? "constructor" : "method");
10344 /* FIXME: WFL yields the wrong message here but I don't know
10345 what else to use. */
10346 parse_error_context (wfl,
10347 "Can't access %s %s `%s.%s' from `%s'",
10348 access, what, klass, fct_name, refklass);
10349 PATCH_METHOD_RETURN_ERROR ();
10352 /* Deprecation check: check whether the method being invoked or the
10353 instance-being-created's type are deprecated. */
10354 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10355 check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10356 else
10357 check_deprecation (wfl, list);
10359 /* If invoking a innerclass constructor, there are hidden parameters
10360 to pass */
10361 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10362 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10364 /* And make sure we add the accessed local variables to be saved
10365 in field aliases. */
10366 args = build_alias_initializer_parameter_list
10367 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10369 /* Secretly pass the current_this/primary as a second argument */
10370 if (primary || current_this)
10372 tree extra_arg;
10373 tree this_type = (current_this ?
10374 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10375 /* Method's (list) enclosing context */
10376 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10377 /* If we have a primary, use it. */
10378 if (primary)
10379 extra_arg = primary;
10380 /* The current `this' is an inner class but isn't a direct
10381 enclosing context for the inner class we're trying to
10382 create. Build an access to the proper enclosing context
10383 and use it. */
10384 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10385 && this_type != TREE_TYPE (mec))
10388 extra_arg = build_access_to_thisn (current_class,
10389 TREE_TYPE (mec), 0);
10390 extra_arg = java_complete_tree (extra_arg);
10392 /* Otherwise, just use the current `this' as an enclosing
10393 context. */
10394 else
10395 extra_arg = current_this;
10396 args = tree_cons (NULL_TREE, extra_arg, args);
10398 else
10399 args = tree_cons (NULL_TREE, integer_zero_node, args);
10402 /* This handles the situation where a constructor invocation needs
10403 to have an enclosing context passed as a second parameter (the
10404 constructor is one of an inner class. We extract it from the
10405 current function. */
10406 if ((is_super_init ||
10407 (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10408 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10410 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10411 tree extra_arg;
10413 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10415 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10416 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10418 else
10420 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10421 extra_arg =
10422 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10423 extra_arg = java_complete_tree (extra_arg);
10425 args = tree_cons (NULL_TREE, extra_arg, args);
10428 is_static_flag = METHOD_STATIC (list);
10429 if (! is_static_flag && this_arg != NULL_TREE)
10430 args = tree_cons (NULL_TREE, this_arg, args);
10432 /* In the context of an explicit constructor invocation, we can't
10433 invoke any method relying on `this'. Exceptions are: we're
10434 invoking a static function, primary exists and is not the current
10435 this, we're creating a new object. */
10436 if (ctxp->explicit_constructor_p
10437 && !is_static_flag
10438 && (!primary || primary == current_this)
10439 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10441 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10442 PATCH_METHOD_RETURN_ERROR ();
10444 java_parser_context_restore_global ();
10445 if (is_static)
10446 *is_static = is_static_flag;
10447 /* Sometimes, we want the decl of the selected method. Such as for
10448 EH checking */
10449 if (ret_decl)
10450 *ret_decl = list;
10451 patch = patch_invoke (patch, list, args);
10453 /* Now is a good time to insert the call to finit$ */
10454 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10456 tree finit_parms, finit_call;
10458 /* Prepare to pass hidden parameters to finit$, if any. */
10459 finit_parms = build_alias_initializer_parameter_list
10460 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10462 finit_call =
10463 build_method_invocation (build_wfl_node (finit_identifier_node),
10464 finit_parms);
10466 /* Generate the code used to initialize fields declared with an
10467 initialization statement and build a compound statement along
10468 with the super constructor invocation. */
10469 patch = build (COMPOUND_EXPR, void_type_node, patch,
10470 java_complete_tree (finit_call));
10471 CAN_COMPLETE_NORMALLY (patch) = 1;
10473 return patch;
10476 /* Check that we're not trying to do a static reference to a method in
10477 non static method. Return 1 if it's the case, 0 otherwise. */
10479 static int
10480 check_for_static_method_reference (wfl, node, method, where, primary)
10481 tree wfl, node, method, where, primary;
10483 if (METHOD_STATIC (current_function_decl)
10484 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10486 char *fct_name = xstrdup (lang_printable_name (method, 0));
10487 parse_error_context
10488 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10489 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10490 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10491 free (fct_name);
10492 return 1;
10494 return 0;
10497 /* Fix the invocation of *MDECL if necessary in the case of a
10498 invocation from an inner class. *THIS_ARG might be modified
10499 appropriately and an alternative access to *MDECL might be
10500 returned. */
10502 static int
10503 maybe_use_access_method (is_super_init, mdecl, this_arg)
10504 int is_super_init;
10505 tree *mdecl, *this_arg;
10507 tree ctx;
10508 tree md = *mdecl, ta = *this_arg;
10509 int to_return = 0;
10510 int non_static_context = !METHOD_STATIC (md);
10512 if (is_super_init
10513 || DECL_CONTEXT (md) == current_class
10514 || !PURE_INNER_CLASS_TYPE_P (current_class)
10515 || DECL_FINIT_P (md)
10516 || DECL_INSTINIT_P (md))
10517 return 0;
10519 /* If we're calling a method found in an enclosing class, generate
10520 what it takes to retrieve the right this. Don't do that if we're
10521 invoking a static method. Note that if MD's type is unrelated to
10522 CURRENT_CLASS, then the current this can be used. */
10524 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10526 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10527 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10529 ta = build_current_thisn (current_class);
10530 ta = build_wfl_node (ta);
10532 else
10534 tree type = ctx;
10535 while (type)
10537 maybe_build_thisn_access_method (type);
10538 if (inherits_from_p (type, DECL_CONTEXT (md)))
10540 ta = build_access_to_thisn (ctx, type, 0);
10541 break;
10543 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10544 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10547 ta = java_complete_tree (ta);
10550 /* We might have to use an access method to get to MD. We can
10551 break the method access rule as far as we're not generating
10552 bytecode */
10553 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10555 md = build_outer_method_access_method (md);
10556 to_return = 1;
10559 *mdecl = md;
10560 *this_arg = ta;
10562 /* Returnin a non zero value indicates we were doing a non static
10563 method invokation that is now a static invocation. It will have
10564 callee displace `this' to insert it in the regular argument
10565 list. */
10566 return (non_static_context && to_return);
10569 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10570 mode. */
10572 static tree
10573 patch_invoke (patch, method, args)
10574 tree patch, method, args;
10576 tree dtable, func;
10577 tree original_call, t, ta;
10578 tree check = NULL_TREE;
10580 /* Last step for args: convert build-in types. If we're dealing with
10581 a new TYPE() type call, the first argument to the constructor
10582 isn't found in the incoming argument list, but delivered by
10583 `new' */
10584 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10585 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10586 t = TREE_CHAIN (t);
10587 for (ta = args; t != end_params_node && ta;
10588 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10589 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10590 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10591 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10593 /* Resolve unresolved returned type isses */
10594 t = TREE_TYPE (TREE_TYPE (method));
10595 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10596 resolve_and_layout (TREE_TYPE (t), NULL);
10598 if (flag_emit_class_files || flag_emit_xref)
10599 func = method;
10600 else
10602 tree signature = build_java_signature (TREE_TYPE (method));
10603 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10605 case INVOKE_VIRTUAL:
10606 dtable = invoke_build_dtable (0, args);
10607 func = build_invokevirtual (dtable, method);
10608 break;
10610 case INVOKE_NONVIRTUAL:
10611 /* If the object for the method call is null, we throw an
10612 exception. We don't do this if the object is the current
10613 method's `this'. In other cases we just rely on an
10614 optimization pass to eliminate redundant checks. */
10615 if (TREE_VALUE (args) != current_this)
10617 /* We use a save_expr here to make sure we only evaluate
10618 the new `self' expression once. */
10619 tree save_arg = save_expr (TREE_VALUE (args));
10620 TREE_VALUE (args) = save_arg;
10621 check = java_check_reference (save_arg, 1);
10623 /* Fall through. */
10625 case INVOKE_SUPER:
10626 case INVOKE_STATIC:
10627 func = build_known_method_ref (method, TREE_TYPE (method),
10628 DECL_CONTEXT (method),
10629 signature, args);
10630 break;
10632 case INVOKE_INTERFACE:
10633 dtable = invoke_build_dtable (1, args);
10634 func = build_invokeinterface (dtable, method);
10635 break;
10637 default:
10638 abort ();
10641 /* Ensure self_type is initialized, (invokestatic). FIXME */
10642 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10645 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10646 TREE_OPERAND (patch, 0) = func;
10647 TREE_OPERAND (patch, 1) = args;
10648 original_call = patch;
10650 /* We're processing a `new TYPE ()' form. New is called and its
10651 returned value is the first argument to the constructor. We build
10652 a COMPOUND_EXPR and use saved expression so that the overall NEW
10653 expression value is a pointer to a newly created and initialized
10654 class. */
10655 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10657 tree class = DECL_CONTEXT (method);
10658 tree c1, saved_new, size, new;
10659 if (flag_emit_class_files || flag_emit_xref)
10661 TREE_TYPE (patch) = build_pointer_type (class);
10662 return patch;
10664 if (!TYPE_SIZE (class))
10665 safe_layout_class (class);
10666 size = size_in_bytes (class);
10667 new = build (CALL_EXPR, promote_type (class),
10668 build_address_of (alloc_object_node),
10669 tree_cons (NULL_TREE, build_class_ref (class),
10670 build_tree_list (NULL_TREE,
10671 size_in_bytes (class))),
10672 NULL_TREE);
10673 saved_new = save_expr (new);
10674 c1 = build_tree_list (NULL_TREE, saved_new);
10675 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10676 TREE_OPERAND (original_call, 1) = c1;
10677 TREE_SET_CODE (original_call, CALL_EXPR);
10678 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10681 /* If CHECK is set, then we are building a check to see if the object
10682 is NULL. */
10683 if (check != NULL_TREE)
10685 patch = build (COMPOUND_EXPR, TREE_TYPE (patch), check, patch);
10686 TREE_SIDE_EFFECTS (patch) = 1;
10689 /* In order to be able to modify PATCH later, we SAVE_EXPR it and
10690 put it as the first expression of a COMPOUND_EXPR. The second
10691 expression being an empty statement to be later patched if
10692 necessary. We remember a TREE_LIST (the PURPOSE is the method,
10693 the VALUE is the compound) in a hashtable and return a
10694 COMPOUND_EXPR built so that the result of the evaluation of the
10695 original PATCH node is returned. */
10696 if (STATIC_CLASS_INIT_OPT_P ()
10697 && current_function_decl && METHOD_STATIC (method))
10699 tree list;
10700 tree fndecl = current_function_decl;
10701 tree save = save_expr (patch);
10702 tree type = TREE_TYPE (patch);
10704 patch = build (COMPOUND_EXPR, type, save, empty_stmt_node);
10705 list = build_tree_list (method, patch);
10707 hash_lookup (&DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl),
10708 (const hash_table_key) list, TRUE, NULL);
10710 patch = build (COMPOUND_EXPR, type, patch, save);
10713 return patch;
10716 static int
10717 invocation_mode (method, super)
10718 tree method;
10719 int super;
10721 int access = get_access_flags_from_decl (method);
10723 if (super)
10724 return INVOKE_SUPER;
10726 if (access & ACC_STATIC)
10727 return INVOKE_STATIC;
10729 /* We have to look for a constructor before we handle nonvirtual
10730 calls; otherwise the constructor will look nonvirtual. */
10731 if (DECL_CONSTRUCTOR_P (method))
10732 return INVOKE_STATIC;
10734 if (access & ACC_FINAL || access & ACC_PRIVATE)
10735 return INVOKE_NONVIRTUAL;
10737 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10738 return INVOKE_NONVIRTUAL;
10740 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10741 return INVOKE_INTERFACE;
10743 return INVOKE_VIRTUAL;
10746 /* Retrieve a refined list of matching methods. It covers the step
10747 15.11.2 (Compile-Time Step 2) */
10749 static tree
10750 lookup_method_invoke (lc, cl, class, name, arg_list)
10751 int lc;
10752 tree cl;
10753 tree class, name, arg_list;
10755 tree atl = end_params_node; /* Arg Type List */
10756 tree method, signature, list, node;
10757 const char *candidates; /* Used for error report */
10758 char *dup;
10760 /* Fix the arguments */
10761 for (node = arg_list; node; node = TREE_CHAIN (node))
10763 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10764 /* Non primitive type may have to be resolved */
10765 if (!JPRIMITIVE_TYPE_P (current_arg))
10766 resolve_and_layout (current_arg, NULL_TREE);
10767 /* And promoted */
10768 if (TREE_CODE (current_arg) == RECORD_TYPE)
10769 current_arg = promote_type (current_arg);
10770 atl = tree_cons (NULL_TREE, current_arg, atl);
10773 /* Presto. If we're dealing with an anonymous class and a
10774 constructor call, generate the right constructor now, since we
10775 know the arguments' types. */
10777 if (lc && ANONYMOUS_CLASS_P (class))
10778 craft_constructor (TYPE_NAME (class), atl);
10780 /* Find all candidates and then refine the list, searching for the
10781 most specific method. */
10782 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10783 list = find_most_specific_methods_list (list);
10784 if (list && !TREE_CHAIN (list))
10785 return TREE_VALUE (list);
10787 /* Issue an error. List candidates if any. Candidates are listed
10788 only if accessible (non accessible methods may end-up here for
10789 the sake of a better error report). */
10790 candidates = NULL;
10791 if (list)
10793 tree current;
10794 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10795 for (current = list; current; current = TREE_CHAIN (current))
10797 tree cm = TREE_VALUE (current);
10798 char string [4096];
10799 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10800 continue;
10801 sprintf
10802 (string, " `%s' in `%s'%s",
10803 get_printable_method_name (cm),
10804 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10805 (TREE_CHAIN (current) ? "\n" : ""));
10806 obstack_grow (&temporary_obstack, string, strlen (string));
10808 obstack_1grow (&temporary_obstack, '\0');
10809 candidates = obstack_finish (&temporary_obstack);
10811 /* Issue the error message */
10812 method = make_node (FUNCTION_TYPE);
10813 TYPE_ARG_TYPES (method) = atl;
10814 signature = build_java_argument_signature (method);
10815 dup = xstrdup (lang_printable_name (class, 0));
10816 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10817 (lc ? "constructor" : "method"),
10818 (lc ? dup : IDENTIFIER_POINTER (name)),
10819 IDENTIFIER_POINTER (signature), dup,
10820 (candidates ? candidates : ""));
10821 free (dup);
10822 return NULL_TREE;
10825 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10826 when we're looking for a constructor. */
10828 static tree
10829 find_applicable_accessible_methods_list (lc, class, name, arglist)
10830 int lc;
10831 tree class, name, arglist;
10833 static struct hash_table t, *searched_classes = NULL;
10834 static int search_not_done = 0;
10835 tree list = NULL_TREE, all_list = NULL_TREE;
10837 /* Check the hash table to determine if this class has been searched
10838 already. */
10839 if (searched_classes)
10841 if (hash_lookup (searched_classes,
10842 (const hash_table_key) class, FALSE, NULL))
10843 return NULL;
10845 else
10847 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10848 java_hash_compare_tree_node);
10849 searched_classes = &t;
10852 search_not_done++;
10853 hash_lookup (searched_classes,
10854 (const hash_table_key) class, TRUE, NULL);
10856 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10858 load_class (class, 1);
10859 safe_layout_class (class);
10862 /* Search interfaces */
10863 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10864 && CLASS_INTERFACE (TYPE_NAME (class)))
10866 int i, n;
10867 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10868 search_applicable_methods_list (lc, TYPE_METHODS (class),
10869 name, arglist, &list, &all_list);
10870 n = TREE_VEC_LENGTH (basetype_vec);
10871 for (i = 1; i < n; i++)
10873 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10874 tree rlist;
10876 rlist = find_applicable_accessible_methods_list (lc, t, name,
10877 arglist);
10878 list = chainon (rlist, list);
10881 /* Search classes */
10882 else
10884 search_applicable_methods_list (lc, TYPE_METHODS (class),
10885 name, arglist, &list, &all_list);
10887 /* When looking finit$, class$ or instinit$, we turn LC to 1 so
10888 that we only search in class. Note that we should have found
10889 something at this point. */
10890 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
10892 lc = 1;
10893 if (!list)
10894 abort ();
10897 /* We must search all interfaces of this class */
10898 if (!lc)
10900 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10901 int n = TREE_VEC_LENGTH (basetype_vec), i;
10902 for (i = 1; i < n; i++)
10904 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10905 if (t != object_type_node)
10907 tree rlist
10908 = find_applicable_accessible_methods_list (lc, t,
10909 name, arglist);
10910 list = chainon (rlist, list);
10915 /* Search superclass */
10916 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10918 tree rlist;
10919 class = CLASSTYPE_SUPER (class);
10920 rlist = find_applicable_accessible_methods_list (lc, class,
10921 name, arglist);
10922 list = chainon (rlist, list);
10926 search_not_done--;
10928 /* We're done. Reset the searched classes list and finally search
10929 java.lang.Object if it wasn't searched already. */
10930 if (!search_not_done)
10932 if (!lc
10933 && TYPE_METHODS (object_type_node)
10934 && !hash_lookup (searched_classes,
10935 (const hash_table_key) object_type_node,
10936 FALSE, NULL))
10938 search_applicable_methods_list (lc,
10939 TYPE_METHODS (object_type_node),
10940 name, arglist, &list, &all_list);
10942 hash_table_free (searched_classes);
10943 searched_classes = NULL;
10946 /* Either return the list obtained or all selected (but
10947 inaccessible) methods for better error report. */
10948 return (!list ? all_list : list);
10951 /* Effectively search for the appropriate method in method */
10953 static void
10954 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10955 int lc;
10956 tree method, name, arglist;
10957 tree *list, *all_list;
10959 for (; method; method = TREE_CHAIN (method))
10961 /* When dealing with constructor, stop here, otherwise search
10962 other classes */
10963 if (lc && !DECL_CONSTRUCTOR_P (method))
10964 continue;
10965 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10966 || (DECL_NAME (method) != name)))
10967 continue;
10969 if (argument_types_convertible (method, arglist))
10971 /* Retain accessible methods only */
10972 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10973 method, NULL_TREE, 0))
10974 *list = tree_cons (NULL_TREE, method, *list);
10975 else
10976 /* Also retain all selected method here */
10977 *all_list = tree_cons (NULL_TREE, method, *list);
10982 /* 15.11.2.2 Choose the Most Specific Method */
10984 static tree
10985 find_most_specific_methods_list (list)
10986 tree list;
10988 int max = 0;
10989 int abstract, candidates;
10990 tree current, new_list = NULL_TREE;
10991 for (current = list; current; current = TREE_CHAIN (current))
10993 tree method;
10994 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10996 for (method = list; method; method = TREE_CHAIN (method))
10998 tree method_v, current_v;
10999 /* Don't test a method against itself */
11000 if (method == current)
11001 continue;
11003 method_v = TREE_VALUE (method);
11004 current_v = TREE_VALUE (current);
11006 /* Compare arguments and location where methods where declared */
11007 if (argument_types_convertible (method_v, current_v))
11009 if (valid_method_invocation_conversion_p
11010 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
11011 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
11012 && enclosing_context_p (DECL_CONTEXT (method_v),
11013 DECL_CONTEXT (current_v))))
11015 int v = (DECL_SPECIFIC_COUNT (current_v) +=
11016 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
11017 max = (v > max ? v : max);
11023 /* Review the list and select the maximally specific methods */
11024 for (current = list, abstract = -1, candidates = -1;
11025 current; current = TREE_CHAIN (current))
11026 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11028 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11029 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11030 candidates++;
11033 /* If we have several and they're all abstract, just pick the
11034 closest one. */
11035 if (candidates > 0 && (candidates == abstract))
11037 new_list = nreverse (new_list);
11038 TREE_CHAIN (new_list) = NULL_TREE;
11041 /* We have several (we couldn't find a most specific), all but one
11042 are abstract, we pick the only non abstract one. */
11043 if (candidates > 0 && (candidates == abstract+1))
11045 for (current = new_list; current; current = TREE_CHAIN (current))
11046 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11048 TREE_CHAIN (current) = NULL_TREE;
11049 new_list = current;
11053 /* If we can't find one, lower expectations and try to gather multiple
11054 maximally specific methods */
11055 while (!new_list && max)
11057 while (--max > 0)
11059 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11060 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11064 return new_list;
11067 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11068 converted by method invocation conversion (5.3) to the type of the
11069 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11070 to change less often than M1. */
11072 static int
11073 argument_types_convertible (m1, m2_or_arglist)
11074 tree m1, m2_or_arglist;
11076 static tree m2_arg_value = NULL_TREE;
11077 static tree m2_arg_cache = NULL_TREE;
11078 static int initialized_p;
11080 register tree m1_arg, m2_arg;
11082 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
11083 collector. */
11084 if (!initialized_p)
11086 ggc_add_tree_root (&m2_arg_value, 1);
11087 ggc_add_tree_root (&m2_arg_cache, 1);
11088 initialized_p = 1;
11091 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11093 if (m2_arg_value == m2_or_arglist)
11094 m2_arg = m2_arg_cache;
11095 else
11097 /* M2_OR_ARGLIST can be a function DECL or a raw list of
11098 argument types */
11099 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11101 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11102 if (!METHOD_STATIC (m2_or_arglist))
11103 m2_arg = TREE_CHAIN (m2_arg);
11105 else
11106 m2_arg = m2_or_arglist;
11108 m2_arg_value = m2_or_arglist;
11109 m2_arg_cache = m2_arg;
11112 while (m1_arg != end_params_node && m2_arg != end_params_node)
11114 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11115 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11116 TREE_VALUE (m2_arg)))
11117 break;
11118 m1_arg = TREE_CHAIN (m1_arg);
11119 m2_arg = TREE_CHAIN (m2_arg);
11121 return m1_arg == end_params_node && m2_arg == end_params_node;
11124 /* Qualification routines */
11126 static void
11127 qualify_ambiguous_name (id)
11128 tree id;
11130 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
11131 saved_current_class;
11132 int again, super_found = 0, this_found = 0, new_array_found = 0;
11133 int code;
11135 /* We first qualify the first element, then derive qualification of
11136 others based on the first one. If the first element is qualified
11137 by a resolution (field or type), this resolution is stored in the
11138 QUAL_RESOLUTION of the qual element being examined. We need to
11139 save the current_class since the use of SUPER might change the
11140 its value. */
11141 saved_current_class = current_class;
11142 qual = EXPR_WFL_QUALIFICATION (id);
11143 do {
11145 /* Simple qualified expression feature a qual_wfl that is a
11146 WFL. Expression derived from a primary feature more complicated
11147 things like a CALL_EXPR. Expression from primary need to be
11148 worked out to extract the part on which the qualification will
11149 take place. */
11150 qual_wfl = QUAL_WFL (qual);
11151 switch (TREE_CODE (qual_wfl))
11153 case CALL_EXPR:
11154 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11155 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11157 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11158 qual_wfl = QUAL_WFL (qual);
11160 break;
11161 case NEW_ARRAY_EXPR:
11162 case NEW_ANONYMOUS_ARRAY_EXPR:
11163 qual = TREE_CHAIN (qual);
11164 again = new_array_found = 1;
11165 continue;
11166 case CONVERT_EXPR:
11167 break;
11168 case NEW_CLASS_EXPR:
11169 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11170 break;
11171 case ARRAY_REF:
11172 while (TREE_CODE (qual_wfl) == ARRAY_REF)
11173 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11174 break;
11175 case STRING_CST:
11176 qual = TREE_CHAIN (qual);
11177 qual_wfl = QUAL_WFL (qual);
11178 break;
11179 case CLASS_LITERAL:
11180 qual = TREE_CHAIN (qual);
11181 qual_wfl = QUAL_WFL (qual);
11182 break;
11183 default:
11184 /* Fix for -Wall. Just break doing nothing */
11185 break;
11188 ptr_type = current_class;
11189 again = 0;
11190 code = TREE_CODE (qual_wfl);
11192 /* Pos evaluation: non WFL leading expression nodes */
11193 if (code == CONVERT_EXPR
11194 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
11195 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
11197 else if (code == INTEGER_CST)
11198 name = qual_wfl;
11200 else if (code == CONVERT_EXPR &&
11201 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11202 name = TREE_OPERAND (qual_wfl, 0);
11204 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
11205 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11206 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
11208 else if (code == TREE_LIST)
11209 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
11211 else if (code == STRING_CST || code == CONDITIONAL_EXPR
11212 || code == PLUS_EXPR)
11214 qual = TREE_CHAIN (qual);
11215 qual_wfl = QUAL_WFL (qual);
11216 again = 1;
11218 else
11220 name = EXPR_WFL_NODE (qual_wfl);
11221 if (!name)
11223 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11224 again = 1;
11228 /* If we have a THIS (from a primary), we set the context accordingly */
11229 if (name == this_identifier_node)
11231 /* This isn't really elegant. One more added irregularity
11232 before I start using COMPONENT_REF (hopefully very soon.) */
11233 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
11234 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11235 EXPR_WITH_FILE_LOCATION
11236 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11237 this_identifier_node)
11239 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
11240 qual = EXPR_WFL_QUALIFICATION (qual);
11242 qual = TREE_CHAIN (qual);
11243 qual_wfl = QUAL_WFL (qual);
11244 if (TREE_CODE (qual_wfl) == CALL_EXPR)
11245 again = 1;
11246 else if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION)
11247 name = EXPR_WFL_NODE (qual_wfl);
11248 else if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
11249 name = TREE_OPERAND (qual_wfl, 0);
11250 this_found = 1;
11252 /* If we have a SUPER, we set the context accordingly */
11253 if (name == super_identifier_node)
11255 current_class = CLASSTYPE_SUPER (ptr_type);
11256 /* Check that there is such a thing as a super class. If not,
11257 return. The error will be caught later on, during the
11258 resolution */
11259 if (!current_class)
11261 current_class = saved_current_class;
11262 return;
11264 qual = TREE_CHAIN (qual);
11265 /* Do one more interation to set things up */
11266 super_found = again = 1;
11268 } while (again);
11270 /* If name appears within the scope of a local variable declaration
11271 or parameter declaration, then it is an expression name. We don't
11272 carry this test out if we're in the context of the use of SUPER
11273 or THIS */
11274 if (!this_found && !super_found
11275 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
11276 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
11278 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11279 QUAL_RESOLUTION (qual) = decl;
11282 /* If within the class/interface NAME was found to be used there
11283 exists a (possibly inherited) field named NAME, then this is an
11284 expression name. If we saw a NEW_ARRAY_EXPR before and want to
11285 address length, it is OK. */
11286 else if ((decl = lookup_field_wrapper (ptr_type, name))
11287 || name == length_identifier_node)
11289 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11290 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
11293 /* We reclassify NAME as yielding to a type name resolution if:
11294 - NAME is a class/interface declared within the compilation
11295 unit containing NAME,
11296 - NAME is imported via a single-type-import declaration,
11297 - NAME is declared in an another compilation unit of the package
11298 of the compilation unit containing NAME,
11299 - NAME is declared by exactly on type-import-on-demand declaration
11300 of the compilation unit containing NAME.
11301 - NAME is actually a STRING_CST.
11302 This can't happen if the expression was qualified by `this.' */
11303 else if (! this_found &&
11304 (TREE_CODE (name) == STRING_CST ||
11305 TREE_CODE (name) == INTEGER_CST ||
11306 (decl = resolve_and_layout (name, NULL_TREE))))
11308 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11309 QUAL_RESOLUTION (qual) = decl;
11312 /* Method call, array references and cast are expression name */
11313 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
11314 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
11315 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR
11316 || TREE_CODE (QUAL_WFL (qual)) == MODIFY_EXPR)
11317 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11319 /* Check here that NAME isn't declared by more than one
11320 type-import-on-demand declaration of the compilation unit
11321 containing NAME. FIXME */
11323 /* Otherwise, NAME is reclassified as a package name */
11324 else
11325 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11327 /* Propagate the qualification accross other components of the
11328 qualified name */
11329 for (qual = TREE_CHAIN (qual); qual;
11330 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11332 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11333 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11334 else
11335 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
11338 /* Store the global qualification for the ambiguous part of ID back
11339 into ID fields */
11340 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
11341 RESOLVE_EXPRESSION_NAME_P (id) = 1;
11342 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
11343 RESOLVE_TYPE_NAME_P (id) = 1;
11344 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11345 RESOLVE_PACKAGE_NAME_P (id) = 1;
11347 /* Restore the current class */
11348 current_class = saved_current_class;
11351 static int
11352 breakdown_qualified (left, right, source)
11353 tree *left, *right, source;
11355 char *p, *base;
11356 int l = IDENTIFIER_LENGTH (source);
11358 base = alloca (l + 1);
11359 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
11361 /* Breakdown NAME into REMAINDER . IDENTIFIER */
11362 p = base + l - 1;
11363 while (*p != '.' && p != base)
11364 p--;
11366 /* We didn't find a '.'. Return an error */
11367 if (p == base)
11368 return 1;
11370 *p = '\0';
11371 if (right)
11372 *right = get_identifier (p+1);
11373 *left = get_identifier (base);
11375 return 0;
11378 /* Return TRUE if two classes are from the same package. */
11380 static int
11381 in_same_package (name1, name2)
11382 tree name1, name2;
11384 tree tmp;
11385 tree pkg1;
11386 tree pkg2;
11388 if (TREE_CODE (name1) == TYPE_DECL)
11389 name1 = DECL_NAME (name1);
11390 if (TREE_CODE (name2) == TYPE_DECL)
11391 name2 = DECL_NAME (name2);
11393 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11394 /* One in empty package. */
11395 return 0;
11397 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11398 /* Both in empty package. */
11399 return 1;
11401 breakdown_qualified (&pkg1, &tmp, name1);
11402 breakdown_qualified (&pkg2, &tmp, name2);
11404 return (pkg1 == pkg2);
11407 /* Patch tree nodes in a function body. When a BLOCK is found, push
11408 local variable decls if present.
11409 Same as java_complete_lhs, but does resolve static finals to values. */
11411 static tree
11412 java_complete_tree (node)
11413 tree node;
11415 node = java_complete_lhs (node);
11416 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11417 && DECL_INITIAL (node) != NULL_TREE
11418 && !flag_emit_xref)
11420 tree value = DECL_INITIAL (node);
11421 DECL_INITIAL (node) = NULL_TREE;
11422 value = fold_constant_for_init (value, node);
11423 DECL_INITIAL (node) = value;
11424 if (value != NULL_TREE)
11426 /* fold_constant_for_init sometimes widen the original type
11427 of the constant (i.e. byte to int.) It's not desirable,
11428 especially if NODE is a function argument. */
11429 if ((TREE_CODE (value) == INTEGER_CST
11430 || TREE_CODE (value) == REAL_CST)
11431 && TREE_TYPE (node) != TREE_TYPE (value))
11432 return convert (TREE_TYPE (node), value);
11433 else
11434 return value;
11436 else
11437 DECL_FIELD_FINAL_IUD (node) = 0;
11439 return node;
11442 static tree
11443 java_stabilize_reference (node)
11444 tree node;
11446 if (TREE_CODE (node) == COMPOUND_EXPR)
11448 tree op0 = TREE_OPERAND (node, 0);
11449 tree op1 = TREE_OPERAND (node, 1);
11450 TREE_OPERAND (node, 0) = save_expr (op0);
11451 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11452 return node;
11454 return stabilize_reference (node);
11457 /* Patch tree nodes in a function body. When a BLOCK is found, push
11458 local variable decls if present.
11459 Same as java_complete_tree, but does not resolve static finals to values. */
11461 static tree
11462 java_complete_lhs (node)
11463 tree node;
11465 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11466 int flag;
11468 /* CONVERT_EXPR always has its type set, even though it needs to be
11469 worked out. */
11470 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11471 return node;
11473 /* The switch block implements cases processing container nodes
11474 first. Contained nodes are always written back. Leaves come
11475 next and return a value. */
11476 switch (TREE_CODE (node))
11478 case BLOCK:
11480 /* 1- Block section.
11481 Set the local values on decl names so we can identify them
11482 faster when they're referenced. At that stage, identifiers
11483 are legal so we don't check for declaration errors. */
11484 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11486 DECL_CONTEXT (cn) = current_function_decl;
11487 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11489 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11490 CAN_COMPLETE_NORMALLY (node) = 1;
11491 else
11493 tree stmt = BLOCK_EXPR_BODY (node);
11494 tree *ptr;
11495 int error_seen = 0;
11496 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11498 /* Re-order from (((A; B); C); ...; Z) to
11499 (A; (B; (C ; (...; Z)))).
11500 This makes it easier to scan the statements left-to-right
11501 without using recursion (which might overflow the stack
11502 if the block has many statements. */
11503 for (;;)
11505 tree left = TREE_OPERAND (stmt, 0);
11506 if (TREE_CODE (left) != COMPOUND_EXPR)
11507 break;
11508 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11509 TREE_OPERAND (left, 1) = stmt;
11510 stmt = left;
11512 BLOCK_EXPR_BODY (node) = stmt;
11515 /* Now do the actual complete, without deep recursion for
11516 long blocks. */
11517 ptr = &BLOCK_EXPR_BODY (node);
11518 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11519 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11521 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11522 tree *next = &TREE_OPERAND (*ptr, 1);
11523 TREE_OPERAND (*ptr, 0) = cur;
11524 if (cur == empty_stmt_node)
11526 /* Optimization; makes it easier to detect empty bodies.
11527 Most useful for <clinit> with all-constant initializer. */
11528 *ptr = *next;
11529 continue;
11531 if (TREE_CODE (cur) == ERROR_MARK)
11532 error_seen++;
11533 else if (! CAN_COMPLETE_NORMALLY (cur))
11535 wfl_op2 = *next;
11536 for (;;)
11538 if (TREE_CODE (wfl_op2) == BLOCK)
11539 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11540 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11541 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11542 else
11543 break;
11545 if (TREE_CODE (wfl_op2) != CASE_EXPR
11546 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11547 unreachable_stmt_error (*ptr);
11549 ptr = next;
11551 *ptr = java_complete_tree (*ptr);
11553 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11554 return error_mark_node;
11555 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11557 /* Turn local bindings to null */
11558 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11559 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11561 TREE_TYPE (node) = void_type_node;
11562 break;
11564 /* 2- They are expressions but ultimately deal with statements */
11566 case THROW_EXPR:
11567 wfl_op1 = TREE_OPERAND (node, 0);
11568 COMPLETE_CHECK_OP_0 (node);
11569 /* 14.19 A throw statement cannot complete normally. */
11570 CAN_COMPLETE_NORMALLY (node) = 0;
11571 return patch_throw_statement (node, wfl_op1);
11573 case SYNCHRONIZED_EXPR:
11574 wfl_op1 = TREE_OPERAND (node, 0);
11575 return patch_synchronized_statement (node, wfl_op1);
11577 case TRY_EXPR:
11578 return patch_try_statement (node);
11580 case TRY_FINALLY_EXPR:
11581 COMPLETE_CHECK_OP_0 (node);
11582 COMPLETE_CHECK_OP_1 (node);
11583 if (TREE_OPERAND (node, 0) == empty_stmt_node)
11584 return TREE_OPERAND (node, 1);
11585 if (TREE_OPERAND (node, 1) == empty_stmt_node)
11586 return TREE_OPERAND (node, 0);
11587 CAN_COMPLETE_NORMALLY (node)
11588 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11589 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11590 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11591 return node;
11593 case CLEANUP_POINT_EXPR:
11594 COMPLETE_CHECK_OP_0 (node);
11595 TREE_TYPE (node) = void_type_node;
11596 CAN_COMPLETE_NORMALLY (node) =
11597 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11598 return node;
11600 case WITH_CLEANUP_EXPR:
11601 COMPLETE_CHECK_OP_0 (node);
11602 COMPLETE_CHECK_OP_1 (node);
11603 CAN_COMPLETE_NORMALLY (node) =
11604 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11605 TREE_TYPE (node) = void_type_node;
11606 return node;
11608 case LABELED_BLOCK_EXPR:
11609 PUSH_LABELED_BLOCK (node);
11610 if (LABELED_BLOCK_BODY (node))
11611 COMPLETE_CHECK_OP_1 (node);
11612 TREE_TYPE (node) = void_type_node;
11613 POP_LABELED_BLOCK ();
11615 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11617 LABELED_BLOCK_BODY (node) = NULL_TREE;
11618 CAN_COMPLETE_NORMALLY (node) = 1;
11620 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11621 CAN_COMPLETE_NORMALLY (node) = 1;
11622 return node;
11624 case EXIT_BLOCK_EXPR:
11625 /* We don't complete operand 1, because it's the return value of
11626 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11627 return patch_bc_statement (node);
11629 case CASE_EXPR:
11630 cn = java_complete_tree (TREE_OPERAND (node, 0));
11631 if (cn == error_mark_node)
11632 return cn;
11634 /* First, the case expression must be constant. Values of final
11635 fields are accepted. */
11636 cn = fold (cn);
11637 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11638 && JDECL_P (TREE_OPERAND (cn, 1))
11639 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11640 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11642 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11643 TREE_OPERAND (cn, 1));
11645 /* Accept final locals too. */
11646 else if (TREE_CODE (cn) == VAR_DECL && LOCAL_FINAL (cn))
11647 cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11649 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11651 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11652 parse_error_context (node, "Constant expression required");
11653 return error_mark_node;
11656 nn = ctxp->current_loop;
11658 /* It must be assignable to the type of the switch expression. */
11659 if (!try_builtin_assignconv (NULL_TREE,
11660 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11662 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11663 parse_error_context
11664 (wfl_operator,
11665 "Incompatible type for case. Can't convert `%s' to `int'",
11666 lang_printable_name (TREE_TYPE (cn), 0));
11667 return error_mark_node;
11670 cn = fold (convert (int_type_node, cn));
11671 TREE_CONSTANT_OVERFLOW (cn) = 0;
11672 CAN_COMPLETE_NORMALLY (cn) = 1;
11674 /* Multiple instance of a case label bearing the same
11675 value is checked during code generation. The case
11676 expression is allright so far. */
11677 if (TREE_CODE (cn) == VAR_DECL)
11678 cn = DECL_INITIAL (cn);
11679 TREE_OPERAND (node, 0) = cn;
11680 TREE_TYPE (node) = void_type_node;
11681 CAN_COMPLETE_NORMALLY (node) = 1;
11682 TREE_SIDE_EFFECTS (node) = 1;
11683 break;
11685 case DEFAULT_EXPR:
11686 nn = ctxp->current_loop;
11687 /* Only one default label is allowed per switch statement */
11688 if (SWITCH_HAS_DEFAULT (nn))
11690 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11691 parse_error_context (wfl_operator,
11692 "Duplicate case label: `default'");
11693 return error_mark_node;
11695 else
11696 SWITCH_HAS_DEFAULT (nn) = 1;
11697 TREE_TYPE (node) = void_type_node;
11698 TREE_SIDE_EFFECTS (node) = 1;
11699 CAN_COMPLETE_NORMALLY (node) = 1;
11700 break;
11702 case SWITCH_EXPR:
11703 case LOOP_EXPR:
11704 PUSH_LOOP (node);
11705 /* Check whether the loop was enclosed in a labeled
11706 statement. If not, create one, insert the loop in it and
11707 return the node */
11708 nn = patch_loop_statement (node);
11710 /* Anyways, walk the body of the loop */
11711 if (TREE_CODE (node) == LOOP_EXPR)
11712 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11713 /* Switch statement: walk the switch expression and the cases */
11714 else
11715 node = patch_switch_statement (node);
11717 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11718 nn = error_mark_node;
11719 else
11721 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11722 /* If we returned something different, that's because we
11723 inserted a label. Pop the label too. */
11724 if (nn != node)
11726 if (CAN_COMPLETE_NORMALLY (node))
11727 CAN_COMPLETE_NORMALLY (nn) = 1;
11728 POP_LABELED_BLOCK ();
11731 POP_LOOP ();
11732 return nn;
11734 case EXIT_EXPR:
11735 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11736 return patch_exit_expr (node);
11738 case COND_EXPR:
11739 /* Condition */
11740 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11741 if (TREE_OPERAND (node, 0) == error_mark_node)
11742 return error_mark_node;
11743 /* then-else branches */
11744 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11745 if (TREE_OPERAND (node, 1) == error_mark_node)
11746 return error_mark_node;
11747 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11748 if (TREE_OPERAND (node, 2) == error_mark_node)
11749 return error_mark_node;
11750 return patch_if_else_statement (node);
11751 break;
11753 case CONDITIONAL_EXPR:
11754 /* Condition */
11755 wfl_op1 = TREE_OPERAND (node, 0);
11756 COMPLETE_CHECK_OP_0 (node);
11757 wfl_op2 = TREE_OPERAND (node, 1);
11758 COMPLETE_CHECK_OP_1 (node);
11759 wfl_op3 = TREE_OPERAND (node, 2);
11760 COMPLETE_CHECK_OP_2 (node);
11761 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11763 /* 3- Expression section */
11764 case COMPOUND_EXPR:
11765 wfl_op2 = TREE_OPERAND (node, 1);
11766 TREE_OPERAND (node, 0) = nn =
11767 java_complete_tree (TREE_OPERAND (node, 0));
11768 if (wfl_op2 == empty_stmt_node)
11769 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11770 else
11772 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11774 /* An unreachable condition in a do-while statement
11775 is *not* (technically) an unreachable statement. */
11776 nn = wfl_op2;
11777 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11778 nn = EXPR_WFL_NODE (nn);
11779 if (TREE_CODE (nn) != EXIT_EXPR)
11781 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11782 parse_error_context (wfl_operator, "Unreachable statement");
11785 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11786 if (TREE_OPERAND (node, 1) == error_mark_node)
11787 return error_mark_node;
11788 CAN_COMPLETE_NORMALLY (node)
11789 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11791 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11792 break;
11794 case RETURN_EXPR:
11795 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11796 return patch_return (node);
11798 case EXPR_WITH_FILE_LOCATION:
11799 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11800 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11802 tree wfl = node;
11803 node = resolve_expression_name (node, NULL);
11804 if (node == error_mark_node)
11805 return node;
11806 /* Keep line number information somewhere were it doesn't
11807 disrupt the completion process. */
11808 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11810 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11811 TREE_OPERAND (node, 1) = wfl;
11813 CAN_COMPLETE_NORMALLY (node) = 1;
11815 else
11817 tree body;
11818 int save_lineno = lineno;
11819 lineno = EXPR_WFL_LINENO (node);
11820 body = java_complete_tree (EXPR_WFL_NODE (node));
11821 lineno = save_lineno;
11822 EXPR_WFL_NODE (node) = body;
11823 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11824 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11825 if (body == empty_stmt_node || TREE_CONSTANT (body))
11827 /* Makes it easier to constant fold, detect empty bodies. */
11828 return body;
11830 if (body == error_mark_node)
11832 /* Its important for the evaluation of assignment that
11833 this mark on the TREE_TYPE is propagated. */
11834 TREE_TYPE (node) = error_mark_node;
11835 return error_mark_node;
11837 else
11838 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11841 break;
11843 case NEW_ARRAY_EXPR:
11844 /* Patch all the dimensions */
11845 flag = 0;
11846 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11848 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11849 tree dim = convert (int_type_node,
11850 java_complete_tree (TREE_VALUE (cn)));
11851 if (dim == error_mark_node)
11853 flag = 1;
11854 continue;
11856 else
11858 TREE_VALUE (cn) = dim;
11859 /* Setup the location of the current dimension, for
11860 later error report. */
11861 TREE_PURPOSE (cn) =
11862 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11863 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11866 /* They complete the array creation expression, if no errors
11867 were found. */
11868 CAN_COMPLETE_NORMALLY (node) = 1;
11869 return (flag ? error_mark_node
11870 : force_evaluation_order (patch_newarray (node)));
11872 case NEW_ANONYMOUS_ARRAY_EXPR:
11873 /* Create the array type if necessary. */
11874 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11876 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11877 if (!(type = resolve_type_during_patch (type)))
11878 return error_mark_node;
11879 type = build_array_from_name (type, NULL_TREE,
11880 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11881 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11883 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11884 ANONYMOUS_ARRAY_INITIALIZER (node));
11885 if (node == error_mark_node)
11886 return error_mark_node;
11887 CAN_COMPLETE_NORMALLY (node) = 1;
11888 return node;
11890 case NEW_CLASS_EXPR:
11891 case CALL_EXPR:
11892 /* Complete function's argument(s) first */
11893 if (complete_function_arguments (node))
11894 return error_mark_node;
11895 else
11897 tree decl, wfl = TREE_OPERAND (node, 0);
11898 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11899 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11900 super_identifier_node);
11902 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11903 from_super, 0, &decl);
11904 if (node == error_mark_node)
11905 return error_mark_node;
11907 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11908 /* If we call this(...), register signature and positions */
11909 if (in_this)
11910 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11911 tree_cons (wfl, decl,
11912 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11913 CAN_COMPLETE_NORMALLY (node) = 1;
11914 return force_evaluation_order (node);
11917 case MODIFY_EXPR:
11918 /* Save potential wfls */
11919 wfl_op1 = TREE_OPERAND (node, 0);
11920 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11922 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11923 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11924 && DECL_INITIAL (nn) != NULL_TREE)
11926 tree value;
11928 value = fold_constant_for_init (nn, nn);
11930 /* When we have a primitype type, or a string and we're not
11931 emitting a class file, we actually don't want to generate
11932 anything for the assignment. */
11933 if (value != NULL_TREE &&
11934 (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
11935 (TREE_TYPE (value) == string_ptr_type_node &&
11936 ! flag_emit_class_files)))
11938 /* Prepare node for patch_assignment */
11939 TREE_OPERAND (node, 1) = value;
11940 /* Call patch assignment to verify the assignment */
11941 if (patch_assignment (node, wfl_op1, value) == error_mark_node)
11942 return error_mark_node;
11943 /* Set DECL_INITIAL properly (a conversion might have
11944 been decided by patch_assignment) and return the
11945 empty statement. */
11946 else
11948 tree patched = patch_string (TREE_OPERAND (node, 1));
11949 if (patched)
11950 DECL_INITIAL (nn) = patched;
11951 else
11952 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
11953 return empty_stmt_node;
11956 if (! flag_emit_class_files)
11957 DECL_INITIAL (nn) = NULL_TREE;
11958 if (CLASS_FINAL_VARIABLE_P (nn))
11959 DECL_FIELD_FINAL_IUD (nn) = 0;
11961 wfl_op2 = TREE_OPERAND (node, 1);
11963 if (TREE_OPERAND (node, 0) == error_mark_node)
11964 return error_mark_node;
11966 flag = COMPOUND_ASSIGN_P (wfl_op2);
11967 if (flag)
11969 /* This might break when accessing outer field from inner
11970 class. TESTME, FIXME */
11971 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11973 /* Hand stabilize the lhs on both places */
11974 TREE_OPERAND (node, 0) = lvalue;
11975 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11976 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11978 /* 15.25.2.a: Left hand is not an array access. FIXME */
11979 /* Now complete the RHS. We write it back later on. */
11980 nn = java_complete_tree (TREE_OPERAND (node, 1));
11982 if ((cn = patch_string (nn)))
11983 nn = cn;
11985 /* The last part of the rewrite for E1 op= E2 is to have
11986 E1 = (T)(E1 op E2), with T being the type of E1. */
11987 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11988 TREE_TYPE (lvalue), nn));
11990 /* If the assignment is compound and has reference type,
11991 then ensure the LHS has type String and nothing else. */
11992 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11993 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11994 parse_error_context (wfl_op2,
11995 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11996 lang_printable_name (TREE_TYPE (lvalue), 0));
11998 /* 15.25.2.b: Left hand is an array access. FIXME */
12001 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12002 function to complete this RHS. Note that a NEW_ARRAY_INIT
12003 might have been already fully expanded if created as a result
12004 of processing an anonymous array initializer. We avoid doing
12005 the operation twice by testing whether the node already bears
12006 a type. */
12007 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12008 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12009 TREE_OPERAND (node, 1));
12010 /* Otherwise we simply complete the RHS */
12011 else
12012 nn = java_complete_tree (TREE_OPERAND (node, 1));
12014 if (nn == error_mark_node)
12015 return error_mark_node;
12017 /* Write back the RHS as we evaluated it. */
12018 TREE_OPERAND (node, 1) = nn;
12020 /* In case we're handling = with a String as a RHS, we need to
12021 produce a String out of the RHS (it might still be a
12022 STRING_CST or a StringBuffer at this stage */
12023 if ((nn = patch_string (TREE_OPERAND (node, 1))))
12024 TREE_OPERAND (node, 1) = nn;
12026 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12027 TREE_OPERAND (node, 1))))
12029 /* We return error_mark_node if outer_field_access_fix
12030 detects we write into a final. */
12031 if (nn == error_mark_node)
12032 return error_mark_node;
12033 node = nn;
12035 else
12037 /* Can't assign to a (blank) final. */
12038 if (check_final_assignment (TREE_OPERAND (node, 0), wfl_op1))
12039 return error_mark_node;
12040 node = patch_assignment (node, wfl_op1, wfl_op2);
12041 /* Reorganize the tree if necessary. */
12042 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12043 || JSTRING_P (TREE_TYPE (node))))
12044 node = java_refold (node);
12047 /* Seek to set DECL_INITIAL to a proper value, since it might have
12048 undergone a conversion in patch_assignment. We do that only when
12049 it's necessary to have DECL_INITIAL properly set. */
12050 nn = TREE_OPERAND (node, 0);
12051 if (TREE_CODE (nn) == VAR_DECL
12052 && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12053 && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12054 && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12055 || TREE_TYPE (nn) == string_ptr_type_node))
12056 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12058 CAN_COMPLETE_NORMALLY (node) = 1;
12059 return node;
12061 case MULT_EXPR:
12062 case PLUS_EXPR:
12063 case MINUS_EXPR:
12064 case LSHIFT_EXPR:
12065 case RSHIFT_EXPR:
12066 case URSHIFT_EXPR:
12067 case BIT_AND_EXPR:
12068 case BIT_XOR_EXPR:
12069 case BIT_IOR_EXPR:
12070 case TRUNC_MOD_EXPR:
12071 case TRUNC_DIV_EXPR:
12072 case RDIV_EXPR:
12073 case TRUTH_ANDIF_EXPR:
12074 case TRUTH_ORIF_EXPR:
12075 case EQ_EXPR:
12076 case NE_EXPR:
12077 case GT_EXPR:
12078 case GE_EXPR:
12079 case LT_EXPR:
12080 case LE_EXPR:
12081 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12082 knows how to handle those cases. */
12083 wfl_op1 = TREE_OPERAND (node, 0);
12084 wfl_op2 = TREE_OPERAND (node, 1);
12086 CAN_COMPLETE_NORMALLY (node) = 1;
12087 /* Don't complete string nodes if dealing with the PLUS operand. */
12088 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12090 nn = java_complete_tree (wfl_op1);
12091 if (nn == error_mark_node)
12092 return error_mark_node;
12094 TREE_OPERAND (node, 0) = nn;
12096 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12098 nn = java_complete_tree (wfl_op2);
12099 if (nn == error_mark_node)
12100 return error_mark_node;
12102 TREE_OPERAND (node, 1) = nn;
12104 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
12106 case INSTANCEOF_EXPR:
12107 wfl_op1 = TREE_OPERAND (node, 0);
12108 COMPLETE_CHECK_OP_0 (node);
12109 if (flag_emit_xref)
12111 TREE_TYPE (node) = boolean_type_node;
12112 return node;
12114 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
12116 case UNARY_PLUS_EXPR:
12117 case NEGATE_EXPR:
12118 case TRUTH_NOT_EXPR:
12119 case BIT_NOT_EXPR:
12120 case PREDECREMENT_EXPR:
12121 case PREINCREMENT_EXPR:
12122 case POSTDECREMENT_EXPR:
12123 case POSTINCREMENT_EXPR:
12124 case CONVERT_EXPR:
12125 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12126 how to handle those cases. */
12127 wfl_op1 = TREE_OPERAND (node, 0);
12128 CAN_COMPLETE_NORMALLY (node) = 1;
12129 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12130 if (TREE_OPERAND (node, 0) == error_mark_node)
12131 return error_mark_node;
12132 node = patch_unaryop (node, wfl_op1);
12133 CAN_COMPLETE_NORMALLY (node) = 1;
12134 break;
12136 case ARRAY_REF:
12137 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12138 how to handle those cases. */
12139 wfl_op1 = TREE_OPERAND (node, 0);
12140 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12141 if (TREE_OPERAND (node, 0) == error_mark_node)
12142 return error_mark_node;
12143 if (!flag_emit_class_files && !flag_emit_xref)
12144 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12145 /* The same applies to wfl_op2 */
12146 wfl_op2 = TREE_OPERAND (node, 1);
12147 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12148 if (TREE_OPERAND (node, 1) == error_mark_node)
12149 return error_mark_node;
12150 if (!flag_emit_class_files && !flag_emit_xref)
12151 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12152 return patch_array_ref (node);
12154 case RECORD_TYPE:
12155 return node;;
12157 case COMPONENT_REF:
12158 /* The first step in the re-write of qualified name handling. FIXME.
12159 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12160 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12161 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12163 tree name = TREE_OPERAND (node, 1);
12164 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12165 if (field == NULL_TREE)
12167 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
12168 return error_mark_node;
12170 if (! FIELD_STATIC (field))
12172 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
12173 return error_mark_node;
12175 return field;
12177 else
12178 abort ();
12179 break;
12181 case THIS_EXPR:
12182 /* Can't use THIS in a static environment */
12183 if (!current_this)
12185 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12186 parse_error_context (wfl_operator,
12187 "Keyword `this' used outside allowed context");
12188 TREE_TYPE (node) = error_mark_node;
12189 return error_mark_node;
12191 if (ctxp->explicit_constructor_p)
12193 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12194 parse_error_context
12195 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
12196 TREE_TYPE (node) = error_mark_node;
12197 return error_mark_node;
12199 return current_this;
12201 case CLASS_LITERAL:
12202 CAN_COMPLETE_NORMALLY (node) = 1;
12203 node = patch_incomplete_class_ref (node);
12204 if (node == error_mark_node)
12205 return error_mark_node;
12206 break;
12208 default:
12209 CAN_COMPLETE_NORMALLY (node) = 1;
12210 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12211 and it's time to turn it into the appropriate String object */
12212 if ((nn = patch_string (node)))
12213 node = nn;
12214 else
12215 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12217 return node;
12220 /* Complete function call's argument. Return a non zero value is an
12221 error was found. */
12223 static int
12224 complete_function_arguments (node)
12225 tree node;
12227 int flag = 0;
12228 tree cn;
12230 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12231 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12233 tree wfl = TREE_VALUE (cn), parm, temp;
12234 parm = java_complete_tree (wfl);
12236 if (parm == error_mark_node)
12238 flag = 1;
12239 continue;
12241 /* If have a string literal that we haven't transformed yet or a
12242 crafted string buffer, as a result of use of the the String
12243 `+' operator. Build `parm.toString()' and expand it. */
12244 if ((temp = patch_string (parm)))
12245 parm = temp;
12246 /* Inline PRIMTYPE.TYPE read access */
12247 parm = maybe_build_primttype_type_ref (parm, wfl);
12249 TREE_VALUE (cn) = parm;
12251 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12252 return flag;
12255 /* Sometimes (for loops and variable initialized during their
12256 declaration), we want to wrap a statement around a WFL and turn it
12257 debugable. */
12259 static tree
12260 build_debugable_stmt (location, stmt)
12261 int location;
12262 tree stmt;
12264 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12266 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12267 EXPR_WFL_LINECOL (stmt) = location;
12269 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12270 return stmt;
12273 static tree
12274 build_expr_block (body, decls)
12275 tree body, decls;
12277 tree node = make_node (BLOCK);
12278 BLOCK_EXPR_DECLS (node) = decls;
12279 BLOCK_EXPR_BODY (node) = body;
12280 if (body)
12281 TREE_TYPE (node) = TREE_TYPE (body);
12282 TREE_SIDE_EFFECTS (node) = 1;
12283 return node;
12286 /* Create a new function block and link it approriately to current
12287 function block chain */
12289 static tree
12290 enter_block ()
12292 tree b = build_expr_block (NULL_TREE, NULL_TREE);
12294 /* Link block B supercontext to the previous block. The current
12295 function DECL is used as supercontext when enter_a_block is called
12296 for the first time for a given function. The current function body
12297 (DECL_FUNCTION_BODY) is set to be block B. */
12299 tree fndecl = current_function_decl;
12301 if (!fndecl) {
12302 BLOCK_SUPERCONTEXT (b) = current_static_block;
12303 current_static_block = b;
12306 else if (!DECL_FUNCTION_BODY (fndecl))
12308 BLOCK_SUPERCONTEXT (b) = fndecl;
12309 DECL_FUNCTION_BODY (fndecl) = b;
12311 else
12313 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12314 DECL_FUNCTION_BODY (fndecl) = b;
12316 return b;
12319 /* Exit a block by changing the current function body
12320 (DECL_FUNCTION_BODY) to the current block super context, only if
12321 the block being exited isn't the method's top level one. */
12323 static tree
12324 exit_block ()
12326 tree b;
12327 if (current_function_decl)
12329 b = DECL_FUNCTION_BODY (current_function_decl);
12330 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12331 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12333 else
12335 b = current_static_block;
12337 if (BLOCK_SUPERCONTEXT (b))
12338 current_static_block = BLOCK_SUPERCONTEXT (b);
12340 return b;
12343 /* Lookup for NAME in the nested function's blocks, all the way up to
12344 the current toplevel one. It complies with Java's local variable
12345 scoping rules. */
12347 static tree
12348 lookup_name_in_blocks (name)
12349 tree name;
12351 tree b = GET_CURRENT_BLOCK (current_function_decl);
12353 while (b != current_function_decl)
12355 tree current;
12357 /* Paranoid sanity check. To be removed */
12358 if (TREE_CODE (b) != BLOCK)
12359 abort ();
12361 for (current = BLOCK_EXPR_DECLS (b); current;
12362 current = TREE_CHAIN (current))
12363 if (DECL_NAME (current) == name)
12364 return current;
12365 b = BLOCK_SUPERCONTEXT (b);
12367 return NULL_TREE;
12370 static void
12371 maybe_absorb_scoping_blocks ()
12373 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12375 tree b = exit_block ();
12376 java_method_add_stmt (current_function_decl, b);
12377 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
12382 /* This section of the source is reserved to build_* functions that
12383 are building incomplete tree nodes and the patch_* functions that
12384 are completing them. */
12386 /* Wrap a non WFL node around a WFL. */
12388 static tree
12389 build_wfl_wrap (node, location)
12390 tree node;
12391 int location;
12393 tree wfl, node_to_insert = node;
12395 /* We want to process THIS . xxx symbolicaly, to keep it consistent
12396 with the way we're processing SUPER. A THIS from a primary as a
12397 different form than a SUPER. Turn THIS into something symbolic */
12398 if (TREE_CODE (node) == THIS_EXPR)
12399 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12400 else
12401 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12403 EXPR_WFL_LINECOL (wfl) = location;
12404 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12405 return wfl;
12408 /* Build a super() constructor invocation. Returns empty_stmt_node if
12409 we're currently dealing with the class java.lang.Object. */
12411 static tree
12412 build_super_invocation (mdecl)
12413 tree mdecl;
12415 if (DECL_CONTEXT (mdecl) == object_type_node)
12416 return empty_stmt_node;
12417 else
12419 tree super_wfl = build_wfl_node (super_identifier_node);
12420 tree a = NULL_TREE, t;
12421 /* If we're dealing with an anonymous class, pass the arguments
12422 of the crafted constructor along. */
12423 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12425 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12426 for (; t != end_params_node; t = TREE_CHAIN (t))
12427 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12429 return build_method_invocation (super_wfl, a);
12433 /* Build a SUPER/THIS qualified method invocation. */
12435 static tree
12436 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12437 int use_this;
12438 tree name, args;
12439 int lloc, rloc;
12441 tree invok;
12442 tree wfl =
12443 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12444 EXPR_WFL_LINECOL (wfl) = lloc;
12445 invok = build_method_invocation (name, args);
12446 return make_qualified_primary (wfl, invok, rloc);
12449 /* Build an incomplete CALL_EXPR node. */
12451 static tree
12452 build_method_invocation (name, args)
12453 tree name;
12454 tree args;
12456 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12457 TREE_SIDE_EFFECTS (call) = 1;
12458 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12459 return call;
12462 /* Build an incomplete new xxx(...) node. */
12464 static tree
12465 build_new_invocation (name, args)
12466 tree name, args;
12468 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12469 TREE_SIDE_EFFECTS (call) = 1;
12470 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12471 return call;
12474 /* Build an incomplete assignment expression. */
12476 static tree
12477 build_assignment (op, op_location, lhs, rhs)
12478 int op, op_location;
12479 tree lhs, rhs;
12481 tree assignment;
12482 /* Build the corresponding binop if we deal with a Compound
12483 Assignment operator. Mark the binop sub-tree as part of a
12484 Compound Assignment expression */
12485 if (op != ASSIGN_TK)
12487 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12488 COMPOUND_ASSIGN_P (rhs) = 1;
12490 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12491 TREE_SIDE_EFFECTS (assignment) = 1;
12492 EXPR_WFL_LINECOL (assignment) = op_location;
12493 return assignment;
12496 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12498 char *
12499 print_int_node (node)
12500 tree node;
12502 static char buffer [80];
12503 if (TREE_CONSTANT_OVERFLOW (node))
12504 sprintf (buffer, "<overflow>");
12506 if (TREE_INT_CST_HIGH (node) == 0)
12507 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12508 TREE_INT_CST_LOW (node));
12509 else if (TREE_INT_CST_HIGH (node) == -1
12510 && TREE_INT_CST_LOW (node) != 0)
12512 buffer [0] = '-';
12513 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12514 -TREE_INT_CST_LOW (node));
12516 else
12517 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12518 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12520 return buffer;
12525 /* This section of the code handle assignment check with FINAL
12526 variables. */
12528 static void
12529 reset_static_final_variable_assignment_flag (class)
12530 tree class;
12532 tree field;
12533 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12534 if (CLASS_FINAL_VARIABLE_P (field))
12535 DECL_FIELD_FINAL_LIIC (field) = 0;
12538 /* Figure whether all final static variable have been initialized. */
12540 static void
12541 check_static_final_variable_assignment_flag (class)
12542 tree class;
12544 tree field;
12546 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12547 if (CLASS_FINAL_VARIABLE_P (field)
12548 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12549 parse_error_context
12550 (DECL_FIELD_FINAL_WFL (field),
12551 "Blank static final variable `%s' may not have been initialized",
12552 IDENTIFIER_POINTER (DECL_NAME (field)));
12555 /* This function marks all final variable locally unassigned. */
12557 static void
12558 reset_final_variable_local_assignment_flag (class)
12559 tree class;
12561 tree field;
12562 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12563 if (FINAL_VARIABLE_P (field))
12564 DECL_FIELD_FINAL_LIIC (field) = 0;
12567 /* Figure whether all final variables have beem initialized in MDECL
12568 and mark MDECL accordingly. */
12570 static void
12571 check_final_variable_local_assignment_flag (class, mdecl)
12572 tree class;
12573 tree mdecl;
12575 tree field;
12576 int initialized = 0;
12577 int non_initialized = 0;
12579 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12580 return;
12582 /* First find out whether all final variables or no final variable
12583 are initialized in this ctor. We don't take into account final
12584 variable that have been initialized upon declaration. */
12585 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12586 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12588 if (DECL_FIELD_FINAL_LIIC (field))
12589 initialized++;
12590 else
12591 non_initialized++;
12594 /* There were no non initialized variable and no initialized variable.
12595 This ctor is fine. */
12596 if (!non_initialized && !initialized)
12597 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12598 /* If no variables have been initialized, that fine. We'll check
12599 later whether this ctor calls a constructor which initializes
12600 them. We mark the ctor as not initializing all its finals. */
12601 else if (initialized == 0)
12602 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12603 /* If we have a mixed bag, then we have a problem. We need to report
12604 all the variables we're not initializing. */
12605 else if (initialized && non_initialized)
12607 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12608 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12609 if (FIELD_FINAL (field)
12610 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12612 parse_error_context
12613 (lookup_cl (mdecl),
12614 "Blank final variable `%s' may not have been initialized in this constructor",
12615 IDENTIFIER_POINTER (DECL_NAME (field)));
12616 DECL_FIELD_FINAL_IERR (field) = 1;
12619 /* Otherwise we know this ctor is initializing all its final
12620 variable. We mark it so. */
12621 else
12622 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12625 /* This function recurses in a simple what through STMT and stops when
12626 it finds a constructor call. It then verifies that the called
12627 constructor initialized its final properly. Return 1 upon success,
12628 0 or -1 otherwise. */
12630 static int
12631 check_final_variable_indirect_assignment (stmt)
12632 tree stmt;
12634 int res;
12635 switch (TREE_CODE (stmt))
12637 case EXPR_WITH_FILE_LOCATION:
12638 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12639 case COMPOUND_EXPR:
12640 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12641 if (res > 0)
12642 return res;
12643 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12644 case SAVE_EXPR:
12645 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12646 case CALL_EXPR:
12648 tree decl = TREE_OPERAND (stmt, 0);
12649 tree fbody;
12651 if (TREE_CODE (decl) != FUNCTION_DECL)
12652 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12653 if (TREE_CODE (decl) != FUNCTION_DECL)
12654 abort ();
12655 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12656 return 1;
12657 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12658 return -1;
12659 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12660 if (fbody == error_mark_node)
12661 return -1;
12662 fbody = BLOCK_EXPR_BODY (fbody);
12663 return check_final_variable_indirect_assignment (fbody);
12665 default:
12666 break;
12668 return 0;
12671 /* This is the last chance to catch a final variable initialization
12672 problem. This routine will report an error if a final variable was
12673 never (globally) initialized and never reported as not having been
12674 initialized properly. */
12676 static void
12677 check_final_variable_global_assignment_flag (class)
12678 tree class;
12680 tree field, mdecl;
12681 int nnctor = 0;
12682 int error_found = 0;
12684 /* We go through all natural ctors and see whether they're
12685 initializing all their final variables or not. */
12686 current_function_decl = NULL_TREE; /* For the error report. */
12687 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12688 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12690 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12692 /* It doesn't. Maybe it calls a constructor that initializes
12693 them. find out. */
12694 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12695 if (fbody == error_mark_node)
12696 continue;
12697 fbody = BLOCK_EXPR_BODY (fbody);
12698 if (check_final_variable_indirect_assignment (fbody) == 1)
12700 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12701 nnctor++;
12703 else
12705 parse_error_context
12706 (lookup_cl (mdecl),
12707 "Final variable initialization error in this constructor");
12708 error_found = 1;
12711 else
12712 nnctor++;
12715 /* Finally we catch final variables that never were initialized */
12716 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12717 if (FINAL_VARIABLE_P (field)
12718 /* If the field wasn't initialized upon declaration */
12719 && !DECL_FIELD_FINAL_IUD (field)
12720 /* There wasn't a natural ctor in which the field could have been
12721 initialized or we found an error looking for one. */
12722 && (error_found || !nnctor)
12723 /* If we never reported a problem with this field */
12724 && !DECL_FIELD_FINAL_IERR (field))
12726 current_function_decl = NULL;
12727 parse_error_context
12728 (DECL_FIELD_FINAL_WFL (field),
12729 "Final variable `%s' hasn't been initialized upon its declaration",
12730 IDENTIFIER_POINTER (DECL_NAME (field)));
12734 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12735 context. */
12737 static int
12738 check_final_assignment (lvalue, wfl)
12739 tree lvalue, wfl;
12741 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12742 return 0;
12744 if (TREE_CODE (lvalue) == COMPONENT_REF
12745 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12746 lvalue = TREE_OPERAND (lvalue, 1);
12748 if (!FIELD_FINAL (lvalue))
12749 return 0;
12751 /* Now the logic. We can modify a final VARIABLE:
12752 1) in finit$, (its declaration was followed by an initialization,)
12753 2) consistently in each natural ctor, if it wasn't initialized in
12754 finit$ or once in <clinit>. In any other cases, an error should be
12755 reported. */
12756 if (DECL_FINIT_P (current_function_decl))
12758 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12759 return 0;
12762 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12763 /* Only if it wasn't given a value upon initialization */
12764 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12765 /* If it was never assigned a value in this constructor */
12766 && !DECL_FIELD_FINAL_LIIC (lvalue))
12768 /* Turn the locally assigned flag on, it will be checked later
12769 on to point out at discrepancies. */
12770 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12771 if (DECL_CLINIT_P (current_function_decl))
12772 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12773 return 0;
12776 /* Other problems should be reported right away. */
12777 parse_error_context
12778 (wfl, "Can't %sassign a value to the final variable `%s'",
12779 (FIELD_STATIC (lvalue) ? "re" : ""),
12780 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12782 /* Note that static field can be initialized once and only once. */
12783 if (FIELD_STATIC (lvalue))
12784 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12786 return 1;
12789 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12790 read. This is needed to avoid circularities in the implementation
12791 of these fields in libjava. */
12793 static tree
12794 maybe_build_primttype_type_ref (rhs, wfl)
12795 tree rhs, wfl;
12797 tree to_return = NULL_TREE;
12798 tree rhs_type = TREE_TYPE (rhs);
12799 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12801 tree n = TREE_OPERAND (rhs, 1);
12802 if (TREE_CODE (n) == VAR_DECL
12803 && DECL_NAME (n) == TYPE_identifier_node
12804 && rhs_type == class_ptr_type
12805 && TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION
12806 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
12808 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
12809 if (!strncmp (self_name, "java.lang.", 10))
12810 to_return = build_primtype_type_ref (self_name);
12813 return (to_return ? to_return : rhs );
12816 /* 15.25 Assignment operators. */
12818 static tree
12819 patch_assignment (node, wfl_op1, wfl_op2)
12820 tree node;
12821 tree wfl_op1;
12822 tree wfl_op2;
12824 tree rhs = TREE_OPERAND (node, 1);
12825 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12826 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12827 int error_found = 0;
12828 int lvalue_from_array = 0;
12830 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12832 /* Lhs can be a named variable */
12833 if (JDECL_P (lvalue))
12835 lhs_type = TREE_TYPE (lvalue);
12837 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12838 comment on reason why */
12839 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12841 lhs_type = TREE_TYPE (lvalue);
12842 lvalue_from_array = 1;
12844 /* Or a field access */
12845 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12846 lhs_type = TREE_TYPE (lvalue);
12847 /* Or a function return slot */
12848 else if (TREE_CODE (lvalue) == RESULT_DECL)
12849 lhs_type = TREE_TYPE (lvalue);
12850 /* Otherwise, we might want to try to write into an optimized static
12851 final, this is an of a different nature, reported further on. */
12852 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12853 && resolve_expression_name (wfl_op1, &llvalue))
12855 if (!error_found && check_final_assignment (llvalue, wfl_op1))
12857 /* What we should do instead is resetting the all the flags
12858 previously set, exchange lvalue for llvalue and continue. */
12859 error_found = 1;
12860 return error_mark_node;
12862 else
12863 lhs_type = TREE_TYPE (lvalue);
12865 else
12867 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12868 error_found = 1;
12871 rhs_type = TREE_TYPE (rhs);
12872 /* 5.1 Try the assignment conversion for builtin type. */
12873 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12875 /* 5.2 If it failed, try a reference conversion */
12876 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12877 lhs_type = promote_type (rhs_type);
12879 /* 15.25.2 If we have a compound assignment, convert RHS into the
12880 type of the LHS */
12881 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12882 new_rhs = convert (lhs_type, rhs);
12884 /* Explicit cast required. This is an error */
12885 if (!new_rhs)
12887 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12888 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12889 tree wfl;
12890 char operation [32]; /* Max size known */
12892 /* If the assignment is part of a declaration, we use the WFL of
12893 the declared variable to point out the error and call it a
12894 declaration problem. If the assignment is a genuine =
12895 operator, we call is a operator `=' problem, otherwise we
12896 call it an assignment problem. In both of these last cases,
12897 we use the WFL of the operator to indicate the error. */
12899 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12901 wfl = wfl_op1;
12902 strcpy (operation, "declaration");
12904 else
12906 wfl = wfl_operator;
12907 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12908 strcpy (operation, "assignment");
12909 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12910 strcpy (operation, "`return'");
12911 else
12912 strcpy (operation, "`='");
12915 if (!valid_cast_to_p (rhs_type, lhs_type))
12916 parse_error_context
12917 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12918 operation, t1, t2);
12919 else
12920 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12921 operation, t1, t2);
12922 free (t1); free (t2);
12923 error_found = 1;
12926 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12927 if (new_rhs)
12928 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
12930 if (error_found)
12931 return error_mark_node;
12933 /* 10.10: Array Store Exception runtime check */
12934 if (!flag_emit_class_files
12935 && !flag_emit_xref
12936 && lvalue_from_array
12937 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12939 tree check;
12940 tree base = lvalue;
12942 /* We need to retrieve the right argument for
12943 _Jv_CheckArrayStore. This is somewhat complicated by bounds
12944 and null pointer checks, both of which wrap the operand in
12945 one layer of COMPOUND_EXPR. */
12946 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12947 base = TREE_OPERAND (lvalue, 0);
12948 else
12950 tree op = TREE_OPERAND (base, 0);
12952 /* We can have a SAVE_EXPR here when doing String +=. */
12953 if (TREE_CODE (op) == SAVE_EXPR)
12954 op = TREE_OPERAND (op, 0);
12955 if (flag_bounds_check)
12956 base = TREE_OPERAND (TREE_OPERAND (op, 1), 0);
12957 else
12958 base = TREE_OPERAND (op, 0);
12961 /* Build the invocation of _Jv_CheckArrayStore */
12962 new_rhs = save_expr (new_rhs);
12963 check = build (CALL_EXPR, void_type_node,
12964 build_address_of (soft_checkarraystore_node),
12965 tree_cons (NULL_TREE, base,
12966 build_tree_list (NULL_TREE, new_rhs)),
12967 NULL_TREE);
12968 TREE_SIDE_EFFECTS (check) = 1;
12970 /* We have to decide on an insertion point */
12971 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12973 tree t;
12974 if (flag_bounds_check)
12976 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12977 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12978 build (COMPOUND_EXPR, void_type_node, t, check);
12980 else
12981 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12982 check, TREE_OPERAND (lvalue, 1));
12984 else if (flag_bounds_check)
12986 tree hook = lvalue;
12987 tree compound = TREE_OPERAND (lvalue, 0);
12988 tree bound_check, new_compound;
12990 if (TREE_CODE (compound) == SAVE_EXPR)
12992 compound = TREE_OPERAND (compound, 0);
12993 hook = TREE_OPERAND (hook, 0);
12996 /* Find the array bound check, hook the original array access. */
12997 bound_check = TREE_OPERAND (compound, 0);
12998 TREE_OPERAND (hook, 0) = TREE_OPERAND (compound, 1);
13000 /* Make sure the bound check will happen before the store check */
13001 new_compound =
13002 build (COMPOUND_EXPR, void_type_node, bound_check, check);
13004 /* Re-assemble the augmented array access. */
13005 lvalue = build (COMPOUND_EXPR, lhs_type, new_compound, lvalue);
13007 else
13008 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
13011 /* Final locals can be used as case values in switch
13012 statement. Prepare them for this eventuality. */
13013 if (TREE_CODE (lvalue) == VAR_DECL
13014 && LOCAL_FINAL_P (lvalue)
13015 && TREE_CONSTANT (new_rhs)
13016 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
13017 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
13020 TREE_CONSTANT (lvalue) = 1;
13021 DECL_INITIAL (lvalue) = new_rhs;
13024 TREE_OPERAND (node, 0) = lvalue;
13025 TREE_OPERAND (node, 1) = new_rhs;
13026 TREE_TYPE (node) = lhs_type;
13027 return node;
13030 /* Check that type SOURCE can be cast into type DEST. If the cast
13031 can't occur at all, return 0 otherwise 1. This function is used to
13032 produce accurate error messages on the reasons why an assignment
13033 failed. */
13035 static tree
13036 try_reference_assignconv (lhs_type, rhs)
13037 tree lhs_type, rhs;
13039 tree new_rhs = NULL_TREE;
13040 tree rhs_type = TREE_TYPE (rhs);
13042 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
13044 /* `null' may be assigned to any reference type */
13045 if (rhs == null_pointer_node)
13046 new_rhs = null_pointer_node;
13047 /* Try the reference assignment conversion */
13048 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
13049 new_rhs = rhs;
13050 /* This is a magic assignment that we process differently */
13051 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
13052 new_rhs = rhs;
13054 return new_rhs;
13057 /* Check that RHS can be converted into LHS_TYPE by the assignment
13058 conversion (5.2), for the cases of RHS being a builtin type. Return
13059 NULL_TREE if the conversion fails or if because RHS isn't of a
13060 builtin type. Return a converted RHS if the conversion is possible. */
13062 static tree
13063 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
13064 tree wfl_op1, lhs_type, rhs;
13066 tree new_rhs = NULL_TREE;
13067 tree rhs_type = TREE_TYPE (rhs);
13069 /* Handle boolean specially. */
13070 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13071 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13073 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13074 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13075 new_rhs = rhs;
13078 /* Zero accepted everywhere */
13079 else if (TREE_CODE (rhs) == INTEGER_CST
13080 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
13081 && JPRIMITIVE_TYPE_P (rhs_type))
13082 new_rhs = convert (lhs_type, rhs);
13084 /* 5.1.1 Try Identity Conversion,
13085 5.1.2 Try Widening Primitive Conversion */
13086 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
13087 new_rhs = convert (lhs_type, rhs);
13089 /* Try a narrowing primitive conversion (5.1.3):
13090 - expression is a constant expression of type int AND
13091 - variable is byte, short or char AND
13092 - The value of the expression is representable in the type of the
13093 variable */
13094 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
13095 && (lhs_type == byte_type_node || lhs_type == char_type_node
13096 || lhs_type == short_type_node))
13098 if (int_fits_type_p (rhs, lhs_type))
13099 new_rhs = convert (lhs_type, rhs);
13100 else if (wfl_op1) /* Might be called with a NULL */
13101 parse_warning_context
13102 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
13103 print_int_node (rhs), lang_printable_name (lhs_type, 0));
13104 /* Reported a warning that will turn into an error further
13105 down, so we don't return */
13108 return new_rhs;
13111 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
13112 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
13113 0 is the conversion test fails. This implements parts the method
13114 invocation convertion (5.3). */
13116 static int
13117 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
13118 tree lhs_type, rhs_type;
13120 /* 5.1.1: This is the identity conversion part. */
13121 if (lhs_type == rhs_type)
13122 return 1;
13124 /* Reject non primitive types and boolean conversions. */
13125 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
13126 return 0;
13128 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
13129 than a char can't be converted into a char. Short can't too, but
13130 the < test below takes care of that */
13131 if (lhs_type == char_type_node && rhs_type == byte_type_node)
13132 return 0;
13134 /* Accept all promoted type here. Note, we can't use <= in the test
13135 below, because we still need to bounce out assignments of short
13136 to char and the likes */
13137 if (lhs_type == int_type_node
13138 && (rhs_type == promoted_byte_type_node
13139 || rhs_type == promoted_short_type_node
13140 || rhs_type == promoted_char_type_node
13141 || rhs_type == promoted_boolean_type_node))
13142 return 1;
13144 /* From here, an integral is widened if its precision is smaller
13145 than the precision of the LHS or if the LHS is a floating point
13146 type, or the RHS is a float and the RHS a double. */
13147 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13148 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13149 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13150 || (rhs_type == float_type_node && lhs_type == double_type_node))
13151 return 1;
13153 return 0;
13156 /* Check that something of SOURCE type can be assigned or cast to
13157 something of DEST type at runtime. Return 1 if the operation is
13158 valid, 0 otherwise. If CAST is set to 1, we're treating the case
13159 were SOURCE is cast into DEST, which borrows a lot of the
13160 assignment check. */
13162 static int
13163 valid_ref_assignconv_cast_p (source, dest, cast)
13164 tree source;
13165 tree dest;
13166 int cast;
13168 /* SOURCE or DEST might be null if not from a declared entity. */
13169 if (!source || !dest)
13170 return 0;
13171 if (JNULLP_TYPE_P (source))
13172 return 1;
13173 if (TREE_CODE (source) == POINTER_TYPE)
13174 source = TREE_TYPE (source);
13175 if (TREE_CODE (dest) == POINTER_TYPE)
13176 dest = TREE_TYPE (dest);
13178 /* If source and dest are being compiled from bytecode, they may need to
13179 be loaded. */
13180 if (CLASS_P (source) && !CLASS_LOADED_P (source))
13182 load_class (source, 1);
13183 safe_layout_class (source);
13185 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13187 load_class (dest, 1);
13188 safe_layout_class (dest);
13191 /* Case where SOURCE is a class type */
13192 if (TYPE_CLASS_P (source))
13194 if (TYPE_CLASS_P (dest))
13195 return (source == dest
13196 || inherits_from_p (source, dest)
13197 || (cast && inherits_from_p (dest, source)));
13198 if (TYPE_INTERFACE_P (dest))
13200 /* If doing a cast and SOURCE is final, the operation is
13201 always correct a compile time (because even if SOURCE
13202 does not implement DEST, a subclass of SOURCE might). */
13203 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13204 return 1;
13205 /* Otherwise, SOURCE must implement DEST */
13206 return interface_of_p (dest, source);
13208 /* DEST is an array, cast permited if SOURCE is of Object type */
13209 return (cast && source == object_type_node ? 1 : 0);
13211 if (TYPE_INTERFACE_P (source))
13213 if (TYPE_CLASS_P (dest))
13215 /* If not casting, DEST must be the Object type */
13216 if (!cast)
13217 return dest == object_type_node;
13218 /* We're doing a cast. The cast is always valid is class
13219 DEST is not final, otherwise, DEST must implement SOURCE */
13220 else if (!CLASS_FINAL (TYPE_NAME (dest)))
13221 return 1;
13222 else
13223 return interface_of_p (source, dest);
13225 if (TYPE_INTERFACE_P (dest))
13227 /* If doing a cast, then if SOURCE and DEST contain method
13228 with the same signature but different return type, then
13229 this is a (compile time) error */
13230 if (cast)
13232 tree method_source, method_dest;
13233 tree source_type;
13234 tree source_sig;
13235 tree source_name;
13236 for (method_source = TYPE_METHODS (source); method_source;
13237 method_source = TREE_CHAIN (method_source))
13239 source_sig =
13240 build_java_argument_signature (TREE_TYPE (method_source));
13241 source_type = TREE_TYPE (TREE_TYPE (method_source));
13242 source_name = DECL_NAME (method_source);
13243 for (method_dest = TYPE_METHODS (dest);
13244 method_dest; method_dest = TREE_CHAIN (method_dest))
13245 if (source_sig ==
13246 build_java_argument_signature (TREE_TYPE (method_dest))
13247 && source_name == DECL_NAME (method_dest)
13248 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13249 return 0;
13251 return 1;
13253 else
13254 return source == dest || interface_of_p (dest, source);
13256 else
13258 /* Array */
13259 return (cast
13260 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
13261 || (DECL_NAME (TYPE_NAME (source))
13262 == java_io_serializable)));
13265 if (TYPE_ARRAY_P (source))
13267 if (TYPE_CLASS_P (dest))
13268 return dest == object_type_node;
13269 /* Can't cast an array to an interface unless the interface is
13270 java.lang.Cloneable or java.io.Serializable. */
13271 if (TYPE_INTERFACE_P (dest))
13272 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
13273 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
13274 else /* Arrays */
13276 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13277 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13279 /* In case of severe errors, they turn out null */
13280 if (!dest_element_type || !source_element_type)
13281 return 0;
13282 if (source_element_type == dest_element_type)
13283 return 1;
13284 return valid_ref_assignconv_cast_p (source_element_type,
13285 dest_element_type, cast);
13287 return 0;
13289 return 0;
13292 static int
13293 valid_cast_to_p (source, dest)
13294 tree source;
13295 tree dest;
13297 if (TREE_CODE (source) == POINTER_TYPE)
13298 source = TREE_TYPE (source);
13299 if (TREE_CODE (dest) == POINTER_TYPE)
13300 dest = TREE_TYPE (dest);
13302 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13303 return valid_ref_assignconv_cast_p (source, dest, 1);
13305 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13306 return 1;
13308 else if (TREE_CODE (source) == BOOLEAN_TYPE
13309 && TREE_CODE (dest) == BOOLEAN_TYPE)
13310 return 1;
13312 return 0;
13315 static tree
13316 do_unary_numeric_promotion (arg)
13317 tree arg;
13319 tree type = TREE_TYPE (arg);
13320 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13321 || TREE_CODE (type) == CHAR_TYPE)
13322 arg = convert (int_type_node, arg);
13323 return arg;
13326 /* Return a non zero value if SOURCE can be converted into DEST using
13327 the method invocation conversion rule (5.3). */
13328 static int
13329 valid_method_invocation_conversion_p (dest, source)
13330 tree dest, source;
13332 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13333 && valid_builtin_assignconv_identity_widening_p (dest, source))
13334 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13335 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13336 && valid_ref_assignconv_cast_p (source, dest, 0)));
13339 /* Build an incomplete binop expression. */
13341 static tree
13342 build_binop (op, op_location, op1, op2)
13343 enum tree_code op;
13344 int op_location;
13345 tree op1, op2;
13347 tree binop = build (op, NULL_TREE, op1, op2);
13348 TREE_SIDE_EFFECTS (binop) = 1;
13349 /* Store the location of the operator, for better error report. The
13350 string of the operator will be rebuild based on the OP value. */
13351 EXPR_WFL_LINECOL (binop) = op_location;
13352 return binop;
13355 /* Build the string of the operator retained by NODE. If NODE is part
13356 of a compound expression, add an '=' at the end of the string. This
13357 function is called when an error needs to be reported on an
13358 operator. The string is returned as a pointer to a static character
13359 buffer. */
13361 static char *
13362 operator_string (node)
13363 tree node;
13365 #define BUILD_OPERATOR_STRING(S) \
13367 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13368 return buffer; \
13371 static char buffer [10];
13372 switch (TREE_CODE (node))
13374 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13375 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13376 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13377 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13378 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13379 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13380 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13381 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13382 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13383 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13384 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13385 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13386 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13387 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13388 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13389 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13390 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13391 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13392 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13393 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13394 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13395 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13396 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13397 case PREINCREMENT_EXPR: /* Fall through */
13398 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13399 case PREDECREMENT_EXPR: /* Fall through */
13400 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13401 default:
13402 internal_error ("unregistered operator %s",
13403 tree_code_name [TREE_CODE (node)]);
13405 return NULL;
13406 #undef BUILD_OPERATOR_STRING
13409 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13411 static int
13412 java_decl_equiv (var_acc1, var_acc2)
13413 tree var_acc1, var_acc2;
13415 if (JDECL_P (var_acc1))
13416 return (var_acc1 == var_acc2);
13418 return (TREE_CODE (var_acc1) == COMPONENT_REF
13419 && TREE_CODE (var_acc2) == COMPONENT_REF
13420 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13421 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13422 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13425 /* Return a non zero value if CODE is one of the operators that can be
13426 used in conjunction with the `=' operator in a compound assignment. */
13428 static int
13429 binop_compound_p (code)
13430 enum tree_code code;
13432 int i;
13433 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13434 if (binop_lookup [i] == code)
13435 break;
13437 return i < BINOP_COMPOUND_CANDIDATES;
13440 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13442 static tree
13443 java_refold (t)
13444 tree t;
13446 tree c, b, ns, decl;
13448 if (TREE_CODE (t) != MODIFY_EXPR)
13449 return t;
13451 c = TREE_OPERAND (t, 1);
13452 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13453 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13454 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13455 return t;
13457 /* Now the left branch of the binary operator. */
13458 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13459 if (! (b && TREE_CODE (b) == NOP_EXPR
13460 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13461 return t;
13463 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13464 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13465 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13466 return t;
13468 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13469 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13470 /* It's got to be the an equivalent decl */
13471 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13473 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13474 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13475 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13476 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13477 /* Change the right part of the BINOP_EXPR */
13478 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13481 return t;
13484 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13485 errors but we modify NODE so that it contains the type computed
13486 according to the expression, when it's fixed. Otherwise, we write
13487 error_mark_node as the type. It allows us to further the analysis
13488 of remaining nodes and detects more errors in certain cases. */
13490 static tree
13491 patch_binop (node, wfl_op1, wfl_op2)
13492 tree node;
13493 tree wfl_op1;
13494 tree wfl_op2;
13496 tree op1 = TREE_OPERAND (node, 0);
13497 tree op2 = TREE_OPERAND (node, 1);
13498 tree op1_type = TREE_TYPE (op1);
13499 tree op2_type = TREE_TYPE (op2);
13500 tree prom_type = NULL_TREE, cn;
13501 enum tree_code code = TREE_CODE (node);
13503 /* If 1, tell the routine that we have to return error_mark_node
13504 after checking for the initialization of the RHS */
13505 int error_found = 0;
13507 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13509 /* If either op<n>_type are NULL, this might be early signs of an
13510 error situation, unless it's too early to tell (in case we're
13511 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13512 correctly so the error can be later on reported accurately. */
13513 if (! (code == PLUS_EXPR || code == NE_EXPR
13514 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13516 tree n;
13517 if (! op1_type)
13519 n = java_complete_tree (op1);
13520 op1_type = TREE_TYPE (n);
13522 if (! op2_type)
13524 n = java_complete_tree (op2);
13525 op2_type = TREE_TYPE (n);
13529 switch (code)
13531 /* 15.16 Multiplicative operators */
13532 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13533 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13534 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13535 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13536 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13538 if (!JNUMERIC_TYPE_P (op1_type))
13539 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13540 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13541 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13542 TREE_TYPE (node) = error_mark_node;
13543 error_found = 1;
13544 break;
13546 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13548 /* Detect integral division by zero */
13549 if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13550 && TREE_CODE (prom_type) == INTEGER_TYPE
13551 && (op2 == integer_zero_node || op2 == long_zero_node ||
13552 (TREE_CODE (op2) == INTEGER_CST &&
13553 ! TREE_INT_CST_LOW (op2) && ! TREE_INT_CST_HIGH (op2))))
13555 parse_warning_context (wfl_operator, "Evaluating this expression will result in an arithmetic exception being thrown.");
13556 TREE_CONSTANT (node) = 0;
13559 /* Change the division operator if necessary */
13560 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13561 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13563 /* Before divisions as is disapear, try to simplify and bail if
13564 applicable, otherwise we won't perform even simple
13565 simplifications like (1-1)/3. We can't do that with floating
13566 point number, folds can't handle them at this stage. */
13567 if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13568 && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13570 TREE_TYPE (node) = prom_type;
13571 node = fold (node);
13572 if (TREE_CODE (node) != code)
13573 return node;
13576 if (TREE_CODE (prom_type) == INTEGER_TYPE
13577 && flag_use_divide_subroutine
13578 && ! flag_emit_class_files
13579 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13580 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13582 /* This one is more complicated. FLOATs are processed by a
13583 function call to soft_fmod. Duplicate the value of the
13584 COMPOUND_ASSIGN_P flag. */
13585 if (code == TRUNC_MOD_EXPR)
13587 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13588 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13589 TREE_SIDE_EFFECTS (mod)
13590 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13591 return mod;
13593 break;
13595 /* 15.17 Additive Operators */
13596 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13598 /* Operation is valid if either one argument is a string
13599 constant, a String object or a StringBuffer crafted for the
13600 purpose of the a previous usage of the String concatenation
13601 operator */
13603 if (TREE_CODE (op1) == STRING_CST
13604 || TREE_CODE (op2) == STRING_CST
13605 || JSTRING_TYPE_P (op1_type)
13606 || JSTRING_TYPE_P (op2_type)
13607 || IS_CRAFTED_STRING_BUFFER_P (op1)
13608 || IS_CRAFTED_STRING_BUFFER_P (op2))
13609 return build_string_concatenation (op1, op2);
13611 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13612 Numeric Types */
13613 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13615 if (!JNUMERIC_TYPE_P (op1_type))
13616 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13617 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13618 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13619 TREE_TYPE (node) = error_mark_node;
13620 error_found = 1;
13621 break;
13623 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13624 break;
13626 /* 15.18 Shift Operators */
13627 case LSHIFT_EXPR:
13628 case RSHIFT_EXPR:
13629 case URSHIFT_EXPR:
13630 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13632 if (!JINTEGRAL_TYPE_P (op1_type))
13633 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13634 else
13636 if (JNUMERIC_TYPE_P (op2_type))
13637 parse_error_context (wfl_operator,
13638 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13639 operator_string (node),
13640 lang_printable_name (op2_type, 0));
13641 else
13642 parse_error_context (wfl_operator,
13643 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13644 operator_string (node),
13645 lang_printable_name (op2_type, 0));
13647 TREE_TYPE (node) = error_mark_node;
13648 error_found = 1;
13649 break;
13652 /* Unary numeric promotion (5.6.1) is performed on each operand
13653 separatly */
13654 op1 = do_unary_numeric_promotion (op1);
13655 op2 = do_unary_numeric_promotion (op2);
13657 /* The type of the shift expression is the type of the promoted
13658 type of the left-hand operand */
13659 prom_type = TREE_TYPE (op1);
13661 /* Shift int only up to 0x1f and long up to 0x3f */
13662 if (prom_type == int_type_node)
13663 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13664 build_int_2 (0x1f, 0)));
13665 else
13666 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13667 build_int_2 (0x3f, 0)));
13669 /* The >>> operator is a >> operating on unsigned quantities */
13670 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13672 tree to_return;
13673 tree utype = unsigned_type (prom_type);
13674 op1 = convert (utype, op1);
13675 TREE_SET_CODE (node, RSHIFT_EXPR);
13676 TREE_OPERAND (node, 0) = op1;
13677 TREE_OPERAND (node, 1) = op2;
13678 TREE_TYPE (node) = utype;
13679 to_return = convert (prom_type, node);
13680 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13681 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13682 TREE_SIDE_EFFECTS (to_return)
13683 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13684 return to_return;
13686 break;
13688 /* 15.19.1 Type Comparison Operator instaceof */
13689 case INSTANCEOF_EXPR:
13691 TREE_TYPE (node) = boolean_type_node;
13693 if (!(op2_type = resolve_type_during_patch (op2)))
13694 return error_mark_node;
13696 /* The first operand must be a reference type or the null type */
13697 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13698 error_found = 1; /* Error reported further below */
13700 /* The second operand must be a reference type */
13701 if (!JREFERENCE_TYPE_P (op2_type))
13703 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13704 parse_error_context
13705 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13706 lang_printable_name (op2_type, 0));
13707 error_found = 1;
13710 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13712 /* If the first operand is null, the result is always false */
13713 if (op1 == null_pointer_node)
13714 return boolean_false_node;
13715 else if (flag_emit_class_files)
13717 TREE_OPERAND (node, 1) = op2_type;
13718 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13719 return node;
13721 /* Otherwise we have to invoke instance of to figure it out */
13722 else
13723 return build_instanceof (op1, op2_type);
13725 /* There is no way the expression operand can be an instance of
13726 the type operand. This is a compile time error. */
13727 else
13729 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13730 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13731 parse_error_context
13732 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13733 t1, lang_printable_name (op2_type, 0));
13734 free (t1);
13735 error_found = 1;
13738 break;
13740 /* 15.21 Bitwise and Logical Operators */
13741 case BIT_AND_EXPR:
13742 case BIT_XOR_EXPR:
13743 case BIT_IOR_EXPR:
13744 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13745 /* Binary numeric promotion is performed on both operand and the
13746 expression retain that type */
13747 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13749 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13750 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13751 /* The type of the bitwise operator expression is BOOLEAN */
13752 prom_type = boolean_type_node;
13753 else
13755 if (!JINTEGRAL_TYPE_P (op1_type))
13756 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13757 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13758 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13759 TREE_TYPE (node) = error_mark_node;
13760 error_found = 1;
13761 /* Insert a break here if adding thing before the switch's
13762 break for this case */
13764 break;
13766 /* 15.22 Conditional-And Operator */
13767 case TRUTH_ANDIF_EXPR:
13768 /* 15.23 Conditional-Or Operator */
13769 case TRUTH_ORIF_EXPR:
13770 /* Operands must be of BOOLEAN type */
13771 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13772 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13774 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13775 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13776 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13777 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13778 TREE_TYPE (node) = boolean_type_node;
13779 error_found = 1;
13780 break;
13782 /* The type of the conditional operators is BOOLEAN */
13783 prom_type = boolean_type_node;
13784 break;
13786 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13787 case LT_EXPR:
13788 case GT_EXPR:
13789 case LE_EXPR:
13790 case GE_EXPR:
13791 /* The type of each of the operands must be a primitive numeric
13792 type */
13793 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13795 if (!JNUMERIC_TYPE_P (op1_type))
13796 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13797 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13798 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13799 TREE_TYPE (node) = boolean_type_node;
13800 error_found = 1;
13801 break;
13803 /* Binary numeric promotion is performed on the operands */
13804 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13805 /* The type of the relation expression is always BOOLEAN */
13806 prom_type = boolean_type_node;
13807 break;
13809 /* 15.20 Equality Operator */
13810 case EQ_EXPR:
13811 case NE_EXPR:
13812 /* It's time for us to patch the strings. */
13813 if ((cn = patch_string (op1)))
13815 op1 = cn;
13816 op1_type = TREE_TYPE (op1);
13818 if ((cn = patch_string (op2)))
13820 op2 = cn;
13821 op2_type = TREE_TYPE (op2);
13824 /* 15.20.1 Numerical Equality Operators == and != */
13825 /* Binary numeric promotion is performed on the operands */
13826 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13827 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13829 /* 15.20.2 Boolean Equality Operators == and != */
13830 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13831 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13832 ; /* Nothing to do here */
13834 /* 15.20.3 Reference Equality Operators == and != */
13835 /* Types have to be either references or the null type. If
13836 they're references, it must be possible to convert either
13837 type to the other by casting conversion. */
13838 else if (op1 == null_pointer_node || op2 == null_pointer_node
13839 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13840 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13841 || valid_ref_assignconv_cast_p (op2_type,
13842 op1_type, 1))))
13843 ; /* Nothing to do here */
13845 /* Else we have an error figure what can't be converted into
13846 what and report the error */
13847 else
13849 char *t1;
13850 t1 = xstrdup (lang_printable_name (op1_type, 0));
13851 parse_error_context
13852 (wfl_operator,
13853 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13854 operator_string (node), t1,
13855 lang_printable_name (op2_type, 0));
13856 free (t1);
13857 TREE_TYPE (node) = boolean_type_node;
13858 error_found = 1;
13859 break;
13861 prom_type = boolean_type_node;
13862 break;
13863 default:
13864 abort ();
13867 if (error_found)
13868 return error_mark_node;
13870 TREE_OPERAND (node, 0) = op1;
13871 TREE_OPERAND (node, 1) = op2;
13872 TREE_TYPE (node) = prom_type;
13873 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13875 if (flag_emit_xref)
13876 return node;
13878 /* fold does not respect side-effect order as required for Java but not C.
13879 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13880 * bytecode.
13882 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13883 : ! TREE_SIDE_EFFECTS (node))
13884 node = fold (node);
13885 return node;
13888 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13889 zero value, the value of CSTE comes after the valude of STRING */
13891 static tree
13892 do_merge_string_cste (cste, string, string_len, after)
13893 tree cste;
13894 const char *string;
13895 int string_len, after;
13897 const char *old = TREE_STRING_POINTER (cste);
13898 int old_len = TREE_STRING_LENGTH (cste);
13899 int len = old_len + string_len;
13900 char *new = alloca (len+1);
13902 if (after)
13904 memcpy (new, string, string_len);
13905 memcpy (&new [string_len], old, old_len);
13907 else
13909 memcpy (new, old, old_len);
13910 memcpy (&new [old_len], string, string_len);
13912 new [len] = '\0';
13913 return build_string (len, new);
13916 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13917 new STRING_CST on success, NULL_TREE on failure */
13919 static tree
13920 merge_string_cste (op1, op2, after)
13921 tree op1, op2;
13922 int after;
13924 /* Handle two string constants right away */
13925 if (TREE_CODE (op2) == STRING_CST)
13926 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13927 TREE_STRING_LENGTH (op2), after);
13929 /* Reasonable integer constant can be treated right away */
13930 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13932 static const char *const boolean_true = "true";
13933 static const char *const boolean_false = "false";
13934 static const char *const null_pointer = "null";
13935 char ch[3];
13936 const char *string;
13938 if (op2 == boolean_true_node)
13939 string = boolean_true;
13940 else if (op2 == boolean_false_node)
13941 string = boolean_false;
13942 else if (op2 == null_pointer_node)
13943 string = null_pointer;
13944 else if (TREE_TYPE (op2) == char_type_node)
13946 ch[0] = (char )TREE_INT_CST_LOW (op2);
13947 ch[1] = '\0';
13948 string = ch;
13950 else
13951 string = print_int_node (op2);
13953 return do_merge_string_cste (op1, string, strlen (string), after);
13955 return NULL_TREE;
13958 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13959 has to be a STRING_CST and the other part must be a STRING_CST or a
13960 INTEGRAL constant. Return a new STRING_CST if the operation
13961 succeed, NULL_TREE otherwise.
13963 If the case we want to optimize for space, we might want to return
13964 NULL_TREE for each invocation of this routine. FIXME */
13966 static tree
13967 string_constant_concatenation (op1, op2)
13968 tree op1, op2;
13970 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13972 tree string, rest;
13973 int invert;
13975 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13976 rest = (string == op1 ? op2 : op1);
13977 invert = (string == op1 ? 0 : 1 );
13979 /* Walk REST, only if it looks reasonable */
13980 if (TREE_CODE (rest) != STRING_CST
13981 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13982 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13983 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13985 rest = java_complete_tree (rest);
13986 if (rest == error_mark_node)
13987 return error_mark_node;
13988 rest = fold (rest);
13990 return merge_string_cste (string, rest, invert);
13992 return NULL_TREE;
13995 /* Implement the `+' operator. Does static optimization if possible,
13996 otherwise create (if necessary) and append elements to a
13997 StringBuffer. The StringBuffer will be carried around until it is
13998 used for a function call or an assignment. Then toString() will be
13999 called on it to turn it into a String object. */
14001 static tree
14002 build_string_concatenation (op1, op2)
14003 tree op1, op2;
14005 tree result;
14006 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
14008 if (flag_emit_xref)
14009 return build (PLUS_EXPR, string_type_node, op1, op2);
14011 /* Try to do some static optimization */
14012 if ((result = string_constant_concatenation (op1, op2)))
14013 return result;
14015 /* Discard empty strings on either side of the expression */
14016 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
14018 op1 = op2;
14019 op2 = NULL_TREE;
14021 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
14022 op2 = NULL_TREE;
14024 /* If operands are string constant, turn then into object references */
14025 if (TREE_CODE (op1) == STRING_CST)
14026 op1 = patch_string_cst (op1);
14027 if (op2 && TREE_CODE (op2) == STRING_CST)
14028 op2 = patch_string_cst (op2);
14030 /* If either one of the constant is null and the other non null
14031 operand is a String object, return it. */
14032 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
14033 return op1;
14035 /* If OP1 isn't already a StringBuffer, create and
14036 initialize a new one */
14037 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
14039 /* Two solutions here:
14040 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
14041 2) OP1 is something else, we call new StringBuffer().append(OP1). */
14042 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
14043 op1 = BUILD_STRING_BUFFER (op1);
14044 else
14046 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
14047 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
14051 if (op2)
14053 /* OP1 is no longer the last node holding a crafted StringBuffer */
14054 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
14055 /* Create a node for `{new...,xxx}.append (op2)' */
14056 if (op2)
14057 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
14060 /* Mark the last node holding a crafted StringBuffer */
14061 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
14063 TREE_SIDE_EFFECTS (op1) = side_effects;
14064 return op1;
14067 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
14068 StringBuffer. If no string were found to be patched, return
14069 NULL. */
14071 static tree
14072 patch_string (node)
14073 tree node;
14075 if (node == error_mark_node)
14076 return error_mark_node;
14077 if (TREE_CODE (node) == STRING_CST)
14078 return patch_string_cst (node);
14079 else if (IS_CRAFTED_STRING_BUFFER_P (node))
14081 int saved = ctxp->explicit_constructor_p;
14082 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
14083 tree ret;
14084 /* Temporary disable forbid the use of `this'. */
14085 ctxp->explicit_constructor_p = 0;
14086 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
14087 /* String concatenation arguments must be evaluated in order too. */
14088 ret = force_evaluation_order (ret);
14089 /* Restore it at its previous value */
14090 ctxp->explicit_constructor_p = saved;
14091 return ret;
14093 return NULL_TREE;
14096 /* Build the internal representation of a string constant. */
14098 static tree
14099 patch_string_cst (node)
14100 tree node;
14102 int location;
14103 if (! flag_emit_class_files)
14105 node = get_identifier (TREE_STRING_POINTER (node));
14106 location = alloc_name_constant (CONSTANT_String, node);
14107 node = build_ref_from_constant_pool (location);
14109 TREE_TYPE (node) = string_ptr_type_node;
14110 TREE_CONSTANT (node) = 1;
14111 return node;
14114 /* Build an incomplete unary operator expression. */
14116 static tree
14117 build_unaryop (op_token, op_location, op1)
14118 int op_token, op_location;
14119 tree op1;
14121 enum tree_code op;
14122 tree unaryop;
14123 switch (op_token)
14125 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14126 case MINUS_TK: op = NEGATE_EXPR; break;
14127 case NEG_TK: op = TRUTH_NOT_EXPR; break;
14128 case NOT_TK: op = BIT_NOT_EXPR; break;
14129 default: abort ();
14132 unaryop = build1 (op, NULL_TREE, op1);
14133 TREE_SIDE_EFFECTS (unaryop) = 1;
14134 /* Store the location of the operator, for better error report. The
14135 string of the operator will be rebuild based on the OP value. */
14136 EXPR_WFL_LINECOL (unaryop) = op_location;
14137 return unaryop;
14140 /* Special case for the ++/-- operators, since they require an extra
14141 argument to build, which is set to NULL and patched
14142 later. IS_POST_P is 1 if the operator, 0 otherwise. */
14144 static tree
14145 build_incdec (op_token, op_location, op1, is_post_p)
14146 int op_token, op_location;
14147 tree op1;
14148 int is_post_p;
14150 static enum tree_code lookup [2][2] =
14152 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14153 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14155 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
14156 NULL_TREE, op1, NULL_TREE);
14157 TREE_SIDE_EFFECTS (node) = 1;
14158 /* Store the location of the operator, for better error report. The
14159 string of the operator will be rebuild based on the OP value. */
14160 EXPR_WFL_LINECOL (node) = op_location;
14161 return node;
14164 /* Build an incomplete cast operator, based on the use of the
14165 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14166 set. java_complete_tree is trained to walk a CONVERT_EXPR even
14167 though its type is already set. */
14169 static tree
14170 build_cast (location, type, exp)
14171 int location;
14172 tree type, exp;
14174 tree node = build1 (CONVERT_EXPR, type, exp);
14175 EXPR_WFL_LINECOL (node) = location;
14176 return node;
14179 /* Build an incomplete class reference operator. */
14180 static tree
14181 build_incomplete_class_ref (location, class_name)
14182 int location;
14183 tree class_name;
14185 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14186 EXPR_WFL_LINECOL (node) = location;
14187 return node;
14190 /* Complete an incomplete class reference operator. */
14191 static tree
14192 patch_incomplete_class_ref (node)
14193 tree node;
14195 tree type = TREE_OPERAND (node, 0);
14196 tree ref_type;
14198 if (!(ref_type = resolve_type_during_patch (type)))
14199 return error_mark_node;
14201 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
14203 tree dot = build_class_ref (ref_type);
14204 /* A class referenced by `foo.class' is initialized. */
14205 if (!flag_emit_class_files)
14206 dot = build_class_init (ref_type, dot);
14207 return java_complete_tree (dot);
14210 /* If we're emitting class files and we have to deal with non
14211 primitive types, we invoke (and consider generating) the
14212 synthetic static method `class$'. */
14213 if (!TYPE_DOT_CLASS (current_class))
14214 build_dot_class_method (current_class);
14215 ref_type = build_dot_class_method_invocation (ref_type);
14216 return java_complete_tree (ref_type);
14219 /* 15.14 Unary operators. We return error_mark_node in case of error,
14220 but preserve the type of NODE if the type is fixed. */
14222 static tree
14223 patch_unaryop (node, wfl_op)
14224 tree node;
14225 tree wfl_op;
14227 tree op = TREE_OPERAND (node, 0);
14228 tree op_type = TREE_TYPE (op);
14229 tree prom_type = NULL_TREE, value, decl;
14230 int outer_field_flag = 0;
14231 int code = TREE_CODE (node);
14232 int error_found = 0;
14234 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14236 switch (code)
14238 /* 15.13.2 Postfix Increment Operator ++ */
14239 case POSTINCREMENT_EXPR:
14240 /* 15.13.3 Postfix Increment Operator -- */
14241 case POSTDECREMENT_EXPR:
14242 /* 15.14.1 Prefix Increment Operator ++ */
14243 case PREINCREMENT_EXPR:
14244 /* 15.14.2 Prefix Decrement Operator -- */
14245 case PREDECREMENT_EXPR:
14246 op = decl = strip_out_static_field_access_decl (op);
14247 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
14248 /* We might be trying to change an outer field accessed using
14249 access method. */
14250 if (outer_field_flag)
14252 /* Retrieve the decl of the field we're trying to access. We
14253 do that by first retrieving the function we would call to
14254 access the field. It has been already verified that this
14255 field isn't final */
14256 if (flag_emit_class_files)
14257 decl = TREE_OPERAND (op, 0);
14258 else
14259 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14260 decl = DECL_FUNCTION_ACCESS_DECL (decl);
14262 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14263 else if (!JDECL_P (decl)
14264 && TREE_CODE (decl) != COMPONENT_REF
14265 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14266 && TREE_CODE (decl) != INDIRECT_REF
14267 && !(TREE_CODE (decl) == COMPOUND_EXPR
14268 && TREE_OPERAND (decl, 1)
14269 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14271 tree lvalue;
14272 /* Before screaming, check that we're not in fact trying to
14273 increment a optimized static final access, in which case
14274 we issue an different error message. */
14275 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
14276 && resolve_expression_name (wfl_op, &lvalue)
14277 && check_final_assignment (lvalue, wfl_op)))
14278 parse_error_context (wfl_operator, "Invalid argument to `%s'",
14279 operator_string (node));
14280 TREE_TYPE (node) = error_mark_node;
14281 error_found = 1;
14284 if (check_final_assignment (op, wfl_op))
14285 error_found = 1;
14287 /* From now on, we know that op if a variable and that it has a
14288 valid wfl. We use wfl_op to locate errors related to the
14289 ++/-- operand. */
14290 else if (!JNUMERIC_TYPE_P (op_type))
14292 parse_error_context
14293 (wfl_op, "Invalid argument type `%s' to `%s'",
14294 lang_printable_name (op_type, 0), operator_string (node));
14295 TREE_TYPE (node) = error_mark_node;
14296 error_found = 1;
14298 else
14300 /* Before the addition, binary numeric promotion is performed on
14301 both operands, if really necessary */
14302 if (JINTEGRAL_TYPE_P (op_type))
14304 value = build_int_2 (1, 0);
14305 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
14307 else
14309 value = build_int_2 (1, 0);
14310 TREE_TYPE (node) =
14311 binary_numeric_promotion (op_type,
14312 TREE_TYPE (value), &op, &value);
14315 /* We remember we might be accessing an outer field */
14316 if (outer_field_flag)
14318 /* We re-generate an access to the field */
14319 value = build (PLUS_EXPR, TREE_TYPE (op),
14320 build_outer_field_access (wfl_op, decl), value);
14322 /* And we patch the original access$() into a write
14323 with plus_op as a rhs */
14324 return outer_field_access_fix (node, op, value);
14327 /* And write back into the node. */
14328 TREE_OPERAND (node, 0) = op;
14329 TREE_OPERAND (node, 1) = value;
14330 /* Convert the overall back into its original type, if
14331 necessary, and return */
14332 if (JINTEGRAL_TYPE_P (op_type))
14333 return fold (node);
14334 else
14335 return fold (convert (op_type, node));
14337 break;
14339 /* 15.14.3 Unary Plus Operator + */
14340 case UNARY_PLUS_EXPR:
14341 /* 15.14.4 Unary Minus Operator - */
14342 case NEGATE_EXPR:
14343 if (!JNUMERIC_TYPE_P (op_type))
14345 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14346 TREE_TYPE (node) = error_mark_node;
14347 error_found = 1;
14349 /* Unary numeric promotion is performed on operand */
14350 else
14352 op = do_unary_numeric_promotion (op);
14353 prom_type = TREE_TYPE (op);
14354 if (code == UNARY_PLUS_EXPR)
14355 return fold (op);
14357 break;
14359 /* 15.14.5 Bitwise Complement Operator ~ */
14360 case BIT_NOT_EXPR:
14361 if (!JINTEGRAL_TYPE_P (op_type))
14363 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14364 TREE_TYPE (node) = error_mark_node;
14365 error_found = 1;
14367 else
14369 op = do_unary_numeric_promotion (op);
14370 prom_type = TREE_TYPE (op);
14372 break;
14374 /* 15.14.6 Logical Complement Operator ! */
14375 case TRUTH_NOT_EXPR:
14376 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14378 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14379 /* But the type is known. We will report an error if further
14380 attempt of a assignment is made with this rhs */
14381 TREE_TYPE (node) = boolean_type_node;
14382 error_found = 1;
14384 else
14385 prom_type = boolean_type_node;
14386 break;
14388 /* 15.15 Cast Expression */
14389 case CONVERT_EXPR:
14390 value = patch_cast (node, wfl_operator);
14391 if (value == error_mark_node)
14393 /* If this cast is part of an assignment, we tell the code
14394 that deals with it not to complain about a mismatch,
14395 because things have been cast, anyways */
14396 TREE_TYPE (node) = error_mark_node;
14397 error_found = 1;
14399 else
14401 value = fold (value);
14402 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
14403 return value;
14405 break;
14408 if (error_found)
14409 return error_mark_node;
14411 /* There are cases where node has been replaced by something else
14412 and we don't end up returning here: UNARY_PLUS_EXPR,
14413 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14414 TREE_OPERAND (node, 0) = fold (op);
14415 TREE_TYPE (node) = prom_type;
14416 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14417 return fold (node);
14420 /* Generic type resolution that sometimes takes place during node
14421 patching. Returned the resolved type or generate an error
14422 message. Return the resolved type or NULL_TREE. */
14424 static tree
14425 resolve_type_during_patch (type)
14426 tree type;
14428 if (unresolved_type_p (type, NULL))
14430 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14431 if (!type_decl)
14433 parse_error_context (type,
14434 "Class `%s' not found in type declaration",
14435 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14436 return NULL_TREE;
14438 return TREE_TYPE (type_decl);
14440 return type;
14442 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14443 found. Otherwise NODE or something meant to replace it is returned. */
14445 static tree
14446 patch_cast (node, wfl_op)
14447 tree node;
14448 tree wfl_op;
14450 tree op = TREE_OPERAND (node, 0);
14451 tree op_type = TREE_TYPE (op);
14452 tree cast_type = TREE_TYPE (node);
14453 char *t1;
14455 /* First resolve OP_TYPE if unresolved */
14456 if (!(cast_type = resolve_type_during_patch (cast_type)))
14457 return error_mark_node;
14459 /* Check on cast that are proven correct at compile time */
14460 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14462 /* Same type */
14463 if (cast_type == op_type)
14464 return node;
14466 /* float and double type are converted to the original type main
14467 variant and then to the target type. */
14468 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14469 op = convert (integer_type_node, op);
14471 /* Try widening/narowwing convertion. Potentially, things need
14472 to be worked out in gcc so we implement the extreme cases
14473 correctly. fold_convert() needs to be fixed. */
14474 return convert (cast_type, op);
14477 /* It's also valid to cast a boolean into a boolean */
14478 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14479 return node;
14481 /* null can be casted to references */
14482 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14483 return build_null_of_type (cast_type);
14485 /* The remaining legal casts involve conversion between reference
14486 types. Check for their compile time correctness. */
14487 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14488 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14490 TREE_TYPE (node) = promote_type (cast_type);
14491 /* Now, the case can be determined correct at compile time if
14492 OP_TYPE can be converted into CAST_TYPE by assignment
14493 conversion (5.2) */
14495 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14497 TREE_SET_CODE (node, NOP_EXPR);
14498 return node;
14501 if (flag_emit_class_files)
14503 TREE_SET_CODE (node, CONVERT_EXPR);
14504 return node;
14507 /* The cast requires a run-time check */
14508 return build (CALL_EXPR, promote_type (cast_type),
14509 build_address_of (soft_checkcast_node),
14510 tree_cons (NULL_TREE, build_class_ref (cast_type),
14511 build_tree_list (NULL_TREE, op)),
14512 NULL_TREE);
14515 /* Any other casts are proven incorrect at compile time */
14516 t1 = xstrdup (lang_printable_name (op_type, 0));
14517 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14518 t1, lang_printable_name (cast_type, 0));
14519 free (t1);
14520 return error_mark_node;
14523 /* Build a null constant and give it the type TYPE. */
14525 static tree
14526 build_null_of_type (type)
14527 tree type;
14529 tree node = build_int_2 (0, 0);
14530 TREE_TYPE (node) = promote_type (type);
14531 return node;
14534 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14535 a list of indices. */
14536 static tree
14537 build_array_ref (location, array, index)
14538 int location;
14539 tree array, index;
14541 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14542 EXPR_WFL_LINECOL (node) = location;
14543 return node;
14546 /* 15.12 Array Access Expression */
14548 static tree
14549 patch_array_ref (node)
14550 tree node;
14552 tree array = TREE_OPERAND (node, 0);
14553 tree array_type = TREE_TYPE (array);
14554 tree index = TREE_OPERAND (node, 1);
14555 tree index_type = TREE_TYPE (index);
14556 int error_found = 0;
14558 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14560 if (TREE_CODE (array_type) == POINTER_TYPE)
14561 array_type = TREE_TYPE (array_type);
14563 /* The array reference must be an array */
14564 if (!TYPE_ARRAY_P (array_type))
14566 parse_error_context
14567 (wfl_operator,
14568 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14569 lang_printable_name (array_type, 0));
14570 TREE_TYPE (node) = error_mark_node;
14571 error_found = 1;
14574 /* The array index undergoes unary numeric promotion. The promoted
14575 type must be int */
14576 index = do_unary_numeric_promotion (index);
14577 if (TREE_TYPE (index) != int_type_node)
14579 if (valid_cast_to_p (index_type, int_type_node))
14580 parse_error_context (wfl_operator,
14581 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14582 lang_printable_name (index_type, 0));
14583 else
14584 parse_error_context (wfl_operator,
14585 "Incompatible type for `[]'. Can't convert `%s' to `int'",
14586 lang_printable_name (index_type, 0));
14587 TREE_TYPE (node) = error_mark_node;
14588 error_found = 1;
14591 if (error_found)
14592 return error_mark_node;
14594 array_type = TYPE_ARRAY_ELEMENT (array_type);
14596 if (flag_emit_class_files || flag_emit_xref)
14598 TREE_OPERAND (node, 0) = array;
14599 TREE_OPERAND (node, 1) = index;
14601 else
14603 /* The save_expr is for correct evaluation order. It would be cleaner
14604 to use force_evaluation_order (see comment there), but that is
14605 difficult when we also have to deal with bounds checking. */
14606 if (TREE_SIDE_EFFECTS (index))
14607 array = save_expr (array);
14608 node = build_java_arrayaccess (array, array_type, index);
14609 if (TREE_SIDE_EFFECTS (index))
14610 node = build (COMPOUND_EXPR, array_type, array, node);
14612 TREE_TYPE (node) = array_type;
14613 return node;
14616 /* 15.9 Array Creation Expressions */
14618 static tree
14619 build_newarray_node (type, dims, extra_dims)
14620 tree type;
14621 tree dims;
14622 int extra_dims;
14624 tree node =
14625 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14626 build_int_2 (extra_dims, 0));
14627 return node;
14630 static tree
14631 patch_newarray (node)
14632 tree node;
14634 tree type = TREE_OPERAND (node, 0);
14635 tree dims = TREE_OPERAND (node, 1);
14636 tree cdim, array_type;
14637 int error_found = 0;
14638 int ndims = 0;
14639 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14641 /* Dimension types are verified. It's better for the types to be
14642 verified in order. */
14643 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14645 int dim_error = 0;
14646 tree dim = TREE_VALUE (cdim);
14648 /* Dim might have been saved during its evaluation */
14649 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14651 /* The type of each specified dimension must be an integral type. */
14652 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14653 dim_error = 1;
14655 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14656 promoted type must be int. */
14657 else
14659 dim = do_unary_numeric_promotion (dim);
14660 if (TREE_TYPE (dim) != int_type_node)
14661 dim_error = 1;
14664 /* Report errors on types here */
14665 if (dim_error)
14667 parse_error_context
14668 (TREE_PURPOSE (cdim),
14669 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14670 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14671 "Explicit cast needed to" : "Can't"),
14672 lang_printable_name (TREE_TYPE (dim), 0));
14673 error_found = 1;
14676 TREE_PURPOSE (cdim) = NULL_TREE;
14679 /* Resolve array base type if unresolved */
14680 if (!(type = resolve_type_during_patch (type)))
14681 error_found = 1;
14683 if (error_found)
14685 /* We don't want further evaluation of this bogus array creation
14686 operation */
14687 TREE_TYPE (node) = error_mark_node;
14688 return error_mark_node;
14691 /* Set array_type to the actual (promoted) array type of the result. */
14692 if (TREE_CODE (type) == RECORD_TYPE)
14693 type = build_pointer_type (type);
14694 while (--xdims >= 0)
14696 type = promote_type (build_java_array_type (type, -1));
14698 dims = nreverse (dims);
14699 array_type = type;
14700 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14702 type = array_type;
14703 array_type
14704 = build_java_array_type (type,
14705 TREE_CODE (cdim) == INTEGER_CST
14706 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14707 : -1);
14708 array_type = promote_type (array_type);
14710 dims = nreverse (dims);
14712 /* The node is transformed into a function call. Things are done
14713 differently according to the number of dimensions. If the number
14714 of dimension is equal to 1, then the nature of the base type
14715 (primitive or not) matters. */
14716 if (ndims == 1)
14717 return build_new_array (type, TREE_VALUE (dims));
14719 /* Can't reuse what's already written in expr.c because it uses the
14720 JVM stack representation. Provide a build_multianewarray. FIXME */
14721 return build (CALL_EXPR, array_type,
14722 build_address_of (soft_multianewarray_node),
14723 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14724 tree_cons (NULL_TREE,
14725 build_int_2 (ndims, 0), dims )),
14726 NULL_TREE);
14729 /* 10.6 Array initializer. */
14731 /* Build a wfl for array element that don't have one, so we can
14732 pin-point errors. */
14734 static tree
14735 maybe_build_array_element_wfl (node)
14736 tree node;
14738 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14739 return build_expr_wfl (NULL_TREE, ctxp->filename,
14740 ctxp->elc.line, ctxp->elc.prev_col);
14741 else
14742 return NULL_TREE;
14745 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14746 identification of initialized arrays easier to detect during walk
14747 and expansion. */
14749 static tree
14750 build_new_array_init (location, values)
14751 int location;
14752 tree values;
14754 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14755 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14756 EXPR_WFL_LINECOL (to_return) = location;
14757 return to_return;
14760 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14761 occurred. Otherwise return NODE after having set its type
14762 appropriately. */
14764 static tree
14765 patch_new_array_init (type, node)
14766 tree type, node;
14768 int error_seen = 0;
14769 tree current, element_type;
14770 HOST_WIDE_INT length;
14771 int all_constant = 1;
14772 tree init = TREE_OPERAND (node, 0);
14774 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14776 parse_error_context (node,
14777 "Invalid array initializer for non-array type `%s'",
14778 lang_printable_name (type, 1));
14779 return error_mark_node;
14781 type = TREE_TYPE (type);
14782 element_type = TYPE_ARRAY_ELEMENT (type);
14784 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14786 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14787 current; length++, current = TREE_CHAIN (current))
14789 tree elt = TREE_VALUE (current);
14790 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14792 error_seen |= array_constructor_check_entry (element_type, current);
14793 elt = TREE_VALUE (current);
14794 /* When compiling to native code, STRING_CST is converted to
14795 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14796 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14797 all_constant = 0;
14799 else
14801 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14802 TREE_PURPOSE (current) = NULL_TREE;
14803 all_constant = 0;
14805 if (elt && TREE_CODE (elt) == TREE_LIST
14806 && TREE_VALUE (elt) == error_mark_node)
14807 error_seen = 1;
14810 if (error_seen)
14811 return error_mark_node;
14813 /* Create a new type. We can't reuse the one we have here by
14814 patching its dimension because it originally is of dimension -1
14815 hence reused by gcc. This would prevent triangular arrays. */
14816 type = build_java_array_type (element_type, length);
14817 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14818 TREE_TYPE (node) = promote_type (type);
14819 TREE_CONSTANT (init) = all_constant;
14820 TREE_CONSTANT (node) = all_constant;
14821 return node;
14824 /* Verify that one entry of the initializer element list can be
14825 assigned to the array base type. Report 1 if an error occurred, 0
14826 otherwise. */
14828 static int
14829 array_constructor_check_entry (type, entry)
14830 tree type, entry;
14832 char *array_type_string = NULL; /* For error reports */
14833 tree value, type_value, new_value, wfl_value, patched;
14834 int error_seen = 0;
14836 new_value = NULL_TREE;
14837 wfl_value = TREE_VALUE (entry);
14839 value = java_complete_tree (TREE_VALUE (entry));
14840 /* patch_string return error_mark_node if arg is error_mark_node */
14841 if ((patched = patch_string (value)))
14842 value = patched;
14843 if (value == error_mark_node)
14844 return 1;
14846 type_value = TREE_TYPE (value);
14848 /* At anytime, try_builtin_assignconv can report a warning on
14849 constant overflow during narrowing. */
14850 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14851 new_value = try_builtin_assignconv (wfl_operator, type, value);
14852 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14853 type_value = promote_type (type);
14855 /* Check and report errors */
14856 if (!new_value)
14858 const char *const msg = (!valid_cast_to_p (type_value, type) ?
14859 "Can't" : "Explicit cast needed to");
14860 if (!array_type_string)
14861 array_type_string = xstrdup (lang_printable_name (type, 1));
14862 parse_error_context
14863 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14864 msg, lang_printable_name (type_value, 1), array_type_string);
14865 error_seen = 1;
14868 if (new_value)
14870 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
14871 TREE_VALUE (entry) = new_value;
14874 if (array_type_string)
14875 free (array_type_string);
14877 TREE_PURPOSE (entry) = NULL_TREE;
14878 return error_seen;
14881 static tree
14882 build_this (location)
14883 int location;
14885 tree node = build_wfl_node (this_identifier_node);
14886 TREE_SET_CODE (node, THIS_EXPR);
14887 EXPR_WFL_LINECOL (node) = location;
14888 return node;
14891 /* 14.15 The return statement. It builds a modify expression that
14892 assigns the returned value to the RESULT_DECL that hold the value
14893 to be returned. */
14895 static tree
14896 build_return (location, op)
14897 int location;
14898 tree op;
14900 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14901 EXPR_WFL_LINECOL (node) = location;
14902 node = build_debugable_stmt (location, node);
14903 return node;
14906 static tree
14907 patch_return (node)
14908 tree node;
14910 tree return_exp = TREE_OPERAND (node, 0);
14911 tree meth = current_function_decl;
14912 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14913 int error_found = 0;
14915 TREE_TYPE (node) = error_mark_node;
14916 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14918 /* It's invalid to have a return value within a function that is
14919 declared with the keyword void or that is a constructor */
14920 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14921 error_found = 1;
14923 /* It's invalid to use a return statement in a static block */
14924 if (DECL_CLINIT_P (current_function_decl))
14925 error_found = 1;
14927 /* It's invalid to have a no return value within a function that
14928 isn't declared with the keyword `void' */
14929 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14930 error_found = 2;
14932 if (DECL_INSTINIT_P (current_function_decl))
14933 error_found = 1;
14935 if (error_found)
14937 if (DECL_INSTINIT_P (current_function_decl))
14938 parse_error_context (wfl_operator,
14939 "`return' inside instance initializer");
14941 else if (DECL_CLINIT_P (current_function_decl))
14942 parse_error_context (wfl_operator,
14943 "`return' inside static initializer");
14945 else if (!DECL_CONSTRUCTOR_P (meth))
14947 char *t = xstrdup (lang_printable_name (mtype, 0));
14948 parse_error_context (wfl_operator,
14949 "`return' with%s value from `%s %s'",
14950 (error_found == 1 ? "" : "out"),
14951 t, lang_printable_name (meth, 0));
14952 free (t);
14954 else
14955 parse_error_context (wfl_operator,
14956 "`return' with value from constructor `%s'",
14957 lang_printable_name (meth, 0));
14958 return error_mark_node;
14961 /* If we have a return_exp, build a modify expression and expand
14962 it. Note: at that point, the assignment is declared valid, but we
14963 may want to carry some more hacks */
14964 if (return_exp)
14966 tree exp = java_complete_tree (return_exp);
14967 tree modify, patched;
14969 /* If the function returned value and EXP are booleans, EXP has
14970 to be converted into the type of DECL_RESULT, which is integer
14971 (see complete_start_java_method) */
14972 if (TREE_TYPE (exp) == boolean_type_node &&
14973 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14974 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14976 /* `null' can be assigned to a function returning a reference */
14977 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14978 exp == null_pointer_node)
14979 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14981 if ((patched = patch_string (exp)))
14982 exp = patched;
14984 modify = build (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;
14993 else
14994 return error_mark_node;
14996 TREE_TYPE (node) = void_type_node;
14997 TREE_SIDE_EFFECTS (node) = 1;
14998 return node;
15001 /* 14.8 The if Statement */
15003 static tree
15004 build_if_else_statement (location, expression, if_body, else_body)
15005 int location;
15006 tree expression, if_body, else_body;
15008 tree node;
15009 if (!else_body)
15010 else_body = empty_stmt_node;
15011 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
15012 EXPR_WFL_LINECOL (node) = location;
15013 node = build_debugable_stmt (location, node);
15014 return node;
15017 static tree
15018 patch_if_else_statement (node)
15019 tree node;
15021 tree expression = TREE_OPERAND (node, 0);
15023 TREE_TYPE (node) = error_mark_node;
15024 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15026 /* The type of expression must be boolean */
15027 if (TREE_TYPE (expression) != boolean_type_node
15028 && TREE_TYPE (expression) != promoted_boolean_type_node)
15030 parse_error_context
15031 (wfl_operator,
15032 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
15033 lang_printable_name (TREE_TYPE (expression), 0));
15034 return error_mark_node;
15037 TREE_TYPE (node) = void_type_node;
15038 TREE_SIDE_EFFECTS (node) = 1;
15039 CAN_COMPLETE_NORMALLY (node)
15040 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15041 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
15042 return node;
15045 /* 14.6 Labeled Statements */
15047 /* Action taken when a lableled statement is parsed. a new
15048 LABELED_BLOCK_EXPR is created. No statement is attached to the
15049 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
15051 static tree
15052 build_labeled_block (location, label)
15053 int location;
15054 tree label;
15056 tree label_name ;
15057 tree label_decl, node;
15058 if (label == NULL_TREE || label == continue_identifier_node)
15059 label_name = label;
15060 else
15062 label_name = merge_qualified_name (label_id, label);
15063 /* Issue an error if we try to reuse a label that was previously
15064 declared */
15065 if (IDENTIFIER_LOCAL_VALUE (label_name))
15067 EXPR_WFL_LINECOL (wfl_operator) = location;
15068 parse_error_context (wfl_operator,
15069 "Declaration of `%s' shadows a previous label declaration",
15070 IDENTIFIER_POINTER (label));
15071 EXPR_WFL_LINECOL (wfl_operator) =
15072 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
15073 parse_error_context (wfl_operator,
15074 "This is the location of the previous declaration of label `%s'",
15075 IDENTIFIER_POINTER (label));
15076 java_error_count--;
15080 label_decl = create_label_decl (label_name);
15081 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
15082 EXPR_WFL_LINECOL (node) = location;
15083 TREE_SIDE_EFFECTS (node) = 1;
15084 return node;
15087 /* A labeled statement LBE is attached a statement. */
15089 static tree
15090 finish_labeled_statement (lbe, statement)
15091 tree lbe; /* Labeled block expr */
15092 tree statement;
15094 /* In anyways, tie the loop to its statement */
15095 LABELED_BLOCK_BODY (lbe) = statement;
15096 pop_labeled_block ();
15097 POP_LABELED_BLOCK ();
15098 return lbe;
15101 /* 14.10, 14.11, 14.12 Loop Statements */
15103 /* Create an empty LOOP_EXPR and make it the last in the nested loop
15104 list. */
15106 static tree
15107 build_new_loop (loop_body)
15108 tree loop_body;
15110 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
15111 TREE_SIDE_EFFECTS (loop) = 1;
15112 PUSH_LOOP (loop);
15113 return loop;
15116 /* Create a loop body according to the following structure:
15117 COMPOUND_EXPR
15118 COMPOUND_EXPR (loop main body)
15119 EXIT_EXPR (this order is for while/for loops.
15120 LABELED_BLOCK_EXPR the order is reversed for do loops)
15121 LABEL_DECL (a continue occuring here branches at the
15122 BODY end of this labeled block)
15123 INCREMENT (if any)
15125 REVERSED, if non zero, tells that the loop condition expr comes
15126 after the body, like in the do-while loop.
15128 To obtain a loop, the loop body structure described above is
15129 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15131 LABELED_BLOCK_EXPR
15132 LABEL_DECL (use this label to exit the loop)
15133 LOOP_EXPR
15134 <structure described above> */
15136 static tree
15137 build_loop_body (location, condition, reversed)
15138 int location;
15139 tree condition;
15140 int reversed;
15142 tree first, second, body;
15144 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15145 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15146 condition = build_debugable_stmt (location, condition);
15147 TREE_SIDE_EFFECTS (condition) = 1;
15149 body = build_labeled_block (0, continue_identifier_node);
15150 first = (reversed ? body : condition);
15151 second = (reversed ? condition : body);
15152 return
15153 build (COMPOUND_EXPR, NULL_TREE,
15154 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
15157 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15158 their order) on the current loop. Unlink the current loop from the
15159 loop list. */
15161 static tree
15162 finish_loop_body (location, condition, body, reversed)
15163 int location;
15164 tree condition, body;
15165 int reversed;
15167 tree to_return = ctxp->current_loop;
15168 tree loop_body = LOOP_EXPR_BODY (to_return);
15169 if (condition)
15171 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15172 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15173 The real EXIT_EXPR is one operand further. */
15174 EXPR_WFL_LINECOL (cnode) = location;
15175 /* This one is for accurate error reports */
15176 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
15177 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
15179 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15180 POP_LOOP ();
15181 return to_return;
15184 /* Tailored version of finish_loop_body for FOR loops, when FOR
15185 loops feature the condition part */
15187 static tree
15188 finish_for_loop (location, condition, update, body)
15189 int location;
15190 tree condition, update, body;
15192 /* Put the condition and the loop body in place */
15193 tree loop = finish_loop_body (location, condition, body, 0);
15194 /* LOOP is the current loop which has been now popped of the loop
15195 stack. Install the update block */
15196 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15197 return loop;
15200 /* Try to find the loop a block might be related to. This comprises
15201 the case where the LOOP_EXPR is found as the second operand of a
15202 COMPOUND_EXPR, because the loop happens to have an initialization
15203 part, then expressed as the first operand of the COMPOUND_EXPR. If
15204 the search finds something, 1 is returned. Otherwise, 0 is
15205 returned. The search is assumed to start from a
15206 LABELED_BLOCK_EXPR's block. */
15208 static tree
15209 search_loop (statement)
15210 tree statement;
15212 if (TREE_CODE (statement) == LOOP_EXPR)
15213 return statement;
15215 if (TREE_CODE (statement) == BLOCK)
15216 statement = BLOCK_SUBBLOCKS (statement);
15217 else
15218 return NULL_TREE;
15220 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15221 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15222 statement = TREE_OPERAND (statement, 1);
15224 return (TREE_CODE (statement) == LOOP_EXPR
15225 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15228 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15229 returned otherwise. */
15231 static int
15232 labeled_block_contains_loop_p (block, loop)
15233 tree block, loop;
15235 if (!block)
15236 return 0;
15238 if (LABELED_BLOCK_BODY (block) == loop)
15239 return 1;
15241 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15242 return 1;
15244 return 0;
15247 /* If the loop isn't surrounded by a labeled statement, create one and
15248 insert LOOP as its body. */
15250 static tree
15251 patch_loop_statement (loop)
15252 tree loop;
15254 tree loop_label;
15256 TREE_TYPE (loop) = void_type_node;
15257 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15258 return loop;
15260 loop_label = build_labeled_block (0, NULL_TREE);
15261 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15262 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15263 LABELED_BLOCK_BODY (loop_label) = loop;
15264 PUSH_LABELED_BLOCK (loop_label);
15265 return loop_label;
15268 /* 14.13, 14.14: break and continue Statements */
15270 /* Build a break or a continue statement. a null NAME indicates an
15271 unlabeled break/continue statement. */
15273 static tree
15274 build_bc_statement (location, is_break, name)
15275 int location, is_break;
15276 tree name;
15278 tree break_continue, label_block_expr = NULL_TREE;
15280 if (name)
15282 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15283 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15284 /* Null means that we don't have a target for this named
15285 break/continue. In this case, we make the target to be the
15286 label name, so that the error can be reported accuratly in
15287 patch_bc_statement. */
15288 label_block_expr = EXPR_WFL_NODE (name);
15290 /* Unlabeled break/continue will be handled during the
15291 break/continue patch operation */
15292 break_continue
15293 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
15295 IS_BREAK_STMT_P (break_continue) = is_break;
15296 TREE_SIDE_EFFECTS (break_continue) = 1;
15297 EXPR_WFL_LINECOL (break_continue) = location;
15298 break_continue = build_debugable_stmt (location, break_continue);
15299 return break_continue;
15302 /* Verification of a break/continue statement. */
15304 static tree
15305 patch_bc_statement (node)
15306 tree node;
15308 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15309 tree labeled_block = ctxp->current_labeled_block;
15310 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15312 /* Having an identifier here means that the target is unknown. */
15313 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15315 parse_error_context (wfl_operator, "No label definition found for `%s'",
15316 IDENTIFIER_POINTER (bc_label));
15317 return error_mark_node;
15319 if (! IS_BREAK_STMT_P (node))
15321 /* It's a continue statement. */
15322 for (;; labeled_block = TREE_CHAIN (labeled_block))
15324 if (labeled_block == NULL_TREE)
15326 if (bc_label == NULL_TREE)
15327 parse_error_context (wfl_operator,
15328 "`continue' must be in loop");
15329 else
15330 parse_error_context
15331 (wfl_operator, "continue label `%s' does not name a loop",
15332 IDENTIFIER_POINTER (bc_label));
15333 return error_mark_node;
15335 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15336 == continue_identifier_node)
15337 && (bc_label == NULL_TREE
15338 || TREE_CHAIN (labeled_block) == bc_label))
15340 bc_label = labeled_block;
15341 break;
15345 else if (!bc_label)
15347 for (;; labeled_block = TREE_CHAIN (labeled_block))
15349 if (labeled_block == NULL_TREE)
15351 parse_error_context (wfl_operator,
15352 "`break' must be in loop or switch");
15353 return error_mark_node;
15355 target_stmt = LABELED_BLOCK_BODY (labeled_block);
15356 if (TREE_CODE (target_stmt) == SWITCH_EXPR
15357 || search_loop (target_stmt))
15359 bc_label = labeled_block;
15360 break;
15365 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15366 CAN_COMPLETE_NORMALLY (bc_label) = 1;
15368 /* Our break/continue don't return values. */
15369 TREE_TYPE (node) = void_type_node;
15370 /* Encapsulate the break within a compound statement so that it's
15371 expanded all the times by expand_expr (and not clobbered
15372 sometimes, like after a if statement) */
15373 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15374 TREE_SIDE_EFFECTS (node) = 1;
15375 return node;
15378 /* Process the exit expression belonging to a loop. Its type must be
15379 boolean. */
15381 static tree
15382 patch_exit_expr (node)
15383 tree node;
15385 tree expression = TREE_OPERAND (node, 0);
15386 TREE_TYPE (node) = error_mark_node;
15387 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15389 /* The type of expression must be boolean */
15390 if (TREE_TYPE (expression) != boolean_type_node)
15392 parse_error_context
15393 (wfl_operator,
15394 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
15395 lang_printable_name (TREE_TYPE (expression), 0));
15396 return error_mark_node;
15398 /* Now we know things are allright, invert the condition, fold and
15399 return */
15400 TREE_OPERAND (node, 0) =
15401 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15403 if (! integer_zerop (TREE_OPERAND (node, 0))
15404 && ctxp->current_loop != NULL_TREE
15405 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15406 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15407 if (! integer_onep (TREE_OPERAND (node, 0)))
15408 CAN_COMPLETE_NORMALLY (node) = 1;
15411 TREE_TYPE (node) = void_type_node;
15412 return node;
15415 /* 14.9 Switch statement */
15417 static tree
15418 patch_switch_statement (node)
15419 tree node;
15421 tree se = TREE_OPERAND (node, 0), se_type;
15423 /* Complete the switch expression */
15424 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15425 se_type = TREE_TYPE (se);
15426 /* The type of the switch expression must be char, byte, short or
15427 int */
15428 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15430 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15431 parse_error_context (wfl_operator,
15432 "Incompatible type for `switch'. Can't convert `%s' to `int'",
15433 lang_printable_name (se_type, 0));
15434 /* This is what java_complete_tree will check */
15435 TREE_OPERAND (node, 0) = error_mark_node;
15436 return error_mark_node;
15439 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15441 /* Ready to return */
15442 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15444 TREE_TYPE (node) = error_mark_node;
15445 return error_mark_node;
15447 TREE_TYPE (node) = void_type_node;
15448 TREE_SIDE_EFFECTS (node) = 1;
15449 CAN_COMPLETE_NORMALLY (node)
15450 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15451 || ! SWITCH_HAS_DEFAULT (node);
15452 return node;
15455 /* 14.18 The try/catch statements */
15457 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15458 catches TYPE and executes CATCH_STMTS. */
15460 static tree
15461 encapsulate_with_try_catch (location, type, try_stmts, catch_stmts)
15462 int location;
15463 tree type, try_stmts, catch_stmts;
15465 tree try_block, catch_clause_param, catch_block, catch;
15467 /* First build a try block */
15468 try_block = build_expr_block (try_stmts, NULL_TREE);
15470 /* Build a catch block: we need a catch clause parameter */
15471 catch_clause_param = build_decl (VAR_DECL,
15472 wpv_id, build_pointer_type (type));
15473 /* And a block */
15474 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15476 /* Initialize the variable and store in the block */
15477 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15478 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15479 add_stmt_to_block (catch_block, NULL_TREE, catch);
15481 /* Add the catch statements */
15482 add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15484 /* Now we can build a CATCH_EXPR */
15485 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
15487 return build_try_statement (location, try_block, catch_block);
15490 static tree
15491 build_try_statement (location, try_block, catches)
15492 int location;
15493 tree try_block, catches;
15495 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
15496 EXPR_WFL_LINECOL (node) = location;
15497 return node;
15500 static tree
15501 build_try_finally_statement (location, try_block, finally)
15502 int location;
15503 tree try_block, finally;
15505 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15506 EXPR_WFL_LINECOL (node) = location;
15507 return node;
15510 static tree
15511 patch_try_statement (node)
15512 tree node;
15514 int error_found = 0;
15515 tree try = TREE_OPERAND (node, 0);
15516 /* Exception handlers are considered in left to right order */
15517 tree catch = nreverse (TREE_OPERAND (node, 1));
15518 tree current, caught_type_list = NULL_TREE;
15520 /* Check catch clauses, if any. Every time we find an error, we try
15521 to process the next catch clause. We process the catch clause before
15522 the try block so that when processing the try block we can check thrown
15523 exceptions againts the caught type list. */
15524 for (current = catch; current; current = TREE_CHAIN (current))
15526 tree carg_decl, carg_type;
15527 tree sub_current, catch_block, catch_clause;
15528 int unreachable;
15530 /* At this point, the structure of the catch clause is
15531 CATCH_EXPR (catch node)
15532 BLOCK (with the decl of the parameter)
15533 COMPOUND_EXPR
15534 MODIFY_EXPR (assignment of the catch parameter)
15535 BLOCK (catch clause block)
15537 catch_clause = TREE_OPERAND (current, 0);
15538 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15539 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15541 /* Catch clauses can't have more than one parameter declared,
15542 but it's already enforced by the grammar. Make sure that the
15543 only parameter of the clause statement in of class Throwable
15544 or a subclass of Throwable, but that was done earlier. The
15545 catch clause parameter type has also been resolved. */
15547 /* Just make sure that the catch clause parameter type inherits
15548 from java.lang.Throwable */
15549 if (!inherits_from_p (carg_type, throwable_type_node))
15551 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15552 parse_error_context (wfl_operator,
15553 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15554 lang_printable_name (carg_type, 0));
15555 error_found = 1;
15556 continue;
15559 /* Partial check for unreachable catch statement: The catch
15560 clause is reachable iff is no earlier catch block A in
15561 the try statement such that the type of the catch
15562 clause's parameter is the same as or a subclass of the
15563 type of A's parameter */
15564 unreachable = 0;
15565 for (sub_current = catch;
15566 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15568 tree sub_catch_clause, decl;
15569 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15570 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15572 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15574 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15575 parse_error_context
15576 (wfl_operator,
15577 "`catch' not reached because of the catch clause at line %d",
15578 EXPR_WFL_LINENO (sub_current));
15579 unreachable = error_found = 1;
15580 break;
15583 /* Complete the catch clause block */
15584 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15585 if (catch_block == error_mark_node)
15587 error_found = 1;
15588 continue;
15590 if (CAN_COMPLETE_NORMALLY (catch_block))
15591 CAN_COMPLETE_NORMALLY (node) = 1;
15592 TREE_OPERAND (current, 0) = catch_block;
15594 if (unreachable)
15595 continue;
15597 /* Things to do here: the exception must be thrown */
15599 /* Link this type to the caught type list */
15600 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15603 PUSH_EXCEPTIONS (caught_type_list);
15604 if ((try = java_complete_tree (try)) == error_mark_node)
15605 error_found = 1;
15606 if (CAN_COMPLETE_NORMALLY (try))
15607 CAN_COMPLETE_NORMALLY (node) = 1;
15608 POP_EXCEPTIONS ();
15610 /* Verification ends here */
15611 if (error_found)
15612 return error_mark_node;
15614 TREE_OPERAND (node, 0) = try;
15615 TREE_OPERAND (node, 1) = catch;
15616 TREE_TYPE (node) = void_type_node;
15617 return node;
15620 /* 14.17 The synchronized Statement */
15622 static tree
15623 patch_synchronized_statement (node, wfl_op1)
15624 tree node, wfl_op1;
15626 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15627 tree block = TREE_OPERAND (node, 1);
15629 tree tmp, enter, exit, expr_decl, assignment;
15631 if (expr == error_mark_node)
15633 block = java_complete_tree (block);
15634 return expr;
15637 /* We might be trying to synchronize on a STRING_CST */
15638 if ((tmp = patch_string (expr)))
15639 expr = tmp;
15641 /* The TYPE of expr must be a reference type */
15642 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15644 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15645 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15646 lang_printable_name (TREE_TYPE (expr), 0));
15647 return error_mark_node;
15650 if (flag_emit_xref)
15652 TREE_OPERAND (node, 0) = expr;
15653 TREE_OPERAND (node, 1) = java_complete_tree (block);
15654 CAN_COMPLETE_NORMALLY (node) = 1;
15655 return node;
15658 /* Generate a try-finally for the synchronized statement, except
15659 that the handler that catches all throw exception calls
15660 _Jv_MonitorExit and then rethrow the exception.
15661 The synchronized statement is then implemented as:
15662 TRY
15664 _Jv_MonitorEnter (expression)
15665 synchronized_block
15666 _Jv_MonitorExit (expression)
15668 CATCH_ALL
15670 e = _Jv_exception_info ();
15671 _Jv_MonitorExit (expression)
15672 Throw (e);
15673 } */
15675 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15676 BUILD_MONITOR_ENTER (enter, expr_decl);
15677 BUILD_MONITOR_EXIT (exit, expr_decl);
15678 CAN_COMPLETE_NORMALLY (enter) = 1;
15679 CAN_COMPLETE_NORMALLY (exit) = 1;
15680 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15681 TREE_SIDE_EFFECTS (assignment) = 1;
15682 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15683 build (COMPOUND_EXPR, NULL_TREE,
15684 build (WITH_CLEANUP_EXPR, NULL_TREE,
15685 build (COMPOUND_EXPR, NULL_TREE,
15686 assignment, enter),
15687 exit, NULL_TREE),
15688 block));
15689 node = build_expr_block (node, expr_decl);
15691 return java_complete_tree (node);
15694 /* 14.16 The throw Statement */
15696 static tree
15697 patch_throw_statement (node, wfl_op1)
15698 tree node, wfl_op1;
15700 tree expr = TREE_OPERAND (node, 0);
15701 tree type = TREE_TYPE (expr);
15702 int unchecked_ok = 0, tryblock_throws_ok = 0;
15704 /* Thrown expression must be assignable to java.lang.Throwable */
15705 if (!try_reference_assignconv (throwable_type_node, expr))
15707 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15708 parse_error_context (wfl_operator,
15709 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15710 lang_printable_name (type, 0));
15711 /* If the thrown expression was a reference, we further the
15712 compile-time check. */
15713 if (!JREFERENCE_TYPE_P (type))
15714 return error_mark_node;
15717 /* At least one of the following must be true */
15719 /* The type of the throw expression is a not checked exception,
15720 i.e. is a unchecked expression. */
15721 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15723 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15724 /* An instance can't throw a checked exception unless that exception
15725 is explicitely declared in the `throws' clause of each
15726 constructor. This doesn't apply to anonymous classes, since they
15727 don't have declared constructors. */
15728 if (!unchecked_ok
15729 && DECL_INSTINIT_P (current_function_decl)
15730 && !ANONYMOUS_CLASS_P (current_class))
15732 tree current;
15733 for (current = TYPE_METHODS (current_class); current;
15734 current = TREE_CHAIN (current))
15735 if (DECL_CONSTRUCTOR_P (current)
15736 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15738 parse_error_context (wfl_operator, "Checked exception `%s' can't be thrown in instance initializer (not all declared constructor are declaring it in their `throws' clause)",
15739 lang_printable_name (TREE_TYPE (expr), 0));
15740 return error_mark_node;
15744 /* Throw is contained in a try statement and at least one catch
15745 clause can receive the thrown expression or the current method is
15746 declared to throw such an exception. Or, the throw statement is
15747 contained in a method or constructor declaration and the type of
15748 the Expression is assignable to at least one type listed in the
15749 throws clause the declaration. */
15750 if (!unchecked_ok)
15751 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15752 if (!(unchecked_ok || tryblock_throws_ok))
15754 /* If there is a surrounding try block that has no matching
15755 clatch clause, report it first. A surrounding try block exits
15756 only if there is something after the list of checked
15757 exception thrown by the current function (if any). */
15758 if (IN_TRY_BLOCK_P ())
15759 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15760 lang_printable_name (type, 0));
15761 /* If we have no surrounding try statement and the method doesn't have
15762 any throws, report it now. FIXME */
15764 /* We report that the exception can't be throw from a try block
15765 in all circumstances but when the `throw' is inside a static
15766 block. */
15767 else if (!EXCEPTIONS_P (currently_caught_type_list)
15768 && !tryblock_throws_ok)
15770 if (DECL_CLINIT_P (current_function_decl))
15771 parse_error_context (wfl_operator,
15772 "Checked exception `%s' can't be thrown in initializer",
15773 lang_printable_name (type, 0));
15774 else
15775 parse_error_context (wfl_operator,
15776 "Checked exception `%s' isn't thrown from a `try' block",
15777 lang_printable_name (type, 0));
15779 /* Otherwise, the current method doesn't have the appropriate
15780 throws declaration */
15781 else
15782 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15783 lang_printable_name (type, 0));
15784 return error_mark_node;
15787 if (! flag_emit_class_files && ! flag_emit_xref)
15788 BUILD_THROW (node, expr);
15790 /* If doing xrefs, keep the location where the `throw' was seen. */
15791 if (flag_emit_xref)
15792 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15793 return node;
15796 /* Check that exception said to be thrown by method DECL can be
15797 effectively caught from where DECL is invoked. */
15799 static void
15800 check_thrown_exceptions (location, decl)
15801 int location;
15802 tree decl;
15804 tree throws;
15805 /* For all the unchecked exceptions thrown by DECL */
15806 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15807 throws = TREE_CHAIN (throws))
15808 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15810 #if 1
15811 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15812 if (DECL_NAME (decl) == get_identifier ("clone"))
15813 continue;
15814 #endif
15815 EXPR_WFL_LINECOL (wfl_operator) = location;
15816 if (DECL_FINIT_P (current_function_decl))
15817 parse_error_context
15818 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15819 lang_printable_name (TREE_VALUE (throws), 0));
15820 else
15822 parse_error_context
15823 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15824 lang_printable_name (TREE_VALUE (throws), 0),
15825 (DECL_INIT_P (current_function_decl) ?
15826 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15827 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15832 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15833 try-catch blocks, OR is listed in the `throws' clause of the
15834 current method. */
15836 static int
15837 check_thrown_exceptions_do (exception)
15838 tree exception;
15840 tree list = currently_caught_type_list;
15841 resolve_and_layout (exception, NULL_TREE);
15842 /* First, all the nested try-catch-finally at that stage. The
15843 last element contains `throws' clause exceptions, if any. */
15844 if (IS_UNCHECKED_EXCEPTION_P (exception))
15845 return 1;
15846 while (list)
15848 tree caught;
15849 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15850 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15851 return 1;
15852 list = TREE_CHAIN (list);
15854 return 0;
15857 static void
15858 purge_unchecked_exceptions (mdecl)
15859 tree mdecl;
15861 tree throws = DECL_FUNCTION_THROWS (mdecl);
15862 tree new = NULL_TREE;
15864 while (throws)
15866 tree next = TREE_CHAIN (throws);
15867 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15869 TREE_CHAIN (throws) = new;
15870 new = throws;
15872 throws = next;
15874 /* List is inverted here, but it doesn't matter */
15875 DECL_FUNCTION_THROWS (mdecl) = new;
15878 /* This function goes over all of CLASS_TYPE ctors and checks whether
15879 each of them features at least one unchecked exception in its
15880 `throws' clause. If it's the case, it returns `true', `false'
15881 otherwise. */
15883 static bool
15884 ctors_unchecked_throws_clause_p (class_type)
15885 tree class_type;
15887 tree current;
15889 for (current = TYPE_METHODS (class_type); current;
15890 current = TREE_CHAIN (current))
15892 bool ctu = false; /* Ctor Throws Unchecked */
15893 if (DECL_CONSTRUCTOR_P (current))
15895 tree throws;
15896 for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
15897 throws = TREE_CHAIN (throws))
15898 if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
15899 ctu = true;
15901 /* We return false as we found one ctor that is unfit. */
15902 if (!ctu && DECL_CONSTRUCTOR_P (current))
15903 return false;
15905 /* All ctors feature at least one unchecked exception in their
15906 `throws' clause. */
15907 return true;
15910 /* 15.24 Conditional Operator ?: */
15912 static tree
15913 patch_conditional_expr (node, wfl_cond, wfl_op1)
15914 tree node, wfl_cond, wfl_op1;
15916 tree cond = TREE_OPERAND (node, 0);
15917 tree op1 = TREE_OPERAND (node, 1);
15918 tree op2 = TREE_OPERAND (node, 2);
15919 tree resulting_type = NULL_TREE;
15920 tree t1, t2, patched;
15921 int error_found = 0;
15923 /* Operands of ?: might be StringBuffers crafted as a result of a
15924 string concatenation. Obtain a descent operand here. */
15925 if ((patched = patch_string (op1)))
15926 TREE_OPERAND (node, 1) = op1 = patched;
15927 if ((patched = patch_string (op2)))
15928 TREE_OPERAND (node, 2) = op2 = patched;
15930 t1 = TREE_TYPE (op1);
15931 t2 = TREE_TYPE (op2);
15933 /* The first expression must be a boolean */
15934 if (TREE_TYPE (cond) != boolean_type_node)
15936 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15937 parse_error_context (wfl_operator,
15938 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15939 lang_printable_name (TREE_TYPE (cond), 0));
15940 error_found = 1;
15943 /* Second and third can be numeric, boolean (i.e. primitive),
15944 references or null. Anything else results in an error */
15945 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15946 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15947 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15948 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15949 error_found = 1;
15951 /* Determine the type of the conditional expression. Same types are
15952 easy to deal with */
15953 else if (t1 == t2)
15954 resulting_type = t1;
15956 /* There are different rules for numeric types */
15957 else if (JNUMERIC_TYPE_P (t1))
15959 /* if byte/short found, the resulting type is short */
15960 if ((t1 == byte_type_node && t2 == short_type_node)
15961 || (t1 == short_type_node && t2 == byte_type_node))
15962 resulting_type = short_type_node;
15964 /* If t1 is a constant int and t2 is of type byte, short or char
15965 and t1's value fits in t2, then the resulting type is t2 */
15966 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15967 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15968 resulting_type = t2;
15970 /* If t2 is a constant int and t1 is of type byte, short or char
15971 and t2's value fits in t1, then the resulting type is t1 */
15972 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15973 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15974 resulting_type = t1;
15976 /* Otherwise, binary numeric promotion is applied and the
15977 resulting type is the promoted type of operand 1 and 2 */
15978 else
15979 resulting_type = binary_numeric_promotion (t1, t2,
15980 &TREE_OPERAND (node, 1),
15981 &TREE_OPERAND (node, 2));
15984 /* Cases of a reference and a null type */
15985 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15986 resulting_type = t1;
15988 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15989 resulting_type = t2;
15991 /* Last case: different reference types. If a type can be converted
15992 into the other one by assignment conversion, the latter
15993 determines the type of the expression */
15994 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15995 resulting_type = promote_type (t1);
15997 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15998 resulting_type = promote_type (t2);
16000 /* If we don't have any resulting type, we're in trouble */
16001 if (!resulting_type)
16003 char *t = xstrdup (lang_printable_name (t1, 0));
16004 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16005 parse_error_context (wfl_operator,
16006 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
16007 t, lang_printable_name (t2, 0));
16008 free (t);
16009 error_found = 1;
16012 if (error_found)
16014 TREE_TYPE (node) = error_mark_node;
16015 return error_mark_node;
16018 TREE_TYPE (node) = resulting_type;
16019 TREE_SET_CODE (node, COND_EXPR);
16020 CAN_COMPLETE_NORMALLY (node) = 1;
16021 return node;
16024 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16026 static tree
16027 maybe_build_class_init_for_field (decl, expr)
16028 tree decl, expr;
16030 tree clas = DECL_CONTEXT (decl);
16031 if (flag_emit_class_files || flag_emit_xref)
16032 return expr;
16034 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16035 && FIELD_FINAL (decl))
16037 tree init = DECL_INITIAL (decl);
16038 if (init != NULL_TREE)
16039 init = fold_constant_for_init (init, decl);
16040 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16041 return expr;
16044 return build_class_init (clas, expr);
16047 /* Try to constant fold NODE.
16048 If NODE is not a constant expression, return NULL_EXPR.
16049 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16051 static tree
16052 fold_constant_for_init (node, context)
16053 tree node;
16054 tree context;
16056 tree op0, op1, val;
16057 enum tree_code code = TREE_CODE (node);
16059 switch (code)
16061 case STRING_CST:
16062 case INTEGER_CST:
16063 case REAL_CST:
16064 return node;
16066 case PLUS_EXPR:
16067 case MINUS_EXPR:
16068 case MULT_EXPR:
16069 case TRUNC_MOD_EXPR:
16070 case RDIV_EXPR:
16071 case LSHIFT_EXPR:
16072 case RSHIFT_EXPR:
16073 case URSHIFT_EXPR:
16074 case BIT_AND_EXPR:
16075 case BIT_XOR_EXPR:
16076 case BIT_IOR_EXPR:
16077 case TRUTH_ANDIF_EXPR:
16078 case TRUTH_ORIF_EXPR:
16079 case EQ_EXPR:
16080 case NE_EXPR:
16081 case GT_EXPR:
16082 case GE_EXPR:
16083 case LT_EXPR:
16084 case LE_EXPR:
16085 op0 = TREE_OPERAND (node, 0);
16086 op1 = TREE_OPERAND (node, 1);
16087 val = fold_constant_for_init (op0, context);
16088 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16089 return NULL_TREE;
16090 TREE_OPERAND (node, 0) = val;
16091 val = fold_constant_for_init (op1, context);
16092 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16093 return NULL_TREE;
16094 TREE_OPERAND (node, 1) = val;
16095 return patch_binop (node, op0, op1);
16097 case UNARY_PLUS_EXPR:
16098 case NEGATE_EXPR:
16099 case TRUTH_NOT_EXPR:
16100 case BIT_NOT_EXPR:
16101 case CONVERT_EXPR:
16102 op0 = TREE_OPERAND (node, 0);
16103 val = fold_constant_for_init (op0, context);
16104 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16105 return NULL_TREE;
16106 TREE_OPERAND (node, 0) = val;
16107 return patch_unaryop (node, op0);
16108 break;
16110 case COND_EXPR:
16111 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16112 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16113 return NULL_TREE;
16114 TREE_OPERAND (node, 0) = val;
16115 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16116 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16117 return NULL_TREE;
16118 TREE_OPERAND (node, 1) = val;
16119 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16120 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16121 return NULL_TREE;
16122 TREE_OPERAND (node, 2) = val;
16123 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
16124 : TREE_OPERAND (node, 2);
16126 case VAR_DECL:
16127 case FIELD_DECL:
16128 if (! FIELD_FINAL (node)
16129 || DECL_INITIAL (node) == NULL_TREE)
16130 return NULL_TREE;
16131 val = DECL_INITIAL (node);
16132 /* Guard against infinite recursion. */
16133 DECL_INITIAL (node) = NULL_TREE;
16134 val = fold_constant_for_init (val, node);
16135 DECL_INITIAL (node) = val;
16136 if (!val && CLASS_FINAL_VARIABLE_P (node))
16137 DECL_FIELD_FINAL_IUD (node) = 0;
16138 return val;
16140 case EXPR_WITH_FILE_LOCATION:
16141 /* Compare java_complete_tree and resolve_expression_name. */
16142 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16143 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16145 tree name = EXPR_WFL_NODE (node);
16146 tree decl;
16147 if (PRIMARY_P (node))
16148 return NULL_TREE;
16149 else if (! QUALIFIED_P (name))
16151 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16152 if (decl == NULL_TREE
16153 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16154 return NULL_TREE;
16155 return fold_constant_for_init (decl, decl);
16157 else
16159 /* Install the proper context for the field resolution.
16160 The prior context is restored once the name is
16161 properly qualified. */
16162 tree saved_current_class = current_class;
16163 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
16164 current_class = DECL_CONTEXT (context);
16165 qualify_ambiguous_name (node);
16166 current_class = saved_current_class;
16167 if (resolve_field_access (node, &decl, NULL)
16168 && decl != NULL_TREE)
16169 return fold_constant_for_init (decl, decl);
16170 return NULL_TREE;
16173 else
16175 op0 = TREE_OPERAND (node, 0);
16176 val = fold_constant_for_init (op0, context);
16177 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16178 return NULL_TREE;
16179 TREE_OPERAND (node, 0) = val;
16180 return val;
16183 #ifdef USE_COMPONENT_REF
16184 case IDENTIFIER:
16185 case COMPONENT_REF:
16187 #endif
16189 default:
16190 return NULL_TREE;
16194 #ifdef USE_COMPONENT_REF
16195 /* Context is 'T' for TypeName, 'P' for PackageName,
16196 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16198 tree
16199 resolve_simple_name (name, context)
16200 tree name;
16201 int context;
16205 tree
16206 resolve_qualified_name (name, context)
16207 tree name;
16208 int context;
16211 #endif
16213 /* Mark P, which is really a `struct parser_ctxt **' for GC. */
16215 static void
16216 mark_parser_ctxt (p)
16217 void *p;
16219 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
16220 int i;
16222 if (!pc)
16223 return;
16225 #ifndef JC1_LITE
16226 for (i = 0; i < 11; ++i)
16227 ggc_mark_tree (pc->modifier_ctx[i]);
16228 ggc_mark_tree (pc->class_type);
16229 ggc_mark_tree (pc->function_decl);
16230 ggc_mark_tree (pc->package);
16231 ggc_mark_tree (pc->class_list);
16232 ggc_mark_tree (pc->current_parsed_class);
16233 ggc_mark_tree (pc->current_parsed_class_un);
16234 ggc_mark_tree (pc->non_static_initialized);
16235 ggc_mark_tree (pc->static_initialized);
16236 ggc_mark_tree (pc->instance_initializers);
16237 ggc_mark_tree (pc->import_list);
16238 ggc_mark_tree (pc->import_demand_list);
16239 ggc_mark_tree (pc->current_loop);
16240 ggc_mark_tree (pc->current_labeled_block);
16241 #endif /* JC1_LITE */
16243 if (pc->next)
16244 mark_parser_ctxt (&pc->next);
16247 void
16248 init_src_parse ()
16250 /* Register roots with the garbage collector. */
16251 ggc_add_tree_root (src_parse_roots, sizeof (src_parse_roots) / sizeof(tree));
16256 /* This section deals with the functions that are called when tables
16257 recording class initialization information are traversed. */
16259 /* Attach to PTR (a block) the declaration found in ENTRY. */
16261 static bool
16262 attach_init_test_initialization_flags (entry, ptr)
16263 struct hash_entry *entry;
16264 PTR ptr;
16266 tree block = (tree)ptr;
16267 struct init_test_hash_entry *ite = (struct init_test_hash_entry *) entry;
16269 TREE_CHAIN (ite->init_test_decl) = BLOCK_EXPR_DECLS (block);
16270 BLOCK_EXPR_DECLS (block) = ite->init_test_decl;
16271 return true;
16274 /* This function is called for each statement calling a static
16275 function. ENTRY is a TREE_LIST whose PURPOSE is the called
16276 function and VALUE is a compound whose second operand can be
16277 patched with static class initialization flag assignments. */
16279 static bool
16280 adjust_init_test_initialization (entry, info)
16281 struct hash_entry *entry;
16282 PTR info ATTRIBUTE_UNUSED;
16284 tree list = (tree)(entry->key);
16285 tree called_method = TREE_PURPOSE (list);
16286 tree compound = TREE_VALUE (list);
16287 tree assignment_compound_list = build_tree_list (called_method, NULL);
16289 /* For each class definitely initialized in CALLED_METHOD, fill
16290 ASSIGNMENT_COMPOUND with assignment to the class initialization flag. */
16291 hash_traverse (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
16292 emit_test_initialization, assignment_compound_list);
16294 if (TREE_VALUE (assignment_compound_list))
16295 TREE_OPERAND (compound, 1) = TREE_VALUE (assignment_compound_list);
16297 return true;
16300 /* This function is called for each classes that is known definitely
16301 assigned when a given static method was called. This function
16302 augments a compound expression (INFO) storing all assignment to
16303 initialized static class flags if a flag already existed, otherwise
16304 a new one is created. */
16306 static bool
16307 emit_test_initialization (entry, info)
16308 struct hash_entry *entry;
16309 PTR info;
16311 tree l = (tree) info;
16312 tree decl, init;
16314 struct init_test_hash_entry *ite = (struct init_test_hash_entry *)
16315 hash_lookup (&DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl),
16316 entry->key, FALSE, NULL);
16318 /* If we haven't found a flag and we're dealing with self registered
16319 with current_function_decl, then don't do anything. Self is
16320 always added as definitely initialized but this information is
16321 valid only if used outside the current function. */
16322 if (! ite)
16324 if (current_function_decl != TREE_PURPOSE (l))
16325 ite = (struct init_test_hash_entry *)
16326 hash_lookup (&DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl),
16327 entry->key, TRUE, NULL);
16328 else
16329 return true;
16332 /* If we don't have a variable, create one and install it. */
16333 if (! ite->init_test_decl)
16335 tree block;
16337 decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16338 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16339 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16340 DECL_CONTEXT (decl) = current_function_decl;
16341 DECL_INITIAL (decl) = boolean_true_node;
16343 /* The trick is to find the right context for it. */
16344 block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16345 TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16346 BLOCK_EXPR_DECLS (block) = decl;
16347 ite->init_test_decl = decl;
16349 else
16350 decl = ite->init_test_decl;
16352 /* Now simply augment the compound that holds all the assignments
16353 pertaining to this method invocation. */
16354 init = build (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16355 TREE_SIDE_EFFECTS (init) = 1;
16356 TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16357 TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16359 return true;