2001-08-22 Alexandre Petit-Bianco <apbianco@redhat.com>
[official-gcc.git] / gcc / java / parse.y
blobd35a47dbc8ea94d45da238037384a4713c81f867
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));
103 static void find_in_imports PARAMS ((tree));
104 static void check_static_final_variable_assignment_flag PARAMS ((tree));
105 static void reset_static_final_variable_assignment_flag PARAMS ((tree));
106 static void check_final_variable_local_assignment_flag PARAMS ((tree, tree));
107 static void reset_final_variable_local_assignment_flag PARAMS ((tree));
108 static int check_final_variable_indirect_assignment PARAMS ((tree));
109 static void check_final_variable_global_assignment_flag PARAMS ((tree));
110 static void check_inner_class_access PARAMS ((tree, tree, tree));
111 static int check_pkg_class_access PARAMS ((tree, tree));
112 static void register_package PARAMS ((tree));
113 static tree resolve_package PARAMS ((tree, tree *));
114 static tree lookup_package_type PARAMS ((const char *, int));
115 static tree resolve_class PARAMS ((tree, tree, tree, tree));
116 static void declare_local_variables PARAMS ((int, tree, tree));
117 static void source_start_java_method PARAMS ((tree));
118 static void source_end_java_method PARAMS ((void));
119 static tree find_name_in_single_imports PARAMS ((tree));
120 static void check_abstract_method_header PARAMS ((tree));
121 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
122 static tree resolve_expression_name PARAMS ((tree, tree *));
123 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
124 static int check_class_interface_creation PARAMS ((int, int, tree,
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 obtain_incomplete_type PARAMS ((tree));
145 static tree java_complete_lhs PARAMS ((tree));
146 static tree java_complete_tree PARAMS ((tree));
147 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
148 static int analyze_clinit_body PARAMS ((tree));
149 static int maybe_yank_clinit PARAMS ((tree));
150 static void start_complete_expand_method PARAMS ((tree));
151 static void java_complete_expand_method PARAMS ((tree));
152 static void java_expand_method_bodies PARAMS ((tree));
153 static int unresolved_type_p PARAMS ((tree, tree *));
154 static void create_jdep_list PARAMS ((struct parser_ctxt *));
155 static tree build_expr_block PARAMS ((tree, tree));
156 static tree enter_block PARAMS ((void));
157 static tree exit_block PARAMS ((void));
158 static tree lookup_name_in_blocks PARAMS ((tree));
159 static void maybe_absorb_scoping_blocks PARAMS ((void));
160 static tree build_method_invocation PARAMS ((tree, tree));
161 static tree build_new_invocation PARAMS ((tree, tree));
162 static tree build_assignment PARAMS ((int, int, tree, tree));
163 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
164 static int check_final_assignment PARAMS ((tree ,tree));
165 static tree patch_assignment PARAMS ((tree, tree, tree ));
166 static tree patch_binop PARAMS ((tree, tree, tree));
167 static tree build_unaryop PARAMS ((int, int, tree));
168 static tree build_incdec PARAMS ((int, int, tree, int));
169 static tree patch_unaryop PARAMS ((tree, tree));
170 static tree build_cast PARAMS ((int, tree, tree));
171 static tree build_null_of_type PARAMS ((tree));
172 static tree patch_cast PARAMS ((tree, tree));
173 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
174 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
175 static int valid_cast_to_p PARAMS ((tree, tree));
176 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
177 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
178 static tree try_reference_assignconv PARAMS ((tree, tree));
179 static tree build_unresolved_array_type PARAMS ((tree));
180 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
181 static tree build_array_ref PARAMS ((int, tree, tree));
182 static tree patch_array_ref PARAMS ((tree));
183 static tree make_qualified_name PARAMS ((tree, tree, int));
184 static tree merge_qualified_name PARAMS ((tree, tree));
185 static tree make_qualified_primary PARAMS ((tree, tree, int));
186 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
187 tree *, tree *));
188 static void qualify_ambiguous_name PARAMS ((tree));
189 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
190 static tree build_newarray_node PARAMS ((tree, tree, int));
191 static tree patch_newarray PARAMS ((tree));
192 static tree resolve_type_during_patch PARAMS ((tree));
193 static tree build_this PARAMS ((int));
194 static tree build_wfl_wrap PARAMS ((tree, int));
195 static tree build_return PARAMS ((int, tree));
196 static tree patch_return PARAMS ((tree));
197 static tree maybe_access_field PARAMS ((tree, tree, tree));
198 static int complete_function_arguments PARAMS ((tree));
199 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
200 tree, tree));
201 static int not_accessible_p PARAMS ((tree, tree, tree, int));
202 static void check_deprecation PARAMS ((tree, tree));
203 static int class_in_current_package PARAMS ((tree));
204 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
205 static tree patch_if_else_statement PARAMS ((tree));
206 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
207 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
208 static tree patch_exit_expr PARAMS ((tree));
209 static tree build_labeled_block PARAMS ((int, tree));
210 static tree finish_labeled_statement PARAMS ((tree, tree));
211 static tree build_bc_statement PARAMS ((int, int, tree));
212 static tree patch_bc_statement PARAMS ((tree));
213 static tree patch_loop_statement PARAMS ((tree));
214 static tree build_new_loop PARAMS ((tree));
215 static tree build_loop_body PARAMS ((int, tree, int));
216 static tree finish_loop_body PARAMS ((int, tree, tree, int));
217 static tree build_debugable_stmt PARAMS ((int, tree));
218 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
219 static tree patch_switch_statement PARAMS ((tree));
220 static tree string_constant_concatenation PARAMS ((tree, tree));
221 static tree build_string_concatenation PARAMS ((tree, tree));
222 static tree patch_string_cst PARAMS ((tree));
223 static tree patch_string PARAMS ((tree));
224 static tree encapsulate_with_try_catch PARAMS ((int, tree, tree, tree));
225 static tree build_try_statement PARAMS ((int, tree, tree));
226 static tree build_try_finally_statement PARAMS ((int, tree, tree));
227 static tree patch_try_statement PARAMS ((tree));
228 static tree patch_synchronized_statement PARAMS ((tree, tree));
229 static tree patch_throw_statement PARAMS ((tree, tree));
230 static void check_thrown_exceptions PARAMS ((int, tree));
231 static int check_thrown_exceptions_do PARAMS ((tree));
232 static void purge_unchecked_exceptions PARAMS ((tree));
233 static bool ctors_unchecked_throws_clause_p PARAMS ((tree));
234 static void check_throws_clauses PARAMS ((tree, tree, tree));
235 static void finish_method_declaration PARAMS ((tree));
236 static tree build_super_invocation PARAMS ((tree));
237 static int verify_constructor_circularity PARAMS ((tree, tree));
238 static char *constructor_circularity_msg PARAMS ((tree, tree));
239 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
240 int, int));
241 static const char *get_printable_method_name PARAMS ((tree));
242 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
243 static tree generate_finit PARAMS ((tree));
244 static tree generate_instinit PARAMS ((tree));
245 static tree build_instinit_invocation PARAMS ((tree));
246 static void fix_constructors PARAMS ((tree));
247 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
248 tree, int *));
249 static void craft_constructor PARAMS ((tree, tree));
250 static int verify_constructor_super PARAMS ((tree));
251 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
252 static void start_artificial_method_body PARAMS ((tree));
253 static void end_artificial_method_body PARAMS ((tree));
254 static int check_method_redefinition PARAMS ((tree, tree));
255 static int check_method_types_complete PARAMS ((tree));
256 static void java_check_regular_methods PARAMS ((tree));
257 static void java_check_abstract_methods PARAMS ((tree));
258 static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
259 static void unreachable_stmt_error PARAMS ((tree));
260 static tree find_expr_with_wfl PARAMS ((tree));
261 static void missing_return_error PARAMS ((tree));
262 static tree build_new_array_init PARAMS ((int, tree));
263 static tree patch_new_array_init PARAMS ((tree, tree));
264 static tree maybe_build_array_element_wfl PARAMS ((tree));
265 static int array_constructor_check_entry PARAMS ((tree, tree));
266 static const char *purify_type_name PARAMS ((const char *));
267 static tree fold_constant_for_init PARAMS ((tree, tree));
268 static tree strip_out_static_field_access_decl PARAMS ((tree));
269 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
270 static void static_ref_err PARAMS ((tree, tree, tree));
271 static void parser_add_interface PARAMS ((tree, tree, tree));
272 static void add_superinterfaces PARAMS ((tree, tree));
273 static tree jdep_resolve_class PARAMS ((jdep *));
274 static int note_possible_classname PARAMS ((const char *, int));
275 static void java_complete_expand_classes PARAMS ((void));
276 static void java_complete_expand_class PARAMS ((tree));
277 static void java_complete_expand_methods PARAMS ((tree));
278 static tree cut_identifier_in_qualified PARAMS ((tree));
279 static tree java_stabilize_reference PARAMS ((tree));
280 static tree do_unary_numeric_promotion PARAMS ((tree));
281 static char * operator_string PARAMS ((tree));
282 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
283 static tree merge_string_cste PARAMS ((tree, tree, int));
284 static tree java_refold PARAMS ((tree));
285 static int java_decl_equiv PARAMS ((tree, tree));
286 static int binop_compound_p PARAMS ((enum tree_code));
287 static tree search_loop PARAMS ((tree));
288 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
289 static int check_abstract_method_definitions PARAMS ((int, tree, tree));
290 static void java_check_abstract_method_definitions PARAMS ((tree));
291 static void java_debug_context_do PARAMS ((int));
292 static void java_parser_context_push_initialized_field PARAMS ((void));
293 static void java_parser_context_pop_initialized_field PARAMS ((void));
294 static tree reorder_static_initialized PARAMS ((tree));
295 static void java_parser_context_suspend PARAMS ((void));
296 static void java_parser_context_resume PARAMS ((void));
297 static int pop_current_osb PARAMS ((struct parser_ctxt *));
299 /* JDK 1.1 work. FIXME */
301 static tree maybe_make_nested_class_name PARAMS ((tree));
302 static void make_nested_class_name PARAMS ((tree));
303 static void set_nested_class_simple_name_value PARAMS ((tree, int));
304 static void link_nested_class_to_enclosing PARAMS ((void));
305 static tree resolve_inner_class PARAMS ((struct hash_table *, tree, tree *,
306 tree *, tree));
307 static tree find_as_inner_class PARAMS ((tree, tree, tree));
308 static tree find_as_inner_class_do PARAMS ((tree, tree));
309 static int check_inner_class_redefinition PARAMS ((tree, tree));
311 static tree build_thisn_assign PARAMS ((void));
312 static tree build_current_thisn PARAMS ((tree));
313 static tree build_access_to_thisn PARAMS ((tree, tree, int));
314 static tree maybe_build_thisn_access_method PARAMS ((tree));
316 static tree build_outer_field_access PARAMS ((tree, tree));
317 static tree build_outer_field_access_methods PARAMS ((tree));
318 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
319 tree, tree));
320 static tree build_outer_method_access_method PARAMS ((tree));
321 static tree build_new_access_id PARAMS ((void));
322 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
323 tree, tree));
325 static int outer_field_access_p PARAMS ((tree, tree));
326 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
327 tree *, tree *));
328 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
329 static tree build_incomplete_class_ref PARAMS ((int, tree));
330 static tree patch_incomplete_class_ref PARAMS ((tree));
331 static tree create_anonymous_class PARAMS ((int, tree));
332 static void patch_anonymous_class PARAMS ((tree, tree, tree));
333 static void add_inner_class_fields PARAMS ((tree, tree));
335 static tree build_dot_class_method PARAMS ((tree));
336 static tree build_dot_class_method_invocation PARAMS ((tree));
337 static void create_new_parser_context PARAMS ((int));
338 static void mark_parser_ctxt PARAMS ((void *));
339 static tree maybe_build_class_init_for_field PARAMS ((tree, tree));
341 static bool attach_init_test_initialization_flags PARAMS ((struct hash_entry *,
342 PTR));
343 static bool adjust_init_test_initialization PARAMS ((struct hash_entry *,
344 PTR));
345 static bool emit_test_initialization PARAMS ((struct hash_entry *, PTR));
347 /* Number of error found so far. */
348 int java_error_count;
349 /* Number of warning found so far. */
350 int java_warning_count;
351 /* Tell when not to fold, when doing xrefs */
352 int do_not_fold;
353 /* Cyclic inheritance report, as it can be set by layout_class */
354 const char *cyclic_inheritance_report;
356 /* The current parser context */
357 struct parser_ctxt *ctxp;
359 /* List of things that were analyzed for which code will be generated */
360 struct parser_ctxt *ctxp_for_generation = NULL;
362 /* binop_lookup maps token to tree_code. It is used where binary
363 operations are involved and required by the parser. RDIV_EXPR
364 covers both integral/floating point division. The code is changed
365 once the type of both operator is worked out. */
367 static enum tree_code binop_lookup[19] =
369 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
370 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
371 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
372 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
373 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
375 #define BINOP_LOOKUP(VALUE) \
376 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
378 /* This is the end index for binary operators that can also be used
379 in compound assignements. */
380 #define BINOP_COMPOUND_CANDIDATES 11
382 /* The "$L" identifier we use to create labels. */
383 static tree label_id = NULL_TREE;
385 /* The "StringBuffer" identifier used for the String `+' operator. */
386 static tree wfl_string_buffer = NULL_TREE;
388 /* The "append" identifier used for String `+' operator. */
389 static tree wfl_append = NULL_TREE;
391 /* The "toString" identifier used for String `+' operator. */
392 static tree wfl_to_string = NULL_TREE;
394 /* The "java.lang" import qualified name. */
395 static tree java_lang_id = NULL_TREE;
397 /* The generated `inst$' identifier used for generated enclosing
398 instance/field access functions. */
399 static tree inst_id = NULL_TREE;
401 /* The "java.lang.Cloneable" qualified name. */
402 static tree java_lang_cloneable = NULL_TREE;
404 /* The "java.io.Serializable" qualified name. */
405 static tree java_io_serializable = NULL_TREE;
407 /* Context and flag for static blocks */
408 static tree current_static_block = NULL_TREE;
410 /* The generated `write_parm_value$' identifier. */
411 static tree wpv_id;
413 /* The list of all packages we've seen so far */
414 static tree package_list = NULL_TREE;
416 /* Hold THIS for the scope of the current public method decl. */
417 static tree current_this;
419 /* Hold a list of catch clauses list. The first element of this list is
420 the list of the catch clauses of the currently analysed try block. */
421 static tree currently_caught_type_list;
423 static tree src_parse_roots[1] = { NULL_TREE };
425 /* All classes seen from source code */
426 #define gclass_list src_parse_roots[0]
428 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
429 line and point it out. */
430 /* Should point out the one that don't fit. ASCII/unicode, going
431 backward. FIXME */
433 #define check_modifiers(__message, __value, __mask) do { \
434 if ((__value) & ~(__mask)) \
436 int i, remainder = (__value) & ~(__mask); \
437 for (i = 0; i <= 10; i++) \
438 if ((1 << i) & remainder) \
439 parse_error_context (ctxp->modifier_ctx [i], (__message), \
440 java_accstring_lookup (1 << i)); \
442 } while (0)
446 %union {
447 tree node;
448 int sub_token;
449 struct {
450 int token;
451 int location;
452 } operator;
453 int value;
457 #include "lex.c"
460 %pure_parser
462 /* Things defined here have to match the order of what's in the
463 binop_lookup table. */
465 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
466 %token LS_TK SRS_TK ZRS_TK
467 %token AND_TK XOR_TK OR_TK
468 %token BOOL_AND_TK BOOL_OR_TK
469 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
471 /* This maps to the same binop_lookup entry than the token above */
473 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
474 %token REM_ASSIGN_TK
475 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
476 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
479 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
481 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
482 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
483 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
484 %token PAD_TK ABSTRACT_TK MODIFIER_TK
485 %token STRICT_TK
487 /* Keep those two in order, too */
488 %token DECR_TK INCR_TK
490 /* From now one, things can be in any order */
492 %token DEFAULT_TK IF_TK THROW_TK
493 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
494 %token THROWS_TK BREAK_TK IMPORT_TK
495 %token ELSE_TK INSTANCEOF_TK RETURN_TK
496 %token VOID_TK CATCH_TK INTERFACE_TK
497 %token CASE_TK EXTENDS_TK FINALLY_TK
498 %token SUPER_TK WHILE_TK CLASS_TK
499 %token SWITCH_TK CONST_TK TRY_TK
500 %token FOR_TK NEW_TK CONTINUE_TK
501 %token GOTO_TK PACKAGE_TK THIS_TK
503 %token BYTE_TK SHORT_TK INT_TK LONG_TK
504 %token CHAR_TK INTEGRAL_TK
506 %token FLOAT_TK DOUBLE_TK FP_TK
508 %token ID_TK
510 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
512 %token ASSIGN_ANY_TK ASSIGN_TK
513 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
515 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
516 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
518 %type <value> modifiers MODIFIER_TK final synchronized
520 %type <node> super ID_TK identifier
521 %type <node> name simple_name qualified_name
522 %type <node> type_declaration compilation_unit
523 field_declaration method_declaration extends_interfaces
524 interfaces interface_type_list
525 class_member_declaration
526 import_declarations package_declaration
527 type_declarations interface_body
528 interface_member_declaration constant_declaration
529 interface_member_declarations interface_type
530 abstract_method_declaration interface_type_list
531 %type <node> class_body_declaration class_member_declaration
532 static_initializer constructor_declaration block
533 %type <node> class_body_declarations constructor_header
534 %type <node> class_or_interface_type class_type class_type_list
535 constructor_declarator explicit_constructor_invocation
536 %type <node> dim_expr dim_exprs this_or_super throws
538 %type <node> variable_declarator_id variable_declarator
539 variable_declarators variable_initializer
540 variable_initializers constructor_body
541 array_initializer
543 %type <node> class_body block_end constructor_block_end
544 %type <node> statement statement_without_trailing_substatement
545 labeled_statement if_then_statement label_decl
546 if_then_else_statement while_statement for_statement
547 statement_nsi labeled_statement_nsi do_statement
548 if_then_else_statement_nsi while_statement_nsi
549 for_statement_nsi statement_expression_list for_init
550 for_update statement_expression expression_statement
551 primary_no_new_array expression primary
552 array_creation_expression array_type
553 class_instance_creation_expression field_access
554 method_invocation array_access something_dot_new
555 argument_list postfix_expression while_expression
556 post_increment_expression post_decrement_expression
557 unary_expression_not_plus_minus unary_expression
558 pre_increment_expression pre_decrement_expression
559 unary_expression_not_plus_minus cast_expression
560 multiplicative_expression additive_expression
561 shift_expression relational_expression
562 equality_expression and_expression
563 exclusive_or_expression inclusive_or_expression
564 conditional_and_expression conditional_or_expression
565 conditional_expression assignment_expression
566 left_hand_side assignment for_header for_begin
567 constant_expression do_statement_begin empty_statement
568 switch_statement synchronized_statement throw_statement
569 try_statement switch_expression switch_block
570 catches catch_clause catch_clause_parameter finally
571 anonymous_class_creation
572 %type <node> return_statement break_statement continue_statement
574 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
575 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
576 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
577 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
578 %type <operator> ASSIGN_ANY_TK assignment_operator
579 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
580 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
581 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
582 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
583 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
584 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
585 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
586 %type <operator> NEW_TK
588 %type <node> method_body
590 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
591 STRING_LIT_TK NULL_TK VOID_TK
593 %type <node> IF_TK WHILE_TK FOR_TK
595 %type <node> formal_parameter_list formal_parameter
596 method_declarator method_header
598 %type <node> primitive_type reference_type type
599 BOOLEAN_TK INTEGRAL_TK FP_TK
601 /* Added or modified JDK 1.1 rule types */
602 %type <node> type_literals
605 /* 19.2 Production from 2.3: The Syntactic Grammar */
606 goal:
608 /* Register static variables with the garbage
609 collector. */
610 ggc_add_tree_root (&label_id, 1);
611 ggc_add_tree_root (&wfl_string_buffer, 1);
612 ggc_add_tree_root (&wfl_append, 1);
613 ggc_add_tree_root (&wfl_to_string, 1);
614 ggc_add_tree_root (&java_lang_id, 1);
615 ggc_add_tree_root (&inst_id, 1);
616 ggc_add_tree_root (&java_lang_cloneable, 1);
617 ggc_add_tree_root (&java_io_serializable, 1);
618 ggc_add_tree_root (&current_static_block, 1);
619 ggc_add_tree_root (&wpv_id, 1);
620 ggc_add_tree_root (&package_list, 1);
621 ggc_add_tree_root (&current_this, 1);
622 ggc_add_tree_root (&currently_caught_type_list, 1);
623 ggc_add_root (&ctxp, 1,
624 sizeof (struct parser_ctxt *),
625 mark_parser_ctxt);
626 ggc_add_root (&ctxp_for_generation, 1,
627 sizeof (struct parser_ctxt *),
628 mark_parser_ctxt);
630 compilation_unit
634 /* 19.3 Productions from 3: Lexical structure */
635 literal:
636 INT_LIT_TK
637 | FP_LIT_TK
638 | BOOL_LIT_TK
639 | CHAR_LIT_TK
640 | STRING_LIT_TK
641 | NULL_TK
644 /* 19.4 Productions from 4: Types, Values and Variables */
645 type:
646 primitive_type
647 | reference_type
650 primitive_type:
651 INTEGRAL_TK
652 | FP_TK
653 | BOOLEAN_TK
656 reference_type:
657 class_or_interface_type
658 | array_type
661 class_or_interface_type:
662 name
665 class_type:
666 class_or_interface_type /* Default rule */
669 interface_type:
670 class_or_interface_type
673 array_type:
674 primitive_type dims
676 int osb = pop_current_osb (ctxp);
677 tree t = build_java_array_type (($1), -1);
678 while (--osb)
679 t = build_unresolved_array_type (t);
680 $$ = t;
682 | name dims
684 int osb = pop_current_osb (ctxp);
685 tree t = $1;
686 while (osb--)
687 t = build_unresolved_array_type (t);
688 $$ = t;
692 /* 19.5 Productions from 6: Names */
693 name:
694 simple_name /* Default rule */
695 | qualified_name /* Default rule */
698 simple_name:
699 identifier /* Default rule */
702 qualified_name:
703 name DOT_TK identifier
704 { $$ = make_qualified_name ($1, $3, $2.location); }
707 identifier:
708 ID_TK
711 /* 19.6: Production from 7: Packages */
712 compilation_unit:
713 {$$ = NULL;}
714 | package_declaration
715 | import_declarations
716 | type_declarations
717 | package_declaration import_declarations
718 | package_declaration type_declarations
719 | import_declarations type_declarations
720 | package_declaration import_declarations type_declarations
723 import_declarations:
724 import_declaration
726 $$ = NULL;
728 | import_declarations import_declaration
730 $$ = NULL;
734 type_declarations:
735 type_declaration
736 | type_declarations type_declaration
739 package_declaration:
740 PACKAGE_TK name SC_TK
742 ctxp->package = EXPR_WFL_NODE ($2);
743 register_package (ctxp->package);
745 | PACKAGE_TK error
746 {yyerror ("Missing name"); RECOVER;}
747 | PACKAGE_TK name error
748 {yyerror ("';' expected"); RECOVER;}
751 import_declaration:
752 single_type_import_declaration
753 | type_import_on_demand_declaration
756 single_type_import_declaration:
757 IMPORT_TK name SC_TK
759 tree name = EXPR_WFL_NODE ($2), last_name;
760 int i = IDENTIFIER_LENGTH (name)-1;
761 const char *last = &IDENTIFIER_POINTER (name)[i];
762 while (last != IDENTIFIER_POINTER (name))
764 if (last [0] == '.')
765 break;
766 last--;
768 last_name = get_identifier (++last);
769 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
771 tree err = find_name_in_single_imports (last_name);
772 if (err && err != name)
773 parse_error_context
774 ($2, "Ambiguous class: `%s' and `%s'",
775 IDENTIFIER_POINTER (name),
776 IDENTIFIER_POINTER (err));
777 else
778 REGISTER_IMPORT ($2, last_name);
780 else
781 REGISTER_IMPORT ($2, last_name);
783 | IMPORT_TK error
784 {yyerror ("Missing name"); RECOVER;}
785 | IMPORT_TK name error
786 {yyerror ("';' expected"); RECOVER;}
789 type_import_on_demand_declaration:
790 IMPORT_TK name DOT_TK MULT_TK SC_TK
792 tree name = EXPR_WFL_NODE ($2);
793 tree it;
794 /* Search for duplicates. */
795 for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
796 if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
797 break;
798 /* Don't import the same thing more than once, just ignore
799 duplicates (7.5.2) */
800 if (! it)
802 read_import_dir ($2);
803 ctxp->import_demand_list =
804 chainon (ctxp->import_demand_list,
805 build_tree_list ($2, NULL_TREE));
808 | IMPORT_TK name DOT_TK error
809 {yyerror ("'*' expected"); RECOVER;}
810 | IMPORT_TK name DOT_TK MULT_TK error
811 {yyerror ("';' expected"); RECOVER;}
814 type_declaration:
815 class_declaration
816 { end_class_declaration (0); }
817 | interface_declaration
818 { end_class_declaration (0); }
819 | empty_statement
820 | error
822 YYERROR_NOW;
823 yyerror ("Class or interface declaration expected");
827 /* 19.7 Shortened from the original:
828 modifiers: modifier | modifiers modifier
829 modifier: any of public... */
830 modifiers:
831 MODIFIER_TK
833 $$ = (1 << $1);
835 | modifiers MODIFIER_TK
837 int acc = (1 << $2);
838 if ($$ & acc)
839 parse_error_context
840 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
841 java_accstring_lookup (acc));
842 else
844 $$ |= acc;
849 /* 19.8.1 Production from $8.1: Class Declaration */
850 class_declaration:
851 modifiers CLASS_TK identifier super interfaces
852 { create_class ($1, $3, $4, $5); }
853 class_body
854 | CLASS_TK identifier super interfaces
855 { create_class (0, $2, $3, $4); }
856 class_body
857 | modifiers CLASS_TK error
858 {yyerror ("Missing class name"); RECOVER;}
859 | CLASS_TK error
860 {yyerror ("Missing class name"); RECOVER;}
861 | CLASS_TK identifier error
863 if (!ctxp->class_err) yyerror ("'{' expected");
864 DRECOVER(class1);
866 | modifiers CLASS_TK identifier error
867 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
870 super:
871 { $$ = NULL; }
872 | EXTENDS_TK class_type
873 { $$ = $2; }
874 | EXTENDS_TK class_type error
875 {yyerror ("'{' expected"); ctxp->class_err=1;}
876 | EXTENDS_TK error
877 {yyerror ("Missing super class name"); ctxp->class_err=1;}
880 interfaces:
881 { $$ = NULL_TREE; }
882 | IMPLEMENTS_TK interface_type_list
883 { $$ = $2; }
884 | IMPLEMENTS_TK error
886 ctxp->class_err=1;
887 yyerror ("Missing interface name");
891 interface_type_list:
892 interface_type
894 ctxp->interface_number = 1;
895 $$ = build_tree_list ($1, NULL_TREE);
897 | interface_type_list C_TK interface_type
899 ctxp->interface_number++;
900 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
902 | interface_type_list C_TK error
903 {yyerror ("Missing interface name"); RECOVER;}
906 class_body:
907 OCB_TK CCB_TK
909 /* Store the location of the `}' when doing xrefs */
910 if (flag_emit_xref)
911 DECL_END_SOURCE_LINE (GET_CPC ()) =
912 EXPR_WFL_ADD_COL ($2.location, 1);
913 $$ = GET_CPC ();
915 | OCB_TK class_body_declarations CCB_TK
917 /* Store the location of the `}' when doing xrefs */
918 if (flag_emit_xref)
919 DECL_END_SOURCE_LINE (GET_CPC ()) =
920 EXPR_WFL_ADD_COL ($3.location, 1);
921 $$ = GET_CPC ();
925 class_body_declarations:
926 class_body_declaration
927 | class_body_declarations class_body_declaration
930 class_body_declaration:
931 class_member_declaration
932 | static_initializer
933 | constructor_declaration
934 | block /* Added, JDK1.1, instance initializer */
936 if ($1 != empty_stmt_node)
938 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
939 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
944 class_member_declaration:
945 field_declaration
946 | method_declaration
947 | class_declaration /* Added, JDK1.1 inner classes */
948 { end_class_declaration (1); }
949 | interface_declaration /* Added, JDK1.1 inner interfaces */
950 { end_class_declaration (1); }
951 | empty_statement
954 /* 19.8.2 Productions from 8.3: Field Declarations */
955 field_declaration:
956 type variable_declarators SC_TK
957 { register_fields (0, $1, $2); }
958 | modifiers type variable_declarators SC_TK
960 check_modifiers
961 ("Illegal modifier `%s' for field declaration",
962 $1, FIELD_MODIFIERS);
963 check_modifiers_consistency ($1);
964 register_fields ($1, $2, $3);
968 variable_declarators:
969 /* Should we use build_decl_list () instead ? FIXME */
970 variable_declarator /* Default rule */
971 | variable_declarators C_TK variable_declarator
972 { $$ = chainon ($1, $3); }
973 | variable_declarators C_TK error
974 {yyerror ("Missing term"); RECOVER;}
977 variable_declarator:
978 variable_declarator_id
979 { $$ = build_tree_list ($1, NULL_TREE); }
980 | variable_declarator_id ASSIGN_TK variable_initializer
982 if (java_error_count)
983 $3 = NULL_TREE;
984 $$ = build_tree_list
985 ($1, build_assignment ($2.token, $2.location, $1, $3));
987 | variable_declarator_id ASSIGN_TK error
989 yyerror ("Missing variable initializer");
990 $$ = build_tree_list ($1, NULL_TREE);
991 RECOVER;
993 | variable_declarator_id ASSIGN_TK variable_initializer error
995 yyerror ("';' expected");
996 $$ = build_tree_list ($1, NULL_TREE);
997 RECOVER;
1001 variable_declarator_id:
1002 identifier
1003 | variable_declarator_id OSB_TK CSB_TK
1004 { $$ = build_unresolved_array_type ($1); }
1005 | identifier error
1006 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
1007 | variable_declarator_id OSB_TK error
1009 tree node = java_lval.node;
1010 if (node && (TREE_CODE (node) == INTEGER_CST
1011 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
1012 yyerror ("Can't specify array dimension in a declaration");
1013 else
1014 yyerror ("']' expected");
1015 DRECOVER(vdi);
1017 | variable_declarator_id CSB_TK error
1018 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1021 variable_initializer:
1022 expression
1023 | array_initializer
1026 /* 19.8.3 Productions from 8.4: Method Declarations */
1027 method_declaration:
1028 method_header
1030 current_function_decl = $1;
1031 if (current_function_decl
1032 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1033 source_start_java_method (current_function_decl);
1034 else
1035 current_function_decl = NULL_TREE;
1037 method_body
1038 { finish_method_declaration ($3); }
1039 | method_header error
1040 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1043 method_header:
1044 type method_declarator throws
1045 { $$ = method_header (0, $1, $2, $3); }
1046 | VOID_TK method_declarator throws
1047 { $$ = method_header (0, void_type_node, $2, $3); }
1048 | modifiers type method_declarator throws
1049 { $$ = method_header ($1, $2, $3, $4); }
1050 | modifiers VOID_TK method_declarator throws
1051 { $$ = method_header ($1, void_type_node, $3, $4); }
1052 | type error
1054 yyerror ("Invalid method declaration, method name required");
1055 RECOVER;
1057 | modifiers type error
1058 {RECOVER;}
1059 | VOID_TK error
1060 {yyerror ("Identifier expected"); RECOVER;}
1061 | modifiers VOID_TK error
1062 {yyerror ("Identifier expected"); RECOVER;}
1063 | modifiers error
1065 yyerror ("Invalid method declaration, return type required");
1066 RECOVER;
1070 method_declarator:
1071 identifier OP_TK CP_TK
1073 ctxp->formal_parameter_number = 0;
1074 $$ = method_declarator ($1, NULL_TREE);
1076 | identifier OP_TK formal_parameter_list CP_TK
1077 { $$ = method_declarator ($1, $3); }
1078 | method_declarator OSB_TK CSB_TK
1080 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1081 TREE_PURPOSE ($1) =
1082 build_unresolved_array_type (TREE_PURPOSE ($1));
1083 parse_warning_context
1084 (wfl_operator,
1085 "Discouraged form of returned type specification");
1087 | identifier OP_TK error
1088 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1089 | method_declarator OSB_TK error
1090 {yyerror ("']' expected"); RECOVER;}
1093 formal_parameter_list:
1094 formal_parameter
1096 ctxp->formal_parameter_number = 1;
1098 | formal_parameter_list C_TK formal_parameter
1100 ctxp->formal_parameter_number += 1;
1101 $$ = chainon ($1, $3);
1103 | formal_parameter_list C_TK error
1104 { yyerror ("Missing formal parameter term"); RECOVER; }
1107 formal_parameter:
1108 type variable_declarator_id
1110 $$ = build_tree_list ($2, $1);
1112 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1114 $$ = build_tree_list ($3, $2);
1115 ARG_FINAL_P ($$) = 1;
1117 | type error
1119 yyerror ("Missing identifier"); RECOVER;
1120 $$ = NULL_TREE;
1122 | final type error
1124 yyerror ("Missing identifier"); RECOVER;
1125 $$ = NULL_TREE;
1129 final:
1130 modifiers
1132 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1133 $1, ACC_FINAL);
1134 if ($1 != ACC_FINAL)
1135 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1139 throws:
1140 { $$ = NULL_TREE; }
1141 | THROWS_TK class_type_list
1142 { $$ = $2; }
1143 | THROWS_TK error
1144 {yyerror ("Missing class type term"); RECOVER;}
1147 class_type_list:
1148 class_type
1149 { $$ = build_tree_list ($1, $1); }
1150 | class_type_list C_TK class_type
1151 { $$ = tree_cons ($3, $3, $1); }
1152 | class_type_list C_TK error
1153 {yyerror ("Missing class type term"); RECOVER;}
1156 method_body:
1157 block
1158 | SC_TK { $$ = NULL_TREE; }
1161 /* 19.8.4 Productions from 8.5: Static Initializers */
1162 static_initializer:
1163 static block
1165 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1166 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1167 current_static_block = NULL_TREE;
1171 static: /* Test lval.sub_token here */
1172 modifiers
1174 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1175 /* Can't have a static initializer in an innerclass */
1176 if ($1 | ACC_STATIC &&
1177 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1178 parse_error_context
1179 (MODIFIER_WFL (STATIC_TK),
1180 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1181 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1182 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1186 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1187 constructor_declaration:
1188 constructor_header
1190 current_function_decl = $1;
1191 source_start_java_method (current_function_decl);
1193 constructor_body
1194 { finish_method_declaration ($3); }
1197 constructor_header:
1198 constructor_declarator throws
1199 { $$ = method_header (0, NULL_TREE, $1, $2); }
1200 | modifiers constructor_declarator throws
1201 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1204 constructor_declarator:
1205 simple_name OP_TK CP_TK
1207 ctxp->formal_parameter_number = 0;
1208 $$ = method_declarator ($1, NULL_TREE);
1210 | simple_name OP_TK formal_parameter_list CP_TK
1211 { $$ = method_declarator ($1, $3); }
1214 constructor_body:
1215 /* Unlike regular method, we always need a complete (empty)
1216 body so we can safely perform all the required code
1217 addition (super invocation and field initialization) */
1218 block_begin constructor_block_end
1220 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1221 $$ = $2;
1223 | block_begin explicit_constructor_invocation constructor_block_end
1224 { $$ = $3; }
1225 | block_begin block_statements constructor_block_end
1226 { $$ = $3; }
1227 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1228 { $$ = $4; }
1231 constructor_block_end:
1232 block_end
1235 /* Error recovery for that rule moved down expression_statement: rule. */
1236 explicit_constructor_invocation:
1237 this_or_super OP_TK CP_TK SC_TK
1239 $$ = build_method_invocation ($1, NULL_TREE);
1240 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1241 $$ = java_method_add_stmt (current_function_decl, $$);
1243 | this_or_super OP_TK argument_list CP_TK SC_TK
1245 $$ = build_method_invocation ($1, $3);
1246 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1247 $$ = java_method_add_stmt (current_function_decl, $$);
1249 /* Added, JDK1.1 inner classes. Modified because the rule
1250 'primary' couldn't work. */
1251 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1252 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1253 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1254 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1257 this_or_super: /* Added, simplifies error diagnostics */
1258 THIS_TK
1260 tree wfl = build_wfl_node (this_identifier_node);
1261 EXPR_WFL_LINECOL (wfl) = $1.location;
1262 $$ = wfl;
1264 | SUPER_TK
1266 tree wfl = build_wfl_node (super_identifier_node);
1267 EXPR_WFL_LINECOL (wfl) = $1.location;
1268 $$ = wfl;
1272 /* 19.9 Productions from 9: Interfaces */
1273 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1274 interface_declaration:
1275 INTERFACE_TK identifier
1276 { create_interface (0, $2, NULL_TREE); }
1277 interface_body
1278 | modifiers INTERFACE_TK identifier
1279 { create_interface ($1, $3, NULL_TREE); }
1280 interface_body
1281 | INTERFACE_TK identifier extends_interfaces
1282 { create_interface (0, $2, $3); }
1283 interface_body
1284 | modifiers INTERFACE_TK identifier extends_interfaces
1285 { create_interface ($1, $3, $4); }
1286 interface_body
1287 | INTERFACE_TK identifier error
1288 {yyerror ("'{' expected"); RECOVER;}
1289 | modifiers INTERFACE_TK identifier error
1290 {yyerror ("'{' expected"); RECOVER;}
1293 extends_interfaces:
1294 EXTENDS_TK interface_type
1296 ctxp->interface_number = 1;
1297 $$ = build_tree_list ($2, NULL_TREE);
1299 | extends_interfaces C_TK interface_type
1301 ctxp->interface_number++;
1302 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1304 | EXTENDS_TK error
1305 {yyerror ("Invalid interface type"); RECOVER;}
1306 | extends_interfaces C_TK error
1307 {yyerror ("Missing term"); RECOVER;}
1310 interface_body:
1311 OCB_TK CCB_TK
1312 { $$ = NULL_TREE; }
1313 | OCB_TK interface_member_declarations CCB_TK
1314 { $$ = NULL_TREE; }
1317 interface_member_declarations:
1318 interface_member_declaration
1319 | interface_member_declarations interface_member_declaration
1322 interface_member_declaration:
1323 constant_declaration
1324 | abstract_method_declaration
1325 | class_declaration /* Added, JDK1.1 inner classes */
1326 { end_class_declaration (1); }
1327 | interface_declaration /* Added, JDK1.1 inner interfaces */
1328 { end_class_declaration (1); }
1331 constant_declaration:
1332 field_declaration
1335 abstract_method_declaration:
1336 method_header SC_TK
1338 check_abstract_method_header ($1);
1339 current_function_decl = NULL_TREE; /* FIXME ? */
1341 | method_header error
1342 {yyerror ("';' expected"); RECOVER;}
1345 /* 19.10 Productions from 10: Arrays */
1346 array_initializer:
1347 OCB_TK CCB_TK
1348 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1349 | OCB_TK variable_initializers CCB_TK
1350 { $$ = build_new_array_init ($1.location, $2); }
1351 | OCB_TK variable_initializers C_TK CCB_TK
1352 { $$ = build_new_array_init ($1.location, $2); }
1355 variable_initializers:
1356 variable_initializer
1358 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1359 $1, NULL_TREE);
1361 | variable_initializers C_TK variable_initializer
1363 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1365 | variable_initializers C_TK error
1366 {yyerror ("Missing term"); RECOVER;}
1369 /* 19.11 Production from 14: Blocks and Statements */
1370 block:
1371 OCB_TK CCB_TK
1373 /* Store the location of the `}' when doing xrefs */
1374 if (current_function_decl && flag_emit_xref)
1375 DECL_END_SOURCE_LINE (current_function_decl) =
1376 EXPR_WFL_ADD_COL ($2.location, 1);
1377 $$ = empty_stmt_node;
1379 | block_begin block_statements block_end
1380 { $$ = $3; }
1383 block_begin:
1384 OCB_TK
1385 { enter_block (); }
1388 block_end:
1389 CCB_TK
1391 maybe_absorb_scoping_blocks ();
1392 /* Store the location of the `}' when doing xrefs */
1393 if (current_function_decl && flag_emit_xref)
1394 DECL_END_SOURCE_LINE (current_function_decl) =
1395 EXPR_WFL_ADD_COL ($1.location, 1);
1396 $$ = exit_block ();
1397 if (!BLOCK_SUBBLOCKS ($$))
1398 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1402 block_statements:
1403 block_statement
1404 | block_statements block_statement
1407 block_statement:
1408 local_variable_declaration_statement
1409 | statement
1410 { java_method_add_stmt (current_function_decl, $1); }
1411 | class_declaration /* Added, JDK1.1 local classes */
1413 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1414 end_class_declaration (1);
1418 local_variable_declaration_statement:
1419 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1422 local_variable_declaration:
1423 type variable_declarators
1424 { declare_local_variables (0, $1, $2); }
1425 | final type variable_declarators /* Added, JDK1.1 final locals */
1426 { declare_local_variables ($1, $2, $3); }
1429 statement:
1430 statement_without_trailing_substatement
1431 | labeled_statement
1432 | if_then_statement
1433 | if_then_else_statement
1434 | while_statement
1435 | for_statement
1436 { $$ = exit_block (); }
1439 statement_nsi:
1440 statement_without_trailing_substatement
1441 | labeled_statement_nsi
1442 | if_then_else_statement_nsi
1443 | while_statement_nsi
1444 | for_statement_nsi
1445 { $$ = exit_block (); }
1448 statement_without_trailing_substatement:
1449 block
1450 | empty_statement
1451 | expression_statement
1452 | switch_statement
1453 | do_statement
1454 | break_statement
1455 | continue_statement
1456 | return_statement
1457 | synchronized_statement
1458 | throw_statement
1459 | try_statement
1462 empty_statement:
1463 SC_TK
1465 if (flag_extraneous_semicolon
1466 && ! current_static_block
1467 && (! current_function_decl ||
1468 /* Verify we're not in a inner class declaration */
1469 (GET_CPC () != TYPE_NAME
1470 (DECL_CONTEXT (current_function_decl)))))
1473 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1474 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1476 $$ = empty_stmt_node;
1480 label_decl:
1481 identifier REL_CL_TK
1483 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1484 EXPR_WFL_NODE ($1));
1485 pushlevel (2);
1486 push_labeled_block ($$);
1487 PUSH_LABELED_BLOCK ($$);
1491 labeled_statement:
1492 label_decl statement
1493 { $$ = finish_labeled_statement ($1, $2); }
1494 | identifier error
1495 {yyerror ("':' expected"); RECOVER;}
1498 labeled_statement_nsi:
1499 label_decl statement_nsi
1500 { $$ = finish_labeled_statement ($1, $2); }
1503 /* We concentrate here a bunch of error handling rules that we couldn't write
1504 earlier, because expression_statement catches a missing ';'. */
1505 expression_statement:
1506 statement_expression SC_TK
1508 /* We have a statement. Generate a WFL around it so
1509 we can debug it */
1510 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1511 /* We know we have a statement, so set the debug
1512 info to be eventually generate here. */
1513 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1515 | error SC_TK
1517 YYNOT_TWICE yyerror ("Invalid expression statement");
1518 DRECOVER (expr_stmt);
1520 | error OCB_TK
1522 YYNOT_TWICE yyerror ("Invalid expression statement");
1523 DRECOVER (expr_stmt);
1525 | error CCB_TK
1527 YYNOT_TWICE yyerror ("Invalid expression statement");
1528 DRECOVER (expr_stmt);
1530 | this_or_super OP_TK error
1531 {yyerror ("')' expected"); RECOVER;}
1532 | this_or_super OP_TK CP_TK error
1534 parse_ctor_invocation_error ();
1535 RECOVER;
1537 | this_or_super OP_TK argument_list error
1538 {yyerror ("')' expected"); RECOVER;}
1539 | this_or_super OP_TK argument_list CP_TK error
1541 parse_ctor_invocation_error ();
1542 RECOVER;
1544 | name DOT_TK SUPER_TK error
1545 {yyerror ("'(' expected"); RECOVER;}
1546 | name DOT_TK SUPER_TK OP_TK error
1547 {yyerror ("')' expected"); RECOVER;}
1548 | name DOT_TK SUPER_TK OP_TK argument_list error
1549 {yyerror ("')' expected"); RECOVER;}
1550 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1551 {yyerror ("';' expected"); RECOVER;}
1552 | name DOT_TK SUPER_TK OP_TK CP_TK error
1553 {yyerror ("';' expected"); RECOVER;}
1556 statement_expression:
1557 assignment
1558 | pre_increment_expression
1559 | pre_decrement_expression
1560 | post_increment_expression
1561 | post_decrement_expression
1562 | method_invocation
1563 | class_instance_creation_expression
1566 if_then_statement:
1567 IF_TK OP_TK expression CP_TK statement
1569 $$ = build_if_else_statement ($2.location, $3,
1570 $5, NULL_TREE);
1572 | IF_TK error
1573 {yyerror ("'(' expected"); RECOVER;}
1574 | IF_TK OP_TK error
1575 {yyerror ("Missing term"); RECOVER;}
1576 | IF_TK OP_TK expression error
1577 {yyerror ("')' expected"); RECOVER;}
1580 if_then_else_statement:
1581 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1582 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1585 if_then_else_statement_nsi:
1586 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1587 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1590 switch_statement:
1591 switch_expression
1593 enter_block ();
1595 switch_block
1597 /* Make into "proper list" of COMPOUND_EXPRs.
1598 I.e. make the last statment also have its own
1599 COMPOUND_EXPR. */
1600 maybe_absorb_scoping_blocks ();
1601 TREE_OPERAND ($1, 1) = exit_block ();
1602 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1606 switch_expression:
1607 SWITCH_TK OP_TK expression CP_TK
1609 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1610 EXPR_WFL_LINECOL ($$) = $2.location;
1612 | SWITCH_TK error
1613 {yyerror ("'(' expected"); RECOVER;}
1614 | SWITCH_TK OP_TK error
1615 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1616 | SWITCH_TK OP_TK expression CP_TK error
1617 {yyerror ("'{' expected"); RECOVER;}
1620 /* Default assignment is there to avoid type node on switch_block
1621 node. */
1623 switch_block:
1624 OCB_TK CCB_TK
1625 { $$ = NULL_TREE; }
1626 | OCB_TK switch_labels CCB_TK
1627 { $$ = NULL_TREE; }
1628 | OCB_TK switch_block_statement_groups CCB_TK
1629 { $$ = NULL_TREE; }
1630 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1631 { $$ = NULL_TREE; }
1634 switch_block_statement_groups:
1635 switch_block_statement_group
1636 | switch_block_statement_groups switch_block_statement_group
1639 switch_block_statement_group:
1640 switch_labels block_statements
1643 switch_labels:
1644 switch_label
1645 | switch_labels switch_label
1648 switch_label:
1649 CASE_TK constant_expression REL_CL_TK
1651 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1652 EXPR_WFL_LINECOL (lab) = $1.location;
1653 java_method_add_stmt (current_function_decl, lab);
1655 | DEFAULT_TK REL_CL_TK
1657 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1658 EXPR_WFL_LINECOL (lab) = $1.location;
1659 java_method_add_stmt (current_function_decl, lab);
1661 | CASE_TK error
1662 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1663 | CASE_TK constant_expression error
1664 {yyerror ("':' expected"); RECOVER;}
1665 | DEFAULT_TK error
1666 {yyerror ("':' expected"); RECOVER;}
1669 while_expression:
1670 WHILE_TK OP_TK expression CP_TK
1672 tree body = build_loop_body ($2.location, $3, 0);
1673 $$ = build_new_loop (body);
1677 while_statement:
1678 while_expression statement
1679 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1680 | WHILE_TK error
1681 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1682 | WHILE_TK OP_TK error
1683 {yyerror ("Missing term and ')' expected"); RECOVER;}
1684 | WHILE_TK OP_TK expression error
1685 {yyerror ("')' expected"); RECOVER;}
1688 while_statement_nsi:
1689 while_expression statement_nsi
1690 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1693 do_statement_begin:
1694 DO_TK
1696 tree body = build_loop_body (0, NULL_TREE, 1);
1697 $$ = build_new_loop (body);
1699 /* Need error handing here. FIXME */
1702 do_statement:
1703 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1704 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1707 for_statement:
1708 for_begin SC_TK expression SC_TK for_update CP_TK statement
1710 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1711 $3 = build_wfl_node ($3);
1712 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1714 | for_begin SC_TK SC_TK for_update CP_TK statement
1716 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1717 /* We have not condition, so we get rid of the EXIT_EXPR */
1718 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1719 empty_stmt_node;
1721 | for_begin SC_TK error
1722 {yyerror ("Invalid control expression"); RECOVER;}
1723 | for_begin SC_TK expression SC_TK error
1724 {yyerror ("Invalid update expression"); RECOVER;}
1725 | for_begin SC_TK SC_TK error
1726 {yyerror ("Invalid update expression"); RECOVER;}
1729 for_statement_nsi:
1730 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1731 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1732 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1734 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1735 /* We have not condition, so we get rid of the EXIT_EXPR */
1736 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1737 empty_stmt_node;
1741 for_header:
1742 FOR_TK OP_TK
1744 /* This scope defined for local variable that may be
1745 defined within the scope of the for loop */
1746 enter_block ();
1748 | FOR_TK error
1749 {yyerror ("'(' expected"); DRECOVER(for_1);}
1750 | FOR_TK OP_TK error
1751 {yyerror ("Invalid init statement"); RECOVER;}
1754 for_begin:
1755 for_header for_init
1757 /* We now declare the loop body. The loop is
1758 declared as a for loop. */
1759 tree body = build_loop_body (0, NULL_TREE, 0);
1760 $$ = build_new_loop (body);
1761 FOR_LOOP_P ($$) = 1;
1762 /* The loop is added to the current block the for
1763 statement is defined within */
1764 java_method_add_stmt (current_function_decl, $$);
1767 for_init: /* Can be empty */
1768 { $$ = empty_stmt_node; }
1769 | statement_expression_list
1771 /* Init statement recorded within the previously
1772 defined block scope */
1773 $$ = java_method_add_stmt (current_function_decl, $1);
1775 | local_variable_declaration
1777 /* Local variable are recorded within the previously
1778 defined block scope */
1779 $$ = NULL_TREE;
1781 | statement_expression_list error
1782 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1785 for_update: /* Can be empty */
1786 {$$ = empty_stmt_node;}
1787 | statement_expression_list
1788 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1791 statement_expression_list:
1792 statement_expression
1793 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1794 | statement_expression_list C_TK statement_expression
1795 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1796 | statement_expression_list C_TK error
1797 {yyerror ("Missing term"); RECOVER;}
1800 break_statement:
1801 BREAK_TK SC_TK
1802 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1803 | BREAK_TK identifier SC_TK
1804 { $$ = build_bc_statement ($1.location, 1, $2); }
1805 | BREAK_TK error
1806 {yyerror ("Missing term"); RECOVER;}
1807 | BREAK_TK identifier error
1808 {yyerror ("';' expected"); RECOVER;}
1811 continue_statement:
1812 CONTINUE_TK SC_TK
1813 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1814 | CONTINUE_TK identifier SC_TK
1815 { $$ = build_bc_statement ($1.location, 0, $2); }
1816 | CONTINUE_TK error
1817 {yyerror ("Missing term"); RECOVER;}
1818 | CONTINUE_TK identifier error
1819 {yyerror ("';' expected"); RECOVER;}
1822 return_statement:
1823 RETURN_TK SC_TK
1824 { $$ = build_return ($1.location, NULL_TREE); }
1825 | RETURN_TK expression SC_TK
1826 { $$ = build_return ($1.location, $2); }
1827 | RETURN_TK error
1828 {yyerror ("Missing term"); RECOVER;}
1829 | RETURN_TK expression error
1830 {yyerror ("';' expected"); RECOVER;}
1833 throw_statement:
1834 THROW_TK expression SC_TK
1836 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1837 EXPR_WFL_LINECOL ($$) = $1.location;
1839 | THROW_TK error
1840 {yyerror ("Missing term"); RECOVER;}
1841 | THROW_TK expression error
1842 {yyerror ("';' expected"); RECOVER;}
1845 synchronized_statement:
1846 synchronized OP_TK expression CP_TK block
1848 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1849 EXPR_WFL_LINECOL ($$) =
1850 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1852 | synchronized OP_TK expression CP_TK error
1853 {yyerror ("'{' expected"); RECOVER;}
1854 | synchronized error
1855 {yyerror ("'(' expected"); RECOVER;}
1856 | synchronized OP_TK error CP_TK
1857 {yyerror ("Missing term"); RECOVER;}
1858 | synchronized OP_TK error
1859 {yyerror ("Missing term"); RECOVER;}
1862 synchronized:
1863 modifiers
1865 check_modifiers (
1866 "Illegal modifier `%s'. Only `synchronized' was expected here",
1867 $1, ACC_SYNCHRONIZED);
1868 if ($1 != ACC_SYNCHRONIZED)
1869 MODIFIER_WFL (SYNCHRONIZED_TK) =
1870 build_wfl_node (NULL_TREE);
1874 try_statement:
1875 TRY_TK block catches
1876 { $$ = build_try_statement ($1.location, $2, $3); }
1877 | TRY_TK block finally
1878 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1879 | TRY_TK block catches finally
1880 { $$ = build_try_finally_statement
1881 ($1.location, build_try_statement ($1.location,
1882 $2, $3), $4);
1884 | TRY_TK error
1885 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1888 catches:
1889 catch_clause
1890 | catches catch_clause
1892 TREE_CHAIN ($2) = $1;
1893 $$ = $2;
1897 catch_clause:
1898 catch_clause_parameter block
1900 java_method_add_stmt (current_function_decl, $2);
1901 exit_block ();
1902 $$ = $1;
1905 catch_clause_parameter:
1906 CATCH_TK OP_TK formal_parameter CP_TK
1908 /* We add a block to define a scope for
1909 formal_parameter (CCBP). The formal parameter is
1910 declared initialized by the appropriate function
1911 call */
1912 tree ccpb = enter_block ();
1913 tree init = build_assignment
1914 (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1915 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1916 declare_local_variables (0, TREE_VALUE ($3),
1917 build_tree_list (TREE_PURPOSE ($3),
1918 init));
1919 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1920 EXPR_WFL_LINECOL ($$) = $1.location;
1922 | CATCH_TK error
1923 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1924 | CATCH_TK OP_TK error
1926 yyerror ("Missing term or ')' expected");
1927 RECOVER; $$ = NULL_TREE;
1929 | CATCH_TK OP_TK error CP_TK /* That's for () */
1930 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1933 finally:
1934 FINALLY_TK block
1935 { $$ = $2; }
1936 | FINALLY_TK error
1937 {yyerror ("'{' expected"); RECOVER; }
1940 /* 19.12 Production from 15: Expressions */
1941 primary:
1942 primary_no_new_array
1943 | array_creation_expression
1946 primary_no_new_array:
1947 literal
1948 | THIS_TK
1949 { $$ = build_this ($1.location); }
1950 | OP_TK expression CP_TK
1951 {$$ = $2;}
1952 | class_instance_creation_expression
1953 | field_access
1954 | method_invocation
1955 | array_access
1956 | type_literals
1957 /* Added, JDK1.1 inner classes. Documentation is wrong
1958 refering to a 'ClassName' (class_name) rule that doesn't
1959 exist. Used name: instead. */
1960 | name DOT_TK THIS_TK
1962 tree wfl = build_wfl_node (this_identifier_node);
1963 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1965 | OP_TK expression error
1966 {yyerror ("')' expected"); RECOVER;}
1967 | name DOT_TK error
1968 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1969 | primitive_type DOT_TK error
1970 {yyerror ("'class' expected" ); RECOVER;}
1971 | VOID_TK DOT_TK error
1972 {yyerror ("'class' expected" ); RECOVER;}
1975 type_literals:
1976 name DOT_TK CLASS_TK
1977 { $$ = build_incomplete_class_ref ($2.location, $1); }
1978 | array_type DOT_TK CLASS_TK
1979 { $$ = build_incomplete_class_ref ($2.location, $1); }
1980 | primitive_type DOT_TK CLASS_TK
1981 { $$ = build_incomplete_class_ref ($2.location, $1); }
1982 | VOID_TK DOT_TK CLASS_TK
1984 $$ = build_incomplete_class_ref ($2.location,
1985 void_type_node);
1989 class_instance_creation_expression:
1990 NEW_TK class_type OP_TK argument_list CP_TK
1991 { $$ = build_new_invocation ($2, $4); }
1992 | NEW_TK class_type OP_TK CP_TK
1993 { $$ = build_new_invocation ($2, NULL_TREE); }
1994 | anonymous_class_creation
1995 /* Added, JDK1.1 inner classes, modified to use name or
1996 primary instead of primary solely which couldn't work in
1997 all situations. */
1998 | something_dot_new identifier OP_TK CP_TK
2000 tree ctor = build_new_invocation ($2, NULL_TREE);
2001 $$ = make_qualified_primary ($1, ctor,
2002 EXPR_WFL_LINECOL ($1));
2004 | something_dot_new identifier OP_TK CP_TK class_body
2005 | something_dot_new identifier OP_TK argument_list CP_TK
2007 tree ctor = build_new_invocation ($2, $4);
2008 $$ = make_qualified_primary ($1, ctor,
2009 EXPR_WFL_LINECOL ($1));
2011 | something_dot_new identifier OP_TK argument_list CP_TK class_body
2012 | NEW_TK error SC_TK
2013 {yyerror ("'(' expected"); DRECOVER(new_1);}
2014 | NEW_TK class_type error
2015 {yyerror ("'(' expected"); RECOVER;}
2016 | NEW_TK class_type OP_TK error
2017 {yyerror ("')' or term expected"); RECOVER;}
2018 | NEW_TK class_type OP_TK argument_list error
2019 {yyerror ("')' expected"); RECOVER;}
2020 | something_dot_new error
2021 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
2022 | something_dot_new identifier error
2023 {yyerror ("'(' expected"); RECOVER;}
2026 /* Created after JDK1.1 rules originally added to
2027 class_instance_creation_expression, but modified to use
2028 'class_type' instead of 'TypeName' (type_name) which is mentionned
2029 in the documentation but doesn't exist. */
2031 anonymous_class_creation:
2032 NEW_TK class_type OP_TK argument_list CP_TK
2033 { create_anonymous_class ($1.location, $2); }
2034 class_body
2036 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2037 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2039 end_class_declaration (1);
2041 /* Now we can craft the new expression */
2042 $$ = build_new_invocation (id, $4);
2044 /* Note that we can't possibly be here if
2045 `class_type' is an interface (in which case the
2046 anonymous class extends Object and implements
2047 `class_type', hence its constructor can't have
2048 arguments.) */
2050 /* Otherwise, the innerclass must feature a
2051 constructor matching `argument_list'. Anonymous
2052 classes are a bit special: it's impossible to
2053 define constructor for them, hence constructors
2054 must be generated following the hints provided by
2055 the `new' expression. Whether a super constructor
2056 of that nature exists or not is to be verified
2057 later on in verify_constructor_super.
2059 It's during the expansion of a `new' statement
2060 refering to an anonymous class that a ctor will
2061 be generated for the anonymous class, with the
2062 right arguments. */
2065 | NEW_TK class_type OP_TK CP_TK
2066 { create_anonymous_class ($1.location, $2); }
2067 class_body
2069 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2070 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2072 end_class_declaration (1);
2074 /* Now we can craft the new expression. The
2075 statement doesn't need to be remember so that a
2076 constructor can be generated, since its signature
2077 is already known. */
2078 $$ = build_new_invocation (id, NULL_TREE);
2082 something_dot_new: /* Added, not part of the specs. */
2083 name DOT_TK NEW_TK
2084 { $$ = $1; }
2085 | primary DOT_TK NEW_TK
2086 { $$ = $1; }
2089 argument_list:
2090 expression
2092 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2093 ctxp->formal_parameter_number = 1;
2095 | argument_list C_TK expression
2097 ctxp->formal_parameter_number += 1;
2098 $$ = tree_cons (NULL_TREE, $3, $1);
2100 | argument_list C_TK error
2101 {yyerror ("Missing term"); RECOVER;}
2104 array_creation_expression:
2105 NEW_TK primitive_type dim_exprs
2106 { $$ = build_newarray_node ($2, $3, 0); }
2107 | NEW_TK class_or_interface_type dim_exprs
2108 { $$ = build_newarray_node ($2, $3, 0); }
2109 | NEW_TK primitive_type dim_exprs dims
2110 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2111 | NEW_TK class_or_interface_type dim_exprs dims
2112 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2113 /* Added, JDK1.1 anonymous array. Initial documentation rule
2114 modified */
2115 | NEW_TK class_or_interface_type dims array_initializer
2117 char *sig;
2118 int osb = pop_current_osb (ctxp);
2119 while (osb--)
2120 obstack_1grow (&temporary_obstack, '[');
2121 sig = obstack_finish (&temporary_obstack);
2122 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2123 $2, get_identifier (sig), $4);
2125 | NEW_TK primitive_type dims array_initializer
2127 int osb = pop_current_osb (ctxp);
2128 tree type = $2;
2129 while (osb--)
2130 type = build_java_array_type (type, -1);
2131 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2132 build_pointer_type (type), NULL_TREE, $4);
2134 | NEW_TK error CSB_TK
2135 {yyerror ("'[' expected"); DRECOVER ("]");}
2136 | NEW_TK error OSB_TK
2137 {yyerror ("']' expected"); RECOVER;}
2140 dim_exprs:
2141 dim_expr
2142 { $$ = build_tree_list (NULL_TREE, $1); }
2143 | dim_exprs dim_expr
2144 { $$ = tree_cons (NULL_TREE, $2, $$); }
2147 dim_expr:
2148 OSB_TK expression CSB_TK
2150 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2152 $2 = build_wfl_node ($2);
2153 TREE_TYPE ($2) = NULL_TREE;
2155 EXPR_WFL_LINECOL ($2) = $1.location;
2156 $$ = $2;
2158 | OSB_TK expression error
2159 {yyerror ("']' expected"); RECOVER;}
2160 | OSB_TK error
2162 yyerror ("Missing term");
2163 yyerror ("']' expected");
2164 RECOVER;
2168 dims:
2169 OSB_TK CSB_TK
2171 int allocate = 0;
2172 /* If not initialized, allocate memory for the osb
2173 numbers stack */
2174 if (!ctxp->osb_limit)
2176 allocate = ctxp->osb_limit = 32;
2177 ctxp->osb_depth = -1;
2179 /* If capacity overflown, reallocate a bigger chunk */
2180 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2181 allocate = ctxp->osb_limit << 1;
2183 if (allocate)
2185 allocate *= sizeof (int);
2186 if (ctxp->osb_number)
2187 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2188 allocate);
2189 else
2190 ctxp->osb_number = (int *)xmalloc (allocate);
2192 ctxp->osb_depth++;
2193 CURRENT_OSB (ctxp) = 1;
2195 | dims OSB_TK CSB_TK
2196 { CURRENT_OSB (ctxp)++; }
2197 | dims OSB_TK error
2198 { yyerror ("']' expected"); RECOVER;}
2201 field_access:
2202 primary DOT_TK identifier
2203 { $$ = make_qualified_primary ($1, $3, $2.location); }
2204 /* FIXME - REWRITE TO:
2205 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2206 | SUPER_TK DOT_TK identifier
2208 tree super_wfl = build_wfl_node (super_identifier_node);
2209 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2210 $$ = make_qualified_name (super_wfl, $3, $2.location);
2212 | SUPER_TK error
2213 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2216 method_invocation:
2217 name OP_TK CP_TK
2218 { $$ = build_method_invocation ($1, NULL_TREE); }
2219 | name OP_TK argument_list CP_TK
2220 { $$ = build_method_invocation ($1, $3); }
2221 | primary DOT_TK identifier OP_TK CP_TK
2223 if (TREE_CODE ($1) == THIS_EXPR)
2224 $$ = build_this_super_qualified_invocation
2225 (1, $3, NULL_TREE, 0, $2.location);
2226 else
2228 tree invok = build_method_invocation ($3, NULL_TREE);
2229 $$ = make_qualified_primary ($1, invok, $2.location);
2232 | primary DOT_TK identifier OP_TK argument_list CP_TK
2234 if (TREE_CODE ($1) == THIS_EXPR)
2235 $$ = build_this_super_qualified_invocation
2236 (1, $3, $5, 0, $2.location);
2237 else
2239 tree invok = build_method_invocation ($3, $5);
2240 $$ = make_qualified_primary ($1, invok, $2.location);
2243 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2245 $$ = build_this_super_qualified_invocation
2246 (0, $3, NULL_TREE, $1.location, $2.location);
2248 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2250 $$ = build_this_super_qualified_invocation
2251 (0, $3, $5, $1.location, $2.location);
2253 /* Screws up thing. I let it here until I'm convinced it can
2254 be removed. FIXME
2255 | primary DOT_TK error
2256 {yyerror ("'(' expected"); DRECOVER(bad);} */
2257 | SUPER_TK DOT_TK error CP_TK
2258 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2259 | SUPER_TK DOT_TK error DOT_TK
2260 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2263 array_access:
2264 name OSB_TK expression CSB_TK
2265 { $$ = build_array_ref ($2.location, $1, $3); }
2266 | primary_no_new_array OSB_TK expression CSB_TK
2267 { $$ = build_array_ref ($2.location, $1, $3); }
2268 | name OSB_TK error
2270 yyerror ("Missing term and ']' expected");
2271 DRECOVER(array_access);
2273 | name OSB_TK expression error
2275 yyerror ("']' expected");
2276 DRECOVER(array_access);
2278 | primary_no_new_array OSB_TK error
2280 yyerror ("Missing term and ']' expected");
2281 DRECOVER(array_access);
2283 | primary_no_new_array OSB_TK expression error
2285 yyerror ("']' expected");
2286 DRECOVER(array_access);
2290 postfix_expression:
2291 primary
2292 | name
2293 | post_increment_expression
2294 | post_decrement_expression
2297 post_increment_expression:
2298 postfix_expression INCR_TK
2299 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2302 post_decrement_expression:
2303 postfix_expression DECR_TK
2304 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2307 unary_expression:
2308 pre_increment_expression
2309 | pre_decrement_expression
2310 | PLUS_TK unary_expression
2311 {$$ = build_unaryop ($1.token, $1.location, $2); }
2312 | MINUS_TK unary_expression
2313 {$$ = build_unaryop ($1.token, $1.location, $2); }
2314 | unary_expression_not_plus_minus
2315 | PLUS_TK error
2316 {yyerror ("Missing term"); RECOVER}
2317 | MINUS_TK error
2318 {yyerror ("Missing term"); RECOVER}
2321 pre_increment_expression:
2322 INCR_TK unary_expression
2323 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2324 | INCR_TK error
2325 {yyerror ("Missing term"); RECOVER}
2328 pre_decrement_expression:
2329 DECR_TK unary_expression
2330 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2331 | DECR_TK error
2332 {yyerror ("Missing term"); RECOVER}
2335 unary_expression_not_plus_minus:
2336 postfix_expression
2337 | NOT_TK unary_expression
2338 {$$ = build_unaryop ($1.token, $1.location, $2); }
2339 | NEG_TK unary_expression
2340 {$$ = build_unaryop ($1.token, $1.location, $2); }
2341 | cast_expression
2342 | NOT_TK error
2343 {yyerror ("Missing term"); RECOVER}
2344 | NEG_TK error
2345 {yyerror ("Missing term"); RECOVER}
2348 cast_expression: /* Error handling here is potentially weak */
2349 OP_TK primitive_type dims CP_TK unary_expression
2351 tree type = $2;
2352 int osb = pop_current_osb (ctxp);
2353 while (osb--)
2354 type = build_java_array_type (type, -1);
2355 $$ = build_cast ($1.location, type, $5);
2357 | OP_TK primitive_type CP_TK unary_expression
2358 { $$ = build_cast ($1.location, $2, $4); }
2359 | OP_TK expression CP_TK unary_expression_not_plus_minus
2360 { $$ = build_cast ($1.location, $2, $4); }
2361 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2363 const char *ptr;
2364 int osb = pop_current_osb (ctxp);
2365 while (osb--)
2366 obstack_1grow (&temporary_obstack, '[');
2367 obstack_grow0 (&temporary_obstack,
2368 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2369 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2370 ptr = obstack_finish (&temporary_obstack);
2371 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2372 $$ = build_cast ($1.location, $2, $5);
2374 | OP_TK primitive_type OSB_TK error
2375 {yyerror ("']' expected, invalid type expression");}
2376 | OP_TK error
2378 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2379 RECOVER;
2381 | OP_TK primitive_type dims CP_TK error
2382 {yyerror ("Missing term"); RECOVER;}
2383 | OP_TK primitive_type CP_TK error
2384 {yyerror ("Missing term"); RECOVER;}
2385 | OP_TK name dims CP_TK error
2386 {yyerror ("Missing term"); RECOVER;}
2389 multiplicative_expression:
2390 unary_expression
2391 | multiplicative_expression MULT_TK unary_expression
2393 $$ = build_binop (BINOP_LOOKUP ($2.token),
2394 $2.location, $1, $3);
2396 | multiplicative_expression DIV_TK unary_expression
2398 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2399 $1, $3);
2401 | multiplicative_expression REM_TK unary_expression
2403 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2404 $1, $3);
2406 | multiplicative_expression MULT_TK error
2407 {yyerror ("Missing term"); RECOVER;}
2408 | multiplicative_expression DIV_TK error
2409 {yyerror ("Missing term"); RECOVER;}
2410 | multiplicative_expression REM_TK error
2411 {yyerror ("Missing term"); RECOVER;}
2414 additive_expression:
2415 multiplicative_expression
2416 | additive_expression PLUS_TK multiplicative_expression
2418 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2419 $1, $3);
2421 | additive_expression MINUS_TK multiplicative_expression
2423 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2424 $1, $3);
2426 | additive_expression PLUS_TK error
2427 {yyerror ("Missing term"); RECOVER;}
2428 | additive_expression MINUS_TK error
2429 {yyerror ("Missing term"); RECOVER;}
2432 shift_expression:
2433 additive_expression
2434 | shift_expression LS_TK additive_expression
2436 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2437 $1, $3);
2439 | shift_expression SRS_TK additive_expression
2441 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2442 $1, $3);
2444 | shift_expression ZRS_TK additive_expression
2446 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2447 $1, $3);
2449 | shift_expression LS_TK error
2450 {yyerror ("Missing term"); RECOVER;}
2451 | shift_expression SRS_TK error
2452 {yyerror ("Missing term"); RECOVER;}
2453 | shift_expression ZRS_TK error
2454 {yyerror ("Missing term"); RECOVER;}
2457 relational_expression:
2458 shift_expression
2459 | relational_expression LT_TK shift_expression
2461 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2462 $1, $3);
2464 | relational_expression GT_TK shift_expression
2466 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2467 $1, $3);
2469 | relational_expression LTE_TK shift_expression
2471 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2472 $1, $3);
2474 | relational_expression GTE_TK shift_expression
2476 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2477 $1, $3);
2479 | relational_expression INSTANCEOF_TK reference_type
2480 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2481 | relational_expression LT_TK error
2482 {yyerror ("Missing term"); RECOVER;}
2483 | relational_expression GT_TK error
2484 {yyerror ("Missing term"); RECOVER;}
2485 | relational_expression LTE_TK error
2486 {yyerror ("Missing term"); RECOVER;}
2487 | relational_expression GTE_TK error
2488 {yyerror ("Missing term"); RECOVER;}
2489 | relational_expression INSTANCEOF_TK error
2490 {yyerror ("Invalid reference type"); RECOVER;}
2493 equality_expression:
2494 relational_expression
2495 | equality_expression EQ_TK relational_expression
2497 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2498 $1, $3);
2500 | equality_expression NEQ_TK relational_expression
2502 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2503 $1, $3);
2505 | equality_expression EQ_TK error
2506 {yyerror ("Missing term"); RECOVER;}
2507 | equality_expression NEQ_TK error
2508 {yyerror ("Missing term"); RECOVER;}
2511 and_expression:
2512 equality_expression
2513 | and_expression AND_TK equality_expression
2515 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2516 $1, $3);
2518 | and_expression AND_TK error
2519 {yyerror ("Missing term"); RECOVER;}
2522 exclusive_or_expression:
2523 and_expression
2524 | exclusive_or_expression XOR_TK and_expression
2526 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2527 $1, $3);
2529 | exclusive_or_expression XOR_TK error
2530 {yyerror ("Missing term"); RECOVER;}
2533 inclusive_or_expression:
2534 exclusive_or_expression
2535 | inclusive_or_expression OR_TK exclusive_or_expression
2537 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2538 $1, $3);
2540 | inclusive_or_expression OR_TK error
2541 {yyerror ("Missing term"); RECOVER;}
2544 conditional_and_expression:
2545 inclusive_or_expression
2546 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2548 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2549 $1, $3);
2551 | conditional_and_expression BOOL_AND_TK error
2552 {yyerror ("Missing term"); RECOVER;}
2555 conditional_or_expression:
2556 conditional_and_expression
2557 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2559 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2560 $1, $3);
2562 | conditional_or_expression BOOL_OR_TK error
2563 {yyerror ("Missing term"); RECOVER;}
2566 conditional_expression: /* Error handling here is weak */
2567 conditional_or_expression
2568 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2570 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2571 EXPR_WFL_LINECOL ($$) = $2.location;
2573 | conditional_or_expression REL_QM_TK REL_CL_TK error
2575 YYERROR_NOW;
2576 yyerror ("Missing term");
2577 DRECOVER (1);
2579 | conditional_or_expression REL_QM_TK error
2580 {yyerror ("Missing term"); DRECOVER (2);}
2581 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2582 {yyerror ("Missing term"); DRECOVER (3);}
2585 assignment_expression:
2586 conditional_expression
2587 | assignment
2590 assignment:
2591 left_hand_side assignment_operator assignment_expression
2592 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2593 | left_hand_side assignment_operator error
2595 YYNOT_TWICE yyerror ("Missing term");
2596 DRECOVER (assign);
2600 left_hand_side:
2601 name
2602 | field_access
2603 | array_access
2606 assignment_operator:
2607 ASSIGN_ANY_TK
2608 | ASSIGN_TK
2611 expression:
2612 assignment_expression
2615 constant_expression:
2616 expression
2621 /* Helper function to retrieve an OSB count. Should be used when the
2622 `dims:' rule is being used. */
2624 static int
2625 pop_current_osb (ctxp)
2626 struct parser_ctxt *ctxp;
2628 int to_return;
2630 if (ctxp->osb_depth < 0)
2631 abort ();
2633 to_return = CURRENT_OSB (ctxp);
2634 ctxp->osb_depth--;
2636 return to_return;
2641 /* This section of the code deal with save/restoring parser contexts.
2642 Add mode documentation here. FIXME */
2644 /* Helper function. Create a new parser context. With
2645 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2646 context is copied, otherwise, the new context is zeroed. The newly
2647 created context becomes the current one. */
2649 static void
2650 create_new_parser_context (copy_from_previous)
2651 int copy_from_previous;
2653 struct parser_ctxt *new;
2655 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2656 if (copy_from_previous)
2658 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2659 new->saved_data_ctx = 1;
2661 else
2662 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2664 new->next = ctxp;
2665 ctxp = new;
2668 /* Create a new parser context and make it the current one. */
2670 void
2671 java_push_parser_context ()
2673 create_new_parser_context (0);
2676 void
2677 java_pop_parser_context (generate)
2678 int generate;
2680 tree current;
2681 struct parser_ctxt *toFree, *next;
2683 if (!ctxp)
2684 return;
2686 toFree = ctxp;
2687 next = ctxp->next;
2688 if (next)
2690 lineno = ctxp->lineno;
2691 current_class = ctxp->class_type;
2694 /* If the old and new lexers differ, then free the old one. */
2695 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2696 java_destroy_lexer (ctxp->lexer);
2698 /* Set the single import class file flag to 0 for the current list
2699 of imported things */
2700 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2701 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2703 /* And restore those of the previous context */
2704 if ((ctxp = next)) /* Assignment is really meant here */
2705 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2706 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2708 /* If we pushed a context to parse a class intended to be generated,
2709 we keep it so we can remember the class. What we could actually
2710 do is to just update a list of class names. */
2711 if (generate)
2713 toFree->next = ctxp_for_generation;
2714 ctxp_for_generation = toFree;
2716 else
2717 free (toFree);
2720 /* Create a parser context for the use of saving some global
2721 variables. */
2723 void
2724 java_parser_context_save_global ()
2726 if (!ctxp)
2728 java_push_parser_context ();
2729 ctxp->saved_data_ctx = 1;
2732 /* If this context already stores data, create a new one suitable
2733 for data storage. */
2734 else if (ctxp->saved_data)
2735 create_new_parser_context (1);
2737 ctxp->lineno = lineno;
2738 ctxp->class_type = current_class;
2739 ctxp->filename = input_filename;
2740 ctxp->function_decl = current_function_decl;
2741 ctxp->saved_data = 1;
2744 /* Restore some global variables from the previous context. Make the
2745 previous context the current one. */
2747 void
2748 java_parser_context_restore_global ()
2750 lineno = ctxp->lineno;
2751 current_class = ctxp->class_type;
2752 input_filename = ctxp->filename;
2753 if (wfl_operator)
2755 tree s;
2756 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2757 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2759 current_function_decl = ctxp->function_decl;
2760 ctxp->saved_data = 0;
2761 if (ctxp->saved_data_ctx)
2762 java_pop_parser_context (0);
2765 /* Suspend vital data for the current class/function being parsed so
2766 that an other class can be parsed. Used to let local/anonymous
2767 classes be parsed. */
2769 static void
2770 java_parser_context_suspend ()
2772 /* This makes debugging through java_debug_context easier */
2773 static const char *name = "<inner buffer context>";
2775 /* Duplicate the previous context, use it to save the globals we're
2776 interested in */
2777 create_new_parser_context (1);
2778 ctxp->function_decl = current_function_decl;
2779 ctxp->class_type = current_class;
2781 /* Then create a new context which inherits all data from the
2782 previous one. This will be the new current context */
2783 create_new_parser_context (1);
2785 /* Help debugging */
2786 ctxp->next->filename = name;
2789 /* Resume vital data for the current class/function being parsed so
2790 that an other class can be parsed. Used to let local/anonymous
2791 classes be parsed. The trick is the data storing file position
2792 informations must be restored to their current value, so parsing
2793 can resume as if no context was ever saved. */
2795 static void
2796 java_parser_context_resume ()
2798 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2799 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2800 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2802 /* We need to inherit the list of classes to complete/generate */
2803 restored->classd_list = old->classd_list;
2804 restored->class_list = old->class_list;
2806 /* Restore the current class and function from the saver */
2807 current_class = saver->class_type;
2808 current_function_decl = saver->function_decl;
2810 /* Retrive the restored context */
2811 ctxp = restored;
2813 /* Re-installed the data for the parsing to carry on */
2814 memcpy (&ctxp->marker_begining, &old->marker_begining,
2815 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2817 /* Buffer context can now be discarded */
2818 free (saver);
2819 free (old);
2822 /* Add a new anchor node to which all statement(s) initializing static
2823 and non static initialized upon declaration field(s) will be
2824 linked. */
2826 static void
2827 java_parser_context_push_initialized_field ()
2829 tree node;
2831 node = build_tree_list (NULL_TREE, NULL_TREE);
2832 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2833 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2835 node = build_tree_list (NULL_TREE, NULL_TREE);
2836 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2837 CPC_INITIALIZER_LIST (ctxp) = node;
2839 node = build_tree_list (NULL_TREE, NULL_TREE);
2840 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2841 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2844 /* Pop the lists of initialized field. If this lists aren't empty,
2845 remember them so we can use it to create and populate the finit$
2846 or <clinit> functions. */
2848 static void
2849 java_parser_context_pop_initialized_field ()
2851 tree stmts;
2852 tree class_type = TREE_TYPE (GET_CPC ());
2854 if (CPC_INITIALIZER_LIST (ctxp))
2856 stmts = CPC_INITIALIZER_STMT (ctxp);
2857 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2858 if (stmts && !java_error_count)
2859 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2862 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2864 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2865 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2866 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2867 /* Keep initialization in order to enforce 8.5 */
2868 if (stmts && !java_error_count)
2869 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2872 /* JDK 1.1 instance initializers */
2873 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2875 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2876 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2877 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2878 if (stmts && !java_error_count)
2879 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2883 static tree
2884 reorder_static_initialized (list)
2885 tree list;
2887 /* We have to keep things in order. The alias initializer have to
2888 come first, then the initialized regular field, in reverse to
2889 keep them in lexical order. */
2890 tree marker, previous = NULL_TREE;
2891 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2892 if (TREE_CODE (marker) == TREE_LIST
2893 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2894 break;
2896 /* No static initialized, the list is fine as is */
2897 if (!previous)
2898 list = TREE_CHAIN (marker);
2900 /* No marker? reverse the whole list */
2901 else if (!marker)
2902 list = nreverse (list);
2904 /* Otherwise, reverse what's after the marker and the new reordered
2905 sublist will replace the marker. */
2906 else
2908 TREE_CHAIN (previous) = NULL_TREE;
2909 list = nreverse (list);
2910 list = chainon (TREE_CHAIN (marker), list);
2912 return list;
2915 /* Helper functions to dump the parser context stack. */
2917 #define TAB_CONTEXT(C) \
2918 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2920 static void
2921 java_debug_context_do (tab)
2922 int tab;
2924 struct parser_ctxt *copy = ctxp;
2925 while (copy)
2927 TAB_CONTEXT (tab);
2928 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2929 TAB_CONTEXT (tab);
2930 fprintf (stderr, "filename: %s\n", copy->filename);
2931 TAB_CONTEXT (tab);
2932 fprintf (stderr, "lineno: %d\n", copy->lineno);
2933 TAB_CONTEXT (tab);
2934 fprintf (stderr, "package: %s\n",
2935 (copy->package ?
2936 IDENTIFIER_POINTER (copy->package) : "<none>"));
2937 TAB_CONTEXT (tab);
2938 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2939 TAB_CONTEXT (tab);
2940 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2941 copy = copy->next;
2942 tab += 2;
2946 /* Dump the stacked up parser contexts. Intended to be called from a
2947 debugger. */
2949 void
2950 java_debug_context ()
2952 java_debug_context_do (0);
2957 /* Flag for the error report routine to issue the error the first time
2958 it's called (overriding the default behavior which is to drop the
2959 first invocation and honor the second one, taking advantage of a
2960 richer context. */
2961 static int force_error = 0;
2963 /* Reporting an constructor invocation error. */
2964 static void
2965 parse_ctor_invocation_error ()
2967 if (DECL_CONSTRUCTOR_P (current_function_decl))
2968 yyerror ("Constructor invocation must be first thing in a constructor");
2969 else
2970 yyerror ("Only constructors can invoke constructors");
2973 /* Reporting JDK1.1 features not implemented. */
2975 static tree
2976 parse_jdk1_1_error (msg)
2977 const char *msg;
2979 sorry (": `%s' JDK1.1(TM) feature", msg);
2980 java_error_count++;
2981 return empty_stmt_node;
2984 static int do_warning = 0;
2986 void
2987 yyerror (msg)
2988 const char *msg;
2990 static java_lc elc;
2991 static int prev_lineno;
2992 static const char *prev_msg;
2994 int save_lineno;
2995 char *remainder, *code_from_source;
2997 if (!force_error && prev_lineno == lineno)
2998 return;
3000 /* Save current error location but report latter, when the context is
3001 richer. */
3002 if (ctxp->java_error_flag == 0)
3004 ctxp->java_error_flag = 1;
3005 elc = ctxp->elc;
3006 /* Do something to use the previous line if we're reaching the
3007 end of the file... */
3008 #ifdef VERBOSE_SKELETON
3009 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
3010 #endif
3011 return;
3014 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3015 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
3016 return;
3018 ctxp->java_error_flag = 0;
3019 if (do_warning)
3020 java_warning_count++;
3021 else
3022 java_error_count++;
3024 if (elc.col == 0 && msg && msg[1] == ';')
3026 elc.col = ctxp->p_line->char_col-1;
3027 elc.line = ctxp->p_line->lineno;
3030 save_lineno = lineno;
3031 prev_lineno = lineno = elc.line;
3032 prev_msg = msg;
3034 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3035 obstack_grow0 (&temporary_obstack,
3036 code_from_source, strlen (code_from_source));
3037 remainder = obstack_finish (&temporary_obstack);
3038 if (do_warning)
3039 warning ("%s.\n%s", msg, remainder);
3040 else
3041 error ("%s.\n%s", msg, remainder);
3043 /* This allow us to cheaply avoid an extra 'Invalid expression
3044 statement' error report when errors have been already reported on
3045 the same line. This occurs when we report an error but don't have
3046 a synchronization point other than ';', which
3047 expression_statement is the only one to take care of. */
3048 ctxp->prevent_ese = lineno = save_lineno;
3051 static void
3052 issue_warning_error_from_context (cl, msg, ap)
3053 tree cl;
3054 const char *msg;
3055 va_list ap;
3057 const char *saved, *saved_input_filename;
3058 char buffer [4096];
3059 vsprintf (buffer, msg, ap);
3060 force_error = 1;
3062 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3063 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3064 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3066 /* We have a CL, that's a good reason for using it if it contains data */
3067 saved = ctxp->filename;
3068 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3069 ctxp->filename = EXPR_WFL_FILENAME (cl);
3070 saved_input_filename = input_filename;
3071 input_filename = ctxp->filename;
3072 java_error (NULL);
3073 java_error (buffer);
3074 ctxp->filename = saved;
3075 input_filename = saved_input_filename;
3076 force_error = 0;
3079 /* Issue an error message at a current source line CL */
3081 void
3082 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3084 #ifndef ANSI_PROTOTYPES
3085 tree cl;
3086 const char *msg;
3087 #endif
3088 va_list ap;
3090 VA_START (ap, msg);
3091 #ifndef ANSI_PROTOTYPES
3092 cl = va_arg (ap, tree);
3093 msg = va_arg (ap, const char *);
3094 #endif
3095 issue_warning_error_from_context (cl, msg, ap);
3096 va_end (ap);
3099 /* Issue a warning at a current source line CL */
3101 static void
3102 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3104 #ifndef ANSI_PROTOTYPES
3105 tree cl;
3106 const char *msg;
3107 #endif
3108 va_list ap;
3110 VA_START (ap, msg);
3111 #ifndef ANSI_PROTOTYPES
3112 cl = va_arg (ap, tree);
3113 msg = va_arg (ap, const char *);
3114 #endif
3116 force_error = do_warning = 1;
3117 issue_warning_error_from_context (cl, msg, ap);
3118 do_warning = force_error = 0;
3119 va_end (ap);
3122 static tree
3123 find_expr_with_wfl (node)
3124 tree node;
3126 while (node)
3128 char code;
3129 tree to_return;
3131 switch (TREE_CODE (node))
3133 case BLOCK:
3134 node = BLOCK_EXPR_BODY (node);
3135 continue;
3137 case COMPOUND_EXPR:
3138 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3139 if (to_return)
3140 return to_return;
3141 node = TREE_OPERAND (node, 1);
3142 continue;
3144 case LOOP_EXPR:
3145 node = TREE_OPERAND (node, 0);
3146 continue;
3148 case LABELED_BLOCK_EXPR:
3149 node = TREE_OPERAND (node, 1);
3150 continue;
3152 default:
3153 code = TREE_CODE_CLASS (TREE_CODE (node));
3154 if (((code == '1') || (code == '2') || (code == 'e'))
3155 && EXPR_WFL_LINECOL (node))
3156 return node;
3157 return NULL_TREE;
3160 return NULL_TREE;
3163 /* Issue a missing return statement error. Uses METHOD to figure the
3164 last line of the method the error occurs in. */
3166 static void
3167 missing_return_error (method)
3168 tree method;
3170 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3171 parse_error_context (wfl_operator, "Missing return statement");
3174 /* Issue an unreachable statement error. From NODE, find the next
3175 statement to report appropriately. */
3176 static void
3177 unreachable_stmt_error (node)
3178 tree node;
3180 /* Browse node to find the next expression node that has a WFL. Use
3181 the location to report the error */
3182 if (TREE_CODE (node) == COMPOUND_EXPR)
3183 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3184 else
3185 node = find_expr_with_wfl (node);
3187 if (node)
3189 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3190 parse_error_context (wfl_operator, "Unreachable statement");
3192 else
3193 abort ();
3197 java_report_errors ()
3199 if (java_error_count)
3200 fprintf (stderr, "%d error%s",
3201 java_error_count, (java_error_count == 1 ? "" : "s"));
3202 if (java_warning_count)
3203 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3204 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3205 if (java_error_count || java_warning_count)
3206 putc ('\n', stderr);
3207 return java_error_count;
3210 static char *
3211 java_accstring_lookup (flags)
3212 int flags;
3214 static char buffer [80];
3215 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3217 /* Access modifier looked-up first for easier report on forbidden
3218 access. */
3219 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3220 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3221 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3222 if (flags & ACC_STATIC) COPY_RETURN ("static");
3223 if (flags & ACC_FINAL) COPY_RETURN ("final");
3224 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3225 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3226 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3227 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3228 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3229 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3231 buffer [0] = '\0';
3232 return buffer;
3233 #undef COPY_RETURN
3236 /* Issuing error messages upon redefinition of classes, interfaces or
3237 variables. */
3239 static void
3240 classitf_redefinition_error (context, id, decl, cl)
3241 const char *context;
3242 tree id, decl, cl;
3244 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3245 context, IDENTIFIER_POINTER (id),
3246 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3247 /* Here we should point out where its redefined. It's a unicode. FIXME */
3250 static void
3251 variable_redefinition_error (context, name, type, line)
3252 tree context, name, type;
3253 int line;
3255 const char *type_name;
3257 /* Figure a proper name for type. We might haven't resolved it */
3258 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3259 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3260 else
3261 type_name = lang_printable_name (type, 0);
3263 parse_error_context (context,
3264 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3265 IDENTIFIER_POINTER (name),
3266 type_name, IDENTIFIER_POINTER (name), line);
3269 static tree
3270 build_array_from_name (type, type_wfl, name, ret_name)
3271 tree type, type_wfl, name, *ret_name;
3273 int more_dims = 0;
3274 const char *string;
3276 /* Eventually get more dims */
3277 string = IDENTIFIER_POINTER (name);
3278 while (string [more_dims] == '[')
3279 more_dims++;
3281 /* If we have, then craft a new type for this variable */
3282 if (more_dims)
3284 tree save = type;
3286 name = get_identifier (&string [more_dims]);
3288 /* If we have a pointer, use its type */
3289 if (TREE_CODE (type) == POINTER_TYPE)
3290 type = TREE_TYPE (type);
3292 /* Building the first dimension of a primitive type uses this
3293 function */
3294 if (JPRIMITIVE_TYPE_P (type))
3296 type = build_java_array_type (type, -1);
3297 more_dims--;
3299 /* Otherwise, if we have a WFL for this type, use it (the type
3300 is already an array on an unresolved type, and we just keep
3301 on adding dimensions) */
3302 else if (type_wfl)
3304 int i = 0;
3305 type = type_wfl;
3306 string = IDENTIFIER_POINTER (TYPE_NAME (save));
3307 while (string[i] == '[')
3308 ++i;
3309 more_dims += i;
3312 /* Add all the dimensions */
3313 while (more_dims--)
3314 type = build_unresolved_array_type (type);
3316 /* The type may have been incomplete in the first place */
3317 if (type_wfl)
3318 type = obtain_incomplete_type (type);
3321 if (ret_name)
3322 *ret_name = name;
3323 return type;
3326 /* Build something that the type identifier resolver will identify as
3327 being an array to an unresolved type. TYPE_WFL is a WFL on a
3328 identifier. */
3330 static tree
3331 build_unresolved_array_type (type_or_wfl)
3332 tree type_or_wfl;
3334 const char *ptr;
3335 tree wfl;
3337 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3338 just create a array type */
3339 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3340 return build_java_array_type (type_or_wfl, -1);
3342 obstack_1grow (&temporary_obstack, '[');
3343 obstack_grow0 (&temporary_obstack,
3344 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3345 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3346 ptr = obstack_finish (&temporary_obstack);
3347 wfl = build_expr_wfl (get_identifier (ptr),
3348 EXPR_WFL_FILENAME (type_or_wfl),
3349 EXPR_WFL_LINENO (type_or_wfl),
3350 EXPR_WFL_COLNO (type_or_wfl));
3351 /* Re-install the existing qualifications so that the type can be
3352 resolved properly. */
3353 EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3354 return wfl;
3357 static void
3358 parser_add_interface (class_decl, interface_decl, wfl)
3359 tree class_decl, interface_decl, wfl;
3361 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3362 parse_error_context (wfl, "Interface `%s' repeated",
3363 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3366 /* Bulk of common class/interface checks. Return 1 if an error was
3367 encountered. TAG is 0 for a class, 1 for an interface. */
3369 static int
3370 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3371 int is_interface, flags;
3372 tree raw_name, qualified_name, decl, cl;
3374 tree node;
3375 int sca = 0; /* Static class allowed */
3376 int icaf = 0; /* Inner class allowed flags */
3377 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3379 if (!quiet_flag)
3380 fprintf (stderr, " %s%s %s",
3381 (CPC_INNER_P () ? "inner" : ""),
3382 (is_interface ? "interface" : "class"),
3383 IDENTIFIER_POINTER (qualified_name));
3385 /* Scope of an interface/class type name:
3386 - Can't be imported by a single type import
3387 - Can't already exists in the package */
3388 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3389 && (node = find_name_in_single_imports (raw_name))
3390 && !CPC_INNER_P ())
3392 parse_error_context
3393 (cl, "%s name `%s' clashes with imported type `%s'",
3394 (is_interface ? "Interface" : "Class"),
3395 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3396 return 1;
3398 if (decl && CLASS_COMPLETE_P (decl))
3400 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3401 qualified_name, decl, cl);
3402 return 1;
3405 if (check_inner_class_redefinition (raw_name, cl))
3406 return 1;
3408 /* If public, file name should match class/interface name, except
3409 when dealing with an inner class */
3410 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3412 const char *f;
3414 /* Contains OS dependent assumption on path separator. FIXME */
3415 for (f = &input_filename [strlen (input_filename)];
3416 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3417 f--)
3419 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3420 f++;
3421 if (strncmp (IDENTIFIER_POINTER (raw_name),
3422 f , IDENTIFIER_LENGTH (raw_name)) ||
3423 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3424 parse_error_context
3425 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3426 (is_interface ? "interface" : "class"),
3427 IDENTIFIER_POINTER (qualified_name),
3428 IDENTIFIER_POINTER (raw_name));
3431 /* Static classes can be declared only in top level classes. Note:
3432 once static, a inner class is a top level class. */
3433 if (flags & ACC_STATIC)
3435 /* Catch the specific error of declaring an class inner class
3436 with no toplevel enclosing class. Prevent check_modifiers from
3437 complaining a second time */
3438 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3440 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3441 IDENTIFIER_POINTER (qualified_name));
3442 sca = ACC_STATIC;
3444 /* Else, in the context of a top-level class declaration, let
3445 `check_modifiers' do its job, otherwise, give it a go */
3446 else
3447 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3450 /* Inner classes can be declared private or protected
3451 within their enclosing classes. */
3452 if (CPC_INNER_P ())
3454 /* A class which is local to a block can't be public, private,
3455 protected or static. But it is created final, so allow this
3456 one. */
3457 if (current_function_decl)
3458 icaf = sca = uaaf = ACC_FINAL;
3459 else
3461 check_modifiers_consistency (flags);
3462 icaf = ACC_PROTECTED;
3463 if (! CLASS_INTERFACE (GET_CPC ()))
3464 icaf |= ACC_PRIVATE;
3468 if (is_interface)
3470 if (CPC_INNER_P ())
3471 uaaf = INTERFACE_INNER_MODIFIERS;
3472 else
3473 uaaf = INTERFACE_MODIFIERS;
3475 check_modifiers ("Illegal modifier `%s' for interface declaration",
3476 flags, uaaf);
3478 else
3479 check_modifiers ((current_function_decl ?
3480 "Illegal modifier `%s' for local class declaration" :
3481 "Illegal modifier `%s' for class declaration"),
3482 flags, uaaf|sca|icaf);
3483 return 0;
3486 static void
3487 make_nested_class_name (cpc_list)
3488 tree cpc_list;
3490 tree name;
3492 if (!cpc_list)
3493 return;
3494 else
3495 make_nested_class_name (TREE_CHAIN (cpc_list));
3497 /* Pick the qualified name when dealing with the first upmost
3498 enclosing class */
3499 name = (TREE_CHAIN (cpc_list) ?
3500 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3501 obstack_grow (&temporary_obstack,
3502 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3503 /* Why is NO_DOLLAR_IN_LABEL defined? */
3504 #if 0
3505 #ifdef NO_DOLLAR_IN_LABEL
3506 internal_error ("Can't use '$' as a separator for inner classes");
3507 #endif
3508 #endif
3509 obstack_1grow (&temporary_obstack, '$');
3512 /* Can't redefine a class already defined in an earlier scope. */
3514 static int
3515 check_inner_class_redefinition (raw_name, cl)
3516 tree raw_name, cl;
3518 tree scope_list;
3520 for (scope_list = GET_CPC_LIST (); scope_list;
3521 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3522 if (raw_name == GET_CPC_UN_NODE (scope_list))
3524 parse_error_context
3525 (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",
3526 IDENTIFIER_POINTER (raw_name));
3527 return 1;
3529 return 0;
3532 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3533 we remember ENCLOSING and SUPER. */
3535 static tree
3536 resolve_inner_class (circularity_hash, cl, enclosing, super, class_type)
3537 struct hash_table *circularity_hash;
3538 tree cl, *enclosing, *super, class_type;
3540 tree local_enclosing = *enclosing;
3541 tree local_super = NULL_TREE;
3543 while (local_enclosing)
3545 tree intermediate, decl;
3547 hash_lookup (circularity_hash,
3548 (const hash_table_key) local_enclosing, TRUE, NULL);
3550 if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3551 return decl;
3553 intermediate = local_enclosing;
3554 /* Explore enclosing contexts. */
3555 while (INNER_CLASS_DECL_P (intermediate))
3557 intermediate = DECL_CONTEXT (intermediate);
3558 if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3559 return decl;
3562 /* Now go to the upper classes, bail out if necessary. We will
3563 analyze the returned SUPER and act accordingly (see
3564 do_resolve_class.) */
3565 local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3566 if (!local_super || local_super == object_type_node)
3567 break;
3569 if (TREE_CODE (local_super) == POINTER_TYPE)
3570 local_super = do_resolve_class (NULL, local_super, NULL, NULL);
3571 else
3572 local_super = TYPE_NAME (local_super);
3574 /* We may not have checked for circular inheritance yet, so do so
3575 here to prevent an infinite loop. */
3576 if (hash_lookup (circularity_hash,
3577 (const hash_table_key) local_super, FALSE, NULL))
3579 if (!cl)
3580 cl = lookup_cl (local_enclosing);
3582 parse_error_context
3583 (cl, "Cyclic inheritance involving %s",
3584 IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3585 local_enclosing = NULL_TREE;
3587 else
3588 local_enclosing = local_super;
3591 /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3592 *super = local_super;
3593 *enclosing = local_enclosing;
3595 return NULL_TREE;
3598 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3599 qualified. */
3601 static tree
3602 find_as_inner_class (enclosing, name, cl)
3603 tree enclosing, name, cl;
3605 tree qual, to_return;
3606 if (!enclosing)
3607 return NULL_TREE;
3609 name = TYPE_NAME (name);
3611 /* First search: within the scope of `enclosing', search for name */
3612 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3613 qual = EXPR_WFL_QUALIFICATION (cl);
3614 else if (cl)
3615 qual = build_tree_list (cl, NULL_TREE);
3616 else
3617 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3619 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3620 return to_return;
3622 /* We're dealing with a qualified name. Try to resolve thing until
3623 we get something that is an enclosing class. */
3624 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3626 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3628 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3629 qual = TREE_CHAIN (qual))
3631 acc = merge_qualified_name (acc,
3632 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3633 BUILD_PTR_FROM_NAME (ptr, acc);
3635 /* Don't try to resolve ACC as a class name if it follows
3636 the current package name. We don't want to pick something
3637 that's accidentally there: for example `a.b.c' in package
3638 `a.b' shouldn't trigger loading `a' if it's there by
3639 itself. */
3640 if (ctxp->package
3641 && strstr (IDENTIFIER_POINTER (ctxp->package),
3642 IDENTIFIER_POINTER (acc)))
3643 decl = NULL;
3644 else
3645 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3648 /* A NULL qual and a decl means that the search ended
3649 successfully?!? We have to do something then. FIXME */
3651 if (decl)
3652 enclosing = decl;
3653 else
3654 qual = EXPR_WFL_QUALIFICATION (cl);
3656 /* Otherwise, create a qual for the other part of the resolution. */
3657 else
3658 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3660 return find_as_inner_class_do (qual, enclosing);
3663 /* We go inside the list of sub classes and try to find a way
3664 through. */
3666 static tree
3667 find_as_inner_class_do (qual, enclosing)
3668 tree qual, enclosing;
3670 if (!qual)
3671 return NULL_TREE;
3673 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3675 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3676 tree next_enclosing = NULL_TREE;
3677 tree inner_list;
3679 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3680 inner_list; inner_list = TREE_CHAIN (inner_list))
3682 if (TREE_VALUE (inner_list) == name_to_match)
3684 next_enclosing = TREE_PURPOSE (inner_list);
3685 break;
3688 enclosing = next_enclosing;
3691 return (!qual && enclosing ? enclosing : NULL_TREE);
3694 /* Reach all inner classes and tie their unqualified name to a
3695 DECL. */
3697 static void
3698 set_nested_class_simple_name_value (outer, set)
3699 tree outer;
3700 int set;
3702 tree l;
3704 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3705 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3706 TREE_PURPOSE (l) : NULL_TREE);
3709 static void
3710 link_nested_class_to_enclosing ()
3712 if (GET_ENCLOSING_CPC ())
3714 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3715 DECL_INNER_CLASS_LIST (enclosing) =
3716 tree_cons (GET_CPC (), GET_CPC_UN (),
3717 DECL_INNER_CLASS_LIST (enclosing));
3718 enclosing = enclosing;
3722 static tree
3723 maybe_make_nested_class_name (name)
3724 tree name;
3726 tree id = NULL_TREE;
3728 if (CPC_INNER_P ())
3730 make_nested_class_name (GET_CPC_LIST ());
3731 obstack_grow0 (&temporary_obstack,
3732 IDENTIFIER_POINTER (name),
3733 IDENTIFIER_LENGTH (name));
3734 id = get_identifier (obstack_finish (&temporary_obstack));
3735 if (ctxp->package)
3736 QUALIFIED_P (id) = 1;
3738 return id;
3741 /* If DECL is NULL, create and push a new DECL, record the current
3742 line CL and do other maintenance things. */
3744 static tree
3745 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3746 tree decl, raw_name, qualified_name, cl;
3748 if (!decl)
3749 decl = push_class (make_class (), qualified_name);
3751 /* Take care of the file and line business */
3752 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3753 /* If we're emiting xrefs, store the line/col number information */
3754 if (flag_emit_xref)
3755 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3756 else
3757 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3758 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3759 CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3760 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3761 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3763 PUSH_CPC (decl, raw_name);
3764 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3766 /* Link the declaration to the already seen ones */
3767 TREE_CHAIN (decl) = ctxp->class_list;
3768 ctxp->class_list = decl;
3770 /* Create a new nodes in the global lists */
3771 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3772 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3774 /* Install a new dependency list element */
3775 create_jdep_list (ctxp);
3777 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3778 IDENTIFIER_POINTER (qualified_name)));
3779 return decl;
3782 static void
3783 add_superinterfaces (decl, interface_list)
3784 tree decl, interface_list;
3786 tree node;
3787 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3788 takes care of ensuring that:
3789 - This is an accessible interface type,
3790 - Circularity detection.
3791 parser_add_interface is then called. If present but not defined,
3792 the check operation is delayed until the super interface gets
3793 defined. */
3794 for (node = interface_list; node; node = TREE_CHAIN (node))
3796 tree current = TREE_PURPOSE (node);
3797 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3798 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3800 if (!parser_check_super_interface (idecl, decl, current))
3801 parser_add_interface (decl, idecl, current);
3803 else
3804 register_incomplete_type (JDEP_INTERFACE,
3805 current, decl, NULL_TREE);
3809 /* Create an interface in pass1 and return its decl. Return the
3810 interface's decl in pass 2. */
3812 static tree
3813 create_interface (flags, id, super)
3814 int flags;
3815 tree id, super;
3817 tree raw_name = EXPR_WFL_NODE (id);
3818 tree q_name = parser_qualified_classname (raw_name);
3819 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3821 /* Certain syntax errors are making SUPER be like ID. Avoid this
3822 case. */
3823 if (ctxp->class_err && id == super)
3824 super = NULL;
3826 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3828 /* Basic checks: scope, redefinition, modifiers */
3829 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3831 PUSH_ERROR ();
3832 return NULL_TREE;
3835 /* Suspend the current parsing context if we're parsing an inner
3836 interface */
3837 if (CPC_INNER_P ())
3839 java_parser_context_suspend ();
3840 /* Interface members are public. */
3841 if (CLASS_INTERFACE (GET_CPC ()))
3842 flags |= ACC_PUBLIC;
3845 /* Push a new context for (static) initialized upon declaration fields */
3846 java_parser_context_push_initialized_field ();
3848 /* Interface modifiers check
3849 - public/abstract allowed (already done at that point)
3850 - abstract is obsolete (comes first, it's a warning, or should be)
3851 - Can't use twice the same (checked in the modifier rule) */
3852 if ((flags & ACC_ABSTRACT) && flag_redundant)
3853 parse_warning_context
3854 (MODIFIER_WFL (ABSTRACT_TK),
3855 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3857 /* Create a new decl if DECL is NULL, otherwise fix it */
3858 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3860 /* Set super info and mark the class a complete */
3861 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3862 object_type_node, ctxp->interface_number);
3863 ctxp->interface_number = 0;
3864 CLASS_COMPLETE_P (decl) = 1;
3865 add_superinterfaces (decl, super);
3867 return decl;
3870 /* Anonymous class counter. Will be reset to 1 every time a non
3871 anonymous class gets created. */
3872 static int anonymous_class_counter = 1;
3874 /* Patch anonymous class CLASS, by either extending or implementing
3875 DEP. */
3877 static void
3878 patch_anonymous_class (type_decl, class_decl, wfl)
3879 tree type_decl, class_decl, wfl;
3881 tree class = TREE_TYPE (class_decl);
3882 tree type = TREE_TYPE (type_decl);
3883 tree binfo = TYPE_BINFO (class);
3885 /* If it's an interface, implement it */
3886 if (CLASS_INTERFACE (type_decl))
3888 tree s_binfo;
3889 int length;
3891 if (parser_check_super_interface (type_decl, class_decl, wfl))
3892 return;
3894 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3895 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3896 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3897 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3898 /* And add the interface */
3899 parser_add_interface (class_decl, type_decl, wfl);
3901 /* Otherwise, it's a type we want to extend */
3902 else
3904 if (parser_check_super (type_decl, class_decl, wfl))
3905 return;
3906 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3910 static tree
3911 create_anonymous_class (location, type_name)
3912 int location;
3913 tree type_name;
3915 char buffer [80];
3916 tree super = NULL_TREE, itf = NULL_TREE;
3917 tree id, type_decl, class;
3919 /* The unqualified name of the anonymous class. It's just a number. */
3920 sprintf (buffer, "%d", anonymous_class_counter++);
3921 id = build_wfl_node (get_identifier (buffer));
3922 EXPR_WFL_LINECOL (id) = location;
3924 /* We know about the type to extend/implement. We go ahead */
3925 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3927 /* Create a class which either implements on extends the designated
3928 class. The class bears an innacessible name. */
3929 if (CLASS_INTERFACE (type_decl))
3931 /* It's OK to modify it here. It's been already used and
3932 shouldn't be reused */
3933 ctxp->interface_number = 1;
3934 /* Interfaces should presented as a list of WFLs */
3935 itf = build_tree_list (type_name, NULL_TREE);
3937 else
3938 super = type_name;
3941 class = create_class (ACC_FINAL, id, super, itf);
3943 /* We didn't know anything about the stuff. We register a dependence. */
3944 if (!type_decl)
3945 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3947 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3948 return class;
3951 /* Create a class in pass1 and return its decl. Return class
3952 interface's decl in pass 2. */
3954 static tree
3955 create_class (flags, id, super, interfaces)
3956 int flags;
3957 tree id, super, interfaces;
3959 tree raw_name = EXPR_WFL_NODE (id);
3960 tree class_id, decl;
3961 tree super_decl_type;
3963 /* Certain syntax errors are making SUPER be like ID. Avoid this
3964 case. */
3965 if (ctxp->class_err && id == super)
3966 super = NULL;
3968 class_id = parser_qualified_classname (raw_name);
3969 decl = IDENTIFIER_CLASS_VALUE (class_id);
3970 EXPR_WFL_NODE (id) = class_id;
3972 /* Basic check: scope, redefinition, modifiers */
3973 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3975 PUSH_ERROR ();
3976 return NULL_TREE;
3979 /* Suspend the current parsing context if we're parsing an inner
3980 class or an anonymous class. */
3981 if (CPC_INNER_P ())
3983 java_parser_context_suspend ();
3984 /* Interface members are public. */
3985 if (CLASS_INTERFACE (GET_CPC ()))
3986 flags |= ACC_PUBLIC;
3989 /* Push a new context for (static) initialized upon declaration fields */
3990 java_parser_context_push_initialized_field ();
3992 /* Class modifier check:
3993 - Allowed modifier (already done at that point)
3994 - abstract AND final forbidden
3995 - Public classes defined in the correct file */
3996 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3997 parse_error_context
3998 (id, "Class `%s' can't be declared both abstract and final",
3999 IDENTIFIER_POINTER (raw_name));
4001 /* Create a new decl if DECL is NULL, otherwise fix it */
4002 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4004 /* If SUPER exists, use it, otherwise use Object */
4005 if (super)
4007 /* Can't extend java.lang.Object */
4008 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4010 parse_error_context (id, "Can't extend `java.lang.Object'");
4011 return NULL_TREE;
4014 super_decl_type =
4015 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4017 else if (TREE_TYPE (decl) != object_type_node)
4018 super_decl_type = object_type_node;
4019 /* We're defining java.lang.Object */
4020 else
4021 super_decl_type = NULL_TREE;
4023 /* A class nested in an interface is implicitly static. */
4024 if (INNER_CLASS_DECL_P (decl)
4025 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4027 flags |= ACC_STATIC;
4030 /* Set super info and mark the class as complete. */
4031 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4032 ctxp->interface_number);
4033 ctxp->interface_number = 0;
4034 CLASS_COMPLETE_P (decl) = 1;
4035 add_superinterfaces (decl, interfaces);
4037 /* Add the private this$<n> field, Replicate final locals still in
4038 scope as private final fields mangled like val$<local_name>.
4039 This doesn't not occur for top level (static) inner classes. */
4040 if (PURE_INNER_CLASS_DECL_P (decl))
4041 add_inner_class_fields (decl, current_function_decl);
4043 /* If doing xref, store the location at which the inherited class
4044 (if any) was seen. */
4045 if (flag_emit_xref && super)
4046 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
4048 /* Eventually sets the @deprecated tag flag */
4049 CHECK_DEPRECATED (decl);
4051 /* Reset the anonymous class counter when declaring non inner classes */
4052 if (!INNER_CLASS_DECL_P (decl))
4053 anonymous_class_counter = 1;
4055 return decl;
4058 /* End a class declaration: register the statements used to create
4059 finit$ and <clinit>, pop the current class and resume the prior
4060 parser context if necessary. */
4062 static void
4063 end_class_declaration (resume)
4064 int resume;
4066 /* If an error occured, context weren't pushed and won't need to be
4067 popped by a resume. */
4068 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
4070 java_parser_context_pop_initialized_field ();
4071 POP_CPC ();
4072 if (resume && no_error_occured)
4073 java_parser_context_resume ();
4075 /* We're ending a class declaration, this is a good time to reset
4076 the interface cout. Note that might have been already done in
4077 create_interface, but if at that time an inner class was being
4078 dealt with, the interface count was reset in a context created
4079 for the sake of handling inner classes declaration. */
4080 ctxp->interface_number = 0;
4083 static void
4084 add_inner_class_fields (class_decl, fct_decl)
4085 tree class_decl;
4086 tree fct_decl;
4088 tree block, marker, f;
4090 f = add_field (TREE_TYPE (class_decl),
4091 build_current_thisn (TREE_TYPE (class_decl)),
4092 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4093 ACC_PRIVATE);
4094 FIELD_THISN (f) = 1;
4096 if (!fct_decl)
4097 return;
4099 for (block = GET_CURRENT_BLOCK (fct_decl);
4100 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4102 tree decl;
4103 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4105 tree name, pname;
4106 tree wfl, init, list;
4108 /* Avoid non final arguments. */
4109 if (!LOCAL_FINAL_P (decl))
4110 continue;
4112 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4113 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4114 wfl = build_wfl_node (name);
4115 init = build_wfl_node (pname);
4116 /* Build an initialization for the field: it will be
4117 initialized by a parameter added to finit$, bearing a
4118 mangled name of the field itself (param$<n>.) The
4119 parameter is provided to finit$ by the constructor
4120 invoking it (hence the constructor will also feature a
4121 hidden parameter, set to the value of the outer context
4122 local at the time the inner class is created.)
4124 Note: we take into account all possible locals that can
4125 be accessed by the inner class. It's actually not trivial
4126 to minimize these aliases down to the ones really
4127 used. One way to do that would be to expand all regular
4128 methods first, then finit$ to get a picture of what's
4129 used. It works with the exception that we would have to
4130 go back on all constructor invoked in regular methods to
4131 have their invokation reworked (to include the right amount
4132 of alias initializer parameters.)
4134 The only real way around, I think, is a first pass to
4135 identify locals really used in the inner class. We leave
4136 the flag FIELD_LOCAL_ALIAS_USED around for that future
4137 use.
4139 On the other hand, it only affect local inner classes,
4140 whose constructors (and finit$ call) will be featuring
4141 unecessary arguments. It's easy for a developper to keep
4142 this number of parameter down by using the `final'
4143 keyword only when necessary. For the time being, we can
4144 issue a warning on unecessary finals. FIXME */
4145 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4146 wfl, init);
4148 /* Register the field. The TREE_LIST holding the part
4149 initialized/initializer will be marked ARG_FINAL_P so
4150 that the created field can be marked
4151 FIELD_LOCAL_ALIAS. */
4152 list = build_tree_list (wfl, init);
4153 ARG_FINAL_P (list) = 1;
4154 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4158 if (!CPC_INITIALIZER_STMT (ctxp))
4159 return;
4161 /* If we ever registered an alias field, insert and marker to
4162 remeber where the list ends. The second part of the list (the one
4163 featuring initialized fields) so it can be later reversed to
4164 enforce 8.5. The marker will be removed during that operation. */
4165 marker = build_tree_list (NULL_TREE, NULL_TREE);
4166 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4167 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4170 /* Can't use lookup_field () since we don't want to load the class and
4171 can't set the CLASS_LOADED_P flag */
4173 static tree
4174 find_field (class, name)
4175 tree class;
4176 tree name;
4178 tree decl;
4179 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4181 if (DECL_NAME (decl) == name)
4182 return decl;
4184 return NULL_TREE;
4187 /* Wrap around lookup_field that doesn't potentially upset the value
4188 of CLASS */
4190 static tree
4191 lookup_field_wrapper (class, name)
4192 tree class, name;
4194 tree type = class;
4195 tree decl = NULL_TREE;
4196 java_parser_context_save_global ();
4198 /* Last chance: if we're within the context of an inner class, we
4199 might be trying to access a local variable defined in an outer
4200 context. We try to look for it now. */
4201 if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4203 tree new_name;
4204 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4205 decl = lookup_field (&type, new_name);
4206 if (decl && decl != error_mark_node)
4207 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4209 if (!decl || decl == error_mark_node)
4211 type = class;
4212 decl = lookup_field (&type, name);
4215 java_parser_context_restore_global ();
4216 return decl == error_mark_node ? NULL : decl;
4219 /* Find duplicate field within the same class declarations and report
4220 the error. Returns 1 if a duplicated field was found, 0
4221 otherwise. */
4223 static int
4224 duplicate_declaration_error_p (new_field_name, new_type, cl)
4225 tree new_field_name, new_type, cl;
4227 /* This might be modified to work with method decl as well */
4228 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4229 if (decl)
4231 char *t1 = xstrdup (purify_type_name
4232 ((TREE_CODE (new_type) == POINTER_TYPE
4233 && TREE_TYPE (new_type) == NULL_TREE) ?
4234 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4235 lang_printable_name (new_type, 1)));
4236 /* The type may not have been completed by the time we report
4237 the error */
4238 char *t2 = xstrdup (purify_type_name
4239 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4240 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4241 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4242 lang_printable_name (TREE_TYPE (decl), 1)));
4243 parse_error_context
4244 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4245 t1, IDENTIFIER_POINTER (new_field_name),
4246 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4247 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4248 free (t1);
4249 free (t2);
4250 return 1;
4252 return 0;
4255 /* Field registration routine. If TYPE doesn't exist, field
4256 declarations are linked to the undefined TYPE dependency list, to
4257 be later resolved in java_complete_class () */
4259 static void
4260 register_fields (flags, type, variable_list)
4261 int flags;
4262 tree type, variable_list;
4264 tree current, saved_type;
4265 tree class_type = NULL_TREE;
4266 int saved_lineno = lineno;
4267 int must_chain = 0;
4268 tree wfl = NULL_TREE;
4270 if (GET_CPC ())
4271 class_type = TREE_TYPE (GET_CPC ());
4273 if (!class_type || class_type == error_mark_node)
4274 return;
4276 /* If we're adding fields to interfaces, those fields are public,
4277 static, final */
4278 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4280 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4281 flags, ACC_PUBLIC, "interface field(s)");
4282 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4283 flags, ACC_STATIC, "interface field(s)");
4284 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4285 flags, ACC_FINAL, "interface field(s)");
4286 check_modifiers ("Illegal interface member modifier `%s'", flags,
4287 INTERFACE_FIELD_MODIFIERS);
4288 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4291 /* Obtain a suitable type for resolution, if necessary */
4292 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4294 /* If TYPE is fully resolved and we don't have a reference, make one */
4295 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4297 for (current = variable_list, saved_type = type; current;
4298 current = TREE_CHAIN (current), type = saved_type)
4300 tree real_type;
4301 tree field_decl;
4302 tree cl = TREE_PURPOSE (current);
4303 tree init = TREE_VALUE (current);
4304 tree current_name = EXPR_WFL_NODE (cl);
4306 /* Can't declare non-final static fields in inner classes */
4307 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4308 && !(flags & ACC_FINAL))
4309 parse_error_context
4310 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4311 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4312 lang_printable_name (class_type, 0));
4314 /* Process NAME, as it may specify extra dimension(s) for it */
4315 type = build_array_from_name (type, wfl, current_name, &current_name);
4317 /* Type adjustment. We may have just readjusted TYPE because
4318 the variable specified more dimensions. Make sure we have
4319 a reference if we can and don't have one already. Also
4320 change the name if we have an init. */
4321 if (type != saved_type)
4323 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4324 if (init)
4325 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4328 real_type = GET_REAL_TYPE (type);
4329 /* Check for redeclarations */
4330 if (duplicate_declaration_error_p (current_name, real_type, cl))
4331 continue;
4333 /* Set lineno to the line the field was found and create a
4334 declaration for it. Eventually sets the @deprecated tag flag. */
4335 if (flag_emit_xref)
4336 lineno = EXPR_WFL_LINECOL (cl);
4337 else
4338 lineno = EXPR_WFL_LINENO (cl);
4339 field_decl = add_field (class_type, current_name, real_type, flags);
4340 CHECK_DEPRECATED (field_decl);
4342 /* If the field denotes a final instance variable, then we
4343 allocate a LANG_DECL_SPECIFIC part to keep track of its
4344 initialization. We also mark whether the field was
4345 initialized upon it's declaration. We don't do that if the
4346 created field is an alias to a final local. */
4347 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4349 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4350 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4351 if ((flags & ACC_STATIC) && init)
4352 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4355 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4356 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4357 hide parameters to this inner class finit$ and
4358 constructors. It also means that the field isn't final per
4359 say. */
4360 if (ARG_FINAL_P (current))
4362 FIELD_LOCAL_ALIAS (field_decl) = 1;
4363 FIELD_FINAL (field_decl) = 0;
4366 /* Check if we must chain. */
4367 if (must_chain)
4368 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4370 /* If we have an initialization value tied to the field */
4371 if (init)
4373 /* The field is declared static */
4374 if (flags & ACC_STATIC)
4376 /* We include the field and its initialization part into
4377 a list used to generate <clinit>. After <clinit> is
4378 walked, field initializations will be processed and
4379 fields initialized with known constants will be taken
4380 out of <clinit> and have their DECL_INITIAL set
4381 appropriately. */
4382 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4383 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4384 if (TREE_OPERAND (init, 1)
4385 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4386 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4388 /* A non-static field declared with an immediate initialization is
4389 to be initialized in <init>, if any. This field is remembered
4390 to be processed at the time of the generation of <init>. */
4391 else
4393 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4394 SET_CPC_INITIALIZER_STMT (ctxp, init);
4396 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4397 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4400 lineno = saved_lineno;
4403 /* Generate finit$, using the list of initialized fields to populate
4404 its body. finit$'s parameter(s) list is adjusted to include the
4405 one(s) used to initialized the field(s) caching outer context
4406 local(s). */
4408 static tree
4409 generate_finit (class_type)
4410 tree class_type;
4412 int count = 0;
4413 tree list = TYPE_FINIT_STMT_LIST (class_type);
4414 tree mdecl, current, parms;
4416 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4417 class_type, NULL_TREE,
4418 &count);
4419 CRAFTED_PARAM_LIST_FIXUP (parms);
4420 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4421 finit_identifier_node, parms);
4422 fix_method_argument_names (parms, mdecl);
4423 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4424 mdecl, NULL_TREE);
4425 DECL_FUNCTION_NAP (mdecl) = count;
4426 start_artificial_method_body (mdecl);
4428 for (current = list; current; current = TREE_CHAIN (current))
4429 java_method_add_stmt (mdecl,
4430 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4431 current));
4432 end_artificial_method_body (mdecl);
4433 return mdecl;
4436 /* Generate a function to run the instance initialization code. The
4437 private method is called `instinit$'. Unless we're dealing with an
4438 anonymous class, we determine whether all ctors of CLASS_TYPE
4439 declare a checked exception in their `throws' clause in order to
4440 see whether it's necessary to encapsulate the instance initializer
4441 statements in a try/catch/rethrow sequence. */
4443 static tree
4444 generate_instinit (class_type)
4445 tree class_type;
4447 tree current;
4448 tree compound = NULL_TREE;
4449 tree parms = tree_cons (this_identifier_node,
4450 build_pointer_type (class_type), end_params_node);
4451 tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4452 void_type_node,
4453 instinit_identifier_node, parms);
4455 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4456 mdecl, NULL_TREE);
4458 /* Gather all the statements in a compound */
4459 for (current = TYPE_II_STMT_LIST (class_type);
4460 current; current = TREE_CHAIN (current))
4461 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4463 /* We need to encapsulate COMPOUND by a try/catch statement to
4464 rethrow exceptions that might occur in the instance initializer.
4465 We do that only if all ctors of CLASS_TYPE are set to catch a
4466 checked exception. This doesn't apply to anonymous classes (since
4467 they don't have declared ctors.) */
4468 if (!ANONYMOUS_CLASS_P (class_type) &&
4469 ctors_unchecked_throws_clause_p (class_type))
4471 compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4472 build1 (THROW_EXPR, NULL_TREE,
4473 build_wfl_node (wpv_id)));
4474 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4475 exception_type_node);
4478 start_artificial_method_body (mdecl);
4479 java_method_add_stmt (mdecl, compound);
4480 end_artificial_method_body (mdecl);
4482 return mdecl;
4485 /* FIXME */
4486 static tree
4487 build_instinit_invocation (class_type)
4488 tree class_type;
4490 tree to_return = NULL_TREE;
4492 if (TYPE_II_STMT_LIST (class_type))
4494 tree parm = build_tree_list (NULL_TREE,
4495 build_wfl_node (this_identifier_node));
4496 to_return =
4497 build_method_invocation (build_wfl_node (instinit_identifier_node),
4498 parm);
4500 return to_return;
4503 /* Shared accros method_declarator and method_header to remember the
4504 patch stage that was reached during the declaration of the method.
4505 A method DECL is built differently is there is no patch
4506 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4507 pending on the currently defined method. */
4509 static int patch_stage;
4511 /* Check the method declaration and add the method to its current
4512 class. If the argument list is known to contain incomplete types,
4513 the method is partially added and the registration will be resume
4514 once the method arguments resolved. If TYPE is NULL, we're dealing
4515 with a constructor. */
4517 static tree
4518 method_header (flags, type, mdecl, throws)
4519 int flags;
4520 tree type, mdecl, throws;
4522 tree type_wfl = NULL_TREE;
4523 tree meth_name = NULL_TREE;
4524 tree current, orig_arg, this_class = NULL;
4525 tree id, meth;
4526 int saved_lineno;
4527 int constructor_ok = 0, must_chain;
4528 int count;
4530 if (mdecl == error_mark_node)
4531 return error_mark_node;
4532 meth = TREE_VALUE (mdecl);
4533 id = TREE_PURPOSE (mdecl);
4535 check_modifiers_consistency (flags);
4537 if (GET_CPC ())
4538 this_class = TREE_TYPE (GET_CPC ());
4540 if (!this_class || this_class == error_mark_node)
4541 return NULL_TREE;
4543 /* There are some forbidden modifiers for an abstract method and its
4544 class must be abstract as well. */
4545 if (type && (flags & ACC_ABSTRACT))
4547 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4548 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4549 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4550 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4551 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4552 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4553 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4554 parse_error_context
4555 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4556 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4557 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4560 /* Things to be checked when declaring a constructor */
4561 if (!type)
4563 int ec = java_error_count;
4564 /* 8.6: Constructor declarations: we might be trying to define a
4565 method without specifying a return type. */
4566 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4567 parse_error_context
4568 (id, "Invalid method declaration, return type required");
4569 /* 8.6.3: Constructor modifiers */
4570 else
4572 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4573 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4574 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4575 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4576 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4578 /* If we found error here, we don't consider it's OK to tread
4579 the method definition as a constructor, for the rest of this
4580 function */
4581 if (ec == java_error_count)
4582 constructor_ok = 1;
4585 /* Method declared within the scope of an interface are implicitly
4586 abstract and public. Conflicts with other erroneously provided
4587 modifiers are checked right after. */
4589 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4591 /* If FLAGS isn't set because of a modifier, turn the
4592 corresponding modifier WFL to NULL so we issue a warning on
4593 the obsolete use of the modifier */
4594 if (!(flags & ACC_PUBLIC))
4595 MODIFIER_WFL (PUBLIC_TK) = NULL;
4596 if (!(flags & ACC_ABSTRACT))
4597 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4598 flags |= ACC_PUBLIC;
4599 flags |= ACC_ABSTRACT;
4602 /* Inner class can't declare static methods */
4603 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4605 parse_error_context
4606 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4607 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4608 lang_printable_name (this_class, 0));
4611 /* Modifiers context reset moved up, so abstract method declaration
4612 modifiers can be later checked. */
4614 /* Set constructor returned type to void and method name to <init>,
4615 unless we found an error identifier the constructor (in which
4616 case we retain the original name) */
4617 if (!type)
4619 type = void_type_node;
4620 if (constructor_ok)
4621 meth_name = init_identifier_node;
4623 else
4624 meth_name = EXPR_WFL_NODE (id);
4626 /* Do the returned type resolution and registration if necessary */
4627 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4629 if (meth_name)
4630 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4631 EXPR_WFL_NODE (id) = meth_name;
4632 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4634 if (must_chain)
4636 patch_stage = JDEP_METHOD_RETURN;
4637 register_incomplete_type (patch_stage, type_wfl, id, type);
4638 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4640 else
4641 TREE_TYPE (meth) = type;
4643 saved_lineno = lineno;
4644 /* When defining an abstract or interface method, the curly
4645 bracket at level 1 doesn't exist because there is no function
4646 body */
4647 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4648 EXPR_WFL_LINENO (id));
4650 /* Remember the original argument list */
4651 orig_arg = TYPE_ARG_TYPES (meth);
4653 if (patch_stage) /* includes ret type and/or all args */
4655 jdep *jdep;
4656 meth = add_method_1 (this_class, flags, meth_name, meth);
4657 /* Patch for the return type */
4658 if (patch_stage == JDEP_METHOD_RETURN)
4660 jdep = CLASSD_LAST (ctxp->classd_list);
4661 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4663 /* This is the stop JDEP. METH allows the function's signature
4664 to be computed. */
4665 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4667 else
4668 meth = add_method (this_class, flags, meth_name,
4669 build_java_signature (meth));
4671 /* Remember final parameters */
4672 MARK_FINAL_PARMS (meth, orig_arg);
4674 /* Fix the method argument list so we have the argument name
4675 information */
4676 fix_method_argument_names (orig_arg, meth);
4678 /* Register the parameter number and re-install the current line
4679 number */
4680 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4681 lineno = saved_lineno;
4683 /* Register exception specified by the `throws' keyword for
4684 resolution and set the method decl appropriate field to the list.
4685 Note: the grammar ensures that what we get here are class
4686 types. */
4687 if (throws)
4689 throws = nreverse (throws);
4690 for (current = throws; current; current = TREE_CHAIN (current))
4692 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4693 NULL_TREE, NULL_TREE);
4694 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4695 &TREE_VALUE (current);
4697 DECL_FUNCTION_THROWS (meth) = throws;
4700 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4701 DECL_FUNCTION_WFL (meth) = id;
4703 /* Set the flag if we correctly processed a constructor */
4704 if (constructor_ok)
4706 DECL_CONSTRUCTOR_P (meth) = 1;
4707 /* Compute and store the number of artificial parameters declared
4708 for this constructor */
4709 for (count = 0, current = TYPE_FIELDS (this_class); current;
4710 current = TREE_CHAIN (current))
4711 if (FIELD_LOCAL_ALIAS (current))
4712 count++;
4713 DECL_FUNCTION_NAP (meth) = count;
4716 /* Eventually set the @deprecated tag flag */
4717 CHECK_DEPRECATED (meth);
4719 /* If doing xref, store column and line number information instead
4720 of the line number only. */
4721 if (flag_emit_xref)
4722 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4724 return meth;
4727 static void
4728 fix_method_argument_names (orig_arg, meth)
4729 tree orig_arg, meth;
4731 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4732 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4734 TREE_PURPOSE (arg) = this_identifier_node;
4735 arg = TREE_CHAIN (arg);
4737 while (orig_arg != end_params_node)
4739 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4740 orig_arg = TREE_CHAIN (orig_arg);
4741 arg = TREE_CHAIN (arg);
4745 /* Complete the method declaration with METHOD_BODY. */
4747 static void
4748 finish_method_declaration (method_body)
4749 tree method_body;
4751 int flags;
4753 if (!current_function_decl)
4754 return;
4756 flags = get_access_flags_from_decl (current_function_decl);
4758 /* 8.4.5 Method Body */
4759 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4761 tree name = DECL_NAME (current_function_decl);
4762 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4763 "%s method `%s' can't have a body defined",
4764 (METHOD_NATIVE (current_function_decl) ?
4765 "Native" : "Abstract"),
4766 IDENTIFIER_POINTER (name));
4767 method_body = NULL_TREE;
4769 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4771 tree name = DECL_NAME (current_function_decl);
4772 parse_error_context
4773 (DECL_FUNCTION_WFL (current_function_decl),
4774 "Non native and non abstract method `%s' must have a body defined",
4775 IDENTIFIER_POINTER (name));
4776 method_body = NULL_TREE;
4779 if (flag_emit_class_files && method_body
4780 && TREE_CODE (method_body) == NOP_EXPR
4781 && TREE_TYPE (current_function_decl)
4782 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4783 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4785 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4786 maybe_absorb_scoping_blocks ();
4787 /* Exit function's body */
4788 exit_block ();
4789 /* Merge last line of the function with first line, directly in the
4790 function decl. It will be used to emit correct debug info. */
4791 if (!flag_emit_xref)
4792 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4794 /* Since function's argument's list are shared, reset the
4795 ARG_FINAL_P parameter that might have been set on some of this
4796 function parameters. */
4797 UNMARK_FINAL_PARMS (current_function_decl);
4799 /* So we don't have an irrelevant function declaration context for
4800 the next static block we'll see. */
4801 current_function_decl = NULL_TREE;
4804 /* Build a an error message for constructor circularity errors. */
4806 static char *
4807 constructor_circularity_msg (from, to)
4808 tree from, to;
4810 static char string [4096];
4811 char *t = xstrdup (lang_printable_name (from, 0));
4812 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4813 free (t);
4814 return string;
4817 /* Verify a circular call to METH. Return 1 if an error is found, 0
4818 otherwise. */
4820 static int
4821 verify_constructor_circularity (meth, current)
4822 tree meth, current;
4824 static tree list = NULL_TREE;
4825 static int initialized_p;
4826 tree c;
4828 /* If we haven't already registered LIST with the garbage collector,
4829 do so now. */
4830 if (!initialized_p)
4832 ggc_add_tree_root (&list, 1);
4833 initialized_p = 1;
4836 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4838 if (TREE_VALUE (c) == meth)
4840 char *t;
4841 if (list)
4843 tree liste;
4844 list = nreverse (list);
4845 for (liste = list; liste; liste = TREE_CHAIN (liste))
4847 parse_error_context
4848 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4849 constructor_circularity_msg
4850 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4851 java_error_count--;
4854 t = xstrdup (lang_printable_name (meth, 0));
4855 parse_error_context (TREE_PURPOSE (c),
4856 "%s: recursive invocation of constructor `%s'",
4857 constructor_circularity_msg (current, meth), t);
4858 free (t);
4859 list = NULL_TREE;
4860 return 1;
4863 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4865 list = tree_cons (c, current, list);
4866 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4867 return 1;
4868 list = TREE_CHAIN (list);
4870 return 0;
4873 /* Check modifiers that can be declared but exclusively */
4875 static void
4876 check_modifiers_consistency (flags)
4877 int flags;
4879 int acc_count = 0;
4880 tree cl = NULL_TREE;
4882 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4883 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4884 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4885 if (acc_count > 1)
4886 parse_error_context
4887 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4889 acc_count = 0;
4890 cl = NULL_TREE;
4891 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4892 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4893 if (acc_count > 1)
4894 parse_error_context (cl,
4895 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4898 /* Check the methode header METH for abstract specifics features */
4900 static void
4901 check_abstract_method_header (meth)
4902 tree meth;
4904 int flags = get_access_flags_from_decl (meth);
4906 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4907 ACC_ABSTRACT, "abstract method",
4908 IDENTIFIER_POINTER (DECL_NAME (meth)));
4909 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4910 ACC_PUBLIC, "abstract method",
4911 IDENTIFIER_POINTER (DECL_NAME (meth)));
4913 check_modifiers ("Illegal modifier `%s' for interface method",
4914 flags, INTERFACE_METHOD_MODIFIERS);
4917 /* Create a FUNCTION_TYPE node and start augmenting it with the
4918 declared function arguments. Arguments type that can't be resolved
4919 are left as they are, but the returned node is marked as containing
4920 incomplete types. */
4922 static tree
4923 method_declarator (id, list)
4924 tree id, list;
4926 tree arg_types = NULL_TREE, current, node;
4927 tree meth = make_node (FUNCTION_TYPE);
4928 jdep *jdep;
4930 patch_stage = JDEP_NO_PATCH;
4932 if (GET_CPC () == error_mark_node)
4933 return error_mark_node;
4935 /* If we're dealing with an inner class constructor, we hide the
4936 this$<n> decl in the name field of its parameter declaration. We
4937 also might have to hide the outer context local alias
4938 initializers. Not done when the class is a toplevel class. */
4939 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4940 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4942 tree aliases_list, type, thisn;
4943 /* First the aliases, linked to the regular parameters */
4944 aliases_list =
4945 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4946 TREE_TYPE (GET_CPC ()),
4947 NULL_TREE, NULL);
4948 list = chainon (nreverse (aliases_list), list);
4950 /* Then this$<n> */
4951 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4952 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4953 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4954 list);
4957 for (current = list; current; current = TREE_CHAIN (current))
4959 int must_chain = 0;
4960 tree wfl_name = TREE_PURPOSE (current);
4961 tree type = TREE_VALUE (current);
4962 tree name = EXPR_WFL_NODE (wfl_name);
4963 tree already, arg_node;
4964 tree type_wfl = NULL_TREE;
4965 tree real_type;
4967 /* Obtain a suitable type for resolution, if necessary */
4968 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4970 /* Process NAME, as it may specify extra dimension(s) for it */
4971 type = build_array_from_name (type, type_wfl, name, &name);
4972 EXPR_WFL_NODE (wfl_name) = name;
4974 real_type = GET_REAL_TYPE (type);
4975 if (TREE_CODE (real_type) == RECORD_TYPE)
4977 real_type = promote_type (real_type);
4978 if (TREE_CODE (type) == TREE_LIST)
4979 TREE_PURPOSE (type) = real_type;
4982 /* Check redefinition */
4983 for (already = arg_types; already; already = TREE_CHAIN (already))
4984 if (TREE_PURPOSE (already) == name)
4986 parse_error_context
4987 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4988 IDENTIFIER_POINTER (name),
4989 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4990 break;
4993 /* If we've an incomplete argument type, we know there is a location
4994 to patch when the type get resolved, later. */
4995 jdep = NULL;
4996 if (must_chain)
4998 patch_stage = JDEP_METHOD;
4999 type = register_incomplete_type (patch_stage,
5000 type_wfl, wfl_name, type);
5001 jdep = CLASSD_LAST (ctxp->classd_list);
5002 JDEP_MISC (jdep) = id;
5005 /* The argument node: a name and a (possibly) incomplete type. */
5006 arg_node = build_tree_list (name, real_type);
5007 /* Remeber arguments declared final. */
5008 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5010 if (jdep)
5011 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5012 TREE_CHAIN (arg_node) = arg_types;
5013 arg_types = arg_node;
5015 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5016 node = build_tree_list (id, meth);
5017 return node;
5020 static int
5021 unresolved_type_p (wfl, returned)
5022 tree wfl;
5023 tree *returned;
5026 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5028 if (returned)
5030 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5031 if (decl && current_class && (decl == TYPE_NAME (current_class)))
5032 *returned = TREE_TYPE (decl);
5033 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5034 *returned = TREE_TYPE (GET_CPC ());
5035 else
5036 *returned = NULL_TREE;
5038 return 1;
5040 if (returned)
5041 *returned = wfl;
5042 return 0;
5045 /* From NAME, build a qualified identifier node using the
5046 qualification from the current package definition. */
5048 static tree
5049 parser_qualified_classname (name)
5050 tree name;
5052 tree nested_class_name;
5054 if ((nested_class_name = maybe_make_nested_class_name (name)))
5055 return nested_class_name;
5057 if (ctxp->package)
5058 return merge_qualified_name (ctxp->package, name);
5059 else
5060 return name;
5063 /* Called once the type a interface extends is resolved. Returns 0 if
5064 everything is OK. */
5066 static int
5067 parser_check_super_interface (super_decl, this_decl, this_wfl)
5068 tree super_decl, this_decl, this_wfl;
5070 tree super_type = TREE_TYPE (super_decl);
5072 /* Has to be an interface */
5073 if (!CLASS_INTERFACE (super_decl))
5075 parse_error_context
5076 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
5077 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5078 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
5079 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5080 "interface" : "class"),
5081 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
5082 return 1;
5085 /* Check top-level interface access. Inner classes are subject to member
5086 access rules (6.6.1). */
5087 if (! INNER_CLASS_P (super_type)
5088 && check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
5089 return 1;
5091 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5092 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5093 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5094 return 0;
5097 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5098 0 if everthing is OK. */
5100 static int
5101 parser_check_super (super_decl, this_decl, wfl)
5102 tree super_decl, this_decl, wfl;
5104 tree super_type = TREE_TYPE (super_decl);
5106 /* SUPER should be a CLASS (neither an array nor an interface) */
5107 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5109 parse_error_context
5110 (wfl, "Class `%s' can't subclass %s `%s'",
5111 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5112 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5113 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5114 return 1;
5117 if (CLASS_FINAL (TYPE_NAME (super_type)))
5119 parse_error_context (wfl, "Can't subclass final classes: %s",
5120 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5121 return 1;
5124 /* Check top-level class scope. Inner classes are subject to member access
5125 rules (6.6.1). */
5126 if (! INNER_CLASS_P (super_type)
5127 && (check_pkg_class_access (DECL_NAME (super_decl), wfl)))
5128 return 1;
5130 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5131 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5132 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5133 return 0;
5136 /* Create a new dependency list and link it (in a LIFO manner) to the
5137 CTXP list of type dependency list. */
5139 static void
5140 create_jdep_list (ctxp)
5141 struct parser_ctxt *ctxp;
5143 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
5144 new->first = new->last = NULL;
5145 new->next = ctxp->classd_list;
5146 ctxp->classd_list = new;
5149 static jdeplist *
5150 reverse_jdep_list (ctxp)
5151 struct parser_ctxt *ctxp;
5153 register jdeplist *prev = NULL, *current, *next;
5154 for (current = ctxp->classd_list; current; current = next)
5156 next = current->next;
5157 current->next = prev;
5158 prev = current;
5160 return prev;
5163 /* Create a fake pointer based on the ID stored in
5164 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5165 registered again. */
5167 static tree
5168 obtain_incomplete_type (type_name)
5169 tree type_name;
5171 tree ptr = NULL_TREE, name;
5173 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5174 name = EXPR_WFL_NODE (type_name);
5175 else if (INCOMPLETE_TYPE_P (type_name))
5176 name = TYPE_NAME (type_name);
5177 else
5178 abort ();
5180 BUILD_PTR_FROM_NAME (ptr, name);
5181 layout_type (ptr);
5183 return ptr;
5186 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5187 non NULL instead of computing a new fake type based on WFL. The new
5188 dependency is inserted in the current type dependency list, in FIFO
5189 manner. */
5191 static tree
5192 register_incomplete_type (kind, wfl, decl, ptr)
5193 int kind;
5194 tree wfl, decl, ptr;
5196 jdep *new = (jdep *)xmalloc (sizeof (jdep));
5198 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5199 ptr = obtain_incomplete_type (wfl);
5201 JDEP_KIND (new) = kind;
5202 JDEP_DECL (new) = decl;
5203 JDEP_TO_RESOLVE (new) = ptr;
5204 JDEP_WFL (new) = wfl;
5205 JDEP_CHAIN (new) = NULL;
5206 JDEP_MISC (new) = NULL_TREE;
5207 /* For some dependencies, set the enclosing class of the current
5208 class to be the enclosing context */
5209 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5210 || kind == JDEP_ANONYMOUS)
5211 && GET_ENCLOSING_CPC ())
5212 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5213 else
5214 JDEP_ENCLOSING (new) = GET_CPC ();
5215 JDEP_GET_PATCH (new) = (tree *)NULL;
5217 JDEP_INSERT (ctxp->classd_list, new);
5219 return ptr;
5222 void
5223 java_check_circular_reference ()
5225 tree current;
5226 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5228 tree type = TREE_TYPE (current);
5229 if (CLASS_INTERFACE (current))
5231 /* Check all interfaces this class extends */
5232 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5233 int n, i;
5235 if (!basetype_vec)
5236 return;
5237 n = TREE_VEC_LENGTH (basetype_vec);
5238 for (i = 0; i < n; i++)
5240 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5241 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5242 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5243 parse_error_context (lookup_cl (current),
5244 "Cyclic interface inheritance");
5247 else
5248 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5249 parse_error_context (lookup_cl (current),
5250 "Cyclic class inheritance%s",
5251 (cyclic_inheritance_report ?
5252 cyclic_inheritance_report : ""));
5256 /* Augment the parameter list PARM with parameters crafted to
5257 initialize outer context locals aliases. Through ARTIFICIAL, a
5258 count is kept of the number of crafted parameters. MODE governs
5259 what eventually gets created: something suitable for a function
5260 creation or a function invocation, either the constructor or
5261 finit$. */
5263 static tree
5264 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5265 int mode;
5266 tree class_type, parm;
5267 int *artificial;
5269 tree field;
5270 tree additional_parms = NULL_TREE;
5272 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5273 if (FIELD_LOCAL_ALIAS (field))
5275 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5276 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5277 tree mangled_id;
5279 switch (mode)
5281 case AIPL_FUNCTION_DECLARATION:
5282 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5283 &buffer [4]);
5284 purpose = build_wfl_node (mangled_id);
5285 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5286 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5287 else
5288 value = TREE_TYPE (field);
5289 break;
5291 case AIPL_FUNCTION_CREATION:
5292 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5293 &buffer [4]);
5294 value = TREE_TYPE (field);
5295 break;
5297 case AIPL_FUNCTION_FINIT_INVOCATION:
5298 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5299 &buffer [4]);
5300 /* Now, this is wrong. purpose should always be the NAME
5301 of something and value its matching value (decl, type,
5302 etc...) FIXME -- but there is a lot to fix. */
5304 /* When invoked for this kind of operation, we already
5305 know whether a field is used or not. */
5306 purpose = TREE_TYPE (field);
5307 value = build_wfl_node (mangled_id);
5308 break;
5310 case AIPL_FUNCTION_CTOR_INVOCATION:
5311 /* There are two case: the constructor invokation happends
5312 outside the local inner, in which case, locales from the outer
5313 context are directly used.
5315 Otherwise, we fold to using the alias directly. */
5316 if (class_type == current_class)
5317 value = field;
5318 else
5320 name = get_identifier (&buffer[4]);
5321 value = IDENTIFIER_LOCAL_VALUE (name);
5323 break;
5325 additional_parms = tree_cons (purpose, value, additional_parms);
5326 if (artificial)
5327 *artificial +=1;
5329 if (additional_parms)
5331 if (ANONYMOUS_CLASS_P (class_type)
5332 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5333 additional_parms = nreverse (additional_parms);
5334 parm = chainon (additional_parms, parm);
5337 return parm;
5340 /* Craft a constructor for CLASS_DECL -- what we should do when none
5341 where found. ARGS is non NULL when a special signature must be
5342 enforced. This is the case for anonymous classes. */
5344 static void
5345 craft_constructor (class_decl, args)
5346 tree class_decl, args;
5348 tree class_type = TREE_TYPE (class_decl);
5349 tree parm = NULL_TREE;
5350 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5351 ACC_PUBLIC : 0);
5352 int i = 0, artificial = 0;
5353 tree decl, ctor_name;
5354 char buffer [80];
5356 /* The constructor name is <init> unless we're dealing with an
5357 anonymous class, in which case the name will be fixed after having
5358 be expanded. */
5359 if (ANONYMOUS_CLASS_P (class_type))
5360 ctor_name = DECL_NAME (class_decl);
5361 else
5362 ctor_name = init_identifier_node;
5364 /* If we're dealing with an inner class constructor, we hide the
5365 this$<n> decl in the name field of its parameter declaration. */
5366 if (PURE_INNER_CLASS_TYPE_P (class_type))
5368 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5369 parm = tree_cons (build_current_thisn (class_type),
5370 build_pointer_type (type), parm);
5372 /* Some more arguments to be hidden here. The values of the local
5373 variables of the outer context that the inner class needs to see. */
5374 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5375 class_type, parm,
5376 &artificial);
5379 /* Then if there are any args to be enforced, enforce them now */
5380 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5382 sprintf (buffer, "parm%d", i++);
5383 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5386 CRAFTED_PARAM_LIST_FIXUP (parm);
5387 decl = create_artificial_method (class_type, flags, void_type_node,
5388 ctor_name, parm);
5389 fix_method_argument_names (parm, decl);
5390 /* Now, mark the artificial parameters. */
5391 DECL_FUNCTION_NAP (decl) = artificial;
5392 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5396 /* Fix the constructors. This will be called right after circular
5397 references have been checked. It is necessary to fix constructors
5398 early even if no code generation will take place for that class:
5399 some generated constructor might be required by the class whose
5400 compilation triggered this one to be simply loaded. */
5402 void
5403 java_fix_constructors ()
5405 tree current;
5407 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5409 tree class_type = TREE_TYPE (current);
5410 int saw_ctor = 0;
5411 tree decl;
5413 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5414 continue;
5416 current_class = class_type;
5417 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5419 if (DECL_CONSTRUCTOR_P (decl))
5421 fix_constructors (decl);
5422 saw_ctor = 1;
5426 /* Anonymous class constructor can't be generated that early. */
5427 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5428 craft_constructor (current, NULL_TREE);
5432 /* safe_layout_class just makes sure that we can load a class without
5433 disrupting the current_class, input_file, lineno, etc, information
5434 about the class processed currently. */
5436 void
5437 safe_layout_class (class)
5438 tree class;
5440 tree save_current_class = current_class;
5441 const char *save_input_filename = input_filename;
5442 int save_lineno = lineno;
5444 layout_class (class);
5446 current_class = save_current_class;
5447 input_filename = save_input_filename;
5448 lineno = save_lineno;
5451 static tree
5452 jdep_resolve_class (dep)
5453 jdep *dep;
5455 tree decl;
5457 if (JDEP_RESOLVED_P (dep))
5458 decl = JDEP_RESOLVED_DECL (dep);
5459 else
5461 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5462 JDEP_DECL (dep), JDEP_WFL (dep));
5463 JDEP_RESOLVED (dep, decl);
5466 if (!decl)
5467 complete_class_report_errors (dep);
5468 else if (PURE_INNER_CLASS_DECL_P (decl))
5470 tree inner = TREE_TYPE (decl);
5471 if (! CLASS_LOADED_P (inner))
5473 safe_layout_class (inner);
5474 if (TYPE_SIZE (inner) == error_mark_node)
5475 TYPE_SIZE (inner) = NULL_TREE;
5477 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5479 return decl;
5482 /* Complete unsatisfied class declaration and their dependencies */
5484 void
5485 java_complete_class ()
5487 tree cclass;
5488 jdeplist *cclassd;
5489 int error_found;
5490 tree type;
5492 /* Process imports */
5493 process_imports ();
5495 /* Rever things so we have the right order */
5496 ctxp->class_list = nreverse (ctxp->class_list);
5497 ctxp->classd_list = reverse_jdep_list (ctxp);
5499 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5500 cclass && cclassd;
5501 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5503 jdep *dep;
5504 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5506 tree decl;
5507 if (!(decl = jdep_resolve_class (dep)))
5508 continue;
5510 /* Now it's time to patch */
5511 switch (JDEP_KIND (dep))
5513 case JDEP_SUPER:
5514 /* Simply patch super */
5515 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5516 continue;
5517 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5518 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5519 break;
5521 case JDEP_FIELD:
5523 /* We do part of the job done in add_field */
5524 tree field_decl = JDEP_DECL (dep);
5525 tree field_type = TREE_TYPE (decl);
5526 if (TREE_CODE (field_type) == RECORD_TYPE)
5527 field_type = promote_type (field_type);
5528 TREE_TYPE (field_decl) = field_type;
5529 DECL_ALIGN (field_decl) = 0;
5530 DECL_USER_ALIGN (field_decl) = 0;
5531 layout_decl (field_decl, 0);
5532 SOURCE_FRONTEND_DEBUG
5533 (("Completed field/var decl `%s' with `%s'",
5534 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5535 IDENTIFIER_POINTER (DECL_NAME (decl))));
5536 break;
5538 case JDEP_METHOD: /* We start patching a method */
5539 case JDEP_METHOD_RETURN:
5540 error_found = 0;
5541 while (1)
5543 if (decl)
5545 type = TREE_TYPE(decl);
5546 if (TREE_CODE (type) == RECORD_TYPE)
5547 type = promote_type (type);
5548 JDEP_APPLY_PATCH (dep, type);
5549 SOURCE_FRONTEND_DEBUG
5550 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5551 "Completing fct `%s' with ret type `%s'":
5552 "Completing arg `%s' with type `%s'"),
5553 IDENTIFIER_POINTER (EXPR_WFL_NODE
5554 (JDEP_DECL_WFL (dep))),
5555 IDENTIFIER_POINTER (DECL_NAME (decl))));
5557 else
5558 error_found = 1;
5559 dep = JDEP_CHAIN (dep);
5560 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5561 break;
5562 else
5563 decl = jdep_resolve_class (dep);
5565 if (!error_found)
5567 tree mdecl = JDEP_DECL (dep), signature;
5568 /* Recompute and reset the signature, check first that
5569 all types are now defined. If they're not,
5570 dont build the signature. */
5571 if (check_method_types_complete (mdecl))
5573 signature = build_java_signature (TREE_TYPE (mdecl));
5574 set_java_signature (TREE_TYPE (mdecl), signature);
5577 else
5578 continue;
5579 break;
5581 case JDEP_INTERFACE:
5582 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5583 JDEP_WFL (dep)))
5584 continue;
5585 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5586 break;
5588 case JDEP_PARM:
5589 case JDEP_VARIABLE:
5590 type = TREE_TYPE(decl);
5591 if (TREE_CODE (type) == RECORD_TYPE)
5592 type = promote_type (type);
5593 JDEP_APPLY_PATCH (dep, type);
5594 break;
5596 case JDEP_TYPE:
5597 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5598 SOURCE_FRONTEND_DEBUG
5599 (("Completing a random type dependency on a '%s' node",
5600 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5601 break;
5603 case JDEP_EXCEPTION:
5604 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5605 SOURCE_FRONTEND_DEBUG
5606 (("Completing `%s' `throws' argument node",
5607 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5608 break;
5610 case JDEP_ANONYMOUS:
5611 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5612 break;
5614 default:
5615 abort ();
5619 return;
5622 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5623 array. */
5625 static tree
5626 resolve_class (enclosing, class_type, decl, cl)
5627 tree enclosing, class_type, decl, cl;
5629 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5630 const char *base = name;
5631 tree resolved_type = TREE_TYPE (class_type);
5632 tree resolved_type_decl;
5634 if (resolved_type != NULL_TREE)
5636 tree resolved_type_decl = TYPE_NAME (resolved_type);
5637 if (resolved_type_decl == NULL_TREE
5638 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5640 resolved_type_decl = build_decl (TYPE_DECL,
5641 TYPE_NAME (class_type),
5642 resolved_type);
5644 return resolved_type_decl;
5647 /* 1- Check to see if we have an array. If true, find what we really
5648 want to resolve */
5649 while (name[0] == '[')
5650 name++;
5651 if (base != name)
5653 TYPE_NAME (class_type) = get_identifier (name);
5654 WFL_STRIP_BRACKET (cl, cl);
5657 /* 2- Resolve the bare type */
5658 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5659 decl, cl)))
5660 return NULL_TREE;
5661 resolved_type = TREE_TYPE (resolved_type_decl);
5663 /* 3- If we have and array, reconstruct the array down to its nesting */
5664 if (base != name)
5666 while (base != name)
5668 resolved_type = build_java_array_type (resolved_type, -1);
5669 name--;
5671 /* A TYPE_NAME that is a TYPE_DECL was set in
5672 build_java_array_type, return it. */
5673 resolved_type_decl = TYPE_NAME (resolved_type);
5675 TREE_TYPE (class_type) = resolved_type;
5676 return resolved_type_decl;
5679 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5680 are used to report error messages. Do not try to replace TYPE_NAME
5681 (class_type) by a variable, since it is changed by
5682 find_in_imports{_on_demand} and (but it doesn't really matter)
5683 qualify_and_find. */
5685 tree
5686 do_resolve_class (enclosing, class_type, decl, cl)
5687 tree enclosing, class_type, decl, cl;
5689 tree new_class_decl = NULL_TREE, super = NULL_TREE;
5690 struct hash_table _ht, *circularity_hash = &_ht;
5692 /* This hash table is used to register the classes we're going
5693 through when searching the current class as an inner class, in
5694 order to detect circular references. Remember to free it before
5695 returning the section 0- of this function. */
5696 hash_table_init (circularity_hash, hash_newfunc,
5697 java_hash_hash_tree_node, java_hash_compare_tree_node);
5699 /* 0- Search in the current class as an inner class.
5700 Maybe some code here should be added to load the class or
5701 something, at least if the class isn't an inner class and ended
5702 being loaded from class file. FIXME. */
5703 while (enclosing)
5705 new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5706 &super, class_type);
5707 if (new_class_decl)
5708 break;
5710 /* If we haven't found anything because SUPER reached Object and
5711 ENCLOSING happens to be an innerclass, try the enclosing context. */
5712 if ((!super || super == object_type_node) &&
5713 enclosing && INNER_CLASS_DECL_P (enclosing))
5714 enclosing = DECL_CONTEXT (enclosing);
5715 else
5716 enclosing = NULL_TREE;
5719 hash_table_free (circularity_hash);
5721 if (new_class_decl)
5722 return new_class_decl;
5724 /* 1- Check for the type in single imports. This will change
5725 TYPE_NAME() if something relevant is found */
5726 find_in_imports (class_type);
5728 /* 2- And check for the type in the current compilation unit */
5729 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5731 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5732 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5733 load_class (TYPE_NAME (class_type), 0);
5734 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5737 /* 3- Search according to the current package definition */
5738 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5740 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5741 TYPE_NAME (class_type))))
5742 return new_class_decl;
5745 /* 4- Check the import on demands. Don't allow bar.baz to be
5746 imported from foo.* */
5747 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5748 if (find_in_imports_on_demand (class_type))
5749 return NULL_TREE;
5751 /* If found in find_in_imports_on_demant, the type has already been
5752 loaded. */
5753 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5754 return new_class_decl;
5756 /* 5- Try with a name qualified with the package name we've seen so far */
5757 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5759 tree package;
5761 /* If there is a current package (ctxp->package), it's the first
5762 element of package_list and we can skip it. */
5763 for (package = (ctxp->package ?
5764 TREE_CHAIN (package_list) : package_list);
5765 package; package = TREE_CHAIN (package))
5766 if ((new_class_decl = qualify_and_find (class_type,
5767 TREE_PURPOSE (package),
5768 TYPE_NAME (class_type))))
5769 return new_class_decl;
5772 /* 5- Check an other compilation unit that bears the name of type */
5773 load_class (TYPE_NAME (class_type), 0);
5775 if (!cl)
5776 cl = lookup_cl (decl);
5778 /* If we don't have a value for CL, then we're being called recursively.
5779 We can't check package access just yet, but it will be taken care of
5780 by the caller. */
5781 if (cl)
5783 if (check_pkg_class_access (TYPE_NAME (class_type), cl))
5784 return NULL_TREE;
5787 /* 6- Last call for a resolution */
5788 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5791 static tree
5792 qualify_and_find (class_type, package, name)
5793 tree class_type, package, name;
5795 tree new_qualified = merge_qualified_name (package, name);
5796 tree new_class_decl;
5798 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5799 load_class (new_qualified, 0);
5800 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5802 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5803 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5804 load_class (new_qualified, 0);
5805 TYPE_NAME (class_type) = new_qualified;
5806 return IDENTIFIER_CLASS_VALUE (new_qualified);
5808 return NULL_TREE;
5811 /* Resolve NAME and lay it out (if not done and if not the current
5812 parsed class). Return a decl node. This function is meant to be
5813 called when type resolution is necessary during the walk pass. */
5815 static tree
5816 resolve_and_layout (something, cl)
5817 tree something;
5818 tree cl;
5820 tree decl, decl_type;
5822 /* Don't do that on the current class */
5823 if (something == current_class)
5824 return TYPE_NAME (current_class);
5826 /* Don't do anything for void and other primitive types */
5827 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5828 return NULL_TREE;
5830 /* Pointer types can be reall pointer types or fake pointers. When
5831 finding a real pointer, recheck for primitive types */
5832 if (TREE_CODE (something) == POINTER_TYPE)
5834 if (TREE_TYPE (something))
5836 something = TREE_TYPE (something);
5837 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5838 return NULL_TREE;
5840 else
5841 something = TYPE_NAME (something);
5844 /* Don't do anything for arrays of primitive types */
5845 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5846 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5847 return NULL_TREE;
5849 /* Something might be a WFL */
5850 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5851 something = EXPR_WFL_NODE (something);
5853 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5854 TYPE_DECL or a real TYPE */
5855 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5856 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5857 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5859 if (!(decl = resolve_no_layout (something, cl)))
5860 return NULL_TREE;
5862 /* Resolve and layout if necessary */
5863 decl_type = TREE_TYPE (decl);
5864 layout_class_methods (decl_type);
5865 /* Check methods */
5866 if (CLASS_FROM_SOURCE_P (decl_type))
5867 java_check_methods (decl);
5868 /* Layout the type if necessary */
5869 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5870 safe_layout_class (decl_type);
5872 return decl;
5875 /* Resolve a class, returns its decl but doesn't perform any
5876 layout. The current parsing context is saved and restored */
5878 static tree
5879 resolve_no_layout (name, cl)
5880 tree name, cl;
5882 tree ptr, decl;
5883 BUILD_PTR_FROM_NAME (ptr, name);
5884 java_parser_context_save_global ();
5885 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5886 java_parser_context_restore_global ();
5888 return decl;
5891 /* Called when reporting errors. Skip leader '[' in a complex array
5892 type description that failed to be resolved. */
5894 static const char *
5895 purify_type_name (name)
5896 const char *name;
5898 while (*name && *name == '[')
5899 name++;
5900 return name;
5903 /* The type CURRENT refers to can't be found. We print error messages. */
5905 static void
5906 complete_class_report_errors (dep)
5907 jdep *dep;
5909 const char *name;
5911 if (!JDEP_WFL (dep))
5912 return;
5914 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5915 switch (JDEP_KIND (dep))
5917 case JDEP_SUPER:
5918 parse_error_context
5919 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5920 purify_type_name (name),
5921 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5922 break;
5923 case JDEP_FIELD:
5924 parse_error_context
5925 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5926 purify_type_name (name),
5927 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5928 break;
5929 case JDEP_METHOD: /* Covers arguments */
5930 parse_error_context
5931 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5932 purify_type_name (name),
5933 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5934 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5935 break;
5936 case JDEP_METHOD_RETURN: /* Covers return type */
5937 parse_error_context
5938 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5939 purify_type_name (name),
5940 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5941 break;
5942 case JDEP_INTERFACE:
5943 parse_error_context
5944 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5945 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5946 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5947 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5948 break;
5949 case JDEP_VARIABLE:
5950 parse_error_context
5951 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5952 purify_type_name (IDENTIFIER_POINTER
5953 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5954 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5955 break;
5956 case JDEP_EXCEPTION: /* As specified by `throws' */
5957 parse_error_context
5958 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5959 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5960 break;
5961 default:
5962 /* Fix for -Wall. Just break doing nothing. The error will be
5963 caught later */
5964 break;
5968 /* Return a static string containing the DECL prototype string. If
5969 DECL is a constructor, use the class name instead of the form
5970 <init> */
5972 static const char *
5973 get_printable_method_name (decl)
5974 tree decl;
5976 const char *to_return;
5977 tree name = NULL_TREE;
5979 if (DECL_CONSTRUCTOR_P (decl))
5981 name = DECL_NAME (decl);
5982 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
5985 to_return = lang_printable_name (decl, 0);
5986 if (DECL_CONSTRUCTOR_P (decl))
5987 DECL_NAME (decl) = name;
5989 return to_return;
5992 /* Track method being redefined inside the same class. As a side
5993 effect, set DECL_NAME to an IDENTIFIER (prior entering this
5994 function it's a FWL, so we can track errors more accurately.) */
5996 static int
5997 check_method_redefinition (class, method)
5998 tree class, method;
6000 tree redef, sig;
6002 /* There's no need to verify <clinit> and finit$ and instinit$ */
6003 if (DECL_CLINIT_P (method)
6004 || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6005 return 0;
6007 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6008 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6010 if (redef == method)
6011 break;
6012 if (DECL_NAME (redef) == DECL_NAME (method)
6013 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6014 && !DECL_ARTIFICIAL (method))
6016 parse_error_context
6017 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
6018 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6019 get_printable_method_name (redef));
6020 return 1;
6023 return 0;
6026 /* Return 1 if check went ok, 0 otherwise. */
6027 static int
6028 check_abstract_method_definitions (do_interface, class_decl, type)
6029 int do_interface;
6030 tree class_decl, type;
6032 tree class = TREE_TYPE (class_decl);
6033 tree method, end_type;
6034 int ok = 1;
6036 end_type = (do_interface ? object_type_node : type);
6037 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6039 tree other_super, other_method, method_sig, method_name;
6040 int found = 0;
6041 int end_type_reached = 0;
6043 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6044 continue;
6046 /* Now verify that somewhere in between TYPE and CLASS,
6047 abstract method METHOD gets a non abstract definition
6048 that is inherited by CLASS. */
6050 method_sig = build_java_signature (TREE_TYPE (method));
6051 method_name = DECL_NAME (method);
6052 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6053 method_name = EXPR_WFL_NODE (method_name);
6055 other_super = class;
6056 do {
6057 if (other_super == end_type)
6058 end_type_reached = 1;
6060 /* Method search */
6061 for (other_method = TYPE_METHODS (other_super); other_method;
6062 other_method = TREE_CHAIN (other_method))
6064 tree s = build_java_signature (TREE_TYPE (other_method));
6065 tree other_name = DECL_NAME (other_method);
6067 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6068 other_name = EXPR_WFL_NODE (other_name);
6069 if (!DECL_CLINIT_P (other_method)
6070 && !DECL_CONSTRUCTOR_P (other_method)
6071 && method_name == other_name
6072 && method_sig == s
6073 && !METHOD_ABSTRACT (other_method))
6075 found = 1;
6076 break;
6079 other_super = CLASSTYPE_SUPER (other_super);
6080 } while (!end_type_reached);
6082 /* Report that abstract METHOD didn't find an implementation
6083 that CLASS can use. */
6084 if (!found)
6086 char *t = xstrdup (lang_printable_name
6087 (TREE_TYPE (TREE_TYPE (method)), 0));
6088 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6090 parse_error_context
6091 (lookup_cl (class_decl),
6092 "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",
6093 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6094 t, lang_printable_name (method, 0),
6095 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6096 "interface" : "class"),
6097 IDENTIFIER_POINTER (ccn),
6098 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6099 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6100 ok = 0;
6101 free (t);
6105 if (ok && do_interface)
6107 /* Check for implemented interfaces. */
6108 int i;
6109 tree vector = TYPE_BINFO_BASETYPES (type);
6110 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
6112 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6113 ok = check_abstract_method_definitions (1, class_decl, super);
6117 return ok;
6120 /* Check that CLASS_DECL somehow implements all inherited abstract
6121 methods. */
6123 static void
6124 java_check_abstract_method_definitions (class_decl)
6125 tree class_decl;
6127 tree class = TREE_TYPE (class_decl);
6128 tree super, vector;
6129 int i;
6131 if (CLASS_ABSTRACT (class_decl))
6132 return;
6134 /* Check for inherited types */
6135 super = class;
6136 do {
6137 super = CLASSTYPE_SUPER (super);
6138 check_abstract_method_definitions (0, class_decl, super);
6139 } while (super != object_type_node);
6141 /* Check for implemented interfaces. */
6142 vector = TYPE_BINFO_BASETYPES (class);
6143 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
6145 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6146 check_abstract_method_definitions (1, class_decl, super);
6150 /* Check all the types method DECL uses and return 1 if all of them
6151 are now complete, 0 otherwise. This is used to check whether its
6152 safe to build a method signature or not. */
6154 static int
6155 check_method_types_complete (decl)
6156 tree decl;
6158 tree type = TREE_TYPE (decl);
6159 tree args;
6161 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6162 return 0;
6164 args = TYPE_ARG_TYPES (type);
6165 if (TREE_CODE (type) == METHOD_TYPE)
6166 args = TREE_CHAIN (args);
6167 for (; args != end_params_node; args = TREE_CHAIN (args))
6168 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6169 return 0;
6171 return 1;
6174 /* Visible interface to check methods contained in CLASS_DECL */
6176 void
6177 java_check_methods (class_decl)
6178 tree class_decl;
6180 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6181 return;
6183 if (CLASS_INTERFACE (class_decl))
6184 java_check_abstract_methods (class_decl);
6185 else
6186 java_check_regular_methods (class_decl);
6188 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6191 /* Check all the methods of CLASS_DECL. Methods are first completed
6192 then checked according to regular method existance rules. If no
6193 constructor for CLASS_DECL were encountered, then build its
6194 declaration. */
6196 static void
6197 java_check_regular_methods (class_decl)
6198 tree class_decl;
6200 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6201 tree method;
6202 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6203 tree found = NULL_TREE;
6204 tree mthrows;
6206 /* It is not necessary to check methods defined in java.lang.Object */
6207 if (class == object_type_node)
6208 return;
6210 if (!TYPE_NVIRTUALS (class))
6211 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6213 /* Should take interfaces into account. FIXME */
6214 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6216 tree sig;
6217 tree method_wfl = DECL_FUNCTION_WFL (method);
6218 int aflags;
6220 /* Check for redefinitions */
6221 if (check_method_redefinition (class, method))
6222 continue;
6224 /* If we see one constructor a mark so we don't generate the
6225 default one. Also skip other verifications: constructors
6226 can't be inherited hence hiden or overriden */
6227 if (DECL_CONSTRUCTOR_P (method))
6229 saw_constructor = 1;
6230 continue;
6233 /* We verify things thrown by the method. They must inherits from
6234 java.lang.Throwable */
6235 for (mthrows = DECL_FUNCTION_THROWS (method);
6236 mthrows; mthrows = TREE_CHAIN (mthrows))
6238 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6239 parse_error_context
6240 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6241 IDENTIFIER_POINTER
6242 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6245 sig = build_java_argument_signature (TREE_TYPE (method));
6246 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6248 /* Inner class can't declare static methods */
6249 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6251 char *t = xstrdup (lang_printable_name (class, 0));
6252 parse_error_context
6253 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6254 lang_printable_name (method, 0), t);
6255 free (t);
6258 /* Nothing overrides or it's a private method. */
6259 if (!found)
6260 continue;
6261 if (METHOD_PRIVATE (found))
6263 found = NULL_TREE;
6264 continue;
6267 /* If `found' is declared in an interface, make sure the
6268 modifier matches. */
6269 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6270 && clinit_identifier_node != DECL_NAME (found)
6271 && !METHOD_PUBLIC (method))
6273 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6274 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6275 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6276 lang_printable_name (method, 0),
6277 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6280 /* Can't override a method with the same name and different return
6281 types. */
6282 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6284 char *t = xstrdup
6285 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6286 parse_error_context
6287 (method_wfl,
6288 "Method `%s' was defined with return type `%s' in class `%s'",
6289 lang_printable_name (found, 0), t,
6290 IDENTIFIER_POINTER
6291 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6292 free (t);
6295 aflags = get_access_flags_from_decl (found);
6297 /* Can't override final. Can't override static. */
6298 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6300 /* Static *can* override static */
6301 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6302 continue;
6303 parse_error_context
6304 (method_wfl,
6305 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6306 (METHOD_FINAL (found) ? "Final" : "Static"),
6307 lang_printable_name (found, 0),
6308 (METHOD_FINAL (found) ? "final" : "static"),
6309 IDENTIFIER_POINTER
6310 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6311 continue;
6314 /* Static method can't override instance method. */
6315 if (METHOD_STATIC (method))
6317 parse_error_context
6318 (method_wfl,
6319 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6320 lang_printable_name (found, 0),
6321 IDENTIFIER_POINTER
6322 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6323 continue;
6326 /* - Overriding/hiding public must be public
6327 - Overriding/hiding protected must be protected or public
6328 - If the overriden or hidden method has default (package)
6329 access, then the overriding or hiding method must not be
6330 private; otherwise, a compile-time error occurs. If
6331 `found' belongs to an interface, things have been already
6332 taken care of. */
6333 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6334 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6335 || (METHOD_PROTECTED (found)
6336 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6337 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6338 && METHOD_PRIVATE (method))))
6340 parse_error_context
6341 (method_wfl,
6342 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6343 (METHOD_PUBLIC (method) ? "public" :
6344 (METHOD_PRIVATE (method) ? "private" : "protected")),
6345 IDENTIFIER_POINTER (DECL_NAME
6346 (TYPE_NAME (DECL_CONTEXT (found)))));
6347 continue;
6350 /* Overriding methods must have compatible `throws' clauses on checked
6351 exceptions, if any */
6352 check_throws_clauses (method, method_wfl, found);
6354 /* Inheriting multiple methods with the same signature. FIXME */
6357 if (!TYPE_NVIRTUALS (class))
6358 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6360 /* Search for inherited abstract method not yet implemented in this
6361 class. */
6362 java_check_abstract_method_definitions (class_decl);
6364 if (!saw_constructor)
6365 abort ();
6368 /* Return a non zero value if the `throws' clause of METHOD (if any)
6369 is incompatible with the `throws' clause of FOUND (if any). */
6371 static void
6372 check_throws_clauses (method, method_wfl, found)
6373 tree method, method_wfl, found;
6375 tree mthrows, fthrows;
6377 /* Can't check these things with class loaded from bytecode. FIXME */
6378 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6379 return;
6381 for (mthrows = DECL_FUNCTION_THROWS (method);
6382 mthrows; mthrows = TREE_CHAIN (mthrows))
6384 /* We don't verify unchecked expressions */
6385 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6386 continue;
6387 /* Checked expression must be compatible */
6388 for (fthrows = DECL_FUNCTION_THROWS (found);
6389 fthrows; fthrows = TREE_CHAIN (fthrows))
6390 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6391 break;
6392 if (!fthrows)
6394 parse_error_context
6395 (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'",
6396 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6397 lang_printable_name (found, 0),
6398 IDENTIFIER_POINTER
6399 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6404 /* Check abstract method of interface INTERFACE */
6406 static void
6407 java_check_abstract_methods (interface_decl)
6408 tree interface_decl;
6410 int i, n;
6411 tree method, basetype_vec, found;
6412 tree interface = TREE_TYPE (interface_decl);
6414 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6416 /* 2- Check for double definition inside the defining interface */
6417 if (check_method_redefinition (interface, method))
6418 continue;
6420 /* 3- Overriding is OK as far as we preserve the return type and
6421 the thrown exceptions (FIXME) */
6422 found = lookup_java_interface_method2 (interface, method);
6423 if (found)
6425 char *t;
6426 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6427 parse_error_context
6428 (DECL_FUNCTION_WFL (found),
6429 "Method `%s' was defined with return type `%s' in class `%s'",
6430 lang_printable_name (found, 0), t,
6431 IDENTIFIER_POINTER
6432 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6433 free (t);
6434 continue;
6438 /* 4- Inherited methods can't differ by their returned types */
6439 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6440 return;
6441 n = TREE_VEC_LENGTH (basetype_vec);
6442 for (i = 0; i < n; i++)
6444 tree sub_interface_method, sub_interface;
6445 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6446 if (!vec_elt)
6447 continue;
6448 sub_interface = BINFO_TYPE (vec_elt);
6449 for (sub_interface_method = TYPE_METHODS (sub_interface);
6450 sub_interface_method;
6451 sub_interface_method = TREE_CHAIN (sub_interface_method))
6453 found = lookup_java_interface_method2 (interface,
6454 sub_interface_method);
6455 if (found && (found != sub_interface_method))
6457 parse_error_context
6458 (lookup_cl (sub_interface_method),
6459 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6460 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6461 lang_printable_name (found, 0),
6462 IDENTIFIER_POINTER
6463 (DECL_NAME (TYPE_NAME
6464 (DECL_CONTEXT (sub_interface_method)))),
6465 IDENTIFIER_POINTER
6466 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6472 /* Lookup methods in interfaces using their name and partial
6473 signature. Return a matching method only if their types differ. */
6475 static tree
6476 lookup_java_interface_method2 (class, method_decl)
6477 tree class, method_decl;
6479 int i, n;
6480 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6482 if (!basetype_vec)
6483 return NULL_TREE;
6485 n = TREE_VEC_LENGTH (basetype_vec);
6486 for (i = 0; i < n; i++)
6488 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6489 if ((BINFO_TYPE (vec_elt) != object_type_node)
6490 && (to_return =
6491 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6492 return to_return;
6494 for (i = 0; i < n; i++)
6496 to_return = lookup_java_interface_method2
6497 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6498 if (to_return)
6499 return to_return;
6502 return NULL_TREE;
6505 /* Lookup method using their name and partial signature. Return a
6506 matching method only if their types differ. */
6508 static tree
6509 lookup_java_method2 (clas, method_decl, do_interface)
6510 tree clas, method_decl;
6511 int do_interface;
6513 tree method, method_signature, method_name, method_type, name;
6515 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6516 name = DECL_NAME (method_decl);
6517 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6518 EXPR_WFL_NODE (name) : name);
6519 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6521 while (clas != NULL_TREE)
6523 for (method = TYPE_METHODS (clas);
6524 method != NULL_TREE; method = TREE_CHAIN (method))
6526 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6527 tree name = DECL_NAME (method);
6528 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6529 EXPR_WFL_NODE (name) : name) == method_name
6530 && method_sig == method_signature
6531 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6532 return method;
6534 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6536 return NULL_TREE;
6539 /* Return the line that matches DECL line number, and try its best to
6540 position the column number. Used during error reports. */
6542 static tree
6543 lookup_cl (decl)
6544 tree decl;
6546 static tree cl = NULL_TREE;
6547 char *line, *found;
6549 if (!decl)
6550 return NULL_TREE;
6552 if (cl == NULL_TREE)
6554 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6555 ggc_add_tree_root (&cl, 1);
6558 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6559 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6561 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
6562 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6564 found = strstr ((const char *)line,
6565 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6566 if (found)
6567 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6569 return cl;
6572 /* Look for a simple name in the single-type import list */
6574 static tree
6575 find_name_in_single_imports (name)
6576 tree name;
6578 tree node;
6580 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6581 if (TREE_VALUE (node) == name)
6582 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6584 return NULL_TREE;
6587 /* Process all single-type import. */
6589 static int
6590 process_imports ()
6592 tree import;
6593 int error_found;
6595 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6597 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6598 char *original_name;
6600 obstack_grow0 (&temporary_obstack,
6601 IDENTIFIER_POINTER (to_be_found),
6602 IDENTIFIER_LENGTH (to_be_found));
6603 original_name = obstack_finish (&temporary_obstack);
6605 /* Don't load twice something already defined. */
6606 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6607 continue;
6609 while (1)
6611 tree left;
6613 QUALIFIED_P (to_be_found) = 1;
6614 load_class (to_be_found, 0);
6615 error_found =
6616 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6618 /* We found it, we can bail out */
6619 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6620 break;
6622 /* We haven't found it. Maybe we're trying to access an
6623 inner class. The only way for us to know is to try again
6624 after having dropped a qualifier. If we can't break it further,
6625 we have an error. */
6626 if (breakdown_qualified (&left, NULL, to_be_found))
6627 break;
6629 to_be_found = left;
6631 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6633 parse_error_context (TREE_PURPOSE (import),
6634 "Class or interface `%s' not found in import",
6635 original_name);
6636 error_found = 1;
6639 obstack_free (&temporary_obstack, original_name);
6640 if (error_found)
6641 return 1;
6643 return 0;
6646 /* Possibly find and mark a class imported by a single-type import
6647 statement. */
6649 static void
6650 find_in_imports (class_type)
6651 tree class_type;
6653 tree import;
6655 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6656 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6658 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6659 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6663 static int
6664 note_possible_classname (name, len)
6665 const char *name;
6666 int len;
6668 tree node;
6669 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6670 len = len - 5;
6671 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6672 len = len - 6;
6673 else
6674 return 0;
6675 node = ident_subst (name, len, "", '/', '.', "");
6676 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6677 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6678 return 1;
6681 /* Read a import directory, gathering potential match for further type
6682 references. Indifferently reads a filesystem or a ZIP archive
6683 directory. */
6685 static void
6686 read_import_dir (wfl)
6687 tree wfl;
6689 tree package_id = EXPR_WFL_NODE (wfl);
6690 const char *package_name = IDENTIFIER_POINTER (package_id);
6691 int package_length = IDENTIFIER_LENGTH (package_id);
6692 DIR *dirp = NULL;
6693 JCF *saved_jcf = current_jcf;
6695 int found = 0;
6696 int k;
6697 void *entry;
6698 struct buffer filename[1];
6701 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6702 return;
6703 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6705 BUFFER_INIT (filename);
6706 buffer_grow (filename, package_length + 100);
6708 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6710 const char *entry_name = jcf_path_name (entry);
6711 int entry_length = strlen (entry_name);
6712 if (jcf_path_is_zipfile (entry))
6714 ZipFile *zipf;
6715 buffer_grow (filename, entry_length);
6716 memcpy (filename->data, entry_name, entry_length - 1);
6717 filename->data[entry_length-1] = '\0';
6718 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6719 if (zipf == NULL)
6720 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6721 else
6723 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6724 BUFFER_RESET (filename);
6725 for (k = 0; k < package_length; k++)
6727 char ch = package_name[k];
6728 *filename->ptr++ = ch == '.' ? '/' : ch;
6730 *filename->ptr++ = '/';
6732 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6734 const char *current_entry = ZIPDIR_FILENAME (zipd);
6735 int current_entry_len = zipd->filename_length;
6737 if (current_entry_len >= BUFFER_LENGTH (filename)
6738 && strncmp (filename->data, current_entry,
6739 BUFFER_LENGTH (filename)) != 0)
6740 continue;
6741 found |= note_possible_classname (current_entry,
6742 current_entry_len);
6746 else
6748 BUFFER_RESET (filename);
6749 buffer_grow (filename, entry_length + package_length + 4);
6750 strcpy (filename->data, entry_name);
6751 filename->ptr = filename->data + entry_length;
6752 for (k = 0; k < package_length; k++)
6754 char ch = package_name[k];
6755 *filename->ptr++ = ch == '.' ? '/' : ch;
6757 *filename->ptr = '\0';
6759 dirp = opendir (filename->data);
6760 if (dirp == NULL)
6761 continue;
6762 *filename->ptr++ = '/';
6763 for (;;)
6765 int len;
6766 const char *d_name;
6767 struct dirent *direntp = readdir (dirp);
6768 if (!direntp)
6769 break;
6770 d_name = direntp->d_name;
6771 len = strlen (direntp->d_name);
6772 buffer_grow (filename, len+1);
6773 strcpy (filename->ptr, d_name);
6774 found |= note_possible_classname (filename->data + entry_length,
6775 package_length+len+1);
6777 if (dirp)
6778 closedir (dirp);
6782 free (filename->data);
6784 /* Here we should have a unified way of retrieving an entry, to be
6785 indexed. */
6786 if (!found)
6788 static int first = 1;
6789 if (first)
6791 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6792 java_error_count++;
6793 first = 0;
6795 else
6796 parse_error_context (wfl, "Package `%s' not found in import",
6797 package_name);
6798 current_jcf = saved_jcf;
6799 return;
6801 current_jcf = saved_jcf;
6804 /* Possibly find a type in the import on demands specified
6805 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6806 entire list, to detected potential double definitions. */
6808 static int
6809 find_in_imports_on_demand (class_type)
6810 tree class_type;
6812 tree node, import, node_to_use = NULL_TREE;
6813 int seen_once = -1;
6814 tree cl = NULL_TREE;
6816 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6818 const char *id_name;
6819 obstack_grow (&temporary_obstack,
6820 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6821 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6822 obstack_1grow (&temporary_obstack, '.');
6823 obstack_grow0 (&temporary_obstack,
6824 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6825 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6826 id_name = obstack_finish (&temporary_obstack);
6828 node = maybe_get_identifier (id_name);
6829 if (node && IS_A_CLASSFILE_NAME (node))
6831 if (seen_once < 0)
6833 cl = TREE_PURPOSE (import);
6834 seen_once = 1;
6835 node_to_use = node;
6837 else
6839 seen_once++;
6840 parse_error_context
6841 (TREE_PURPOSE (import),
6842 "Type `%s' also potentially defined in package `%s'",
6843 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6844 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6849 if (seen_once == 1)
6851 /* Setup lineno so that it refers to the line of the import (in
6852 case we parse a class file and encounter errors */
6853 tree decl;
6854 int saved_lineno = lineno;
6855 lineno = EXPR_WFL_LINENO (cl);
6856 TYPE_NAME (class_type) = node_to_use;
6857 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6858 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6859 /* If there is no DECL set for the class or if the class isn't
6860 loaded and not seen in source yet, the load */
6861 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6862 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6864 load_class (node_to_use, 0);
6865 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6867 lineno = saved_lineno;
6868 if (! INNER_CLASS_P (TREE_TYPE (decl)))
6869 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6870 else
6871 /* 6.6.1: Inner classes are subject to member access rules. */
6872 return 0;
6874 else
6875 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6878 /* Add package NAME to the list of package encountered so far. To
6879 speed up class lookup in do_resolve_class, we make sure a
6880 particular package is added only once. */
6882 static void
6883 register_package (name)
6884 tree name;
6886 static struct hash_table _pht, *pht = NULL;
6888 if (!pht)
6890 hash_table_init (&_pht, hash_newfunc,
6891 java_hash_hash_tree_node, java_hash_compare_tree_node);
6892 pht = &_pht;
6895 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6897 package_list = chainon (package_list, build_tree_list (name, NULL));
6898 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6902 static tree
6903 resolve_package (pkg, next)
6904 tree pkg, *next;
6906 tree current, acc;
6907 tree type_name = NULL_TREE;
6908 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6910 /* The trick is to determine when the package name stops and were
6911 the name of something contained in the package starts. Then we
6912 return a fully qualified name of what we want to get. */
6914 /* Do a quick search on well known package names */
6915 if (!strncmp (name, "java.lang.reflect", 17))
6917 *next =
6918 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6919 type_name = lookup_package_type (name, 17);
6921 else if (!strncmp (name, "java.lang", 9))
6923 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6924 type_name = lookup_package_type (name, 9);
6927 /* If we found something here, return */
6928 if (type_name)
6929 return type_name;
6931 *next = EXPR_WFL_QUALIFICATION (pkg);
6933 /* Try to progressively construct a type name */
6934 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6935 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6936 current; current = TREE_CHAIN (current))
6938 /* If we don't have what we're expecting, exit now. TYPE_NAME
6939 will be null and the error caught later. */
6940 if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
6941 break;
6942 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6943 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6945 type_name = acc;
6946 /* resolve_package should be used in a loop, hence we
6947 point at this one to naturally process the next one at
6948 the next iteration. */
6949 *next = current;
6950 break;
6953 return type_name;
6956 static tree
6957 lookup_package_type (name, from)
6958 const char *name;
6959 int from;
6961 char subname [128];
6962 const char *sub = &name[from+1];
6963 while (*sub != '.' && *sub)
6964 sub++;
6965 strncpy (subname, name, sub-name);
6966 subname [sub-name] = '\0';
6967 return get_identifier (subname);
6970 /* Check accessibility of inner classes according to member access rules.
6971 DECL is the inner class, ENCLOSING_DECL is the class from which the
6972 access is being attempted. */
6974 static void
6975 check_inner_class_access (decl, enclosing_decl, cl)
6976 tree decl, enclosing_decl, cl;
6978 const char *access;
6979 tree enclosing_decl_type;
6981 /* We don't issue an error message when CL is null. CL can be null
6982 as a result of processing a JDEP crafted by source_start_java_method
6983 for the purpose of patching its parm decl. But the error would
6984 have been already trapped when fixing the method's signature.
6985 DECL can also be NULL in case of earlier errors. */
6986 if (!decl || !cl)
6987 return;
6989 enclosing_decl_type = TREE_TYPE (enclosing_decl);
6991 if (CLASS_PRIVATE (decl))
6993 /* Access is permitted only within the body of the top-level
6994 class in which DECL is declared. */
6995 tree top_level = decl;
6996 while (DECL_CONTEXT (top_level))
6997 top_level = DECL_CONTEXT (top_level);
6998 while (DECL_CONTEXT (enclosing_decl))
6999 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7000 if (top_level == enclosing_decl)
7001 return;
7002 access = "private";
7004 else if (CLASS_PROTECTED (decl))
7006 tree decl_context;
7007 /* Access is permitted from within the same package... */
7008 if (in_same_package (decl, enclosing_decl))
7009 return;
7011 /* ... or from within the body of a subtype of the context in which
7012 DECL is declared. */
7013 decl_context = DECL_CONTEXT (decl);
7014 while (enclosing_decl)
7016 if (CLASS_INTERFACE (decl))
7018 if (interface_of_p (TREE_TYPE (decl_context),
7019 enclosing_decl_type))
7020 return;
7022 else
7024 /* Eww. The order of the arguments is different!! */
7025 if (inherits_from_p (enclosing_decl_type,
7026 TREE_TYPE (decl_context)))
7027 return;
7029 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7031 access = "protected";
7033 else if (! CLASS_PUBLIC (decl))
7035 /* Access is permitted only from within the same package as DECL. */
7036 if (in_same_package (decl, enclosing_decl))
7037 return;
7038 access = "non-public";
7040 else
7041 /* Class is public. */
7042 return;
7044 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7045 (CLASS_INTERFACE (decl) ? "interface" : "class"),
7046 lang_printable_name (decl, 0), access);
7049 /* Accessibility check for top-level classes. If CLASS_NAME is in a foreign
7050 package, it must be PUBLIC. Return 0 if no access violations were found,
7051 1 otherwise. */
7053 static int
7054 check_pkg_class_access (class_name, cl)
7055 tree class_name;
7056 tree cl;
7058 tree type;
7060 if (!IDENTIFIER_CLASS_VALUE (class_name))
7061 return 0;
7063 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7064 return 0;
7066 if (!CLASS_PUBLIC (TYPE_NAME (type)))
7068 /* Access to a private class within the same package is
7069 allowed. */
7070 tree l, r;
7071 breakdown_qualified (&l, &r, class_name);
7072 if (!QUALIFIED_P (class_name) && !ctxp->package)
7073 /* Both in the empty package. */
7074 return 0;
7075 if (l == ctxp->package)
7076 /* Both in the same package. */
7077 return 0;
7079 parse_error_context
7080 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
7081 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7082 IDENTIFIER_POINTER (class_name));
7083 return 1;
7085 return 0;
7088 /* Local variable declaration. */
7090 static void
7091 declare_local_variables (modifier, type, vlist)
7092 int modifier;
7093 tree type;
7094 tree vlist;
7096 tree decl, current, saved_type;
7097 tree type_wfl = NULL_TREE;
7098 int must_chain = 0;
7099 int final_p = 0;
7101 /* Push a new block if statements were seen between the last time we
7102 pushed a block and now. Keep a count of blocks to close */
7103 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7105 tree b = enter_block ();
7106 BLOCK_IS_IMPLICIT (b) = 1;
7109 if (modifier)
7111 int i;
7112 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
7113 if (modifier == ACC_FINAL)
7114 final_p = 1;
7115 else
7117 parse_error_context
7118 (ctxp->modifier_ctx [i],
7119 "Only `final' is allowed as a local variables modifier");
7120 return;
7124 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7125 hold the TYPE value if a new incomplete has to be created (as
7126 opposed to being found already existing and reused). */
7127 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7129 /* If TYPE is fully resolved and we don't have a reference, make one */
7130 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7132 /* Go through all the declared variables */
7133 for (current = vlist, saved_type = type; current;
7134 current = TREE_CHAIN (current), type = saved_type)
7136 tree other, real_type;
7137 tree wfl = TREE_PURPOSE (current);
7138 tree name = EXPR_WFL_NODE (wfl);
7139 tree init = TREE_VALUE (current);
7141 /* Process NAME, as it may specify extra dimension(s) for it */
7142 type = build_array_from_name (type, type_wfl, name, &name);
7144 /* Variable redefinition check */
7145 if ((other = lookup_name_in_blocks (name)))
7147 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7148 DECL_SOURCE_LINE (other));
7149 continue;
7152 /* Type adjustment. We may have just readjusted TYPE because
7153 the variable specified more dimensions. Make sure we have
7154 a reference if we can and don't have one already. */
7155 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7157 real_type = GET_REAL_TYPE (type);
7158 /* Never layout this decl. This will be done when its scope
7159 will be entered */
7160 decl = build_decl (VAR_DECL, name, real_type);
7161 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7162 LOCAL_FINAL (decl) = final_p;
7163 BLOCK_CHAIN_DECL (decl);
7165 /* If doing xreferencing, replace the line number with the WFL
7166 compound value */
7167 if (flag_emit_xref)
7168 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7170 /* Don't try to use an INIT statement when an error was found */
7171 if (init && java_error_count)
7172 init = NULL_TREE;
7174 /* Add the initialization function to the current function's code */
7175 if (init)
7177 /* Name might have been readjusted */
7178 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7179 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7180 java_method_add_stmt (current_function_decl,
7181 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7182 init));
7185 /* Setup dependency the type of the decl */
7186 if (must_chain)
7188 jdep *dep;
7189 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7190 dep = CLASSD_LAST (ctxp->classd_list);
7191 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7194 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7197 /* Called during parsing. Build decls from argument list. */
7199 static void
7200 source_start_java_method (fndecl)
7201 tree fndecl;
7203 tree tem;
7204 tree parm_decl;
7205 int i;
7207 if (!fndecl)
7208 return;
7210 current_function_decl = fndecl;
7212 /* New scope for the function */
7213 enter_block ();
7214 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7215 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7217 tree type = TREE_VALUE (tem);
7218 tree name = TREE_PURPOSE (tem);
7220 /* If type is incomplete. Create an incomplete decl and ask for
7221 the decl to be patched later */
7222 if (INCOMPLETE_TYPE_P (type))
7224 jdep *jdep;
7225 tree real_type = GET_REAL_TYPE (type);
7226 parm_decl = build_decl (PARM_DECL, name, real_type);
7227 type = obtain_incomplete_type (type);
7228 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7229 jdep = CLASSD_LAST (ctxp->classd_list);
7230 JDEP_MISC (jdep) = name;
7231 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7233 else
7234 parm_decl = build_decl (PARM_DECL, name, type);
7236 /* Remember if a local variable was declared final (via its
7237 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7238 if (ARG_FINAL_P (tem))
7240 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7241 LOCAL_FINAL (parm_decl) = 1;
7244 BLOCK_CHAIN_DECL (parm_decl);
7246 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7247 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7248 nreverse (tem);
7249 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7250 DECL_MAX_LOCALS (current_function_decl) = i;
7253 /* Called during parsing. Creates an artificial method declaration. */
7255 static tree
7256 create_artificial_method (class, flags, type, name, args)
7257 tree class;
7258 int flags;
7259 tree type, name, args;
7261 tree mdecl;
7263 java_parser_context_save_global ();
7264 lineno = 0;
7265 mdecl = make_node (FUNCTION_TYPE);
7266 TREE_TYPE (mdecl) = type;
7267 TYPE_ARG_TYPES (mdecl) = args;
7268 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7269 java_parser_context_restore_global ();
7270 DECL_ARTIFICIAL (mdecl) = 1;
7271 return mdecl;
7274 /* Starts the body if an artifical method. */
7276 static void
7277 start_artificial_method_body (mdecl)
7278 tree mdecl;
7280 DECL_SOURCE_LINE (mdecl) = 1;
7281 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7282 source_start_java_method (mdecl);
7283 enter_block ();
7286 static void
7287 end_artificial_method_body (mdecl)
7288 tree mdecl;
7290 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7291 It has to be evaluated first. (if mdecl is current_function_decl,
7292 we have an undefined behavior if no temporary variable is used.) */
7293 tree b = exit_block ();
7294 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7295 exit_block ();
7298 /* Terminate a function and expand its body. */
7300 static void
7301 source_end_java_method ()
7303 tree fndecl = current_function_decl;
7305 if (!fndecl)
7306 return;
7308 java_parser_context_save_global ();
7309 lineno = ctxp->last_ccb_indent1;
7311 /* Turn function bodies with only a NOP expr null, so they don't get
7312 generated at all and we won't get warnings when using the -W
7313 -Wall flags. */
7314 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7315 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7317 /* Generate function's code */
7318 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7319 && ! flag_emit_class_files
7320 && ! flag_emit_xref)
7321 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7323 /* pop out of its parameters */
7324 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7325 poplevel (1, 0, 1);
7326 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7328 /* Generate rtl for function exit. */
7329 if (! flag_emit_class_files && ! flag_emit_xref)
7331 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7332 expand_function_end (input_filename, lineno, 0);
7334 /* Run the optimizers and output assembler code for this function. */
7335 rest_of_compilation (fndecl);
7338 current_function_decl = NULL_TREE;
7339 java_parser_context_restore_global ();
7342 /* Record EXPR in the current function block. Complements compound
7343 expression second operand if necessary. */
7345 tree
7346 java_method_add_stmt (fndecl, expr)
7347 tree fndecl, expr;
7349 if (!GET_CURRENT_BLOCK (fndecl))
7350 return NULL_TREE;
7351 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7354 static tree
7355 add_stmt_to_block (b, type, stmt)
7356 tree b, type, stmt;
7358 tree body = BLOCK_EXPR_BODY (b), c;
7360 if (java_error_count)
7361 return body;
7363 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7364 return body;
7366 BLOCK_EXPR_BODY (b) = c;
7367 TREE_SIDE_EFFECTS (c) = 1;
7368 return c;
7371 /* Add STMT to EXISTING if possible, otherwise create a new
7372 COMPOUND_EXPR and add STMT to it. */
7374 static tree
7375 add_stmt_to_compound (existing, type, stmt)
7376 tree existing, type, stmt;
7378 if (existing)
7379 return build (COMPOUND_EXPR, type, existing, stmt);
7380 else
7381 return stmt;
7384 void java_layout_seen_class_methods ()
7386 tree previous_list = all_class_list;
7387 tree end = NULL_TREE;
7388 tree current;
7390 while (1)
7392 for (current = previous_list;
7393 current != end; current = TREE_CHAIN (current))
7394 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7396 if (previous_list != all_class_list)
7398 end = previous_list;
7399 previous_list = all_class_list;
7401 else
7402 break;
7406 void
7407 java_reorder_fields ()
7409 static tree stop_reordering = NULL_TREE;
7410 static int initialized_p;
7411 tree current;
7413 /* Register STOP_REORDERING with the garbage collector. */
7414 if (!initialized_p)
7416 ggc_add_tree_root (&stop_reordering, 1);
7417 initialized_p = 1;
7420 for (current = gclass_list; current; current = TREE_CHAIN (current))
7422 current_class = TREE_TYPE (TREE_VALUE (current));
7424 if (current_class == stop_reordering)
7425 break;
7427 /* Reverse the fields, but leave the dummy field in front.
7428 Fields are already ordered for Object and Class */
7429 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7430 && current_class != class_type_node)
7432 /* If the dummy field is there, reverse the right fields and
7433 just layout the type for proper fields offset */
7434 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7436 tree fields = TYPE_FIELDS (current_class);
7437 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7438 TYPE_SIZE (current_class) = NULL_TREE;
7440 /* We don't have a dummy field, we need to layout the class,
7441 after having reversed the fields */
7442 else
7444 TYPE_FIELDS (current_class) =
7445 nreverse (TYPE_FIELDS (current_class));
7446 TYPE_SIZE (current_class) = NULL_TREE;
7450 /* There are cases were gclass_list will be empty. */
7451 if (gclass_list)
7452 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7455 /* Layout the methods of all classes loaded in one way or another.
7456 Check methods of source parsed classes. Then reorder the
7457 fields and layout the classes or the type of all source parsed
7458 classes */
7460 void
7461 java_layout_classes ()
7463 tree current;
7464 int save_error_count = java_error_count;
7466 /* Layout the methods of all classes seen so far */
7467 java_layout_seen_class_methods ();
7468 java_parse_abort_on_error ();
7469 all_class_list = NULL_TREE;
7471 /* Then check the methods of all parsed classes */
7472 for (current = gclass_list; current; current = TREE_CHAIN (current))
7473 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7474 java_check_methods (TREE_VALUE (current));
7475 java_parse_abort_on_error ();
7477 for (current = gclass_list; current; current = TREE_CHAIN (current))
7479 current_class = TREE_TYPE (TREE_VALUE (current));
7480 layout_class (current_class);
7482 /* Error reported by the caller */
7483 if (java_error_count)
7484 return;
7487 /* We might have reloaded classes durign the process of laying out
7488 classes for code generation. We must layout the methods of those
7489 late additions, as constructor checks might use them */
7490 java_layout_seen_class_methods ();
7491 java_parse_abort_on_error ();
7494 /* Expand methods in the current set of classes rememebered for
7495 generation. */
7497 static void
7498 java_complete_expand_classes ()
7500 tree current;
7502 do_not_fold = flag_emit_xref;
7504 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7505 if (!INNER_CLASS_DECL_P (current))
7506 java_complete_expand_class (current);
7509 /* Expand the methods found in OUTER, starting first by OUTER's inner
7510 classes, if any. */
7512 static void
7513 java_complete_expand_class (outer)
7514 tree outer;
7516 tree inner_list;
7518 set_nested_class_simple_name_value (outer, 1); /* Set */
7520 /* We need to go after all inner classes and start expanding them,
7521 starting with most nested ones. We have to do that because nested
7522 classes might add functions to outer classes */
7524 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7525 inner_list; inner_list = TREE_CHAIN (inner_list))
7526 java_complete_expand_class (TREE_PURPOSE (inner_list));
7528 java_complete_expand_methods (outer);
7529 set_nested_class_simple_name_value (outer, 0); /* Reset */
7532 /* Expand methods registered in CLASS_DECL. The general idea is that
7533 we expand regular methods first. This allows us get an estimate on
7534 how outer context local alias fields are really used so we can add
7535 to the constructor just enough code to initialize them properly (it
7536 also lets us generate finit$ correctly.) Then we expand the
7537 constructors and then <clinit>. */
7539 static void
7540 java_complete_expand_methods (class_decl)
7541 tree class_decl;
7543 tree clinit, decl, first_decl;
7545 current_class = TREE_TYPE (class_decl);
7547 /* Find whether the class has final variables */
7548 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7549 if (FIELD_FINAL (decl))
7551 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7552 break;
7555 /* Initialize a new constant pool */
7556 init_outgoing_cpool ();
7558 /* Pre-expand <clinit> to figure whether we really need it or
7559 not. If we do need it, we pre-expand the static fields so they're
7560 ready to be used somewhere else. <clinit> will be fully expanded
7561 after we processed the constructors. */
7562 first_decl = TYPE_METHODS (current_class);
7563 clinit = maybe_generate_pre_expand_clinit (current_class);
7565 /* Then generate finit$ (if we need to) because constructors will
7566 try to use it.*/
7567 if (TYPE_FINIT_STMT_LIST (current_class))
7568 java_complete_expand_method (generate_finit (current_class));
7570 /* Then generate instinit$ (if we need to) because constructors will
7571 try to use it. */
7572 if (TYPE_II_STMT_LIST (current_class))
7573 java_complete_expand_method (generate_instinit (current_class));
7575 /* Now do the constructors */
7576 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7578 int no_body;
7580 if (!DECL_CONSTRUCTOR_P (decl))
7581 continue;
7583 no_body = !DECL_FUNCTION_BODY (decl);
7584 /* Don't generate debug info on line zero when expanding a
7585 generated constructor. */
7586 if (no_body)
7587 restore_line_number_status (1);
7589 /* Reset the final local variable assignment flags */
7590 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7591 reset_final_variable_local_assignment_flag (current_class);
7593 java_complete_expand_method (decl);
7595 /* Check for missed out final variable assignment */
7596 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7597 check_final_variable_local_assignment_flag (current_class, decl);
7599 if (no_body)
7600 restore_line_number_status (0);
7603 /* First, do the ordinary methods. */
7604 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7606 /* Ctors aren't part of this batch. */
7607 if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7608 continue;
7610 /* Skip abstract or native methods -- but do handle native
7611 methods when generating JNI stubs. */
7612 if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7614 DECL_FUNCTION_BODY (decl) = NULL_TREE;
7615 continue;
7618 if (METHOD_NATIVE (decl))
7620 tree body = build_jni_stub (decl);
7621 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7624 java_complete_expand_method (decl);
7627 /* If there is indeed a <clinit>, fully expand it now */
7628 if (clinit)
7630 /* Reset the final local variable assignment flags */
7631 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7632 reset_static_final_variable_assignment_flag (current_class);
7633 /* Prevent the use of `this' inside <clinit> */
7634 ctxp->explicit_constructor_p = 1;
7635 java_complete_expand_method (clinit);
7636 ctxp->explicit_constructor_p = 0;
7637 /* Check for missed out static final variable assignment */
7638 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7639 && !CLASS_INTERFACE (class_decl))
7640 check_static_final_variable_assignment_flag (current_class);
7643 /* We might have generated a class$ that we now want to expand */
7644 if (TYPE_DOT_CLASS (current_class))
7645 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7647 /* Now verify constructor circularity (stop after the first one we
7648 prove wrong.) */
7649 if (!CLASS_INTERFACE (class_decl))
7650 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7651 if (DECL_CONSTRUCTOR_P (decl)
7652 && verify_constructor_circularity (decl, decl))
7653 break;
7655 /* Final check on the initialization of final variables. */
7656 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7658 check_final_variable_global_assignment_flag (current_class);
7659 /* If we have an interface, check for uninitialized fields. */
7660 if (CLASS_INTERFACE (class_decl))
7661 check_static_final_variable_assignment_flag (current_class);
7664 /* Save the constant pool. We'll need to restore it later. */
7665 TYPE_CPOOL (current_class) = outgoing_cpool;
7668 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7669 safely used in some other methods/constructors. */
7671 static tree
7672 maybe_generate_pre_expand_clinit (class_type)
7673 tree class_type;
7675 tree current, mdecl;
7677 if (!TYPE_CLINIT_STMT_LIST (class_type))
7678 return NULL_TREE;
7680 /* Go through all static fields and pre expand them */
7681 for (current = TYPE_FIELDS (class_type); current;
7682 current = TREE_CHAIN (current))
7683 if (FIELD_STATIC (current))
7684 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7686 /* Then build the <clinit> method */
7687 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7688 clinit_identifier_node, end_params_node);
7689 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7690 mdecl, NULL_TREE);
7691 start_artificial_method_body (mdecl);
7693 /* We process the list of assignment we produced as the result of
7694 the declaration of initialized static field and add them as
7695 statement to the <clinit> method. */
7696 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7697 current = TREE_CHAIN (current))
7699 tree stmt = current;
7700 /* We build the assignment expression that will initialize the
7701 field to its value. There are strict rules on static
7702 initializers (8.5). FIXME */
7703 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7704 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7705 java_method_add_stmt (mdecl, stmt);
7708 end_artificial_method_body (mdecl);
7710 /* Now we want to place <clinit> as the last method (because we need
7711 it at least for interface so that it doesn't interfere with the
7712 dispatch table based lookup. */
7713 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7715 current = TREE_CHAIN (TYPE_METHODS (class_type));
7716 TYPE_METHODS (class_type) = current;
7718 while (TREE_CHAIN (current))
7719 current = TREE_CHAIN (current);
7721 TREE_CHAIN (current) = mdecl;
7722 TREE_CHAIN (mdecl) = NULL_TREE;
7725 return mdecl;
7728 /* Analyzes a method body and look for something that isn't a
7729 MODIFY_EXPR with a constant value. */
7731 static int
7732 analyze_clinit_body (bbody)
7733 tree bbody;
7735 while (bbody)
7736 switch (TREE_CODE (bbody))
7738 case BLOCK:
7739 bbody = BLOCK_EXPR_BODY (bbody);
7740 break;
7742 case EXPR_WITH_FILE_LOCATION:
7743 bbody = EXPR_WFL_NODE (bbody);
7744 break;
7746 case COMPOUND_EXPR:
7747 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7748 return 1;
7749 bbody = TREE_OPERAND (bbody, 1);
7750 break;
7752 case MODIFY_EXPR:
7753 /* Return 0 if the operand is constant, 1 otherwise. */
7754 return ! TREE_CONSTANT (TREE_OPERAND (bbody, 1));
7756 default:
7757 return 1;
7759 return 0;
7763 /* See whether we could get rid of <clinit>. Criteria are: all static
7764 final fields have constant initial values and the body of <clinit>
7765 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7767 static int
7768 maybe_yank_clinit (mdecl)
7769 tree mdecl;
7771 tree type, current;
7772 tree fbody, bbody;
7773 int found = 0;
7775 if (!DECL_CLINIT_P (mdecl))
7776 return 0;
7778 /* If the body isn't empty, then we keep <clinit>. Note that if
7779 we're emitting classfiles, this isn't enough not to rule it
7780 out. */
7781 fbody = DECL_FUNCTION_BODY (mdecl);
7782 bbody = BLOCK_EXPR_BODY (fbody);
7783 if (bbody && bbody != error_mark_node)
7784 bbody = BLOCK_EXPR_BODY (bbody);
7785 else
7786 return 0;
7787 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7788 return 0;
7790 type = DECL_CONTEXT (mdecl);
7791 current = TYPE_FIELDS (type);
7793 for (current = (current ? TREE_CHAIN (current) : current);
7794 current; current = TREE_CHAIN (current))
7796 tree f_init;
7798 /* We're not interested in non static field */
7799 if (!FIELD_STATIC (current))
7800 continue;
7802 /* Anything that isn't String or a basic type is ruled out -- or
7803 if we know how to deal with it (when doing things natively) we
7804 should generated an empty <clinit> so that SUID are computed
7805 correctly. */
7806 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7807 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7808 break;
7810 f_init = DECL_INITIAL (current);
7811 /* If we're emitting native code, we want static final fields to
7812 have constant initializers. If we don't meet these
7813 conditions, we keep <clinit> */
7814 if (!flag_emit_class_files
7815 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7816 break;
7817 /* If we're emitting bytecode, we want static fields to have
7818 constant initializers or no initializer. If we don't meet
7819 these conditions, we keep <clinit> */
7820 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7821 break;
7824 /* Now we analyze the method body and look for something that
7825 isn't a MODIFY_EXPR */
7826 if (bbody == empty_stmt_node)
7827 found = 0;
7828 else
7829 found = analyze_clinit_body (bbody);
7831 if (current || found)
7832 return 0;
7834 /* Get rid of <clinit> in the class' list of methods */
7835 if (TYPE_METHODS (type) == mdecl)
7836 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7837 else
7838 for (current = TYPE_METHODS (type); current;
7839 current = TREE_CHAIN (current))
7840 if (TREE_CHAIN (current) == mdecl)
7842 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7843 break;
7846 return 1;
7849 /* Install the argument from MDECL. Suitable to completion and
7850 expansion of mdecl's body. */
7852 static void
7853 start_complete_expand_method (mdecl)
7854 tree mdecl;
7856 tree tem, *ptr;
7858 pushlevel (1); /* Prepare for a parameter push */
7859 ptr = &DECL_ARGUMENTS (mdecl);
7860 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7862 while (tem)
7864 tree next = TREE_CHAIN (tem);
7865 tree type = TREE_TYPE (tem);
7866 if (PROMOTE_PROTOTYPES
7867 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7868 && INTEGRAL_TYPE_P (type))
7869 type = integer_type_node;
7870 DECL_ARG_TYPE (tem) = type;
7871 layout_decl (tem, 0);
7872 pushdecl (tem);
7873 *ptr = tem;
7874 ptr = &TREE_CHAIN (tem);
7875 tem = next;
7877 *ptr = NULL_TREE;
7878 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7879 lineno = DECL_SOURCE_LINE_FIRST (mdecl);
7880 build_result_decl (mdecl);
7884 /* Complete and expand a method. */
7886 static void
7887 java_complete_expand_method (mdecl)
7888 tree mdecl;
7890 tree fbody, block_body, exception_copy;
7892 current_function_decl = mdecl;
7893 /* Fix constructors before expanding them */
7894 if (DECL_CONSTRUCTOR_P (mdecl))
7895 fix_constructors (mdecl);
7897 /* Expand functions that have a body */
7898 if (!DECL_FUNCTION_BODY (mdecl))
7899 return;
7901 fbody = DECL_FUNCTION_BODY (mdecl);
7902 block_body = BLOCK_EXPR_BODY (fbody);
7903 exception_copy = NULL_TREE;
7905 current_function_decl = mdecl;
7907 if (! quiet_flag)
7908 fprintf (stderr, " [%s.",
7909 lang_printable_name (DECL_CONTEXT (mdecl), 0));
7910 announce_function (mdecl);
7911 if (! quiet_flag)
7912 fprintf (stderr, "]");
7914 /* Prepare the function for tree completion */
7915 start_complete_expand_method (mdecl);
7917 /* Install the current this */
7918 current_this = (!METHOD_STATIC (mdecl) ?
7919 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7921 /* Purge the `throws' list of unchecked exceptions. If we're doing
7922 xref, save a copy of the list and re-install it later. */
7923 if (flag_emit_xref)
7924 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7925 purge_unchecked_exceptions (mdecl);
7927 /* Install exceptions thrown with `throws' */
7928 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7930 if (block_body != NULL_TREE)
7932 block_body = java_complete_tree (block_body);
7934 /* Before we check initialization, attached all class initialization
7935 variable to the block_body */
7936 hash_traverse (&DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
7937 attach_init_test_initialization_flags, block_body);
7939 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7941 unsigned int state = check_for_initialization (block_body);
7943 /* Go through all the flags marking the initialization of
7944 static variables and see whether they're definitively
7945 assigned, in which case the type is remembered as
7946 definitively initialized in MDECL. */
7947 if (STATIC_CLASS_INIT_OPT_P ())
7949 hash_traverse (&DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
7950 attach_initialized_static_class, (PTR)&state);
7952 /* Always register the context as properly initialized in
7953 MDECL. This used with caution helps removing extra
7954 initialization of self. */
7955 if (METHOD_STATIC (mdecl))
7956 hash_lookup (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
7957 (hash_table_key) DECL_CONTEXT (mdecl),
7958 TRUE, NULL);
7961 ctxp->explicit_constructor_p = 0;
7964 BLOCK_EXPR_BODY (fbody) = block_body;
7966 /* If we saw a return but couldn't evaluate it properly, we'll have
7967 an error_mark_node here. */
7968 if (block_body != error_mark_node
7969 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7970 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7971 && !flag_emit_xref)
7972 missing_return_error (current_function_decl);
7974 /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
7975 maybe_yank_clinit (mdecl);
7977 /* Pop the current level, with special measures if we found errors. */
7978 if (java_error_count)
7979 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7980 poplevel (1, 0, 1);
7982 /* Pop the exceptions and sanity check */
7983 POP_EXCEPTIONS();
7984 if (currently_caught_type_list)
7985 abort ();
7987 /* Restore the copy of the list of exceptions if emitting xrefs. */
7988 if (flag_emit_xref)
7989 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7992 /* For with each class for which there's code to generate. */
7994 static void
7995 java_expand_method_bodies (class)
7996 tree class;
7998 tree decl;
7999 for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8001 if (!DECL_FUNCTION_BODY (decl))
8002 continue;
8004 current_function_decl = decl;
8006 /* It's time to assign the variable flagging static class
8007 initialization based on which classes invoked static methods
8008 are definitely initializing. This should be flagged. */
8009 if (STATIC_CLASS_INIT_OPT_P ())
8010 hash_traverse (&DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl),
8011 adjust_init_test_initialization, NULL);
8013 /* Prepare the function for RTL expansion */
8014 start_complete_expand_method (decl);
8016 /* Expand function start, generate initialization flag
8017 assignment, and handle synchronized methods. */
8018 complete_start_java_method (decl);
8020 /* Expand the rest of the function body and terminate
8021 expansion. */
8022 source_end_java_method ();
8028 /* This section of the code deals with accessing enclosing context
8029 fields either directly by using the relevant access to this$<n> or
8030 by invoking an access method crafted for that purpose. */
8032 /* Build the necessary access from an inner class to an outer
8033 class. This routine could be optimized to cache previous result
8034 (decl, current_class and returned access). When an access method
8035 needs to be generated, it always takes the form of a read. It might
8036 be later turned into a write by calling outer_field_access_fix. */
8038 static tree
8039 build_outer_field_access (id, decl)
8040 tree id, decl;
8042 tree access = NULL_TREE;
8043 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8044 tree decl_ctx = DECL_CONTEXT (decl);
8046 /* If the immediate enclosing context of the current class is the
8047 field decl's class or inherits from it; build the access as
8048 `this$<n>.<field>'. Note that we will break the `private' barrier
8049 if we're not emitting bytecodes. */
8050 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8051 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8053 tree thisn = build_current_thisn (current_class);
8054 access = make_qualified_primary (build_wfl_node (thisn),
8055 id, EXPR_WFL_LINECOL (id));
8057 /* Otherwise, generate access methods to outer this and access the
8058 field (either using an access method or by direct access.) */
8059 else
8061 int lc = EXPR_WFL_LINECOL (id);
8063 /* Now we chain the required number of calls to the access$0 to
8064 get a hold to the enclosing instance we need, and then we
8065 build the field access. */
8066 access = build_access_to_thisn (current_class, decl_ctx, lc);
8068 /* If the field is private and we're generating bytecode, then
8069 we generate an access method */
8070 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8072 tree name = build_outer_field_access_methods (decl);
8073 access = build_outer_field_access_expr (lc, decl_ctx,
8074 name, access, NULL_TREE);
8076 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8077 Once again we break the `private' access rule from a foreign
8078 class. */
8079 else
8080 access = make_qualified_primary (access, id, lc);
8082 return resolve_expression_name (access, NULL);
8085 /* Return a non zero value if NODE describes an outer field inner
8086 access. */
8088 static int
8089 outer_field_access_p (type, decl)
8090 tree type, decl;
8092 if (!INNER_CLASS_TYPE_P (type)
8093 || TREE_CODE (decl) != FIELD_DECL
8094 || DECL_CONTEXT (decl) == type)
8095 return 0;
8097 /* If the inner class extends the declaration context of the field
8098 we're try to acces, then this isn't an outer field access */
8099 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8100 return 0;
8102 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8103 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8105 if (type == DECL_CONTEXT (decl))
8106 return 1;
8108 if (!DECL_CONTEXT (TYPE_NAME (type)))
8110 /* Before we give up, see whether the field is inherited from
8111 the enclosing context we're considering. */
8112 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8113 return 1;
8114 break;
8118 return 0;
8121 /* Return a non zero value if NODE represents an outer field inner
8122 access that was been already expanded. As a side effect, it returns
8123 the name of the field being accessed and the argument passed to the
8124 access function, suitable for a regeneration of the access method
8125 call if necessary. */
8127 static int
8128 outer_field_expanded_access_p (node, name, arg_type, arg)
8129 tree node, *name, *arg_type, *arg;
8131 int identified = 0;
8133 if (TREE_CODE (node) != CALL_EXPR)
8134 return 0;
8136 /* Well, gcj generates slightly different tree nodes when compiling
8137 to native or bytecodes. It's the case for function calls. */
8139 if (flag_emit_class_files
8140 && TREE_CODE (node) == CALL_EXPR
8141 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8142 identified = 1;
8143 else if (!flag_emit_class_files)
8145 node = TREE_OPERAND (node, 0);
8147 if (node && TREE_OPERAND (node, 0)
8148 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8150 node = TREE_OPERAND (node, 0);
8151 if (TREE_OPERAND (node, 0)
8152 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8153 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8154 (DECL_NAME (TREE_OPERAND (node, 0)))))
8155 identified = 1;
8159 if (identified && name && arg_type && arg)
8161 tree argument = TREE_OPERAND (node, 1);
8162 *name = DECL_NAME (TREE_OPERAND (node, 0));
8163 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8164 *arg = TREE_VALUE (argument);
8166 return identified;
8169 /* Detect in NODE an outer field read access from an inner class and
8170 transform it into a write with RHS as an argument. This function is
8171 called from the java_complete_lhs when an assignment to a LHS can
8172 be identified. */
8174 static tree
8175 outer_field_access_fix (wfl, node, rhs)
8176 tree wfl, node, rhs;
8178 tree name, arg_type, arg;
8180 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8182 /* At any rate, check whether we're trying to assign a value to
8183 a final. */
8184 tree accessed = (JDECL_P (node) ? node :
8185 (TREE_CODE (node) == COMPONENT_REF ?
8186 TREE_OPERAND (node, 1) : node));
8187 if (check_final_assignment (accessed, wfl))
8188 return error_mark_node;
8190 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8191 arg_type, name, arg, rhs);
8192 return java_complete_tree (node);
8194 return NULL_TREE;
8197 /* Construct the expression that calls an access method:
8198 <type>.access$<n>(<arg1> [, <arg2>]);
8200 ARG2 can be NULL and will be omitted in that case. It will denote a
8201 read access. */
8203 static tree
8204 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
8205 int lc;
8206 tree type, access_method_name, arg1, arg2;
8208 tree args, cn, access;
8210 args = arg1 ? arg1 :
8211 build_wfl_node (build_current_thisn (current_class));
8212 args = build_tree_list (NULL_TREE, args);
8214 if (arg2)
8215 args = tree_cons (NULL_TREE, arg2, args);
8217 access = build_method_invocation (build_wfl_node (access_method_name), args);
8218 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8219 return make_qualified_primary (cn, access, lc);
8222 static tree
8223 build_new_access_id ()
8225 static int access_n_counter = 1;
8226 char buffer [128];
8228 sprintf (buffer, "access$%d", access_n_counter++);
8229 return get_identifier (buffer);
8232 /* Create the static access functions for the outer field DECL. We define a
8233 read:
8234 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8235 return inst$.field;
8237 and a write access:
8238 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8239 TREE_TYPE (<field>) value$) {
8240 return inst$.field = value$;
8242 We should have a usage flags on the DECL so we can lazily turn the ones
8243 we're using for code generation. FIXME.
8246 static tree
8247 build_outer_field_access_methods (decl)
8248 tree decl;
8250 tree id, args, stmt, mdecl;
8252 if (FIELD_INNER_ACCESS_P (decl))
8253 return FIELD_INNER_ACCESS (decl);
8255 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8257 /* Create the identifier and a function named after it. */
8258 id = build_new_access_id ();
8260 /* The identifier is marked as bearing the name of a generated write
8261 access function for outer field accessed from inner classes. */
8262 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8264 /* Create the read access */
8265 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8266 TREE_CHAIN (args) = end_params_node;
8267 stmt = make_qualified_primary (build_wfl_node (inst_id),
8268 build_wfl_node (DECL_NAME (decl)), 0);
8269 stmt = build_return (0, stmt);
8270 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8271 TREE_TYPE (decl), id, args, stmt);
8272 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8274 /* Create the write access method. No write access for final variable */
8275 if (!FIELD_FINAL (decl))
8277 args = build_tree_list (inst_id,
8278 build_pointer_type (DECL_CONTEXT (decl)));
8279 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8280 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8281 stmt = make_qualified_primary (build_wfl_node (inst_id),
8282 build_wfl_node (DECL_NAME (decl)), 0);
8283 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8284 build_wfl_node (wpv_id)));
8285 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8286 TREE_TYPE (decl), id,
8287 args, stmt);
8289 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8291 /* Return the access name */
8292 return FIELD_INNER_ACCESS (decl) = id;
8295 /* Build an field access method NAME. */
8297 static tree
8298 build_outer_field_access_method (class, type, name, args, body)
8299 tree class, type, name, args, body;
8301 tree saved_current_function_decl, mdecl;
8303 /* Create the method */
8304 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8305 fix_method_argument_names (args, mdecl);
8306 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8308 /* Attach the method body. */
8309 saved_current_function_decl = current_function_decl;
8310 start_artificial_method_body (mdecl);
8311 java_method_add_stmt (mdecl, body);
8312 end_artificial_method_body (mdecl);
8313 current_function_decl = saved_current_function_decl;
8315 return mdecl;
8319 /* This section deals with building access function necessary for
8320 certain kinds of method invocation from inner classes. */
8322 static tree
8323 build_outer_method_access_method (decl)
8324 tree decl;
8326 tree saved_current_function_decl, mdecl;
8327 tree args = NULL_TREE, call_args = NULL_TREE;
8328 tree carg, id, body, class;
8329 char buffer [80];
8330 int parm_id_count = 0;
8332 /* Test this abort with an access to a private field */
8333 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8334 abort ();
8336 /* Check the cache first */
8337 if (DECL_FUNCTION_INNER_ACCESS (decl))
8338 return DECL_FUNCTION_INNER_ACCESS (decl);
8340 class = DECL_CONTEXT (decl);
8342 /* Obtain an access identifier and mark it */
8343 id = build_new_access_id ();
8344 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8346 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8347 /* Create the arguments, as much as the original */
8348 for (; carg && carg != end_params_node;
8349 carg = TREE_CHAIN (carg))
8351 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8352 args = chainon (args, build_tree_list (get_identifier (buffer),
8353 TREE_VALUE (carg)));
8355 args = chainon (args, end_params_node);
8357 /* Create the method */
8358 mdecl = create_artificial_method (class, ACC_STATIC,
8359 TREE_TYPE (TREE_TYPE (decl)), id, args);
8360 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8361 /* There is a potential bug here. We should be able to use
8362 fix_method_argument_names, but then arg names get mixed up and
8363 eventually a constructor will have its this$0 altered and the
8364 outer context won't be assignment properly. The test case is
8365 stub.java FIXME */
8366 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8368 /* Attach the method body. */
8369 saved_current_function_decl = current_function_decl;
8370 start_artificial_method_body (mdecl);
8372 /* The actual method invocation uses the same args. When invoking a
8373 static methods that way, we don't want to skip the first
8374 argument. */
8375 carg = args;
8376 if (!METHOD_STATIC (decl))
8377 carg = TREE_CHAIN (carg);
8378 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8379 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8380 call_args);
8382 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8383 call_args);
8384 if (!METHOD_STATIC (decl))
8385 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8386 body, 0);
8387 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8388 body = build_return (0, body);
8389 java_method_add_stmt (mdecl,body);
8390 end_artificial_method_body (mdecl);
8391 current_function_decl = saved_current_function_decl;
8393 /* Back tag the access function so it know what it accesses */
8394 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8396 /* Tag the current method so it knows it has an access generated */
8397 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8401 /* This section of the code deals with building expressions to access
8402 the enclosing instance of an inner class. The enclosing instance is
8403 kept in a generated field called this$<n>, with <n> being the
8404 inner class nesting level (starting from 0.) */
8406 /* Build an access to a given this$<n>, always chaining access call to
8407 others. Access methods to this$<n> are build on the fly if
8408 necessary. This CAN'T be used to solely access this$<n-1> from
8409 this$<n> (which alway yield to special cases and optimization, see
8410 for example build_outer_field_access). */
8412 static tree
8413 build_access_to_thisn (from, to, lc)
8414 tree from, to;
8415 int lc;
8417 tree access = NULL_TREE;
8419 while (from != to)
8421 if (!access)
8423 access = build_current_thisn (from);
8424 access = build_wfl_node (access);
8426 else
8428 tree access0_wfl, cn;
8430 maybe_build_thisn_access_method (from);
8431 access0_wfl = build_wfl_node (access0_identifier_node);
8432 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8433 EXPR_WFL_LINECOL (access0_wfl) = lc;
8434 access = build_tree_list (NULL_TREE, access);
8435 access = build_method_invocation (access0_wfl, access);
8436 access = make_qualified_primary (cn, access, lc);
8439 /* if FROM isn't an inter class, that's fine, we've done
8440 enough. What we're looking for can be accessed from there. */
8441 from = DECL_CONTEXT (TYPE_NAME (from));
8442 if (!from)
8443 break;
8444 from = TREE_TYPE (from);
8446 return access;
8449 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8450 is returned if nothing needs to be generated. Otherwise, the method
8451 generated and a method decl is returned.
8453 NOTE: These generated methods should be declared in a class file
8454 attribute so that they can't be referred to directly. */
8456 static tree
8457 maybe_build_thisn_access_method (type)
8458 tree type;
8460 tree mdecl, args, stmt, rtype;
8461 tree saved_current_function_decl;
8463 /* If TYPE is a top-level class, no access method is required.
8464 If there already is such an access method, bail out. */
8465 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8466 return NULL_TREE;
8468 /* We generate the method. The method looks like:
8469 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8471 args = build_tree_list (inst_id, build_pointer_type (type));
8472 TREE_CHAIN (args) = end_params_node;
8473 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8474 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8475 access0_identifier_node, args);
8476 fix_method_argument_names (args, mdecl);
8477 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8478 stmt = build_current_thisn (type);
8479 stmt = make_qualified_primary (build_wfl_node (inst_id),
8480 build_wfl_node (stmt), 0);
8481 stmt = build_return (0, stmt);
8483 saved_current_function_decl = current_function_decl;
8484 start_artificial_method_body (mdecl);
8485 java_method_add_stmt (mdecl, stmt);
8486 end_artificial_method_body (mdecl);
8487 current_function_decl = saved_current_function_decl;
8489 CLASS_ACCESS0_GENERATED_P (type) = 1;
8491 return mdecl;
8494 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8495 the first level of innerclassing. this$1 for the next one, etc...
8496 This function can be invoked with TYPE to NULL, available and then
8497 has to count the parser context. */
8499 static tree
8500 build_current_thisn (type)
8501 tree type;
8503 static int saved_i = -1;
8504 static tree saved_thisn = NULL_TREE;
8505 static tree saved_type = NULL_TREE;
8506 static int saved_type_i = 0;
8507 static int initialized_p;
8508 tree decl;
8509 char buffer [80];
8510 int i = 0;
8512 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8513 if (!initialized_p)
8515 ggc_add_tree_root (&saved_thisn, 1);
8516 ggc_add_tree_root (&saved_type, 1);
8517 initialized_p = 1;
8520 if (type)
8522 if (type == saved_type)
8523 i = saved_type_i;
8524 else
8526 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8527 decl; decl = DECL_CONTEXT (decl), i++)
8530 saved_type = type;
8531 saved_type_i = i;
8534 else
8535 i = list_length (GET_CPC_LIST ())-2;
8537 if (i == saved_i)
8538 return saved_thisn;
8540 sprintf (buffer, "this$%d", i);
8541 saved_i = i;
8542 saved_thisn = get_identifier (buffer);
8543 return saved_thisn;
8546 /* Return the assignement to the hidden enclosing context `this$<n>'
8547 by the second incoming parameter to the innerclass constructor. The
8548 form used is `this.this$<n> = this$<n>;'. */
8550 static tree
8551 build_thisn_assign ()
8553 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8555 tree thisn = build_current_thisn (current_class);
8556 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8557 build_wfl_node (thisn), 0);
8558 tree rhs = build_wfl_node (thisn);
8559 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8560 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8562 return NULL_TREE;
8566 /* Building the synthetic `class$' used to implement the `.class' 1.1
8567 extension for non primitive types. This method looks like:
8569 static Class class$(String type) throws NoClassDefFoundError
8571 try {return (java.lang.Class.forName (String));}
8572 catch (ClassNotFoundException e) {
8573 throw new NoClassDefFoundError(e.getMessage());}
8574 } */
8576 static tree
8577 build_dot_class_method (class)
8578 tree class;
8580 #define BWF(S) build_wfl_node (get_identifier ((S)))
8581 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8582 tree args, tmp, saved_current_function_decl, mdecl;
8583 tree stmt, throw_stmt;
8585 static tree get_message_wfl, type_parm_wfl;
8587 if (!get_message_wfl)
8589 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8590 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8591 ggc_add_tree_root (&get_message_wfl, 1);
8592 ggc_add_tree_root (&type_parm_wfl, 1);
8595 /* Build the arguments */
8596 args = build_tree_list (get_identifier ("type$"),
8597 build_pointer_type (string_type_node));
8598 TREE_CHAIN (args) = end_params_node;
8600 /* Build the qualified name java.lang.Class.forName */
8601 tmp = MQN (MQN (MQN (BWF ("java"),
8602 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8603 load_class (class_not_found_type_node, 1);
8604 load_class (no_class_def_found_type_node, 1);
8606 /* Create the "class$" function */
8607 mdecl = create_artificial_method (class, ACC_STATIC,
8608 build_pointer_type (class_type_node),
8609 classdollar_identifier_node, args);
8610 DECL_FUNCTION_THROWS (mdecl) =
8611 build_tree_list (NULL_TREE, no_class_def_found_type_node);
8613 /* We start by building the try block. We need to build:
8614 return (java.lang.Class.forName (type)); */
8615 stmt = build_method_invocation (tmp,
8616 build_tree_list (NULL_TREE, type_parm_wfl));
8617 stmt = build_return (0, stmt);
8619 /* Now onto the catch block. We start by building the expression
8620 throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8621 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8622 get_message_wfl, 0);
8623 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8625 /* Build new NoClassDefFoundError (_.getMessage) */
8626 throw_stmt = build_new_invocation
8627 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8628 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8630 /* Build the throw, (it's too early to use BUILD_THROW) */
8631 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8633 /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8634 stmt = encapsulate_with_try_catch (0, class_not_found_type_node,
8635 stmt, throw_stmt);
8637 fix_method_argument_names (args, mdecl);
8638 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8639 saved_current_function_decl = current_function_decl;
8640 start_artificial_method_body (mdecl);
8641 java_method_add_stmt (mdecl, stmt);
8642 end_artificial_method_body (mdecl);
8643 current_function_decl = saved_current_function_decl;
8644 TYPE_DOT_CLASS (class) = mdecl;
8646 return mdecl;
8649 static tree
8650 build_dot_class_method_invocation (type)
8651 tree type;
8653 tree sig_id, s;
8655 if (TYPE_ARRAY_P (type))
8656 sig_id = build_java_signature (type);
8657 else
8658 sig_id = DECL_NAME (TYPE_NAME (type));
8660 s = build_string (IDENTIFIER_LENGTH (sig_id),
8661 IDENTIFIER_POINTER (sig_id));
8662 return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8663 build_tree_list (NULL_TREE, s));
8666 /* This section of the code deals with constructor. */
8668 /* Craft a body for default constructor. Patch existing constructor
8669 bodies with call to super() and field initialization statements if
8670 necessary. */
8672 static void
8673 fix_constructors (mdecl)
8674 tree mdecl;
8676 tree iii; /* Instance Initializer Invocation */
8677 tree body = DECL_FUNCTION_BODY (mdecl);
8678 tree thisn_assign, compound = NULL_TREE;
8679 tree class_type = DECL_CONTEXT (mdecl);
8681 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8682 return;
8683 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8685 if (!body)
8687 /* It is an error for the compiler to generate a default
8688 constructor if the superclass doesn't have a constructor that
8689 takes no argument, or the same args for an anonymous class */
8690 if (verify_constructor_super (mdecl))
8692 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8693 tree save = DECL_NAME (mdecl);
8694 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8695 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8696 parse_error_context
8697 (lookup_cl (TYPE_NAME (class_type)),
8698 "No constructor matching `%s' found in class `%s'",
8699 lang_printable_name (mdecl, 0), n);
8700 DECL_NAME (mdecl) = save;
8703 /* The constructor body must be crafted by hand. It's the
8704 constructor we defined when we realize we didn't have the
8705 CLASSNAME() constructor */
8706 start_artificial_method_body (mdecl);
8708 /* Insert an assignment to the this$<n> hidden field, if
8709 necessary */
8710 if ((thisn_assign = build_thisn_assign ()))
8711 java_method_add_stmt (mdecl, thisn_assign);
8713 /* We don't generate a super constructor invocation if we're
8714 compiling java.lang.Object. build_super_invocation takes care
8715 of that. */
8716 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8718 /* FIXME */
8719 if ((iii = build_instinit_invocation (class_type)))
8720 java_method_add_stmt (mdecl, iii);
8722 end_artificial_method_body (mdecl);
8724 /* Search for an explicit constructor invocation */
8725 else
8727 int found = 0;
8728 int invokes_this = 0;
8729 tree found_call = NULL_TREE;
8730 tree main_block = BLOCK_EXPR_BODY (body);
8732 while (body)
8733 switch (TREE_CODE (body))
8735 case CALL_EXPR:
8736 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8737 if (CALL_THIS_CONSTRUCTOR_P (body))
8738 invokes_this = 1;
8739 body = NULL_TREE;
8740 break;
8741 case COMPOUND_EXPR:
8742 case EXPR_WITH_FILE_LOCATION:
8743 found_call = body;
8744 body = TREE_OPERAND (body, 0);
8745 break;
8746 case BLOCK:
8747 found_call = body;
8748 body = BLOCK_EXPR_BODY (body);
8749 break;
8750 default:
8751 found = 0;
8752 body = NULL_TREE;
8755 /* Generate the assignment to this$<n>, if necessary */
8756 if ((thisn_assign = build_thisn_assign ()))
8757 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8759 /* The constructor is missing an invocation of super() */
8760 if (!found)
8761 compound = add_stmt_to_compound (compound, NULL_TREE,
8762 build_super_invocation (mdecl));
8763 /* Explicit super() invokation should take place before the
8764 instance initializer blocks. */
8765 else
8767 compound = add_stmt_to_compound (compound, NULL_TREE,
8768 TREE_OPERAND (found_call, 0));
8769 TREE_OPERAND (found_call, 0) = empty_stmt_node;
8772 /* Insert the instance initializer block right after. */
8773 if (!invokes_this && (iii = build_instinit_invocation (class_type)))
8774 compound = add_stmt_to_compound (compound, NULL_TREE, iii);
8776 /* Fix the constructor main block if we're adding extra stmts */
8777 if (compound)
8779 compound = add_stmt_to_compound (compound, NULL_TREE,
8780 BLOCK_EXPR_BODY (main_block));
8781 BLOCK_EXPR_BODY (main_block) = compound;
8786 /* Browse constructors in the super class, searching for a constructor
8787 that doesn't take any argument. Return 0 if one is found, 1
8788 otherwise. If the current class is an anonymous inner class, look
8789 for something that has the same signature. */
8791 static int
8792 verify_constructor_super (mdecl)
8793 tree mdecl;
8795 tree class = CLASSTYPE_SUPER (current_class);
8796 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8797 tree sdecl;
8799 if (!class)
8800 return 0;
8802 if (ANONYMOUS_CLASS_P (current_class))
8804 tree mdecl_arg_type;
8805 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8806 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8807 if (DECL_CONSTRUCTOR_P (sdecl))
8809 tree m_arg_type;
8810 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8811 if (super_inner)
8812 arg_type = TREE_CHAIN (arg_type);
8813 for (m_arg_type = mdecl_arg_type;
8814 (arg_type != end_params_node
8815 && m_arg_type != end_params_node);
8816 arg_type = TREE_CHAIN (arg_type),
8817 m_arg_type = TREE_CHAIN (m_arg_type))
8818 if (!valid_method_invocation_conversion_p
8819 (TREE_VALUE (arg_type),
8820 TREE_VALUE (m_arg_type)))
8821 break;
8823 if (arg_type == end_params_node && m_arg_type == end_params_node)
8824 return 0;
8827 else
8829 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8831 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8832 if (super_inner)
8833 arg = TREE_CHAIN (arg);
8834 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8835 return 0;
8838 return 1;
8841 /* Generate code for all context remembered for code generation. */
8843 void
8844 java_expand_classes ()
8846 int save_error_count = 0;
8847 static struct parser_ctxt *cur_ctxp = NULL;
8849 java_parse_abort_on_error ();
8850 if (!(ctxp = ctxp_for_generation))
8851 return;
8852 java_layout_classes ();
8853 java_parse_abort_on_error ();
8855 cur_ctxp = ctxp_for_generation;
8856 for (; cur_ctxp; cur_ctxp = cur_ctxp->next)
8858 ctxp = cur_ctxp;
8859 input_filename = ctxp->filename;
8860 lang_init_source (2); /* Error msgs have method prototypes */
8861 java_complete_expand_classes (); /* Complete and expand classes */
8862 java_parse_abort_on_error ();
8864 input_filename = main_input_filename;
8866 /* Find anonymous classes and expand their constructor, now they
8867 have been fixed. */
8868 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8870 tree current;
8871 ctxp = cur_ctxp;
8872 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8874 current_class = TREE_TYPE (current);
8875 if (ANONYMOUS_CLASS_P (current_class))
8877 tree d;
8878 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8880 if (DECL_CONSTRUCTOR_P (d))
8882 restore_line_number_status (1);
8883 java_complete_expand_method (d);
8884 restore_line_number_status (0);
8885 break; /* We now there are no other ones */
8892 /* If we've found error at that stage, don't try to generate
8893 anything, unless we're emitting xrefs or checking the syntax only
8894 (but not using -fsyntax-only for the purpose of generating
8895 bytecode. */
8896 if (java_error_count && !flag_emit_xref
8897 && (!flag_syntax_only && !flag_emit_class_files))
8898 return;
8900 /* Now things are stable, go for generation of the class data. */
8901 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8903 tree current;
8904 ctxp = cur_ctxp;
8905 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8907 current_class = TREE_TYPE (current);
8908 outgoing_cpool = TYPE_CPOOL (current_class);
8909 if (flag_emit_class_files)
8910 write_classfile (current_class);
8911 if (flag_emit_xref)
8912 expand_xref (current_class);
8913 else if (! flag_syntax_only)
8915 java_expand_method_bodies (current_class);
8916 finish_class ();
8922 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8923 a tree list node containing RIGHT. Fore coming RIGHTs will be
8924 chained to this hook. LOCATION contains the location of the
8925 separating `.' operator. */
8927 static tree
8928 make_qualified_primary (primary, right, location)
8929 tree primary, right;
8930 int location;
8932 tree wfl;
8934 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8935 wfl = build_wfl_wrap (primary, location);
8936 else
8938 wfl = primary;
8939 /* If wfl wasn't qualified, we build a first anchor */
8940 if (!EXPR_WFL_QUALIFICATION (wfl))
8941 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8944 /* And chain them */
8945 EXPR_WFL_LINECOL (right) = location;
8946 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8947 PRIMARY_P (wfl) = 1;
8948 return wfl;
8951 /* Simple merge of two name separated by a `.' */
8953 static tree
8954 merge_qualified_name (left, right)
8955 tree left, right;
8957 tree node;
8958 if (!left && !right)
8959 return NULL_TREE;
8961 if (!left)
8962 return right;
8964 if (!right)
8965 return left;
8967 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8968 IDENTIFIER_LENGTH (left));
8969 obstack_1grow (&temporary_obstack, '.');
8970 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8971 IDENTIFIER_LENGTH (right));
8972 node = get_identifier (obstack_base (&temporary_obstack));
8973 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8974 QUALIFIED_P (node) = 1;
8975 return node;
8978 /* Merge the two parts of a qualified name into LEFT. Set the
8979 location information of the resulting node to LOCATION, usually
8980 inherited from the location information of the `.' operator. */
8982 static tree
8983 make_qualified_name (left, right, location)
8984 tree left, right;
8985 int location;
8987 #ifdef USE_COMPONENT_REF
8988 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8989 EXPR_WFL_LINECOL (node) = location;
8990 return node;
8991 #else
8992 tree left_id = EXPR_WFL_NODE (left);
8993 tree right_id = EXPR_WFL_NODE (right);
8994 tree wfl, merge;
8996 merge = merge_qualified_name (left_id, right_id);
8998 /* Left wasn't qualified and is now qualified */
8999 if (!QUALIFIED_P (left_id))
9001 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9002 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9003 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9006 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9007 EXPR_WFL_LINECOL (wfl) = location;
9008 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9010 EXPR_WFL_NODE (left) = merge;
9011 return left;
9012 #endif
9015 /* Extract the last identifier component of the qualified in WFL. The
9016 last identifier is removed from the linked list */
9018 static tree
9019 cut_identifier_in_qualified (wfl)
9020 tree wfl;
9022 tree q;
9023 tree previous = NULL_TREE;
9024 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9025 if (!TREE_CHAIN (q))
9027 if (!previous)
9028 /* Operating on a non qualified qualified WFL. */
9029 abort ();
9031 TREE_CHAIN (previous) = NULL_TREE;
9032 return TREE_PURPOSE (q);
9036 /* Resolve the expression name NAME. Return its decl. */
9038 static tree
9039 resolve_expression_name (id, orig)
9040 tree id;
9041 tree *orig;
9043 tree name = EXPR_WFL_NODE (id);
9044 tree decl;
9046 /* 6.5.5.1: Simple expression names */
9047 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9049 /* 15.13.1: NAME can appear within the scope of a local variable
9050 declaration */
9051 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9052 return decl;
9054 /* 15.13.1: NAME can appear within a class declaration */
9055 else
9057 decl = lookup_field_wrapper (current_class, name);
9058 if (decl)
9060 tree access = NULL_TREE;
9061 int fs = FIELD_STATIC (decl);
9063 /* If we're accessing an outer scope local alias, make
9064 sure we change the name of the field we're going to
9065 build access to. */
9066 if (FIELD_LOCAL_ALIAS_USED (decl))
9067 name = DECL_NAME (decl);
9069 /* Instance variable (8.3.1.1) can't appear within
9070 static method, static initializer or initializer for
9071 a static variable. */
9072 if (!fs && METHOD_STATIC (current_function_decl))
9074 static_ref_err (id, name, current_class);
9075 return error_mark_node;
9077 /* Instance variables can't appear as an argument of
9078 an explicit constructor invocation */
9079 if (!fs && ctxp->explicit_constructor_p
9080 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9082 parse_error_context
9083 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9084 return error_mark_node;
9087 /* If we're processing an inner class and we're trying
9088 to access a field belonging to an outer class, build
9089 the access to the field */
9090 if (!fs && outer_field_access_p (current_class, decl))
9092 if (CLASS_STATIC (TYPE_NAME (current_class)))
9094 static_ref_err (id, DECL_NAME (decl), current_class);
9095 return error_mark_node;
9097 access = build_outer_field_access (id, decl);
9098 if (orig)
9099 *orig = access;
9100 return access;
9103 /* Otherwise build what it takes to access the field */
9104 access = build_field_ref ((fs ? NULL_TREE : current_this),
9105 DECL_CONTEXT (decl), name);
9106 if (fs)
9107 access = maybe_build_class_init_for_field (decl, access);
9108 /* We may be asked to save the real field access node */
9109 if (orig)
9110 *orig = access;
9111 /* And we return what we got */
9112 return access;
9114 /* Fall down to error report on undefined variable */
9117 /* 6.5.5.2 Qualified Expression Names */
9118 else
9120 if (orig)
9121 *orig = NULL_TREE;
9122 qualify_ambiguous_name (id);
9123 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9124 /* 15.10.2: Accessing Superclass Members using super */
9125 return resolve_field_access (id, orig, NULL);
9128 /* We've got an error here */
9129 if (INNER_CLASS_TYPE_P (current_class))
9130 parse_error_context (id,
9131 "Local variable `%s' can't be accessed from within the inner class `%s' unless it is declared final",
9132 IDENTIFIER_POINTER (name),
9133 IDENTIFIER_POINTER (DECL_NAME
9134 (TYPE_NAME (current_class))));
9135 else
9136 parse_error_context (id, "Undefined variable `%s'",
9137 IDENTIFIER_POINTER (name));
9139 return error_mark_node;
9142 static void
9143 static_ref_err (wfl, field_id, class_type)
9144 tree wfl, field_id, class_type;
9146 parse_error_context
9147 (wfl,
9148 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
9149 IDENTIFIER_POINTER (field_id),
9150 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9153 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
9154 We return something suitable to generate the field access. We also
9155 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9156 recipient's address can be null. */
9158 static tree
9159 resolve_field_access (qual_wfl, field_decl, field_type)
9160 tree qual_wfl;
9161 tree *field_decl, *field_type;
9163 int is_static = 0;
9164 tree field_ref;
9165 tree decl, where_found, type_found;
9167 if (resolve_qualified_expression_name (qual_wfl, &decl,
9168 &where_found, &type_found))
9169 return error_mark_node;
9171 /* Resolve the LENGTH field of an array here */
9172 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9173 && type_found && TYPE_ARRAY_P (type_found)
9174 && ! flag_emit_class_files && ! flag_emit_xref)
9176 tree length = build_java_array_length_access (where_found);
9177 field_ref = length;
9179 /* In case we're dealing with a static array, we need to
9180 initialize its class before the array length can be fetched.
9181 It's also a good time to create a DECL_RTL for the field if
9182 none already exists, otherwise if the field was declared in a
9183 class found in an external file and hasn't been (and won't
9184 be) accessed for its value, none will be created. */
9185 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9187 build_static_field_ref (where_found);
9188 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9191 /* We might have been trying to resolve field.method(). In which
9192 case, the resolution is over and decl is the answer */
9193 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9194 field_ref = decl;
9195 else if (JDECL_P (decl))
9197 if (!type_found)
9198 type_found = DECL_CONTEXT (decl);
9199 is_static = FIELD_STATIC (decl);
9200 field_ref = build_field_ref ((is_static && !flag_emit_xref?
9201 NULL_TREE : where_found),
9202 type_found, DECL_NAME (decl));
9203 if (field_ref == error_mark_node)
9204 return error_mark_node;
9205 if (is_static)
9206 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9208 else
9209 field_ref = decl;
9211 if (field_decl)
9212 *field_decl = decl;
9213 if (field_type)
9214 *field_type = (QUAL_DECL_TYPE (decl) ?
9215 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9216 return field_ref;
9219 /* If NODE is an access to f static field, strip out the class
9220 initialization part and return the field decl, otherwise, return
9221 NODE. */
9223 static tree
9224 strip_out_static_field_access_decl (node)
9225 tree node;
9227 if (TREE_CODE (node) == COMPOUND_EXPR)
9229 tree op1 = TREE_OPERAND (node, 1);
9230 if (TREE_CODE (op1) == COMPOUND_EXPR)
9232 tree call = TREE_OPERAND (op1, 0);
9233 if (TREE_CODE (call) == CALL_EXPR
9234 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9235 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9236 == soft_initclass_node)
9237 return TREE_OPERAND (op1, 1);
9239 else if (JDECL_P (op1))
9240 return op1;
9242 return node;
9245 /* 6.5.5.2: Qualified Expression Names */
9247 static int
9248 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9249 tree wfl;
9250 tree *found_decl, *type_found, *where_found;
9252 int from_type = 0; /* Field search initiated from a type */
9253 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9254 int previous_call_static = 0;
9255 int is_static;
9256 tree decl = NULL_TREE, type = NULL_TREE, q;
9257 /* For certain for of inner class instantiation */
9258 tree saved_current, saved_this;
9259 #define RESTORE_THIS_AND_CURRENT_CLASS \
9260 { current_class = saved_current; current_this = saved_this;}
9262 *type_found = *where_found = NULL_TREE;
9264 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9266 tree qual_wfl = QUAL_WFL (q);
9267 tree ret_decl; /* for EH checking */
9268 int location; /* for EH checking */
9270 /* 15.10.1 Field Access Using a Primary */
9271 switch (TREE_CODE (qual_wfl))
9273 case CALL_EXPR:
9274 case NEW_CLASS_EXPR:
9275 /* If the access to the function call is a non static field,
9276 build the code to access it. */
9277 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9279 decl = maybe_access_field (decl, *where_found,
9280 DECL_CONTEXT (decl));
9281 if (decl == error_mark_node)
9282 return 1;
9285 /* And code for the function call */
9286 if (complete_function_arguments (qual_wfl))
9287 return 1;
9289 /* We might have to setup a new current class and a new this
9290 for the search of an inner class, relative to the type of
9291 a expression resolved as `decl'. The current values are
9292 saved and restored shortly after */
9293 saved_current = current_class;
9294 saved_this = current_this;
9295 if (decl
9296 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9297 || from_qualified_this))
9299 /* If we still have `from_qualified_this', we have the form
9300 <T>.this.f() and we need to build <T>.this */
9301 if (from_qualified_this)
9303 decl = build_access_to_thisn (current_class, type, 0);
9304 decl = java_complete_tree (decl);
9305 type = TREE_TYPE (TREE_TYPE (decl));
9307 current_class = type;
9308 current_this = decl;
9309 from_qualified_this = 0;
9312 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9313 CALL_USING_SUPER (qual_wfl) = 1;
9314 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9315 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9316 *where_found = patch_method_invocation (qual_wfl, decl, type,
9317 from_super,
9318 &is_static, &ret_decl);
9319 if (*where_found == error_mark_node)
9321 RESTORE_THIS_AND_CURRENT_CLASS;
9322 return 1;
9324 *type_found = type = QUAL_DECL_TYPE (*where_found);
9326 /* If we're creating an inner class instance, check for that
9327 an enclosing instance is in scope */
9328 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9329 && INNER_ENCLOSING_SCOPE_CHECK (type))
9331 parse_error_context
9332 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9333 lang_printable_name (type, 0),
9334 (!current_this ? "" :
9335 "; an explicit one must be provided when creating this inner class"));
9336 RESTORE_THIS_AND_CURRENT_CLASS;
9337 return 1;
9340 /* In case we had to change then to resolve a inner class
9341 instantiation using a primary qualified by a `new' */
9342 RESTORE_THIS_AND_CURRENT_CLASS;
9344 /* EH check. No check on access$<n> functions */
9345 if (location
9346 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9347 (DECL_NAME (current_function_decl)))
9348 check_thrown_exceptions (location, ret_decl);
9350 /* If the previous call was static and this one is too,
9351 build a compound expression to hold the two (because in
9352 that case, previous function calls aren't transported as
9353 forcoming function's argument. */
9354 if (previous_call_static && is_static)
9356 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9357 TREE_SIDE_EFFECTS (decl) = 1;
9359 else
9361 previous_call_static = is_static;
9362 decl = *where_found;
9364 from_type = 0;
9365 continue;
9367 case NEW_ARRAY_EXPR:
9368 case NEW_ANONYMOUS_ARRAY_EXPR:
9369 *where_found = decl = java_complete_tree (qual_wfl);
9370 if (decl == error_mark_node)
9371 return 1;
9372 *type_found = type = QUAL_DECL_TYPE (decl);
9373 continue;
9375 case CONVERT_EXPR:
9376 *where_found = decl = java_complete_tree (qual_wfl);
9377 if (decl == error_mark_node)
9378 return 1;
9379 *type_found = type = QUAL_DECL_TYPE (decl);
9380 from_cast = 1;
9381 continue;
9383 case CONDITIONAL_EXPR:
9384 case STRING_CST:
9385 case MODIFY_EXPR:
9386 *where_found = decl = java_complete_tree (qual_wfl);
9387 if (decl == error_mark_node)
9388 return 1;
9389 *type_found = type = QUAL_DECL_TYPE (decl);
9390 continue;
9392 case ARRAY_REF:
9393 /* If the access to the function call is a non static field,
9394 build the code to access it. */
9395 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9397 decl = maybe_access_field (decl, *where_found, type);
9398 if (decl == error_mark_node)
9399 return 1;
9401 /* And code for the array reference expression */
9402 decl = java_complete_tree (qual_wfl);
9403 if (decl == error_mark_node)
9404 return 1;
9405 type = QUAL_DECL_TYPE (decl);
9406 continue;
9408 case PLUS_EXPR:
9409 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9410 return 1;
9411 if ((type = patch_string (decl)))
9412 decl = type;
9413 *where_found = QUAL_RESOLUTION (q) = decl;
9414 *type_found = type = TREE_TYPE (decl);
9415 break;
9417 case CLASS_LITERAL:
9418 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9419 return 1;
9420 *where_found = QUAL_RESOLUTION (q) = decl;
9421 *type_found = type = TREE_TYPE (decl);
9422 break;
9424 default:
9425 /* Fix for -Wall Just go to the next statement. Don't
9426 continue */
9427 break;
9430 /* If we fall here, we weren't processing a (static) function call. */
9431 previous_call_static = 0;
9433 /* It can be the keyword THIS */
9434 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9435 && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9437 if (!current_this)
9439 parse_error_context
9440 (wfl, "Keyword `this' used outside allowed context");
9441 return 1;
9443 if (ctxp->explicit_constructor_p
9444 && type == current_class)
9446 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9447 return 1;
9449 /* We have to generate code for intermediate acess */
9450 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9452 *where_found = decl = current_this;
9453 *type_found = type = QUAL_DECL_TYPE (decl);
9455 /* We're trying to access the this from somewhere else. Make sure
9456 it's allowed before doing so. */
9457 else
9459 if (!enclosing_context_p (type, current_class))
9461 char *p = xstrdup (lang_printable_name (type, 0));
9462 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9463 p, p,
9464 lang_printable_name (current_class, 0));
9465 free (p);
9466 return 1;
9468 from_qualified_this = 1;
9469 /* If there's nothing else after that, we need to
9470 produce something now, otherwise, the section of the
9471 code that needs to produce <T>.this will generate
9472 what is necessary. */
9473 if (!TREE_CHAIN (q))
9475 decl = build_access_to_thisn (current_class, type, 0);
9476 *where_found = decl = java_complete_tree (decl);
9477 *type_found = type = TREE_TYPE (decl);
9481 from_type = 0;
9482 continue;
9485 /* 15.10.2 Accessing Superclass Members using SUPER */
9486 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9487 && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9489 tree node;
9490 /* Check on the restricted use of SUPER */
9491 if (METHOD_STATIC (current_function_decl)
9492 || current_class == object_type_node)
9494 parse_error_context
9495 (wfl, "Keyword `super' used outside allowed context");
9496 return 1;
9498 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9499 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9500 CLASSTYPE_SUPER (current_class),
9501 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9502 *where_found = decl = java_complete_tree (node);
9503 if (decl == error_mark_node)
9504 return 1;
9505 *type_found = type = QUAL_DECL_TYPE (decl);
9506 from_super = from_type = 1;
9507 continue;
9510 /* 15.13.1: Can't search for field name in packages, so we
9511 assume a variable/class name was meant. */
9512 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9514 tree name = resolve_package (wfl, &q);
9515 if (name)
9517 tree list;
9518 *where_found = decl = resolve_no_layout (name, qual_wfl);
9519 /* We want to be absolutely sure that the class is laid
9520 out. We're going to search something inside it. */
9521 *type_found = type = TREE_TYPE (decl);
9522 layout_class (type);
9523 from_type = 1;
9525 /* Fix them all the way down, if any are left. */
9526 if (q)
9528 list = TREE_CHAIN (q);
9529 while (list)
9531 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9532 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9533 list = TREE_CHAIN (list);
9537 else
9539 if (from_super || from_cast)
9540 parse_error_context
9541 ((from_cast ? qual_wfl : wfl),
9542 "No variable `%s' defined in class `%s'",
9543 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9544 lang_printable_name (type, 0));
9545 else
9546 parse_error_context
9547 (qual_wfl, "Undefined variable or class name: `%s'",
9548 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9549 return 1;
9553 /* We have a type name. It's been already resolved when the
9554 expression was qualified. */
9555 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9557 if (!(decl = QUAL_RESOLUTION (q)))
9558 return 1; /* Error reported already */
9560 /* Sneak preview. If next we see a `new', we're facing a
9561 qualification with resulted in a type being selected
9562 instead of a field. Report the error */
9563 if(TREE_CHAIN (q)
9564 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9566 parse_error_context (qual_wfl, "Undefined variable `%s'",
9567 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9568 return 1;
9571 if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9573 parse_error_context
9574 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9575 java_accstring_lookup (get_access_flags_from_decl (decl)),
9576 GET_TYPE_NAME (type),
9577 IDENTIFIER_POINTER (DECL_NAME (decl)),
9578 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9579 return 1;
9581 check_deprecation (qual_wfl, decl);
9583 type = TREE_TYPE (decl);
9584 from_type = 1;
9586 /* We resolve and expression name */
9587 else
9589 tree field_decl = NULL_TREE;
9591 /* If there exists an early resolution, use it. That occurs
9592 only once and we know that there are more things to
9593 come. Don't do that when processing something after SUPER
9594 (we need more thing to be put in place below */
9595 if (!from_super && QUAL_RESOLUTION (q))
9597 decl = QUAL_RESOLUTION (q);
9598 if (!type)
9600 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9602 if (current_this)
9603 *where_found = current_this;
9604 else
9606 static_ref_err (qual_wfl, DECL_NAME (decl),
9607 current_class);
9608 return 1;
9610 if (outer_field_access_p (current_class, decl))
9611 decl = build_outer_field_access (qual_wfl, decl);
9613 else
9615 *where_found = TREE_TYPE (decl);
9616 if (TREE_CODE (*where_found) == POINTER_TYPE)
9617 *where_found = TREE_TYPE (*where_found);
9622 /* We have to search for a field, knowing the type of its
9623 container. The flag FROM_TYPE indicates that we resolved
9624 the last member of the expression as a type name, which
9625 means that for the resolution of this field, we'll look
9626 for other errors than if it was resolved as a member of
9627 an other field. */
9628 else
9630 int is_static;
9631 tree field_decl_type; /* For layout */
9633 if (!from_type && !JREFERENCE_TYPE_P (type))
9635 parse_error_context
9636 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9637 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9638 lang_printable_name (type, 0),
9639 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9640 return 1;
9643 field_decl = lookup_field_wrapper (type,
9644 EXPR_WFL_NODE (qual_wfl));
9646 /* Maybe what we're trying to access to is an inner
9647 class, only if decl is a TYPE_DECL. */
9648 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9650 tree ptr, inner_decl;
9652 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9653 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9654 if (inner_decl)
9656 check_inner_class_access (inner_decl, decl, qual_wfl);
9657 type = TREE_TYPE (inner_decl);
9658 decl = inner_decl;
9659 from_type = 1;
9660 continue;
9664 if (field_decl == NULL_TREE)
9666 parse_error_context
9667 (qual_wfl, "No variable `%s' defined in type `%s'",
9668 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9669 GET_TYPE_NAME (type));
9670 return 1;
9672 if (field_decl == error_mark_node)
9673 return 1;
9675 /* Layout the type of field_decl, since we may need
9676 it. Don't do primitive types or loaded classes. The
9677 situation of non primitive arrays may not handled
9678 properly here. FIXME */
9679 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9680 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9681 else
9682 field_decl_type = TREE_TYPE (field_decl);
9683 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9684 && !CLASS_LOADED_P (field_decl_type)
9685 && !TYPE_ARRAY_P (field_decl_type))
9686 resolve_and_layout (field_decl_type, NULL_TREE);
9688 /* Check on accessibility here */
9689 if (not_accessible_p (current_class, field_decl,
9690 DECL_CONTEXT (field_decl), from_super))
9692 parse_error_context
9693 (qual_wfl,
9694 "Can't access %s field `%s.%s' from `%s'",
9695 java_accstring_lookup
9696 (get_access_flags_from_decl (field_decl)),
9697 GET_TYPE_NAME (type),
9698 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9699 IDENTIFIER_POINTER
9700 (DECL_NAME (TYPE_NAME (current_class))));
9701 return 1;
9703 check_deprecation (qual_wfl, field_decl);
9705 /* There are things to check when fields are accessed
9706 from type. There are no restrictions on a static
9707 declaration of the field when it is accessed from an
9708 interface */
9709 is_static = FIELD_STATIC (field_decl);
9710 if (!from_super && from_type
9711 && !TYPE_INTERFACE_P (type)
9712 && !is_static
9713 && (current_function_decl
9714 && METHOD_STATIC (current_function_decl)))
9716 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9717 return 1;
9719 from_cast = from_super = 0;
9721 /* It's an access from a type but it isn't static, we
9722 make it relative to `this'. */
9723 if (!is_static && from_type)
9724 decl = current_this;
9726 /* If we need to generate something to get a proper
9727 handle on what this field is accessed from, do it
9728 now. */
9729 if (!is_static)
9731 decl = maybe_access_field (decl, *where_found, *type_found);
9732 if (decl == error_mark_node)
9733 return 1;
9736 /* We want to keep the location were found it, and the type
9737 we found. */
9738 *where_found = decl;
9739 *type_found = type;
9741 /* Generate the correct expression for field access from
9742 qualified this */
9743 if (from_qualified_this)
9745 field_decl = build_outer_field_access (qual_wfl, field_decl);
9746 from_qualified_this = 0;
9749 /* This is the decl found and eventually the next one to
9750 search from */
9751 decl = field_decl;
9753 from_type = 0;
9754 type = QUAL_DECL_TYPE (decl);
9756 /* Sneak preview. If decl is qualified by a `new', report
9757 the error here to be accurate on the peculiar construct */
9758 if (TREE_CHAIN (q)
9759 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9760 && !JREFERENCE_TYPE_P (type))
9762 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9763 lang_printable_name (type, 0));
9764 return 1;
9767 /* `q' might have changed due to a after package resolution
9768 re-qualification */
9769 if (!q)
9770 break;
9772 *found_decl = decl;
9773 return 0;
9776 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9777 can't be accessed from REFERENCE (a record type). If MEMBER
9778 features a protected access, we then use WHERE which, if non null,
9779 holds the type of MEMBER's access that is checked against
9780 6.6.2.1. This function should be used when decl is a field or a
9781 method. */
9783 static int
9784 not_accessible_p (reference, member, where, from_super)
9785 tree reference, member;
9786 tree where;
9787 int from_super;
9789 int access_flag = get_access_flags_from_decl (member);
9791 /* Inner classes are processed by check_inner_class_access */
9792 if (INNER_CLASS_TYPE_P (reference))
9793 return 0;
9795 /* Access always granted for members declared public */
9796 if (access_flag & ACC_PUBLIC)
9797 return 0;
9799 /* Check access on protected members */
9800 if (access_flag & ACC_PROTECTED)
9802 /* Access granted if it occurs from within the package
9803 containing the class in which the protected member is
9804 declared */
9805 if (class_in_current_package (DECL_CONTEXT (member)))
9806 return 0;
9808 /* If accessed with the form `super.member', then access is granted */
9809 if (from_super)
9810 return 0;
9812 /* If where is active, access was made through a
9813 qualifier. Access is granted if the type of the qualifier is
9814 or is a sublass of the type the access made from (6.6.2.1.) */
9815 if (where && !inherits_from_p (reference, where))
9816 return 1;
9818 /* Otherwise, access is granted if occuring from the class where
9819 member is declared or a subclass of it. Find the right
9820 context to perform the check */
9821 if (PURE_INNER_CLASS_TYPE_P (reference))
9823 while (INNER_CLASS_TYPE_P (reference))
9825 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9826 return 0;
9827 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9830 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9831 return 0;
9832 return 1;
9835 /* Check access on private members. Access is granted only if it
9836 occurs from within the class in which it is declared -- that does
9837 it for innerclasses too. */
9838 if (access_flag & ACC_PRIVATE)
9840 if (reference == DECL_CONTEXT (member))
9841 return 0;
9842 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9843 return 0;
9844 return 1;
9847 /* Default access are permitted only when occuring within the
9848 package in which the type (REFERENCE) is declared. In other words,
9849 REFERENCE is defined in the current package */
9850 if (ctxp->package)
9851 return !class_in_current_package (reference);
9853 /* Otherwise, access is granted */
9854 return 0;
9857 /* Test deprecated decl access. */
9858 static void
9859 check_deprecation (wfl, decl)
9860 tree wfl, decl;
9862 const char *file = DECL_SOURCE_FILE (decl);
9863 /* Complain if the field is deprecated and the file it was defined
9864 in isn't compiled at the same time the file which contains its
9865 use is */
9866 if (DECL_DEPRECATED (decl)
9867 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9869 char the [20];
9870 switch (TREE_CODE (decl))
9872 case FUNCTION_DECL:
9873 strcpy (the, "method");
9874 break;
9875 case FIELD_DECL:
9876 strcpy (the, "field");
9877 break;
9878 case TYPE_DECL:
9879 strcpy (the, "class");
9880 break;
9881 default:
9882 abort ();
9884 parse_warning_context
9885 (wfl, "The %s `%s' in class `%s' has been deprecated",
9886 the, lang_printable_name (decl, 0),
9887 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9891 /* Returns 1 if class was declared in the current package, 0 otherwise */
9893 static int
9894 class_in_current_package (class)
9895 tree class;
9897 static tree cache = NULL_TREE;
9898 int qualified_flag;
9899 tree left;
9901 if (cache == class)
9902 return 1;
9904 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9906 /* If the current package is empty and the name of CLASS is
9907 qualified, class isn't in the current package. If there is a
9908 current package and the name of the CLASS is not qualified, class
9909 isn't in the current package */
9910 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9911 return 0;
9913 /* If there is not package and the name of CLASS isn't qualified,
9914 they belong to the same unnamed package */
9915 if (!ctxp->package && !qualified_flag)
9916 return 1;
9918 /* Compare the left part of the name of CLASS with the package name */
9919 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9920 if (ctxp->package == left)
9922 static int initialized_p;
9923 /* Register CACHE with the garbage collector. */
9924 if (!initialized_p)
9926 ggc_add_tree_root (&cache, 1);
9927 initialized_p = 1;
9930 cache = class;
9931 return 1;
9933 return 0;
9936 /* This function may generate code to access DECL from WHERE. This is
9937 done only if certain conditions meet. */
9939 static tree
9940 maybe_access_field (decl, where, type)
9941 tree decl, where, type;
9943 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9944 && !FIELD_STATIC (decl))
9945 decl = build_field_ref (where ? where : current_this,
9946 (type ? type : DECL_CONTEXT (decl)),
9947 DECL_NAME (decl));
9948 return decl;
9951 /* Build a method invocation, by patching PATCH. If non NULL
9952 and according to the situation, PRIMARY and WHERE may be
9953 used. IS_STATIC is set to 1 if the invoked function is static. */
9955 static tree
9956 patch_method_invocation (patch, primary, where, from_super,
9957 is_static, ret_decl)
9958 tree patch, primary, where;
9959 int from_super;
9960 int *is_static;
9961 tree *ret_decl;
9963 tree wfl = TREE_OPERAND (patch, 0);
9964 tree args = TREE_OPERAND (patch, 1);
9965 tree name = EXPR_WFL_NODE (wfl);
9966 tree list;
9967 int is_static_flag = 0;
9968 int is_super_init = 0;
9969 tree this_arg = NULL_TREE;
9970 int is_array_clone_call = 0;
9972 /* Should be overriden if everything goes well. Otherwise, if
9973 something fails, it should keep this value. It stop the
9974 evaluation of a bogus assignment. See java_complete_tree,
9975 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9976 evaluating an assignment */
9977 TREE_TYPE (patch) = error_mark_node;
9979 /* Since lookup functions are messing with line numbers, save the
9980 context now. */
9981 java_parser_context_save_global ();
9983 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9985 /* Resolution of qualified name, excluding constructors */
9986 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9988 tree identifier, identifier_wfl, type, resolved;
9989 /* Extract the last IDENTIFIER of the qualified
9990 expression. This is a wfl and we will use it's location
9991 data during error report. */
9992 identifier_wfl = cut_identifier_in_qualified (wfl);
9993 identifier = EXPR_WFL_NODE (identifier_wfl);
9995 /* Given the context, IDENTIFIER is syntactically qualified
9996 as a MethodName. We need to qualify what's before */
9997 qualify_ambiguous_name (wfl);
9998 resolved = resolve_field_access (wfl, NULL, NULL);
10000 if (resolved == error_mark_node)
10001 PATCH_METHOD_RETURN_ERROR ();
10003 type = GET_SKIP_TYPE (resolved);
10004 resolve_and_layout (type, NULL_TREE);
10006 if (JPRIMITIVE_TYPE_P (type))
10008 parse_error_context
10009 (identifier_wfl,
10010 "Can't invoke a method on primitive type `%s'",
10011 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10012 PATCH_METHOD_RETURN_ERROR ();
10015 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10016 args = nreverse (args);
10018 /* We're resolving a call from a type */
10019 if (TREE_CODE (resolved) == TYPE_DECL)
10021 if (CLASS_INTERFACE (resolved))
10023 parse_error_context
10024 (identifier_wfl,
10025 "Can't make static reference to method `%s' in interface `%s'",
10026 IDENTIFIER_POINTER (identifier),
10027 IDENTIFIER_POINTER (name));
10028 PATCH_METHOD_RETURN_ERROR ();
10030 if (list && !METHOD_STATIC (list))
10032 char *fct_name = xstrdup (lang_printable_name (list, 0));
10033 parse_error_context
10034 (identifier_wfl,
10035 "Can't make static reference to method `%s %s' in class `%s'",
10036 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10037 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10038 free (fct_name);
10039 PATCH_METHOD_RETURN_ERROR ();
10042 else
10043 this_arg = primary = resolved;
10045 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10046 is_array_clone_call = 1;
10048 /* IDENTIFIER_WFL will be used to report any problem further */
10049 wfl = identifier_wfl;
10051 /* Resolution of simple names, names generated after a primary: or
10052 constructors */
10053 else
10055 tree class_to_search = NULL_TREE;
10056 int lc; /* Looking for Constructor */
10058 /* We search constructor in their target class */
10059 if (CALL_CONSTRUCTOR_P (patch))
10061 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10062 class_to_search = EXPR_WFL_NODE (wfl);
10063 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10064 this_identifier_node)
10065 class_to_search = NULL_TREE;
10066 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10067 super_identifier_node)
10069 is_super_init = 1;
10070 if (CLASSTYPE_SUPER (current_class))
10071 class_to_search =
10072 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10073 else
10075 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10076 PATCH_METHOD_RETURN_ERROR ();
10080 /* Class to search is NULL if we're searching the current one */
10081 if (class_to_search)
10083 class_to_search = resolve_and_layout (class_to_search, wfl);
10085 if (!class_to_search)
10087 parse_error_context
10088 (wfl, "Class `%s' not found in type declaration",
10089 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10090 PATCH_METHOD_RETURN_ERROR ();
10093 /* Can't instantiate an abstract class, but we can
10094 invoke it's constructor. It's use within the `new'
10095 context is denied here. */
10096 if (CLASS_ABSTRACT (class_to_search)
10097 && TREE_CODE (patch) == NEW_CLASS_EXPR)
10099 parse_error_context
10100 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
10101 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10102 PATCH_METHOD_RETURN_ERROR ();
10105 class_to_search = TREE_TYPE (class_to_search);
10107 else
10108 class_to_search = current_class;
10109 lc = 1;
10111 /* This is a regular search in the local class, unless an
10112 alternate class is specified. */
10113 else
10115 if (where != NULL_TREE)
10116 class_to_search = where;
10117 else if (QUALIFIED_P (name))
10118 class_to_search = current_class;
10119 else
10121 class_to_search = current_class;
10123 for (;;)
10125 if (has_method (class_to_search, name))
10126 break;
10127 if (! INNER_CLASS_TYPE_P (class_to_search))
10129 parse_error_context (wfl,
10130 "No method named `%s' in scope",
10131 IDENTIFIER_POINTER (name));
10132 PATCH_METHOD_RETURN_ERROR ();
10134 class_to_search
10135 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10138 lc = 0;
10141 /* NAME is a simple identifier or comes from a primary. Search
10142 in the class whose declaration contain the method being
10143 invoked. */
10144 resolve_and_layout (class_to_search, NULL_TREE);
10146 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10147 /* Don't continue if no method were found, as the next statement
10148 can't be executed then. */
10149 if (!list)
10150 PATCH_METHOD_RETURN_ERROR ();
10152 if (TYPE_ARRAY_P (class_to_search)
10153 && DECL_NAME (list) == get_identifier ("clone"))
10154 is_array_clone_call = 1;
10156 /* Check for static reference if non static methods */
10157 if (check_for_static_method_reference (wfl, patch, list,
10158 class_to_search, primary))
10159 PATCH_METHOD_RETURN_ERROR ();
10161 /* Check for inner classes creation from illegal contexts */
10162 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10163 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10164 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
10166 parse_error_context
10167 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
10168 lang_printable_name (class_to_search, 0),
10169 (!current_this ? "" :
10170 "; an explicit one must be provided when creating this inner class"));
10171 PATCH_METHOD_RETURN_ERROR ();
10174 /* Non static methods are called with the current object extra
10175 argument. If patch a `new TYPE()', the argument is the value
10176 returned by the object allocator. If method is resolved as a
10177 primary, use the primary otherwise use the current THIS. */
10178 args = nreverse (args);
10179 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10181 this_arg = primary ? primary : current_this;
10183 /* If we're using an access method, things are different.
10184 There are two familly of cases:
10186 1) We're not generating bytecodes:
10188 - LIST is non static. It's invocation is transformed from
10189 x(a1,...,an) into this$<n>.x(a1,....an).
10190 - LIST is static. It's invocation is transformed from
10191 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10193 2) We're generating bytecodes:
10195 - LIST is non static. It's invocation is transformed from
10196 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10197 - LIST is static. It's invocation is transformed from
10198 x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10200 Of course, this$<n> can be abitrary complex, ranging from
10201 this$0 (the immediate outer context) to
10202 access$0(access$0(...(this$0))).
10204 maybe_use_access_method returns a non zero value if the
10205 this_arg has to be moved into the (then generated) stub
10206 argument list. In the meantime, the selected function
10207 might have be replaced by a generated stub. */
10208 if (!primary &&
10209 maybe_use_access_method (is_super_init, &list, &this_arg))
10211 args = tree_cons (NULL_TREE, this_arg, args);
10212 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10217 /* Merge point of all resolution schemes. If we have nothing, this
10218 is an error, already signaled */
10219 if (!list)
10220 PATCH_METHOD_RETURN_ERROR ();
10222 /* Check accessibility, position the is_static flag, build and
10223 return the call */
10224 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10225 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10226 NULL_TREE), from_super)
10227 /* Calls to clone() on array types are permitted as a special-case. */
10228 && !is_array_clone_call)
10230 const char *fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10231 const char *access =
10232 java_accstring_lookup (get_access_flags_from_decl (list));
10233 const char *klass =
10234 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10235 const char *refklass =
10236 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10237 const char *what = (DECL_CONSTRUCTOR_P (list)
10238 ? "constructor" : "method");
10239 /* FIXME: WFL yields the wrong message here but I don't know
10240 what else to use. */
10241 parse_error_context (wfl,
10242 "Can't access %s %s `%s.%s' from `%s'",
10243 access, what, klass, fct_name, refklass);
10244 PATCH_METHOD_RETURN_ERROR ();
10246 check_deprecation (wfl, list);
10248 /* If invoking a innerclass constructor, there are hidden parameters
10249 to pass */
10250 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10251 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10253 /* And make sure we add the accessed local variables to be saved
10254 in field aliases. */
10255 args = build_alias_initializer_parameter_list
10256 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10258 /* Secretly pass the current_this/primary as a second argument */
10259 if (primary || current_this)
10261 tree extra_arg;
10262 tree this_type = (current_this ?
10263 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10264 /* Method's (list) enclosing context */
10265 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10266 /* If we have a primary, use it. */
10267 if (primary)
10268 extra_arg = primary;
10269 /* The current `this' is an inner class but isn't a direct
10270 enclosing context for the inner class we're trying to
10271 create. Build an access to the proper enclosing context
10272 and use it. */
10273 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10274 && this_type != TREE_TYPE (mec))
10277 extra_arg = build_access_to_thisn (current_class,
10278 TREE_TYPE (mec), 0);
10279 extra_arg = java_complete_tree (extra_arg);
10281 /* Otherwise, just use the current `this' as an enclosing
10282 context. */
10283 else
10284 extra_arg = current_this;
10285 args = tree_cons (NULL_TREE, extra_arg, args);
10287 else
10288 args = tree_cons (NULL_TREE, integer_zero_node, args);
10291 /* This handles the situation where a constructor invocation needs
10292 to have an enclosing context passed as a second parameter (the
10293 constructor is one of an inner class. We extract it from the
10294 current function. */
10295 if ((is_super_init ||
10296 (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10297 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10299 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10300 tree extra_arg;
10302 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10304 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10305 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10307 else
10309 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10310 extra_arg =
10311 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10312 extra_arg = java_complete_tree (extra_arg);
10314 args = tree_cons (NULL_TREE, extra_arg, args);
10317 is_static_flag = METHOD_STATIC (list);
10318 if (! is_static_flag && this_arg != NULL_TREE)
10319 args = tree_cons (NULL_TREE, this_arg, args);
10321 /* In the context of an explicit constructor invocation, we can't
10322 invoke any method relying on `this'. Exceptions are: we're
10323 invoking a static function, primary exists and is not the current
10324 this, we're creating a new object. */
10325 if (ctxp->explicit_constructor_p
10326 && !is_static_flag
10327 && (!primary || primary == current_this)
10328 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10330 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10331 PATCH_METHOD_RETURN_ERROR ();
10333 java_parser_context_restore_global ();
10334 if (is_static)
10335 *is_static = is_static_flag;
10336 /* Sometimes, we want the decl of the selected method. Such as for
10337 EH checking */
10338 if (ret_decl)
10339 *ret_decl = list;
10340 patch = patch_invoke (patch, list, args);
10342 /* Now is a good time to insert the call to finit$ */
10343 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10345 tree finit_parms, finit_call;
10347 /* Prepare to pass hidden parameters to finit$, if any. */
10348 finit_parms = build_alias_initializer_parameter_list
10349 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10351 finit_call =
10352 build_method_invocation (build_wfl_node (finit_identifier_node),
10353 finit_parms);
10355 /* Generate the code used to initialize fields declared with an
10356 initialization statement and build a compound statement along
10357 with the super constructor invocation. */
10358 patch = build (COMPOUND_EXPR, void_type_node, patch,
10359 java_complete_tree (finit_call));
10360 CAN_COMPLETE_NORMALLY (patch) = 1;
10362 return patch;
10365 /* Check that we're not trying to do a static reference to a method in
10366 non static method. Return 1 if it's the case, 0 otherwise. */
10368 static int
10369 check_for_static_method_reference (wfl, node, method, where, primary)
10370 tree wfl, node, method, where, primary;
10372 if (METHOD_STATIC (current_function_decl)
10373 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10375 char *fct_name = xstrdup (lang_printable_name (method, 0));
10376 parse_error_context
10377 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10378 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10379 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10380 free (fct_name);
10381 return 1;
10383 return 0;
10386 /* Fix the invocation of *MDECL if necessary in the case of a
10387 invocation from an inner class. *THIS_ARG might be modified
10388 appropriately and an alternative access to *MDECL might be
10389 returned. */
10391 static int
10392 maybe_use_access_method (is_super_init, mdecl, this_arg)
10393 int is_super_init;
10394 tree *mdecl, *this_arg;
10396 tree ctx;
10397 tree md = *mdecl, ta = *this_arg;
10398 int to_return = 0;
10399 int non_static_context = !METHOD_STATIC (md);
10401 if (is_super_init
10402 || DECL_CONTEXT (md) == current_class
10403 || !PURE_INNER_CLASS_TYPE_P (current_class)
10404 || DECL_FINIT_P (md)
10405 || DECL_INSTINIT_P (md))
10406 return 0;
10408 /* If we're calling a method found in an enclosing class, generate
10409 what it takes to retrieve the right this. Don't do that if we're
10410 invoking a static method. Note that if MD's type is unrelated to
10411 CURRENT_CLASS, then the current this can be used. */
10413 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10415 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10416 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10418 ta = build_current_thisn (current_class);
10419 ta = build_wfl_node (ta);
10421 else
10423 tree type = ctx;
10424 while (type)
10426 maybe_build_thisn_access_method (type);
10427 if (inherits_from_p (type, DECL_CONTEXT (md)))
10429 ta = build_access_to_thisn (ctx, type, 0);
10430 break;
10432 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10433 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10436 ta = java_complete_tree (ta);
10439 /* We might have to use an access method to get to MD. We can
10440 break the method access rule as far as we're not generating
10441 bytecode */
10442 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10444 md = build_outer_method_access_method (md);
10445 to_return = 1;
10448 *mdecl = md;
10449 *this_arg = ta;
10451 /* Returnin a non zero value indicates we were doing a non static
10452 method invokation that is now a static invocation. It will have
10453 callee displace `this' to insert it in the regular argument
10454 list. */
10455 return (non_static_context && to_return);
10458 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10459 mode. */
10461 static tree
10462 patch_invoke (patch, method, args)
10463 tree patch, method, args;
10465 tree dtable, func;
10466 tree original_call, t, ta;
10467 tree check = NULL_TREE;
10469 /* Last step for args: convert build-in types. If we're dealing with
10470 a new TYPE() type call, the first argument to the constructor
10471 isn't found in the incoming argument list, but delivered by
10472 `new' */
10473 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10474 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10475 t = TREE_CHAIN (t);
10476 for (ta = args; t != end_params_node && ta;
10477 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10478 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10479 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10480 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10482 /* Resolve unresolved returned type isses */
10483 t = TREE_TYPE (TREE_TYPE (method));
10484 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10485 resolve_and_layout (TREE_TYPE (t), NULL);
10487 if (flag_emit_class_files || flag_emit_xref)
10488 func = method;
10489 else
10491 tree signature = build_java_signature (TREE_TYPE (method));
10492 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10494 case INVOKE_VIRTUAL:
10495 dtable = invoke_build_dtable (0, args);
10496 func = build_invokevirtual (dtable, method);
10497 break;
10499 case INVOKE_NONVIRTUAL:
10500 /* If the object for the method call is null, we throw an
10501 exception. We don't do this if the object is the current
10502 method's `this'. In other cases we just rely on an
10503 optimization pass to eliminate redundant checks. */
10504 if (TREE_VALUE (args) != current_this)
10506 /* We use a save_expr here to make sure we only evaluate
10507 the new `self' expression once. */
10508 tree save_arg = save_expr (TREE_VALUE (args));
10509 TREE_VALUE (args) = save_arg;
10510 check = java_check_reference (save_arg, 1);
10512 /* Fall through. */
10514 case INVOKE_SUPER:
10515 case INVOKE_STATIC:
10516 func = build_known_method_ref (method, TREE_TYPE (method),
10517 DECL_CONTEXT (method),
10518 signature, args);
10519 break;
10521 case INVOKE_INTERFACE:
10522 dtable = invoke_build_dtable (1, args);
10523 func = build_invokeinterface (dtable, method);
10524 break;
10526 default:
10527 abort ();
10530 /* Ensure self_type is initialized, (invokestatic). FIXME */
10531 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10534 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10535 TREE_OPERAND (patch, 0) = func;
10536 TREE_OPERAND (patch, 1) = args;
10537 original_call = patch;
10539 /* We're processing a `new TYPE ()' form. New is called and its
10540 returned value is the first argument to the constructor. We build
10541 a COMPOUND_EXPR and use saved expression so that the overall NEW
10542 expression value is a pointer to a newly created and initialized
10543 class. */
10544 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10546 tree class = DECL_CONTEXT (method);
10547 tree c1, saved_new, size, new;
10548 if (flag_emit_class_files || flag_emit_xref)
10550 TREE_TYPE (patch) = build_pointer_type (class);
10551 return patch;
10553 if (!TYPE_SIZE (class))
10554 safe_layout_class (class);
10555 size = size_in_bytes (class);
10556 new = build (CALL_EXPR, promote_type (class),
10557 build_address_of (alloc_object_node),
10558 tree_cons (NULL_TREE, build_class_ref (class),
10559 build_tree_list (NULL_TREE,
10560 size_in_bytes (class))),
10561 NULL_TREE);
10562 saved_new = save_expr (new);
10563 c1 = build_tree_list (NULL_TREE, saved_new);
10564 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10565 TREE_OPERAND (original_call, 1) = c1;
10566 TREE_SET_CODE (original_call, CALL_EXPR);
10567 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10570 /* If CHECK is set, then we are building a check to see if the object
10571 is NULL. */
10572 if (check != NULL_TREE)
10574 patch = build (COMPOUND_EXPR, TREE_TYPE (patch), check, patch);
10575 TREE_SIDE_EFFECTS (patch) = 1;
10578 /* In order to be able to modify PATCH later, we SAVE_EXPR it and
10579 put it as the first expression of a COMPOUND_EXPR. The second
10580 expression being an empty statement to be later patched if
10581 necessary. We remember a TREE_LIST (the PURPOSE is the method,
10582 the VALUE is the compound) in a hashtable and return a
10583 COMPOUND_EXPR built so that the result of the evaluation of the
10584 original PATCH node is returned. */
10585 if (STATIC_CLASS_INIT_OPT_P ()
10586 && current_function_decl && METHOD_STATIC (method))
10588 tree list;
10589 tree fndecl = current_function_decl;
10590 tree save = save_expr (patch);
10591 tree type = TREE_TYPE (patch);
10593 patch = build (COMPOUND_EXPR, type, save, empty_stmt_node);
10594 list = build_tree_list (method, patch);
10596 hash_lookup (&DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl),
10597 (const hash_table_key) list, TRUE, NULL);
10599 patch = build (COMPOUND_EXPR, type, patch, save);
10602 return patch;
10605 static int
10606 invocation_mode (method, super)
10607 tree method;
10608 int super;
10610 int access = get_access_flags_from_decl (method);
10612 if (super)
10613 return INVOKE_SUPER;
10615 if (access & ACC_STATIC)
10616 return INVOKE_STATIC;
10618 /* We have to look for a constructor before we handle nonvirtual
10619 calls; otherwise the constructor will look nonvirtual. */
10620 if (DECL_CONSTRUCTOR_P (method))
10621 return INVOKE_STATIC;
10623 if (access & ACC_FINAL || access & ACC_PRIVATE)
10624 return INVOKE_NONVIRTUAL;
10626 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10627 return INVOKE_NONVIRTUAL;
10629 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10630 return INVOKE_INTERFACE;
10632 return INVOKE_VIRTUAL;
10635 /* Retrieve a refined list of matching methods. It covers the step
10636 15.11.2 (Compile-Time Step 2) */
10638 static tree
10639 lookup_method_invoke (lc, cl, class, name, arg_list)
10640 int lc;
10641 tree cl;
10642 tree class, name, arg_list;
10644 tree atl = end_params_node; /* Arg Type List */
10645 tree method, signature, list, node;
10646 const char *candidates; /* Used for error report */
10647 char *dup;
10649 /* Fix the arguments */
10650 for (node = arg_list; node; node = TREE_CHAIN (node))
10652 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10653 /* Non primitive type may have to be resolved */
10654 if (!JPRIMITIVE_TYPE_P (current_arg))
10655 resolve_and_layout (current_arg, NULL_TREE);
10656 /* And promoted */
10657 if (TREE_CODE (current_arg) == RECORD_TYPE)
10658 current_arg = promote_type (current_arg);
10659 atl = tree_cons (NULL_TREE, current_arg, atl);
10662 /* Presto. If we're dealing with an anonymous class and a
10663 constructor call, generate the right constructor now, since we
10664 know the arguments' types. */
10666 if (lc && ANONYMOUS_CLASS_P (class))
10667 craft_constructor (TYPE_NAME (class), atl);
10669 /* Find all candidates and then refine the list, searching for the
10670 most specific method. */
10671 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10672 list = find_most_specific_methods_list (list);
10673 if (list && !TREE_CHAIN (list))
10674 return TREE_VALUE (list);
10676 /* Issue an error. List candidates if any. Candidates are listed
10677 only if accessible (non accessible methods may end-up here for
10678 the sake of a better error report). */
10679 candidates = NULL;
10680 if (list)
10682 tree current;
10683 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10684 for (current = list; current; current = TREE_CHAIN (current))
10686 tree cm = TREE_VALUE (current);
10687 char string [4096];
10688 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10689 continue;
10690 sprintf
10691 (string, " `%s' in `%s'%s",
10692 get_printable_method_name (cm),
10693 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10694 (TREE_CHAIN (current) ? "\n" : ""));
10695 obstack_grow (&temporary_obstack, string, strlen (string));
10697 obstack_1grow (&temporary_obstack, '\0');
10698 candidates = obstack_finish (&temporary_obstack);
10700 /* Issue the error message */
10701 method = make_node (FUNCTION_TYPE);
10702 TYPE_ARG_TYPES (method) = atl;
10703 signature = build_java_argument_signature (method);
10704 dup = xstrdup (lang_printable_name (class, 0));
10705 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10706 (lc ? "constructor" : "method"),
10707 (lc ? dup : IDENTIFIER_POINTER (name)),
10708 IDENTIFIER_POINTER (signature), dup,
10709 (candidates ? candidates : ""));
10710 free (dup);
10711 return NULL_TREE;
10714 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10715 when we're looking for a constructor. */
10717 static tree
10718 find_applicable_accessible_methods_list (lc, class, name, arglist)
10719 int lc;
10720 tree class, name, arglist;
10722 static struct hash_table t, *searched_classes = NULL;
10723 static int search_not_done = 0;
10724 tree list = NULL_TREE, all_list = NULL_TREE;
10726 /* Check the hash table to determine if this class has been searched
10727 already. */
10728 if (searched_classes)
10730 if (hash_lookup (searched_classes,
10731 (const hash_table_key) class, FALSE, NULL))
10732 return NULL;
10734 else
10736 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10737 java_hash_compare_tree_node);
10738 searched_classes = &t;
10741 search_not_done++;
10742 hash_lookup (searched_classes,
10743 (const hash_table_key) class, TRUE, NULL);
10745 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10747 load_class (class, 1);
10748 safe_layout_class (class);
10751 /* Search interfaces */
10752 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10753 && CLASS_INTERFACE (TYPE_NAME (class)))
10755 int i, n;
10756 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10757 search_applicable_methods_list (lc, TYPE_METHODS (class),
10758 name, arglist, &list, &all_list);
10759 n = TREE_VEC_LENGTH (basetype_vec);
10760 for (i = 1; i < n; i++)
10762 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10763 tree rlist;
10765 rlist = find_applicable_accessible_methods_list (lc, t, name,
10766 arglist);
10767 list = chainon (rlist, list);
10770 /* Search classes */
10771 else
10773 search_applicable_methods_list (lc, TYPE_METHODS (class),
10774 name, arglist, &list, &all_list);
10776 /* When looking finit$, class$ or instinit$, we turn LC to 1 so
10777 that we only search in class. Note that we should have found
10778 something at this point. */
10779 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
10781 lc = 1;
10782 if (!list)
10783 abort ();
10786 /* We must search all interfaces of this class */
10787 if (!lc)
10789 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10790 int n = TREE_VEC_LENGTH (basetype_vec), i;
10791 for (i = 1; i < n; i++)
10793 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10794 if (t != object_type_node)
10796 tree rlist
10797 = find_applicable_accessible_methods_list (lc, t,
10798 name, arglist);
10799 list = chainon (rlist, list);
10804 /* Search superclass */
10805 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10807 tree rlist;
10808 class = CLASSTYPE_SUPER (class);
10809 rlist = find_applicable_accessible_methods_list (lc, class,
10810 name, arglist);
10811 list = chainon (rlist, list);
10815 search_not_done--;
10817 /* We're done. Reset the searched classes list and finally search
10818 java.lang.Object if it wasn't searched already. */
10819 if (!search_not_done)
10821 if (!lc
10822 && TYPE_METHODS (object_type_node)
10823 && !hash_lookup (searched_classes,
10824 (const hash_table_key) object_type_node,
10825 FALSE, NULL))
10827 search_applicable_methods_list (lc,
10828 TYPE_METHODS (object_type_node),
10829 name, arglist, &list, &all_list);
10831 hash_table_free (searched_classes);
10832 searched_classes = NULL;
10835 /* Either return the list obtained or all selected (but
10836 inaccessible) methods for better error report. */
10837 return (!list ? all_list : list);
10840 /* Effectively search for the appropriate method in method */
10842 static void
10843 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10844 int lc;
10845 tree method, name, arglist;
10846 tree *list, *all_list;
10848 for (; method; method = TREE_CHAIN (method))
10850 /* When dealing with constructor, stop here, otherwise search
10851 other classes */
10852 if (lc && !DECL_CONSTRUCTOR_P (method))
10853 continue;
10854 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10855 || (DECL_NAME (method) != name)))
10856 continue;
10858 if (argument_types_convertible (method, arglist))
10860 /* Retain accessible methods only */
10861 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10862 method, NULL_TREE, 0))
10863 *list = tree_cons (NULL_TREE, method, *list);
10864 else
10865 /* Also retain all selected method here */
10866 *all_list = tree_cons (NULL_TREE, method, *list);
10871 /* 15.11.2.2 Choose the Most Specific Method */
10873 static tree
10874 find_most_specific_methods_list (list)
10875 tree list;
10877 int max = 0;
10878 int abstract, candidates;
10879 tree current, new_list = NULL_TREE;
10880 for (current = list; current; current = TREE_CHAIN (current))
10882 tree method;
10883 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10885 for (method = list; method; method = TREE_CHAIN (method))
10887 tree method_v, current_v;
10888 /* Don't test a method against itself */
10889 if (method == current)
10890 continue;
10892 method_v = TREE_VALUE (method);
10893 current_v = TREE_VALUE (current);
10895 /* Compare arguments and location where methods where declared */
10896 if (argument_types_convertible (method_v, current_v))
10898 if (valid_method_invocation_conversion_p
10899 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10900 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10901 && enclosing_context_p (DECL_CONTEXT (method_v),
10902 DECL_CONTEXT (current_v))))
10904 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10905 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10906 max = (v > max ? v : max);
10912 /* Review the list and select the maximally specific methods */
10913 for (current = list, abstract = -1, candidates = -1;
10914 current; current = TREE_CHAIN (current))
10915 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10917 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10918 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10919 candidates++;
10922 /* If we have several and they're all abstract, just pick the
10923 closest one. */
10924 if (candidates > 0 && (candidates == abstract))
10926 new_list = nreverse (new_list);
10927 TREE_CHAIN (new_list) = NULL_TREE;
10930 /* We have several (we couldn't find a most specific), all but one
10931 are abstract, we pick the only non abstract one. */
10932 if (candidates > 0 && (candidates == abstract+1))
10934 for (current = new_list; current; current = TREE_CHAIN (current))
10935 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10937 TREE_CHAIN (current) = NULL_TREE;
10938 new_list = current;
10942 /* If we can't find one, lower expectations and try to gather multiple
10943 maximally specific methods */
10944 while (!new_list && max)
10946 while (--max > 0)
10948 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10949 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10953 return new_list;
10956 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10957 converted by method invocation conversion (5.3) to the type of the
10958 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10959 to change less often than M1. */
10961 static int
10962 argument_types_convertible (m1, m2_or_arglist)
10963 tree m1, m2_or_arglist;
10965 static tree m2_arg_value = NULL_TREE;
10966 static tree m2_arg_cache = NULL_TREE;
10967 static int initialized_p;
10969 register tree m1_arg, m2_arg;
10971 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10972 collector. */
10973 if (!initialized_p)
10975 ggc_add_tree_root (&m2_arg_value, 1);
10976 ggc_add_tree_root (&m2_arg_cache, 1);
10977 initialized_p = 1;
10980 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10982 if (m2_arg_value == m2_or_arglist)
10983 m2_arg = m2_arg_cache;
10984 else
10986 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10987 argument types */
10988 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10990 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10991 if (!METHOD_STATIC (m2_or_arglist))
10992 m2_arg = TREE_CHAIN (m2_arg);
10994 else
10995 m2_arg = m2_or_arglist;
10997 m2_arg_value = m2_or_arglist;
10998 m2_arg_cache = m2_arg;
11001 while (m1_arg != end_params_node && m2_arg != end_params_node)
11003 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11004 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11005 TREE_VALUE (m2_arg)))
11006 break;
11007 m1_arg = TREE_CHAIN (m1_arg);
11008 m2_arg = TREE_CHAIN (m2_arg);
11010 return m1_arg == end_params_node && m2_arg == end_params_node;
11013 /* Qualification routines */
11015 static void
11016 qualify_ambiguous_name (id)
11017 tree id;
11019 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
11020 saved_current_class;
11021 int again, super_found = 0, this_found = 0, new_array_found = 0;
11022 int code;
11024 /* We first qualify the first element, then derive qualification of
11025 others based on the first one. If the first element is qualified
11026 by a resolution (field or type), this resolution is stored in the
11027 QUAL_RESOLUTION of the qual element being examined. We need to
11028 save the current_class since the use of SUPER might change the
11029 its value. */
11030 saved_current_class = current_class;
11031 qual = EXPR_WFL_QUALIFICATION (id);
11032 do {
11034 /* Simple qualified expression feature a qual_wfl that is a
11035 WFL. Expression derived from a primary feature more complicated
11036 things like a CALL_EXPR. Expression from primary need to be
11037 worked out to extract the part on which the qualification will
11038 take place. */
11039 qual_wfl = QUAL_WFL (qual);
11040 switch (TREE_CODE (qual_wfl))
11042 case CALL_EXPR:
11043 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11044 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11046 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11047 qual_wfl = QUAL_WFL (qual);
11049 break;
11050 case NEW_ARRAY_EXPR:
11051 case NEW_ANONYMOUS_ARRAY_EXPR:
11052 qual = TREE_CHAIN (qual);
11053 again = new_array_found = 1;
11054 continue;
11055 case CONVERT_EXPR:
11056 break;
11057 case NEW_CLASS_EXPR:
11058 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11059 break;
11060 case ARRAY_REF:
11061 while (TREE_CODE (qual_wfl) == ARRAY_REF)
11062 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11063 break;
11064 case STRING_CST:
11065 qual = TREE_CHAIN (qual);
11066 qual_wfl = QUAL_WFL (qual);
11067 break;
11068 case CLASS_LITERAL:
11069 qual = TREE_CHAIN (qual);
11070 qual_wfl = QUAL_WFL (qual);
11071 break;
11072 default:
11073 /* Fix for -Wall. Just break doing nothing */
11074 break;
11077 ptr_type = current_class;
11078 again = 0;
11079 code = TREE_CODE (qual_wfl);
11081 /* Pos evaluation: non WFL leading expression nodes */
11082 if (code == CONVERT_EXPR
11083 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
11084 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
11086 else if (code == INTEGER_CST)
11087 name = qual_wfl;
11089 else if (code == CONVERT_EXPR &&
11090 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11091 name = TREE_OPERAND (qual_wfl, 0);
11093 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
11094 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11095 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
11097 else if (code == TREE_LIST)
11098 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
11100 else if (code == STRING_CST || code == CONDITIONAL_EXPR
11101 || code == PLUS_EXPR)
11103 qual = TREE_CHAIN (qual);
11104 qual_wfl = QUAL_WFL (qual);
11105 again = 1;
11107 else
11109 name = EXPR_WFL_NODE (qual_wfl);
11110 if (!name)
11112 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11113 again = 1;
11117 /* If we have a THIS (from a primary), we set the context accordingly */
11118 if (name == this_identifier_node)
11120 /* This isn't really elegant. One more added irregularity
11121 before I start using COMPONENT_REF (hopefully very soon.) */
11122 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
11123 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11124 EXPR_WITH_FILE_LOCATION
11125 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11126 this_identifier_node)
11128 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
11129 qual = EXPR_WFL_QUALIFICATION (qual);
11131 qual = TREE_CHAIN (qual);
11132 qual_wfl = QUAL_WFL (qual);
11133 if (TREE_CODE (qual_wfl) == CALL_EXPR)
11134 again = 1;
11135 else if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION)
11136 name = EXPR_WFL_NODE (qual_wfl);
11137 else if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
11138 name = TREE_OPERAND (qual_wfl, 0);
11139 this_found = 1;
11141 /* If we have a SUPER, we set the context accordingly */
11142 if (name == super_identifier_node)
11144 current_class = CLASSTYPE_SUPER (ptr_type);
11145 /* Check that there is such a thing as a super class. If not,
11146 return. The error will be caught later on, during the
11147 resolution */
11148 if (!current_class)
11150 current_class = saved_current_class;
11151 return;
11153 qual = TREE_CHAIN (qual);
11154 /* Do one more interation to set things up */
11155 super_found = again = 1;
11157 } while (again);
11159 /* If name appears within the scope of a local variable declaration
11160 or parameter declaration, then it is an expression name. We don't
11161 carry this test out if we're in the context of the use of SUPER
11162 or THIS */
11163 if (!this_found && !super_found
11164 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
11165 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
11167 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11168 QUAL_RESOLUTION (qual) = decl;
11171 /* If within the class/interface NAME was found to be used there
11172 exists a (possibly inherited) field named NAME, then this is an
11173 expression name. If we saw a NEW_ARRAY_EXPR before and want to
11174 address length, it is OK. */
11175 else if ((decl = lookup_field_wrapper (ptr_type, name))
11176 || name == length_identifier_node)
11178 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11179 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
11182 /* We reclassify NAME as yielding to a type name resolution if:
11183 - NAME is a class/interface declared within the compilation
11184 unit containing NAME,
11185 - NAME is imported via a single-type-import declaration,
11186 - NAME is declared in an another compilation unit of the package
11187 of the compilation unit containing NAME,
11188 - NAME is declared by exactly on type-import-on-demand declaration
11189 of the compilation unit containing NAME.
11190 - NAME is actually a STRING_CST.
11191 This can't happen if the expression was qualified by `this.' */
11192 else if (! this_found &&
11193 (TREE_CODE (name) == STRING_CST ||
11194 TREE_CODE (name) == INTEGER_CST ||
11195 (decl = resolve_and_layout (name, NULL_TREE))))
11197 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11198 QUAL_RESOLUTION (qual) = decl;
11201 /* Method call, array references and cast are expression name */
11202 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
11203 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
11204 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR
11205 || TREE_CODE (QUAL_WFL (qual)) == MODIFY_EXPR)
11206 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11208 /* Check here that NAME isn't declared by more than one
11209 type-import-on-demand declaration of the compilation unit
11210 containing NAME. FIXME */
11212 /* Otherwise, NAME is reclassified as a package name */
11213 else
11214 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11216 /* Propagate the qualification accross other components of the
11217 qualified name */
11218 for (qual = TREE_CHAIN (qual); qual;
11219 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11221 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11222 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11223 else
11224 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
11227 /* Store the global qualification for the ambiguous part of ID back
11228 into ID fields */
11229 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
11230 RESOLVE_EXPRESSION_NAME_P (id) = 1;
11231 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
11232 RESOLVE_TYPE_NAME_P (id) = 1;
11233 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11234 RESOLVE_PACKAGE_NAME_P (id) = 1;
11236 /* Restore the current class */
11237 current_class = saved_current_class;
11240 static int
11241 breakdown_qualified (left, right, source)
11242 tree *left, *right, source;
11244 char *p, *base;
11245 int l = IDENTIFIER_LENGTH (source);
11247 base = alloca (l + 1);
11248 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
11250 /* Breakdown NAME into REMAINDER . IDENTIFIER */
11251 p = base + l - 1;
11252 while (*p != '.' && p != base)
11253 p--;
11255 /* We didn't find a '.'. Return an error */
11256 if (p == base)
11257 return 1;
11259 *p = '\0';
11260 if (right)
11261 *right = get_identifier (p+1);
11262 *left = get_identifier (base);
11264 return 0;
11267 /* Return TRUE if two classes are from the same package. */
11269 static int
11270 in_same_package (name1, name2)
11271 tree name1, name2;
11273 tree tmp;
11274 tree pkg1;
11275 tree pkg2;
11277 if (TREE_CODE (name1) == TYPE_DECL)
11278 name1 = DECL_NAME (name1);
11279 if (TREE_CODE (name2) == TYPE_DECL)
11280 name2 = DECL_NAME (name2);
11282 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11283 /* One in empty package. */
11284 return 0;
11286 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11287 /* Both in empty package. */
11288 return 1;
11290 breakdown_qualified (&pkg1, &tmp, name1);
11291 breakdown_qualified (&pkg2, &tmp, name2);
11293 return (pkg1 == pkg2);
11296 /* Patch tree nodes in a function body. When a BLOCK is found, push
11297 local variable decls if present.
11298 Same as java_complete_lhs, but does resolve static finals to values. */
11300 static tree
11301 java_complete_tree (node)
11302 tree node;
11304 node = java_complete_lhs (node);
11305 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11306 && DECL_INITIAL (node) != NULL_TREE
11307 && !flag_emit_xref)
11309 tree value = DECL_INITIAL (node);
11310 DECL_INITIAL (node) = NULL_TREE;
11311 value = fold_constant_for_init (value, node);
11312 DECL_INITIAL (node) = value;
11313 if (value != NULL_TREE)
11315 /* fold_constant_for_init sometimes widen the original type
11316 of the constant (i.e. byte to int.) It's not desirable,
11317 especially if NODE is a function argument. */
11318 if (TREE_CODE (value) == INTEGER_CST
11319 && TREE_TYPE (node) != TREE_TYPE (value))
11320 return convert (TREE_TYPE (node), value);
11321 else
11322 return value;
11324 else
11325 DECL_FIELD_FINAL_IUD (node) = 0;
11327 return node;
11330 static tree
11331 java_stabilize_reference (node)
11332 tree node;
11334 if (TREE_CODE (node) == COMPOUND_EXPR)
11336 tree op0 = TREE_OPERAND (node, 0);
11337 tree op1 = TREE_OPERAND (node, 1);
11338 TREE_OPERAND (node, 0) = save_expr (op0);
11339 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11340 return node;
11342 return stabilize_reference (node);
11345 /* Patch tree nodes in a function body. When a BLOCK is found, push
11346 local variable decls if present.
11347 Same as java_complete_tree, but does not resolve static finals to values. */
11349 static tree
11350 java_complete_lhs (node)
11351 tree node;
11353 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11354 int flag;
11356 /* CONVERT_EXPR always has its type set, even though it needs to be
11357 worked out. */
11358 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11359 return node;
11361 /* The switch block implements cases processing container nodes
11362 first. Contained nodes are always written back. Leaves come
11363 next and return a value. */
11364 switch (TREE_CODE (node))
11366 case BLOCK:
11368 /* 1- Block section.
11369 Set the local values on decl names so we can identify them
11370 faster when they're referenced. At that stage, identifiers
11371 are legal so we don't check for declaration errors. */
11372 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11374 DECL_CONTEXT (cn) = current_function_decl;
11375 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11377 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11378 CAN_COMPLETE_NORMALLY (node) = 1;
11379 else
11381 tree stmt = BLOCK_EXPR_BODY (node);
11382 tree *ptr;
11383 int error_seen = 0;
11384 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11386 /* Re-order from (((A; B); C); ...; Z) to
11387 (A; (B; (C ; (...; Z)))).
11388 This makes it easier to scan the statements left-to-right
11389 without using recursion (which might overflow the stack
11390 if the block has many statements. */
11391 for (;;)
11393 tree left = TREE_OPERAND (stmt, 0);
11394 if (TREE_CODE (left) != COMPOUND_EXPR)
11395 break;
11396 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11397 TREE_OPERAND (left, 1) = stmt;
11398 stmt = left;
11400 BLOCK_EXPR_BODY (node) = stmt;
11403 /* Now do the actual complete, without deep recursion for
11404 long blocks. */
11405 ptr = &BLOCK_EXPR_BODY (node);
11406 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11407 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11409 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11410 tree *next = &TREE_OPERAND (*ptr, 1);
11411 TREE_OPERAND (*ptr, 0) = cur;
11412 if (cur == empty_stmt_node)
11414 /* Optimization; makes it easier to detect empty bodies.
11415 Most useful for <clinit> with all-constant initializer. */
11416 *ptr = *next;
11417 continue;
11419 if (TREE_CODE (cur) == ERROR_MARK)
11420 error_seen++;
11421 else if (! CAN_COMPLETE_NORMALLY (cur))
11423 wfl_op2 = *next;
11424 for (;;)
11426 if (TREE_CODE (wfl_op2) == BLOCK)
11427 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11428 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11429 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11430 else
11431 break;
11433 if (TREE_CODE (wfl_op2) != CASE_EXPR
11434 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11435 unreachable_stmt_error (*ptr);
11437 ptr = next;
11439 *ptr = java_complete_tree (*ptr);
11441 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11442 return error_mark_node;
11443 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11445 /* Turn local bindings to null */
11446 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11447 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11449 TREE_TYPE (node) = void_type_node;
11450 break;
11452 /* 2- They are expressions but ultimately deal with statements */
11454 case THROW_EXPR:
11455 wfl_op1 = TREE_OPERAND (node, 0);
11456 COMPLETE_CHECK_OP_0 (node);
11457 /* 14.19 A throw statement cannot complete normally. */
11458 CAN_COMPLETE_NORMALLY (node) = 0;
11459 return patch_throw_statement (node, wfl_op1);
11461 case SYNCHRONIZED_EXPR:
11462 wfl_op1 = TREE_OPERAND (node, 0);
11463 return patch_synchronized_statement (node, wfl_op1);
11465 case TRY_EXPR:
11466 return patch_try_statement (node);
11468 case TRY_FINALLY_EXPR:
11469 COMPLETE_CHECK_OP_0 (node);
11470 COMPLETE_CHECK_OP_1 (node);
11471 if (TREE_OPERAND (node, 0) == empty_stmt_node)
11472 return TREE_OPERAND (node, 1);
11473 if (TREE_OPERAND (node, 1) == empty_stmt_node)
11474 return TREE_OPERAND (node, 0);
11475 CAN_COMPLETE_NORMALLY (node)
11476 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11477 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11478 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11479 return node;
11481 case CLEANUP_POINT_EXPR:
11482 COMPLETE_CHECK_OP_0 (node);
11483 TREE_TYPE (node) = void_type_node;
11484 CAN_COMPLETE_NORMALLY (node) =
11485 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11486 return node;
11488 case WITH_CLEANUP_EXPR:
11489 COMPLETE_CHECK_OP_0 (node);
11490 COMPLETE_CHECK_OP_1 (node);
11491 CAN_COMPLETE_NORMALLY (node) =
11492 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11493 TREE_TYPE (node) = void_type_node;
11494 return node;
11496 case LABELED_BLOCK_EXPR:
11497 PUSH_LABELED_BLOCK (node);
11498 if (LABELED_BLOCK_BODY (node))
11499 COMPLETE_CHECK_OP_1 (node);
11500 TREE_TYPE (node) = void_type_node;
11501 POP_LABELED_BLOCK ();
11503 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11505 LABELED_BLOCK_BODY (node) = NULL_TREE;
11506 CAN_COMPLETE_NORMALLY (node) = 1;
11508 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11509 CAN_COMPLETE_NORMALLY (node) = 1;
11510 return node;
11512 case EXIT_BLOCK_EXPR:
11513 /* We don't complete operand 1, because it's the return value of
11514 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11515 return patch_bc_statement (node);
11517 case CASE_EXPR:
11518 cn = java_complete_tree (TREE_OPERAND (node, 0));
11519 if (cn == error_mark_node)
11520 return cn;
11522 /* First, the case expression must be constant. Values of final
11523 fields are accepted. */
11524 cn = fold (cn);
11525 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11526 && JDECL_P (TREE_OPERAND (cn, 1))
11527 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11528 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11530 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11531 TREE_OPERAND (cn, 1));
11534 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11536 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11537 parse_error_context (node, "Constant expression required");
11538 return error_mark_node;
11541 nn = ctxp->current_loop;
11543 /* It must be assignable to the type of the switch expression. */
11544 if (!try_builtin_assignconv (NULL_TREE,
11545 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11547 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11548 parse_error_context
11549 (wfl_operator,
11550 "Incompatible type for case. Can't convert `%s' to `int'",
11551 lang_printable_name (TREE_TYPE (cn), 0));
11552 return error_mark_node;
11555 cn = fold (convert (int_type_node, cn));
11556 TREE_CONSTANT_OVERFLOW (cn) = 0;
11557 CAN_COMPLETE_NORMALLY (cn) = 1;
11559 /* Multiple instance of a case label bearing the same
11560 value is checked during code generation. The case
11561 expression is allright so far. */
11562 if (TREE_CODE (cn) == VAR_DECL)
11563 cn = DECL_INITIAL (cn);
11564 TREE_OPERAND (node, 0) = cn;
11565 TREE_TYPE (node) = void_type_node;
11566 CAN_COMPLETE_NORMALLY (node) = 1;
11567 TREE_SIDE_EFFECTS (node) = 1;
11568 break;
11570 case DEFAULT_EXPR:
11571 nn = ctxp->current_loop;
11572 /* Only one default label is allowed per switch statement */
11573 if (SWITCH_HAS_DEFAULT (nn))
11575 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11576 parse_error_context (wfl_operator,
11577 "Duplicate case label: `default'");
11578 return error_mark_node;
11580 else
11581 SWITCH_HAS_DEFAULT (nn) = 1;
11582 TREE_TYPE (node) = void_type_node;
11583 TREE_SIDE_EFFECTS (node) = 1;
11584 CAN_COMPLETE_NORMALLY (node) = 1;
11585 break;
11587 case SWITCH_EXPR:
11588 case LOOP_EXPR:
11589 PUSH_LOOP (node);
11590 /* Check whether the loop was enclosed in a labeled
11591 statement. If not, create one, insert the loop in it and
11592 return the node */
11593 nn = patch_loop_statement (node);
11595 /* Anyways, walk the body of the loop */
11596 if (TREE_CODE (node) == LOOP_EXPR)
11597 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11598 /* Switch statement: walk the switch expression and the cases */
11599 else
11600 node = patch_switch_statement (node);
11602 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11603 nn = error_mark_node;
11604 else
11606 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11607 /* If we returned something different, that's because we
11608 inserted a label. Pop the label too. */
11609 if (nn != node)
11611 if (CAN_COMPLETE_NORMALLY (node))
11612 CAN_COMPLETE_NORMALLY (nn) = 1;
11613 POP_LABELED_BLOCK ();
11616 POP_LOOP ();
11617 return nn;
11619 case EXIT_EXPR:
11620 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11621 return patch_exit_expr (node);
11623 case COND_EXPR:
11624 /* Condition */
11625 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11626 if (TREE_OPERAND (node, 0) == error_mark_node)
11627 return error_mark_node;
11628 /* then-else branches */
11629 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11630 if (TREE_OPERAND (node, 1) == error_mark_node)
11631 return error_mark_node;
11632 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11633 if (TREE_OPERAND (node, 2) == error_mark_node)
11634 return error_mark_node;
11635 return patch_if_else_statement (node);
11636 break;
11638 case CONDITIONAL_EXPR:
11639 /* Condition */
11640 wfl_op1 = TREE_OPERAND (node, 0);
11641 COMPLETE_CHECK_OP_0 (node);
11642 wfl_op2 = TREE_OPERAND (node, 1);
11643 COMPLETE_CHECK_OP_1 (node);
11644 wfl_op3 = TREE_OPERAND (node, 2);
11645 COMPLETE_CHECK_OP_2 (node);
11646 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11648 /* 3- Expression section */
11649 case COMPOUND_EXPR:
11650 wfl_op2 = TREE_OPERAND (node, 1);
11651 TREE_OPERAND (node, 0) = nn =
11652 java_complete_tree (TREE_OPERAND (node, 0));
11653 if (wfl_op2 == empty_stmt_node)
11654 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11655 else
11657 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11659 /* An unreachable condition in a do-while statement
11660 is *not* (technically) an unreachable statement. */
11661 nn = wfl_op2;
11662 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11663 nn = EXPR_WFL_NODE (nn);
11664 if (TREE_CODE (nn) != EXIT_EXPR)
11666 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11667 parse_error_context (wfl_operator, "Unreachable statement");
11670 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11671 if (TREE_OPERAND (node, 1) == error_mark_node)
11672 return error_mark_node;
11673 CAN_COMPLETE_NORMALLY (node)
11674 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11676 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11677 break;
11679 case RETURN_EXPR:
11680 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11681 return patch_return (node);
11683 case EXPR_WITH_FILE_LOCATION:
11684 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11685 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11687 tree wfl = node;
11688 node = resolve_expression_name (node, NULL);
11689 if (node == error_mark_node)
11690 return node;
11691 /* Keep line number information somewhere were it doesn't
11692 disrupt the completion process. */
11693 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11695 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11696 TREE_OPERAND (node, 1) = wfl;
11698 CAN_COMPLETE_NORMALLY (node) = 1;
11700 else
11702 tree body;
11703 int save_lineno = lineno;
11704 lineno = EXPR_WFL_LINENO (node);
11705 body = java_complete_tree (EXPR_WFL_NODE (node));
11706 lineno = save_lineno;
11707 EXPR_WFL_NODE (node) = body;
11708 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11709 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11710 if (body == empty_stmt_node || TREE_CONSTANT (body))
11712 /* Makes it easier to constant fold, detect empty bodies. */
11713 return body;
11715 if (body == error_mark_node)
11717 /* Its important for the evaluation of assignment that
11718 this mark on the TREE_TYPE is propagated. */
11719 TREE_TYPE (node) = error_mark_node;
11720 return error_mark_node;
11722 else
11723 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11726 break;
11728 case NEW_ARRAY_EXPR:
11729 /* Patch all the dimensions */
11730 flag = 0;
11731 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11733 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11734 tree dim = convert (int_type_node,
11735 java_complete_tree (TREE_VALUE (cn)));
11736 if (dim == error_mark_node)
11738 flag = 1;
11739 continue;
11741 else
11743 TREE_VALUE (cn) = dim;
11744 /* Setup the location of the current dimension, for
11745 later error report. */
11746 TREE_PURPOSE (cn) =
11747 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11748 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11751 /* They complete the array creation expression, if no errors
11752 were found. */
11753 CAN_COMPLETE_NORMALLY (node) = 1;
11754 return (flag ? error_mark_node
11755 : force_evaluation_order (patch_newarray (node)));
11757 case NEW_ANONYMOUS_ARRAY_EXPR:
11758 /* Create the array type if necessary. */
11759 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11761 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11762 if (!(type = resolve_type_during_patch (type)))
11763 return error_mark_node;
11764 type = build_array_from_name (type, NULL_TREE,
11765 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11766 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11768 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11769 ANONYMOUS_ARRAY_INITIALIZER (node));
11770 if (node == error_mark_node)
11771 return error_mark_node;
11772 CAN_COMPLETE_NORMALLY (node) = 1;
11773 return node;
11775 case NEW_CLASS_EXPR:
11776 case CALL_EXPR:
11777 /* Complete function's argument(s) first */
11778 if (complete_function_arguments (node))
11779 return error_mark_node;
11780 else
11782 tree decl, wfl = TREE_OPERAND (node, 0);
11783 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11784 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11785 super_identifier_node);
11787 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11788 from_super, 0, &decl);
11789 if (node == error_mark_node)
11790 return error_mark_node;
11792 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11793 /* If we call this(...), register signature and positions */
11794 if (in_this)
11795 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11796 tree_cons (wfl, decl,
11797 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11798 CAN_COMPLETE_NORMALLY (node) = 1;
11799 return force_evaluation_order (node);
11802 case MODIFY_EXPR:
11803 /* Save potential wfls */
11804 wfl_op1 = TREE_OPERAND (node, 0);
11805 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11807 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11808 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11809 && DECL_INITIAL (nn) != NULL_TREE)
11811 tree value;
11813 value = fold_constant_for_init (nn, nn);
11815 if (value != NULL_TREE)
11817 tree type = TREE_TYPE (value);
11818 if (JPRIMITIVE_TYPE_P (type) ||
11819 (type == string_ptr_type_node && ! flag_emit_class_files))
11820 return empty_stmt_node;
11822 if (! flag_emit_class_files)
11823 DECL_INITIAL (nn) = NULL_TREE;
11824 if (CLASS_FINAL_VARIABLE_P (nn))
11825 DECL_FIELD_FINAL_IUD (nn) = 0;
11827 wfl_op2 = TREE_OPERAND (node, 1);
11829 if (TREE_OPERAND (node, 0) == error_mark_node)
11830 return error_mark_node;
11832 flag = COMPOUND_ASSIGN_P (wfl_op2);
11833 if (flag)
11835 /* This might break when accessing outer field from inner
11836 class. TESTME, FIXME */
11837 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11839 /* Hand stabilize the lhs on both places */
11840 TREE_OPERAND (node, 0) = lvalue;
11841 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11842 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11844 /* 15.25.2.a: Left hand is not an array access. FIXME */
11845 /* Now complete the RHS. We write it back later on. */
11846 nn = java_complete_tree (TREE_OPERAND (node, 1));
11848 if ((cn = patch_string (nn)))
11849 nn = cn;
11851 /* The last part of the rewrite for E1 op= E2 is to have
11852 E1 = (T)(E1 op E2), with T being the type of E1. */
11853 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11854 TREE_TYPE (lvalue), nn));
11856 /* If the assignment is compound and has reference type,
11857 then ensure the LHS has type String and nothing else. */
11858 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11859 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11860 parse_error_context (wfl_op2,
11861 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11862 lang_printable_name (TREE_TYPE (lvalue), 0));
11864 /* 15.25.2.b: Left hand is an array access. FIXME */
11867 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11868 function to complete this RHS. Note that a NEW_ARRAY_INIT
11869 might have been already fully expanded if created as a result
11870 of processing an anonymous array initializer. We avoid doing
11871 the operation twice by testing whether the node already bears
11872 a type. */
11873 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11874 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11875 TREE_OPERAND (node, 1));
11876 /* Otherwise we simply complete the RHS */
11877 else
11878 nn = java_complete_tree (TREE_OPERAND (node, 1));
11880 if (nn == error_mark_node)
11881 return error_mark_node;
11883 /* Write back the RHS as we evaluated it. */
11884 TREE_OPERAND (node, 1) = nn;
11886 /* In case we're handling = with a String as a RHS, we need to
11887 produce a String out of the RHS (it might still be a
11888 STRING_CST or a StringBuffer at this stage */
11889 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11890 TREE_OPERAND (node, 1) = nn;
11892 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11893 TREE_OPERAND (node, 1))))
11895 /* We return error_mark_node if outer_field_access_fix
11896 detects we write into a final. */
11897 if (nn == error_mark_node)
11898 return error_mark_node;
11899 node = nn;
11901 else
11903 node = patch_assignment (node, wfl_op1, wfl_op2);
11904 /* Reorganize the tree if necessary. */
11905 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11906 || JSTRING_P (TREE_TYPE (node))))
11907 node = java_refold (node);
11910 CAN_COMPLETE_NORMALLY (node) = 1;
11911 return node;
11913 case MULT_EXPR:
11914 case PLUS_EXPR:
11915 case MINUS_EXPR:
11916 case LSHIFT_EXPR:
11917 case RSHIFT_EXPR:
11918 case URSHIFT_EXPR:
11919 case BIT_AND_EXPR:
11920 case BIT_XOR_EXPR:
11921 case BIT_IOR_EXPR:
11922 case TRUNC_MOD_EXPR:
11923 case TRUNC_DIV_EXPR:
11924 case RDIV_EXPR:
11925 case TRUTH_ANDIF_EXPR:
11926 case TRUTH_ORIF_EXPR:
11927 case EQ_EXPR:
11928 case NE_EXPR:
11929 case GT_EXPR:
11930 case GE_EXPR:
11931 case LT_EXPR:
11932 case LE_EXPR:
11933 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11934 knows how to handle those cases. */
11935 wfl_op1 = TREE_OPERAND (node, 0);
11936 wfl_op2 = TREE_OPERAND (node, 1);
11938 CAN_COMPLETE_NORMALLY (node) = 1;
11939 /* Don't complete string nodes if dealing with the PLUS operand. */
11940 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11942 nn = java_complete_tree (wfl_op1);
11943 if (nn == error_mark_node)
11944 return error_mark_node;
11946 TREE_OPERAND (node, 0) = nn;
11948 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11950 nn = java_complete_tree (wfl_op2);
11951 if (nn == error_mark_node)
11952 return error_mark_node;
11954 TREE_OPERAND (node, 1) = nn;
11956 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11958 case INSTANCEOF_EXPR:
11959 wfl_op1 = TREE_OPERAND (node, 0);
11960 COMPLETE_CHECK_OP_0 (node);
11961 if (flag_emit_xref)
11963 TREE_TYPE (node) = boolean_type_node;
11964 return node;
11966 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11968 case UNARY_PLUS_EXPR:
11969 case NEGATE_EXPR:
11970 case TRUTH_NOT_EXPR:
11971 case BIT_NOT_EXPR:
11972 case PREDECREMENT_EXPR:
11973 case PREINCREMENT_EXPR:
11974 case POSTDECREMENT_EXPR:
11975 case POSTINCREMENT_EXPR:
11976 case CONVERT_EXPR:
11977 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11978 how to handle those cases. */
11979 wfl_op1 = TREE_OPERAND (node, 0);
11980 CAN_COMPLETE_NORMALLY (node) = 1;
11981 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11982 if (TREE_OPERAND (node, 0) == error_mark_node)
11983 return error_mark_node;
11984 node = patch_unaryop (node, wfl_op1);
11985 CAN_COMPLETE_NORMALLY (node) = 1;
11986 break;
11988 case ARRAY_REF:
11989 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11990 how to handle those cases. */
11991 wfl_op1 = TREE_OPERAND (node, 0);
11992 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11993 if (TREE_OPERAND (node, 0) == error_mark_node)
11994 return error_mark_node;
11995 if (!flag_emit_class_files && !flag_emit_xref)
11996 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
11997 /* The same applies to wfl_op2 */
11998 wfl_op2 = TREE_OPERAND (node, 1);
11999 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12000 if (TREE_OPERAND (node, 1) == error_mark_node)
12001 return error_mark_node;
12002 if (!flag_emit_class_files && !flag_emit_xref)
12003 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12004 return patch_array_ref (node);
12006 case RECORD_TYPE:
12007 return node;;
12009 case COMPONENT_REF:
12010 /* The first step in the re-write of qualified name handling. FIXME.
12011 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12012 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12013 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12015 tree name = TREE_OPERAND (node, 1);
12016 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12017 if (field == NULL_TREE)
12019 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
12020 return error_mark_node;
12022 if (! FIELD_STATIC (field))
12024 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
12025 return error_mark_node;
12027 return field;
12029 else
12030 abort ();
12031 break;
12033 case THIS_EXPR:
12034 /* Can't use THIS in a static environment */
12035 if (!current_this)
12037 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12038 parse_error_context (wfl_operator,
12039 "Keyword `this' used outside allowed context");
12040 TREE_TYPE (node) = error_mark_node;
12041 return error_mark_node;
12043 if (ctxp->explicit_constructor_p)
12045 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12046 parse_error_context
12047 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
12048 TREE_TYPE (node) = error_mark_node;
12049 return error_mark_node;
12051 return current_this;
12053 case CLASS_LITERAL:
12054 CAN_COMPLETE_NORMALLY (node) = 1;
12055 node = patch_incomplete_class_ref (node);
12056 if (node == error_mark_node)
12057 return error_mark_node;
12058 break;
12060 default:
12061 CAN_COMPLETE_NORMALLY (node) = 1;
12062 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12063 and it's time to turn it into the appropriate String object */
12064 if ((nn = patch_string (node)))
12065 node = nn;
12066 else
12067 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12069 return node;
12072 /* Complete function call's argument. Return a non zero value is an
12073 error was found. */
12075 static int
12076 complete_function_arguments (node)
12077 tree node;
12079 int flag = 0;
12080 tree cn;
12082 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12083 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12085 tree wfl = TREE_VALUE (cn), parm, temp;
12086 parm = java_complete_tree (wfl);
12088 if (parm == error_mark_node)
12090 flag = 1;
12091 continue;
12093 /* If have a string literal that we haven't transformed yet or a
12094 crafted string buffer, as a result of use of the the String
12095 `+' operator. Build `parm.toString()' and expand it. */
12096 if ((temp = patch_string (parm)))
12097 parm = temp;
12098 /* Inline PRIMTYPE.TYPE read access */
12099 parm = maybe_build_primttype_type_ref (parm, wfl);
12101 TREE_VALUE (cn) = parm;
12103 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12104 return flag;
12107 /* Sometimes (for loops and variable initialized during their
12108 declaration), we want to wrap a statement around a WFL and turn it
12109 debugable. */
12111 static tree
12112 build_debugable_stmt (location, stmt)
12113 int location;
12114 tree stmt;
12116 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12118 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12119 EXPR_WFL_LINECOL (stmt) = location;
12121 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12122 return stmt;
12125 static tree
12126 build_expr_block (body, decls)
12127 tree body, decls;
12129 tree node = make_node (BLOCK);
12130 BLOCK_EXPR_DECLS (node) = decls;
12131 BLOCK_EXPR_BODY (node) = body;
12132 if (body)
12133 TREE_TYPE (node) = TREE_TYPE (body);
12134 TREE_SIDE_EFFECTS (node) = 1;
12135 return node;
12138 /* Create a new function block and link it approriately to current
12139 function block chain */
12141 static tree
12142 enter_block ()
12144 tree b = build_expr_block (NULL_TREE, NULL_TREE);
12146 /* Link block B supercontext to the previous block. The current
12147 function DECL is used as supercontext when enter_a_block is called
12148 for the first time for a given function. The current function body
12149 (DECL_FUNCTION_BODY) is set to be block B. */
12151 tree fndecl = current_function_decl;
12153 if (!fndecl) {
12154 BLOCK_SUPERCONTEXT (b) = current_static_block;
12155 current_static_block = b;
12158 else if (!DECL_FUNCTION_BODY (fndecl))
12160 BLOCK_SUPERCONTEXT (b) = fndecl;
12161 DECL_FUNCTION_BODY (fndecl) = b;
12163 else
12165 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12166 DECL_FUNCTION_BODY (fndecl) = b;
12168 return b;
12171 /* Exit a block by changing the current function body
12172 (DECL_FUNCTION_BODY) to the current block super context, only if
12173 the block being exited isn't the method's top level one. */
12175 static tree
12176 exit_block ()
12178 tree b;
12179 if (current_function_decl)
12181 b = DECL_FUNCTION_BODY (current_function_decl);
12182 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12183 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12185 else
12187 b = current_static_block;
12189 if (BLOCK_SUPERCONTEXT (b))
12190 current_static_block = BLOCK_SUPERCONTEXT (b);
12192 return b;
12195 /* Lookup for NAME in the nested function's blocks, all the way up to
12196 the current toplevel one. It complies with Java's local variable
12197 scoping rules. */
12199 static tree
12200 lookup_name_in_blocks (name)
12201 tree name;
12203 tree b = GET_CURRENT_BLOCK (current_function_decl);
12205 while (b != current_function_decl)
12207 tree current;
12209 /* Paranoid sanity check. To be removed */
12210 if (TREE_CODE (b) != BLOCK)
12211 abort ();
12213 for (current = BLOCK_EXPR_DECLS (b); current;
12214 current = TREE_CHAIN (current))
12215 if (DECL_NAME (current) == name)
12216 return current;
12217 b = BLOCK_SUPERCONTEXT (b);
12219 return NULL_TREE;
12222 static void
12223 maybe_absorb_scoping_blocks ()
12225 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12227 tree b = exit_block ();
12228 java_method_add_stmt (current_function_decl, b);
12229 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
12234 /* This section of the source is reserved to build_* functions that
12235 are building incomplete tree nodes and the patch_* functions that
12236 are completing them. */
12238 /* Wrap a non WFL node around a WFL. */
12240 static tree
12241 build_wfl_wrap (node, location)
12242 tree node;
12243 int location;
12245 tree wfl, node_to_insert = node;
12247 /* We want to process THIS . xxx symbolicaly, to keep it consistent
12248 with the way we're processing SUPER. A THIS from a primary as a
12249 different form than a SUPER. Turn THIS into something symbolic */
12250 if (TREE_CODE (node) == THIS_EXPR)
12251 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12252 else
12253 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12255 EXPR_WFL_LINECOL (wfl) = location;
12256 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12257 return wfl;
12260 /* Build a super() constructor invocation. Returns empty_stmt_node if
12261 we're currently dealing with the class java.lang.Object. */
12263 static tree
12264 build_super_invocation (mdecl)
12265 tree mdecl;
12267 if (DECL_CONTEXT (mdecl) == object_type_node)
12268 return empty_stmt_node;
12269 else
12271 tree super_wfl = build_wfl_node (super_identifier_node);
12272 tree a = NULL_TREE, t;
12273 /* If we're dealing with an anonymous class, pass the arguments
12274 of the crafted constructor along. */
12275 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12277 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12278 for (; t != end_params_node; t = TREE_CHAIN (t))
12279 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12281 return build_method_invocation (super_wfl, a);
12285 /* Build a SUPER/THIS qualified method invocation. */
12287 static tree
12288 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12289 int use_this;
12290 tree name, args;
12291 int lloc, rloc;
12293 tree invok;
12294 tree wfl =
12295 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12296 EXPR_WFL_LINECOL (wfl) = lloc;
12297 invok = build_method_invocation (name, args);
12298 return make_qualified_primary (wfl, invok, rloc);
12301 /* Build an incomplete CALL_EXPR node. */
12303 static tree
12304 build_method_invocation (name, args)
12305 tree name;
12306 tree args;
12308 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12309 TREE_SIDE_EFFECTS (call) = 1;
12310 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12311 return call;
12314 /* Build an incomplete new xxx(...) node. */
12316 static tree
12317 build_new_invocation (name, args)
12318 tree name, args;
12320 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12321 TREE_SIDE_EFFECTS (call) = 1;
12322 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12323 return call;
12326 /* Build an incomplete assignment expression. */
12328 static tree
12329 build_assignment (op, op_location, lhs, rhs)
12330 int op, op_location;
12331 tree lhs, rhs;
12333 tree assignment;
12334 /* Build the corresponding binop if we deal with a Compound
12335 Assignment operator. Mark the binop sub-tree as part of a
12336 Compound Assignment expression */
12337 if (op != ASSIGN_TK)
12339 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12340 COMPOUND_ASSIGN_P (rhs) = 1;
12342 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12343 TREE_SIDE_EFFECTS (assignment) = 1;
12344 EXPR_WFL_LINECOL (assignment) = op_location;
12345 return assignment;
12348 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12350 char *
12351 print_int_node (node)
12352 tree node;
12354 static char buffer [80];
12355 if (TREE_CONSTANT_OVERFLOW (node))
12356 sprintf (buffer, "<overflow>");
12358 if (TREE_INT_CST_HIGH (node) == 0)
12359 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12360 TREE_INT_CST_LOW (node));
12361 else if (TREE_INT_CST_HIGH (node) == -1
12362 && TREE_INT_CST_LOW (node) != 0)
12364 buffer [0] = '-';
12365 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12366 -TREE_INT_CST_LOW (node));
12368 else
12369 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12370 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12372 return buffer;
12377 /* This section of the code handle assignment check with FINAL
12378 variables. */
12380 static void
12381 reset_static_final_variable_assignment_flag (class)
12382 tree class;
12384 tree field;
12385 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12386 if (CLASS_FINAL_VARIABLE_P (field))
12387 DECL_FIELD_FINAL_LIIC (field) = 0;
12390 /* Figure whether all final static variable have been initialized. */
12392 static void
12393 check_static_final_variable_assignment_flag (class)
12394 tree class;
12396 tree field;
12398 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12399 if (CLASS_FINAL_VARIABLE_P (field)
12400 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12401 parse_error_context
12402 (DECL_FIELD_FINAL_WFL (field),
12403 "Blank static final variable `%s' may not have been initialized",
12404 IDENTIFIER_POINTER (DECL_NAME (field)));
12407 /* This function marks all final variable locally unassigned. */
12409 static void
12410 reset_final_variable_local_assignment_flag (class)
12411 tree class;
12413 tree field;
12414 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12415 if (FINAL_VARIABLE_P (field))
12416 DECL_FIELD_FINAL_LIIC (field) = 0;
12419 /* Figure whether all final variables have beem initialized in MDECL
12420 and mark MDECL accordingly. */
12422 static void
12423 check_final_variable_local_assignment_flag (class, mdecl)
12424 tree class;
12425 tree mdecl;
12427 tree field;
12428 int initialized = 0;
12429 int non_initialized = 0;
12431 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12432 return;
12434 /* First find out whether all final variables or no final variable
12435 are initialized in this ctor. We don't take into account final
12436 variable that have been initialized upon declaration. */
12437 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12438 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12440 if (DECL_FIELD_FINAL_LIIC (field))
12441 initialized++;
12442 else
12443 non_initialized++;
12446 /* There were no non initialized variable and no initialized variable.
12447 This ctor is fine. */
12448 if (!non_initialized && !initialized)
12449 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12450 /* If no variables have been initialized, that fine. We'll check
12451 later whether this ctor calls a constructor which initializes
12452 them. We mark the ctor as not initializing all its finals. */
12453 else if (initialized == 0)
12454 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12455 /* If we have a mixed bag, then we have a problem. We need to report
12456 all the variables we're not initializing. */
12457 else if (initialized && non_initialized)
12459 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12460 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12461 if (FIELD_FINAL (field)
12462 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12464 parse_error_context
12465 (lookup_cl (mdecl),
12466 "Blank final variable `%s' may not have been initialized in this constructor",
12467 IDENTIFIER_POINTER (DECL_NAME (field)));
12468 DECL_FIELD_FINAL_IERR (field) = 1;
12471 /* Otherwise we know this ctor is initializing all its final
12472 variable. We mark it so. */
12473 else
12474 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12477 /* This function recurses in a simple what through STMT and stops when
12478 it finds a constructor call. It then verifies that the called
12479 constructor initialized its final properly. Return 1 upon success,
12480 0 or -1 otherwise. */
12482 static int
12483 check_final_variable_indirect_assignment (stmt)
12484 tree stmt;
12486 int res;
12487 switch (TREE_CODE (stmt))
12489 case EXPR_WITH_FILE_LOCATION:
12490 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12491 case COMPOUND_EXPR:
12492 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12493 if (res)
12494 return res;
12495 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12496 case SAVE_EXPR:
12497 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12498 case CALL_EXPR:
12500 tree decl = TREE_OPERAND (stmt, 0);
12501 tree fbody;
12503 if (TREE_CODE (decl) != FUNCTION_DECL)
12504 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12505 if (TREE_CODE (decl) != FUNCTION_DECL)
12506 abort ();
12507 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12508 return 1;
12509 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12510 return -1;
12511 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12512 if (fbody == error_mark_node)
12513 return -1;
12514 fbody = BLOCK_EXPR_BODY (fbody);
12515 return check_final_variable_indirect_assignment (fbody);
12517 default:
12518 break;
12520 return 0;
12523 /* This is the last chance to catch a final variable initialization
12524 problem. This routine will report an error if a final variable was
12525 never (globally) initialized and never reported as not having been
12526 initialized properly. */
12528 static void
12529 check_final_variable_global_assignment_flag (class)
12530 tree class;
12532 tree field, mdecl;
12533 int nnctor = 0;
12535 /* We go through all natural ctors and see whether they're
12536 initializing all their final variables or not. */
12537 current_function_decl = NULL_TREE; /* For the error report. */
12538 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12539 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12541 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12543 /* It doesn't. Maybe it calls a constructor that initializes
12544 them. find out. */
12545 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12546 if (fbody == error_mark_node)
12547 continue;
12548 fbody = BLOCK_EXPR_BODY (fbody);
12549 if (check_final_variable_indirect_assignment (fbody) == 1)
12551 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12552 nnctor++;
12554 else
12555 parse_error_context
12556 (lookup_cl (mdecl),
12557 "Final variable initialization error in this constructor");
12559 else
12560 nnctor++;
12563 /* Finally we catch final variables that never were initialized */
12564 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12565 if (FINAL_VARIABLE_P (field)
12566 /* If the field wasn't initialized upon declaration */
12567 && !DECL_FIELD_FINAL_IUD (field)
12568 /* There wasn't natural ctor in which the field could have been
12569 initialized */
12570 && !nnctor
12571 /* If we never reported a problem with this field */
12572 && !DECL_FIELD_FINAL_IERR (field))
12574 current_function_decl = NULL;
12575 parse_error_context
12576 (DECL_FIELD_FINAL_WFL (field),
12577 "Final variable `%s' hasn't been initialized upon its declaration",
12578 IDENTIFIER_POINTER (DECL_NAME (field)));
12583 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12584 context. */
12586 static int
12587 check_final_assignment (lvalue, wfl)
12588 tree lvalue, wfl;
12590 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12591 return 0;
12593 if (TREE_CODE (lvalue) == COMPONENT_REF
12594 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12595 lvalue = TREE_OPERAND (lvalue, 1);
12597 if (!FIELD_FINAL (lvalue))
12598 return 0;
12600 /* Now the logic. We can modify a final VARIABLE:
12601 1) in finit$, (its declaration was followed by an initialization,)
12602 2) consistently in each natural ctor, if it wasn't initialized in
12603 finit$ or once in <clinit>. In any other cases, an error should be
12604 reported. */
12605 if (DECL_FINIT_P (current_function_decl))
12607 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12608 return 0;
12611 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12612 /* Only if it wasn't given a value upon initialization */
12613 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12614 /* If it was never assigned a value in this constructor */
12615 && !DECL_FIELD_FINAL_LIIC (lvalue))
12617 /* Turn the locally assigned flag on, it will be checked later
12618 on to point out at discrepancies. */
12619 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12620 if (DECL_CLINIT_P (current_function_decl))
12621 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12622 return 0;
12625 /* Other problems should be reported right away. */
12626 parse_error_context
12627 (wfl, "Can't %sassign a value to the final variable `%s'",
12628 (FIELD_STATIC (lvalue) ? "re" : ""),
12629 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12631 /* Note that static field can be initialized once and only once. */
12632 if (FIELD_STATIC (lvalue))
12633 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12635 return 1;
12638 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12639 read. This is needed to avoid circularities in the implementation
12640 of these fields in libjava. */
12642 static tree
12643 maybe_build_primttype_type_ref (rhs, wfl)
12644 tree rhs, wfl;
12646 tree to_return = NULL_TREE;
12647 tree rhs_type = TREE_TYPE (rhs);
12648 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12650 tree n = TREE_OPERAND (rhs, 1);
12651 if (TREE_CODE (n) == VAR_DECL
12652 && DECL_NAME (n) == TYPE_identifier_node
12653 && rhs_type == class_ptr_type
12654 && TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION
12655 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
12657 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
12658 if (!strncmp (self_name, "java.lang.", 10))
12659 to_return = build_primtype_type_ref (self_name);
12662 return (to_return ? to_return : rhs );
12665 /* 15.25 Assignment operators. */
12667 static tree
12668 patch_assignment (node, wfl_op1, wfl_op2)
12669 tree node;
12670 tree wfl_op1;
12671 tree wfl_op2;
12673 tree rhs = TREE_OPERAND (node, 1);
12674 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12675 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12676 int error_found = 0;
12677 int lvalue_from_array = 0;
12679 /* Can't assign to a (blank) final. */
12680 if (check_final_assignment (lvalue, wfl_op1))
12681 error_found = 1;
12683 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12685 /* Lhs can be a named variable */
12686 if (JDECL_P (lvalue))
12688 lhs_type = TREE_TYPE (lvalue);
12690 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12691 comment on reason why */
12692 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12694 lhs_type = TREE_TYPE (lvalue);
12695 lvalue_from_array = 1;
12697 /* Or a field access */
12698 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12699 lhs_type = TREE_TYPE (lvalue);
12700 /* Or a function return slot */
12701 else if (TREE_CODE (lvalue) == RESULT_DECL)
12702 lhs_type = TREE_TYPE (lvalue);
12703 /* Otherwise, we might want to try to write into an optimized static
12704 final, this is an of a different nature, reported further on. */
12705 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12706 && resolve_expression_name (wfl_op1, &llvalue))
12708 if (!error_found && check_final_assignment (llvalue, wfl_op1))
12710 /* What we should do instead is resetting the all the flags
12711 previously set, exchange lvalue for llvalue and continue. */
12712 error_found = 1;
12713 return error_mark_node;
12715 else
12716 lhs_type = TREE_TYPE (lvalue);
12718 else
12720 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12721 error_found = 1;
12724 rhs_type = TREE_TYPE (rhs);
12725 /* 5.1 Try the assignment conversion for builtin type. */
12726 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12728 /* 5.2 If it failed, try a reference conversion */
12729 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12730 lhs_type = promote_type (rhs_type);
12732 /* 15.25.2 If we have a compound assignment, convert RHS into the
12733 type of the LHS */
12734 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12735 new_rhs = convert (lhs_type, rhs);
12737 /* Explicit cast required. This is an error */
12738 if (!new_rhs)
12740 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12741 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12742 tree wfl;
12743 char operation [32]; /* Max size known */
12745 /* If the assignment is part of a declaration, we use the WFL of
12746 the declared variable to point out the error and call it a
12747 declaration problem. If the assignment is a genuine =
12748 operator, we call is a operator `=' problem, otherwise we
12749 call it an assignment problem. In both of these last cases,
12750 we use the WFL of the operator to indicate the error. */
12752 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12754 wfl = wfl_op1;
12755 strcpy (operation, "declaration");
12757 else
12759 wfl = wfl_operator;
12760 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12761 strcpy (operation, "assignment");
12762 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12763 strcpy (operation, "`return'");
12764 else
12765 strcpy (operation, "`='");
12768 if (!valid_cast_to_p (rhs_type, lhs_type))
12769 parse_error_context
12770 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12771 operation, t1, t2);
12772 else
12773 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12774 operation, t1, t2);
12775 free (t1); free (t2);
12776 error_found = 1;
12779 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12780 if (new_rhs)
12781 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
12783 if (error_found)
12784 return error_mark_node;
12786 /* 10.10: Array Store Exception runtime check */
12787 if (!flag_emit_class_files
12788 && !flag_emit_xref
12789 && lvalue_from_array
12790 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12792 tree check;
12793 tree base = lvalue;
12795 /* We need to retrieve the right argument for
12796 _Jv_CheckArrayStore. This is somewhat complicated by bounds
12797 and null pointer checks, both of which wrap the operand in
12798 one layer of COMPOUND_EXPR. */
12799 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12800 base = TREE_OPERAND (lvalue, 0);
12801 else
12803 tree op = TREE_OPERAND (base, 0);
12805 /* We can have a SAVE_EXPR here when doing String +=. */
12806 if (TREE_CODE (op) == SAVE_EXPR)
12807 op = TREE_OPERAND (op, 0);
12808 if (flag_bounds_check)
12809 base = TREE_OPERAND (TREE_OPERAND (op, 1), 0);
12810 else
12811 base = TREE_OPERAND (op, 0);
12814 /* Build the invocation of _Jv_CheckArrayStore */
12815 new_rhs = save_expr (new_rhs);
12816 check = build (CALL_EXPR, void_type_node,
12817 build_address_of (soft_checkarraystore_node),
12818 tree_cons (NULL_TREE, base,
12819 build_tree_list (NULL_TREE, new_rhs)),
12820 NULL_TREE);
12821 TREE_SIDE_EFFECTS (check) = 1;
12823 /* We have to decide on an insertion point */
12824 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12826 tree t;
12827 if (flag_bounds_check)
12829 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12830 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12831 build (COMPOUND_EXPR, void_type_node, t, check);
12833 else
12834 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12835 check, TREE_OPERAND (lvalue, 1));
12837 else if (flag_bounds_check)
12839 tree hook = lvalue;
12840 tree compound = TREE_OPERAND (lvalue, 0);
12841 tree bound_check, new_compound;
12843 if (TREE_CODE (compound) == SAVE_EXPR)
12845 compound = TREE_OPERAND (compound, 0);
12846 hook = TREE_OPERAND (hook, 0);
12849 /* Find the array bound check, hook the original array access. */
12850 bound_check = TREE_OPERAND (compound, 0);
12851 TREE_OPERAND (hook, 0) = TREE_OPERAND (compound, 1);
12853 /* Make sure the bound check will happen before the store check */
12854 new_compound =
12855 build (COMPOUND_EXPR, void_type_node, bound_check, check);
12857 /* Re-assemble the augmented array access. */
12858 lvalue = build (COMPOUND_EXPR, lhs_type, new_compound, lvalue);
12860 else
12861 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12864 /* Final locals can be used as case values in switch
12865 statement. Prepare them for this eventuality. */
12866 if (TREE_CODE (lvalue) == VAR_DECL
12867 && LOCAL_FINAL_P (lvalue)
12868 && TREE_CONSTANT (new_rhs)
12869 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12870 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12873 TREE_CONSTANT (lvalue) = 1;
12874 DECL_INITIAL (lvalue) = new_rhs;
12877 TREE_OPERAND (node, 0) = lvalue;
12878 TREE_OPERAND (node, 1) = new_rhs;
12879 TREE_TYPE (node) = lhs_type;
12880 return node;
12883 /* Check that type SOURCE can be cast into type DEST. If the cast
12884 can't occur at all, return 0 otherwise 1. This function is used to
12885 produce accurate error messages on the reasons why an assignment
12886 failed. */
12888 static tree
12889 try_reference_assignconv (lhs_type, rhs)
12890 tree lhs_type, rhs;
12892 tree new_rhs = NULL_TREE;
12893 tree rhs_type = TREE_TYPE (rhs);
12895 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12897 /* `null' may be assigned to any reference type */
12898 if (rhs == null_pointer_node)
12899 new_rhs = null_pointer_node;
12900 /* Try the reference assignment conversion */
12901 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12902 new_rhs = rhs;
12903 /* This is a magic assignment that we process differently */
12904 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
12905 new_rhs = rhs;
12907 return new_rhs;
12910 /* Check that RHS can be converted into LHS_TYPE by the assignment
12911 conversion (5.2), for the cases of RHS being a builtin type. Return
12912 NULL_TREE if the conversion fails or if because RHS isn't of a
12913 builtin type. Return a converted RHS if the conversion is possible. */
12915 static tree
12916 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12917 tree wfl_op1, lhs_type, rhs;
12919 tree new_rhs = NULL_TREE;
12920 tree rhs_type = TREE_TYPE (rhs);
12922 /* Handle boolean specially. */
12923 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12924 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12926 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12927 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12928 new_rhs = rhs;
12931 /* Zero accepted everywhere */
12932 else if (TREE_CODE (rhs) == INTEGER_CST
12933 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12934 && JPRIMITIVE_TYPE_P (rhs_type))
12935 new_rhs = convert (lhs_type, rhs);
12937 /* 5.1.1 Try Identity Conversion,
12938 5.1.2 Try Widening Primitive Conversion */
12939 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12940 new_rhs = convert (lhs_type, rhs);
12942 /* Try a narrowing primitive conversion (5.1.3):
12943 - expression is a constant expression of type int AND
12944 - variable is byte, short or char AND
12945 - The value of the expression is representable in the type of the
12946 variable */
12947 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12948 && (lhs_type == byte_type_node || lhs_type == char_type_node
12949 || lhs_type == short_type_node))
12951 if (int_fits_type_p (rhs, lhs_type))
12952 new_rhs = convert (lhs_type, rhs);
12953 else if (wfl_op1) /* Might be called with a NULL */
12954 parse_warning_context
12955 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12956 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12957 /* Reported a warning that will turn into an error further
12958 down, so we don't return */
12961 return new_rhs;
12964 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12965 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12966 0 is the conversion test fails. This implements parts the method
12967 invocation convertion (5.3). */
12969 static int
12970 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12971 tree lhs_type, rhs_type;
12973 /* 5.1.1: This is the identity conversion part. */
12974 if (lhs_type == rhs_type)
12975 return 1;
12977 /* Reject non primitive types and boolean conversions. */
12978 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12979 return 0;
12981 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12982 than a char can't be converted into a char. Short can't too, but
12983 the < test below takes care of that */
12984 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12985 return 0;
12987 /* Accept all promoted type here. Note, we can't use <= in the test
12988 below, because we still need to bounce out assignments of short
12989 to char and the likes */
12990 if (lhs_type == int_type_node
12991 && (rhs_type == promoted_byte_type_node
12992 || rhs_type == promoted_short_type_node
12993 || rhs_type == promoted_char_type_node
12994 || rhs_type == promoted_boolean_type_node))
12995 return 1;
12997 /* From here, an integral is widened if its precision is smaller
12998 than the precision of the LHS or if the LHS is a floating point
12999 type, or the RHS is a float and the RHS a double. */
13000 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13001 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13002 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13003 || (rhs_type == float_type_node && lhs_type == double_type_node))
13004 return 1;
13006 return 0;
13009 /* Check that something of SOURCE type can be assigned or cast to
13010 something of DEST type at runtime. Return 1 if the operation is
13011 valid, 0 otherwise. If CAST is set to 1, we're treating the case
13012 were SOURCE is cast into DEST, which borrows a lot of the
13013 assignment check. */
13015 static int
13016 valid_ref_assignconv_cast_p (source, dest, cast)
13017 tree source;
13018 tree dest;
13019 int cast;
13021 /* SOURCE or DEST might be null if not from a declared entity. */
13022 if (!source || !dest)
13023 return 0;
13024 if (JNULLP_TYPE_P (source))
13025 return 1;
13026 if (TREE_CODE (source) == POINTER_TYPE)
13027 source = TREE_TYPE (source);
13028 if (TREE_CODE (dest) == POINTER_TYPE)
13029 dest = TREE_TYPE (dest);
13031 /* If source and dest are being compiled from bytecode, they may need to
13032 be loaded. */
13033 if (CLASS_P (source) && !CLASS_LOADED_P (source))
13035 load_class (source, 1);
13036 safe_layout_class (source);
13038 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13040 load_class (dest, 1);
13041 safe_layout_class (dest);
13044 /* Case where SOURCE is a class type */
13045 if (TYPE_CLASS_P (source))
13047 if (TYPE_CLASS_P (dest))
13048 return (source == dest
13049 || inherits_from_p (source, dest)
13050 || (cast && inherits_from_p (dest, source)));
13051 if (TYPE_INTERFACE_P (dest))
13053 /* If doing a cast and SOURCE is final, the operation is
13054 always correct a compile time (because even if SOURCE
13055 does not implement DEST, a subclass of SOURCE might). */
13056 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13057 return 1;
13058 /* Otherwise, SOURCE must implement DEST */
13059 return interface_of_p (dest, source);
13061 /* DEST is an array, cast permited if SOURCE is of Object type */
13062 return (cast && source == object_type_node ? 1 : 0);
13064 if (TYPE_INTERFACE_P (source))
13066 if (TYPE_CLASS_P (dest))
13068 /* If not casting, DEST must be the Object type */
13069 if (!cast)
13070 return dest == object_type_node;
13071 /* We're doing a cast. The cast is always valid is class
13072 DEST is not final, otherwise, DEST must implement SOURCE */
13073 else if (!CLASS_FINAL (TYPE_NAME (dest)))
13074 return 1;
13075 else
13076 return interface_of_p (source, dest);
13078 if (TYPE_INTERFACE_P (dest))
13080 /* If doing a cast, then if SOURCE and DEST contain method
13081 with the same signature but different return type, then
13082 this is a (compile time) error */
13083 if (cast)
13085 tree method_source, method_dest;
13086 tree source_type;
13087 tree source_sig;
13088 tree source_name;
13089 for (method_source = TYPE_METHODS (source); method_source;
13090 method_source = TREE_CHAIN (method_source))
13092 source_sig =
13093 build_java_argument_signature (TREE_TYPE (method_source));
13094 source_type = TREE_TYPE (TREE_TYPE (method_source));
13095 source_name = DECL_NAME (method_source);
13096 for (method_dest = TYPE_METHODS (dest);
13097 method_dest; method_dest = TREE_CHAIN (method_dest))
13098 if (source_sig ==
13099 build_java_argument_signature (TREE_TYPE (method_dest))
13100 && source_name == DECL_NAME (method_dest)
13101 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13102 return 0;
13104 return 1;
13106 else
13107 return source == dest || interface_of_p (dest, source);
13109 else
13111 /* Array */
13112 return (cast
13113 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
13114 || (DECL_NAME (TYPE_NAME (source))
13115 == java_io_serializable)));
13118 if (TYPE_ARRAY_P (source))
13120 if (TYPE_CLASS_P (dest))
13121 return dest == object_type_node;
13122 /* Can't cast an array to an interface unless the interface is
13123 java.lang.Cloneable or java.io.Serializable. */
13124 if (TYPE_INTERFACE_P (dest))
13125 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
13126 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
13127 else /* Arrays */
13129 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13130 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13132 /* In case of severe errors, they turn out null */
13133 if (!dest_element_type || !source_element_type)
13134 return 0;
13135 if (source_element_type == dest_element_type)
13136 return 1;
13137 return valid_ref_assignconv_cast_p (source_element_type,
13138 dest_element_type, cast);
13140 return 0;
13142 return 0;
13145 static int
13146 valid_cast_to_p (source, dest)
13147 tree source;
13148 tree dest;
13150 if (TREE_CODE (source) == POINTER_TYPE)
13151 source = TREE_TYPE (source);
13152 if (TREE_CODE (dest) == POINTER_TYPE)
13153 dest = TREE_TYPE (dest);
13155 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13156 return valid_ref_assignconv_cast_p (source, dest, 1);
13158 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13159 return 1;
13161 else if (TREE_CODE (source) == BOOLEAN_TYPE
13162 && TREE_CODE (dest) == BOOLEAN_TYPE)
13163 return 1;
13165 return 0;
13168 static tree
13169 do_unary_numeric_promotion (arg)
13170 tree arg;
13172 tree type = TREE_TYPE (arg);
13173 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13174 || TREE_CODE (type) == CHAR_TYPE)
13175 arg = convert (int_type_node, arg);
13176 return arg;
13179 /* Return a non zero value if SOURCE can be converted into DEST using
13180 the method invocation conversion rule (5.3). */
13181 static int
13182 valid_method_invocation_conversion_p (dest, source)
13183 tree dest, source;
13185 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13186 && valid_builtin_assignconv_identity_widening_p (dest, source))
13187 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13188 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13189 && valid_ref_assignconv_cast_p (source, dest, 0)));
13192 /* Build an incomplete binop expression. */
13194 static tree
13195 build_binop (op, op_location, op1, op2)
13196 enum tree_code op;
13197 int op_location;
13198 tree op1, op2;
13200 tree binop = build (op, NULL_TREE, op1, op2);
13201 TREE_SIDE_EFFECTS (binop) = 1;
13202 /* Store the location of the operator, for better error report. The
13203 string of the operator will be rebuild based on the OP value. */
13204 EXPR_WFL_LINECOL (binop) = op_location;
13205 return binop;
13208 /* Build the string of the operator retained by NODE. If NODE is part
13209 of a compound expression, add an '=' at the end of the string. This
13210 function is called when an error needs to be reported on an
13211 operator. The string is returned as a pointer to a static character
13212 buffer. */
13214 static char *
13215 operator_string (node)
13216 tree node;
13218 #define BUILD_OPERATOR_STRING(S) \
13220 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13221 return buffer; \
13224 static char buffer [10];
13225 switch (TREE_CODE (node))
13227 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13228 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13229 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13230 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13231 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13232 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13233 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13234 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13235 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13236 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13237 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13238 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13239 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13240 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13241 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13242 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13243 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13244 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13245 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13246 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13247 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13248 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13249 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13250 case PREINCREMENT_EXPR: /* Fall through */
13251 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13252 case PREDECREMENT_EXPR: /* Fall through */
13253 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13254 default:
13255 internal_error ("unregistered operator %s",
13256 tree_code_name [TREE_CODE (node)]);
13258 return NULL;
13259 #undef BUILD_OPERATOR_STRING
13262 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13264 static int
13265 java_decl_equiv (var_acc1, var_acc2)
13266 tree var_acc1, var_acc2;
13268 if (JDECL_P (var_acc1))
13269 return (var_acc1 == var_acc2);
13271 return (TREE_CODE (var_acc1) == COMPONENT_REF
13272 && TREE_CODE (var_acc2) == COMPONENT_REF
13273 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13274 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13275 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13278 /* Return a non zero value if CODE is one of the operators that can be
13279 used in conjunction with the `=' operator in a compound assignment. */
13281 static int
13282 binop_compound_p (code)
13283 enum tree_code code;
13285 int i;
13286 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13287 if (binop_lookup [i] == code)
13288 break;
13290 return i < BINOP_COMPOUND_CANDIDATES;
13293 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13295 static tree
13296 java_refold (t)
13297 tree t;
13299 tree c, b, ns, decl;
13301 if (TREE_CODE (t) != MODIFY_EXPR)
13302 return t;
13304 c = TREE_OPERAND (t, 1);
13305 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13306 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13307 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13308 return t;
13310 /* Now the left branch of the binary operator. */
13311 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13312 if (! (b && TREE_CODE (b) == NOP_EXPR
13313 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13314 return t;
13316 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13317 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13318 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13319 return t;
13321 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13322 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13323 /* It's got to be the an equivalent decl */
13324 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13326 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13327 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13328 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13329 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13330 /* Change the right part of the BINOP_EXPR */
13331 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13334 return t;
13337 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13338 errors but we modify NODE so that it contains the type computed
13339 according to the expression, when it's fixed. Otherwise, we write
13340 error_mark_node as the type. It allows us to further the analysis
13341 of remaining nodes and detects more errors in certain cases. */
13343 static tree
13344 patch_binop (node, wfl_op1, wfl_op2)
13345 tree node;
13346 tree wfl_op1;
13347 tree wfl_op2;
13349 tree op1 = TREE_OPERAND (node, 0);
13350 tree op2 = TREE_OPERAND (node, 1);
13351 tree op1_type = TREE_TYPE (op1);
13352 tree op2_type = TREE_TYPE (op2);
13353 tree prom_type = NULL_TREE, cn;
13354 int code = TREE_CODE (node);
13356 /* If 1, tell the routine that we have to return error_mark_node
13357 after checking for the initialization of the RHS */
13358 int error_found = 0;
13360 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13362 /* If either op<n>_type are NULL, this might be early signs of an
13363 error situation, unless it's too early to tell (in case we're
13364 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13365 correctly so the error can be later on reported accurately. */
13366 if (! (code == PLUS_EXPR || code == NE_EXPR
13367 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13369 tree n;
13370 if (! op1_type)
13372 n = java_complete_tree (op1);
13373 op1_type = TREE_TYPE (n);
13375 if (! op2_type)
13377 n = java_complete_tree (op2);
13378 op2_type = TREE_TYPE (n);
13382 switch (code)
13384 /* 15.16 Multiplicative operators */
13385 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13386 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13387 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13388 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13389 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13391 if (!JNUMERIC_TYPE_P (op1_type))
13392 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13393 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13394 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13395 TREE_TYPE (node) = error_mark_node;
13396 error_found = 1;
13397 break;
13399 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13400 /* Change the division operator if necessary */
13401 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13402 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13404 if (TREE_CODE (prom_type) == INTEGER_TYPE
13405 && flag_use_divide_subroutine
13406 && ! flag_emit_class_files
13407 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13408 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13410 /* This one is more complicated. FLOATs are processed by a
13411 function call to soft_fmod. Duplicate the value of the
13412 COMPOUND_ASSIGN_P flag. */
13413 if (code == TRUNC_MOD_EXPR)
13415 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13416 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13417 TREE_SIDE_EFFECTS (mod)
13418 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13419 return mod;
13421 break;
13423 /* 15.17 Additive Operators */
13424 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13426 /* Operation is valid if either one argument is a string
13427 constant, a String object or a StringBuffer crafted for the
13428 purpose of the a previous usage of the String concatenation
13429 operator */
13431 if (TREE_CODE (op1) == STRING_CST
13432 || TREE_CODE (op2) == STRING_CST
13433 || JSTRING_TYPE_P (op1_type)
13434 || JSTRING_TYPE_P (op2_type)
13435 || IS_CRAFTED_STRING_BUFFER_P (op1)
13436 || IS_CRAFTED_STRING_BUFFER_P (op2))
13437 return build_string_concatenation (op1, op2);
13439 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13440 Numeric Types */
13441 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13443 if (!JNUMERIC_TYPE_P (op1_type))
13444 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13445 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13446 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13447 TREE_TYPE (node) = error_mark_node;
13448 error_found = 1;
13449 break;
13451 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13452 break;
13454 /* 15.18 Shift Operators */
13455 case LSHIFT_EXPR:
13456 case RSHIFT_EXPR:
13457 case URSHIFT_EXPR:
13458 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13460 if (!JINTEGRAL_TYPE_P (op1_type))
13461 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13462 else
13464 if (JNUMERIC_TYPE_P (op2_type))
13465 parse_error_context (wfl_operator,
13466 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13467 operator_string (node),
13468 lang_printable_name (op2_type, 0));
13469 else
13470 parse_error_context (wfl_operator,
13471 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13472 operator_string (node),
13473 lang_printable_name (op2_type, 0));
13475 TREE_TYPE (node) = error_mark_node;
13476 error_found = 1;
13477 break;
13480 /* Unary numeric promotion (5.6.1) is performed on each operand
13481 separatly */
13482 op1 = do_unary_numeric_promotion (op1);
13483 op2 = do_unary_numeric_promotion (op2);
13485 /* The type of the shift expression is the type of the promoted
13486 type of the left-hand operand */
13487 prom_type = TREE_TYPE (op1);
13489 /* Shift int only up to 0x1f and long up to 0x3f */
13490 if (prom_type == int_type_node)
13491 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13492 build_int_2 (0x1f, 0)));
13493 else
13494 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13495 build_int_2 (0x3f, 0)));
13497 /* The >>> operator is a >> operating on unsigned quantities */
13498 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13500 tree to_return;
13501 tree utype = unsigned_type (prom_type);
13502 op1 = convert (utype, op1);
13503 TREE_SET_CODE (node, RSHIFT_EXPR);
13504 TREE_OPERAND (node, 0) = op1;
13505 TREE_OPERAND (node, 1) = op2;
13506 TREE_TYPE (node) = utype;
13507 to_return = convert (prom_type, node);
13508 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13509 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13510 TREE_SIDE_EFFECTS (to_return)
13511 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13512 return to_return;
13514 break;
13516 /* 15.19.1 Type Comparison Operator instaceof */
13517 case INSTANCEOF_EXPR:
13519 TREE_TYPE (node) = boolean_type_node;
13521 if (!(op2_type = resolve_type_during_patch (op2)))
13522 return error_mark_node;
13524 /* The first operand must be a reference type or the null type */
13525 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13526 error_found = 1; /* Error reported further below */
13528 /* The second operand must be a reference type */
13529 if (!JREFERENCE_TYPE_P (op2_type))
13531 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13532 parse_error_context
13533 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13534 lang_printable_name (op2_type, 0));
13535 error_found = 1;
13538 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13540 /* If the first operand is null, the result is always false */
13541 if (op1 == null_pointer_node)
13542 return boolean_false_node;
13543 else if (flag_emit_class_files)
13545 TREE_OPERAND (node, 1) = op2_type;
13546 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13547 return node;
13549 /* Otherwise we have to invoke instance of to figure it out */
13550 else
13551 return build_instanceof (op1, op2_type);
13553 /* There is no way the expression operand can be an instance of
13554 the type operand. This is a compile time error. */
13555 else
13557 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13558 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13559 parse_error_context
13560 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13561 t1, lang_printable_name (op2_type, 0));
13562 free (t1);
13563 error_found = 1;
13566 break;
13568 /* 15.21 Bitwise and Logical Operators */
13569 case BIT_AND_EXPR:
13570 case BIT_XOR_EXPR:
13571 case BIT_IOR_EXPR:
13572 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13573 /* Binary numeric promotion is performed on both operand and the
13574 expression retain that type */
13575 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13577 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13578 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13579 /* The type of the bitwise operator expression is BOOLEAN */
13580 prom_type = boolean_type_node;
13581 else
13583 if (!JINTEGRAL_TYPE_P (op1_type))
13584 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13585 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13586 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13587 TREE_TYPE (node) = error_mark_node;
13588 error_found = 1;
13589 /* Insert a break here if adding thing before the switch's
13590 break for this case */
13592 break;
13594 /* 15.22 Conditional-And Operator */
13595 case TRUTH_ANDIF_EXPR:
13596 /* 15.23 Conditional-Or Operator */
13597 case TRUTH_ORIF_EXPR:
13598 /* Operands must be of BOOLEAN type */
13599 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13600 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13602 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13603 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13604 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13605 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13606 TREE_TYPE (node) = boolean_type_node;
13607 error_found = 1;
13608 break;
13610 /* The type of the conditional operators is BOOLEAN */
13611 prom_type = boolean_type_node;
13612 break;
13614 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13615 case LT_EXPR:
13616 case GT_EXPR:
13617 case LE_EXPR:
13618 case GE_EXPR:
13619 /* The type of each of the operands must be a primitive numeric
13620 type */
13621 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13623 if (!JNUMERIC_TYPE_P (op1_type))
13624 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13625 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13626 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13627 TREE_TYPE (node) = boolean_type_node;
13628 error_found = 1;
13629 break;
13631 /* Binary numeric promotion is performed on the operands */
13632 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13633 /* The type of the relation expression is always BOOLEAN */
13634 prom_type = boolean_type_node;
13635 break;
13637 /* 15.20 Equality Operator */
13638 case EQ_EXPR:
13639 case NE_EXPR:
13640 /* It's time for us to patch the strings. */
13641 if ((cn = patch_string (op1)))
13643 op1 = cn;
13644 op1_type = TREE_TYPE (op1);
13646 if ((cn = patch_string (op2)))
13648 op2 = cn;
13649 op2_type = TREE_TYPE (op2);
13652 /* 15.20.1 Numerical Equality Operators == and != */
13653 /* Binary numeric promotion is performed on the operands */
13654 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13655 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13657 /* 15.20.2 Boolean Equality Operators == and != */
13658 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13659 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13660 ; /* Nothing to do here */
13662 /* 15.20.3 Reference Equality Operators == and != */
13663 /* Types have to be either references or the null type. If
13664 they're references, it must be possible to convert either
13665 type to the other by casting conversion. */
13666 else if (op1 == null_pointer_node || op2 == null_pointer_node
13667 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13668 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13669 || valid_ref_assignconv_cast_p (op2_type,
13670 op1_type, 1))))
13671 ; /* Nothing to do here */
13673 /* Else we have an error figure what can't be converted into
13674 what and report the error */
13675 else
13677 char *t1;
13678 t1 = xstrdup (lang_printable_name (op1_type, 0));
13679 parse_error_context
13680 (wfl_operator,
13681 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13682 operator_string (node), t1,
13683 lang_printable_name (op2_type, 0));
13684 free (t1);
13685 TREE_TYPE (node) = boolean_type_node;
13686 error_found = 1;
13687 break;
13689 prom_type = boolean_type_node;
13690 break;
13693 if (error_found)
13694 return error_mark_node;
13696 TREE_OPERAND (node, 0) = op1;
13697 TREE_OPERAND (node, 1) = op2;
13698 TREE_TYPE (node) = prom_type;
13699 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13701 if (flag_emit_xref)
13702 return node;
13704 /* fold does not respect side-effect order as required for Java but not C.
13705 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13706 * bytecode.
13708 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13709 : ! TREE_SIDE_EFFECTS (node))
13710 node = fold (node);
13711 return node;
13714 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13715 zero value, the value of CSTE comes after the valude of STRING */
13717 static tree
13718 do_merge_string_cste (cste, string, string_len, after)
13719 tree cste;
13720 const char *string;
13721 int string_len, after;
13723 const char *old = TREE_STRING_POINTER (cste);
13724 int old_len = TREE_STRING_LENGTH (cste);
13725 int len = old_len + string_len;
13726 char *new = alloca (len+1);
13728 if (after)
13730 memcpy (new, string, string_len);
13731 memcpy (&new [string_len], old, old_len);
13733 else
13735 memcpy (new, old, old_len);
13736 memcpy (&new [old_len], string, string_len);
13738 new [len] = '\0';
13739 return build_string (len, new);
13742 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13743 new STRING_CST on success, NULL_TREE on failure */
13745 static tree
13746 merge_string_cste (op1, op2, after)
13747 tree op1, op2;
13748 int after;
13750 /* Handle two string constants right away */
13751 if (TREE_CODE (op2) == STRING_CST)
13752 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13753 TREE_STRING_LENGTH (op2), after);
13755 /* Reasonable integer constant can be treated right away */
13756 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13758 static const char *boolean_true = "true";
13759 static const char *boolean_false = "false";
13760 static const char *null_pointer = "null";
13761 char ch[3];
13762 const char *string;
13764 if (op2 == boolean_true_node)
13765 string = boolean_true;
13766 else if (op2 == boolean_false_node)
13767 string = boolean_false;
13768 else if (op2 == null_pointer_node)
13769 string = null_pointer;
13770 else if (TREE_TYPE (op2) == char_type_node)
13772 ch[0] = (char )TREE_INT_CST_LOW (op2);
13773 ch[1] = '\0';
13774 string = ch;
13776 else
13777 string = print_int_node (op2);
13779 return do_merge_string_cste (op1, string, strlen (string), after);
13781 return NULL_TREE;
13784 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13785 has to be a STRING_CST and the other part must be a STRING_CST or a
13786 INTEGRAL constant. Return a new STRING_CST if the operation
13787 succeed, NULL_TREE otherwise.
13789 If the case we want to optimize for space, we might want to return
13790 NULL_TREE for each invocation of this routine. FIXME */
13792 static tree
13793 string_constant_concatenation (op1, op2)
13794 tree op1, op2;
13796 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13798 tree string, rest;
13799 int invert;
13801 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13802 rest = (string == op1 ? op2 : op1);
13803 invert = (string == op1 ? 0 : 1 );
13805 /* Walk REST, only if it looks reasonable */
13806 if (TREE_CODE (rest) != STRING_CST
13807 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13808 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13809 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13811 rest = java_complete_tree (rest);
13812 if (rest == error_mark_node)
13813 return error_mark_node;
13814 rest = fold (rest);
13816 return merge_string_cste (string, rest, invert);
13818 return NULL_TREE;
13821 /* Implement the `+' operator. Does static optimization if possible,
13822 otherwise create (if necessary) and append elements to a
13823 StringBuffer. The StringBuffer will be carried around until it is
13824 used for a function call or an assignment. Then toString() will be
13825 called on it to turn it into a String object. */
13827 static tree
13828 build_string_concatenation (op1, op2)
13829 tree op1, op2;
13831 tree result;
13832 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13834 if (flag_emit_xref)
13835 return build (PLUS_EXPR, string_type_node, op1, op2);
13837 /* Try to do some static optimization */
13838 if ((result = string_constant_concatenation (op1, op2)))
13839 return result;
13841 /* Discard empty strings on either side of the expression */
13842 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13844 op1 = op2;
13845 op2 = NULL_TREE;
13847 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13848 op2 = NULL_TREE;
13850 /* If operands are string constant, turn then into object references */
13851 if (TREE_CODE (op1) == STRING_CST)
13852 op1 = patch_string_cst (op1);
13853 if (op2 && TREE_CODE (op2) == STRING_CST)
13854 op2 = patch_string_cst (op2);
13856 /* If either one of the constant is null and the other non null
13857 operand is a String object, return it. */
13858 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13859 return op1;
13861 /* If OP1 isn't already a StringBuffer, create and
13862 initialize a new one */
13863 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13865 /* Two solutions here:
13866 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13867 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13868 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13869 op1 = BUILD_STRING_BUFFER (op1);
13870 else
13872 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13873 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13877 if (op2)
13879 /* OP1 is no longer the last node holding a crafted StringBuffer */
13880 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13881 /* Create a node for `{new...,xxx}.append (op2)' */
13882 if (op2)
13883 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13886 /* Mark the last node holding a crafted StringBuffer */
13887 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13889 TREE_SIDE_EFFECTS (op1) = side_effects;
13890 return op1;
13893 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13894 StringBuffer. If no string were found to be patched, return
13895 NULL. */
13897 static tree
13898 patch_string (node)
13899 tree node;
13901 if (node == error_mark_node)
13902 return error_mark_node;
13903 if (TREE_CODE (node) == STRING_CST)
13904 return patch_string_cst (node);
13905 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13907 int saved = ctxp->explicit_constructor_p;
13908 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13909 tree ret;
13910 /* Temporary disable forbid the use of `this'. */
13911 ctxp->explicit_constructor_p = 0;
13912 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13913 /* String concatenation arguments must be evaluated in order too. */
13914 ret = force_evaluation_order (ret);
13915 /* Restore it at its previous value */
13916 ctxp->explicit_constructor_p = saved;
13917 return ret;
13919 return NULL_TREE;
13922 /* Build the internal representation of a string constant. */
13924 static tree
13925 patch_string_cst (node)
13926 tree node;
13928 int location;
13929 if (! flag_emit_class_files)
13931 node = get_identifier (TREE_STRING_POINTER (node));
13932 location = alloc_name_constant (CONSTANT_String, node);
13933 node = build_ref_from_constant_pool (location);
13935 TREE_TYPE (node) = string_ptr_type_node;
13936 TREE_CONSTANT (node) = 1;
13937 return node;
13940 /* Build an incomplete unary operator expression. */
13942 static tree
13943 build_unaryop (op_token, op_location, op1)
13944 int op_token, op_location;
13945 tree op1;
13947 enum tree_code op;
13948 tree unaryop;
13949 switch (op_token)
13951 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13952 case MINUS_TK: op = NEGATE_EXPR; break;
13953 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13954 case NOT_TK: op = BIT_NOT_EXPR; break;
13955 default: abort ();
13958 unaryop = build1 (op, NULL_TREE, op1);
13959 TREE_SIDE_EFFECTS (unaryop) = 1;
13960 /* Store the location of the operator, for better error report. The
13961 string of the operator will be rebuild based on the OP value. */
13962 EXPR_WFL_LINECOL (unaryop) = op_location;
13963 return unaryop;
13966 /* Special case for the ++/-- operators, since they require an extra
13967 argument to build, which is set to NULL and patched
13968 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13970 static tree
13971 build_incdec (op_token, op_location, op1, is_post_p)
13972 int op_token, op_location;
13973 tree op1;
13974 int is_post_p;
13976 static enum tree_code lookup [2][2] =
13978 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13979 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13981 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13982 NULL_TREE, op1, NULL_TREE);
13983 TREE_SIDE_EFFECTS (node) = 1;
13984 /* Store the location of the operator, for better error report. The
13985 string of the operator will be rebuild based on the OP value. */
13986 EXPR_WFL_LINECOL (node) = op_location;
13987 return node;
13990 /* Build an incomplete cast operator, based on the use of the
13991 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13992 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13993 though its type is already set. */
13995 static tree
13996 build_cast (location, type, exp)
13997 int location;
13998 tree type, exp;
14000 tree node = build1 (CONVERT_EXPR, type, exp);
14001 EXPR_WFL_LINECOL (node) = location;
14002 return node;
14005 /* Build an incomplete class reference operator. */
14006 static tree
14007 build_incomplete_class_ref (location, class_name)
14008 int location;
14009 tree class_name;
14011 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14012 EXPR_WFL_LINECOL (node) = location;
14013 return node;
14016 /* Complete an incomplete class reference operator. */
14017 static tree
14018 patch_incomplete_class_ref (node)
14019 tree node;
14021 tree type = TREE_OPERAND (node, 0);
14022 tree ref_type;
14024 if (!(ref_type = resolve_type_during_patch (type)))
14025 return error_mark_node;
14027 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
14029 tree dot = build_class_ref (ref_type);
14030 /* A class referenced by `foo.class' is initialized. */
14031 if (!flag_emit_class_files)
14032 dot = build_class_init (ref_type, dot);
14033 return java_complete_tree (dot);
14036 /* If we're emitting class files and we have to deal with non
14037 primitive types, we invoke (and consider generating) the
14038 synthetic static method `class$'. */
14039 if (!TYPE_DOT_CLASS (current_class))
14040 build_dot_class_method (current_class);
14041 ref_type = build_dot_class_method_invocation (ref_type);
14042 return java_complete_tree (ref_type);
14045 /* 15.14 Unary operators. We return error_mark_node in case of error,
14046 but preserve the type of NODE if the type is fixed. */
14048 static tree
14049 patch_unaryop (node, wfl_op)
14050 tree node;
14051 tree wfl_op;
14053 tree op = TREE_OPERAND (node, 0);
14054 tree op_type = TREE_TYPE (op);
14055 tree prom_type = NULL_TREE, value, decl;
14056 int outer_field_flag = 0;
14057 int code = TREE_CODE (node);
14058 int error_found = 0;
14060 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14062 switch (code)
14064 /* 15.13.2 Postfix Increment Operator ++ */
14065 case POSTINCREMENT_EXPR:
14066 /* 15.13.3 Postfix Increment Operator -- */
14067 case POSTDECREMENT_EXPR:
14068 /* 15.14.1 Prefix Increment Operator ++ */
14069 case PREINCREMENT_EXPR:
14070 /* 15.14.2 Prefix Decrement Operator -- */
14071 case PREDECREMENT_EXPR:
14072 op = decl = strip_out_static_field_access_decl (op);
14073 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
14074 /* We might be trying to change an outer field accessed using
14075 access method. */
14076 if (outer_field_flag)
14078 /* Retrieve the decl of the field we're trying to access. We
14079 do that by first retrieving the function we would call to
14080 access the field. It has been already verified that this
14081 field isn't final */
14082 if (flag_emit_class_files)
14083 decl = TREE_OPERAND (op, 0);
14084 else
14085 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14086 decl = DECL_FUNCTION_ACCESS_DECL (decl);
14088 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14089 else if (!JDECL_P (decl)
14090 && TREE_CODE (decl) != COMPONENT_REF
14091 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14092 && TREE_CODE (decl) != INDIRECT_REF
14093 && !(TREE_CODE (decl) == COMPOUND_EXPR
14094 && TREE_OPERAND (decl, 1)
14095 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14097 tree lvalue;
14098 /* Before screaming, check that we're not in fact trying to
14099 increment a optimized static final access, in which case
14100 we issue an different error message. */
14101 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
14102 && resolve_expression_name (wfl_op, &lvalue)
14103 && check_final_assignment (lvalue, wfl_op)))
14104 parse_error_context (wfl_operator, "Invalid argument to `%s'",
14105 operator_string (node));
14106 TREE_TYPE (node) = error_mark_node;
14107 error_found = 1;
14110 if (check_final_assignment (op, wfl_op))
14111 error_found = 1;
14113 /* From now on, we know that op if a variable and that it has a
14114 valid wfl. We use wfl_op to locate errors related to the
14115 ++/-- operand. */
14116 else if (!JNUMERIC_TYPE_P (op_type))
14118 parse_error_context
14119 (wfl_op, "Invalid argument type `%s' to `%s'",
14120 lang_printable_name (op_type, 0), operator_string (node));
14121 TREE_TYPE (node) = error_mark_node;
14122 error_found = 1;
14124 else
14126 /* Before the addition, binary numeric promotion is performed on
14127 both operands, if really necessary */
14128 if (JINTEGRAL_TYPE_P (op_type))
14130 value = build_int_2 (1, 0);
14131 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
14133 else
14135 value = build_int_2 (1, 0);
14136 TREE_TYPE (node) =
14137 binary_numeric_promotion (op_type,
14138 TREE_TYPE (value), &op, &value);
14141 /* We remember we might be accessing an outer field */
14142 if (outer_field_flag)
14144 /* We re-generate an access to the field */
14145 value = build (PLUS_EXPR, TREE_TYPE (op),
14146 build_outer_field_access (wfl_op, decl), value);
14148 /* And we patch the original access$() into a write
14149 with plus_op as a rhs */
14150 return outer_field_access_fix (node, op, value);
14153 /* And write back into the node. */
14154 TREE_OPERAND (node, 0) = op;
14155 TREE_OPERAND (node, 1) = value;
14156 /* Convert the overall back into its original type, if
14157 necessary, and return */
14158 if (JINTEGRAL_TYPE_P (op_type))
14159 return fold (node);
14160 else
14161 return fold (convert (op_type, node));
14163 break;
14165 /* 15.14.3 Unary Plus Operator + */
14166 case UNARY_PLUS_EXPR:
14167 /* 15.14.4 Unary Minus Operator - */
14168 case NEGATE_EXPR:
14169 if (!JNUMERIC_TYPE_P (op_type))
14171 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14172 TREE_TYPE (node) = error_mark_node;
14173 error_found = 1;
14175 /* Unary numeric promotion is performed on operand */
14176 else
14178 op = do_unary_numeric_promotion (op);
14179 prom_type = TREE_TYPE (op);
14180 if (code == UNARY_PLUS_EXPR)
14181 return fold (op);
14183 break;
14185 /* 15.14.5 Bitwise Complement Operator ~ */
14186 case BIT_NOT_EXPR:
14187 if (!JINTEGRAL_TYPE_P (op_type))
14189 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14190 TREE_TYPE (node) = error_mark_node;
14191 error_found = 1;
14193 else
14195 op = do_unary_numeric_promotion (op);
14196 prom_type = TREE_TYPE (op);
14198 break;
14200 /* 15.14.6 Logical Complement Operator ! */
14201 case TRUTH_NOT_EXPR:
14202 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14204 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14205 /* But the type is known. We will report an error if further
14206 attempt of a assignment is made with this rhs */
14207 TREE_TYPE (node) = boolean_type_node;
14208 error_found = 1;
14210 else
14211 prom_type = boolean_type_node;
14212 break;
14214 /* 15.15 Cast Expression */
14215 case CONVERT_EXPR:
14216 value = patch_cast (node, wfl_operator);
14217 if (value == error_mark_node)
14219 /* If this cast is part of an assignment, we tell the code
14220 that deals with it not to complain about a mismatch,
14221 because things have been cast, anyways */
14222 TREE_TYPE (node) = error_mark_node;
14223 error_found = 1;
14225 else
14227 value = fold (value);
14228 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
14229 return value;
14231 break;
14234 if (error_found)
14235 return error_mark_node;
14237 /* There are cases where node has been replaced by something else
14238 and we don't end up returning here: UNARY_PLUS_EXPR,
14239 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14240 TREE_OPERAND (node, 0) = fold (op);
14241 TREE_TYPE (node) = prom_type;
14242 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14243 return fold (node);
14246 /* Generic type resolution that sometimes takes place during node
14247 patching. Returned the resolved type or generate an error
14248 message. Return the resolved type or NULL_TREE. */
14250 static tree
14251 resolve_type_during_patch (type)
14252 tree type;
14254 if (unresolved_type_p (type, NULL))
14256 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14257 if (!type_decl)
14259 parse_error_context (type,
14260 "Class `%s' not found in type declaration",
14261 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14262 return NULL_TREE;
14264 return TREE_TYPE (type_decl);
14266 return type;
14268 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14269 found. Otherwise NODE or something meant to replace it is returned. */
14271 static tree
14272 patch_cast (node, wfl_op)
14273 tree node;
14274 tree wfl_op;
14276 tree op = TREE_OPERAND (node, 0);
14277 tree op_type = TREE_TYPE (op);
14278 tree cast_type = TREE_TYPE (node);
14279 char *t1;
14281 /* First resolve OP_TYPE if unresolved */
14282 if (!(cast_type = resolve_type_during_patch (cast_type)))
14283 return error_mark_node;
14285 /* Check on cast that are proven correct at compile time */
14286 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14288 /* Same type */
14289 if (cast_type == op_type)
14290 return node;
14292 /* float and double type are converted to the original type main
14293 variant and then to the target type. */
14294 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14295 op = convert (integer_type_node, op);
14297 /* Try widening/narowwing convertion. Potentially, things need
14298 to be worked out in gcc so we implement the extreme cases
14299 correctly. fold_convert() needs to be fixed. */
14300 return convert (cast_type, op);
14303 /* It's also valid to cast a boolean into a boolean */
14304 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14305 return node;
14307 /* null can be casted to references */
14308 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14309 return build_null_of_type (cast_type);
14311 /* The remaining legal casts involve conversion between reference
14312 types. Check for their compile time correctness. */
14313 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14314 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14316 TREE_TYPE (node) = promote_type (cast_type);
14317 /* Now, the case can be determined correct at compile time if
14318 OP_TYPE can be converted into CAST_TYPE by assignment
14319 conversion (5.2) */
14321 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14323 TREE_SET_CODE (node, NOP_EXPR);
14324 return node;
14327 if (flag_emit_class_files)
14329 TREE_SET_CODE (node, CONVERT_EXPR);
14330 return node;
14333 /* The cast requires a run-time check */
14334 return build (CALL_EXPR, promote_type (cast_type),
14335 build_address_of (soft_checkcast_node),
14336 tree_cons (NULL_TREE, build_class_ref (cast_type),
14337 build_tree_list (NULL_TREE, op)),
14338 NULL_TREE);
14341 /* Any other casts are proven incorrect at compile time */
14342 t1 = xstrdup (lang_printable_name (op_type, 0));
14343 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14344 t1, lang_printable_name (cast_type, 0));
14345 free (t1);
14346 return error_mark_node;
14349 /* Build a null constant and give it the type TYPE. */
14351 static tree
14352 build_null_of_type (type)
14353 tree type;
14355 tree node = build_int_2 (0, 0);
14356 TREE_TYPE (node) = promote_type (type);
14357 return node;
14360 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14361 a list of indices. */
14362 static tree
14363 build_array_ref (location, array, index)
14364 int location;
14365 tree array, index;
14367 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14368 EXPR_WFL_LINECOL (node) = location;
14369 return node;
14372 /* 15.12 Array Access Expression */
14374 static tree
14375 patch_array_ref (node)
14376 tree node;
14378 tree array = TREE_OPERAND (node, 0);
14379 tree array_type = TREE_TYPE (array);
14380 tree index = TREE_OPERAND (node, 1);
14381 tree index_type = TREE_TYPE (index);
14382 int error_found = 0;
14384 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14386 if (TREE_CODE (array_type) == POINTER_TYPE)
14387 array_type = TREE_TYPE (array_type);
14389 /* The array reference must be an array */
14390 if (!TYPE_ARRAY_P (array_type))
14392 parse_error_context
14393 (wfl_operator,
14394 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14395 lang_printable_name (array_type, 0));
14396 TREE_TYPE (node) = error_mark_node;
14397 error_found = 1;
14400 /* The array index undergoes unary numeric promotion. The promoted
14401 type must be int */
14402 index = do_unary_numeric_promotion (index);
14403 if (TREE_TYPE (index) != int_type_node)
14405 if (valid_cast_to_p (index_type, int_type_node))
14406 parse_error_context (wfl_operator,
14407 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14408 lang_printable_name (index_type, 0));
14409 else
14410 parse_error_context (wfl_operator,
14411 "Incompatible type for `[]'. Can't convert `%s' to `int'",
14412 lang_printable_name (index_type, 0));
14413 TREE_TYPE (node) = error_mark_node;
14414 error_found = 1;
14417 if (error_found)
14418 return error_mark_node;
14420 array_type = TYPE_ARRAY_ELEMENT (array_type);
14422 if (flag_emit_class_files || flag_emit_xref)
14424 TREE_OPERAND (node, 0) = array;
14425 TREE_OPERAND (node, 1) = index;
14427 else
14429 /* The save_expr is for correct evaluation order. It would be cleaner
14430 to use force_evaluation_order (see comment there), but that is
14431 difficult when we also have to deal with bounds checking. */
14432 if (TREE_SIDE_EFFECTS (index))
14433 array = save_expr (array);
14434 node = build_java_arrayaccess (array, array_type, index);
14435 if (TREE_SIDE_EFFECTS (index))
14436 node = build (COMPOUND_EXPR, array_type, array, node);
14438 TREE_TYPE (node) = array_type;
14439 return node;
14442 /* 15.9 Array Creation Expressions */
14444 static tree
14445 build_newarray_node (type, dims, extra_dims)
14446 tree type;
14447 tree dims;
14448 int extra_dims;
14450 tree node =
14451 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14452 build_int_2 (extra_dims, 0));
14453 return node;
14456 static tree
14457 patch_newarray (node)
14458 tree node;
14460 tree type = TREE_OPERAND (node, 0);
14461 tree dims = TREE_OPERAND (node, 1);
14462 tree cdim, array_type;
14463 int error_found = 0;
14464 int ndims = 0;
14465 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14467 /* Dimension types are verified. It's better for the types to be
14468 verified in order. */
14469 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14471 int dim_error = 0;
14472 tree dim = TREE_VALUE (cdim);
14474 /* Dim might have been saved during its evaluation */
14475 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14477 /* The type of each specified dimension must be an integral type. */
14478 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14479 dim_error = 1;
14481 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14482 promoted type must be int. */
14483 else
14485 dim = do_unary_numeric_promotion (dim);
14486 if (TREE_TYPE (dim) != int_type_node)
14487 dim_error = 1;
14490 /* Report errors on types here */
14491 if (dim_error)
14493 parse_error_context
14494 (TREE_PURPOSE (cdim),
14495 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14496 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14497 "Explicit cast needed to" : "Can't"),
14498 lang_printable_name (TREE_TYPE (dim), 0));
14499 error_found = 1;
14502 TREE_PURPOSE (cdim) = NULL_TREE;
14505 /* Resolve array base type if unresolved */
14506 if (!(type = resolve_type_during_patch (type)))
14507 error_found = 1;
14509 if (error_found)
14511 /* We don't want further evaluation of this bogus array creation
14512 operation */
14513 TREE_TYPE (node) = error_mark_node;
14514 return error_mark_node;
14517 /* Set array_type to the actual (promoted) array type of the result. */
14518 if (TREE_CODE (type) == RECORD_TYPE)
14519 type = build_pointer_type (type);
14520 while (--xdims >= 0)
14522 type = promote_type (build_java_array_type (type, -1));
14524 dims = nreverse (dims);
14525 array_type = type;
14526 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14528 type = array_type;
14529 array_type
14530 = build_java_array_type (type,
14531 TREE_CODE (cdim) == INTEGER_CST
14532 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14533 : -1);
14534 array_type = promote_type (array_type);
14536 dims = nreverse (dims);
14538 /* The node is transformed into a function call. Things are done
14539 differently according to the number of dimensions. If the number
14540 of dimension is equal to 1, then the nature of the base type
14541 (primitive or not) matters. */
14542 if (ndims == 1)
14543 return build_new_array (type, TREE_VALUE (dims));
14545 /* Can't reuse what's already written in expr.c because it uses the
14546 JVM stack representation. Provide a build_multianewarray. FIXME */
14547 return build (CALL_EXPR, array_type,
14548 build_address_of (soft_multianewarray_node),
14549 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14550 tree_cons (NULL_TREE,
14551 build_int_2 (ndims, 0), dims )),
14552 NULL_TREE);
14555 /* 10.6 Array initializer. */
14557 /* Build a wfl for array element that don't have one, so we can
14558 pin-point errors. */
14560 static tree
14561 maybe_build_array_element_wfl (node)
14562 tree node;
14564 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14565 return build_expr_wfl (NULL_TREE, ctxp->filename,
14566 ctxp->elc.line, ctxp->elc.prev_col);
14567 else
14568 return NULL_TREE;
14571 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14572 identification of initialized arrays easier to detect during walk
14573 and expansion. */
14575 static tree
14576 build_new_array_init (location, values)
14577 int location;
14578 tree values;
14580 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14581 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14582 EXPR_WFL_LINECOL (to_return) = location;
14583 return to_return;
14586 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14587 occurred. Otherwise return NODE after having set its type
14588 appropriately. */
14590 static tree
14591 patch_new_array_init (type, node)
14592 tree type, node;
14594 int error_seen = 0;
14595 tree current, element_type;
14596 HOST_WIDE_INT length;
14597 int all_constant = 1;
14598 tree init = TREE_OPERAND (node, 0);
14600 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14602 parse_error_context (node,
14603 "Invalid array initializer for non-array type `%s'",
14604 lang_printable_name (type, 1));
14605 return error_mark_node;
14607 type = TREE_TYPE (type);
14608 element_type = TYPE_ARRAY_ELEMENT (type);
14610 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14612 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14613 current; length++, current = TREE_CHAIN (current))
14615 tree elt = TREE_VALUE (current);
14616 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14618 error_seen |= array_constructor_check_entry (element_type, current);
14619 elt = TREE_VALUE (current);
14620 /* When compiling to native code, STRING_CST is converted to
14621 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14622 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14623 all_constant = 0;
14625 else
14627 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14628 TREE_PURPOSE (current) = NULL_TREE;
14629 all_constant = 0;
14631 if (elt && TREE_CODE (elt) == TREE_LIST
14632 && TREE_VALUE (elt) == error_mark_node)
14633 error_seen = 1;
14636 if (error_seen)
14637 return error_mark_node;
14639 /* Create a new type. We can't reuse the one we have here by
14640 patching its dimension because it originally is of dimension -1
14641 hence reused by gcc. This would prevent triangular arrays. */
14642 type = build_java_array_type (element_type, length);
14643 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14644 TREE_TYPE (node) = promote_type (type);
14645 TREE_CONSTANT (init) = all_constant;
14646 TREE_CONSTANT (node) = all_constant;
14647 return node;
14650 /* Verify that one entry of the initializer element list can be
14651 assigned to the array base type. Report 1 if an error occurred, 0
14652 otherwise. */
14654 static int
14655 array_constructor_check_entry (type, entry)
14656 tree type, entry;
14658 char *array_type_string = NULL; /* For error reports */
14659 tree value, type_value, new_value, wfl_value, patched;
14660 int error_seen = 0;
14662 new_value = NULL_TREE;
14663 wfl_value = TREE_VALUE (entry);
14665 value = java_complete_tree (TREE_VALUE (entry));
14666 /* patch_string return error_mark_node if arg is error_mark_node */
14667 if ((patched = patch_string (value)))
14668 value = patched;
14669 if (value == error_mark_node)
14670 return 1;
14672 type_value = TREE_TYPE (value);
14674 /* At anytime, try_builtin_assignconv can report a warning on
14675 constant overflow during narrowing. */
14676 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14677 new_value = try_builtin_assignconv (wfl_operator, type, value);
14678 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14679 type_value = promote_type (type);
14681 /* Check and report errors */
14682 if (!new_value)
14684 const char *msg = (!valid_cast_to_p (type_value, type) ?
14685 "Can't" : "Explicit cast needed to");
14686 if (!array_type_string)
14687 array_type_string = xstrdup (lang_printable_name (type, 1));
14688 parse_error_context
14689 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14690 msg, lang_printable_name (type_value, 1), array_type_string);
14691 error_seen = 1;
14694 if (new_value)
14696 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
14697 TREE_VALUE (entry) = new_value;
14700 if (array_type_string)
14701 free (array_type_string);
14703 TREE_PURPOSE (entry) = NULL_TREE;
14704 return error_seen;
14707 static tree
14708 build_this (location)
14709 int location;
14711 tree node = build_wfl_node (this_identifier_node);
14712 TREE_SET_CODE (node, THIS_EXPR);
14713 EXPR_WFL_LINECOL (node) = location;
14714 return node;
14717 /* 14.15 The return statement. It builds a modify expression that
14718 assigns the returned value to the RESULT_DECL that hold the value
14719 to be returned. */
14721 static tree
14722 build_return (location, op)
14723 int location;
14724 tree op;
14726 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14727 EXPR_WFL_LINECOL (node) = location;
14728 node = build_debugable_stmt (location, node);
14729 return node;
14732 static tree
14733 patch_return (node)
14734 tree node;
14736 tree return_exp = TREE_OPERAND (node, 0);
14737 tree meth = current_function_decl;
14738 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14739 int error_found = 0;
14741 TREE_TYPE (node) = error_mark_node;
14742 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14744 /* It's invalid to have a return value within a function that is
14745 declared with the keyword void or that is a constructor */
14746 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14747 error_found = 1;
14749 /* It's invalid to use a return statement in a static block */
14750 if (DECL_CLINIT_P (current_function_decl))
14751 error_found = 1;
14753 /* It's invalid to have a no return value within a function that
14754 isn't declared with the keyword `void' */
14755 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14756 error_found = 2;
14758 if (DECL_INSTINIT_P (current_function_decl))
14759 error_found = 1;
14761 if (error_found)
14763 if (DECL_INSTINIT_P (current_function_decl))
14764 parse_error_context (wfl_operator,
14765 "`return' inside instance initializer");
14767 else if (DECL_CLINIT_P (current_function_decl))
14768 parse_error_context (wfl_operator,
14769 "`return' inside static initializer");
14771 else if (!DECL_CONSTRUCTOR_P (meth))
14773 char *t = xstrdup (lang_printable_name (mtype, 0));
14774 parse_error_context (wfl_operator,
14775 "`return' with%s value from `%s %s'",
14776 (error_found == 1 ? "" : "out"),
14777 t, lang_printable_name (meth, 0));
14778 free (t);
14780 else
14781 parse_error_context (wfl_operator,
14782 "`return' with value from constructor `%s'",
14783 lang_printable_name (meth, 0));
14784 return error_mark_node;
14787 /* If we have a return_exp, build a modify expression and expand
14788 it. Note: at that point, the assignment is declared valid, but we
14789 may want to carry some more hacks */
14790 if (return_exp)
14792 tree exp = java_complete_tree (return_exp);
14793 tree modify, patched;
14795 /* If the function returned value and EXP are booleans, EXP has
14796 to be converted into the type of DECL_RESULT, which is integer
14797 (see complete_start_java_method) */
14798 if (TREE_TYPE (exp) == boolean_type_node &&
14799 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14800 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14802 /* `null' can be assigned to a function returning a reference */
14803 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14804 exp == null_pointer_node)
14805 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14807 if ((patched = patch_string (exp)))
14808 exp = patched;
14810 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14811 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14812 modify = java_complete_tree (modify);
14814 if (modify != error_mark_node)
14816 TREE_SIDE_EFFECTS (modify) = 1;
14817 TREE_OPERAND (node, 0) = modify;
14819 else
14820 return error_mark_node;
14822 TREE_TYPE (node) = void_type_node;
14823 TREE_SIDE_EFFECTS (node) = 1;
14824 return node;
14827 /* 14.8 The if Statement */
14829 static tree
14830 build_if_else_statement (location, expression, if_body, else_body)
14831 int location;
14832 tree expression, if_body, else_body;
14834 tree node;
14835 if (!else_body)
14836 else_body = empty_stmt_node;
14837 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14838 EXPR_WFL_LINECOL (node) = location;
14839 node = build_debugable_stmt (location, node);
14840 return node;
14843 static tree
14844 patch_if_else_statement (node)
14845 tree node;
14847 tree expression = TREE_OPERAND (node, 0);
14849 TREE_TYPE (node) = error_mark_node;
14850 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14852 /* The type of expression must be boolean */
14853 if (TREE_TYPE (expression) != boolean_type_node
14854 && TREE_TYPE (expression) != promoted_boolean_type_node)
14856 parse_error_context
14857 (wfl_operator,
14858 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14859 lang_printable_name (TREE_TYPE (expression), 0));
14860 return error_mark_node;
14863 TREE_TYPE (node) = void_type_node;
14864 TREE_SIDE_EFFECTS (node) = 1;
14865 CAN_COMPLETE_NORMALLY (node)
14866 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14867 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
14868 return node;
14871 /* 14.6 Labeled Statements */
14873 /* Action taken when a lableled statement is parsed. a new
14874 LABELED_BLOCK_EXPR is created. No statement is attached to the
14875 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14877 static tree
14878 build_labeled_block (location, label)
14879 int location;
14880 tree label;
14882 tree label_name ;
14883 tree label_decl, node;
14884 if (label == NULL_TREE || label == continue_identifier_node)
14885 label_name = label;
14886 else
14888 label_name = merge_qualified_name (label_id, label);
14889 /* Issue an error if we try to reuse a label that was previously
14890 declared */
14891 if (IDENTIFIER_LOCAL_VALUE (label_name))
14893 EXPR_WFL_LINECOL (wfl_operator) = location;
14894 parse_error_context (wfl_operator,
14895 "Declaration of `%s' shadows a previous label declaration",
14896 IDENTIFIER_POINTER (label));
14897 EXPR_WFL_LINECOL (wfl_operator) =
14898 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14899 parse_error_context (wfl_operator,
14900 "This is the location of the previous declaration of label `%s'",
14901 IDENTIFIER_POINTER (label));
14902 java_error_count--;
14906 label_decl = create_label_decl (label_name);
14907 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14908 EXPR_WFL_LINECOL (node) = location;
14909 TREE_SIDE_EFFECTS (node) = 1;
14910 return node;
14913 /* A labeled statement LBE is attached a statement. */
14915 static tree
14916 finish_labeled_statement (lbe, statement)
14917 tree lbe; /* Labeled block expr */
14918 tree statement;
14920 /* In anyways, tie the loop to its statement */
14921 LABELED_BLOCK_BODY (lbe) = statement;
14922 pop_labeled_block ();
14923 POP_LABELED_BLOCK ();
14924 return lbe;
14927 /* 14.10, 14.11, 14.12 Loop Statements */
14929 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14930 list. */
14932 static tree
14933 build_new_loop (loop_body)
14934 tree loop_body;
14936 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14937 TREE_SIDE_EFFECTS (loop) = 1;
14938 PUSH_LOOP (loop);
14939 return loop;
14942 /* Create a loop body according to the following structure:
14943 COMPOUND_EXPR
14944 COMPOUND_EXPR (loop main body)
14945 EXIT_EXPR (this order is for while/for loops.
14946 LABELED_BLOCK_EXPR the order is reversed for do loops)
14947 LABEL_DECL (a continue occuring here branches at the
14948 BODY end of this labeled block)
14949 INCREMENT (if any)
14951 REVERSED, if non zero, tells that the loop condition expr comes
14952 after the body, like in the do-while loop.
14954 To obtain a loop, the loop body structure described above is
14955 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14957 LABELED_BLOCK_EXPR
14958 LABEL_DECL (use this label to exit the loop)
14959 LOOP_EXPR
14960 <structure described above> */
14962 static tree
14963 build_loop_body (location, condition, reversed)
14964 int location;
14965 tree condition;
14966 int reversed;
14968 tree first, second, body;
14970 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14971 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14972 condition = build_debugable_stmt (location, condition);
14973 TREE_SIDE_EFFECTS (condition) = 1;
14975 body = build_labeled_block (0, continue_identifier_node);
14976 first = (reversed ? body : condition);
14977 second = (reversed ? condition : body);
14978 return
14979 build (COMPOUND_EXPR, NULL_TREE,
14980 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14983 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14984 their order) on the current loop. Unlink the current loop from the
14985 loop list. */
14987 static tree
14988 finish_loop_body (location, condition, body, reversed)
14989 int location;
14990 tree condition, body;
14991 int reversed;
14993 tree to_return = ctxp->current_loop;
14994 tree loop_body = LOOP_EXPR_BODY (to_return);
14995 if (condition)
14997 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14998 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14999 The real EXIT_EXPR is one operand further. */
15000 EXPR_WFL_LINECOL (cnode) = location;
15001 /* This one is for accurate error reports */
15002 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
15003 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
15005 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15006 POP_LOOP ();
15007 return to_return;
15010 /* Tailored version of finish_loop_body for FOR loops, when FOR
15011 loops feature the condition part */
15013 static tree
15014 finish_for_loop (location, condition, update, body)
15015 int location;
15016 tree condition, update, body;
15018 /* Put the condition and the loop body in place */
15019 tree loop = finish_loop_body (location, condition, body, 0);
15020 /* LOOP is the current loop which has been now popped of the loop
15021 stack. Install the update block */
15022 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15023 return loop;
15026 /* Try to find the loop a block might be related to. This comprises
15027 the case where the LOOP_EXPR is found as the second operand of a
15028 COMPOUND_EXPR, because the loop happens to have an initialization
15029 part, then expressed as the first operand of the COMPOUND_EXPR. If
15030 the search finds something, 1 is returned. Otherwise, 0 is
15031 returned. The search is assumed to start from a
15032 LABELED_BLOCK_EXPR's block. */
15034 static tree
15035 search_loop (statement)
15036 tree statement;
15038 if (TREE_CODE (statement) == LOOP_EXPR)
15039 return statement;
15041 if (TREE_CODE (statement) == BLOCK)
15042 statement = BLOCK_SUBBLOCKS (statement);
15043 else
15044 return NULL_TREE;
15046 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15047 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15048 statement = TREE_OPERAND (statement, 1);
15050 return (TREE_CODE (statement) == LOOP_EXPR
15051 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15054 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15055 returned otherwise. */
15057 static int
15058 labeled_block_contains_loop_p (block, loop)
15059 tree block, loop;
15061 if (!block)
15062 return 0;
15064 if (LABELED_BLOCK_BODY (block) == loop)
15065 return 1;
15067 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15068 return 1;
15070 return 0;
15073 /* If the loop isn't surrounded by a labeled statement, create one and
15074 insert LOOP as its body. */
15076 static tree
15077 patch_loop_statement (loop)
15078 tree loop;
15080 tree loop_label;
15082 TREE_TYPE (loop) = void_type_node;
15083 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15084 return loop;
15086 loop_label = build_labeled_block (0, NULL_TREE);
15087 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15088 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15089 LABELED_BLOCK_BODY (loop_label) = loop;
15090 PUSH_LABELED_BLOCK (loop_label);
15091 return loop_label;
15094 /* 14.13, 14.14: break and continue Statements */
15096 /* Build a break or a continue statement. a null NAME indicates an
15097 unlabeled break/continue statement. */
15099 static tree
15100 build_bc_statement (location, is_break, name)
15101 int location, is_break;
15102 tree name;
15104 tree break_continue, label_block_expr = NULL_TREE;
15106 if (name)
15108 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15109 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15110 /* Null means that we don't have a target for this named
15111 break/continue. In this case, we make the target to be the
15112 label name, so that the error can be reported accuratly in
15113 patch_bc_statement. */
15114 label_block_expr = EXPR_WFL_NODE (name);
15116 /* Unlabeled break/continue will be handled during the
15117 break/continue patch operation */
15118 break_continue
15119 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
15121 IS_BREAK_STMT_P (break_continue) = is_break;
15122 TREE_SIDE_EFFECTS (break_continue) = 1;
15123 EXPR_WFL_LINECOL (break_continue) = location;
15124 break_continue = build_debugable_stmt (location, break_continue);
15125 return break_continue;
15128 /* Verification of a break/continue statement. */
15130 static tree
15131 patch_bc_statement (node)
15132 tree node;
15134 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15135 tree labeled_block = ctxp->current_labeled_block;
15136 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15138 /* Having an identifier here means that the target is unknown. */
15139 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15141 parse_error_context (wfl_operator, "No label definition found for `%s'",
15142 IDENTIFIER_POINTER (bc_label));
15143 return error_mark_node;
15145 if (! IS_BREAK_STMT_P (node))
15147 /* It's a continue statement. */
15148 for (;; labeled_block = TREE_CHAIN (labeled_block))
15150 if (labeled_block == NULL_TREE)
15152 if (bc_label == NULL_TREE)
15153 parse_error_context (wfl_operator,
15154 "`continue' must be in loop");
15155 else
15156 parse_error_context
15157 (wfl_operator, "continue label `%s' does not name a loop",
15158 IDENTIFIER_POINTER (bc_label));
15159 return error_mark_node;
15161 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15162 == continue_identifier_node)
15163 && (bc_label == NULL_TREE
15164 || TREE_CHAIN (labeled_block) == bc_label))
15166 bc_label = labeled_block;
15167 break;
15171 else if (!bc_label)
15173 for (;; labeled_block = TREE_CHAIN (labeled_block))
15175 if (labeled_block == NULL_TREE)
15177 parse_error_context (wfl_operator,
15178 "`break' must be in loop or switch");
15179 return error_mark_node;
15181 target_stmt = LABELED_BLOCK_BODY (labeled_block);
15182 if (TREE_CODE (target_stmt) == SWITCH_EXPR
15183 || search_loop (target_stmt))
15185 bc_label = labeled_block;
15186 break;
15191 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15192 CAN_COMPLETE_NORMALLY (bc_label) = 1;
15194 /* Our break/continue don't return values. */
15195 TREE_TYPE (node) = void_type_node;
15196 /* Encapsulate the break within a compound statement so that it's
15197 expanded all the times by expand_expr (and not clobbered
15198 sometimes, like after a if statement) */
15199 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15200 TREE_SIDE_EFFECTS (node) = 1;
15201 return node;
15204 /* Process the exit expression belonging to a loop. Its type must be
15205 boolean. */
15207 static tree
15208 patch_exit_expr (node)
15209 tree node;
15211 tree expression = TREE_OPERAND (node, 0);
15212 TREE_TYPE (node) = error_mark_node;
15213 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15215 /* The type of expression must be boolean */
15216 if (TREE_TYPE (expression) != boolean_type_node)
15218 parse_error_context
15219 (wfl_operator,
15220 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
15221 lang_printable_name (TREE_TYPE (expression), 0));
15222 return error_mark_node;
15224 /* Now we know things are allright, invert the condition, fold and
15225 return */
15226 TREE_OPERAND (node, 0) =
15227 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15229 if (! integer_zerop (TREE_OPERAND (node, 0))
15230 && ctxp->current_loop != NULL_TREE
15231 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15232 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15233 if (! integer_onep (TREE_OPERAND (node, 0)))
15234 CAN_COMPLETE_NORMALLY (node) = 1;
15237 TREE_TYPE (node) = void_type_node;
15238 return node;
15241 /* 14.9 Switch statement */
15243 static tree
15244 patch_switch_statement (node)
15245 tree node;
15247 tree se = TREE_OPERAND (node, 0), se_type;
15249 /* Complete the switch expression */
15250 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15251 se_type = TREE_TYPE (se);
15252 /* The type of the switch expression must be char, byte, short or
15253 int */
15254 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15256 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15257 parse_error_context (wfl_operator,
15258 "Incompatible type for `switch'. Can't convert `%s' to `int'",
15259 lang_printable_name (se_type, 0));
15260 /* This is what java_complete_tree will check */
15261 TREE_OPERAND (node, 0) = error_mark_node;
15262 return error_mark_node;
15265 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15267 /* Ready to return */
15268 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15270 TREE_TYPE (node) = error_mark_node;
15271 return error_mark_node;
15273 TREE_TYPE (node) = void_type_node;
15274 TREE_SIDE_EFFECTS (node) = 1;
15275 CAN_COMPLETE_NORMALLY (node)
15276 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15277 || ! SWITCH_HAS_DEFAULT (node);
15278 return node;
15281 /* 14.18 The try/catch statements */
15283 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15284 catches TYPE and executes CATCH_STMTS. */
15286 static tree
15287 encapsulate_with_try_catch (location, type, try_stmts, catch_stmts)
15288 int location;
15289 tree type, try_stmts, catch_stmts;
15291 tree try_block, catch_clause_param, catch_block, catch;
15293 /* First build a try block */
15294 try_block = build_expr_block (try_stmts, NULL_TREE);
15296 /* Build a catch block: we need a catch clause parameter */
15297 catch_clause_param = build_decl (VAR_DECL,
15298 wpv_id, build_pointer_type (type));
15299 /* And a block */
15300 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15302 /* Initialize the variable and store in the block */
15303 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15304 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15305 add_stmt_to_block (catch_block, NULL_TREE, catch);
15307 /* Add the catch statements */
15308 add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15310 /* Now we can build a CATCH_EXPR */
15311 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
15313 return build_try_statement (location, try_block, catch_block);
15316 static tree
15317 build_try_statement (location, try_block, catches)
15318 int location;
15319 tree try_block, catches;
15321 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
15322 EXPR_WFL_LINECOL (node) = location;
15323 return node;
15326 static tree
15327 build_try_finally_statement (location, try_block, finally)
15328 int location;
15329 tree try_block, finally;
15331 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15332 EXPR_WFL_LINECOL (node) = location;
15333 return node;
15336 static tree
15337 patch_try_statement (node)
15338 tree node;
15340 int error_found = 0;
15341 tree try = TREE_OPERAND (node, 0);
15342 /* Exception handlers are considered in left to right order */
15343 tree catch = nreverse (TREE_OPERAND (node, 1));
15344 tree current, caught_type_list = NULL_TREE;
15346 /* Check catch clauses, if any. Every time we find an error, we try
15347 to process the next catch clause. We process the catch clause before
15348 the try block so that when processing the try block we can check thrown
15349 exceptions againts the caught type list. */
15350 for (current = catch; current; current = TREE_CHAIN (current))
15352 tree carg_decl, carg_type;
15353 tree sub_current, catch_block, catch_clause;
15354 int unreachable;
15356 /* At this point, the structure of the catch clause is
15357 CATCH_EXPR (catch node)
15358 BLOCK (with the decl of the parameter)
15359 COMPOUND_EXPR
15360 MODIFY_EXPR (assignment of the catch parameter)
15361 BLOCK (catch clause block)
15363 catch_clause = TREE_OPERAND (current, 0);
15364 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15365 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15367 /* Catch clauses can't have more than one parameter declared,
15368 but it's already enforced by the grammar. Make sure that the
15369 only parameter of the clause statement in of class Throwable
15370 or a subclass of Throwable, but that was done earlier. The
15371 catch clause parameter type has also been resolved. */
15373 /* Just make sure that the catch clause parameter type inherits
15374 from java.lang.Throwable */
15375 if (!inherits_from_p (carg_type, throwable_type_node))
15377 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15378 parse_error_context (wfl_operator,
15379 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15380 lang_printable_name (carg_type, 0));
15381 error_found = 1;
15382 continue;
15385 /* Partial check for unreachable catch statement: The catch
15386 clause is reachable iff is no earlier catch block A in
15387 the try statement such that the type of the catch
15388 clause's parameter is the same as or a subclass of the
15389 type of A's parameter */
15390 unreachable = 0;
15391 for (sub_current = catch;
15392 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15394 tree sub_catch_clause, decl;
15395 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15396 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15398 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15400 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15401 parse_error_context
15402 (wfl_operator,
15403 "`catch' not reached because of the catch clause at line %d",
15404 EXPR_WFL_LINENO (sub_current));
15405 unreachable = error_found = 1;
15406 break;
15409 /* Complete the catch clause block */
15410 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15411 if (catch_block == error_mark_node)
15413 error_found = 1;
15414 continue;
15416 if (CAN_COMPLETE_NORMALLY (catch_block))
15417 CAN_COMPLETE_NORMALLY (node) = 1;
15418 TREE_OPERAND (current, 0) = catch_block;
15420 if (unreachable)
15421 continue;
15423 /* Things to do here: the exception must be thrown */
15425 /* Link this type to the caught type list */
15426 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15429 PUSH_EXCEPTIONS (caught_type_list);
15430 if ((try = java_complete_tree (try)) == error_mark_node)
15431 error_found = 1;
15432 if (CAN_COMPLETE_NORMALLY (try))
15433 CAN_COMPLETE_NORMALLY (node) = 1;
15434 POP_EXCEPTIONS ();
15436 /* Verification ends here */
15437 if (error_found)
15438 return error_mark_node;
15440 TREE_OPERAND (node, 0) = try;
15441 TREE_OPERAND (node, 1) = catch;
15442 TREE_TYPE (node) = void_type_node;
15443 return node;
15446 /* 14.17 The synchronized Statement */
15448 static tree
15449 patch_synchronized_statement (node, wfl_op1)
15450 tree node, wfl_op1;
15452 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15453 tree block = TREE_OPERAND (node, 1);
15455 tree tmp, enter, exit, expr_decl, assignment;
15457 if (expr == error_mark_node)
15459 block = java_complete_tree (block);
15460 return expr;
15463 /* We might be trying to synchronize on a STRING_CST */
15464 if ((tmp = patch_string (expr)))
15465 expr = tmp;
15467 /* The TYPE of expr must be a reference type */
15468 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15470 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15471 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15472 lang_printable_name (TREE_TYPE (expr), 0));
15473 return error_mark_node;
15476 if (flag_emit_xref)
15478 TREE_OPERAND (node, 0) = expr;
15479 TREE_OPERAND (node, 1) = java_complete_tree (block);
15480 CAN_COMPLETE_NORMALLY (node) = 1;
15481 return node;
15484 /* Generate a try-finally for the synchronized statement, except
15485 that the handler that catches all throw exception calls
15486 _Jv_MonitorExit and then rethrow the exception.
15487 The synchronized statement is then implemented as:
15488 TRY
15490 _Jv_MonitorEnter (expression)
15491 synchronized_block
15492 _Jv_MonitorExit (expression)
15494 CATCH_ALL
15496 e = _Jv_exception_info ();
15497 _Jv_MonitorExit (expression)
15498 Throw (e);
15499 } */
15501 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15502 BUILD_MONITOR_ENTER (enter, expr_decl);
15503 BUILD_MONITOR_EXIT (exit, expr_decl);
15504 CAN_COMPLETE_NORMALLY (enter) = 1;
15505 CAN_COMPLETE_NORMALLY (exit) = 1;
15506 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15507 TREE_SIDE_EFFECTS (assignment) = 1;
15508 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15509 build (COMPOUND_EXPR, NULL_TREE,
15510 build (WITH_CLEANUP_EXPR, NULL_TREE,
15511 build (COMPOUND_EXPR, NULL_TREE,
15512 assignment, enter),
15513 exit, NULL_TREE),
15514 block));
15515 node = build_expr_block (node, expr_decl);
15517 return java_complete_tree (node);
15520 /* 14.16 The throw Statement */
15522 static tree
15523 patch_throw_statement (node, wfl_op1)
15524 tree node, wfl_op1;
15526 tree expr = TREE_OPERAND (node, 0);
15527 tree type = TREE_TYPE (expr);
15528 int unchecked_ok = 0, tryblock_throws_ok = 0;
15530 /* Thrown expression must be assignable to java.lang.Throwable */
15531 if (!try_reference_assignconv (throwable_type_node, expr))
15533 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15534 parse_error_context (wfl_operator,
15535 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15536 lang_printable_name (type, 0));
15537 /* If the thrown expression was a reference, we further the
15538 compile-time check. */
15539 if (!JREFERENCE_TYPE_P (type))
15540 return error_mark_node;
15543 /* At least one of the following must be true */
15545 /* The type of the throw expression is a not checked exception,
15546 i.e. is a unchecked expression. */
15547 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15549 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15550 /* An instance can't throw a checked exception unless that exception
15551 is explicitely declared in the `throws' clause of each
15552 constructor. This doesn't apply to anonymous classes, since they
15553 don't have declared constructors. */
15554 if (!unchecked_ok
15555 && DECL_INSTINIT_P (current_function_decl)
15556 && !ANONYMOUS_CLASS_P (current_class))
15558 tree current;
15559 for (current = TYPE_METHODS (current_class); current;
15560 current = TREE_CHAIN (current))
15561 if (DECL_CONSTRUCTOR_P (current)
15562 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15564 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)",
15565 lang_printable_name (TREE_TYPE (expr), 0));
15566 return error_mark_node;
15570 /* Throw is contained in a try statement and at least one catch
15571 clause can receive the thrown expression or the current method is
15572 declared to throw such an exception. Or, the throw statement is
15573 contained in a method or constructor declaration and the type of
15574 the Expression is assignable to at least one type listed in the
15575 throws clause the declaration. */
15576 if (!unchecked_ok)
15577 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15578 if (!(unchecked_ok || tryblock_throws_ok))
15580 /* If there is a surrounding try block that has no matching
15581 clatch clause, report it first. A surrounding try block exits
15582 only if there is something after the list of checked
15583 exception thrown by the current function (if any). */
15584 if (IN_TRY_BLOCK_P ())
15585 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15586 lang_printable_name (type, 0));
15587 /* If we have no surrounding try statement and the method doesn't have
15588 any throws, report it now. FIXME */
15590 /* We report that the exception can't be throw from a try block
15591 in all circumstances but when the `throw' is inside a static
15592 block. */
15593 else if (!EXCEPTIONS_P (currently_caught_type_list)
15594 && !tryblock_throws_ok)
15596 if (DECL_CLINIT_P (current_function_decl))
15597 parse_error_context (wfl_operator,
15598 "Checked exception `%s' can't be thrown in initializer",
15599 lang_printable_name (type, 0));
15600 else
15601 parse_error_context (wfl_operator,
15602 "Checked exception `%s' isn't thrown from a `try' block",
15603 lang_printable_name (type, 0));
15605 /* Otherwise, the current method doesn't have the appropriate
15606 throws declaration */
15607 else
15608 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15609 lang_printable_name (type, 0));
15610 return error_mark_node;
15613 if (! flag_emit_class_files && ! flag_emit_xref)
15614 BUILD_THROW (node, expr);
15616 /* If doing xrefs, keep the location where the `throw' was seen. */
15617 if (flag_emit_xref)
15618 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15619 return node;
15622 /* Check that exception said to be thrown by method DECL can be
15623 effectively caught from where DECL is invoked. */
15625 static void
15626 check_thrown_exceptions (location, decl)
15627 int location;
15628 tree decl;
15630 tree throws;
15631 /* For all the unchecked exceptions thrown by DECL */
15632 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15633 throws = TREE_CHAIN (throws))
15634 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15636 #if 1
15637 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15638 if (DECL_NAME (decl) == get_identifier ("clone"))
15639 continue;
15640 #endif
15641 EXPR_WFL_LINECOL (wfl_operator) = location;
15642 if (DECL_FINIT_P (current_function_decl))
15643 parse_error_context
15644 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15645 lang_printable_name (TREE_VALUE (throws), 0));
15646 else
15648 parse_error_context
15649 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15650 lang_printable_name (TREE_VALUE (throws), 0),
15651 (DECL_INIT_P (current_function_decl) ?
15652 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15653 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15658 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15659 try-catch blocks, OR is listed in the `throws' clause of the
15660 current method. */
15662 static int
15663 check_thrown_exceptions_do (exception)
15664 tree exception;
15666 tree list = currently_caught_type_list;
15667 resolve_and_layout (exception, NULL_TREE);
15668 /* First, all the nested try-catch-finally at that stage. The
15669 last element contains `throws' clause exceptions, if any. */
15670 if (IS_UNCHECKED_EXCEPTION_P (exception))
15671 return 1;
15672 while (list)
15674 tree caught;
15675 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15676 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15677 return 1;
15678 list = TREE_CHAIN (list);
15680 return 0;
15683 static void
15684 purge_unchecked_exceptions (mdecl)
15685 tree mdecl;
15687 tree throws = DECL_FUNCTION_THROWS (mdecl);
15688 tree new = NULL_TREE;
15690 while (throws)
15692 tree next = TREE_CHAIN (throws);
15693 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15695 TREE_CHAIN (throws) = new;
15696 new = throws;
15698 throws = next;
15700 /* List is inverted here, but it doesn't matter */
15701 DECL_FUNCTION_THROWS (mdecl) = new;
15704 /* This function goes over all of CLASS_TYPE ctors and checks whether
15705 each of them features at least one unchecked exception in its
15706 `throws' clause. If it's the case, it returns `true', `false'
15707 otherwise. */
15709 static bool
15710 ctors_unchecked_throws_clause_p (class_type)
15711 tree class_type;
15713 tree current;
15715 for (current = TYPE_METHODS (class_type); current;
15716 current = TREE_CHAIN (current))
15718 bool ctu = false; /* Ctor Throws Unchecked */
15719 if (DECL_CONSTRUCTOR_P (current))
15721 tree throws;
15722 for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
15723 throws = TREE_CHAIN (throws))
15724 if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
15725 ctu = true;
15727 /* We return false as we found one ctor that is unfit. */
15728 if (!ctu && DECL_CONSTRUCTOR_P (current))
15729 return false;
15731 /* All ctors feature at least one unchecked exception in their
15732 `throws' clause. */
15733 return true;
15736 /* 15.24 Conditional Operator ?: */
15738 static tree
15739 patch_conditional_expr (node, wfl_cond, wfl_op1)
15740 tree node, wfl_cond, wfl_op1;
15742 tree cond = TREE_OPERAND (node, 0);
15743 tree op1 = TREE_OPERAND (node, 1);
15744 tree op2 = TREE_OPERAND (node, 2);
15745 tree resulting_type = NULL_TREE;
15746 tree t1, t2, patched;
15747 int error_found = 0;
15749 /* Operands of ?: might be StringBuffers crafted as a result of a
15750 string concatenation. Obtain a descent operand here. */
15751 if ((patched = patch_string (op1)))
15752 TREE_OPERAND (node, 1) = op1 = patched;
15753 if ((patched = patch_string (op2)))
15754 TREE_OPERAND (node, 2) = op2 = patched;
15756 t1 = TREE_TYPE (op1);
15757 t2 = TREE_TYPE (op2);
15759 /* The first expression must be a boolean */
15760 if (TREE_TYPE (cond) != boolean_type_node)
15762 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15763 parse_error_context (wfl_operator,
15764 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15765 lang_printable_name (TREE_TYPE (cond), 0));
15766 error_found = 1;
15769 /* Second and third can be numeric, boolean (i.e. primitive),
15770 references or null. Anything else results in an error */
15771 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15772 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15773 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15774 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15775 error_found = 1;
15777 /* Determine the type of the conditional expression. Same types are
15778 easy to deal with */
15779 else if (t1 == t2)
15780 resulting_type = t1;
15782 /* There are different rules for numeric types */
15783 else if (JNUMERIC_TYPE_P (t1))
15785 /* if byte/short found, the resulting type is short */
15786 if ((t1 == byte_type_node && t2 == short_type_node)
15787 || (t1 == short_type_node && t2 == byte_type_node))
15788 resulting_type = short_type_node;
15790 /* If t1 is a constant int and t2 is of type byte, short or char
15791 and t1's value fits in t2, then the resulting type is t2 */
15792 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15793 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15794 resulting_type = t2;
15796 /* If t2 is a constant int and t1 is of type byte, short or char
15797 and t2's value fits in t1, then the resulting type is t1 */
15798 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15799 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15800 resulting_type = t1;
15802 /* Otherwise, binary numeric promotion is applied and the
15803 resulting type is the promoted type of operand 1 and 2 */
15804 else
15805 resulting_type = binary_numeric_promotion (t1, t2,
15806 &TREE_OPERAND (node, 1),
15807 &TREE_OPERAND (node, 2));
15810 /* Cases of a reference and a null type */
15811 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15812 resulting_type = t1;
15814 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15815 resulting_type = t2;
15817 /* Last case: different reference types. If a type can be converted
15818 into the other one by assignment conversion, the latter
15819 determines the type of the expression */
15820 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15821 resulting_type = promote_type (t1);
15823 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15824 resulting_type = promote_type (t2);
15826 /* If we don't have any resulting type, we're in trouble */
15827 if (!resulting_type)
15829 char *t = xstrdup (lang_printable_name (t1, 0));
15830 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15831 parse_error_context (wfl_operator,
15832 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15833 t, lang_printable_name (t2, 0));
15834 free (t);
15835 error_found = 1;
15838 if (error_found)
15840 TREE_TYPE (node) = error_mark_node;
15841 return error_mark_node;
15844 TREE_TYPE (node) = resulting_type;
15845 TREE_SET_CODE (node, COND_EXPR);
15846 CAN_COMPLETE_NORMALLY (node) = 1;
15847 return node;
15850 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
15852 static tree
15853 maybe_build_class_init_for_field (decl, expr)
15854 tree decl, expr;
15856 tree clas = DECL_CONTEXT (decl);
15857 if (flag_emit_class_files || flag_emit_xref)
15858 return expr;
15860 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
15861 && FIELD_FINAL (decl))
15863 tree init = DECL_INITIAL (decl);
15864 if (init != NULL_TREE)
15865 init = fold_constant_for_init (init, decl);
15866 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
15867 return expr;
15870 return build_class_init (clas, expr);
15873 /* Try to constant fold NODE.
15874 If NODE is not a constant expression, return NULL_EXPR.
15875 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15877 static tree
15878 fold_constant_for_init (node, context)
15879 tree node;
15880 tree context;
15882 tree op0, op1, val;
15883 enum tree_code code = TREE_CODE (node);
15885 switch (code)
15887 case STRING_CST:
15888 case INTEGER_CST:
15889 case REAL_CST:
15890 return node;
15892 case PLUS_EXPR:
15893 case MINUS_EXPR:
15894 case MULT_EXPR:
15895 case TRUNC_MOD_EXPR:
15896 case RDIV_EXPR:
15897 case LSHIFT_EXPR:
15898 case RSHIFT_EXPR:
15899 case URSHIFT_EXPR:
15900 case BIT_AND_EXPR:
15901 case BIT_XOR_EXPR:
15902 case BIT_IOR_EXPR:
15903 case TRUTH_ANDIF_EXPR:
15904 case TRUTH_ORIF_EXPR:
15905 case EQ_EXPR:
15906 case NE_EXPR:
15907 case GT_EXPR:
15908 case GE_EXPR:
15909 case LT_EXPR:
15910 case LE_EXPR:
15911 op0 = TREE_OPERAND (node, 0);
15912 op1 = TREE_OPERAND (node, 1);
15913 val = fold_constant_for_init (op0, context);
15914 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15915 return NULL_TREE;
15916 TREE_OPERAND (node, 0) = val;
15917 val = fold_constant_for_init (op1, context);
15918 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15919 return NULL_TREE;
15920 TREE_OPERAND (node, 1) = val;
15921 return patch_binop (node, op0, op1);
15923 case UNARY_PLUS_EXPR:
15924 case NEGATE_EXPR:
15925 case TRUTH_NOT_EXPR:
15926 case BIT_NOT_EXPR:
15927 case CONVERT_EXPR:
15928 op0 = TREE_OPERAND (node, 0);
15929 val = fold_constant_for_init (op0, context);
15930 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15931 return NULL_TREE;
15932 TREE_OPERAND (node, 0) = val;
15933 return patch_unaryop (node, op0);
15934 break;
15936 case COND_EXPR:
15937 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15938 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15939 return NULL_TREE;
15940 TREE_OPERAND (node, 0) = val;
15941 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15942 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15943 return NULL_TREE;
15944 TREE_OPERAND (node, 1) = val;
15945 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15946 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15947 return NULL_TREE;
15948 TREE_OPERAND (node, 2) = val;
15949 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15950 : TREE_OPERAND (node, 2);
15952 case VAR_DECL:
15953 case FIELD_DECL:
15954 if (! FIELD_FINAL (node)
15955 || DECL_INITIAL (node) == NULL_TREE)
15956 return NULL_TREE;
15957 val = DECL_INITIAL (node);
15958 /* Guard against infinite recursion. */
15959 DECL_INITIAL (node) = NULL_TREE;
15960 val = fold_constant_for_init (val, node);
15961 DECL_INITIAL (node) = val;
15962 if (!val && CLASS_FINAL_VARIABLE_P (node))
15963 DECL_FIELD_FINAL_IUD (node) = 0;
15964 return val;
15966 case EXPR_WITH_FILE_LOCATION:
15967 /* Compare java_complete_tree and resolve_expression_name. */
15968 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15969 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15971 tree name = EXPR_WFL_NODE (node);
15972 tree decl;
15973 if (PRIMARY_P (node))
15974 return NULL_TREE;
15975 else if (! QUALIFIED_P (name))
15977 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15978 if (decl == NULL_TREE
15979 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15980 return NULL_TREE;
15981 return fold_constant_for_init (decl, decl);
15983 else
15985 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15986 qualify_ambiguous_name (node);
15987 if (resolve_field_access (node, &decl, NULL)
15988 && decl != NULL_TREE)
15989 return fold_constant_for_init (decl, decl);
15990 return NULL_TREE;
15993 else
15995 op0 = TREE_OPERAND (node, 0);
15996 val = fold_constant_for_init (op0, context);
15997 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15998 return NULL_TREE;
15999 TREE_OPERAND (node, 0) = val;
16000 return val;
16003 #ifdef USE_COMPONENT_REF
16004 case IDENTIFIER:
16005 case COMPONENT_REF:
16007 #endif
16009 default:
16010 return NULL_TREE;
16014 #ifdef USE_COMPONENT_REF
16015 /* Context is 'T' for TypeName, 'P' for PackageName,
16016 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16018 tree
16019 resolve_simple_name (name, context)
16020 tree name;
16021 int context;
16025 tree
16026 resolve_qualified_name (name, context)
16027 tree name;
16028 int context;
16031 #endif
16033 /* Mark P, which is really a `struct parser_ctxt **' for GC. */
16035 static void
16036 mark_parser_ctxt (p)
16037 void *p;
16039 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
16040 int i;
16042 if (!pc)
16043 return;
16045 #ifndef JC1_LITE
16046 for (i = 0; i < 11; ++i)
16047 ggc_mark_tree (pc->modifier_ctx[i]);
16048 ggc_mark_tree (pc->class_type);
16049 ggc_mark_tree (pc->function_decl);
16050 ggc_mark_tree (pc->package);
16051 ggc_mark_tree (pc->class_list);
16052 ggc_mark_tree (pc->current_parsed_class);
16053 ggc_mark_tree (pc->current_parsed_class_un);
16054 ggc_mark_tree (pc->non_static_initialized);
16055 ggc_mark_tree (pc->static_initialized);
16056 ggc_mark_tree (pc->instance_initializers);
16057 ggc_mark_tree (pc->import_list);
16058 ggc_mark_tree (pc->import_demand_list);
16059 ggc_mark_tree (pc->current_loop);
16060 ggc_mark_tree (pc->current_labeled_block);
16061 #endif /* JC1_LITE */
16063 if (pc->next)
16064 mark_parser_ctxt (&pc->next);
16067 void
16068 init_src_parse ()
16070 /* Register roots with the garbage collector. */
16071 ggc_add_tree_root (src_parse_roots, sizeof (src_parse_roots) / sizeof(tree));
16076 /* This section deals with the functions that are called when tables
16077 recording class initialization information are traversed. */
16079 /* Attach to PTR (a block) the declaration found in ENTRY. */
16081 static bool
16082 attach_init_test_initialization_flags (entry, ptr)
16083 struct hash_entry *entry;
16084 PTR ptr;
16086 tree block = (tree)ptr;
16087 struct init_test_hash_entry *ite = (struct init_test_hash_entry *) entry;
16089 TREE_CHAIN (ite->init_test_decl) = BLOCK_EXPR_DECLS (block);
16090 BLOCK_EXPR_DECLS (block) = ite->init_test_decl;
16091 return true;
16094 /* This function is called for each statement calling a static
16095 function. ENTRY is a TREE_LIST whose PURPOSE is the called
16096 function and VALUE is a compound whose second operand can be
16097 patched with static class initialization flag assignments. */
16099 static bool
16100 adjust_init_test_initialization (entry, info)
16101 struct hash_entry *entry;
16102 PTR info ATTRIBUTE_UNUSED;
16104 tree list = (tree)(entry->key);
16105 tree called_method = TREE_PURPOSE (list);
16106 tree compound = TREE_VALUE (list);
16107 tree assignment_compound_list = build_tree_list (called_method, NULL);
16109 /* For each class definitely initialized in CALLED_METHOD, fill
16110 ASSIGNMENT_COMPOUND with assignment to the class initialization flag. */
16111 hash_traverse (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
16112 emit_test_initialization, assignment_compound_list);
16114 if (TREE_VALUE (assignment_compound_list))
16115 TREE_OPERAND (compound, 1) = TREE_VALUE (assignment_compound_list);
16117 return true;
16120 /* This function is called for each classes that is known definitely
16121 assigned when a given static method was called. This function
16122 augments a compound expression (INFO) storing all assignment to
16123 initialized static class flags if a flag already existed, otherwise
16124 a new one is created. */
16126 static bool
16127 emit_test_initialization (entry, info)
16128 struct hash_entry *entry;
16129 PTR info;
16131 tree l = (tree) info;
16132 tree decl, init;
16134 struct init_test_hash_entry *ite = (struct init_test_hash_entry *)
16135 hash_lookup (&DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl),
16136 entry->key, FALSE, NULL);
16138 /* If we haven't found a flag and we're dealing with self registered
16139 with current_function_decl, then don't do anything. Self is
16140 always added as definitely initialized but this information is
16141 valid only if used outside the current function. */
16142 if (! ite)
16144 if (current_function_decl != TREE_PURPOSE (l))
16145 ite = (struct init_test_hash_entry *)
16146 hash_lookup (&DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl),
16147 entry->key, TRUE, NULL);
16148 else
16149 return true;
16152 /* If we don't have a variable, create one and install it. */
16153 if (! ite->init_test_decl)
16155 tree block;
16157 decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16158 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16159 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16160 DECL_CONTEXT (decl) = current_function_decl;
16161 DECL_INITIAL (decl) = boolean_true_node;
16163 /* The trick is to find the right context for it. */
16164 block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16165 TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16166 BLOCK_EXPR_DECLS (block) = decl;
16167 ite->init_test_decl = decl;
16169 else
16170 decl = ite->init_test_decl;
16172 /* Now simply augment the compound that holds all the assignments
16173 pertaining to this method invocation. */
16174 init = build (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16175 TREE_SIDE_EFFECTS (init) = 1;
16176 TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16177 TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16179 return true;