2001-01-23 Alexandre Petit-Bianco <apbianco@cygnus.com>
[official-gcc.git] / gcc / java / parse.y
blob187c5e97c79ed6aa44fad4dbbe4cf75a10658aae
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 "defaults.h"
68 #include "ggc.h"
70 #ifndef DIR_SEPARATOR
71 #define DIR_SEPARATOR '/'
72 #endif
74 /* Local function prototypes */
75 static char *java_accstring_lookup PARAMS ((int));
76 static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
77 static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
78 static tree create_class PARAMS ((int, tree, tree, tree));
79 static tree create_interface PARAMS ((int, tree, tree));
80 static void end_class_declaration PARAMS ((int));
81 static tree find_field PARAMS ((tree, tree));
82 static tree lookup_field_wrapper PARAMS ((tree, tree));
83 static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
84 static void register_fields PARAMS ((int, tree, tree));
85 static tree parser_qualified_classname PARAMS ((tree));
86 static int parser_check_super PARAMS ((tree, tree, tree));
87 static int parser_check_super_interface PARAMS ((tree, tree, tree));
88 static void check_modifiers_consistency PARAMS ((int));
89 static tree lookup_cl PARAMS ((tree));
90 static tree lookup_java_method2 PARAMS ((tree, tree, int));
91 static tree method_header PARAMS ((int, tree, tree, tree));
92 static void fix_method_argument_names PARAMS ((tree ,tree));
93 static tree method_declarator PARAMS ((tree, tree));
94 static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
95 ATTRIBUTE_PRINTF_2;
96 static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list))
97 ATTRIBUTE_PRINTF (2, 0);
98 static void parse_ctor_invocation_error PARAMS ((void));
99 static tree parse_jdk1_1_error PARAMS ((const char *));
100 static void complete_class_report_errors PARAMS ((jdep *));
101 static int process_imports PARAMS ((void));
102 static void read_import_dir PARAMS ((tree));
103 static int find_in_imports_on_demand PARAMS ((tree));
104 static void find_in_imports PARAMS ((tree));
105 static void check_static_final_variable_assignment_flag PARAMS ((tree));
106 static void reset_static_final_variable_assignment_flag PARAMS ((tree));
107 static void check_final_variable_local_assignment_flag PARAMS ((tree, tree));
108 static void reset_final_variable_local_assignment_flag PARAMS ((tree));
109 static int check_final_variable_indirect_assignment PARAMS ((tree));
110 static void check_final_variable_global_assignment_flag PARAMS ((tree));
111 static void check_inner_class_access PARAMS ((tree, tree, tree));
112 static int check_pkg_class_access PARAMS ((tree, tree));
113 static void register_package PARAMS ((tree));
114 static tree resolve_package PARAMS ((tree, tree *));
115 static tree lookup_package_type PARAMS ((const char *, int));
116 static tree resolve_class PARAMS ((tree, tree, tree, tree));
117 static void declare_local_variables PARAMS ((int, tree, tree));
118 static void source_start_java_method PARAMS ((tree));
119 static void source_end_java_method PARAMS ((void));
120 static void expand_start_java_method PARAMS ((tree));
121 static tree find_name_in_single_imports PARAMS ((tree));
122 static void check_abstract_method_header PARAMS ((tree));
123 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
124 static tree resolve_expression_name PARAMS ((tree, tree *));
125 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
126 static int check_class_interface_creation PARAMS ((int, int, tree,
127 tree, tree, tree));
128 static tree patch_method_invocation PARAMS ((tree, tree, tree,
129 int *, tree *));
130 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
131 static tree resolve_and_layout PARAMS ((tree, tree));
132 static tree qualify_and_find PARAMS ((tree, tree, tree));
133 static tree resolve_no_layout PARAMS ((tree, tree));
134 static int invocation_mode PARAMS ((tree, int));
135 static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
136 tree, tree));
137 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
138 tree *, tree *));
139 static tree find_most_specific_methods_list PARAMS ((tree));
140 static int argument_types_convertible PARAMS ((tree, tree));
141 static tree patch_invoke PARAMS ((tree, tree, tree));
142 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
143 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
144 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
145 static tree obtain_incomplete_type PARAMS ((tree));
146 static tree java_complete_lhs PARAMS ((tree));
147 static tree java_complete_tree PARAMS ((tree));
148 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
149 static int analyze_clinit_body PARAMS ((tree));
150 static int maybe_yank_clinit PARAMS ((tree));
151 static void java_complete_expand_method PARAMS ((tree));
152 static int unresolved_type_p PARAMS ((tree, tree *));
153 static void create_jdep_list PARAMS ((struct parser_ctxt *));
154 static tree build_expr_block PARAMS ((tree, tree));
155 static tree enter_block PARAMS ((void));
156 static tree enter_a_block PARAMS ((tree));
157 static tree exit_block PARAMS ((void));
158 static tree lookup_name_in_blocks PARAMS ((tree));
159 static void maybe_absorb_scoping_blocks PARAMS ((void));
160 static tree build_method_invocation PARAMS ((tree, tree));
161 static tree build_new_invocation PARAMS ((tree, tree));
162 static tree build_assignment PARAMS ((int, int, tree, tree));
163 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
164 static int check_final_assignment PARAMS ((tree ,tree));
165 static tree patch_assignment PARAMS ((tree, tree, tree ));
166 static tree patch_binop PARAMS ((tree, tree, tree));
167 static tree build_unaryop PARAMS ((int, int, tree));
168 static tree build_incdec PARAMS ((int, int, tree, int));
169 static tree patch_unaryop PARAMS ((tree, tree));
170 static tree build_cast PARAMS ((int, tree, tree));
171 static tree build_null_of_type PARAMS ((tree));
172 static tree patch_cast PARAMS ((tree, tree));
173 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
174 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
175 static int valid_cast_to_p PARAMS ((tree, tree));
176 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
177 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
178 static tree try_reference_assignconv PARAMS ((tree, tree));
179 static tree build_unresolved_array_type PARAMS ((tree));
180 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
181 static tree build_array_ref PARAMS ((int, tree, tree));
182 static tree patch_array_ref PARAMS ((tree));
183 static tree make_qualified_name PARAMS ((tree, tree, int));
184 static tree merge_qualified_name PARAMS ((tree, tree));
185 static tree make_qualified_primary PARAMS ((tree, tree, int));
186 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
187 tree *, tree *));
188 static void qualify_ambiguous_name PARAMS ((tree));
189 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
190 static tree build_newarray_node PARAMS ((tree, tree, int));
191 static tree patch_newarray PARAMS ((tree));
192 static tree resolve_type_during_patch PARAMS ((tree));
193 static tree build_this PARAMS ((int));
194 static tree build_wfl_wrap PARAMS ((tree, int));
195 static tree build_return PARAMS ((int, tree));
196 static tree patch_return PARAMS ((tree));
197 static tree maybe_access_field PARAMS ((tree, tree, tree));
198 static int complete_function_arguments PARAMS ((tree));
199 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
200 tree, tree));
201 static int not_accessible_p PARAMS ((tree, tree, int));
202 static void check_deprecation PARAMS ((tree, tree));
203 static int class_in_current_package PARAMS ((tree));
204 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
205 static tree patch_if_else_statement PARAMS ((tree));
206 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
207 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
208 static tree patch_exit_expr PARAMS ((tree));
209 static tree build_labeled_block PARAMS ((int, tree));
210 static tree finish_labeled_statement PARAMS ((tree, tree));
211 static tree build_bc_statement PARAMS ((int, int, tree));
212 static tree patch_bc_statement PARAMS ((tree));
213 static tree patch_loop_statement PARAMS ((tree));
214 static tree build_new_loop PARAMS ((tree));
215 static tree build_loop_body PARAMS ((int, tree, int));
216 static tree finish_loop_body PARAMS ((int, tree, tree, int));
217 static tree build_debugable_stmt PARAMS ((int, tree));
218 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
219 static tree patch_switch_statement PARAMS ((tree));
220 static tree string_constant_concatenation PARAMS ((tree, tree));
221 static tree build_string_concatenation PARAMS ((tree, tree));
222 static tree patch_string_cst PARAMS ((tree));
223 static tree patch_string PARAMS ((tree));
224 static tree build_try_statement PARAMS ((int, tree, tree));
225 static tree build_try_finally_statement PARAMS ((int, tree, tree));
226 static tree patch_try_statement PARAMS ((tree));
227 static tree patch_synchronized_statement PARAMS ((tree, tree));
228 static tree patch_throw_statement PARAMS ((tree, tree));
229 static void check_thrown_exceptions PARAMS ((int, tree));
230 static int check_thrown_exceptions_do PARAMS ((tree));
231 static void purge_unchecked_exceptions PARAMS ((tree));
232 static void check_throws_clauses PARAMS ((tree, tree, tree));
233 static void finish_method_declaration PARAMS ((tree));
234 static tree build_super_invocation PARAMS ((tree));
235 static int verify_constructor_circularity PARAMS ((tree, tree));
236 static char *constructor_circularity_msg PARAMS ((tree, tree));
237 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
238 int, int));
239 static const char *get_printable_method_name PARAMS ((tree));
240 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
241 static tree generate_finit PARAMS ((tree));
242 static void add_instance_initializer PARAMS ((tree));
243 static void fix_constructors PARAMS ((tree));
244 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
245 tree, int *));
246 static void craft_constructor PARAMS ((tree, tree));
247 static int verify_constructor_super PARAMS ((tree));
248 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
249 static void start_artificial_method_body PARAMS ((tree));
250 static void end_artificial_method_body PARAMS ((tree));
251 static int check_method_redefinition PARAMS ((tree, tree));
252 static int reset_method_name PARAMS ((tree));
253 static int check_method_types_complete PARAMS ((tree));
254 static void java_check_regular_methods PARAMS ((tree));
255 static void java_check_abstract_methods PARAMS ((tree));
256 static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
257 static void unreachable_stmt_error PARAMS ((tree));
258 static tree find_expr_with_wfl PARAMS ((tree));
259 static void missing_return_error PARAMS ((tree));
260 static tree build_new_array_init PARAMS ((int, tree));
261 static tree patch_new_array_init PARAMS ((tree, tree));
262 static tree maybe_build_array_element_wfl PARAMS ((tree));
263 static int array_constructor_check_entry PARAMS ((tree, tree));
264 static const char *purify_type_name PARAMS ((const char *));
265 static tree fold_constant_for_init PARAMS ((tree, tree));
266 static tree strip_out_static_field_access_decl PARAMS ((tree));
267 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
268 static void static_ref_err PARAMS ((tree, tree, tree));
269 static void parser_add_interface PARAMS ((tree, tree, tree));
270 static void add_superinterfaces PARAMS ((tree, tree));
271 static tree jdep_resolve_class PARAMS ((jdep *));
272 static int note_possible_classname PARAMS ((const char *, int));
273 static void java_complete_expand_classes PARAMS ((void));
274 static void java_complete_expand_class PARAMS ((tree));
275 static void java_complete_expand_methods PARAMS ((tree));
276 static tree cut_identifier_in_qualified PARAMS ((tree));
277 static tree java_stabilize_reference PARAMS ((tree));
278 static tree do_unary_numeric_promotion PARAMS ((tree));
279 static char * operator_string PARAMS ((tree));
280 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
281 static tree merge_string_cste PARAMS ((tree, tree, int));
282 static tree java_refold PARAMS ((tree));
283 static int java_decl_equiv PARAMS ((tree, tree));
284 static int binop_compound_p PARAMS ((enum tree_code));
285 static tree search_loop PARAMS ((tree));
286 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
287 static int check_abstract_method_definitions PARAMS ((int, tree, tree));
288 static void java_check_abstract_method_definitions PARAMS ((tree));
289 static void java_debug_context_do PARAMS ((int));
290 static void java_parser_context_push_initialized_field PARAMS ((void));
291 static void java_parser_context_pop_initialized_field PARAMS ((void));
292 static tree reorder_static_initialized PARAMS ((tree));
293 static void java_parser_context_suspend PARAMS ((void));
294 static void java_parser_context_resume PARAMS ((void));
295 static int pop_current_osb PARAMS ((struct parser_ctxt *));
297 /* JDK 1.1 work. FIXME */
299 static tree maybe_make_nested_class_name PARAMS ((tree));
300 static void make_nested_class_name PARAMS ((tree));
301 static void set_nested_class_simple_name_value PARAMS ((tree, int));
302 static void link_nested_class_to_enclosing PARAMS ((void));
303 static tree find_as_inner_class PARAMS ((tree, tree, tree));
304 static tree find_as_inner_class_do PARAMS ((tree, tree));
305 static int check_inner_class_redefinition PARAMS ((tree, tree));
307 static tree build_thisn_assign PARAMS ((void));
308 static tree build_current_thisn PARAMS ((tree));
309 static tree build_access_to_thisn PARAMS ((tree, tree, int));
310 static tree maybe_build_thisn_access_method PARAMS ((tree));
312 static tree build_outer_field_access PARAMS ((tree, tree));
313 static tree build_outer_field_access_methods PARAMS ((tree));
314 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
315 tree, tree));
316 static tree build_outer_method_access_method PARAMS ((tree));
317 static tree build_new_access_id PARAMS ((void));
318 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
319 tree, tree));
321 static int outer_field_access_p PARAMS ((tree, tree));
322 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
323 tree *, tree *));
324 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
325 static tree build_incomplete_class_ref PARAMS ((int, tree));
326 static tree patch_incomplete_class_ref PARAMS ((tree));
327 static tree create_anonymous_class PARAMS ((int, tree));
328 static void patch_anonymous_class PARAMS ((tree, tree, tree));
329 static void add_inner_class_fields PARAMS ((tree, tree));
331 static tree build_dot_class_method PARAMS ((tree));
332 static tree build_dot_class_method_invocation PARAMS ((tree));
333 static void create_new_parser_context PARAMS ((int));
334 static void mark_parser_ctxt PARAMS ((void *));
336 /* Number of error found so far. */
337 int java_error_count;
338 /* Number of warning found so far. */
339 int java_warning_count;
340 /* Tell when not to fold, when doing xrefs */
341 int do_not_fold;
342 /* Cyclic inheritance report, as it can be set by layout_class */
343 const char *cyclic_inheritance_report;
345 /* Tell when we're within an instance initializer */
346 static int in_instance_initializer;
348 /* The current parser context */
349 struct parser_ctxt *ctxp;
351 /* List of things that were analyzed for which code will be generated */
352 static struct parser_ctxt *ctxp_for_generation = NULL;
354 /* binop_lookup maps token to tree_code. It is used where binary
355 operations are involved and required by the parser. RDIV_EXPR
356 covers both integral/floating point division. The code is changed
357 once the type of both operator is worked out. */
359 static enum tree_code binop_lookup[19] =
361 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
362 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
363 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
364 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
365 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
367 #define BINOP_LOOKUP(VALUE) \
368 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
370 /* This is the end index for binary operators that can also be used
371 in compound assignements. */
372 #define BINOP_COMPOUND_CANDIDATES 11
374 /* The "$L" identifier we use to create labels. */
375 static tree label_id = NULL_TREE;
377 /* The "StringBuffer" identifier used for the String `+' operator. */
378 static tree wfl_string_buffer = NULL_TREE;
380 /* The "append" identifier used for String `+' operator. */
381 static tree wfl_append = NULL_TREE;
383 /* The "toString" identifier used for String `+' operator. */
384 static tree wfl_to_string = NULL_TREE;
386 /* The "java.lang" import qualified name. */
387 static tree java_lang_id = NULL_TREE;
389 /* The generated `inst$' identifier used for generated enclosing
390 instance/field access functions. */
391 static tree inst_id = NULL_TREE;
393 /* The "java.lang.Cloneable" qualified name. */
394 static tree java_lang_cloneable = NULL_TREE;
396 /* The "java.io.Serializable" qualified name. */
397 static tree java_io_serializable = NULL_TREE;
399 /* Context and flag for static blocks */
400 static tree current_static_block = NULL_TREE;
402 /* The generated `write_parm_value$' identifier. */
403 static tree wpv_id;
405 /* The list of all packages we've seen so far */
406 static tree package_list = NULL_TREE;
408 /* Hold THIS for the scope of the current public method decl. */
409 static tree current_this;
411 /* Hold a list of catch clauses list. The first element of this list is
412 the list of the catch clauses of the currently analysed try block. */
413 static tree currently_caught_type_list;
415 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
416 line and point it out. */
417 /* Should point out the one that don't fit. ASCII/unicode, going
418 backward. FIXME */
420 #define check_modifiers(__message, __value, __mask) do { \
421 if ((__value) & ~(__mask)) \
423 int i, remainder = (__value) & ~(__mask); \
424 for (i = 0; i <= 10; i++) \
425 if ((1 << i) & remainder) \
426 parse_error_context (ctxp->modifier_ctx [i], (__message), \
427 java_accstring_lookup (1 << i)); \
429 } while (0)
433 %union {
434 tree node;
435 int sub_token;
436 struct {
437 int token;
438 int location;
439 } operator;
440 int value;
444 #include "lex.c"
447 %pure_parser
449 /* Things defined here have to match the order of what's in the
450 binop_lookup table. */
452 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
453 %token LS_TK SRS_TK ZRS_TK
454 %token AND_TK XOR_TK OR_TK
455 %token BOOL_AND_TK BOOL_OR_TK
456 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
458 /* This maps to the same binop_lookup entry than the token above */
460 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
461 %token REM_ASSIGN_TK
462 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
463 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
466 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
468 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
469 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
470 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
471 %token PAD_TK ABSTRACT_TK MODIFIER_TK
472 %token STRICT_TK
474 /* Keep those two in order, too */
475 %token DECR_TK INCR_TK
477 /* From now one, things can be in any order */
479 %token DEFAULT_TK IF_TK THROW_TK
480 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
481 %token THROWS_TK BREAK_TK IMPORT_TK
482 %token ELSE_TK INSTANCEOF_TK RETURN_TK
483 %token VOID_TK CATCH_TK INTERFACE_TK
484 %token CASE_TK EXTENDS_TK FINALLY_TK
485 %token SUPER_TK WHILE_TK CLASS_TK
486 %token SWITCH_TK CONST_TK TRY_TK
487 %token FOR_TK NEW_TK CONTINUE_TK
488 %token GOTO_TK PACKAGE_TK THIS_TK
490 %token BYTE_TK SHORT_TK INT_TK LONG_TK
491 %token CHAR_TK INTEGRAL_TK
493 %token FLOAT_TK DOUBLE_TK FP_TK
495 %token ID_TK
497 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
499 %token ASSIGN_ANY_TK ASSIGN_TK
500 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
502 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
503 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
505 %type <value> modifiers MODIFIER_TK final synchronized
507 %type <node> super ID_TK identifier
508 %type <node> name simple_name qualified_name
509 %type <node> type_declaration compilation_unit
510 field_declaration method_declaration extends_interfaces
511 interfaces interface_type_list
512 class_member_declaration
513 import_declarations package_declaration
514 type_declarations interface_body
515 interface_member_declaration constant_declaration
516 interface_member_declarations interface_type
517 abstract_method_declaration interface_type_list
518 %type <node> class_body_declaration class_member_declaration
519 static_initializer constructor_declaration block
520 %type <node> class_body_declarations constructor_header
521 %type <node> class_or_interface_type class_type class_type_list
522 constructor_declarator explicit_constructor_invocation
523 %type <node> dim_expr dim_exprs this_or_super throws
525 %type <node> variable_declarator_id variable_declarator
526 variable_declarators variable_initializer
527 variable_initializers constructor_body
528 array_initializer
530 %type <node> class_body block_end constructor_block_end
531 %type <node> statement statement_without_trailing_substatement
532 labeled_statement if_then_statement label_decl
533 if_then_else_statement while_statement for_statement
534 statement_nsi labeled_statement_nsi do_statement
535 if_then_else_statement_nsi while_statement_nsi
536 for_statement_nsi statement_expression_list for_init
537 for_update statement_expression expression_statement
538 primary_no_new_array expression primary
539 array_creation_expression array_type
540 class_instance_creation_expression field_access
541 method_invocation array_access something_dot_new
542 argument_list postfix_expression while_expression
543 post_increment_expression post_decrement_expression
544 unary_expression_not_plus_minus unary_expression
545 pre_increment_expression pre_decrement_expression
546 unary_expression_not_plus_minus cast_expression
547 multiplicative_expression additive_expression
548 shift_expression relational_expression
549 equality_expression and_expression
550 exclusive_or_expression inclusive_or_expression
551 conditional_and_expression conditional_or_expression
552 conditional_expression assignment_expression
553 left_hand_side assignment for_header for_begin
554 constant_expression do_statement_begin empty_statement
555 switch_statement synchronized_statement throw_statement
556 try_statement switch_expression switch_block
557 catches catch_clause catch_clause_parameter finally
558 anonymous_class_creation
559 %type <node> return_statement break_statement continue_statement
561 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
562 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
563 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
564 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
565 %type <operator> ASSIGN_ANY_TK assignment_operator
566 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
567 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
568 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
569 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
570 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
571 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
572 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
573 %type <operator> NEW_TK
575 %type <node> method_body
577 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
578 STRING_LIT_TK NULL_TK VOID_TK
580 %type <node> IF_TK WHILE_TK FOR_TK
582 %type <node> formal_parameter_list formal_parameter
583 method_declarator method_header
585 %type <node> primitive_type reference_type type
586 BOOLEAN_TK INTEGRAL_TK FP_TK
588 /* Added or modified JDK 1.1 rule types */
589 %type <node> type_literals
592 /* 19.2 Production from 2.3: The Syntactic Grammar */
593 goal:
595 /* Register static variables with the garbage
596 collector. */
597 ggc_add_tree_root (&label_id, 1);
598 ggc_add_tree_root (&wfl_string_buffer, 1);
599 ggc_add_tree_root (&wfl_append, 1);
600 ggc_add_tree_root (&wfl_to_string, 1);
601 ggc_add_tree_root (&java_lang_id, 1);
602 ggc_add_tree_root (&inst_id, 1);
603 ggc_add_tree_root (&java_lang_cloneable, 1);
604 ggc_add_tree_root (&java_io_serializable, 1);
605 ggc_add_tree_root (&current_static_block, 1);
606 ggc_add_tree_root (&wpv_id, 1);
607 ggc_add_tree_root (&package_list, 1);
608 ggc_add_tree_root (&current_this, 1);
609 ggc_add_tree_root (&currently_caught_type_list, 1);
610 ggc_add_root (&ctxp, 1,
611 sizeof (struct parser_ctxt *),
612 mark_parser_ctxt);
613 ggc_add_root (&ctxp_for_generation, 1,
614 sizeof (struct parser_ctxt *),
615 mark_parser_ctxt);
617 compilation_unit
621 /* 19.3 Productions from 3: Lexical structure */
622 literal:
623 INT_LIT_TK
624 | FP_LIT_TK
625 | BOOL_LIT_TK
626 | CHAR_LIT_TK
627 | STRING_LIT_TK
628 | NULL_TK
631 /* 19.4 Productions from 4: Types, Values and Variables */
632 type:
633 primitive_type
634 | reference_type
637 primitive_type:
638 INTEGRAL_TK
639 | FP_TK
640 | BOOLEAN_TK
643 reference_type:
644 class_or_interface_type
645 | array_type
648 class_or_interface_type:
649 name
652 class_type:
653 class_or_interface_type /* Default rule */
656 interface_type:
657 class_or_interface_type
660 array_type:
661 primitive_type dims
663 int osb = pop_current_osb (ctxp);
664 tree t = build_java_array_type (($1), -1);
665 CLASS_LOADED_P (t) = 1;
666 while (--osb)
667 t = build_unresolved_array_type (t);
668 $$ = t;
670 | name dims
672 int osb = pop_current_osb (ctxp);
673 tree t = $1;
674 while (osb--)
675 t = build_unresolved_array_type (t);
676 $$ = t;
680 /* 19.5 Productions from 6: Names */
681 name:
682 simple_name /* Default rule */
683 | qualified_name /* Default rule */
686 simple_name:
687 identifier /* Default rule */
690 qualified_name:
691 name DOT_TK identifier
692 { $$ = make_qualified_name ($1, $3, $2.location); }
695 identifier:
696 ID_TK
699 /* 19.6: Production from 7: Packages */
700 compilation_unit:
701 {$$ = NULL;}
702 | package_declaration
703 | import_declarations
704 | type_declarations
705 | package_declaration import_declarations
706 | package_declaration type_declarations
707 | import_declarations type_declarations
708 | package_declaration import_declarations type_declarations
711 import_declarations:
712 import_declaration
714 $$ = NULL;
716 | import_declarations import_declaration
718 $$ = NULL;
722 type_declarations:
723 type_declaration
724 | type_declarations type_declaration
727 package_declaration:
728 PACKAGE_TK name SC_TK
730 ctxp->package = EXPR_WFL_NODE ($2);
731 register_package (ctxp->package);
733 | PACKAGE_TK error
734 {yyerror ("Missing name"); RECOVER;}
735 | PACKAGE_TK name error
736 {yyerror ("';' expected"); RECOVER;}
739 import_declaration:
740 single_type_import_declaration
741 | type_import_on_demand_declaration
744 single_type_import_declaration:
745 IMPORT_TK name SC_TK
747 tree name = EXPR_WFL_NODE ($2), last_name;
748 int i = IDENTIFIER_LENGTH (name)-1;
749 const char *last = &IDENTIFIER_POINTER (name)[i];
750 while (last != IDENTIFIER_POINTER (name))
752 if (last [0] == '.')
753 break;
754 last--;
756 last_name = get_identifier (++last);
757 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
759 tree err = find_name_in_single_imports (last_name);
760 if (err && err != name)
761 parse_error_context
762 ($2, "Ambiguous class: `%s' and `%s'",
763 IDENTIFIER_POINTER (name),
764 IDENTIFIER_POINTER (err));
765 else
766 REGISTER_IMPORT ($2, last_name);
768 else
769 REGISTER_IMPORT ($2, last_name);
771 | IMPORT_TK error
772 {yyerror ("Missing name"); RECOVER;}
773 | IMPORT_TK name error
774 {yyerror ("';' expected"); RECOVER;}
777 type_import_on_demand_declaration:
778 IMPORT_TK name DOT_TK MULT_TK SC_TK
780 tree name = EXPR_WFL_NODE ($2);
781 /* Don't import java.lang.* twice. */
782 if (name != java_lang_id)
784 read_import_dir ($2);
785 ctxp->import_demand_list =
786 chainon (ctxp->import_demand_list,
787 build_tree_list ($2, NULL_TREE));
790 | IMPORT_TK name DOT_TK error
791 {yyerror ("'*' expected"); RECOVER;}
792 | IMPORT_TK name DOT_TK MULT_TK error
793 {yyerror ("';' expected"); RECOVER;}
796 type_declaration:
797 class_declaration
798 { end_class_declaration (0); }
799 | interface_declaration
800 { end_class_declaration (0); }
801 | empty_statement
802 | error
804 YYERROR_NOW;
805 yyerror ("Class or interface declaration expected");
809 /* 19.7 Shortened from the original:
810 modifiers: modifier | modifiers modifier
811 modifier: any of public... */
812 modifiers:
813 MODIFIER_TK
815 $$ = (1 << $1);
817 | modifiers MODIFIER_TK
819 int acc = (1 << $2);
820 if ($$ & acc)
821 parse_error_context
822 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
823 java_accstring_lookup (acc));
824 else
826 $$ |= acc;
831 /* 19.8.1 Production from $8.1: Class Declaration */
832 class_declaration:
833 modifiers CLASS_TK identifier super interfaces
834 { create_class ($1, $3, $4, $5); }
835 class_body
836 | CLASS_TK identifier super interfaces
837 { create_class (0, $2, $3, $4); }
838 class_body
839 | modifiers CLASS_TK error
840 {yyerror ("Missing class name"); RECOVER;}
841 | CLASS_TK error
842 {yyerror ("Missing class name"); RECOVER;}
843 | CLASS_TK identifier error
845 if (!ctxp->class_err) yyerror ("'{' expected");
846 DRECOVER(class1);
848 | modifiers CLASS_TK identifier error
849 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
852 super:
853 { $$ = NULL; }
854 | EXTENDS_TK class_type
855 { $$ = $2; }
856 | EXTENDS_TK class_type error
857 {yyerror ("'{' expected"); ctxp->class_err=1;}
858 | EXTENDS_TK error
859 {yyerror ("Missing super class name"); ctxp->class_err=1;}
862 interfaces:
863 { $$ = NULL_TREE; }
864 | IMPLEMENTS_TK interface_type_list
865 { $$ = $2; }
866 | IMPLEMENTS_TK error
868 ctxp->class_err=1;
869 yyerror ("Missing interface name");
873 interface_type_list:
874 interface_type
876 ctxp->interface_number = 1;
877 $$ = build_tree_list ($1, NULL_TREE);
879 | interface_type_list C_TK interface_type
881 ctxp->interface_number++;
882 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
884 | interface_type_list C_TK error
885 {yyerror ("Missing interface name"); RECOVER;}
888 class_body:
889 OCB_TK CCB_TK
891 /* Store the location of the `}' when doing xrefs */
892 if (flag_emit_xref)
893 DECL_END_SOURCE_LINE (GET_CPC ()) =
894 EXPR_WFL_ADD_COL ($2.location, 1);
895 $$ = GET_CPC ();
897 | OCB_TK class_body_declarations CCB_TK
899 /* Store the location of the `}' when doing xrefs */
900 if (flag_emit_xref)
901 DECL_END_SOURCE_LINE (GET_CPC ()) =
902 EXPR_WFL_ADD_COL ($3.location, 1);
903 $$ = GET_CPC ();
907 class_body_declarations:
908 class_body_declaration
909 | class_body_declarations class_body_declaration
912 class_body_declaration:
913 class_member_declaration
914 | static_initializer
915 | constructor_declaration
916 | block /* Added, JDK1.1, instance initializer */
918 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
919 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
923 class_member_declaration:
924 field_declaration
925 | method_declaration
926 | class_declaration /* Added, JDK1.1 inner classes */
927 { end_class_declaration (1); }
928 | interface_declaration /* Added, JDK1.1 inner interfaces */
929 { end_class_declaration (1); }
930 | empty_statement
933 /* 19.8.2 Productions from 8.3: Field Declarations */
934 field_declaration:
935 type variable_declarators SC_TK
936 { register_fields (0, $1, $2); }
937 | modifiers type variable_declarators SC_TK
939 check_modifiers
940 ("Illegal modifier `%s' for field declaration",
941 $1, FIELD_MODIFIERS);
942 check_modifiers_consistency ($1);
943 register_fields ($1, $2, $3);
947 variable_declarators:
948 /* Should we use build_decl_list () instead ? FIXME */
949 variable_declarator /* Default rule */
950 | variable_declarators C_TK variable_declarator
951 { $$ = chainon ($1, $3); }
952 | variable_declarators C_TK error
953 {yyerror ("Missing term"); RECOVER;}
956 variable_declarator:
957 variable_declarator_id
958 { $$ = build_tree_list ($1, NULL_TREE); }
959 | variable_declarator_id ASSIGN_TK variable_initializer
961 if (java_error_count)
962 $3 = NULL_TREE;
963 $$ = build_tree_list
964 ($1, build_assignment ($2.token, $2.location, $1, $3));
966 | variable_declarator_id ASSIGN_TK error
968 yyerror ("Missing variable initializer");
969 $$ = build_tree_list ($1, NULL_TREE);
970 RECOVER;
972 | variable_declarator_id ASSIGN_TK variable_initializer error
974 yyerror ("';' expected");
975 $$ = build_tree_list ($1, NULL_TREE);
976 RECOVER;
980 variable_declarator_id:
981 identifier
982 | variable_declarator_id OSB_TK CSB_TK
983 { $$ = build_unresolved_array_type ($1); }
984 | identifier error
985 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
986 | variable_declarator_id OSB_TK error
988 tree node = java_lval.node;
989 if (node && (TREE_CODE (node) == INTEGER_CST
990 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
991 yyerror ("Can't specify array dimension in a declaration");
992 else
993 yyerror ("']' expected");
994 DRECOVER(vdi);
996 | variable_declarator_id CSB_TK error
997 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1000 variable_initializer:
1001 expression
1002 | array_initializer
1005 /* 19.8.3 Productions from 8.4: Method Declarations */
1006 method_declaration:
1007 method_header
1009 current_function_decl = $1;
1010 if (current_function_decl
1011 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1012 source_start_java_method (current_function_decl);
1013 else
1014 current_function_decl = NULL_TREE;
1016 method_body
1017 { finish_method_declaration ($3); }
1018 | method_header error
1019 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1022 method_header:
1023 type method_declarator throws
1024 { $$ = method_header (0, $1, $2, $3); }
1025 | VOID_TK method_declarator throws
1026 { $$ = method_header (0, void_type_node, $2, $3); }
1027 | modifiers type method_declarator throws
1028 { $$ = method_header ($1, $2, $3, $4); }
1029 | modifiers VOID_TK method_declarator throws
1030 { $$ = method_header ($1, void_type_node, $3, $4); }
1031 | type error
1033 yyerror ("Invalid method declaration, method name required");
1034 RECOVER;
1036 | modifiers type error
1037 {RECOVER;}
1038 | VOID_TK error
1039 {yyerror ("Identifier expected"); RECOVER;}
1040 | modifiers VOID_TK error
1041 {yyerror ("Identifier expected"); RECOVER;}
1042 | modifiers error
1044 yyerror ("Invalid method declaration, return type required");
1045 RECOVER;
1049 method_declarator:
1050 identifier OP_TK CP_TK
1052 ctxp->formal_parameter_number = 0;
1053 $$ = method_declarator ($1, NULL_TREE);
1055 | identifier OP_TK formal_parameter_list CP_TK
1056 { $$ = method_declarator ($1, $3); }
1057 | method_declarator OSB_TK CSB_TK
1059 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1060 TREE_PURPOSE ($1) =
1061 build_unresolved_array_type (TREE_PURPOSE ($1));
1062 parse_warning_context
1063 (wfl_operator,
1064 "Discouraged form of returned type specification");
1066 | identifier OP_TK error
1067 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1068 | method_declarator OSB_TK error
1069 {yyerror ("']' expected"); RECOVER;}
1072 formal_parameter_list:
1073 formal_parameter
1075 ctxp->formal_parameter_number = 1;
1077 | formal_parameter_list C_TK formal_parameter
1079 ctxp->formal_parameter_number += 1;
1080 $$ = chainon ($1, $3);
1082 | formal_parameter_list C_TK error
1083 { yyerror ("Missing formal parameter term"); RECOVER; }
1086 formal_parameter:
1087 type variable_declarator_id
1089 $$ = build_tree_list ($2, $1);
1091 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1093 $$ = build_tree_list ($3, $2);
1094 ARG_FINAL_P ($$) = 1;
1096 | type error
1098 yyerror ("Missing identifier"); RECOVER;
1099 $$ = NULL_TREE;
1101 | final type error
1103 yyerror ("Missing identifier"); RECOVER;
1104 $$ = NULL_TREE;
1108 final:
1109 modifiers
1111 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1112 $1, ACC_FINAL);
1113 if ($1 != ACC_FINAL)
1114 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1118 throws:
1119 { $$ = NULL_TREE; }
1120 | THROWS_TK class_type_list
1121 { $$ = $2; }
1122 | THROWS_TK error
1123 {yyerror ("Missing class type term"); RECOVER;}
1126 class_type_list:
1127 class_type
1128 { $$ = build_tree_list ($1, $1); }
1129 | class_type_list C_TK class_type
1130 { $$ = tree_cons ($3, $3, $1); }
1131 | class_type_list C_TK error
1132 {yyerror ("Missing class type term"); RECOVER;}
1135 method_body:
1136 block
1137 | SC_TK { $$ = NULL_TREE; }
1140 /* 19.8.4 Productions from 8.5: Static Initializers */
1141 static_initializer:
1142 static block
1144 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1145 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1146 current_static_block = NULL_TREE;
1150 static: /* Test lval.sub_token here */
1151 modifiers
1153 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1154 /* Can't have a static initializer in an innerclass */
1155 if ($1 | ACC_STATIC &&
1156 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1157 parse_error_context
1158 (MODIFIER_WFL (STATIC_TK),
1159 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1160 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1161 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1165 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1166 constructor_declaration:
1167 constructor_header
1169 current_function_decl = $1;
1170 source_start_java_method (current_function_decl);
1172 constructor_body
1173 { finish_method_declaration ($3); }
1176 constructor_header:
1177 constructor_declarator throws
1178 { $$ = method_header (0, NULL_TREE, $1, $2); }
1179 | modifiers constructor_declarator throws
1180 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1183 constructor_declarator:
1184 simple_name OP_TK CP_TK
1186 ctxp->formal_parameter_number = 0;
1187 $$ = method_declarator ($1, NULL_TREE);
1189 | simple_name OP_TK formal_parameter_list CP_TK
1190 { $$ = method_declarator ($1, $3); }
1193 constructor_body:
1194 /* Unlike regular method, we always need a complete (empty)
1195 body so we can safely perform all the required code
1196 addition (super invocation and field initialization) */
1197 block_begin constructor_block_end
1199 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1200 $$ = $2;
1202 | block_begin explicit_constructor_invocation constructor_block_end
1203 { $$ = $3; }
1204 | block_begin block_statements constructor_block_end
1205 { $$ = $3; }
1206 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1207 { $$ = $4; }
1210 constructor_block_end:
1211 block_end
1214 /* Error recovery for that rule moved down expression_statement: rule. */
1215 explicit_constructor_invocation:
1216 this_or_super OP_TK CP_TK SC_TK
1218 $$ = build_method_invocation ($1, NULL_TREE);
1219 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1220 $$ = java_method_add_stmt (current_function_decl, $$);
1222 | this_or_super OP_TK argument_list CP_TK SC_TK
1224 $$ = build_method_invocation ($1, $3);
1225 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1226 $$ = java_method_add_stmt (current_function_decl, $$);
1228 /* Added, JDK1.1 inner classes. Modified because the rule
1229 'primary' couldn't work. */
1230 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1231 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1232 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1233 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1236 this_or_super: /* Added, simplifies error diagnostics */
1237 THIS_TK
1239 tree wfl = build_wfl_node (this_identifier_node);
1240 EXPR_WFL_LINECOL (wfl) = $1.location;
1241 $$ = wfl;
1243 | SUPER_TK
1245 tree wfl = build_wfl_node (super_identifier_node);
1246 EXPR_WFL_LINECOL (wfl) = $1.location;
1247 $$ = wfl;
1251 /* 19.9 Productions from 9: Interfaces */
1252 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1253 interface_declaration:
1254 INTERFACE_TK identifier
1255 { create_interface (0, $2, NULL_TREE); }
1256 interface_body
1257 | modifiers INTERFACE_TK identifier
1258 { create_interface ($1, $3, NULL_TREE); }
1259 interface_body
1260 | INTERFACE_TK identifier extends_interfaces
1261 { create_interface (0, $2, $3); }
1262 interface_body
1263 | modifiers INTERFACE_TK identifier extends_interfaces
1264 { create_interface ($1, $3, $4); }
1265 interface_body
1266 | INTERFACE_TK identifier error
1267 {yyerror ("'{' expected"); RECOVER;}
1268 | modifiers INTERFACE_TK identifier error
1269 {yyerror ("'{' expected"); RECOVER;}
1272 extends_interfaces:
1273 EXTENDS_TK interface_type
1275 ctxp->interface_number = 1;
1276 $$ = build_tree_list ($2, NULL_TREE);
1278 | extends_interfaces C_TK interface_type
1280 ctxp->interface_number++;
1281 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1283 | EXTENDS_TK error
1284 {yyerror ("Invalid interface type"); RECOVER;}
1285 | extends_interfaces C_TK error
1286 {yyerror ("Missing term"); RECOVER;}
1289 interface_body:
1290 OCB_TK CCB_TK
1291 { $$ = NULL_TREE; }
1292 | OCB_TK interface_member_declarations CCB_TK
1293 { $$ = NULL_TREE; }
1296 interface_member_declarations:
1297 interface_member_declaration
1298 | interface_member_declarations interface_member_declaration
1301 interface_member_declaration:
1302 constant_declaration
1303 | abstract_method_declaration
1304 | class_declaration /* Added, JDK1.1 inner classes */
1305 { end_class_declaration (1); }
1306 | interface_declaration /* Added, JDK1.1 inner interfaces */
1307 { end_class_declaration (1); }
1310 constant_declaration:
1311 field_declaration
1314 abstract_method_declaration:
1315 method_header SC_TK
1317 check_abstract_method_header ($1);
1318 current_function_decl = NULL_TREE; /* FIXME ? */
1320 | method_header error
1321 {yyerror ("';' expected"); RECOVER;}
1324 /* 19.10 Productions from 10: Arrays */
1325 array_initializer:
1326 OCB_TK CCB_TK
1327 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1328 | OCB_TK variable_initializers CCB_TK
1329 { $$ = build_new_array_init ($1.location, $2); }
1330 | OCB_TK variable_initializers C_TK CCB_TK
1331 { $$ = build_new_array_init ($1.location, $2); }
1334 variable_initializers:
1335 variable_initializer
1337 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1338 $1, NULL_TREE);
1340 | variable_initializers C_TK variable_initializer
1342 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1344 | variable_initializers C_TK error
1345 {yyerror ("Missing term"); RECOVER;}
1348 /* 19.11 Production from 14: Blocks and Statements */
1349 block:
1350 OCB_TK CCB_TK
1352 /* Store the location of the `}' when doing xrefs */
1353 if (current_function_decl && flag_emit_xref)
1354 DECL_END_SOURCE_LINE (current_function_decl) =
1355 EXPR_WFL_ADD_COL ($2.location, 1);
1356 $$ = empty_stmt_node;
1358 | block_begin block_statements block_end
1359 { $$ = $3; }
1362 block_begin:
1363 OCB_TK
1364 { enter_block (); }
1367 block_end:
1368 CCB_TK
1370 maybe_absorb_scoping_blocks ();
1371 /* Store the location of the `}' when doing xrefs */
1372 if (current_function_decl && flag_emit_xref)
1373 DECL_END_SOURCE_LINE (current_function_decl) =
1374 EXPR_WFL_ADD_COL ($1.location, 1);
1375 $$ = exit_block ();
1376 if (!BLOCK_SUBBLOCKS ($$))
1377 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1381 block_statements:
1382 block_statement
1383 | block_statements block_statement
1386 block_statement:
1387 local_variable_declaration_statement
1388 | statement
1389 { java_method_add_stmt (current_function_decl, $1); }
1390 | class_declaration /* Added, JDK1.1 local classes */
1392 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1393 end_class_declaration (1);
1397 local_variable_declaration_statement:
1398 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1401 local_variable_declaration:
1402 type variable_declarators
1403 { declare_local_variables (0, $1, $2); }
1404 | final type variable_declarators /* Added, JDK1.1 final locals */
1405 { declare_local_variables ($1, $2, $3); }
1408 statement:
1409 statement_without_trailing_substatement
1410 | labeled_statement
1411 | if_then_statement
1412 | if_then_else_statement
1413 | while_statement
1414 | for_statement
1415 { $$ = exit_block (); }
1418 statement_nsi:
1419 statement_without_trailing_substatement
1420 | labeled_statement_nsi
1421 | if_then_else_statement_nsi
1422 | while_statement_nsi
1423 | for_statement_nsi
1424 { $$ = exit_block (); }
1427 statement_without_trailing_substatement:
1428 block
1429 | empty_statement
1430 | expression_statement
1431 | switch_statement
1432 | do_statement
1433 | break_statement
1434 | continue_statement
1435 | return_statement
1436 | synchronized_statement
1437 | throw_statement
1438 | try_statement
1441 empty_statement:
1442 SC_TK
1444 if (flag_extraneous_semicolon)
1446 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1447 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1449 $$ = empty_stmt_node;
1453 label_decl:
1454 identifier REL_CL_TK
1456 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1457 EXPR_WFL_NODE ($1));
1458 pushlevel (2);
1459 push_labeled_block ($$);
1460 PUSH_LABELED_BLOCK ($$);
1464 labeled_statement:
1465 label_decl statement
1466 { $$ = finish_labeled_statement ($1, $2); }
1467 | identifier error
1468 {yyerror ("':' expected"); RECOVER;}
1471 labeled_statement_nsi:
1472 label_decl statement_nsi
1473 { $$ = finish_labeled_statement ($1, $2); }
1476 /* We concentrate here a bunch of error handling rules that we couldn't write
1477 earlier, because expression_statement catches a missing ';'. */
1478 expression_statement:
1479 statement_expression SC_TK
1481 /* We have a statement. Generate a WFL around it so
1482 we can debug it */
1483 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1484 /* We know we have a statement, so set the debug
1485 info to be eventually generate here. */
1486 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1488 | error SC_TK
1490 YYNOT_TWICE yyerror ("Invalid expression statement");
1491 DRECOVER (expr_stmt);
1493 | error OCB_TK
1495 YYNOT_TWICE yyerror ("Invalid expression statement");
1496 DRECOVER (expr_stmt);
1498 | error CCB_TK
1500 YYNOT_TWICE yyerror ("Invalid expression statement");
1501 DRECOVER (expr_stmt);
1503 | this_or_super OP_TK error
1504 {yyerror ("')' expected"); RECOVER;}
1505 | this_or_super OP_TK CP_TK error
1507 parse_ctor_invocation_error ();
1508 RECOVER;
1510 | this_or_super OP_TK argument_list error
1511 {yyerror ("')' expected"); RECOVER;}
1512 | this_or_super OP_TK argument_list CP_TK error
1514 parse_ctor_invocation_error ();
1515 RECOVER;
1517 | name DOT_TK SUPER_TK error
1518 {yyerror ("'(' expected"); RECOVER;}
1519 | name DOT_TK SUPER_TK OP_TK error
1520 {yyerror ("')' expected"); RECOVER;}
1521 | name DOT_TK SUPER_TK OP_TK argument_list error
1522 {yyerror ("')' expected"); RECOVER;}
1523 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1524 {yyerror ("';' expected"); RECOVER;}
1525 | name DOT_TK SUPER_TK OP_TK CP_TK error
1526 {yyerror ("';' expected"); RECOVER;}
1529 statement_expression:
1530 assignment
1531 | pre_increment_expression
1532 | pre_decrement_expression
1533 | post_increment_expression
1534 | post_decrement_expression
1535 | method_invocation
1536 | class_instance_creation_expression
1539 if_then_statement:
1540 IF_TK OP_TK expression CP_TK statement
1542 $$ = build_if_else_statement ($2.location, $3,
1543 $5, NULL_TREE);
1545 | IF_TK error
1546 {yyerror ("'(' expected"); RECOVER;}
1547 | IF_TK OP_TK error
1548 {yyerror ("Missing term"); RECOVER;}
1549 | IF_TK OP_TK expression error
1550 {yyerror ("')' expected"); RECOVER;}
1553 if_then_else_statement:
1554 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1555 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1558 if_then_else_statement_nsi:
1559 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1560 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1563 switch_statement:
1564 switch_expression
1566 enter_block ();
1568 switch_block
1570 /* Make into "proper list" of COMPOUND_EXPRs.
1571 I.e. make the last statment also have its own
1572 COMPOUND_EXPR. */
1573 maybe_absorb_scoping_blocks ();
1574 TREE_OPERAND ($1, 1) = exit_block ();
1575 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1579 switch_expression:
1580 SWITCH_TK OP_TK expression CP_TK
1582 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1583 EXPR_WFL_LINECOL ($$) = $2.location;
1585 | SWITCH_TK error
1586 {yyerror ("'(' expected"); RECOVER;}
1587 | SWITCH_TK OP_TK error
1588 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1589 | SWITCH_TK OP_TK expression CP_TK error
1590 {yyerror ("'{' expected"); RECOVER;}
1593 /* Default assignment is there to avoid type node on switch_block
1594 node. */
1596 switch_block:
1597 OCB_TK CCB_TK
1598 { $$ = NULL_TREE; }
1599 | OCB_TK switch_labels CCB_TK
1600 { $$ = NULL_TREE; }
1601 | OCB_TK switch_block_statement_groups CCB_TK
1602 { $$ = NULL_TREE; }
1603 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1604 { $$ = NULL_TREE; }
1607 switch_block_statement_groups:
1608 switch_block_statement_group
1609 | switch_block_statement_groups switch_block_statement_group
1612 switch_block_statement_group:
1613 switch_labels block_statements
1616 switch_labels:
1617 switch_label
1618 | switch_labels switch_label
1621 switch_label:
1622 CASE_TK constant_expression REL_CL_TK
1624 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1625 EXPR_WFL_LINECOL (lab) = $1.location;
1626 java_method_add_stmt (current_function_decl, lab);
1628 | DEFAULT_TK REL_CL_TK
1630 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1631 EXPR_WFL_LINECOL (lab) = $1.location;
1632 java_method_add_stmt (current_function_decl, lab);
1634 | CASE_TK error
1635 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1636 | CASE_TK constant_expression error
1637 {yyerror ("':' expected"); RECOVER;}
1638 | DEFAULT_TK error
1639 {yyerror ("':' expected"); RECOVER;}
1642 while_expression:
1643 WHILE_TK OP_TK expression CP_TK
1645 tree body = build_loop_body ($2.location, $3, 0);
1646 $$ = build_new_loop (body);
1650 while_statement:
1651 while_expression statement
1652 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1653 | WHILE_TK error
1654 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1655 | WHILE_TK OP_TK error
1656 {yyerror ("Missing term and ')' expected"); RECOVER;}
1657 | WHILE_TK OP_TK expression error
1658 {yyerror ("')' expected"); RECOVER;}
1661 while_statement_nsi:
1662 while_expression statement_nsi
1663 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1666 do_statement_begin:
1667 DO_TK
1669 tree body = build_loop_body (0, NULL_TREE, 1);
1670 $$ = build_new_loop (body);
1672 /* Need error handing here. FIXME */
1675 do_statement:
1676 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1677 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1680 for_statement:
1681 for_begin SC_TK expression SC_TK for_update CP_TK statement
1683 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1684 $3 = build_wfl_node ($3);
1685 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1687 | for_begin SC_TK SC_TK for_update CP_TK statement
1689 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1690 /* We have not condition, so we get rid of the EXIT_EXPR */
1691 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1692 empty_stmt_node;
1694 | for_begin SC_TK error
1695 {yyerror ("Invalid control expression"); RECOVER;}
1696 | for_begin SC_TK expression SC_TK error
1697 {yyerror ("Invalid update expression"); RECOVER;}
1698 | for_begin SC_TK SC_TK error
1699 {yyerror ("Invalid update expression"); RECOVER;}
1702 for_statement_nsi:
1703 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1704 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1705 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1707 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1708 /* We have not condition, so we get rid of the EXIT_EXPR */
1709 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1710 empty_stmt_node;
1714 for_header:
1715 FOR_TK OP_TK
1717 /* This scope defined for local variable that may be
1718 defined within the scope of the for loop */
1719 enter_block ();
1721 | FOR_TK error
1722 {yyerror ("'(' expected"); DRECOVER(for_1);}
1723 | FOR_TK OP_TK error
1724 {yyerror ("Invalid init statement"); RECOVER;}
1727 for_begin:
1728 for_header for_init
1730 /* We now declare the loop body. The loop is
1731 declared as a for loop. */
1732 tree body = build_loop_body (0, NULL_TREE, 0);
1733 $$ = build_new_loop (body);
1734 FOR_LOOP_P ($$) = 1;
1735 /* The loop is added to the current block the for
1736 statement is defined within */
1737 java_method_add_stmt (current_function_decl, $$);
1740 for_init: /* Can be empty */
1741 { $$ = empty_stmt_node; }
1742 | statement_expression_list
1744 /* Init statement recorded within the previously
1745 defined block scope */
1746 $$ = java_method_add_stmt (current_function_decl, $1);
1748 | local_variable_declaration
1750 /* Local variable are recorded within the previously
1751 defined block scope */
1752 $$ = NULL_TREE;
1754 | statement_expression_list error
1755 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1758 for_update: /* Can be empty */
1759 {$$ = empty_stmt_node;}
1760 | statement_expression_list
1761 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1764 statement_expression_list:
1765 statement_expression
1766 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1767 | statement_expression_list C_TK statement_expression
1768 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1769 | statement_expression_list C_TK error
1770 {yyerror ("Missing term"); RECOVER;}
1773 break_statement:
1774 BREAK_TK SC_TK
1775 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1776 | BREAK_TK identifier SC_TK
1777 { $$ = build_bc_statement ($1.location, 1, $2); }
1778 | BREAK_TK error
1779 {yyerror ("Missing term"); RECOVER;}
1780 | BREAK_TK identifier error
1781 {yyerror ("';' expected"); RECOVER;}
1784 continue_statement:
1785 CONTINUE_TK SC_TK
1786 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1787 | CONTINUE_TK identifier SC_TK
1788 { $$ = build_bc_statement ($1.location, 0, $2); }
1789 | CONTINUE_TK error
1790 {yyerror ("Missing term"); RECOVER;}
1791 | CONTINUE_TK identifier error
1792 {yyerror ("';' expected"); RECOVER;}
1795 return_statement:
1796 RETURN_TK SC_TK
1797 { $$ = build_return ($1.location, NULL_TREE); }
1798 | RETURN_TK expression SC_TK
1799 { $$ = build_return ($1.location, $2); }
1800 | RETURN_TK error
1801 {yyerror ("Missing term"); RECOVER;}
1802 | RETURN_TK expression error
1803 {yyerror ("';' expected"); RECOVER;}
1806 throw_statement:
1807 THROW_TK expression SC_TK
1809 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1810 EXPR_WFL_LINECOL ($$) = $1.location;
1812 | THROW_TK error
1813 {yyerror ("Missing term"); RECOVER;}
1814 | THROW_TK expression error
1815 {yyerror ("';' expected"); RECOVER;}
1818 synchronized_statement:
1819 synchronized OP_TK expression CP_TK block
1821 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1822 EXPR_WFL_LINECOL ($$) =
1823 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1825 | synchronized OP_TK expression CP_TK error
1826 {yyerror ("'{' expected"); RECOVER;}
1827 | synchronized error
1828 {yyerror ("'(' expected"); RECOVER;}
1829 | synchronized OP_TK error CP_TK
1830 {yyerror ("Missing term"); RECOVER;}
1831 | synchronized OP_TK error
1832 {yyerror ("Missing term"); RECOVER;}
1835 synchronized:
1836 modifiers
1838 check_modifiers (
1839 "Illegal modifier `%s'. Only `synchronized' was expected here",
1840 $1, ACC_SYNCHRONIZED);
1841 if ($1 != ACC_SYNCHRONIZED)
1842 MODIFIER_WFL (SYNCHRONIZED_TK) =
1843 build_wfl_node (NULL_TREE);
1847 try_statement:
1848 TRY_TK block catches
1849 { $$ = build_try_statement ($1.location, $2, $3); }
1850 | TRY_TK block finally
1851 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1852 | TRY_TK block catches finally
1853 { $$ = build_try_finally_statement
1854 ($1.location, build_try_statement ($1.location,
1855 $2, $3), $4);
1857 | TRY_TK error
1858 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1861 catches:
1862 catch_clause
1863 | catches catch_clause
1865 TREE_CHAIN ($2) = $1;
1866 $$ = $2;
1870 catch_clause:
1871 catch_clause_parameter block
1873 java_method_add_stmt (current_function_decl, $2);
1874 exit_block ();
1875 $$ = $1;
1878 catch_clause_parameter:
1879 CATCH_TK OP_TK formal_parameter CP_TK
1881 /* We add a block to define a scope for
1882 formal_parameter (CCBP). The formal parameter is
1883 declared initialized by the appropriate function
1884 call */
1885 tree ccpb = enter_block ();
1886 tree init = build_assignment (ASSIGN_TK, $2.location,
1887 TREE_PURPOSE ($3),
1888 soft_exceptioninfo_call_node);
1889 declare_local_variables (0, TREE_VALUE ($3),
1890 build_tree_list (TREE_PURPOSE ($3),
1891 init));
1892 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1893 EXPR_WFL_LINECOL ($$) = $1.location;
1895 | CATCH_TK error
1896 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1897 | CATCH_TK OP_TK error
1899 yyerror ("Missing term or ')' expected");
1900 RECOVER; $$ = NULL_TREE;
1902 | CATCH_TK OP_TK error CP_TK /* That's for () */
1903 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1906 finally:
1907 FINALLY_TK block
1908 { $$ = $2; }
1909 | FINALLY_TK error
1910 {yyerror ("'{' expected"); RECOVER; }
1913 /* 19.12 Production from 15: Expressions */
1914 primary:
1915 primary_no_new_array
1916 | array_creation_expression
1919 primary_no_new_array:
1920 literal
1921 | THIS_TK
1922 { $$ = build_this ($1.location); }
1923 | OP_TK expression CP_TK
1924 {$$ = $2;}
1925 | class_instance_creation_expression
1926 | field_access
1927 | method_invocation
1928 | array_access
1929 | type_literals
1930 /* Added, JDK1.1 inner classes. Documentation is wrong
1931 refering to a 'ClassName' (class_name) rule that doesn't
1932 exist. Used name: instead. */
1933 | name DOT_TK THIS_TK
1935 tree wfl = build_wfl_node (this_identifier_node);
1936 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1938 | OP_TK expression error
1939 {yyerror ("')' expected"); RECOVER;}
1940 | name DOT_TK error
1941 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1942 | primitive_type DOT_TK error
1943 {yyerror ("'class' expected" ); RECOVER;}
1944 | VOID_TK DOT_TK error
1945 {yyerror ("'class' expected" ); RECOVER;}
1948 type_literals:
1949 name DOT_TK CLASS_TK
1950 { $$ = build_incomplete_class_ref ($2.location, $1); }
1951 | array_type DOT_TK CLASS_TK
1952 { $$ = build_incomplete_class_ref ($2.location, $1); }
1953 | primitive_type DOT_TK CLASS_TK
1954 { $$ = build_class_ref ($1); }
1955 | VOID_TK DOT_TK CLASS_TK
1956 { $$ = build_class_ref (void_type_node); }
1959 class_instance_creation_expression:
1960 NEW_TK class_type OP_TK argument_list CP_TK
1961 { $$ = build_new_invocation ($2, $4); }
1962 | NEW_TK class_type OP_TK CP_TK
1963 { $$ = build_new_invocation ($2, NULL_TREE); }
1964 | anonymous_class_creation
1965 /* Added, JDK1.1 inner classes, modified to use name or
1966 primary instead of primary solely which couldn't work in
1967 all situations. */
1968 | something_dot_new identifier OP_TK CP_TK
1970 tree ctor = build_new_invocation ($2, NULL_TREE);
1971 $$ = make_qualified_primary ($1, ctor,
1972 EXPR_WFL_LINECOL ($1));
1974 | something_dot_new identifier OP_TK CP_TK class_body
1975 | something_dot_new identifier OP_TK argument_list CP_TK
1977 tree ctor = build_new_invocation ($2, $4);
1978 $$ = make_qualified_primary ($1, ctor,
1979 EXPR_WFL_LINECOL ($1));
1981 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1982 | NEW_TK error SC_TK
1983 {yyerror ("'(' expected"); DRECOVER(new_1);}
1984 | NEW_TK class_type error
1985 {yyerror ("'(' expected"); RECOVER;}
1986 | NEW_TK class_type OP_TK error
1987 {yyerror ("')' or term expected"); RECOVER;}
1988 | NEW_TK class_type OP_TK argument_list error
1989 {yyerror ("')' expected"); RECOVER;}
1990 | something_dot_new error
1991 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1992 | something_dot_new identifier error
1993 {yyerror ("'(' expected"); RECOVER;}
1996 /* Created after JDK1.1 rules originally added to
1997 class_instance_creation_expression, but modified to use
1998 'class_type' instead of 'TypeName' (type_name) which is mentionned
1999 in the documentation but doesn't exist. */
2001 anonymous_class_creation:
2002 NEW_TK class_type OP_TK argument_list CP_TK
2003 { create_anonymous_class ($1.location, $2); }
2004 class_body
2006 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2007 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2009 end_class_declaration (1);
2011 /* Now we can craft the new expression */
2012 $$ = build_new_invocation (id, $4);
2014 /* Note that we can't possibly be here if
2015 `class_type' is an interface (in which case the
2016 anonymous class extends Object and implements
2017 `class_type', hence its constructor can't have
2018 arguments.) */
2020 /* Otherwise, the innerclass must feature a
2021 constructor matching `argument_list'. Anonymous
2022 classes are a bit special: it's impossible to
2023 define constructor for them, hence constructors
2024 must be generated following the hints provided by
2025 the `new' expression. Whether a super constructor
2026 of that nature exists or not is to be verified
2027 later on in verify_constructor_super.
2029 It's during the expansion of a `new' statement
2030 refering to an anonymous class that a ctor will
2031 be generated for the anonymous class, with the
2032 right arguments. */
2035 | NEW_TK class_type OP_TK CP_TK
2036 { create_anonymous_class ($1.location, $2); }
2037 class_body
2039 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2040 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2042 end_class_declaration (1);
2044 /* Now we can craft the new expression. The
2045 statement doesn't need to be remember so that a
2046 constructor can be generated, since its signature
2047 is already known. */
2048 $$ = build_new_invocation (id, NULL_TREE);
2052 something_dot_new: /* Added, not part of the specs. */
2053 name DOT_TK NEW_TK
2054 { $$ = $1; }
2055 | primary DOT_TK NEW_TK
2056 { $$ = $1; }
2059 argument_list:
2060 expression
2062 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2063 ctxp->formal_parameter_number = 1;
2065 | argument_list C_TK expression
2067 ctxp->formal_parameter_number += 1;
2068 $$ = tree_cons (NULL_TREE, $3, $1);
2070 | argument_list C_TK error
2071 {yyerror ("Missing term"); RECOVER;}
2074 array_creation_expression:
2075 NEW_TK primitive_type dim_exprs
2076 { $$ = build_newarray_node ($2, $3, 0); }
2077 | NEW_TK class_or_interface_type dim_exprs
2078 { $$ = build_newarray_node ($2, $3, 0); }
2079 | NEW_TK primitive_type dim_exprs dims
2080 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2081 | NEW_TK class_or_interface_type dim_exprs dims
2082 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2083 /* Added, JDK1.1 anonymous array. Initial documentation rule
2084 modified */
2085 | NEW_TK class_or_interface_type dims array_initializer
2087 char *sig;
2088 int osb = pop_current_osb (ctxp);
2089 while (osb--)
2090 obstack_1grow (&temporary_obstack, '[');
2091 sig = obstack_finish (&temporary_obstack);
2092 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2093 $2, get_identifier (sig), $4);
2095 | NEW_TK primitive_type dims array_initializer
2097 int osb = pop_current_osb (ctxp);
2098 tree type = $2;
2099 while (osb--)
2100 type = build_java_array_type (type, -1);
2101 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2102 build_pointer_type (type), NULL_TREE, $4);
2104 | NEW_TK error CSB_TK
2105 {yyerror ("'[' expected"); DRECOVER ("]");}
2106 | NEW_TK error OSB_TK
2107 {yyerror ("']' expected"); RECOVER;}
2110 dim_exprs:
2111 dim_expr
2112 { $$ = build_tree_list (NULL_TREE, $1); }
2113 | dim_exprs dim_expr
2114 { $$ = tree_cons (NULL_TREE, $2, $$); }
2117 dim_expr:
2118 OSB_TK expression CSB_TK
2120 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2122 $2 = build_wfl_node ($2);
2123 TREE_TYPE ($2) = NULL_TREE;
2125 EXPR_WFL_LINECOL ($2) = $1.location;
2126 $$ = $2;
2128 | OSB_TK expression error
2129 {yyerror ("']' expected"); RECOVER;}
2130 | OSB_TK error
2132 yyerror ("Missing term");
2133 yyerror ("']' expected");
2134 RECOVER;
2138 dims:
2139 OSB_TK CSB_TK
2141 int allocate = 0;
2142 /* If not initialized, allocate memory for the osb
2143 numbers stack */
2144 if (!ctxp->osb_limit)
2146 allocate = ctxp->osb_limit = 32;
2147 ctxp->osb_depth = -1;
2149 /* If capacity overflown, reallocate a bigger chunk */
2150 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2151 allocate = ctxp->osb_limit << 1;
2153 if (allocate)
2155 allocate *= sizeof (int);
2156 if (ctxp->osb_number)
2157 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2158 allocate);
2159 else
2160 ctxp->osb_number = (int *)xmalloc (allocate);
2162 ctxp->osb_depth++;
2163 CURRENT_OSB (ctxp) = 1;
2165 | dims OSB_TK CSB_TK
2166 { CURRENT_OSB (ctxp)++; }
2167 | dims OSB_TK error
2168 { yyerror ("']' expected"); RECOVER;}
2171 field_access:
2172 primary DOT_TK identifier
2173 { $$ = make_qualified_primary ($1, $3, $2.location); }
2174 /* FIXME - REWRITE TO:
2175 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2176 | SUPER_TK DOT_TK identifier
2178 tree super_wfl = build_wfl_node (super_identifier_node);
2179 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2180 $$ = make_qualified_name (super_wfl, $3, $2.location);
2182 | SUPER_TK error
2183 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2186 method_invocation:
2187 name OP_TK CP_TK
2188 { $$ = build_method_invocation ($1, NULL_TREE); }
2189 | name OP_TK argument_list CP_TK
2190 { $$ = build_method_invocation ($1, $3); }
2191 | primary DOT_TK identifier OP_TK CP_TK
2193 if (TREE_CODE ($1) == THIS_EXPR)
2194 $$ = build_this_super_qualified_invocation
2195 (1, $3, NULL_TREE, 0, $2.location);
2196 else
2198 tree invok = build_method_invocation ($3, NULL_TREE);
2199 $$ = make_qualified_primary ($1, invok, $2.location);
2202 | primary DOT_TK identifier OP_TK argument_list CP_TK
2204 if (TREE_CODE ($1) == THIS_EXPR)
2205 $$ = build_this_super_qualified_invocation
2206 (1, $3, $5, 0, $2.location);
2207 else
2209 tree invok = build_method_invocation ($3, $5);
2210 $$ = make_qualified_primary ($1, invok, $2.location);
2213 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2215 $$ = build_this_super_qualified_invocation
2216 (0, $3, NULL_TREE, $1.location, $2.location);
2218 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2220 $$ = build_this_super_qualified_invocation
2221 (0, $3, $5, $1.location, $2.location);
2223 /* Screws up thing. I let it here until I'm convinced it can
2224 be removed. FIXME
2225 | primary DOT_TK error
2226 {yyerror ("'(' expected"); DRECOVER(bad);} */
2227 | SUPER_TK DOT_TK error CP_TK
2228 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2229 | SUPER_TK DOT_TK error DOT_TK
2230 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2233 array_access:
2234 name OSB_TK expression CSB_TK
2235 { $$ = build_array_ref ($2.location, $1, $3); }
2236 | primary_no_new_array OSB_TK expression CSB_TK
2237 { $$ = build_array_ref ($2.location, $1, $3); }
2238 | name OSB_TK error
2240 yyerror ("Missing term and ']' expected");
2241 DRECOVER(array_access);
2243 | name OSB_TK expression error
2245 yyerror ("']' expected");
2246 DRECOVER(array_access);
2248 | primary_no_new_array OSB_TK error
2250 yyerror ("Missing term and ']' expected");
2251 DRECOVER(array_access);
2253 | primary_no_new_array OSB_TK expression error
2255 yyerror ("']' expected");
2256 DRECOVER(array_access);
2260 postfix_expression:
2261 primary
2262 | name
2263 | post_increment_expression
2264 | post_decrement_expression
2267 post_increment_expression:
2268 postfix_expression INCR_TK
2269 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2272 post_decrement_expression:
2273 postfix_expression DECR_TK
2274 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2277 unary_expression:
2278 pre_increment_expression
2279 | pre_decrement_expression
2280 | PLUS_TK unary_expression
2281 {$$ = build_unaryop ($1.token, $1.location, $2); }
2282 | MINUS_TK unary_expression
2283 {$$ = build_unaryop ($1.token, $1.location, $2); }
2284 | unary_expression_not_plus_minus
2285 | PLUS_TK error
2286 {yyerror ("Missing term"); RECOVER}
2287 | MINUS_TK error
2288 {yyerror ("Missing term"); RECOVER}
2291 pre_increment_expression:
2292 INCR_TK unary_expression
2293 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2294 | INCR_TK error
2295 {yyerror ("Missing term"); RECOVER}
2298 pre_decrement_expression:
2299 DECR_TK unary_expression
2300 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2301 | DECR_TK error
2302 {yyerror ("Missing term"); RECOVER}
2305 unary_expression_not_plus_minus:
2306 postfix_expression
2307 | NOT_TK unary_expression
2308 {$$ = build_unaryop ($1.token, $1.location, $2); }
2309 | NEG_TK unary_expression
2310 {$$ = build_unaryop ($1.token, $1.location, $2); }
2311 | cast_expression
2312 | NOT_TK error
2313 {yyerror ("Missing term"); RECOVER}
2314 | NEG_TK error
2315 {yyerror ("Missing term"); RECOVER}
2318 cast_expression: /* Error handling here is potentially weak */
2319 OP_TK primitive_type dims CP_TK unary_expression
2321 tree type = $2;
2322 int osb = pop_current_osb (ctxp);
2323 while (osb--)
2324 type = build_java_array_type (type, -1);
2325 $$ = build_cast ($1.location, type, $5);
2327 | OP_TK primitive_type CP_TK unary_expression
2328 { $$ = build_cast ($1.location, $2, $4); }
2329 | OP_TK expression CP_TK unary_expression_not_plus_minus
2330 { $$ = build_cast ($1.location, $2, $4); }
2331 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2333 const char *ptr;
2334 int osb = pop_current_osb (ctxp);
2335 while (osb--)
2336 obstack_1grow (&temporary_obstack, '[');
2337 obstack_grow0 (&temporary_obstack,
2338 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2339 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2340 ptr = obstack_finish (&temporary_obstack);
2341 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2342 $$ = build_cast ($1.location, $2, $5);
2344 | OP_TK primitive_type OSB_TK error
2345 {yyerror ("']' expected, invalid type expression");}
2346 | OP_TK error
2348 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2349 RECOVER;
2351 | OP_TK primitive_type dims CP_TK error
2352 {yyerror ("Missing term"); RECOVER;}
2353 | OP_TK primitive_type CP_TK error
2354 {yyerror ("Missing term"); RECOVER;}
2355 | OP_TK name dims CP_TK error
2356 {yyerror ("Missing term"); RECOVER;}
2359 multiplicative_expression:
2360 unary_expression
2361 | multiplicative_expression MULT_TK unary_expression
2363 $$ = build_binop (BINOP_LOOKUP ($2.token),
2364 $2.location, $1, $3);
2366 | multiplicative_expression DIV_TK unary_expression
2368 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2369 $1, $3);
2371 | multiplicative_expression REM_TK unary_expression
2373 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2374 $1, $3);
2376 | multiplicative_expression MULT_TK error
2377 {yyerror ("Missing term"); RECOVER;}
2378 | multiplicative_expression DIV_TK error
2379 {yyerror ("Missing term"); RECOVER;}
2380 | multiplicative_expression REM_TK error
2381 {yyerror ("Missing term"); RECOVER;}
2384 additive_expression:
2385 multiplicative_expression
2386 | additive_expression PLUS_TK multiplicative_expression
2388 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2389 $1, $3);
2391 | additive_expression MINUS_TK multiplicative_expression
2393 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2394 $1, $3);
2396 | additive_expression PLUS_TK error
2397 {yyerror ("Missing term"); RECOVER;}
2398 | additive_expression MINUS_TK error
2399 {yyerror ("Missing term"); RECOVER;}
2402 shift_expression:
2403 additive_expression
2404 | shift_expression LS_TK additive_expression
2406 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2407 $1, $3);
2409 | shift_expression SRS_TK additive_expression
2411 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2412 $1, $3);
2414 | shift_expression ZRS_TK additive_expression
2416 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2417 $1, $3);
2419 | shift_expression LS_TK error
2420 {yyerror ("Missing term"); RECOVER;}
2421 | shift_expression SRS_TK error
2422 {yyerror ("Missing term"); RECOVER;}
2423 | shift_expression ZRS_TK error
2424 {yyerror ("Missing term"); RECOVER;}
2427 relational_expression:
2428 shift_expression
2429 | relational_expression LT_TK shift_expression
2431 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2432 $1, $3);
2434 | relational_expression GT_TK shift_expression
2436 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2437 $1, $3);
2439 | relational_expression LTE_TK shift_expression
2441 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2442 $1, $3);
2444 | relational_expression GTE_TK shift_expression
2446 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2447 $1, $3);
2449 | relational_expression INSTANCEOF_TK reference_type
2450 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2451 | relational_expression LT_TK error
2452 {yyerror ("Missing term"); RECOVER;}
2453 | relational_expression GT_TK error
2454 {yyerror ("Missing term"); RECOVER;}
2455 | relational_expression LTE_TK error
2456 {yyerror ("Missing term"); RECOVER;}
2457 | relational_expression GTE_TK error
2458 {yyerror ("Missing term"); RECOVER;}
2459 | relational_expression INSTANCEOF_TK error
2460 {yyerror ("Invalid reference type"); RECOVER;}
2463 equality_expression:
2464 relational_expression
2465 | equality_expression EQ_TK relational_expression
2467 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2468 $1, $3);
2470 | equality_expression NEQ_TK relational_expression
2472 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2473 $1, $3);
2475 | equality_expression EQ_TK error
2476 {yyerror ("Missing term"); RECOVER;}
2477 | equality_expression NEQ_TK error
2478 {yyerror ("Missing term"); RECOVER;}
2481 and_expression:
2482 equality_expression
2483 | and_expression AND_TK equality_expression
2485 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2486 $1, $3);
2488 | and_expression AND_TK error
2489 {yyerror ("Missing term"); RECOVER;}
2492 exclusive_or_expression:
2493 and_expression
2494 | exclusive_or_expression XOR_TK and_expression
2496 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2497 $1, $3);
2499 | exclusive_or_expression XOR_TK error
2500 {yyerror ("Missing term"); RECOVER;}
2503 inclusive_or_expression:
2504 exclusive_or_expression
2505 | inclusive_or_expression OR_TK exclusive_or_expression
2507 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2508 $1, $3);
2510 | inclusive_or_expression OR_TK error
2511 {yyerror ("Missing term"); RECOVER;}
2514 conditional_and_expression:
2515 inclusive_or_expression
2516 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2518 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2519 $1, $3);
2521 | conditional_and_expression BOOL_AND_TK error
2522 {yyerror ("Missing term"); RECOVER;}
2525 conditional_or_expression:
2526 conditional_and_expression
2527 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2529 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2530 $1, $3);
2532 | conditional_or_expression BOOL_OR_TK error
2533 {yyerror ("Missing term"); RECOVER;}
2536 conditional_expression: /* Error handling here is weak */
2537 conditional_or_expression
2538 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2540 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2541 EXPR_WFL_LINECOL ($$) = $2.location;
2543 | conditional_or_expression REL_QM_TK REL_CL_TK error
2545 YYERROR_NOW;
2546 yyerror ("Missing term");
2547 DRECOVER (1);
2549 | conditional_or_expression REL_QM_TK error
2550 {yyerror ("Missing term"); DRECOVER (2);}
2551 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2552 {yyerror ("Missing term"); DRECOVER (3);}
2555 assignment_expression:
2556 conditional_expression
2557 | assignment
2560 assignment:
2561 left_hand_side assignment_operator assignment_expression
2562 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2563 | left_hand_side assignment_operator error
2565 YYNOT_TWICE yyerror ("Missing term");
2566 DRECOVER (assign);
2570 left_hand_side:
2571 name
2572 | field_access
2573 | array_access
2576 assignment_operator:
2577 ASSIGN_ANY_TK
2578 | ASSIGN_TK
2581 expression:
2582 assignment_expression
2585 constant_expression:
2586 expression
2591 /* Helper function to retrieve an OSB count. Should be used when the
2592 `dims:' rule is being used. */
2594 static int
2595 pop_current_osb (ctxp)
2596 struct parser_ctxt *ctxp;
2598 int to_return;
2600 if (ctxp->osb_depth < 0)
2601 fatal ("osb stack underflow");
2603 to_return = CURRENT_OSB (ctxp);
2604 ctxp->osb_depth--;
2606 return to_return;
2611 /* This section of the code deal with save/restoring parser contexts.
2612 Add mode documentation here. FIXME */
2614 /* Helper function. Create a new parser context. With
2615 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2616 context is copied, otherwise, the new context is zeroed. The newly
2617 created context becomes the current one. */
2619 static void
2620 create_new_parser_context (copy_from_previous)
2621 int copy_from_previous;
2623 struct parser_ctxt *new;
2625 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2626 if (copy_from_previous)
2628 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2629 new->saved_data_ctx = 1;
2631 else
2632 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2634 new->next = ctxp;
2635 ctxp = new;
2638 /* Create a new parser context and make it the current one. */
2640 void
2641 java_push_parser_context ()
2643 create_new_parser_context (0);
2644 if (ctxp->next)
2646 ctxp->incomplete_class = ctxp->next->incomplete_class;
2647 ctxp->gclass_list = ctxp->next->gclass_list;
2651 void
2652 java_pop_parser_context (generate)
2653 int generate;
2655 tree current;
2656 struct parser_ctxt *toFree, *next;
2658 if (!ctxp)
2659 return;
2661 toFree = ctxp;
2662 next = ctxp->next;
2663 if (next)
2665 next->incomplete_class = ctxp->incomplete_class;
2666 next->gclass_list = ctxp->gclass_list;
2667 lineno = ctxp->lineno;
2668 current_class = ctxp->class_type;
2671 /* If the old and new lexers differ, then free the old one. */
2672 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2673 java_destroy_lexer (ctxp->lexer);
2675 /* Set the single import class file flag to 0 for the current list
2676 of imported things */
2677 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2678 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2680 /* And restore those of the previous context */
2681 if ((ctxp = next)) /* Assignment is really meant here */
2682 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2683 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2685 /* If we pushed a context to parse a class intended to be generated,
2686 we keep it so we can remember the class. What we could actually
2687 do is to just update a list of class names. */
2688 if (generate)
2690 toFree->next = ctxp_for_generation;
2691 ctxp_for_generation = toFree;
2693 else
2694 free (toFree);
2697 /* Create a parser context for the use of saving some global
2698 variables. */
2700 void
2701 java_parser_context_save_global ()
2703 if (!ctxp)
2705 java_push_parser_context ();
2706 ctxp->saved_data_ctx = 1;
2709 /* If this context already stores data, create a new one suitable
2710 for data storage. */
2711 else if (ctxp->saved_data)
2712 create_new_parser_context (1);
2714 ctxp->lineno = lineno;
2715 ctxp->class_type = current_class;
2716 ctxp->filename = input_filename;
2717 ctxp->function_decl = current_function_decl;
2718 ctxp->saved_data = 1;
2721 /* Restore some global variables from the previous context. Make the
2722 previous context the current one. */
2724 void
2725 java_parser_context_restore_global ()
2727 lineno = ctxp->lineno;
2728 current_class = ctxp->class_type;
2729 input_filename = ctxp->filename;
2730 if (wfl_operator)
2732 tree s;
2733 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2734 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2736 current_function_decl = ctxp->function_decl;
2737 ctxp->saved_data = 0;
2738 if (ctxp->saved_data_ctx)
2739 java_pop_parser_context (0);
2742 /* Suspend vital data for the current class/function being parsed so
2743 that an other class can be parsed. Used to let local/anonymous
2744 classes be parsed. */
2746 static void
2747 java_parser_context_suspend ()
2749 /* This makes debugging through java_debug_context easier */
2750 static const char *name = "<inner buffer context>";
2752 /* Duplicate the previous context, use it to save the globals we're
2753 interested in */
2754 create_new_parser_context (1);
2755 ctxp->function_decl = current_function_decl;
2756 ctxp->class_type = current_class;
2758 /* Then create a new context which inherits all data from the
2759 previous one. This will be the new current context */
2760 create_new_parser_context (1);
2762 /* Help debugging */
2763 ctxp->next->filename = name;
2766 /* Resume vital data for the current class/function being parsed so
2767 that an other class can be parsed. Used to let local/anonymous
2768 classes be parsed. The trick is the data storing file position
2769 informations must be restored to their current value, so parsing
2770 can resume as if no context was ever saved. */
2772 static void
2773 java_parser_context_resume ()
2775 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2776 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2777 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2779 /* We need to inherit the list of classes to complete/generate */
2780 restored->incomplete_class = old->incomplete_class;
2781 restored->gclass_list = old->gclass_list;
2782 restored->classd_list = old->classd_list;
2783 restored->class_list = old->class_list;
2785 /* Restore the current class and function from the saver */
2786 current_class = saver->class_type;
2787 current_function_decl = saver->function_decl;
2789 /* Retrive the restored context */
2790 ctxp = restored;
2792 /* Re-installed the data for the parsing to carry on */
2793 bcopy (&old->marker_begining, &ctxp->marker_begining,
2794 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2796 /* Buffer context can now be discarded */
2797 free (saver);
2798 free (old);
2801 /* Add a new anchor node to which all statement(s) initializing static
2802 and non static initialized upon declaration field(s) will be
2803 linked. */
2805 static void
2806 java_parser_context_push_initialized_field ()
2808 tree node;
2810 node = build_tree_list (NULL_TREE, NULL_TREE);
2811 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2812 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2814 node = build_tree_list (NULL_TREE, NULL_TREE);
2815 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2816 CPC_INITIALIZER_LIST (ctxp) = node;
2818 node = build_tree_list (NULL_TREE, NULL_TREE);
2819 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2820 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2823 /* Pop the lists of initialized field. If this lists aren't empty,
2824 remember them so we can use it to create and populate the finit$
2825 or <clinit> functions. */
2827 static void
2828 java_parser_context_pop_initialized_field ()
2830 tree stmts;
2831 tree class_type = TREE_TYPE (GET_CPC ());
2833 if (CPC_INITIALIZER_LIST (ctxp))
2835 stmts = CPC_INITIALIZER_STMT (ctxp);
2836 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2837 if (stmts && !java_error_count)
2838 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2841 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2843 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2844 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2845 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2846 /* Keep initialization in order to enforce 8.5 */
2847 if (stmts && !java_error_count)
2848 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2851 /* JDK 1.1 instance initializers */
2852 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2854 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2855 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2856 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2857 if (stmts && !java_error_count)
2858 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2862 static tree
2863 reorder_static_initialized (list)
2864 tree list;
2866 /* We have to keep things in order. The alias initializer have to
2867 come first, then the initialized regular field, in reverse to
2868 keep them in lexical order. */
2869 tree marker, previous = NULL_TREE;
2870 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2871 if (TREE_CODE (marker) == TREE_LIST
2872 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2873 break;
2875 /* No static initialized, the list is fine as is */
2876 if (!previous)
2877 list = TREE_CHAIN (marker);
2879 /* No marker? reverse the whole list */
2880 else if (!marker)
2881 list = nreverse (list);
2883 /* Otherwise, reverse what's after the marker and the new reordered
2884 sublist will replace the marker. */
2885 else
2887 TREE_CHAIN (previous) = NULL_TREE;
2888 list = nreverse (list);
2889 list = chainon (TREE_CHAIN (marker), list);
2891 return list;
2894 /* Helper functions to dump the parser context stack. */
2896 #define TAB_CONTEXT(C) \
2897 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2899 static void
2900 java_debug_context_do (tab)
2901 int tab;
2903 struct parser_ctxt *copy = ctxp;
2904 while (copy)
2906 TAB_CONTEXT (tab);
2907 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2908 TAB_CONTEXT (tab);
2909 fprintf (stderr, "filename: %s\n", copy->filename);
2910 TAB_CONTEXT (tab);
2911 fprintf (stderr, "lineno: %d\n", copy->lineno);
2912 TAB_CONTEXT (tab);
2913 fprintf (stderr, "package: %s\n",
2914 (copy->package ?
2915 IDENTIFIER_POINTER (copy->package) : "<none>"));
2916 TAB_CONTEXT (tab);
2917 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2918 TAB_CONTEXT (tab);
2919 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2920 copy = copy->next;
2921 tab += 2;
2925 /* Dump the stacked up parser contexts. Intended to be called from a
2926 debugger. */
2928 void
2929 java_debug_context ()
2931 java_debug_context_do (0);
2936 /* Flag for the error report routine to issue the error the first time
2937 it's called (overriding the default behavior which is to drop the
2938 first invocation and honor the second one, taking advantage of a
2939 richer context. */
2940 static int force_error = 0;
2942 /* Reporting an constructor invocation error. */
2943 static void
2944 parse_ctor_invocation_error ()
2946 if (DECL_CONSTRUCTOR_P (current_function_decl))
2947 yyerror ("Constructor invocation must be first thing in a constructor");
2948 else
2949 yyerror ("Only constructors can invoke constructors");
2952 /* Reporting JDK1.1 features not implemented. */
2954 static tree
2955 parse_jdk1_1_error (msg)
2956 const char *msg;
2958 sorry (": `%s' JDK1.1(TM) feature", msg);
2959 java_error_count++;
2960 return empty_stmt_node;
2963 static int do_warning = 0;
2965 void
2966 yyerror (msg)
2967 const char *msg;
2969 static java_lc elc;
2970 static int prev_lineno;
2971 static const char *prev_msg;
2973 int save_lineno;
2974 char *remainder, *code_from_source;
2976 if (!force_error && prev_lineno == lineno)
2977 return;
2979 /* Save current error location but report latter, when the context is
2980 richer. */
2981 if (ctxp->java_error_flag == 0)
2983 ctxp->java_error_flag = 1;
2984 elc = ctxp->elc;
2985 /* Do something to use the previous line if we're reaching the
2986 end of the file... */
2987 #ifdef VERBOSE_SKELETON
2988 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2989 #endif
2990 return;
2993 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2994 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2995 return;
2997 ctxp->java_error_flag = 0;
2998 if (do_warning)
2999 java_warning_count++;
3000 else
3001 java_error_count++;
3003 if (elc.col == 0 && msg && msg[1] == ';')
3005 elc.col = ctxp->p_line->char_col-1;
3006 elc.line = ctxp->p_line->lineno;
3009 save_lineno = lineno;
3010 prev_lineno = lineno = elc.line;
3011 prev_msg = msg;
3013 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3014 obstack_grow0 (&temporary_obstack,
3015 code_from_source, strlen (code_from_source));
3016 remainder = obstack_finish (&temporary_obstack);
3017 if (do_warning)
3018 warning ("%s.\n%s", msg, remainder);
3019 else
3020 error ("%s.\n%s", msg, remainder);
3022 /* This allow us to cheaply avoid an extra 'Invalid expression
3023 statement' error report when errors have been already reported on
3024 the same line. This occurs when we report an error but don't have
3025 a synchronization point other than ';', which
3026 expression_statement is the only one to take care of. */
3027 ctxp->prevent_ese = lineno = save_lineno;
3030 static void
3031 issue_warning_error_from_context (cl, msg, ap)
3032 tree cl;
3033 const char *msg;
3034 va_list ap;
3036 const char *saved, *saved_input_filename;
3037 char buffer [4096];
3038 vsprintf (buffer, msg, ap);
3039 force_error = 1;
3041 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3042 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3043 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3045 /* We have a CL, that's a good reason for using it if it contains data */
3046 saved = ctxp->filename;
3047 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3048 ctxp->filename = EXPR_WFL_FILENAME (cl);
3049 saved_input_filename = input_filename;
3050 input_filename = ctxp->filename;
3051 java_error (NULL);
3052 java_error (buffer);
3053 ctxp->filename = saved;
3054 input_filename = saved_input_filename;
3055 force_error = 0;
3058 /* Issue an error message at a current source line CL */
3060 void
3061 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3063 #ifndef ANSI_PROTOTYPES
3064 tree cl;
3065 const char *msg;
3066 #endif
3067 va_list ap;
3069 VA_START (ap, msg);
3070 #ifndef ANSI_PROTOTYPES
3071 cl = va_arg (ap, tree);
3072 msg = va_arg (ap, const char *);
3073 #endif
3074 issue_warning_error_from_context (cl, msg, ap);
3075 va_end (ap);
3078 /* Issue a warning at a current source line CL */
3080 static void
3081 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3083 #ifndef ANSI_PROTOTYPES
3084 tree cl;
3085 const char *msg;
3086 #endif
3087 va_list ap;
3089 VA_START (ap, msg);
3090 #ifndef ANSI_PROTOTYPES
3091 cl = va_arg (ap, tree);
3092 msg = va_arg (ap, const char *);
3093 #endif
3095 force_error = do_warning = 1;
3096 issue_warning_error_from_context (cl, msg, ap);
3097 do_warning = force_error = 0;
3098 va_end (ap);
3101 static tree
3102 find_expr_with_wfl (node)
3103 tree node;
3105 while (node)
3107 char code;
3108 tree to_return;
3110 switch (TREE_CODE (node))
3112 case BLOCK:
3113 node = BLOCK_EXPR_BODY (node);
3114 continue;
3116 case COMPOUND_EXPR:
3117 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3118 if (to_return)
3119 return to_return;
3120 node = TREE_OPERAND (node, 1);
3121 continue;
3123 case LOOP_EXPR:
3124 node = TREE_OPERAND (node, 0);
3125 continue;
3127 case LABELED_BLOCK_EXPR:
3128 node = TREE_OPERAND (node, 1);
3129 continue;
3131 default:
3132 code = TREE_CODE_CLASS (TREE_CODE (node));
3133 if (((code == '1') || (code == '2') || (code == 'e'))
3134 && EXPR_WFL_LINECOL (node))
3135 return node;
3136 return NULL_TREE;
3139 return NULL_TREE;
3142 /* Issue a missing return statement error. Uses METHOD to figure the
3143 last line of the method the error occurs in. */
3145 static void
3146 missing_return_error (method)
3147 tree method;
3149 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3150 parse_error_context (wfl_operator, "Missing return statement");
3153 /* Issue an unreachable statement error. From NODE, find the next
3154 statement to report appropriately. */
3155 static void
3156 unreachable_stmt_error (node)
3157 tree node;
3159 /* Browse node to find the next expression node that has a WFL. Use
3160 the location to report the error */
3161 if (TREE_CODE (node) == COMPOUND_EXPR)
3162 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3163 else
3164 node = find_expr_with_wfl (node);
3166 if (node)
3168 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3169 parse_error_context (wfl_operator, "Unreachable statement");
3171 else
3172 fatal ("Can't get valid statement - unreachable_stmt_error");
3176 java_report_errors ()
3178 if (java_error_count)
3179 fprintf (stderr, "%d error%s",
3180 java_error_count, (java_error_count == 1 ? "" : "s"));
3181 if (java_warning_count)
3182 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3183 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3184 if (java_error_count || java_warning_count)
3185 putc ('\n', stderr);
3186 return java_error_count;
3189 static char *
3190 java_accstring_lookup (flags)
3191 int flags;
3193 static char buffer [80];
3194 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3196 /* Access modifier looked-up first for easier report on forbidden
3197 access. */
3198 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3199 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3200 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3201 if (flags & ACC_STATIC) COPY_RETURN ("static");
3202 if (flags & ACC_FINAL) COPY_RETURN ("final");
3203 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3204 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3205 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3206 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3207 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3208 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3210 buffer [0] = '\0';
3211 return buffer;
3212 #undef COPY_RETURN
3215 /* Issuing error messages upon redefinition of classes, interfaces or
3216 variables. */
3218 static void
3219 classitf_redefinition_error (context, id, decl, cl)
3220 const char *context;
3221 tree id, decl, cl;
3223 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3224 context, IDENTIFIER_POINTER (id),
3225 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3226 /* Here we should point out where its redefined. It's a unicode. FIXME */
3229 static void
3230 variable_redefinition_error (context, name, type, line)
3231 tree context, name, type;
3232 int line;
3234 const char *type_name;
3236 /* Figure a proper name for type. We might haven't resolved it */
3237 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3238 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3239 else
3240 type_name = lang_printable_name (type, 0);
3242 parse_error_context (context,
3243 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3244 IDENTIFIER_POINTER (name),
3245 type_name, IDENTIFIER_POINTER (name), line);
3248 static tree
3249 build_array_from_name (type, type_wfl, name, ret_name)
3250 tree type, type_wfl, name, *ret_name;
3252 int more_dims = 0;
3253 const char *string;
3255 /* Eventually get more dims */
3256 string = IDENTIFIER_POINTER (name);
3257 while (string [more_dims] == '[')
3258 more_dims++;
3260 /* If we have, then craft a new type for this variable */
3261 if (more_dims)
3263 name = get_identifier (&string [more_dims]);
3265 /* If we have a pointer, use its type */
3266 if (TREE_CODE (type) == POINTER_TYPE)
3267 type = TREE_TYPE (type);
3269 /* Building the first dimension of a primitive type uses this
3270 function */
3271 if (JPRIMITIVE_TYPE_P (type))
3273 type = build_java_array_type (type, -1);
3274 CLASS_LOADED_P (type) = 1;
3275 more_dims--;
3277 /* Otherwise, if we have a WFL for this type, use it (the type
3278 is already an array on an unresolved type, and we just keep
3279 on adding dimensions) */
3280 else if (type_wfl)
3281 type = type_wfl;
3283 /* Add all the dimensions */
3284 while (more_dims--)
3285 type = build_unresolved_array_type (type);
3287 /* The type may have been incomplete in the first place */
3288 if (type_wfl)
3289 type = obtain_incomplete_type (type);
3292 if (ret_name)
3293 *ret_name = name;
3294 return type;
3297 /* Build something that the type identifier resolver will identify as
3298 being an array to an unresolved type. TYPE_WFL is a WFL on a
3299 identifier. */
3301 static tree
3302 build_unresolved_array_type (type_or_wfl)
3303 tree type_or_wfl;
3305 const char *ptr;
3307 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3308 just create a array type */
3309 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3311 tree type = build_java_array_type (type_or_wfl, -1);
3312 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3313 return type;
3316 obstack_1grow (&temporary_obstack, '[');
3317 obstack_grow0 (&temporary_obstack,
3318 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3319 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3320 ptr = obstack_finish (&temporary_obstack);
3321 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3322 return type_or_wfl;
3325 static void
3326 parser_add_interface (class_decl, interface_decl, wfl)
3327 tree class_decl, interface_decl, wfl;
3329 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3330 parse_error_context (wfl, "Interface `%s' repeated",
3331 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3334 /* Bulk of common class/interface checks. Return 1 if an error was
3335 encountered. TAG is 0 for a class, 1 for an interface. */
3337 static int
3338 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3339 int is_interface, flags;
3340 tree raw_name, qualified_name, decl, cl;
3342 tree node;
3343 int sca = 0; /* Static class allowed */
3344 int icaf = 0; /* Inner class allowed flags */
3345 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3347 if (!quiet_flag)
3348 fprintf (stderr, " %s%s %s",
3349 (CPC_INNER_P () ? "inner" : ""),
3350 (is_interface ? "interface" : "class"),
3351 IDENTIFIER_POINTER (qualified_name));
3353 /* Scope of an interface/class type name:
3354 - Can't be imported by a single type import
3355 - Can't already exists in the package */
3356 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3357 && (node = find_name_in_single_imports (raw_name))
3358 && !CPC_INNER_P ())
3360 parse_error_context
3361 (cl, "%s name `%s' clashes with imported type `%s'",
3362 (is_interface ? "Interface" : "Class"),
3363 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3364 return 1;
3366 if (decl && CLASS_COMPLETE_P (decl))
3368 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3369 qualified_name, decl, cl);
3370 return 1;
3373 if (check_inner_class_redefinition (raw_name, cl))
3374 return 1;
3376 /* If public, file name should match class/interface name, except
3377 when dealing with an inner class */
3378 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3380 const char *f;
3382 /* Contains OS dependent assumption on path separator. FIXME */
3383 for (f = &input_filename [strlen (input_filename)];
3384 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3385 f--)
3387 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3388 f++;
3389 if (strncmp (IDENTIFIER_POINTER (raw_name),
3390 f , IDENTIFIER_LENGTH (raw_name)) ||
3391 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3392 parse_error_context
3393 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3394 (is_interface ? "interface" : "class"),
3395 IDENTIFIER_POINTER (qualified_name),
3396 IDENTIFIER_POINTER (raw_name));
3399 /* Static classes can be declared only in top level classes. Note:
3400 once static, a inner class is a top level class. */
3401 if (flags & ACC_STATIC)
3403 /* Catch the specific error of declaring an class inner class
3404 with no toplevel enclosing class. Prevent check_modifiers from
3405 complaining a second time */
3406 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3408 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3409 IDENTIFIER_POINTER (qualified_name));
3410 sca = ACC_STATIC;
3412 /* Else, in the context of a top-level class declaration, let
3413 `check_modifiers' do its job, otherwise, give it a go */
3414 else
3415 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3418 /* Inner classes can be declared private or protected
3419 within their enclosing classes. */
3420 if (CPC_INNER_P ())
3422 /* A class which is local to a block can't be public, private,
3423 protected or static. But it is created final, so allow this
3424 one. */
3425 if (current_function_decl)
3426 icaf = sca = uaaf = ACC_FINAL;
3427 else
3429 check_modifiers_consistency (flags);
3430 icaf = ACC_PRIVATE|ACC_PROTECTED;
3434 if (is_interface)
3436 if (CPC_INNER_P ())
3437 uaaf = INTERFACE_INNER_MODIFIERS;
3438 else
3439 uaaf = INTERFACE_MODIFIERS;
3441 check_modifiers ("Illegal modifier `%s' for interface declaration",
3442 flags, uaaf);
3444 else
3445 check_modifiers ((current_function_decl ?
3446 "Illegal modifier `%s' for local class declaration" :
3447 "Illegal modifier `%s' for class declaration"),
3448 flags, uaaf|sca|icaf);
3449 return 0;
3452 static void
3453 make_nested_class_name (cpc_list)
3454 tree cpc_list;
3456 tree name;
3458 if (!cpc_list)
3459 return;
3460 else
3461 make_nested_class_name (TREE_CHAIN (cpc_list));
3463 /* Pick the qualified name when dealing with the first upmost
3464 enclosing class */
3465 name = (TREE_CHAIN (cpc_list) ?
3466 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3467 obstack_grow (&temporary_obstack,
3468 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3469 /* Why is NO_DOLLAR_IN_LABEL defined? */
3470 #if 0
3471 #ifdef NO_DOLLAR_IN_LABEL
3472 fatal ("make_nested_class_name: Can't use '$' as a separator "
3473 "for inner classes");
3474 #endif
3475 #endif
3476 obstack_1grow (&temporary_obstack, '$');
3479 /* Can't redefine a class already defined in an earlier scope. */
3481 static int
3482 check_inner_class_redefinition (raw_name, cl)
3483 tree raw_name, cl;
3485 tree scope_list;
3487 for (scope_list = GET_CPC_LIST (); scope_list;
3488 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3489 if (raw_name == GET_CPC_UN_NODE (scope_list))
3491 parse_error_context
3492 (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",
3493 IDENTIFIER_POINTER (raw_name));
3494 return 1;
3496 return 0;
3499 static tree
3500 find_as_inner_class (enclosing, name, cl)
3501 tree enclosing, name, cl;
3503 tree qual, to_return;
3504 if (!enclosing)
3505 return NULL_TREE;
3507 name = TYPE_NAME (name);
3509 /* First search: within the scope of `enclosing', search for name */
3510 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3511 qual = EXPR_WFL_QUALIFICATION (cl);
3512 else if (cl)
3513 qual = build_tree_list (cl, NULL_TREE);
3514 else
3515 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3517 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3518 return to_return;
3520 /* We're dealing with a qualified name. Try to resolve thing until
3521 we get something that is an enclosing class. */
3522 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3524 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3526 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3527 qual = TREE_CHAIN (qual))
3529 acc = merge_qualified_name (acc,
3530 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3531 BUILD_PTR_FROM_NAME (ptr, acc);
3532 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3535 /* A NULL qual and a decl means that the search ended
3536 successfully?!? We have to do something then. FIXME */
3538 if (decl)
3539 enclosing = decl;
3540 else
3541 qual = EXPR_WFL_QUALIFICATION (cl);
3543 /* Otherwise, create a qual for the other part of the resolution. */
3544 else
3545 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3547 return find_as_inner_class_do (qual, enclosing);
3550 /* We go inside the list of sub classes and try to find a way
3551 through. */
3553 static tree
3554 find_as_inner_class_do (qual, enclosing)
3555 tree qual, enclosing;
3557 if (!qual)
3558 return NULL_TREE;
3560 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3562 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3563 tree next_enclosing = NULL_TREE;
3564 tree inner_list;
3566 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3567 inner_list; inner_list = TREE_CHAIN (inner_list))
3569 if (TREE_VALUE (inner_list) == name_to_match)
3571 next_enclosing = TREE_PURPOSE (inner_list);
3572 break;
3575 enclosing = next_enclosing;
3578 return (!qual && enclosing ? enclosing : NULL_TREE);
3581 /* Reach all inner classes and tie their unqualified name to a
3582 DECL. */
3584 static void
3585 set_nested_class_simple_name_value (outer, set)
3586 tree outer;
3587 int set;
3589 tree l;
3591 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3592 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3593 TREE_PURPOSE (l) : NULL_TREE);
3596 static void
3597 link_nested_class_to_enclosing ()
3599 if (GET_ENCLOSING_CPC ())
3601 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3602 DECL_INNER_CLASS_LIST (enclosing) =
3603 tree_cons (GET_CPC (), GET_CPC_UN (),
3604 DECL_INNER_CLASS_LIST (enclosing));
3605 enclosing = enclosing;
3609 static tree
3610 maybe_make_nested_class_name (name)
3611 tree name;
3613 tree id = NULL_TREE;
3615 if (CPC_INNER_P ())
3617 make_nested_class_name (GET_CPC_LIST ());
3618 obstack_grow0 (&temporary_obstack,
3619 IDENTIFIER_POINTER (name),
3620 IDENTIFIER_LENGTH (name));
3621 id = get_identifier (obstack_finish (&temporary_obstack));
3622 if (ctxp->package)
3623 QUALIFIED_P (id) = 1;
3625 return id;
3628 /* If DECL is NULL, create and push a new DECL, record the current
3629 line CL and do other maintenance things. */
3631 static tree
3632 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3633 tree decl, raw_name, qualified_name, cl;
3635 if (!decl)
3636 decl = push_class (make_class (), qualified_name);
3638 /* Take care of the file and line business */
3639 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3640 /* If we're emiting xrefs, store the line/col number information */
3641 if (flag_emit_xref)
3642 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3643 else
3644 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3645 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3646 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3647 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3649 PUSH_CPC (decl, raw_name);
3650 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3652 /* Link the declaration to the already seen ones */
3653 TREE_CHAIN (decl) = ctxp->class_list;
3654 ctxp->class_list = decl;
3656 /* Create a new nodes in the global lists */
3657 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
3658 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3660 /* Install a new dependency list element */
3661 create_jdep_list (ctxp);
3663 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3664 IDENTIFIER_POINTER (qualified_name)));
3665 return decl;
3668 static void
3669 add_superinterfaces (decl, interface_list)
3670 tree decl, interface_list;
3672 tree node;
3673 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3674 takes care of ensuring that:
3675 - This is an accessible interface type,
3676 - Circularity detection.
3677 parser_add_interface is then called. If present but not defined,
3678 the check operation is delayed until the super interface gets
3679 defined. */
3680 for (node = interface_list; node; node = TREE_CHAIN (node))
3682 tree current = TREE_PURPOSE (node);
3683 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3684 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3686 if (!parser_check_super_interface (idecl, decl, current))
3687 parser_add_interface (decl, idecl, current);
3689 else
3690 register_incomplete_type (JDEP_INTERFACE,
3691 current, decl, NULL_TREE);
3695 /* Create an interface in pass1 and return its decl. Return the
3696 interface's decl in pass 2. */
3698 static tree
3699 create_interface (flags, id, super)
3700 int flags;
3701 tree id, super;
3703 tree raw_name = EXPR_WFL_NODE (id);
3704 tree q_name = parser_qualified_classname (raw_name);
3705 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3707 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3709 /* Basic checks: scope, redefinition, modifiers */
3710 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3712 PUSH_ERROR ();
3713 return NULL_TREE;
3716 /* Suspend the current parsing context if we're parsing an inner
3717 interface */
3718 if (CPC_INNER_P ())
3719 java_parser_context_suspend ();
3721 /* Push a new context for (static) initialized upon declaration fields */
3722 java_parser_context_push_initialized_field ();
3724 /* Interface modifiers check
3725 - public/abstract allowed (already done at that point)
3726 - abstract is obsolete (comes first, it's a warning, or should be)
3727 - Can't use twice the same (checked in the modifier rule) */
3728 if ((flags & ACC_ABSTRACT) && flag_redundant)
3729 parse_warning_context
3730 (MODIFIER_WFL (ABSTRACT_TK),
3731 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3733 /* Create a new decl if DECL is NULL, otherwise fix it */
3734 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3736 /* Set super info and mark the class a complete */
3737 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3738 object_type_node, ctxp->interface_number);
3739 ctxp->interface_number = 0;
3740 CLASS_COMPLETE_P (decl) = 1;
3741 add_superinterfaces (decl, super);
3743 return decl;
3746 /* Anonymous class counter. Will be reset to 1 every time a non
3747 anonymous class gets created. */
3748 static int anonymous_class_counter = 1;
3750 /* Patch anonymous class CLASS, by either extending or implementing
3751 DEP. */
3753 static void
3754 patch_anonymous_class (type_decl, class_decl, wfl)
3755 tree type_decl, class_decl, wfl;
3757 tree class = TREE_TYPE (class_decl);
3758 tree type = TREE_TYPE (type_decl);
3759 tree binfo = TYPE_BINFO (class);
3761 /* If it's an interface, implement it */
3762 if (CLASS_INTERFACE (type_decl))
3764 tree s_binfo;
3765 int length;
3767 if (parser_check_super_interface (type_decl, class_decl, wfl))
3768 return;
3770 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3771 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3772 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3773 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3774 /* And add the interface */
3775 parser_add_interface (class_decl, type_decl, wfl);
3777 /* Otherwise, it's a type we want to extend */
3778 else
3780 if (parser_check_super (type_decl, class_decl, wfl))
3781 return;
3782 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3786 static tree
3787 create_anonymous_class (location, type_name)
3788 int location;
3789 tree type_name;
3791 char buffer [80];
3792 tree super = NULL_TREE, itf = NULL_TREE;
3793 tree id, type_decl, class;
3795 /* The unqualified name of the anonymous class. It's just a number. */
3796 sprintf (buffer, "%d", anonymous_class_counter++);
3797 id = build_wfl_node (get_identifier (buffer));
3798 EXPR_WFL_LINECOL (id) = location;
3800 /* We know about the type to extend/implement. We go ahead */
3801 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3803 /* Create a class which either implements on extends the designated
3804 class. The class bears an innacessible name. */
3805 if (CLASS_INTERFACE (type_decl))
3807 /* It's OK to modify it here. It's been already used and
3808 shouldn't be reused */
3809 ctxp->interface_number = 1;
3810 /* Interfaces should presented as a list of WFLs */
3811 itf = build_tree_list (type_name, NULL_TREE);
3813 else
3814 super = type_name;
3817 class = create_class (ACC_FINAL, id, super, itf);
3819 /* We didn't know anything about the stuff. We register a dependence. */
3820 if (!type_decl)
3821 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3823 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3824 return class;
3827 /* Create a class in pass1 and return its decl. Return class
3828 interface's decl in pass 2. */
3830 static tree
3831 create_class (flags, id, super, interfaces)
3832 int flags;
3833 tree id, super, interfaces;
3835 tree raw_name = EXPR_WFL_NODE (id);
3836 tree class_id, decl;
3837 tree super_decl_type;
3839 class_id = parser_qualified_classname (raw_name);
3840 decl = IDENTIFIER_CLASS_VALUE (class_id);
3841 EXPR_WFL_NODE (id) = class_id;
3843 /* Basic check: scope, redefinition, modifiers */
3844 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3846 PUSH_ERROR ();
3847 return NULL_TREE;
3850 /* Suspend the current parsing context if we're parsing an inner
3851 class or an anonymous class. */
3852 if (CPC_INNER_P ())
3853 java_parser_context_suspend ();
3854 /* Push a new context for (static) initialized upon declaration fields */
3855 java_parser_context_push_initialized_field ();
3857 /* Class modifier check:
3858 - Allowed modifier (already done at that point)
3859 - abstract AND final forbidden
3860 - Public classes defined in the correct file */
3861 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3862 parse_error_context
3863 (id, "Class `%s' can't be declared both abstract and final",
3864 IDENTIFIER_POINTER (raw_name));
3866 /* Create a new decl if DECL is NULL, otherwise fix it */
3867 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3869 /* If SUPER exists, use it, otherwise use Object */
3870 if (super)
3872 /* Can't extend java.lang.Object */
3873 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3875 parse_error_context (id, "Can't extend `java.lang.Object'");
3876 return NULL_TREE;
3879 super_decl_type =
3880 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3882 else if (TREE_TYPE (decl) != object_type_node)
3883 super_decl_type = object_type_node;
3884 /* We're defining java.lang.Object */
3885 else
3886 super_decl_type = NULL_TREE;
3888 /* A class nested in an interface is implicitly static. */
3889 if (INNER_CLASS_DECL_P (decl)
3890 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3892 flags |= ACC_STATIC;
3895 /* Set super info and mark the class as complete. */
3896 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3897 ctxp->interface_number);
3898 ctxp->interface_number = 0;
3899 CLASS_COMPLETE_P (decl) = 1;
3900 add_superinterfaces (decl, interfaces);
3902 /* Add the private this$<n> field, Replicate final locals still in
3903 scope as private final fields mangled like val$<local_name>.
3904 This doesn't not occur for top level (static) inner classes. */
3905 if (PURE_INNER_CLASS_DECL_P (decl))
3906 add_inner_class_fields (decl, current_function_decl);
3908 /* If doing xref, store the location at which the inherited class
3909 (if any) was seen. */
3910 if (flag_emit_xref && super)
3911 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3913 /* Eventually sets the @deprecated tag flag */
3914 CHECK_DEPRECATED (decl);
3916 /* Reset the anonymous class counter when declaring non inner classes */
3917 if (!INNER_CLASS_DECL_P (decl))
3918 anonymous_class_counter = 1;
3920 return decl;
3923 /* End a class declaration: register the statements used to create
3924 finit$ and <clinit>, pop the current class and resume the prior
3925 parser context if necessary. */
3927 static void
3928 end_class_declaration (resume)
3929 int resume;
3931 /* If an error occured, context weren't pushed and won't need to be
3932 popped by a resume. */
3933 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3935 java_parser_context_pop_initialized_field ();
3936 POP_CPC ();
3937 if (resume && no_error_occured)
3938 java_parser_context_resume ();
3940 /* We're ending a class declaration, this is a good time to reset
3941 the interface cout. Note that might have been already done in
3942 create_interface, but if at that time an inner class was being
3943 dealt with, the interface count was reset in a context created
3944 for the sake of handling inner classes declaration. */
3945 ctxp->interface_number = 0;
3948 static void
3949 add_inner_class_fields (class_decl, fct_decl)
3950 tree class_decl;
3951 tree fct_decl;
3953 tree block, marker, f;
3955 f = add_field (TREE_TYPE (class_decl),
3956 build_current_thisn (TREE_TYPE (class_decl)),
3957 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3958 ACC_PRIVATE);
3959 FIELD_THISN (f) = 1;
3961 if (!fct_decl)
3962 return;
3964 for (block = GET_CURRENT_BLOCK (fct_decl);
3965 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3967 tree decl;
3968 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3970 tree name, pname;
3971 tree wfl, init, list;
3973 /* Avoid non final arguments. */
3974 if (!LOCAL_FINAL_P (decl))
3975 continue;
3977 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3978 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3979 wfl = build_wfl_node (name);
3980 init = build_wfl_node (pname);
3981 /* Build an initialization for the field: it will be
3982 initialized by a parameter added to finit$, bearing a
3983 mangled name of the field itself (param$<n>.) The
3984 parameter is provided to finit$ by the constructor
3985 invoking it (hence the constructor will also feature a
3986 hidden parameter, set to the value of the outer context
3987 local at the time the inner class is created.)
3989 Note: we take into account all possible locals that can
3990 be accessed by the inner class. It's actually not trivial
3991 to minimize these aliases down to the ones really
3992 used. One way to do that would be to expand all regular
3993 methods first, then finit$ to get a picture of what's
3994 used. It works with the exception that we would have to
3995 go back on all constructor invoked in regular methods to
3996 have their invokation reworked (to include the right amount
3997 of alias initializer parameters.)
3999 The only real way around, I think, is a first pass to
4000 identify locals really used in the inner class. We leave
4001 the flag FIELD_LOCAL_ALIAS_USED around for that future
4002 use.
4004 On the other hand, it only affect local inner classes,
4005 whose constructors (and finit$ call) will be featuring
4006 unecessary arguments. It's easy for a developper to keep
4007 this number of parameter down by using the `final'
4008 keyword only when necessary. For the time being, we can
4009 issue a warning on unecessary finals. FIXME */
4010 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4011 wfl, init);
4013 /* Register the field. The TREE_LIST holding the part
4014 initialized/initializer will be marked ARG_FINAL_P so
4015 that the created field can be marked
4016 FIELD_LOCAL_ALIAS. */
4017 list = build_tree_list (wfl, init);
4018 ARG_FINAL_P (list) = 1;
4019 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4023 if (!CPC_INITIALIZER_STMT (ctxp))
4024 return;
4026 /* If we ever registered an alias field, insert and marker to
4027 remeber where the list ends. The second part of the list (the one
4028 featuring initialized fields) so it can be later reversed to
4029 enforce 8.5. The marker will be removed during that operation. */
4030 marker = build_tree_list (NULL_TREE, NULL_TREE);
4031 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4032 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4035 /* Can't use lookup_field () since we don't want to load the class and
4036 can't set the CLASS_LOADED_P flag */
4038 static tree
4039 find_field (class, name)
4040 tree class;
4041 tree name;
4043 tree decl;
4044 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4046 if (DECL_NAME (decl) == name)
4047 return decl;
4049 return NULL_TREE;
4052 /* Wrap around lookup_field that doesn't potentially upset the value
4053 of CLASS */
4055 static tree
4056 lookup_field_wrapper (class, name)
4057 tree class, name;
4059 tree type = class;
4060 tree decl = NULL_TREE;
4061 java_parser_context_save_global ();
4063 /* Last chance: if we're within the context of an inner class, we
4064 might be trying to access a local variable defined in an outer
4065 context. We try to look for it now. */
4066 if (INNER_CLASS_TYPE_P (class))
4068 tree new_name;
4069 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4070 decl = lookup_field (&type, new_name);
4071 if (decl && decl != error_mark_node)
4072 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4074 if (!decl || decl == error_mark_node)
4076 type = class;
4077 decl = lookup_field (&type, name);
4080 java_parser_context_restore_global ();
4081 return decl == error_mark_node ? NULL : decl;
4084 /* Find duplicate field within the same class declarations and report
4085 the error. Returns 1 if a duplicated field was found, 0
4086 otherwise. */
4088 static int
4089 duplicate_declaration_error_p (new_field_name, new_type, cl)
4090 tree new_field_name, new_type, cl;
4092 /* This might be modified to work with method decl as well */
4093 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4094 if (decl)
4096 char *t1 = xstrdup (purify_type_name
4097 ((TREE_CODE (new_type) == POINTER_TYPE
4098 && TREE_TYPE (new_type) == NULL_TREE) ?
4099 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4100 lang_printable_name (new_type, 1)));
4101 /* The type may not have been completed by the time we report
4102 the error */
4103 char *t2 = xstrdup (purify_type_name
4104 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4105 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4106 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4107 lang_printable_name (TREE_TYPE (decl), 1)));
4108 parse_error_context
4109 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4110 t1, IDENTIFIER_POINTER (new_field_name),
4111 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4112 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4113 free (t1);
4114 free (t2);
4115 return 1;
4117 return 0;
4120 /* Field registration routine. If TYPE doesn't exist, field
4121 declarations are linked to the undefined TYPE dependency list, to
4122 be later resolved in java_complete_class () */
4124 static void
4125 register_fields (flags, type, variable_list)
4126 int flags;
4127 tree type, variable_list;
4129 tree current, saved_type;
4130 tree class_type = NULL_TREE;
4131 int saved_lineno = lineno;
4132 int must_chain = 0;
4133 tree wfl = NULL_TREE;
4135 if (GET_CPC ())
4136 class_type = TREE_TYPE (GET_CPC ());
4138 if (!class_type || class_type == error_mark_node)
4139 return;
4141 /* If we're adding fields to interfaces, those fields are public,
4142 static, final */
4143 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4145 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4146 flags, ACC_PUBLIC, "interface field(s)");
4147 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4148 flags, ACC_STATIC, "interface field(s)");
4149 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4150 flags, ACC_FINAL, "interface field(s)");
4151 check_modifiers ("Illegal interface member modifier `%s'", flags,
4152 INTERFACE_FIELD_MODIFIERS);
4153 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4156 /* Obtain a suitable type for resolution, if necessary */
4157 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4159 /* If TYPE is fully resolved and we don't have a reference, make one */
4160 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4162 for (current = variable_list, saved_type = type; current;
4163 current = TREE_CHAIN (current), type = saved_type)
4165 tree real_type;
4166 tree field_decl;
4167 tree cl = TREE_PURPOSE (current);
4168 tree init = TREE_VALUE (current);
4169 tree current_name = EXPR_WFL_NODE (cl);
4171 /* Can't declare non-final static fields in inner classes */
4172 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4173 && !(flags & ACC_FINAL))
4174 parse_error_context
4175 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4176 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4177 lang_printable_name (class_type, 0));
4179 /* Process NAME, as it may specify extra dimension(s) for it */
4180 type = build_array_from_name (type, wfl, current_name, &current_name);
4182 /* Type adjustment. We may have just readjusted TYPE because
4183 the variable specified more dimensions. Make sure we have
4184 a reference if we can and don't have one already. Also
4185 change the name if we have an init. */
4186 if (type != saved_type)
4188 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4189 if (init)
4190 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4193 real_type = GET_REAL_TYPE (type);
4194 /* Check for redeclarations */
4195 if (duplicate_declaration_error_p (current_name, real_type, cl))
4196 continue;
4198 /* Set lineno to the line the field was found and create a
4199 declaration for it. Eventually sets the @deprecated tag flag. */
4200 if (flag_emit_xref)
4201 lineno = EXPR_WFL_LINECOL (cl);
4202 else
4203 lineno = EXPR_WFL_LINENO (cl);
4204 field_decl = add_field (class_type, current_name, real_type, flags);
4205 CHECK_DEPRECATED (field_decl);
4207 /* If the field denotes a final instance variable, then we
4208 allocate a LANG_DECL_SPECIFIC part to keep track of its
4209 initialization. We also mark whether the field was
4210 initialized upon it's declaration. We don't do that if the
4211 created field is an alias to a final local. */
4212 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4214 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4215 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4216 if ((flags & ACC_STATIC) && init)
4217 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4220 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4221 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4222 hide parameters to this inner class finit$ and
4223 constructors. It also means that the field isn't final per
4224 say. */
4225 if (ARG_FINAL_P (current))
4227 FIELD_LOCAL_ALIAS (field_decl) = 1;
4228 FIELD_FINAL (field_decl) = 0;
4231 /* Check if we must chain. */
4232 if (must_chain)
4233 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4235 /* If we have an initialization value tied to the field */
4236 if (init)
4238 /* The field is declared static */
4239 if (flags & ACC_STATIC)
4241 /* We include the field and its initialization part into
4242 a list used to generate <clinit>. After <clinit> is
4243 walked, field initializations will be processed and
4244 fields initialized with known constants will be taken
4245 out of <clinit> and have their DECL_INITIAL set
4246 appropriately. */
4247 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4248 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4249 if (TREE_OPERAND (init, 1)
4250 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4251 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4253 /* A non-static field declared with an immediate initialization is
4254 to be initialized in <init>, if any. This field is remembered
4255 to be processed at the time of the generation of <init>. */
4256 else
4258 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4259 SET_CPC_INITIALIZER_STMT (ctxp, init);
4261 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4262 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4265 lineno = saved_lineno;
4268 /* Generate finit$, using the list of initialized fields to populate
4269 its body. finit$'s parameter(s) list is adjusted to include the
4270 one(s) used to initialized the field(s) caching outer context
4271 local(s). */
4273 static tree
4274 generate_finit (class_type)
4275 tree class_type;
4277 int count = 0;
4278 tree list = TYPE_FINIT_STMT_LIST (class_type);
4279 tree mdecl, current, parms;
4281 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4282 class_type, NULL_TREE,
4283 &count);
4284 CRAFTED_PARAM_LIST_FIXUP (parms);
4285 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4286 finit_identifier_node, parms);
4287 fix_method_argument_names (parms, mdecl);
4288 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4289 mdecl, NULL_TREE);
4290 DECL_FUNCTION_NAP (mdecl) = count;
4291 start_artificial_method_body (mdecl);
4293 for (current = list; current; current = TREE_CHAIN (current))
4294 java_method_add_stmt (mdecl,
4295 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4296 current));
4297 end_artificial_method_body (mdecl);
4298 return mdecl;
4301 static void
4302 add_instance_initializer (mdecl)
4303 tree mdecl;
4305 tree current;
4306 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4307 tree compound = NULL_TREE;
4309 if (stmt_list)
4311 for (current = stmt_list; current; current = TREE_CHAIN (current))
4312 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4314 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4315 NULL_TREE, compound));
4319 /* Shared accros method_declarator and method_header to remember the
4320 patch stage that was reached during the declaration of the method.
4321 A method DECL is built differently is there is no patch
4322 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4323 pending on the currently defined method. */
4325 static int patch_stage;
4327 /* Check the method declaration and add the method to its current
4328 class. If the argument list is known to contain incomplete types,
4329 the method is partially added and the registration will be resume
4330 once the method arguments resolved. If TYPE is NULL, we're dealing
4331 with a constructor. */
4333 static tree
4334 method_header (flags, type, mdecl, throws)
4335 int flags;
4336 tree type, mdecl, throws;
4338 tree type_wfl = NULL_TREE;
4339 tree meth_name = NULL_TREE;
4340 tree current, orig_arg, this_class = NULL;
4341 tree id, meth;
4342 int saved_lineno;
4343 int constructor_ok = 0, must_chain;
4344 int count;
4346 if (mdecl == error_mark_node)
4347 return error_mark_node;
4348 meth = TREE_VALUE (mdecl);
4349 id = TREE_PURPOSE (mdecl);
4351 check_modifiers_consistency (flags);
4353 if (GET_CPC ())
4354 this_class = TREE_TYPE (GET_CPC ());
4356 if (!this_class || this_class == error_mark_node)
4357 return NULL_TREE;
4359 /* There are some forbidden modifiers for an abstract method and its
4360 class must be abstract as well. */
4361 if (type && (flags & ACC_ABSTRACT))
4363 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4364 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4365 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4366 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4367 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4368 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4369 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4370 parse_error_context
4371 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4372 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4373 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4376 /* Things to be checked when declaring a constructor */
4377 if (!type)
4379 int ec = java_error_count;
4380 /* 8.6: Constructor declarations: we might be trying to define a
4381 method without specifying a return type. */
4382 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4383 parse_error_context
4384 (id, "Invalid method declaration, return type required");
4385 /* 8.6.3: Constructor modifiers */
4386 else
4388 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4389 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4390 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4391 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4392 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4394 /* If we found error here, we don't consider it's OK to tread
4395 the method definition as a constructor, for the rest of this
4396 function */
4397 if (ec == java_error_count)
4398 constructor_ok = 1;
4401 /* Method declared within the scope of an interface are implicitly
4402 abstract and public. Conflicts with other erroneously provided
4403 modifiers are checked right after. */
4405 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4407 /* If FLAGS isn't set because of a modifier, turn the
4408 corresponding modifier WFL to NULL so we issue a warning on
4409 the obsolete use of the modifier */
4410 if (!(flags & ACC_PUBLIC))
4411 MODIFIER_WFL (PUBLIC_TK) = NULL;
4412 if (!(flags & ACC_ABSTRACT))
4413 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4414 flags |= ACC_PUBLIC;
4415 flags |= ACC_ABSTRACT;
4418 /* Inner class can't declare static methods */
4419 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4421 parse_error_context
4422 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4423 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4424 lang_printable_name (this_class, 0));
4427 /* Modifiers context reset moved up, so abstract method declaration
4428 modifiers can be later checked. */
4430 /* Set constructor returned type to void and method name to <init>,
4431 unless we found an error identifier the constructor (in which
4432 case we retain the original name) */
4433 if (!type)
4435 type = void_type_node;
4436 if (constructor_ok)
4437 meth_name = init_identifier_node;
4439 else
4440 meth_name = EXPR_WFL_NODE (id);
4442 /* Do the returned type resolution and registration if necessary */
4443 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4445 if (meth_name)
4446 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4447 EXPR_WFL_NODE (id) = meth_name;
4448 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4450 if (must_chain)
4452 patch_stage = JDEP_METHOD_RETURN;
4453 register_incomplete_type (patch_stage, type_wfl, id, type);
4454 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4456 else
4457 TREE_TYPE (meth) = type;
4459 saved_lineno = lineno;
4460 /* When defining an abstract or interface method, the curly
4461 bracket at level 1 doesn't exist because there is no function
4462 body */
4463 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4464 EXPR_WFL_LINENO (id));
4466 /* Remember the original argument list */
4467 orig_arg = TYPE_ARG_TYPES (meth);
4469 if (patch_stage) /* includes ret type and/or all args */
4471 jdep *jdep;
4472 meth = add_method_1 (this_class, flags, meth_name, meth);
4473 /* Patch for the return type */
4474 if (patch_stage == JDEP_METHOD_RETURN)
4476 jdep = CLASSD_LAST (ctxp->classd_list);
4477 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4479 /* This is the stop JDEP. METH allows the function's signature
4480 to be computed. */
4481 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4483 else
4484 meth = add_method (this_class, flags, meth_name,
4485 build_java_signature (meth));
4487 /* Remember final parameters */
4488 MARK_FINAL_PARMS (meth, orig_arg);
4490 /* Fix the method argument list so we have the argument name
4491 information */
4492 fix_method_argument_names (orig_arg, meth);
4494 /* Register the parameter number and re-install the current line
4495 number */
4496 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4497 lineno = saved_lineno;
4499 /* Register exception specified by the `throws' keyword for
4500 resolution and set the method decl appropriate field to the list.
4501 Note: the grammar ensures that what we get here are class
4502 types. */
4503 if (throws)
4505 throws = nreverse (throws);
4506 for (current = throws; current; current = TREE_CHAIN (current))
4508 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4509 NULL_TREE, NULL_TREE);
4510 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4511 &TREE_VALUE (current);
4513 DECL_FUNCTION_THROWS (meth) = throws;
4516 /* Set the flag if we correctly processed a constructor */
4517 if (constructor_ok)
4519 DECL_CONSTRUCTOR_P (meth) = 1;
4520 /* Compute and store the number of artificial parameters declared
4521 for this constructor */
4522 for (count = 0, current = TYPE_FIELDS (this_class); current;
4523 current = TREE_CHAIN (current))
4524 if (FIELD_LOCAL_ALIAS (current))
4525 count++;
4526 DECL_FUNCTION_NAP (meth) = count;
4529 /* Eventually set the @deprecated tag flag */
4530 CHECK_DEPRECATED (meth);
4532 /* If doing xref, store column and line number information instead
4533 of the line number only. */
4534 if (flag_emit_xref)
4535 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4537 return meth;
4540 static void
4541 fix_method_argument_names (orig_arg, meth)
4542 tree orig_arg, meth;
4544 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4545 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4547 TREE_PURPOSE (arg) = this_identifier_node;
4548 arg = TREE_CHAIN (arg);
4550 while (orig_arg != end_params_node)
4552 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4553 orig_arg = TREE_CHAIN (orig_arg);
4554 arg = TREE_CHAIN (arg);
4558 /* Complete the method declaration with METHOD_BODY. */
4560 static void
4561 finish_method_declaration (method_body)
4562 tree method_body;
4564 int flags;
4566 if (!current_function_decl)
4567 return;
4569 flags = get_access_flags_from_decl (current_function_decl);
4571 /* 8.4.5 Method Body */
4572 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4574 tree wfl = DECL_NAME (current_function_decl);
4575 parse_error_context (wfl,
4576 "%s method `%s' can't have a body defined",
4577 (METHOD_NATIVE (current_function_decl) ?
4578 "Native" : "Abstract"),
4579 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4580 method_body = NULL_TREE;
4582 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4584 tree wfl = DECL_NAME (current_function_decl);
4585 parse_error_context
4586 (wfl,
4587 "Non native and non abstract method `%s' must have a body defined",
4588 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4589 method_body = NULL_TREE;
4592 if (flag_emit_class_files && method_body
4593 && TREE_CODE (method_body) == NOP_EXPR
4594 && TREE_TYPE (current_function_decl)
4595 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4596 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4598 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4599 maybe_absorb_scoping_blocks ();
4600 /* Exit function's body */
4601 exit_block ();
4602 /* Merge last line of the function with first line, directly in the
4603 function decl. It will be used to emit correct debug info. */
4604 if (!flag_emit_xref)
4605 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4607 /* Since function's argument's list are shared, reset the
4608 ARG_FINAL_P parameter that might have been set on some of this
4609 function parameters. */
4610 UNMARK_FINAL_PARMS (current_function_decl);
4612 /* So we don't have an irrelevant function declaration context for
4613 the next static block we'll see. */
4614 current_function_decl = NULL_TREE;
4617 /* Build a an error message for constructor circularity errors. */
4619 static char *
4620 constructor_circularity_msg (from, to)
4621 tree from, to;
4623 static char string [4096];
4624 char *t = xstrdup (lang_printable_name (from, 0));
4625 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4626 free (t);
4627 return string;
4630 /* Verify a circular call to METH. Return 1 if an error is found, 0
4631 otherwise. */
4633 static int
4634 verify_constructor_circularity (meth, current)
4635 tree meth, current;
4637 static tree list = NULL_TREE;
4638 static int initialized_p;
4639 tree c;
4641 /* If we haven't already registered LIST with the garbage collector,
4642 do so now. */
4643 if (!initialized_p)
4645 ggc_add_tree_root (&list, 1);
4646 initialized_p = 1;
4649 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4651 if (TREE_VALUE (c) == meth)
4653 char *t;
4654 if (list)
4656 tree liste;
4657 list = nreverse (list);
4658 for (liste = list; liste; liste = TREE_CHAIN (liste))
4660 parse_error_context
4661 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4662 constructor_circularity_msg
4663 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4664 java_error_count--;
4667 t = xstrdup (lang_printable_name (meth, 0));
4668 parse_error_context (TREE_PURPOSE (c),
4669 "%s: recursive invocation of constructor `%s'",
4670 constructor_circularity_msg (current, meth), t);
4671 free (t);
4672 list = NULL_TREE;
4673 return 1;
4676 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4678 list = tree_cons (c, current, list);
4679 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4680 return 1;
4681 list = TREE_CHAIN (list);
4683 return 0;
4686 /* Check modifiers that can be declared but exclusively */
4688 static void
4689 check_modifiers_consistency (flags)
4690 int flags;
4692 int acc_count = 0;
4693 tree cl = NULL_TREE;
4695 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4696 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4697 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4698 if (acc_count > 1)
4699 parse_error_context
4700 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4702 acc_count = 0;
4703 cl = NULL_TREE;
4704 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4705 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4706 if (acc_count > 1)
4707 parse_error_context (cl,
4708 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4711 /* Check the methode header METH for abstract specifics features */
4713 static void
4714 check_abstract_method_header (meth)
4715 tree meth;
4717 int flags = get_access_flags_from_decl (meth);
4718 /* DECL_NAME might still be a WFL node */
4719 tree name = GET_METHOD_NAME (meth);
4721 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4722 ACC_ABSTRACT, "abstract method",
4723 IDENTIFIER_POINTER (name));
4724 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4725 ACC_PUBLIC, "abstract method",
4726 IDENTIFIER_POINTER (name));
4728 check_modifiers ("Illegal modifier `%s' for interface method",
4729 flags, INTERFACE_METHOD_MODIFIERS);
4732 /* Create a FUNCTION_TYPE node and start augmenting it with the
4733 declared function arguments. Arguments type that can't be resolved
4734 are left as they are, but the returned node is marked as containing
4735 incomplete types. */
4737 static tree
4738 method_declarator (id, list)
4739 tree id, list;
4741 tree arg_types = NULL_TREE, current, node;
4742 tree meth = make_node (FUNCTION_TYPE);
4743 jdep *jdep;
4745 patch_stage = JDEP_NO_PATCH;
4747 if (GET_CPC () == error_mark_node)
4748 return error_mark_node;
4750 /* If we're dealing with an inner class constructor, we hide the
4751 this$<n> decl in the name field of its parameter declaration. We
4752 also might have to hide the outer context local alias
4753 initializers. Not done when the class is a toplevel class. */
4754 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4755 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4757 tree aliases_list, type, thisn;
4758 /* First the aliases, linked to the regular parameters */
4759 aliases_list =
4760 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4761 TREE_TYPE (GET_CPC ()),
4762 NULL_TREE, NULL);
4763 list = chainon (nreverse (aliases_list), list);
4765 /* Then this$<n> */
4766 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4767 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4768 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4769 list);
4772 for (current = list; current; current = TREE_CHAIN (current))
4774 int must_chain = 0;
4775 tree wfl_name = TREE_PURPOSE (current);
4776 tree type = TREE_VALUE (current);
4777 tree name = EXPR_WFL_NODE (wfl_name);
4778 tree already, arg_node;
4779 tree type_wfl = NULL_TREE;
4780 tree real_type;
4782 /* Obtain a suitable type for resolution, if necessary */
4783 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4785 /* Process NAME, as it may specify extra dimension(s) for it */
4786 type = build_array_from_name (type, type_wfl, name, &name);
4787 EXPR_WFL_NODE (wfl_name) = name;
4789 real_type = GET_REAL_TYPE (type);
4790 if (TREE_CODE (real_type) == RECORD_TYPE)
4792 real_type = promote_type (real_type);
4793 if (TREE_CODE (type) == TREE_LIST)
4794 TREE_PURPOSE (type) = real_type;
4797 /* Check redefinition */
4798 for (already = arg_types; already; already = TREE_CHAIN (already))
4799 if (TREE_PURPOSE (already) == name)
4801 parse_error_context
4802 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4803 IDENTIFIER_POINTER (name),
4804 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4805 break;
4808 /* If we've an incomplete argument type, we know there is a location
4809 to patch when the type get resolved, later. */
4810 jdep = NULL;
4811 if (must_chain)
4813 patch_stage = JDEP_METHOD;
4814 type = register_incomplete_type (patch_stage,
4815 type_wfl, wfl_name, type);
4816 jdep = CLASSD_LAST (ctxp->classd_list);
4817 JDEP_MISC (jdep) = id;
4820 /* The argument node: a name and a (possibly) incomplete type. */
4821 arg_node = build_tree_list (name, real_type);
4822 /* Remeber arguments declared final. */
4823 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4825 if (jdep)
4826 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4827 TREE_CHAIN (arg_node) = arg_types;
4828 arg_types = arg_node;
4830 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4831 node = build_tree_list (id, meth);
4832 return node;
4835 static int
4836 unresolved_type_p (wfl, returned)
4837 tree wfl;
4838 tree *returned;
4841 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4843 if (returned)
4845 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4846 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4847 *returned = TREE_TYPE (decl);
4848 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4849 *returned = TREE_TYPE (GET_CPC ());
4850 else
4851 *returned = NULL_TREE;
4853 return 1;
4855 if (returned)
4856 *returned = wfl;
4857 return 0;
4860 /* From NAME, build a qualified identifier node using the
4861 qualification from the current package definition. */
4863 static tree
4864 parser_qualified_classname (name)
4865 tree name;
4867 tree nested_class_name;
4869 if ((nested_class_name = maybe_make_nested_class_name (name)))
4870 return nested_class_name;
4872 if (ctxp->package)
4873 return merge_qualified_name (ctxp->package, name);
4874 else
4875 return name;
4878 /* Called once the type a interface extends is resolved. Returns 0 if
4879 everything is OK. */
4881 static int
4882 parser_check_super_interface (super_decl, this_decl, this_wfl)
4883 tree super_decl, this_decl, this_wfl;
4885 tree super_type = TREE_TYPE (super_decl);
4887 /* Has to be an interface */
4888 if (!CLASS_INTERFACE (super_decl))
4890 parse_error_context
4891 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4892 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4893 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4894 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4895 "interface" : "class"),
4896 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4897 return 1;
4900 /* Check scope: same package OK, other package: OK if public */
4901 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4902 return 1;
4904 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4905 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4906 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4907 return 0;
4910 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4911 0 if everthing is OK. */
4913 static int
4914 parser_check_super (super_decl, this_decl, wfl)
4915 tree super_decl, this_decl, wfl;
4917 tree super_type = TREE_TYPE (super_decl);
4919 /* SUPER should be a CLASS (neither an array nor an interface) */
4920 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4922 parse_error_context
4923 (wfl, "Class `%s' can't subclass %s `%s'",
4924 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4925 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4926 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4927 return 1;
4930 if (CLASS_FINAL (TYPE_NAME (super_type)))
4932 parse_error_context (wfl, "Can't subclass final classes: %s",
4933 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4934 return 1;
4937 /* Check scope: same package OK, other package: OK if public */
4938 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4939 return 1;
4941 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4942 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4943 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4944 return 0;
4947 /* Create a new dependency list and link it (in a LIFO manner) to the
4948 CTXP list of type dependency list. */
4950 static void
4951 create_jdep_list (ctxp)
4952 struct parser_ctxt *ctxp;
4954 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
4955 new->first = new->last = NULL;
4956 new->next = ctxp->classd_list;
4957 ctxp->classd_list = new;
4960 static jdeplist *
4961 reverse_jdep_list (ctxp)
4962 struct parser_ctxt *ctxp;
4964 register jdeplist *prev = NULL, *current, *next;
4965 for (current = ctxp->classd_list; current; current = next)
4967 next = current->next;
4968 current->next = prev;
4969 prev = current;
4971 return prev;
4974 /* Create a fake pointer based on the ID stored in
4975 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4976 registered again. */
4978 static tree
4979 obtain_incomplete_type (type_name)
4980 tree type_name;
4982 tree ptr, name;
4984 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4985 name = EXPR_WFL_NODE (type_name);
4986 else if (INCOMPLETE_TYPE_P (type_name))
4987 name = TYPE_NAME (type_name);
4988 else
4989 fatal ("invalid type name - obtain_incomplete_type");
4991 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
4992 if (TYPE_NAME (ptr) == name)
4993 break;
4995 if (!ptr)
4997 BUILD_PTR_FROM_NAME (ptr, name);
4998 layout_type (ptr);
4999 TREE_CHAIN (ptr) = ctxp->incomplete_class;
5000 ctxp->incomplete_class = ptr;
5003 return ptr;
5006 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5007 non NULL instead of computing a new fake type based on WFL. The new
5008 dependency is inserted in the current type dependency list, in FIFO
5009 manner. */
5011 static tree
5012 register_incomplete_type (kind, wfl, decl, ptr)
5013 int kind;
5014 tree wfl, decl, ptr;
5016 jdep *new = (jdep *)xmalloc (sizeof (jdep));
5018 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5019 ptr = obtain_incomplete_type (wfl);
5021 JDEP_KIND (new) = kind;
5022 JDEP_DECL (new) = decl;
5023 JDEP_SOLV (new) = ptr;
5024 JDEP_WFL (new) = wfl;
5025 JDEP_CHAIN (new) = NULL;
5026 JDEP_MISC (new) = NULL_TREE;
5027 /* For some dependencies, set the enclosing class of the current
5028 class to be the enclosing context */
5029 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5030 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
5031 && GET_ENCLOSING_CPC ())
5032 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5033 else
5034 JDEP_ENCLOSING (new) = GET_CPC ();
5035 JDEP_GET_PATCH (new) = (tree *)NULL;
5037 JDEP_INSERT (ctxp->classd_list, new);
5039 return ptr;
5042 void
5043 java_check_circular_reference ()
5045 tree current;
5046 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5048 tree type = TREE_TYPE (current);
5049 if (CLASS_INTERFACE (current))
5051 /* Check all interfaces this class extends */
5052 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5053 int n, i;
5055 if (!basetype_vec)
5056 return;
5057 n = TREE_VEC_LENGTH (basetype_vec);
5058 for (i = 0; i < n; i++)
5060 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5061 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5062 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5063 parse_error_context (lookup_cl (current),
5064 "Cyclic interface inheritance");
5067 else
5068 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5069 parse_error_context (lookup_cl (current),
5070 "Cyclic class inheritance%s",
5071 (cyclic_inheritance_report ?
5072 cyclic_inheritance_report : ""));
5076 /* Augment the parameter list PARM with parameters crafted to
5077 initialize outer context locals aliases. Through ARTIFICIAL, a
5078 count is kept of the number of crafted parameters. MODE governs
5079 what eventually gets created: something suitable for a function
5080 creation or a function invocation, either the constructor or
5081 finit$. */
5083 static tree
5084 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5085 int mode;
5086 tree class_type, parm;
5087 int *artificial;
5089 tree field;
5090 tree additional_parms = NULL_TREE;
5092 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5093 if (FIELD_LOCAL_ALIAS (field))
5095 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5096 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5097 tree mangled_id;
5099 switch (mode)
5101 case AIPL_FUNCTION_DECLARATION:
5102 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5103 &buffer [4]);
5104 purpose = build_wfl_node (mangled_id);
5105 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5106 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5107 else
5108 value = TREE_TYPE (field);
5109 break;
5111 case AIPL_FUNCTION_CREATION:
5112 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5113 &buffer [4]);
5114 value = TREE_TYPE (field);
5115 break;
5117 case AIPL_FUNCTION_FINIT_INVOCATION:
5118 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5119 &buffer [4]);
5120 /* Now, this is wrong. purpose should always be the NAME
5121 of something and value its matching value (decl, type,
5122 etc...) FIXME -- but there is a lot to fix. */
5124 /* When invoked for this kind of operation, we already
5125 know whether a field is used or not. */
5126 purpose = TREE_TYPE (field);
5127 value = build_wfl_node (mangled_id);
5128 break;
5130 case AIPL_FUNCTION_CTOR_INVOCATION:
5131 /* There are two case: the constructor invokation happends
5132 outside the local inner, in which case, locales from the outer
5133 context are directly used.
5135 Otherwise, we fold to using the alias directly. */
5136 if (class_type == current_class)
5137 value = field;
5138 else
5140 name = get_identifier (&buffer[4]);
5141 value = IDENTIFIER_LOCAL_VALUE (name);
5143 break;
5145 additional_parms = tree_cons (purpose, value, additional_parms);
5146 if (artificial)
5147 *artificial +=1;
5149 if (additional_parms)
5151 if (ANONYMOUS_CLASS_P (class_type)
5152 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5153 additional_parms = nreverse (additional_parms);
5154 parm = chainon (additional_parms, parm);
5157 return parm;
5160 /* Craft a constructor for CLASS_DECL -- what we should do when none
5161 where found. ARGS is non NULL when a special signature must be
5162 enforced. This is the case for anonymous classes. */
5164 static void
5165 craft_constructor (class_decl, args)
5166 tree class_decl, args;
5168 tree class_type = TREE_TYPE (class_decl);
5169 tree parm = NULL_TREE;
5170 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5171 ACC_PUBLIC : 0);
5172 int i = 0, artificial = 0;
5173 tree decl, ctor_name;
5174 char buffer [80];
5176 /* The constructor name is <init> unless we're dealing with an
5177 anonymous class, in which case the name will be fixed after having
5178 be expanded. */
5179 if (ANONYMOUS_CLASS_P (class_type))
5180 ctor_name = DECL_NAME (class_decl);
5181 else
5182 ctor_name = init_identifier_node;
5184 /* If we're dealing with an inner class constructor, we hide the
5185 this$<n> decl in the name field of its parameter declaration. */
5186 if (PURE_INNER_CLASS_TYPE_P (class_type))
5188 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5189 parm = tree_cons (build_current_thisn (class_type),
5190 build_pointer_type (type), parm);
5192 /* Some more arguments to be hidden here. The values of the local
5193 variables of the outer context that the inner class needs to see. */
5194 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5195 class_type, parm,
5196 &artificial);
5199 /* Then if there are any args to be enforced, enforce them now */
5200 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5202 sprintf (buffer, "parm%d", i++);
5203 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5206 CRAFTED_PARAM_LIST_FIXUP (parm);
5207 decl = create_artificial_method (class_type, flags, void_type_node,
5208 ctor_name, parm);
5209 fix_method_argument_names (parm, decl);
5210 /* Now, mark the artificial parameters. */
5211 DECL_FUNCTION_NAP (decl) = artificial;
5212 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5216 /* Fix the constructors. This will be called right after circular
5217 references have been checked. It is necessary to fix constructors
5218 early even if no code generation will take place for that class:
5219 some generated constructor might be required by the class whose
5220 compilation triggered this one to be simply loaded. */
5222 void
5223 java_fix_constructors ()
5225 tree current;
5227 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5229 tree class_type = TREE_TYPE (current);
5230 int saw_ctor = 0;
5231 tree decl;
5233 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5234 continue;
5236 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5238 if (DECL_CONSTRUCTOR_P (decl))
5240 fix_constructors (decl);
5241 saw_ctor = 1;
5245 /* Anonymous class constructor can't be generated that early. */
5246 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5247 craft_constructor (current, NULL_TREE);
5251 /* safe_layout_class just makes sure that we can load a class without
5252 disrupting the current_class, input_file, lineno, etc, information
5253 about the class processed currently. */
5255 void
5256 safe_layout_class (class)
5257 tree class;
5259 tree save_current_class = current_class;
5260 const char *save_input_filename = input_filename;
5261 int save_lineno = lineno;
5263 layout_class (class);
5265 current_class = save_current_class;
5266 input_filename = save_input_filename;
5267 lineno = save_lineno;
5268 CLASS_LOADED_P (class) = 1;
5271 static tree
5272 jdep_resolve_class (dep)
5273 jdep *dep;
5275 tree decl;
5277 if (JDEP_RESOLVED_P (dep))
5278 decl = JDEP_RESOLVED_DECL (dep);
5279 else
5281 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5282 JDEP_DECL (dep), JDEP_WFL (dep));
5283 JDEP_RESOLVED (dep, decl);
5286 if (!decl)
5287 complete_class_report_errors (dep);
5288 else if (PURE_INNER_CLASS_DECL_P (decl))
5289 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5290 return decl;
5293 /* Complete unsatisfied class declaration and their dependencies */
5295 void
5296 java_complete_class ()
5298 tree cclass;
5299 jdeplist *cclassd;
5300 int error_found;
5301 tree type;
5303 /* Process imports */
5304 process_imports ();
5306 /* Rever things so we have the right order */
5307 ctxp->class_list = nreverse (ctxp->class_list);
5308 ctxp->classd_list = reverse_jdep_list (ctxp);
5310 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5311 cclass && cclassd;
5312 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5314 jdep *dep;
5315 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5317 tree decl;
5318 if (!(decl = jdep_resolve_class (dep)))
5319 continue;
5321 /* Now it's time to patch */
5322 switch (JDEP_KIND (dep))
5324 case JDEP_SUPER:
5325 /* Simply patch super */
5326 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5327 continue;
5328 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5329 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5330 break;
5332 case JDEP_FIELD:
5334 /* We do part of the job done in add_field */
5335 tree field_decl = JDEP_DECL (dep);
5336 tree field_type = TREE_TYPE (decl);
5337 if (TREE_CODE (field_type) == RECORD_TYPE)
5338 field_type = promote_type (field_type);
5339 TREE_TYPE (field_decl) = field_type;
5340 DECL_ALIGN (field_decl) = 0;
5341 DECL_USER_ALIGN (field_decl) = 0;
5342 layout_decl (field_decl, 0);
5343 SOURCE_FRONTEND_DEBUG
5344 (("Completed field/var decl `%s' with `%s'",
5345 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5346 IDENTIFIER_POINTER (DECL_NAME (decl))));
5347 break;
5349 case JDEP_METHOD: /* We start patching a method */
5350 case JDEP_METHOD_RETURN:
5351 error_found = 0;
5352 while (1)
5354 if (decl)
5356 type = TREE_TYPE(decl);
5357 if (TREE_CODE (type) == RECORD_TYPE)
5358 type = promote_type (type);
5359 JDEP_APPLY_PATCH (dep, type);
5360 SOURCE_FRONTEND_DEBUG
5361 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5362 "Completing fct `%s' with ret type `%s'":
5363 "Completing arg `%s' with type `%s'"),
5364 IDENTIFIER_POINTER (EXPR_WFL_NODE
5365 (JDEP_DECL_WFL (dep))),
5366 IDENTIFIER_POINTER (DECL_NAME (decl))));
5368 else
5369 error_found = 1;
5370 dep = JDEP_CHAIN (dep);
5371 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5372 break;
5373 else
5374 decl = jdep_resolve_class (dep);
5376 if (!error_found)
5378 tree mdecl = JDEP_DECL (dep), signature;
5379 /* Recompute and reset the signature, check first that
5380 all types are now defined. If they're not,
5381 dont build the signature. */
5382 if (check_method_types_complete (mdecl))
5384 signature = build_java_signature (TREE_TYPE (mdecl));
5385 set_java_signature (TREE_TYPE (mdecl), signature);
5388 else
5389 continue;
5390 break;
5392 case JDEP_INTERFACE:
5393 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5394 JDEP_WFL (dep)))
5395 continue;
5396 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5397 break;
5399 case JDEP_PARM:
5400 case JDEP_VARIABLE:
5401 type = TREE_TYPE(decl);
5402 if (TREE_CODE (type) == RECORD_TYPE)
5403 type = promote_type (type);
5404 JDEP_APPLY_PATCH (dep, type);
5405 break;
5407 case JDEP_TYPE:
5408 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5409 SOURCE_FRONTEND_DEBUG
5410 (("Completing a random type dependency on a '%s' node",
5411 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5412 break;
5414 case JDEP_EXCEPTION:
5415 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5416 SOURCE_FRONTEND_DEBUG
5417 (("Completing `%s' `throws' argument node",
5418 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5419 break;
5421 case JDEP_ANONYMOUS:
5422 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5423 break;
5425 default:
5426 fatal ("Can't handle patch code %d - java_complete_class",
5427 JDEP_KIND (dep));
5431 return;
5434 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5435 array. */
5437 static tree
5438 resolve_class (enclosing, class_type, decl, cl)
5439 tree enclosing, class_type, decl, cl;
5441 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5442 const char *base = name;
5443 tree resolved_type = TREE_TYPE (class_type);
5444 tree resolved_type_decl;
5446 if (resolved_type != NULL_TREE)
5448 tree resolved_type_decl = TYPE_NAME (resolved_type);
5449 if (resolved_type_decl == NULL_TREE
5450 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5452 resolved_type_decl = build_decl (TYPE_DECL,
5453 TYPE_NAME (class_type),
5454 resolved_type);
5456 return resolved_type_decl;
5459 /* 1- Check to see if we have an array. If true, find what we really
5460 want to resolve */
5461 while (name[0] == '[')
5462 name++;
5463 if (base != name)
5465 TYPE_NAME (class_type) = get_identifier (name);
5466 WFL_STRIP_BRACKET (cl, cl);
5469 /* 2- Resolve the bare type */
5470 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5471 decl, cl)))
5472 return NULL_TREE;
5473 resolved_type = TREE_TYPE (resolved_type_decl);
5475 /* 3- If we have and array, reconstruct the array down to its nesting */
5476 if (base != name)
5478 while (base != name)
5480 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5481 resolved_type = promote_type (resolved_type);
5482 resolved_type = build_java_array_type (resolved_type, -1);
5483 CLASS_LOADED_P (resolved_type) = 1;
5484 name--;
5486 /* Build a fake decl for this, since this is what is expected to
5487 be returned. */
5488 resolved_type_decl =
5489 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5490 /* Figure how those two things are important for error report. FIXME */
5491 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5492 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
5493 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
5495 TREE_TYPE (class_type) = resolved_type;
5496 return resolved_type_decl;
5499 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5500 are used to report error messages. */
5502 tree
5503 do_resolve_class (enclosing, class_type, decl, cl)
5504 tree enclosing, class_type, decl, cl;
5506 tree new_class_decl;
5508 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5509 it is changed by find_in_imports{_on_demand} and (but it doesn't
5510 really matter) qualify_and_find */
5512 /* 0- Search in the current class as an inner class */
5514 /* Maybe some code here should be added to load the class or
5515 something, at least if the class isn't an inner class and ended
5516 being loaded from class file. FIXME. */
5517 while (enclosing)
5519 tree name;
5520 tree intermediate;
5522 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5523 return new_class_decl;
5525 intermediate = enclosing;
5526 /* Explore enclosing contexts. */
5527 while (INNER_CLASS_DECL_P (intermediate))
5529 intermediate = DECL_CONTEXT (intermediate);
5530 if ((new_class_decl = find_as_inner_class (intermediate,
5531 class_type, cl)))
5532 return new_class_decl;
5535 /* Now go to the upper classes, bail out if necessary. */
5536 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5537 if (!enclosing || enclosing == object_type_node)
5538 break;
5540 if (TREE_CODE (enclosing) == RECORD_TYPE)
5542 enclosing = TYPE_NAME (enclosing);
5543 continue;
5546 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
5547 BUILD_PTR_FROM_NAME (name, enclosing);
5548 else
5549 name = enclosing;
5550 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5553 /* 1- Check for the type in single imports. This will change
5554 TYPE_NAME() if something relevant is found */
5555 find_in_imports (class_type);
5557 /* 2- And check for the type in the current compilation unit */
5558 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5560 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5561 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5562 load_class (TYPE_NAME (class_type), 0);
5563 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5566 /* 3- Search according to the current package definition */
5567 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5569 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5570 TYPE_NAME (class_type))))
5571 return new_class_decl;
5574 /* 4- Check the import on demands. Don't allow bar.baz to be
5575 imported from foo.* */
5576 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5577 if (find_in_imports_on_demand (class_type))
5578 return NULL_TREE;
5580 /* If found in find_in_imports_on_demant, the type has already been
5581 loaded. */
5582 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5583 return new_class_decl;
5585 /* 5- Try with a name qualified with the package name we've seen so far */
5586 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5588 tree package;
5590 /* If there is a current package (ctxp->package), it's the first
5591 element of package_list and we can skip it. */
5592 for (package = (ctxp->package ?
5593 TREE_CHAIN (package_list) : package_list);
5594 package; package = TREE_CHAIN (package))
5595 if ((new_class_decl = qualify_and_find (class_type,
5596 TREE_PURPOSE (package),
5597 TYPE_NAME (class_type))))
5598 return new_class_decl;
5601 /* 5- Check an other compilation unit that bears the name of type */
5602 load_class (TYPE_NAME (class_type), 0);
5603 if (check_pkg_class_access (TYPE_NAME (class_type),
5604 (cl ? cl : lookup_cl (decl))))
5605 return NULL_TREE;
5607 /* 6- Last call for a resolution */
5608 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5611 static tree
5612 qualify_and_find (class_type, package, name)
5613 tree class_type, package, name;
5615 tree new_qualified = merge_qualified_name (package, name);
5616 tree new_class_decl;
5618 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5619 load_class (new_qualified, 0);
5620 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5622 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5623 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5624 load_class (new_qualified, 0);
5625 TYPE_NAME (class_type) = new_qualified;
5626 return IDENTIFIER_CLASS_VALUE (new_qualified);
5628 return NULL_TREE;
5631 /* Resolve NAME and lay it out (if not done and if not the current
5632 parsed class). Return a decl node. This function is meant to be
5633 called when type resolution is necessary during the walk pass. */
5635 static tree
5636 resolve_and_layout (something, cl)
5637 tree something;
5638 tree cl;
5640 tree decl, decl_type;
5642 /* Don't do that on the current class */
5643 if (something == current_class)
5644 return TYPE_NAME (current_class);
5646 /* Don't do anything for void and other primitive types */
5647 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5648 return NULL_TREE;
5650 /* Pointer types can be reall pointer types or fake pointers. When
5651 finding a real pointer, recheck for primitive types */
5652 if (TREE_CODE (something) == POINTER_TYPE)
5654 if (TREE_TYPE (something))
5656 something = TREE_TYPE (something);
5657 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5658 return NULL_TREE;
5660 else
5661 something = TYPE_NAME (something);
5664 /* Don't do anything for arrays of primitive types */
5665 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5666 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5667 return NULL_TREE;
5669 /* Something might be a WFL */
5670 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5671 something = EXPR_WFL_NODE (something);
5673 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5674 TYPE_DECL or a real TYPE */
5675 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5676 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5677 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5679 if (!(decl = resolve_no_layout (something, cl)))
5680 return NULL_TREE;
5682 /* Resolve and layout if necessary */
5683 decl_type = TREE_TYPE (decl);
5684 layout_class_methods (decl_type);
5685 /* Check methods */
5686 if (CLASS_FROM_SOURCE_P (decl_type))
5687 java_check_methods (decl);
5688 /* Layout the type if necessary */
5689 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5690 safe_layout_class (decl_type);
5692 return decl;
5695 /* Resolve a class, returns its decl but doesn't perform any
5696 layout. The current parsing context is saved and restored */
5698 static tree
5699 resolve_no_layout (name, cl)
5700 tree name, cl;
5702 tree ptr, decl;
5703 BUILD_PTR_FROM_NAME (ptr, name);
5704 java_parser_context_save_global ();
5705 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5706 java_parser_context_restore_global ();
5708 return decl;
5711 /* Called when reporting errors. Skip leader '[' in a complex array
5712 type description that failed to be resolved. */
5714 static const char *
5715 purify_type_name (name)
5716 const char *name;
5718 while (*name && *name == '[')
5719 name++;
5720 return name;
5723 /* The type CURRENT refers to can't be found. We print error messages. */
5725 static void
5726 complete_class_report_errors (dep)
5727 jdep *dep;
5729 const char *name;
5731 if (!JDEP_WFL (dep))
5732 return;
5734 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5735 switch (JDEP_KIND (dep))
5737 case JDEP_SUPER:
5738 parse_error_context
5739 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5740 purify_type_name (name),
5741 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5742 break;
5743 case JDEP_FIELD:
5744 parse_error_context
5745 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5746 purify_type_name (name),
5747 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5748 break;
5749 case JDEP_METHOD: /* Covers arguments */
5750 parse_error_context
5751 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5752 purify_type_name (name),
5753 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5754 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5755 break;
5756 case JDEP_METHOD_RETURN: /* Covers return type */
5757 parse_error_context
5758 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5759 purify_type_name (name),
5760 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5761 break;
5762 case JDEP_INTERFACE:
5763 parse_error_context
5764 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5765 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5766 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5767 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5768 break;
5769 case JDEP_VARIABLE:
5770 parse_error_context
5771 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5772 purify_type_name (IDENTIFIER_POINTER
5773 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5774 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5775 break;
5776 case JDEP_EXCEPTION: /* As specified by `throws' */
5777 parse_error_context
5778 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5779 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5780 break;
5781 default:
5782 /* Fix for -Wall. Just break doing nothing. The error will be
5783 caught later */
5784 break;
5788 /* Return a static string containing the DECL prototype string. If
5789 DECL is a constructor, use the class name instead of the form
5790 <init> */
5792 static const char *
5793 get_printable_method_name (decl)
5794 tree decl;
5796 const char *to_return;
5797 tree name = NULL_TREE;
5799 if (DECL_CONSTRUCTOR_P (decl))
5801 name = DECL_NAME (decl);
5802 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
5805 to_return = lang_printable_name (decl, 0);
5806 if (DECL_CONSTRUCTOR_P (decl))
5807 DECL_NAME (decl) = name;
5809 return to_return;
5812 /* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5813 nevertheless needs to be verfied, 1 otherwise. */
5815 static int
5816 reset_method_name (method)
5817 tree method;
5819 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5821 /* NAME is just the plain name when Object is being defined */
5822 if (DECL_CONTEXT (method) != object_type_node)
5823 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5824 init_identifier_node : GET_METHOD_NAME (method));
5825 return 0;
5827 else
5828 return 1;
5831 /* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5833 tree
5834 java_get_real_method_name (method_decl)
5835 tree method_decl;
5837 tree method_name = DECL_NAME (method_decl);
5838 if (DECL_CONSTRUCTOR_P (method_decl))
5839 return init_identifier_node;
5841 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5842 and still can be a constructor. FIXME */
5844 /* Don't confuse method only bearing the name of their class as
5845 constructors */
5846 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5847 && ctxp
5848 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
5849 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5850 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
5851 return init_identifier_node;
5852 else
5853 return EXPR_WFL_NODE (method_name);
5856 /* Track method being redefined inside the same class. As a side
5857 effect, set DECL_NAME to an IDENTIFIER (prior entering this
5858 function it's a FWL, so we can track errors more accurately.) */
5860 static int
5861 check_method_redefinition (class, method)
5862 tree class, method;
5864 tree redef, name;
5865 tree cl = DECL_NAME (method);
5866 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
5867 /* decl name of artificial <clinit> and finit$ doesn't need to be
5868 fixed and checked */
5870 /* Reset the method name before running the check. If it returns 1,
5871 the method doesn't need to be verified with respect to method
5872 redeclaration and we return 0 */
5873 if (reset_method_name (method))
5874 return 0;
5876 name = DECL_NAME (method);
5877 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5879 if (redef == method)
5880 break;
5881 if (DECL_NAME (redef) == name
5882 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
5884 parse_error_context
5885 (cl, "Duplicate %s declaration `%s'",
5886 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5887 get_printable_method_name (redef));
5888 return 1;
5891 return 0;
5894 /* Return 1 if check went ok, 0 otherwise. */
5895 static int
5896 check_abstract_method_definitions (do_interface, class_decl, type)
5897 int do_interface;
5898 tree class_decl, type;
5900 tree class = TREE_TYPE (class_decl);
5901 tree method, end_type;
5902 int ok = 1;
5904 end_type = (do_interface ? object_type_node : type);
5905 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5907 tree other_super, other_method, method_sig, method_name;
5908 int found = 0;
5909 int end_type_reached = 0;
5911 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5912 continue;
5914 /* Now verify that somewhere in between TYPE and CLASS,
5915 abstract method METHOD gets a non abstract definition
5916 that is inherited by CLASS. */
5918 method_sig = build_java_signature (TREE_TYPE (method));
5919 method_name = DECL_NAME (method);
5920 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5921 method_name = EXPR_WFL_NODE (method_name);
5923 other_super = class;
5924 do {
5925 if (other_super == end_type)
5926 end_type_reached = 1;
5928 /* Method search */
5929 for (other_method = TYPE_METHODS (other_super); other_method;
5930 other_method = TREE_CHAIN (other_method))
5932 tree s = build_java_signature (TREE_TYPE (other_method));
5933 tree other_name = DECL_NAME (other_method);
5935 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5936 other_name = EXPR_WFL_NODE (other_name);
5937 if (!DECL_CLINIT_P (other_method)
5938 && !DECL_CONSTRUCTOR_P (other_method)
5939 && method_name == other_name
5940 && method_sig == s
5941 && !METHOD_ABSTRACT (other_method))
5943 found = 1;
5944 break;
5947 other_super = CLASSTYPE_SUPER (other_super);
5948 } while (!end_type_reached);
5950 /* Report that abstract METHOD didn't find an implementation
5951 that CLASS can use. */
5952 if (!found)
5954 char *t = xstrdup (lang_printable_name
5955 (TREE_TYPE (TREE_TYPE (method)), 0));
5956 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5957 tree saved_wfl = NULL_TREE;
5959 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5961 saved_wfl = DECL_NAME (method);
5962 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5965 parse_error_context
5966 (lookup_cl (class_decl),
5967 "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",
5968 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5969 t, lang_printable_name (method, 0),
5970 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5971 "interface" : "class"),
5972 IDENTIFIER_POINTER (ccn),
5973 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5974 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5975 ok = 0;
5976 free (t);
5978 if (saved_wfl)
5979 DECL_NAME (method) = saved_wfl;
5983 if (ok && do_interface)
5985 /* Check for implemented interfaces. */
5986 int i;
5987 tree vector = TYPE_BINFO_BASETYPES (type);
5988 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5990 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5991 ok = check_abstract_method_definitions (1, class_decl, super);
5995 return ok;
5998 /* Check that CLASS_DECL somehow implements all inherited abstract
5999 methods. */
6001 static void
6002 java_check_abstract_method_definitions (class_decl)
6003 tree class_decl;
6005 tree class = TREE_TYPE (class_decl);
6006 tree super, vector;
6007 int i;
6009 if (CLASS_ABSTRACT (class_decl))
6010 return;
6012 /* Check for inherited types */
6013 super = class;
6014 do {
6015 super = CLASSTYPE_SUPER (super);
6016 check_abstract_method_definitions (0, class_decl, super);
6017 } while (super != object_type_node);
6019 /* Check for implemented interfaces. */
6020 vector = TYPE_BINFO_BASETYPES (class);
6021 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
6023 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6024 check_abstract_method_definitions (1, class_decl, super);
6028 /* Check all the types method DECL uses and return 1 if all of them
6029 are now complete, 0 otherwise. This is used to check whether its
6030 safe to build a method signature or not. */
6032 static int
6033 check_method_types_complete (decl)
6034 tree decl;
6036 tree type = TREE_TYPE (decl);
6037 tree args;
6039 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6040 return 0;
6042 args = TYPE_ARG_TYPES (type);
6043 if (TREE_CODE (type) == METHOD_TYPE)
6044 args = TREE_CHAIN (args);
6045 for (; args != end_params_node; args = TREE_CHAIN (args))
6046 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6047 return 0;
6049 return 1;
6052 /* Visible interface to check methods contained in CLASS_DECL */
6054 void
6055 java_check_methods (class_decl)
6056 tree class_decl;
6058 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6059 return;
6061 if (CLASS_INTERFACE (class_decl))
6062 java_check_abstract_methods (class_decl);
6063 else
6064 java_check_regular_methods (class_decl);
6066 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6069 /* Check all the methods of CLASS_DECL. Methods are first completed
6070 then checked according to regular method existance rules. If no
6071 constructor for CLASS_DECL were encountered, then build its
6072 declaration. */
6074 static void
6075 java_check_regular_methods (class_decl)
6076 tree class_decl;
6078 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6079 tree method;
6080 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6081 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
6082 tree mthrows;
6084 /* It is not necessary to check methods defined in java.lang.Object */
6085 if (class == object_type_node)
6086 return;
6088 if (!TYPE_NVIRTUALS (class))
6089 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6091 /* Should take interfaces into account. FIXME */
6092 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6094 tree sig;
6095 tree method_wfl = DECL_NAME (method);
6096 int aflags;
6098 /* If we previously found something and its name was saved,
6099 reinstall it now */
6100 if (found && saved_found_wfl)
6102 DECL_NAME (found) = saved_found_wfl;
6103 saved_found_wfl = NULL_TREE;
6106 /* Check for redefinitions */
6107 if (check_method_redefinition (class, method))
6108 continue;
6110 /* If we see one constructor a mark so we don't generate the
6111 default one. Also skip other verifications: constructors
6112 can't be inherited hence hiden or overriden */
6113 if (DECL_CONSTRUCTOR_P (method))
6115 saw_constructor = 1;
6116 continue;
6119 /* We verify things thrown by the method. They must inherits from
6120 java.lang.Throwable */
6121 for (mthrows = DECL_FUNCTION_THROWS (method);
6122 mthrows; mthrows = TREE_CHAIN (mthrows))
6124 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6125 parse_error_context
6126 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6127 IDENTIFIER_POINTER
6128 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6131 sig = build_java_argument_signature (TREE_TYPE (method));
6132 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6134 /* Inner class can't declare static methods */
6135 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6137 char *t = xstrdup (lang_printable_name (class, 0));
6138 parse_error_context
6139 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6140 lang_printable_name (method, 0), t);
6141 free (t);
6144 /* Nothing overrides or it's a private method. */
6145 if (!found)
6146 continue;
6147 if (METHOD_PRIVATE (found))
6149 found = NULL_TREE;
6150 continue;
6153 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6154 We set it temporarily for the sake of the error report. */
6155 saved_found_wfl = DECL_NAME (found);
6156 reset_method_name (found);
6158 /* If `found' is declared in an interface, make sure the
6159 modifier matches. */
6160 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6161 && clinit_identifier_node != DECL_NAME (found)
6162 && !METHOD_PUBLIC (method))
6164 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6165 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6166 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6167 lang_printable_name (method, 0),
6168 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6171 /* Can't override a method with the same name and different return
6172 types. */
6173 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6175 char *t = xstrdup
6176 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6177 parse_error_context
6178 (method_wfl,
6179 "Method `%s' was defined with return type `%s' in class `%s'",
6180 lang_printable_name (found, 0), t,
6181 IDENTIFIER_POINTER
6182 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6183 free (t);
6186 aflags = get_access_flags_from_decl (found);
6188 /* Can't override final. Can't override static. */
6189 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6191 /* Static *can* override static */
6192 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6193 continue;
6194 parse_error_context
6195 (method_wfl,
6196 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6197 (METHOD_FINAL (found) ? "Final" : "Static"),
6198 lang_printable_name (found, 0),
6199 (METHOD_FINAL (found) ? "final" : "static"),
6200 IDENTIFIER_POINTER
6201 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6202 continue;
6205 /* Static method can't override instance method. */
6206 if (METHOD_STATIC (method))
6208 parse_error_context
6209 (method_wfl,
6210 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6211 lang_printable_name (found, 0),
6212 IDENTIFIER_POINTER
6213 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6214 continue;
6217 /* - Overriding/hiding public must be public
6218 - Overriding/hiding protected must be protected or public
6219 - If the overriden or hidden method has default (package)
6220 access, then the overriding or hiding method must not be
6221 private; otherwise, a compile-time error occurs. If
6222 `found' belongs to an interface, things have been already
6223 taken care of. */
6224 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6225 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6226 || (METHOD_PROTECTED (found)
6227 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6228 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6229 && METHOD_PRIVATE (method))))
6231 parse_error_context
6232 (method_wfl,
6233 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6234 (METHOD_PUBLIC (method) ? "public" :
6235 (METHOD_PRIVATE (method) ? "private" : "protected")),
6236 IDENTIFIER_POINTER (DECL_NAME
6237 (TYPE_NAME (DECL_CONTEXT (found)))));
6238 continue;
6241 /* Overriding methods must have compatible `throws' clauses on checked
6242 exceptions, if any */
6243 check_throws_clauses (method, method_wfl, found);
6245 /* Inheriting multiple methods with the same signature. FIXME */
6248 /* Don't forget eventual pending found and saved_found_wfl. Take
6249 into account that we might have exited because we saw an
6250 artificial method as the last entry. */
6252 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6253 DECL_NAME (found) = saved_found_wfl;
6255 if (!TYPE_NVIRTUALS (class))
6256 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6258 /* Search for inherited abstract method not yet implemented in this
6259 class. */
6260 java_check_abstract_method_definitions (class_decl);
6262 if (!saw_constructor)
6263 fatal ("No constructor found");
6266 /* Return a non zero value if the `throws' clause of METHOD (if any)
6267 is incompatible with the `throws' clause of FOUND (if any). */
6269 static void
6270 check_throws_clauses (method, method_wfl, found)
6271 tree method, method_wfl, found;
6273 tree mthrows, fthrows;
6275 /* Can't check these things with class loaded from bytecode. FIXME */
6276 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6277 return;
6279 for (mthrows = DECL_FUNCTION_THROWS (method);
6280 mthrows; mthrows = TREE_CHAIN (mthrows))
6282 /* We don't verify unchecked expressions */
6283 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6284 continue;
6285 /* Checked expression must be compatible */
6286 for (fthrows = DECL_FUNCTION_THROWS (found);
6287 fthrows; fthrows = TREE_CHAIN (fthrows))
6288 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6289 break;
6290 if (!fthrows)
6292 parse_error_context
6293 (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'",
6294 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6295 lang_printable_name (found, 0),
6296 IDENTIFIER_POINTER
6297 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6302 /* Check abstract method of interface INTERFACE */
6304 static void
6305 java_check_abstract_methods (interface_decl)
6306 tree interface_decl;
6308 int i, n;
6309 tree method, basetype_vec, found;
6310 tree interface = TREE_TYPE (interface_decl);
6312 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6314 tree method_wfl = DECL_NAME (method);
6316 /* 2- Check for double definition inside the defining interface */
6317 if (check_method_redefinition (interface, method))
6318 continue;
6320 /* 3- Overriding is OK as far as we preserve the return type and
6321 the thrown exceptions (FIXME) */
6322 found = lookup_java_interface_method2 (interface, method);
6323 if (found)
6325 char *t;
6326 tree saved_found_wfl = DECL_NAME (found);
6327 reset_method_name (found);
6328 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6329 parse_error_context
6330 (method_wfl,
6331 "Method `%s' was defined with return type `%s' in class `%s'",
6332 lang_printable_name (found, 0), t,
6333 IDENTIFIER_POINTER
6334 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6335 free (t);
6336 DECL_NAME (found) = saved_found_wfl;
6337 continue;
6341 /* 4- Inherited methods can't differ by their returned types */
6342 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6343 return;
6344 n = TREE_VEC_LENGTH (basetype_vec);
6345 for (i = 0; i < n; i++)
6347 tree sub_interface_method, sub_interface;
6348 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6349 if (!vec_elt)
6350 continue;
6351 sub_interface = BINFO_TYPE (vec_elt);
6352 for (sub_interface_method = TYPE_METHODS (sub_interface);
6353 sub_interface_method;
6354 sub_interface_method = TREE_CHAIN (sub_interface_method))
6356 found = lookup_java_interface_method2 (interface,
6357 sub_interface_method);
6358 if (found && (found != sub_interface_method))
6360 tree saved_found_wfl = DECL_NAME (found);
6361 reset_method_name (found);
6362 parse_error_context
6363 (lookup_cl (sub_interface_method),
6364 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6365 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6366 lang_printable_name (found, 0),
6367 IDENTIFIER_POINTER
6368 (DECL_NAME (TYPE_NAME
6369 (DECL_CONTEXT (sub_interface_method)))),
6370 IDENTIFIER_POINTER
6371 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6372 DECL_NAME (found) = saved_found_wfl;
6378 /* Lookup methods in interfaces using their name and partial
6379 signature. Return a matching method only if their types differ. */
6381 static tree
6382 lookup_java_interface_method2 (class, method_decl)
6383 tree class, method_decl;
6385 int i, n;
6386 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6388 if (!basetype_vec)
6389 return NULL_TREE;
6391 n = TREE_VEC_LENGTH (basetype_vec);
6392 for (i = 0; i < n; i++)
6394 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6395 if ((BINFO_TYPE (vec_elt) != object_type_node)
6396 && (to_return =
6397 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6398 return to_return;
6400 for (i = 0; i < n; i++)
6402 to_return = lookup_java_interface_method2
6403 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6404 if (to_return)
6405 return to_return;
6408 return NULL_TREE;
6411 /* Lookup method using their name and partial signature. Return a
6412 matching method only if their types differ. */
6414 static tree
6415 lookup_java_method2 (clas, method_decl, do_interface)
6416 tree clas, method_decl;
6417 int do_interface;
6419 tree method, method_signature, method_name, method_type, name;
6421 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6422 name = DECL_NAME (method_decl);
6423 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6424 EXPR_WFL_NODE (name) : name);
6425 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6427 while (clas != NULL_TREE)
6429 for (method = TYPE_METHODS (clas);
6430 method != NULL_TREE; method = TREE_CHAIN (method))
6432 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6433 tree name = DECL_NAME (method);
6434 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6435 EXPR_WFL_NODE (name) : name) == method_name
6436 && method_sig == method_signature
6437 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6438 return method;
6440 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6442 return NULL_TREE;
6445 /* Return the line that matches DECL line number, and try its best to
6446 position the column number. Used during error reports. */
6448 static tree
6449 lookup_cl (decl)
6450 tree decl;
6452 static tree cl = NULL_TREE;
6453 char *line, *found;
6455 if (!decl)
6456 return NULL_TREE;
6458 if (cl == NULL_TREE)
6460 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6461 ggc_add_tree_root (&cl, 1);
6464 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6465 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6467 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
6468 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6470 found = strstr ((const char *)line,
6471 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6472 if (found)
6473 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6475 return cl;
6478 /* Look for a simple name in the single-type import list */
6480 static tree
6481 find_name_in_single_imports (name)
6482 tree name;
6484 tree node;
6486 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6487 if (TREE_VALUE (node) == name)
6488 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6490 return NULL_TREE;
6493 /* Process all single-type import. */
6495 static int
6496 process_imports ()
6498 tree import;
6499 int error_found;
6501 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6503 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6505 /* Don't load twice something already defined. */
6506 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6507 continue;
6509 while (1)
6511 tree left;
6513 QUALIFIED_P (to_be_found) = 1;
6514 load_class (to_be_found, 0);
6515 error_found =
6516 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6518 /* We found it, we can bail out */
6519 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6520 break;
6522 /* We haven't found it. Maybe we're trying to access an
6523 inner class. The only way for us to know is to try again
6524 after having dropped a qualifier. If we can't break it further,
6525 we have an error. */
6526 if (breakdown_qualified (&left, NULL, to_be_found))
6527 break;
6529 to_be_found = left;
6531 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6533 parse_error_context (TREE_PURPOSE (import),
6534 "Class or interface `%s' not found in import",
6535 IDENTIFIER_POINTER (to_be_found));
6536 return 1;
6538 if (error_found)
6539 return 1;
6541 return 0;
6544 /* Possibly find and mark a class imported by a single-type import
6545 statement. */
6547 static void
6548 find_in_imports (class_type)
6549 tree class_type;
6551 tree import;
6553 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6554 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6556 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6557 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6561 static int
6562 note_possible_classname (name, len)
6563 const char *name;
6564 int len;
6566 tree node;
6567 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6568 len = len - 5;
6569 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6570 len = len - 6;
6571 else
6572 return 0;
6573 node = ident_subst (name, len, "", '/', '.', "");
6574 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6575 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6576 return 1;
6579 /* Read a import directory, gathering potential match for further type
6580 references. Indifferently reads a filesystem or a ZIP archive
6581 directory. */
6583 static void
6584 read_import_dir (wfl)
6585 tree wfl;
6587 tree package_id = EXPR_WFL_NODE (wfl);
6588 const char *package_name = IDENTIFIER_POINTER (package_id);
6589 int package_length = IDENTIFIER_LENGTH (package_id);
6590 DIR *dirp = NULL;
6591 JCF *saved_jcf = current_jcf;
6593 int found = 0;
6594 int k;
6595 void *entry;
6596 struct buffer filename[1];
6599 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6600 return;
6601 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6603 BUFFER_INIT (filename);
6604 buffer_grow (filename, package_length + 100);
6606 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6608 const char *entry_name = jcf_path_name (entry);
6609 int entry_length = strlen (entry_name);
6610 if (jcf_path_is_zipfile (entry))
6612 ZipFile *zipf;
6613 buffer_grow (filename, entry_length);
6614 memcpy (filename->data, entry_name, entry_length - 1);
6615 filename->data[entry_length-1] = '\0';
6616 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6617 if (zipf == NULL)
6618 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6619 else
6621 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6622 BUFFER_RESET (filename);
6623 for (k = 0; k < package_length; k++)
6625 char ch = package_name[k];
6626 *filename->ptr++ = ch == '.' ? '/' : ch;
6628 *filename->ptr++ = '/';
6630 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6632 const char *current_entry = ZIPDIR_FILENAME (zipd);
6633 int current_entry_len = zipd->filename_length;
6635 if (current_entry_len >= BUFFER_LENGTH (filename)
6636 && strncmp (filename->data, current_entry,
6637 BUFFER_LENGTH (filename)) != 0)
6638 continue;
6639 found |= note_possible_classname (current_entry,
6640 current_entry_len);
6644 else
6646 BUFFER_RESET (filename);
6647 buffer_grow (filename, entry_length + package_length + 4);
6648 strcpy (filename->data, entry_name);
6649 filename->ptr = filename->data + entry_length;
6650 for (k = 0; k < package_length; k++)
6652 char ch = package_name[k];
6653 *filename->ptr++ = ch == '.' ? '/' : ch;
6655 *filename->ptr = '\0';
6657 dirp = opendir (filename->data);
6658 if (dirp == NULL)
6659 continue;
6660 *filename->ptr++ = '/';
6661 for (;;)
6663 int len;
6664 const char *d_name;
6665 struct dirent *direntp = readdir (dirp);
6666 if (!direntp)
6667 break;
6668 d_name = direntp->d_name;
6669 len = strlen (direntp->d_name);
6670 buffer_grow (filename, len+1);
6671 strcpy (filename->ptr, d_name);
6672 found |= note_possible_classname (filename->data + entry_length,
6673 package_length+len+1);
6675 if (dirp)
6676 closedir (dirp);
6680 free (filename->data);
6682 /* Here we should have a unified way of retrieving an entry, to be
6683 indexed. */
6684 if (!found)
6686 static int first = 1;
6687 if (first)
6689 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6690 java_error_count++;
6691 first = 0;
6693 else
6694 parse_error_context (wfl, "Package `%s' not found in import",
6695 package_name);
6696 current_jcf = saved_jcf;
6697 return;
6699 current_jcf = saved_jcf;
6702 /* Possibly find a type in the import on demands specified
6703 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6704 entire list, to detected potential double definitions. */
6706 static int
6707 find_in_imports_on_demand (class_type)
6708 tree class_type;
6710 tree node, import, node_to_use = NULL_TREE;
6711 int seen_once = -1;
6712 tree cl = NULL_TREE;
6714 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6716 const char *id_name;
6717 obstack_grow (&temporary_obstack,
6718 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6719 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6720 obstack_1grow (&temporary_obstack, '.');
6721 obstack_grow0 (&temporary_obstack,
6722 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6723 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6724 id_name = obstack_finish (&temporary_obstack);
6726 node = maybe_get_identifier (id_name);
6727 if (node && IS_A_CLASSFILE_NAME (node))
6729 if (seen_once < 0)
6731 cl = TREE_PURPOSE (import);
6732 seen_once = 1;
6733 node_to_use = node;
6735 else
6737 seen_once++;
6738 parse_error_context
6739 (TREE_PURPOSE (import),
6740 "Type `%s' also potentially defined in package `%s'",
6741 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6742 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6747 if (seen_once == 1)
6749 /* Setup lineno so that it refers to the line of the import (in
6750 case we parse a class file and encounter errors */
6751 tree decl;
6752 int saved_lineno = lineno;
6753 lineno = EXPR_WFL_LINENO (cl);
6754 TYPE_NAME (class_type) = node_to_use;
6755 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6756 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6757 /* If there is no DECL set for the class or if the class isn't
6758 loaded and not seen in source yet, the load */
6759 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6760 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6761 load_class (node_to_use, 0);
6762 lineno = saved_lineno;
6763 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6765 else
6766 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6769 /* Add package NAME to the list of package encountered so far. To
6770 speed up class lookup in do_resolve_class, we make sure a
6771 particular package is added only once. */
6773 static void
6774 register_package (name)
6775 tree name;
6777 static struct hash_table _pht, *pht = NULL;
6779 if (!pht)
6781 hash_table_init (&_pht, hash_newfunc,
6782 java_hash_hash_tree_node, java_hash_compare_tree_node);
6783 pht = &_pht;
6786 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6788 package_list = chainon (package_list, build_tree_list (name, NULL));
6789 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6793 static tree
6794 resolve_package (pkg, next)
6795 tree pkg, *next;
6797 tree current, acc;
6798 tree type_name = NULL_TREE;
6799 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6801 /* The trick is to determine when the package name stops and were
6802 the name of something contained in the package starts. Then we
6803 return a fully qualified name of what we want to get. */
6805 /* Do a quick search on well known package names */
6806 if (!strncmp (name, "java.lang.reflect", 17))
6808 *next =
6809 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6810 type_name = lookup_package_type (name, 17);
6812 else if (!strncmp (name, "java.lang", 9))
6814 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6815 type_name = lookup_package_type (name, 9);
6818 /* If we found something here, return */
6819 if (type_name)
6820 return type_name;
6822 *next = EXPR_WFL_QUALIFICATION (pkg);
6824 /* Try to progressively construct a type name */
6825 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6826 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6827 current; current = TREE_CHAIN (current))
6829 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6830 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6832 type_name = acc;
6833 /* resolve_package should be used in a loop, hence we
6834 point at this one to naturally process the next one at
6835 the next iteration. */
6836 *next = current;
6837 break;
6840 return type_name;
6843 static tree
6844 lookup_package_type (name, from)
6845 const char *name;
6846 int from;
6848 char subname [128];
6849 const char *sub = &name[from+1];
6850 while (*sub != '.' && *sub)
6851 sub++;
6852 strncpy (subname, name, sub-name);
6853 subname [sub-name] = '\0';
6854 return get_identifier (subname);
6857 static void
6858 check_inner_class_access (decl, enclosing_decl, cl)
6859 tree decl, enclosing_decl, cl;
6861 int access = 0;
6863 /* We don't issue an error message when CL is null. CL can be null
6864 as a result of processing a JDEP crafted by source_start_java_method
6865 for the purpose of patching its parm decl. But the error would
6866 have been already trapped when fixing the method's signature.
6867 DECL can also be NULL in case of earlier errors. */
6868 if (!decl || !cl)
6869 return;
6871 /* We grant access to private and protected inner classes if the
6872 location from where we're trying to access DECL is an enclosing
6873 context for DECL or if both have a common enclosing context. */
6874 if (CLASS_PRIVATE (decl))
6875 access = 1;
6876 if (CLASS_PROTECTED (decl))
6877 access = 2;
6878 if (!access)
6879 return;
6881 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6882 TREE_TYPE (decl))
6883 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6884 TREE_TYPE (decl)))
6885 return;
6887 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6888 (access == 1 ? "private" : "protected"),
6889 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6890 lang_printable_name (decl, 0));
6893 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6894 access violations were found, 1 otherwise. */
6896 static int
6897 check_pkg_class_access (class_name, cl)
6898 tree class_name;
6899 tree cl;
6901 tree type;
6903 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6904 return 0;
6906 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6907 return 0;
6909 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6911 /* Access to a private class within the same package is
6912 allowed. */
6913 tree l, r;
6914 breakdown_qualified (&l, &r, class_name);
6915 if (l == ctxp->package)
6916 return 0;
6918 parse_error_context
6919 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
6920 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6921 IDENTIFIER_POINTER (class_name));
6922 return 1;
6924 return 0;
6927 /* Local variable declaration. */
6929 static void
6930 declare_local_variables (modifier, type, vlist)
6931 int modifier;
6932 tree type;
6933 tree vlist;
6935 tree decl, current, saved_type;
6936 tree type_wfl = NULL_TREE;
6937 int must_chain = 0;
6938 int final_p = 0;
6940 /* Push a new block if statements were seen between the last time we
6941 pushed a block and now. Keep a cound of block to close */
6942 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
6944 tree body = GET_CURRENT_BLOCK (current_function_decl);
6945 tree b = enter_block ();
6946 BLOCK_EXPR_ORIGIN (b) = body;
6949 if (modifier)
6951 int i;
6952 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
6953 if (modifier == ACC_FINAL)
6954 final_p = 1;
6955 else
6957 parse_error_context
6958 (ctxp->modifier_ctx [i],
6959 "Only `final' is allowed as a local variables modifier");
6960 return;
6964 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6965 hold the TYPE value if a new incomplete has to be created (as
6966 opposed to being found already existing and reused). */
6967 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6969 /* If TYPE is fully resolved and we don't have a reference, make one */
6970 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6972 /* Go through all the declared variables */
6973 for (current = vlist, saved_type = type; current;
6974 current = TREE_CHAIN (current), type = saved_type)
6976 tree other, real_type;
6977 tree wfl = TREE_PURPOSE (current);
6978 tree name = EXPR_WFL_NODE (wfl);
6979 tree init = TREE_VALUE (current);
6981 /* Process NAME, as it may specify extra dimension(s) for it */
6982 type = build_array_from_name (type, type_wfl, name, &name);
6984 /* Variable redefinition check */
6985 if ((other = lookup_name_in_blocks (name)))
6987 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6988 DECL_SOURCE_LINE (other));
6989 continue;
6992 /* Type adjustment. We may have just readjusted TYPE because
6993 the variable specified more dimensions. Make sure we have
6994 a reference if we can and don't have one already. */
6995 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6997 real_type = GET_REAL_TYPE (type);
6998 /* Never layout this decl. This will be done when its scope
6999 will be entered */
7000 decl = build_decl (VAR_DECL, name, real_type);
7001 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7002 LOCAL_FINAL (decl) = final_p;
7003 BLOCK_CHAIN_DECL (decl);
7005 /* If doing xreferencing, replace the line number with the WFL
7006 compound value */
7007 if (flag_emit_xref)
7008 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7010 /* Don't try to use an INIT statement when an error was found */
7011 if (init && java_error_count)
7012 init = NULL_TREE;
7014 /* Add the initialization function to the current function's code */
7015 if (init)
7017 /* Name might have been readjusted */
7018 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7019 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7020 java_method_add_stmt (current_function_decl,
7021 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7022 init));
7025 /* Setup dependency the type of the decl */
7026 if (must_chain)
7028 jdep *dep;
7029 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7030 dep = CLASSD_LAST (ctxp->classd_list);
7031 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7034 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7037 /* Called during parsing. Build decls from argument list. */
7039 static void
7040 source_start_java_method (fndecl)
7041 tree fndecl;
7043 tree tem;
7044 tree parm_decl;
7045 int i;
7047 if (!fndecl)
7048 return;
7050 current_function_decl = fndecl;
7052 /* New scope for the function */
7053 enter_block ();
7054 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7055 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7057 tree type = TREE_VALUE (tem);
7058 tree name = TREE_PURPOSE (tem);
7060 /* If type is incomplete. Create an incomplete decl and ask for
7061 the decl to be patched later */
7062 if (INCOMPLETE_TYPE_P (type))
7064 jdep *jdep;
7065 tree real_type = GET_REAL_TYPE (type);
7066 parm_decl = build_decl (PARM_DECL, name, real_type);
7067 type = obtain_incomplete_type (type);
7068 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7069 jdep = CLASSD_LAST (ctxp->classd_list);
7070 JDEP_MISC (jdep) = name;
7071 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7073 else
7074 parm_decl = build_decl (PARM_DECL, name, type);
7076 /* Remember if a local variable was declared final (via its
7077 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7078 if (ARG_FINAL_P (tem))
7080 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7081 LOCAL_FINAL (parm_decl) = 1;
7084 BLOCK_CHAIN_DECL (parm_decl);
7086 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7087 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7088 nreverse (tem);
7089 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7090 DECL_MAX_LOCALS (current_function_decl) = i;
7093 /* Called during parsing. Creates an artificial method declaration. */
7095 static tree
7096 create_artificial_method (class, flags, type, name, args)
7097 tree class;
7098 int flags;
7099 tree type, name, args;
7101 tree mdecl;
7103 java_parser_context_save_global ();
7104 lineno = 0;
7105 mdecl = make_node (FUNCTION_TYPE);
7106 TREE_TYPE (mdecl) = type;
7107 TYPE_ARG_TYPES (mdecl) = args;
7108 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7109 java_parser_context_restore_global ();
7110 DECL_ARTIFICIAL (mdecl) = 1;
7111 return mdecl;
7114 /* Starts the body if an artifical method. */
7116 static void
7117 start_artificial_method_body (mdecl)
7118 tree mdecl;
7120 DECL_SOURCE_LINE (mdecl) = 1;
7121 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7122 source_start_java_method (mdecl);
7123 enter_block ();
7126 static void
7127 end_artificial_method_body (mdecl)
7128 tree mdecl;
7130 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7131 It has to be evaluated first. (if mdecl is current_function_decl,
7132 we have an undefined behavior if no temporary variable is used.) */
7133 tree b = exit_block ();
7134 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7135 exit_block ();
7138 /* Called during expansion. Push decls formerly built from argument
7139 list so they're usable during expansion. */
7141 static void
7142 expand_start_java_method (fndecl)
7143 tree fndecl;
7145 tree tem, *ptr;
7147 current_function_decl = fndecl;
7149 if (! quiet_flag)
7150 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
7151 announce_function (fndecl);
7152 if (! quiet_flag)
7153 fprintf (stderr, "]");
7155 pushlevel (1); /* Prepare for a parameter push */
7156 ptr = &DECL_ARGUMENTS (fndecl);
7157 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7158 while (tem)
7160 tree next = TREE_CHAIN (tem);
7161 tree type = TREE_TYPE (tem);
7162 if (PROMOTE_PROTOTYPES
7163 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7164 && INTEGRAL_TYPE_P (type))
7165 type = integer_type_node;
7166 DECL_ARG_TYPE (tem) = type;
7167 layout_decl (tem, 0);
7168 pushdecl (tem);
7169 *ptr = tem;
7170 ptr = &TREE_CHAIN (tem);
7171 tem = next;
7173 *ptr = NULL_TREE;
7174 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7175 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
7178 /* Terminate a function and expand its body. */
7180 static void
7181 source_end_java_method ()
7183 tree fndecl = current_function_decl;
7184 int flag_asynchronous_exceptions = asynchronous_exceptions;
7186 if (!fndecl)
7187 return;
7189 java_parser_context_save_global ();
7190 lineno = ctxp->last_ccb_indent1;
7192 /* Set EH language codes */
7193 java_set_exception_lang_code ();
7195 /* Turn function bodies with only a NOP expr null, so they don't get
7196 generated at all and we won't get warnings when using the -W
7197 -Wall flags. */
7198 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7199 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7201 /* Generate function's code */
7202 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7203 && ! flag_emit_class_files
7204 && ! flag_emit_xref)
7205 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7207 /* pop out of its parameters */
7208 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7209 poplevel (1, 0, 1);
7210 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7212 /* Generate rtl for function exit. */
7213 if (! flag_emit_class_files && ! flag_emit_xref)
7215 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7216 /* Emit catch-finally clauses */
7217 emit_handlers ();
7218 expand_function_end (input_filename, lineno, 0);
7220 /* FIXME: If the current method contains any exception handlers,
7221 force asynchronous_exceptions: this is necessary because signal
7222 handlers in libjava may throw exceptions. This is far from being
7223 a perfect solution, but it's better than doing nothing at all.*/
7224 if (catch_clauses)
7225 asynchronous_exceptions = 1;
7227 /* Run the optimizers and output assembler code for this function. */
7228 rest_of_compilation (fndecl);
7231 current_function_decl = NULL_TREE;
7232 java_parser_context_restore_global ();
7233 asynchronous_exceptions = flag_asynchronous_exceptions;
7236 /* Record EXPR in the current function block. Complements compound
7237 expression second operand if necessary. */
7239 tree
7240 java_method_add_stmt (fndecl, expr)
7241 tree fndecl, expr;
7243 if (!GET_CURRENT_BLOCK (fndecl))
7244 return NULL_TREE;
7245 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7248 static tree
7249 add_stmt_to_block (b, type, stmt)
7250 tree b, type, stmt;
7252 tree body = BLOCK_EXPR_BODY (b), c;
7254 if (java_error_count)
7255 return body;
7257 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7258 return body;
7260 BLOCK_EXPR_BODY (b) = c;
7261 TREE_SIDE_EFFECTS (c) = 1;
7262 return c;
7265 /* Add STMT to EXISTING if possible, otherwise create a new
7266 COMPOUND_EXPR and add STMT to it. */
7268 static tree
7269 add_stmt_to_compound (existing, type, stmt)
7270 tree existing, type, stmt;
7272 if (existing)
7273 return build (COMPOUND_EXPR, type, existing, stmt);
7274 else
7275 return stmt;
7278 void java_layout_seen_class_methods ()
7280 tree previous_list = all_class_list;
7281 tree end = NULL_TREE;
7282 tree current;
7284 while (1)
7286 for (current = previous_list;
7287 current != end; current = TREE_CHAIN (current))
7288 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7290 if (previous_list != all_class_list)
7292 end = previous_list;
7293 previous_list = all_class_list;
7295 else
7296 break;
7300 void
7301 java_reorder_fields ()
7303 static tree stop_reordering = NULL_TREE;
7304 static int initialized_p;
7305 tree current;
7307 /* Register STOP_REORDERING with the garbage collector. */
7308 if (!initialized_p)
7310 ggc_add_tree_root (&stop_reordering, 1);
7311 initialized_p = 1;
7314 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7316 current_class = TREE_TYPE (TREE_VALUE (current));
7318 if (current_class == stop_reordering)
7319 break;
7321 /* Reverse the fields, but leave the dummy field in front.
7322 Fields are already ordered for Object and Class */
7323 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7324 && current_class != class_type_node)
7326 /* If the dummy field is there, reverse the right fields and
7327 just layout the type for proper fields offset */
7328 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7330 tree fields = TYPE_FIELDS (current_class);
7331 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7332 TYPE_SIZE (current_class) = NULL_TREE;
7334 /* We don't have a dummy field, we need to layout the class,
7335 after having reversed the fields */
7336 else
7338 TYPE_FIELDS (current_class) =
7339 nreverse (TYPE_FIELDS (current_class));
7340 TYPE_SIZE (current_class) = NULL_TREE;
7344 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7347 /* Layout the methods of all classes loaded in one way on an
7348 other. Check methods of source parsed classes. Then reorder the
7349 fields and layout the classes or the type of all source parsed
7350 classes */
7352 void
7353 java_layout_classes ()
7355 tree current;
7356 int save_error_count = java_error_count;
7358 /* Layout the methods of all classes seen so far */
7359 java_layout_seen_class_methods ();
7360 java_parse_abort_on_error ();
7361 all_class_list = NULL_TREE;
7363 /* Then check the methods of all parsed classes */
7364 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7365 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7366 java_check_methods (TREE_VALUE (current));
7367 java_parse_abort_on_error ();
7369 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7371 current_class = TREE_TYPE (TREE_VALUE (current));
7372 layout_class (current_class);
7374 /* From now on, the class is considered completely loaded */
7375 CLASS_LOADED_P (current_class) = 1;
7377 /* Error reported by the caller */
7378 if (java_error_count)
7379 return;
7382 /* We might have reloaded classes durign the process of laying out
7383 classes for code generation. We must layout the methods of those
7384 late additions, as constructor checks might use them */
7385 java_layout_seen_class_methods ();
7386 java_parse_abort_on_error ();
7389 /* Expand methods in the current set of classes rememebered for
7390 generation. */
7392 static void
7393 java_complete_expand_classes ()
7395 tree current;
7397 do_not_fold = flag_emit_xref;
7399 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7400 if (!INNER_CLASS_DECL_P (current))
7401 java_complete_expand_class (current);
7404 /* Expand the methods found in OUTER, starting first by OUTER's inner
7405 classes, if any. */
7407 static void
7408 java_complete_expand_class (outer)
7409 tree outer;
7411 tree inner_list;
7413 set_nested_class_simple_name_value (outer, 1); /* Set */
7415 /* We need to go after all inner classes and start expanding them,
7416 starting with most nested ones. We have to do that because nested
7417 classes might add functions to outer classes */
7419 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7420 inner_list; inner_list = TREE_CHAIN (inner_list))
7421 java_complete_expand_class (TREE_PURPOSE (inner_list));
7423 java_complete_expand_methods (outer);
7424 set_nested_class_simple_name_value (outer, 0); /* Reset */
7427 /* Expand methods registered in CLASS_DECL. The general idea is that
7428 we expand regular methods first. This allows us get an estimate on
7429 how outer context local alias fields are really used so we can add
7430 to the constructor just enough code to initialize them properly (it
7431 also lets us generate finit$ correctly.) Then we expand the
7432 constructors and then <clinit>. */
7434 static void
7435 java_complete_expand_methods (class_decl)
7436 tree class_decl;
7438 tree clinit, finit, decl, first_decl;
7440 current_class = TREE_TYPE (class_decl);
7442 /* Find whether the class has final variables */
7443 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7444 if (FIELD_FINAL (decl))
7446 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7447 break;
7450 /* Initialize a new constant pool */
7451 init_outgoing_cpool ();
7453 /* Pre-expand <clinit> to figure whether we really need it or
7454 not. If we do need it, we pre-expand the static fields so they're
7455 ready to be used somewhere else. <clinit> will be fully expanded
7456 after we processed the constructors. */
7457 first_decl = TYPE_METHODS (current_class);
7458 clinit = maybe_generate_pre_expand_clinit (current_class);
7460 /* Then generate finit$ (if we need to) because constructor will
7461 try to use it.*/
7462 if (TYPE_FINIT_STMT_LIST (current_class))
7464 finit = generate_finit (current_class);
7465 java_complete_expand_method (finit);
7468 /* Now do the constructors */
7469 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7471 int no_body;
7473 if (!DECL_CONSTRUCTOR_P (decl))
7474 continue;
7476 no_body = !DECL_FUNCTION_BODY (decl);
7477 /* Don't generate debug info on line zero when expanding a
7478 generated constructor. */
7479 if (no_body)
7480 restore_line_number_status (1);
7482 /* Reset the final local variable assignment flags */
7483 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7484 reset_final_variable_local_assignment_flag (current_class);
7486 java_complete_expand_method (decl);
7488 /* Check for missed out final variable assignment */
7489 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7490 check_final_variable_local_assignment_flag (current_class, decl);
7492 if (no_body)
7493 restore_line_number_status (0);
7496 /* First, do the ordinary methods. */
7497 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7499 /* Skip abstract or native methods -- but do handle native
7500 methods when generating JNI stubs. */
7501 if (METHOD_ABSTRACT (decl)
7502 || (! flag_jni && METHOD_NATIVE (decl))
7503 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7504 continue;
7506 if (METHOD_NATIVE (decl))
7508 tree body = build_jni_stub (decl);
7509 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7512 java_complete_expand_method (decl);
7515 /* If there is indeed a <clinit>, fully expand it now */
7516 if (clinit)
7518 /* Reset the final local variable assignment flags */
7519 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7520 reset_static_final_variable_assignment_flag (current_class);
7521 /* Prevent the use of `this' inside <clinit> */
7522 ctxp->explicit_constructor_p = 1;
7523 java_complete_expand_method (clinit);
7524 ctxp->explicit_constructor_p = 0;
7525 /* Check for missed out static final variable assignment */
7526 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7527 && !CLASS_INTERFACE (class_decl))
7528 check_static_final_variable_assignment_flag (current_class);
7531 /* We might have generated a class$ that we now want to expand */
7532 if (TYPE_DOT_CLASS (current_class))
7533 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7535 /* Now verify constructor circularity (stop after the first one we
7536 prove wrong.) */
7537 if (!CLASS_INTERFACE (class_decl))
7538 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7539 if (DECL_CONSTRUCTOR_P (decl)
7540 && verify_constructor_circularity (decl, decl))
7541 break;
7543 /* Final check on the initialization of final variables. */
7544 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7546 check_final_variable_global_assignment_flag (current_class);
7547 /* If we have an interface, check for uninitialized fields. */
7548 if (CLASS_INTERFACE (class_decl))
7549 check_static_final_variable_assignment_flag (current_class);
7552 /* Save the constant pool. We'll need to restore it later. */
7553 TYPE_CPOOL (current_class) = outgoing_cpool;
7556 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7557 safely used in some other methods/constructors. */
7559 static tree
7560 maybe_generate_pre_expand_clinit (class_type)
7561 tree class_type;
7563 tree current, mdecl;
7565 if (!TYPE_CLINIT_STMT_LIST (class_type))
7566 return NULL_TREE;
7568 /* Go through all static fields and pre expand them */
7569 for (current = TYPE_FIELDS (class_type); current;
7570 current = TREE_CHAIN (current))
7571 if (FIELD_STATIC (current))
7572 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7574 /* Then build the <clinit> method */
7575 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7576 clinit_identifier_node, end_params_node);
7577 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7578 mdecl, NULL_TREE);
7579 start_artificial_method_body (mdecl);
7581 /* We process the list of assignment we produced as the result of
7582 the declaration of initialized static field and add them as
7583 statement to the <clinit> method. */
7584 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7585 current = TREE_CHAIN (current))
7587 tree stmt = current;
7588 /* We build the assignment expression that will initialize the
7589 field to its value. There are strict rules on static
7590 initializers (8.5). FIXME */
7591 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7592 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7593 java_method_add_stmt (mdecl, stmt);
7596 end_artificial_method_body (mdecl);
7598 /* Now we want to place <clinit> as the last method (because we need
7599 it at least for interface so that it doesn't interfere with the
7600 dispatch table based lookup. */
7601 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7603 current = TREE_CHAIN (TYPE_METHODS (class_type));
7604 TYPE_METHODS (class_type) = current;
7606 while (TREE_CHAIN (current))
7607 current = TREE_CHAIN (current);
7609 TREE_CHAIN (current) = mdecl;
7610 TREE_CHAIN (mdecl) = NULL_TREE;
7613 return mdecl;
7616 /* Analyzes a method body and look for something that isn't a
7617 MODIFY_EXPR. */
7619 static int
7620 analyze_clinit_body (bbody)
7621 tree bbody;
7623 while (bbody)
7624 switch (TREE_CODE (bbody))
7626 case BLOCK:
7627 bbody = BLOCK_EXPR_BODY (bbody);
7628 break;
7630 case EXPR_WITH_FILE_LOCATION:
7631 bbody = EXPR_WFL_NODE (bbody);
7632 break;
7634 case COMPOUND_EXPR:
7635 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7636 return 1;
7637 bbody = TREE_OPERAND (bbody, 1);
7638 break;
7640 case MODIFY_EXPR:
7641 bbody = NULL_TREE;
7642 break;
7644 default:
7645 bbody = NULL_TREE;
7646 return 1;
7648 return 0;
7652 /* See whether we could get rid of <clinit>. Criteria are: all static
7653 final fields have constant initial values and the body of <clinit>
7654 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7656 static int
7657 maybe_yank_clinit (mdecl)
7658 tree mdecl;
7660 tree type, current;
7661 tree fbody, bbody;
7662 int found = 0;
7664 if (!DECL_CLINIT_P (mdecl))
7665 return 0;
7667 /* If the body isn't empty, then we keep <clinit>. Note that if
7668 we're emitting classfiles, this isn't enough not to rule it
7669 out. */
7670 fbody = DECL_FUNCTION_BODY (mdecl);
7671 bbody = BLOCK_EXPR_BODY (fbody);
7672 if (bbody && bbody != error_mark_node)
7673 bbody = BLOCK_EXPR_BODY (bbody);
7674 else
7675 return 0;
7676 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7677 return 0;
7679 type = DECL_CONTEXT (mdecl);
7680 current = TYPE_FIELDS (type);
7682 for (current = (current ? TREE_CHAIN (current) : current);
7683 current; current = TREE_CHAIN (current))
7685 tree f_init;
7687 /* We're not interested in non static field */
7688 if (!FIELD_STATIC (current))
7689 continue;
7691 /* Anything that isn't String or a basic type is ruled out -- or
7692 if we know how to deal with it (when doing things natively) we
7693 should generated an empty <clinit> so that SUID are computed
7694 correctly. */
7695 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7696 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7697 break;
7699 f_init = DECL_INITIAL (current);
7700 /* If we're emitting native code, we want static final fields to
7701 have constant initializers. If we don't meet these
7702 conditions, we keep <clinit> */
7703 if (!flag_emit_class_files
7704 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7705 break;
7706 /* If we're emitting bytecode, we want static fields to have
7707 constant initializers or no initializer. If we don't meet
7708 these conditions, we keep <clinit> */
7709 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7710 break;
7713 /* Now we analyze the method body and look for something that
7714 isn't a MODIFY_EXPR */
7715 if (bbody == empty_stmt_node)
7716 found = 0;
7717 else
7718 found = analyze_clinit_body (bbody);
7720 if (current || found)
7721 return 0;
7723 /* Get rid of <clinit> in the class' list of methods */
7724 if (TYPE_METHODS (type) == mdecl)
7725 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7726 else
7727 for (current = TYPE_METHODS (type); current;
7728 current = TREE_CHAIN (current))
7729 if (TREE_CHAIN (current) == mdecl)
7731 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7732 break;
7735 return 1;
7739 /* Complete and expand a method. */
7741 static void
7742 java_complete_expand_method (mdecl)
7743 tree mdecl;
7745 int yank_clinit = 0;
7747 current_function_decl = mdecl;
7748 /* Fix constructors before expanding them */
7749 if (DECL_CONSTRUCTOR_P (mdecl))
7750 fix_constructors (mdecl);
7752 /* Expand functions that have a body */
7753 if (DECL_FUNCTION_BODY (mdecl))
7755 tree fbody = DECL_FUNCTION_BODY (mdecl);
7756 tree block_body = BLOCK_EXPR_BODY (fbody);
7757 tree exception_copy = NULL_TREE;
7758 expand_start_java_method (mdecl);
7759 build_result_decl (mdecl);
7761 current_this
7762 = (!METHOD_STATIC (mdecl) ?
7763 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7765 /* Purge the `throws' list of unchecked exceptions. If we're
7766 doing xref, save a copy of the list and re-install it
7767 later. */
7768 if (flag_emit_xref)
7769 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7771 purge_unchecked_exceptions (mdecl);
7773 /* Install exceptions thrown with `throws' */
7774 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7776 if (block_body != NULL_TREE)
7778 block_body = java_complete_tree (block_body);
7780 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7781 check_for_initialization (block_body);
7782 ctxp->explicit_constructor_p = 0;
7785 BLOCK_EXPR_BODY (fbody) = block_body;
7787 /* If we saw a return but couldn't evaluate it properly, we'll
7788 have an error_mark_node here. */
7789 if (block_body != error_mark_node
7790 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7791 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7792 && !flag_emit_xref)
7793 missing_return_error (current_function_decl);
7795 /* Check wether we could just get rid of clinit, now the picture
7796 is complete. */
7797 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7798 complete_start_java_method (mdecl);
7800 /* Don't go any further if we've found error(s) during the
7801 expansion */
7802 if (!java_error_count && !yank_clinit)
7803 source_end_java_method ();
7804 else
7806 if (java_error_count)
7807 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7808 poplevel (1, 0, 1);
7811 /* Pop the exceptions and sanity check */
7812 POP_EXCEPTIONS();
7813 if (currently_caught_type_list)
7814 fatal ("Exception list non empty - java_complete_expand_method");
7816 if (flag_emit_xref)
7817 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7823 /* This section of the code deals with accessing enclosing context
7824 fields either directly by using the relevant access to this$<n> or
7825 by invoking an access method crafted for that purpose. */
7827 /* Build the necessary access from an inner class to an outer
7828 class. This routine could be optimized to cache previous result
7829 (decl, current_class and returned access). When an access method
7830 needs to be generated, it always takes the form of a read. It might
7831 be later turned into a write by calling outer_field_access_fix. */
7833 static tree
7834 build_outer_field_access (id, decl)
7835 tree id, decl;
7837 tree access = NULL_TREE;
7838 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7839 tree decl_ctx = DECL_CONTEXT (decl);
7841 /* If the immediate enclosing context of the current class is the
7842 field decl's class or inherits from it; build the access as
7843 `this$<n>.<field>'. Note that we will break the `private' barrier
7844 if we're not emitting bytecodes. */
7845 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
7846 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7848 tree thisn = build_current_thisn (current_class);
7849 access = make_qualified_primary (build_wfl_node (thisn),
7850 id, EXPR_WFL_LINECOL (id));
7852 /* Otherwise, generate access methods to outer this and access the
7853 field (either using an access method or by direct access.) */
7854 else
7856 int lc = EXPR_WFL_LINECOL (id);
7858 /* Now we chain the required number of calls to the access$0 to
7859 get a hold to the enclosing instance we need, and then we
7860 build the field access. */
7861 access = build_access_to_thisn (current_class, decl_ctx, lc);
7863 /* If the field is private and we're generating bytecode, then
7864 we generate an access method */
7865 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7867 tree name = build_outer_field_access_methods (decl);
7868 access = build_outer_field_access_expr (lc, decl_ctx,
7869 name, access, NULL_TREE);
7871 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7872 Once again we break the `private' access rule from a foreign
7873 class. */
7874 else
7875 access = make_qualified_primary (access, id, lc);
7877 return resolve_expression_name (access, NULL);
7880 /* Return a non zero value if NODE describes an outer field inner
7881 access. */
7883 static int
7884 outer_field_access_p (type, decl)
7885 tree type, decl;
7887 if (!INNER_CLASS_TYPE_P (type)
7888 || TREE_CODE (decl) != FIELD_DECL
7889 || DECL_CONTEXT (decl) == type)
7890 return 0;
7892 /* If the inner class extends the declaration context of the field
7893 we're try to acces, then this isn't an outer field access */
7894 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7895 return 0;
7897 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7898 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7900 if (type == DECL_CONTEXT (decl))
7901 return 1;
7903 if (!DECL_CONTEXT (TYPE_NAME (type)))
7905 /* Before we give up, see whether the field is inherited from
7906 the enclosing context we're considering. */
7907 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7908 return 1;
7909 break;
7913 return 0;
7916 /* Return a non zero value if NODE represents an outer field inner
7917 access that was been already expanded. As a side effect, it returns
7918 the name of the field being accessed and the argument passed to the
7919 access function, suitable for a regeneration of the access method
7920 call if necessary. */
7922 static int
7923 outer_field_expanded_access_p (node, name, arg_type, arg)
7924 tree node, *name, *arg_type, *arg;
7926 int identified = 0;
7928 if (TREE_CODE (node) != CALL_EXPR)
7929 return 0;
7931 /* Well, gcj generates slightly different tree nodes when compiling
7932 to native or bytecodes. It's the case for function calls. */
7934 if (flag_emit_class_files
7935 && TREE_CODE (node) == CALL_EXPR
7936 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7937 identified = 1;
7938 else if (!flag_emit_class_files)
7940 node = TREE_OPERAND (node, 0);
7942 if (node && TREE_OPERAND (node, 0)
7943 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7945 node = TREE_OPERAND (node, 0);
7946 if (TREE_OPERAND (node, 0)
7947 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7948 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7949 (DECL_NAME (TREE_OPERAND (node, 0)))))
7950 identified = 1;
7954 if (identified && name && arg_type && arg)
7956 tree argument = TREE_OPERAND (node, 1);
7957 *name = DECL_NAME (TREE_OPERAND (node, 0));
7958 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7959 *arg = TREE_VALUE (argument);
7961 return identified;
7964 /* Detect in NODE an outer field read access from an inner class and
7965 transform it into a write with RHS as an argument. This function is
7966 called from the java_complete_lhs when an assignment to a LHS can
7967 be identified. */
7969 static tree
7970 outer_field_access_fix (wfl, node, rhs)
7971 tree wfl, node, rhs;
7973 tree name, arg_type, arg;
7975 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7977 /* At any rate, check whether we're trying to assign a value to
7978 a final. */
7979 tree accessed = (JDECL_P (node) ? node :
7980 (TREE_CODE (node) == COMPONENT_REF ?
7981 TREE_OPERAND (node, 1) : node));
7982 if (check_final_assignment (accessed, wfl))
7983 return error_mark_node;
7985 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7986 arg_type, name, arg, rhs);
7987 return java_complete_tree (node);
7989 return NULL_TREE;
7992 /* Construct the expression that calls an access method:
7993 <type>.access$<n>(<arg1> [, <arg2>]);
7995 ARG2 can be NULL and will be omitted in that case. It will denote a
7996 read access. */
7998 static tree
7999 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
8000 int lc;
8001 tree type, access_method_name, arg1, arg2;
8003 tree args, cn, access;
8005 args = arg1 ? arg1 :
8006 build_wfl_node (build_current_thisn (current_class));
8007 args = build_tree_list (NULL_TREE, args);
8009 if (arg2)
8010 args = tree_cons (NULL_TREE, arg2, args);
8012 access = build_method_invocation (build_wfl_node (access_method_name), args);
8013 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8014 return make_qualified_primary (cn, access, lc);
8017 static tree
8018 build_new_access_id ()
8020 static int access_n_counter = 1;
8021 char buffer [128];
8023 sprintf (buffer, "access$%d", access_n_counter++);
8024 return get_identifier (buffer);
8027 /* Create the static access functions for the outer field DECL. We define a
8028 read:
8029 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8030 return inst$.field;
8032 and a write access:
8033 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8034 TREE_TYPE (<field>) value$) {
8035 return inst$.field = value$;
8037 We should have a usage flags on the DECL so we can lazily turn the ones
8038 we're using for code generation. FIXME.
8041 static tree
8042 build_outer_field_access_methods (decl)
8043 tree decl;
8045 tree id, args, stmt, mdecl;
8047 if (FIELD_INNER_ACCESS_P (decl))
8048 return FIELD_INNER_ACCESS (decl);
8050 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8052 /* Create the identifier and a function named after it. */
8053 id = build_new_access_id ();
8055 /* The identifier is marked as bearing the name of a generated write
8056 access function for outer field accessed from inner classes. */
8057 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8059 /* Create the read access */
8060 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8061 TREE_CHAIN (args) = end_params_node;
8062 stmt = make_qualified_primary (build_wfl_node (inst_id),
8063 build_wfl_node (DECL_NAME (decl)), 0);
8064 stmt = build_return (0, stmt);
8065 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8066 TREE_TYPE (decl), id, args, stmt);
8067 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8069 /* Create the write access method. No write access for final variable */
8070 if (!FIELD_FINAL (decl))
8072 args = build_tree_list (inst_id,
8073 build_pointer_type (DECL_CONTEXT (decl)));
8074 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8075 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8076 stmt = make_qualified_primary (build_wfl_node (inst_id),
8077 build_wfl_node (DECL_NAME (decl)), 0);
8078 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8079 build_wfl_node (wpv_id)));
8080 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8081 TREE_TYPE (decl), id,
8082 args, stmt);
8084 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8086 /* Return the access name */
8087 return FIELD_INNER_ACCESS (decl) = id;
8090 /* Build an field access method NAME. */
8092 static tree
8093 build_outer_field_access_method (class, type, name, args, body)
8094 tree class, type, name, args, body;
8096 tree saved_current_function_decl, mdecl;
8098 /* Create the method */
8099 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8100 fix_method_argument_names (args, mdecl);
8101 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8103 /* Attach the method body. */
8104 saved_current_function_decl = current_function_decl;
8105 start_artificial_method_body (mdecl);
8106 java_method_add_stmt (mdecl, body);
8107 end_artificial_method_body (mdecl);
8108 current_function_decl = saved_current_function_decl;
8110 return mdecl;
8114 /* This section deals with building access function necessary for
8115 certain kinds of method invocation from inner classes. */
8117 static tree
8118 build_outer_method_access_method (decl)
8119 tree decl;
8121 tree saved_current_function_decl, mdecl;
8122 tree args = NULL_TREE, call_args = NULL_TREE;
8123 tree carg, id, body, class;
8124 char buffer [80];
8125 int parm_id_count = 0;
8127 /* Test this abort with an access to a private field */
8128 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8129 abort ();
8131 /* Check the cache first */
8132 if (DECL_FUNCTION_INNER_ACCESS (decl))
8133 return DECL_FUNCTION_INNER_ACCESS (decl);
8135 class = DECL_CONTEXT (decl);
8137 /* Obtain an access identifier and mark it */
8138 id = build_new_access_id ();
8139 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8141 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8142 /* Create the arguments, as much as the original */
8143 for (; carg && carg != end_params_node;
8144 carg = TREE_CHAIN (carg))
8146 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8147 args = chainon (args, build_tree_list (get_identifier (buffer),
8148 TREE_VALUE (carg)));
8150 args = chainon (args, end_params_node);
8152 /* Create the method */
8153 mdecl = create_artificial_method (class, ACC_STATIC,
8154 TREE_TYPE (TREE_TYPE (decl)), id, args);
8155 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8156 /* There is a potential bug here. We should be able to use
8157 fix_method_argument_names, but then arg names get mixed up and
8158 eventually a constructor will have its this$0 altered and the
8159 outer context won't be assignment properly. The test case is
8160 stub.java FIXME */
8161 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8163 /* Attach the method body. */
8164 saved_current_function_decl = current_function_decl;
8165 start_artificial_method_body (mdecl);
8167 /* The actual method invocation uses the same args. When invoking a
8168 static methods that way, we don't want to skip the first
8169 argument. */
8170 carg = args;
8171 if (!METHOD_STATIC (decl))
8172 carg = TREE_CHAIN (carg);
8173 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8174 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8175 call_args);
8177 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8178 call_args);
8179 if (!METHOD_STATIC (decl))
8180 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8181 body, 0);
8182 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8183 body = build_return (0, body);
8184 java_method_add_stmt (mdecl,body);
8185 end_artificial_method_body (mdecl);
8186 current_function_decl = saved_current_function_decl;
8188 /* Back tag the access function so it know what it accesses */
8189 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8191 /* Tag the current method so it knows it has an access generated */
8192 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8196 /* This section of the code deals with building expressions to access
8197 the enclosing instance of an inner class. The enclosing instance is
8198 kept in a generated field called this$<n>, with <n> being the
8199 inner class nesting level (starting from 0.) */
8201 /* Build an access to a given this$<n>, always chaining access call to
8202 others. Access methods to this$<n> are build on the fly if
8203 necessary. This CAN'T be used to solely access this$<n-1> from
8204 this$<n> (which alway yield to special cases and optimization, see
8205 for example build_outer_field_access). */
8207 static tree
8208 build_access_to_thisn (from, to, lc)
8209 tree from, to;
8210 int lc;
8212 tree access = NULL_TREE;
8214 while (from != to)
8216 if (!access)
8218 access = build_current_thisn (from);
8219 access = build_wfl_node (access);
8221 else
8223 tree access0_wfl, cn;
8225 maybe_build_thisn_access_method (from);
8226 access0_wfl = build_wfl_node (access0_identifier_node);
8227 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8228 EXPR_WFL_LINECOL (access0_wfl) = lc;
8229 access = build_tree_list (NULL_TREE, access);
8230 access = build_method_invocation (access0_wfl, access);
8231 access = make_qualified_primary (cn, access, lc);
8234 /* if FROM isn't an inter class, that's fine, we've done
8235 enough. What we're looking for can be accessed from there. */
8236 from = DECL_CONTEXT (TYPE_NAME (from));
8237 if (!from)
8238 break;
8239 from = TREE_TYPE (from);
8241 return access;
8244 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8245 is returned if nothing needs to be generated. Otherwise, the method
8246 generated and a method decl is returned.
8248 NOTE: These generated methods should be declared in a class file
8249 attribute so that they can't be referred to directly. */
8251 static tree
8252 maybe_build_thisn_access_method (type)
8253 tree type;
8255 tree mdecl, args, stmt, rtype;
8256 tree saved_current_function_decl;
8258 /* If TYPE is a top-level class, no access method is required.
8259 If there already is such an access method, bail out. */
8260 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8261 return NULL_TREE;
8263 /* We generate the method. The method looks like:
8264 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8266 args = build_tree_list (inst_id, build_pointer_type (type));
8267 TREE_CHAIN (args) = end_params_node;
8268 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8269 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8270 access0_identifier_node, args);
8271 fix_method_argument_names (args, mdecl);
8272 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8273 stmt = build_current_thisn (type);
8274 stmt = make_qualified_primary (build_wfl_node (inst_id),
8275 build_wfl_node (stmt), 0);
8276 stmt = build_return (0, stmt);
8278 saved_current_function_decl = current_function_decl;
8279 start_artificial_method_body (mdecl);
8280 java_method_add_stmt (mdecl, stmt);
8281 end_artificial_method_body (mdecl);
8282 current_function_decl = saved_current_function_decl;
8284 CLASS_ACCESS0_GENERATED_P (type) = 1;
8286 return mdecl;
8289 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8290 the first level of innerclassing. this$1 for the next one, etc...
8291 This function can be invoked with TYPE to NULL, available and then
8292 has to count the parser context. */
8294 static tree
8295 build_current_thisn (type)
8296 tree type;
8298 static int saved_i = -1;
8299 static tree saved_thisn = NULL_TREE;
8300 static tree saved_type = NULL_TREE;
8301 static int saved_type_i = 0;
8302 static int initialized_p;
8303 tree decl;
8304 char buffer [80];
8305 int i = 0;
8307 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8308 if (!initialized_p)
8310 ggc_add_tree_root (&saved_thisn, 1);
8311 ggc_add_tree_root (&saved_type, 1);
8312 initialized_p = 1;
8315 if (type)
8317 if (type == saved_type)
8318 i = saved_type_i;
8319 else
8321 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8322 decl; decl = DECL_CONTEXT (decl), i++)
8325 saved_type = type;
8326 saved_type_i = i;
8329 else
8330 i = list_length (GET_CPC_LIST ())-2;
8332 if (i == saved_i)
8333 return saved_thisn;
8335 sprintf (buffer, "this$%d", i);
8336 saved_i = i;
8337 saved_thisn = get_identifier (buffer);
8338 return saved_thisn;
8341 /* Return the assignement to the hidden enclosing context `this$<n>'
8342 by the second incoming parameter to the innerclass constructor. The
8343 form used is `this.this$<n> = this$<n>;'. */
8345 static tree
8346 build_thisn_assign ()
8348 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8350 tree thisn = build_current_thisn (current_class);
8351 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8352 build_wfl_node (thisn), 0);
8353 tree rhs = build_wfl_node (thisn);
8354 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8355 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8357 return NULL_TREE;
8361 /* Building the synthetic `class$' used to implement the `.class' 1.1
8362 extension for non primitive types. This method looks like:
8364 static Class class$(String type) throws NoClassDefFoundError
8366 try {return (java.lang.Class.forName (String));}
8367 catch (ClassNotFoundException e) {
8368 throw new NoClassDefFoundError(e.getMessage());}
8369 } */
8371 static tree
8372 build_dot_class_method (class)
8373 tree class;
8375 #define BWF(S) build_wfl_node (get_identifier ((S)))
8376 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8377 tree args, tmp, saved_current_function_decl, mdecl;
8378 tree stmt, throw_stmt, catch, catch_block, try_block;
8379 tree catch_clause_param;
8380 tree class_not_found_exception, no_class_def_found_error;
8382 static tree get_message_wfl, type_parm_wfl;
8384 if (!get_message_wfl)
8386 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8387 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8388 ggc_add_tree_root (&get_message_wfl, 1);
8389 ggc_add_tree_root (&type_parm_wfl, 1);
8392 /* Build the arguments */
8393 args = build_tree_list (get_identifier ("type$"),
8394 build_pointer_type (string_type_node));
8395 TREE_CHAIN (args) = end_params_node;
8397 /* Build the qualified name java.lang.Class.forName */
8398 tmp = MQN (MQN (MQN (BWF ("java"),
8399 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8401 /* For things we have to catch and throw */
8402 class_not_found_exception =
8403 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8404 no_class_def_found_error =
8405 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8406 load_class (class_not_found_exception, 1);
8407 load_class (no_class_def_found_error, 1);
8409 /* Create the "class$" function */
8410 mdecl = create_artificial_method (class, ACC_STATIC,
8411 build_pointer_type (class_type_node),
8412 get_identifier ("class$"), args);
8413 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8414 no_class_def_found_error);
8416 /* We start by building the try block. We need to build:
8417 return (java.lang.Class.forName (type)); */
8418 stmt = build_method_invocation (tmp,
8419 build_tree_list (NULL_TREE, type_parm_wfl));
8420 stmt = build_return (0, stmt);
8421 /* Put it in a block. That's the try block */
8422 try_block = build_expr_block (stmt, NULL_TREE);
8424 /* Now onto the catch block. We start by building the expression
8425 throwing a new exception:
8426 throw new NoClassDefFoundError (_.getMessage); */
8427 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8428 get_message_wfl, 0);
8429 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8431 /* Build new NoClassDefFoundError (_.getMessage) */
8432 throw_stmt = build_new_invocation
8433 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8434 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8436 /* Build the throw, (it's too early to use BUILD_THROW) */
8437 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8439 /* Build the catch block to encapsulate all this. We begin by
8440 building an decl for the catch clause parameter and link it to
8441 newly created block, the catch block. */
8442 catch_clause_param =
8443 build_decl (VAR_DECL, wpv_id,
8444 build_pointer_type (class_not_found_exception));
8445 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8447 /* We initialize the variable with the exception handler. */
8448 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8449 soft_exceptioninfo_call_node);
8450 add_stmt_to_block (catch_block, NULL_TREE, catch);
8452 /* We add the statement throwing the new exception */
8453 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8455 /* Build a catch expression for all this */
8456 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8458 /* Build the try/catch sequence */
8459 stmt = build_try_statement (0, try_block, catch_block);
8461 fix_method_argument_names (args, mdecl);
8462 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8463 saved_current_function_decl = current_function_decl;
8464 start_artificial_method_body (mdecl);
8465 java_method_add_stmt (mdecl, stmt);
8466 end_artificial_method_body (mdecl);
8467 current_function_decl = saved_current_function_decl;
8468 TYPE_DOT_CLASS (class) = mdecl;
8470 return mdecl;
8473 static tree
8474 build_dot_class_method_invocation (type)
8475 tree type;
8477 tree sig_id, s;
8479 if (TYPE_ARRAY_P (type))
8480 sig_id = build_java_signature (type);
8481 else
8482 sig_id = DECL_NAME (TYPE_NAME (type));
8484 s = build_string (IDENTIFIER_LENGTH (sig_id),
8485 IDENTIFIER_POINTER (sig_id));
8486 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8487 build_tree_list (NULL_TREE, s));
8490 /* This section of the code deals with constructor. */
8492 /* Craft a body for default constructor. Patch existing constructor
8493 bodies with call to super() and field initialization statements if
8494 necessary. */
8496 static void
8497 fix_constructors (mdecl)
8498 tree mdecl;
8500 tree body = DECL_FUNCTION_BODY (mdecl);
8501 tree thisn_assign, compound = NULL_TREE;
8502 tree class_type = DECL_CONTEXT (mdecl);
8504 if (!body)
8506 /* It is an error for the compiler to generate a default
8507 constructor if the superclass doesn't have a constructor that
8508 takes no argument, or the same args for an anonymous class */
8509 if (verify_constructor_super (mdecl))
8511 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8512 tree save = DECL_NAME (mdecl);
8513 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8514 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8515 parse_error_context
8516 (lookup_cl (TYPE_NAME (class_type)),
8517 "No constructor matching `%s' found in class `%s'",
8518 lang_printable_name (mdecl, 0), n);
8519 DECL_NAME (mdecl) = save;
8522 /* The constructor body must be crafted by hand. It's the
8523 constructor we defined when we realize we didn't have the
8524 CLASSNAME() constructor */
8525 start_artificial_method_body (mdecl);
8527 /* Insert an assignment to the this$<n> hidden field, if
8528 necessary */
8529 if ((thisn_assign = build_thisn_assign ()))
8530 java_method_add_stmt (mdecl, thisn_assign);
8532 /* We don't generate a super constructor invocation if we're
8533 compiling java.lang.Object. build_super_invocation takes care
8534 of that. */
8535 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8537 /* Insert the instance initializer block right here, after the
8538 super invocation. */
8539 add_instance_initializer (mdecl);
8541 end_artificial_method_body (mdecl);
8543 /* Search for an explicit constructor invocation */
8544 else
8546 int found = 0;
8547 tree main_block = BLOCK_EXPR_BODY (body);
8549 while (body)
8550 switch (TREE_CODE (body))
8552 case CALL_EXPR:
8553 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8554 body = NULL_TREE;
8555 break;
8556 case COMPOUND_EXPR:
8557 case EXPR_WITH_FILE_LOCATION:
8558 body = TREE_OPERAND (body, 0);
8559 break;
8560 case BLOCK:
8561 body = BLOCK_EXPR_BODY (body);
8562 break;
8563 default:
8564 found = 0;
8565 body = NULL_TREE;
8567 /* The constructor is missing an invocation of super() */
8568 if (!found)
8569 compound = add_stmt_to_compound (compound, NULL_TREE,
8570 build_super_invocation (mdecl));
8572 /* Generate the assignment to this$<n>, if necessary */
8573 if ((thisn_assign = build_thisn_assign ()))
8574 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8576 /* Insert the instance initializer block right here, after the
8577 super invocation. */
8578 add_instance_initializer (mdecl);
8580 /* Fix the constructor main block if we're adding extra stmts */
8581 if (compound)
8583 compound = add_stmt_to_compound (compound, NULL_TREE,
8584 BLOCK_EXPR_BODY (main_block));
8585 BLOCK_EXPR_BODY (main_block) = compound;
8590 /* Browse constructors in the super class, searching for a constructor
8591 that doesn't take any argument. Return 0 if one is found, 1
8592 otherwise. If the current class is an anonymous inner class, look
8593 for something that has the same signature. */
8595 static int
8596 verify_constructor_super (mdecl)
8597 tree mdecl;
8599 tree class = CLASSTYPE_SUPER (current_class);
8600 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8601 tree sdecl;
8603 if (!class)
8604 return 0;
8606 if (ANONYMOUS_CLASS_P (current_class))
8608 tree mdecl_arg_type;
8609 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8610 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8611 if (DECL_CONSTRUCTOR_P (sdecl))
8613 tree m_arg_type;
8614 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8615 if (super_inner)
8616 arg_type = TREE_CHAIN (arg_type);
8617 for (m_arg_type = mdecl_arg_type;
8618 (arg_type != end_params_node
8619 && m_arg_type != end_params_node);
8620 arg_type = TREE_CHAIN (arg_type),
8621 m_arg_type = TREE_CHAIN (m_arg_type))
8622 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
8623 break;
8625 if (arg_type == end_params_node && m_arg_type == end_params_node)
8626 return 0;
8629 else
8631 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8633 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8634 if (super_inner)
8635 arg = TREE_CHAIN (arg);
8636 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8637 return 0;
8640 return 1;
8643 /* Generate code for all context remembered for code generation. */
8645 void
8646 java_expand_classes ()
8648 int save_error_count = 0;
8649 static struct parser_ctxt *saved_ctxp = NULL;
8651 java_parse_abort_on_error ();
8652 if (!(ctxp = ctxp_for_generation))
8653 return;
8654 java_layout_classes ();
8655 java_parse_abort_on_error ();
8657 saved_ctxp = ctxp_for_generation;
8658 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8660 ctxp = ctxp_for_generation;
8661 lang_init_source (2); /* Error msgs have method prototypes */
8662 java_complete_expand_classes (); /* Complete and expand classes */
8663 java_parse_abort_on_error ();
8666 /* Find anonymous classes and expand their constructor, now they
8667 have been fixed. */
8668 for (ctxp_for_generation = saved_ctxp;
8669 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8671 tree current;
8672 ctxp = ctxp_for_generation;
8673 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8675 current_class = TREE_TYPE (current);
8676 if (ANONYMOUS_CLASS_P (current_class))
8678 tree d;
8679 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8681 if (DECL_CONSTRUCTOR_P (d))
8683 restore_line_number_status (1);
8684 reset_method_name (d);
8685 java_complete_expand_method (d);
8686 restore_line_number_status (0);
8687 break; /* We now there are no other ones */
8694 /* If we've found error at that stage, don't try to generate
8695 anything, unless we're emitting xrefs or checking the syntax only
8696 (but not using -fsyntax-only for the purpose of generating
8697 bytecode. */
8698 if (java_error_count && !flag_emit_xref
8699 && (!flag_syntax_only && !flag_emit_class_files))
8700 return;
8702 /* Now things are stable, go for generation of the class data. */
8703 for (ctxp_for_generation = saved_ctxp;
8704 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8706 tree current;
8707 ctxp = ctxp_for_generation;
8708 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8710 current_class = TREE_TYPE (current);
8711 outgoing_cpool = TYPE_CPOOL (current_class);
8712 if (flag_emit_class_files)
8713 write_classfile (current_class);
8714 if (flag_emit_xref)
8715 expand_xref (current_class);
8716 else if (! flag_syntax_only)
8717 finish_class ();
8722 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8723 a tree list node containing RIGHT. Fore coming RIGHTs will be
8724 chained to this hook. LOCATION contains the location of the
8725 separating `.' operator. */
8727 static tree
8728 make_qualified_primary (primary, right, location)
8729 tree primary, right;
8730 int location;
8732 tree wfl;
8734 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8735 wfl = build_wfl_wrap (primary, location);
8736 else
8738 wfl = primary;
8739 /* If wfl wasn't qualified, we build a first anchor */
8740 if (!EXPR_WFL_QUALIFICATION (wfl))
8741 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8744 /* And chain them */
8745 EXPR_WFL_LINECOL (right) = location;
8746 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8747 PRIMARY_P (wfl) = 1;
8748 return wfl;
8751 /* Simple merge of two name separated by a `.' */
8753 static tree
8754 merge_qualified_name (left, right)
8755 tree left, right;
8757 tree node;
8758 if (!left && !right)
8759 return NULL_TREE;
8761 if (!left)
8762 return right;
8764 if (!right)
8765 return left;
8767 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8768 IDENTIFIER_LENGTH (left));
8769 obstack_1grow (&temporary_obstack, '.');
8770 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8771 IDENTIFIER_LENGTH (right));
8772 node = get_identifier (obstack_base (&temporary_obstack));
8773 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8774 QUALIFIED_P (node) = 1;
8775 return node;
8778 /* Merge the two parts of a qualified name into LEFT. Set the
8779 location information of the resulting node to LOCATION, usually
8780 inherited from the location information of the `.' operator. */
8782 static tree
8783 make_qualified_name (left, right, location)
8784 tree left, right;
8785 int location;
8787 #ifdef USE_COMPONENT_REF
8788 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8789 EXPR_WFL_LINECOL (node) = location;
8790 return node;
8791 #else
8792 tree left_id = EXPR_WFL_NODE (left);
8793 tree right_id = EXPR_WFL_NODE (right);
8794 tree wfl, merge;
8796 merge = merge_qualified_name (left_id, right_id);
8798 /* Left wasn't qualified and is now qualified */
8799 if (!QUALIFIED_P (left_id))
8801 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8802 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8803 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8806 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8807 EXPR_WFL_LINECOL (wfl) = location;
8808 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8810 EXPR_WFL_NODE (left) = merge;
8811 return left;
8812 #endif
8815 /* Extract the last identifier component of the qualified in WFL. The
8816 last identifier is removed from the linked list */
8818 static tree
8819 cut_identifier_in_qualified (wfl)
8820 tree wfl;
8822 tree q;
8823 tree previous = NULL_TREE;
8824 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8825 if (!TREE_CHAIN (q))
8827 if (!previous)
8828 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
8829 TREE_CHAIN (previous) = NULL_TREE;
8830 return TREE_PURPOSE (q);
8834 /* Resolve the expression name NAME. Return its decl. */
8836 static tree
8837 resolve_expression_name (id, orig)
8838 tree id;
8839 tree *orig;
8841 tree name = EXPR_WFL_NODE (id);
8842 tree decl;
8844 /* 6.5.5.1: Simple expression names */
8845 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8847 /* 15.13.1: NAME can appear within the scope of a local variable
8848 declaration */
8849 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8850 return decl;
8852 /* 15.13.1: NAME can appear within a class declaration */
8853 else
8855 decl = lookup_field_wrapper (current_class, name);
8856 if (decl)
8858 tree access = NULL_TREE;
8859 int fs = FIELD_STATIC (decl);
8861 /* If we're accessing an outer scope local alias, make
8862 sure we change the name of the field we're going to
8863 build access to. */
8864 if (FIELD_LOCAL_ALIAS_USED (decl))
8865 name = DECL_NAME (decl);
8867 /* Instance variable (8.3.1.1) can't appear within
8868 static method, static initializer or initializer for
8869 a static variable. */
8870 if (!fs && METHOD_STATIC (current_function_decl))
8872 static_ref_err (id, name, current_class);
8873 return error_mark_node;
8875 /* Instance variables can't appear as an argument of
8876 an explicit constructor invocation */
8877 if (!fs && ctxp->explicit_constructor_p
8878 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
8880 parse_error_context
8881 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
8882 return error_mark_node;
8885 /* If we're processing an inner class and we're trying
8886 to access a field belonging to an outer class, build
8887 the access to the field */
8888 if (!fs && outer_field_access_p (current_class, decl))
8890 if (CLASS_STATIC (TYPE_NAME (current_class)))
8892 static_ref_err (id, DECL_NAME (decl), current_class);
8893 return error_mark_node;
8895 return build_outer_field_access (id, decl);
8898 /* Otherwise build what it takes to access the field */
8899 access = build_field_ref ((fs ? NULL_TREE : current_this),
8900 DECL_CONTEXT (decl), name);
8901 if (fs && !flag_emit_class_files && !flag_emit_xref)
8902 access = build_class_init (DECL_CONTEXT (access), access);
8903 /* We may be asked to save the real field access node */
8904 if (orig)
8905 *orig = access;
8906 /* And we return what we got */
8907 return access;
8909 /* Fall down to error report on undefined variable */
8912 /* 6.5.5.2 Qualified Expression Names */
8913 else
8915 if (orig)
8916 *orig = NULL_TREE;
8917 qualify_ambiguous_name (id);
8918 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8919 /* 15.10.2: Accessing Superclass Members using super */
8920 return resolve_field_access (id, orig, NULL);
8923 /* We've got an error here */
8924 parse_error_context (id, "Undefined variable `%s'",
8925 IDENTIFIER_POINTER (name));
8927 return error_mark_node;
8930 static void
8931 static_ref_err (wfl, field_id, class_type)
8932 tree wfl, field_id, class_type;
8934 parse_error_context
8935 (wfl,
8936 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8937 IDENTIFIER_POINTER (field_id),
8938 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8941 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8942 We return something suitable to generate the field access. We also
8943 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8944 recipient's address can be null. */
8946 static tree
8947 resolve_field_access (qual_wfl, field_decl, field_type)
8948 tree qual_wfl;
8949 tree *field_decl, *field_type;
8951 int is_static = 0;
8952 tree field_ref;
8953 tree decl, where_found, type_found;
8955 if (resolve_qualified_expression_name (qual_wfl, &decl,
8956 &where_found, &type_found))
8957 return error_mark_node;
8959 /* Resolve the LENGTH field of an array here */
8960 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
8961 && type_found && TYPE_ARRAY_P (type_found)
8962 && ! flag_emit_class_files && ! flag_emit_xref)
8964 tree length = build_java_array_length_access (where_found);
8965 field_ref =
8966 build_java_arraynull_check (type_found, length, int_type_node);
8968 /* In case we're dealing with a static array, we need to
8969 initialize its class before the array length can be fetched.
8970 It's also a good time to create a DECL_RTL for the field if
8971 none already exists, otherwise if the field was declared in a
8972 class found in an external file and hasn't been (and won't
8973 be) accessed for its value, none will be created. */
8974 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8976 build_static_field_ref (where_found);
8977 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8980 /* We might have been trying to resolve field.method(). In which
8981 case, the resolution is over and decl is the answer */
8982 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
8983 field_ref = decl;
8984 else if (JDECL_P (decl))
8986 int static_final_found = 0;
8987 if (!type_found)
8988 type_found = DECL_CONTEXT (decl);
8989 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
8990 if (CLASS_FINAL_VARIABLE_P (decl)
8991 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
8992 && DECL_INITIAL (decl))
8994 /* When called on a FIELD_DECL of the right (primitive)
8995 type, java_complete_tree will try to substitue the decl
8996 for it's initial value. */
8997 field_ref = java_complete_tree (decl);
8998 static_final_found = 1;
9000 else
9001 field_ref = build_field_ref ((is_static && !flag_emit_xref?
9002 NULL_TREE : where_found),
9003 type_found, DECL_NAME (decl));
9004 if (field_ref == error_mark_node)
9005 return error_mark_node;
9006 if (is_static && !static_final_found
9007 && !flag_emit_class_files && !flag_emit_xref)
9008 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
9010 else
9011 field_ref = decl;
9013 if (field_decl)
9014 *field_decl = decl;
9015 if (field_type)
9016 *field_type = (QUAL_DECL_TYPE (decl) ?
9017 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9018 return field_ref;
9021 /* If NODE is an access to f static field, strip out the class
9022 initialization part and return the field decl, otherwise, return
9023 NODE. */
9025 static tree
9026 strip_out_static_field_access_decl (node)
9027 tree node;
9029 if (TREE_CODE (node) == COMPOUND_EXPR)
9031 tree op1 = TREE_OPERAND (node, 1);
9032 if (TREE_CODE (op1) == COMPOUND_EXPR)
9034 tree call = TREE_OPERAND (op1, 0);
9035 if (TREE_CODE (call) == CALL_EXPR
9036 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9037 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9038 == soft_initclass_node)
9039 return TREE_OPERAND (op1, 1);
9041 else if (JDECL_P (op1))
9042 return op1;
9044 return node;
9047 /* 6.5.5.2: Qualified Expression Names */
9049 static int
9050 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9051 tree wfl;
9052 tree *found_decl, *type_found, *where_found;
9054 int from_type = 0; /* Field search initiated from a type */
9055 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9056 int previous_call_static = 0;
9057 int is_static;
9058 tree decl = NULL_TREE, type = NULL_TREE, q;
9059 /* For certain for of inner class instantiation */
9060 tree saved_current, saved_this;
9061 #define RESTORE_THIS_AND_CURRENT_CLASS \
9062 { current_class = saved_current; current_this = saved_this;}
9064 *type_found = *where_found = NULL_TREE;
9066 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9068 tree qual_wfl = QUAL_WFL (q);
9069 tree ret_decl; /* for EH checking */
9070 int location; /* for EH checking */
9072 /* 15.10.1 Field Access Using a Primary */
9073 switch (TREE_CODE (qual_wfl))
9075 case CALL_EXPR:
9076 case NEW_CLASS_EXPR:
9077 /* If the access to the function call is a non static field,
9078 build the code to access it. */
9079 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9081 decl = maybe_access_field (decl, *where_found,
9082 DECL_CONTEXT (decl));
9083 if (decl == error_mark_node)
9084 return 1;
9087 /* And code for the function call */
9088 if (complete_function_arguments (qual_wfl))
9089 return 1;
9091 /* We might have to setup a new current class and a new this
9092 for the search of an inner class, relative to the type of
9093 a expression resolved as `decl'. The current values are
9094 saved and restored shortly after */
9095 saved_current = current_class;
9096 saved_this = current_this;
9097 if (decl
9098 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9099 || from_qualified_this))
9101 /* If we still have `from_qualified_this', we have the form
9102 <T>.this.f() and we need to build <T>.this */
9103 if (from_qualified_this)
9105 decl = build_access_to_thisn (current_class, type, 0);
9106 decl = java_complete_tree (decl);
9107 type = TREE_TYPE (TREE_TYPE (decl));
9109 current_class = type;
9110 current_this = decl;
9111 from_qualified_this = 0;
9114 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9115 CALL_USING_SUPER (qual_wfl) = 1;
9116 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9117 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9118 *where_found = patch_method_invocation (qual_wfl, decl, type,
9119 &is_static, &ret_decl);
9120 if (*where_found == error_mark_node)
9122 RESTORE_THIS_AND_CURRENT_CLASS;
9123 return 1;
9125 *type_found = type = QUAL_DECL_TYPE (*where_found);
9127 /* If we're creating an inner class instance, check for that
9128 an enclosing instance is in scope */
9129 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9130 && INNER_ENCLOSING_SCOPE_CHECK (type))
9132 parse_error_context
9133 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9134 lang_printable_name (type, 0),
9135 (!current_this ? "" :
9136 "; an explicit one must be provided when creating this inner class"));
9137 RESTORE_THIS_AND_CURRENT_CLASS;
9138 return 1;
9141 /* In case we had to change then to resolve a inner class
9142 instantiation using a primary qualified by a `new' */
9143 RESTORE_THIS_AND_CURRENT_CLASS;
9145 /* EH check. No check on access$<n> functions */
9146 if (location
9147 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9148 (DECL_NAME (current_function_decl)))
9149 check_thrown_exceptions (location, ret_decl);
9151 /* If the previous call was static and this one is too,
9152 build a compound expression to hold the two (because in
9153 that case, previous function calls aren't transported as
9154 forcoming function's argument. */
9155 if (previous_call_static && is_static)
9157 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9158 TREE_SIDE_EFFECTS (decl) = 1;
9160 else
9162 previous_call_static = is_static;
9163 decl = *where_found;
9165 from_type = 0;
9166 continue;
9168 case NEW_ARRAY_EXPR:
9169 case NEW_ANONYMOUS_ARRAY_EXPR:
9170 *where_found = decl = java_complete_tree (qual_wfl);
9171 if (decl == error_mark_node)
9172 return 1;
9173 *type_found = type = QUAL_DECL_TYPE (decl);
9174 CLASS_LOADED_P (type) = 1;
9175 continue;
9177 case CONVERT_EXPR:
9178 *where_found = decl = java_complete_tree (qual_wfl);
9179 if (decl == error_mark_node)
9180 return 1;
9181 *type_found = type = QUAL_DECL_TYPE (decl);
9182 from_cast = 1;
9183 continue;
9185 case CONDITIONAL_EXPR:
9186 case STRING_CST:
9187 case MODIFY_EXPR:
9188 *where_found = decl = java_complete_tree (qual_wfl);
9189 if (decl == error_mark_node)
9190 return 1;
9191 *type_found = type = QUAL_DECL_TYPE (decl);
9192 continue;
9194 case ARRAY_REF:
9195 /* If the access to the function call is a non static field,
9196 build the code to access it. */
9197 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9199 decl = maybe_access_field (decl, *where_found, type);
9200 if (decl == error_mark_node)
9201 return 1;
9203 /* And code for the array reference expression */
9204 decl = java_complete_tree (qual_wfl);
9205 if (decl == error_mark_node)
9206 return 1;
9207 type = QUAL_DECL_TYPE (decl);
9208 continue;
9210 case PLUS_EXPR:
9211 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9212 return 1;
9213 if ((type = patch_string (decl)))
9214 decl = type;
9215 *where_found = QUAL_RESOLUTION (q) = decl;
9216 *type_found = type = TREE_TYPE (decl);
9217 break;
9219 case CLASS_LITERAL:
9220 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9221 return 1;
9222 *where_found = QUAL_RESOLUTION (q) = decl;
9223 *type_found = type = TREE_TYPE (decl);
9224 break;
9226 default:
9227 /* Fix for -Wall Just go to the next statement. Don't
9228 continue */
9229 break;
9232 /* If we fall here, we weren't processing a (static) function call. */
9233 previous_call_static = 0;
9235 /* It can be the keyword THIS */
9236 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9238 if (!current_this)
9240 parse_error_context
9241 (wfl, "Keyword `this' used outside allowed context");
9242 return 1;
9244 if (ctxp->explicit_constructor_p
9245 && type == current_class)
9247 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9248 return 1;
9250 /* We have to generate code for intermediate acess */
9251 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9253 *where_found = decl = current_this;
9254 *type_found = type = QUAL_DECL_TYPE (decl);
9256 /* We're trying to access the this from somewhere else. Make sure
9257 it's allowed before doing so. */
9258 else
9260 if (!enclosing_context_p (type, current_class))
9262 char *p = xstrdup (lang_printable_name (type, 0));
9263 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9264 p, p,
9265 lang_printable_name (current_class, 0));
9266 free (p);
9267 return 1;
9269 from_qualified_this = 1;
9270 /* If there's nothing else after that, we need to
9271 produce something now, otherwise, the section of the
9272 code that needs to produce <T>.this will generate
9273 what is necessary. */
9274 if (!TREE_CHAIN (q))
9276 decl = build_access_to_thisn (current_class, type, 0);
9277 *where_found = decl = java_complete_tree (decl);
9278 *type_found = type = TREE_TYPE (decl);
9282 from_type = 0;
9283 continue;
9286 /* 15.10.2 Accessing Superclass Members using SUPER */
9287 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9289 tree node;
9290 /* Check on the restricted use of SUPER */
9291 if (METHOD_STATIC (current_function_decl)
9292 || current_class == object_type_node)
9294 parse_error_context
9295 (wfl, "Keyword `super' used outside allowed context");
9296 return 1;
9298 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9299 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9300 CLASSTYPE_SUPER (current_class),
9301 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9302 *where_found = decl = java_complete_tree (node);
9303 if (decl == error_mark_node)
9304 return 1;
9305 *type_found = type = QUAL_DECL_TYPE (decl);
9306 from_super = from_type = 1;
9307 continue;
9310 /* 15.13.1: Can't search for field name in packages, so we
9311 assume a variable/class name was meant. */
9312 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9314 tree name = resolve_package (wfl, &q);
9315 if (name)
9317 tree list;
9318 *where_found = decl = resolve_no_layout (name, qual_wfl);
9319 /* We want to be absolutely sure that the class is laid
9320 out. We're going to search something inside it. */
9321 *type_found = type = TREE_TYPE (decl);
9322 layout_class (type);
9323 from_type = 1;
9325 /* Fix them all the way down, if any are left. */
9326 if (q)
9328 list = TREE_CHAIN (q);
9329 while (list)
9331 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9332 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9333 list = TREE_CHAIN (list);
9337 else
9339 if (from_super || from_cast)
9340 parse_error_context
9341 ((from_cast ? qual_wfl : wfl),
9342 "No variable `%s' defined in class `%s'",
9343 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9344 lang_printable_name (type, 0));
9345 else
9346 parse_error_context
9347 (qual_wfl, "Undefined variable or class name: `%s'",
9348 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9349 return 1;
9353 /* We have a type name. It's been already resolved when the
9354 expression was qualified. */
9355 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9357 if (!(decl = QUAL_RESOLUTION (q)))
9358 return 1; /* Error reported already */
9360 /* Sneak preview. If next we see a `new', we're facing a
9361 qualification with resulted in a type being selected
9362 instead of a field. Report the error */
9363 if(TREE_CHAIN (q)
9364 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9366 parse_error_context (qual_wfl, "Undefined variable `%s'",
9367 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9368 return 1;
9371 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9373 parse_error_context
9374 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9375 java_accstring_lookup (get_access_flags_from_decl (decl)),
9376 GET_TYPE_NAME (type),
9377 IDENTIFIER_POINTER (DECL_NAME (decl)),
9378 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9379 return 1;
9381 check_deprecation (qual_wfl, decl);
9383 type = TREE_TYPE (decl);
9384 from_type = 1;
9386 /* We resolve and expression name */
9387 else
9389 tree field_decl = NULL_TREE;
9391 /* If there exists an early resolution, use it. That occurs
9392 only once and we know that there are more things to
9393 come. Don't do that when processing something after SUPER
9394 (we need more thing to be put in place below */
9395 if (!from_super && QUAL_RESOLUTION (q))
9397 decl = QUAL_RESOLUTION (q);
9398 if (!type)
9400 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9402 if (current_this)
9403 *where_found = current_this;
9404 else
9406 static_ref_err (qual_wfl, DECL_NAME (decl),
9407 current_class);
9408 return 1;
9410 if (outer_field_access_p (current_class, decl))
9411 decl = build_outer_field_access (qual_wfl, decl);
9413 else
9415 *where_found = TREE_TYPE (decl);
9416 if (TREE_CODE (*where_found) == POINTER_TYPE)
9417 *where_found = TREE_TYPE (*where_found);
9422 /* We have to search for a field, knowing the type of its
9423 container. The flag FROM_TYPE indicates that we resolved
9424 the last member of the expression as a type name, which
9425 means that for the resolution of this field, we'll look
9426 for other errors than if it was resolved as a member of
9427 an other field. */
9428 else
9430 int is_static;
9431 tree field_decl_type; /* For layout */
9433 if (!from_type && !JREFERENCE_TYPE_P (type))
9435 parse_error_context
9436 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9437 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9438 lang_printable_name (type, 0),
9439 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9440 return 1;
9443 field_decl = lookup_field_wrapper (type,
9444 EXPR_WFL_NODE (qual_wfl));
9446 /* Maybe what we're trying to access an inner class. */
9447 if (!field_decl)
9449 tree ptr, inner_decl;
9451 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9452 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9453 if (inner_decl)
9455 check_inner_class_access (inner_decl, decl, qual_wfl);
9456 type = TREE_TYPE (inner_decl);
9457 decl = inner_decl;
9458 from_type = 1;
9459 continue;
9463 if (field_decl == NULL_TREE)
9465 parse_error_context
9466 (qual_wfl, "No variable `%s' defined in type `%s'",
9467 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9468 GET_TYPE_NAME (type));
9469 return 1;
9471 if (field_decl == error_mark_node)
9472 return 1;
9474 /* Layout the type of field_decl, since we may need
9475 it. Don't do primitive types or loaded classes. The
9476 situation of non primitive arrays may not handled
9477 properly here. FIXME */
9478 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9479 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9480 else
9481 field_decl_type = TREE_TYPE (field_decl);
9482 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9483 && !CLASS_LOADED_P (field_decl_type)
9484 && !TYPE_ARRAY_P (field_decl_type))
9485 resolve_and_layout (field_decl_type, NULL_TREE);
9486 if (TYPE_ARRAY_P (field_decl_type))
9487 CLASS_LOADED_P (field_decl_type) = 1;
9489 /* Check on accessibility here */
9490 if (not_accessible_p (type, field_decl, from_super))
9492 parse_error_context
9493 (qual_wfl,
9494 "Can't access %s field `%s.%s' from `%s'",
9495 java_accstring_lookup
9496 (get_access_flags_from_decl (field_decl)),
9497 GET_TYPE_NAME (type),
9498 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9499 IDENTIFIER_POINTER
9500 (DECL_NAME (TYPE_NAME (current_class))));
9501 return 1;
9503 check_deprecation (qual_wfl, field_decl);
9505 /* There are things to check when fields are accessed
9506 from type. There are no restrictions on a static
9507 declaration of the field when it is accessed from an
9508 interface */
9509 is_static = FIELD_STATIC (field_decl);
9510 if (!from_super && from_type
9511 && !TYPE_INTERFACE_P (type)
9512 && !is_static
9513 && (current_function_decl
9514 && METHOD_STATIC (current_function_decl)))
9516 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9517 return 1;
9519 from_cast = from_super = 0;
9521 /* It's an access from a type but it isn't static, we
9522 make it relative to `this'. */
9523 if (!is_static && from_type)
9524 decl = current_this;
9526 /* If we need to generate something to get a proper
9527 handle on what this field is accessed from, do it
9528 now. */
9529 if (!is_static)
9531 decl = maybe_access_field (decl, *where_found, *type_found);
9532 if (decl == error_mark_node)
9533 return 1;
9536 /* We want to keep the location were found it, and the type
9537 we found. */
9538 *where_found = decl;
9539 *type_found = type;
9541 /* Generate the correct expression for field access from
9542 qualified this */
9543 if (from_qualified_this)
9545 field_decl = build_outer_field_access (qual_wfl, field_decl);
9546 from_qualified_this = 0;
9549 /* This is the decl found and eventually the next one to
9550 search from */
9551 decl = field_decl;
9553 from_type = 0;
9554 type = QUAL_DECL_TYPE (decl);
9556 /* Sneak preview. If decl is qualified by a `new', report
9557 the error here to be accurate on the peculiar construct */
9558 if (TREE_CHAIN (q)
9559 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9560 && !JREFERENCE_TYPE_P (type))
9562 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9563 lang_printable_name (type, 0));
9564 return 1;
9567 /* `q' might have changed due to a after package resolution
9568 re-qualification */
9569 if (!q)
9570 break;
9572 *found_decl = decl;
9573 return 0;
9576 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9577 can't be accessed from REFERENCE (a record type). This should be
9578 used when decl is a field or a method.*/
9580 static int
9581 not_accessible_p (reference, member, from_super)
9582 tree reference, member;
9583 int from_super;
9585 int access_flag = get_access_flags_from_decl (member);
9587 /* Inner classes are processed by check_inner_class_access */
9588 if (INNER_CLASS_TYPE_P (reference))
9589 return 0;
9591 /* Access always granted for members declared public */
9592 if (access_flag & ACC_PUBLIC)
9593 return 0;
9595 /* Check access on protected members */
9596 if (access_flag & ACC_PROTECTED)
9598 /* Access granted if it occurs from within the package
9599 containing the class in which the protected member is
9600 declared */
9601 if (class_in_current_package (DECL_CONTEXT (member)))
9602 return 0;
9604 /* If accessed with the form `super.member', then access is granted */
9605 if (from_super)
9606 return 0;
9608 /* Otherwise, access is granted if occuring from the class where
9609 member is declared or a subclass of it. Find the right
9610 context to perform the check */
9611 if (PURE_INNER_CLASS_TYPE_P (reference))
9613 while (INNER_CLASS_TYPE_P (reference))
9615 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9616 return 0;
9617 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9620 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9621 return 0;
9622 return 1;
9625 /* Check access on private members. Access is granted only if it
9626 occurs from within the class in which it is declared -- that does
9627 it for innerclasses too. */
9628 if (access_flag & ACC_PRIVATE)
9630 if (reference == DECL_CONTEXT (member))
9631 return 0;
9632 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9633 return 0;
9634 return 1;
9637 /* Default access are permitted only when occuring within the
9638 package in which the type (REFERENCE) is declared. In other words,
9639 REFERENCE is defined in the current package */
9640 if (ctxp->package)
9641 return !class_in_current_package (reference);
9643 /* Otherwise, access is granted */
9644 return 0;
9647 /* Test deprecated decl access. */
9648 static void
9649 check_deprecation (wfl, decl)
9650 tree wfl, decl;
9652 const char *file = DECL_SOURCE_FILE (decl);
9653 /* Complain if the field is deprecated and the file it was defined
9654 in isn't compiled at the same time the file which contains its
9655 use is */
9656 if (DECL_DEPRECATED (decl)
9657 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9659 char the [20];
9660 switch (TREE_CODE (decl))
9662 case FUNCTION_DECL:
9663 strcpy (the, "method");
9664 break;
9665 case FIELD_DECL:
9666 strcpy (the, "field");
9667 break;
9668 case TYPE_DECL:
9669 strcpy (the, "class");
9670 break;
9671 default:
9672 fatal ("unexpected DECL code - check_deprecation");
9674 parse_warning_context
9675 (wfl, "The %s `%s' in class `%s' has been deprecated",
9676 the, lang_printable_name (decl, 0),
9677 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9681 /* Returns 1 if class was declared in the current package, 0 otherwise */
9683 static int
9684 class_in_current_package (class)
9685 tree class;
9687 static tree cache = NULL_TREE;
9688 int qualified_flag;
9689 tree left;
9691 if (cache == class)
9692 return 1;
9694 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9696 /* If the current package is empty and the name of CLASS is
9697 qualified, class isn't in the current package. If there is a
9698 current package and the name of the CLASS is not qualified, class
9699 isn't in the current package */
9700 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9701 return 0;
9703 /* If there is not package and the name of CLASS isn't qualified,
9704 they belong to the same unnamed package */
9705 if (!ctxp->package && !qualified_flag)
9706 return 1;
9708 /* Compare the left part of the name of CLASS with the package name */
9709 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9710 if (ctxp->package == left)
9712 static int initialized_p;
9713 /* Register CACHE with the garbage collector. */
9714 if (!initialized_p)
9716 ggc_add_tree_root (&cache, 1);
9717 initialized_p = 1;
9720 cache = class;
9721 return 1;
9723 return 0;
9726 /* This function may generate code to access DECL from WHERE. This is
9727 done only if certain conditions meet. */
9729 static tree
9730 maybe_access_field (decl, where, type)
9731 tree decl, where, type;
9733 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9734 && !FIELD_STATIC (decl))
9735 decl = build_field_ref (where ? where : current_this,
9736 (type ? type : DECL_CONTEXT (decl)),
9737 DECL_NAME (decl));
9738 return decl;
9741 /* Build a method invocation, by patching PATCH. If non NULL
9742 and according to the situation, PRIMARY and WHERE may be
9743 used. IS_STATIC is set to 1 if the invoked function is static. */
9745 static tree
9746 patch_method_invocation (patch, primary, where, is_static, ret_decl)
9747 tree patch, primary, where;
9748 int *is_static;
9749 tree *ret_decl;
9751 tree wfl = TREE_OPERAND (patch, 0);
9752 tree args = TREE_OPERAND (patch, 1);
9753 tree name = EXPR_WFL_NODE (wfl);
9754 tree list;
9755 int is_static_flag = 0;
9756 int is_super_init = 0;
9757 tree this_arg = NULL_TREE;
9759 /* Should be overriden if everything goes well. Otherwise, if
9760 something fails, it should keep this value. It stop the
9761 evaluation of a bogus assignment. See java_complete_tree,
9762 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9763 evaluating an assignment */
9764 TREE_TYPE (patch) = error_mark_node;
9766 /* Since lookup functions are messing with line numbers, save the
9767 context now. */
9768 java_parser_context_save_global ();
9770 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9772 /* Resolution of qualified name, excluding constructors */
9773 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9775 tree identifier, identifier_wfl, type, resolved;
9776 /* Extract the last IDENTIFIER of the qualified
9777 expression. This is a wfl and we will use it's location
9778 data during error report. */
9779 identifier_wfl = cut_identifier_in_qualified (wfl);
9780 identifier = EXPR_WFL_NODE (identifier_wfl);
9782 /* Given the context, IDENTIFIER is syntactically qualified
9783 as a MethodName. We need to qualify what's before */
9784 qualify_ambiguous_name (wfl);
9785 resolved = resolve_field_access (wfl, NULL, NULL);
9787 if (resolved == error_mark_node)
9788 PATCH_METHOD_RETURN_ERROR ();
9790 type = GET_SKIP_TYPE (resolved);
9791 resolve_and_layout (type, NULL_TREE);
9793 if (JPRIMITIVE_TYPE_P (type))
9795 parse_error_context
9796 (identifier_wfl,
9797 "Can't invoke a method on primitive type `%s'",
9798 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9799 PATCH_METHOD_RETURN_ERROR ();
9802 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9803 args = nreverse (args);
9805 /* We're resolving a call from a type */
9806 if (TREE_CODE (resolved) == TYPE_DECL)
9808 if (CLASS_INTERFACE (resolved))
9810 parse_error_context
9811 (identifier_wfl,
9812 "Can't make static reference to method `%s' in interface `%s'",
9813 IDENTIFIER_POINTER (identifier),
9814 IDENTIFIER_POINTER (name));
9815 PATCH_METHOD_RETURN_ERROR ();
9817 if (list && !METHOD_STATIC (list))
9819 char *fct_name = xstrdup (lang_printable_name (list, 0));
9820 parse_error_context
9821 (identifier_wfl,
9822 "Can't make static reference to method `%s %s' in class `%s'",
9823 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9824 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9825 free (fct_name);
9826 PATCH_METHOD_RETURN_ERROR ();
9829 else
9830 this_arg = primary = resolved;
9832 /* IDENTIFIER_WFL will be used to report any problem further */
9833 wfl = identifier_wfl;
9835 /* Resolution of simple names, names generated after a primary: or
9836 constructors */
9837 else
9839 tree class_to_search = NULL_TREE;
9840 int lc; /* Looking for Constructor */
9842 /* We search constructor in their target class */
9843 if (CALL_CONSTRUCTOR_P (patch))
9845 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9846 class_to_search = EXPR_WFL_NODE (wfl);
9847 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9848 this_identifier_node)
9849 class_to_search = NULL_TREE;
9850 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9851 super_identifier_node)
9853 is_super_init = 1;
9854 if (CLASSTYPE_SUPER (current_class))
9855 class_to_search =
9856 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9857 else
9859 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
9860 PATCH_METHOD_RETURN_ERROR ();
9864 /* Class to search is NULL if we're searching the current one */
9865 if (class_to_search)
9867 class_to_search = resolve_and_layout (class_to_search, wfl);
9869 if (!class_to_search)
9871 parse_error_context
9872 (wfl, "Class `%s' not found in type declaration",
9873 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9874 PATCH_METHOD_RETURN_ERROR ();
9877 /* Can't instantiate an abstract class, but we can
9878 invoke it's constructor. It's use within the `new'
9879 context is denied here. */
9880 if (CLASS_ABSTRACT (class_to_search)
9881 && TREE_CODE (patch) == NEW_CLASS_EXPR)
9883 parse_error_context
9884 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9885 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9886 PATCH_METHOD_RETURN_ERROR ();
9889 class_to_search = TREE_TYPE (class_to_search);
9891 else
9892 class_to_search = current_class;
9893 lc = 1;
9895 /* This is a regular search in the local class, unless an
9896 alternate class is specified. */
9897 else
9899 class_to_search = (where ? where : current_class);
9900 lc = 0;
9903 /* NAME is a simple identifier or comes from a primary. Search
9904 in the class whose declaration contain the method being
9905 invoked. */
9906 resolve_and_layout (class_to_search, NULL_TREE);
9908 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
9909 /* Don't continue if no method were found, as the next statement
9910 can't be executed then. */
9911 if (!list)
9912 PATCH_METHOD_RETURN_ERROR ();
9914 /* Check for static reference if non static methods */
9915 if (check_for_static_method_reference (wfl, patch, list,
9916 class_to_search, primary))
9917 PATCH_METHOD_RETURN_ERROR ();
9919 /* Check for inner classes creation from illegal contexts */
9920 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9921 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9922 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9924 parse_error_context
9925 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9926 lang_printable_name (class_to_search, 0),
9927 (!current_this ? "" :
9928 "; an explicit one must be provided when creating this inner class"));
9929 PATCH_METHOD_RETURN_ERROR ();
9932 /* Non static methods are called with the current object extra
9933 argument. If patch a `new TYPE()', the argument is the value
9934 returned by the object allocator. If method is resolved as a
9935 primary, use the primary otherwise use the current THIS. */
9936 args = nreverse (args);
9937 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
9939 this_arg = primary ? primary : current_this;
9941 /* If we're using an access method, things are different.
9942 There are two familly of cases:
9944 1) We're not generating bytecodes:
9946 - LIST is non static. It's invocation is transformed from
9947 x(a1,...,an) into this$<n>.x(a1,....an).
9948 - LIST is static. It's invocation is transformed from
9949 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9951 2) We're generating bytecodes:
9953 - LIST is non static. It's invocation is transformed from
9954 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9955 - LIST is static. It's invocation is transformed from
9956 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9958 Of course, this$<n> can be abitrary complex, ranging from
9959 this$0 (the immediate outer context) to
9960 access$0(access$0(...(this$0))).
9962 maybe_use_access_method returns a non zero value if the
9963 this_arg has to be moved into the (then generated) stub
9964 argument list. In the meantime, the selected function
9965 might have be replaced by a generated stub. */
9966 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9968 args = tree_cons (NULL_TREE, this_arg, args);
9969 this_arg = NULL_TREE; /* So it doesn't get chained twice */
9974 /* Merge point of all resolution schemes. If we have nothing, this
9975 is an error, already signaled */
9976 if (!list)
9977 PATCH_METHOD_RETURN_ERROR ();
9979 /* Check accessibility, position the is_static flag, build and
9980 return the call */
9981 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
9983 char *fct_name = xstrdup (lang_printable_name (list, 0));
9984 int ctor_p = DECL_CONSTRUCTOR_P (list);
9985 parse_error_context
9986 (wfl, "Can't access %s %s `%s%s.%s' from `%s'",
9987 java_accstring_lookup (get_access_flags_from_decl (list)),
9988 (ctor_p ? "constructor" : "method"),
9989 (ctor_p ?
9990 "" : lang_printable_name_wls (TREE_TYPE (TREE_TYPE (list)), 0)),
9991 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9992 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9993 free (fct_name);
9994 PATCH_METHOD_RETURN_ERROR ();
9996 check_deprecation (wfl, list);
9998 /* If invoking a innerclass constructor, there are hidden parameters
9999 to pass */
10000 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10001 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10003 /* And make sure we add the accessed local variables to be saved
10004 in field aliases. */
10005 args = build_alias_initializer_parameter_list
10006 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10008 /* Secretly pass the current_this/primary as a second argument */
10009 if (primary || current_this)
10011 tree extra_arg;
10012 tree this_type = (current_this ?
10013 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10014 /* Method's (list) enclosing context */
10015 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10016 /* If we have a primary, use it. */
10017 if (primary)
10018 extra_arg = primary;
10019 /* The current `this' is an inner class but isn't a direct
10020 enclosing context for the inner class we're trying to
10021 create. Build an access to the proper enclosing context
10022 and use it. */
10023 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10024 && this_type != TREE_TYPE (mec))
10027 extra_arg = build_access_to_thisn (current_class,
10028 TREE_TYPE (mec), 0);
10029 extra_arg = java_complete_tree (extra_arg);
10031 /* Otherwise, just use the current `this' as an enclosing
10032 context. */
10033 else
10034 extra_arg = current_this;
10035 args = tree_cons (NULL_TREE, extra_arg, args);
10037 else
10038 args = tree_cons (NULL_TREE, integer_zero_node, args);
10041 /* This handles the situation where a constructor invocation needs
10042 to have an enclosing context passed as a second parameter (the
10043 constructor is one of an inner class. We extract it from the
10044 current function. */
10045 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10047 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10048 tree extra_arg;
10050 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10052 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10053 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10055 else
10057 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10058 extra_arg =
10059 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10060 extra_arg = java_complete_tree (extra_arg);
10062 args = tree_cons (NULL_TREE, extra_arg, args);
10065 is_static_flag = METHOD_STATIC (list);
10066 if (! is_static_flag && this_arg != NULL_TREE)
10067 args = tree_cons (NULL_TREE, this_arg, args);
10069 /* In the context of an explicit constructor invocation, we can't
10070 invoke any method relying on `this'. Exceptions are: we're
10071 invoking a static function, primary exists and is not the current
10072 this, we're creating a new object. */
10073 if (ctxp->explicit_constructor_p
10074 && !is_static_flag
10075 && (!primary || primary == current_this)
10076 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10078 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10079 PATCH_METHOD_RETURN_ERROR ();
10081 java_parser_context_restore_global ();
10082 if (is_static)
10083 *is_static = is_static_flag;
10084 /* Sometimes, we want the decl of the selected method. Such as for
10085 EH checking */
10086 if (ret_decl)
10087 *ret_decl = list;
10088 patch = patch_invoke (patch, list, args);
10089 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10091 tree finit_parms, finit_call;
10093 /* Prepare to pass hidden parameters to finit$, if any. */
10094 finit_parms = build_alias_initializer_parameter_list
10095 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10097 finit_call =
10098 build_method_invocation (build_wfl_node (finit_identifier_node),
10099 finit_parms);
10101 /* Generate the code used to initialize fields declared with an
10102 initialization statement and build a compound statement along
10103 with the super constructor invocation. */
10104 patch = build (COMPOUND_EXPR, void_type_node, patch,
10105 java_complete_tree (finit_call));
10106 CAN_COMPLETE_NORMALLY (patch) = 1;
10108 return patch;
10111 /* Check that we're not trying to do a static reference to a method in
10112 non static method. Return 1 if it's the case, 0 otherwise. */
10114 static int
10115 check_for_static_method_reference (wfl, node, method, where, primary)
10116 tree wfl, node, method, where, primary;
10118 if (METHOD_STATIC (current_function_decl)
10119 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10121 char *fct_name = xstrdup (lang_printable_name (method, 0));
10122 parse_error_context
10123 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10124 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10125 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10126 free (fct_name);
10127 return 1;
10129 return 0;
10132 /* Fix the invocation of *MDECL if necessary in the case of a
10133 invocation from an inner class. *THIS_ARG might be modified
10134 appropriately and an alternative access to *MDECL might be
10135 returned. */
10137 static int
10138 maybe_use_access_method (is_super_init, mdecl, this_arg)
10139 int is_super_init;
10140 tree *mdecl, *this_arg;
10142 tree ctx;
10143 tree md = *mdecl, ta = *this_arg;
10144 int to_return = 0;
10145 int non_static_context = !METHOD_STATIC (md);
10147 if (is_super_init
10148 || DECL_CONTEXT (md) == current_class
10149 || !PURE_INNER_CLASS_TYPE_P (current_class)
10150 || DECL_FINIT_P (md))
10151 return 0;
10153 /* If we're calling a method found in an enclosing class, generate
10154 what it takes to retrieve the right this. Don't do that if we're
10155 invoking a static method. Note that if MD's type is unrelated to
10156 CURRENT_CLASS, then the current this can be used. */
10158 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10160 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10161 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10163 ta = build_current_thisn (current_class);
10164 ta = build_wfl_node (ta);
10166 else
10168 tree type = ctx;
10169 while (type)
10171 maybe_build_thisn_access_method (type);
10172 if (inherits_from_p (type, DECL_CONTEXT (md)))
10174 ta = build_access_to_thisn (ctx, type, 0);
10175 break;
10177 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10178 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10181 ta = java_complete_tree (ta);
10184 /* We might have to use an access method to get to MD. We can
10185 break the method access rule as far as we're not generating
10186 bytecode */
10187 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10189 md = build_outer_method_access_method (md);
10190 to_return = 1;
10193 *mdecl = md;
10194 *this_arg = ta;
10196 /* Returnin a non zero value indicates we were doing a non static
10197 method invokation that is now a static invocation. It will have
10198 callee displace `this' to insert it in the regular argument
10199 list. */
10200 return (non_static_context && to_return);
10203 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10204 mode. */
10206 static tree
10207 patch_invoke (patch, method, args)
10208 tree patch, method, args;
10210 tree dtable, func;
10211 tree original_call, t, ta;
10212 tree cond = NULL_TREE;
10214 /* Last step for args: convert build-in types. If we're dealing with
10215 a new TYPE() type call, the first argument to the constructor
10216 isn't found in the incoming argument list, but delivered by
10217 `new' */
10218 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10219 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10220 t = TREE_CHAIN (t);
10221 for (ta = args; t != end_params_node && ta;
10222 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10223 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10224 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10225 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10227 /* Resolve unresolved returned type isses */
10228 t = TREE_TYPE (TREE_TYPE (method));
10229 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10230 resolve_and_layout (TREE_TYPE (t), NULL);
10232 if (flag_emit_class_files || flag_emit_xref)
10233 func = method;
10234 else
10236 tree signature = build_java_signature (TREE_TYPE (method));
10237 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10239 case INVOKE_VIRTUAL:
10240 dtable = invoke_build_dtable (0, args);
10241 func = build_invokevirtual (dtable, method);
10242 break;
10244 case INVOKE_NONVIRTUAL:
10245 /* If the object for the method call is null, we throw an
10246 exception. We don't do this if the object is the current
10247 method's `this'. In other cases we just rely on an
10248 optimization pass to eliminate redundant checks. */
10249 if (TREE_VALUE (args) != current_this)
10251 /* We use a SAVE_EXPR here to make sure we only evaluate
10252 the new `self' expression once. */
10253 tree save_arg = save_expr (TREE_VALUE (args));
10254 TREE_VALUE (args) = save_arg;
10255 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10256 null_pointer_node);
10258 /* Fall through. */
10260 case INVOKE_SUPER:
10261 case INVOKE_STATIC:
10262 func = build_known_method_ref (method, TREE_TYPE (method),
10263 DECL_CONTEXT (method),
10264 signature, args);
10265 break;
10267 case INVOKE_INTERFACE:
10268 dtable = invoke_build_dtable (1, args);
10269 func = build_invokeinterface (dtable, method);
10270 break;
10272 default:
10273 fatal ("internal error - unknown invocation_mode result");
10276 /* Ensure self_type is initialized, (invokestatic). FIXME */
10277 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10280 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10281 TREE_OPERAND (patch, 0) = func;
10282 TREE_OPERAND (patch, 1) = args;
10283 original_call = patch;
10285 /* We're processing a `new TYPE ()' form. New is called and its
10286 returned value is the first argument to the constructor. We build
10287 a COMPOUND_EXPR and use saved expression so that the overall NEW
10288 expression value is a pointer to a newly created and initialized
10289 class. */
10290 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10292 tree class = DECL_CONTEXT (method);
10293 tree c1, saved_new, size, new;
10294 if (flag_emit_class_files || flag_emit_xref)
10296 TREE_TYPE (patch) = build_pointer_type (class);
10297 return patch;
10299 if (!TYPE_SIZE (class))
10300 safe_layout_class (class);
10301 size = size_in_bytes (class);
10302 new = build (CALL_EXPR, promote_type (class),
10303 build_address_of (alloc_object_node),
10304 tree_cons (NULL_TREE, build_class_ref (class),
10305 build_tree_list (NULL_TREE,
10306 size_in_bytes (class))),
10307 NULL_TREE);
10308 saved_new = save_expr (new);
10309 c1 = build_tree_list (NULL_TREE, saved_new);
10310 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10311 TREE_OPERAND (original_call, 1) = c1;
10312 TREE_SET_CODE (original_call, CALL_EXPR);
10313 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10316 /* If COND is set, then we are building a check to see if the object
10317 is NULL. */
10318 if (cond != NULL_TREE)
10320 /* We have to make the `then' branch a compound expression to
10321 make the types turn out right. This seems bizarre. */
10322 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10323 build (COMPOUND_EXPR, TREE_TYPE (patch),
10324 build (CALL_EXPR, void_type_node,
10325 build_address_of (soft_nullpointer_node),
10326 NULL_TREE, NULL_TREE),
10327 (FLOAT_TYPE_P (TREE_TYPE (patch))
10328 ? build_real (TREE_TYPE (patch), dconst0)
10329 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10330 integer_zero_node))),
10331 patch);
10332 TREE_SIDE_EFFECTS (patch) = 1;
10335 return patch;
10338 static int
10339 invocation_mode (method, super)
10340 tree method;
10341 int super;
10343 int access = get_access_flags_from_decl (method);
10345 if (super)
10346 return INVOKE_SUPER;
10348 if (access & ACC_STATIC)
10349 return INVOKE_STATIC;
10351 /* We have to look for a constructor before we handle nonvirtual
10352 calls; otherwise the constructor will look nonvirtual. */
10353 if (DECL_CONSTRUCTOR_P (method))
10354 return INVOKE_STATIC;
10356 if (access & ACC_FINAL || access & ACC_PRIVATE)
10357 return INVOKE_NONVIRTUAL;
10359 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10360 return INVOKE_NONVIRTUAL;
10362 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10363 return INVOKE_INTERFACE;
10365 return INVOKE_VIRTUAL;
10368 /* Retrieve a refined list of matching methods. It covers the step
10369 15.11.2 (Compile-Time Step 2) */
10371 static tree
10372 lookup_method_invoke (lc, cl, class, name, arg_list)
10373 int lc;
10374 tree cl;
10375 tree class, name, arg_list;
10377 tree atl = end_params_node; /* Arg Type List */
10378 tree method, signature, list, node;
10379 const char *candidates; /* Used for error report */
10380 char *dup;
10382 /* Fix the arguments */
10383 for (node = arg_list; node; node = TREE_CHAIN (node))
10385 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10386 /* Non primitive type may have to be resolved */
10387 if (!JPRIMITIVE_TYPE_P (current_arg))
10388 resolve_and_layout (current_arg, NULL_TREE);
10389 /* And promoted */
10390 if (TREE_CODE (current_arg) == RECORD_TYPE)
10391 current_arg = promote_type (current_arg);
10392 atl = tree_cons (NULL_TREE, current_arg, atl);
10395 /* Presto. If we're dealing with an anonymous class and a
10396 constructor call, generate the right constructor now, since we
10397 know the arguments' types. */
10399 if (lc && ANONYMOUS_CLASS_P (class))
10400 craft_constructor (TYPE_NAME (class), atl);
10402 /* Find all candidates and then refine the list, searching for the
10403 most specific method. */
10404 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10405 list = find_most_specific_methods_list (list);
10406 if (list && !TREE_CHAIN (list))
10407 return TREE_VALUE (list);
10409 /* Issue an error. List candidates if any. Candidates are listed
10410 only if accessible (non accessible methods may end-up here for
10411 the sake of a better error report). */
10412 candidates = NULL;
10413 if (list)
10415 tree current;
10416 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10417 for (current = list; current; current = TREE_CHAIN (current))
10419 tree cm = TREE_VALUE (current);
10420 char string [4096];
10421 if (!cm || not_accessible_p (class, cm, 0))
10422 continue;
10423 sprintf
10424 (string, " `%s' in `%s'%s",
10425 get_printable_method_name (cm),
10426 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10427 (TREE_CHAIN (current) ? "\n" : ""));
10428 obstack_grow (&temporary_obstack, string, strlen (string));
10430 obstack_1grow (&temporary_obstack, '\0');
10431 candidates = obstack_finish (&temporary_obstack);
10433 /* Issue the error message */
10434 method = make_node (FUNCTION_TYPE);
10435 TYPE_ARG_TYPES (method) = atl;
10436 signature = build_java_argument_signature (method);
10437 dup = xstrdup (lang_printable_name (class, 0));
10438 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10439 (lc ? "constructor" : "method"),
10440 (lc ? dup : IDENTIFIER_POINTER (name)),
10441 IDENTIFIER_POINTER (signature), dup,
10442 (candidates ? candidates : ""));
10443 free (dup);
10444 return NULL_TREE;
10447 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10448 when we're looking for a constructor. */
10450 static tree
10451 find_applicable_accessible_methods_list (lc, class, name, arglist)
10452 int lc;
10453 tree class, name, arglist;
10455 static struct hash_table t, *searched_classes = NULL;
10456 static int search_not_done = 0;
10457 tree list = NULL_TREE, all_list = NULL_TREE;
10459 /* Check the hash table to determine if this class has been searched
10460 already. */
10461 if (searched_classes)
10463 if (hash_lookup (searched_classes,
10464 (const hash_table_key) class, FALSE, NULL))
10465 return NULL;
10467 else
10469 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10470 java_hash_compare_tree_node);
10471 searched_classes = &t;
10474 search_not_done++;
10475 hash_lookup (searched_classes,
10476 (const hash_table_key) class, TRUE, NULL);
10478 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10480 load_class (class, 1);
10481 safe_layout_class (class);
10484 /* Search interfaces */
10485 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10486 && CLASS_INTERFACE (TYPE_NAME (class)))
10488 int i, n;
10489 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10490 search_applicable_methods_list (lc, TYPE_METHODS (class),
10491 name, arglist, &list, &all_list);
10492 n = TREE_VEC_LENGTH (basetype_vec);
10493 for (i = 1; i < n; i++)
10495 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10496 tree rlist;
10498 rlist = find_applicable_accessible_methods_list (lc, t, name,
10499 arglist);
10500 list = chainon (rlist, list);
10503 /* Search classes */
10504 else
10506 tree sc = class;
10507 int seen_inner_class = 0;
10508 search_applicable_methods_list (lc, TYPE_METHODS (class),
10509 name, arglist, &list, &all_list);
10511 /* When looking finit$, we turn LC to 1 so that we only search
10512 in class. Note that we should have found something at
10513 this point. */
10514 if (ID_FINIT_P (name))
10516 lc = 1;
10517 if (!list)
10518 fatal ("finit$ not found in class -- find_applicable_accessible_methods_list");
10521 /* We must search all interfaces of this class */
10522 if (!lc)
10524 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10525 int n = TREE_VEC_LENGTH (basetype_vec), i;
10526 for (i = 1; i < n; i++)
10528 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10529 if (t != object_type_node)
10531 tree rlist
10532 = find_applicable_accessible_methods_list (lc, t,
10533 name, arglist);
10534 list = chainon (rlist, list);
10539 /* Search enclosing context of inner classes before looking
10540 ancestors up. */
10541 while (!lc && INNER_CLASS_TYPE_P (class))
10543 tree rlist;
10544 seen_inner_class = 1;
10545 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
10546 rlist = find_applicable_accessible_methods_list (lc, class,
10547 name, arglist);
10548 list = chainon (rlist, list);
10551 if (!lc && seen_inner_class
10552 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10553 class = CLASSTYPE_SUPER (sc);
10554 else
10555 class = sc;
10557 /* Search superclass */
10558 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10560 tree rlist;
10561 class = CLASSTYPE_SUPER (class);
10562 rlist = find_applicable_accessible_methods_list (lc, class,
10563 name, arglist);
10564 list = chainon (rlist, list);
10568 search_not_done--;
10570 /* We're done. Reset the searched classes list and finally search
10571 java.lang.Object if it wasn't searched already. */
10572 if (!search_not_done)
10574 if (!lc
10575 && TYPE_METHODS (object_type_node)
10576 && !hash_lookup (searched_classes,
10577 (const hash_table_key) object_type_node,
10578 FALSE, NULL))
10580 search_applicable_methods_list (lc,
10581 TYPE_METHODS (object_type_node),
10582 name, arglist, &list, &all_list);
10584 hash_table_free (searched_classes);
10585 searched_classes = NULL;
10588 /* Either return the list obtained or all selected (but
10589 inaccessible) methods for better error report. */
10590 return (!list ? all_list : list);
10593 /* Effectively search for the appropriate method in method */
10595 static void
10596 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10597 int lc;
10598 tree method, name, arglist;
10599 tree *list, *all_list;
10601 for (; method; method = TREE_CHAIN (method))
10603 /* When dealing with constructor, stop here, otherwise search
10604 other classes */
10605 if (lc && !DECL_CONSTRUCTOR_P (method))
10606 continue;
10607 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10608 || (GET_METHOD_NAME (method) != name)))
10609 continue;
10611 if (argument_types_convertible (method, arglist))
10613 /* Retain accessible methods only */
10614 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10615 method, 0))
10616 *list = tree_cons (NULL_TREE, method, *list);
10617 else
10618 /* Also retain all selected method here */
10619 *all_list = tree_cons (NULL_TREE, method, *list);
10624 /* 15.11.2.2 Choose the Most Specific Method */
10626 static tree
10627 find_most_specific_methods_list (list)
10628 tree list;
10630 int max = 0;
10631 int abstract, candidates;
10632 tree current, new_list = NULL_TREE;
10633 for (current = list; current; current = TREE_CHAIN (current))
10635 tree method;
10636 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10638 for (method = list; method; method = TREE_CHAIN (method))
10640 tree method_v, current_v;
10641 /* Don't test a method against itself */
10642 if (method == current)
10643 continue;
10645 method_v = TREE_VALUE (method);
10646 current_v = TREE_VALUE (current);
10648 /* Compare arguments and location where methods where declared */
10649 if (argument_types_convertible (method_v, current_v))
10651 if (valid_method_invocation_conversion_p
10652 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10653 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10654 && enclosing_context_p (DECL_CONTEXT (method_v),
10655 DECL_CONTEXT (current_v))))
10657 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10658 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10659 max = (v > max ? v : max);
10665 /* Review the list and select the maximally specific methods */
10666 for (current = list, abstract = -1, candidates = -1;
10667 current; current = TREE_CHAIN (current))
10668 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10670 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10671 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10672 candidates++;
10675 /* If we have several and they're all abstract, just pick the
10676 closest one. */
10677 if (candidates > 0 && (candidates == abstract))
10679 new_list = nreverse (new_list);
10680 TREE_CHAIN (new_list) = NULL_TREE;
10683 /* We have several (we couldn't find a most specific), all but one
10684 are abstract, we pick the only non abstract one. */
10685 if (candidates > 0 && (candidates == abstract+1))
10687 for (current = new_list; current; current = TREE_CHAIN (current))
10688 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10690 TREE_CHAIN (current) = NULL_TREE;
10691 new_list = current;
10695 /* If we can't find one, lower expectations and try to gather multiple
10696 maximally specific methods */
10697 while (!new_list && max)
10699 while (--max > 0)
10701 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10702 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10706 return new_list;
10709 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10710 converted by method invocation conversion (5.3) to the type of the
10711 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10712 to change less often than M1. */
10714 static int
10715 argument_types_convertible (m1, m2_or_arglist)
10716 tree m1, m2_or_arglist;
10718 static tree m2_arg_value = NULL_TREE;
10719 static tree m2_arg_cache = NULL_TREE;
10720 static int initialized_p;
10722 register tree m1_arg, m2_arg;
10724 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10725 collector. */
10726 if (!initialized_p)
10728 ggc_add_tree_root (&m2_arg_value, 1);
10729 ggc_add_tree_root (&m2_arg_cache, 1);
10730 initialized_p = 1;
10733 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10735 if (m2_arg_value == m2_or_arglist)
10736 m2_arg = m2_arg_cache;
10737 else
10739 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10740 argument types */
10741 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10743 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10744 if (!METHOD_STATIC (m2_or_arglist))
10745 m2_arg = TREE_CHAIN (m2_arg);
10747 else
10748 m2_arg = m2_or_arglist;
10750 m2_arg_value = m2_or_arglist;
10751 m2_arg_cache = m2_arg;
10754 while (m1_arg != end_params_node && m2_arg != end_params_node)
10756 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
10757 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10758 TREE_VALUE (m2_arg)))
10759 break;
10760 m1_arg = TREE_CHAIN (m1_arg);
10761 m2_arg = TREE_CHAIN (m2_arg);
10763 return m1_arg == end_params_node && m2_arg == end_params_node;
10766 /* Qualification routines */
10768 static void
10769 qualify_ambiguous_name (id)
10770 tree id;
10772 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10773 saved_current_class;
10774 int again, super_found = 0, this_found = 0, new_array_found = 0;
10775 int code;
10777 /* We first qualify the first element, then derive qualification of
10778 others based on the first one. If the first element is qualified
10779 by a resolution (field or type), this resolution is stored in the
10780 QUAL_RESOLUTION of the qual element being examined. We need to
10781 save the current_class since the use of SUPER might change the
10782 its value. */
10783 saved_current_class = current_class;
10784 qual = EXPR_WFL_QUALIFICATION (id);
10785 do {
10787 /* Simple qualified expression feature a qual_wfl that is a
10788 WFL. Expression derived from a primary feature more complicated
10789 things like a CALL_EXPR. Expression from primary need to be
10790 worked out to extract the part on which the qualification will
10791 take place. */
10792 qual_wfl = QUAL_WFL (qual);
10793 switch (TREE_CODE (qual_wfl))
10795 case CALL_EXPR:
10796 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10797 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10799 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10800 qual_wfl = QUAL_WFL (qual);
10802 break;
10803 case NEW_ARRAY_EXPR:
10804 case NEW_ANONYMOUS_ARRAY_EXPR:
10805 qual = TREE_CHAIN (qual);
10806 again = new_array_found = 1;
10807 continue;
10808 case CONVERT_EXPR:
10809 break;
10810 case NEW_CLASS_EXPR:
10811 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10812 break;
10813 case ARRAY_REF:
10814 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10815 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10816 break;
10817 case STRING_CST:
10818 qual = TREE_CHAIN (qual);
10819 qual_wfl = QUAL_WFL (qual);
10820 break;
10821 case CLASS_LITERAL:
10822 qual = TREE_CHAIN (qual);
10823 qual_wfl = QUAL_WFL (qual);
10824 break;
10825 default:
10826 /* Fix for -Wall. Just break doing nothing */
10827 break;
10830 ptr_type = current_class;
10831 again = 0;
10832 code = TREE_CODE (qual_wfl);
10834 /* Pos evaluation: non WFL leading expression nodes */
10835 if (code == CONVERT_EXPR
10836 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10837 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10839 else if (code == INTEGER_CST)
10840 name = qual_wfl;
10842 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
10843 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10844 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10846 else if (code == TREE_LIST)
10847 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10849 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10850 || code == PLUS_EXPR)
10852 qual = TREE_CHAIN (qual);
10853 qual_wfl = QUAL_WFL (qual);
10854 again = 1;
10856 else
10858 name = EXPR_WFL_NODE (qual_wfl);
10859 if (!name)
10861 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10862 again = 1;
10866 /* If we have a THIS (from a primary), we set the context accordingly */
10867 if (name == this_identifier_node)
10869 /* This isn't really elegant. One more added irregularity
10870 before I start using COMPONENT_REF (hopefully very soon.) */
10871 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10872 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10873 EXPR_WITH_FILE_LOCATION
10874 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10875 this_identifier_node)
10877 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10878 qual = EXPR_WFL_QUALIFICATION (qual);
10880 qual = TREE_CHAIN (qual);
10881 qual_wfl = QUAL_WFL (qual);
10882 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10883 again = 1;
10884 else
10885 name = EXPR_WFL_NODE (qual_wfl);
10886 this_found = 1;
10888 /* If we have a SUPER, we set the context accordingly */
10889 if (name == super_identifier_node)
10891 current_class = CLASSTYPE_SUPER (ptr_type);
10892 /* Check that there is such a thing as a super class. If not,
10893 return. The error will be caught later on, during the
10894 resolution */
10895 if (!current_class)
10897 current_class = saved_current_class;
10898 return;
10900 qual = TREE_CHAIN (qual);
10901 /* Do one more interation to set things up */
10902 super_found = again = 1;
10904 } while (again);
10906 /* If name appears within the scope of a local variable declaration
10907 or parameter declaration, then it is an expression name. We don't
10908 carry this test out if we're in the context of the use of SUPER
10909 or THIS */
10910 if (!this_found && !super_found
10911 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10912 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
10914 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10915 QUAL_RESOLUTION (qual) = decl;
10918 /* If within the class/interface NAME was found to be used there
10919 exists a (possibly inherited) field named NAME, then this is an
10920 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10921 address length, it is OK. */
10922 else if ((decl = lookup_field_wrapper (ptr_type, name))
10923 || (new_array_found && name == length_identifier_node))
10925 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10926 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
10929 /* We reclassify NAME as yielding to a type name resolution if:
10930 - NAME is a class/interface declared within the compilation
10931 unit containing NAME,
10932 - NAME is imported via a single-type-import declaration,
10933 - NAME is declared in an another compilation unit of the package
10934 of the compilation unit containing NAME,
10935 - NAME is declared by exactly on type-import-on-demand declaration
10936 of the compilation unit containing NAME.
10937 - NAME is actually a STRING_CST. */
10938 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10939 || (decl = resolve_and_layout (name, NULL_TREE)))
10941 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10942 QUAL_RESOLUTION (qual) = decl;
10945 /* Method call, array references and cast are expression name */
10946 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
10947 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10948 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
10949 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10951 /* Check here that NAME isn't declared by more than one
10952 type-import-on-demand declaration of the compilation unit
10953 containing NAME. FIXME */
10955 /* Otherwise, NAME is reclassified as a package name */
10956 else
10957 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10959 /* Propagate the qualification accross other components of the
10960 qualified name */
10961 for (qual = TREE_CHAIN (qual); qual;
10962 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10964 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10965 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10966 else
10967 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10970 /* Store the global qualification for the ambiguous part of ID back
10971 into ID fields */
10972 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10973 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10974 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10975 RESOLVE_TYPE_NAME_P (id) = 1;
10976 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10977 RESOLVE_PACKAGE_NAME_P (id) = 1;
10979 /* Restore the current class */
10980 current_class = saved_current_class;
10983 static int
10984 breakdown_qualified (left, right, source)
10985 tree *left, *right, source;
10987 char *p, *base;
10988 int l = IDENTIFIER_LENGTH (source);
10990 base = alloca (l + 1);
10991 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10993 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10994 p = base + l - 1;
10995 while (*p != '.' && p != base)
10996 p--;
10998 /* We didn't find a '.'. Return an error */
10999 if (p == base)
11000 return 1;
11002 *p = '\0';
11003 if (right)
11004 *right = get_identifier (p+1);
11005 *left = get_identifier (base);
11007 return 0;
11010 /* Patch tree nodes in a function body. When a BLOCK is found, push
11011 local variable decls if present.
11012 Same as java_complete_lhs, but does resolve static finals to values. */
11014 static tree
11015 java_complete_tree (node)
11016 tree node;
11018 node = java_complete_lhs (node);
11019 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11020 && DECL_INITIAL (node) != NULL_TREE
11021 && !flag_emit_xref)
11023 tree value = DECL_INITIAL (node);
11024 DECL_INITIAL (node) = NULL_TREE;
11025 value = fold_constant_for_init (value, node);
11026 DECL_INITIAL (node) = value;
11027 if (value != NULL_TREE)
11029 /* fold_constant_for_init sometimes widen the original type
11030 of the constant (i.e. byte to int.) It's not desirable,
11031 especially if NODE is a function argument. */
11032 if (TREE_CODE (value) == INTEGER_CST
11033 && TREE_TYPE (node) != TREE_TYPE (value))
11034 return convert (TREE_TYPE (node), value);
11035 else
11036 return value;
11038 else
11039 DECL_FIELD_FINAL_IUD (node) = 0;
11041 return node;
11044 static tree
11045 java_stabilize_reference (node)
11046 tree node;
11048 if (TREE_CODE (node) == COMPOUND_EXPR)
11050 tree op0 = TREE_OPERAND (node, 0);
11051 tree op1 = TREE_OPERAND (node, 1);
11052 TREE_OPERAND (node, 0) = save_expr (op0);
11053 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11054 return node;
11056 return stabilize_reference (node);
11059 /* Patch tree nodes in a function body. When a BLOCK is found, push
11060 local variable decls if present.
11061 Same as java_complete_tree, but does not resolve static finals to values. */
11063 static tree
11064 java_complete_lhs (node)
11065 tree node;
11067 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11068 int flag;
11070 /* CONVERT_EXPR always has its type set, even though it needs to be
11071 worked out. */
11072 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11073 return node;
11075 /* The switch block implements cases processing container nodes
11076 first. Contained nodes are always written back. Leaves come
11077 next and return a value. */
11078 switch (TREE_CODE (node))
11080 case BLOCK:
11082 /* 1- Block section.
11083 Set the local values on decl names so we can identify them
11084 faster when they're referenced. At that stage, identifiers
11085 are legal so we don't check for declaration errors. */
11086 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11088 DECL_CONTEXT (cn) = current_function_decl;
11089 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11091 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11092 CAN_COMPLETE_NORMALLY (node) = 1;
11093 else
11095 tree stmt = BLOCK_EXPR_BODY (node);
11096 tree *ptr;
11097 int error_seen = 0;
11098 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11100 /* Re-order from (((A; B); C); ...; Z) to
11101 (A; (B; (C ; (...; Z)))).
11102 This makes it easier to scan the statements left-to-right
11103 without using recursion (which might overflow the stack
11104 if the block has many statements. */
11105 for (;;)
11107 tree left = TREE_OPERAND (stmt, 0);
11108 if (TREE_CODE (left) != COMPOUND_EXPR)
11109 break;
11110 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11111 TREE_OPERAND (left, 1) = stmt;
11112 stmt = left;
11114 BLOCK_EXPR_BODY (node) = stmt;
11117 /* Now do the actual complete, without deep recursion for
11118 long blocks. */
11119 ptr = &BLOCK_EXPR_BODY (node);
11120 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11121 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11123 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11124 tree *next = &TREE_OPERAND (*ptr, 1);
11125 TREE_OPERAND (*ptr, 0) = cur;
11126 if (cur == empty_stmt_node)
11128 /* Optimization; makes it easier to detect empty bodies.
11129 Most useful for <clinit> with all-constant initializer. */
11130 *ptr = *next;
11131 continue;
11133 if (TREE_CODE (cur) == ERROR_MARK)
11134 error_seen++;
11135 else if (! CAN_COMPLETE_NORMALLY (cur))
11137 wfl_op2 = *next;
11138 for (;;)
11140 if (TREE_CODE (wfl_op2) == BLOCK)
11141 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11142 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11143 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11144 else
11145 break;
11147 if (TREE_CODE (wfl_op2) != CASE_EXPR
11148 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11149 unreachable_stmt_error (*ptr);
11151 ptr = next;
11153 *ptr = java_complete_tree (*ptr);
11155 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11156 return error_mark_node;
11157 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11159 /* Turn local bindings to null */
11160 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11161 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11163 TREE_TYPE (node) = void_type_node;
11164 break;
11166 /* 2- They are expressions but ultimately deal with statements */
11168 case THROW_EXPR:
11169 wfl_op1 = TREE_OPERAND (node, 0);
11170 COMPLETE_CHECK_OP_0 (node);
11171 /* 14.19 A throw statement cannot complete normally. */
11172 CAN_COMPLETE_NORMALLY (node) = 0;
11173 return patch_throw_statement (node, wfl_op1);
11175 case SYNCHRONIZED_EXPR:
11176 wfl_op1 = TREE_OPERAND (node, 0);
11177 return patch_synchronized_statement (node, wfl_op1);
11179 case TRY_EXPR:
11180 return patch_try_statement (node);
11182 case TRY_FINALLY_EXPR:
11183 COMPLETE_CHECK_OP_0 (node);
11184 COMPLETE_CHECK_OP_1 (node);
11185 CAN_COMPLETE_NORMALLY (node)
11186 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11187 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11188 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11189 return node;
11191 case CLEANUP_POINT_EXPR:
11192 COMPLETE_CHECK_OP_0 (node);
11193 TREE_TYPE (node) = void_type_node;
11194 CAN_COMPLETE_NORMALLY (node) =
11195 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11196 return node;
11198 case WITH_CLEANUP_EXPR:
11199 COMPLETE_CHECK_OP_0 (node);
11200 COMPLETE_CHECK_OP_2 (node);
11201 CAN_COMPLETE_NORMALLY (node) =
11202 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11203 TREE_TYPE (node) = void_type_node;
11204 return node;
11206 case LABELED_BLOCK_EXPR:
11207 PUSH_LABELED_BLOCK (node);
11208 if (LABELED_BLOCK_BODY (node))
11209 COMPLETE_CHECK_OP_1 (node);
11210 TREE_TYPE (node) = void_type_node;
11211 POP_LABELED_BLOCK ();
11213 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11215 LABELED_BLOCK_BODY (node) = NULL_TREE;
11216 CAN_COMPLETE_NORMALLY (node) = 1;
11218 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11219 CAN_COMPLETE_NORMALLY (node) = 1;
11220 return node;
11222 case EXIT_BLOCK_EXPR:
11223 /* We don't complete operand 1, because it's the return value of
11224 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11225 return patch_bc_statement (node);
11227 case CASE_EXPR:
11228 cn = java_complete_tree (TREE_OPERAND (node, 0));
11229 if (cn == error_mark_node)
11230 return cn;
11232 /* First, the case expression must be constant. Values of final
11233 fields are accepted. */
11234 cn = fold (cn);
11235 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11236 && JDECL_P (TREE_OPERAND (cn, 1))
11237 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11238 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11240 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11241 TREE_OPERAND (cn, 1));
11244 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11246 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11247 parse_error_context (node, "Constant expression required");
11248 return error_mark_node;
11251 nn = ctxp->current_loop;
11253 /* It must be assignable to the type of the switch expression. */
11254 if (!try_builtin_assignconv (NULL_TREE,
11255 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11257 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11258 parse_error_context
11259 (wfl_operator,
11260 "Incompatible type for case. Can't convert `%s' to `int'",
11261 lang_printable_name (TREE_TYPE (cn), 0));
11262 return error_mark_node;
11265 cn = fold (convert (int_type_node, cn));
11267 /* Multiple instance of a case label bearing the same
11268 value is checked during code generation. The case
11269 expression is allright so far. */
11270 if (TREE_CODE (cn) == VAR_DECL)
11271 cn = DECL_INITIAL (cn);
11272 TREE_OPERAND (node, 0) = cn;
11273 TREE_TYPE (node) = void_type_node;
11274 CAN_COMPLETE_NORMALLY (node) = 1;
11275 TREE_SIDE_EFFECTS (node) = 1;
11276 break;
11278 case DEFAULT_EXPR:
11279 nn = ctxp->current_loop;
11280 /* Only one default label is allowed per switch statement */
11281 if (SWITCH_HAS_DEFAULT (nn))
11283 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11284 parse_error_context (wfl_operator,
11285 "Duplicate case label: `default'");
11286 return error_mark_node;
11288 else
11289 SWITCH_HAS_DEFAULT (nn) = 1;
11290 TREE_TYPE (node) = void_type_node;
11291 TREE_SIDE_EFFECTS (node) = 1;
11292 CAN_COMPLETE_NORMALLY (node) = 1;
11293 break;
11295 case SWITCH_EXPR:
11296 case LOOP_EXPR:
11297 PUSH_LOOP (node);
11298 /* Check whether the loop was enclosed in a labeled
11299 statement. If not, create one, insert the loop in it and
11300 return the node */
11301 nn = patch_loop_statement (node);
11303 /* Anyways, walk the body of the loop */
11304 if (TREE_CODE (node) == LOOP_EXPR)
11305 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11306 /* Switch statement: walk the switch expression and the cases */
11307 else
11308 node = patch_switch_statement (node);
11310 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11311 nn = error_mark_node;
11312 else
11314 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11315 /* If we returned something different, that's because we
11316 inserted a label. Pop the label too. */
11317 if (nn != node)
11319 if (CAN_COMPLETE_NORMALLY (node))
11320 CAN_COMPLETE_NORMALLY (nn) = 1;
11321 POP_LABELED_BLOCK ();
11324 POP_LOOP ();
11325 return nn;
11327 case EXIT_EXPR:
11328 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11329 return patch_exit_expr (node);
11331 case COND_EXPR:
11332 /* Condition */
11333 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11334 if (TREE_OPERAND (node, 0) == error_mark_node)
11335 return error_mark_node;
11336 /* then-else branches */
11337 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11338 if (TREE_OPERAND (node, 1) == error_mark_node)
11339 return error_mark_node;
11340 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11341 if (TREE_OPERAND (node, 2) == error_mark_node)
11342 return error_mark_node;
11343 return patch_if_else_statement (node);
11344 break;
11346 case CONDITIONAL_EXPR:
11347 /* Condition */
11348 wfl_op1 = TREE_OPERAND (node, 0);
11349 COMPLETE_CHECK_OP_0 (node);
11350 wfl_op2 = TREE_OPERAND (node, 1);
11351 COMPLETE_CHECK_OP_1 (node);
11352 wfl_op3 = TREE_OPERAND (node, 2);
11353 COMPLETE_CHECK_OP_2 (node);
11354 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11356 /* 3- Expression section */
11357 case COMPOUND_EXPR:
11358 wfl_op2 = TREE_OPERAND (node, 1);
11359 TREE_OPERAND (node, 0) = nn =
11360 java_complete_tree (TREE_OPERAND (node, 0));
11361 if (wfl_op2 == empty_stmt_node)
11362 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11363 else
11365 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11367 /* An unreachable condition in a do-while statement
11368 is *not* (technically) an unreachable statement. */
11369 nn = wfl_op2;
11370 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11371 nn = EXPR_WFL_NODE (nn);
11372 if (TREE_CODE (nn) != EXIT_EXPR)
11374 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11375 parse_error_context (wfl_operator, "Unreachable statement");
11378 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11379 if (TREE_OPERAND (node, 1) == error_mark_node)
11380 return error_mark_node;
11381 CAN_COMPLETE_NORMALLY (node)
11382 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11384 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11385 break;
11387 case RETURN_EXPR:
11388 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11389 return patch_return (node);
11391 case EXPR_WITH_FILE_LOCATION:
11392 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11393 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11395 tree wfl = node;
11396 node = resolve_expression_name (node, NULL);
11397 if (node == error_mark_node)
11398 return node;
11399 /* Keep line number information somewhere were it doesn't
11400 disrupt the completion process. */
11401 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11403 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11404 TREE_OPERAND (node, 1) = wfl;
11406 CAN_COMPLETE_NORMALLY (node) = 1;
11408 else
11410 tree body;
11411 int save_lineno = lineno;
11412 lineno = EXPR_WFL_LINENO (node);
11413 body = java_complete_tree (EXPR_WFL_NODE (node));
11414 lineno = save_lineno;
11415 EXPR_WFL_NODE (node) = body;
11416 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11417 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11418 if (body == empty_stmt_node || TREE_CONSTANT (body))
11420 /* Makes it easier to constant fold, detect empty bodies. */
11421 return body;
11423 if (body == error_mark_node)
11425 /* Its important for the evaluation of assignment that
11426 this mark on the TREE_TYPE is propagated. */
11427 TREE_TYPE (node) = error_mark_node;
11428 return error_mark_node;
11430 else
11431 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11434 break;
11436 case NEW_ARRAY_EXPR:
11437 /* Patch all the dimensions */
11438 flag = 0;
11439 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11441 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11442 tree dim = convert (int_type_node,
11443 java_complete_tree (TREE_VALUE (cn)));
11444 if (dim == error_mark_node)
11446 flag = 1;
11447 continue;
11449 else
11451 TREE_VALUE (cn) = dim;
11452 /* Setup the location of the current dimension, for
11453 later error report. */
11454 TREE_PURPOSE (cn) =
11455 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11456 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11459 /* They complete the array creation expression, if no errors
11460 were found. */
11461 CAN_COMPLETE_NORMALLY (node) = 1;
11462 return (flag ? error_mark_node
11463 : force_evaluation_order (patch_newarray (node)));
11465 case NEW_ANONYMOUS_ARRAY_EXPR:
11466 /* Create the array type if necessary. */
11467 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11469 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11470 if (!(type = resolve_type_during_patch (type)))
11471 return error_mark_node;
11472 type = build_array_from_name (type, NULL_TREE,
11473 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11474 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11476 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11477 ANONYMOUS_ARRAY_INITIALIZER (node));
11478 if (node == error_mark_node)
11479 return error_mark_node;
11480 CAN_COMPLETE_NORMALLY (node) = 1;
11481 return node;
11483 case NEW_CLASS_EXPR:
11484 case CALL_EXPR:
11485 /* Complete function's argument(s) first */
11486 if (complete_function_arguments (node))
11487 return error_mark_node;
11488 else
11490 tree decl, wfl = TREE_OPERAND (node, 0);
11491 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11493 node = patch_method_invocation (node, NULL_TREE,
11494 NULL_TREE, 0, &decl);
11495 if (node == error_mark_node)
11496 return error_mark_node;
11498 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11499 /* If we call this(...), register signature and positions */
11500 if (in_this)
11501 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11502 tree_cons (wfl, decl,
11503 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11504 CAN_COMPLETE_NORMALLY (node) = 1;
11505 return force_evaluation_order (node);
11508 case MODIFY_EXPR:
11509 /* Save potential wfls */
11510 wfl_op1 = TREE_OPERAND (node, 0);
11511 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11513 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11514 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11515 && DECL_INITIAL (nn) != NULL_TREE)
11517 tree value;
11519 value = fold_constant_for_init (nn, nn);
11521 if (value != NULL_TREE)
11523 tree type = TREE_TYPE (value);
11524 if (JPRIMITIVE_TYPE_P (type) ||
11525 (type == string_ptr_type_node && ! flag_emit_class_files))
11526 return empty_stmt_node;
11528 if (! flag_emit_class_files)
11529 DECL_INITIAL (nn) = NULL_TREE;
11530 if (CLASS_FINAL_VARIABLE_P (nn))
11531 DECL_FIELD_FINAL_IUD (nn) = 0;
11533 wfl_op2 = TREE_OPERAND (node, 1);
11535 if (TREE_OPERAND (node, 0) == error_mark_node)
11536 return error_mark_node;
11538 flag = COMPOUND_ASSIGN_P (wfl_op2);
11539 if (flag)
11541 /* This might break when accessing outer field from inner
11542 class. TESTME, FIXME */
11543 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11545 /* Hand stablize the lhs on both places */
11546 TREE_OPERAND (node, 0) = lvalue;
11547 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11548 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11550 /* 15.25.2.a: Left hand is not an array access. FIXME */
11551 /* Now complete the RHS. We write it back later on. */
11552 nn = java_complete_tree (TREE_OPERAND (node, 1));
11554 if ((cn = patch_string (nn)))
11555 nn = cn;
11557 /* The last part of the rewrite for E1 op= E2 is to have
11558 E1 = (T)(E1 op E2), with T being the type of E1. */
11559 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11560 TREE_TYPE (lvalue), nn));
11562 /* If the assignment is compound and has reference type,
11563 then ensure the LHS has type String and nothing else. */
11564 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11565 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11566 parse_error_context (wfl_op2,
11567 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11568 lang_printable_name (TREE_TYPE (lvalue), 0));
11570 /* 15.25.2.b: Left hand is an array access. FIXME */
11573 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11574 function to complete this RHS. Note that a NEW_ARRAY_INIT
11575 might have been already fully expanded if created as a result
11576 of processing an anonymous array initializer. We avoid doing
11577 the operation twice by testing whether the node already bears
11578 a type. */
11579 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11580 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11581 TREE_OPERAND (node, 1));
11582 /* Otherwise we simply complete the RHS */
11583 else
11584 nn = java_complete_tree (TREE_OPERAND (node, 1));
11586 if (nn == error_mark_node)
11587 return error_mark_node;
11589 /* Write back the RHS as we evaluated it. */
11590 TREE_OPERAND (node, 1) = nn;
11592 /* In case we're handling = with a String as a RHS, we need to
11593 produce a String out of the RHS (it might still be a
11594 STRING_CST or a StringBuffer at this stage */
11595 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11596 TREE_OPERAND (node, 1) = nn;
11598 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11599 TREE_OPERAND (node, 1))))
11601 /* We return error_mark_node if outer_field_access_fix
11602 detects we write into a final. */
11603 if (nn == error_mark_node)
11604 return error_mark_node;
11605 node = nn;
11607 else
11609 node = patch_assignment (node, wfl_op1, wfl_op2);
11610 /* Reorganize the tree if necessary. */
11611 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11612 || JSTRING_P (TREE_TYPE (node))))
11613 node = java_refold (node);
11616 CAN_COMPLETE_NORMALLY (node) = 1;
11617 return node;
11619 case MULT_EXPR:
11620 case PLUS_EXPR:
11621 case MINUS_EXPR:
11622 case LSHIFT_EXPR:
11623 case RSHIFT_EXPR:
11624 case URSHIFT_EXPR:
11625 case BIT_AND_EXPR:
11626 case BIT_XOR_EXPR:
11627 case BIT_IOR_EXPR:
11628 case TRUNC_MOD_EXPR:
11629 case TRUNC_DIV_EXPR:
11630 case RDIV_EXPR:
11631 case TRUTH_ANDIF_EXPR:
11632 case TRUTH_ORIF_EXPR:
11633 case EQ_EXPR:
11634 case NE_EXPR:
11635 case GT_EXPR:
11636 case GE_EXPR:
11637 case LT_EXPR:
11638 case LE_EXPR:
11639 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11640 knows how to handle those cases. */
11641 wfl_op1 = TREE_OPERAND (node, 0);
11642 wfl_op2 = TREE_OPERAND (node, 1);
11644 CAN_COMPLETE_NORMALLY (node) = 1;
11645 /* Don't complete string nodes if dealing with the PLUS operand. */
11646 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11648 nn = java_complete_tree (wfl_op1);
11649 if (nn == error_mark_node)
11650 return error_mark_node;
11652 TREE_OPERAND (node, 0) = nn;
11654 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11656 nn = java_complete_tree (wfl_op2);
11657 if (nn == error_mark_node)
11658 return error_mark_node;
11660 TREE_OPERAND (node, 1) = nn;
11662 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11664 case INSTANCEOF_EXPR:
11665 wfl_op1 = TREE_OPERAND (node, 0);
11666 COMPLETE_CHECK_OP_0 (node);
11667 if (flag_emit_xref)
11669 TREE_TYPE (node) = boolean_type_node;
11670 return node;
11672 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11674 case UNARY_PLUS_EXPR:
11675 case NEGATE_EXPR:
11676 case TRUTH_NOT_EXPR:
11677 case BIT_NOT_EXPR:
11678 case PREDECREMENT_EXPR:
11679 case PREINCREMENT_EXPR:
11680 case POSTDECREMENT_EXPR:
11681 case POSTINCREMENT_EXPR:
11682 case CONVERT_EXPR:
11683 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11684 how to handle those cases. */
11685 wfl_op1 = TREE_OPERAND (node, 0);
11686 CAN_COMPLETE_NORMALLY (node) = 1;
11687 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11688 if (TREE_OPERAND (node, 0) == error_mark_node)
11689 return error_mark_node;
11690 node = patch_unaryop (node, wfl_op1);
11691 CAN_COMPLETE_NORMALLY (node) = 1;
11692 break;
11694 case ARRAY_REF:
11695 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11696 how to handle those cases. */
11697 wfl_op1 = TREE_OPERAND (node, 0);
11698 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11699 if (TREE_OPERAND (node, 0) == error_mark_node)
11700 return error_mark_node;
11701 if (!flag_emit_class_files && !flag_emit_xref)
11702 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
11703 /* The same applies to wfl_op2 */
11704 wfl_op2 = TREE_OPERAND (node, 1);
11705 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11706 if (TREE_OPERAND (node, 1) == error_mark_node)
11707 return error_mark_node;
11708 if (!flag_emit_class_files && !flag_emit_xref)
11709 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
11710 return patch_array_ref (node);
11712 case RECORD_TYPE:
11713 return node;;
11715 case COMPONENT_REF:
11716 /* The first step in the re-write of qualified name handling. FIXME.
11717 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
11718 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11719 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11721 tree name = TREE_OPERAND (node, 1);
11722 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11723 if (field == NULL_TREE)
11725 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11726 return error_mark_node;
11728 if (! FIELD_STATIC (field))
11730 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11731 return error_mark_node;
11733 return field;
11735 else
11736 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
11737 break;
11739 case THIS_EXPR:
11740 /* Can't use THIS in a static environment */
11741 if (!current_this)
11743 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11744 parse_error_context (wfl_operator,
11745 "Keyword `this' used outside allowed context");
11746 TREE_TYPE (node) = error_mark_node;
11747 return error_mark_node;
11749 if (ctxp->explicit_constructor_p)
11751 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11752 parse_error_context
11753 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
11754 TREE_TYPE (node) = error_mark_node;
11755 return error_mark_node;
11757 return current_this;
11759 case CLASS_LITERAL:
11760 CAN_COMPLETE_NORMALLY (node) = 1;
11761 node = patch_incomplete_class_ref (node);
11762 if (node == error_mark_node)
11763 return error_mark_node;
11764 break;
11766 case INSTANCE_INITIALIZERS_EXPR:
11767 in_instance_initializer++;
11768 node = java_complete_tree (TREE_OPERAND (node, 0));
11769 in_instance_initializer--;
11770 if (node != error_mark_node)
11771 TREE_TYPE (node) = void_type_node;
11772 else
11773 return error_mark_node;
11774 break;
11776 default:
11777 CAN_COMPLETE_NORMALLY (node) = 1;
11778 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
11779 and it's time to turn it into the appropriate String object */
11780 if ((nn = patch_string (node)))
11781 node = nn;
11782 else
11783 fatal ("No case for tree code `%s' - java_complete_tree\n",
11784 tree_code_name [TREE_CODE (node)]);
11786 return node;
11789 /* Complete function call's argument. Return a non zero value is an
11790 error was found. */
11792 static int
11793 complete_function_arguments (node)
11794 tree node;
11796 int flag = 0;
11797 tree cn;
11799 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11800 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11802 tree wfl = TREE_VALUE (cn), parm, temp;
11803 parm = java_complete_tree (wfl);
11805 if (parm == error_mark_node)
11807 flag = 1;
11808 continue;
11810 /* If have a string literal that we haven't transformed yet or a
11811 crafted string buffer, as a result of use of the the String
11812 `+' operator. Build `parm.toString()' and expand it. */
11813 if ((temp = patch_string (parm)))
11814 parm = temp;
11815 /* Inline PRIMTYPE.TYPE read access */
11816 parm = maybe_build_primttype_type_ref (parm, wfl);
11818 TREE_VALUE (cn) = parm;
11820 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11821 return flag;
11824 /* Sometimes (for loops and variable initialized during their
11825 declaration), we want to wrap a statement around a WFL and turn it
11826 debugable. */
11828 static tree
11829 build_debugable_stmt (location, stmt)
11830 int location;
11831 tree stmt;
11833 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11835 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11836 EXPR_WFL_LINECOL (stmt) = location;
11838 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11839 return stmt;
11842 static tree
11843 build_expr_block (body, decls)
11844 tree body, decls;
11846 tree node = make_node (BLOCK);
11847 BLOCK_EXPR_DECLS (node) = decls;
11848 BLOCK_EXPR_BODY (node) = body;
11849 if (body)
11850 TREE_TYPE (node) = TREE_TYPE (body);
11851 TREE_SIDE_EFFECTS (node) = 1;
11852 return node;
11855 /* Create a new function block and link it approriately to current
11856 function block chain */
11858 static tree
11859 enter_block ()
11861 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11864 /* Link block B supercontext to the previous block. The current
11865 function DECL is used as supercontext when enter_a_block is called
11866 for the first time for a given function. The current function body
11867 (DECL_FUNCTION_BODY) is set to be block B. */
11869 static tree
11870 enter_a_block (b)
11871 tree b;
11873 tree fndecl = current_function_decl;
11875 if (!fndecl) {
11876 BLOCK_SUPERCONTEXT (b) = current_static_block;
11877 current_static_block = b;
11880 else if (!DECL_FUNCTION_BODY (fndecl))
11882 BLOCK_SUPERCONTEXT (b) = fndecl;
11883 DECL_FUNCTION_BODY (fndecl) = b;
11885 else
11887 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11888 DECL_FUNCTION_BODY (fndecl) = b;
11890 return b;
11893 /* Exit a block by changing the current function body
11894 (DECL_FUNCTION_BODY) to the current block super context, only if
11895 the block being exited isn't the method's top level one. */
11897 static tree
11898 exit_block ()
11900 tree b;
11901 if (current_function_decl)
11903 b = DECL_FUNCTION_BODY (current_function_decl);
11904 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11905 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11907 else
11909 b = current_static_block;
11911 if (BLOCK_SUPERCONTEXT (b))
11912 current_static_block = BLOCK_SUPERCONTEXT (b);
11914 return b;
11917 /* Lookup for NAME in the nested function's blocks, all the way up to
11918 the current toplevel one. It complies with Java's local variable
11919 scoping rules. */
11921 static tree
11922 lookup_name_in_blocks (name)
11923 tree name;
11925 tree b = GET_CURRENT_BLOCK (current_function_decl);
11927 while (b != current_function_decl)
11929 tree current;
11931 /* Paranoid sanity check. To be removed */
11932 if (TREE_CODE (b) != BLOCK)
11933 fatal ("non block expr function body - lookup_name_in_blocks");
11935 for (current = BLOCK_EXPR_DECLS (b); current;
11936 current = TREE_CHAIN (current))
11937 if (DECL_NAME (current) == name)
11938 return current;
11939 b = BLOCK_SUPERCONTEXT (b);
11941 return NULL_TREE;
11944 static void
11945 maybe_absorb_scoping_blocks ()
11947 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
11949 tree b = exit_block ();
11950 java_method_add_stmt (current_function_decl, b);
11951 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11956 /* This section of the source is reserved to build_* functions that
11957 are building incomplete tree nodes and the patch_* functions that
11958 are completing them. */
11960 /* Wrap a non WFL node around a WFL. */
11961 static tree
11962 build_wfl_wrap (node, location)
11963 tree node;
11964 int location;
11966 tree wfl, node_to_insert = node;
11968 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11969 with the way we're processing SUPER. A THIS from a primary as a
11970 different form than a SUPER. Turn THIS into something symbolic */
11971 if (TREE_CODE (node) == THIS_EXPR)
11972 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11973 else
11974 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11976 EXPR_WFL_LINECOL (wfl) = location;
11977 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11978 return wfl;
11982 /* Build a super() constructor invocation. Returns empty_stmt_node if
11983 we're currently dealing with the class java.lang.Object. */
11985 static tree
11986 build_super_invocation (mdecl)
11987 tree mdecl;
11989 if (DECL_CONTEXT (mdecl) == object_type_node)
11990 return empty_stmt_node;
11991 else
11993 tree super_wfl = build_wfl_node (super_identifier_node);
11994 tree a = NULL_TREE, t;
11995 /* If we're dealing with an anonymous class, pass the arguments
11996 of the crafted constructor along. */
11997 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11999 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12000 for (; t != end_params_node; t = TREE_CHAIN (t))
12001 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12003 return build_method_invocation (super_wfl, a);
12007 /* Build a SUPER/THIS qualified method invocation. */
12009 static tree
12010 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12011 int use_this;
12012 tree name, args;
12013 int lloc, rloc;
12015 tree invok;
12016 tree wfl =
12017 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12018 EXPR_WFL_LINECOL (wfl) = lloc;
12019 invok = build_method_invocation (name, args);
12020 return make_qualified_primary (wfl, invok, rloc);
12023 /* Build an incomplete CALL_EXPR node. */
12025 static tree
12026 build_method_invocation (name, args)
12027 tree name;
12028 tree args;
12030 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12031 TREE_SIDE_EFFECTS (call) = 1;
12032 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12033 return call;
12036 /* Build an incomplete new xxx(...) node. */
12038 static tree
12039 build_new_invocation (name, args)
12040 tree name, args;
12042 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12043 TREE_SIDE_EFFECTS (call) = 1;
12044 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12045 return call;
12048 /* Build an incomplete assignment expression. */
12050 static tree
12051 build_assignment (op, op_location, lhs, rhs)
12052 int op, op_location;
12053 tree lhs, rhs;
12055 tree assignment;
12056 /* Build the corresponding binop if we deal with a Compound
12057 Assignment operator. Mark the binop sub-tree as part of a
12058 Compound Assignment expression */
12059 if (op != ASSIGN_TK)
12061 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12062 COMPOUND_ASSIGN_P (rhs) = 1;
12064 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12065 TREE_SIDE_EFFECTS (assignment) = 1;
12066 EXPR_WFL_LINECOL (assignment) = op_location;
12067 return assignment;
12070 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12072 char *
12073 print_int_node (node)
12074 tree node;
12076 static char buffer [80];
12077 if (TREE_CONSTANT_OVERFLOW (node))
12078 sprintf (buffer, "<overflow>");
12080 if (TREE_INT_CST_HIGH (node) == 0)
12081 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12082 TREE_INT_CST_LOW (node));
12083 else if (TREE_INT_CST_HIGH (node) == -1
12084 && TREE_INT_CST_LOW (node) != 0)
12086 buffer [0] = '-';
12087 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12088 -TREE_INT_CST_LOW (node));
12090 else
12091 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12092 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12094 return buffer;
12099 /* This section of the code handle assignment check with FINAL
12100 variables. */
12102 static void
12103 reset_static_final_variable_assignment_flag (class)
12104 tree class;
12106 tree field;
12107 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12108 if (CLASS_FINAL_VARIABLE_P (field))
12109 DECL_FIELD_FINAL_LIIC (field) = 0;
12112 /* Figure whether all final static variable have been initialized. */
12114 static void
12115 check_static_final_variable_assignment_flag (class)
12116 tree class;
12118 tree field;
12120 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12121 if (CLASS_FINAL_VARIABLE_P (field)
12122 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12123 parse_error_context
12124 (DECL_FIELD_FINAL_WFL (field),
12125 "Blank static final variable `%s' may not have been initialized",
12126 IDENTIFIER_POINTER (DECL_NAME (field)));
12129 /* This function marks all final variable locally unassigned. */
12131 static void
12132 reset_final_variable_local_assignment_flag (class)
12133 tree class;
12135 tree field;
12136 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12137 if (FINAL_VARIABLE_P (field))
12138 DECL_FIELD_FINAL_LIIC (field) = 0;
12141 /* Figure whether all final variables have beem initialized in MDECL
12142 and mark MDECL accordingly. */
12144 static void
12145 check_final_variable_local_assignment_flag (class, mdecl)
12146 tree class;
12147 tree mdecl;
12149 tree field;
12150 int initialized = 0;
12151 int non_initialized = 0;
12153 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12154 return;
12156 /* First find out whether all final variables or no final variable
12157 are initialized in this ctor. We don't take into account final
12158 variable that have been initialized upon declaration. */
12159 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12160 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12162 if (DECL_FIELD_FINAL_LIIC (field))
12163 initialized++;
12164 else
12165 non_initialized++;
12168 /* There were no non initialized variable and no initialized variable.
12169 This ctor is fine. */
12170 if (!non_initialized && !initialized)
12171 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12172 /* If no variables have been initialized, that fine. We'll check
12173 later whether this ctor calls a constructor which initializes
12174 them. We mark the ctor as not initializing all its finals. */
12175 else if (initialized == 0)
12176 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12177 /* If we have a mixed bag, then we have a problem. We need to report
12178 all the variables we're not initializing. */
12179 else if (initialized && non_initialized)
12181 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12182 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12183 if (FIELD_FINAL (field)
12184 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12186 parse_error_context
12187 (lookup_cl (mdecl),
12188 "Blank final variable `%s' may not have been initialized in this constructor",
12189 IDENTIFIER_POINTER (DECL_NAME (field)));
12190 DECL_FIELD_FINAL_IERR (field) = 1;
12193 /* Otherwise we know this ctor is initializing all its final
12194 variable. We mark it so. */
12195 else
12196 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12199 /* This function recurses in a simple what through STMT and stops when
12200 it finds a constructor call. It then verifies that the called
12201 constructor initialized its final properly. Return 1 upon success,
12202 0 or -1 otherwise. */
12204 static int
12205 check_final_variable_indirect_assignment (stmt)
12206 tree stmt;
12208 int res;
12209 switch (TREE_CODE (stmt))
12211 case EXPR_WITH_FILE_LOCATION:
12212 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12213 case COMPOUND_EXPR:
12214 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12215 if (res)
12216 return res;
12217 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12218 case SAVE_EXPR:
12219 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12220 case CALL_EXPR:
12222 tree decl = TREE_OPERAND (stmt, 0);
12223 tree fbody;
12225 if (TREE_CODE (decl) != FUNCTION_DECL)
12226 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12227 if (TREE_CODE (decl) != FUNCTION_DECL)
12228 fatal ("Can't find FUNCTION_DECL in CALL_EXPR - check_final_variable_indirect_assignment");
12229 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12230 return 1;
12231 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12232 return -1;
12233 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12234 if (fbody == error_mark_node)
12235 return -1;
12236 fbody = BLOCK_EXPR_BODY (fbody);
12237 return check_final_variable_indirect_assignment (fbody);
12239 default:
12240 break;
12242 return 0;
12245 /* This is the last chance to catch a final variable initialization
12246 problem. This routine will report an error if a final variable was
12247 never (globally) initialized and never reported as not having been
12248 initialized properly. */
12250 static void
12251 check_final_variable_global_assignment_flag (class)
12252 tree class;
12254 tree field, mdecl;
12255 int nnctor = 0;
12257 /* We go through all natural ctors and see whether they're
12258 initializing all their final variables or not. */
12259 current_function_decl = NULL_TREE; /* For the error report. */
12260 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12261 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12263 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12265 /* It doesn't. Maybe it calls a constructor that initializes
12266 them. find out. */
12267 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12268 if (fbody == error_mark_node)
12269 continue;
12270 fbody = BLOCK_EXPR_BODY (fbody);
12271 if (check_final_variable_indirect_assignment (fbody) == 1)
12273 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12274 nnctor++;
12276 else
12277 parse_error_context
12278 (lookup_cl (mdecl),
12279 "Final variable initialization error in this constructor");
12281 else
12282 nnctor++;
12285 /* Finally we catch final variables that never were initialized */
12286 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12287 if (FINAL_VARIABLE_P (field)
12288 /* If the field wasn't initialized upon declaration */
12289 && !DECL_FIELD_FINAL_IUD (field)
12290 /* There wasn't natural ctor in which the field could have been
12291 initialized */
12292 && !nnctor
12293 /* If we never reported a problem with this field */
12294 && !DECL_FIELD_FINAL_IERR (field))
12296 current_function_decl = NULL;
12297 parse_error_context
12298 (DECL_FIELD_FINAL_WFL (field),
12299 "Final variable `%s' hasn't been initialized upon its declaration",
12300 IDENTIFIER_POINTER (DECL_NAME (field)));
12305 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12306 context. */
12308 static int
12309 check_final_assignment (lvalue, wfl)
12310 tree lvalue, wfl;
12312 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12313 return 0;
12315 if (TREE_CODE (lvalue) == COMPONENT_REF
12316 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12317 lvalue = TREE_OPERAND (lvalue, 1);
12319 if (!FIELD_FINAL (lvalue))
12320 return 0;
12322 /* Now the logic. We can modify a final VARIABLE:
12323 1) in finit$, (its declaration was followed by an initialization,)
12324 2) consistently in each natural ctor, if it wasn't initialized in
12325 finit$ or once in <clinit>. In any other cases, an error should be
12326 reported. */
12327 if (DECL_FINIT_P (current_function_decl))
12329 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12330 return 0;
12333 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12334 /* Only if it wasn't given a value upon initialization */
12335 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12336 /* If it was never assigned a value in this constructor */
12337 && !DECL_FIELD_FINAL_LIIC (lvalue))
12339 /* Turn the locally assigned flag on, it will be checked later
12340 on to point out at discrepancies. */
12341 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12342 if (DECL_CLINIT_P (current_function_decl))
12343 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12344 return 0;
12347 /* Other problems should be reported right away. */
12348 parse_error_context
12349 (wfl, "Can't %sassign a value to the final variable `%s'",
12350 (FIELD_STATIC (lvalue) ? "re" : ""),
12351 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12353 /* Note that static field can be initialized once and only once. */
12354 if (FIELD_STATIC (lvalue))
12355 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12357 return 1;
12360 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12361 read. This is needed to avoid circularities in the implementation
12362 of these fields in libjava. */
12364 static tree
12365 maybe_build_primttype_type_ref (rhs, wfl)
12366 tree rhs, wfl;
12368 tree to_return = NULL_TREE;
12369 tree rhs_type = TREE_TYPE (rhs);
12370 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12372 tree n = TREE_OPERAND (rhs, 1);
12373 if (TREE_CODE (n) == VAR_DECL
12374 && DECL_NAME (n) == TYPE_identifier_node
12375 && rhs_type == class_ptr_type
12376 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
12378 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
12379 if (!strncmp (self_name, "java.lang.", 10))
12380 to_return = build_primtype_type_ref (self_name);
12383 return (to_return ? to_return : rhs );
12386 /* 15.25 Assignment operators. */
12388 static tree
12389 patch_assignment (node, wfl_op1, wfl_op2)
12390 tree node;
12391 tree wfl_op1;
12392 tree wfl_op2;
12394 tree rhs = TREE_OPERAND (node, 1);
12395 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12396 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12397 int error_found = 0;
12398 int lvalue_from_array = 0;
12400 /* Can't assign to a (blank) final. */
12401 if (check_final_assignment (lvalue, wfl_op1))
12402 error_found = 1;
12404 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12406 /* Lhs can be a named variable */
12407 if (JDECL_P (lvalue))
12409 lhs_type = TREE_TYPE (lvalue);
12411 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12412 comment on reason why */
12413 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12415 lhs_type = TREE_TYPE (lvalue);
12416 lvalue_from_array = 1;
12418 /* Or a field access */
12419 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12420 lhs_type = TREE_TYPE (lvalue);
12421 /* Or a function return slot */
12422 else if (TREE_CODE (lvalue) == RESULT_DECL)
12423 lhs_type = TREE_TYPE (lvalue);
12424 /* Otherwise, we might want to try to write into an optimized static
12425 final, this is an of a different nature, reported further on. */
12426 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12427 && resolve_expression_name (wfl_op1, &llvalue))
12429 if (!error_found && check_final_assignment (llvalue, wfl_op1))
12431 /* What we should do instead is resetting the all the flags
12432 previously set, exchange lvalue for llvalue and continue. */
12433 error_found = 1;
12434 return error_mark_node;
12436 else
12437 lhs_type = TREE_TYPE (lvalue);
12439 else
12441 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12442 error_found = 1;
12445 rhs_type = TREE_TYPE (rhs);
12446 /* 5.1 Try the assignment conversion for builtin type. */
12447 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12449 /* 5.2 If it failed, try a reference conversion */
12450 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12451 lhs_type = promote_type (rhs_type);
12453 /* 15.25.2 If we have a compound assignment, convert RHS into the
12454 type of the LHS */
12455 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12456 new_rhs = convert (lhs_type, rhs);
12458 /* Explicit cast required. This is an error */
12459 if (!new_rhs)
12461 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12462 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12463 tree wfl;
12464 char operation [32]; /* Max size known */
12466 /* If the assignment is part of a declaration, we use the WFL of
12467 the declared variable to point out the error and call it a
12468 declaration problem. If the assignment is a genuine =
12469 operator, we call is a operator `=' problem, otherwise we
12470 call it an assignment problem. In both of these last cases,
12471 we use the WFL of the operator to indicate the error. */
12473 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12475 wfl = wfl_op1;
12476 strcpy (operation, "declaration");
12478 else
12480 wfl = wfl_operator;
12481 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12482 strcpy (operation, "assignment");
12483 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12484 strcpy (operation, "`return'");
12485 else
12486 strcpy (operation, "`='");
12489 if (!valid_cast_to_p (rhs_type, lhs_type))
12490 parse_error_context
12491 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12492 operation, t1, t2);
12493 else
12494 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12495 operation, t1, t2);
12496 free (t1); free (t2);
12497 error_found = 1;
12500 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12501 if (new_rhs)
12502 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
12504 if (error_found)
12505 return error_mark_node;
12507 /* 10.10: Array Store Exception runtime check */
12508 if (!flag_emit_class_files
12509 && !flag_emit_xref
12510 && lvalue_from_array
12511 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12513 tree check;
12514 tree base = lvalue;
12516 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12517 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12518 base = TREE_OPERAND (lvalue, 0);
12519 else
12521 if (flag_bounds_check)
12522 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12523 else
12524 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12527 /* Build the invocation of _Jv_CheckArrayStore */
12528 new_rhs = save_expr (new_rhs);
12529 check = build (CALL_EXPR, void_type_node,
12530 build_address_of (soft_checkarraystore_node),
12531 tree_cons (NULL_TREE, base,
12532 build_tree_list (NULL_TREE, new_rhs)),
12533 NULL_TREE);
12534 TREE_SIDE_EFFECTS (check) = 1;
12536 /* We have to decide on an insertion point */
12537 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12539 tree t;
12540 if (flag_bounds_check)
12542 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12543 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12544 build (COMPOUND_EXPR, void_type_node, t, check);
12546 else
12547 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12548 check, TREE_OPERAND (lvalue, 1));
12550 else
12552 /* Make sure the bound check will happen before the store check */
12553 if (flag_bounds_check)
12554 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12555 build (COMPOUND_EXPR, void_type_node,
12556 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12557 else
12558 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12562 /* Final locals can be used as case values in switch
12563 statement. Prepare them for this eventuality. */
12564 if (TREE_CODE (lvalue) == VAR_DECL
12565 && LOCAL_FINAL_P (lvalue)
12566 && TREE_CONSTANT (new_rhs)
12567 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12568 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12571 TREE_CONSTANT (lvalue) = 1;
12572 DECL_INITIAL (lvalue) = new_rhs;
12575 TREE_OPERAND (node, 0) = lvalue;
12576 TREE_OPERAND (node, 1) = new_rhs;
12577 TREE_TYPE (node) = lhs_type;
12578 return node;
12581 /* Check that type SOURCE can be cast into type DEST. If the cast
12582 can't occur at all, return 0 otherwise 1. This function is used to
12583 produce accurate error messages on the reasons why an assignment
12584 failed. */
12586 static tree
12587 try_reference_assignconv (lhs_type, rhs)
12588 tree lhs_type, rhs;
12590 tree new_rhs = NULL_TREE;
12591 tree rhs_type = TREE_TYPE (rhs);
12593 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12595 /* `null' may be assigned to any reference type */
12596 if (rhs == null_pointer_node)
12597 new_rhs = null_pointer_node;
12598 /* Try the reference assignment conversion */
12599 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12600 new_rhs = rhs;
12601 /* This is a magic assignment that we process differently */
12602 else if (rhs == soft_exceptioninfo_call_node)
12603 new_rhs = rhs;
12605 return new_rhs;
12608 /* Check that RHS can be converted into LHS_TYPE by the assignment
12609 conversion (5.2), for the cases of RHS being a builtin type. Return
12610 NULL_TREE if the conversion fails or if because RHS isn't of a
12611 builtin type. Return a converted RHS if the conversion is possible. */
12613 static tree
12614 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12615 tree wfl_op1, lhs_type, rhs;
12617 tree new_rhs = NULL_TREE;
12618 tree rhs_type = TREE_TYPE (rhs);
12620 /* Handle boolean specially. */
12621 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12622 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12624 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12625 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12626 new_rhs = rhs;
12629 /* Zero accepted everywhere */
12630 else if (TREE_CODE (rhs) == INTEGER_CST
12631 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12632 && JPRIMITIVE_TYPE_P (rhs_type))
12633 new_rhs = convert (lhs_type, rhs);
12635 /* 5.1.1 Try Identity Conversion,
12636 5.1.2 Try Widening Primitive Conversion */
12637 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12638 new_rhs = convert (lhs_type, rhs);
12640 /* Try a narrowing primitive conversion (5.1.3):
12641 - expression is a constant expression of type int AND
12642 - variable is byte, short or char AND
12643 - The value of the expression is representable in the type of the
12644 variable */
12645 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12646 && (lhs_type == byte_type_node || lhs_type == char_type_node
12647 || lhs_type == short_type_node))
12649 if (int_fits_type_p (rhs, lhs_type))
12650 new_rhs = convert (lhs_type, rhs);
12651 else if (wfl_op1) /* Might be called with a NULL */
12652 parse_warning_context
12653 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12654 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12655 /* Reported a warning that will turn into an error further
12656 down, so we don't return */
12659 return new_rhs;
12662 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12663 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12664 0 is the conversion test fails. This implements parts the method
12665 invocation convertion (5.3). */
12667 static int
12668 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12669 tree lhs_type, rhs_type;
12671 /* 5.1.1: This is the identity conversion part. */
12672 if (lhs_type == rhs_type)
12673 return 1;
12675 /* Reject non primitive types and boolean conversions. */
12676 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12677 return 0;
12679 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12680 than a char can't be converted into a char. Short can't too, but
12681 the < test below takes care of that */
12682 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12683 return 0;
12685 /* Accept all promoted type here. Note, we can't use <= in the test
12686 below, because we still need to bounce out assignments of short
12687 to char and the likes */
12688 if (lhs_type == int_type_node
12689 && (rhs_type == promoted_byte_type_node
12690 || rhs_type == promoted_short_type_node
12691 || rhs_type == promoted_char_type_node
12692 || rhs_type == promoted_boolean_type_node))
12693 return 1;
12695 /* From here, an integral is widened if its precision is smaller
12696 than the precision of the LHS or if the LHS is a floating point
12697 type, or the RHS is a float and the RHS a double. */
12698 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12699 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12700 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12701 || (rhs_type == float_type_node && lhs_type == double_type_node))
12702 return 1;
12704 return 0;
12707 /* Check that something of SOURCE type can be assigned or cast to
12708 something of DEST type at runtime. Return 1 if the operation is
12709 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12710 were SOURCE is cast into DEST, which borrows a lot of the
12711 assignment check. */
12713 static int
12714 valid_ref_assignconv_cast_p (source, dest, cast)
12715 tree source;
12716 tree dest;
12717 int cast;
12719 /* SOURCE or DEST might be null if not from a declared entity. */
12720 if (!source || !dest)
12721 return 0;
12722 if (JNULLP_TYPE_P (source))
12723 return 1;
12724 if (TREE_CODE (source) == POINTER_TYPE)
12725 source = TREE_TYPE (source);
12726 if (TREE_CODE (dest) == POINTER_TYPE)
12727 dest = TREE_TYPE (dest);
12729 /* If source and dest are being compiled from bytecode, they may need to
12730 be loaded. */
12731 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12733 load_class (source, 1);
12734 safe_layout_class (source);
12736 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12738 load_class (dest, 1);
12739 safe_layout_class (dest);
12742 /* Case where SOURCE is a class type */
12743 if (TYPE_CLASS_P (source))
12745 if (TYPE_CLASS_P (dest))
12746 return (source == dest
12747 || inherits_from_p (source, dest)
12748 || (cast && inherits_from_p (dest, source)));
12749 if (TYPE_INTERFACE_P (dest))
12751 /* If doing a cast and SOURCE is final, the operation is
12752 always correct a compile time (because even if SOURCE
12753 does not implement DEST, a subclass of SOURCE might). */
12754 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12755 return 1;
12756 /* Otherwise, SOURCE must implement DEST */
12757 return interface_of_p (dest, source);
12759 /* DEST is an array, cast permited if SOURCE is of Object type */
12760 return (cast && source == object_type_node ? 1 : 0);
12762 if (TYPE_INTERFACE_P (source))
12764 if (TYPE_CLASS_P (dest))
12766 /* If not casting, DEST must be the Object type */
12767 if (!cast)
12768 return dest == object_type_node;
12769 /* We're doing a cast. The cast is always valid is class
12770 DEST is not final, otherwise, DEST must implement SOURCE */
12771 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12772 return 1;
12773 else
12774 return interface_of_p (source, dest);
12776 if (TYPE_INTERFACE_P (dest))
12778 /* If doing a cast, then if SOURCE and DEST contain method
12779 with the same signature but different return type, then
12780 this is a (compile time) error */
12781 if (cast)
12783 tree method_source, method_dest;
12784 tree source_type;
12785 tree source_sig;
12786 tree source_name;
12787 for (method_source = TYPE_METHODS (source); method_source;
12788 method_source = TREE_CHAIN (method_source))
12790 source_sig =
12791 build_java_argument_signature (TREE_TYPE (method_source));
12792 source_type = TREE_TYPE (TREE_TYPE (method_source));
12793 source_name = DECL_NAME (method_source);
12794 for (method_dest = TYPE_METHODS (dest);
12795 method_dest; method_dest = TREE_CHAIN (method_dest))
12796 if (source_sig ==
12797 build_java_argument_signature (TREE_TYPE (method_dest))
12798 && source_name == DECL_NAME (method_dest)
12799 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12800 return 0;
12802 return 1;
12804 else
12805 return source == dest || interface_of_p (dest, source);
12807 else
12809 /* Array */
12810 return (cast
12811 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12812 || (DECL_NAME (TYPE_NAME (source))
12813 == java_io_serializable)));
12816 if (TYPE_ARRAY_P (source))
12818 if (TYPE_CLASS_P (dest))
12819 return dest == object_type_node;
12820 /* Can't cast an array to an interface unless the interface is
12821 java.lang.Cloneable or java.io.Serializable. */
12822 if (TYPE_INTERFACE_P (dest))
12823 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12824 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
12825 else /* Arrays */
12827 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12828 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12830 /* In case of severe errors, they turn out null */
12831 if (!dest_element_type || !source_element_type)
12832 return 0;
12833 if (source_element_type == dest_element_type)
12834 return 1;
12835 return valid_ref_assignconv_cast_p (source_element_type,
12836 dest_element_type, cast);
12838 return 0;
12840 return 0;
12843 static int
12844 valid_cast_to_p (source, dest)
12845 tree source;
12846 tree dest;
12848 if (TREE_CODE (source) == POINTER_TYPE)
12849 source = TREE_TYPE (source);
12850 if (TREE_CODE (dest) == POINTER_TYPE)
12851 dest = TREE_TYPE (dest);
12853 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12854 return valid_ref_assignconv_cast_p (source, dest, 1);
12856 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12857 return 1;
12859 else if (TREE_CODE (source) == BOOLEAN_TYPE
12860 && TREE_CODE (dest) == BOOLEAN_TYPE)
12861 return 1;
12863 return 0;
12866 static tree
12867 do_unary_numeric_promotion (arg)
12868 tree arg;
12870 tree type = TREE_TYPE (arg);
12871 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12872 || TREE_CODE (type) == CHAR_TYPE)
12873 arg = convert (int_type_node, arg);
12874 return arg;
12877 /* Return a non zero value if SOURCE can be converted into DEST using
12878 the method invocation conversion rule (5.3). */
12879 static int
12880 valid_method_invocation_conversion_p (dest, source)
12881 tree dest, source;
12883 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12884 && valid_builtin_assignconv_identity_widening_p (dest, source))
12885 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12886 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12887 && valid_ref_assignconv_cast_p (source, dest, 0)));
12890 /* Build an incomplete binop expression. */
12892 static tree
12893 build_binop (op, op_location, op1, op2)
12894 enum tree_code op;
12895 int op_location;
12896 tree op1, op2;
12898 tree binop = build (op, NULL_TREE, op1, op2);
12899 TREE_SIDE_EFFECTS (binop) = 1;
12900 /* Store the location of the operator, for better error report. The
12901 string of the operator will be rebuild based on the OP value. */
12902 EXPR_WFL_LINECOL (binop) = op_location;
12903 return binop;
12906 /* Build the string of the operator retained by NODE. If NODE is part
12907 of a compound expression, add an '=' at the end of the string. This
12908 function is called when an error needs to be reported on an
12909 operator. The string is returned as a pointer to a static character
12910 buffer. */
12912 static char *
12913 operator_string (node)
12914 tree node;
12916 #define BUILD_OPERATOR_STRING(S) \
12918 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12919 return buffer; \
12922 static char buffer [10];
12923 switch (TREE_CODE (node))
12925 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12926 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12927 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12928 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12929 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12930 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12931 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12932 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12933 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12934 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12935 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12936 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12937 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12938 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12939 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12940 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12941 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12942 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12943 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
12944 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12945 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12946 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12947 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12948 case PREINCREMENT_EXPR: /* Fall through */
12949 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12950 case PREDECREMENT_EXPR: /* Fall through */
12951 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12952 default:
12953 fatal ("unregistered operator %s - operator_string",
12954 tree_code_name [TREE_CODE (node)]);
12956 return NULL;
12957 #undef BUILD_OPERATOR_STRING
12960 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12962 static int
12963 java_decl_equiv (var_acc1, var_acc2)
12964 tree var_acc1, var_acc2;
12966 if (JDECL_P (var_acc1))
12967 return (var_acc1 == var_acc2);
12969 return (TREE_CODE (var_acc1) == COMPONENT_REF
12970 && TREE_CODE (var_acc2) == COMPONENT_REF
12971 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12972 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12973 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12976 /* Return a non zero value if CODE is one of the operators that can be
12977 used in conjunction with the `=' operator in a compound assignment. */
12979 static int
12980 binop_compound_p (code)
12981 enum tree_code code;
12983 int i;
12984 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12985 if (binop_lookup [i] == code)
12986 break;
12988 return i < BINOP_COMPOUND_CANDIDATES;
12991 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12993 static tree
12994 java_refold (t)
12995 tree t;
12997 tree c, b, ns, decl;
12999 if (TREE_CODE (t) != MODIFY_EXPR)
13000 return t;
13002 c = TREE_OPERAND (t, 1);
13003 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13004 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13005 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13006 return t;
13008 /* Now the left branch of the binary operator. */
13009 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13010 if (! (b && TREE_CODE (b) == NOP_EXPR
13011 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13012 return t;
13014 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13015 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13016 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13017 return t;
13019 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13020 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13021 /* It's got to be the an equivalent decl */
13022 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13024 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13025 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13026 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13027 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13028 /* Change the right part of the BINOP_EXPR */
13029 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13032 return t;
13035 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13036 errors but we modify NODE so that it contains the type computed
13037 according to the expression, when it's fixed. Otherwise, we write
13038 error_mark_node as the type. It allows us to further the analysis
13039 of remaining nodes and detects more errors in certain cases. */
13041 static tree
13042 patch_binop (node, wfl_op1, wfl_op2)
13043 tree node;
13044 tree wfl_op1;
13045 tree wfl_op2;
13047 tree op1 = TREE_OPERAND (node, 0);
13048 tree op2 = TREE_OPERAND (node, 1);
13049 tree op1_type = TREE_TYPE (op1);
13050 tree op2_type = TREE_TYPE (op2);
13051 tree prom_type = NULL_TREE, cn;
13052 int code = TREE_CODE (node);
13054 /* If 1, tell the routine that we have to return error_mark_node
13055 after checking for the initialization of the RHS */
13056 int error_found = 0;
13058 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13060 /* If either op<n>_type are NULL, this might be early signs of an
13061 error situation, unless it's too early to tell (in case we're
13062 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13063 correctly so the error can be later on reported accurately. */
13064 if (! (code == PLUS_EXPR || code == NE_EXPR
13065 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13067 tree n;
13068 if (! op1_type)
13070 n = java_complete_tree (op1);
13071 op1_type = TREE_TYPE (n);
13073 if (! op2_type)
13075 n = java_complete_tree (op2);
13076 op2_type = TREE_TYPE (n);
13080 switch (code)
13082 /* 15.16 Multiplicative operators */
13083 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13084 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13085 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13086 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13087 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13089 if (!JNUMERIC_TYPE_P (op1_type))
13090 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13091 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13092 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13093 TREE_TYPE (node) = error_mark_node;
13094 error_found = 1;
13095 break;
13097 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13098 /* Change the division operator if necessary */
13099 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13100 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13102 if (TREE_CODE (prom_type) == INTEGER_TYPE
13103 && flag_use_divide_subroutine
13104 && ! flag_emit_class_files
13105 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13106 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13108 /* This one is more complicated. FLOATs are processed by a
13109 function call to soft_fmod. Duplicate the value of the
13110 COMPOUND_ASSIGN_P flag. */
13111 if (code == TRUNC_MOD_EXPR)
13113 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13114 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13115 TREE_SIDE_EFFECTS (mod)
13116 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13117 return mod;
13119 break;
13121 /* 15.17 Additive Operators */
13122 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13124 /* Operation is valid if either one argument is a string
13125 constant, a String object or a StringBuffer crafted for the
13126 purpose of the a previous usage of the String concatenation
13127 operator */
13129 if (TREE_CODE (op1) == STRING_CST
13130 || TREE_CODE (op2) == STRING_CST
13131 || JSTRING_TYPE_P (op1_type)
13132 || JSTRING_TYPE_P (op2_type)
13133 || IS_CRAFTED_STRING_BUFFER_P (op1)
13134 || IS_CRAFTED_STRING_BUFFER_P (op2))
13135 return build_string_concatenation (op1, op2);
13137 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13138 Numeric Types */
13139 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13141 if (!JNUMERIC_TYPE_P (op1_type))
13142 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13143 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13144 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13145 TREE_TYPE (node) = error_mark_node;
13146 error_found = 1;
13147 break;
13149 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13150 break;
13152 /* 15.18 Shift Operators */
13153 case LSHIFT_EXPR:
13154 case RSHIFT_EXPR:
13155 case URSHIFT_EXPR:
13156 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13158 if (!JINTEGRAL_TYPE_P (op1_type))
13159 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13160 else
13162 if (JNUMERIC_TYPE_P (op2_type))
13163 parse_error_context (wfl_operator,
13164 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13165 operator_string (node),
13166 lang_printable_name (op2_type, 0));
13167 else
13168 parse_error_context (wfl_operator,
13169 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13170 operator_string (node),
13171 lang_printable_name (op2_type, 0));
13173 TREE_TYPE (node) = error_mark_node;
13174 error_found = 1;
13175 break;
13178 /* Unary numeric promotion (5.6.1) is performed on each operand
13179 separatly */
13180 op1 = do_unary_numeric_promotion (op1);
13181 op2 = do_unary_numeric_promotion (op2);
13183 /* The type of the shift expression is the type of the promoted
13184 type of the left-hand operand */
13185 prom_type = TREE_TYPE (op1);
13187 /* Shift int only up to 0x1f and long up to 0x3f */
13188 if (prom_type == int_type_node)
13189 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13190 build_int_2 (0x1f, 0)));
13191 else
13192 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13193 build_int_2 (0x3f, 0)));
13195 /* The >>> operator is a >> operating on unsigned quantities */
13196 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13198 tree to_return;
13199 tree utype = unsigned_type (prom_type);
13200 op1 = convert (utype, op1);
13201 TREE_SET_CODE (node, RSHIFT_EXPR);
13202 TREE_OPERAND (node, 0) = op1;
13203 TREE_OPERAND (node, 1) = op2;
13204 TREE_TYPE (node) = utype;
13205 to_return = convert (prom_type, node);
13206 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13207 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13208 TREE_SIDE_EFFECTS (to_return)
13209 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13210 return to_return;
13212 break;
13214 /* 15.19.1 Type Comparison Operator instaceof */
13215 case INSTANCEOF_EXPR:
13217 TREE_TYPE (node) = boolean_type_node;
13219 if (!(op2_type = resolve_type_during_patch (op2)))
13220 return error_mark_node;
13222 /* The first operand must be a reference type or the null type */
13223 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13224 error_found = 1; /* Error reported further below */
13226 /* The second operand must be a reference type */
13227 if (!JREFERENCE_TYPE_P (op2_type))
13229 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13230 parse_error_context
13231 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13232 lang_printable_name (op2_type, 0));
13233 error_found = 1;
13236 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13238 /* If the first operand is null, the result is always false */
13239 if (op1 == null_pointer_node)
13240 return boolean_false_node;
13241 else if (flag_emit_class_files)
13243 TREE_OPERAND (node, 1) = op2_type;
13244 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13245 return node;
13247 /* Otherwise we have to invoke instance of to figure it out */
13248 else
13249 return build_instanceof (op1, op2_type);
13251 /* There is no way the expression operand can be an instance of
13252 the type operand. This is a compile time error. */
13253 else
13255 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13256 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13257 parse_error_context
13258 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13259 t1, lang_printable_name (op2_type, 0));
13260 free (t1);
13261 error_found = 1;
13264 break;
13266 /* 15.21 Bitwise and Logical Operators */
13267 case BIT_AND_EXPR:
13268 case BIT_XOR_EXPR:
13269 case BIT_IOR_EXPR:
13270 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13271 /* Binary numeric promotion is performed on both operand and the
13272 expression retain that type */
13273 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13275 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13276 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13277 /* The type of the bitwise operator expression is BOOLEAN */
13278 prom_type = boolean_type_node;
13279 else
13281 if (!JINTEGRAL_TYPE_P (op1_type))
13282 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13283 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13284 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13285 TREE_TYPE (node) = error_mark_node;
13286 error_found = 1;
13287 /* Insert a break here if adding thing before the switch's
13288 break for this case */
13290 break;
13292 /* 15.22 Conditional-And Operator */
13293 case TRUTH_ANDIF_EXPR:
13294 /* 15.23 Conditional-Or Operator */
13295 case TRUTH_ORIF_EXPR:
13296 /* Operands must be of BOOLEAN type */
13297 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13298 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13300 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13301 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13302 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13303 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13304 TREE_TYPE (node) = boolean_type_node;
13305 error_found = 1;
13306 break;
13308 /* The type of the conditional operators is BOOLEAN */
13309 prom_type = boolean_type_node;
13310 break;
13312 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13313 case LT_EXPR:
13314 case GT_EXPR:
13315 case LE_EXPR:
13316 case GE_EXPR:
13317 /* The type of each of the operands must be a primitive numeric
13318 type */
13319 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13321 if (!JNUMERIC_TYPE_P (op1_type))
13322 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13323 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13324 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13325 TREE_TYPE (node) = boolean_type_node;
13326 error_found = 1;
13327 break;
13329 /* Binary numeric promotion is performed on the operands */
13330 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13331 /* The type of the relation expression is always BOOLEAN */
13332 prom_type = boolean_type_node;
13333 break;
13335 /* 15.20 Equality Operator */
13336 case EQ_EXPR:
13337 case NE_EXPR:
13338 /* It's time for us to patch the strings. */
13339 if ((cn = patch_string (op1)))
13341 op1 = cn;
13342 op1_type = TREE_TYPE (op1);
13344 if ((cn = patch_string (op2)))
13346 op2 = cn;
13347 op2_type = TREE_TYPE (op2);
13350 /* 15.20.1 Numerical Equality Operators == and != */
13351 /* Binary numeric promotion is performed on the operands */
13352 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13353 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13355 /* 15.20.2 Boolean Equality Operators == and != */
13356 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13357 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13358 ; /* Nothing to do here */
13360 /* 15.20.3 Reference Equality Operators == and != */
13361 /* Types have to be either references or the null type. If
13362 they're references, it must be possible to convert either
13363 type to the other by casting conversion. */
13364 else if (op1 == null_pointer_node || op2 == null_pointer_node
13365 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13366 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13367 || valid_ref_assignconv_cast_p (op2_type,
13368 op1_type, 1))))
13369 ; /* Nothing to do here */
13371 /* Else we have an error figure what can't be converted into
13372 what and report the error */
13373 else
13375 char *t1;
13376 t1 = xstrdup (lang_printable_name (op1_type, 0));
13377 parse_error_context
13378 (wfl_operator,
13379 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13380 operator_string (node), t1,
13381 lang_printable_name (op2_type, 0));
13382 free (t1);
13383 TREE_TYPE (node) = boolean_type_node;
13384 error_found = 1;
13385 break;
13387 prom_type = boolean_type_node;
13388 break;
13391 if (error_found)
13392 return error_mark_node;
13394 TREE_OPERAND (node, 0) = op1;
13395 TREE_OPERAND (node, 1) = op2;
13396 TREE_TYPE (node) = prom_type;
13397 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13399 if (flag_emit_xref)
13400 return node;
13402 /* fold does not respect side-effect order as required for Java but not C.
13403 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13404 * bytecode.
13406 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13407 : ! TREE_SIDE_EFFECTS (node))
13408 node = fold (node);
13409 return node;
13412 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13413 zero value, the value of CSTE comes after the valude of STRING */
13415 static tree
13416 do_merge_string_cste (cste, string, string_len, after)
13417 tree cste;
13418 const char *string;
13419 int string_len, after;
13421 const char *old = TREE_STRING_POINTER (cste);
13422 int old_len = TREE_STRING_LENGTH (cste);
13423 int len = old_len + string_len;
13424 char *new = alloca (len+1);
13426 if (after)
13428 memcpy (new, string, string_len);
13429 memcpy (&new [string_len], old, old_len);
13431 else
13433 memcpy (new, old, old_len);
13434 memcpy (&new [old_len], string, string_len);
13436 new [len] = '\0';
13437 return build_string (len, new);
13440 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13441 new STRING_CST on success, NULL_TREE on failure */
13443 static tree
13444 merge_string_cste (op1, op2, after)
13445 tree op1, op2;
13446 int after;
13448 /* Handle two string constants right away */
13449 if (TREE_CODE (op2) == STRING_CST)
13450 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13451 TREE_STRING_LENGTH (op2), after);
13453 /* Reasonable integer constant can be treated right away */
13454 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13456 static const char *boolean_true = "true";
13457 static const char *boolean_false = "false";
13458 static const char *null_pointer = "null";
13459 char ch[3];
13460 const char *string;
13462 if (op2 == boolean_true_node)
13463 string = boolean_true;
13464 else if (op2 == boolean_false_node)
13465 string = boolean_false;
13466 else if (op2 == null_pointer_node)
13467 string = null_pointer;
13468 else if (TREE_TYPE (op2) == char_type_node)
13470 ch[0] = (char )TREE_INT_CST_LOW (op2);
13471 ch[1] = '\0';
13472 string = ch;
13474 else
13475 string = print_int_node (op2);
13477 return do_merge_string_cste (op1, string, strlen (string), after);
13479 return NULL_TREE;
13482 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13483 has to be a STRING_CST and the other part must be a STRING_CST or a
13484 INTEGRAL constant. Return a new STRING_CST if the operation
13485 succeed, NULL_TREE otherwise.
13487 If the case we want to optimize for space, we might want to return
13488 NULL_TREE for each invocation of this routine. FIXME */
13490 static tree
13491 string_constant_concatenation (op1, op2)
13492 tree op1, op2;
13494 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13496 tree string, rest;
13497 int invert;
13499 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13500 rest = (string == op1 ? op2 : op1);
13501 invert = (string == op1 ? 0 : 1 );
13503 /* Walk REST, only if it looks reasonable */
13504 if (TREE_CODE (rest) != STRING_CST
13505 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13506 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13507 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13509 rest = java_complete_tree (rest);
13510 if (rest == error_mark_node)
13511 return error_mark_node;
13512 rest = fold (rest);
13514 return merge_string_cste (string, rest, invert);
13516 return NULL_TREE;
13519 /* Implement the `+' operator. Does static optimization if possible,
13520 otherwise create (if necessary) and append elements to a
13521 StringBuffer. The StringBuffer will be carried around until it is
13522 used for a function call or an assignment. Then toString() will be
13523 called on it to turn it into a String object. */
13525 static tree
13526 build_string_concatenation (op1, op2)
13527 tree op1, op2;
13529 tree result;
13530 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13532 if (flag_emit_xref)
13533 return build (PLUS_EXPR, string_type_node, op1, op2);
13535 /* Try to do some static optimization */
13536 if ((result = string_constant_concatenation (op1, op2)))
13537 return result;
13539 /* Discard empty strings on either side of the expression */
13540 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13542 op1 = op2;
13543 op2 = NULL_TREE;
13545 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13546 op2 = NULL_TREE;
13548 /* If operands are string constant, turn then into object references */
13549 if (TREE_CODE (op1) == STRING_CST)
13550 op1 = patch_string_cst (op1);
13551 if (op2 && TREE_CODE (op2) == STRING_CST)
13552 op2 = patch_string_cst (op2);
13554 /* If either one of the constant is null and the other non null
13555 operand is a String object, return it. */
13556 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13557 return op1;
13559 /* If OP1 isn't already a StringBuffer, create and
13560 initialize a new one */
13561 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13563 /* Two solutions here:
13564 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13565 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13566 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13567 op1 = BUILD_STRING_BUFFER (op1);
13568 else
13570 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13571 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13575 if (op2)
13577 /* OP1 is no longer the last node holding a crafted StringBuffer */
13578 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13579 /* Create a node for `{new...,xxx}.append (op2)' */
13580 if (op2)
13581 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13584 /* Mark the last node holding a crafted StringBuffer */
13585 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13587 TREE_SIDE_EFFECTS (op1) = side_effects;
13588 return op1;
13591 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13592 StringBuffer. If no string were found to be patched, return
13593 NULL. */
13595 static tree
13596 patch_string (node)
13597 tree node;
13599 if (node == error_mark_node)
13600 return error_mark_node;
13601 if (TREE_CODE (node) == STRING_CST)
13602 return patch_string_cst (node);
13603 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13605 int saved = ctxp->explicit_constructor_p;
13606 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13607 tree ret;
13608 /* Temporary disable forbid the use of `this'. */
13609 ctxp->explicit_constructor_p = 0;
13610 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13611 /* String concatenation arguments must be evaluated in order too. */
13612 ret = force_evaluation_order (ret);
13613 /* Restore it at its previous value */
13614 ctxp->explicit_constructor_p = saved;
13615 return ret;
13617 return NULL_TREE;
13620 /* Build the internal representation of a string constant. */
13622 static tree
13623 patch_string_cst (node)
13624 tree node;
13626 int location;
13627 if (! flag_emit_class_files)
13629 node = get_identifier (TREE_STRING_POINTER (node));
13630 location = alloc_name_constant (CONSTANT_String, node);
13631 node = build_ref_from_constant_pool (location);
13633 TREE_TYPE (node) = string_ptr_type_node;
13634 TREE_CONSTANT (node) = 1;
13635 return node;
13638 /* Build an incomplete unary operator expression. */
13640 static tree
13641 build_unaryop (op_token, op_location, op1)
13642 int op_token, op_location;
13643 tree op1;
13645 enum tree_code op;
13646 tree unaryop;
13647 switch (op_token)
13649 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13650 case MINUS_TK: op = NEGATE_EXPR; break;
13651 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13652 case NOT_TK: op = BIT_NOT_EXPR; break;
13653 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13654 op_token);
13657 unaryop = build1 (op, NULL_TREE, op1);
13658 TREE_SIDE_EFFECTS (unaryop) = 1;
13659 /* Store the location of the operator, for better error report. The
13660 string of the operator will be rebuild based on the OP value. */
13661 EXPR_WFL_LINECOL (unaryop) = op_location;
13662 return unaryop;
13665 /* Special case for the ++/-- operators, since they require an extra
13666 argument to build, which is set to NULL and patched
13667 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13669 static tree
13670 build_incdec (op_token, op_location, op1, is_post_p)
13671 int op_token, op_location;
13672 tree op1;
13673 int is_post_p;
13675 static enum tree_code lookup [2][2] =
13677 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13678 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13680 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13681 NULL_TREE, op1, NULL_TREE);
13682 TREE_SIDE_EFFECTS (node) = 1;
13683 /* Store the location of the operator, for better error report. The
13684 string of the operator will be rebuild based on the OP value. */
13685 EXPR_WFL_LINECOL (node) = op_location;
13686 return node;
13689 /* Build an incomplete cast operator, based on the use of the
13690 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13691 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13692 though its type is already set. */
13694 static tree
13695 build_cast (location, type, exp)
13696 int location;
13697 tree type, exp;
13699 tree node = build1 (CONVERT_EXPR, type, exp);
13700 EXPR_WFL_LINECOL (node) = location;
13701 return node;
13704 /* Build an incomplete class reference operator. */
13705 static tree
13706 build_incomplete_class_ref (location, class_name)
13707 int location;
13708 tree class_name;
13710 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13711 EXPR_WFL_LINECOL (node) = location;
13712 return node;
13715 /* Complete an incomplete class reference operator. */
13716 static tree
13717 patch_incomplete_class_ref (node)
13718 tree node;
13720 tree type = TREE_OPERAND (node, 0);
13721 tree ref_type;
13723 if (!(ref_type = resolve_type_during_patch (type)))
13724 return error_mark_node;
13726 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13728 /* A class referenced by `foo.class' is initialized. */
13729 return build_class_init (ref_type, build_class_ref (ref_type));
13732 /* If we're emitting class files and we have to deal with non
13733 primitive types, we invoke (and consider generating) the
13734 synthetic static method `class$'. */
13735 if (!TYPE_DOT_CLASS (current_class))
13736 build_dot_class_method (current_class);
13737 ref_type = build_dot_class_method_invocation (ref_type);
13738 return java_complete_tree (ref_type);
13741 /* 15.14 Unary operators. We return error_mark_node in case of error,
13742 but preserve the type of NODE if the type is fixed. */
13744 static tree
13745 patch_unaryop (node, wfl_op)
13746 tree node;
13747 tree wfl_op;
13749 tree op = TREE_OPERAND (node, 0);
13750 tree op_type = TREE_TYPE (op);
13751 tree prom_type = NULL_TREE, value, decl;
13752 int outer_field_flag = 0;
13753 int code = TREE_CODE (node);
13754 int error_found = 0;
13756 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13758 switch (code)
13760 /* 15.13.2 Postfix Increment Operator ++ */
13761 case POSTINCREMENT_EXPR:
13762 /* 15.13.3 Postfix Increment Operator -- */
13763 case POSTDECREMENT_EXPR:
13764 /* 15.14.1 Prefix Increment Operator ++ */
13765 case PREINCREMENT_EXPR:
13766 /* 15.14.2 Prefix Decrement Operator -- */
13767 case PREDECREMENT_EXPR:
13768 op = decl = strip_out_static_field_access_decl (op);
13769 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13770 /* We might be trying to change an outer field accessed using
13771 access method. */
13772 if (outer_field_flag)
13774 /* Retrieve the decl of the field we're trying to access. We
13775 do that by first retrieving the function we would call to
13776 access the field. It has been already verified that this
13777 field isn't final */
13778 if (flag_emit_class_files)
13779 decl = TREE_OPERAND (op, 0);
13780 else
13781 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13782 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13784 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13785 else if (!JDECL_P (decl)
13786 && TREE_CODE (decl) != COMPONENT_REF
13787 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13788 && TREE_CODE (decl) != INDIRECT_REF
13789 && !(TREE_CODE (decl) == COMPOUND_EXPR
13790 && TREE_OPERAND (decl, 1)
13791 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13793 tree lvalue;
13794 /* Before screaming, check that we're not in fact trying to
13795 increment a optimized static final access, in which case
13796 we issue an different error message. */
13797 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13798 && resolve_expression_name (wfl_op, &lvalue)
13799 && check_final_assignment (lvalue, wfl_op)))
13800 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13801 operator_string (node));
13802 TREE_TYPE (node) = error_mark_node;
13803 error_found = 1;
13806 if (check_final_assignment (op, wfl_op))
13807 error_found = 1;
13809 /* From now on, we know that op if a variable and that it has a
13810 valid wfl. We use wfl_op to locate errors related to the
13811 ++/-- operand. */
13812 else if (!JNUMERIC_TYPE_P (op_type))
13814 parse_error_context
13815 (wfl_op, "Invalid argument type `%s' to `%s'",
13816 lang_printable_name (op_type, 0), operator_string (node));
13817 TREE_TYPE (node) = error_mark_node;
13818 error_found = 1;
13820 else
13822 /* Before the addition, binary numeric promotion is performed on
13823 both operands, if really necessary */
13824 if (JINTEGRAL_TYPE_P (op_type))
13826 value = build_int_2 (1, 0);
13827 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13829 else
13831 value = build_int_2 (1, 0);
13832 TREE_TYPE (node) =
13833 binary_numeric_promotion (op_type,
13834 TREE_TYPE (value), &op, &value);
13837 /* We remember we might be accessing an outer field */
13838 if (outer_field_flag)
13840 /* We re-generate an access to the field */
13841 value = build (PLUS_EXPR, TREE_TYPE (op),
13842 build_outer_field_access (wfl_op, decl), value);
13844 /* And we patch the original access$() into a write
13845 with plus_op as a rhs */
13846 return outer_field_access_fix (node, op, value);
13849 /* And write back into the node. */
13850 TREE_OPERAND (node, 0) = op;
13851 TREE_OPERAND (node, 1) = value;
13852 /* Convert the overall back into its original type, if
13853 necessary, and return */
13854 if (JINTEGRAL_TYPE_P (op_type))
13855 return fold (node);
13856 else
13857 return fold (convert (op_type, node));
13859 break;
13861 /* 15.14.3 Unary Plus Operator + */
13862 case UNARY_PLUS_EXPR:
13863 /* 15.14.4 Unary Minus Operator - */
13864 case NEGATE_EXPR:
13865 if (!JNUMERIC_TYPE_P (op_type))
13867 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13868 TREE_TYPE (node) = error_mark_node;
13869 error_found = 1;
13871 /* Unary numeric promotion is performed on operand */
13872 else
13874 op = do_unary_numeric_promotion (op);
13875 prom_type = TREE_TYPE (op);
13876 if (code == UNARY_PLUS_EXPR)
13877 return fold (op);
13879 break;
13881 /* 15.14.5 Bitwise Complement Operator ~ */
13882 case BIT_NOT_EXPR:
13883 if (!JINTEGRAL_TYPE_P (op_type))
13885 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13886 TREE_TYPE (node) = error_mark_node;
13887 error_found = 1;
13889 else
13891 op = do_unary_numeric_promotion (op);
13892 prom_type = TREE_TYPE (op);
13894 break;
13896 /* 15.14.6 Logical Complement Operator ! */
13897 case TRUTH_NOT_EXPR:
13898 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13900 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
13901 /* But the type is known. We will report an error if further
13902 attempt of a assignment is made with this rhs */
13903 TREE_TYPE (node) = boolean_type_node;
13904 error_found = 1;
13906 else
13907 prom_type = boolean_type_node;
13908 break;
13910 /* 15.15 Cast Expression */
13911 case CONVERT_EXPR:
13912 value = patch_cast (node, wfl_operator);
13913 if (value == error_mark_node)
13915 /* If this cast is part of an assignment, we tell the code
13916 that deals with it not to complain about a mismatch,
13917 because things have been cast, anyways */
13918 TREE_TYPE (node) = error_mark_node;
13919 error_found = 1;
13921 else
13923 value = fold (value);
13924 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13925 return value;
13927 break;
13930 if (error_found)
13931 return error_mark_node;
13933 /* There are cases where node has been replaced by something else
13934 and we don't end up returning here: UNARY_PLUS_EXPR,
13935 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
13936 TREE_OPERAND (node, 0) = fold (op);
13937 TREE_TYPE (node) = prom_type;
13938 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
13939 return fold (node);
13942 /* Generic type resolution that sometimes takes place during node
13943 patching. Returned the resolved type or generate an error
13944 message. Return the resolved type or NULL_TREE. */
13946 static tree
13947 resolve_type_during_patch (type)
13948 tree type;
13950 if (unresolved_type_p (type, NULL))
13952 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
13953 if (!type_decl)
13955 parse_error_context (type,
13956 "Class `%s' not found in type declaration",
13957 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13958 return NULL_TREE;
13960 else
13962 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13963 return TREE_TYPE (type_decl);
13966 return type;
13968 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
13969 found. Otherwise NODE or something meant to replace it is returned. */
13971 static tree
13972 patch_cast (node, wfl_op)
13973 tree node;
13974 tree wfl_op;
13976 tree op = TREE_OPERAND (node, 0);
13977 tree op_type = TREE_TYPE (op);
13978 tree cast_type = TREE_TYPE (node);
13979 char *t1;
13981 /* First resolve OP_TYPE if unresolved */
13982 if (!(cast_type = resolve_type_during_patch (cast_type)))
13983 return error_mark_node;
13985 /* Check on cast that are proven correct at compile time */
13986 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13988 /* Same type */
13989 if (cast_type == op_type)
13990 return node;
13992 /* float and double type are converted to the original type main
13993 variant and then to the target type. */
13994 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13995 op = convert (integer_type_node, op);
13997 /* Try widening/narowwing convertion. Potentially, things need
13998 to be worked out in gcc so we implement the extreme cases
13999 correctly. fold_convert() needs to be fixed. */
14000 return convert (cast_type, op);
14003 /* It's also valid to cast a boolean into a boolean */
14004 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14005 return node;
14007 /* null can be casted to references */
14008 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14009 return build_null_of_type (cast_type);
14011 /* The remaining legal casts involve conversion between reference
14012 types. Check for their compile time correctness. */
14013 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14014 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14016 TREE_TYPE (node) = promote_type (cast_type);
14017 /* Now, the case can be determined correct at compile time if
14018 OP_TYPE can be converted into CAST_TYPE by assignment
14019 conversion (5.2) */
14021 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14023 TREE_SET_CODE (node, NOP_EXPR);
14024 return node;
14027 if (flag_emit_class_files)
14029 TREE_SET_CODE (node, CONVERT_EXPR);
14030 return node;
14033 /* The cast requires a run-time check */
14034 return build (CALL_EXPR, promote_type (cast_type),
14035 build_address_of (soft_checkcast_node),
14036 tree_cons (NULL_TREE, build_class_ref (cast_type),
14037 build_tree_list (NULL_TREE, op)),
14038 NULL_TREE);
14041 /* Any other casts are proven incorrect at compile time */
14042 t1 = xstrdup (lang_printable_name (op_type, 0));
14043 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14044 t1, lang_printable_name (cast_type, 0));
14045 free (t1);
14046 return error_mark_node;
14049 /* Build a null constant and give it the type TYPE. */
14051 static tree
14052 build_null_of_type (type)
14053 tree type;
14055 tree node = build_int_2 (0, 0);
14056 TREE_TYPE (node) = promote_type (type);
14057 return node;
14060 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14061 a list of indices. */
14062 static tree
14063 build_array_ref (location, array, index)
14064 int location;
14065 tree array, index;
14067 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14068 EXPR_WFL_LINECOL (node) = location;
14069 return node;
14072 /* 15.12 Array Access Expression */
14074 static tree
14075 patch_array_ref (node)
14076 tree node;
14078 tree array = TREE_OPERAND (node, 0);
14079 tree array_type = TREE_TYPE (array);
14080 tree index = TREE_OPERAND (node, 1);
14081 tree index_type = TREE_TYPE (index);
14082 int error_found = 0;
14084 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14086 if (TREE_CODE (array_type) == POINTER_TYPE)
14087 array_type = TREE_TYPE (array_type);
14089 /* The array reference must be an array */
14090 if (!TYPE_ARRAY_P (array_type))
14092 parse_error_context
14093 (wfl_operator,
14094 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14095 lang_printable_name (array_type, 0));
14096 TREE_TYPE (node) = error_mark_node;
14097 error_found = 1;
14100 /* The array index undergoes unary numeric promotion. The promoted
14101 type must be int */
14102 index = do_unary_numeric_promotion (index);
14103 if (TREE_TYPE (index) != int_type_node)
14105 if (valid_cast_to_p (index_type, int_type_node))
14106 parse_error_context (wfl_operator,
14107 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14108 lang_printable_name (index_type, 0));
14109 else
14110 parse_error_context (wfl_operator,
14111 "Incompatible type for `[]'. Can't convert `%s' to `int'",
14112 lang_printable_name (index_type, 0));
14113 TREE_TYPE (node) = error_mark_node;
14114 error_found = 1;
14117 if (error_found)
14118 return error_mark_node;
14120 array_type = TYPE_ARRAY_ELEMENT (array_type);
14122 if (flag_emit_class_files || flag_emit_xref)
14124 TREE_OPERAND (node, 0) = array;
14125 TREE_OPERAND (node, 1) = index;
14127 else
14129 /* The save_expr is for correct evaluation order. It would be cleaner
14130 to use force_evaluation_order (see comment there), but that is
14131 difficult when we also have to deal with bounds checking. */
14132 if (TREE_SIDE_EFFECTS (index))
14133 array = save_expr (array);
14134 node = build_java_arrayaccess (array, array_type, index);
14135 if (TREE_SIDE_EFFECTS (index))
14136 node = build (COMPOUND_EXPR, array_type, array, node);
14138 TREE_TYPE (node) = array_type;
14139 return node;
14142 /* 15.9 Array Creation Expressions */
14144 static tree
14145 build_newarray_node (type, dims, extra_dims)
14146 tree type;
14147 tree dims;
14148 int extra_dims;
14150 tree node =
14151 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14152 build_int_2 (extra_dims, 0));
14153 return node;
14156 static tree
14157 patch_newarray (node)
14158 tree node;
14160 tree type = TREE_OPERAND (node, 0);
14161 tree dims = TREE_OPERAND (node, 1);
14162 tree cdim, array_type;
14163 int error_found = 0;
14164 int ndims = 0;
14165 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14167 /* Dimension types are verified. It's better for the types to be
14168 verified in order. */
14169 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14171 int dim_error = 0;
14172 tree dim = TREE_VALUE (cdim);
14174 /* Dim might have been saved during its evaluation */
14175 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14177 /* The type of each specified dimension must be an integral type. */
14178 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14179 dim_error = 1;
14181 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14182 promoted type must be int. */
14183 else
14185 dim = do_unary_numeric_promotion (dim);
14186 if (TREE_TYPE (dim) != int_type_node)
14187 dim_error = 1;
14190 /* Report errors on types here */
14191 if (dim_error)
14193 parse_error_context
14194 (TREE_PURPOSE (cdim),
14195 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14196 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14197 "Explicit cast needed to" : "Can't"),
14198 lang_printable_name (TREE_TYPE (dim), 0));
14199 error_found = 1;
14202 TREE_PURPOSE (cdim) = NULL_TREE;
14205 /* Resolve array base type if unresolved */
14206 if (!(type = resolve_type_during_patch (type)))
14207 error_found = 1;
14209 if (error_found)
14211 /* We don't want further evaluation of this bogus array creation
14212 operation */
14213 TREE_TYPE (node) = error_mark_node;
14214 return error_mark_node;
14217 /* Set array_type to the actual (promoted) array type of the result. */
14218 if (TREE_CODE (type) == RECORD_TYPE)
14219 type = build_pointer_type (type);
14220 while (--xdims >= 0)
14222 type = promote_type (build_java_array_type (type, -1));
14224 dims = nreverse (dims);
14225 array_type = type;
14226 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14228 type = array_type;
14229 array_type
14230 = build_java_array_type (type,
14231 TREE_CODE (cdim) == INTEGER_CST
14232 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14233 : -1);
14234 array_type = promote_type (array_type);
14236 dims = nreverse (dims);
14238 /* The node is transformed into a function call. Things are done
14239 differently according to the number of dimensions. If the number
14240 of dimension is equal to 1, then the nature of the base type
14241 (primitive or not) matters. */
14242 if (ndims == 1)
14243 return build_new_array (type, TREE_VALUE (dims));
14245 /* Can't reuse what's already written in expr.c because it uses the
14246 JVM stack representation. Provide a build_multianewarray. FIXME */
14247 return build (CALL_EXPR, array_type,
14248 build_address_of (soft_multianewarray_node),
14249 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14250 tree_cons (NULL_TREE,
14251 build_int_2 (ndims, 0), dims )),
14252 NULL_TREE);
14255 /* 10.6 Array initializer. */
14257 /* Build a wfl for array element that don't have one, so we can
14258 pin-point errors. */
14260 static tree
14261 maybe_build_array_element_wfl (node)
14262 tree node;
14264 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14265 return build_expr_wfl (NULL_TREE, ctxp->filename,
14266 ctxp->elc.line, ctxp->elc.prev_col);
14267 else
14268 return NULL_TREE;
14271 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14272 identification of initialized arrays easier to detect during walk
14273 and expansion. */
14275 static tree
14276 build_new_array_init (location, values)
14277 int location;
14278 tree values;
14280 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14281 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14282 EXPR_WFL_LINECOL (to_return) = location;
14283 return to_return;
14286 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14287 occurred. Otherwise return NODE after having set its type
14288 appropriately. */
14290 static tree
14291 patch_new_array_init (type, node)
14292 tree type, node;
14294 int error_seen = 0;
14295 tree current, element_type;
14296 HOST_WIDE_INT length;
14297 int all_constant = 1;
14298 tree init = TREE_OPERAND (node, 0);
14300 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14302 parse_error_context (node,
14303 "Invalid array initializer for non-array type `%s'",
14304 lang_printable_name (type, 1));
14305 return error_mark_node;
14307 type = TREE_TYPE (type);
14308 element_type = TYPE_ARRAY_ELEMENT (type);
14310 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14312 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14313 current; length++, current = TREE_CHAIN (current))
14315 tree elt = TREE_VALUE (current);
14316 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14318 error_seen |= array_constructor_check_entry (element_type, current);
14319 elt = TREE_VALUE (current);
14320 /* When compiling to native code, STRING_CST is converted to
14321 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14322 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14323 all_constant = 0;
14325 else
14327 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14328 TREE_PURPOSE (current) = NULL_TREE;
14329 all_constant = 0;
14331 if (elt && TREE_CODE (elt) == TREE_LIST
14332 && TREE_VALUE (elt) == error_mark_node)
14333 error_seen = 1;
14336 if (error_seen)
14337 return error_mark_node;
14339 /* Create a new type. We can't reuse the one we have here by
14340 patching its dimension because it originally is of dimension -1
14341 hence reused by gcc. This would prevent triangular arrays. */
14342 type = build_java_array_type (element_type, length);
14343 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14344 TREE_TYPE (node) = promote_type (type);
14345 TREE_CONSTANT (init) = all_constant;
14346 TREE_CONSTANT (node) = all_constant;
14347 return node;
14350 /* Verify that one entry of the initializer element list can be
14351 assigned to the array base type. Report 1 if an error occurred, 0
14352 otherwise. */
14354 static int
14355 array_constructor_check_entry (type, entry)
14356 tree type, entry;
14358 char *array_type_string = NULL; /* For error reports */
14359 tree value, type_value, new_value, wfl_value, patched;
14360 int error_seen = 0;
14362 new_value = NULL_TREE;
14363 wfl_value = TREE_VALUE (entry);
14365 value = java_complete_tree (TREE_VALUE (entry));
14366 /* patch_string return error_mark_node if arg is error_mark_node */
14367 if ((patched = patch_string (value)))
14368 value = patched;
14369 if (value == error_mark_node)
14370 return 1;
14372 type_value = TREE_TYPE (value);
14374 /* At anytime, try_builtin_assignconv can report a warning on
14375 constant overflow during narrowing. */
14376 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14377 new_value = try_builtin_assignconv (wfl_operator, type, value);
14378 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14379 type_value = promote_type (type);
14381 /* Check and report errors */
14382 if (!new_value)
14384 const char *msg = (!valid_cast_to_p (type_value, type) ?
14385 "Can't" : "Explicit cast needed to");
14386 if (!array_type_string)
14387 array_type_string = xstrdup (lang_printable_name (type, 1));
14388 parse_error_context
14389 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14390 msg, lang_printable_name (type_value, 1), array_type_string);
14391 error_seen = 1;
14394 if (new_value)
14396 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
14397 TREE_VALUE (entry) = new_value;
14400 if (array_type_string)
14401 free (array_type_string);
14403 TREE_PURPOSE (entry) = NULL_TREE;
14404 return error_seen;
14407 static tree
14408 build_this (location)
14409 int location;
14411 tree node = build_wfl_node (this_identifier_node);
14412 TREE_SET_CODE (node, THIS_EXPR);
14413 EXPR_WFL_LINECOL (node) = location;
14414 return node;
14417 /* 14.15 The return statement. It builds a modify expression that
14418 assigns the returned value to the RESULT_DECL that hold the value
14419 to be returned. */
14421 static tree
14422 build_return (location, op)
14423 int location;
14424 tree op;
14426 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14427 EXPR_WFL_LINECOL (node) = location;
14428 node = build_debugable_stmt (location, node);
14429 return node;
14432 static tree
14433 patch_return (node)
14434 tree node;
14436 tree return_exp = TREE_OPERAND (node, 0);
14437 tree meth = current_function_decl;
14438 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14439 int error_found = 0;
14441 TREE_TYPE (node) = error_mark_node;
14442 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14444 /* It's invalid to have a return value within a function that is
14445 declared with the keyword void or that is a constructor */
14446 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14447 error_found = 1;
14449 /* It's invalid to use a return statement in a static block */
14450 if (DECL_CLINIT_P (current_function_decl))
14451 error_found = 1;
14453 /* It's invalid to have a no return value within a function that
14454 isn't declared with the keyword `void' */
14455 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14456 error_found = 2;
14458 if (in_instance_initializer)
14459 error_found = 1;
14461 if (error_found)
14463 if (in_instance_initializer)
14464 parse_error_context (wfl_operator,
14465 "`return' inside instance initializer");
14467 else if (DECL_CLINIT_P (current_function_decl))
14468 parse_error_context (wfl_operator,
14469 "`return' inside static initializer");
14471 else if (!DECL_CONSTRUCTOR_P (meth))
14473 char *t = xstrdup (lang_printable_name (mtype, 0));
14474 parse_error_context (wfl_operator,
14475 "`return' with%s value from `%s %s'",
14476 (error_found == 1 ? "" : "out"),
14477 t, lang_printable_name (meth, 0));
14478 free (t);
14480 else
14481 parse_error_context (wfl_operator,
14482 "`return' with value from constructor `%s'",
14483 lang_printable_name (meth, 0));
14484 return error_mark_node;
14487 /* If we have a return_exp, build a modify expression and expand
14488 it. Note: at that point, the assignment is declared valid, but we
14489 may want to carry some more hacks */
14490 if (return_exp)
14492 tree exp = java_complete_tree (return_exp);
14493 tree modify, patched;
14495 /* If the function returned value and EXP are booleans, EXP has
14496 to be converted into the type of DECL_RESULT, which is integer
14497 (see complete_start_java_method) */
14498 if (TREE_TYPE (exp) == boolean_type_node &&
14499 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14500 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14502 /* `null' can be assigned to a function returning a reference */
14503 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14504 exp == null_pointer_node)
14505 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14507 if ((patched = patch_string (exp)))
14508 exp = patched;
14510 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14511 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14512 modify = java_complete_tree (modify);
14514 if (modify != error_mark_node)
14516 TREE_SIDE_EFFECTS (modify) = 1;
14517 TREE_OPERAND (node, 0) = modify;
14519 else
14520 return error_mark_node;
14522 TREE_TYPE (node) = void_type_node;
14523 TREE_SIDE_EFFECTS (node) = 1;
14524 return node;
14527 /* 14.8 The if Statement */
14529 static tree
14530 build_if_else_statement (location, expression, if_body, else_body)
14531 int location;
14532 tree expression, if_body, else_body;
14534 tree node;
14535 if (!else_body)
14536 else_body = empty_stmt_node;
14537 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14538 EXPR_WFL_LINECOL (node) = location;
14539 node = build_debugable_stmt (location, node);
14540 return node;
14543 static tree
14544 patch_if_else_statement (node)
14545 tree node;
14547 tree expression = TREE_OPERAND (node, 0);
14549 TREE_TYPE (node) = error_mark_node;
14550 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14552 /* The type of expression must be boolean */
14553 if (TREE_TYPE (expression) != boolean_type_node
14554 && TREE_TYPE (expression) != promoted_boolean_type_node)
14556 parse_error_context
14557 (wfl_operator,
14558 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14559 lang_printable_name (TREE_TYPE (expression), 0));
14560 return error_mark_node;
14563 TREE_TYPE (node) = void_type_node;
14564 TREE_SIDE_EFFECTS (node) = 1;
14565 CAN_COMPLETE_NORMALLY (node)
14566 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14567 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
14568 return node;
14571 /* 14.6 Labeled Statements */
14573 /* Action taken when a lableled statement is parsed. a new
14574 LABELED_BLOCK_EXPR is created. No statement is attached to the
14575 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14577 static tree
14578 build_labeled_block (location, label)
14579 int location;
14580 tree label;
14582 tree label_name ;
14583 tree label_decl, node;
14584 if (label == NULL_TREE || label == continue_identifier_node)
14585 label_name = label;
14586 else
14588 label_name = merge_qualified_name (label_id, label);
14589 /* Issue an error if we try to reuse a label that was previously
14590 declared */
14591 if (IDENTIFIER_LOCAL_VALUE (label_name))
14593 EXPR_WFL_LINECOL (wfl_operator) = location;
14594 parse_error_context (wfl_operator,
14595 "Declaration of `%s' shadows a previous label declaration",
14596 IDENTIFIER_POINTER (label));
14597 EXPR_WFL_LINECOL (wfl_operator) =
14598 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14599 parse_error_context (wfl_operator,
14600 "This is the location of the previous declaration of label `%s'",
14601 IDENTIFIER_POINTER (label));
14602 java_error_count--;
14606 label_decl = create_label_decl (label_name);
14607 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14608 EXPR_WFL_LINECOL (node) = location;
14609 TREE_SIDE_EFFECTS (node) = 1;
14610 return node;
14613 /* A labeled statement LBE is attached a statement. */
14615 static tree
14616 finish_labeled_statement (lbe, statement)
14617 tree lbe; /* Labeled block expr */
14618 tree statement;
14620 /* In anyways, tie the loop to its statement */
14621 LABELED_BLOCK_BODY (lbe) = statement;
14622 pop_labeled_block ();
14623 POP_LABELED_BLOCK ();
14624 return lbe;
14627 /* 14.10, 14.11, 14.12 Loop Statements */
14629 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14630 list. */
14632 static tree
14633 build_new_loop (loop_body)
14634 tree loop_body;
14636 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14637 TREE_SIDE_EFFECTS (loop) = 1;
14638 PUSH_LOOP (loop);
14639 return loop;
14642 /* Create a loop body according to the following structure:
14643 COMPOUND_EXPR
14644 COMPOUND_EXPR (loop main body)
14645 EXIT_EXPR (this order is for while/for loops.
14646 LABELED_BLOCK_EXPR the order is reversed for do loops)
14647 LABEL_DECL (a continue occuring here branches at the
14648 BODY end of this labeled block)
14649 INCREMENT (if any)
14651 REVERSED, if non zero, tells that the loop condition expr comes
14652 after the body, like in the do-while loop.
14654 To obtain a loop, the loop body structure described above is
14655 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14657 LABELED_BLOCK_EXPR
14658 LABEL_DECL (use this label to exit the loop)
14659 LOOP_EXPR
14660 <structure described above> */
14662 static tree
14663 build_loop_body (location, condition, reversed)
14664 int location;
14665 tree condition;
14666 int reversed;
14668 tree first, second, body;
14670 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14671 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14672 condition = build_debugable_stmt (location, condition);
14673 TREE_SIDE_EFFECTS (condition) = 1;
14675 body = build_labeled_block (0, continue_identifier_node);
14676 first = (reversed ? body : condition);
14677 second = (reversed ? condition : body);
14678 return
14679 build (COMPOUND_EXPR, NULL_TREE,
14680 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14683 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14684 their order) on the current loop. Unlink the current loop from the
14685 loop list. */
14687 static tree
14688 finish_loop_body (location, condition, body, reversed)
14689 int location;
14690 tree condition, body;
14691 int reversed;
14693 tree to_return = ctxp->current_loop;
14694 tree loop_body = LOOP_EXPR_BODY (to_return);
14695 if (condition)
14697 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14698 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14699 The real EXIT_EXPR is one operand further. */
14700 EXPR_WFL_LINECOL (cnode) = location;
14701 /* This one is for accurate error reports */
14702 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14703 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14705 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14706 POP_LOOP ();
14707 return to_return;
14710 /* Tailored version of finish_loop_body for FOR loops, when FOR
14711 loops feature the condition part */
14713 static tree
14714 finish_for_loop (location, condition, update, body)
14715 int location;
14716 tree condition, update, body;
14718 /* Put the condition and the loop body in place */
14719 tree loop = finish_loop_body (location, condition, body, 0);
14720 /* LOOP is the current loop which has been now popped of the loop
14721 stack. Install the update block */
14722 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14723 return loop;
14726 /* Try to find the loop a block might be related to. This comprises
14727 the case where the LOOP_EXPR is found as the second operand of a
14728 COMPOUND_EXPR, because the loop happens to have an initialization
14729 part, then expressed as the first operand of the COMPOUND_EXPR. If
14730 the search finds something, 1 is returned. Otherwise, 0 is
14731 returned. The search is assumed to start from a
14732 LABELED_BLOCK_EXPR's block. */
14734 static tree
14735 search_loop (statement)
14736 tree statement;
14738 if (TREE_CODE (statement) == LOOP_EXPR)
14739 return statement;
14741 if (TREE_CODE (statement) == BLOCK)
14742 statement = BLOCK_SUBBLOCKS (statement);
14743 else
14744 return NULL_TREE;
14746 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14747 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14748 statement = TREE_OPERAND (statement, 1);
14750 return (TREE_CODE (statement) == LOOP_EXPR
14751 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14754 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14755 returned otherwise. */
14757 static int
14758 labeled_block_contains_loop_p (block, loop)
14759 tree block, loop;
14761 if (!block)
14762 return 0;
14764 if (LABELED_BLOCK_BODY (block) == loop)
14765 return 1;
14767 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14768 return 1;
14770 return 0;
14773 /* If the loop isn't surrounded by a labeled statement, create one and
14774 insert LOOP as its body. */
14776 static tree
14777 patch_loop_statement (loop)
14778 tree loop;
14780 tree loop_label;
14782 TREE_TYPE (loop) = void_type_node;
14783 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14784 return loop;
14786 loop_label = build_labeled_block (0, NULL_TREE);
14787 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14788 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14789 LABELED_BLOCK_BODY (loop_label) = loop;
14790 PUSH_LABELED_BLOCK (loop_label);
14791 return loop_label;
14794 /* 14.13, 14.14: break and continue Statements */
14796 /* Build a break or a continue statement. a null NAME indicates an
14797 unlabeled break/continue statement. */
14799 static tree
14800 build_bc_statement (location, is_break, name)
14801 int location, is_break;
14802 tree name;
14804 tree break_continue, label_block_expr = NULL_TREE;
14806 if (name)
14808 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14809 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14810 /* Null means that we don't have a target for this named
14811 break/continue. In this case, we make the target to be the
14812 label name, so that the error can be reported accuratly in
14813 patch_bc_statement. */
14814 label_block_expr = EXPR_WFL_NODE (name);
14816 /* Unlabeled break/continue will be handled during the
14817 break/continue patch operation */
14818 break_continue
14819 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14821 IS_BREAK_STMT_P (break_continue) = is_break;
14822 TREE_SIDE_EFFECTS (break_continue) = 1;
14823 EXPR_WFL_LINECOL (break_continue) = location;
14824 break_continue = build_debugable_stmt (location, break_continue);
14825 return break_continue;
14828 /* Verification of a break/continue statement. */
14830 static tree
14831 patch_bc_statement (node)
14832 tree node;
14834 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14835 tree labeled_block = ctxp->current_labeled_block;
14836 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14838 /* Having an identifier here means that the target is unknown. */
14839 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14841 parse_error_context (wfl_operator, "No label definition found for `%s'",
14842 IDENTIFIER_POINTER (bc_label));
14843 return error_mark_node;
14845 if (! IS_BREAK_STMT_P (node))
14847 /* It's a continue statement. */
14848 for (;; labeled_block = TREE_CHAIN (labeled_block))
14850 if (labeled_block == NULL_TREE)
14852 if (bc_label == NULL_TREE)
14853 parse_error_context (wfl_operator,
14854 "`continue' must be in loop");
14855 else
14856 parse_error_context
14857 (wfl_operator, "continue label `%s' does not name a loop",
14858 IDENTIFIER_POINTER (bc_label));
14859 return error_mark_node;
14861 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14862 == continue_identifier_node)
14863 && (bc_label == NULL_TREE
14864 || TREE_CHAIN (labeled_block) == bc_label))
14866 bc_label = labeled_block;
14867 break;
14871 else if (!bc_label)
14873 for (;; labeled_block = TREE_CHAIN (labeled_block))
14875 if (labeled_block == NULL_TREE)
14877 parse_error_context (wfl_operator,
14878 "`break' must be in loop or switch");
14879 return error_mark_node;
14881 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14882 if (TREE_CODE (target_stmt) == SWITCH_EXPR
14883 || search_loop (target_stmt))
14885 bc_label = labeled_block;
14886 break;
14891 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14892 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14894 /* Our break/continue don't return values. */
14895 TREE_TYPE (node) = void_type_node;
14896 /* Encapsulate the break within a compound statement so that it's
14897 expanded all the times by expand_expr (and not clobbered
14898 sometimes, like after a if statement) */
14899 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14900 TREE_SIDE_EFFECTS (node) = 1;
14901 return node;
14904 /* Process the exit expression belonging to a loop. Its type must be
14905 boolean. */
14907 static tree
14908 patch_exit_expr (node)
14909 tree node;
14911 tree expression = TREE_OPERAND (node, 0);
14912 TREE_TYPE (node) = error_mark_node;
14913 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14915 /* The type of expression must be boolean */
14916 if (TREE_TYPE (expression) != boolean_type_node)
14918 parse_error_context
14919 (wfl_operator,
14920 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
14921 lang_printable_name (TREE_TYPE (expression), 0));
14922 return error_mark_node;
14924 /* Now we know things are allright, invert the condition, fold and
14925 return */
14926 TREE_OPERAND (node, 0) =
14927 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
14929 if (! integer_zerop (TREE_OPERAND (node, 0))
14930 && ctxp->current_loop != NULL_TREE
14931 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14932 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14933 if (! integer_onep (TREE_OPERAND (node, 0)))
14934 CAN_COMPLETE_NORMALLY (node) = 1;
14937 TREE_TYPE (node) = void_type_node;
14938 return node;
14941 /* 14.9 Switch statement */
14943 static tree
14944 patch_switch_statement (node)
14945 tree node;
14947 tree se = TREE_OPERAND (node, 0), se_type;
14949 /* Complete the switch expression */
14950 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14951 se_type = TREE_TYPE (se);
14952 /* The type of the switch expression must be char, byte, short or
14953 int */
14954 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
14956 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14957 parse_error_context (wfl_operator,
14958 "Incompatible type for `switch'. Can't convert `%s' to `int'",
14959 lang_printable_name (se_type, 0));
14960 /* This is what java_complete_tree will check */
14961 TREE_OPERAND (node, 0) = error_mark_node;
14962 return error_mark_node;
14965 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
14967 /* Ready to return */
14968 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
14970 TREE_TYPE (node) = error_mark_node;
14971 return error_mark_node;
14973 TREE_TYPE (node) = void_type_node;
14974 TREE_SIDE_EFFECTS (node) = 1;
14975 CAN_COMPLETE_NORMALLY (node)
14976 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14977 || ! SWITCH_HAS_DEFAULT (node);
14978 return node;
14981 /* 14.18 The try/catch statements */
14983 static tree
14984 build_try_statement (location, try_block, catches)
14985 int location;
14986 tree try_block, catches;
14988 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
14989 EXPR_WFL_LINECOL (node) = location;
14990 return node;
14993 static tree
14994 build_try_finally_statement (location, try_block, finally)
14995 int location;
14996 tree try_block, finally;
14998 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14999 EXPR_WFL_LINECOL (node) = location;
15000 return node;
15003 static tree
15004 patch_try_statement (node)
15005 tree node;
15007 int error_found = 0;
15008 tree try = TREE_OPERAND (node, 0);
15009 /* Exception handlers are considered in left to right order */
15010 tree catch = nreverse (TREE_OPERAND (node, 1));
15011 tree current, caught_type_list = NULL_TREE;
15013 /* Check catch clauses, if any. Every time we find an error, we try
15014 to process the next catch clause. We process the catch clause before
15015 the try block so that when processing the try block we can check thrown
15016 exceptions againts the caught type list. */
15017 for (current = catch; current; current = TREE_CHAIN (current))
15019 tree carg_decl, carg_type;
15020 tree sub_current, catch_block, catch_clause;
15021 int unreachable;
15023 /* At this point, the structure of the catch clause is
15024 CATCH_EXPR (catch node)
15025 BLOCK (with the decl of the parameter)
15026 COMPOUND_EXPR
15027 MODIFY_EXPR (assignment of the catch parameter)
15028 BLOCK (catch clause block)
15030 catch_clause = TREE_OPERAND (current, 0);
15031 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15032 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15034 /* Catch clauses can't have more than one parameter declared,
15035 but it's already enforced by the grammar. Make sure that the
15036 only parameter of the clause statement in of class Throwable
15037 or a subclass of Throwable, but that was done earlier. The
15038 catch clause parameter type has also been resolved. */
15040 /* Just make sure that the catch clause parameter type inherits
15041 from java.lang.Throwable */
15042 if (!inherits_from_p (carg_type, throwable_type_node))
15044 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15045 parse_error_context (wfl_operator,
15046 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15047 lang_printable_name (carg_type, 0));
15048 error_found = 1;
15049 continue;
15052 /* Partial check for unreachable catch statement: The catch
15053 clause is reachable iff is no earlier catch block A in
15054 the try statement such that the type of the catch
15055 clause's parameter is the same as or a subclass of the
15056 type of A's parameter */
15057 unreachable = 0;
15058 for (sub_current = catch;
15059 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15061 tree sub_catch_clause, decl;
15062 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15063 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15065 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15067 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15068 parse_error_context
15069 (wfl_operator,
15070 "`catch' not reached because of the catch clause at line %d",
15071 EXPR_WFL_LINENO (sub_current));
15072 unreachable = error_found = 1;
15073 break;
15076 /* Complete the catch clause block */
15077 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15078 if (catch_block == error_mark_node)
15080 error_found = 1;
15081 continue;
15083 if (CAN_COMPLETE_NORMALLY (catch_block))
15084 CAN_COMPLETE_NORMALLY (node) = 1;
15085 TREE_OPERAND (current, 0) = catch_block;
15087 if (unreachable)
15088 continue;
15090 /* Things to do here: the exception must be thrown */
15092 /* Link this type to the caught type list */
15093 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15096 PUSH_EXCEPTIONS (caught_type_list);
15097 if ((try = java_complete_tree (try)) == error_mark_node)
15098 error_found = 1;
15099 if (CAN_COMPLETE_NORMALLY (try))
15100 CAN_COMPLETE_NORMALLY (node) = 1;
15101 POP_EXCEPTIONS ();
15103 /* Verification ends here */
15104 if (error_found)
15105 return error_mark_node;
15107 TREE_OPERAND (node, 0) = try;
15108 TREE_OPERAND (node, 1) = catch;
15109 TREE_TYPE (node) = void_type_node;
15110 return node;
15113 /* 14.17 The synchronized Statement */
15115 static tree
15116 patch_synchronized_statement (node, wfl_op1)
15117 tree node, wfl_op1;
15119 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15120 tree block = TREE_OPERAND (node, 1);
15122 tree tmp, enter, exit, expr_decl, assignment;
15124 if (expr == error_mark_node)
15126 block = java_complete_tree (block);
15127 return expr;
15130 /* We might be trying to synchronize on a STRING_CST */
15131 if ((tmp = patch_string (expr)))
15132 expr = tmp;
15134 /* The TYPE of expr must be a reference type */
15135 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15137 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15138 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15139 lang_printable_name (TREE_TYPE (expr), 0));
15140 return error_mark_node;
15143 if (flag_emit_xref)
15145 TREE_OPERAND (node, 0) = expr;
15146 TREE_OPERAND (node, 1) = java_complete_tree (block);
15147 CAN_COMPLETE_NORMALLY (node) = 1;
15148 return node;
15151 /* Generate a try-finally for the synchronized statement, except
15152 that the handler that catches all throw exception calls
15153 _Jv_MonitorExit and then rethrow the exception.
15154 The synchronized statement is then implemented as:
15155 TRY
15157 _Jv_MonitorEnter (expression)
15158 synchronized_block
15159 _Jv_MonitorExit (expression)
15161 CATCH_ALL
15163 e = _Jv_exception_info ();
15164 _Jv_MonitorExit (expression)
15165 Throw (e);
15166 } */
15168 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15169 BUILD_MONITOR_ENTER (enter, expr_decl);
15170 BUILD_MONITOR_EXIT (exit, expr_decl);
15171 CAN_COMPLETE_NORMALLY (enter) = 1;
15172 CAN_COMPLETE_NORMALLY (exit) = 1;
15173 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15174 TREE_SIDE_EFFECTS (assignment) = 1;
15175 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15176 build (COMPOUND_EXPR, NULL_TREE,
15177 build (WITH_CLEANUP_EXPR, NULL_TREE,
15178 build (COMPOUND_EXPR, NULL_TREE,
15179 assignment, enter),
15180 NULL_TREE, exit),
15181 block));
15182 node = build_expr_block (node, expr_decl);
15184 return java_complete_tree (node);
15187 /* 14.16 The throw Statement */
15189 static tree
15190 patch_throw_statement (node, wfl_op1)
15191 tree node, wfl_op1;
15193 tree expr = TREE_OPERAND (node, 0);
15194 tree type = TREE_TYPE (expr);
15195 int unchecked_ok = 0, tryblock_throws_ok = 0;
15197 /* Thrown expression must be assignable to java.lang.Throwable */
15198 if (!try_reference_assignconv (throwable_type_node, expr))
15200 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15201 parse_error_context (wfl_operator,
15202 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15203 lang_printable_name (type, 0));
15204 /* If the thrown expression was a reference, we further the
15205 compile-time check. */
15206 if (!JREFERENCE_TYPE_P (type))
15207 return error_mark_node;
15210 /* At least one of the following must be true */
15212 /* The type of the throw expression is a not checked exception,
15213 i.e. is a unchecked expression. */
15214 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15216 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15217 /* An instance can't throw a checked excetion unless that exception
15218 is explicitely declared in the `throws' clause of each
15219 constructor. This doesn't apply to anonymous classes, since they
15220 don't have declared constructors. */
15221 if (!unchecked_ok
15222 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15224 tree current;
15225 for (current = TYPE_METHODS (current_class); current;
15226 current = TREE_CHAIN (current))
15227 if (DECL_CONSTRUCTOR_P (current)
15228 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15230 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)",
15231 lang_printable_name (TREE_TYPE (expr), 0));
15232 return error_mark_node;
15236 /* Throw is contained in a try statement and at least one catch
15237 clause can receive the thrown expression or the current method is
15238 declared to throw such an exception. Or, the throw statement is
15239 contained in a method or constructor declaration and the type of
15240 the Expression is assignable to at least one type listed in the
15241 throws clause the declaration. */
15242 if (!unchecked_ok)
15243 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15244 if (!(unchecked_ok || tryblock_throws_ok))
15246 /* If there is a surrounding try block that has no matching
15247 clatch clause, report it first. A surrounding try block exits
15248 only if there is something after the list of checked
15249 exception thrown by the current function (if any). */
15250 if (IN_TRY_BLOCK_P ())
15251 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15252 lang_printable_name (type, 0));
15253 /* If we have no surrounding try statement and the method doesn't have
15254 any throws, report it now. FIXME */
15256 /* We report that the exception can't be throw from a try block
15257 in all circumstances but when the `throw' is inside a static
15258 block. */
15259 else if (!EXCEPTIONS_P (currently_caught_type_list)
15260 && !tryblock_throws_ok)
15262 if (DECL_CLINIT_P (current_function_decl))
15263 parse_error_context (wfl_operator,
15264 "Checked exception `%s' can't be thrown in initializer",
15265 lang_printable_name (type, 0));
15266 else
15267 parse_error_context (wfl_operator,
15268 "Checked exception `%s' isn't thrown from a `try' block",
15269 lang_printable_name (type, 0));
15271 /* Otherwise, the current method doesn't have the appropriate
15272 throws declaration */
15273 else
15274 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15275 lang_printable_name (type, 0));
15276 return error_mark_node;
15279 if (! flag_emit_class_files && ! flag_emit_xref)
15280 BUILD_THROW (node, expr);
15282 /* If doing xrefs, keep the location where the `throw' was seen. */
15283 if (flag_emit_xref)
15284 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15285 return node;
15288 /* Check that exception said to be thrown by method DECL can be
15289 effectively caught from where DECL is invoked. */
15291 static void
15292 check_thrown_exceptions (location, decl)
15293 int location;
15294 tree decl;
15296 tree throws;
15297 /* For all the unchecked exceptions thrown by DECL */
15298 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15299 throws = TREE_CHAIN (throws))
15300 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15302 #if 1
15303 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15304 if (DECL_NAME (decl) == get_identifier ("clone"))
15305 continue;
15306 #endif
15307 EXPR_WFL_LINECOL (wfl_operator) = location;
15308 if (DECL_FINIT_P (current_function_decl))
15309 parse_error_context
15310 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15311 lang_printable_name (TREE_VALUE (throws), 0));
15312 else
15314 parse_error_context
15315 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15316 lang_printable_name (TREE_VALUE (throws), 0),
15317 (DECL_INIT_P (current_function_decl) ?
15318 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15319 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15324 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15325 try-catch blocks, OR is listed in the `throws' clause of the
15326 current method. */
15328 static int
15329 check_thrown_exceptions_do (exception)
15330 tree exception;
15332 tree list = currently_caught_type_list;
15333 resolve_and_layout (exception, NULL_TREE);
15334 /* First, all the nested try-catch-finally at that stage. The
15335 last element contains `throws' clause exceptions, if any. */
15336 if (IS_UNCHECKED_EXCEPTION_P (exception))
15337 return 1;
15338 while (list)
15340 tree caught;
15341 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15342 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15343 return 1;
15344 list = TREE_CHAIN (list);
15346 return 0;
15349 static void
15350 purge_unchecked_exceptions (mdecl)
15351 tree mdecl;
15353 tree throws = DECL_FUNCTION_THROWS (mdecl);
15354 tree new = NULL_TREE;
15356 while (throws)
15358 tree next = TREE_CHAIN (throws);
15359 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15361 TREE_CHAIN (throws) = new;
15362 new = throws;
15364 throws = next;
15366 /* List is inverted here, but it doesn't matter */
15367 DECL_FUNCTION_THROWS (mdecl) = new;
15370 /* 15.24 Conditional Operator ?: */
15372 static tree
15373 patch_conditional_expr (node, wfl_cond, wfl_op1)
15374 tree node, wfl_cond, wfl_op1;
15376 tree cond = TREE_OPERAND (node, 0);
15377 tree op1 = TREE_OPERAND (node, 1);
15378 tree op2 = TREE_OPERAND (node, 2);
15379 tree resulting_type = NULL_TREE;
15380 tree t1, t2, patched;
15381 int error_found = 0;
15383 /* Operands of ?: might be StringBuffers crafted as a result of a
15384 string concatenation. Obtain a descent operand here. */
15385 if ((patched = patch_string (op1)))
15386 TREE_OPERAND (node, 1) = op1 = patched;
15387 if ((patched = patch_string (op2)))
15388 TREE_OPERAND (node, 2) = op2 = patched;
15390 t1 = TREE_TYPE (op1);
15391 t2 = TREE_TYPE (op2);
15393 /* The first expression must be a boolean */
15394 if (TREE_TYPE (cond) != boolean_type_node)
15396 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15397 parse_error_context (wfl_operator,
15398 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15399 lang_printable_name (TREE_TYPE (cond), 0));
15400 error_found = 1;
15403 /* Second and third can be numeric, boolean (i.e. primitive),
15404 references or null. Anything else results in an error */
15405 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15406 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15407 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15408 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15409 error_found = 1;
15411 /* Determine the type of the conditional expression. Same types are
15412 easy to deal with */
15413 else if (t1 == t2)
15414 resulting_type = t1;
15416 /* There are different rules for numeric types */
15417 else if (JNUMERIC_TYPE_P (t1))
15419 /* if byte/short found, the resulting type is short */
15420 if ((t1 == byte_type_node && t2 == short_type_node)
15421 || (t1 == short_type_node && t2 == byte_type_node))
15422 resulting_type = short_type_node;
15424 /* If t1 is a constant int and t2 is of type byte, short or char
15425 and t1's value fits in t2, then the resulting type is t2 */
15426 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15427 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15428 resulting_type = t2;
15430 /* If t2 is a constant int and t1 is of type byte, short or char
15431 and t2's value fits in t1, then the resulting type is t1 */
15432 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15433 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15434 resulting_type = t1;
15436 /* Otherwise, binary numeric promotion is applied and the
15437 resulting type is the promoted type of operand 1 and 2 */
15438 else
15439 resulting_type = binary_numeric_promotion (t1, t2,
15440 &TREE_OPERAND (node, 1),
15441 &TREE_OPERAND (node, 2));
15444 /* Cases of a reference and a null type */
15445 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15446 resulting_type = t1;
15448 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15449 resulting_type = t2;
15451 /* Last case: different reference types. If a type can be converted
15452 into the other one by assignment conversion, the latter
15453 determines the type of the expression */
15454 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15455 resulting_type = promote_type (t1);
15457 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15458 resulting_type = promote_type (t2);
15460 /* If we don't have any resulting type, we're in trouble */
15461 if (!resulting_type)
15463 char *t = xstrdup (lang_printable_name (t1, 0));
15464 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15465 parse_error_context (wfl_operator,
15466 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15467 t, lang_printable_name (t2, 0));
15468 free (t);
15469 error_found = 1;
15472 if (error_found)
15474 TREE_TYPE (node) = error_mark_node;
15475 return error_mark_node;
15478 TREE_TYPE (node) = resulting_type;
15479 TREE_SET_CODE (node, COND_EXPR);
15480 CAN_COMPLETE_NORMALLY (node) = 1;
15481 return node;
15484 /* Try to constant fold NODE.
15485 If NODE is not a constant expression, return NULL_EXPR.
15486 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15488 static tree
15489 fold_constant_for_init (node, context)
15490 tree node;
15491 tree context;
15493 tree op0, op1, val;
15494 enum tree_code code = TREE_CODE (node);
15496 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
15497 return node;
15499 switch (code)
15501 case PLUS_EXPR:
15502 case MINUS_EXPR:
15503 case MULT_EXPR:
15504 case TRUNC_MOD_EXPR:
15505 case RDIV_EXPR:
15506 case LSHIFT_EXPR:
15507 case RSHIFT_EXPR:
15508 case URSHIFT_EXPR:
15509 case BIT_AND_EXPR:
15510 case BIT_XOR_EXPR:
15511 case BIT_IOR_EXPR:
15512 case TRUTH_ANDIF_EXPR:
15513 case TRUTH_ORIF_EXPR:
15514 case EQ_EXPR:
15515 case NE_EXPR:
15516 case GT_EXPR:
15517 case GE_EXPR:
15518 case LT_EXPR:
15519 case LE_EXPR:
15520 op0 = TREE_OPERAND (node, 0);
15521 op1 = TREE_OPERAND (node, 1);
15522 val = fold_constant_for_init (op0, context);
15523 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15524 return NULL_TREE;
15525 TREE_OPERAND (node, 0) = val;
15526 val = fold_constant_for_init (op1, context);
15527 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15528 return NULL_TREE;
15529 TREE_OPERAND (node, 1) = val;
15530 return patch_binop (node, op0, op1);
15532 case UNARY_PLUS_EXPR:
15533 case NEGATE_EXPR:
15534 case TRUTH_NOT_EXPR:
15535 case BIT_NOT_EXPR:
15536 case CONVERT_EXPR:
15537 op0 = TREE_OPERAND (node, 0);
15538 val = fold_constant_for_init (op0, context);
15539 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15540 return NULL_TREE;
15541 TREE_OPERAND (node, 0) = val;
15542 return patch_unaryop (node, op0);
15543 break;
15545 case COND_EXPR:
15546 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15547 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15548 return NULL_TREE;
15549 TREE_OPERAND (node, 0) = val;
15550 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15551 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15552 return NULL_TREE;
15553 TREE_OPERAND (node, 1) = val;
15554 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15555 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15556 return NULL_TREE;
15557 TREE_OPERAND (node, 2) = val;
15558 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15559 : TREE_OPERAND (node, 2);
15561 case VAR_DECL:
15562 case FIELD_DECL:
15563 if (! FIELD_FINAL (node)
15564 || DECL_INITIAL (node) == NULL_TREE)
15565 return NULL_TREE;
15566 val = DECL_INITIAL (node);
15567 /* Guard against infinite recursion. */
15568 DECL_INITIAL (node) = NULL_TREE;
15569 val = fold_constant_for_init (val, node);
15570 DECL_INITIAL (node) = val;
15571 if (!val && CLASS_FINAL_VARIABLE_P (node))
15572 DECL_FIELD_FINAL_IUD (node) = 0;
15573 return val;
15575 case EXPR_WITH_FILE_LOCATION:
15576 /* Compare java_complete_tree and resolve_expression_name. */
15577 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15578 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15580 tree name = EXPR_WFL_NODE (node);
15581 tree decl;
15582 if (PRIMARY_P (node))
15583 return NULL_TREE;
15584 else if (! QUALIFIED_P (name))
15586 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15587 if (decl == NULL_TREE
15588 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15589 return NULL_TREE;
15590 return fold_constant_for_init (decl, decl);
15592 else
15594 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15595 qualify_ambiguous_name (node);
15596 if (resolve_field_access (node, &decl, NULL)
15597 && decl != NULL_TREE)
15598 return fold_constant_for_init (decl, decl);
15599 return NULL_TREE;
15602 else
15604 op0 = TREE_OPERAND (node, 0);
15605 val = fold_constant_for_init (op0, context);
15606 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15607 return NULL_TREE;
15608 TREE_OPERAND (node, 0) = val;
15609 return val;
15612 #ifdef USE_COMPONENT_REF
15613 case IDENTIFIER:
15614 case COMPONENT_REF:
15616 #endif
15618 default:
15619 return NULL_TREE;
15623 #ifdef USE_COMPONENT_REF
15624 /* Context is 'T' for TypeName, 'P' for PackageName,
15625 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15627 tree
15628 resolve_simple_name (name, context)
15629 tree name;
15630 int context;
15634 tree
15635 resolve_qualified_name (name, context)
15636 tree name;
15637 int context;
15640 #endif
15642 /* Mark P, which is really a `struct parser_ctxt **' for GC. */
15644 static void
15645 mark_parser_ctxt (p)
15646 void *p;
15648 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15649 int i;
15651 if (!pc)
15652 return;
15654 #ifndef JC1_LITE
15655 for (i = 0; i < 11; ++i)
15656 ggc_mark_tree (pc->modifier_ctx[i]);
15657 ggc_mark_tree (pc->class_type);
15658 ggc_mark_tree (pc->function_decl);
15659 ggc_mark_tree (pc->package);
15660 ggc_mark_tree (pc->incomplete_class);
15661 ggc_mark_tree (pc->gclass_list);
15662 ggc_mark_tree (pc->class_list);
15663 ggc_mark_tree (pc->current_parsed_class);
15664 ggc_mark_tree (pc->current_parsed_class_un);
15665 ggc_mark_tree (pc->non_static_initialized);
15666 ggc_mark_tree (pc->static_initialized);
15667 ggc_mark_tree (pc->instance_initializers);
15668 ggc_mark_tree (pc->import_list);
15669 ggc_mark_tree (pc->import_demand_list);
15670 ggc_mark_tree (pc->current_loop);
15671 ggc_mark_tree (pc->current_labeled_block);
15672 #endif /* JC1_LITE */
15674 if (pc->next)
15675 mark_parser_ctxt (&pc->next);