* configure.in: Arrange to include defaults.h in [ht]config.h/tm.h.
[official-gcc.git] / gcc / java / parse.y
blobcf438e92840113a65b6556b14d39942b86b53343
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 void expand_start_java_method PARAMS ((tree));
120 static tree find_name_in_single_imports PARAMS ((tree));
121 static void check_abstract_method_header PARAMS ((tree));
122 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
123 static tree resolve_expression_name PARAMS ((tree, tree *));
124 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
125 static int check_class_interface_creation PARAMS ((int, int, tree,
126 tree, tree, tree));
127 static tree patch_method_invocation PARAMS ((tree, tree, tree,
128 int *, tree *));
129 static int breakdown_qualified PARAMS ((tree *, 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 java_complete_expand_method PARAMS ((tree));
151 static int unresolved_type_p PARAMS ((tree, tree *));
152 static void create_jdep_list PARAMS ((struct parser_ctxt *));
153 static tree build_expr_block PARAMS ((tree, tree));
154 static tree enter_block PARAMS ((void));
155 static tree enter_a_block PARAMS ((tree));
156 static tree exit_block PARAMS ((void));
157 static tree lookup_name_in_blocks PARAMS ((tree));
158 static void maybe_absorb_scoping_blocks PARAMS ((void));
159 static tree build_method_invocation PARAMS ((tree, tree));
160 static tree build_new_invocation PARAMS ((tree, tree));
161 static tree build_assignment PARAMS ((int, int, tree, tree));
162 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
163 static int check_final_assignment PARAMS ((tree ,tree));
164 static tree patch_assignment PARAMS ((tree, tree, tree ));
165 static tree patch_binop PARAMS ((tree, tree, tree));
166 static tree build_unaryop PARAMS ((int, int, tree));
167 static tree build_incdec PARAMS ((int, int, tree, int));
168 static tree patch_unaryop PARAMS ((tree, tree));
169 static tree build_cast PARAMS ((int, tree, tree));
170 static tree build_null_of_type PARAMS ((tree));
171 static tree patch_cast PARAMS ((tree, tree));
172 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
173 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
174 static int valid_cast_to_p PARAMS ((tree, tree));
175 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
176 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
177 static tree try_reference_assignconv PARAMS ((tree, tree));
178 static tree build_unresolved_array_type PARAMS ((tree));
179 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
180 static tree build_array_ref PARAMS ((int, tree, tree));
181 static tree patch_array_ref PARAMS ((tree));
182 static tree make_qualified_name PARAMS ((tree, tree, int));
183 static tree merge_qualified_name PARAMS ((tree, tree));
184 static tree make_qualified_primary PARAMS ((tree, tree, int));
185 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
186 tree *, tree *));
187 static void qualify_ambiguous_name PARAMS ((tree));
188 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
189 static tree build_newarray_node PARAMS ((tree, tree, int));
190 static tree patch_newarray PARAMS ((tree));
191 static tree resolve_type_during_patch PARAMS ((tree));
192 static tree build_this PARAMS ((int));
193 static tree build_wfl_wrap PARAMS ((tree, int));
194 static tree build_return PARAMS ((int, tree));
195 static tree patch_return PARAMS ((tree));
196 static tree maybe_access_field PARAMS ((tree, tree, tree));
197 static int complete_function_arguments PARAMS ((tree));
198 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
199 tree, tree));
200 static int not_accessible_p PARAMS ((tree, tree, int));
201 static void check_deprecation PARAMS ((tree, tree));
202 static int class_in_current_package PARAMS ((tree));
203 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
204 static tree patch_if_else_statement PARAMS ((tree));
205 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
206 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
207 static tree patch_exit_expr PARAMS ((tree));
208 static tree build_labeled_block PARAMS ((int, tree));
209 static tree finish_labeled_statement PARAMS ((tree, tree));
210 static tree build_bc_statement PARAMS ((int, int, tree));
211 static tree patch_bc_statement PARAMS ((tree));
212 static tree patch_loop_statement PARAMS ((tree));
213 static tree build_new_loop PARAMS ((tree));
214 static tree build_loop_body PARAMS ((int, tree, int));
215 static tree finish_loop_body PARAMS ((int, tree, tree, int));
216 static tree build_debugable_stmt PARAMS ((int, tree));
217 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
218 static tree patch_switch_statement PARAMS ((tree));
219 static tree string_constant_concatenation PARAMS ((tree, tree));
220 static tree build_string_concatenation PARAMS ((tree, tree));
221 static tree patch_string_cst PARAMS ((tree));
222 static tree patch_string PARAMS ((tree));
223 static tree build_try_statement PARAMS ((int, tree, tree));
224 static tree build_try_finally_statement PARAMS ((int, tree, tree));
225 static tree patch_try_statement PARAMS ((tree));
226 static tree patch_synchronized_statement PARAMS ((tree, tree));
227 static tree patch_throw_statement PARAMS ((tree, tree));
228 static void check_thrown_exceptions PARAMS ((int, tree));
229 static int check_thrown_exceptions_do PARAMS ((tree));
230 static void purge_unchecked_exceptions PARAMS ((tree));
231 static void check_throws_clauses PARAMS ((tree, tree, tree));
232 static void finish_method_declaration PARAMS ((tree));
233 static tree build_super_invocation PARAMS ((tree));
234 static int verify_constructor_circularity PARAMS ((tree, tree));
235 static char *constructor_circularity_msg PARAMS ((tree, tree));
236 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
237 int, int));
238 static const char *get_printable_method_name PARAMS ((tree));
239 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
240 static tree generate_finit PARAMS ((tree));
241 static void add_instance_initializer PARAMS ((tree));
242 static void fix_constructors PARAMS ((tree));
243 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
244 tree, int *));
245 static void craft_constructor PARAMS ((tree, tree));
246 static int verify_constructor_super PARAMS ((tree));
247 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
248 static void start_artificial_method_body PARAMS ((tree));
249 static void end_artificial_method_body PARAMS ((tree));
250 static int check_method_redefinition PARAMS ((tree, tree));
251 static int check_method_types_complete PARAMS ((tree));
252 static void java_check_regular_methods PARAMS ((tree));
253 static void java_check_abstract_methods PARAMS ((tree));
254 static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
255 static void unreachable_stmt_error PARAMS ((tree));
256 static tree find_expr_with_wfl PARAMS ((tree));
257 static void missing_return_error PARAMS ((tree));
258 static tree build_new_array_init PARAMS ((int, tree));
259 static tree patch_new_array_init PARAMS ((tree, tree));
260 static tree maybe_build_array_element_wfl PARAMS ((tree));
261 static int array_constructor_check_entry PARAMS ((tree, tree));
262 static const char *purify_type_name PARAMS ((const char *));
263 static tree fold_constant_for_init PARAMS ((tree, tree));
264 static tree strip_out_static_field_access_decl PARAMS ((tree));
265 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
266 static void static_ref_err PARAMS ((tree, tree, tree));
267 static void parser_add_interface PARAMS ((tree, tree, tree));
268 static void add_superinterfaces PARAMS ((tree, tree));
269 static tree jdep_resolve_class PARAMS ((jdep *));
270 static int note_possible_classname PARAMS ((const char *, int));
271 static void java_complete_expand_classes PARAMS ((void));
272 static void java_complete_expand_class PARAMS ((tree));
273 static void java_complete_expand_methods PARAMS ((tree));
274 static tree cut_identifier_in_qualified PARAMS ((tree));
275 static tree java_stabilize_reference PARAMS ((tree));
276 static tree do_unary_numeric_promotion PARAMS ((tree));
277 static char * operator_string PARAMS ((tree));
278 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
279 static tree merge_string_cste PARAMS ((tree, tree, int));
280 static tree java_refold PARAMS ((tree));
281 static int java_decl_equiv PARAMS ((tree, tree));
282 static int binop_compound_p PARAMS ((enum tree_code));
283 static tree search_loop PARAMS ((tree));
284 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
285 static int check_abstract_method_definitions PARAMS ((int, tree, tree));
286 static void java_check_abstract_method_definitions PARAMS ((tree));
287 static void java_debug_context_do PARAMS ((int));
288 static void java_parser_context_push_initialized_field PARAMS ((void));
289 static void java_parser_context_pop_initialized_field PARAMS ((void));
290 static tree reorder_static_initialized PARAMS ((tree));
291 static void java_parser_context_suspend PARAMS ((void));
292 static void java_parser_context_resume PARAMS ((void));
293 static int pop_current_osb PARAMS ((struct parser_ctxt *));
295 /* JDK 1.1 work. FIXME */
297 static tree maybe_make_nested_class_name PARAMS ((tree));
298 static void make_nested_class_name PARAMS ((tree));
299 static void set_nested_class_simple_name_value PARAMS ((tree, int));
300 static void link_nested_class_to_enclosing PARAMS ((void));
301 static tree find_as_inner_class PARAMS ((tree, tree, tree));
302 static tree find_as_inner_class_do PARAMS ((tree, tree));
303 static int check_inner_class_redefinition PARAMS ((tree, tree));
305 static tree build_thisn_assign PARAMS ((void));
306 static tree build_current_thisn PARAMS ((tree));
307 static tree build_access_to_thisn PARAMS ((tree, tree, int));
308 static tree maybe_build_thisn_access_method PARAMS ((tree));
310 static tree build_outer_field_access PARAMS ((tree, tree));
311 static tree build_outer_field_access_methods PARAMS ((tree));
312 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
313 tree, tree));
314 static tree build_outer_method_access_method PARAMS ((tree));
315 static tree build_new_access_id PARAMS ((void));
316 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
317 tree, tree));
319 static int outer_field_access_p PARAMS ((tree, tree));
320 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
321 tree *, tree *));
322 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
323 static tree build_incomplete_class_ref PARAMS ((int, tree));
324 static tree patch_incomplete_class_ref PARAMS ((tree));
325 static tree create_anonymous_class PARAMS ((int, tree));
326 static void patch_anonymous_class PARAMS ((tree, tree, tree));
327 static void add_inner_class_fields PARAMS ((tree, tree));
329 static tree build_dot_class_method PARAMS ((tree));
330 static tree build_dot_class_method_invocation PARAMS ((tree));
331 static void create_new_parser_context PARAMS ((int));
332 static void mark_parser_ctxt PARAMS ((void *));
334 /* Number of error found so far. */
335 int java_error_count;
336 /* Number of warning found so far. */
337 int java_warning_count;
338 /* Tell when not to fold, when doing xrefs */
339 int do_not_fold;
340 /* Cyclic inheritance report, as it can be set by layout_class */
341 const char *cyclic_inheritance_report;
343 /* Tell when we're within an instance initializer */
344 static int in_instance_initializer;
346 /* The current parser context */
347 struct parser_ctxt *ctxp;
349 /* List of things that were analyzed for which code will be generated */
350 static struct parser_ctxt *ctxp_for_generation = NULL;
352 /* binop_lookup maps token to tree_code. It is used where binary
353 operations are involved and required by the parser. RDIV_EXPR
354 covers both integral/floating point division. The code is changed
355 once the type of both operator is worked out. */
357 static enum tree_code binop_lookup[19] =
359 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
360 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
361 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
362 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
363 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
365 #define BINOP_LOOKUP(VALUE) \
366 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
368 /* This is the end index for binary operators that can also be used
369 in compound assignements. */
370 #define BINOP_COMPOUND_CANDIDATES 11
372 /* The "$L" identifier we use to create labels. */
373 static tree label_id = NULL_TREE;
375 /* The "StringBuffer" identifier used for the String `+' operator. */
376 static tree wfl_string_buffer = NULL_TREE;
378 /* The "append" identifier used for String `+' operator. */
379 static tree wfl_append = NULL_TREE;
381 /* The "toString" identifier used for String `+' operator. */
382 static tree wfl_to_string = NULL_TREE;
384 /* The "java.lang" import qualified name. */
385 static tree java_lang_id = NULL_TREE;
387 /* The generated `inst$' identifier used for generated enclosing
388 instance/field access functions. */
389 static tree inst_id = NULL_TREE;
391 /* The "java.lang.Cloneable" qualified name. */
392 static tree java_lang_cloneable = NULL_TREE;
394 /* The "java.io.Serializable" qualified name. */
395 static tree java_io_serializable = NULL_TREE;
397 /* Context and flag for static blocks */
398 static tree current_static_block = NULL_TREE;
400 /* The generated `write_parm_value$' identifier. */
401 static tree wpv_id;
403 /* The list of all packages we've seen so far */
404 static tree package_list = NULL_TREE;
406 /* Hold THIS for the scope of the current public method decl. */
407 static tree current_this;
409 /* Hold a list of catch clauses list. The first element of this list is
410 the list of the catch clauses of the currently analysed try block. */
411 static tree currently_caught_type_list;
413 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
414 line and point it out. */
415 /* Should point out the one that don't fit. ASCII/unicode, going
416 backward. FIXME */
418 #define check_modifiers(__message, __value, __mask) do { \
419 if ((__value) & ~(__mask)) \
421 int i, remainder = (__value) & ~(__mask); \
422 for (i = 0; i <= 10; i++) \
423 if ((1 << i) & remainder) \
424 parse_error_context (ctxp->modifier_ctx [i], (__message), \
425 java_accstring_lookup (1 << i)); \
427 } while (0)
431 %union {
432 tree node;
433 int sub_token;
434 struct {
435 int token;
436 int location;
437 } operator;
438 int value;
442 #include "lex.c"
445 %pure_parser
447 /* Things defined here have to match the order of what's in the
448 binop_lookup table. */
450 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
451 %token LS_TK SRS_TK ZRS_TK
452 %token AND_TK XOR_TK OR_TK
453 %token BOOL_AND_TK BOOL_OR_TK
454 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
456 /* This maps to the same binop_lookup entry than the token above */
458 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
459 %token REM_ASSIGN_TK
460 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
461 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
464 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
466 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
467 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
468 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
469 %token PAD_TK ABSTRACT_TK MODIFIER_TK
470 %token STRICT_TK
472 /* Keep those two in order, too */
473 %token DECR_TK INCR_TK
475 /* From now one, things can be in any order */
477 %token DEFAULT_TK IF_TK THROW_TK
478 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
479 %token THROWS_TK BREAK_TK IMPORT_TK
480 %token ELSE_TK INSTANCEOF_TK RETURN_TK
481 %token VOID_TK CATCH_TK INTERFACE_TK
482 %token CASE_TK EXTENDS_TK FINALLY_TK
483 %token SUPER_TK WHILE_TK CLASS_TK
484 %token SWITCH_TK CONST_TK TRY_TK
485 %token FOR_TK NEW_TK CONTINUE_TK
486 %token GOTO_TK PACKAGE_TK THIS_TK
488 %token BYTE_TK SHORT_TK INT_TK LONG_TK
489 %token CHAR_TK INTEGRAL_TK
491 %token FLOAT_TK DOUBLE_TK FP_TK
493 %token ID_TK
495 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
497 %token ASSIGN_ANY_TK ASSIGN_TK
498 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
500 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
501 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
503 %type <value> modifiers MODIFIER_TK final synchronized
505 %type <node> super ID_TK identifier
506 %type <node> name simple_name qualified_name
507 %type <node> type_declaration compilation_unit
508 field_declaration method_declaration extends_interfaces
509 interfaces interface_type_list
510 class_member_declaration
511 import_declarations package_declaration
512 type_declarations interface_body
513 interface_member_declaration constant_declaration
514 interface_member_declarations interface_type
515 abstract_method_declaration interface_type_list
516 %type <node> class_body_declaration class_member_declaration
517 static_initializer constructor_declaration block
518 %type <node> class_body_declarations constructor_header
519 %type <node> class_or_interface_type class_type class_type_list
520 constructor_declarator explicit_constructor_invocation
521 %type <node> dim_expr dim_exprs this_or_super throws
523 %type <node> variable_declarator_id variable_declarator
524 variable_declarators variable_initializer
525 variable_initializers constructor_body
526 array_initializer
528 %type <node> class_body block_end constructor_block_end
529 %type <node> statement statement_without_trailing_substatement
530 labeled_statement if_then_statement label_decl
531 if_then_else_statement while_statement for_statement
532 statement_nsi labeled_statement_nsi do_statement
533 if_then_else_statement_nsi while_statement_nsi
534 for_statement_nsi statement_expression_list for_init
535 for_update statement_expression expression_statement
536 primary_no_new_array expression primary
537 array_creation_expression array_type
538 class_instance_creation_expression field_access
539 method_invocation array_access something_dot_new
540 argument_list postfix_expression while_expression
541 post_increment_expression post_decrement_expression
542 unary_expression_not_plus_minus unary_expression
543 pre_increment_expression pre_decrement_expression
544 unary_expression_not_plus_minus cast_expression
545 multiplicative_expression additive_expression
546 shift_expression relational_expression
547 equality_expression and_expression
548 exclusive_or_expression inclusive_or_expression
549 conditional_and_expression conditional_or_expression
550 conditional_expression assignment_expression
551 left_hand_side assignment for_header for_begin
552 constant_expression do_statement_begin empty_statement
553 switch_statement synchronized_statement throw_statement
554 try_statement switch_expression switch_block
555 catches catch_clause catch_clause_parameter finally
556 anonymous_class_creation
557 %type <node> return_statement break_statement continue_statement
559 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
560 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
561 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
562 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
563 %type <operator> ASSIGN_ANY_TK assignment_operator
564 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
565 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
566 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
567 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
568 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
569 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
570 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
571 %type <operator> NEW_TK
573 %type <node> method_body
575 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
576 STRING_LIT_TK NULL_TK VOID_TK
578 %type <node> IF_TK WHILE_TK FOR_TK
580 %type <node> formal_parameter_list formal_parameter
581 method_declarator method_header
583 %type <node> primitive_type reference_type type
584 BOOLEAN_TK INTEGRAL_TK FP_TK
586 /* Added or modified JDK 1.1 rule types */
587 %type <node> type_literals
590 /* 19.2 Production from 2.3: The Syntactic Grammar */
591 goal:
593 /* Register static variables with the garbage
594 collector. */
595 ggc_add_tree_root (&label_id, 1);
596 ggc_add_tree_root (&wfl_string_buffer, 1);
597 ggc_add_tree_root (&wfl_append, 1);
598 ggc_add_tree_root (&wfl_to_string, 1);
599 ggc_add_tree_root (&java_lang_id, 1);
600 ggc_add_tree_root (&inst_id, 1);
601 ggc_add_tree_root (&java_lang_cloneable, 1);
602 ggc_add_tree_root (&java_io_serializable, 1);
603 ggc_add_tree_root (&current_static_block, 1);
604 ggc_add_tree_root (&wpv_id, 1);
605 ggc_add_tree_root (&package_list, 1);
606 ggc_add_tree_root (&current_this, 1);
607 ggc_add_tree_root (&currently_caught_type_list, 1);
608 ggc_add_root (&ctxp, 1,
609 sizeof (struct parser_ctxt *),
610 mark_parser_ctxt);
611 ggc_add_root (&ctxp_for_generation, 1,
612 sizeof (struct parser_ctxt *),
613 mark_parser_ctxt);
615 compilation_unit
619 /* 19.3 Productions from 3: Lexical structure */
620 literal:
621 INT_LIT_TK
622 | FP_LIT_TK
623 | BOOL_LIT_TK
624 | CHAR_LIT_TK
625 | STRING_LIT_TK
626 | NULL_TK
629 /* 19.4 Productions from 4: Types, Values and Variables */
630 type:
631 primitive_type
632 | reference_type
635 primitive_type:
636 INTEGRAL_TK
637 | FP_TK
638 | BOOLEAN_TK
641 reference_type:
642 class_or_interface_type
643 | array_type
646 class_or_interface_type:
647 name
650 class_type:
651 class_or_interface_type /* Default rule */
654 interface_type:
655 class_or_interface_type
658 array_type:
659 primitive_type dims
661 int osb = pop_current_osb (ctxp);
662 tree t = build_java_array_type (($1), -1);
663 CLASS_LOADED_P (t) = 1;
664 while (--osb)
665 t = build_unresolved_array_type (t);
666 $$ = t;
668 | name dims
670 int osb = pop_current_osb (ctxp);
671 tree t = $1;
672 while (osb--)
673 t = build_unresolved_array_type (t);
674 $$ = t;
678 /* 19.5 Productions from 6: Names */
679 name:
680 simple_name /* Default rule */
681 | qualified_name /* Default rule */
684 simple_name:
685 identifier /* Default rule */
688 qualified_name:
689 name DOT_TK identifier
690 { $$ = make_qualified_name ($1, $3, $2.location); }
693 identifier:
694 ID_TK
697 /* 19.6: Production from 7: Packages */
698 compilation_unit:
699 {$$ = NULL;}
700 | package_declaration
701 | import_declarations
702 | type_declarations
703 | package_declaration import_declarations
704 | package_declaration type_declarations
705 | import_declarations type_declarations
706 | package_declaration import_declarations type_declarations
709 import_declarations:
710 import_declaration
712 $$ = NULL;
714 | import_declarations import_declaration
716 $$ = NULL;
720 type_declarations:
721 type_declaration
722 | type_declarations type_declaration
725 package_declaration:
726 PACKAGE_TK name SC_TK
728 ctxp->package = EXPR_WFL_NODE ($2);
729 register_package (ctxp->package);
731 | PACKAGE_TK error
732 {yyerror ("Missing name"); RECOVER;}
733 | PACKAGE_TK name error
734 {yyerror ("';' expected"); RECOVER;}
737 import_declaration:
738 single_type_import_declaration
739 | type_import_on_demand_declaration
742 single_type_import_declaration:
743 IMPORT_TK name SC_TK
745 tree name = EXPR_WFL_NODE ($2), last_name;
746 int i = IDENTIFIER_LENGTH (name)-1;
747 const char *last = &IDENTIFIER_POINTER (name)[i];
748 while (last != IDENTIFIER_POINTER (name))
750 if (last [0] == '.')
751 break;
752 last--;
754 last_name = get_identifier (++last);
755 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
757 tree err = find_name_in_single_imports (last_name);
758 if (err && err != name)
759 parse_error_context
760 ($2, "Ambiguous class: `%s' and `%s'",
761 IDENTIFIER_POINTER (name),
762 IDENTIFIER_POINTER (err));
763 else
764 REGISTER_IMPORT ($2, last_name);
766 else
767 REGISTER_IMPORT ($2, last_name);
769 | IMPORT_TK error
770 {yyerror ("Missing name"); RECOVER;}
771 | IMPORT_TK name error
772 {yyerror ("';' expected"); RECOVER;}
775 type_import_on_demand_declaration:
776 IMPORT_TK name DOT_TK MULT_TK SC_TK
778 tree name = EXPR_WFL_NODE ($2);
779 /* Don't import java.lang.* twice. */
780 if (name != java_lang_id)
782 read_import_dir ($2);
783 ctxp->import_demand_list =
784 chainon (ctxp->import_demand_list,
785 build_tree_list ($2, NULL_TREE));
788 | IMPORT_TK name DOT_TK error
789 {yyerror ("'*' expected"); RECOVER;}
790 | IMPORT_TK name DOT_TK MULT_TK error
791 {yyerror ("';' expected"); RECOVER;}
794 type_declaration:
795 class_declaration
796 { end_class_declaration (0); }
797 | interface_declaration
798 { end_class_declaration (0); }
799 | empty_statement
800 | error
802 YYERROR_NOW;
803 yyerror ("Class or interface declaration expected");
807 /* 19.7 Shortened from the original:
808 modifiers: modifier | modifiers modifier
809 modifier: any of public... */
810 modifiers:
811 MODIFIER_TK
813 $$ = (1 << $1);
815 | modifiers MODIFIER_TK
817 int acc = (1 << $2);
818 if ($$ & acc)
819 parse_error_context
820 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
821 java_accstring_lookup (acc));
822 else
824 $$ |= acc;
829 /* 19.8.1 Production from $8.1: Class Declaration */
830 class_declaration:
831 modifiers CLASS_TK identifier super interfaces
832 { create_class ($1, $3, $4, $5); }
833 class_body
834 | CLASS_TK identifier super interfaces
835 { create_class (0, $2, $3, $4); }
836 class_body
837 | modifiers CLASS_TK error
838 {yyerror ("Missing class name"); RECOVER;}
839 | CLASS_TK error
840 {yyerror ("Missing class name"); RECOVER;}
841 | CLASS_TK identifier error
843 if (!ctxp->class_err) yyerror ("'{' expected");
844 DRECOVER(class1);
846 | modifiers CLASS_TK identifier error
847 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
850 super:
851 { $$ = NULL; }
852 | EXTENDS_TK class_type
853 { $$ = $2; }
854 | EXTENDS_TK class_type error
855 {yyerror ("'{' expected"); ctxp->class_err=1;}
856 | EXTENDS_TK error
857 {yyerror ("Missing super class name"); ctxp->class_err=1;}
860 interfaces:
861 { $$ = NULL_TREE; }
862 | IMPLEMENTS_TK interface_type_list
863 { $$ = $2; }
864 | IMPLEMENTS_TK error
866 ctxp->class_err=1;
867 yyerror ("Missing interface name");
871 interface_type_list:
872 interface_type
874 ctxp->interface_number = 1;
875 $$ = build_tree_list ($1, NULL_TREE);
877 | interface_type_list C_TK interface_type
879 ctxp->interface_number++;
880 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
882 | interface_type_list C_TK error
883 {yyerror ("Missing interface name"); RECOVER;}
886 class_body:
887 OCB_TK CCB_TK
889 /* Store the location of the `}' when doing xrefs */
890 if (flag_emit_xref)
891 DECL_END_SOURCE_LINE (GET_CPC ()) =
892 EXPR_WFL_ADD_COL ($2.location, 1);
893 $$ = GET_CPC ();
895 | OCB_TK class_body_declarations CCB_TK
897 /* Store the location of the `}' when doing xrefs */
898 if (flag_emit_xref)
899 DECL_END_SOURCE_LINE (GET_CPC ()) =
900 EXPR_WFL_ADD_COL ($3.location, 1);
901 $$ = GET_CPC ();
905 class_body_declarations:
906 class_body_declaration
907 | class_body_declarations class_body_declaration
910 class_body_declaration:
911 class_member_declaration
912 | static_initializer
913 | constructor_declaration
914 | block /* Added, JDK1.1, instance initializer */
916 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
917 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
921 class_member_declaration:
922 field_declaration
923 | method_declaration
924 | class_declaration /* Added, JDK1.1 inner classes */
925 { end_class_declaration (1); }
926 | interface_declaration /* Added, JDK1.1 inner interfaces */
927 { end_class_declaration (1); }
928 | empty_statement
931 /* 19.8.2 Productions from 8.3: Field Declarations */
932 field_declaration:
933 type variable_declarators SC_TK
934 { register_fields (0, $1, $2); }
935 | modifiers type variable_declarators SC_TK
937 check_modifiers
938 ("Illegal modifier `%s' for field declaration",
939 $1, FIELD_MODIFIERS);
940 check_modifiers_consistency ($1);
941 register_fields ($1, $2, $3);
945 variable_declarators:
946 /* Should we use build_decl_list () instead ? FIXME */
947 variable_declarator /* Default rule */
948 | variable_declarators C_TK variable_declarator
949 { $$ = chainon ($1, $3); }
950 | variable_declarators C_TK error
951 {yyerror ("Missing term"); RECOVER;}
954 variable_declarator:
955 variable_declarator_id
956 { $$ = build_tree_list ($1, NULL_TREE); }
957 | variable_declarator_id ASSIGN_TK variable_initializer
959 if (java_error_count)
960 $3 = NULL_TREE;
961 $$ = build_tree_list
962 ($1, build_assignment ($2.token, $2.location, $1, $3));
964 | variable_declarator_id ASSIGN_TK error
966 yyerror ("Missing variable initializer");
967 $$ = build_tree_list ($1, NULL_TREE);
968 RECOVER;
970 | variable_declarator_id ASSIGN_TK variable_initializer error
972 yyerror ("';' expected");
973 $$ = build_tree_list ($1, NULL_TREE);
974 RECOVER;
978 variable_declarator_id:
979 identifier
980 | variable_declarator_id OSB_TK CSB_TK
981 { $$ = build_unresolved_array_type ($1); }
982 | identifier error
983 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
984 | variable_declarator_id OSB_TK error
986 tree node = java_lval.node;
987 if (node && (TREE_CODE (node) == INTEGER_CST
988 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
989 yyerror ("Can't specify array dimension in a declaration");
990 else
991 yyerror ("']' expected");
992 DRECOVER(vdi);
994 | variable_declarator_id CSB_TK error
995 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
998 variable_initializer:
999 expression
1000 | array_initializer
1003 /* 19.8.3 Productions from 8.4: Method Declarations */
1004 method_declaration:
1005 method_header
1007 current_function_decl = $1;
1008 if (current_function_decl
1009 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1010 source_start_java_method (current_function_decl);
1011 else
1012 current_function_decl = NULL_TREE;
1014 method_body
1015 { finish_method_declaration ($3); }
1016 | method_header error
1017 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1020 method_header:
1021 type method_declarator throws
1022 { $$ = method_header (0, $1, $2, $3); }
1023 | VOID_TK method_declarator throws
1024 { $$ = method_header (0, void_type_node, $2, $3); }
1025 | modifiers type method_declarator throws
1026 { $$ = method_header ($1, $2, $3, $4); }
1027 | modifiers VOID_TK method_declarator throws
1028 { $$ = method_header ($1, void_type_node, $3, $4); }
1029 | type error
1031 yyerror ("Invalid method declaration, method name required");
1032 RECOVER;
1034 | modifiers type error
1035 {RECOVER;}
1036 | VOID_TK error
1037 {yyerror ("Identifier expected"); RECOVER;}
1038 | modifiers VOID_TK error
1039 {yyerror ("Identifier expected"); RECOVER;}
1040 | modifiers error
1042 yyerror ("Invalid method declaration, return type required");
1043 RECOVER;
1047 method_declarator:
1048 identifier OP_TK CP_TK
1050 ctxp->formal_parameter_number = 0;
1051 $$ = method_declarator ($1, NULL_TREE);
1053 | identifier OP_TK formal_parameter_list CP_TK
1054 { $$ = method_declarator ($1, $3); }
1055 | method_declarator OSB_TK CSB_TK
1057 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1058 TREE_PURPOSE ($1) =
1059 build_unresolved_array_type (TREE_PURPOSE ($1));
1060 parse_warning_context
1061 (wfl_operator,
1062 "Discouraged form of returned type specification");
1064 | identifier OP_TK error
1065 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1066 | method_declarator OSB_TK error
1067 {yyerror ("']' expected"); RECOVER;}
1070 formal_parameter_list:
1071 formal_parameter
1073 ctxp->formal_parameter_number = 1;
1075 | formal_parameter_list C_TK formal_parameter
1077 ctxp->formal_parameter_number += 1;
1078 $$ = chainon ($1, $3);
1080 | formal_parameter_list C_TK error
1081 { yyerror ("Missing formal parameter term"); RECOVER; }
1084 formal_parameter:
1085 type variable_declarator_id
1087 $$ = build_tree_list ($2, $1);
1089 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1091 $$ = build_tree_list ($3, $2);
1092 ARG_FINAL_P ($$) = 1;
1094 | type error
1096 yyerror ("Missing identifier"); RECOVER;
1097 $$ = NULL_TREE;
1099 | final type error
1101 yyerror ("Missing identifier"); RECOVER;
1102 $$ = NULL_TREE;
1106 final:
1107 modifiers
1109 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1110 $1, ACC_FINAL);
1111 if ($1 != ACC_FINAL)
1112 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1116 throws:
1117 { $$ = NULL_TREE; }
1118 | THROWS_TK class_type_list
1119 { $$ = $2; }
1120 | THROWS_TK error
1121 {yyerror ("Missing class type term"); RECOVER;}
1124 class_type_list:
1125 class_type
1126 { $$ = build_tree_list ($1, $1); }
1127 | class_type_list C_TK class_type
1128 { $$ = tree_cons ($3, $3, $1); }
1129 | class_type_list C_TK error
1130 {yyerror ("Missing class type term"); RECOVER;}
1133 method_body:
1134 block
1135 | SC_TK { $$ = NULL_TREE; }
1138 /* 19.8.4 Productions from 8.5: Static Initializers */
1139 static_initializer:
1140 static block
1142 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1143 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1144 current_static_block = NULL_TREE;
1148 static: /* Test lval.sub_token here */
1149 modifiers
1151 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1152 /* Can't have a static initializer in an innerclass */
1153 if ($1 | ACC_STATIC &&
1154 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1155 parse_error_context
1156 (MODIFIER_WFL (STATIC_TK),
1157 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1158 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1159 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1163 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1164 constructor_declaration:
1165 constructor_header
1167 current_function_decl = $1;
1168 source_start_java_method (current_function_decl);
1170 constructor_body
1171 { finish_method_declaration ($3); }
1174 constructor_header:
1175 constructor_declarator throws
1176 { $$ = method_header (0, NULL_TREE, $1, $2); }
1177 | modifiers constructor_declarator throws
1178 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1181 constructor_declarator:
1182 simple_name OP_TK CP_TK
1184 ctxp->formal_parameter_number = 0;
1185 $$ = method_declarator ($1, NULL_TREE);
1187 | simple_name OP_TK formal_parameter_list CP_TK
1188 { $$ = method_declarator ($1, $3); }
1191 constructor_body:
1192 /* Unlike regular method, we always need a complete (empty)
1193 body so we can safely perform all the required code
1194 addition (super invocation and field initialization) */
1195 block_begin constructor_block_end
1197 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1198 $$ = $2;
1200 | block_begin explicit_constructor_invocation constructor_block_end
1201 { $$ = $3; }
1202 | block_begin block_statements constructor_block_end
1203 { $$ = $3; }
1204 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1205 { $$ = $4; }
1208 constructor_block_end:
1209 block_end
1212 /* Error recovery for that rule moved down expression_statement: rule. */
1213 explicit_constructor_invocation:
1214 this_or_super OP_TK CP_TK SC_TK
1216 $$ = build_method_invocation ($1, NULL_TREE);
1217 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1218 $$ = java_method_add_stmt (current_function_decl, $$);
1220 | this_or_super OP_TK argument_list CP_TK SC_TK
1222 $$ = build_method_invocation ($1, $3);
1223 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1224 $$ = java_method_add_stmt (current_function_decl, $$);
1226 /* Added, JDK1.1 inner classes. Modified because the rule
1227 'primary' couldn't work. */
1228 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1229 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1230 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1231 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1234 this_or_super: /* Added, simplifies error diagnostics */
1235 THIS_TK
1237 tree wfl = build_wfl_node (this_identifier_node);
1238 EXPR_WFL_LINECOL (wfl) = $1.location;
1239 $$ = wfl;
1241 | SUPER_TK
1243 tree wfl = build_wfl_node (super_identifier_node);
1244 EXPR_WFL_LINECOL (wfl) = $1.location;
1245 $$ = wfl;
1249 /* 19.9 Productions from 9: Interfaces */
1250 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1251 interface_declaration:
1252 INTERFACE_TK identifier
1253 { create_interface (0, $2, NULL_TREE); }
1254 interface_body
1255 | modifiers INTERFACE_TK identifier
1256 { create_interface ($1, $3, NULL_TREE); }
1257 interface_body
1258 | INTERFACE_TK identifier extends_interfaces
1259 { create_interface (0, $2, $3); }
1260 interface_body
1261 | modifiers INTERFACE_TK identifier extends_interfaces
1262 { create_interface ($1, $3, $4); }
1263 interface_body
1264 | INTERFACE_TK identifier error
1265 {yyerror ("'{' expected"); RECOVER;}
1266 | modifiers INTERFACE_TK identifier error
1267 {yyerror ("'{' expected"); RECOVER;}
1270 extends_interfaces:
1271 EXTENDS_TK interface_type
1273 ctxp->interface_number = 1;
1274 $$ = build_tree_list ($2, NULL_TREE);
1276 | extends_interfaces C_TK interface_type
1278 ctxp->interface_number++;
1279 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1281 | EXTENDS_TK error
1282 {yyerror ("Invalid interface type"); RECOVER;}
1283 | extends_interfaces C_TK error
1284 {yyerror ("Missing term"); RECOVER;}
1287 interface_body:
1288 OCB_TK CCB_TK
1289 { $$ = NULL_TREE; }
1290 | OCB_TK interface_member_declarations CCB_TK
1291 { $$ = NULL_TREE; }
1294 interface_member_declarations:
1295 interface_member_declaration
1296 | interface_member_declarations interface_member_declaration
1299 interface_member_declaration:
1300 constant_declaration
1301 | abstract_method_declaration
1302 | class_declaration /* Added, JDK1.1 inner classes */
1303 { end_class_declaration (1); }
1304 | interface_declaration /* Added, JDK1.1 inner interfaces */
1305 { end_class_declaration (1); }
1308 constant_declaration:
1309 field_declaration
1312 abstract_method_declaration:
1313 method_header SC_TK
1315 check_abstract_method_header ($1);
1316 current_function_decl = NULL_TREE; /* FIXME ? */
1318 | method_header error
1319 {yyerror ("';' expected"); RECOVER;}
1322 /* 19.10 Productions from 10: Arrays */
1323 array_initializer:
1324 OCB_TK CCB_TK
1325 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1326 | OCB_TK variable_initializers CCB_TK
1327 { $$ = build_new_array_init ($1.location, $2); }
1328 | OCB_TK variable_initializers C_TK CCB_TK
1329 { $$ = build_new_array_init ($1.location, $2); }
1332 variable_initializers:
1333 variable_initializer
1335 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1336 $1, NULL_TREE);
1338 | variable_initializers C_TK variable_initializer
1340 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1342 | variable_initializers C_TK error
1343 {yyerror ("Missing term"); RECOVER;}
1346 /* 19.11 Production from 14: Blocks and Statements */
1347 block:
1348 OCB_TK CCB_TK
1350 /* Store the location of the `}' when doing xrefs */
1351 if (current_function_decl && flag_emit_xref)
1352 DECL_END_SOURCE_LINE (current_function_decl) =
1353 EXPR_WFL_ADD_COL ($2.location, 1);
1354 $$ = empty_stmt_node;
1356 | block_begin block_statements block_end
1357 { $$ = $3; }
1360 block_begin:
1361 OCB_TK
1362 { enter_block (); }
1365 block_end:
1366 CCB_TK
1368 maybe_absorb_scoping_blocks ();
1369 /* Store the location of the `}' when doing xrefs */
1370 if (current_function_decl && flag_emit_xref)
1371 DECL_END_SOURCE_LINE (current_function_decl) =
1372 EXPR_WFL_ADD_COL ($1.location, 1);
1373 $$ = exit_block ();
1374 if (!BLOCK_SUBBLOCKS ($$))
1375 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1379 block_statements:
1380 block_statement
1381 | block_statements block_statement
1384 block_statement:
1385 local_variable_declaration_statement
1386 | statement
1387 { java_method_add_stmt (current_function_decl, $1); }
1388 | class_declaration /* Added, JDK1.1 local classes */
1390 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1391 end_class_declaration (1);
1395 local_variable_declaration_statement:
1396 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1399 local_variable_declaration:
1400 type variable_declarators
1401 { declare_local_variables (0, $1, $2); }
1402 | final type variable_declarators /* Added, JDK1.1 final locals */
1403 { declare_local_variables ($1, $2, $3); }
1406 statement:
1407 statement_without_trailing_substatement
1408 | labeled_statement
1409 | if_then_statement
1410 | if_then_else_statement
1411 | while_statement
1412 | for_statement
1413 { $$ = exit_block (); }
1416 statement_nsi:
1417 statement_without_trailing_substatement
1418 | labeled_statement_nsi
1419 | if_then_else_statement_nsi
1420 | while_statement_nsi
1421 | for_statement_nsi
1422 { $$ = exit_block (); }
1425 statement_without_trailing_substatement:
1426 block
1427 | empty_statement
1428 | expression_statement
1429 | switch_statement
1430 | do_statement
1431 | break_statement
1432 | continue_statement
1433 | return_statement
1434 | synchronized_statement
1435 | throw_statement
1436 | try_statement
1439 empty_statement:
1440 SC_TK
1442 if (flag_extraneous_semicolon)
1444 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1445 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1447 $$ = empty_stmt_node;
1451 label_decl:
1452 identifier REL_CL_TK
1454 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1455 EXPR_WFL_NODE ($1));
1456 pushlevel (2);
1457 push_labeled_block ($$);
1458 PUSH_LABELED_BLOCK ($$);
1462 labeled_statement:
1463 label_decl statement
1464 { $$ = finish_labeled_statement ($1, $2); }
1465 | identifier error
1466 {yyerror ("':' expected"); RECOVER;}
1469 labeled_statement_nsi:
1470 label_decl statement_nsi
1471 { $$ = finish_labeled_statement ($1, $2); }
1474 /* We concentrate here a bunch of error handling rules that we couldn't write
1475 earlier, because expression_statement catches a missing ';'. */
1476 expression_statement:
1477 statement_expression SC_TK
1479 /* We have a statement. Generate a WFL around it so
1480 we can debug it */
1481 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1482 /* We know we have a statement, so set the debug
1483 info to be eventually generate here. */
1484 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1486 | error SC_TK
1488 YYNOT_TWICE yyerror ("Invalid expression statement");
1489 DRECOVER (expr_stmt);
1491 | error OCB_TK
1493 YYNOT_TWICE yyerror ("Invalid expression statement");
1494 DRECOVER (expr_stmt);
1496 | error CCB_TK
1498 YYNOT_TWICE yyerror ("Invalid expression statement");
1499 DRECOVER (expr_stmt);
1501 | this_or_super OP_TK error
1502 {yyerror ("')' expected"); RECOVER;}
1503 | this_or_super OP_TK CP_TK error
1505 parse_ctor_invocation_error ();
1506 RECOVER;
1508 | this_or_super OP_TK argument_list error
1509 {yyerror ("')' expected"); RECOVER;}
1510 | this_or_super OP_TK argument_list CP_TK error
1512 parse_ctor_invocation_error ();
1513 RECOVER;
1515 | name DOT_TK SUPER_TK error
1516 {yyerror ("'(' expected"); RECOVER;}
1517 | name DOT_TK SUPER_TK OP_TK error
1518 {yyerror ("')' expected"); RECOVER;}
1519 | name DOT_TK SUPER_TK OP_TK argument_list error
1520 {yyerror ("')' expected"); RECOVER;}
1521 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1522 {yyerror ("';' expected"); RECOVER;}
1523 | name DOT_TK SUPER_TK OP_TK CP_TK error
1524 {yyerror ("';' expected"); RECOVER;}
1527 statement_expression:
1528 assignment
1529 | pre_increment_expression
1530 | pre_decrement_expression
1531 | post_increment_expression
1532 | post_decrement_expression
1533 | method_invocation
1534 | class_instance_creation_expression
1537 if_then_statement:
1538 IF_TK OP_TK expression CP_TK statement
1540 $$ = build_if_else_statement ($2.location, $3,
1541 $5, NULL_TREE);
1543 | IF_TK error
1544 {yyerror ("'(' expected"); RECOVER;}
1545 | IF_TK OP_TK error
1546 {yyerror ("Missing term"); RECOVER;}
1547 | IF_TK OP_TK expression error
1548 {yyerror ("')' expected"); RECOVER;}
1551 if_then_else_statement:
1552 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1553 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1556 if_then_else_statement_nsi:
1557 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1558 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1561 switch_statement:
1562 switch_expression
1564 enter_block ();
1566 switch_block
1568 /* Make into "proper list" of COMPOUND_EXPRs.
1569 I.e. make the last statment also have its own
1570 COMPOUND_EXPR. */
1571 maybe_absorb_scoping_blocks ();
1572 TREE_OPERAND ($1, 1) = exit_block ();
1573 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1577 switch_expression:
1578 SWITCH_TK OP_TK expression CP_TK
1580 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1581 EXPR_WFL_LINECOL ($$) = $2.location;
1583 | SWITCH_TK error
1584 {yyerror ("'(' expected"); RECOVER;}
1585 | SWITCH_TK OP_TK error
1586 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1587 | SWITCH_TK OP_TK expression CP_TK error
1588 {yyerror ("'{' expected"); RECOVER;}
1591 /* Default assignment is there to avoid type node on switch_block
1592 node. */
1594 switch_block:
1595 OCB_TK CCB_TK
1596 { $$ = NULL_TREE; }
1597 | OCB_TK switch_labels CCB_TK
1598 { $$ = NULL_TREE; }
1599 | OCB_TK switch_block_statement_groups CCB_TK
1600 { $$ = NULL_TREE; }
1601 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1602 { $$ = NULL_TREE; }
1605 switch_block_statement_groups:
1606 switch_block_statement_group
1607 | switch_block_statement_groups switch_block_statement_group
1610 switch_block_statement_group:
1611 switch_labels block_statements
1614 switch_labels:
1615 switch_label
1616 | switch_labels switch_label
1619 switch_label:
1620 CASE_TK constant_expression REL_CL_TK
1622 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1623 EXPR_WFL_LINECOL (lab) = $1.location;
1624 java_method_add_stmt (current_function_decl, lab);
1626 | DEFAULT_TK REL_CL_TK
1628 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1629 EXPR_WFL_LINECOL (lab) = $1.location;
1630 java_method_add_stmt (current_function_decl, lab);
1632 | CASE_TK error
1633 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1634 | CASE_TK constant_expression error
1635 {yyerror ("':' expected"); RECOVER;}
1636 | DEFAULT_TK error
1637 {yyerror ("':' expected"); RECOVER;}
1640 while_expression:
1641 WHILE_TK OP_TK expression CP_TK
1643 tree body = build_loop_body ($2.location, $3, 0);
1644 $$ = build_new_loop (body);
1648 while_statement:
1649 while_expression statement
1650 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1651 | WHILE_TK error
1652 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1653 | WHILE_TK OP_TK error
1654 {yyerror ("Missing term and ')' expected"); RECOVER;}
1655 | WHILE_TK OP_TK expression error
1656 {yyerror ("')' expected"); RECOVER;}
1659 while_statement_nsi:
1660 while_expression statement_nsi
1661 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1664 do_statement_begin:
1665 DO_TK
1667 tree body = build_loop_body (0, NULL_TREE, 1);
1668 $$ = build_new_loop (body);
1670 /* Need error handing here. FIXME */
1673 do_statement:
1674 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1675 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1678 for_statement:
1679 for_begin SC_TK expression SC_TK for_update CP_TK statement
1681 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1682 $3 = build_wfl_node ($3);
1683 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1685 | for_begin SC_TK SC_TK for_update CP_TK statement
1687 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1688 /* We have not condition, so we get rid of the EXIT_EXPR */
1689 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1690 empty_stmt_node;
1692 | for_begin SC_TK error
1693 {yyerror ("Invalid control expression"); RECOVER;}
1694 | for_begin SC_TK expression SC_TK error
1695 {yyerror ("Invalid update expression"); RECOVER;}
1696 | for_begin SC_TK SC_TK error
1697 {yyerror ("Invalid update expression"); RECOVER;}
1700 for_statement_nsi:
1701 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1702 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1703 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1705 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1706 /* We have not condition, so we get rid of the EXIT_EXPR */
1707 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1708 empty_stmt_node;
1712 for_header:
1713 FOR_TK OP_TK
1715 /* This scope defined for local variable that may be
1716 defined within the scope of the for loop */
1717 enter_block ();
1719 | FOR_TK error
1720 {yyerror ("'(' expected"); DRECOVER(for_1);}
1721 | FOR_TK OP_TK error
1722 {yyerror ("Invalid init statement"); RECOVER;}
1725 for_begin:
1726 for_header for_init
1728 /* We now declare the loop body. The loop is
1729 declared as a for loop. */
1730 tree body = build_loop_body (0, NULL_TREE, 0);
1731 $$ = build_new_loop (body);
1732 FOR_LOOP_P ($$) = 1;
1733 /* The loop is added to the current block the for
1734 statement is defined within */
1735 java_method_add_stmt (current_function_decl, $$);
1738 for_init: /* Can be empty */
1739 { $$ = empty_stmt_node; }
1740 | statement_expression_list
1742 /* Init statement recorded within the previously
1743 defined block scope */
1744 $$ = java_method_add_stmt (current_function_decl, $1);
1746 | local_variable_declaration
1748 /* Local variable are recorded within the previously
1749 defined block scope */
1750 $$ = NULL_TREE;
1752 | statement_expression_list error
1753 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1756 for_update: /* Can be empty */
1757 {$$ = empty_stmt_node;}
1758 | statement_expression_list
1759 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1762 statement_expression_list:
1763 statement_expression
1764 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1765 | statement_expression_list C_TK statement_expression
1766 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1767 | statement_expression_list C_TK error
1768 {yyerror ("Missing term"); RECOVER;}
1771 break_statement:
1772 BREAK_TK SC_TK
1773 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1774 | BREAK_TK identifier SC_TK
1775 { $$ = build_bc_statement ($1.location, 1, $2); }
1776 | BREAK_TK error
1777 {yyerror ("Missing term"); RECOVER;}
1778 | BREAK_TK identifier error
1779 {yyerror ("';' expected"); RECOVER;}
1782 continue_statement:
1783 CONTINUE_TK SC_TK
1784 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1785 | CONTINUE_TK identifier SC_TK
1786 { $$ = build_bc_statement ($1.location, 0, $2); }
1787 | CONTINUE_TK error
1788 {yyerror ("Missing term"); RECOVER;}
1789 | CONTINUE_TK identifier error
1790 {yyerror ("';' expected"); RECOVER;}
1793 return_statement:
1794 RETURN_TK SC_TK
1795 { $$ = build_return ($1.location, NULL_TREE); }
1796 | RETURN_TK expression SC_TK
1797 { $$ = build_return ($1.location, $2); }
1798 | RETURN_TK error
1799 {yyerror ("Missing term"); RECOVER;}
1800 | RETURN_TK expression error
1801 {yyerror ("';' expected"); RECOVER;}
1804 throw_statement:
1805 THROW_TK expression SC_TK
1807 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1808 EXPR_WFL_LINECOL ($$) = $1.location;
1810 | THROW_TK error
1811 {yyerror ("Missing term"); RECOVER;}
1812 | THROW_TK expression error
1813 {yyerror ("';' expected"); RECOVER;}
1816 synchronized_statement:
1817 synchronized OP_TK expression CP_TK block
1819 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1820 EXPR_WFL_LINECOL ($$) =
1821 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1823 | synchronized OP_TK expression CP_TK error
1824 {yyerror ("'{' expected"); RECOVER;}
1825 | synchronized error
1826 {yyerror ("'(' expected"); RECOVER;}
1827 | synchronized OP_TK error CP_TK
1828 {yyerror ("Missing term"); RECOVER;}
1829 | synchronized OP_TK error
1830 {yyerror ("Missing term"); RECOVER;}
1833 synchronized:
1834 modifiers
1836 check_modifiers (
1837 "Illegal modifier `%s'. Only `synchronized' was expected here",
1838 $1, ACC_SYNCHRONIZED);
1839 if ($1 != ACC_SYNCHRONIZED)
1840 MODIFIER_WFL (SYNCHRONIZED_TK) =
1841 build_wfl_node (NULL_TREE);
1845 try_statement:
1846 TRY_TK block catches
1847 { $$ = build_try_statement ($1.location, $2, $3); }
1848 | TRY_TK block finally
1849 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1850 | TRY_TK block catches finally
1851 { $$ = build_try_finally_statement
1852 ($1.location, build_try_statement ($1.location,
1853 $2, $3), $4);
1855 | TRY_TK error
1856 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1859 catches:
1860 catch_clause
1861 | catches catch_clause
1863 TREE_CHAIN ($2) = $1;
1864 $$ = $2;
1868 catch_clause:
1869 catch_clause_parameter block
1871 java_method_add_stmt (current_function_decl, $2);
1872 exit_block ();
1873 $$ = $1;
1876 catch_clause_parameter:
1877 CATCH_TK OP_TK formal_parameter CP_TK
1879 /* We add a block to define a scope for
1880 formal_parameter (CCBP). The formal parameter is
1881 declared initialized by the appropriate function
1882 call */
1883 tree ccpb = enter_block ();
1884 tree init = build_assignment (ASSIGN_TK, $2.location,
1885 TREE_PURPOSE ($3),
1886 soft_exceptioninfo_call_node);
1887 declare_local_variables (0, TREE_VALUE ($3),
1888 build_tree_list (TREE_PURPOSE ($3),
1889 init));
1890 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1891 EXPR_WFL_LINECOL ($$) = $1.location;
1893 | CATCH_TK error
1894 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1895 | CATCH_TK OP_TK error
1897 yyerror ("Missing term or ')' expected");
1898 RECOVER; $$ = NULL_TREE;
1900 | CATCH_TK OP_TK error CP_TK /* That's for () */
1901 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1904 finally:
1905 FINALLY_TK block
1906 { $$ = $2; }
1907 | FINALLY_TK error
1908 {yyerror ("'{' expected"); RECOVER; }
1911 /* 19.12 Production from 15: Expressions */
1912 primary:
1913 primary_no_new_array
1914 | array_creation_expression
1917 primary_no_new_array:
1918 literal
1919 | THIS_TK
1920 { $$ = build_this ($1.location); }
1921 | OP_TK expression CP_TK
1922 {$$ = $2;}
1923 | class_instance_creation_expression
1924 | field_access
1925 | method_invocation
1926 | array_access
1927 | type_literals
1928 /* Added, JDK1.1 inner classes. Documentation is wrong
1929 refering to a 'ClassName' (class_name) rule that doesn't
1930 exist. Used name: instead. */
1931 | name DOT_TK THIS_TK
1933 tree wfl = build_wfl_node (this_identifier_node);
1934 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1936 | OP_TK expression error
1937 {yyerror ("')' expected"); RECOVER;}
1938 | name DOT_TK error
1939 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1940 | primitive_type DOT_TK error
1941 {yyerror ("'class' expected" ); RECOVER;}
1942 | VOID_TK DOT_TK error
1943 {yyerror ("'class' expected" ); RECOVER;}
1946 type_literals:
1947 name DOT_TK CLASS_TK
1948 { $$ = build_incomplete_class_ref ($2.location, $1); }
1949 | array_type DOT_TK CLASS_TK
1950 { $$ = build_incomplete_class_ref ($2.location, $1); }
1951 | primitive_type DOT_TK CLASS_TK
1952 { $$ = build_class_ref ($1); }
1953 | VOID_TK DOT_TK CLASS_TK
1954 { $$ = build_class_ref (void_type_node); }
1957 class_instance_creation_expression:
1958 NEW_TK class_type OP_TK argument_list CP_TK
1959 { $$ = build_new_invocation ($2, $4); }
1960 | NEW_TK class_type OP_TK CP_TK
1961 { $$ = build_new_invocation ($2, NULL_TREE); }
1962 | anonymous_class_creation
1963 /* Added, JDK1.1 inner classes, modified to use name or
1964 primary instead of primary solely which couldn't work in
1965 all situations. */
1966 | something_dot_new identifier OP_TK CP_TK
1968 tree ctor = build_new_invocation ($2, NULL_TREE);
1969 $$ = make_qualified_primary ($1, ctor,
1970 EXPR_WFL_LINECOL ($1));
1972 | something_dot_new identifier OP_TK CP_TK class_body
1973 | something_dot_new identifier OP_TK argument_list CP_TK
1975 tree ctor = build_new_invocation ($2, $4);
1976 $$ = make_qualified_primary ($1, ctor,
1977 EXPR_WFL_LINECOL ($1));
1979 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1980 | NEW_TK error SC_TK
1981 {yyerror ("'(' expected"); DRECOVER(new_1);}
1982 | NEW_TK class_type error
1983 {yyerror ("'(' expected"); RECOVER;}
1984 | NEW_TK class_type OP_TK error
1985 {yyerror ("')' or term expected"); RECOVER;}
1986 | NEW_TK class_type OP_TK argument_list error
1987 {yyerror ("')' expected"); RECOVER;}
1988 | something_dot_new error
1989 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1990 | something_dot_new identifier error
1991 {yyerror ("'(' expected"); RECOVER;}
1994 /* Created after JDK1.1 rules originally added to
1995 class_instance_creation_expression, but modified to use
1996 'class_type' instead of 'TypeName' (type_name) which is mentionned
1997 in the documentation but doesn't exist. */
1999 anonymous_class_creation:
2000 NEW_TK class_type OP_TK argument_list CP_TK
2001 { create_anonymous_class ($1.location, $2); }
2002 class_body
2004 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2005 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2007 end_class_declaration (1);
2009 /* Now we can craft the new expression */
2010 $$ = build_new_invocation (id, $4);
2012 /* Note that we can't possibly be here if
2013 `class_type' is an interface (in which case the
2014 anonymous class extends Object and implements
2015 `class_type', hence its constructor can't have
2016 arguments.) */
2018 /* Otherwise, the innerclass must feature a
2019 constructor matching `argument_list'. Anonymous
2020 classes are a bit special: it's impossible to
2021 define constructor for them, hence constructors
2022 must be generated following the hints provided by
2023 the `new' expression. Whether a super constructor
2024 of that nature exists or not is to be verified
2025 later on in verify_constructor_super.
2027 It's during the expansion of a `new' statement
2028 refering to an anonymous class that a ctor will
2029 be generated for the anonymous class, with the
2030 right arguments. */
2033 | NEW_TK class_type OP_TK CP_TK
2034 { create_anonymous_class ($1.location, $2); }
2035 class_body
2037 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2038 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2040 end_class_declaration (1);
2042 /* Now we can craft the new expression. The
2043 statement doesn't need to be remember so that a
2044 constructor can be generated, since its signature
2045 is already known. */
2046 $$ = build_new_invocation (id, NULL_TREE);
2050 something_dot_new: /* Added, not part of the specs. */
2051 name DOT_TK NEW_TK
2052 { $$ = $1; }
2053 | primary DOT_TK NEW_TK
2054 { $$ = $1; }
2057 argument_list:
2058 expression
2060 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2061 ctxp->formal_parameter_number = 1;
2063 | argument_list C_TK expression
2065 ctxp->formal_parameter_number += 1;
2066 $$ = tree_cons (NULL_TREE, $3, $1);
2068 | argument_list C_TK error
2069 {yyerror ("Missing term"); RECOVER;}
2072 array_creation_expression:
2073 NEW_TK primitive_type dim_exprs
2074 { $$ = build_newarray_node ($2, $3, 0); }
2075 | NEW_TK class_or_interface_type dim_exprs
2076 { $$ = build_newarray_node ($2, $3, 0); }
2077 | NEW_TK primitive_type dim_exprs dims
2078 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2079 | NEW_TK class_or_interface_type dim_exprs dims
2080 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2081 /* Added, JDK1.1 anonymous array. Initial documentation rule
2082 modified */
2083 | NEW_TK class_or_interface_type dims array_initializer
2085 char *sig;
2086 int osb = pop_current_osb (ctxp);
2087 while (osb--)
2088 obstack_1grow (&temporary_obstack, '[');
2089 sig = obstack_finish (&temporary_obstack);
2090 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2091 $2, get_identifier (sig), $4);
2093 | NEW_TK primitive_type dims array_initializer
2095 int osb = pop_current_osb (ctxp);
2096 tree type = $2;
2097 while (osb--)
2098 type = build_java_array_type (type, -1);
2099 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2100 build_pointer_type (type), NULL_TREE, $4);
2102 | NEW_TK error CSB_TK
2103 {yyerror ("'[' expected"); DRECOVER ("]");}
2104 | NEW_TK error OSB_TK
2105 {yyerror ("']' expected"); RECOVER;}
2108 dim_exprs:
2109 dim_expr
2110 { $$ = build_tree_list (NULL_TREE, $1); }
2111 | dim_exprs dim_expr
2112 { $$ = tree_cons (NULL_TREE, $2, $$); }
2115 dim_expr:
2116 OSB_TK expression CSB_TK
2118 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2120 $2 = build_wfl_node ($2);
2121 TREE_TYPE ($2) = NULL_TREE;
2123 EXPR_WFL_LINECOL ($2) = $1.location;
2124 $$ = $2;
2126 | OSB_TK expression error
2127 {yyerror ("']' expected"); RECOVER;}
2128 | OSB_TK error
2130 yyerror ("Missing term");
2131 yyerror ("']' expected");
2132 RECOVER;
2136 dims:
2137 OSB_TK CSB_TK
2139 int allocate = 0;
2140 /* If not initialized, allocate memory for the osb
2141 numbers stack */
2142 if (!ctxp->osb_limit)
2144 allocate = ctxp->osb_limit = 32;
2145 ctxp->osb_depth = -1;
2147 /* If capacity overflown, reallocate a bigger chunk */
2148 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2149 allocate = ctxp->osb_limit << 1;
2151 if (allocate)
2153 allocate *= sizeof (int);
2154 if (ctxp->osb_number)
2155 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2156 allocate);
2157 else
2158 ctxp->osb_number = (int *)xmalloc (allocate);
2160 ctxp->osb_depth++;
2161 CURRENT_OSB (ctxp) = 1;
2163 | dims OSB_TK CSB_TK
2164 { CURRENT_OSB (ctxp)++; }
2165 | dims OSB_TK error
2166 { yyerror ("']' expected"); RECOVER;}
2169 field_access:
2170 primary DOT_TK identifier
2171 { $$ = make_qualified_primary ($1, $3, $2.location); }
2172 /* FIXME - REWRITE TO:
2173 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2174 | SUPER_TK DOT_TK identifier
2176 tree super_wfl = build_wfl_node (super_identifier_node);
2177 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2178 $$ = make_qualified_name (super_wfl, $3, $2.location);
2180 | SUPER_TK error
2181 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2184 method_invocation:
2185 name OP_TK CP_TK
2186 { $$ = build_method_invocation ($1, NULL_TREE); }
2187 | name OP_TK argument_list CP_TK
2188 { $$ = build_method_invocation ($1, $3); }
2189 | primary DOT_TK identifier OP_TK CP_TK
2191 if (TREE_CODE ($1) == THIS_EXPR)
2192 $$ = build_this_super_qualified_invocation
2193 (1, $3, NULL_TREE, 0, $2.location);
2194 else
2196 tree invok = build_method_invocation ($3, NULL_TREE);
2197 $$ = make_qualified_primary ($1, invok, $2.location);
2200 | primary DOT_TK identifier OP_TK argument_list CP_TK
2202 if (TREE_CODE ($1) == THIS_EXPR)
2203 $$ = build_this_super_qualified_invocation
2204 (1, $3, $5, 0, $2.location);
2205 else
2207 tree invok = build_method_invocation ($3, $5);
2208 $$ = make_qualified_primary ($1, invok, $2.location);
2211 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2213 $$ = build_this_super_qualified_invocation
2214 (0, $3, NULL_TREE, $1.location, $2.location);
2216 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2218 $$ = build_this_super_qualified_invocation
2219 (0, $3, $5, $1.location, $2.location);
2221 /* Screws up thing. I let it here until I'm convinced it can
2222 be removed. FIXME
2223 | primary DOT_TK error
2224 {yyerror ("'(' expected"); DRECOVER(bad);} */
2225 | SUPER_TK DOT_TK error CP_TK
2226 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2227 | SUPER_TK DOT_TK error DOT_TK
2228 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2231 array_access:
2232 name OSB_TK expression CSB_TK
2233 { $$ = build_array_ref ($2.location, $1, $3); }
2234 | primary_no_new_array OSB_TK expression CSB_TK
2235 { $$ = build_array_ref ($2.location, $1, $3); }
2236 | name OSB_TK error
2238 yyerror ("Missing term and ']' expected");
2239 DRECOVER(array_access);
2241 | name OSB_TK expression error
2243 yyerror ("']' expected");
2244 DRECOVER(array_access);
2246 | primary_no_new_array OSB_TK error
2248 yyerror ("Missing term and ']' expected");
2249 DRECOVER(array_access);
2251 | primary_no_new_array OSB_TK expression error
2253 yyerror ("']' expected");
2254 DRECOVER(array_access);
2258 postfix_expression:
2259 primary
2260 | name
2261 | post_increment_expression
2262 | post_decrement_expression
2265 post_increment_expression:
2266 postfix_expression INCR_TK
2267 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2270 post_decrement_expression:
2271 postfix_expression DECR_TK
2272 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2275 unary_expression:
2276 pre_increment_expression
2277 | pre_decrement_expression
2278 | PLUS_TK unary_expression
2279 {$$ = build_unaryop ($1.token, $1.location, $2); }
2280 | MINUS_TK unary_expression
2281 {$$ = build_unaryop ($1.token, $1.location, $2); }
2282 | unary_expression_not_plus_minus
2283 | PLUS_TK error
2284 {yyerror ("Missing term"); RECOVER}
2285 | MINUS_TK error
2286 {yyerror ("Missing term"); RECOVER}
2289 pre_increment_expression:
2290 INCR_TK unary_expression
2291 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2292 | INCR_TK error
2293 {yyerror ("Missing term"); RECOVER}
2296 pre_decrement_expression:
2297 DECR_TK unary_expression
2298 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2299 | DECR_TK error
2300 {yyerror ("Missing term"); RECOVER}
2303 unary_expression_not_plus_minus:
2304 postfix_expression
2305 | NOT_TK unary_expression
2306 {$$ = build_unaryop ($1.token, $1.location, $2); }
2307 | NEG_TK unary_expression
2308 {$$ = build_unaryop ($1.token, $1.location, $2); }
2309 | cast_expression
2310 | NOT_TK error
2311 {yyerror ("Missing term"); RECOVER}
2312 | NEG_TK error
2313 {yyerror ("Missing term"); RECOVER}
2316 cast_expression: /* Error handling here is potentially weak */
2317 OP_TK primitive_type dims CP_TK unary_expression
2319 tree type = $2;
2320 int osb = pop_current_osb (ctxp);
2321 while (osb--)
2322 type = build_java_array_type (type, -1);
2323 $$ = build_cast ($1.location, type, $5);
2325 | OP_TK primitive_type CP_TK unary_expression
2326 { $$ = build_cast ($1.location, $2, $4); }
2327 | OP_TK expression CP_TK unary_expression_not_plus_minus
2328 { $$ = build_cast ($1.location, $2, $4); }
2329 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2331 const char *ptr;
2332 int osb = pop_current_osb (ctxp);
2333 while (osb--)
2334 obstack_1grow (&temporary_obstack, '[');
2335 obstack_grow0 (&temporary_obstack,
2336 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2337 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2338 ptr = obstack_finish (&temporary_obstack);
2339 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2340 $$ = build_cast ($1.location, $2, $5);
2342 | OP_TK primitive_type OSB_TK error
2343 {yyerror ("']' expected, invalid type expression");}
2344 | OP_TK error
2346 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2347 RECOVER;
2349 | OP_TK primitive_type dims CP_TK error
2350 {yyerror ("Missing term"); RECOVER;}
2351 | OP_TK primitive_type CP_TK error
2352 {yyerror ("Missing term"); RECOVER;}
2353 | OP_TK name dims CP_TK error
2354 {yyerror ("Missing term"); RECOVER;}
2357 multiplicative_expression:
2358 unary_expression
2359 | multiplicative_expression MULT_TK unary_expression
2361 $$ = build_binop (BINOP_LOOKUP ($2.token),
2362 $2.location, $1, $3);
2364 | multiplicative_expression DIV_TK unary_expression
2366 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2367 $1, $3);
2369 | multiplicative_expression REM_TK unary_expression
2371 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2372 $1, $3);
2374 | multiplicative_expression MULT_TK error
2375 {yyerror ("Missing term"); RECOVER;}
2376 | multiplicative_expression DIV_TK error
2377 {yyerror ("Missing term"); RECOVER;}
2378 | multiplicative_expression REM_TK error
2379 {yyerror ("Missing term"); RECOVER;}
2382 additive_expression:
2383 multiplicative_expression
2384 | additive_expression PLUS_TK multiplicative_expression
2386 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2387 $1, $3);
2389 | additive_expression MINUS_TK multiplicative_expression
2391 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2392 $1, $3);
2394 | additive_expression PLUS_TK error
2395 {yyerror ("Missing term"); RECOVER;}
2396 | additive_expression MINUS_TK error
2397 {yyerror ("Missing term"); RECOVER;}
2400 shift_expression:
2401 additive_expression
2402 | shift_expression LS_TK additive_expression
2404 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2405 $1, $3);
2407 | shift_expression SRS_TK additive_expression
2409 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2410 $1, $3);
2412 | shift_expression ZRS_TK additive_expression
2414 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2415 $1, $3);
2417 | shift_expression LS_TK error
2418 {yyerror ("Missing term"); RECOVER;}
2419 | shift_expression SRS_TK error
2420 {yyerror ("Missing term"); RECOVER;}
2421 | shift_expression ZRS_TK error
2422 {yyerror ("Missing term"); RECOVER;}
2425 relational_expression:
2426 shift_expression
2427 | relational_expression LT_TK shift_expression
2429 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2430 $1, $3);
2432 | relational_expression GT_TK shift_expression
2434 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2435 $1, $3);
2437 | relational_expression LTE_TK shift_expression
2439 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2440 $1, $3);
2442 | relational_expression GTE_TK shift_expression
2444 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2445 $1, $3);
2447 | relational_expression INSTANCEOF_TK reference_type
2448 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2449 | relational_expression LT_TK error
2450 {yyerror ("Missing term"); RECOVER;}
2451 | relational_expression GT_TK error
2452 {yyerror ("Missing term"); RECOVER;}
2453 | relational_expression LTE_TK error
2454 {yyerror ("Missing term"); RECOVER;}
2455 | relational_expression GTE_TK error
2456 {yyerror ("Missing term"); RECOVER;}
2457 | relational_expression INSTANCEOF_TK error
2458 {yyerror ("Invalid reference type"); RECOVER;}
2461 equality_expression:
2462 relational_expression
2463 | equality_expression EQ_TK relational_expression
2465 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2466 $1, $3);
2468 | equality_expression NEQ_TK relational_expression
2470 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2471 $1, $3);
2473 | equality_expression EQ_TK error
2474 {yyerror ("Missing term"); RECOVER;}
2475 | equality_expression NEQ_TK error
2476 {yyerror ("Missing term"); RECOVER;}
2479 and_expression:
2480 equality_expression
2481 | and_expression AND_TK equality_expression
2483 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2484 $1, $3);
2486 | and_expression AND_TK error
2487 {yyerror ("Missing term"); RECOVER;}
2490 exclusive_or_expression:
2491 and_expression
2492 | exclusive_or_expression XOR_TK and_expression
2494 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2495 $1, $3);
2497 | exclusive_or_expression XOR_TK error
2498 {yyerror ("Missing term"); RECOVER;}
2501 inclusive_or_expression:
2502 exclusive_or_expression
2503 | inclusive_or_expression OR_TK exclusive_or_expression
2505 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2506 $1, $3);
2508 | inclusive_or_expression OR_TK error
2509 {yyerror ("Missing term"); RECOVER;}
2512 conditional_and_expression:
2513 inclusive_or_expression
2514 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2516 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2517 $1, $3);
2519 | conditional_and_expression BOOL_AND_TK error
2520 {yyerror ("Missing term"); RECOVER;}
2523 conditional_or_expression:
2524 conditional_and_expression
2525 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2527 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2528 $1, $3);
2530 | conditional_or_expression BOOL_OR_TK error
2531 {yyerror ("Missing term"); RECOVER;}
2534 conditional_expression: /* Error handling here is weak */
2535 conditional_or_expression
2536 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2538 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2539 EXPR_WFL_LINECOL ($$) = $2.location;
2541 | conditional_or_expression REL_QM_TK REL_CL_TK error
2543 YYERROR_NOW;
2544 yyerror ("Missing term");
2545 DRECOVER (1);
2547 | conditional_or_expression REL_QM_TK error
2548 {yyerror ("Missing term"); DRECOVER (2);}
2549 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2550 {yyerror ("Missing term"); DRECOVER (3);}
2553 assignment_expression:
2554 conditional_expression
2555 | assignment
2558 assignment:
2559 left_hand_side assignment_operator assignment_expression
2560 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2561 | left_hand_side assignment_operator error
2563 YYNOT_TWICE yyerror ("Missing term");
2564 DRECOVER (assign);
2568 left_hand_side:
2569 name
2570 | field_access
2571 | array_access
2574 assignment_operator:
2575 ASSIGN_ANY_TK
2576 | ASSIGN_TK
2579 expression:
2580 assignment_expression
2583 constant_expression:
2584 expression
2589 /* Helper function to retrieve an OSB count. Should be used when the
2590 `dims:' rule is being used. */
2592 static int
2593 pop_current_osb (ctxp)
2594 struct parser_ctxt *ctxp;
2596 int to_return;
2598 if (ctxp->osb_depth < 0)
2599 fatal ("osb stack underflow");
2601 to_return = CURRENT_OSB (ctxp);
2602 ctxp->osb_depth--;
2604 return to_return;
2609 /* This section of the code deal with save/restoring parser contexts.
2610 Add mode documentation here. FIXME */
2612 /* Helper function. Create a new parser context. With
2613 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2614 context is copied, otherwise, the new context is zeroed. The newly
2615 created context becomes the current one. */
2617 static void
2618 create_new_parser_context (copy_from_previous)
2619 int copy_from_previous;
2621 struct parser_ctxt *new;
2623 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2624 if (copy_from_previous)
2626 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2627 new->saved_data_ctx = 1;
2629 else
2630 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2632 new->next = ctxp;
2633 ctxp = new;
2636 /* Create a new parser context and make it the current one. */
2638 void
2639 java_push_parser_context ()
2641 create_new_parser_context (0);
2642 if (ctxp->next)
2644 ctxp->incomplete_class = ctxp->next->incomplete_class;
2645 ctxp->gclass_list = ctxp->next->gclass_list;
2649 void
2650 java_pop_parser_context (generate)
2651 int generate;
2653 tree current;
2654 struct parser_ctxt *toFree, *next;
2656 if (!ctxp)
2657 return;
2659 toFree = ctxp;
2660 next = ctxp->next;
2661 if (next)
2663 next->incomplete_class = ctxp->incomplete_class;
2664 next->gclass_list = ctxp->gclass_list;
2665 lineno = ctxp->lineno;
2666 current_class = ctxp->class_type;
2669 /* If the old and new lexers differ, then free the old one. */
2670 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2671 java_destroy_lexer (ctxp->lexer);
2673 /* Set the single import class file flag to 0 for the current list
2674 of imported things */
2675 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2676 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2678 /* And restore those of the previous context */
2679 if ((ctxp = next)) /* Assignment is really meant here */
2680 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2681 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2683 /* If we pushed a context to parse a class intended to be generated,
2684 we keep it so we can remember the class. What we could actually
2685 do is to just update a list of class names. */
2686 if (generate)
2688 toFree->next = ctxp_for_generation;
2689 ctxp_for_generation = toFree;
2691 else
2692 free (toFree);
2695 /* Create a parser context for the use of saving some global
2696 variables. */
2698 void
2699 java_parser_context_save_global ()
2701 if (!ctxp)
2703 java_push_parser_context ();
2704 ctxp->saved_data_ctx = 1;
2707 /* If this context already stores data, create a new one suitable
2708 for data storage. */
2709 else if (ctxp->saved_data)
2710 create_new_parser_context (1);
2712 ctxp->lineno = lineno;
2713 ctxp->class_type = current_class;
2714 ctxp->filename = input_filename;
2715 ctxp->function_decl = current_function_decl;
2716 ctxp->saved_data = 1;
2719 /* Restore some global variables from the previous context. Make the
2720 previous context the current one. */
2722 void
2723 java_parser_context_restore_global ()
2725 lineno = ctxp->lineno;
2726 current_class = ctxp->class_type;
2727 input_filename = ctxp->filename;
2728 if (wfl_operator)
2730 tree s;
2731 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2732 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2734 current_function_decl = ctxp->function_decl;
2735 ctxp->saved_data = 0;
2736 if (ctxp->saved_data_ctx)
2737 java_pop_parser_context (0);
2740 /* Suspend vital data for the current class/function being parsed so
2741 that an other class can be parsed. Used to let local/anonymous
2742 classes be parsed. */
2744 static void
2745 java_parser_context_suspend ()
2747 /* This makes debugging through java_debug_context easier */
2748 static const char *name = "<inner buffer context>";
2750 /* Duplicate the previous context, use it to save the globals we're
2751 interested in */
2752 create_new_parser_context (1);
2753 ctxp->function_decl = current_function_decl;
2754 ctxp->class_type = current_class;
2756 /* Then create a new context which inherits all data from the
2757 previous one. This will be the new current context */
2758 create_new_parser_context (1);
2760 /* Help debugging */
2761 ctxp->next->filename = name;
2764 /* Resume vital data for the current class/function being parsed so
2765 that an other class can be parsed. Used to let local/anonymous
2766 classes be parsed. The trick is the data storing file position
2767 informations must be restored to their current value, so parsing
2768 can resume as if no context was ever saved. */
2770 static void
2771 java_parser_context_resume ()
2773 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2774 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2775 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2777 /* We need to inherit the list of classes to complete/generate */
2778 restored->incomplete_class = old->incomplete_class;
2779 restored->gclass_list = old->gclass_list;
2780 restored->classd_list = old->classd_list;
2781 restored->class_list = old->class_list;
2783 /* Restore the current class and function from the saver */
2784 current_class = saver->class_type;
2785 current_function_decl = saver->function_decl;
2787 /* Retrive the restored context */
2788 ctxp = restored;
2790 /* Re-installed the data for the parsing to carry on */
2791 bcopy (&old->marker_begining, &ctxp->marker_begining,
2792 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2794 /* Buffer context can now be discarded */
2795 free (saver);
2796 free (old);
2799 /* Add a new anchor node to which all statement(s) initializing static
2800 and non static initialized upon declaration field(s) will be
2801 linked. */
2803 static void
2804 java_parser_context_push_initialized_field ()
2806 tree node;
2808 node = build_tree_list (NULL_TREE, NULL_TREE);
2809 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2810 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2812 node = build_tree_list (NULL_TREE, NULL_TREE);
2813 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2814 CPC_INITIALIZER_LIST (ctxp) = node;
2816 node = build_tree_list (NULL_TREE, NULL_TREE);
2817 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2818 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2821 /* Pop the lists of initialized field. If this lists aren't empty,
2822 remember them so we can use it to create and populate the finit$
2823 or <clinit> functions. */
2825 static void
2826 java_parser_context_pop_initialized_field ()
2828 tree stmts;
2829 tree class_type = TREE_TYPE (GET_CPC ());
2831 if (CPC_INITIALIZER_LIST (ctxp))
2833 stmts = CPC_INITIALIZER_STMT (ctxp);
2834 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2835 if (stmts && !java_error_count)
2836 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2839 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2841 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2842 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2843 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2844 /* Keep initialization in order to enforce 8.5 */
2845 if (stmts && !java_error_count)
2846 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2849 /* JDK 1.1 instance initializers */
2850 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2852 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2853 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2854 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2855 if (stmts && !java_error_count)
2856 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2860 static tree
2861 reorder_static_initialized (list)
2862 tree list;
2864 /* We have to keep things in order. The alias initializer have to
2865 come first, then the initialized regular field, in reverse to
2866 keep them in lexical order. */
2867 tree marker, previous = NULL_TREE;
2868 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2869 if (TREE_CODE (marker) == TREE_LIST
2870 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2871 break;
2873 /* No static initialized, the list is fine as is */
2874 if (!previous)
2875 list = TREE_CHAIN (marker);
2877 /* No marker? reverse the whole list */
2878 else if (!marker)
2879 list = nreverse (list);
2881 /* Otherwise, reverse what's after the marker and the new reordered
2882 sublist will replace the marker. */
2883 else
2885 TREE_CHAIN (previous) = NULL_TREE;
2886 list = nreverse (list);
2887 list = chainon (TREE_CHAIN (marker), list);
2889 return list;
2892 /* Helper functions to dump the parser context stack. */
2894 #define TAB_CONTEXT(C) \
2895 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2897 static void
2898 java_debug_context_do (tab)
2899 int tab;
2901 struct parser_ctxt *copy = ctxp;
2902 while (copy)
2904 TAB_CONTEXT (tab);
2905 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2906 TAB_CONTEXT (tab);
2907 fprintf (stderr, "filename: %s\n", copy->filename);
2908 TAB_CONTEXT (tab);
2909 fprintf (stderr, "lineno: %d\n", copy->lineno);
2910 TAB_CONTEXT (tab);
2911 fprintf (stderr, "package: %s\n",
2912 (copy->package ?
2913 IDENTIFIER_POINTER (copy->package) : "<none>"));
2914 TAB_CONTEXT (tab);
2915 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2916 TAB_CONTEXT (tab);
2917 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2918 copy = copy->next;
2919 tab += 2;
2923 /* Dump the stacked up parser contexts. Intended to be called from a
2924 debugger. */
2926 void
2927 java_debug_context ()
2929 java_debug_context_do (0);
2934 /* Flag for the error report routine to issue the error the first time
2935 it's called (overriding the default behavior which is to drop the
2936 first invocation and honor the second one, taking advantage of a
2937 richer context. */
2938 static int force_error = 0;
2940 /* Reporting an constructor invocation error. */
2941 static void
2942 parse_ctor_invocation_error ()
2944 if (DECL_CONSTRUCTOR_P (current_function_decl))
2945 yyerror ("Constructor invocation must be first thing in a constructor");
2946 else
2947 yyerror ("Only constructors can invoke constructors");
2950 /* Reporting JDK1.1 features not implemented. */
2952 static tree
2953 parse_jdk1_1_error (msg)
2954 const char *msg;
2956 sorry (": `%s' JDK1.1(TM) feature", msg);
2957 java_error_count++;
2958 return empty_stmt_node;
2961 static int do_warning = 0;
2963 void
2964 yyerror (msg)
2965 const char *msg;
2967 static java_lc elc;
2968 static int prev_lineno;
2969 static const char *prev_msg;
2971 int save_lineno;
2972 char *remainder, *code_from_source;
2974 if (!force_error && prev_lineno == lineno)
2975 return;
2977 /* Save current error location but report latter, when the context is
2978 richer. */
2979 if (ctxp->java_error_flag == 0)
2981 ctxp->java_error_flag = 1;
2982 elc = ctxp->elc;
2983 /* Do something to use the previous line if we're reaching the
2984 end of the file... */
2985 #ifdef VERBOSE_SKELETON
2986 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2987 #endif
2988 return;
2991 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2992 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2993 return;
2995 ctxp->java_error_flag = 0;
2996 if (do_warning)
2997 java_warning_count++;
2998 else
2999 java_error_count++;
3001 if (elc.col == 0 && msg && msg[1] == ';')
3003 elc.col = ctxp->p_line->char_col-1;
3004 elc.line = ctxp->p_line->lineno;
3007 save_lineno = lineno;
3008 prev_lineno = lineno = elc.line;
3009 prev_msg = msg;
3011 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3012 obstack_grow0 (&temporary_obstack,
3013 code_from_source, strlen (code_from_source));
3014 remainder = obstack_finish (&temporary_obstack);
3015 if (do_warning)
3016 warning ("%s.\n%s", msg, remainder);
3017 else
3018 error ("%s.\n%s", msg, remainder);
3020 /* This allow us to cheaply avoid an extra 'Invalid expression
3021 statement' error report when errors have been already reported on
3022 the same line. This occurs when we report an error but don't have
3023 a synchronization point other than ';', which
3024 expression_statement is the only one to take care of. */
3025 ctxp->prevent_ese = lineno = save_lineno;
3028 static void
3029 issue_warning_error_from_context (cl, msg, ap)
3030 tree cl;
3031 const char *msg;
3032 va_list ap;
3034 const char *saved, *saved_input_filename;
3035 char buffer [4096];
3036 vsprintf (buffer, msg, ap);
3037 force_error = 1;
3039 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3040 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3041 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3043 /* We have a CL, that's a good reason for using it if it contains data */
3044 saved = ctxp->filename;
3045 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3046 ctxp->filename = EXPR_WFL_FILENAME (cl);
3047 saved_input_filename = input_filename;
3048 input_filename = ctxp->filename;
3049 java_error (NULL);
3050 java_error (buffer);
3051 ctxp->filename = saved;
3052 input_filename = saved_input_filename;
3053 force_error = 0;
3056 /* Issue an error message at a current source line CL */
3058 void
3059 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3061 #ifndef ANSI_PROTOTYPES
3062 tree cl;
3063 const char *msg;
3064 #endif
3065 va_list ap;
3067 VA_START (ap, msg);
3068 #ifndef ANSI_PROTOTYPES
3069 cl = va_arg (ap, tree);
3070 msg = va_arg (ap, const char *);
3071 #endif
3072 issue_warning_error_from_context (cl, msg, ap);
3073 va_end (ap);
3076 /* Issue a warning at a current source line CL */
3078 static void
3079 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3081 #ifndef ANSI_PROTOTYPES
3082 tree cl;
3083 const char *msg;
3084 #endif
3085 va_list ap;
3087 VA_START (ap, msg);
3088 #ifndef ANSI_PROTOTYPES
3089 cl = va_arg (ap, tree);
3090 msg = va_arg (ap, const char *);
3091 #endif
3093 force_error = do_warning = 1;
3094 issue_warning_error_from_context (cl, msg, ap);
3095 do_warning = force_error = 0;
3096 va_end (ap);
3099 static tree
3100 find_expr_with_wfl (node)
3101 tree node;
3103 while (node)
3105 char code;
3106 tree to_return;
3108 switch (TREE_CODE (node))
3110 case BLOCK:
3111 node = BLOCK_EXPR_BODY (node);
3112 continue;
3114 case COMPOUND_EXPR:
3115 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3116 if (to_return)
3117 return to_return;
3118 node = TREE_OPERAND (node, 1);
3119 continue;
3121 case LOOP_EXPR:
3122 node = TREE_OPERAND (node, 0);
3123 continue;
3125 case LABELED_BLOCK_EXPR:
3126 node = TREE_OPERAND (node, 1);
3127 continue;
3129 default:
3130 code = TREE_CODE_CLASS (TREE_CODE (node));
3131 if (((code == '1') || (code == '2') || (code == 'e'))
3132 && EXPR_WFL_LINECOL (node))
3133 return node;
3134 return NULL_TREE;
3137 return NULL_TREE;
3140 /* Issue a missing return statement error. Uses METHOD to figure the
3141 last line of the method the error occurs in. */
3143 static void
3144 missing_return_error (method)
3145 tree method;
3147 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3148 parse_error_context (wfl_operator, "Missing return statement");
3151 /* Issue an unreachable statement error. From NODE, find the next
3152 statement to report appropriately. */
3153 static void
3154 unreachable_stmt_error (node)
3155 tree node;
3157 /* Browse node to find the next expression node that has a WFL. Use
3158 the location to report the error */
3159 if (TREE_CODE (node) == COMPOUND_EXPR)
3160 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3161 else
3162 node = find_expr_with_wfl (node);
3164 if (node)
3166 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3167 parse_error_context (wfl_operator, "Unreachable statement");
3169 else
3170 fatal ("Can't get valid statement - unreachable_stmt_error");
3174 java_report_errors ()
3176 if (java_error_count)
3177 fprintf (stderr, "%d error%s",
3178 java_error_count, (java_error_count == 1 ? "" : "s"));
3179 if (java_warning_count)
3180 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3181 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3182 if (java_error_count || java_warning_count)
3183 putc ('\n', stderr);
3184 return java_error_count;
3187 static char *
3188 java_accstring_lookup (flags)
3189 int flags;
3191 static char buffer [80];
3192 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3194 /* Access modifier looked-up first for easier report on forbidden
3195 access. */
3196 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3197 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3198 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3199 if (flags & ACC_STATIC) COPY_RETURN ("static");
3200 if (flags & ACC_FINAL) COPY_RETURN ("final");
3201 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3202 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3203 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3204 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3205 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3206 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3208 buffer [0] = '\0';
3209 return buffer;
3210 #undef COPY_RETURN
3213 /* Issuing error messages upon redefinition of classes, interfaces or
3214 variables. */
3216 static void
3217 classitf_redefinition_error (context, id, decl, cl)
3218 const char *context;
3219 tree id, decl, cl;
3221 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3222 context, IDENTIFIER_POINTER (id),
3223 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3224 /* Here we should point out where its redefined. It's a unicode. FIXME */
3227 static void
3228 variable_redefinition_error (context, name, type, line)
3229 tree context, name, type;
3230 int line;
3232 const char *type_name;
3234 /* Figure a proper name for type. We might haven't resolved it */
3235 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3236 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3237 else
3238 type_name = lang_printable_name (type, 0);
3240 parse_error_context (context,
3241 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3242 IDENTIFIER_POINTER (name),
3243 type_name, IDENTIFIER_POINTER (name), line);
3246 static tree
3247 build_array_from_name (type, type_wfl, name, ret_name)
3248 tree type, type_wfl, name, *ret_name;
3250 int more_dims = 0;
3251 const char *string;
3253 /* Eventually get more dims */
3254 string = IDENTIFIER_POINTER (name);
3255 while (string [more_dims] == '[')
3256 more_dims++;
3258 /* If we have, then craft a new type for this variable */
3259 if (more_dims)
3261 name = get_identifier (&string [more_dims]);
3263 /* If we have a pointer, use its type */
3264 if (TREE_CODE (type) == POINTER_TYPE)
3265 type = TREE_TYPE (type);
3267 /* Building the first dimension of a primitive type uses this
3268 function */
3269 if (JPRIMITIVE_TYPE_P (type))
3271 type = build_java_array_type (type, -1);
3272 CLASS_LOADED_P (type) = 1;
3273 more_dims--;
3275 /* Otherwise, if we have a WFL for this type, use it (the type
3276 is already an array on an unresolved type, and we just keep
3277 on adding dimensions) */
3278 else if (type_wfl)
3279 type = type_wfl;
3281 /* Add all the dimensions */
3282 while (more_dims--)
3283 type = build_unresolved_array_type (type);
3285 /* The type may have been incomplete in the first place */
3286 if (type_wfl)
3287 type = obtain_incomplete_type (type);
3290 if (ret_name)
3291 *ret_name = name;
3292 return type;
3295 /* Build something that the type identifier resolver will identify as
3296 being an array to an unresolved type. TYPE_WFL is a WFL on a
3297 identifier. */
3299 static tree
3300 build_unresolved_array_type (type_or_wfl)
3301 tree type_or_wfl;
3303 const char *ptr;
3305 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3306 just create a array type */
3307 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3309 tree type = build_java_array_type (type_or_wfl, -1);
3310 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3311 return type;
3314 obstack_1grow (&temporary_obstack, '[');
3315 obstack_grow0 (&temporary_obstack,
3316 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3317 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3318 ptr = obstack_finish (&temporary_obstack);
3319 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3320 return type_or_wfl;
3323 static void
3324 parser_add_interface (class_decl, interface_decl, wfl)
3325 tree class_decl, interface_decl, wfl;
3327 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3328 parse_error_context (wfl, "Interface `%s' repeated",
3329 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3332 /* Bulk of common class/interface checks. Return 1 if an error was
3333 encountered. TAG is 0 for a class, 1 for an interface. */
3335 static int
3336 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3337 int is_interface, flags;
3338 tree raw_name, qualified_name, decl, cl;
3340 tree node;
3341 int sca = 0; /* Static class allowed */
3342 int icaf = 0; /* Inner class allowed flags */
3343 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3345 if (!quiet_flag)
3346 fprintf (stderr, " %s%s %s",
3347 (CPC_INNER_P () ? "inner" : ""),
3348 (is_interface ? "interface" : "class"),
3349 IDENTIFIER_POINTER (qualified_name));
3351 /* Scope of an interface/class type name:
3352 - Can't be imported by a single type import
3353 - Can't already exists in the package */
3354 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3355 && (node = find_name_in_single_imports (raw_name))
3356 && !CPC_INNER_P ())
3358 parse_error_context
3359 (cl, "%s name `%s' clashes with imported type `%s'",
3360 (is_interface ? "Interface" : "Class"),
3361 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3362 return 1;
3364 if (decl && CLASS_COMPLETE_P (decl))
3366 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3367 qualified_name, decl, cl);
3368 return 1;
3371 if (check_inner_class_redefinition (raw_name, cl))
3372 return 1;
3374 /* If public, file name should match class/interface name, except
3375 when dealing with an inner class */
3376 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3378 const char *f;
3380 /* Contains OS dependent assumption on path separator. FIXME */
3381 for (f = &input_filename [strlen (input_filename)];
3382 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3383 f--)
3385 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3386 f++;
3387 if (strncmp (IDENTIFIER_POINTER (raw_name),
3388 f , IDENTIFIER_LENGTH (raw_name)) ||
3389 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3390 parse_error_context
3391 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3392 (is_interface ? "interface" : "class"),
3393 IDENTIFIER_POINTER (qualified_name),
3394 IDENTIFIER_POINTER (raw_name));
3397 /* Static classes can be declared only in top level classes. Note:
3398 once static, a inner class is a top level class. */
3399 if (flags & ACC_STATIC)
3401 /* Catch the specific error of declaring an class inner class
3402 with no toplevel enclosing class. Prevent check_modifiers from
3403 complaining a second time */
3404 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3406 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3407 IDENTIFIER_POINTER (qualified_name));
3408 sca = ACC_STATIC;
3410 /* Else, in the context of a top-level class declaration, let
3411 `check_modifiers' do its job, otherwise, give it a go */
3412 else
3413 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3416 /* Inner classes can be declared private or protected
3417 within their enclosing classes. */
3418 if (CPC_INNER_P ())
3420 /* A class which is local to a block can't be public, private,
3421 protected or static. But it is created final, so allow this
3422 one. */
3423 if (current_function_decl)
3424 icaf = sca = uaaf = ACC_FINAL;
3425 else
3427 check_modifiers_consistency (flags);
3428 icaf = ACC_PRIVATE|ACC_PROTECTED;
3432 if (is_interface)
3434 if (CPC_INNER_P ())
3435 uaaf = INTERFACE_INNER_MODIFIERS;
3436 else
3437 uaaf = INTERFACE_MODIFIERS;
3439 check_modifiers ("Illegal modifier `%s' for interface declaration",
3440 flags, uaaf);
3442 else
3443 check_modifiers ((current_function_decl ?
3444 "Illegal modifier `%s' for local class declaration" :
3445 "Illegal modifier `%s' for class declaration"),
3446 flags, uaaf|sca|icaf);
3447 return 0;
3450 static void
3451 make_nested_class_name (cpc_list)
3452 tree cpc_list;
3454 tree name;
3456 if (!cpc_list)
3457 return;
3458 else
3459 make_nested_class_name (TREE_CHAIN (cpc_list));
3461 /* Pick the qualified name when dealing with the first upmost
3462 enclosing class */
3463 name = (TREE_CHAIN (cpc_list) ?
3464 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3465 obstack_grow (&temporary_obstack,
3466 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3467 /* Why is NO_DOLLAR_IN_LABEL defined? */
3468 #if 0
3469 #ifdef NO_DOLLAR_IN_LABEL
3470 fatal ("make_nested_class_name: Can't use '$' as a separator "
3471 "for inner classes");
3472 #endif
3473 #endif
3474 obstack_1grow (&temporary_obstack, '$');
3477 /* Can't redefine a class already defined in an earlier scope. */
3479 static int
3480 check_inner_class_redefinition (raw_name, cl)
3481 tree raw_name, cl;
3483 tree scope_list;
3485 for (scope_list = GET_CPC_LIST (); scope_list;
3486 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3487 if (raw_name == GET_CPC_UN_NODE (scope_list))
3489 parse_error_context
3490 (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",
3491 IDENTIFIER_POINTER (raw_name));
3492 return 1;
3494 return 0;
3497 static tree
3498 find_as_inner_class (enclosing, name, cl)
3499 tree enclosing, name, cl;
3501 tree qual, to_return;
3502 if (!enclosing)
3503 return NULL_TREE;
3505 name = TYPE_NAME (name);
3507 /* First search: within the scope of `enclosing', search for name */
3508 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3509 qual = EXPR_WFL_QUALIFICATION (cl);
3510 else if (cl)
3511 qual = build_tree_list (cl, NULL_TREE);
3512 else
3513 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3515 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3516 return to_return;
3518 /* We're dealing with a qualified name. Try to resolve thing until
3519 we get something that is an enclosing class. */
3520 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3522 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3524 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3525 qual = TREE_CHAIN (qual))
3527 acc = merge_qualified_name (acc,
3528 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3529 BUILD_PTR_FROM_NAME (ptr, acc);
3530 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3533 /* A NULL qual and a decl means that the search ended
3534 successfully?!? We have to do something then. FIXME */
3536 if (decl)
3537 enclosing = decl;
3538 else
3539 qual = EXPR_WFL_QUALIFICATION (cl);
3541 /* Otherwise, create a qual for the other part of the resolution. */
3542 else
3543 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3545 return find_as_inner_class_do (qual, enclosing);
3548 /* We go inside the list of sub classes and try to find a way
3549 through. */
3551 static tree
3552 find_as_inner_class_do (qual, enclosing)
3553 tree qual, enclosing;
3555 if (!qual)
3556 return NULL_TREE;
3558 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3560 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3561 tree next_enclosing = NULL_TREE;
3562 tree inner_list;
3564 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3565 inner_list; inner_list = TREE_CHAIN (inner_list))
3567 if (TREE_VALUE (inner_list) == name_to_match)
3569 next_enclosing = TREE_PURPOSE (inner_list);
3570 break;
3573 enclosing = next_enclosing;
3576 return (!qual && enclosing ? enclosing : NULL_TREE);
3579 /* Reach all inner classes and tie their unqualified name to a
3580 DECL. */
3582 static void
3583 set_nested_class_simple_name_value (outer, set)
3584 tree outer;
3585 int set;
3587 tree l;
3589 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3590 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3591 TREE_PURPOSE (l) : NULL_TREE);
3594 static void
3595 link_nested_class_to_enclosing ()
3597 if (GET_ENCLOSING_CPC ())
3599 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3600 DECL_INNER_CLASS_LIST (enclosing) =
3601 tree_cons (GET_CPC (), GET_CPC_UN (),
3602 DECL_INNER_CLASS_LIST (enclosing));
3603 enclosing = enclosing;
3607 static tree
3608 maybe_make_nested_class_name (name)
3609 tree name;
3611 tree id = NULL_TREE;
3613 if (CPC_INNER_P ())
3615 make_nested_class_name (GET_CPC_LIST ());
3616 obstack_grow0 (&temporary_obstack,
3617 IDENTIFIER_POINTER (name),
3618 IDENTIFIER_LENGTH (name));
3619 id = get_identifier (obstack_finish (&temporary_obstack));
3620 if (ctxp->package)
3621 QUALIFIED_P (id) = 1;
3623 return id;
3626 /* If DECL is NULL, create and push a new DECL, record the current
3627 line CL and do other maintenance things. */
3629 static tree
3630 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3631 tree decl, raw_name, qualified_name, cl;
3633 if (!decl)
3634 decl = push_class (make_class (), qualified_name);
3636 /* Take care of the file and line business */
3637 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3638 /* If we're emiting xrefs, store the line/col number information */
3639 if (flag_emit_xref)
3640 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3641 else
3642 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3643 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3644 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3645 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3647 PUSH_CPC (decl, raw_name);
3648 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3650 /* Link the declaration to the already seen ones */
3651 TREE_CHAIN (decl) = ctxp->class_list;
3652 ctxp->class_list = decl;
3654 /* Create a new nodes in the global lists */
3655 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
3656 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3658 /* Install a new dependency list element */
3659 create_jdep_list (ctxp);
3661 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3662 IDENTIFIER_POINTER (qualified_name)));
3663 return decl;
3666 static void
3667 add_superinterfaces (decl, interface_list)
3668 tree decl, interface_list;
3670 tree node;
3671 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3672 takes care of ensuring that:
3673 - This is an accessible interface type,
3674 - Circularity detection.
3675 parser_add_interface is then called. If present but not defined,
3676 the check operation is delayed until the super interface gets
3677 defined. */
3678 for (node = interface_list; node; node = TREE_CHAIN (node))
3680 tree current = TREE_PURPOSE (node);
3681 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3682 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3684 if (!parser_check_super_interface (idecl, decl, current))
3685 parser_add_interface (decl, idecl, current);
3687 else
3688 register_incomplete_type (JDEP_INTERFACE,
3689 current, decl, NULL_TREE);
3693 /* Create an interface in pass1 and return its decl. Return the
3694 interface's decl in pass 2. */
3696 static tree
3697 create_interface (flags, id, super)
3698 int flags;
3699 tree id, super;
3701 tree raw_name = EXPR_WFL_NODE (id);
3702 tree q_name = parser_qualified_classname (raw_name);
3703 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3705 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3707 /* Basic checks: scope, redefinition, modifiers */
3708 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3710 PUSH_ERROR ();
3711 return NULL_TREE;
3714 /* Suspend the current parsing context if we're parsing an inner
3715 interface */
3716 if (CPC_INNER_P ())
3717 java_parser_context_suspend ();
3719 /* Push a new context for (static) initialized upon declaration fields */
3720 java_parser_context_push_initialized_field ();
3722 /* Interface modifiers check
3723 - public/abstract allowed (already done at that point)
3724 - abstract is obsolete (comes first, it's a warning, or should be)
3725 - Can't use twice the same (checked in the modifier rule) */
3726 if ((flags & ACC_ABSTRACT) && flag_redundant)
3727 parse_warning_context
3728 (MODIFIER_WFL (ABSTRACT_TK),
3729 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3731 /* Create a new decl if DECL is NULL, otherwise fix it */
3732 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3734 /* Set super info and mark the class a complete */
3735 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3736 object_type_node, ctxp->interface_number);
3737 ctxp->interface_number = 0;
3738 CLASS_COMPLETE_P (decl) = 1;
3739 add_superinterfaces (decl, super);
3741 return decl;
3744 /* Anonymous class counter. Will be reset to 1 every time a non
3745 anonymous class gets created. */
3746 static int anonymous_class_counter = 1;
3748 /* Patch anonymous class CLASS, by either extending or implementing
3749 DEP. */
3751 static void
3752 patch_anonymous_class (type_decl, class_decl, wfl)
3753 tree type_decl, class_decl, wfl;
3755 tree class = TREE_TYPE (class_decl);
3756 tree type = TREE_TYPE (type_decl);
3757 tree binfo = TYPE_BINFO (class);
3759 /* If it's an interface, implement it */
3760 if (CLASS_INTERFACE (type_decl))
3762 tree s_binfo;
3763 int length;
3765 if (parser_check_super_interface (type_decl, class_decl, wfl))
3766 return;
3768 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3769 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3770 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3771 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3772 /* And add the interface */
3773 parser_add_interface (class_decl, type_decl, wfl);
3775 /* Otherwise, it's a type we want to extend */
3776 else
3778 if (parser_check_super (type_decl, class_decl, wfl))
3779 return;
3780 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3784 static tree
3785 create_anonymous_class (location, type_name)
3786 int location;
3787 tree type_name;
3789 char buffer [80];
3790 tree super = NULL_TREE, itf = NULL_TREE;
3791 tree id, type_decl, class;
3793 /* The unqualified name of the anonymous class. It's just a number. */
3794 sprintf (buffer, "%d", anonymous_class_counter++);
3795 id = build_wfl_node (get_identifier (buffer));
3796 EXPR_WFL_LINECOL (id) = location;
3798 /* We know about the type to extend/implement. We go ahead */
3799 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3801 /* Create a class which either implements on extends the designated
3802 class. The class bears an innacessible name. */
3803 if (CLASS_INTERFACE (type_decl))
3805 /* It's OK to modify it here. It's been already used and
3806 shouldn't be reused */
3807 ctxp->interface_number = 1;
3808 /* Interfaces should presented as a list of WFLs */
3809 itf = build_tree_list (type_name, NULL_TREE);
3811 else
3812 super = type_name;
3815 class = create_class (ACC_FINAL, id, super, itf);
3817 /* We didn't know anything about the stuff. We register a dependence. */
3818 if (!type_decl)
3819 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3821 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3822 return class;
3825 /* Create a class in pass1 and return its decl. Return class
3826 interface's decl in pass 2. */
3828 static tree
3829 create_class (flags, id, super, interfaces)
3830 int flags;
3831 tree id, super, interfaces;
3833 tree raw_name = EXPR_WFL_NODE (id);
3834 tree class_id, decl;
3835 tree super_decl_type;
3837 class_id = parser_qualified_classname (raw_name);
3838 decl = IDENTIFIER_CLASS_VALUE (class_id);
3839 EXPR_WFL_NODE (id) = class_id;
3841 /* Basic check: scope, redefinition, modifiers */
3842 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3844 PUSH_ERROR ();
3845 return NULL_TREE;
3848 /* Suspend the current parsing context if we're parsing an inner
3849 class or an anonymous class. */
3850 if (CPC_INNER_P ())
3851 java_parser_context_suspend ();
3852 /* Push a new context for (static) initialized upon declaration fields */
3853 java_parser_context_push_initialized_field ();
3855 /* Class modifier check:
3856 - Allowed modifier (already done at that point)
3857 - abstract AND final forbidden
3858 - Public classes defined in the correct file */
3859 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3860 parse_error_context
3861 (id, "Class `%s' can't be declared both abstract and final",
3862 IDENTIFIER_POINTER (raw_name));
3864 /* Create a new decl if DECL is NULL, otherwise fix it */
3865 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3867 /* If SUPER exists, use it, otherwise use Object */
3868 if (super)
3870 /* Can't extend java.lang.Object */
3871 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3873 parse_error_context (id, "Can't extend `java.lang.Object'");
3874 return NULL_TREE;
3877 super_decl_type =
3878 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3880 else if (TREE_TYPE (decl) != object_type_node)
3881 super_decl_type = object_type_node;
3882 /* We're defining java.lang.Object */
3883 else
3884 super_decl_type = NULL_TREE;
3886 /* A class nested in an interface is implicitly static. */
3887 if (INNER_CLASS_DECL_P (decl)
3888 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3890 flags |= ACC_STATIC;
3893 /* Set super info and mark the class as complete. */
3894 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3895 ctxp->interface_number);
3896 ctxp->interface_number = 0;
3897 CLASS_COMPLETE_P (decl) = 1;
3898 add_superinterfaces (decl, interfaces);
3900 /* Add the private this$<n> field, Replicate final locals still in
3901 scope as private final fields mangled like val$<local_name>.
3902 This doesn't not occur for top level (static) inner classes. */
3903 if (PURE_INNER_CLASS_DECL_P (decl))
3904 add_inner_class_fields (decl, current_function_decl);
3906 /* If doing xref, store the location at which the inherited class
3907 (if any) was seen. */
3908 if (flag_emit_xref && super)
3909 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3911 /* Eventually sets the @deprecated tag flag */
3912 CHECK_DEPRECATED (decl);
3914 /* Reset the anonymous class counter when declaring non inner classes */
3915 if (!INNER_CLASS_DECL_P (decl))
3916 anonymous_class_counter = 1;
3918 return decl;
3921 /* End a class declaration: register the statements used to create
3922 finit$ and <clinit>, pop the current class and resume the prior
3923 parser context if necessary. */
3925 static void
3926 end_class_declaration (resume)
3927 int resume;
3929 /* If an error occured, context weren't pushed and won't need to be
3930 popped by a resume. */
3931 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3933 java_parser_context_pop_initialized_field ();
3934 POP_CPC ();
3935 if (resume && no_error_occured)
3936 java_parser_context_resume ();
3938 /* We're ending a class declaration, this is a good time to reset
3939 the interface cout. Note that might have been already done in
3940 create_interface, but if at that time an inner class was being
3941 dealt with, the interface count was reset in a context created
3942 for the sake of handling inner classes declaration. */
3943 ctxp->interface_number = 0;
3946 static void
3947 add_inner_class_fields (class_decl, fct_decl)
3948 tree class_decl;
3949 tree fct_decl;
3951 tree block, marker, f;
3953 f = add_field (TREE_TYPE (class_decl),
3954 build_current_thisn (TREE_TYPE (class_decl)),
3955 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3956 ACC_PRIVATE);
3957 FIELD_THISN (f) = 1;
3959 if (!fct_decl)
3960 return;
3962 for (block = GET_CURRENT_BLOCK (fct_decl);
3963 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3965 tree decl;
3966 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3968 tree name, pname;
3969 tree wfl, init, list;
3971 /* Avoid non final arguments. */
3972 if (!LOCAL_FINAL_P (decl))
3973 continue;
3975 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3976 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3977 wfl = build_wfl_node (name);
3978 init = build_wfl_node (pname);
3979 /* Build an initialization for the field: it will be
3980 initialized by a parameter added to finit$, bearing a
3981 mangled name of the field itself (param$<n>.) The
3982 parameter is provided to finit$ by the constructor
3983 invoking it (hence the constructor will also feature a
3984 hidden parameter, set to the value of the outer context
3985 local at the time the inner class is created.)
3987 Note: we take into account all possible locals that can
3988 be accessed by the inner class. It's actually not trivial
3989 to minimize these aliases down to the ones really
3990 used. One way to do that would be to expand all regular
3991 methods first, then finit$ to get a picture of what's
3992 used. It works with the exception that we would have to
3993 go back on all constructor invoked in regular methods to
3994 have their invokation reworked (to include the right amount
3995 of alias initializer parameters.)
3997 The only real way around, I think, is a first pass to
3998 identify locals really used in the inner class. We leave
3999 the flag FIELD_LOCAL_ALIAS_USED around for that future
4000 use.
4002 On the other hand, it only affect local inner classes,
4003 whose constructors (and finit$ call) will be featuring
4004 unecessary arguments. It's easy for a developper to keep
4005 this number of parameter down by using the `final'
4006 keyword only when necessary. For the time being, we can
4007 issue a warning on unecessary finals. FIXME */
4008 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4009 wfl, init);
4011 /* Register the field. The TREE_LIST holding the part
4012 initialized/initializer will be marked ARG_FINAL_P so
4013 that the created field can be marked
4014 FIELD_LOCAL_ALIAS. */
4015 list = build_tree_list (wfl, init);
4016 ARG_FINAL_P (list) = 1;
4017 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4021 if (!CPC_INITIALIZER_STMT (ctxp))
4022 return;
4024 /* If we ever registered an alias field, insert and marker to
4025 remeber where the list ends. The second part of the list (the one
4026 featuring initialized fields) so it can be later reversed to
4027 enforce 8.5. The marker will be removed during that operation. */
4028 marker = build_tree_list (NULL_TREE, NULL_TREE);
4029 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4030 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4033 /* Can't use lookup_field () since we don't want to load the class and
4034 can't set the CLASS_LOADED_P flag */
4036 static tree
4037 find_field (class, name)
4038 tree class;
4039 tree name;
4041 tree decl;
4042 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4044 if (DECL_NAME (decl) == name)
4045 return decl;
4047 return NULL_TREE;
4050 /* Wrap around lookup_field that doesn't potentially upset the value
4051 of CLASS */
4053 static tree
4054 lookup_field_wrapper (class, name)
4055 tree class, name;
4057 tree type = class;
4058 tree decl = NULL_TREE;
4059 java_parser_context_save_global ();
4061 /* Last chance: if we're within the context of an inner class, we
4062 might be trying to access a local variable defined in an outer
4063 context. We try to look for it now. */
4064 if (INNER_CLASS_TYPE_P (class))
4066 tree new_name;
4067 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4068 decl = lookup_field (&type, new_name);
4069 if (decl && decl != error_mark_node)
4070 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4072 if (!decl || decl == error_mark_node)
4074 type = class;
4075 decl = lookup_field (&type, name);
4078 java_parser_context_restore_global ();
4079 return decl == error_mark_node ? NULL : decl;
4082 /* Find duplicate field within the same class declarations and report
4083 the error. Returns 1 if a duplicated field was found, 0
4084 otherwise. */
4086 static int
4087 duplicate_declaration_error_p (new_field_name, new_type, cl)
4088 tree new_field_name, new_type, cl;
4090 /* This might be modified to work with method decl as well */
4091 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4092 if (decl)
4094 char *t1 = xstrdup (purify_type_name
4095 ((TREE_CODE (new_type) == POINTER_TYPE
4096 && TREE_TYPE (new_type) == NULL_TREE) ?
4097 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4098 lang_printable_name (new_type, 1)));
4099 /* The type may not have been completed by the time we report
4100 the error */
4101 char *t2 = xstrdup (purify_type_name
4102 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4103 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4104 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4105 lang_printable_name (TREE_TYPE (decl), 1)));
4106 parse_error_context
4107 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4108 t1, IDENTIFIER_POINTER (new_field_name),
4109 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4110 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4111 free (t1);
4112 free (t2);
4113 return 1;
4115 return 0;
4118 /* Field registration routine. If TYPE doesn't exist, field
4119 declarations are linked to the undefined TYPE dependency list, to
4120 be later resolved in java_complete_class () */
4122 static void
4123 register_fields (flags, type, variable_list)
4124 int flags;
4125 tree type, variable_list;
4127 tree current, saved_type;
4128 tree class_type = NULL_TREE;
4129 int saved_lineno = lineno;
4130 int must_chain = 0;
4131 tree wfl = NULL_TREE;
4133 if (GET_CPC ())
4134 class_type = TREE_TYPE (GET_CPC ());
4136 if (!class_type || class_type == error_mark_node)
4137 return;
4139 /* If we're adding fields to interfaces, those fields are public,
4140 static, final */
4141 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4143 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4144 flags, ACC_PUBLIC, "interface field(s)");
4145 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4146 flags, ACC_STATIC, "interface field(s)");
4147 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4148 flags, ACC_FINAL, "interface field(s)");
4149 check_modifiers ("Illegal interface member modifier `%s'", flags,
4150 INTERFACE_FIELD_MODIFIERS);
4151 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4154 /* Obtain a suitable type for resolution, if necessary */
4155 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4157 /* If TYPE is fully resolved and we don't have a reference, make one */
4158 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4160 for (current = variable_list, saved_type = type; current;
4161 current = TREE_CHAIN (current), type = saved_type)
4163 tree real_type;
4164 tree field_decl;
4165 tree cl = TREE_PURPOSE (current);
4166 tree init = TREE_VALUE (current);
4167 tree current_name = EXPR_WFL_NODE (cl);
4169 /* Can't declare non-final static fields in inner classes */
4170 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4171 && !(flags & ACC_FINAL))
4172 parse_error_context
4173 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4174 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4175 lang_printable_name (class_type, 0));
4177 /* Process NAME, as it may specify extra dimension(s) for it */
4178 type = build_array_from_name (type, wfl, current_name, &current_name);
4180 /* Type adjustment. We may have just readjusted TYPE because
4181 the variable specified more dimensions. Make sure we have
4182 a reference if we can and don't have one already. Also
4183 change the name if we have an init. */
4184 if (type != saved_type)
4186 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4187 if (init)
4188 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4191 real_type = GET_REAL_TYPE (type);
4192 /* Check for redeclarations */
4193 if (duplicate_declaration_error_p (current_name, real_type, cl))
4194 continue;
4196 /* Set lineno to the line the field was found and create a
4197 declaration for it. Eventually sets the @deprecated tag flag. */
4198 if (flag_emit_xref)
4199 lineno = EXPR_WFL_LINECOL (cl);
4200 else
4201 lineno = EXPR_WFL_LINENO (cl);
4202 field_decl = add_field (class_type, current_name, real_type, flags);
4203 CHECK_DEPRECATED (field_decl);
4205 /* If the field denotes a final instance variable, then we
4206 allocate a LANG_DECL_SPECIFIC part to keep track of its
4207 initialization. We also mark whether the field was
4208 initialized upon it's declaration. We don't do that if the
4209 created field is an alias to a final local. */
4210 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4212 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4213 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4214 if ((flags & ACC_STATIC) && init)
4215 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4218 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4219 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4220 hide parameters to this inner class finit$ and
4221 constructors. It also means that the field isn't final per
4222 say. */
4223 if (ARG_FINAL_P (current))
4225 FIELD_LOCAL_ALIAS (field_decl) = 1;
4226 FIELD_FINAL (field_decl) = 0;
4229 /* Check if we must chain. */
4230 if (must_chain)
4231 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4233 /* If we have an initialization value tied to the field */
4234 if (init)
4236 /* The field is declared static */
4237 if (flags & ACC_STATIC)
4239 /* We include the field and its initialization part into
4240 a list used to generate <clinit>. After <clinit> is
4241 walked, field initializations will be processed and
4242 fields initialized with known constants will be taken
4243 out of <clinit> and have their DECL_INITIAL set
4244 appropriately. */
4245 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4246 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4247 if (TREE_OPERAND (init, 1)
4248 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4249 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4251 /* A non-static field declared with an immediate initialization is
4252 to be initialized in <init>, if any. This field is remembered
4253 to be processed at the time of the generation of <init>. */
4254 else
4256 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4257 SET_CPC_INITIALIZER_STMT (ctxp, init);
4259 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4260 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4263 lineno = saved_lineno;
4266 /* Generate finit$, using the list of initialized fields to populate
4267 its body. finit$'s parameter(s) list is adjusted to include the
4268 one(s) used to initialized the field(s) caching outer context
4269 local(s). */
4271 static tree
4272 generate_finit (class_type)
4273 tree class_type;
4275 int count = 0;
4276 tree list = TYPE_FINIT_STMT_LIST (class_type);
4277 tree mdecl, current, parms;
4279 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4280 class_type, NULL_TREE,
4281 &count);
4282 CRAFTED_PARAM_LIST_FIXUP (parms);
4283 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4284 finit_identifier_node, parms);
4285 fix_method_argument_names (parms, mdecl);
4286 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4287 mdecl, NULL_TREE);
4288 DECL_FUNCTION_NAP (mdecl) = count;
4289 start_artificial_method_body (mdecl);
4291 for (current = list; current; current = TREE_CHAIN (current))
4292 java_method_add_stmt (mdecl,
4293 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4294 current));
4295 end_artificial_method_body (mdecl);
4296 return mdecl;
4299 static void
4300 add_instance_initializer (mdecl)
4301 tree mdecl;
4303 tree current;
4304 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4305 tree compound = NULL_TREE;
4307 if (stmt_list)
4309 for (current = stmt_list; current; current = TREE_CHAIN (current))
4310 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4312 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4313 NULL_TREE, compound));
4317 /* Shared accros method_declarator and method_header to remember the
4318 patch stage that was reached during the declaration of the method.
4319 A method DECL is built differently is there is no patch
4320 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4321 pending on the currently defined method. */
4323 static int patch_stage;
4325 /* Check the method declaration and add the method to its current
4326 class. If the argument list is known to contain incomplete types,
4327 the method is partially added and the registration will be resume
4328 once the method arguments resolved. If TYPE is NULL, we're dealing
4329 with a constructor. */
4331 static tree
4332 method_header (flags, type, mdecl, throws)
4333 int flags;
4334 tree type, mdecl, throws;
4336 tree type_wfl = NULL_TREE;
4337 tree meth_name = NULL_TREE;
4338 tree current, orig_arg, this_class = NULL;
4339 tree id, meth;
4340 int saved_lineno;
4341 int constructor_ok = 0, must_chain;
4342 int count;
4344 if (mdecl == error_mark_node)
4345 return error_mark_node;
4346 meth = TREE_VALUE (mdecl);
4347 id = TREE_PURPOSE (mdecl);
4349 check_modifiers_consistency (flags);
4351 if (GET_CPC ())
4352 this_class = TREE_TYPE (GET_CPC ());
4354 if (!this_class || this_class == error_mark_node)
4355 return NULL_TREE;
4357 /* There are some forbidden modifiers for an abstract method and its
4358 class must be abstract as well. */
4359 if (type && (flags & ACC_ABSTRACT))
4361 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4362 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4363 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4364 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4365 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4366 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4367 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4368 parse_error_context
4369 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4370 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4371 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4374 /* Things to be checked when declaring a constructor */
4375 if (!type)
4377 int ec = java_error_count;
4378 /* 8.6: Constructor declarations: we might be trying to define a
4379 method without specifying a return type. */
4380 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4381 parse_error_context
4382 (id, "Invalid method declaration, return type required");
4383 /* 8.6.3: Constructor modifiers */
4384 else
4386 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4387 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4388 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4389 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4390 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4392 /* If we found error here, we don't consider it's OK to tread
4393 the method definition as a constructor, for the rest of this
4394 function */
4395 if (ec == java_error_count)
4396 constructor_ok = 1;
4399 /* Method declared within the scope of an interface are implicitly
4400 abstract and public. Conflicts with other erroneously provided
4401 modifiers are checked right after. */
4403 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4405 /* If FLAGS isn't set because of a modifier, turn the
4406 corresponding modifier WFL to NULL so we issue a warning on
4407 the obsolete use of the modifier */
4408 if (!(flags & ACC_PUBLIC))
4409 MODIFIER_WFL (PUBLIC_TK) = NULL;
4410 if (!(flags & ACC_ABSTRACT))
4411 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4412 flags |= ACC_PUBLIC;
4413 flags |= ACC_ABSTRACT;
4416 /* Inner class can't declare static methods */
4417 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4419 parse_error_context
4420 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4421 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4422 lang_printable_name (this_class, 0));
4425 /* Modifiers context reset moved up, so abstract method declaration
4426 modifiers can be later checked. */
4428 /* Set constructor returned type to void and method name to <init>,
4429 unless we found an error identifier the constructor (in which
4430 case we retain the original name) */
4431 if (!type)
4433 type = void_type_node;
4434 if (constructor_ok)
4435 meth_name = init_identifier_node;
4437 else
4438 meth_name = EXPR_WFL_NODE (id);
4440 /* Do the returned type resolution and registration if necessary */
4441 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4443 if (meth_name)
4444 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4445 EXPR_WFL_NODE (id) = meth_name;
4446 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4448 if (must_chain)
4450 patch_stage = JDEP_METHOD_RETURN;
4451 register_incomplete_type (patch_stage, type_wfl, id, type);
4452 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4454 else
4455 TREE_TYPE (meth) = type;
4457 saved_lineno = lineno;
4458 /* When defining an abstract or interface method, the curly
4459 bracket at level 1 doesn't exist because there is no function
4460 body */
4461 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4462 EXPR_WFL_LINENO (id));
4464 /* Remember the original argument list */
4465 orig_arg = TYPE_ARG_TYPES (meth);
4467 if (patch_stage) /* includes ret type and/or all args */
4469 jdep *jdep;
4470 meth = add_method_1 (this_class, flags, meth_name, meth);
4471 /* Patch for the return type */
4472 if (patch_stage == JDEP_METHOD_RETURN)
4474 jdep = CLASSD_LAST (ctxp->classd_list);
4475 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4477 /* This is the stop JDEP. METH allows the function's signature
4478 to be computed. */
4479 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4481 else
4482 meth = add_method (this_class, flags, meth_name,
4483 build_java_signature (meth));
4485 /* Remember final parameters */
4486 MARK_FINAL_PARMS (meth, orig_arg);
4488 /* Fix the method argument list so we have the argument name
4489 information */
4490 fix_method_argument_names (orig_arg, meth);
4492 /* Register the parameter number and re-install the current line
4493 number */
4494 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4495 lineno = saved_lineno;
4497 /* Register exception specified by the `throws' keyword for
4498 resolution and set the method decl appropriate field to the list.
4499 Note: the grammar ensures that what we get here are class
4500 types. */
4501 if (throws)
4503 throws = nreverse (throws);
4504 for (current = throws; current; current = TREE_CHAIN (current))
4506 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4507 NULL_TREE, NULL_TREE);
4508 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4509 &TREE_VALUE (current);
4511 DECL_FUNCTION_THROWS (meth) = throws;
4514 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4515 DECL_FUNCTION_WFL (meth) = id;
4517 /* Set the flag if we correctly processed a constructor */
4518 if (constructor_ok)
4520 DECL_CONSTRUCTOR_P (meth) = 1;
4521 /* Compute and store the number of artificial parameters declared
4522 for this constructor */
4523 for (count = 0, current = TYPE_FIELDS (this_class); current;
4524 current = TREE_CHAIN (current))
4525 if (FIELD_LOCAL_ALIAS (current))
4526 count++;
4527 DECL_FUNCTION_NAP (meth) = count;
4530 /* Eventually set the @deprecated tag flag */
4531 CHECK_DEPRECATED (meth);
4533 /* If doing xref, store column and line number information instead
4534 of the line number only. */
4535 if (flag_emit_xref)
4536 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4538 return meth;
4541 static void
4542 fix_method_argument_names (orig_arg, meth)
4543 tree orig_arg, meth;
4545 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4546 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4548 TREE_PURPOSE (arg) = this_identifier_node;
4549 arg = TREE_CHAIN (arg);
4551 while (orig_arg != end_params_node)
4553 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4554 orig_arg = TREE_CHAIN (orig_arg);
4555 arg = TREE_CHAIN (arg);
4559 /* Complete the method declaration with METHOD_BODY. */
4561 static void
4562 finish_method_declaration (method_body)
4563 tree method_body;
4565 int flags;
4567 if (!current_function_decl)
4568 return;
4570 flags = get_access_flags_from_decl (current_function_decl);
4572 /* 8.4.5 Method Body */
4573 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4575 tree wfl = DECL_NAME (current_function_decl);
4576 parse_error_context (wfl,
4577 "%s method `%s' can't have a body defined",
4578 (METHOD_NATIVE (current_function_decl) ?
4579 "Native" : "Abstract"),
4580 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4581 method_body = NULL_TREE;
4583 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4585 tree wfl = DECL_NAME (current_function_decl);
4586 parse_error_context
4587 (wfl,
4588 "Non native and non abstract method `%s' must have a body defined",
4589 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4590 method_body = NULL_TREE;
4593 if (flag_emit_class_files && method_body
4594 && TREE_CODE (method_body) == NOP_EXPR
4595 && TREE_TYPE (current_function_decl)
4596 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4597 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4599 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4600 maybe_absorb_scoping_blocks ();
4601 /* Exit function's body */
4602 exit_block ();
4603 /* Merge last line of the function with first line, directly in the
4604 function decl. It will be used to emit correct debug info. */
4605 if (!flag_emit_xref)
4606 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4608 /* Since function's argument's list are shared, reset the
4609 ARG_FINAL_P parameter that might have been set on some of this
4610 function parameters. */
4611 UNMARK_FINAL_PARMS (current_function_decl);
4613 /* So we don't have an irrelevant function declaration context for
4614 the next static block we'll see. */
4615 current_function_decl = NULL_TREE;
4618 /* Build a an error message for constructor circularity errors. */
4620 static char *
4621 constructor_circularity_msg (from, to)
4622 tree from, to;
4624 static char string [4096];
4625 char *t = xstrdup (lang_printable_name (from, 0));
4626 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4627 free (t);
4628 return string;
4631 /* Verify a circular call to METH. Return 1 if an error is found, 0
4632 otherwise. */
4634 static int
4635 verify_constructor_circularity (meth, current)
4636 tree meth, current;
4638 static tree list = NULL_TREE;
4639 static int initialized_p;
4640 tree c;
4642 /* If we haven't already registered LIST with the garbage collector,
4643 do so now. */
4644 if (!initialized_p)
4646 ggc_add_tree_root (&list, 1);
4647 initialized_p = 1;
4650 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4652 if (TREE_VALUE (c) == meth)
4654 char *t;
4655 if (list)
4657 tree liste;
4658 list = nreverse (list);
4659 for (liste = list; liste; liste = TREE_CHAIN (liste))
4661 parse_error_context
4662 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4663 constructor_circularity_msg
4664 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4665 java_error_count--;
4668 t = xstrdup (lang_printable_name (meth, 0));
4669 parse_error_context (TREE_PURPOSE (c),
4670 "%s: recursive invocation of constructor `%s'",
4671 constructor_circularity_msg (current, meth), t);
4672 free (t);
4673 list = NULL_TREE;
4674 return 1;
4677 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4679 list = tree_cons (c, current, list);
4680 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4681 return 1;
4682 list = TREE_CHAIN (list);
4684 return 0;
4687 /* Check modifiers that can be declared but exclusively */
4689 static void
4690 check_modifiers_consistency (flags)
4691 int flags;
4693 int acc_count = 0;
4694 tree cl = NULL_TREE;
4696 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4697 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4698 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4699 if (acc_count > 1)
4700 parse_error_context
4701 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4703 acc_count = 0;
4704 cl = NULL_TREE;
4705 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4706 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4707 if (acc_count > 1)
4708 parse_error_context (cl,
4709 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4712 /* Check the methode header METH for abstract specifics features */
4714 static void
4715 check_abstract_method_header (meth)
4716 tree meth;
4718 int flags = get_access_flags_from_decl (meth);
4720 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4721 ACC_ABSTRACT, "abstract method",
4722 IDENTIFIER_POINTER (DECL_NAME (meth)));
4723 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4724 ACC_PUBLIC, "abstract method",
4725 IDENTIFIER_POINTER (DECL_NAME (meth)));
4727 check_modifiers ("Illegal modifier `%s' for interface method",
4728 flags, INTERFACE_METHOD_MODIFIERS);
4731 /* Create a FUNCTION_TYPE node and start augmenting it with the
4732 declared function arguments. Arguments type that can't be resolved
4733 are left as they are, but the returned node is marked as containing
4734 incomplete types. */
4736 static tree
4737 method_declarator (id, list)
4738 tree id, list;
4740 tree arg_types = NULL_TREE, current, node;
4741 tree meth = make_node (FUNCTION_TYPE);
4742 jdep *jdep;
4744 patch_stage = JDEP_NO_PATCH;
4746 if (GET_CPC () == error_mark_node)
4747 return error_mark_node;
4749 /* If we're dealing with an inner class constructor, we hide the
4750 this$<n> decl in the name field of its parameter declaration. We
4751 also might have to hide the outer context local alias
4752 initializers. Not done when the class is a toplevel class. */
4753 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4754 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4756 tree aliases_list, type, thisn;
4757 /* First the aliases, linked to the regular parameters */
4758 aliases_list =
4759 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4760 TREE_TYPE (GET_CPC ()),
4761 NULL_TREE, NULL);
4762 list = chainon (nreverse (aliases_list), list);
4764 /* Then this$<n> */
4765 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4766 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4767 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4768 list);
4771 for (current = list; current; current = TREE_CHAIN (current))
4773 int must_chain = 0;
4774 tree wfl_name = TREE_PURPOSE (current);
4775 tree type = TREE_VALUE (current);
4776 tree name = EXPR_WFL_NODE (wfl_name);
4777 tree already, arg_node;
4778 tree type_wfl = NULL_TREE;
4779 tree real_type;
4781 /* Obtain a suitable type for resolution, if necessary */
4782 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4784 /* Process NAME, as it may specify extra dimension(s) for it */
4785 type = build_array_from_name (type, type_wfl, name, &name);
4786 EXPR_WFL_NODE (wfl_name) = name;
4788 real_type = GET_REAL_TYPE (type);
4789 if (TREE_CODE (real_type) == RECORD_TYPE)
4791 real_type = promote_type (real_type);
4792 if (TREE_CODE (type) == TREE_LIST)
4793 TREE_PURPOSE (type) = real_type;
4796 /* Check redefinition */
4797 for (already = arg_types; already; already = TREE_CHAIN (already))
4798 if (TREE_PURPOSE (already) == name)
4800 parse_error_context
4801 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4802 IDENTIFIER_POINTER (name),
4803 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4804 break;
4807 /* If we've an incomplete argument type, we know there is a location
4808 to patch when the type get resolved, later. */
4809 jdep = NULL;
4810 if (must_chain)
4812 patch_stage = JDEP_METHOD;
4813 type = register_incomplete_type (patch_stage,
4814 type_wfl, wfl_name, type);
4815 jdep = CLASSD_LAST (ctxp->classd_list);
4816 JDEP_MISC (jdep) = id;
4819 /* The argument node: a name and a (possibly) incomplete type. */
4820 arg_node = build_tree_list (name, real_type);
4821 /* Remeber arguments declared final. */
4822 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4824 if (jdep)
4825 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4826 TREE_CHAIN (arg_node) = arg_types;
4827 arg_types = arg_node;
4829 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4830 node = build_tree_list (id, meth);
4831 return node;
4834 static int
4835 unresolved_type_p (wfl, returned)
4836 tree wfl;
4837 tree *returned;
4840 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4842 if (returned)
4844 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4845 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4846 *returned = TREE_TYPE (decl);
4847 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4848 *returned = TREE_TYPE (GET_CPC ());
4849 else
4850 *returned = NULL_TREE;
4852 return 1;
4854 if (returned)
4855 *returned = wfl;
4856 return 0;
4859 /* From NAME, build a qualified identifier node using the
4860 qualification from the current package definition. */
4862 static tree
4863 parser_qualified_classname (name)
4864 tree name;
4866 tree nested_class_name;
4868 if ((nested_class_name = maybe_make_nested_class_name (name)))
4869 return nested_class_name;
4871 if (ctxp->package)
4872 return merge_qualified_name (ctxp->package, name);
4873 else
4874 return name;
4877 /* Called once the type a interface extends is resolved. Returns 0 if
4878 everything is OK. */
4880 static int
4881 parser_check_super_interface (super_decl, this_decl, this_wfl)
4882 tree super_decl, this_decl, this_wfl;
4884 tree super_type = TREE_TYPE (super_decl);
4886 /* Has to be an interface */
4887 if (!CLASS_INTERFACE (super_decl))
4889 parse_error_context
4890 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4891 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4892 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4893 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4894 "interface" : "class"),
4895 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4896 return 1;
4899 /* Check scope: same package OK, other package: OK if public */
4900 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4901 return 1;
4903 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4904 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4905 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4906 return 0;
4909 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4910 0 if everthing is OK. */
4912 static int
4913 parser_check_super (super_decl, this_decl, wfl)
4914 tree super_decl, this_decl, wfl;
4916 tree super_type = TREE_TYPE (super_decl);
4918 /* SUPER should be a CLASS (neither an array nor an interface) */
4919 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4921 parse_error_context
4922 (wfl, "Class `%s' can't subclass %s `%s'",
4923 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4924 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4925 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4926 return 1;
4929 if (CLASS_FINAL (TYPE_NAME (super_type)))
4931 parse_error_context (wfl, "Can't subclass final classes: %s",
4932 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4933 return 1;
4936 /* Check scope: same package OK, other package: OK if public */
4937 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4938 return 1;
4940 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4941 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4942 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4943 return 0;
4946 /* Create a new dependency list and link it (in a LIFO manner) to the
4947 CTXP list of type dependency list. */
4949 static void
4950 create_jdep_list (ctxp)
4951 struct parser_ctxt *ctxp;
4953 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
4954 new->first = new->last = NULL;
4955 new->next = ctxp->classd_list;
4956 ctxp->classd_list = new;
4959 static jdeplist *
4960 reverse_jdep_list (ctxp)
4961 struct parser_ctxt *ctxp;
4963 register jdeplist *prev = NULL, *current, *next;
4964 for (current = ctxp->classd_list; current; current = next)
4966 next = current->next;
4967 current->next = prev;
4968 prev = current;
4970 return prev;
4973 /* Create a fake pointer based on the ID stored in
4974 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4975 registered again. */
4977 static tree
4978 obtain_incomplete_type (type_name)
4979 tree type_name;
4981 tree ptr, name;
4983 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4984 name = EXPR_WFL_NODE (type_name);
4985 else if (INCOMPLETE_TYPE_P (type_name))
4986 name = TYPE_NAME (type_name);
4987 else
4988 fatal ("invalid type name - obtain_incomplete_type");
4990 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
4991 if (TYPE_NAME (ptr) == name)
4992 break;
4994 if (!ptr)
4996 BUILD_PTR_FROM_NAME (ptr, name);
4997 layout_type (ptr);
4998 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4999 ctxp->incomplete_class = ptr;
5002 return ptr;
5005 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5006 non NULL instead of computing a new fake type based on WFL. The new
5007 dependency is inserted in the current type dependency list, in FIFO
5008 manner. */
5010 static tree
5011 register_incomplete_type (kind, wfl, decl, ptr)
5012 int kind;
5013 tree wfl, decl, ptr;
5015 jdep *new = (jdep *)xmalloc (sizeof (jdep));
5017 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5018 ptr = obtain_incomplete_type (wfl);
5020 JDEP_KIND (new) = kind;
5021 JDEP_DECL (new) = decl;
5022 JDEP_SOLV (new) = ptr;
5023 JDEP_WFL (new) = wfl;
5024 JDEP_CHAIN (new) = NULL;
5025 JDEP_MISC (new) = NULL_TREE;
5026 /* For some dependencies, set the enclosing class of the current
5027 class to be the enclosing context */
5028 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5029 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
5030 && GET_ENCLOSING_CPC ())
5031 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5032 else
5033 JDEP_ENCLOSING (new) = GET_CPC ();
5034 JDEP_GET_PATCH (new) = (tree *)NULL;
5036 JDEP_INSERT (ctxp->classd_list, new);
5038 return ptr;
5041 void
5042 java_check_circular_reference ()
5044 tree current;
5045 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5047 tree type = TREE_TYPE (current);
5048 if (CLASS_INTERFACE (current))
5050 /* Check all interfaces this class extends */
5051 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5052 int n, i;
5054 if (!basetype_vec)
5055 return;
5056 n = TREE_VEC_LENGTH (basetype_vec);
5057 for (i = 0; i < n; i++)
5059 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5060 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5061 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5062 parse_error_context (lookup_cl (current),
5063 "Cyclic interface inheritance");
5066 else
5067 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5068 parse_error_context (lookup_cl (current),
5069 "Cyclic class inheritance%s",
5070 (cyclic_inheritance_report ?
5071 cyclic_inheritance_report : ""));
5075 /* Augment the parameter list PARM with parameters crafted to
5076 initialize outer context locals aliases. Through ARTIFICIAL, a
5077 count is kept of the number of crafted parameters. MODE governs
5078 what eventually gets created: something suitable for a function
5079 creation or a function invocation, either the constructor or
5080 finit$. */
5082 static tree
5083 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5084 int mode;
5085 tree class_type, parm;
5086 int *artificial;
5088 tree field;
5089 tree additional_parms = NULL_TREE;
5091 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5092 if (FIELD_LOCAL_ALIAS (field))
5094 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5095 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5096 tree mangled_id;
5098 switch (mode)
5100 case AIPL_FUNCTION_DECLARATION:
5101 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5102 &buffer [4]);
5103 purpose = build_wfl_node (mangled_id);
5104 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5105 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5106 else
5107 value = TREE_TYPE (field);
5108 break;
5110 case AIPL_FUNCTION_CREATION:
5111 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5112 &buffer [4]);
5113 value = TREE_TYPE (field);
5114 break;
5116 case AIPL_FUNCTION_FINIT_INVOCATION:
5117 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5118 &buffer [4]);
5119 /* Now, this is wrong. purpose should always be the NAME
5120 of something and value its matching value (decl, type,
5121 etc...) FIXME -- but there is a lot to fix. */
5123 /* When invoked for this kind of operation, we already
5124 know whether a field is used or not. */
5125 purpose = TREE_TYPE (field);
5126 value = build_wfl_node (mangled_id);
5127 break;
5129 case AIPL_FUNCTION_CTOR_INVOCATION:
5130 /* There are two case: the constructor invokation happends
5131 outside the local inner, in which case, locales from the outer
5132 context are directly used.
5134 Otherwise, we fold to using the alias directly. */
5135 if (class_type == current_class)
5136 value = field;
5137 else
5139 name = get_identifier (&buffer[4]);
5140 value = IDENTIFIER_LOCAL_VALUE (name);
5142 break;
5144 additional_parms = tree_cons (purpose, value, additional_parms);
5145 if (artificial)
5146 *artificial +=1;
5148 if (additional_parms)
5150 if (ANONYMOUS_CLASS_P (class_type)
5151 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5152 additional_parms = nreverse (additional_parms);
5153 parm = chainon (additional_parms, parm);
5156 return parm;
5159 /* Craft a constructor for CLASS_DECL -- what we should do when none
5160 where found. ARGS is non NULL when a special signature must be
5161 enforced. This is the case for anonymous classes. */
5163 static void
5164 craft_constructor (class_decl, args)
5165 tree class_decl, args;
5167 tree class_type = TREE_TYPE (class_decl);
5168 tree parm = NULL_TREE;
5169 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5170 ACC_PUBLIC : 0);
5171 int i = 0, artificial = 0;
5172 tree decl, ctor_name;
5173 char buffer [80];
5175 /* The constructor name is <init> unless we're dealing with an
5176 anonymous class, in which case the name will be fixed after having
5177 be expanded. */
5178 if (ANONYMOUS_CLASS_P (class_type))
5179 ctor_name = DECL_NAME (class_decl);
5180 else
5181 ctor_name = init_identifier_node;
5183 /* If we're dealing with an inner class constructor, we hide the
5184 this$<n> decl in the name field of its parameter declaration. */
5185 if (PURE_INNER_CLASS_TYPE_P (class_type))
5187 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5188 parm = tree_cons (build_current_thisn (class_type),
5189 build_pointer_type (type), parm);
5191 /* Some more arguments to be hidden here. The values of the local
5192 variables of the outer context that the inner class needs to see. */
5193 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5194 class_type, parm,
5195 &artificial);
5198 /* Then if there are any args to be enforced, enforce them now */
5199 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5201 sprintf (buffer, "parm%d", i++);
5202 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5205 CRAFTED_PARAM_LIST_FIXUP (parm);
5206 decl = create_artificial_method (class_type, flags, void_type_node,
5207 ctor_name, parm);
5208 fix_method_argument_names (parm, decl);
5209 /* Now, mark the artificial parameters. */
5210 DECL_FUNCTION_NAP (decl) = artificial;
5211 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5215 /* Fix the constructors. This will be called right after circular
5216 references have been checked. It is necessary to fix constructors
5217 early even if no code generation will take place for that class:
5218 some generated constructor might be required by the class whose
5219 compilation triggered this one to be simply loaded. */
5221 void
5222 java_fix_constructors ()
5224 tree current;
5226 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5228 tree class_type = TREE_TYPE (current);
5229 int saw_ctor = 0;
5230 tree decl;
5232 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5233 continue;
5235 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5237 if (DECL_CONSTRUCTOR_P (decl))
5239 fix_constructors (decl);
5240 saw_ctor = 1;
5244 /* Anonymous class constructor can't be generated that early. */
5245 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5246 craft_constructor (current, NULL_TREE);
5250 /* safe_layout_class just makes sure that we can load a class without
5251 disrupting the current_class, input_file, lineno, etc, information
5252 about the class processed currently. */
5254 void
5255 safe_layout_class (class)
5256 tree class;
5258 tree save_current_class = current_class;
5259 const char *save_input_filename = input_filename;
5260 int save_lineno = lineno;
5262 layout_class (class);
5264 current_class = save_current_class;
5265 input_filename = save_input_filename;
5266 lineno = save_lineno;
5267 CLASS_LOADED_P (class) = 1;
5270 static tree
5271 jdep_resolve_class (dep)
5272 jdep *dep;
5274 tree decl;
5276 if (JDEP_RESOLVED_P (dep))
5277 decl = JDEP_RESOLVED_DECL (dep);
5278 else
5280 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5281 JDEP_DECL (dep), JDEP_WFL (dep));
5282 JDEP_RESOLVED (dep, decl);
5285 if (!decl)
5286 complete_class_report_errors (dep);
5287 else if (PURE_INNER_CLASS_DECL_P (decl))
5288 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5289 return decl;
5292 /* Complete unsatisfied class declaration and their dependencies */
5294 void
5295 java_complete_class ()
5297 tree cclass;
5298 jdeplist *cclassd;
5299 int error_found;
5300 tree type;
5302 /* Process imports */
5303 process_imports ();
5305 /* Rever things so we have the right order */
5306 ctxp->class_list = nreverse (ctxp->class_list);
5307 ctxp->classd_list = reverse_jdep_list (ctxp);
5309 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5310 cclass && cclassd;
5311 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5313 jdep *dep;
5314 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5316 tree decl;
5317 if (!(decl = jdep_resolve_class (dep)))
5318 continue;
5320 /* Now it's time to patch */
5321 switch (JDEP_KIND (dep))
5323 case JDEP_SUPER:
5324 /* Simply patch super */
5325 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5326 continue;
5327 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5328 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5329 break;
5331 case JDEP_FIELD:
5333 /* We do part of the job done in add_field */
5334 tree field_decl = JDEP_DECL (dep);
5335 tree field_type = TREE_TYPE (decl);
5336 if (TREE_CODE (field_type) == RECORD_TYPE)
5337 field_type = promote_type (field_type);
5338 TREE_TYPE (field_decl) = field_type;
5339 DECL_ALIGN (field_decl) = 0;
5340 DECL_USER_ALIGN (field_decl) = 0;
5341 layout_decl (field_decl, 0);
5342 SOURCE_FRONTEND_DEBUG
5343 (("Completed field/var decl `%s' with `%s'",
5344 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5345 IDENTIFIER_POINTER (DECL_NAME (decl))));
5346 break;
5348 case JDEP_METHOD: /* We start patching a method */
5349 case JDEP_METHOD_RETURN:
5350 error_found = 0;
5351 while (1)
5353 if (decl)
5355 type = TREE_TYPE(decl);
5356 if (TREE_CODE (type) == RECORD_TYPE)
5357 type = promote_type (type);
5358 JDEP_APPLY_PATCH (dep, type);
5359 SOURCE_FRONTEND_DEBUG
5360 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5361 "Completing fct `%s' with ret type `%s'":
5362 "Completing arg `%s' with type `%s'"),
5363 IDENTIFIER_POINTER (EXPR_WFL_NODE
5364 (JDEP_DECL_WFL (dep))),
5365 IDENTIFIER_POINTER (DECL_NAME (decl))));
5367 else
5368 error_found = 1;
5369 dep = JDEP_CHAIN (dep);
5370 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5371 break;
5372 else
5373 decl = jdep_resolve_class (dep);
5375 if (!error_found)
5377 tree mdecl = JDEP_DECL (dep), signature;
5378 /* Recompute and reset the signature, check first that
5379 all types are now defined. If they're not,
5380 dont build the signature. */
5381 if (check_method_types_complete (mdecl))
5383 signature = build_java_signature (TREE_TYPE (mdecl));
5384 set_java_signature (TREE_TYPE (mdecl), signature);
5387 else
5388 continue;
5389 break;
5391 case JDEP_INTERFACE:
5392 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5393 JDEP_WFL (dep)))
5394 continue;
5395 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5396 break;
5398 case JDEP_PARM:
5399 case JDEP_VARIABLE:
5400 type = TREE_TYPE(decl);
5401 if (TREE_CODE (type) == RECORD_TYPE)
5402 type = promote_type (type);
5403 JDEP_APPLY_PATCH (dep, type);
5404 break;
5406 case JDEP_TYPE:
5407 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5408 SOURCE_FRONTEND_DEBUG
5409 (("Completing a random type dependency on a '%s' node",
5410 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5411 break;
5413 case JDEP_EXCEPTION:
5414 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5415 SOURCE_FRONTEND_DEBUG
5416 (("Completing `%s' `throws' argument node",
5417 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5418 break;
5420 case JDEP_ANONYMOUS:
5421 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5422 break;
5424 default:
5425 fatal ("Can't handle patch code %d - java_complete_class",
5426 JDEP_KIND (dep));
5430 return;
5433 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5434 array. */
5436 static tree
5437 resolve_class (enclosing, class_type, decl, cl)
5438 tree enclosing, class_type, decl, cl;
5440 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5441 const char *base = name;
5442 tree resolved_type = TREE_TYPE (class_type);
5443 tree resolved_type_decl;
5445 if (resolved_type != NULL_TREE)
5447 tree resolved_type_decl = TYPE_NAME (resolved_type);
5448 if (resolved_type_decl == NULL_TREE
5449 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5451 resolved_type_decl = build_decl (TYPE_DECL,
5452 TYPE_NAME (class_type),
5453 resolved_type);
5455 return resolved_type_decl;
5458 /* 1- Check to see if we have an array. If true, find what we really
5459 want to resolve */
5460 while (name[0] == '[')
5461 name++;
5462 if (base != name)
5464 TYPE_NAME (class_type) = get_identifier (name);
5465 WFL_STRIP_BRACKET (cl, cl);
5468 /* 2- Resolve the bare type */
5469 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5470 decl, cl)))
5471 return NULL_TREE;
5472 resolved_type = TREE_TYPE (resolved_type_decl);
5474 /* 3- If we have and array, reconstruct the array down to its nesting */
5475 if (base != name)
5477 while (base != name)
5479 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5480 resolved_type = promote_type (resolved_type);
5481 resolved_type = build_java_array_type (resolved_type, -1);
5482 CLASS_LOADED_P (resolved_type) = 1;
5483 name--;
5485 /* Build a fake decl for this, since this is what is expected to
5486 be returned. */
5487 resolved_type_decl =
5488 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5489 /* Figure how those two things are important for error report. FIXME */
5490 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5491 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
5492 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
5494 TREE_TYPE (class_type) = resolved_type;
5495 return resolved_type_decl;
5498 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5499 are used to report error messages. */
5501 tree
5502 do_resolve_class (enclosing, class_type, decl, cl)
5503 tree enclosing, class_type, decl, cl;
5505 tree new_class_decl;
5507 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5508 it is changed by find_in_imports{_on_demand} and (but it doesn't
5509 really matter) qualify_and_find */
5511 /* 0- Search in the current class as an inner class */
5513 /* Maybe some code here should be added to load the class or
5514 something, at least if the class isn't an inner class and ended
5515 being loaded from class file. FIXME. */
5516 while (enclosing)
5518 tree name;
5519 tree intermediate;
5521 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5522 return new_class_decl;
5524 intermediate = enclosing;
5525 /* Explore enclosing contexts. */
5526 while (INNER_CLASS_DECL_P (intermediate))
5528 intermediate = DECL_CONTEXT (intermediate);
5529 if ((new_class_decl = find_as_inner_class (intermediate,
5530 class_type, cl)))
5531 return new_class_decl;
5534 /* Now go to the upper classes, bail out if necessary. */
5535 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5536 if (!enclosing || enclosing == object_type_node)
5537 break;
5539 if (TREE_CODE (enclosing) == RECORD_TYPE)
5541 enclosing = TYPE_NAME (enclosing);
5542 continue;
5545 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
5546 BUILD_PTR_FROM_NAME (name, enclosing);
5547 else
5548 name = enclosing;
5549 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5552 /* 1- Check for the type in single imports. This will change
5553 TYPE_NAME() if something relevant is found */
5554 find_in_imports (class_type);
5556 /* 2- And check for the type in the current compilation unit */
5557 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5559 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5560 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5561 load_class (TYPE_NAME (class_type), 0);
5562 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5565 /* 3- Search according to the current package definition */
5566 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5568 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5569 TYPE_NAME (class_type))))
5570 return new_class_decl;
5573 /* 4- Check the import on demands. Don't allow bar.baz to be
5574 imported from foo.* */
5575 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5576 if (find_in_imports_on_demand (class_type))
5577 return NULL_TREE;
5579 /* If found in find_in_imports_on_demant, the type has already been
5580 loaded. */
5581 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5582 return new_class_decl;
5584 /* 5- Try with a name qualified with the package name we've seen so far */
5585 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5587 tree package;
5589 /* If there is a current package (ctxp->package), it's the first
5590 element of package_list and we can skip it. */
5591 for (package = (ctxp->package ?
5592 TREE_CHAIN (package_list) : package_list);
5593 package; package = TREE_CHAIN (package))
5594 if ((new_class_decl = qualify_and_find (class_type,
5595 TREE_PURPOSE (package),
5596 TYPE_NAME (class_type))))
5597 return new_class_decl;
5600 /* 5- Check an other compilation unit that bears the name of type */
5601 load_class (TYPE_NAME (class_type), 0);
5602 if (check_pkg_class_access (TYPE_NAME (class_type),
5603 (cl ? cl : lookup_cl (decl))))
5604 return NULL_TREE;
5606 /* 6- Last call for a resolution */
5607 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5610 static tree
5611 qualify_and_find (class_type, package, name)
5612 tree class_type, package, name;
5614 tree new_qualified = merge_qualified_name (package, name);
5615 tree new_class_decl;
5617 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5618 load_class (new_qualified, 0);
5619 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5621 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5622 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5623 load_class (new_qualified, 0);
5624 TYPE_NAME (class_type) = new_qualified;
5625 return IDENTIFIER_CLASS_VALUE (new_qualified);
5627 return NULL_TREE;
5630 /* Resolve NAME and lay it out (if not done and if not the current
5631 parsed class). Return a decl node. This function is meant to be
5632 called when type resolution is necessary during the walk pass. */
5634 static tree
5635 resolve_and_layout (something, cl)
5636 tree something;
5637 tree cl;
5639 tree decl, decl_type;
5641 /* Don't do that on the current class */
5642 if (something == current_class)
5643 return TYPE_NAME (current_class);
5645 /* Don't do anything for void and other primitive types */
5646 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5647 return NULL_TREE;
5649 /* Pointer types can be reall pointer types or fake pointers. When
5650 finding a real pointer, recheck for primitive types */
5651 if (TREE_CODE (something) == POINTER_TYPE)
5653 if (TREE_TYPE (something))
5655 something = TREE_TYPE (something);
5656 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5657 return NULL_TREE;
5659 else
5660 something = TYPE_NAME (something);
5663 /* Don't do anything for arrays of primitive types */
5664 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5665 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5666 return NULL_TREE;
5668 /* Something might be a WFL */
5669 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5670 something = EXPR_WFL_NODE (something);
5672 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5673 TYPE_DECL or a real TYPE */
5674 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5675 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5676 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5678 if (!(decl = resolve_no_layout (something, cl)))
5679 return NULL_TREE;
5681 /* Resolve and layout if necessary */
5682 decl_type = TREE_TYPE (decl);
5683 layout_class_methods (decl_type);
5684 /* Check methods */
5685 if (CLASS_FROM_SOURCE_P (decl_type))
5686 java_check_methods (decl);
5687 /* Layout the type if necessary */
5688 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5689 safe_layout_class (decl_type);
5691 return decl;
5694 /* Resolve a class, returns its decl but doesn't perform any
5695 layout. The current parsing context is saved and restored */
5697 static tree
5698 resolve_no_layout (name, cl)
5699 tree name, cl;
5701 tree ptr, decl;
5702 BUILD_PTR_FROM_NAME (ptr, name);
5703 java_parser_context_save_global ();
5704 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5705 java_parser_context_restore_global ();
5707 return decl;
5710 /* Called when reporting errors. Skip leader '[' in a complex array
5711 type description that failed to be resolved. */
5713 static const char *
5714 purify_type_name (name)
5715 const char *name;
5717 while (*name && *name == '[')
5718 name++;
5719 return name;
5722 /* The type CURRENT refers to can't be found. We print error messages. */
5724 static void
5725 complete_class_report_errors (dep)
5726 jdep *dep;
5728 const char *name;
5730 if (!JDEP_WFL (dep))
5731 return;
5733 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5734 switch (JDEP_KIND (dep))
5736 case JDEP_SUPER:
5737 parse_error_context
5738 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5739 purify_type_name (name),
5740 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5741 break;
5742 case JDEP_FIELD:
5743 parse_error_context
5744 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5745 purify_type_name (name),
5746 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5747 break;
5748 case JDEP_METHOD: /* Covers arguments */
5749 parse_error_context
5750 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5751 purify_type_name (name),
5752 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5753 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5754 break;
5755 case JDEP_METHOD_RETURN: /* Covers return type */
5756 parse_error_context
5757 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5758 purify_type_name (name),
5759 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5760 break;
5761 case JDEP_INTERFACE:
5762 parse_error_context
5763 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5764 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5765 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5766 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5767 break;
5768 case JDEP_VARIABLE:
5769 parse_error_context
5770 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5771 purify_type_name (IDENTIFIER_POINTER
5772 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5773 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5774 break;
5775 case JDEP_EXCEPTION: /* As specified by `throws' */
5776 parse_error_context
5777 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5778 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5779 break;
5780 default:
5781 /* Fix for -Wall. Just break doing nothing. The error will be
5782 caught later */
5783 break;
5787 /* Return a static string containing the DECL prototype string. If
5788 DECL is a constructor, use the class name instead of the form
5789 <init> */
5791 static const char *
5792 get_printable_method_name (decl)
5793 tree decl;
5795 const char *to_return;
5796 tree name = NULL_TREE;
5798 if (DECL_CONSTRUCTOR_P (decl))
5800 name = DECL_NAME (decl);
5801 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
5804 to_return = lang_printable_name (decl, 0);
5805 if (DECL_CONSTRUCTOR_P (decl))
5806 DECL_NAME (decl) = name;
5808 return to_return;
5811 /* Track method being redefined inside the same class. As a side
5812 effect, set DECL_NAME to an IDENTIFIER (prior entering this
5813 function it's a FWL, so we can track errors more accurately.) */
5815 static int
5816 check_method_redefinition (class, method)
5817 tree class, method;
5819 tree redef, sig;
5821 /* There's no need to verify <clinit> and finit$ */
5822 if (DECL_CLINIT_P (method) || DECL_FINIT_P (method))
5823 return 0;
5825 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
5826 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5828 if (redef == method)
5829 break;
5830 if (DECL_NAME (redef) == DECL_NAME (method)
5831 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
5832 && !DECL_ARTIFICIAL (method))
5834 parse_error_context
5835 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
5836 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5837 get_printable_method_name (redef));
5838 return 1;
5841 return 0;
5844 /* Return 1 if check went ok, 0 otherwise. */
5845 static int
5846 check_abstract_method_definitions (do_interface, class_decl, type)
5847 int do_interface;
5848 tree class_decl, type;
5850 tree class = TREE_TYPE (class_decl);
5851 tree method, end_type;
5852 int ok = 1;
5854 end_type = (do_interface ? object_type_node : type);
5855 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5857 tree other_super, other_method, method_sig, method_name;
5858 int found = 0;
5859 int end_type_reached = 0;
5861 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5862 continue;
5864 /* Now verify that somewhere in between TYPE and CLASS,
5865 abstract method METHOD gets a non abstract definition
5866 that is inherited by CLASS. */
5868 method_sig = build_java_signature (TREE_TYPE (method));
5869 method_name = DECL_NAME (method);
5870 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5871 method_name = EXPR_WFL_NODE (method_name);
5873 other_super = class;
5874 do {
5875 if (other_super == end_type)
5876 end_type_reached = 1;
5878 /* Method search */
5879 for (other_method = TYPE_METHODS (other_super); other_method;
5880 other_method = TREE_CHAIN (other_method))
5882 tree s = build_java_signature (TREE_TYPE (other_method));
5883 tree other_name = DECL_NAME (other_method);
5885 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5886 other_name = EXPR_WFL_NODE (other_name);
5887 if (!DECL_CLINIT_P (other_method)
5888 && !DECL_CONSTRUCTOR_P (other_method)
5889 && method_name == other_name
5890 && method_sig == s
5891 && !METHOD_ABSTRACT (other_method))
5893 found = 1;
5894 break;
5897 other_super = CLASSTYPE_SUPER (other_super);
5898 } while (!end_type_reached);
5900 /* Report that abstract METHOD didn't find an implementation
5901 that CLASS can use. */
5902 if (!found)
5904 char *t = xstrdup (lang_printable_name
5905 (TREE_TYPE (TREE_TYPE (method)), 0));
5906 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5907 tree saved_wfl = NULL_TREE;
5909 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5911 saved_wfl = DECL_NAME (method);
5912 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5915 parse_error_context
5916 (lookup_cl (class_decl),
5917 "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",
5918 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5919 t, lang_printable_name (method, 0),
5920 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5921 "interface" : "class"),
5922 IDENTIFIER_POINTER (ccn),
5923 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5924 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5925 ok = 0;
5926 free (t);
5928 if (saved_wfl)
5929 DECL_NAME (method) = saved_wfl;
5933 if (ok && do_interface)
5935 /* Check for implemented interfaces. */
5936 int i;
5937 tree vector = TYPE_BINFO_BASETYPES (type);
5938 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5940 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5941 ok = check_abstract_method_definitions (1, class_decl, super);
5945 return ok;
5948 /* Check that CLASS_DECL somehow implements all inherited abstract
5949 methods. */
5951 static void
5952 java_check_abstract_method_definitions (class_decl)
5953 tree class_decl;
5955 tree class = TREE_TYPE (class_decl);
5956 tree super, vector;
5957 int i;
5959 if (CLASS_ABSTRACT (class_decl))
5960 return;
5962 /* Check for inherited types */
5963 super = class;
5964 do {
5965 super = CLASSTYPE_SUPER (super);
5966 check_abstract_method_definitions (0, class_decl, super);
5967 } while (super != object_type_node);
5969 /* Check for implemented interfaces. */
5970 vector = TYPE_BINFO_BASETYPES (class);
5971 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5973 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5974 check_abstract_method_definitions (1, class_decl, super);
5978 /* Check all the types method DECL uses and return 1 if all of them
5979 are now complete, 0 otherwise. This is used to check whether its
5980 safe to build a method signature or not. */
5982 static int
5983 check_method_types_complete (decl)
5984 tree decl;
5986 tree type = TREE_TYPE (decl);
5987 tree args;
5989 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5990 return 0;
5992 args = TYPE_ARG_TYPES (type);
5993 if (TREE_CODE (type) == METHOD_TYPE)
5994 args = TREE_CHAIN (args);
5995 for (; args != end_params_node; args = TREE_CHAIN (args))
5996 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5997 return 0;
5999 return 1;
6002 /* Visible interface to check methods contained in CLASS_DECL */
6004 void
6005 java_check_methods (class_decl)
6006 tree class_decl;
6008 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6009 return;
6011 if (CLASS_INTERFACE (class_decl))
6012 java_check_abstract_methods (class_decl);
6013 else
6014 java_check_regular_methods (class_decl);
6016 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6019 /* Check all the methods of CLASS_DECL. Methods are first completed
6020 then checked according to regular method existance rules. If no
6021 constructor for CLASS_DECL were encountered, then build its
6022 declaration. */
6024 static void
6025 java_check_regular_methods (class_decl)
6026 tree class_decl;
6028 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6029 tree method;
6030 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6031 tree found = NULL_TREE;
6032 tree mthrows;
6034 /* It is not necessary to check methods defined in java.lang.Object */
6035 if (class == object_type_node)
6036 return;
6038 if (!TYPE_NVIRTUALS (class))
6039 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6041 /* Should take interfaces into account. FIXME */
6042 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6044 tree sig;
6045 tree method_wfl = DECL_FUNCTION_WFL (method);
6046 int aflags;
6048 /* Check for redefinitions */
6049 if (check_method_redefinition (class, method))
6050 continue;
6052 /* If we see one constructor a mark so we don't generate the
6053 default one. Also skip other verifications: constructors
6054 can't be inherited hence hiden or overriden */
6055 if (DECL_CONSTRUCTOR_P (method))
6057 saw_constructor = 1;
6058 continue;
6061 /* We verify things thrown by the method. They must inherits from
6062 java.lang.Throwable */
6063 for (mthrows = DECL_FUNCTION_THROWS (method);
6064 mthrows; mthrows = TREE_CHAIN (mthrows))
6066 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6067 parse_error_context
6068 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6069 IDENTIFIER_POINTER
6070 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6073 sig = build_java_argument_signature (TREE_TYPE (method));
6074 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6076 /* Inner class can't declare static methods */
6077 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6079 char *t = xstrdup (lang_printable_name (class, 0));
6080 parse_error_context
6081 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6082 lang_printable_name (method, 0), t);
6083 free (t);
6086 /* Nothing overrides or it's a private method. */
6087 if (!found)
6088 continue;
6089 if (METHOD_PRIVATE (found))
6091 found = NULL_TREE;
6092 continue;
6095 /* If `found' is declared in an interface, make sure the
6096 modifier matches. */
6097 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6098 && clinit_identifier_node != DECL_NAME (found)
6099 && !METHOD_PUBLIC (method))
6101 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6102 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6103 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6104 lang_printable_name (method, 0),
6105 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6108 /* Can't override a method with the same name and different return
6109 types. */
6110 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6112 char *t = xstrdup
6113 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6114 parse_error_context
6115 (method_wfl,
6116 "Method `%s' was defined with return type `%s' in class `%s'",
6117 lang_printable_name (found, 0), t,
6118 IDENTIFIER_POINTER
6119 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6120 free (t);
6123 aflags = get_access_flags_from_decl (found);
6125 /* Can't override final. Can't override static. */
6126 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6128 /* Static *can* override static */
6129 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6130 continue;
6131 parse_error_context
6132 (method_wfl,
6133 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6134 (METHOD_FINAL (found) ? "Final" : "Static"),
6135 lang_printable_name (found, 0),
6136 (METHOD_FINAL (found) ? "final" : "static"),
6137 IDENTIFIER_POINTER
6138 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6139 continue;
6142 /* Static method can't override instance method. */
6143 if (METHOD_STATIC (method))
6145 parse_error_context
6146 (method_wfl,
6147 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6148 lang_printable_name (found, 0),
6149 IDENTIFIER_POINTER
6150 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6151 continue;
6154 /* - Overriding/hiding public must be public
6155 - Overriding/hiding protected must be protected or public
6156 - If the overriden or hidden method has default (package)
6157 access, then the overriding or hiding method must not be
6158 private; otherwise, a compile-time error occurs. If
6159 `found' belongs to an interface, things have been already
6160 taken care of. */
6161 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6162 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6163 || (METHOD_PROTECTED (found)
6164 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6165 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6166 && METHOD_PRIVATE (method))))
6168 parse_error_context
6169 (method_wfl,
6170 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6171 (METHOD_PUBLIC (method) ? "public" :
6172 (METHOD_PRIVATE (method) ? "private" : "protected")),
6173 IDENTIFIER_POINTER (DECL_NAME
6174 (TYPE_NAME (DECL_CONTEXT (found)))));
6175 continue;
6178 /* Overriding methods must have compatible `throws' clauses on checked
6179 exceptions, if any */
6180 check_throws_clauses (method, method_wfl, found);
6182 /* Inheriting multiple methods with the same signature. FIXME */
6185 if (!TYPE_NVIRTUALS (class))
6186 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6188 /* Search for inherited abstract method not yet implemented in this
6189 class. */
6190 java_check_abstract_method_definitions (class_decl);
6192 if (!saw_constructor)
6193 fatal ("No constructor found");
6196 /* Return a non zero value if the `throws' clause of METHOD (if any)
6197 is incompatible with the `throws' clause of FOUND (if any). */
6199 static void
6200 check_throws_clauses (method, method_wfl, found)
6201 tree method, method_wfl, found;
6203 tree mthrows, fthrows;
6205 /* Can't check these things with class loaded from bytecode. FIXME */
6206 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6207 return;
6209 for (mthrows = DECL_FUNCTION_THROWS (method);
6210 mthrows; mthrows = TREE_CHAIN (mthrows))
6212 /* We don't verify unchecked expressions */
6213 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6214 continue;
6215 /* Checked expression must be compatible */
6216 for (fthrows = DECL_FUNCTION_THROWS (found);
6217 fthrows; fthrows = TREE_CHAIN (fthrows))
6218 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6219 break;
6220 if (!fthrows)
6222 parse_error_context
6223 (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'",
6224 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6225 lang_printable_name (found, 0),
6226 IDENTIFIER_POINTER
6227 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6232 /* Check abstract method of interface INTERFACE */
6234 static void
6235 java_check_abstract_methods (interface_decl)
6236 tree interface_decl;
6238 int i, n;
6239 tree method, basetype_vec, found;
6240 tree interface = TREE_TYPE (interface_decl);
6242 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6244 /* 2- Check for double definition inside the defining interface */
6245 if (check_method_redefinition (interface, method))
6246 continue;
6248 /* 3- Overriding is OK as far as we preserve the return type and
6249 the thrown exceptions (FIXME) */
6250 found = lookup_java_interface_method2 (interface, method);
6251 if (found)
6253 char *t;
6254 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6255 parse_error_context
6256 (DECL_FUNCTION_WFL (found),
6257 "Method `%s' was defined with return type `%s' in class `%s'",
6258 lang_printable_name (found, 0), t,
6259 IDENTIFIER_POINTER
6260 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6261 free (t);
6262 continue;
6266 /* 4- Inherited methods can't differ by their returned types */
6267 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6268 return;
6269 n = TREE_VEC_LENGTH (basetype_vec);
6270 for (i = 0; i < n; i++)
6272 tree sub_interface_method, sub_interface;
6273 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6274 if (!vec_elt)
6275 continue;
6276 sub_interface = BINFO_TYPE (vec_elt);
6277 for (sub_interface_method = TYPE_METHODS (sub_interface);
6278 sub_interface_method;
6279 sub_interface_method = TREE_CHAIN (sub_interface_method))
6281 found = lookup_java_interface_method2 (interface,
6282 sub_interface_method);
6283 if (found && (found != sub_interface_method))
6285 parse_error_context
6286 (lookup_cl (sub_interface_method),
6287 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6288 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6289 lang_printable_name (found, 0),
6290 IDENTIFIER_POINTER
6291 (DECL_NAME (TYPE_NAME
6292 (DECL_CONTEXT (sub_interface_method)))),
6293 IDENTIFIER_POINTER
6294 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6300 /* Lookup methods in interfaces using their name and partial
6301 signature. Return a matching method only if their types differ. */
6303 static tree
6304 lookup_java_interface_method2 (class, method_decl)
6305 tree class, method_decl;
6307 int i, n;
6308 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6310 if (!basetype_vec)
6311 return NULL_TREE;
6313 n = TREE_VEC_LENGTH (basetype_vec);
6314 for (i = 0; i < n; i++)
6316 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6317 if ((BINFO_TYPE (vec_elt) != object_type_node)
6318 && (to_return =
6319 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6320 return to_return;
6322 for (i = 0; i < n; i++)
6324 to_return = lookup_java_interface_method2
6325 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6326 if (to_return)
6327 return to_return;
6330 return NULL_TREE;
6333 /* Lookup method using their name and partial signature. Return a
6334 matching method only if their types differ. */
6336 static tree
6337 lookup_java_method2 (clas, method_decl, do_interface)
6338 tree clas, method_decl;
6339 int do_interface;
6341 tree method, method_signature, method_name, method_type, name;
6343 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6344 name = DECL_NAME (method_decl);
6345 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6346 EXPR_WFL_NODE (name) : name);
6347 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6349 while (clas != NULL_TREE)
6351 for (method = TYPE_METHODS (clas);
6352 method != NULL_TREE; method = TREE_CHAIN (method))
6354 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6355 tree name = DECL_NAME (method);
6356 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6357 EXPR_WFL_NODE (name) : name) == method_name
6358 && method_sig == method_signature
6359 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6360 return method;
6362 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6364 return NULL_TREE;
6367 /* Return the line that matches DECL line number, and try its best to
6368 position the column number. Used during error reports. */
6370 static tree
6371 lookup_cl (decl)
6372 tree decl;
6374 static tree cl = NULL_TREE;
6375 char *line, *found;
6377 if (!decl)
6378 return NULL_TREE;
6380 if (cl == NULL_TREE)
6382 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6383 ggc_add_tree_root (&cl, 1);
6386 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6387 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6389 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
6390 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6392 found = strstr ((const char *)line,
6393 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6394 if (found)
6395 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6397 return cl;
6400 /* Look for a simple name in the single-type import list */
6402 static tree
6403 find_name_in_single_imports (name)
6404 tree name;
6406 tree node;
6408 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6409 if (TREE_VALUE (node) == name)
6410 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6412 return NULL_TREE;
6415 /* Process all single-type import. */
6417 static int
6418 process_imports ()
6420 tree import;
6421 int error_found;
6423 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6425 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6427 /* Don't load twice something already defined. */
6428 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6429 continue;
6431 while (1)
6433 tree left;
6435 QUALIFIED_P (to_be_found) = 1;
6436 load_class (to_be_found, 0);
6437 error_found =
6438 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6440 /* We found it, we can bail out */
6441 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6442 break;
6444 /* We haven't found it. Maybe we're trying to access an
6445 inner class. The only way for us to know is to try again
6446 after having dropped a qualifier. If we can't break it further,
6447 we have an error. */
6448 if (breakdown_qualified (&left, NULL, to_be_found))
6449 break;
6451 to_be_found = left;
6453 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6455 parse_error_context (TREE_PURPOSE (import),
6456 "Class or interface `%s' not found in import",
6457 IDENTIFIER_POINTER (to_be_found));
6458 return 1;
6460 if (error_found)
6461 return 1;
6463 return 0;
6466 /* Possibly find and mark a class imported by a single-type import
6467 statement. */
6469 static void
6470 find_in_imports (class_type)
6471 tree class_type;
6473 tree import;
6475 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6476 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6478 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6479 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6483 static int
6484 note_possible_classname (name, len)
6485 const char *name;
6486 int len;
6488 tree node;
6489 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6490 len = len - 5;
6491 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6492 len = len - 6;
6493 else
6494 return 0;
6495 node = ident_subst (name, len, "", '/', '.', "");
6496 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6497 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6498 return 1;
6501 /* Read a import directory, gathering potential match for further type
6502 references. Indifferently reads a filesystem or a ZIP archive
6503 directory. */
6505 static void
6506 read_import_dir (wfl)
6507 tree wfl;
6509 tree package_id = EXPR_WFL_NODE (wfl);
6510 const char *package_name = IDENTIFIER_POINTER (package_id);
6511 int package_length = IDENTIFIER_LENGTH (package_id);
6512 DIR *dirp = NULL;
6513 JCF *saved_jcf = current_jcf;
6515 int found = 0;
6516 int k;
6517 void *entry;
6518 struct buffer filename[1];
6521 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6522 return;
6523 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6525 BUFFER_INIT (filename);
6526 buffer_grow (filename, package_length + 100);
6528 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6530 const char *entry_name = jcf_path_name (entry);
6531 int entry_length = strlen (entry_name);
6532 if (jcf_path_is_zipfile (entry))
6534 ZipFile *zipf;
6535 buffer_grow (filename, entry_length);
6536 memcpy (filename->data, entry_name, entry_length - 1);
6537 filename->data[entry_length-1] = '\0';
6538 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6539 if (zipf == NULL)
6540 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6541 else
6543 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6544 BUFFER_RESET (filename);
6545 for (k = 0; k < package_length; k++)
6547 char ch = package_name[k];
6548 *filename->ptr++ = ch == '.' ? '/' : ch;
6550 *filename->ptr++ = '/';
6552 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6554 const char *current_entry = ZIPDIR_FILENAME (zipd);
6555 int current_entry_len = zipd->filename_length;
6557 if (current_entry_len >= BUFFER_LENGTH (filename)
6558 && strncmp (filename->data, current_entry,
6559 BUFFER_LENGTH (filename)) != 0)
6560 continue;
6561 found |= note_possible_classname (current_entry,
6562 current_entry_len);
6566 else
6568 BUFFER_RESET (filename);
6569 buffer_grow (filename, entry_length + package_length + 4);
6570 strcpy (filename->data, entry_name);
6571 filename->ptr = filename->data + entry_length;
6572 for (k = 0; k < package_length; k++)
6574 char ch = package_name[k];
6575 *filename->ptr++ = ch == '.' ? '/' : ch;
6577 *filename->ptr = '\0';
6579 dirp = opendir (filename->data);
6580 if (dirp == NULL)
6581 continue;
6582 *filename->ptr++ = '/';
6583 for (;;)
6585 int len;
6586 const char *d_name;
6587 struct dirent *direntp = readdir (dirp);
6588 if (!direntp)
6589 break;
6590 d_name = direntp->d_name;
6591 len = strlen (direntp->d_name);
6592 buffer_grow (filename, len+1);
6593 strcpy (filename->ptr, d_name);
6594 found |= note_possible_classname (filename->data + entry_length,
6595 package_length+len+1);
6597 if (dirp)
6598 closedir (dirp);
6602 free (filename->data);
6604 /* Here we should have a unified way of retrieving an entry, to be
6605 indexed. */
6606 if (!found)
6608 static int first = 1;
6609 if (first)
6611 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6612 java_error_count++;
6613 first = 0;
6615 else
6616 parse_error_context (wfl, "Package `%s' not found in import",
6617 package_name);
6618 current_jcf = saved_jcf;
6619 return;
6621 current_jcf = saved_jcf;
6624 /* Possibly find a type in the import on demands specified
6625 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6626 entire list, to detected potential double definitions. */
6628 static int
6629 find_in_imports_on_demand (class_type)
6630 tree class_type;
6632 tree node, import, node_to_use = NULL_TREE;
6633 int seen_once = -1;
6634 tree cl = NULL_TREE;
6636 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6638 const char *id_name;
6639 obstack_grow (&temporary_obstack,
6640 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6641 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6642 obstack_1grow (&temporary_obstack, '.');
6643 obstack_grow0 (&temporary_obstack,
6644 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6645 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6646 id_name = obstack_finish (&temporary_obstack);
6648 node = maybe_get_identifier (id_name);
6649 if (node && IS_A_CLASSFILE_NAME (node))
6651 if (seen_once < 0)
6653 cl = TREE_PURPOSE (import);
6654 seen_once = 1;
6655 node_to_use = node;
6657 else
6659 seen_once++;
6660 parse_error_context
6661 (TREE_PURPOSE (import),
6662 "Type `%s' also potentially defined in package `%s'",
6663 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6664 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6669 if (seen_once == 1)
6671 /* Setup lineno so that it refers to the line of the import (in
6672 case we parse a class file and encounter errors */
6673 tree decl;
6674 int saved_lineno = lineno;
6675 lineno = EXPR_WFL_LINENO (cl);
6676 TYPE_NAME (class_type) = node_to_use;
6677 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6678 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6679 /* If there is no DECL set for the class or if the class isn't
6680 loaded and not seen in source yet, the load */
6681 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6682 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6683 load_class (node_to_use, 0);
6684 lineno = saved_lineno;
6685 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6687 else
6688 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6691 /* Add package NAME to the list of package encountered so far. To
6692 speed up class lookup in do_resolve_class, we make sure a
6693 particular package is added only once. */
6695 static void
6696 register_package (name)
6697 tree name;
6699 static struct hash_table _pht, *pht = NULL;
6701 if (!pht)
6703 hash_table_init (&_pht, hash_newfunc,
6704 java_hash_hash_tree_node, java_hash_compare_tree_node);
6705 pht = &_pht;
6708 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6710 package_list = chainon (package_list, build_tree_list (name, NULL));
6711 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6715 static tree
6716 resolve_package (pkg, next)
6717 tree pkg, *next;
6719 tree current, acc;
6720 tree type_name = NULL_TREE;
6721 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6723 /* The trick is to determine when the package name stops and were
6724 the name of something contained in the package starts. Then we
6725 return a fully qualified name of what we want to get. */
6727 /* Do a quick search on well known package names */
6728 if (!strncmp (name, "java.lang.reflect", 17))
6730 *next =
6731 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6732 type_name = lookup_package_type (name, 17);
6734 else if (!strncmp (name, "java.lang", 9))
6736 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6737 type_name = lookup_package_type (name, 9);
6740 /* If we found something here, return */
6741 if (type_name)
6742 return type_name;
6744 *next = EXPR_WFL_QUALIFICATION (pkg);
6746 /* Try to progressively construct a type name */
6747 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6748 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6749 current; current = TREE_CHAIN (current))
6751 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6752 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6754 type_name = acc;
6755 /* resolve_package should be used in a loop, hence we
6756 point at this one to naturally process the next one at
6757 the next iteration. */
6758 *next = current;
6759 break;
6762 return type_name;
6765 static tree
6766 lookup_package_type (name, from)
6767 const char *name;
6768 int from;
6770 char subname [128];
6771 const char *sub = &name[from+1];
6772 while (*sub != '.' && *sub)
6773 sub++;
6774 strncpy (subname, name, sub-name);
6775 subname [sub-name] = '\0';
6776 return get_identifier (subname);
6779 static void
6780 check_inner_class_access (decl, enclosing_decl, cl)
6781 tree decl, enclosing_decl, cl;
6783 int access = 0;
6785 /* We don't issue an error message when CL is null. CL can be null
6786 as a result of processing a JDEP crafted by source_start_java_method
6787 for the purpose of patching its parm decl. But the error would
6788 have been already trapped when fixing the method's signature.
6789 DECL can also be NULL in case of earlier errors. */
6790 if (!decl || !cl)
6791 return;
6793 /* We grant access to private and protected inner classes if the
6794 location from where we're trying to access DECL is an enclosing
6795 context for DECL or if both have a common enclosing context. */
6796 if (CLASS_PRIVATE (decl))
6797 access = 1;
6798 if (CLASS_PROTECTED (decl))
6799 access = 2;
6800 if (!access)
6801 return;
6803 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6804 TREE_TYPE (decl))
6805 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6806 TREE_TYPE (decl)))
6807 return;
6809 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6810 (access == 1 ? "private" : "protected"),
6811 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6812 lang_printable_name (decl, 0));
6815 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6816 access violations were found, 1 otherwise. */
6818 static int
6819 check_pkg_class_access (class_name, cl)
6820 tree class_name;
6821 tree cl;
6823 tree type;
6825 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6826 return 0;
6828 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6829 return 0;
6831 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6833 /* Access to a private class within the same package is
6834 allowed. */
6835 tree l, r;
6836 breakdown_qualified (&l, &r, class_name);
6837 if (l == ctxp->package)
6838 return 0;
6840 parse_error_context
6841 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
6842 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6843 IDENTIFIER_POINTER (class_name));
6844 return 1;
6846 return 0;
6849 /* Local variable declaration. */
6851 static void
6852 declare_local_variables (modifier, type, vlist)
6853 int modifier;
6854 tree type;
6855 tree vlist;
6857 tree decl, current, saved_type;
6858 tree type_wfl = NULL_TREE;
6859 int must_chain = 0;
6860 int final_p = 0;
6862 /* Push a new block if statements were seen between the last time we
6863 pushed a block and now. Keep a cound of block to close */
6864 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
6866 tree body = GET_CURRENT_BLOCK (current_function_decl);
6867 tree b = enter_block ();
6868 BLOCK_EXPR_ORIGIN (b) = body;
6871 if (modifier)
6873 int i;
6874 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
6875 if (modifier == ACC_FINAL)
6876 final_p = 1;
6877 else
6879 parse_error_context
6880 (ctxp->modifier_ctx [i],
6881 "Only `final' is allowed as a local variables modifier");
6882 return;
6886 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6887 hold the TYPE value if a new incomplete has to be created (as
6888 opposed to being found already existing and reused). */
6889 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6891 /* If TYPE is fully resolved and we don't have a reference, make one */
6892 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6894 /* Go through all the declared variables */
6895 for (current = vlist, saved_type = type; current;
6896 current = TREE_CHAIN (current), type = saved_type)
6898 tree other, real_type;
6899 tree wfl = TREE_PURPOSE (current);
6900 tree name = EXPR_WFL_NODE (wfl);
6901 tree init = TREE_VALUE (current);
6903 /* Process NAME, as it may specify extra dimension(s) for it */
6904 type = build_array_from_name (type, type_wfl, name, &name);
6906 /* Variable redefinition check */
6907 if ((other = lookup_name_in_blocks (name)))
6909 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6910 DECL_SOURCE_LINE (other));
6911 continue;
6914 /* Type adjustment. We may have just readjusted TYPE because
6915 the variable specified more dimensions. Make sure we have
6916 a reference if we can and don't have one already. */
6917 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6919 real_type = GET_REAL_TYPE (type);
6920 /* Never layout this decl. This will be done when its scope
6921 will be entered */
6922 decl = build_decl (VAR_DECL, name, real_type);
6923 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
6924 LOCAL_FINAL (decl) = final_p;
6925 BLOCK_CHAIN_DECL (decl);
6927 /* If doing xreferencing, replace the line number with the WFL
6928 compound value */
6929 if (flag_emit_xref)
6930 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6932 /* Don't try to use an INIT statement when an error was found */
6933 if (init && java_error_count)
6934 init = NULL_TREE;
6936 /* Add the initialization function to the current function's code */
6937 if (init)
6939 /* Name might have been readjusted */
6940 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6941 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6942 java_method_add_stmt (current_function_decl,
6943 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6944 init));
6947 /* Setup dependency the type of the decl */
6948 if (must_chain)
6950 jdep *dep;
6951 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
6952 dep = CLASSD_LAST (ctxp->classd_list);
6953 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
6956 SOURCE_FRONTEND_DEBUG (("Defined locals"));
6959 /* Called during parsing. Build decls from argument list. */
6961 static void
6962 source_start_java_method (fndecl)
6963 tree fndecl;
6965 tree tem;
6966 tree parm_decl;
6967 int i;
6969 if (!fndecl)
6970 return;
6972 current_function_decl = fndecl;
6974 /* New scope for the function */
6975 enter_block ();
6976 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
6977 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
6979 tree type = TREE_VALUE (tem);
6980 tree name = TREE_PURPOSE (tem);
6982 /* If type is incomplete. Create an incomplete decl and ask for
6983 the decl to be patched later */
6984 if (INCOMPLETE_TYPE_P (type))
6986 jdep *jdep;
6987 tree real_type = GET_REAL_TYPE (type);
6988 parm_decl = build_decl (PARM_DECL, name, real_type);
6989 type = obtain_incomplete_type (type);
6990 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
6991 jdep = CLASSD_LAST (ctxp->classd_list);
6992 JDEP_MISC (jdep) = name;
6993 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
6995 else
6996 parm_decl = build_decl (PARM_DECL, name, type);
6998 /* Remember if a local variable was declared final (via its
6999 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7000 if (ARG_FINAL_P (tem))
7002 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7003 LOCAL_FINAL (parm_decl) = 1;
7006 BLOCK_CHAIN_DECL (parm_decl);
7008 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7009 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7010 nreverse (tem);
7011 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7012 DECL_MAX_LOCALS (current_function_decl) = i;
7015 /* Called during parsing. Creates an artificial method declaration. */
7017 static tree
7018 create_artificial_method (class, flags, type, name, args)
7019 tree class;
7020 int flags;
7021 tree type, name, args;
7023 tree mdecl;
7025 java_parser_context_save_global ();
7026 lineno = 0;
7027 mdecl = make_node (FUNCTION_TYPE);
7028 TREE_TYPE (mdecl) = type;
7029 TYPE_ARG_TYPES (mdecl) = args;
7030 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7031 java_parser_context_restore_global ();
7032 DECL_ARTIFICIAL (mdecl) = 1;
7033 return mdecl;
7036 /* Starts the body if an artifical method. */
7038 static void
7039 start_artificial_method_body (mdecl)
7040 tree mdecl;
7042 DECL_SOURCE_LINE (mdecl) = 1;
7043 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7044 source_start_java_method (mdecl);
7045 enter_block ();
7048 static void
7049 end_artificial_method_body (mdecl)
7050 tree mdecl;
7052 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7053 It has to be evaluated first. (if mdecl is current_function_decl,
7054 we have an undefined behavior if no temporary variable is used.) */
7055 tree b = exit_block ();
7056 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7057 exit_block ();
7060 /* Called during expansion. Push decls formerly built from argument
7061 list so they're usable during expansion. */
7063 static void
7064 expand_start_java_method (fndecl)
7065 tree fndecl;
7067 tree tem, *ptr;
7069 current_function_decl = fndecl;
7071 if (! quiet_flag)
7072 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
7073 announce_function (fndecl);
7074 if (! quiet_flag)
7075 fprintf (stderr, "]");
7077 pushlevel (1); /* Prepare for a parameter push */
7078 ptr = &DECL_ARGUMENTS (fndecl);
7079 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7080 while (tem)
7082 tree next = TREE_CHAIN (tem);
7083 tree type = TREE_TYPE (tem);
7084 if (PROMOTE_PROTOTYPES
7085 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7086 && INTEGRAL_TYPE_P (type))
7087 type = integer_type_node;
7088 DECL_ARG_TYPE (tem) = type;
7089 layout_decl (tem, 0);
7090 pushdecl (tem);
7091 *ptr = tem;
7092 ptr = &TREE_CHAIN (tem);
7093 tem = next;
7095 *ptr = NULL_TREE;
7096 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7097 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
7100 /* Terminate a function and expand its body. */
7102 static void
7103 source_end_java_method ()
7105 tree fndecl = current_function_decl;
7106 int flag_asynchronous_exceptions = asynchronous_exceptions;
7108 if (!fndecl)
7109 return;
7111 java_parser_context_save_global ();
7112 lineno = ctxp->last_ccb_indent1;
7114 /* Set EH language codes */
7115 java_set_exception_lang_code ();
7117 /* Turn function bodies with only a NOP expr null, so they don't get
7118 generated at all and we won't get warnings when using the -W
7119 -Wall flags. */
7120 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7121 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7123 /* Generate function's code */
7124 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7125 && ! flag_emit_class_files
7126 && ! flag_emit_xref)
7127 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7129 /* pop out of its parameters */
7130 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7131 poplevel (1, 0, 1);
7132 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7134 /* Generate rtl for function exit. */
7135 if (! flag_emit_class_files && ! flag_emit_xref)
7137 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7138 /* Emit catch-finally clauses */
7139 emit_handlers ();
7140 expand_function_end (input_filename, lineno, 0);
7142 /* FIXME: If the current method contains any exception handlers,
7143 force asynchronous_exceptions: this is necessary because signal
7144 handlers in libjava may throw exceptions. This is far from being
7145 a perfect solution, but it's better than doing nothing at all.*/
7146 if (catch_clauses)
7147 asynchronous_exceptions = 1;
7149 /* Run the optimizers and output assembler code for this function. */
7150 rest_of_compilation (fndecl);
7153 current_function_decl = NULL_TREE;
7154 java_parser_context_restore_global ();
7155 asynchronous_exceptions = flag_asynchronous_exceptions;
7158 /* Record EXPR in the current function block. Complements compound
7159 expression second operand if necessary. */
7161 tree
7162 java_method_add_stmt (fndecl, expr)
7163 tree fndecl, expr;
7165 if (!GET_CURRENT_BLOCK (fndecl))
7166 return NULL_TREE;
7167 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7170 static tree
7171 add_stmt_to_block (b, type, stmt)
7172 tree b, type, stmt;
7174 tree body = BLOCK_EXPR_BODY (b), c;
7176 if (java_error_count)
7177 return body;
7179 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7180 return body;
7182 BLOCK_EXPR_BODY (b) = c;
7183 TREE_SIDE_EFFECTS (c) = 1;
7184 return c;
7187 /* Add STMT to EXISTING if possible, otherwise create a new
7188 COMPOUND_EXPR and add STMT to it. */
7190 static tree
7191 add_stmt_to_compound (existing, type, stmt)
7192 tree existing, type, stmt;
7194 if (existing)
7195 return build (COMPOUND_EXPR, type, existing, stmt);
7196 else
7197 return stmt;
7200 void java_layout_seen_class_methods ()
7202 tree previous_list = all_class_list;
7203 tree end = NULL_TREE;
7204 tree current;
7206 while (1)
7208 for (current = previous_list;
7209 current != end; current = TREE_CHAIN (current))
7210 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7212 if (previous_list != all_class_list)
7214 end = previous_list;
7215 previous_list = all_class_list;
7217 else
7218 break;
7222 void
7223 java_reorder_fields ()
7225 static tree stop_reordering = NULL_TREE;
7226 static int initialized_p;
7227 tree current;
7229 /* Register STOP_REORDERING with the garbage collector. */
7230 if (!initialized_p)
7232 ggc_add_tree_root (&stop_reordering, 1);
7233 initialized_p = 1;
7236 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7238 current_class = TREE_TYPE (TREE_VALUE (current));
7240 if (current_class == stop_reordering)
7241 break;
7243 /* Reverse the fields, but leave the dummy field in front.
7244 Fields are already ordered for Object and Class */
7245 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7246 && current_class != class_type_node)
7248 /* If the dummy field is there, reverse the right fields and
7249 just layout the type for proper fields offset */
7250 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7252 tree fields = TYPE_FIELDS (current_class);
7253 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7254 TYPE_SIZE (current_class) = NULL_TREE;
7256 /* We don't have a dummy field, we need to layout the class,
7257 after having reversed the fields */
7258 else
7260 TYPE_FIELDS (current_class) =
7261 nreverse (TYPE_FIELDS (current_class));
7262 TYPE_SIZE (current_class) = NULL_TREE;
7266 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7269 /* Layout the methods of all classes loaded in one way on an
7270 other. Check methods of source parsed classes. Then reorder the
7271 fields and layout the classes or the type of all source parsed
7272 classes */
7274 void
7275 java_layout_classes ()
7277 tree current;
7278 int save_error_count = java_error_count;
7280 /* Layout the methods of all classes seen so far */
7281 java_layout_seen_class_methods ();
7282 java_parse_abort_on_error ();
7283 all_class_list = NULL_TREE;
7285 /* Then check the methods of all parsed classes */
7286 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7287 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7288 java_check_methods (TREE_VALUE (current));
7289 java_parse_abort_on_error ();
7291 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7293 current_class = TREE_TYPE (TREE_VALUE (current));
7294 layout_class (current_class);
7296 /* From now on, the class is considered completely loaded */
7297 CLASS_LOADED_P (current_class) = 1;
7299 /* Error reported by the caller */
7300 if (java_error_count)
7301 return;
7304 /* We might have reloaded classes durign the process of laying out
7305 classes for code generation. We must layout the methods of those
7306 late additions, as constructor checks might use them */
7307 java_layout_seen_class_methods ();
7308 java_parse_abort_on_error ();
7311 /* Expand methods in the current set of classes rememebered for
7312 generation. */
7314 static void
7315 java_complete_expand_classes ()
7317 tree current;
7319 do_not_fold = flag_emit_xref;
7321 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7322 if (!INNER_CLASS_DECL_P (current))
7323 java_complete_expand_class (current);
7326 /* Expand the methods found in OUTER, starting first by OUTER's inner
7327 classes, if any. */
7329 static void
7330 java_complete_expand_class (outer)
7331 tree outer;
7333 tree inner_list;
7335 set_nested_class_simple_name_value (outer, 1); /* Set */
7337 /* We need to go after all inner classes and start expanding them,
7338 starting with most nested ones. We have to do that because nested
7339 classes might add functions to outer classes */
7341 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7342 inner_list; inner_list = TREE_CHAIN (inner_list))
7343 java_complete_expand_class (TREE_PURPOSE (inner_list));
7345 java_complete_expand_methods (outer);
7346 set_nested_class_simple_name_value (outer, 0); /* Reset */
7349 /* Expand methods registered in CLASS_DECL. The general idea is that
7350 we expand regular methods first. This allows us get an estimate on
7351 how outer context local alias fields are really used so we can add
7352 to the constructor just enough code to initialize them properly (it
7353 also lets us generate finit$ correctly.) Then we expand the
7354 constructors and then <clinit>. */
7356 static void
7357 java_complete_expand_methods (class_decl)
7358 tree class_decl;
7360 tree clinit, finit, decl, first_decl;
7362 current_class = TREE_TYPE (class_decl);
7364 /* Find whether the class has final variables */
7365 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7366 if (FIELD_FINAL (decl))
7368 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7369 break;
7372 /* Initialize a new constant pool */
7373 init_outgoing_cpool ();
7375 /* Pre-expand <clinit> to figure whether we really need it or
7376 not. If we do need it, we pre-expand the static fields so they're
7377 ready to be used somewhere else. <clinit> will be fully expanded
7378 after we processed the constructors. */
7379 first_decl = TYPE_METHODS (current_class);
7380 clinit = maybe_generate_pre_expand_clinit (current_class);
7382 /* Then generate finit$ (if we need to) because constructor will
7383 try to use it.*/
7384 if (TYPE_FINIT_STMT_LIST (current_class))
7386 finit = generate_finit (current_class);
7387 java_complete_expand_method (finit);
7390 /* Now do the constructors */
7391 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7393 int no_body;
7395 if (!DECL_CONSTRUCTOR_P (decl))
7396 continue;
7398 no_body = !DECL_FUNCTION_BODY (decl);
7399 /* Don't generate debug info on line zero when expanding a
7400 generated constructor. */
7401 if (no_body)
7402 restore_line_number_status (1);
7404 /* Reset the final local variable assignment flags */
7405 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7406 reset_final_variable_local_assignment_flag (current_class);
7408 java_complete_expand_method (decl);
7410 /* Check for missed out final variable assignment */
7411 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7412 check_final_variable_local_assignment_flag (current_class, decl);
7414 if (no_body)
7415 restore_line_number_status (0);
7418 /* First, do the ordinary methods. */
7419 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7421 /* Skip abstract or native methods -- but do handle native
7422 methods when generating JNI stubs. */
7423 if (METHOD_ABSTRACT (decl)
7424 || (! flag_jni && METHOD_NATIVE (decl))
7425 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7426 continue;
7428 if (METHOD_NATIVE (decl))
7430 tree body = build_jni_stub (decl);
7431 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7434 java_complete_expand_method (decl);
7437 /* If there is indeed a <clinit>, fully expand it now */
7438 if (clinit)
7440 /* Reset the final local variable assignment flags */
7441 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7442 reset_static_final_variable_assignment_flag (current_class);
7443 /* Prevent the use of `this' inside <clinit> */
7444 ctxp->explicit_constructor_p = 1;
7445 java_complete_expand_method (clinit);
7446 ctxp->explicit_constructor_p = 0;
7447 /* Check for missed out static final variable assignment */
7448 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7449 && !CLASS_INTERFACE (class_decl))
7450 check_static_final_variable_assignment_flag (current_class);
7453 /* We might have generated a class$ that we now want to expand */
7454 if (TYPE_DOT_CLASS (current_class))
7455 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7457 /* Now verify constructor circularity (stop after the first one we
7458 prove wrong.) */
7459 if (!CLASS_INTERFACE (class_decl))
7460 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7461 if (DECL_CONSTRUCTOR_P (decl)
7462 && verify_constructor_circularity (decl, decl))
7463 break;
7465 /* Final check on the initialization of final variables. */
7466 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7468 check_final_variable_global_assignment_flag (current_class);
7469 /* If we have an interface, check for uninitialized fields. */
7470 if (CLASS_INTERFACE (class_decl))
7471 check_static_final_variable_assignment_flag (current_class);
7474 /* Save the constant pool. We'll need to restore it later. */
7475 TYPE_CPOOL (current_class) = outgoing_cpool;
7478 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7479 safely used in some other methods/constructors. */
7481 static tree
7482 maybe_generate_pre_expand_clinit (class_type)
7483 tree class_type;
7485 tree current, mdecl;
7487 if (!TYPE_CLINIT_STMT_LIST (class_type))
7488 return NULL_TREE;
7490 /* Go through all static fields and pre expand them */
7491 for (current = TYPE_FIELDS (class_type); current;
7492 current = TREE_CHAIN (current))
7493 if (FIELD_STATIC (current))
7494 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7496 /* Then build the <clinit> method */
7497 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7498 clinit_identifier_node, end_params_node);
7499 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7500 mdecl, NULL_TREE);
7501 start_artificial_method_body (mdecl);
7503 /* We process the list of assignment we produced as the result of
7504 the declaration of initialized static field and add them as
7505 statement to the <clinit> method. */
7506 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7507 current = TREE_CHAIN (current))
7509 tree stmt = current;
7510 /* We build the assignment expression that will initialize the
7511 field to its value. There are strict rules on static
7512 initializers (8.5). FIXME */
7513 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7514 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7515 java_method_add_stmt (mdecl, stmt);
7518 end_artificial_method_body (mdecl);
7520 /* Now we want to place <clinit> as the last method (because we need
7521 it at least for interface so that it doesn't interfere with the
7522 dispatch table based lookup. */
7523 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7525 current = TREE_CHAIN (TYPE_METHODS (class_type));
7526 TYPE_METHODS (class_type) = current;
7528 while (TREE_CHAIN (current))
7529 current = TREE_CHAIN (current);
7531 TREE_CHAIN (current) = mdecl;
7532 TREE_CHAIN (mdecl) = NULL_TREE;
7535 return mdecl;
7538 /* Analyzes a method body and look for something that isn't a
7539 MODIFY_EXPR. */
7541 static int
7542 analyze_clinit_body (bbody)
7543 tree bbody;
7545 while (bbody)
7546 switch (TREE_CODE (bbody))
7548 case BLOCK:
7549 bbody = BLOCK_EXPR_BODY (bbody);
7550 break;
7552 case EXPR_WITH_FILE_LOCATION:
7553 bbody = EXPR_WFL_NODE (bbody);
7554 break;
7556 case COMPOUND_EXPR:
7557 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7558 return 1;
7559 bbody = TREE_OPERAND (bbody, 1);
7560 break;
7562 case MODIFY_EXPR:
7563 bbody = NULL_TREE;
7564 break;
7566 default:
7567 bbody = NULL_TREE;
7568 return 1;
7570 return 0;
7574 /* See whether we could get rid of <clinit>. Criteria are: all static
7575 final fields have constant initial values and the body of <clinit>
7576 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7578 static int
7579 maybe_yank_clinit (mdecl)
7580 tree mdecl;
7582 tree type, current;
7583 tree fbody, bbody;
7584 int found = 0;
7586 if (!DECL_CLINIT_P (mdecl))
7587 return 0;
7589 /* If the body isn't empty, then we keep <clinit>. Note that if
7590 we're emitting classfiles, this isn't enough not to rule it
7591 out. */
7592 fbody = DECL_FUNCTION_BODY (mdecl);
7593 bbody = BLOCK_EXPR_BODY (fbody);
7594 if (bbody && bbody != error_mark_node)
7595 bbody = BLOCK_EXPR_BODY (bbody);
7596 else
7597 return 0;
7598 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7599 return 0;
7601 type = DECL_CONTEXT (mdecl);
7602 current = TYPE_FIELDS (type);
7604 for (current = (current ? TREE_CHAIN (current) : current);
7605 current; current = TREE_CHAIN (current))
7607 tree f_init;
7609 /* We're not interested in non static field */
7610 if (!FIELD_STATIC (current))
7611 continue;
7613 /* Anything that isn't String or a basic type is ruled out -- or
7614 if we know how to deal with it (when doing things natively) we
7615 should generated an empty <clinit> so that SUID are computed
7616 correctly. */
7617 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7618 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7619 break;
7621 f_init = DECL_INITIAL (current);
7622 /* If we're emitting native code, we want static final fields to
7623 have constant initializers. If we don't meet these
7624 conditions, we keep <clinit> */
7625 if (!flag_emit_class_files
7626 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7627 break;
7628 /* If we're emitting bytecode, we want static fields to have
7629 constant initializers or no initializer. If we don't meet
7630 these conditions, we keep <clinit> */
7631 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7632 break;
7635 /* Now we analyze the method body and look for something that
7636 isn't a MODIFY_EXPR */
7637 if (bbody == empty_stmt_node)
7638 found = 0;
7639 else
7640 found = analyze_clinit_body (bbody);
7642 if (current || found)
7643 return 0;
7645 /* Get rid of <clinit> in the class' list of methods */
7646 if (TYPE_METHODS (type) == mdecl)
7647 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7648 else
7649 for (current = TYPE_METHODS (type); current;
7650 current = TREE_CHAIN (current))
7651 if (TREE_CHAIN (current) == mdecl)
7653 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7654 break;
7657 return 1;
7661 /* Complete and expand a method. */
7663 static void
7664 java_complete_expand_method (mdecl)
7665 tree mdecl;
7667 int yank_clinit = 0;
7669 current_function_decl = mdecl;
7670 /* Fix constructors before expanding them */
7671 if (DECL_CONSTRUCTOR_P (mdecl))
7672 fix_constructors (mdecl);
7674 /* Expand functions that have a body */
7675 if (DECL_FUNCTION_BODY (mdecl))
7677 tree fbody = DECL_FUNCTION_BODY (mdecl);
7678 tree block_body = BLOCK_EXPR_BODY (fbody);
7679 tree exception_copy = NULL_TREE;
7680 expand_start_java_method (mdecl);
7681 build_result_decl (mdecl);
7683 current_this
7684 = (!METHOD_STATIC (mdecl) ?
7685 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7687 /* Purge the `throws' list of unchecked exceptions. If we're
7688 doing xref, save a copy of the list and re-install it
7689 later. */
7690 if (flag_emit_xref)
7691 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7693 purge_unchecked_exceptions (mdecl);
7695 /* Install exceptions thrown with `throws' */
7696 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7698 if (block_body != NULL_TREE)
7700 block_body = java_complete_tree (block_body);
7702 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7703 check_for_initialization (block_body);
7704 ctxp->explicit_constructor_p = 0;
7707 BLOCK_EXPR_BODY (fbody) = block_body;
7709 /* If we saw a return but couldn't evaluate it properly, we'll
7710 have an error_mark_node here. */
7711 if (block_body != error_mark_node
7712 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7713 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7714 && !flag_emit_xref)
7715 missing_return_error (current_function_decl);
7717 /* Check wether we could just get rid of clinit, now the picture
7718 is complete. */
7719 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7720 complete_start_java_method (mdecl);
7722 /* Don't go any further if we've found error(s) during the
7723 expansion */
7724 if (!java_error_count && !yank_clinit)
7725 source_end_java_method ();
7726 else
7728 if (java_error_count)
7729 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7730 poplevel (1, 0, 1);
7733 /* Pop the exceptions and sanity check */
7734 POP_EXCEPTIONS();
7735 if (currently_caught_type_list)
7736 fatal ("Exception list non empty - java_complete_expand_method");
7738 if (flag_emit_xref)
7739 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7745 /* This section of the code deals with accessing enclosing context
7746 fields either directly by using the relevant access to this$<n> or
7747 by invoking an access method crafted for that purpose. */
7749 /* Build the necessary access from an inner class to an outer
7750 class. This routine could be optimized to cache previous result
7751 (decl, current_class and returned access). When an access method
7752 needs to be generated, it always takes the form of a read. It might
7753 be later turned into a write by calling outer_field_access_fix. */
7755 static tree
7756 build_outer_field_access (id, decl)
7757 tree id, decl;
7759 tree access = NULL_TREE;
7760 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7761 tree decl_ctx = DECL_CONTEXT (decl);
7763 /* If the immediate enclosing context of the current class is the
7764 field decl's class or inherits from it; build the access as
7765 `this$<n>.<field>'. Note that we will break the `private' barrier
7766 if we're not emitting bytecodes. */
7767 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
7768 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7770 tree thisn = build_current_thisn (current_class);
7771 access = make_qualified_primary (build_wfl_node (thisn),
7772 id, EXPR_WFL_LINECOL (id));
7774 /* Otherwise, generate access methods to outer this and access the
7775 field (either using an access method or by direct access.) */
7776 else
7778 int lc = EXPR_WFL_LINECOL (id);
7780 /* Now we chain the required number of calls to the access$0 to
7781 get a hold to the enclosing instance we need, and then we
7782 build the field access. */
7783 access = build_access_to_thisn (current_class, decl_ctx, lc);
7785 /* If the field is private and we're generating bytecode, then
7786 we generate an access method */
7787 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7789 tree name = build_outer_field_access_methods (decl);
7790 access = build_outer_field_access_expr (lc, decl_ctx,
7791 name, access, NULL_TREE);
7793 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7794 Once again we break the `private' access rule from a foreign
7795 class. */
7796 else
7797 access = make_qualified_primary (access, id, lc);
7799 return resolve_expression_name (access, NULL);
7802 /* Return a non zero value if NODE describes an outer field inner
7803 access. */
7805 static int
7806 outer_field_access_p (type, decl)
7807 tree type, decl;
7809 if (!INNER_CLASS_TYPE_P (type)
7810 || TREE_CODE (decl) != FIELD_DECL
7811 || DECL_CONTEXT (decl) == type)
7812 return 0;
7814 /* If the inner class extends the declaration context of the field
7815 we're try to acces, then this isn't an outer field access */
7816 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7817 return 0;
7819 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7820 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7822 if (type == DECL_CONTEXT (decl))
7823 return 1;
7825 if (!DECL_CONTEXT (TYPE_NAME (type)))
7827 /* Before we give up, see whether the field is inherited from
7828 the enclosing context we're considering. */
7829 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7830 return 1;
7831 break;
7835 return 0;
7838 /* Return a non zero value if NODE represents an outer field inner
7839 access that was been already expanded. As a side effect, it returns
7840 the name of the field being accessed and the argument passed to the
7841 access function, suitable for a regeneration of the access method
7842 call if necessary. */
7844 static int
7845 outer_field_expanded_access_p (node, name, arg_type, arg)
7846 tree node, *name, *arg_type, *arg;
7848 int identified = 0;
7850 if (TREE_CODE (node) != CALL_EXPR)
7851 return 0;
7853 /* Well, gcj generates slightly different tree nodes when compiling
7854 to native or bytecodes. It's the case for function calls. */
7856 if (flag_emit_class_files
7857 && TREE_CODE (node) == CALL_EXPR
7858 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7859 identified = 1;
7860 else if (!flag_emit_class_files)
7862 node = TREE_OPERAND (node, 0);
7864 if (node && TREE_OPERAND (node, 0)
7865 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7867 node = TREE_OPERAND (node, 0);
7868 if (TREE_OPERAND (node, 0)
7869 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7870 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7871 (DECL_NAME (TREE_OPERAND (node, 0)))))
7872 identified = 1;
7876 if (identified && name && arg_type && arg)
7878 tree argument = TREE_OPERAND (node, 1);
7879 *name = DECL_NAME (TREE_OPERAND (node, 0));
7880 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7881 *arg = TREE_VALUE (argument);
7883 return identified;
7886 /* Detect in NODE an outer field read access from an inner class and
7887 transform it into a write with RHS as an argument. This function is
7888 called from the java_complete_lhs when an assignment to a LHS can
7889 be identified. */
7891 static tree
7892 outer_field_access_fix (wfl, node, rhs)
7893 tree wfl, node, rhs;
7895 tree name, arg_type, arg;
7897 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7899 /* At any rate, check whether we're trying to assign a value to
7900 a final. */
7901 tree accessed = (JDECL_P (node) ? node :
7902 (TREE_CODE (node) == COMPONENT_REF ?
7903 TREE_OPERAND (node, 1) : node));
7904 if (check_final_assignment (accessed, wfl))
7905 return error_mark_node;
7907 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7908 arg_type, name, arg, rhs);
7909 return java_complete_tree (node);
7911 return NULL_TREE;
7914 /* Construct the expression that calls an access method:
7915 <type>.access$<n>(<arg1> [, <arg2>]);
7917 ARG2 can be NULL and will be omitted in that case. It will denote a
7918 read access. */
7920 static tree
7921 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7922 int lc;
7923 tree type, access_method_name, arg1, arg2;
7925 tree args, cn, access;
7927 args = arg1 ? arg1 :
7928 build_wfl_node (build_current_thisn (current_class));
7929 args = build_tree_list (NULL_TREE, args);
7931 if (arg2)
7932 args = tree_cons (NULL_TREE, arg2, args);
7934 access = build_method_invocation (build_wfl_node (access_method_name), args);
7935 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7936 return make_qualified_primary (cn, access, lc);
7939 static tree
7940 build_new_access_id ()
7942 static int access_n_counter = 1;
7943 char buffer [128];
7945 sprintf (buffer, "access$%d", access_n_counter++);
7946 return get_identifier (buffer);
7949 /* Create the static access functions for the outer field DECL. We define a
7950 read:
7951 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7952 return inst$.field;
7954 and a write access:
7955 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7956 TREE_TYPE (<field>) value$) {
7957 return inst$.field = value$;
7959 We should have a usage flags on the DECL so we can lazily turn the ones
7960 we're using for code generation. FIXME.
7963 static tree
7964 build_outer_field_access_methods (decl)
7965 tree decl;
7967 tree id, args, stmt, mdecl;
7969 if (FIELD_INNER_ACCESS_P (decl))
7970 return FIELD_INNER_ACCESS (decl);
7972 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7974 /* Create the identifier and a function named after it. */
7975 id = build_new_access_id ();
7977 /* The identifier is marked as bearing the name of a generated write
7978 access function for outer field accessed from inner classes. */
7979 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7981 /* Create the read access */
7982 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7983 TREE_CHAIN (args) = end_params_node;
7984 stmt = make_qualified_primary (build_wfl_node (inst_id),
7985 build_wfl_node (DECL_NAME (decl)), 0);
7986 stmt = build_return (0, stmt);
7987 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7988 TREE_TYPE (decl), id, args, stmt);
7989 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7991 /* Create the write access method. No write access for final variable */
7992 if (!FIELD_FINAL (decl))
7994 args = build_tree_list (inst_id,
7995 build_pointer_type (DECL_CONTEXT (decl)));
7996 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
7997 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
7998 stmt = make_qualified_primary (build_wfl_node (inst_id),
7999 build_wfl_node (DECL_NAME (decl)), 0);
8000 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8001 build_wfl_node (wpv_id)));
8002 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8003 TREE_TYPE (decl), id,
8004 args, stmt);
8006 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8008 /* Return the access name */
8009 return FIELD_INNER_ACCESS (decl) = id;
8012 /* Build an field access method NAME. */
8014 static tree
8015 build_outer_field_access_method (class, type, name, args, body)
8016 tree class, type, name, args, body;
8018 tree saved_current_function_decl, mdecl;
8020 /* Create the method */
8021 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8022 fix_method_argument_names (args, mdecl);
8023 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8025 /* Attach the method body. */
8026 saved_current_function_decl = current_function_decl;
8027 start_artificial_method_body (mdecl);
8028 java_method_add_stmt (mdecl, body);
8029 end_artificial_method_body (mdecl);
8030 current_function_decl = saved_current_function_decl;
8032 return mdecl;
8036 /* This section deals with building access function necessary for
8037 certain kinds of method invocation from inner classes. */
8039 static tree
8040 build_outer_method_access_method (decl)
8041 tree decl;
8043 tree saved_current_function_decl, mdecl;
8044 tree args = NULL_TREE, call_args = NULL_TREE;
8045 tree carg, id, body, class;
8046 char buffer [80];
8047 int parm_id_count = 0;
8049 /* Test this abort with an access to a private field */
8050 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8051 abort ();
8053 /* Check the cache first */
8054 if (DECL_FUNCTION_INNER_ACCESS (decl))
8055 return DECL_FUNCTION_INNER_ACCESS (decl);
8057 class = DECL_CONTEXT (decl);
8059 /* Obtain an access identifier and mark it */
8060 id = build_new_access_id ();
8061 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8063 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8064 /* Create the arguments, as much as the original */
8065 for (; carg && carg != end_params_node;
8066 carg = TREE_CHAIN (carg))
8068 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8069 args = chainon (args, build_tree_list (get_identifier (buffer),
8070 TREE_VALUE (carg)));
8072 args = chainon (args, end_params_node);
8074 /* Create the method */
8075 mdecl = create_artificial_method (class, ACC_STATIC,
8076 TREE_TYPE (TREE_TYPE (decl)), id, args);
8077 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8078 /* There is a potential bug here. We should be able to use
8079 fix_method_argument_names, but then arg names get mixed up and
8080 eventually a constructor will have its this$0 altered and the
8081 outer context won't be assignment properly. The test case is
8082 stub.java FIXME */
8083 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8085 /* Attach the method body. */
8086 saved_current_function_decl = current_function_decl;
8087 start_artificial_method_body (mdecl);
8089 /* The actual method invocation uses the same args. When invoking a
8090 static methods that way, we don't want to skip the first
8091 argument. */
8092 carg = args;
8093 if (!METHOD_STATIC (decl))
8094 carg = TREE_CHAIN (carg);
8095 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8096 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8097 call_args);
8099 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8100 call_args);
8101 if (!METHOD_STATIC (decl))
8102 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8103 body, 0);
8104 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8105 body = build_return (0, body);
8106 java_method_add_stmt (mdecl,body);
8107 end_artificial_method_body (mdecl);
8108 current_function_decl = saved_current_function_decl;
8110 /* Back tag the access function so it know what it accesses */
8111 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8113 /* Tag the current method so it knows it has an access generated */
8114 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8118 /* This section of the code deals with building expressions to access
8119 the enclosing instance of an inner class. The enclosing instance is
8120 kept in a generated field called this$<n>, with <n> being the
8121 inner class nesting level (starting from 0.) */
8123 /* Build an access to a given this$<n>, always chaining access call to
8124 others. Access methods to this$<n> are build on the fly if
8125 necessary. This CAN'T be used to solely access this$<n-1> from
8126 this$<n> (which alway yield to special cases and optimization, see
8127 for example build_outer_field_access). */
8129 static tree
8130 build_access_to_thisn (from, to, lc)
8131 tree from, to;
8132 int lc;
8134 tree access = NULL_TREE;
8136 while (from != to)
8138 if (!access)
8140 access = build_current_thisn (from);
8141 access = build_wfl_node (access);
8143 else
8145 tree access0_wfl, cn;
8147 maybe_build_thisn_access_method (from);
8148 access0_wfl = build_wfl_node (access0_identifier_node);
8149 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8150 EXPR_WFL_LINECOL (access0_wfl) = lc;
8151 access = build_tree_list (NULL_TREE, access);
8152 access = build_method_invocation (access0_wfl, access);
8153 access = make_qualified_primary (cn, access, lc);
8156 /* if FROM isn't an inter class, that's fine, we've done
8157 enough. What we're looking for can be accessed from there. */
8158 from = DECL_CONTEXT (TYPE_NAME (from));
8159 if (!from)
8160 break;
8161 from = TREE_TYPE (from);
8163 return access;
8166 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8167 is returned if nothing needs to be generated. Otherwise, the method
8168 generated and a method decl is returned.
8170 NOTE: These generated methods should be declared in a class file
8171 attribute so that they can't be referred to directly. */
8173 static tree
8174 maybe_build_thisn_access_method (type)
8175 tree type;
8177 tree mdecl, args, stmt, rtype;
8178 tree saved_current_function_decl;
8180 /* If TYPE is a top-level class, no access method is required.
8181 If there already is such an access method, bail out. */
8182 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8183 return NULL_TREE;
8185 /* We generate the method. The method looks like:
8186 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8188 args = build_tree_list (inst_id, build_pointer_type (type));
8189 TREE_CHAIN (args) = end_params_node;
8190 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8191 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8192 access0_identifier_node, args);
8193 fix_method_argument_names (args, mdecl);
8194 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8195 stmt = build_current_thisn (type);
8196 stmt = make_qualified_primary (build_wfl_node (inst_id),
8197 build_wfl_node (stmt), 0);
8198 stmt = build_return (0, stmt);
8200 saved_current_function_decl = current_function_decl;
8201 start_artificial_method_body (mdecl);
8202 java_method_add_stmt (mdecl, stmt);
8203 end_artificial_method_body (mdecl);
8204 current_function_decl = saved_current_function_decl;
8206 CLASS_ACCESS0_GENERATED_P (type) = 1;
8208 return mdecl;
8211 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8212 the first level of innerclassing. this$1 for the next one, etc...
8213 This function can be invoked with TYPE to NULL, available and then
8214 has to count the parser context. */
8216 static tree
8217 build_current_thisn (type)
8218 tree type;
8220 static int saved_i = -1;
8221 static tree saved_thisn = NULL_TREE;
8222 static tree saved_type = NULL_TREE;
8223 static int saved_type_i = 0;
8224 static int initialized_p;
8225 tree decl;
8226 char buffer [80];
8227 int i = 0;
8229 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8230 if (!initialized_p)
8232 ggc_add_tree_root (&saved_thisn, 1);
8233 ggc_add_tree_root (&saved_type, 1);
8234 initialized_p = 1;
8237 if (type)
8239 if (type == saved_type)
8240 i = saved_type_i;
8241 else
8243 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8244 decl; decl = DECL_CONTEXT (decl), i++)
8247 saved_type = type;
8248 saved_type_i = i;
8251 else
8252 i = list_length (GET_CPC_LIST ())-2;
8254 if (i == saved_i)
8255 return saved_thisn;
8257 sprintf (buffer, "this$%d", i);
8258 saved_i = i;
8259 saved_thisn = get_identifier (buffer);
8260 return saved_thisn;
8263 /* Return the assignement to the hidden enclosing context `this$<n>'
8264 by the second incoming parameter to the innerclass constructor. The
8265 form used is `this.this$<n> = this$<n>;'. */
8267 static tree
8268 build_thisn_assign ()
8270 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8272 tree thisn = build_current_thisn (current_class);
8273 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8274 build_wfl_node (thisn), 0);
8275 tree rhs = build_wfl_node (thisn);
8276 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8277 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8279 return NULL_TREE;
8283 /* Building the synthetic `class$' used to implement the `.class' 1.1
8284 extension for non primitive types. This method looks like:
8286 static Class class$(String type) throws NoClassDefFoundError
8288 try {return (java.lang.Class.forName (String));}
8289 catch (ClassNotFoundException e) {
8290 throw new NoClassDefFoundError(e.getMessage());}
8291 } */
8293 static tree
8294 build_dot_class_method (class)
8295 tree class;
8297 #define BWF(S) build_wfl_node (get_identifier ((S)))
8298 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8299 tree args, tmp, saved_current_function_decl, mdecl;
8300 tree stmt, throw_stmt, catch, catch_block, try_block;
8301 tree catch_clause_param;
8302 tree class_not_found_exception, no_class_def_found_error;
8304 static tree get_message_wfl, type_parm_wfl;
8306 if (!get_message_wfl)
8308 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8309 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8310 ggc_add_tree_root (&get_message_wfl, 1);
8311 ggc_add_tree_root (&type_parm_wfl, 1);
8314 /* Build the arguments */
8315 args = build_tree_list (get_identifier ("type$"),
8316 build_pointer_type (string_type_node));
8317 TREE_CHAIN (args) = end_params_node;
8319 /* Build the qualified name java.lang.Class.forName */
8320 tmp = MQN (MQN (MQN (BWF ("java"),
8321 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8323 /* For things we have to catch and throw */
8324 class_not_found_exception =
8325 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8326 no_class_def_found_error =
8327 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8328 load_class (class_not_found_exception, 1);
8329 load_class (no_class_def_found_error, 1);
8331 /* Create the "class$" function */
8332 mdecl = create_artificial_method (class, ACC_STATIC,
8333 build_pointer_type (class_type_node),
8334 get_identifier ("class$"), args);
8335 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8336 no_class_def_found_error);
8338 /* We start by building the try block. We need to build:
8339 return (java.lang.Class.forName (type)); */
8340 stmt = build_method_invocation (tmp,
8341 build_tree_list (NULL_TREE, type_parm_wfl));
8342 stmt = build_return (0, stmt);
8343 /* Put it in a block. That's the try block */
8344 try_block = build_expr_block (stmt, NULL_TREE);
8346 /* Now onto the catch block. We start by building the expression
8347 throwing a new exception:
8348 throw new NoClassDefFoundError (_.getMessage); */
8349 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8350 get_message_wfl, 0);
8351 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8353 /* Build new NoClassDefFoundError (_.getMessage) */
8354 throw_stmt = build_new_invocation
8355 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8356 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8358 /* Build the throw, (it's too early to use BUILD_THROW) */
8359 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8361 /* Build the catch block to encapsulate all this. We begin by
8362 building an decl for the catch clause parameter and link it to
8363 newly created block, the catch block. */
8364 catch_clause_param =
8365 build_decl (VAR_DECL, wpv_id,
8366 build_pointer_type (class_not_found_exception));
8367 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8369 /* We initialize the variable with the exception handler. */
8370 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8371 soft_exceptioninfo_call_node);
8372 add_stmt_to_block (catch_block, NULL_TREE, catch);
8374 /* We add the statement throwing the new exception */
8375 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8377 /* Build a catch expression for all this */
8378 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8380 /* Build the try/catch sequence */
8381 stmt = build_try_statement (0, try_block, catch_block);
8383 fix_method_argument_names (args, mdecl);
8384 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8385 saved_current_function_decl = current_function_decl;
8386 start_artificial_method_body (mdecl);
8387 java_method_add_stmt (mdecl, stmt);
8388 end_artificial_method_body (mdecl);
8389 current_function_decl = saved_current_function_decl;
8390 TYPE_DOT_CLASS (class) = mdecl;
8392 return mdecl;
8395 static tree
8396 build_dot_class_method_invocation (type)
8397 tree type;
8399 tree sig_id, s;
8401 if (TYPE_ARRAY_P (type))
8402 sig_id = build_java_signature (type);
8403 else
8404 sig_id = DECL_NAME (TYPE_NAME (type));
8406 s = build_string (IDENTIFIER_LENGTH (sig_id),
8407 IDENTIFIER_POINTER (sig_id));
8408 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8409 build_tree_list (NULL_TREE, s));
8412 /* This section of the code deals with constructor. */
8414 /* Craft a body for default constructor. Patch existing constructor
8415 bodies with call to super() and field initialization statements if
8416 necessary. */
8418 static void
8419 fix_constructors (mdecl)
8420 tree mdecl;
8422 tree body = DECL_FUNCTION_BODY (mdecl);
8423 tree thisn_assign, compound = NULL_TREE;
8424 tree class_type = DECL_CONTEXT (mdecl);
8426 if (!body)
8428 /* It is an error for the compiler to generate a default
8429 constructor if the superclass doesn't have a constructor that
8430 takes no argument, or the same args for an anonymous class */
8431 if (verify_constructor_super (mdecl))
8433 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8434 tree save = DECL_NAME (mdecl);
8435 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8436 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8437 parse_error_context
8438 (lookup_cl (TYPE_NAME (class_type)),
8439 "No constructor matching `%s' found in class `%s'",
8440 lang_printable_name (mdecl, 0), n);
8441 DECL_NAME (mdecl) = save;
8444 /* The constructor body must be crafted by hand. It's the
8445 constructor we defined when we realize we didn't have the
8446 CLASSNAME() constructor */
8447 start_artificial_method_body (mdecl);
8449 /* Insert an assignment to the this$<n> hidden field, if
8450 necessary */
8451 if ((thisn_assign = build_thisn_assign ()))
8452 java_method_add_stmt (mdecl, thisn_assign);
8454 /* We don't generate a super constructor invocation if we're
8455 compiling java.lang.Object. build_super_invocation takes care
8456 of that. */
8457 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8459 /* Insert the instance initializer block right here, after the
8460 super invocation. */
8461 add_instance_initializer (mdecl);
8463 end_artificial_method_body (mdecl);
8465 /* Search for an explicit constructor invocation */
8466 else
8468 int found = 0;
8469 tree main_block = BLOCK_EXPR_BODY (body);
8471 while (body)
8472 switch (TREE_CODE (body))
8474 case CALL_EXPR:
8475 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8476 body = NULL_TREE;
8477 break;
8478 case COMPOUND_EXPR:
8479 case EXPR_WITH_FILE_LOCATION:
8480 body = TREE_OPERAND (body, 0);
8481 break;
8482 case BLOCK:
8483 body = BLOCK_EXPR_BODY (body);
8484 break;
8485 default:
8486 found = 0;
8487 body = NULL_TREE;
8489 /* The constructor is missing an invocation of super() */
8490 if (!found)
8491 compound = add_stmt_to_compound (compound, NULL_TREE,
8492 build_super_invocation (mdecl));
8494 /* Generate the assignment to this$<n>, if necessary */
8495 if ((thisn_assign = build_thisn_assign ()))
8496 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8498 /* Insert the instance initializer block right here, after the
8499 super invocation. */
8500 add_instance_initializer (mdecl);
8502 /* Fix the constructor main block if we're adding extra stmts */
8503 if (compound)
8505 compound = add_stmt_to_compound (compound, NULL_TREE,
8506 BLOCK_EXPR_BODY (main_block));
8507 BLOCK_EXPR_BODY (main_block) = compound;
8512 /* Browse constructors in the super class, searching for a constructor
8513 that doesn't take any argument. Return 0 if one is found, 1
8514 otherwise. If the current class is an anonymous inner class, look
8515 for something that has the same signature. */
8517 static int
8518 verify_constructor_super (mdecl)
8519 tree mdecl;
8521 tree class = CLASSTYPE_SUPER (current_class);
8522 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8523 tree sdecl;
8525 if (!class)
8526 return 0;
8528 if (ANONYMOUS_CLASS_P (current_class))
8530 tree mdecl_arg_type;
8531 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8532 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8533 if (DECL_CONSTRUCTOR_P (sdecl))
8535 tree m_arg_type;
8536 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8537 if (super_inner)
8538 arg_type = TREE_CHAIN (arg_type);
8539 for (m_arg_type = mdecl_arg_type;
8540 (arg_type != end_params_node
8541 && m_arg_type != end_params_node);
8542 arg_type = TREE_CHAIN (arg_type),
8543 m_arg_type = TREE_CHAIN (m_arg_type))
8544 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
8545 break;
8547 if (arg_type == end_params_node && m_arg_type == end_params_node)
8548 return 0;
8551 else
8553 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8555 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8556 if (super_inner)
8557 arg = TREE_CHAIN (arg);
8558 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8559 return 0;
8562 return 1;
8565 /* Generate code for all context remembered for code generation. */
8567 void
8568 java_expand_classes ()
8570 int save_error_count = 0;
8571 static struct parser_ctxt *saved_ctxp = NULL;
8573 java_parse_abort_on_error ();
8574 if (!(ctxp = ctxp_for_generation))
8575 return;
8576 java_layout_classes ();
8577 java_parse_abort_on_error ();
8579 saved_ctxp = ctxp_for_generation;
8580 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8582 ctxp = ctxp_for_generation;
8583 lang_init_source (2); /* Error msgs have method prototypes */
8584 java_complete_expand_classes (); /* Complete and expand classes */
8585 java_parse_abort_on_error ();
8588 /* Find anonymous classes and expand their constructor, now they
8589 have been fixed. */
8590 for (ctxp_for_generation = saved_ctxp;
8591 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8593 tree current;
8594 ctxp = ctxp_for_generation;
8595 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8597 current_class = TREE_TYPE (current);
8598 if (ANONYMOUS_CLASS_P (current_class))
8600 tree d;
8601 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8603 if (DECL_CONSTRUCTOR_P (d))
8605 restore_line_number_status (1);
8606 java_complete_expand_method (d);
8607 restore_line_number_status (0);
8608 break; /* We now there are no other ones */
8615 /* If we've found error at that stage, don't try to generate
8616 anything, unless we're emitting xrefs or checking the syntax only
8617 (but not using -fsyntax-only for the purpose of generating
8618 bytecode. */
8619 if (java_error_count && !flag_emit_xref
8620 && (!flag_syntax_only && !flag_emit_class_files))
8621 return;
8623 /* Now things are stable, go for generation of the class data. */
8624 for (ctxp_for_generation = saved_ctxp;
8625 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8627 tree current;
8628 ctxp = ctxp_for_generation;
8629 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8631 current_class = TREE_TYPE (current);
8632 outgoing_cpool = TYPE_CPOOL (current_class);
8633 if (flag_emit_class_files)
8634 write_classfile (current_class);
8635 if (flag_emit_xref)
8636 expand_xref (current_class);
8637 else if (! flag_syntax_only)
8638 finish_class ();
8643 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8644 a tree list node containing RIGHT. Fore coming RIGHTs will be
8645 chained to this hook. LOCATION contains the location of the
8646 separating `.' operator. */
8648 static tree
8649 make_qualified_primary (primary, right, location)
8650 tree primary, right;
8651 int location;
8653 tree wfl;
8655 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8656 wfl = build_wfl_wrap (primary, location);
8657 else
8659 wfl = primary;
8660 /* If wfl wasn't qualified, we build a first anchor */
8661 if (!EXPR_WFL_QUALIFICATION (wfl))
8662 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8665 /* And chain them */
8666 EXPR_WFL_LINECOL (right) = location;
8667 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8668 PRIMARY_P (wfl) = 1;
8669 return wfl;
8672 /* Simple merge of two name separated by a `.' */
8674 static tree
8675 merge_qualified_name (left, right)
8676 tree left, right;
8678 tree node;
8679 if (!left && !right)
8680 return NULL_TREE;
8682 if (!left)
8683 return right;
8685 if (!right)
8686 return left;
8688 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8689 IDENTIFIER_LENGTH (left));
8690 obstack_1grow (&temporary_obstack, '.');
8691 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8692 IDENTIFIER_LENGTH (right));
8693 node = get_identifier (obstack_base (&temporary_obstack));
8694 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8695 QUALIFIED_P (node) = 1;
8696 return node;
8699 /* Merge the two parts of a qualified name into LEFT. Set the
8700 location information of the resulting node to LOCATION, usually
8701 inherited from the location information of the `.' operator. */
8703 static tree
8704 make_qualified_name (left, right, location)
8705 tree left, right;
8706 int location;
8708 #ifdef USE_COMPONENT_REF
8709 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8710 EXPR_WFL_LINECOL (node) = location;
8711 return node;
8712 #else
8713 tree left_id = EXPR_WFL_NODE (left);
8714 tree right_id = EXPR_WFL_NODE (right);
8715 tree wfl, merge;
8717 merge = merge_qualified_name (left_id, right_id);
8719 /* Left wasn't qualified and is now qualified */
8720 if (!QUALIFIED_P (left_id))
8722 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8723 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8724 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8727 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8728 EXPR_WFL_LINECOL (wfl) = location;
8729 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8731 EXPR_WFL_NODE (left) = merge;
8732 return left;
8733 #endif
8736 /* Extract the last identifier component of the qualified in WFL. The
8737 last identifier is removed from the linked list */
8739 static tree
8740 cut_identifier_in_qualified (wfl)
8741 tree wfl;
8743 tree q;
8744 tree previous = NULL_TREE;
8745 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8746 if (!TREE_CHAIN (q))
8748 if (!previous)
8749 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
8750 TREE_CHAIN (previous) = NULL_TREE;
8751 return TREE_PURPOSE (q);
8755 /* Resolve the expression name NAME. Return its decl. */
8757 static tree
8758 resolve_expression_name (id, orig)
8759 tree id;
8760 tree *orig;
8762 tree name = EXPR_WFL_NODE (id);
8763 tree decl;
8765 /* 6.5.5.1: Simple expression names */
8766 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8768 /* 15.13.1: NAME can appear within the scope of a local variable
8769 declaration */
8770 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8771 return decl;
8773 /* 15.13.1: NAME can appear within a class declaration */
8774 else
8776 decl = lookup_field_wrapper (current_class, name);
8777 if (decl)
8779 tree access = NULL_TREE;
8780 int fs = FIELD_STATIC (decl);
8782 /* If we're accessing an outer scope local alias, make
8783 sure we change the name of the field we're going to
8784 build access to. */
8785 if (FIELD_LOCAL_ALIAS_USED (decl))
8786 name = DECL_NAME (decl);
8788 /* Instance variable (8.3.1.1) can't appear within
8789 static method, static initializer or initializer for
8790 a static variable. */
8791 if (!fs && METHOD_STATIC (current_function_decl))
8793 static_ref_err (id, name, current_class);
8794 return error_mark_node;
8796 /* Instance variables can't appear as an argument of
8797 an explicit constructor invocation */
8798 if (!fs && ctxp->explicit_constructor_p
8799 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
8801 parse_error_context
8802 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
8803 return error_mark_node;
8806 /* If we're processing an inner class and we're trying
8807 to access a field belonging to an outer class, build
8808 the access to the field */
8809 if (!fs && outer_field_access_p (current_class, decl))
8811 if (CLASS_STATIC (TYPE_NAME (current_class)))
8813 static_ref_err (id, DECL_NAME (decl), current_class);
8814 return error_mark_node;
8816 return build_outer_field_access (id, decl);
8819 /* Otherwise build what it takes to access the field */
8820 access = build_field_ref ((fs ? NULL_TREE : current_this),
8821 DECL_CONTEXT (decl), name);
8822 if (fs && !flag_emit_class_files && !flag_emit_xref)
8823 access = build_class_init (DECL_CONTEXT (access), access);
8824 /* We may be asked to save the real field access node */
8825 if (orig)
8826 *orig = access;
8827 /* And we return what we got */
8828 return access;
8830 /* Fall down to error report on undefined variable */
8833 /* 6.5.5.2 Qualified Expression Names */
8834 else
8836 if (orig)
8837 *orig = NULL_TREE;
8838 qualify_ambiguous_name (id);
8839 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8840 /* 15.10.2: Accessing Superclass Members using super */
8841 return resolve_field_access (id, orig, NULL);
8844 /* We've got an error here */
8845 parse_error_context (id, "Undefined variable `%s'",
8846 IDENTIFIER_POINTER (name));
8848 return error_mark_node;
8851 static void
8852 static_ref_err (wfl, field_id, class_type)
8853 tree wfl, field_id, class_type;
8855 parse_error_context
8856 (wfl,
8857 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8858 IDENTIFIER_POINTER (field_id),
8859 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8862 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8863 We return something suitable to generate the field access. We also
8864 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8865 recipient's address can be null. */
8867 static tree
8868 resolve_field_access (qual_wfl, field_decl, field_type)
8869 tree qual_wfl;
8870 tree *field_decl, *field_type;
8872 int is_static = 0;
8873 tree field_ref;
8874 tree decl, where_found, type_found;
8876 if (resolve_qualified_expression_name (qual_wfl, &decl,
8877 &where_found, &type_found))
8878 return error_mark_node;
8880 /* Resolve the LENGTH field of an array here */
8881 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
8882 && type_found && TYPE_ARRAY_P (type_found)
8883 && ! flag_emit_class_files && ! flag_emit_xref)
8885 tree length = build_java_array_length_access (where_found);
8886 field_ref =
8887 build_java_arraynull_check (type_found, length, int_type_node);
8889 /* In case we're dealing with a static array, we need to
8890 initialize its class before the array length can be fetched.
8891 It's also a good time to create a DECL_RTL for the field if
8892 none already exists, otherwise if the field was declared in a
8893 class found in an external file and hasn't been (and won't
8894 be) accessed for its value, none will be created. */
8895 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8897 build_static_field_ref (where_found);
8898 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8901 /* We might have been trying to resolve field.method(). In which
8902 case, the resolution is over and decl is the answer */
8903 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
8904 field_ref = decl;
8905 else if (JDECL_P (decl))
8907 int static_final_found = 0;
8908 if (!type_found)
8909 type_found = DECL_CONTEXT (decl);
8910 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
8911 if (CLASS_FINAL_VARIABLE_P (decl)
8912 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
8913 && DECL_INITIAL (decl))
8915 /* When called on a FIELD_DECL of the right (primitive)
8916 type, java_complete_tree will try to substitue the decl
8917 for it's initial value. */
8918 field_ref = java_complete_tree (decl);
8919 static_final_found = 1;
8921 else
8922 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8923 NULL_TREE : where_found),
8924 type_found, DECL_NAME (decl));
8925 if (field_ref == error_mark_node)
8926 return error_mark_node;
8927 if (is_static && !static_final_found
8928 && !flag_emit_class_files && !flag_emit_xref)
8929 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
8931 else
8932 field_ref = decl;
8934 if (field_decl)
8935 *field_decl = decl;
8936 if (field_type)
8937 *field_type = (QUAL_DECL_TYPE (decl) ?
8938 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
8939 return field_ref;
8942 /* If NODE is an access to f static field, strip out the class
8943 initialization part and return the field decl, otherwise, return
8944 NODE. */
8946 static tree
8947 strip_out_static_field_access_decl (node)
8948 tree node;
8950 if (TREE_CODE (node) == COMPOUND_EXPR)
8952 tree op1 = TREE_OPERAND (node, 1);
8953 if (TREE_CODE (op1) == COMPOUND_EXPR)
8955 tree call = TREE_OPERAND (op1, 0);
8956 if (TREE_CODE (call) == CALL_EXPR
8957 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8958 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8959 == soft_initclass_node)
8960 return TREE_OPERAND (op1, 1);
8962 else if (JDECL_P (op1))
8963 return op1;
8965 return node;
8968 /* 6.5.5.2: Qualified Expression Names */
8970 static int
8971 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8972 tree wfl;
8973 tree *found_decl, *type_found, *where_found;
8975 int from_type = 0; /* Field search initiated from a type */
8976 int from_super = 0, from_cast = 0, from_qualified_this = 0;
8977 int previous_call_static = 0;
8978 int is_static;
8979 tree decl = NULL_TREE, type = NULL_TREE, q;
8980 /* For certain for of inner class instantiation */
8981 tree saved_current, saved_this;
8982 #define RESTORE_THIS_AND_CURRENT_CLASS \
8983 { current_class = saved_current; current_this = saved_this;}
8985 *type_found = *where_found = NULL_TREE;
8987 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8989 tree qual_wfl = QUAL_WFL (q);
8990 tree ret_decl; /* for EH checking */
8991 int location; /* for EH checking */
8993 /* 15.10.1 Field Access Using a Primary */
8994 switch (TREE_CODE (qual_wfl))
8996 case CALL_EXPR:
8997 case NEW_CLASS_EXPR:
8998 /* If the access to the function call is a non static field,
8999 build the code to access it. */
9000 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9002 decl = maybe_access_field (decl, *where_found,
9003 DECL_CONTEXT (decl));
9004 if (decl == error_mark_node)
9005 return 1;
9008 /* And code for the function call */
9009 if (complete_function_arguments (qual_wfl))
9010 return 1;
9012 /* We might have to setup a new current class and a new this
9013 for the search of an inner class, relative to the type of
9014 a expression resolved as `decl'. The current values are
9015 saved and restored shortly after */
9016 saved_current = current_class;
9017 saved_this = current_this;
9018 if (decl
9019 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9020 || from_qualified_this))
9022 /* If we still have `from_qualified_this', we have the form
9023 <T>.this.f() and we need to build <T>.this */
9024 if (from_qualified_this)
9026 decl = build_access_to_thisn (current_class, type, 0);
9027 decl = java_complete_tree (decl);
9028 type = TREE_TYPE (TREE_TYPE (decl));
9030 current_class = type;
9031 current_this = decl;
9032 from_qualified_this = 0;
9035 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9036 CALL_USING_SUPER (qual_wfl) = 1;
9037 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9038 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9039 *where_found = patch_method_invocation (qual_wfl, decl, type,
9040 &is_static, &ret_decl);
9041 if (*where_found == error_mark_node)
9043 RESTORE_THIS_AND_CURRENT_CLASS;
9044 return 1;
9046 *type_found = type = QUAL_DECL_TYPE (*where_found);
9048 /* If we're creating an inner class instance, check for that
9049 an enclosing instance is in scope */
9050 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9051 && INNER_ENCLOSING_SCOPE_CHECK (type))
9053 parse_error_context
9054 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9055 lang_printable_name (type, 0),
9056 (!current_this ? "" :
9057 "; an explicit one must be provided when creating this inner class"));
9058 RESTORE_THIS_AND_CURRENT_CLASS;
9059 return 1;
9062 /* In case we had to change then to resolve a inner class
9063 instantiation using a primary qualified by a `new' */
9064 RESTORE_THIS_AND_CURRENT_CLASS;
9066 /* EH check. No check on access$<n> functions */
9067 if (location
9068 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9069 (DECL_NAME (current_function_decl)))
9070 check_thrown_exceptions (location, ret_decl);
9072 /* If the previous call was static and this one is too,
9073 build a compound expression to hold the two (because in
9074 that case, previous function calls aren't transported as
9075 forcoming function's argument. */
9076 if (previous_call_static && is_static)
9078 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9079 TREE_SIDE_EFFECTS (decl) = 1;
9081 else
9083 previous_call_static = is_static;
9084 decl = *where_found;
9086 from_type = 0;
9087 continue;
9089 case NEW_ARRAY_EXPR:
9090 case NEW_ANONYMOUS_ARRAY_EXPR:
9091 *where_found = decl = java_complete_tree (qual_wfl);
9092 if (decl == error_mark_node)
9093 return 1;
9094 *type_found = type = QUAL_DECL_TYPE (decl);
9095 CLASS_LOADED_P (type) = 1;
9096 continue;
9098 case CONVERT_EXPR:
9099 *where_found = decl = java_complete_tree (qual_wfl);
9100 if (decl == error_mark_node)
9101 return 1;
9102 *type_found = type = QUAL_DECL_TYPE (decl);
9103 from_cast = 1;
9104 continue;
9106 case CONDITIONAL_EXPR:
9107 case STRING_CST:
9108 case MODIFY_EXPR:
9109 *where_found = decl = java_complete_tree (qual_wfl);
9110 if (decl == error_mark_node)
9111 return 1;
9112 *type_found = type = QUAL_DECL_TYPE (decl);
9113 continue;
9115 case ARRAY_REF:
9116 /* If the access to the function call is a non static field,
9117 build the code to access it. */
9118 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9120 decl = maybe_access_field (decl, *where_found, type);
9121 if (decl == error_mark_node)
9122 return 1;
9124 /* And code for the array reference expression */
9125 decl = java_complete_tree (qual_wfl);
9126 if (decl == error_mark_node)
9127 return 1;
9128 type = QUAL_DECL_TYPE (decl);
9129 continue;
9131 case PLUS_EXPR:
9132 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9133 return 1;
9134 if ((type = patch_string (decl)))
9135 decl = type;
9136 *where_found = QUAL_RESOLUTION (q) = decl;
9137 *type_found = type = TREE_TYPE (decl);
9138 break;
9140 case CLASS_LITERAL:
9141 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9142 return 1;
9143 *where_found = QUAL_RESOLUTION (q) = decl;
9144 *type_found = type = TREE_TYPE (decl);
9145 break;
9147 default:
9148 /* Fix for -Wall Just go to the next statement. Don't
9149 continue */
9150 break;
9153 /* If we fall here, we weren't processing a (static) function call. */
9154 previous_call_static = 0;
9156 /* It can be the keyword THIS */
9157 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9159 if (!current_this)
9161 parse_error_context
9162 (wfl, "Keyword `this' used outside allowed context");
9163 return 1;
9165 if (ctxp->explicit_constructor_p
9166 && type == current_class)
9168 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9169 return 1;
9171 /* We have to generate code for intermediate acess */
9172 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9174 *where_found = decl = current_this;
9175 *type_found = type = QUAL_DECL_TYPE (decl);
9177 /* We're trying to access the this from somewhere else. Make sure
9178 it's allowed before doing so. */
9179 else
9181 if (!enclosing_context_p (type, current_class))
9183 char *p = xstrdup (lang_printable_name (type, 0));
9184 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9185 p, p,
9186 lang_printable_name (current_class, 0));
9187 free (p);
9188 return 1;
9190 from_qualified_this = 1;
9191 /* If there's nothing else after that, we need to
9192 produce something now, otherwise, the section of the
9193 code that needs to produce <T>.this will generate
9194 what is necessary. */
9195 if (!TREE_CHAIN (q))
9197 decl = build_access_to_thisn (current_class, type, 0);
9198 *where_found = decl = java_complete_tree (decl);
9199 *type_found = type = TREE_TYPE (decl);
9203 from_type = 0;
9204 continue;
9207 /* 15.10.2 Accessing Superclass Members using SUPER */
9208 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9210 tree node;
9211 /* Check on the restricted use of SUPER */
9212 if (METHOD_STATIC (current_function_decl)
9213 || current_class == object_type_node)
9215 parse_error_context
9216 (wfl, "Keyword `super' used outside allowed context");
9217 return 1;
9219 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9220 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9221 CLASSTYPE_SUPER (current_class),
9222 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9223 *where_found = decl = java_complete_tree (node);
9224 if (decl == error_mark_node)
9225 return 1;
9226 *type_found = type = QUAL_DECL_TYPE (decl);
9227 from_super = from_type = 1;
9228 continue;
9231 /* 15.13.1: Can't search for field name in packages, so we
9232 assume a variable/class name was meant. */
9233 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9235 tree name = resolve_package (wfl, &q);
9236 if (name)
9238 tree list;
9239 *where_found = decl = resolve_no_layout (name, qual_wfl);
9240 /* We want to be absolutely sure that the class is laid
9241 out. We're going to search something inside it. */
9242 *type_found = type = TREE_TYPE (decl);
9243 layout_class (type);
9244 from_type = 1;
9246 /* Fix them all the way down, if any are left. */
9247 if (q)
9249 list = TREE_CHAIN (q);
9250 while (list)
9252 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9253 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9254 list = TREE_CHAIN (list);
9258 else
9260 if (from_super || from_cast)
9261 parse_error_context
9262 ((from_cast ? qual_wfl : wfl),
9263 "No variable `%s' defined in class `%s'",
9264 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9265 lang_printable_name (type, 0));
9266 else
9267 parse_error_context
9268 (qual_wfl, "Undefined variable or class name: `%s'",
9269 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9270 return 1;
9274 /* We have a type name. It's been already resolved when the
9275 expression was qualified. */
9276 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9278 if (!(decl = QUAL_RESOLUTION (q)))
9279 return 1; /* Error reported already */
9281 /* Sneak preview. If next we see a `new', we're facing a
9282 qualification with resulted in a type being selected
9283 instead of a field. Report the error */
9284 if(TREE_CHAIN (q)
9285 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9287 parse_error_context (qual_wfl, "Undefined variable `%s'",
9288 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9289 return 1;
9292 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9294 parse_error_context
9295 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9296 java_accstring_lookup (get_access_flags_from_decl (decl)),
9297 GET_TYPE_NAME (type),
9298 IDENTIFIER_POINTER (DECL_NAME (decl)),
9299 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9300 return 1;
9302 check_deprecation (qual_wfl, decl);
9304 type = TREE_TYPE (decl);
9305 from_type = 1;
9307 /* We resolve and expression name */
9308 else
9310 tree field_decl = NULL_TREE;
9312 /* If there exists an early resolution, use it. That occurs
9313 only once and we know that there are more things to
9314 come. Don't do that when processing something after SUPER
9315 (we need more thing to be put in place below */
9316 if (!from_super && QUAL_RESOLUTION (q))
9318 decl = QUAL_RESOLUTION (q);
9319 if (!type)
9321 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9323 if (current_this)
9324 *where_found = current_this;
9325 else
9327 static_ref_err (qual_wfl, DECL_NAME (decl),
9328 current_class);
9329 return 1;
9331 if (outer_field_access_p (current_class, decl))
9332 decl = build_outer_field_access (qual_wfl, decl);
9334 else
9336 *where_found = TREE_TYPE (decl);
9337 if (TREE_CODE (*where_found) == POINTER_TYPE)
9338 *where_found = TREE_TYPE (*where_found);
9343 /* We have to search for a field, knowing the type of its
9344 container. The flag FROM_TYPE indicates that we resolved
9345 the last member of the expression as a type name, which
9346 means that for the resolution of this field, we'll look
9347 for other errors than if it was resolved as a member of
9348 an other field. */
9349 else
9351 int is_static;
9352 tree field_decl_type; /* For layout */
9354 if (!from_type && !JREFERENCE_TYPE_P (type))
9356 parse_error_context
9357 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9358 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9359 lang_printable_name (type, 0),
9360 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9361 return 1;
9364 field_decl = lookup_field_wrapper (type,
9365 EXPR_WFL_NODE (qual_wfl));
9367 /* Maybe what we're trying to access an inner class. */
9368 if (!field_decl)
9370 tree ptr, inner_decl;
9372 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9373 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9374 if (inner_decl)
9376 check_inner_class_access (inner_decl, decl, qual_wfl);
9377 type = TREE_TYPE (inner_decl);
9378 decl = inner_decl;
9379 from_type = 1;
9380 continue;
9384 if (field_decl == NULL_TREE)
9386 parse_error_context
9387 (qual_wfl, "No variable `%s' defined in type `%s'",
9388 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9389 GET_TYPE_NAME (type));
9390 return 1;
9392 if (field_decl == error_mark_node)
9393 return 1;
9395 /* Layout the type of field_decl, since we may need
9396 it. Don't do primitive types or loaded classes. The
9397 situation of non primitive arrays may not handled
9398 properly here. FIXME */
9399 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9400 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9401 else
9402 field_decl_type = TREE_TYPE (field_decl);
9403 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9404 && !CLASS_LOADED_P (field_decl_type)
9405 && !TYPE_ARRAY_P (field_decl_type))
9406 resolve_and_layout (field_decl_type, NULL_TREE);
9407 if (TYPE_ARRAY_P (field_decl_type))
9408 CLASS_LOADED_P (field_decl_type) = 1;
9410 /* Check on accessibility here */
9411 if (not_accessible_p (type, field_decl, from_super))
9413 parse_error_context
9414 (qual_wfl,
9415 "Can't access %s field `%s.%s' from `%s'",
9416 java_accstring_lookup
9417 (get_access_flags_from_decl (field_decl)),
9418 GET_TYPE_NAME (type),
9419 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9420 IDENTIFIER_POINTER
9421 (DECL_NAME (TYPE_NAME (current_class))));
9422 return 1;
9424 check_deprecation (qual_wfl, field_decl);
9426 /* There are things to check when fields are accessed
9427 from type. There are no restrictions on a static
9428 declaration of the field when it is accessed from an
9429 interface */
9430 is_static = FIELD_STATIC (field_decl);
9431 if (!from_super && from_type
9432 && !TYPE_INTERFACE_P (type)
9433 && !is_static
9434 && (current_function_decl
9435 && METHOD_STATIC (current_function_decl)))
9437 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9438 return 1;
9440 from_cast = from_super = 0;
9442 /* It's an access from a type but it isn't static, we
9443 make it relative to `this'. */
9444 if (!is_static && from_type)
9445 decl = current_this;
9447 /* If we need to generate something to get a proper
9448 handle on what this field is accessed from, do it
9449 now. */
9450 if (!is_static)
9452 decl = maybe_access_field (decl, *where_found, *type_found);
9453 if (decl == error_mark_node)
9454 return 1;
9457 /* We want to keep the location were found it, and the type
9458 we found. */
9459 *where_found = decl;
9460 *type_found = type;
9462 /* Generate the correct expression for field access from
9463 qualified this */
9464 if (from_qualified_this)
9466 field_decl = build_outer_field_access (qual_wfl, field_decl);
9467 from_qualified_this = 0;
9470 /* This is the decl found and eventually the next one to
9471 search from */
9472 decl = field_decl;
9474 from_type = 0;
9475 type = QUAL_DECL_TYPE (decl);
9477 /* Sneak preview. If decl is qualified by a `new', report
9478 the error here to be accurate on the peculiar construct */
9479 if (TREE_CHAIN (q)
9480 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9481 && !JREFERENCE_TYPE_P (type))
9483 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9484 lang_printable_name (type, 0));
9485 return 1;
9488 /* `q' might have changed due to a after package resolution
9489 re-qualification */
9490 if (!q)
9491 break;
9493 *found_decl = decl;
9494 return 0;
9497 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9498 can't be accessed from REFERENCE (a record type). This should be
9499 used when decl is a field or a method.*/
9501 static int
9502 not_accessible_p (reference, member, from_super)
9503 tree reference, member;
9504 int from_super;
9506 int access_flag = get_access_flags_from_decl (member);
9508 /* Inner classes are processed by check_inner_class_access */
9509 if (INNER_CLASS_TYPE_P (reference))
9510 return 0;
9512 /* Access always granted for members declared public */
9513 if (access_flag & ACC_PUBLIC)
9514 return 0;
9516 /* Check access on protected members */
9517 if (access_flag & ACC_PROTECTED)
9519 /* Access granted if it occurs from within the package
9520 containing the class in which the protected member is
9521 declared */
9522 if (class_in_current_package (DECL_CONTEXT (member)))
9523 return 0;
9525 /* If accessed with the form `super.member', then access is granted */
9526 if (from_super)
9527 return 0;
9529 /* Otherwise, access is granted if occuring from the class where
9530 member is declared or a subclass of it. Find the right
9531 context to perform the check */
9532 if (PURE_INNER_CLASS_TYPE_P (reference))
9534 while (INNER_CLASS_TYPE_P (reference))
9536 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9537 return 0;
9538 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9541 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9542 return 0;
9543 return 1;
9546 /* Check access on private members. Access is granted only if it
9547 occurs from within the class in which it is declared -- that does
9548 it for innerclasses too. */
9549 if (access_flag & ACC_PRIVATE)
9551 if (reference == DECL_CONTEXT (member))
9552 return 0;
9553 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9554 return 0;
9555 return 1;
9558 /* Default access are permitted only when occuring within the
9559 package in which the type (REFERENCE) is declared. In other words,
9560 REFERENCE is defined in the current package */
9561 if (ctxp->package)
9562 return !class_in_current_package (reference);
9564 /* Otherwise, access is granted */
9565 return 0;
9568 /* Test deprecated decl access. */
9569 static void
9570 check_deprecation (wfl, decl)
9571 tree wfl, decl;
9573 const char *file = DECL_SOURCE_FILE (decl);
9574 /* Complain if the field is deprecated and the file it was defined
9575 in isn't compiled at the same time the file which contains its
9576 use is */
9577 if (DECL_DEPRECATED (decl)
9578 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9580 char the [20];
9581 switch (TREE_CODE (decl))
9583 case FUNCTION_DECL:
9584 strcpy (the, "method");
9585 break;
9586 case FIELD_DECL:
9587 strcpy (the, "field");
9588 break;
9589 case TYPE_DECL:
9590 strcpy (the, "class");
9591 break;
9592 default:
9593 fatal ("unexpected DECL code - check_deprecation");
9595 parse_warning_context
9596 (wfl, "The %s `%s' in class `%s' has been deprecated",
9597 the, lang_printable_name (decl, 0),
9598 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9602 /* Returns 1 if class was declared in the current package, 0 otherwise */
9604 static int
9605 class_in_current_package (class)
9606 tree class;
9608 static tree cache = NULL_TREE;
9609 int qualified_flag;
9610 tree left;
9612 if (cache == class)
9613 return 1;
9615 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9617 /* If the current package is empty and the name of CLASS is
9618 qualified, class isn't in the current package. If there is a
9619 current package and the name of the CLASS is not qualified, class
9620 isn't in the current package */
9621 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9622 return 0;
9624 /* If there is not package and the name of CLASS isn't qualified,
9625 they belong to the same unnamed package */
9626 if (!ctxp->package && !qualified_flag)
9627 return 1;
9629 /* Compare the left part of the name of CLASS with the package name */
9630 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9631 if (ctxp->package == left)
9633 static int initialized_p;
9634 /* Register CACHE with the garbage collector. */
9635 if (!initialized_p)
9637 ggc_add_tree_root (&cache, 1);
9638 initialized_p = 1;
9641 cache = class;
9642 return 1;
9644 return 0;
9647 /* This function may generate code to access DECL from WHERE. This is
9648 done only if certain conditions meet. */
9650 static tree
9651 maybe_access_field (decl, where, type)
9652 tree decl, where, type;
9654 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9655 && !FIELD_STATIC (decl))
9656 decl = build_field_ref (where ? where : current_this,
9657 (type ? type : DECL_CONTEXT (decl)),
9658 DECL_NAME (decl));
9659 return decl;
9662 /* Build a method invocation, by patching PATCH. If non NULL
9663 and according to the situation, PRIMARY and WHERE may be
9664 used. IS_STATIC is set to 1 if the invoked function is static. */
9666 static tree
9667 patch_method_invocation (patch, primary, where, is_static, ret_decl)
9668 tree patch, primary, where;
9669 int *is_static;
9670 tree *ret_decl;
9672 tree wfl = TREE_OPERAND (patch, 0);
9673 tree args = TREE_OPERAND (patch, 1);
9674 tree name = EXPR_WFL_NODE (wfl);
9675 tree list;
9676 int is_static_flag = 0;
9677 int is_super_init = 0;
9678 tree this_arg = NULL_TREE;
9680 /* Should be overriden if everything goes well. Otherwise, if
9681 something fails, it should keep this value. It stop the
9682 evaluation of a bogus assignment. See java_complete_tree,
9683 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9684 evaluating an assignment */
9685 TREE_TYPE (patch) = error_mark_node;
9687 /* Since lookup functions are messing with line numbers, save the
9688 context now. */
9689 java_parser_context_save_global ();
9691 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9693 /* Resolution of qualified name, excluding constructors */
9694 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9696 tree identifier, identifier_wfl, type, resolved;
9697 /* Extract the last IDENTIFIER of the qualified
9698 expression. This is a wfl and we will use it's location
9699 data during error report. */
9700 identifier_wfl = cut_identifier_in_qualified (wfl);
9701 identifier = EXPR_WFL_NODE (identifier_wfl);
9703 /* Given the context, IDENTIFIER is syntactically qualified
9704 as a MethodName. We need to qualify what's before */
9705 qualify_ambiguous_name (wfl);
9706 resolved = resolve_field_access (wfl, NULL, NULL);
9708 if (resolved == error_mark_node)
9709 PATCH_METHOD_RETURN_ERROR ();
9711 type = GET_SKIP_TYPE (resolved);
9712 resolve_and_layout (type, NULL_TREE);
9714 if (JPRIMITIVE_TYPE_P (type))
9716 parse_error_context
9717 (identifier_wfl,
9718 "Can't invoke a method on primitive type `%s'",
9719 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9720 PATCH_METHOD_RETURN_ERROR ();
9723 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9724 args = nreverse (args);
9726 /* We're resolving a call from a type */
9727 if (TREE_CODE (resolved) == TYPE_DECL)
9729 if (CLASS_INTERFACE (resolved))
9731 parse_error_context
9732 (identifier_wfl,
9733 "Can't make static reference to method `%s' in interface `%s'",
9734 IDENTIFIER_POINTER (identifier),
9735 IDENTIFIER_POINTER (name));
9736 PATCH_METHOD_RETURN_ERROR ();
9738 if (list && !METHOD_STATIC (list))
9740 char *fct_name = xstrdup (lang_printable_name (list, 0));
9741 parse_error_context
9742 (identifier_wfl,
9743 "Can't make static reference to method `%s %s' in class `%s'",
9744 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9745 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9746 free (fct_name);
9747 PATCH_METHOD_RETURN_ERROR ();
9750 else
9751 this_arg = primary = resolved;
9753 /* IDENTIFIER_WFL will be used to report any problem further */
9754 wfl = identifier_wfl;
9756 /* Resolution of simple names, names generated after a primary: or
9757 constructors */
9758 else
9760 tree class_to_search = NULL_TREE;
9761 int lc; /* Looking for Constructor */
9763 /* We search constructor in their target class */
9764 if (CALL_CONSTRUCTOR_P (patch))
9766 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9767 class_to_search = EXPR_WFL_NODE (wfl);
9768 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9769 this_identifier_node)
9770 class_to_search = NULL_TREE;
9771 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9772 super_identifier_node)
9774 is_super_init = 1;
9775 if (CLASSTYPE_SUPER (current_class))
9776 class_to_search =
9777 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9778 else
9780 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
9781 PATCH_METHOD_RETURN_ERROR ();
9785 /* Class to search is NULL if we're searching the current one */
9786 if (class_to_search)
9788 class_to_search = resolve_and_layout (class_to_search, wfl);
9790 if (!class_to_search)
9792 parse_error_context
9793 (wfl, "Class `%s' not found in type declaration",
9794 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9795 PATCH_METHOD_RETURN_ERROR ();
9798 /* Can't instantiate an abstract class, but we can
9799 invoke it's constructor. It's use within the `new'
9800 context is denied here. */
9801 if (CLASS_ABSTRACT (class_to_search)
9802 && TREE_CODE (patch) == NEW_CLASS_EXPR)
9804 parse_error_context
9805 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9806 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9807 PATCH_METHOD_RETURN_ERROR ();
9810 class_to_search = TREE_TYPE (class_to_search);
9812 else
9813 class_to_search = current_class;
9814 lc = 1;
9816 /* This is a regular search in the local class, unless an
9817 alternate class is specified. */
9818 else
9820 class_to_search = (where ? where : current_class);
9821 lc = 0;
9824 /* NAME is a simple identifier or comes from a primary. Search
9825 in the class whose declaration contain the method being
9826 invoked. */
9827 resolve_and_layout (class_to_search, NULL_TREE);
9829 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
9830 /* Don't continue if no method were found, as the next statement
9831 can't be executed then. */
9832 if (!list)
9833 PATCH_METHOD_RETURN_ERROR ();
9835 /* Check for static reference if non static methods */
9836 if (check_for_static_method_reference (wfl, patch, list,
9837 class_to_search, primary))
9838 PATCH_METHOD_RETURN_ERROR ();
9840 /* Check for inner classes creation from illegal contexts */
9841 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9842 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9843 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9845 parse_error_context
9846 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9847 lang_printable_name (class_to_search, 0),
9848 (!current_this ? "" :
9849 "; an explicit one must be provided when creating this inner class"));
9850 PATCH_METHOD_RETURN_ERROR ();
9853 /* Non static methods are called with the current object extra
9854 argument. If patch a `new TYPE()', the argument is the value
9855 returned by the object allocator. If method is resolved as a
9856 primary, use the primary otherwise use the current THIS. */
9857 args = nreverse (args);
9858 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
9860 this_arg = primary ? primary : current_this;
9862 /* If we're using an access method, things are different.
9863 There are two familly of cases:
9865 1) We're not generating bytecodes:
9867 - LIST is non static. It's invocation is transformed from
9868 x(a1,...,an) into this$<n>.x(a1,....an).
9869 - LIST is static. It's invocation is transformed from
9870 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9872 2) We're generating bytecodes:
9874 - LIST is non static. It's invocation is transformed from
9875 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9876 - LIST is static. It's invocation is transformed from
9877 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9879 Of course, this$<n> can be abitrary complex, ranging from
9880 this$0 (the immediate outer context) to
9881 access$0(access$0(...(this$0))).
9883 maybe_use_access_method returns a non zero value if the
9884 this_arg has to be moved into the (then generated) stub
9885 argument list. In the meantime, the selected function
9886 might have be replaced by a generated stub. */
9887 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9889 args = tree_cons (NULL_TREE, this_arg, args);
9890 this_arg = NULL_TREE; /* So it doesn't get chained twice */
9895 /* Merge point of all resolution schemes. If we have nothing, this
9896 is an error, already signaled */
9897 if (!list)
9898 PATCH_METHOD_RETURN_ERROR ();
9900 /* Check accessibility, position the is_static flag, build and
9901 return the call */
9902 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
9904 char *fct_name = xstrdup (lang_printable_name (list, 0));
9905 int ctor_p = DECL_CONSTRUCTOR_P (list);
9906 parse_error_context
9907 (wfl, "Can't access %s %s `%s%s.%s' from `%s'",
9908 java_accstring_lookup (get_access_flags_from_decl (list)),
9909 (ctor_p ? "constructor" : "method"),
9910 (ctor_p ?
9911 "" : lang_printable_name_wls (TREE_TYPE (TREE_TYPE (list)), 0)),
9912 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9913 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9914 free (fct_name);
9915 PATCH_METHOD_RETURN_ERROR ();
9917 check_deprecation (wfl, list);
9919 /* If invoking a innerclass constructor, there are hidden parameters
9920 to pass */
9921 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9922 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9924 /* And make sure we add the accessed local variables to be saved
9925 in field aliases. */
9926 args = build_alias_initializer_parameter_list
9927 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9929 /* Secretly pass the current_this/primary as a second argument */
9930 if (primary || current_this)
9932 tree extra_arg;
9933 tree this_type = (current_this ?
9934 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
9935 /* Method's (list) enclosing context */
9936 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
9937 /* If we have a primary, use it. */
9938 if (primary)
9939 extra_arg = primary;
9940 /* The current `this' is an inner class but isn't a direct
9941 enclosing context for the inner class we're trying to
9942 create. Build an access to the proper enclosing context
9943 and use it. */
9944 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
9945 && this_type != TREE_TYPE (mec))
9948 extra_arg = build_access_to_thisn (current_class,
9949 TREE_TYPE (mec), 0);
9950 extra_arg = java_complete_tree (extra_arg);
9952 /* Otherwise, just use the current `this' as an enclosing
9953 context. */
9954 else
9955 extra_arg = current_this;
9956 args = tree_cons (NULL_TREE, extra_arg, args);
9958 else
9959 args = tree_cons (NULL_TREE, integer_zero_node, args);
9962 /* This handles the situation where a constructor invocation needs
9963 to have an enclosing context passed as a second parameter (the
9964 constructor is one of an inner class. We extract it from the
9965 current function. */
9966 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9968 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9969 tree extra_arg;
9971 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9973 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9974 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9976 else
9978 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9979 extra_arg =
9980 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9981 extra_arg = java_complete_tree (extra_arg);
9983 args = tree_cons (NULL_TREE, extra_arg, args);
9986 is_static_flag = METHOD_STATIC (list);
9987 if (! is_static_flag && this_arg != NULL_TREE)
9988 args = tree_cons (NULL_TREE, this_arg, args);
9990 /* In the context of an explicit constructor invocation, we can't
9991 invoke any method relying on `this'. Exceptions are: we're
9992 invoking a static function, primary exists and is not the current
9993 this, we're creating a new object. */
9994 if (ctxp->explicit_constructor_p
9995 && !is_static_flag
9996 && (!primary || primary == current_this)
9997 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
9999 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10000 PATCH_METHOD_RETURN_ERROR ();
10002 java_parser_context_restore_global ();
10003 if (is_static)
10004 *is_static = is_static_flag;
10005 /* Sometimes, we want the decl of the selected method. Such as for
10006 EH checking */
10007 if (ret_decl)
10008 *ret_decl = list;
10009 patch = patch_invoke (patch, list, args);
10010 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10012 tree finit_parms, finit_call;
10014 /* Prepare to pass hidden parameters to finit$, if any. */
10015 finit_parms = build_alias_initializer_parameter_list
10016 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10018 finit_call =
10019 build_method_invocation (build_wfl_node (finit_identifier_node),
10020 finit_parms);
10022 /* Generate the code used to initialize fields declared with an
10023 initialization statement and build a compound statement along
10024 with the super constructor invocation. */
10025 patch = build (COMPOUND_EXPR, void_type_node, patch,
10026 java_complete_tree (finit_call));
10027 CAN_COMPLETE_NORMALLY (patch) = 1;
10029 return patch;
10032 /* Check that we're not trying to do a static reference to a method in
10033 non static method. Return 1 if it's the case, 0 otherwise. */
10035 static int
10036 check_for_static_method_reference (wfl, node, method, where, primary)
10037 tree wfl, node, method, where, primary;
10039 if (METHOD_STATIC (current_function_decl)
10040 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10042 char *fct_name = xstrdup (lang_printable_name (method, 0));
10043 parse_error_context
10044 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10045 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10046 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10047 free (fct_name);
10048 return 1;
10050 return 0;
10053 /* Fix the invocation of *MDECL if necessary in the case of a
10054 invocation from an inner class. *THIS_ARG might be modified
10055 appropriately and an alternative access to *MDECL might be
10056 returned. */
10058 static int
10059 maybe_use_access_method (is_super_init, mdecl, this_arg)
10060 int is_super_init;
10061 tree *mdecl, *this_arg;
10063 tree ctx;
10064 tree md = *mdecl, ta = *this_arg;
10065 int to_return = 0;
10066 int non_static_context = !METHOD_STATIC (md);
10068 if (is_super_init
10069 || DECL_CONTEXT (md) == current_class
10070 || !PURE_INNER_CLASS_TYPE_P (current_class)
10071 || DECL_FINIT_P (md))
10072 return 0;
10074 /* If we're calling a method found in an enclosing class, generate
10075 what it takes to retrieve the right this. Don't do that if we're
10076 invoking a static method. Note that if MD's type is unrelated to
10077 CURRENT_CLASS, then the current this can be used. */
10079 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10081 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10082 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10084 ta = build_current_thisn (current_class);
10085 ta = build_wfl_node (ta);
10087 else
10089 tree type = ctx;
10090 while (type)
10092 maybe_build_thisn_access_method (type);
10093 if (inherits_from_p (type, DECL_CONTEXT (md)))
10095 ta = build_access_to_thisn (ctx, type, 0);
10096 break;
10098 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10099 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10102 ta = java_complete_tree (ta);
10105 /* We might have to use an access method to get to MD. We can
10106 break the method access rule as far as we're not generating
10107 bytecode */
10108 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10110 md = build_outer_method_access_method (md);
10111 to_return = 1;
10114 *mdecl = md;
10115 *this_arg = ta;
10117 /* Returnin a non zero value indicates we were doing a non static
10118 method invokation that is now a static invocation. It will have
10119 callee displace `this' to insert it in the regular argument
10120 list. */
10121 return (non_static_context && to_return);
10124 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10125 mode. */
10127 static tree
10128 patch_invoke (patch, method, args)
10129 tree patch, method, args;
10131 tree dtable, func;
10132 tree original_call, t, ta;
10133 tree cond = NULL_TREE;
10135 /* Last step for args: convert build-in types. If we're dealing with
10136 a new TYPE() type call, the first argument to the constructor
10137 isn't found in the incoming argument list, but delivered by
10138 `new' */
10139 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10140 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10141 t = TREE_CHAIN (t);
10142 for (ta = args; t != end_params_node && ta;
10143 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10144 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10145 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10146 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10148 /* Resolve unresolved returned type isses */
10149 t = TREE_TYPE (TREE_TYPE (method));
10150 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10151 resolve_and_layout (TREE_TYPE (t), NULL);
10153 if (flag_emit_class_files || flag_emit_xref)
10154 func = method;
10155 else
10157 tree signature = build_java_signature (TREE_TYPE (method));
10158 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10160 case INVOKE_VIRTUAL:
10161 dtable = invoke_build_dtable (0, args);
10162 func = build_invokevirtual (dtable, method);
10163 break;
10165 case INVOKE_NONVIRTUAL:
10166 /* If the object for the method call is null, we throw an
10167 exception. We don't do this if the object is the current
10168 method's `this'. In other cases we just rely on an
10169 optimization pass to eliminate redundant checks. */
10170 if (TREE_VALUE (args) != current_this)
10172 /* We use a SAVE_EXPR here to make sure we only evaluate
10173 the new `self' expression once. */
10174 tree save_arg = save_expr (TREE_VALUE (args));
10175 TREE_VALUE (args) = save_arg;
10176 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10177 null_pointer_node);
10179 /* Fall through. */
10181 case INVOKE_SUPER:
10182 case INVOKE_STATIC:
10183 func = build_known_method_ref (method, TREE_TYPE (method),
10184 DECL_CONTEXT (method),
10185 signature, args);
10186 break;
10188 case INVOKE_INTERFACE:
10189 dtable = invoke_build_dtable (1, args);
10190 func = build_invokeinterface (dtable, method);
10191 break;
10193 default:
10194 fatal ("internal error - unknown invocation_mode result");
10197 /* Ensure self_type is initialized, (invokestatic). FIXME */
10198 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10201 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10202 TREE_OPERAND (patch, 0) = func;
10203 TREE_OPERAND (patch, 1) = args;
10204 original_call = patch;
10206 /* We're processing a `new TYPE ()' form. New is called and its
10207 returned value is the first argument to the constructor. We build
10208 a COMPOUND_EXPR and use saved expression so that the overall NEW
10209 expression value is a pointer to a newly created and initialized
10210 class. */
10211 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10213 tree class = DECL_CONTEXT (method);
10214 tree c1, saved_new, size, new;
10215 if (flag_emit_class_files || flag_emit_xref)
10217 TREE_TYPE (patch) = build_pointer_type (class);
10218 return patch;
10220 if (!TYPE_SIZE (class))
10221 safe_layout_class (class);
10222 size = size_in_bytes (class);
10223 new = build (CALL_EXPR, promote_type (class),
10224 build_address_of (alloc_object_node),
10225 tree_cons (NULL_TREE, build_class_ref (class),
10226 build_tree_list (NULL_TREE,
10227 size_in_bytes (class))),
10228 NULL_TREE);
10229 saved_new = save_expr (new);
10230 c1 = build_tree_list (NULL_TREE, saved_new);
10231 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10232 TREE_OPERAND (original_call, 1) = c1;
10233 TREE_SET_CODE (original_call, CALL_EXPR);
10234 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10237 /* If COND is set, then we are building a check to see if the object
10238 is NULL. */
10239 if (cond != NULL_TREE)
10241 /* We have to make the `then' branch a compound expression to
10242 make the types turn out right. This seems bizarre. */
10243 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10244 build (COMPOUND_EXPR, TREE_TYPE (patch),
10245 build (CALL_EXPR, void_type_node,
10246 build_address_of (soft_nullpointer_node),
10247 NULL_TREE, NULL_TREE),
10248 (FLOAT_TYPE_P (TREE_TYPE (patch))
10249 ? build_real (TREE_TYPE (patch), dconst0)
10250 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10251 integer_zero_node))),
10252 patch);
10253 TREE_SIDE_EFFECTS (patch) = 1;
10256 return patch;
10259 static int
10260 invocation_mode (method, super)
10261 tree method;
10262 int super;
10264 int access = get_access_flags_from_decl (method);
10266 if (super)
10267 return INVOKE_SUPER;
10269 if (access & ACC_STATIC)
10270 return INVOKE_STATIC;
10272 /* We have to look for a constructor before we handle nonvirtual
10273 calls; otherwise the constructor will look nonvirtual. */
10274 if (DECL_CONSTRUCTOR_P (method))
10275 return INVOKE_STATIC;
10277 if (access & ACC_FINAL || access & ACC_PRIVATE)
10278 return INVOKE_NONVIRTUAL;
10280 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10281 return INVOKE_NONVIRTUAL;
10283 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10284 return INVOKE_INTERFACE;
10286 return INVOKE_VIRTUAL;
10289 /* Retrieve a refined list of matching methods. It covers the step
10290 15.11.2 (Compile-Time Step 2) */
10292 static tree
10293 lookup_method_invoke (lc, cl, class, name, arg_list)
10294 int lc;
10295 tree cl;
10296 tree class, name, arg_list;
10298 tree atl = end_params_node; /* Arg Type List */
10299 tree method, signature, list, node;
10300 const char *candidates; /* Used for error report */
10301 char *dup;
10303 /* Fix the arguments */
10304 for (node = arg_list; node; node = TREE_CHAIN (node))
10306 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10307 /* Non primitive type may have to be resolved */
10308 if (!JPRIMITIVE_TYPE_P (current_arg))
10309 resolve_and_layout (current_arg, NULL_TREE);
10310 /* And promoted */
10311 if (TREE_CODE (current_arg) == RECORD_TYPE)
10312 current_arg = promote_type (current_arg);
10313 atl = tree_cons (NULL_TREE, current_arg, atl);
10316 /* Presto. If we're dealing with an anonymous class and a
10317 constructor call, generate the right constructor now, since we
10318 know the arguments' types. */
10320 if (lc && ANONYMOUS_CLASS_P (class))
10321 craft_constructor (TYPE_NAME (class), atl);
10323 /* Find all candidates and then refine the list, searching for the
10324 most specific method. */
10325 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10326 list = find_most_specific_methods_list (list);
10327 if (list && !TREE_CHAIN (list))
10328 return TREE_VALUE (list);
10330 /* Issue an error. List candidates if any. Candidates are listed
10331 only if accessible (non accessible methods may end-up here for
10332 the sake of a better error report). */
10333 candidates = NULL;
10334 if (list)
10336 tree current;
10337 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10338 for (current = list; current; current = TREE_CHAIN (current))
10340 tree cm = TREE_VALUE (current);
10341 char string [4096];
10342 if (!cm || not_accessible_p (class, cm, 0))
10343 continue;
10344 sprintf
10345 (string, " `%s' in `%s'%s",
10346 get_printable_method_name (cm),
10347 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10348 (TREE_CHAIN (current) ? "\n" : ""));
10349 obstack_grow (&temporary_obstack, string, strlen (string));
10351 obstack_1grow (&temporary_obstack, '\0');
10352 candidates = obstack_finish (&temporary_obstack);
10354 /* Issue the error message */
10355 method = make_node (FUNCTION_TYPE);
10356 TYPE_ARG_TYPES (method) = atl;
10357 signature = build_java_argument_signature (method);
10358 dup = xstrdup (lang_printable_name (class, 0));
10359 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10360 (lc ? "constructor" : "method"),
10361 (lc ? dup : IDENTIFIER_POINTER (name)),
10362 IDENTIFIER_POINTER (signature), dup,
10363 (candidates ? candidates : ""));
10364 free (dup);
10365 return NULL_TREE;
10368 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10369 when we're looking for a constructor. */
10371 static tree
10372 find_applicable_accessible_methods_list (lc, class, name, arglist)
10373 int lc;
10374 tree class, name, arglist;
10376 static struct hash_table t, *searched_classes = NULL;
10377 static int search_not_done = 0;
10378 tree list = NULL_TREE, all_list = NULL_TREE;
10380 /* Check the hash table to determine if this class has been searched
10381 already. */
10382 if (searched_classes)
10384 if (hash_lookup (searched_classes,
10385 (const hash_table_key) class, FALSE, NULL))
10386 return NULL;
10388 else
10390 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10391 java_hash_compare_tree_node);
10392 searched_classes = &t;
10395 search_not_done++;
10396 hash_lookup (searched_classes,
10397 (const hash_table_key) class, TRUE, NULL);
10399 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10401 load_class (class, 1);
10402 safe_layout_class (class);
10405 /* Search interfaces */
10406 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10407 && CLASS_INTERFACE (TYPE_NAME (class)))
10409 int i, n;
10410 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10411 search_applicable_methods_list (lc, TYPE_METHODS (class),
10412 name, arglist, &list, &all_list);
10413 n = TREE_VEC_LENGTH (basetype_vec);
10414 for (i = 1; i < n; i++)
10416 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10417 tree rlist;
10419 rlist = find_applicable_accessible_methods_list (lc, t, name,
10420 arglist);
10421 list = chainon (rlist, list);
10424 /* Search classes */
10425 else
10427 tree sc = class;
10428 int seen_inner_class = 0;
10429 search_applicable_methods_list (lc, TYPE_METHODS (class),
10430 name, arglist, &list, &all_list);
10432 /* When looking finit$, we turn LC to 1 so that we only search
10433 in class. Note that we should have found something at
10434 this point. */
10435 if (ID_FINIT_P (name))
10437 lc = 1;
10438 if (!list)
10439 fatal ("finit$ not found in class -- find_applicable_accessible_methods_list");
10442 /* We must search all interfaces of this class */
10443 if (!lc)
10445 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10446 int n = TREE_VEC_LENGTH (basetype_vec), i;
10447 for (i = 1; i < n; i++)
10449 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10450 if (t != object_type_node)
10452 tree rlist
10453 = find_applicable_accessible_methods_list (lc, t,
10454 name, arglist);
10455 list = chainon (rlist, list);
10460 /* Search enclosing context of inner classes before looking
10461 ancestors up. */
10462 while (!lc && INNER_CLASS_TYPE_P (class))
10464 tree rlist;
10465 seen_inner_class = 1;
10466 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
10467 rlist = find_applicable_accessible_methods_list (lc, class,
10468 name, arglist);
10469 list = chainon (rlist, list);
10472 if (!lc && seen_inner_class
10473 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10474 class = CLASSTYPE_SUPER (sc);
10475 else
10476 class = sc;
10478 /* Search superclass */
10479 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10481 tree rlist;
10482 class = CLASSTYPE_SUPER (class);
10483 rlist = find_applicable_accessible_methods_list (lc, class,
10484 name, arglist);
10485 list = chainon (rlist, list);
10489 search_not_done--;
10491 /* We're done. Reset the searched classes list and finally search
10492 java.lang.Object if it wasn't searched already. */
10493 if (!search_not_done)
10495 if (!lc
10496 && TYPE_METHODS (object_type_node)
10497 && !hash_lookup (searched_classes,
10498 (const hash_table_key) object_type_node,
10499 FALSE, NULL))
10501 search_applicable_methods_list (lc,
10502 TYPE_METHODS (object_type_node),
10503 name, arglist, &list, &all_list);
10505 hash_table_free (searched_classes);
10506 searched_classes = NULL;
10509 /* Either return the list obtained or all selected (but
10510 inaccessible) methods for better error report. */
10511 return (!list ? all_list : list);
10514 /* Effectively search for the appropriate method in method */
10516 static void
10517 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10518 int lc;
10519 tree method, name, arglist;
10520 tree *list, *all_list;
10522 for (; method; method = TREE_CHAIN (method))
10524 /* When dealing with constructor, stop here, otherwise search
10525 other classes */
10526 if (lc && !DECL_CONSTRUCTOR_P (method))
10527 continue;
10528 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10529 || (DECL_NAME (method) != name)))
10530 continue;
10532 if (argument_types_convertible (method, arglist))
10534 /* Retain accessible methods only */
10535 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10536 method, 0))
10537 *list = tree_cons (NULL_TREE, method, *list);
10538 else
10539 /* Also retain all selected method here */
10540 *all_list = tree_cons (NULL_TREE, method, *list);
10545 /* 15.11.2.2 Choose the Most Specific Method */
10547 static tree
10548 find_most_specific_methods_list (list)
10549 tree list;
10551 int max = 0;
10552 int abstract, candidates;
10553 tree current, new_list = NULL_TREE;
10554 for (current = list; current; current = TREE_CHAIN (current))
10556 tree method;
10557 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10559 for (method = list; method; method = TREE_CHAIN (method))
10561 tree method_v, current_v;
10562 /* Don't test a method against itself */
10563 if (method == current)
10564 continue;
10566 method_v = TREE_VALUE (method);
10567 current_v = TREE_VALUE (current);
10569 /* Compare arguments and location where methods where declared */
10570 if (argument_types_convertible (method_v, current_v))
10572 if (valid_method_invocation_conversion_p
10573 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10574 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10575 && enclosing_context_p (DECL_CONTEXT (method_v),
10576 DECL_CONTEXT (current_v))))
10578 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10579 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10580 max = (v > max ? v : max);
10586 /* Review the list and select the maximally specific methods */
10587 for (current = list, abstract = -1, candidates = -1;
10588 current; current = TREE_CHAIN (current))
10589 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10591 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10592 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10593 candidates++;
10596 /* If we have several and they're all abstract, just pick the
10597 closest one. */
10598 if (candidates > 0 && (candidates == abstract))
10600 new_list = nreverse (new_list);
10601 TREE_CHAIN (new_list) = NULL_TREE;
10604 /* We have several (we couldn't find a most specific), all but one
10605 are abstract, we pick the only non abstract one. */
10606 if (candidates > 0 && (candidates == abstract+1))
10608 for (current = new_list; current; current = TREE_CHAIN (current))
10609 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10611 TREE_CHAIN (current) = NULL_TREE;
10612 new_list = current;
10616 /* If we can't find one, lower expectations and try to gather multiple
10617 maximally specific methods */
10618 while (!new_list && max)
10620 while (--max > 0)
10622 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10623 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10627 return new_list;
10630 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10631 converted by method invocation conversion (5.3) to the type of the
10632 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10633 to change less often than M1. */
10635 static int
10636 argument_types_convertible (m1, m2_or_arglist)
10637 tree m1, m2_or_arglist;
10639 static tree m2_arg_value = NULL_TREE;
10640 static tree m2_arg_cache = NULL_TREE;
10641 static int initialized_p;
10643 register tree m1_arg, m2_arg;
10645 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10646 collector. */
10647 if (!initialized_p)
10649 ggc_add_tree_root (&m2_arg_value, 1);
10650 ggc_add_tree_root (&m2_arg_cache, 1);
10651 initialized_p = 1;
10654 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10656 if (m2_arg_value == m2_or_arglist)
10657 m2_arg = m2_arg_cache;
10658 else
10660 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10661 argument types */
10662 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10664 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10665 if (!METHOD_STATIC (m2_or_arglist))
10666 m2_arg = TREE_CHAIN (m2_arg);
10668 else
10669 m2_arg = m2_or_arglist;
10671 m2_arg_value = m2_or_arglist;
10672 m2_arg_cache = m2_arg;
10675 while (m1_arg != end_params_node && m2_arg != end_params_node)
10677 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
10678 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10679 TREE_VALUE (m2_arg)))
10680 break;
10681 m1_arg = TREE_CHAIN (m1_arg);
10682 m2_arg = TREE_CHAIN (m2_arg);
10684 return m1_arg == end_params_node && m2_arg == end_params_node;
10687 /* Qualification routines */
10689 static void
10690 qualify_ambiguous_name (id)
10691 tree id;
10693 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10694 saved_current_class;
10695 int again, super_found = 0, this_found = 0, new_array_found = 0;
10696 int code;
10698 /* We first qualify the first element, then derive qualification of
10699 others based on the first one. If the first element is qualified
10700 by a resolution (field or type), this resolution is stored in the
10701 QUAL_RESOLUTION of the qual element being examined. We need to
10702 save the current_class since the use of SUPER might change the
10703 its value. */
10704 saved_current_class = current_class;
10705 qual = EXPR_WFL_QUALIFICATION (id);
10706 do {
10708 /* Simple qualified expression feature a qual_wfl that is a
10709 WFL. Expression derived from a primary feature more complicated
10710 things like a CALL_EXPR. Expression from primary need to be
10711 worked out to extract the part on which the qualification will
10712 take place. */
10713 qual_wfl = QUAL_WFL (qual);
10714 switch (TREE_CODE (qual_wfl))
10716 case CALL_EXPR:
10717 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10718 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10720 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10721 qual_wfl = QUAL_WFL (qual);
10723 break;
10724 case NEW_ARRAY_EXPR:
10725 case NEW_ANONYMOUS_ARRAY_EXPR:
10726 qual = TREE_CHAIN (qual);
10727 again = new_array_found = 1;
10728 continue;
10729 case CONVERT_EXPR:
10730 break;
10731 case NEW_CLASS_EXPR:
10732 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10733 break;
10734 case ARRAY_REF:
10735 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10736 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10737 break;
10738 case STRING_CST:
10739 qual = TREE_CHAIN (qual);
10740 qual_wfl = QUAL_WFL (qual);
10741 break;
10742 case CLASS_LITERAL:
10743 qual = TREE_CHAIN (qual);
10744 qual_wfl = QUAL_WFL (qual);
10745 break;
10746 default:
10747 /* Fix for -Wall. Just break doing nothing */
10748 break;
10751 ptr_type = current_class;
10752 again = 0;
10753 code = TREE_CODE (qual_wfl);
10755 /* Pos evaluation: non WFL leading expression nodes */
10756 if (code == CONVERT_EXPR
10757 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10758 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10760 else if (code == INTEGER_CST)
10761 name = qual_wfl;
10763 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
10764 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10765 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10767 else if (code == TREE_LIST)
10768 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10770 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10771 || code == PLUS_EXPR)
10773 qual = TREE_CHAIN (qual);
10774 qual_wfl = QUAL_WFL (qual);
10775 again = 1;
10777 else
10779 name = EXPR_WFL_NODE (qual_wfl);
10780 if (!name)
10782 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10783 again = 1;
10787 /* If we have a THIS (from a primary), we set the context accordingly */
10788 if (name == this_identifier_node)
10790 /* This isn't really elegant. One more added irregularity
10791 before I start using COMPONENT_REF (hopefully very soon.) */
10792 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10793 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10794 EXPR_WITH_FILE_LOCATION
10795 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10796 this_identifier_node)
10798 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10799 qual = EXPR_WFL_QUALIFICATION (qual);
10801 qual = TREE_CHAIN (qual);
10802 qual_wfl = QUAL_WFL (qual);
10803 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10804 again = 1;
10805 else
10806 name = EXPR_WFL_NODE (qual_wfl);
10807 this_found = 1;
10809 /* If we have a SUPER, we set the context accordingly */
10810 if (name == super_identifier_node)
10812 current_class = CLASSTYPE_SUPER (ptr_type);
10813 /* Check that there is such a thing as a super class. If not,
10814 return. The error will be caught later on, during the
10815 resolution */
10816 if (!current_class)
10818 current_class = saved_current_class;
10819 return;
10821 qual = TREE_CHAIN (qual);
10822 /* Do one more interation to set things up */
10823 super_found = again = 1;
10825 } while (again);
10827 /* If name appears within the scope of a local variable declaration
10828 or parameter declaration, then it is an expression name. We don't
10829 carry this test out if we're in the context of the use of SUPER
10830 or THIS */
10831 if (!this_found && !super_found
10832 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10833 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
10835 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10836 QUAL_RESOLUTION (qual) = decl;
10839 /* If within the class/interface NAME was found to be used there
10840 exists a (possibly inherited) field named NAME, then this is an
10841 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10842 address length, it is OK. */
10843 else if ((decl = lookup_field_wrapper (ptr_type, name))
10844 || (new_array_found && name == length_identifier_node))
10846 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10847 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
10850 /* We reclassify NAME as yielding to a type name resolution if:
10851 - NAME is a class/interface declared within the compilation
10852 unit containing NAME,
10853 - NAME is imported via a single-type-import declaration,
10854 - NAME is declared in an another compilation unit of the package
10855 of the compilation unit containing NAME,
10856 - NAME is declared by exactly on type-import-on-demand declaration
10857 of the compilation unit containing NAME.
10858 - NAME is actually a STRING_CST. */
10859 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10860 || (decl = resolve_and_layout (name, NULL_TREE)))
10862 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10863 QUAL_RESOLUTION (qual) = decl;
10866 /* Method call, array references and cast are expression name */
10867 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
10868 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10869 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
10870 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10872 /* Check here that NAME isn't declared by more than one
10873 type-import-on-demand declaration of the compilation unit
10874 containing NAME. FIXME */
10876 /* Otherwise, NAME is reclassified as a package name */
10877 else
10878 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10880 /* Propagate the qualification accross other components of the
10881 qualified name */
10882 for (qual = TREE_CHAIN (qual); qual;
10883 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10885 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10886 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10887 else
10888 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10891 /* Store the global qualification for the ambiguous part of ID back
10892 into ID fields */
10893 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10894 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10895 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10896 RESOLVE_TYPE_NAME_P (id) = 1;
10897 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10898 RESOLVE_PACKAGE_NAME_P (id) = 1;
10900 /* Restore the current class */
10901 current_class = saved_current_class;
10904 static int
10905 breakdown_qualified (left, right, source)
10906 tree *left, *right, source;
10908 char *p, *base;
10909 int l = IDENTIFIER_LENGTH (source);
10911 base = alloca (l + 1);
10912 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10914 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10915 p = base + l - 1;
10916 while (*p != '.' && p != base)
10917 p--;
10919 /* We didn't find a '.'. Return an error */
10920 if (p == base)
10921 return 1;
10923 *p = '\0';
10924 if (right)
10925 *right = get_identifier (p+1);
10926 *left = get_identifier (base);
10928 return 0;
10931 /* Patch tree nodes in a function body. When a BLOCK is found, push
10932 local variable decls if present.
10933 Same as java_complete_lhs, but does resolve static finals to values. */
10935 static tree
10936 java_complete_tree (node)
10937 tree node;
10939 node = java_complete_lhs (node);
10940 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
10941 && DECL_INITIAL (node) != NULL_TREE
10942 && !flag_emit_xref)
10944 tree value = DECL_INITIAL (node);
10945 DECL_INITIAL (node) = NULL_TREE;
10946 value = fold_constant_for_init (value, node);
10947 DECL_INITIAL (node) = value;
10948 if (value != NULL_TREE)
10950 /* fold_constant_for_init sometimes widen the original type
10951 of the constant (i.e. byte to int.) It's not desirable,
10952 especially if NODE is a function argument. */
10953 if (TREE_CODE (value) == INTEGER_CST
10954 && TREE_TYPE (node) != TREE_TYPE (value))
10955 return convert (TREE_TYPE (node), value);
10956 else
10957 return value;
10959 else
10960 DECL_FIELD_FINAL_IUD (node) = 0;
10962 return node;
10965 static tree
10966 java_stabilize_reference (node)
10967 tree node;
10969 if (TREE_CODE (node) == COMPOUND_EXPR)
10971 tree op0 = TREE_OPERAND (node, 0);
10972 tree op1 = TREE_OPERAND (node, 1);
10973 TREE_OPERAND (node, 0) = save_expr (op0);
10974 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10975 return node;
10977 return stabilize_reference (node);
10980 /* Patch tree nodes in a function body. When a BLOCK is found, push
10981 local variable decls if present.
10982 Same as java_complete_tree, but does not resolve static finals to values. */
10984 static tree
10985 java_complete_lhs (node)
10986 tree node;
10988 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
10989 int flag;
10991 /* CONVERT_EXPR always has its type set, even though it needs to be
10992 worked out. */
10993 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10994 return node;
10996 /* The switch block implements cases processing container nodes
10997 first. Contained nodes are always written back. Leaves come
10998 next and return a value. */
10999 switch (TREE_CODE (node))
11001 case BLOCK:
11003 /* 1- Block section.
11004 Set the local values on decl names so we can identify them
11005 faster when they're referenced. At that stage, identifiers
11006 are legal so we don't check for declaration errors. */
11007 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11009 DECL_CONTEXT (cn) = current_function_decl;
11010 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11012 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11013 CAN_COMPLETE_NORMALLY (node) = 1;
11014 else
11016 tree stmt = BLOCK_EXPR_BODY (node);
11017 tree *ptr;
11018 int error_seen = 0;
11019 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11021 /* Re-order from (((A; B); C); ...; Z) to
11022 (A; (B; (C ; (...; Z)))).
11023 This makes it easier to scan the statements left-to-right
11024 without using recursion (which might overflow the stack
11025 if the block has many statements. */
11026 for (;;)
11028 tree left = TREE_OPERAND (stmt, 0);
11029 if (TREE_CODE (left) != COMPOUND_EXPR)
11030 break;
11031 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11032 TREE_OPERAND (left, 1) = stmt;
11033 stmt = left;
11035 BLOCK_EXPR_BODY (node) = stmt;
11038 /* Now do the actual complete, without deep recursion for
11039 long blocks. */
11040 ptr = &BLOCK_EXPR_BODY (node);
11041 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11042 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11044 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11045 tree *next = &TREE_OPERAND (*ptr, 1);
11046 TREE_OPERAND (*ptr, 0) = cur;
11047 if (cur == empty_stmt_node)
11049 /* Optimization; makes it easier to detect empty bodies.
11050 Most useful for <clinit> with all-constant initializer. */
11051 *ptr = *next;
11052 continue;
11054 if (TREE_CODE (cur) == ERROR_MARK)
11055 error_seen++;
11056 else if (! CAN_COMPLETE_NORMALLY (cur))
11058 wfl_op2 = *next;
11059 for (;;)
11061 if (TREE_CODE (wfl_op2) == BLOCK)
11062 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11063 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11064 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11065 else
11066 break;
11068 if (TREE_CODE (wfl_op2) != CASE_EXPR
11069 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11070 unreachable_stmt_error (*ptr);
11072 ptr = next;
11074 *ptr = java_complete_tree (*ptr);
11076 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11077 return error_mark_node;
11078 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11080 /* Turn local bindings to null */
11081 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11082 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11084 TREE_TYPE (node) = void_type_node;
11085 break;
11087 /* 2- They are expressions but ultimately deal with statements */
11089 case THROW_EXPR:
11090 wfl_op1 = TREE_OPERAND (node, 0);
11091 COMPLETE_CHECK_OP_0 (node);
11092 /* 14.19 A throw statement cannot complete normally. */
11093 CAN_COMPLETE_NORMALLY (node) = 0;
11094 return patch_throw_statement (node, wfl_op1);
11096 case SYNCHRONIZED_EXPR:
11097 wfl_op1 = TREE_OPERAND (node, 0);
11098 return patch_synchronized_statement (node, wfl_op1);
11100 case TRY_EXPR:
11101 return patch_try_statement (node);
11103 case TRY_FINALLY_EXPR:
11104 COMPLETE_CHECK_OP_0 (node);
11105 COMPLETE_CHECK_OP_1 (node);
11106 CAN_COMPLETE_NORMALLY (node)
11107 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11108 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11109 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11110 return node;
11112 case CLEANUP_POINT_EXPR:
11113 COMPLETE_CHECK_OP_0 (node);
11114 TREE_TYPE (node) = void_type_node;
11115 CAN_COMPLETE_NORMALLY (node) =
11116 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11117 return node;
11119 case WITH_CLEANUP_EXPR:
11120 COMPLETE_CHECK_OP_0 (node);
11121 COMPLETE_CHECK_OP_2 (node);
11122 CAN_COMPLETE_NORMALLY (node) =
11123 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11124 TREE_TYPE (node) = void_type_node;
11125 return node;
11127 case LABELED_BLOCK_EXPR:
11128 PUSH_LABELED_BLOCK (node);
11129 if (LABELED_BLOCK_BODY (node))
11130 COMPLETE_CHECK_OP_1 (node);
11131 TREE_TYPE (node) = void_type_node;
11132 POP_LABELED_BLOCK ();
11134 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11136 LABELED_BLOCK_BODY (node) = NULL_TREE;
11137 CAN_COMPLETE_NORMALLY (node) = 1;
11139 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11140 CAN_COMPLETE_NORMALLY (node) = 1;
11141 return node;
11143 case EXIT_BLOCK_EXPR:
11144 /* We don't complete operand 1, because it's the return value of
11145 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11146 return patch_bc_statement (node);
11148 case CASE_EXPR:
11149 cn = java_complete_tree (TREE_OPERAND (node, 0));
11150 if (cn == error_mark_node)
11151 return cn;
11153 /* First, the case expression must be constant. Values of final
11154 fields are accepted. */
11155 cn = fold (cn);
11156 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11157 && JDECL_P (TREE_OPERAND (cn, 1))
11158 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11159 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11161 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11162 TREE_OPERAND (cn, 1));
11165 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11167 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11168 parse_error_context (node, "Constant expression required");
11169 return error_mark_node;
11172 nn = ctxp->current_loop;
11174 /* It must be assignable to the type of the switch expression. */
11175 if (!try_builtin_assignconv (NULL_TREE,
11176 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11178 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11179 parse_error_context
11180 (wfl_operator,
11181 "Incompatible type for case. Can't convert `%s' to `int'",
11182 lang_printable_name (TREE_TYPE (cn), 0));
11183 return error_mark_node;
11186 cn = fold (convert (int_type_node, cn));
11188 /* Multiple instance of a case label bearing the same
11189 value is checked during code generation. The case
11190 expression is allright so far. */
11191 if (TREE_CODE (cn) == VAR_DECL)
11192 cn = DECL_INITIAL (cn);
11193 TREE_OPERAND (node, 0) = cn;
11194 TREE_TYPE (node) = void_type_node;
11195 CAN_COMPLETE_NORMALLY (node) = 1;
11196 TREE_SIDE_EFFECTS (node) = 1;
11197 break;
11199 case DEFAULT_EXPR:
11200 nn = ctxp->current_loop;
11201 /* Only one default label is allowed per switch statement */
11202 if (SWITCH_HAS_DEFAULT (nn))
11204 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11205 parse_error_context (wfl_operator,
11206 "Duplicate case label: `default'");
11207 return error_mark_node;
11209 else
11210 SWITCH_HAS_DEFAULT (nn) = 1;
11211 TREE_TYPE (node) = void_type_node;
11212 TREE_SIDE_EFFECTS (node) = 1;
11213 CAN_COMPLETE_NORMALLY (node) = 1;
11214 break;
11216 case SWITCH_EXPR:
11217 case LOOP_EXPR:
11218 PUSH_LOOP (node);
11219 /* Check whether the loop was enclosed in a labeled
11220 statement. If not, create one, insert the loop in it and
11221 return the node */
11222 nn = patch_loop_statement (node);
11224 /* Anyways, walk the body of the loop */
11225 if (TREE_CODE (node) == LOOP_EXPR)
11226 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11227 /* Switch statement: walk the switch expression and the cases */
11228 else
11229 node = patch_switch_statement (node);
11231 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11232 nn = error_mark_node;
11233 else
11235 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11236 /* If we returned something different, that's because we
11237 inserted a label. Pop the label too. */
11238 if (nn != node)
11240 if (CAN_COMPLETE_NORMALLY (node))
11241 CAN_COMPLETE_NORMALLY (nn) = 1;
11242 POP_LABELED_BLOCK ();
11245 POP_LOOP ();
11246 return nn;
11248 case EXIT_EXPR:
11249 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11250 return patch_exit_expr (node);
11252 case COND_EXPR:
11253 /* Condition */
11254 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11255 if (TREE_OPERAND (node, 0) == error_mark_node)
11256 return error_mark_node;
11257 /* then-else branches */
11258 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11259 if (TREE_OPERAND (node, 1) == error_mark_node)
11260 return error_mark_node;
11261 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11262 if (TREE_OPERAND (node, 2) == error_mark_node)
11263 return error_mark_node;
11264 return patch_if_else_statement (node);
11265 break;
11267 case CONDITIONAL_EXPR:
11268 /* Condition */
11269 wfl_op1 = TREE_OPERAND (node, 0);
11270 COMPLETE_CHECK_OP_0 (node);
11271 wfl_op2 = TREE_OPERAND (node, 1);
11272 COMPLETE_CHECK_OP_1 (node);
11273 wfl_op3 = TREE_OPERAND (node, 2);
11274 COMPLETE_CHECK_OP_2 (node);
11275 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11277 /* 3- Expression section */
11278 case COMPOUND_EXPR:
11279 wfl_op2 = TREE_OPERAND (node, 1);
11280 TREE_OPERAND (node, 0) = nn =
11281 java_complete_tree (TREE_OPERAND (node, 0));
11282 if (wfl_op2 == empty_stmt_node)
11283 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11284 else
11286 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11288 /* An unreachable condition in a do-while statement
11289 is *not* (technically) an unreachable statement. */
11290 nn = wfl_op2;
11291 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11292 nn = EXPR_WFL_NODE (nn);
11293 if (TREE_CODE (nn) != EXIT_EXPR)
11295 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11296 parse_error_context (wfl_operator, "Unreachable statement");
11299 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11300 if (TREE_OPERAND (node, 1) == error_mark_node)
11301 return error_mark_node;
11302 CAN_COMPLETE_NORMALLY (node)
11303 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11305 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11306 break;
11308 case RETURN_EXPR:
11309 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11310 return patch_return (node);
11312 case EXPR_WITH_FILE_LOCATION:
11313 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11314 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11316 tree wfl = node;
11317 node = resolve_expression_name (node, NULL);
11318 if (node == error_mark_node)
11319 return node;
11320 /* Keep line number information somewhere were it doesn't
11321 disrupt the completion process. */
11322 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11324 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11325 TREE_OPERAND (node, 1) = wfl;
11327 CAN_COMPLETE_NORMALLY (node) = 1;
11329 else
11331 tree body;
11332 int save_lineno = lineno;
11333 lineno = EXPR_WFL_LINENO (node);
11334 body = java_complete_tree (EXPR_WFL_NODE (node));
11335 lineno = save_lineno;
11336 EXPR_WFL_NODE (node) = body;
11337 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11338 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11339 if (body == empty_stmt_node || TREE_CONSTANT (body))
11341 /* Makes it easier to constant fold, detect empty bodies. */
11342 return body;
11344 if (body == error_mark_node)
11346 /* Its important for the evaluation of assignment that
11347 this mark on the TREE_TYPE is propagated. */
11348 TREE_TYPE (node) = error_mark_node;
11349 return error_mark_node;
11351 else
11352 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11355 break;
11357 case NEW_ARRAY_EXPR:
11358 /* Patch all the dimensions */
11359 flag = 0;
11360 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11362 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11363 tree dim = convert (int_type_node,
11364 java_complete_tree (TREE_VALUE (cn)));
11365 if (dim == error_mark_node)
11367 flag = 1;
11368 continue;
11370 else
11372 TREE_VALUE (cn) = dim;
11373 /* Setup the location of the current dimension, for
11374 later error report. */
11375 TREE_PURPOSE (cn) =
11376 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11377 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11380 /* They complete the array creation expression, if no errors
11381 were found. */
11382 CAN_COMPLETE_NORMALLY (node) = 1;
11383 return (flag ? error_mark_node
11384 : force_evaluation_order (patch_newarray (node)));
11386 case NEW_ANONYMOUS_ARRAY_EXPR:
11387 /* Create the array type if necessary. */
11388 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11390 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11391 if (!(type = resolve_type_during_patch (type)))
11392 return error_mark_node;
11393 type = build_array_from_name (type, NULL_TREE,
11394 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11395 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11397 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11398 ANONYMOUS_ARRAY_INITIALIZER (node));
11399 if (node == error_mark_node)
11400 return error_mark_node;
11401 CAN_COMPLETE_NORMALLY (node) = 1;
11402 return node;
11404 case NEW_CLASS_EXPR:
11405 case CALL_EXPR:
11406 /* Complete function's argument(s) first */
11407 if (complete_function_arguments (node))
11408 return error_mark_node;
11409 else
11411 tree decl, wfl = TREE_OPERAND (node, 0);
11412 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11414 node = patch_method_invocation (node, NULL_TREE,
11415 NULL_TREE, 0, &decl);
11416 if (node == error_mark_node)
11417 return error_mark_node;
11419 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11420 /* If we call this(...), register signature and positions */
11421 if (in_this)
11422 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11423 tree_cons (wfl, decl,
11424 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11425 CAN_COMPLETE_NORMALLY (node) = 1;
11426 return force_evaluation_order (node);
11429 case MODIFY_EXPR:
11430 /* Save potential wfls */
11431 wfl_op1 = TREE_OPERAND (node, 0);
11432 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11434 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11435 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11436 && DECL_INITIAL (nn) != NULL_TREE)
11438 tree value;
11440 value = fold_constant_for_init (nn, nn);
11442 if (value != NULL_TREE)
11444 tree type = TREE_TYPE (value);
11445 if (JPRIMITIVE_TYPE_P (type) ||
11446 (type == string_ptr_type_node && ! flag_emit_class_files))
11447 return empty_stmt_node;
11449 if (! flag_emit_class_files)
11450 DECL_INITIAL (nn) = NULL_TREE;
11451 if (CLASS_FINAL_VARIABLE_P (nn))
11452 DECL_FIELD_FINAL_IUD (nn) = 0;
11454 wfl_op2 = TREE_OPERAND (node, 1);
11456 if (TREE_OPERAND (node, 0) == error_mark_node)
11457 return error_mark_node;
11459 flag = COMPOUND_ASSIGN_P (wfl_op2);
11460 if (flag)
11462 /* This might break when accessing outer field from inner
11463 class. TESTME, FIXME */
11464 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11466 /* Hand stablize the lhs on both places */
11467 TREE_OPERAND (node, 0) = lvalue;
11468 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11469 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11471 /* 15.25.2.a: Left hand is not an array access. FIXME */
11472 /* Now complete the RHS. We write it back later on. */
11473 nn = java_complete_tree (TREE_OPERAND (node, 1));
11475 if ((cn = patch_string (nn)))
11476 nn = cn;
11478 /* The last part of the rewrite for E1 op= E2 is to have
11479 E1 = (T)(E1 op E2), with T being the type of E1. */
11480 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11481 TREE_TYPE (lvalue), nn));
11483 /* If the assignment is compound and has reference type,
11484 then ensure the LHS has type String and nothing else. */
11485 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11486 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11487 parse_error_context (wfl_op2,
11488 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11489 lang_printable_name (TREE_TYPE (lvalue), 0));
11491 /* 15.25.2.b: Left hand is an array access. FIXME */
11494 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11495 function to complete this RHS. Note that a NEW_ARRAY_INIT
11496 might have been already fully expanded if created as a result
11497 of processing an anonymous array initializer. We avoid doing
11498 the operation twice by testing whether the node already bears
11499 a type. */
11500 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11501 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11502 TREE_OPERAND (node, 1));
11503 /* Otherwise we simply complete the RHS */
11504 else
11505 nn = java_complete_tree (TREE_OPERAND (node, 1));
11507 if (nn == error_mark_node)
11508 return error_mark_node;
11510 /* Write back the RHS as we evaluated it. */
11511 TREE_OPERAND (node, 1) = nn;
11513 /* In case we're handling = with a String as a RHS, we need to
11514 produce a String out of the RHS (it might still be a
11515 STRING_CST or a StringBuffer at this stage */
11516 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11517 TREE_OPERAND (node, 1) = nn;
11519 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11520 TREE_OPERAND (node, 1))))
11522 /* We return error_mark_node if outer_field_access_fix
11523 detects we write into a final. */
11524 if (nn == error_mark_node)
11525 return error_mark_node;
11526 node = nn;
11528 else
11530 node = patch_assignment (node, wfl_op1, wfl_op2);
11531 /* Reorganize the tree if necessary. */
11532 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11533 || JSTRING_P (TREE_TYPE (node))))
11534 node = java_refold (node);
11537 CAN_COMPLETE_NORMALLY (node) = 1;
11538 return node;
11540 case MULT_EXPR:
11541 case PLUS_EXPR:
11542 case MINUS_EXPR:
11543 case LSHIFT_EXPR:
11544 case RSHIFT_EXPR:
11545 case URSHIFT_EXPR:
11546 case BIT_AND_EXPR:
11547 case BIT_XOR_EXPR:
11548 case BIT_IOR_EXPR:
11549 case TRUNC_MOD_EXPR:
11550 case TRUNC_DIV_EXPR:
11551 case RDIV_EXPR:
11552 case TRUTH_ANDIF_EXPR:
11553 case TRUTH_ORIF_EXPR:
11554 case EQ_EXPR:
11555 case NE_EXPR:
11556 case GT_EXPR:
11557 case GE_EXPR:
11558 case LT_EXPR:
11559 case LE_EXPR:
11560 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11561 knows how to handle those cases. */
11562 wfl_op1 = TREE_OPERAND (node, 0);
11563 wfl_op2 = TREE_OPERAND (node, 1);
11565 CAN_COMPLETE_NORMALLY (node) = 1;
11566 /* Don't complete string nodes if dealing with the PLUS operand. */
11567 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11569 nn = java_complete_tree (wfl_op1);
11570 if (nn == error_mark_node)
11571 return error_mark_node;
11573 TREE_OPERAND (node, 0) = nn;
11575 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11577 nn = java_complete_tree (wfl_op2);
11578 if (nn == error_mark_node)
11579 return error_mark_node;
11581 TREE_OPERAND (node, 1) = nn;
11583 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11585 case INSTANCEOF_EXPR:
11586 wfl_op1 = TREE_OPERAND (node, 0);
11587 COMPLETE_CHECK_OP_0 (node);
11588 if (flag_emit_xref)
11590 TREE_TYPE (node) = boolean_type_node;
11591 return node;
11593 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11595 case UNARY_PLUS_EXPR:
11596 case NEGATE_EXPR:
11597 case TRUTH_NOT_EXPR:
11598 case BIT_NOT_EXPR:
11599 case PREDECREMENT_EXPR:
11600 case PREINCREMENT_EXPR:
11601 case POSTDECREMENT_EXPR:
11602 case POSTINCREMENT_EXPR:
11603 case CONVERT_EXPR:
11604 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11605 how to handle those cases. */
11606 wfl_op1 = TREE_OPERAND (node, 0);
11607 CAN_COMPLETE_NORMALLY (node) = 1;
11608 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11609 if (TREE_OPERAND (node, 0) == error_mark_node)
11610 return error_mark_node;
11611 node = patch_unaryop (node, wfl_op1);
11612 CAN_COMPLETE_NORMALLY (node) = 1;
11613 break;
11615 case ARRAY_REF:
11616 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11617 how to handle those cases. */
11618 wfl_op1 = TREE_OPERAND (node, 0);
11619 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11620 if (TREE_OPERAND (node, 0) == error_mark_node)
11621 return error_mark_node;
11622 if (!flag_emit_class_files && !flag_emit_xref)
11623 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
11624 /* The same applies to wfl_op2 */
11625 wfl_op2 = TREE_OPERAND (node, 1);
11626 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11627 if (TREE_OPERAND (node, 1) == error_mark_node)
11628 return error_mark_node;
11629 if (!flag_emit_class_files && !flag_emit_xref)
11630 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
11631 return patch_array_ref (node);
11633 case RECORD_TYPE:
11634 return node;;
11636 case COMPONENT_REF:
11637 /* The first step in the re-write of qualified name handling. FIXME.
11638 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
11639 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11640 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11642 tree name = TREE_OPERAND (node, 1);
11643 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11644 if (field == NULL_TREE)
11646 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11647 return error_mark_node;
11649 if (! FIELD_STATIC (field))
11651 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11652 return error_mark_node;
11654 return field;
11656 else
11657 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
11658 break;
11660 case THIS_EXPR:
11661 /* Can't use THIS in a static environment */
11662 if (!current_this)
11664 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11665 parse_error_context (wfl_operator,
11666 "Keyword `this' used outside allowed context");
11667 TREE_TYPE (node) = error_mark_node;
11668 return error_mark_node;
11670 if (ctxp->explicit_constructor_p)
11672 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11673 parse_error_context
11674 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
11675 TREE_TYPE (node) = error_mark_node;
11676 return error_mark_node;
11678 return current_this;
11680 case CLASS_LITERAL:
11681 CAN_COMPLETE_NORMALLY (node) = 1;
11682 node = patch_incomplete_class_ref (node);
11683 if (node == error_mark_node)
11684 return error_mark_node;
11685 break;
11687 case INSTANCE_INITIALIZERS_EXPR:
11688 in_instance_initializer++;
11689 node = java_complete_tree (TREE_OPERAND (node, 0));
11690 in_instance_initializer--;
11691 if (node != error_mark_node)
11692 TREE_TYPE (node) = void_type_node;
11693 else
11694 return error_mark_node;
11695 break;
11697 default:
11698 CAN_COMPLETE_NORMALLY (node) = 1;
11699 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
11700 and it's time to turn it into the appropriate String object */
11701 if ((nn = patch_string (node)))
11702 node = nn;
11703 else
11704 fatal ("No case for tree code `%s' - java_complete_tree\n",
11705 tree_code_name [TREE_CODE (node)]);
11707 return node;
11710 /* Complete function call's argument. Return a non zero value is an
11711 error was found. */
11713 static int
11714 complete_function_arguments (node)
11715 tree node;
11717 int flag = 0;
11718 tree cn;
11720 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11721 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11723 tree wfl = TREE_VALUE (cn), parm, temp;
11724 parm = java_complete_tree (wfl);
11726 if (parm == error_mark_node)
11728 flag = 1;
11729 continue;
11731 /* If have a string literal that we haven't transformed yet or a
11732 crafted string buffer, as a result of use of the the String
11733 `+' operator. Build `parm.toString()' and expand it. */
11734 if ((temp = patch_string (parm)))
11735 parm = temp;
11736 /* Inline PRIMTYPE.TYPE read access */
11737 parm = maybe_build_primttype_type_ref (parm, wfl);
11739 TREE_VALUE (cn) = parm;
11741 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11742 return flag;
11745 /* Sometimes (for loops and variable initialized during their
11746 declaration), we want to wrap a statement around a WFL and turn it
11747 debugable. */
11749 static tree
11750 build_debugable_stmt (location, stmt)
11751 int location;
11752 tree stmt;
11754 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11756 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11757 EXPR_WFL_LINECOL (stmt) = location;
11759 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11760 return stmt;
11763 static tree
11764 build_expr_block (body, decls)
11765 tree body, decls;
11767 tree node = make_node (BLOCK);
11768 BLOCK_EXPR_DECLS (node) = decls;
11769 BLOCK_EXPR_BODY (node) = body;
11770 if (body)
11771 TREE_TYPE (node) = TREE_TYPE (body);
11772 TREE_SIDE_EFFECTS (node) = 1;
11773 return node;
11776 /* Create a new function block and link it approriately to current
11777 function block chain */
11779 static tree
11780 enter_block ()
11782 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11785 /* Link block B supercontext to the previous block. The current
11786 function DECL is used as supercontext when enter_a_block is called
11787 for the first time for a given function. The current function body
11788 (DECL_FUNCTION_BODY) is set to be block B. */
11790 static tree
11791 enter_a_block (b)
11792 tree b;
11794 tree fndecl = current_function_decl;
11796 if (!fndecl) {
11797 BLOCK_SUPERCONTEXT (b) = current_static_block;
11798 current_static_block = b;
11801 else if (!DECL_FUNCTION_BODY (fndecl))
11803 BLOCK_SUPERCONTEXT (b) = fndecl;
11804 DECL_FUNCTION_BODY (fndecl) = b;
11806 else
11808 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11809 DECL_FUNCTION_BODY (fndecl) = b;
11811 return b;
11814 /* Exit a block by changing the current function body
11815 (DECL_FUNCTION_BODY) to the current block super context, only if
11816 the block being exited isn't the method's top level one. */
11818 static tree
11819 exit_block ()
11821 tree b;
11822 if (current_function_decl)
11824 b = DECL_FUNCTION_BODY (current_function_decl);
11825 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11826 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11828 else
11830 b = current_static_block;
11832 if (BLOCK_SUPERCONTEXT (b))
11833 current_static_block = BLOCK_SUPERCONTEXT (b);
11835 return b;
11838 /* Lookup for NAME in the nested function's blocks, all the way up to
11839 the current toplevel one. It complies with Java's local variable
11840 scoping rules. */
11842 static tree
11843 lookup_name_in_blocks (name)
11844 tree name;
11846 tree b = GET_CURRENT_BLOCK (current_function_decl);
11848 while (b != current_function_decl)
11850 tree current;
11852 /* Paranoid sanity check. To be removed */
11853 if (TREE_CODE (b) != BLOCK)
11854 fatal ("non block expr function body - lookup_name_in_blocks");
11856 for (current = BLOCK_EXPR_DECLS (b); current;
11857 current = TREE_CHAIN (current))
11858 if (DECL_NAME (current) == name)
11859 return current;
11860 b = BLOCK_SUPERCONTEXT (b);
11862 return NULL_TREE;
11865 static void
11866 maybe_absorb_scoping_blocks ()
11868 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
11870 tree b = exit_block ();
11871 java_method_add_stmt (current_function_decl, b);
11872 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11877 /* This section of the source is reserved to build_* functions that
11878 are building incomplete tree nodes and the patch_* functions that
11879 are completing them. */
11881 /* Wrap a non WFL node around a WFL. */
11882 static tree
11883 build_wfl_wrap (node, location)
11884 tree node;
11885 int location;
11887 tree wfl, node_to_insert = node;
11889 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11890 with the way we're processing SUPER. A THIS from a primary as a
11891 different form than a SUPER. Turn THIS into something symbolic */
11892 if (TREE_CODE (node) == THIS_EXPR)
11893 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11894 else
11895 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11897 EXPR_WFL_LINECOL (wfl) = location;
11898 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11899 return wfl;
11903 /* Build a super() constructor invocation. Returns empty_stmt_node if
11904 we're currently dealing with the class java.lang.Object. */
11906 static tree
11907 build_super_invocation (mdecl)
11908 tree mdecl;
11910 if (DECL_CONTEXT (mdecl) == object_type_node)
11911 return empty_stmt_node;
11912 else
11914 tree super_wfl = build_wfl_node (super_identifier_node);
11915 tree a = NULL_TREE, t;
11916 /* If we're dealing with an anonymous class, pass the arguments
11917 of the crafted constructor along. */
11918 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11920 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11921 for (; t != end_params_node; t = TREE_CHAIN (t))
11922 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11924 return build_method_invocation (super_wfl, a);
11928 /* Build a SUPER/THIS qualified method invocation. */
11930 static tree
11931 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11932 int use_this;
11933 tree name, args;
11934 int lloc, rloc;
11936 tree invok;
11937 tree wfl =
11938 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
11939 EXPR_WFL_LINECOL (wfl) = lloc;
11940 invok = build_method_invocation (name, args);
11941 return make_qualified_primary (wfl, invok, rloc);
11944 /* Build an incomplete CALL_EXPR node. */
11946 static tree
11947 build_method_invocation (name, args)
11948 tree name;
11949 tree args;
11951 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11952 TREE_SIDE_EFFECTS (call) = 1;
11953 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11954 return call;
11957 /* Build an incomplete new xxx(...) node. */
11959 static tree
11960 build_new_invocation (name, args)
11961 tree name, args;
11963 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11964 TREE_SIDE_EFFECTS (call) = 1;
11965 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11966 return call;
11969 /* Build an incomplete assignment expression. */
11971 static tree
11972 build_assignment (op, op_location, lhs, rhs)
11973 int op, op_location;
11974 tree lhs, rhs;
11976 tree assignment;
11977 /* Build the corresponding binop if we deal with a Compound
11978 Assignment operator. Mark the binop sub-tree as part of a
11979 Compound Assignment expression */
11980 if (op != ASSIGN_TK)
11982 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11983 COMPOUND_ASSIGN_P (rhs) = 1;
11985 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11986 TREE_SIDE_EFFECTS (assignment) = 1;
11987 EXPR_WFL_LINECOL (assignment) = op_location;
11988 return assignment;
11991 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11993 char *
11994 print_int_node (node)
11995 tree node;
11997 static char buffer [80];
11998 if (TREE_CONSTANT_OVERFLOW (node))
11999 sprintf (buffer, "<overflow>");
12001 if (TREE_INT_CST_HIGH (node) == 0)
12002 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12003 TREE_INT_CST_LOW (node));
12004 else if (TREE_INT_CST_HIGH (node) == -1
12005 && TREE_INT_CST_LOW (node) != 0)
12007 buffer [0] = '-';
12008 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12009 -TREE_INT_CST_LOW (node));
12011 else
12012 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12013 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12015 return buffer;
12020 /* This section of the code handle assignment check with FINAL
12021 variables. */
12023 static void
12024 reset_static_final_variable_assignment_flag (class)
12025 tree class;
12027 tree field;
12028 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12029 if (CLASS_FINAL_VARIABLE_P (field))
12030 DECL_FIELD_FINAL_LIIC (field) = 0;
12033 /* Figure whether all final static variable have been initialized. */
12035 static void
12036 check_static_final_variable_assignment_flag (class)
12037 tree class;
12039 tree field;
12041 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12042 if (CLASS_FINAL_VARIABLE_P (field)
12043 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12044 parse_error_context
12045 (DECL_FIELD_FINAL_WFL (field),
12046 "Blank static final variable `%s' may not have been initialized",
12047 IDENTIFIER_POINTER (DECL_NAME (field)));
12050 /* This function marks all final variable locally unassigned. */
12052 static void
12053 reset_final_variable_local_assignment_flag (class)
12054 tree class;
12056 tree field;
12057 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12058 if (FINAL_VARIABLE_P (field))
12059 DECL_FIELD_FINAL_LIIC (field) = 0;
12062 /* Figure whether all final variables have beem initialized in MDECL
12063 and mark MDECL accordingly. */
12065 static void
12066 check_final_variable_local_assignment_flag (class, mdecl)
12067 tree class;
12068 tree mdecl;
12070 tree field;
12071 int initialized = 0;
12072 int non_initialized = 0;
12074 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12075 return;
12077 /* First find out whether all final variables or no final variable
12078 are initialized in this ctor. We don't take into account final
12079 variable that have been initialized upon declaration. */
12080 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12081 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12083 if (DECL_FIELD_FINAL_LIIC (field))
12084 initialized++;
12085 else
12086 non_initialized++;
12089 /* There were no non initialized variable and no initialized variable.
12090 This ctor is fine. */
12091 if (!non_initialized && !initialized)
12092 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12093 /* If no variables have been initialized, that fine. We'll check
12094 later whether this ctor calls a constructor which initializes
12095 them. We mark the ctor as not initializing all its finals. */
12096 else if (initialized == 0)
12097 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12098 /* If we have a mixed bag, then we have a problem. We need to report
12099 all the variables we're not initializing. */
12100 else if (initialized && non_initialized)
12102 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12103 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12104 if (FIELD_FINAL (field)
12105 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12107 parse_error_context
12108 (lookup_cl (mdecl),
12109 "Blank final variable `%s' may not have been initialized in this constructor",
12110 IDENTIFIER_POINTER (DECL_NAME (field)));
12111 DECL_FIELD_FINAL_IERR (field) = 1;
12114 /* Otherwise we know this ctor is initializing all its final
12115 variable. We mark it so. */
12116 else
12117 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12120 /* This function recurses in a simple what through STMT and stops when
12121 it finds a constructor call. It then verifies that the called
12122 constructor initialized its final properly. Return 1 upon success,
12123 0 or -1 otherwise. */
12125 static int
12126 check_final_variable_indirect_assignment (stmt)
12127 tree stmt;
12129 int res;
12130 switch (TREE_CODE (stmt))
12132 case EXPR_WITH_FILE_LOCATION:
12133 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12134 case COMPOUND_EXPR:
12135 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12136 if (res)
12137 return res;
12138 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12139 case SAVE_EXPR:
12140 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12141 case CALL_EXPR:
12143 tree decl = TREE_OPERAND (stmt, 0);
12144 tree fbody;
12146 if (TREE_CODE (decl) != FUNCTION_DECL)
12147 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12148 if (TREE_CODE (decl) != FUNCTION_DECL)
12149 fatal ("Can't find FUNCTION_DECL in CALL_EXPR - check_final_variable_indirect_assignment");
12150 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12151 return 1;
12152 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12153 return -1;
12154 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12155 if (fbody == error_mark_node)
12156 return -1;
12157 fbody = BLOCK_EXPR_BODY (fbody);
12158 return check_final_variable_indirect_assignment (fbody);
12160 default:
12161 break;
12163 return 0;
12166 /* This is the last chance to catch a final variable initialization
12167 problem. This routine will report an error if a final variable was
12168 never (globally) initialized and never reported as not having been
12169 initialized properly. */
12171 static void
12172 check_final_variable_global_assignment_flag (class)
12173 tree class;
12175 tree field, mdecl;
12176 int nnctor = 0;
12178 /* We go through all natural ctors and see whether they're
12179 initializing all their final variables or not. */
12180 current_function_decl = NULL_TREE; /* For the error report. */
12181 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12182 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12184 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12186 /* It doesn't. Maybe it calls a constructor that initializes
12187 them. find out. */
12188 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12189 if (fbody == error_mark_node)
12190 continue;
12191 fbody = BLOCK_EXPR_BODY (fbody);
12192 if (check_final_variable_indirect_assignment (fbody) == 1)
12194 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12195 nnctor++;
12197 else
12198 parse_error_context
12199 (lookup_cl (mdecl),
12200 "Final variable initialization error in this constructor");
12202 else
12203 nnctor++;
12206 /* Finally we catch final variables that never were initialized */
12207 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12208 if (FINAL_VARIABLE_P (field)
12209 /* If the field wasn't initialized upon declaration */
12210 && !DECL_FIELD_FINAL_IUD (field)
12211 /* There wasn't natural ctor in which the field could have been
12212 initialized */
12213 && !nnctor
12214 /* If we never reported a problem with this field */
12215 && !DECL_FIELD_FINAL_IERR (field))
12217 current_function_decl = NULL;
12218 parse_error_context
12219 (DECL_FIELD_FINAL_WFL (field),
12220 "Final variable `%s' hasn't been initialized upon its declaration",
12221 IDENTIFIER_POINTER (DECL_NAME (field)));
12226 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12227 context. */
12229 static int
12230 check_final_assignment (lvalue, wfl)
12231 tree lvalue, wfl;
12233 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12234 return 0;
12236 if (TREE_CODE (lvalue) == COMPONENT_REF
12237 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12238 lvalue = TREE_OPERAND (lvalue, 1);
12240 if (!FIELD_FINAL (lvalue))
12241 return 0;
12243 /* Now the logic. We can modify a final VARIABLE:
12244 1) in finit$, (its declaration was followed by an initialization,)
12245 2) consistently in each natural ctor, if it wasn't initialized in
12246 finit$ or once in <clinit>. In any other cases, an error should be
12247 reported. */
12248 if (DECL_FINIT_P (current_function_decl))
12250 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12251 return 0;
12254 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12255 /* Only if it wasn't given a value upon initialization */
12256 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12257 /* If it was never assigned a value in this constructor */
12258 && !DECL_FIELD_FINAL_LIIC (lvalue))
12260 /* Turn the locally assigned flag on, it will be checked later
12261 on to point out at discrepancies. */
12262 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12263 if (DECL_CLINIT_P (current_function_decl))
12264 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12265 return 0;
12268 /* Other problems should be reported right away. */
12269 parse_error_context
12270 (wfl, "Can't %sassign a value to the final variable `%s'",
12271 (FIELD_STATIC (lvalue) ? "re" : ""),
12272 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12274 /* Note that static field can be initialized once and only once. */
12275 if (FIELD_STATIC (lvalue))
12276 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12278 return 1;
12281 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12282 read. This is needed to avoid circularities in the implementation
12283 of these fields in libjava. */
12285 static tree
12286 maybe_build_primttype_type_ref (rhs, wfl)
12287 tree rhs, wfl;
12289 tree to_return = NULL_TREE;
12290 tree rhs_type = TREE_TYPE (rhs);
12291 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12293 tree n = TREE_OPERAND (rhs, 1);
12294 if (TREE_CODE (n) == VAR_DECL
12295 && DECL_NAME (n) == TYPE_identifier_node
12296 && rhs_type == class_ptr_type
12297 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
12299 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
12300 if (!strncmp (self_name, "java.lang.", 10))
12301 to_return = build_primtype_type_ref (self_name);
12304 return (to_return ? to_return : rhs );
12307 /* 15.25 Assignment operators. */
12309 static tree
12310 patch_assignment (node, wfl_op1, wfl_op2)
12311 tree node;
12312 tree wfl_op1;
12313 tree wfl_op2;
12315 tree rhs = TREE_OPERAND (node, 1);
12316 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12317 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12318 int error_found = 0;
12319 int lvalue_from_array = 0;
12321 /* Can't assign to a (blank) final. */
12322 if (check_final_assignment (lvalue, wfl_op1))
12323 error_found = 1;
12325 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12327 /* Lhs can be a named variable */
12328 if (JDECL_P (lvalue))
12330 lhs_type = TREE_TYPE (lvalue);
12332 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12333 comment on reason why */
12334 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12336 lhs_type = TREE_TYPE (lvalue);
12337 lvalue_from_array = 1;
12339 /* Or a field access */
12340 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12341 lhs_type = TREE_TYPE (lvalue);
12342 /* Or a function return slot */
12343 else if (TREE_CODE (lvalue) == RESULT_DECL)
12344 lhs_type = TREE_TYPE (lvalue);
12345 /* Otherwise, we might want to try to write into an optimized static
12346 final, this is an of a different nature, reported further on. */
12347 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12348 && resolve_expression_name (wfl_op1, &llvalue))
12350 if (!error_found && check_final_assignment (llvalue, wfl_op1))
12352 /* What we should do instead is resetting the all the flags
12353 previously set, exchange lvalue for llvalue and continue. */
12354 error_found = 1;
12355 return error_mark_node;
12357 else
12358 lhs_type = TREE_TYPE (lvalue);
12360 else
12362 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12363 error_found = 1;
12366 rhs_type = TREE_TYPE (rhs);
12367 /* 5.1 Try the assignment conversion for builtin type. */
12368 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12370 /* 5.2 If it failed, try a reference conversion */
12371 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12372 lhs_type = promote_type (rhs_type);
12374 /* 15.25.2 If we have a compound assignment, convert RHS into the
12375 type of the LHS */
12376 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12377 new_rhs = convert (lhs_type, rhs);
12379 /* Explicit cast required. This is an error */
12380 if (!new_rhs)
12382 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12383 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12384 tree wfl;
12385 char operation [32]; /* Max size known */
12387 /* If the assignment is part of a declaration, we use the WFL of
12388 the declared variable to point out the error and call it a
12389 declaration problem. If the assignment is a genuine =
12390 operator, we call is a operator `=' problem, otherwise we
12391 call it an assignment problem. In both of these last cases,
12392 we use the WFL of the operator to indicate the error. */
12394 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12396 wfl = wfl_op1;
12397 strcpy (operation, "declaration");
12399 else
12401 wfl = wfl_operator;
12402 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12403 strcpy (operation, "assignment");
12404 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12405 strcpy (operation, "`return'");
12406 else
12407 strcpy (operation, "`='");
12410 if (!valid_cast_to_p (rhs_type, lhs_type))
12411 parse_error_context
12412 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12413 operation, t1, t2);
12414 else
12415 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12416 operation, t1, t2);
12417 free (t1); free (t2);
12418 error_found = 1;
12421 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12422 if (new_rhs)
12423 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
12425 if (error_found)
12426 return error_mark_node;
12428 /* 10.10: Array Store Exception runtime check */
12429 if (!flag_emit_class_files
12430 && !flag_emit_xref
12431 && lvalue_from_array
12432 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12434 tree check;
12435 tree base = lvalue;
12437 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12438 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12439 base = TREE_OPERAND (lvalue, 0);
12440 else
12442 if (flag_bounds_check)
12443 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12444 else
12445 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12448 /* Build the invocation of _Jv_CheckArrayStore */
12449 new_rhs = save_expr (new_rhs);
12450 check = build (CALL_EXPR, void_type_node,
12451 build_address_of (soft_checkarraystore_node),
12452 tree_cons (NULL_TREE, base,
12453 build_tree_list (NULL_TREE, new_rhs)),
12454 NULL_TREE);
12455 TREE_SIDE_EFFECTS (check) = 1;
12457 /* We have to decide on an insertion point */
12458 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12460 tree t;
12461 if (flag_bounds_check)
12463 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12464 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12465 build (COMPOUND_EXPR, void_type_node, t, check);
12467 else
12468 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12469 check, TREE_OPERAND (lvalue, 1));
12471 else
12473 /* Make sure the bound check will happen before the store check */
12474 if (flag_bounds_check)
12475 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12476 build (COMPOUND_EXPR, void_type_node,
12477 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12478 else
12479 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12483 /* Final locals can be used as case values in switch
12484 statement. Prepare them for this eventuality. */
12485 if (TREE_CODE (lvalue) == VAR_DECL
12486 && LOCAL_FINAL_P (lvalue)
12487 && TREE_CONSTANT (new_rhs)
12488 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12489 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12492 TREE_CONSTANT (lvalue) = 1;
12493 DECL_INITIAL (lvalue) = new_rhs;
12496 TREE_OPERAND (node, 0) = lvalue;
12497 TREE_OPERAND (node, 1) = new_rhs;
12498 TREE_TYPE (node) = lhs_type;
12499 return node;
12502 /* Check that type SOURCE can be cast into type DEST. If the cast
12503 can't occur at all, return 0 otherwise 1. This function is used to
12504 produce accurate error messages on the reasons why an assignment
12505 failed. */
12507 static tree
12508 try_reference_assignconv (lhs_type, rhs)
12509 tree lhs_type, rhs;
12511 tree new_rhs = NULL_TREE;
12512 tree rhs_type = TREE_TYPE (rhs);
12514 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12516 /* `null' may be assigned to any reference type */
12517 if (rhs == null_pointer_node)
12518 new_rhs = null_pointer_node;
12519 /* Try the reference assignment conversion */
12520 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12521 new_rhs = rhs;
12522 /* This is a magic assignment that we process differently */
12523 else if (rhs == soft_exceptioninfo_call_node)
12524 new_rhs = rhs;
12526 return new_rhs;
12529 /* Check that RHS can be converted into LHS_TYPE by the assignment
12530 conversion (5.2), for the cases of RHS being a builtin type. Return
12531 NULL_TREE if the conversion fails or if because RHS isn't of a
12532 builtin type. Return a converted RHS if the conversion is possible. */
12534 static tree
12535 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12536 tree wfl_op1, lhs_type, rhs;
12538 tree new_rhs = NULL_TREE;
12539 tree rhs_type = TREE_TYPE (rhs);
12541 /* Handle boolean specially. */
12542 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12543 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12545 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12546 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12547 new_rhs = rhs;
12550 /* Zero accepted everywhere */
12551 else if (TREE_CODE (rhs) == INTEGER_CST
12552 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12553 && JPRIMITIVE_TYPE_P (rhs_type))
12554 new_rhs = convert (lhs_type, rhs);
12556 /* 5.1.1 Try Identity Conversion,
12557 5.1.2 Try Widening Primitive Conversion */
12558 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12559 new_rhs = convert (lhs_type, rhs);
12561 /* Try a narrowing primitive conversion (5.1.3):
12562 - expression is a constant expression of type int AND
12563 - variable is byte, short or char AND
12564 - The value of the expression is representable in the type of the
12565 variable */
12566 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12567 && (lhs_type == byte_type_node || lhs_type == char_type_node
12568 || lhs_type == short_type_node))
12570 if (int_fits_type_p (rhs, lhs_type))
12571 new_rhs = convert (lhs_type, rhs);
12572 else if (wfl_op1) /* Might be called with a NULL */
12573 parse_warning_context
12574 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12575 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12576 /* Reported a warning that will turn into an error further
12577 down, so we don't return */
12580 return new_rhs;
12583 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12584 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12585 0 is the conversion test fails. This implements parts the method
12586 invocation convertion (5.3). */
12588 static int
12589 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12590 tree lhs_type, rhs_type;
12592 /* 5.1.1: This is the identity conversion part. */
12593 if (lhs_type == rhs_type)
12594 return 1;
12596 /* Reject non primitive types and boolean conversions. */
12597 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12598 return 0;
12600 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12601 than a char can't be converted into a char. Short can't too, but
12602 the < test below takes care of that */
12603 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12604 return 0;
12606 /* Accept all promoted type here. Note, we can't use <= in the test
12607 below, because we still need to bounce out assignments of short
12608 to char and the likes */
12609 if (lhs_type == int_type_node
12610 && (rhs_type == promoted_byte_type_node
12611 || rhs_type == promoted_short_type_node
12612 || rhs_type == promoted_char_type_node
12613 || rhs_type == promoted_boolean_type_node))
12614 return 1;
12616 /* From here, an integral is widened if its precision is smaller
12617 than the precision of the LHS or if the LHS is a floating point
12618 type, or the RHS is a float and the RHS a double. */
12619 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12620 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12621 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12622 || (rhs_type == float_type_node && lhs_type == double_type_node))
12623 return 1;
12625 return 0;
12628 /* Check that something of SOURCE type can be assigned or cast to
12629 something of DEST type at runtime. Return 1 if the operation is
12630 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12631 were SOURCE is cast into DEST, which borrows a lot of the
12632 assignment check. */
12634 static int
12635 valid_ref_assignconv_cast_p (source, dest, cast)
12636 tree source;
12637 tree dest;
12638 int cast;
12640 /* SOURCE or DEST might be null if not from a declared entity. */
12641 if (!source || !dest)
12642 return 0;
12643 if (JNULLP_TYPE_P (source))
12644 return 1;
12645 if (TREE_CODE (source) == POINTER_TYPE)
12646 source = TREE_TYPE (source);
12647 if (TREE_CODE (dest) == POINTER_TYPE)
12648 dest = TREE_TYPE (dest);
12650 /* If source and dest are being compiled from bytecode, they may need to
12651 be loaded. */
12652 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12654 load_class (source, 1);
12655 safe_layout_class (source);
12657 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12659 load_class (dest, 1);
12660 safe_layout_class (dest);
12663 /* Case where SOURCE is a class type */
12664 if (TYPE_CLASS_P (source))
12666 if (TYPE_CLASS_P (dest))
12667 return (source == dest
12668 || inherits_from_p (source, dest)
12669 || (cast && inherits_from_p (dest, source)));
12670 if (TYPE_INTERFACE_P (dest))
12672 /* If doing a cast and SOURCE is final, the operation is
12673 always correct a compile time (because even if SOURCE
12674 does not implement DEST, a subclass of SOURCE might). */
12675 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12676 return 1;
12677 /* Otherwise, SOURCE must implement DEST */
12678 return interface_of_p (dest, source);
12680 /* DEST is an array, cast permited if SOURCE is of Object type */
12681 return (cast && source == object_type_node ? 1 : 0);
12683 if (TYPE_INTERFACE_P (source))
12685 if (TYPE_CLASS_P (dest))
12687 /* If not casting, DEST must be the Object type */
12688 if (!cast)
12689 return dest == object_type_node;
12690 /* We're doing a cast. The cast is always valid is class
12691 DEST is not final, otherwise, DEST must implement SOURCE */
12692 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12693 return 1;
12694 else
12695 return interface_of_p (source, dest);
12697 if (TYPE_INTERFACE_P (dest))
12699 /* If doing a cast, then if SOURCE and DEST contain method
12700 with the same signature but different return type, then
12701 this is a (compile time) error */
12702 if (cast)
12704 tree method_source, method_dest;
12705 tree source_type;
12706 tree source_sig;
12707 tree source_name;
12708 for (method_source = TYPE_METHODS (source); method_source;
12709 method_source = TREE_CHAIN (method_source))
12711 source_sig =
12712 build_java_argument_signature (TREE_TYPE (method_source));
12713 source_type = TREE_TYPE (TREE_TYPE (method_source));
12714 source_name = DECL_NAME (method_source);
12715 for (method_dest = TYPE_METHODS (dest);
12716 method_dest; method_dest = TREE_CHAIN (method_dest))
12717 if (source_sig ==
12718 build_java_argument_signature (TREE_TYPE (method_dest))
12719 && source_name == DECL_NAME (method_dest)
12720 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12721 return 0;
12723 return 1;
12725 else
12726 return source == dest || interface_of_p (dest, source);
12728 else
12730 /* Array */
12731 return (cast
12732 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12733 || (DECL_NAME (TYPE_NAME (source))
12734 == java_io_serializable)));
12737 if (TYPE_ARRAY_P (source))
12739 if (TYPE_CLASS_P (dest))
12740 return dest == object_type_node;
12741 /* Can't cast an array to an interface unless the interface is
12742 java.lang.Cloneable or java.io.Serializable. */
12743 if (TYPE_INTERFACE_P (dest))
12744 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12745 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
12746 else /* Arrays */
12748 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12749 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12751 /* In case of severe errors, they turn out null */
12752 if (!dest_element_type || !source_element_type)
12753 return 0;
12754 if (source_element_type == dest_element_type)
12755 return 1;
12756 return valid_ref_assignconv_cast_p (source_element_type,
12757 dest_element_type, cast);
12759 return 0;
12761 return 0;
12764 static int
12765 valid_cast_to_p (source, dest)
12766 tree source;
12767 tree dest;
12769 if (TREE_CODE (source) == POINTER_TYPE)
12770 source = TREE_TYPE (source);
12771 if (TREE_CODE (dest) == POINTER_TYPE)
12772 dest = TREE_TYPE (dest);
12774 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12775 return valid_ref_assignconv_cast_p (source, dest, 1);
12777 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12778 return 1;
12780 else if (TREE_CODE (source) == BOOLEAN_TYPE
12781 && TREE_CODE (dest) == BOOLEAN_TYPE)
12782 return 1;
12784 return 0;
12787 static tree
12788 do_unary_numeric_promotion (arg)
12789 tree arg;
12791 tree type = TREE_TYPE (arg);
12792 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12793 || TREE_CODE (type) == CHAR_TYPE)
12794 arg = convert (int_type_node, arg);
12795 return arg;
12798 /* Return a non zero value if SOURCE can be converted into DEST using
12799 the method invocation conversion rule (5.3). */
12800 static int
12801 valid_method_invocation_conversion_p (dest, source)
12802 tree dest, source;
12804 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12805 && valid_builtin_assignconv_identity_widening_p (dest, source))
12806 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12807 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12808 && valid_ref_assignconv_cast_p (source, dest, 0)));
12811 /* Build an incomplete binop expression. */
12813 static tree
12814 build_binop (op, op_location, op1, op2)
12815 enum tree_code op;
12816 int op_location;
12817 tree op1, op2;
12819 tree binop = build (op, NULL_TREE, op1, op2);
12820 TREE_SIDE_EFFECTS (binop) = 1;
12821 /* Store the location of the operator, for better error report. The
12822 string of the operator will be rebuild based on the OP value. */
12823 EXPR_WFL_LINECOL (binop) = op_location;
12824 return binop;
12827 /* Build the string of the operator retained by NODE. If NODE is part
12828 of a compound expression, add an '=' at the end of the string. This
12829 function is called when an error needs to be reported on an
12830 operator. The string is returned as a pointer to a static character
12831 buffer. */
12833 static char *
12834 operator_string (node)
12835 tree node;
12837 #define BUILD_OPERATOR_STRING(S) \
12839 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12840 return buffer; \
12843 static char buffer [10];
12844 switch (TREE_CODE (node))
12846 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12847 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12848 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12849 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12850 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12851 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12852 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12853 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12854 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12855 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12856 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12857 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12858 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12859 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12860 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12861 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12862 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12863 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12864 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
12865 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12866 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12867 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12868 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12869 case PREINCREMENT_EXPR: /* Fall through */
12870 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12871 case PREDECREMENT_EXPR: /* Fall through */
12872 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12873 default:
12874 fatal ("unregistered operator %s - operator_string",
12875 tree_code_name [TREE_CODE (node)]);
12877 return NULL;
12878 #undef BUILD_OPERATOR_STRING
12881 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12883 static int
12884 java_decl_equiv (var_acc1, var_acc2)
12885 tree var_acc1, var_acc2;
12887 if (JDECL_P (var_acc1))
12888 return (var_acc1 == var_acc2);
12890 return (TREE_CODE (var_acc1) == COMPONENT_REF
12891 && TREE_CODE (var_acc2) == COMPONENT_REF
12892 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12893 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12894 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12897 /* Return a non zero value if CODE is one of the operators that can be
12898 used in conjunction with the `=' operator in a compound assignment. */
12900 static int
12901 binop_compound_p (code)
12902 enum tree_code code;
12904 int i;
12905 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12906 if (binop_lookup [i] == code)
12907 break;
12909 return i < BINOP_COMPOUND_CANDIDATES;
12912 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12914 static tree
12915 java_refold (t)
12916 tree t;
12918 tree c, b, ns, decl;
12920 if (TREE_CODE (t) != MODIFY_EXPR)
12921 return t;
12923 c = TREE_OPERAND (t, 1);
12924 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12925 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12926 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12927 return t;
12929 /* Now the left branch of the binary operator. */
12930 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12931 if (! (b && TREE_CODE (b) == NOP_EXPR
12932 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12933 return t;
12935 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12936 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12937 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12938 return t;
12940 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12941 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12942 /* It's got to be the an equivalent decl */
12943 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12945 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12946 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12947 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12948 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12949 /* Change the right part of the BINOP_EXPR */
12950 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12953 return t;
12956 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
12957 errors but we modify NODE so that it contains the type computed
12958 according to the expression, when it's fixed. Otherwise, we write
12959 error_mark_node as the type. It allows us to further the analysis
12960 of remaining nodes and detects more errors in certain cases. */
12962 static tree
12963 patch_binop (node, wfl_op1, wfl_op2)
12964 tree node;
12965 tree wfl_op1;
12966 tree wfl_op2;
12968 tree op1 = TREE_OPERAND (node, 0);
12969 tree op2 = TREE_OPERAND (node, 1);
12970 tree op1_type = TREE_TYPE (op1);
12971 tree op2_type = TREE_TYPE (op2);
12972 tree prom_type = NULL_TREE, cn;
12973 int code = TREE_CODE (node);
12975 /* If 1, tell the routine that we have to return error_mark_node
12976 after checking for the initialization of the RHS */
12977 int error_found = 0;
12979 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12981 /* If either op<n>_type are NULL, this might be early signs of an
12982 error situation, unless it's too early to tell (in case we're
12983 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
12984 correctly so the error can be later on reported accurately. */
12985 if (! (code == PLUS_EXPR || code == NE_EXPR
12986 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
12988 tree n;
12989 if (! op1_type)
12991 n = java_complete_tree (op1);
12992 op1_type = TREE_TYPE (n);
12994 if (! op2_type)
12996 n = java_complete_tree (op2);
12997 op2_type = TREE_TYPE (n);
13001 switch (code)
13003 /* 15.16 Multiplicative operators */
13004 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13005 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13006 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13007 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13008 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13010 if (!JNUMERIC_TYPE_P (op1_type))
13011 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13012 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13013 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13014 TREE_TYPE (node) = error_mark_node;
13015 error_found = 1;
13016 break;
13018 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13019 /* Change the division operator if necessary */
13020 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13021 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13023 if (TREE_CODE (prom_type) == INTEGER_TYPE
13024 && flag_use_divide_subroutine
13025 && ! flag_emit_class_files
13026 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13027 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13029 /* This one is more complicated. FLOATs are processed by a
13030 function call to soft_fmod. Duplicate the value of the
13031 COMPOUND_ASSIGN_P flag. */
13032 if (code == TRUNC_MOD_EXPR)
13034 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13035 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13036 TREE_SIDE_EFFECTS (mod)
13037 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13038 return mod;
13040 break;
13042 /* 15.17 Additive Operators */
13043 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13045 /* Operation is valid if either one argument is a string
13046 constant, a String object or a StringBuffer crafted for the
13047 purpose of the a previous usage of the String concatenation
13048 operator */
13050 if (TREE_CODE (op1) == STRING_CST
13051 || TREE_CODE (op2) == STRING_CST
13052 || JSTRING_TYPE_P (op1_type)
13053 || JSTRING_TYPE_P (op2_type)
13054 || IS_CRAFTED_STRING_BUFFER_P (op1)
13055 || IS_CRAFTED_STRING_BUFFER_P (op2))
13056 return build_string_concatenation (op1, op2);
13058 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13059 Numeric Types */
13060 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13062 if (!JNUMERIC_TYPE_P (op1_type))
13063 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13064 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13065 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13066 TREE_TYPE (node) = error_mark_node;
13067 error_found = 1;
13068 break;
13070 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13071 break;
13073 /* 15.18 Shift Operators */
13074 case LSHIFT_EXPR:
13075 case RSHIFT_EXPR:
13076 case URSHIFT_EXPR:
13077 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13079 if (!JINTEGRAL_TYPE_P (op1_type))
13080 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13081 else
13083 if (JNUMERIC_TYPE_P (op2_type))
13084 parse_error_context (wfl_operator,
13085 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13086 operator_string (node),
13087 lang_printable_name (op2_type, 0));
13088 else
13089 parse_error_context (wfl_operator,
13090 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13091 operator_string (node),
13092 lang_printable_name (op2_type, 0));
13094 TREE_TYPE (node) = error_mark_node;
13095 error_found = 1;
13096 break;
13099 /* Unary numeric promotion (5.6.1) is performed on each operand
13100 separatly */
13101 op1 = do_unary_numeric_promotion (op1);
13102 op2 = do_unary_numeric_promotion (op2);
13104 /* The type of the shift expression is the type of the promoted
13105 type of the left-hand operand */
13106 prom_type = TREE_TYPE (op1);
13108 /* Shift int only up to 0x1f and long up to 0x3f */
13109 if (prom_type == int_type_node)
13110 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13111 build_int_2 (0x1f, 0)));
13112 else
13113 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13114 build_int_2 (0x3f, 0)));
13116 /* The >>> operator is a >> operating on unsigned quantities */
13117 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13119 tree to_return;
13120 tree utype = unsigned_type (prom_type);
13121 op1 = convert (utype, op1);
13122 TREE_SET_CODE (node, RSHIFT_EXPR);
13123 TREE_OPERAND (node, 0) = op1;
13124 TREE_OPERAND (node, 1) = op2;
13125 TREE_TYPE (node) = utype;
13126 to_return = convert (prom_type, node);
13127 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13128 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13129 TREE_SIDE_EFFECTS (to_return)
13130 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13131 return to_return;
13133 break;
13135 /* 15.19.1 Type Comparison Operator instaceof */
13136 case INSTANCEOF_EXPR:
13138 TREE_TYPE (node) = boolean_type_node;
13140 if (!(op2_type = resolve_type_during_patch (op2)))
13141 return error_mark_node;
13143 /* The first operand must be a reference type or the null type */
13144 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13145 error_found = 1; /* Error reported further below */
13147 /* The second operand must be a reference type */
13148 if (!JREFERENCE_TYPE_P (op2_type))
13150 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13151 parse_error_context
13152 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13153 lang_printable_name (op2_type, 0));
13154 error_found = 1;
13157 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13159 /* If the first operand is null, the result is always false */
13160 if (op1 == null_pointer_node)
13161 return boolean_false_node;
13162 else if (flag_emit_class_files)
13164 TREE_OPERAND (node, 1) = op2_type;
13165 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13166 return node;
13168 /* Otherwise we have to invoke instance of to figure it out */
13169 else
13170 return build_instanceof (op1, op2_type);
13172 /* There is no way the expression operand can be an instance of
13173 the type operand. This is a compile time error. */
13174 else
13176 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13177 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13178 parse_error_context
13179 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13180 t1, lang_printable_name (op2_type, 0));
13181 free (t1);
13182 error_found = 1;
13185 break;
13187 /* 15.21 Bitwise and Logical Operators */
13188 case BIT_AND_EXPR:
13189 case BIT_XOR_EXPR:
13190 case BIT_IOR_EXPR:
13191 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13192 /* Binary numeric promotion is performed on both operand and the
13193 expression retain that type */
13194 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13196 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13197 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13198 /* The type of the bitwise operator expression is BOOLEAN */
13199 prom_type = boolean_type_node;
13200 else
13202 if (!JINTEGRAL_TYPE_P (op1_type))
13203 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13204 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13205 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13206 TREE_TYPE (node) = error_mark_node;
13207 error_found = 1;
13208 /* Insert a break here if adding thing before the switch's
13209 break for this case */
13211 break;
13213 /* 15.22 Conditional-And Operator */
13214 case TRUTH_ANDIF_EXPR:
13215 /* 15.23 Conditional-Or Operator */
13216 case TRUTH_ORIF_EXPR:
13217 /* Operands must be of BOOLEAN type */
13218 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13219 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13221 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13222 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13223 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13224 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13225 TREE_TYPE (node) = boolean_type_node;
13226 error_found = 1;
13227 break;
13229 /* The type of the conditional operators is BOOLEAN */
13230 prom_type = boolean_type_node;
13231 break;
13233 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13234 case LT_EXPR:
13235 case GT_EXPR:
13236 case LE_EXPR:
13237 case GE_EXPR:
13238 /* The type of each of the operands must be a primitive numeric
13239 type */
13240 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13242 if (!JNUMERIC_TYPE_P (op1_type))
13243 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13244 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13245 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13246 TREE_TYPE (node) = boolean_type_node;
13247 error_found = 1;
13248 break;
13250 /* Binary numeric promotion is performed on the operands */
13251 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13252 /* The type of the relation expression is always BOOLEAN */
13253 prom_type = boolean_type_node;
13254 break;
13256 /* 15.20 Equality Operator */
13257 case EQ_EXPR:
13258 case NE_EXPR:
13259 /* It's time for us to patch the strings. */
13260 if ((cn = patch_string (op1)))
13262 op1 = cn;
13263 op1_type = TREE_TYPE (op1);
13265 if ((cn = patch_string (op2)))
13267 op2 = cn;
13268 op2_type = TREE_TYPE (op2);
13271 /* 15.20.1 Numerical Equality Operators == and != */
13272 /* Binary numeric promotion is performed on the operands */
13273 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13274 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13276 /* 15.20.2 Boolean Equality Operators == and != */
13277 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13278 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13279 ; /* Nothing to do here */
13281 /* 15.20.3 Reference Equality Operators == and != */
13282 /* Types have to be either references or the null type. If
13283 they're references, it must be possible to convert either
13284 type to the other by casting conversion. */
13285 else if (op1 == null_pointer_node || op2 == null_pointer_node
13286 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13287 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13288 || valid_ref_assignconv_cast_p (op2_type,
13289 op1_type, 1))))
13290 ; /* Nothing to do here */
13292 /* Else we have an error figure what can't be converted into
13293 what and report the error */
13294 else
13296 char *t1;
13297 t1 = xstrdup (lang_printable_name (op1_type, 0));
13298 parse_error_context
13299 (wfl_operator,
13300 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13301 operator_string (node), t1,
13302 lang_printable_name (op2_type, 0));
13303 free (t1);
13304 TREE_TYPE (node) = boolean_type_node;
13305 error_found = 1;
13306 break;
13308 prom_type = boolean_type_node;
13309 break;
13312 if (error_found)
13313 return error_mark_node;
13315 TREE_OPERAND (node, 0) = op1;
13316 TREE_OPERAND (node, 1) = op2;
13317 TREE_TYPE (node) = prom_type;
13318 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13320 if (flag_emit_xref)
13321 return node;
13323 /* fold does not respect side-effect order as required for Java but not C.
13324 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13325 * bytecode.
13327 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13328 : ! TREE_SIDE_EFFECTS (node))
13329 node = fold (node);
13330 return node;
13333 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13334 zero value, the value of CSTE comes after the valude of STRING */
13336 static tree
13337 do_merge_string_cste (cste, string, string_len, after)
13338 tree cste;
13339 const char *string;
13340 int string_len, after;
13342 const char *old = TREE_STRING_POINTER (cste);
13343 int old_len = TREE_STRING_LENGTH (cste);
13344 int len = old_len + string_len;
13345 char *new = alloca (len+1);
13347 if (after)
13349 memcpy (new, string, string_len);
13350 memcpy (&new [string_len], old, old_len);
13352 else
13354 memcpy (new, old, old_len);
13355 memcpy (&new [old_len], string, string_len);
13357 new [len] = '\0';
13358 return build_string (len, new);
13361 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13362 new STRING_CST on success, NULL_TREE on failure */
13364 static tree
13365 merge_string_cste (op1, op2, after)
13366 tree op1, op2;
13367 int after;
13369 /* Handle two string constants right away */
13370 if (TREE_CODE (op2) == STRING_CST)
13371 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13372 TREE_STRING_LENGTH (op2), after);
13374 /* Reasonable integer constant can be treated right away */
13375 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13377 static const char *boolean_true = "true";
13378 static const char *boolean_false = "false";
13379 static const char *null_pointer = "null";
13380 char ch[3];
13381 const char *string;
13383 if (op2 == boolean_true_node)
13384 string = boolean_true;
13385 else if (op2 == boolean_false_node)
13386 string = boolean_false;
13387 else if (op2 == null_pointer_node)
13388 string = null_pointer;
13389 else if (TREE_TYPE (op2) == char_type_node)
13391 ch[0] = (char )TREE_INT_CST_LOW (op2);
13392 ch[1] = '\0';
13393 string = ch;
13395 else
13396 string = print_int_node (op2);
13398 return do_merge_string_cste (op1, string, strlen (string), after);
13400 return NULL_TREE;
13403 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13404 has to be a STRING_CST and the other part must be a STRING_CST or a
13405 INTEGRAL constant. Return a new STRING_CST if the operation
13406 succeed, NULL_TREE otherwise.
13408 If the case we want to optimize for space, we might want to return
13409 NULL_TREE for each invocation of this routine. FIXME */
13411 static tree
13412 string_constant_concatenation (op1, op2)
13413 tree op1, op2;
13415 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13417 tree string, rest;
13418 int invert;
13420 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13421 rest = (string == op1 ? op2 : op1);
13422 invert = (string == op1 ? 0 : 1 );
13424 /* Walk REST, only if it looks reasonable */
13425 if (TREE_CODE (rest) != STRING_CST
13426 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13427 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13428 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13430 rest = java_complete_tree (rest);
13431 if (rest == error_mark_node)
13432 return error_mark_node;
13433 rest = fold (rest);
13435 return merge_string_cste (string, rest, invert);
13437 return NULL_TREE;
13440 /* Implement the `+' operator. Does static optimization if possible,
13441 otherwise create (if necessary) and append elements to a
13442 StringBuffer. The StringBuffer will be carried around until it is
13443 used for a function call or an assignment. Then toString() will be
13444 called on it to turn it into a String object. */
13446 static tree
13447 build_string_concatenation (op1, op2)
13448 tree op1, op2;
13450 tree result;
13451 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13453 if (flag_emit_xref)
13454 return build (PLUS_EXPR, string_type_node, op1, op2);
13456 /* Try to do some static optimization */
13457 if ((result = string_constant_concatenation (op1, op2)))
13458 return result;
13460 /* Discard empty strings on either side of the expression */
13461 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13463 op1 = op2;
13464 op2 = NULL_TREE;
13466 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13467 op2 = NULL_TREE;
13469 /* If operands are string constant, turn then into object references */
13470 if (TREE_CODE (op1) == STRING_CST)
13471 op1 = patch_string_cst (op1);
13472 if (op2 && TREE_CODE (op2) == STRING_CST)
13473 op2 = patch_string_cst (op2);
13475 /* If either one of the constant is null and the other non null
13476 operand is a String object, return it. */
13477 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13478 return op1;
13480 /* If OP1 isn't already a StringBuffer, create and
13481 initialize a new one */
13482 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13484 /* Two solutions here:
13485 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13486 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13487 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13488 op1 = BUILD_STRING_BUFFER (op1);
13489 else
13491 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13492 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13496 if (op2)
13498 /* OP1 is no longer the last node holding a crafted StringBuffer */
13499 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13500 /* Create a node for `{new...,xxx}.append (op2)' */
13501 if (op2)
13502 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13505 /* Mark the last node holding a crafted StringBuffer */
13506 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13508 TREE_SIDE_EFFECTS (op1) = side_effects;
13509 return op1;
13512 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13513 StringBuffer. If no string were found to be patched, return
13514 NULL. */
13516 static tree
13517 patch_string (node)
13518 tree node;
13520 if (node == error_mark_node)
13521 return error_mark_node;
13522 if (TREE_CODE (node) == STRING_CST)
13523 return patch_string_cst (node);
13524 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13526 int saved = ctxp->explicit_constructor_p;
13527 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13528 tree ret;
13529 /* Temporary disable forbid the use of `this'. */
13530 ctxp->explicit_constructor_p = 0;
13531 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13532 /* String concatenation arguments must be evaluated in order too. */
13533 ret = force_evaluation_order (ret);
13534 /* Restore it at its previous value */
13535 ctxp->explicit_constructor_p = saved;
13536 return ret;
13538 return NULL_TREE;
13541 /* Build the internal representation of a string constant. */
13543 static tree
13544 patch_string_cst (node)
13545 tree node;
13547 int location;
13548 if (! flag_emit_class_files)
13550 node = get_identifier (TREE_STRING_POINTER (node));
13551 location = alloc_name_constant (CONSTANT_String, node);
13552 node = build_ref_from_constant_pool (location);
13554 TREE_TYPE (node) = string_ptr_type_node;
13555 TREE_CONSTANT (node) = 1;
13556 return node;
13559 /* Build an incomplete unary operator expression. */
13561 static tree
13562 build_unaryop (op_token, op_location, op1)
13563 int op_token, op_location;
13564 tree op1;
13566 enum tree_code op;
13567 tree unaryop;
13568 switch (op_token)
13570 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13571 case MINUS_TK: op = NEGATE_EXPR; break;
13572 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13573 case NOT_TK: op = BIT_NOT_EXPR; break;
13574 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13575 op_token);
13578 unaryop = build1 (op, NULL_TREE, op1);
13579 TREE_SIDE_EFFECTS (unaryop) = 1;
13580 /* Store the location of the operator, for better error report. The
13581 string of the operator will be rebuild based on the OP value. */
13582 EXPR_WFL_LINECOL (unaryop) = op_location;
13583 return unaryop;
13586 /* Special case for the ++/-- operators, since they require an extra
13587 argument to build, which is set to NULL and patched
13588 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13590 static tree
13591 build_incdec (op_token, op_location, op1, is_post_p)
13592 int op_token, op_location;
13593 tree op1;
13594 int is_post_p;
13596 static enum tree_code lookup [2][2] =
13598 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13599 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13601 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13602 NULL_TREE, op1, NULL_TREE);
13603 TREE_SIDE_EFFECTS (node) = 1;
13604 /* Store the location of the operator, for better error report. The
13605 string of the operator will be rebuild based on the OP value. */
13606 EXPR_WFL_LINECOL (node) = op_location;
13607 return node;
13610 /* Build an incomplete cast operator, based on the use of the
13611 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13612 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13613 though its type is already set. */
13615 static tree
13616 build_cast (location, type, exp)
13617 int location;
13618 tree type, exp;
13620 tree node = build1 (CONVERT_EXPR, type, exp);
13621 EXPR_WFL_LINECOL (node) = location;
13622 return node;
13625 /* Build an incomplete class reference operator. */
13626 static tree
13627 build_incomplete_class_ref (location, class_name)
13628 int location;
13629 tree class_name;
13631 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13632 EXPR_WFL_LINECOL (node) = location;
13633 return node;
13636 /* Complete an incomplete class reference operator. */
13637 static tree
13638 patch_incomplete_class_ref (node)
13639 tree node;
13641 tree type = TREE_OPERAND (node, 0);
13642 tree ref_type;
13644 if (!(ref_type = resolve_type_during_patch (type)))
13645 return error_mark_node;
13647 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13649 /* A class referenced by `foo.class' is initialized. */
13650 return build_class_init (ref_type, build_class_ref (ref_type));
13653 /* If we're emitting class files and we have to deal with non
13654 primitive types, we invoke (and consider generating) the
13655 synthetic static method `class$'. */
13656 if (!TYPE_DOT_CLASS (current_class))
13657 build_dot_class_method (current_class);
13658 ref_type = build_dot_class_method_invocation (ref_type);
13659 return java_complete_tree (ref_type);
13662 /* 15.14 Unary operators. We return error_mark_node in case of error,
13663 but preserve the type of NODE if the type is fixed. */
13665 static tree
13666 patch_unaryop (node, wfl_op)
13667 tree node;
13668 tree wfl_op;
13670 tree op = TREE_OPERAND (node, 0);
13671 tree op_type = TREE_TYPE (op);
13672 tree prom_type = NULL_TREE, value, decl;
13673 int outer_field_flag = 0;
13674 int code = TREE_CODE (node);
13675 int error_found = 0;
13677 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13679 switch (code)
13681 /* 15.13.2 Postfix Increment Operator ++ */
13682 case POSTINCREMENT_EXPR:
13683 /* 15.13.3 Postfix Increment Operator -- */
13684 case POSTDECREMENT_EXPR:
13685 /* 15.14.1 Prefix Increment Operator ++ */
13686 case PREINCREMENT_EXPR:
13687 /* 15.14.2 Prefix Decrement Operator -- */
13688 case PREDECREMENT_EXPR:
13689 op = decl = strip_out_static_field_access_decl (op);
13690 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13691 /* We might be trying to change an outer field accessed using
13692 access method. */
13693 if (outer_field_flag)
13695 /* Retrieve the decl of the field we're trying to access. We
13696 do that by first retrieving the function we would call to
13697 access the field. It has been already verified that this
13698 field isn't final */
13699 if (flag_emit_class_files)
13700 decl = TREE_OPERAND (op, 0);
13701 else
13702 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13703 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13705 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13706 else if (!JDECL_P (decl)
13707 && TREE_CODE (decl) != COMPONENT_REF
13708 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13709 && TREE_CODE (decl) != INDIRECT_REF
13710 && !(TREE_CODE (decl) == COMPOUND_EXPR
13711 && TREE_OPERAND (decl, 1)
13712 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13714 tree lvalue;
13715 /* Before screaming, check that we're not in fact trying to
13716 increment a optimized static final access, in which case
13717 we issue an different error message. */
13718 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13719 && resolve_expression_name (wfl_op, &lvalue)
13720 && check_final_assignment (lvalue, wfl_op)))
13721 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13722 operator_string (node));
13723 TREE_TYPE (node) = error_mark_node;
13724 error_found = 1;
13727 if (check_final_assignment (op, wfl_op))
13728 error_found = 1;
13730 /* From now on, we know that op if a variable and that it has a
13731 valid wfl. We use wfl_op to locate errors related to the
13732 ++/-- operand. */
13733 else if (!JNUMERIC_TYPE_P (op_type))
13735 parse_error_context
13736 (wfl_op, "Invalid argument type `%s' to `%s'",
13737 lang_printable_name (op_type, 0), operator_string (node));
13738 TREE_TYPE (node) = error_mark_node;
13739 error_found = 1;
13741 else
13743 /* Before the addition, binary numeric promotion is performed on
13744 both operands, if really necessary */
13745 if (JINTEGRAL_TYPE_P (op_type))
13747 value = build_int_2 (1, 0);
13748 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13750 else
13752 value = build_int_2 (1, 0);
13753 TREE_TYPE (node) =
13754 binary_numeric_promotion (op_type,
13755 TREE_TYPE (value), &op, &value);
13758 /* We remember we might be accessing an outer field */
13759 if (outer_field_flag)
13761 /* We re-generate an access to the field */
13762 value = build (PLUS_EXPR, TREE_TYPE (op),
13763 build_outer_field_access (wfl_op, decl), value);
13765 /* And we patch the original access$() into a write
13766 with plus_op as a rhs */
13767 return outer_field_access_fix (node, op, value);
13770 /* And write back into the node. */
13771 TREE_OPERAND (node, 0) = op;
13772 TREE_OPERAND (node, 1) = value;
13773 /* Convert the overall back into its original type, if
13774 necessary, and return */
13775 if (JINTEGRAL_TYPE_P (op_type))
13776 return fold (node);
13777 else
13778 return fold (convert (op_type, node));
13780 break;
13782 /* 15.14.3 Unary Plus Operator + */
13783 case UNARY_PLUS_EXPR:
13784 /* 15.14.4 Unary Minus Operator - */
13785 case NEGATE_EXPR:
13786 if (!JNUMERIC_TYPE_P (op_type))
13788 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13789 TREE_TYPE (node) = error_mark_node;
13790 error_found = 1;
13792 /* Unary numeric promotion is performed on operand */
13793 else
13795 op = do_unary_numeric_promotion (op);
13796 prom_type = TREE_TYPE (op);
13797 if (code == UNARY_PLUS_EXPR)
13798 return fold (op);
13800 break;
13802 /* 15.14.5 Bitwise Complement Operator ~ */
13803 case BIT_NOT_EXPR:
13804 if (!JINTEGRAL_TYPE_P (op_type))
13806 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13807 TREE_TYPE (node) = error_mark_node;
13808 error_found = 1;
13810 else
13812 op = do_unary_numeric_promotion (op);
13813 prom_type = TREE_TYPE (op);
13815 break;
13817 /* 15.14.6 Logical Complement Operator ! */
13818 case TRUTH_NOT_EXPR:
13819 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13821 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
13822 /* But the type is known. We will report an error if further
13823 attempt of a assignment is made with this rhs */
13824 TREE_TYPE (node) = boolean_type_node;
13825 error_found = 1;
13827 else
13828 prom_type = boolean_type_node;
13829 break;
13831 /* 15.15 Cast Expression */
13832 case CONVERT_EXPR:
13833 value = patch_cast (node, wfl_operator);
13834 if (value == error_mark_node)
13836 /* If this cast is part of an assignment, we tell the code
13837 that deals with it not to complain about a mismatch,
13838 because things have been cast, anyways */
13839 TREE_TYPE (node) = error_mark_node;
13840 error_found = 1;
13842 else
13844 value = fold (value);
13845 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13846 return value;
13848 break;
13851 if (error_found)
13852 return error_mark_node;
13854 /* There are cases where node has been replaced by something else
13855 and we don't end up returning here: UNARY_PLUS_EXPR,
13856 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
13857 TREE_OPERAND (node, 0) = fold (op);
13858 TREE_TYPE (node) = prom_type;
13859 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
13860 return fold (node);
13863 /* Generic type resolution that sometimes takes place during node
13864 patching. Returned the resolved type or generate an error
13865 message. Return the resolved type or NULL_TREE. */
13867 static tree
13868 resolve_type_during_patch (type)
13869 tree type;
13871 if (unresolved_type_p (type, NULL))
13873 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
13874 if (!type_decl)
13876 parse_error_context (type,
13877 "Class `%s' not found in type declaration",
13878 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13879 return NULL_TREE;
13881 else
13883 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13884 return TREE_TYPE (type_decl);
13887 return type;
13889 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
13890 found. Otherwise NODE or something meant to replace it is returned. */
13892 static tree
13893 patch_cast (node, wfl_op)
13894 tree node;
13895 tree wfl_op;
13897 tree op = TREE_OPERAND (node, 0);
13898 tree op_type = TREE_TYPE (op);
13899 tree cast_type = TREE_TYPE (node);
13900 char *t1;
13902 /* First resolve OP_TYPE if unresolved */
13903 if (!(cast_type = resolve_type_during_patch (cast_type)))
13904 return error_mark_node;
13906 /* Check on cast that are proven correct at compile time */
13907 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13909 /* Same type */
13910 if (cast_type == op_type)
13911 return node;
13913 /* float and double type are converted to the original type main
13914 variant and then to the target type. */
13915 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13916 op = convert (integer_type_node, op);
13918 /* Try widening/narowwing convertion. Potentially, things need
13919 to be worked out in gcc so we implement the extreme cases
13920 correctly. fold_convert() needs to be fixed. */
13921 return convert (cast_type, op);
13924 /* It's also valid to cast a boolean into a boolean */
13925 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13926 return node;
13928 /* null can be casted to references */
13929 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13930 return build_null_of_type (cast_type);
13932 /* The remaining legal casts involve conversion between reference
13933 types. Check for their compile time correctness. */
13934 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
13935 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
13937 TREE_TYPE (node) = promote_type (cast_type);
13938 /* Now, the case can be determined correct at compile time if
13939 OP_TYPE can be converted into CAST_TYPE by assignment
13940 conversion (5.2) */
13942 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
13944 TREE_SET_CODE (node, NOP_EXPR);
13945 return node;
13948 if (flag_emit_class_files)
13950 TREE_SET_CODE (node, CONVERT_EXPR);
13951 return node;
13954 /* The cast requires a run-time check */
13955 return build (CALL_EXPR, promote_type (cast_type),
13956 build_address_of (soft_checkcast_node),
13957 tree_cons (NULL_TREE, build_class_ref (cast_type),
13958 build_tree_list (NULL_TREE, op)),
13959 NULL_TREE);
13962 /* Any other casts are proven incorrect at compile time */
13963 t1 = xstrdup (lang_printable_name (op_type, 0));
13964 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
13965 t1, lang_printable_name (cast_type, 0));
13966 free (t1);
13967 return error_mark_node;
13970 /* Build a null constant and give it the type TYPE. */
13972 static tree
13973 build_null_of_type (type)
13974 tree type;
13976 tree node = build_int_2 (0, 0);
13977 TREE_TYPE (node) = promote_type (type);
13978 return node;
13981 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13982 a list of indices. */
13983 static tree
13984 build_array_ref (location, array, index)
13985 int location;
13986 tree array, index;
13988 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13989 EXPR_WFL_LINECOL (node) = location;
13990 return node;
13993 /* 15.12 Array Access Expression */
13995 static tree
13996 patch_array_ref (node)
13997 tree node;
13999 tree array = TREE_OPERAND (node, 0);
14000 tree array_type = TREE_TYPE (array);
14001 tree index = TREE_OPERAND (node, 1);
14002 tree index_type = TREE_TYPE (index);
14003 int error_found = 0;
14005 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14007 if (TREE_CODE (array_type) == POINTER_TYPE)
14008 array_type = TREE_TYPE (array_type);
14010 /* The array reference must be an array */
14011 if (!TYPE_ARRAY_P (array_type))
14013 parse_error_context
14014 (wfl_operator,
14015 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14016 lang_printable_name (array_type, 0));
14017 TREE_TYPE (node) = error_mark_node;
14018 error_found = 1;
14021 /* The array index undergoes unary numeric promotion. The promoted
14022 type must be int */
14023 index = do_unary_numeric_promotion (index);
14024 if (TREE_TYPE (index) != int_type_node)
14026 if (valid_cast_to_p (index_type, int_type_node))
14027 parse_error_context (wfl_operator,
14028 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14029 lang_printable_name (index_type, 0));
14030 else
14031 parse_error_context (wfl_operator,
14032 "Incompatible type for `[]'. Can't convert `%s' to `int'",
14033 lang_printable_name (index_type, 0));
14034 TREE_TYPE (node) = error_mark_node;
14035 error_found = 1;
14038 if (error_found)
14039 return error_mark_node;
14041 array_type = TYPE_ARRAY_ELEMENT (array_type);
14043 if (flag_emit_class_files || flag_emit_xref)
14045 TREE_OPERAND (node, 0) = array;
14046 TREE_OPERAND (node, 1) = index;
14048 else
14050 /* The save_expr is for correct evaluation order. It would be cleaner
14051 to use force_evaluation_order (see comment there), but that is
14052 difficult when we also have to deal with bounds checking. */
14053 if (TREE_SIDE_EFFECTS (index))
14054 array = save_expr (array);
14055 node = build_java_arrayaccess (array, array_type, index);
14056 if (TREE_SIDE_EFFECTS (index))
14057 node = build (COMPOUND_EXPR, array_type, array, node);
14059 TREE_TYPE (node) = array_type;
14060 return node;
14063 /* 15.9 Array Creation Expressions */
14065 static tree
14066 build_newarray_node (type, dims, extra_dims)
14067 tree type;
14068 tree dims;
14069 int extra_dims;
14071 tree node =
14072 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14073 build_int_2 (extra_dims, 0));
14074 return node;
14077 static tree
14078 patch_newarray (node)
14079 tree node;
14081 tree type = TREE_OPERAND (node, 0);
14082 tree dims = TREE_OPERAND (node, 1);
14083 tree cdim, array_type;
14084 int error_found = 0;
14085 int ndims = 0;
14086 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14088 /* Dimension types are verified. It's better for the types to be
14089 verified in order. */
14090 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14092 int dim_error = 0;
14093 tree dim = TREE_VALUE (cdim);
14095 /* Dim might have been saved during its evaluation */
14096 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14098 /* The type of each specified dimension must be an integral type. */
14099 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14100 dim_error = 1;
14102 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14103 promoted type must be int. */
14104 else
14106 dim = do_unary_numeric_promotion (dim);
14107 if (TREE_TYPE (dim) != int_type_node)
14108 dim_error = 1;
14111 /* Report errors on types here */
14112 if (dim_error)
14114 parse_error_context
14115 (TREE_PURPOSE (cdim),
14116 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14117 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14118 "Explicit cast needed to" : "Can't"),
14119 lang_printable_name (TREE_TYPE (dim), 0));
14120 error_found = 1;
14123 TREE_PURPOSE (cdim) = NULL_TREE;
14126 /* Resolve array base type if unresolved */
14127 if (!(type = resolve_type_during_patch (type)))
14128 error_found = 1;
14130 if (error_found)
14132 /* We don't want further evaluation of this bogus array creation
14133 operation */
14134 TREE_TYPE (node) = error_mark_node;
14135 return error_mark_node;
14138 /* Set array_type to the actual (promoted) array type of the result. */
14139 if (TREE_CODE (type) == RECORD_TYPE)
14140 type = build_pointer_type (type);
14141 while (--xdims >= 0)
14143 type = promote_type (build_java_array_type (type, -1));
14145 dims = nreverse (dims);
14146 array_type = type;
14147 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14149 type = array_type;
14150 array_type
14151 = build_java_array_type (type,
14152 TREE_CODE (cdim) == INTEGER_CST
14153 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14154 : -1);
14155 array_type = promote_type (array_type);
14157 dims = nreverse (dims);
14159 /* The node is transformed into a function call. Things are done
14160 differently according to the number of dimensions. If the number
14161 of dimension is equal to 1, then the nature of the base type
14162 (primitive or not) matters. */
14163 if (ndims == 1)
14164 return build_new_array (type, TREE_VALUE (dims));
14166 /* Can't reuse what's already written in expr.c because it uses the
14167 JVM stack representation. Provide a build_multianewarray. FIXME */
14168 return build (CALL_EXPR, array_type,
14169 build_address_of (soft_multianewarray_node),
14170 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14171 tree_cons (NULL_TREE,
14172 build_int_2 (ndims, 0), dims )),
14173 NULL_TREE);
14176 /* 10.6 Array initializer. */
14178 /* Build a wfl for array element that don't have one, so we can
14179 pin-point errors. */
14181 static tree
14182 maybe_build_array_element_wfl (node)
14183 tree node;
14185 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14186 return build_expr_wfl (NULL_TREE, ctxp->filename,
14187 ctxp->elc.line, ctxp->elc.prev_col);
14188 else
14189 return NULL_TREE;
14192 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14193 identification of initialized arrays easier to detect during walk
14194 and expansion. */
14196 static tree
14197 build_new_array_init (location, values)
14198 int location;
14199 tree values;
14201 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14202 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14203 EXPR_WFL_LINECOL (to_return) = location;
14204 return to_return;
14207 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14208 occurred. Otherwise return NODE after having set its type
14209 appropriately. */
14211 static tree
14212 patch_new_array_init (type, node)
14213 tree type, node;
14215 int error_seen = 0;
14216 tree current, element_type;
14217 HOST_WIDE_INT length;
14218 int all_constant = 1;
14219 tree init = TREE_OPERAND (node, 0);
14221 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14223 parse_error_context (node,
14224 "Invalid array initializer for non-array type `%s'",
14225 lang_printable_name (type, 1));
14226 return error_mark_node;
14228 type = TREE_TYPE (type);
14229 element_type = TYPE_ARRAY_ELEMENT (type);
14231 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14233 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14234 current; length++, current = TREE_CHAIN (current))
14236 tree elt = TREE_VALUE (current);
14237 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14239 error_seen |= array_constructor_check_entry (element_type, current);
14240 elt = TREE_VALUE (current);
14241 /* When compiling to native code, STRING_CST is converted to
14242 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14243 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14244 all_constant = 0;
14246 else
14248 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14249 TREE_PURPOSE (current) = NULL_TREE;
14250 all_constant = 0;
14252 if (elt && TREE_CODE (elt) == TREE_LIST
14253 && TREE_VALUE (elt) == error_mark_node)
14254 error_seen = 1;
14257 if (error_seen)
14258 return error_mark_node;
14260 /* Create a new type. We can't reuse the one we have here by
14261 patching its dimension because it originally is of dimension -1
14262 hence reused by gcc. This would prevent triangular arrays. */
14263 type = build_java_array_type (element_type, length);
14264 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14265 TREE_TYPE (node) = promote_type (type);
14266 TREE_CONSTANT (init) = all_constant;
14267 TREE_CONSTANT (node) = all_constant;
14268 return node;
14271 /* Verify that one entry of the initializer element list can be
14272 assigned to the array base type. Report 1 if an error occurred, 0
14273 otherwise. */
14275 static int
14276 array_constructor_check_entry (type, entry)
14277 tree type, entry;
14279 char *array_type_string = NULL; /* For error reports */
14280 tree value, type_value, new_value, wfl_value, patched;
14281 int error_seen = 0;
14283 new_value = NULL_TREE;
14284 wfl_value = TREE_VALUE (entry);
14286 value = java_complete_tree (TREE_VALUE (entry));
14287 /* patch_string return error_mark_node if arg is error_mark_node */
14288 if ((patched = patch_string (value)))
14289 value = patched;
14290 if (value == error_mark_node)
14291 return 1;
14293 type_value = TREE_TYPE (value);
14295 /* At anytime, try_builtin_assignconv can report a warning on
14296 constant overflow during narrowing. */
14297 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14298 new_value = try_builtin_assignconv (wfl_operator, type, value);
14299 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14300 type_value = promote_type (type);
14302 /* Check and report errors */
14303 if (!new_value)
14305 const char *msg = (!valid_cast_to_p (type_value, type) ?
14306 "Can't" : "Explicit cast needed to");
14307 if (!array_type_string)
14308 array_type_string = xstrdup (lang_printable_name (type, 1));
14309 parse_error_context
14310 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14311 msg, lang_printable_name (type_value, 1), array_type_string);
14312 error_seen = 1;
14315 if (new_value)
14317 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
14318 TREE_VALUE (entry) = new_value;
14321 if (array_type_string)
14322 free (array_type_string);
14324 TREE_PURPOSE (entry) = NULL_TREE;
14325 return error_seen;
14328 static tree
14329 build_this (location)
14330 int location;
14332 tree node = build_wfl_node (this_identifier_node);
14333 TREE_SET_CODE (node, THIS_EXPR);
14334 EXPR_WFL_LINECOL (node) = location;
14335 return node;
14338 /* 14.15 The return statement. It builds a modify expression that
14339 assigns the returned value to the RESULT_DECL that hold the value
14340 to be returned. */
14342 static tree
14343 build_return (location, op)
14344 int location;
14345 tree op;
14347 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14348 EXPR_WFL_LINECOL (node) = location;
14349 node = build_debugable_stmt (location, node);
14350 return node;
14353 static tree
14354 patch_return (node)
14355 tree node;
14357 tree return_exp = TREE_OPERAND (node, 0);
14358 tree meth = current_function_decl;
14359 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14360 int error_found = 0;
14362 TREE_TYPE (node) = error_mark_node;
14363 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14365 /* It's invalid to have a return value within a function that is
14366 declared with the keyword void or that is a constructor */
14367 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14368 error_found = 1;
14370 /* It's invalid to use a return statement in a static block */
14371 if (DECL_CLINIT_P (current_function_decl))
14372 error_found = 1;
14374 /* It's invalid to have a no return value within a function that
14375 isn't declared with the keyword `void' */
14376 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14377 error_found = 2;
14379 if (in_instance_initializer)
14380 error_found = 1;
14382 if (error_found)
14384 if (in_instance_initializer)
14385 parse_error_context (wfl_operator,
14386 "`return' inside instance initializer");
14388 else if (DECL_CLINIT_P (current_function_decl))
14389 parse_error_context (wfl_operator,
14390 "`return' inside static initializer");
14392 else if (!DECL_CONSTRUCTOR_P (meth))
14394 char *t = xstrdup (lang_printable_name (mtype, 0));
14395 parse_error_context (wfl_operator,
14396 "`return' with%s value from `%s %s'",
14397 (error_found == 1 ? "" : "out"),
14398 t, lang_printable_name (meth, 0));
14399 free (t);
14401 else
14402 parse_error_context (wfl_operator,
14403 "`return' with value from constructor `%s'",
14404 lang_printable_name (meth, 0));
14405 return error_mark_node;
14408 /* If we have a return_exp, build a modify expression and expand
14409 it. Note: at that point, the assignment is declared valid, but we
14410 may want to carry some more hacks */
14411 if (return_exp)
14413 tree exp = java_complete_tree (return_exp);
14414 tree modify, patched;
14416 /* If the function returned value and EXP are booleans, EXP has
14417 to be converted into the type of DECL_RESULT, which is integer
14418 (see complete_start_java_method) */
14419 if (TREE_TYPE (exp) == boolean_type_node &&
14420 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14421 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14423 /* `null' can be assigned to a function returning a reference */
14424 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14425 exp == null_pointer_node)
14426 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14428 if ((patched = patch_string (exp)))
14429 exp = patched;
14431 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14432 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14433 modify = java_complete_tree (modify);
14435 if (modify != error_mark_node)
14437 TREE_SIDE_EFFECTS (modify) = 1;
14438 TREE_OPERAND (node, 0) = modify;
14440 else
14441 return error_mark_node;
14443 TREE_TYPE (node) = void_type_node;
14444 TREE_SIDE_EFFECTS (node) = 1;
14445 return node;
14448 /* 14.8 The if Statement */
14450 static tree
14451 build_if_else_statement (location, expression, if_body, else_body)
14452 int location;
14453 tree expression, if_body, else_body;
14455 tree node;
14456 if (!else_body)
14457 else_body = empty_stmt_node;
14458 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14459 EXPR_WFL_LINECOL (node) = location;
14460 node = build_debugable_stmt (location, node);
14461 return node;
14464 static tree
14465 patch_if_else_statement (node)
14466 tree node;
14468 tree expression = TREE_OPERAND (node, 0);
14470 TREE_TYPE (node) = error_mark_node;
14471 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14473 /* The type of expression must be boolean */
14474 if (TREE_TYPE (expression) != boolean_type_node
14475 && TREE_TYPE (expression) != promoted_boolean_type_node)
14477 parse_error_context
14478 (wfl_operator,
14479 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14480 lang_printable_name (TREE_TYPE (expression), 0));
14481 return error_mark_node;
14484 TREE_TYPE (node) = void_type_node;
14485 TREE_SIDE_EFFECTS (node) = 1;
14486 CAN_COMPLETE_NORMALLY (node)
14487 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14488 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
14489 return node;
14492 /* 14.6 Labeled Statements */
14494 /* Action taken when a lableled statement is parsed. a new
14495 LABELED_BLOCK_EXPR is created. No statement is attached to the
14496 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14498 static tree
14499 build_labeled_block (location, label)
14500 int location;
14501 tree label;
14503 tree label_name ;
14504 tree label_decl, node;
14505 if (label == NULL_TREE || label == continue_identifier_node)
14506 label_name = label;
14507 else
14509 label_name = merge_qualified_name (label_id, label);
14510 /* Issue an error if we try to reuse a label that was previously
14511 declared */
14512 if (IDENTIFIER_LOCAL_VALUE (label_name))
14514 EXPR_WFL_LINECOL (wfl_operator) = location;
14515 parse_error_context (wfl_operator,
14516 "Declaration of `%s' shadows a previous label declaration",
14517 IDENTIFIER_POINTER (label));
14518 EXPR_WFL_LINECOL (wfl_operator) =
14519 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14520 parse_error_context (wfl_operator,
14521 "This is the location of the previous declaration of label `%s'",
14522 IDENTIFIER_POINTER (label));
14523 java_error_count--;
14527 label_decl = create_label_decl (label_name);
14528 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14529 EXPR_WFL_LINECOL (node) = location;
14530 TREE_SIDE_EFFECTS (node) = 1;
14531 return node;
14534 /* A labeled statement LBE is attached a statement. */
14536 static tree
14537 finish_labeled_statement (lbe, statement)
14538 tree lbe; /* Labeled block expr */
14539 tree statement;
14541 /* In anyways, tie the loop to its statement */
14542 LABELED_BLOCK_BODY (lbe) = statement;
14543 pop_labeled_block ();
14544 POP_LABELED_BLOCK ();
14545 return lbe;
14548 /* 14.10, 14.11, 14.12 Loop Statements */
14550 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14551 list. */
14553 static tree
14554 build_new_loop (loop_body)
14555 tree loop_body;
14557 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14558 TREE_SIDE_EFFECTS (loop) = 1;
14559 PUSH_LOOP (loop);
14560 return loop;
14563 /* Create a loop body according to the following structure:
14564 COMPOUND_EXPR
14565 COMPOUND_EXPR (loop main body)
14566 EXIT_EXPR (this order is for while/for loops.
14567 LABELED_BLOCK_EXPR the order is reversed for do loops)
14568 LABEL_DECL (a continue occuring here branches at the
14569 BODY end of this labeled block)
14570 INCREMENT (if any)
14572 REVERSED, if non zero, tells that the loop condition expr comes
14573 after the body, like in the do-while loop.
14575 To obtain a loop, the loop body structure described above is
14576 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14578 LABELED_BLOCK_EXPR
14579 LABEL_DECL (use this label to exit the loop)
14580 LOOP_EXPR
14581 <structure described above> */
14583 static tree
14584 build_loop_body (location, condition, reversed)
14585 int location;
14586 tree condition;
14587 int reversed;
14589 tree first, second, body;
14591 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14592 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14593 condition = build_debugable_stmt (location, condition);
14594 TREE_SIDE_EFFECTS (condition) = 1;
14596 body = build_labeled_block (0, continue_identifier_node);
14597 first = (reversed ? body : condition);
14598 second = (reversed ? condition : body);
14599 return
14600 build (COMPOUND_EXPR, NULL_TREE,
14601 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14604 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14605 their order) on the current loop. Unlink the current loop from the
14606 loop list. */
14608 static tree
14609 finish_loop_body (location, condition, body, reversed)
14610 int location;
14611 tree condition, body;
14612 int reversed;
14614 tree to_return = ctxp->current_loop;
14615 tree loop_body = LOOP_EXPR_BODY (to_return);
14616 if (condition)
14618 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14619 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14620 The real EXIT_EXPR is one operand further. */
14621 EXPR_WFL_LINECOL (cnode) = location;
14622 /* This one is for accurate error reports */
14623 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14624 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14626 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14627 POP_LOOP ();
14628 return to_return;
14631 /* Tailored version of finish_loop_body for FOR loops, when FOR
14632 loops feature the condition part */
14634 static tree
14635 finish_for_loop (location, condition, update, body)
14636 int location;
14637 tree condition, update, body;
14639 /* Put the condition and the loop body in place */
14640 tree loop = finish_loop_body (location, condition, body, 0);
14641 /* LOOP is the current loop which has been now popped of the loop
14642 stack. Install the update block */
14643 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14644 return loop;
14647 /* Try to find the loop a block might be related to. This comprises
14648 the case where the LOOP_EXPR is found as the second operand of a
14649 COMPOUND_EXPR, because the loop happens to have an initialization
14650 part, then expressed as the first operand of the COMPOUND_EXPR. If
14651 the search finds something, 1 is returned. Otherwise, 0 is
14652 returned. The search is assumed to start from a
14653 LABELED_BLOCK_EXPR's block. */
14655 static tree
14656 search_loop (statement)
14657 tree statement;
14659 if (TREE_CODE (statement) == LOOP_EXPR)
14660 return statement;
14662 if (TREE_CODE (statement) == BLOCK)
14663 statement = BLOCK_SUBBLOCKS (statement);
14664 else
14665 return NULL_TREE;
14667 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14668 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14669 statement = TREE_OPERAND (statement, 1);
14671 return (TREE_CODE (statement) == LOOP_EXPR
14672 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14675 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14676 returned otherwise. */
14678 static int
14679 labeled_block_contains_loop_p (block, loop)
14680 tree block, loop;
14682 if (!block)
14683 return 0;
14685 if (LABELED_BLOCK_BODY (block) == loop)
14686 return 1;
14688 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14689 return 1;
14691 return 0;
14694 /* If the loop isn't surrounded by a labeled statement, create one and
14695 insert LOOP as its body. */
14697 static tree
14698 patch_loop_statement (loop)
14699 tree loop;
14701 tree loop_label;
14703 TREE_TYPE (loop) = void_type_node;
14704 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14705 return loop;
14707 loop_label = build_labeled_block (0, NULL_TREE);
14708 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14709 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14710 LABELED_BLOCK_BODY (loop_label) = loop;
14711 PUSH_LABELED_BLOCK (loop_label);
14712 return loop_label;
14715 /* 14.13, 14.14: break and continue Statements */
14717 /* Build a break or a continue statement. a null NAME indicates an
14718 unlabeled break/continue statement. */
14720 static tree
14721 build_bc_statement (location, is_break, name)
14722 int location, is_break;
14723 tree name;
14725 tree break_continue, label_block_expr = NULL_TREE;
14727 if (name)
14729 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14730 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14731 /* Null means that we don't have a target for this named
14732 break/continue. In this case, we make the target to be the
14733 label name, so that the error can be reported accuratly in
14734 patch_bc_statement. */
14735 label_block_expr = EXPR_WFL_NODE (name);
14737 /* Unlabeled break/continue will be handled during the
14738 break/continue patch operation */
14739 break_continue
14740 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14742 IS_BREAK_STMT_P (break_continue) = is_break;
14743 TREE_SIDE_EFFECTS (break_continue) = 1;
14744 EXPR_WFL_LINECOL (break_continue) = location;
14745 break_continue = build_debugable_stmt (location, break_continue);
14746 return break_continue;
14749 /* Verification of a break/continue statement. */
14751 static tree
14752 patch_bc_statement (node)
14753 tree node;
14755 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14756 tree labeled_block = ctxp->current_labeled_block;
14757 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14759 /* Having an identifier here means that the target is unknown. */
14760 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14762 parse_error_context (wfl_operator, "No label definition found for `%s'",
14763 IDENTIFIER_POINTER (bc_label));
14764 return error_mark_node;
14766 if (! IS_BREAK_STMT_P (node))
14768 /* It's a continue statement. */
14769 for (;; labeled_block = TREE_CHAIN (labeled_block))
14771 if (labeled_block == NULL_TREE)
14773 if (bc_label == NULL_TREE)
14774 parse_error_context (wfl_operator,
14775 "`continue' must be in loop");
14776 else
14777 parse_error_context
14778 (wfl_operator, "continue label `%s' does not name a loop",
14779 IDENTIFIER_POINTER (bc_label));
14780 return error_mark_node;
14782 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14783 == continue_identifier_node)
14784 && (bc_label == NULL_TREE
14785 || TREE_CHAIN (labeled_block) == bc_label))
14787 bc_label = labeled_block;
14788 break;
14792 else if (!bc_label)
14794 for (;; labeled_block = TREE_CHAIN (labeled_block))
14796 if (labeled_block == NULL_TREE)
14798 parse_error_context (wfl_operator,
14799 "`break' must be in loop or switch");
14800 return error_mark_node;
14802 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14803 if (TREE_CODE (target_stmt) == SWITCH_EXPR
14804 || search_loop (target_stmt))
14806 bc_label = labeled_block;
14807 break;
14812 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14813 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14815 /* Our break/continue don't return values. */
14816 TREE_TYPE (node) = void_type_node;
14817 /* Encapsulate the break within a compound statement so that it's
14818 expanded all the times by expand_expr (and not clobbered
14819 sometimes, like after a if statement) */
14820 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14821 TREE_SIDE_EFFECTS (node) = 1;
14822 return node;
14825 /* Process the exit expression belonging to a loop. Its type must be
14826 boolean. */
14828 static tree
14829 patch_exit_expr (node)
14830 tree node;
14832 tree expression = TREE_OPERAND (node, 0);
14833 TREE_TYPE (node) = error_mark_node;
14834 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14836 /* The type of expression must be boolean */
14837 if (TREE_TYPE (expression) != boolean_type_node)
14839 parse_error_context
14840 (wfl_operator,
14841 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
14842 lang_printable_name (TREE_TYPE (expression), 0));
14843 return error_mark_node;
14845 /* Now we know things are allright, invert the condition, fold and
14846 return */
14847 TREE_OPERAND (node, 0) =
14848 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
14850 if (! integer_zerop (TREE_OPERAND (node, 0))
14851 && ctxp->current_loop != NULL_TREE
14852 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14853 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14854 if (! integer_onep (TREE_OPERAND (node, 0)))
14855 CAN_COMPLETE_NORMALLY (node) = 1;
14858 TREE_TYPE (node) = void_type_node;
14859 return node;
14862 /* 14.9 Switch statement */
14864 static tree
14865 patch_switch_statement (node)
14866 tree node;
14868 tree se = TREE_OPERAND (node, 0), se_type;
14870 /* Complete the switch expression */
14871 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14872 se_type = TREE_TYPE (se);
14873 /* The type of the switch expression must be char, byte, short or
14874 int */
14875 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
14877 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14878 parse_error_context (wfl_operator,
14879 "Incompatible type for `switch'. Can't convert `%s' to `int'",
14880 lang_printable_name (se_type, 0));
14881 /* This is what java_complete_tree will check */
14882 TREE_OPERAND (node, 0) = error_mark_node;
14883 return error_mark_node;
14886 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
14888 /* Ready to return */
14889 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
14891 TREE_TYPE (node) = error_mark_node;
14892 return error_mark_node;
14894 TREE_TYPE (node) = void_type_node;
14895 TREE_SIDE_EFFECTS (node) = 1;
14896 CAN_COMPLETE_NORMALLY (node)
14897 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14898 || ! SWITCH_HAS_DEFAULT (node);
14899 return node;
14902 /* 14.18 The try/catch statements */
14904 static tree
14905 build_try_statement (location, try_block, catches)
14906 int location;
14907 tree try_block, catches;
14909 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
14910 EXPR_WFL_LINECOL (node) = location;
14911 return node;
14914 static tree
14915 build_try_finally_statement (location, try_block, finally)
14916 int location;
14917 tree try_block, finally;
14919 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14920 EXPR_WFL_LINECOL (node) = location;
14921 return node;
14924 static tree
14925 patch_try_statement (node)
14926 tree node;
14928 int error_found = 0;
14929 tree try = TREE_OPERAND (node, 0);
14930 /* Exception handlers are considered in left to right order */
14931 tree catch = nreverse (TREE_OPERAND (node, 1));
14932 tree current, caught_type_list = NULL_TREE;
14934 /* Check catch clauses, if any. Every time we find an error, we try
14935 to process the next catch clause. We process the catch clause before
14936 the try block so that when processing the try block we can check thrown
14937 exceptions againts the caught type list. */
14938 for (current = catch; current; current = TREE_CHAIN (current))
14940 tree carg_decl, carg_type;
14941 tree sub_current, catch_block, catch_clause;
14942 int unreachable;
14944 /* At this point, the structure of the catch clause is
14945 CATCH_EXPR (catch node)
14946 BLOCK (with the decl of the parameter)
14947 COMPOUND_EXPR
14948 MODIFY_EXPR (assignment of the catch parameter)
14949 BLOCK (catch clause block)
14951 catch_clause = TREE_OPERAND (current, 0);
14952 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14953 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14955 /* Catch clauses can't have more than one parameter declared,
14956 but it's already enforced by the grammar. Make sure that the
14957 only parameter of the clause statement in of class Throwable
14958 or a subclass of Throwable, but that was done earlier. The
14959 catch clause parameter type has also been resolved. */
14961 /* Just make sure that the catch clause parameter type inherits
14962 from java.lang.Throwable */
14963 if (!inherits_from_p (carg_type, throwable_type_node))
14965 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14966 parse_error_context (wfl_operator,
14967 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
14968 lang_printable_name (carg_type, 0));
14969 error_found = 1;
14970 continue;
14973 /* Partial check for unreachable catch statement: The catch
14974 clause is reachable iff is no earlier catch block A in
14975 the try statement such that the type of the catch
14976 clause's parameter is the same as or a subclass of the
14977 type of A's parameter */
14978 unreachable = 0;
14979 for (sub_current = catch;
14980 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14982 tree sub_catch_clause, decl;
14983 sub_catch_clause = TREE_OPERAND (sub_current, 0);
14984 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14986 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14988 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14989 parse_error_context
14990 (wfl_operator,
14991 "`catch' not reached because of the catch clause at line %d",
14992 EXPR_WFL_LINENO (sub_current));
14993 unreachable = error_found = 1;
14994 break;
14997 /* Complete the catch clause block */
14998 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14999 if (catch_block == error_mark_node)
15001 error_found = 1;
15002 continue;
15004 if (CAN_COMPLETE_NORMALLY (catch_block))
15005 CAN_COMPLETE_NORMALLY (node) = 1;
15006 TREE_OPERAND (current, 0) = catch_block;
15008 if (unreachable)
15009 continue;
15011 /* Things to do here: the exception must be thrown */
15013 /* Link this type to the caught type list */
15014 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15017 PUSH_EXCEPTIONS (caught_type_list);
15018 if ((try = java_complete_tree (try)) == error_mark_node)
15019 error_found = 1;
15020 if (CAN_COMPLETE_NORMALLY (try))
15021 CAN_COMPLETE_NORMALLY (node) = 1;
15022 POP_EXCEPTIONS ();
15024 /* Verification ends here */
15025 if (error_found)
15026 return error_mark_node;
15028 TREE_OPERAND (node, 0) = try;
15029 TREE_OPERAND (node, 1) = catch;
15030 TREE_TYPE (node) = void_type_node;
15031 return node;
15034 /* 14.17 The synchronized Statement */
15036 static tree
15037 patch_synchronized_statement (node, wfl_op1)
15038 tree node, wfl_op1;
15040 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15041 tree block = TREE_OPERAND (node, 1);
15043 tree tmp, enter, exit, expr_decl, assignment;
15045 if (expr == error_mark_node)
15047 block = java_complete_tree (block);
15048 return expr;
15051 /* We might be trying to synchronize on a STRING_CST */
15052 if ((tmp = patch_string (expr)))
15053 expr = tmp;
15055 /* The TYPE of expr must be a reference type */
15056 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15058 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15059 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15060 lang_printable_name (TREE_TYPE (expr), 0));
15061 return error_mark_node;
15064 if (flag_emit_xref)
15066 TREE_OPERAND (node, 0) = expr;
15067 TREE_OPERAND (node, 1) = java_complete_tree (block);
15068 CAN_COMPLETE_NORMALLY (node) = 1;
15069 return node;
15072 /* Generate a try-finally for the synchronized statement, except
15073 that the handler that catches all throw exception calls
15074 _Jv_MonitorExit and then rethrow the exception.
15075 The synchronized statement is then implemented as:
15076 TRY
15078 _Jv_MonitorEnter (expression)
15079 synchronized_block
15080 _Jv_MonitorExit (expression)
15082 CATCH_ALL
15084 e = _Jv_exception_info ();
15085 _Jv_MonitorExit (expression)
15086 Throw (e);
15087 } */
15089 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15090 BUILD_MONITOR_ENTER (enter, expr_decl);
15091 BUILD_MONITOR_EXIT (exit, expr_decl);
15092 CAN_COMPLETE_NORMALLY (enter) = 1;
15093 CAN_COMPLETE_NORMALLY (exit) = 1;
15094 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15095 TREE_SIDE_EFFECTS (assignment) = 1;
15096 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15097 build (COMPOUND_EXPR, NULL_TREE,
15098 build (WITH_CLEANUP_EXPR, NULL_TREE,
15099 build (COMPOUND_EXPR, NULL_TREE,
15100 assignment, enter),
15101 NULL_TREE, exit),
15102 block));
15103 node = build_expr_block (node, expr_decl);
15105 return java_complete_tree (node);
15108 /* 14.16 The throw Statement */
15110 static tree
15111 patch_throw_statement (node, wfl_op1)
15112 tree node, wfl_op1;
15114 tree expr = TREE_OPERAND (node, 0);
15115 tree type = TREE_TYPE (expr);
15116 int unchecked_ok = 0, tryblock_throws_ok = 0;
15118 /* Thrown expression must be assignable to java.lang.Throwable */
15119 if (!try_reference_assignconv (throwable_type_node, expr))
15121 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15122 parse_error_context (wfl_operator,
15123 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15124 lang_printable_name (type, 0));
15125 /* If the thrown expression was a reference, we further the
15126 compile-time check. */
15127 if (!JREFERENCE_TYPE_P (type))
15128 return error_mark_node;
15131 /* At least one of the following must be true */
15133 /* The type of the throw expression is a not checked exception,
15134 i.e. is a unchecked expression. */
15135 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15137 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15138 /* An instance can't throw a checked excetion unless that exception
15139 is explicitely declared in the `throws' clause of each
15140 constructor. This doesn't apply to anonymous classes, since they
15141 don't have declared constructors. */
15142 if (!unchecked_ok
15143 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15145 tree current;
15146 for (current = TYPE_METHODS (current_class); current;
15147 current = TREE_CHAIN (current))
15148 if (DECL_CONSTRUCTOR_P (current)
15149 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15151 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)",
15152 lang_printable_name (TREE_TYPE (expr), 0));
15153 return error_mark_node;
15157 /* Throw is contained in a try statement and at least one catch
15158 clause can receive the thrown expression or the current method is
15159 declared to throw such an exception. Or, the throw statement is
15160 contained in a method or constructor declaration and the type of
15161 the Expression is assignable to at least one type listed in the
15162 throws clause the declaration. */
15163 if (!unchecked_ok)
15164 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15165 if (!(unchecked_ok || tryblock_throws_ok))
15167 /* If there is a surrounding try block that has no matching
15168 clatch clause, report it first. A surrounding try block exits
15169 only if there is something after the list of checked
15170 exception thrown by the current function (if any). */
15171 if (IN_TRY_BLOCK_P ())
15172 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15173 lang_printable_name (type, 0));
15174 /* If we have no surrounding try statement and the method doesn't have
15175 any throws, report it now. FIXME */
15177 /* We report that the exception can't be throw from a try block
15178 in all circumstances but when the `throw' is inside a static
15179 block. */
15180 else if (!EXCEPTIONS_P (currently_caught_type_list)
15181 && !tryblock_throws_ok)
15183 if (DECL_CLINIT_P (current_function_decl))
15184 parse_error_context (wfl_operator,
15185 "Checked exception `%s' can't be thrown in initializer",
15186 lang_printable_name (type, 0));
15187 else
15188 parse_error_context (wfl_operator,
15189 "Checked exception `%s' isn't thrown from a `try' block",
15190 lang_printable_name (type, 0));
15192 /* Otherwise, the current method doesn't have the appropriate
15193 throws declaration */
15194 else
15195 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15196 lang_printable_name (type, 0));
15197 return error_mark_node;
15200 if (! flag_emit_class_files && ! flag_emit_xref)
15201 BUILD_THROW (node, expr);
15203 /* If doing xrefs, keep the location where the `throw' was seen. */
15204 if (flag_emit_xref)
15205 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15206 return node;
15209 /* Check that exception said to be thrown by method DECL can be
15210 effectively caught from where DECL is invoked. */
15212 static void
15213 check_thrown_exceptions (location, decl)
15214 int location;
15215 tree decl;
15217 tree throws;
15218 /* For all the unchecked exceptions thrown by DECL */
15219 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15220 throws = TREE_CHAIN (throws))
15221 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15223 #if 1
15224 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15225 if (DECL_NAME (decl) == get_identifier ("clone"))
15226 continue;
15227 #endif
15228 EXPR_WFL_LINECOL (wfl_operator) = location;
15229 if (DECL_FINIT_P (current_function_decl))
15230 parse_error_context
15231 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15232 lang_printable_name (TREE_VALUE (throws), 0));
15233 else
15235 parse_error_context
15236 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15237 lang_printable_name (TREE_VALUE (throws), 0),
15238 (DECL_INIT_P (current_function_decl) ?
15239 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15240 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15245 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15246 try-catch blocks, OR is listed in the `throws' clause of the
15247 current method. */
15249 static int
15250 check_thrown_exceptions_do (exception)
15251 tree exception;
15253 tree list = currently_caught_type_list;
15254 resolve_and_layout (exception, NULL_TREE);
15255 /* First, all the nested try-catch-finally at that stage. The
15256 last element contains `throws' clause exceptions, if any. */
15257 if (IS_UNCHECKED_EXCEPTION_P (exception))
15258 return 1;
15259 while (list)
15261 tree caught;
15262 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15263 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15264 return 1;
15265 list = TREE_CHAIN (list);
15267 return 0;
15270 static void
15271 purge_unchecked_exceptions (mdecl)
15272 tree mdecl;
15274 tree throws = DECL_FUNCTION_THROWS (mdecl);
15275 tree new = NULL_TREE;
15277 while (throws)
15279 tree next = TREE_CHAIN (throws);
15280 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15282 TREE_CHAIN (throws) = new;
15283 new = throws;
15285 throws = next;
15287 /* List is inverted here, but it doesn't matter */
15288 DECL_FUNCTION_THROWS (mdecl) = new;
15291 /* 15.24 Conditional Operator ?: */
15293 static tree
15294 patch_conditional_expr (node, wfl_cond, wfl_op1)
15295 tree node, wfl_cond, wfl_op1;
15297 tree cond = TREE_OPERAND (node, 0);
15298 tree op1 = TREE_OPERAND (node, 1);
15299 tree op2 = TREE_OPERAND (node, 2);
15300 tree resulting_type = NULL_TREE;
15301 tree t1, t2, patched;
15302 int error_found = 0;
15304 /* Operands of ?: might be StringBuffers crafted as a result of a
15305 string concatenation. Obtain a descent operand here. */
15306 if ((patched = patch_string (op1)))
15307 TREE_OPERAND (node, 1) = op1 = patched;
15308 if ((patched = patch_string (op2)))
15309 TREE_OPERAND (node, 2) = op2 = patched;
15311 t1 = TREE_TYPE (op1);
15312 t2 = TREE_TYPE (op2);
15314 /* The first expression must be a boolean */
15315 if (TREE_TYPE (cond) != boolean_type_node)
15317 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15318 parse_error_context (wfl_operator,
15319 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15320 lang_printable_name (TREE_TYPE (cond), 0));
15321 error_found = 1;
15324 /* Second and third can be numeric, boolean (i.e. primitive),
15325 references or null. Anything else results in an error */
15326 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15327 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15328 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15329 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15330 error_found = 1;
15332 /* Determine the type of the conditional expression. Same types are
15333 easy to deal with */
15334 else if (t1 == t2)
15335 resulting_type = t1;
15337 /* There are different rules for numeric types */
15338 else if (JNUMERIC_TYPE_P (t1))
15340 /* if byte/short found, the resulting type is short */
15341 if ((t1 == byte_type_node && t2 == short_type_node)
15342 || (t1 == short_type_node && t2 == byte_type_node))
15343 resulting_type = short_type_node;
15345 /* If t1 is a constant int and t2 is of type byte, short or char
15346 and t1's value fits in t2, then the resulting type is t2 */
15347 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15348 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15349 resulting_type = t2;
15351 /* If t2 is a constant int and t1 is of type byte, short or char
15352 and t2's value fits in t1, then the resulting type is t1 */
15353 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15354 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15355 resulting_type = t1;
15357 /* Otherwise, binary numeric promotion is applied and the
15358 resulting type is the promoted type of operand 1 and 2 */
15359 else
15360 resulting_type = binary_numeric_promotion (t1, t2,
15361 &TREE_OPERAND (node, 1),
15362 &TREE_OPERAND (node, 2));
15365 /* Cases of a reference and a null type */
15366 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15367 resulting_type = t1;
15369 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15370 resulting_type = t2;
15372 /* Last case: different reference types. If a type can be converted
15373 into the other one by assignment conversion, the latter
15374 determines the type of the expression */
15375 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15376 resulting_type = promote_type (t1);
15378 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15379 resulting_type = promote_type (t2);
15381 /* If we don't have any resulting type, we're in trouble */
15382 if (!resulting_type)
15384 char *t = xstrdup (lang_printable_name (t1, 0));
15385 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15386 parse_error_context (wfl_operator,
15387 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15388 t, lang_printable_name (t2, 0));
15389 free (t);
15390 error_found = 1;
15393 if (error_found)
15395 TREE_TYPE (node) = error_mark_node;
15396 return error_mark_node;
15399 TREE_TYPE (node) = resulting_type;
15400 TREE_SET_CODE (node, COND_EXPR);
15401 CAN_COMPLETE_NORMALLY (node) = 1;
15402 return node;
15405 /* Try to constant fold NODE.
15406 If NODE is not a constant expression, return NULL_EXPR.
15407 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15409 static tree
15410 fold_constant_for_init (node, context)
15411 tree node;
15412 tree context;
15414 tree op0, op1, val;
15415 enum tree_code code = TREE_CODE (node);
15417 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
15418 return node;
15420 switch (code)
15422 case PLUS_EXPR:
15423 case MINUS_EXPR:
15424 case MULT_EXPR:
15425 case TRUNC_MOD_EXPR:
15426 case RDIV_EXPR:
15427 case LSHIFT_EXPR:
15428 case RSHIFT_EXPR:
15429 case URSHIFT_EXPR:
15430 case BIT_AND_EXPR:
15431 case BIT_XOR_EXPR:
15432 case BIT_IOR_EXPR:
15433 case TRUTH_ANDIF_EXPR:
15434 case TRUTH_ORIF_EXPR:
15435 case EQ_EXPR:
15436 case NE_EXPR:
15437 case GT_EXPR:
15438 case GE_EXPR:
15439 case LT_EXPR:
15440 case LE_EXPR:
15441 op0 = TREE_OPERAND (node, 0);
15442 op1 = TREE_OPERAND (node, 1);
15443 val = fold_constant_for_init (op0, context);
15444 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15445 return NULL_TREE;
15446 TREE_OPERAND (node, 0) = val;
15447 val = fold_constant_for_init (op1, context);
15448 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15449 return NULL_TREE;
15450 TREE_OPERAND (node, 1) = val;
15451 return patch_binop (node, op0, op1);
15453 case UNARY_PLUS_EXPR:
15454 case NEGATE_EXPR:
15455 case TRUTH_NOT_EXPR:
15456 case BIT_NOT_EXPR:
15457 case CONVERT_EXPR:
15458 op0 = TREE_OPERAND (node, 0);
15459 val = fold_constant_for_init (op0, context);
15460 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15461 return NULL_TREE;
15462 TREE_OPERAND (node, 0) = val;
15463 return patch_unaryop (node, op0);
15464 break;
15466 case COND_EXPR:
15467 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15468 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15469 return NULL_TREE;
15470 TREE_OPERAND (node, 0) = val;
15471 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15472 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15473 return NULL_TREE;
15474 TREE_OPERAND (node, 1) = val;
15475 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15476 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15477 return NULL_TREE;
15478 TREE_OPERAND (node, 2) = val;
15479 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15480 : TREE_OPERAND (node, 2);
15482 case VAR_DECL:
15483 case FIELD_DECL:
15484 if (! FIELD_FINAL (node)
15485 || DECL_INITIAL (node) == NULL_TREE)
15486 return NULL_TREE;
15487 val = DECL_INITIAL (node);
15488 /* Guard against infinite recursion. */
15489 DECL_INITIAL (node) = NULL_TREE;
15490 val = fold_constant_for_init (val, node);
15491 DECL_INITIAL (node) = val;
15492 if (!val && CLASS_FINAL_VARIABLE_P (node))
15493 DECL_FIELD_FINAL_IUD (node) = 0;
15494 return val;
15496 case EXPR_WITH_FILE_LOCATION:
15497 /* Compare java_complete_tree and resolve_expression_name. */
15498 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15499 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15501 tree name = EXPR_WFL_NODE (node);
15502 tree decl;
15503 if (PRIMARY_P (node))
15504 return NULL_TREE;
15505 else if (! QUALIFIED_P (name))
15507 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15508 if (decl == NULL_TREE
15509 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15510 return NULL_TREE;
15511 return fold_constant_for_init (decl, decl);
15513 else
15515 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15516 qualify_ambiguous_name (node);
15517 if (resolve_field_access (node, &decl, NULL)
15518 && decl != NULL_TREE)
15519 return fold_constant_for_init (decl, decl);
15520 return NULL_TREE;
15523 else
15525 op0 = TREE_OPERAND (node, 0);
15526 val = fold_constant_for_init (op0, context);
15527 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15528 return NULL_TREE;
15529 TREE_OPERAND (node, 0) = val;
15530 return val;
15533 #ifdef USE_COMPONENT_REF
15534 case IDENTIFIER:
15535 case COMPONENT_REF:
15537 #endif
15539 default:
15540 return NULL_TREE;
15544 #ifdef USE_COMPONENT_REF
15545 /* Context is 'T' for TypeName, 'P' for PackageName,
15546 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15548 tree
15549 resolve_simple_name (name, context)
15550 tree name;
15551 int context;
15555 tree
15556 resolve_qualified_name (name, context)
15557 tree name;
15558 int context;
15561 #endif
15563 /* Mark P, which is really a `struct parser_ctxt **' for GC. */
15565 static void
15566 mark_parser_ctxt (p)
15567 void *p;
15569 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15570 int i;
15572 if (!pc)
15573 return;
15575 #ifndef JC1_LITE
15576 for (i = 0; i < 11; ++i)
15577 ggc_mark_tree (pc->modifier_ctx[i]);
15578 ggc_mark_tree (pc->class_type);
15579 ggc_mark_tree (pc->function_decl);
15580 ggc_mark_tree (pc->package);
15581 ggc_mark_tree (pc->incomplete_class);
15582 ggc_mark_tree (pc->gclass_list);
15583 ggc_mark_tree (pc->class_list);
15584 ggc_mark_tree (pc->current_parsed_class);
15585 ggc_mark_tree (pc->current_parsed_class_un);
15586 ggc_mark_tree (pc->non_static_initialized);
15587 ggc_mark_tree (pc->static_initialized);
15588 ggc_mark_tree (pc->instance_initializers);
15589 ggc_mark_tree (pc->import_list);
15590 ggc_mark_tree (pc->import_demand_list);
15591 ggc_mark_tree (pc->current_loop);
15592 ggc_mark_tree (pc->current_labeled_block);
15593 #endif /* JC1_LITE */
15595 if (pc->next)
15596 mark_parser_ctxt (&pc->next);