2000-07-13 Alexandre Petit-Bianco <apbianco@cygnus.com>
[official-gcc.git] / gcc / java / parse.y
blobaf7b19b11bd4cf60da0ba73d042b9da8f9e31e04
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 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"
69 #ifndef DIR_SEPARATOR
70 #define DIR_SEPARATOR '/'
71 #endif
73 /* Local function prototypes */
74 static char *java_accstring_lookup PARAMS ((int));
75 static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
76 static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
77 static tree create_class PARAMS ((int, tree, tree, tree));
78 static tree create_interface PARAMS ((int, tree, tree));
79 static void end_class_declaration PARAMS ((int));
80 static tree find_field PARAMS ((tree, tree));
81 static tree lookup_field_wrapper PARAMS ((tree, tree));
82 static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
83 static void register_fields PARAMS ((int, tree, tree));
84 static tree parser_qualified_classname PARAMS ((tree));
85 static int parser_check_super PARAMS ((tree, tree, tree));
86 static int parser_check_super_interface PARAMS ((tree, tree, tree));
87 static void check_modifiers_consistency PARAMS ((int));
88 static tree lookup_cl PARAMS ((tree));
89 static tree lookup_java_method2 PARAMS ((tree, tree, int));
90 static tree method_header PARAMS ((int, tree, tree, tree));
91 static void fix_method_argument_names PARAMS ((tree ,tree));
92 static tree method_declarator PARAMS ((tree, tree));
93 static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
94 ATTRIBUTE_PRINTF_2;
95 static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list));
96 static void parse_ctor_invocation_error PARAMS ((void));
97 static tree parse_jdk1_1_error PARAMS ((const char *));
98 static void complete_class_report_errors PARAMS ((jdep *));
99 static int process_imports PARAMS ((void));
100 static void read_import_dir PARAMS ((tree));
101 static int find_in_imports_on_demand PARAMS ((tree));
102 static void find_in_imports PARAMS ((tree));
103 static void check_inner_class_access PARAMS ((tree, tree, tree));
104 static int check_pkg_class_access PARAMS ((tree, tree));
105 static void register_package PARAMS ((tree));
106 static tree resolve_package PARAMS ((tree, tree *));
107 static tree lookup_package_type PARAMS ((const char *, int));
108 static tree lookup_package_type_and_set_next PARAMS ((const char *, int, tree *));
109 static tree resolve_class PARAMS ((tree, tree, tree, tree));
110 static void declare_local_variables PARAMS ((int, tree, tree));
111 static void source_start_java_method PARAMS ((tree));
112 static void source_end_java_method PARAMS ((void));
113 static void expand_start_java_method PARAMS ((tree));
114 static tree find_name_in_single_imports PARAMS ((tree));
115 static void check_abstract_method_header PARAMS ((tree));
116 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
117 static tree resolve_expression_name PARAMS ((tree, tree *));
118 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
119 static int check_class_interface_creation PARAMS ((int, int, tree,
120 tree, tree, tree));
121 static tree patch_method_invocation PARAMS ((tree, tree, tree,
122 int *, tree *));
123 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
124 static tree resolve_and_layout PARAMS ((tree, tree));
125 static tree qualify_and_find PARAMS ((tree, tree, tree));
126 static tree resolve_no_layout PARAMS ((tree, tree));
127 static int invocation_mode PARAMS ((tree, int));
128 static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
129 tree, tree));
130 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
131 tree *, tree *));
132 static tree find_most_specific_methods_list PARAMS ((tree));
133 static int argument_types_convertible PARAMS ((tree, tree));
134 static tree patch_invoke PARAMS ((tree, tree, tree));
135 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
136 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
137 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
138 static tree obtain_incomplete_type PARAMS ((tree));
139 static tree java_complete_lhs PARAMS ((tree));
140 static tree java_complete_tree PARAMS ((tree));
141 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
142 static int maybe_yank_clinit PARAMS ((tree));
143 static void java_complete_expand_method PARAMS ((tree));
144 static int unresolved_type_p PARAMS ((tree, tree *));
145 static void create_jdep_list PARAMS ((struct parser_ctxt *));
146 static tree build_expr_block PARAMS ((tree, tree));
147 static tree enter_block PARAMS ((void));
148 static tree enter_a_block PARAMS ((tree));
149 static tree exit_block PARAMS ((void));
150 static tree lookup_name_in_blocks PARAMS ((tree));
151 static void maybe_absorb_scoping_blocks PARAMS ((void));
152 static tree build_method_invocation PARAMS ((tree, tree));
153 static tree build_new_invocation PARAMS ((tree, tree));
154 static tree build_assignment PARAMS ((int, int, tree, tree));
155 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
156 static int check_final_assignment PARAMS ((tree ,tree));
157 static tree patch_assignment PARAMS ((tree, tree, tree ));
158 static tree patch_binop PARAMS ((tree, tree, tree));
159 static tree build_unaryop PARAMS ((int, int, tree));
160 static tree build_incdec PARAMS ((int, int, tree, int));
161 static tree patch_unaryop PARAMS ((tree, tree));
162 static tree build_cast PARAMS ((int, tree, tree));
163 static tree build_null_of_type PARAMS ((tree));
164 static tree patch_cast PARAMS ((tree, tree));
165 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
166 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
167 static int valid_cast_to_p PARAMS ((tree, tree));
168 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
169 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
170 static tree try_reference_assignconv PARAMS ((tree, tree));
171 static tree build_unresolved_array_type PARAMS ((tree));
172 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
173 static tree build_array_ref PARAMS ((int, tree, tree));
174 static tree patch_array_ref PARAMS ((tree));
175 static tree make_qualified_name PARAMS ((tree, tree, int));
176 static tree merge_qualified_name PARAMS ((tree, tree));
177 static tree make_qualified_primary PARAMS ((tree, tree, int));
178 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
179 tree *, tree *));
180 static void qualify_ambiguous_name PARAMS ((tree));
181 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
182 static tree build_newarray_node PARAMS ((tree, tree, int));
183 static tree patch_newarray PARAMS ((tree));
184 static tree resolve_type_during_patch PARAMS ((tree));
185 static tree build_this PARAMS ((int));
186 static tree build_wfl_wrap PARAMS ((tree, int));
187 static tree build_return PARAMS ((int, tree));
188 static tree patch_return PARAMS ((tree));
189 static tree maybe_access_field PARAMS ((tree, tree, tree));
190 static int complete_function_arguments PARAMS ((tree));
191 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
192 tree, tree));
193 static int not_accessible_p PARAMS ((tree, tree, int));
194 static void check_deprecation PARAMS ((tree, tree));
195 static int class_in_current_package PARAMS ((tree));
196 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
197 static tree patch_if_else_statement PARAMS ((tree));
198 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
199 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
200 static tree patch_exit_expr PARAMS ((tree));
201 static tree build_labeled_block PARAMS ((int, tree));
202 static tree finish_labeled_statement PARAMS ((tree, tree));
203 static tree build_bc_statement PARAMS ((int, int, tree));
204 static tree patch_bc_statement PARAMS ((tree));
205 static tree patch_loop_statement PARAMS ((tree));
206 static tree build_new_loop PARAMS ((tree));
207 static tree build_loop_body PARAMS ((int, tree, int));
208 static tree finish_loop_body PARAMS ((int, tree, tree, int));
209 static tree build_debugable_stmt PARAMS ((int, tree));
210 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
211 static tree patch_switch_statement PARAMS ((tree));
212 static tree string_constant_concatenation PARAMS ((tree, tree));
213 static tree build_string_concatenation PARAMS ((tree, tree));
214 static tree patch_string_cst PARAMS ((tree));
215 static tree patch_string PARAMS ((tree));
216 static tree build_try_statement PARAMS ((int, tree, tree));
217 static tree build_try_finally_statement PARAMS ((int, tree, tree));
218 static tree patch_try_statement PARAMS ((tree));
219 static tree patch_synchronized_statement PARAMS ((tree, tree));
220 static tree patch_throw_statement PARAMS ((tree, tree));
221 static void check_thrown_exceptions PARAMS ((int, tree));
222 static int check_thrown_exceptions_do PARAMS ((tree));
223 static void purge_unchecked_exceptions PARAMS ((tree));
224 static void check_throws_clauses PARAMS ((tree, tree, tree));
225 static void finish_method_declaration PARAMS ((tree));
226 static tree build_super_invocation PARAMS ((tree));
227 static int verify_constructor_circularity PARAMS ((tree, tree));
228 static char *constructor_circularity_msg PARAMS ((tree, tree));
229 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
230 int, int));
231 static const char *get_printable_method_name PARAMS ((tree));
232 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
233 static tree generate_finit PARAMS ((tree));
234 static void add_instance_initializer PARAMS ((tree));
235 static void fix_constructors PARAMS ((tree));
236 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
237 tree, int *));
238 static void craft_constructor PARAMS ((tree, tree));
239 static int verify_constructor_super PARAMS ((tree));
240 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
241 static void start_artificial_method_body PARAMS ((tree));
242 static void end_artificial_method_body PARAMS ((tree));
243 static int check_method_redefinition PARAMS ((tree, tree));
244 static int reset_method_name PARAMS ((tree));
245 static int check_method_types_complete PARAMS ((tree));
246 static void java_check_regular_methods PARAMS ((tree));
247 static void java_check_abstract_methods PARAMS ((tree));
248 static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
249 static void unreachable_stmt_error PARAMS ((tree));
250 static tree find_expr_with_wfl PARAMS ((tree));
251 static void missing_return_error PARAMS ((tree));
252 static tree build_new_array_init PARAMS ((int, tree));
253 static tree patch_new_array_init PARAMS ((tree, tree));
254 static tree maybe_build_array_element_wfl PARAMS ((tree));
255 static int array_constructor_check_entry PARAMS ((tree, tree));
256 static const char *purify_type_name PARAMS ((const char *));
257 static tree fold_constant_for_init PARAMS ((tree, tree));
258 static tree strip_out_static_field_access_decl PARAMS ((tree));
259 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
260 static void static_ref_err PARAMS ((tree, tree, tree));
261 static void parser_add_interface PARAMS ((tree, tree, tree));
262 static void add_superinterfaces PARAMS ((tree, tree));
263 static tree jdep_resolve_class PARAMS ((jdep *));
264 static int note_possible_classname PARAMS ((const char *, int));
265 static void java_complete_expand_classes PARAMS ((void));
266 static void java_complete_expand_class PARAMS ((tree));
267 static void java_complete_expand_methods PARAMS ((tree));
268 static tree cut_identifier_in_qualified PARAMS ((tree));
269 static tree java_stabilize_reference PARAMS ((tree));
270 static tree do_unary_numeric_promotion PARAMS ((tree));
271 static char * operator_string PARAMS ((tree));
272 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
273 static tree merge_string_cste PARAMS ((tree, tree, int));
274 static tree java_refold PARAMS ((tree));
275 static int java_decl_equiv PARAMS ((tree, tree));
276 static int binop_compound_p PARAMS ((enum tree_code));
277 static tree search_loop PARAMS ((tree));
278 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
279 static void check_abstract_method_definitions PARAMS ((int, tree, tree));
280 static void java_check_abstract_method_definitions PARAMS ((tree));
281 static void java_debug_context_do PARAMS ((int));
282 static void java_parser_context_push_initialized_field PARAMS ((void));
283 static void java_parser_context_pop_initialized_field PARAMS ((void));
284 static tree reorder_static_initialized PARAMS ((tree));
285 static void java_parser_context_suspend PARAMS ((void));
286 static void java_parser_context_resume PARAMS ((void));
288 /* JDK 1.1 work. FIXME */
290 static tree maybe_make_nested_class_name PARAMS ((tree));
291 static void make_nested_class_name PARAMS ((tree));
292 static void set_nested_class_simple_name_value PARAMS ((tree, int));
293 static void link_nested_class_to_enclosing PARAMS ((void));
294 static tree find_as_inner_class PARAMS ((tree, tree, tree));
295 static tree find_as_inner_class_do PARAMS ((tree, tree));
296 static int check_inner_class_redefinition PARAMS ((tree, tree));
298 static tree build_thisn_assign PARAMS ((void));
299 static tree build_current_thisn PARAMS ((tree));
300 static tree build_access_to_thisn PARAMS ((tree, tree, int));
301 static tree maybe_build_thisn_access_method PARAMS ((tree));
303 static tree build_outer_field_access PARAMS ((tree, tree));
304 static tree build_outer_field_access_methods PARAMS ((tree));
305 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
306 tree, tree));
307 static tree build_outer_method_access_method PARAMS ((tree));
308 static tree build_new_access_id PARAMS ((void));
309 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
310 tree, tree));
312 static int outer_field_access_p PARAMS ((tree, tree));
313 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
314 tree *, tree *));
315 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
316 static tree build_incomplete_class_ref PARAMS ((int, tree));
317 static tree patch_incomplete_class_ref PARAMS ((tree));
318 static tree create_anonymous_class PARAMS ((int, tree));
319 static void patch_anonymous_class PARAMS ((tree, tree, tree));
320 static void add_inner_class_fields PARAMS ((tree, tree));
322 static tree build_dot_class_method PARAMS ((tree));
323 static tree build_dot_class_method_invocation PARAMS ((tree));
324 static void create_new_parser_context PARAMS ((int));
326 /* Number of error found so far. */
327 int java_error_count;
328 /* Number of warning found so far. */
329 int java_warning_count;
330 /* Tell when not to fold, when doing xrefs */
331 int do_not_fold;
332 /* Cyclic inheritance report, as it can be set by layout_class */
333 char *cyclic_inheritance_report;
335 /* Tell when we're within an instance initializer */
336 static int in_instance_initializer;
338 /* The current parser context */
339 struct parser_ctxt *ctxp;
341 /* List of things that were analyzed for which code will be generated */
342 static struct parser_ctxt *ctxp_for_generation = NULL;
344 /* binop_lookup maps token to tree_code. It is used where binary
345 operations are involved and required by the parser. RDIV_EXPR
346 covers both integral/floating point division. The code is changed
347 once the type of both operator is worked out. */
349 static enum tree_code binop_lookup[19] =
351 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
352 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
353 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
354 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
355 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
357 #define BINOP_LOOKUP(VALUE) \
358 binop_lookup [((VALUE) - PLUS_TK)% \
359 (sizeof (binop_lookup) / sizeof (binop_lookup[0]))]
361 /* This is the end index for binary operators that can also be used
362 in compound assignements. */
363 #define BINOP_COMPOUND_CANDIDATES 11
365 /* Fake WFL used to report error message. It is initialized once if
366 needed and reused with it's location information is overriden. */
367 tree wfl_operator = NULL_TREE;
369 /* The "$L" identifier we use to create labels. */
370 static tree label_id = NULL_TREE;
372 /* The "StringBuffer" identifier used for the String `+' operator. */
373 static tree wfl_string_buffer = NULL_TREE;
375 /* The "append" identifier used for String `+' operator. */
376 static tree wfl_append = NULL_TREE;
378 /* The "toString" identifier used for String `+' operator. */
379 static tree wfl_to_string = NULL_TREE;
381 /* The "java.lang" import qualified name. */
382 static tree java_lang_id = NULL_TREE;
384 /* The generated `inst$' identifier used for generated enclosing
385 instance/field access functions. */
386 static tree inst_id = NULL_TREE;
388 /* The "java.lang.Cloneable" qualified name. */
389 static tree java_lang_cloneable = NULL_TREE;
391 /* Context and flag for static blocks */
392 static tree current_static_block = NULL_TREE;
394 /* The generated `write_parm_value$' identifier. */
395 static tree wpv_id;
397 /* The list of all packages we've seen so far */
398 static tree package_list = NULL_TREE;
400 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
401 line and point it out. */
402 /* Should point out the one that don't fit. ASCII/unicode, going
403 backward. FIXME */
405 #define check_modifiers(__message, __value, __mask) do { \
406 if ((__value) & ~(__mask)) \
408 int i, remainder = (__value) & ~(__mask); \
409 for (i = 0; i <= 10; i++) \
410 if ((1 << i) & remainder) \
411 parse_error_context (ctxp->modifier_ctx [i], (__message), \
412 java_accstring_lookup (1 << i)); \
414 } while (0)
418 %union {
419 tree node;
420 int sub_token;
421 struct {
422 int token;
423 int location;
424 } operator;
425 int value;
429 #include "lex.c"
432 %pure_parser
434 /* Things defined here have to match the order of what's in the
435 binop_lookup table. */
437 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
438 %token LS_TK SRS_TK ZRS_TK
439 %token AND_TK XOR_TK OR_TK
440 %token BOOL_AND_TK BOOL_OR_TK
441 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
443 /* This maps to the same binop_lookup entry than the token above */
445 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
446 %token REM_ASSIGN_TK
447 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
448 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
451 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
453 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
454 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
455 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
456 %token PAD_TK ABSTRACT_TK MODIFIER_TK
458 /* Keep those two in order, too */
459 %token DECR_TK INCR_TK
461 /* From now one, things can be in any order */
463 %token DEFAULT_TK IF_TK THROW_TK
464 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
465 %token THROWS_TK BREAK_TK IMPORT_TK
466 %token ELSE_TK INSTANCEOF_TK RETURN_TK
467 %token VOID_TK CATCH_TK INTERFACE_TK
468 %token CASE_TK EXTENDS_TK FINALLY_TK
469 %token SUPER_TK WHILE_TK CLASS_TK
470 %token SWITCH_TK CONST_TK TRY_TK
471 %token FOR_TK NEW_TK CONTINUE_TK
472 %token GOTO_TK PACKAGE_TK THIS_TK
474 %token BYTE_TK SHORT_TK INT_TK LONG_TK
475 %token CHAR_TK INTEGRAL_TK
477 %token FLOAT_TK DOUBLE_TK FP_TK
479 %token ID_TK
481 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
483 %token ASSIGN_ANY_TK ASSIGN_TK
484 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
486 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
487 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
489 %type <value> modifiers MODIFIER_TK final synchronized
491 %type <node> super ID_TK identifier
492 %type <node> name simple_name qualified_name
493 %type <node> type_declaration compilation_unit
494 field_declaration method_declaration extends_interfaces
495 interfaces interface_type_list
496 class_member_declaration
497 import_declarations package_declaration
498 type_declarations interface_body
499 interface_member_declaration constant_declaration
500 interface_member_declarations interface_type
501 abstract_method_declaration interface_type_list
502 %type <node> class_body_declaration class_member_declaration
503 static_initializer constructor_declaration block
504 %type <node> class_body_declarations constructor_header
505 %type <node> class_or_interface_type class_type class_type_list
506 constructor_declarator explicit_constructor_invocation
507 %type <node> dim_expr dim_exprs this_or_super throws
509 %type <node> variable_declarator_id variable_declarator
510 variable_declarators variable_initializer
511 variable_initializers constructor_body
512 array_initializer
514 %type <node> class_body block_end constructor_block_end
515 %type <node> statement statement_without_trailing_substatement
516 labeled_statement if_then_statement label_decl
517 if_then_else_statement while_statement for_statement
518 statement_nsi labeled_statement_nsi do_statement
519 if_then_else_statement_nsi while_statement_nsi
520 for_statement_nsi statement_expression_list for_init
521 for_update statement_expression expression_statement
522 primary_no_new_array expression primary
523 array_creation_expression array_type
524 class_instance_creation_expression field_access
525 method_invocation array_access something_dot_new
526 argument_list postfix_expression while_expression
527 post_increment_expression post_decrement_expression
528 unary_expression_not_plus_minus unary_expression
529 pre_increment_expression pre_decrement_expression
530 unary_expression_not_plus_minus cast_expression
531 multiplicative_expression additive_expression
532 shift_expression relational_expression
533 equality_expression and_expression
534 exclusive_or_expression inclusive_or_expression
535 conditional_and_expression conditional_or_expression
536 conditional_expression assignment_expression
537 left_hand_side assignment for_header for_begin
538 constant_expression do_statement_begin empty_statement
539 switch_statement synchronized_statement throw_statement
540 try_statement switch_expression switch_block
541 catches catch_clause catch_clause_parameter finally
542 anonymous_class_creation
543 %type <node> return_statement break_statement continue_statement
545 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
546 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
547 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
548 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
549 %type <operator> ASSIGN_ANY_TK assignment_operator
550 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
551 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
552 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
553 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
554 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
555 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
556 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
557 %type <operator> NEW_TK
559 %type <node> method_body
561 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
562 STRING_LIT_TK NULL_TK VOID_TK
564 %type <node> IF_TK WHILE_TK FOR_TK
566 %type <node> formal_parameter_list formal_parameter
567 method_declarator method_header
569 %type <node> primitive_type reference_type type
570 BOOLEAN_TK INTEGRAL_TK FP_TK
572 /* Added or modified JDK 1.1 rule types */
573 %type <node> type_literals array_type_literal
576 /* 19.2 Production from 2.3: The Syntactic Grammar */
577 goal:
578 compilation_unit
582 /* 19.3 Productions from 3: Lexical structure */
583 literal:
584 INT_LIT_TK
585 | FP_LIT_TK
586 | BOOL_LIT_TK
587 | CHAR_LIT_TK
588 | STRING_LIT_TK
589 | NULL_TK
592 /* 19.4 Productions from 4: Types, Values and Variables */
593 type:
594 primitive_type
595 | reference_type
598 primitive_type:
599 INTEGRAL_TK
600 | FP_TK
601 | BOOLEAN_TK
604 reference_type:
605 class_or_interface_type
606 | array_type
609 class_or_interface_type:
610 name
613 class_type:
614 class_or_interface_type /* Default rule */
617 interface_type:
618 class_or_interface_type
621 array_type:
622 primitive_type OSB_TK CSB_TK
624 $$ = build_java_array_type ($1, -1);
625 CLASS_LOADED_P ($$) = 1;
627 | name OSB_TK CSB_TK
628 { $$ = build_unresolved_array_type ($1); }
629 | array_type OSB_TK CSB_TK
630 { $$ = build_unresolved_array_type ($1); }
631 | primitive_type OSB_TK error
632 {RULE ("']' expected"); RECOVER;}
633 | array_type OSB_TK error
634 {RULE ("']' expected"); RECOVER;}
637 /* 19.5 Productions from 6: Names */
638 name:
639 simple_name /* Default rule */
640 | qualified_name /* Default rule */
643 simple_name:
644 identifier /* Default rule */
647 qualified_name:
648 name DOT_TK identifier
649 { $$ = make_qualified_name ($1, $3, $2.location); }
652 identifier:
653 ID_TK
656 /* 19.6: Production from 7: Packages */
657 compilation_unit:
658 {$$ = NULL;}
659 | package_declaration
660 | import_declarations
661 | type_declarations
662 | package_declaration import_declarations
663 | package_declaration type_declarations
664 | import_declarations type_declarations
665 | package_declaration import_declarations type_declarations
668 import_declarations:
669 import_declaration
671 $$ = NULL;
673 | import_declarations import_declaration
675 $$ = NULL;
679 type_declarations:
680 type_declaration
681 | type_declarations type_declaration
684 package_declaration:
685 PACKAGE_TK name SC_TK
687 ctxp->package = EXPR_WFL_NODE ($2);
688 register_package (ctxp->package);
690 | PACKAGE_TK error
691 {yyerror ("Missing name"); RECOVER;}
692 | PACKAGE_TK name error
693 {yyerror ("';' expected"); RECOVER;}
696 import_declaration:
697 single_type_import_declaration
698 | type_import_on_demand_declaration
701 single_type_import_declaration:
702 IMPORT_TK name SC_TK
704 tree name = EXPR_WFL_NODE ($2), last_name;
705 int i = IDENTIFIER_LENGTH (name)-1;
706 const char *last = &IDENTIFIER_POINTER (name)[i];
707 while (last != IDENTIFIER_POINTER (name))
709 if (last [0] == '.')
710 break;
711 last--;
713 last_name = get_identifier (++last);
714 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
716 tree err = find_name_in_single_imports (last_name);
717 if (err && err != name)
718 parse_error_context
719 ($2, "Ambiguous class: `%s' and `%s'",
720 IDENTIFIER_POINTER (name),
721 IDENTIFIER_POINTER (err));
722 else
723 REGISTER_IMPORT ($2, last_name);
725 else
726 REGISTER_IMPORT ($2, last_name);
728 | IMPORT_TK error
729 {yyerror ("Missing name"); RECOVER;}
730 | IMPORT_TK name error
731 {yyerror ("';' expected"); RECOVER;}
734 type_import_on_demand_declaration:
735 IMPORT_TK name DOT_TK MULT_TK SC_TK
737 tree name = EXPR_WFL_NODE ($2);
738 /* Don't import java.lang.* twice. */
739 if (name != java_lang_id)
741 read_import_dir ($2);
742 ctxp->import_demand_list =
743 chainon (ctxp->import_demand_list,
744 build_tree_list ($2, NULL_TREE));
747 | IMPORT_TK name DOT_TK error
748 {yyerror ("'*' expected"); RECOVER;}
749 | IMPORT_TK name DOT_TK MULT_TK error
750 {yyerror ("';' expected"); RECOVER;}
753 type_declaration:
754 class_declaration
755 { end_class_declaration (0); }
756 | interface_declaration
757 { end_class_declaration (0); }
758 | SC_TK
759 { $$ = NULL; }
760 | error
762 YYERROR_NOW;
763 yyerror ("Class or interface declaration expected");
767 /* 19.7 Shortened from the original:
768 modifiers: modifier | modifiers modifier
769 modifier: any of public... */
770 modifiers:
771 MODIFIER_TK
773 $$ = (1 << $1);
775 | modifiers MODIFIER_TK
777 int acc = (1 << $2);
778 if ($$ & acc)
779 parse_error_context
780 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
781 java_accstring_lookup (acc));
782 else
784 $$ |= acc;
789 /* 19.8.1 Production from $8.1: Class Declaration */
790 class_declaration:
791 modifiers CLASS_TK identifier super interfaces
792 { create_class ($1, $3, $4, $5); }
793 class_body
794 | CLASS_TK identifier super interfaces
795 { create_class (0, $2, $3, $4); }
796 class_body
797 | modifiers CLASS_TK error
798 {yyerror ("Missing class name"); RECOVER;}
799 | CLASS_TK error
800 {yyerror ("Missing class name"); RECOVER;}
801 | CLASS_TK identifier error
803 if (!ctxp->class_err) yyerror ("'{' expected");
804 DRECOVER(class1);
806 | modifiers CLASS_TK identifier error
807 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
810 super:
811 { $$ = NULL; }
812 | EXTENDS_TK class_type
813 { $$ = $2; }
814 | EXTENDS_TK class_type error
815 {yyerror ("'{' expected"); ctxp->class_err=1;}
816 | EXTENDS_TK error
817 {yyerror ("Missing super class name"); ctxp->class_err=1;}
820 interfaces:
821 { $$ = NULL_TREE; }
822 | IMPLEMENTS_TK interface_type_list
823 { $$ = $2; }
824 | IMPLEMENTS_TK error
826 ctxp->class_err=1;
827 yyerror ("Missing interface name");
831 interface_type_list:
832 interface_type
834 ctxp->interface_number = 1;
835 $$ = build_tree_list ($1, NULL_TREE);
837 | interface_type_list C_TK interface_type
839 ctxp->interface_number++;
840 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
842 | interface_type_list C_TK error
843 {yyerror ("Missing interface name"); RECOVER;}
846 class_body:
847 OCB_TK CCB_TK
849 /* Store the location of the `}' when doing xrefs */
850 if (flag_emit_xref)
851 DECL_END_SOURCE_LINE (GET_CPC ()) =
852 EXPR_WFL_ADD_COL ($2.location, 1);
853 $$ = GET_CPC ();
855 | OCB_TK class_body_declarations CCB_TK
857 /* Store the location of the `}' when doing xrefs */
858 if (flag_emit_xref)
859 DECL_END_SOURCE_LINE (GET_CPC ()) =
860 EXPR_WFL_ADD_COL ($3.location, 1);
861 $$ = GET_CPC ();
865 class_body_declarations:
866 class_body_declaration
867 | class_body_declarations class_body_declaration
870 class_body_declaration:
871 class_member_declaration
872 | static_initializer
873 | constructor_declaration
874 | block /* Added, JDK1.1, instance initializer */
876 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
877 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
881 class_member_declaration:
882 field_declaration
883 | field_declaration SC_TK
884 { $$ = $1; }
885 | method_declaration
886 | class_declaration /* Added, JDK1.1 inner classes */
887 { end_class_declaration (1); }
888 | interface_declaration /* Added, JDK1.1 inner interfaces */
889 { end_class_declaration (1); }
892 /* 19.8.2 Productions from 8.3: Field Declarations */
893 field_declaration:
894 type variable_declarators SC_TK
895 { register_fields (0, $1, $2); }
896 | modifiers type variable_declarators SC_TK
898 check_modifiers
899 ("Illegal modifier `%s' for field declaration",
900 $1, FIELD_MODIFIERS);
901 check_modifiers_consistency ($1);
902 register_fields ($1, $2, $3);
906 variable_declarators:
907 /* Should we use build_decl_list () instead ? FIXME */
908 variable_declarator /* Default rule */
909 | variable_declarators C_TK variable_declarator
910 { $$ = chainon ($1, $3); }
911 | variable_declarators C_TK error
912 {yyerror ("Missing term"); RECOVER;}
915 variable_declarator:
916 variable_declarator_id
917 { $$ = build_tree_list ($1, NULL_TREE); }
918 | variable_declarator_id ASSIGN_TK variable_initializer
920 if (java_error_count)
921 $3 = NULL_TREE;
922 $$ = build_tree_list
923 ($1, build_assignment ($2.token, $2.location, $1, $3));
925 | variable_declarator_id ASSIGN_TK error
927 yyerror ("Missing variable initializer");
928 $$ = build_tree_list ($1, NULL_TREE);
929 RECOVER;
931 | variable_declarator_id ASSIGN_TK variable_initializer error
933 yyerror ("';' expected");
934 $$ = build_tree_list ($1, NULL_TREE);
935 RECOVER;
939 variable_declarator_id:
940 identifier
941 | variable_declarator_id OSB_TK CSB_TK
942 { $$ = build_unresolved_array_type ($1); }
943 | identifier error
944 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
945 | variable_declarator_id OSB_TK error
946 {yyerror ("']' expected"); DRECOVER(vdi);}
947 | variable_declarator_id CSB_TK error
948 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
951 variable_initializer:
952 expression
953 | array_initializer
956 /* 19.8.3 Productions from 8.4: Method Declarations */
957 method_declaration:
958 method_header
960 current_function_decl = $1;
961 if (current_function_decl
962 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
963 source_start_java_method (current_function_decl);
964 else
965 current_function_decl = NULL_TREE;
967 method_body
968 { finish_method_declaration ($3); }
969 | method_header error
970 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
973 method_header:
974 type method_declarator throws
975 { $$ = method_header (0, $1, $2, $3); }
976 | VOID_TK method_declarator throws
977 { $$ = method_header (0, void_type_node, $2, $3); }
978 | modifiers type method_declarator throws
979 { $$ = method_header ($1, $2, $3, $4); }
980 | modifiers VOID_TK method_declarator throws
981 { $$ = method_header ($1, void_type_node, $3, $4); }
982 | type error
984 yyerror ("Invalid method declaration, method name required");
985 RECOVER;
987 | modifiers type error
988 {RECOVER;}
989 | VOID_TK error
990 {yyerror ("Identifier expected"); RECOVER;}
991 | modifiers VOID_TK error
992 {yyerror ("Identifier expected"); RECOVER;}
993 | modifiers error
995 yyerror ("Invalid method declaration, return type required");
996 RECOVER;
1000 method_declarator:
1001 identifier OP_TK CP_TK
1003 ctxp->formal_parameter_number = 0;
1004 $$ = method_declarator ($1, NULL_TREE);
1006 | identifier OP_TK formal_parameter_list CP_TK
1007 { $$ = method_declarator ($1, $3); }
1008 | method_declarator OSB_TK CSB_TK
1010 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1011 TREE_PURPOSE ($1) =
1012 build_unresolved_array_type (TREE_PURPOSE ($1));
1013 parse_warning_context
1014 (wfl_operator,
1015 "Discouraged form of returned type specification");
1017 | identifier OP_TK error
1018 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1019 | method_declarator OSB_TK error
1020 {yyerror ("']' expected"); RECOVER;}
1023 formal_parameter_list:
1024 formal_parameter
1026 ctxp->formal_parameter_number = 1;
1028 | formal_parameter_list C_TK formal_parameter
1030 ctxp->formal_parameter_number += 1;
1031 $$ = chainon ($1, $3);
1033 | formal_parameter_list C_TK error
1034 { yyerror ("Missing formal parameter term"); RECOVER; }
1037 formal_parameter:
1038 type variable_declarator_id
1040 $$ = build_tree_list ($2, $1);
1042 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1044 $$ = build_tree_list ($3, $2);
1045 ARG_FINAL_P ($$) = 1;
1047 | type error
1049 yyerror ("Missing identifier"); RECOVER;
1050 $$ = NULL_TREE;
1052 | final type error
1054 yyerror ("Missing identifier"); RECOVER;
1055 $$ = NULL_TREE;
1059 final:
1060 modifiers
1062 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1063 $1, ACC_FINAL);
1064 if ($1 != ACC_FINAL)
1065 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1069 throws:
1070 { $$ = NULL_TREE; }
1071 | THROWS_TK class_type_list
1072 { $$ = $2; }
1073 | THROWS_TK error
1074 {yyerror ("Missing class type term"); RECOVER;}
1077 class_type_list:
1078 class_type
1079 { $$ = build_tree_list ($1, $1); }
1080 | class_type_list C_TK class_type
1081 { $$ = tree_cons ($3, $3, $1); }
1082 | class_type_list C_TK error
1083 {yyerror ("Missing class type term"); RECOVER;}
1086 method_body:
1087 block
1088 | block SC_TK
1089 | SC_TK
1090 { $$ = NULL_TREE; } /* Probably not the right thing to do. */
1093 /* 19.8.4 Productions from 8.5: Static Initializers */
1094 static_initializer:
1095 static block
1097 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1098 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1100 | static block SC_TK /* Shouldn't be here. FIXME */
1102 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1103 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1107 static: /* Test lval.sub_token here */
1108 modifiers
1110 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1111 /* Can't have a static initializer in an innerclass */
1112 if ($1 | ACC_STATIC &&
1113 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1114 parse_error_context
1115 (MODIFIER_WFL (STATIC_TK),
1116 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1117 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1118 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1122 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1123 constructor_declaration:
1124 constructor_header
1126 current_function_decl = $1;
1127 source_start_java_method (current_function_decl);
1129 constructor_body
1130 { finish_method_declaration ($3); }
1133 constructor_header:
1134 constructor_declarator throws
1135 { $$ = method_header (0, NULL_TREE, $1, $2); }
1136 | modifiers constructor_declarator throws
1137 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1140 constructor_declarator:
1141 simple_name OP_TK CP_TK
1143 ctxp->formal_parameter_number = 0;
1144 $$ = method_declarator ($1, NULL_TREE);
1146 | simple_name OP_TK formal_parameter_list CP_TK
1147 { $$ = method_declarator ($1, $3); }
1150 constructor_body:
1151 /* Unlike regular method, we always need a complete (empty)
1152 body so we can safely perform all the required code
1153 addition (super invocation and field initialization) */
1154 block_begin constructor_block_end
1156 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1157 $$ = $2;
1159 | block_begin explicit_constructor_invocation constructor_block_end
1160 { $$ = $3; }
1161 | block_begin block_statements constructor_block_end
1162 { $$ = $3; }
1163 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1164 { $$ = $4; }
1167 constructor_block_end:
1168 block_end
1169 | block_end SC_TK
1171 /* Error recovery for that rule moved down expression_statement: rule. */
1172 explicit_constructor_invocation:
1173 this_or_super OP_TK CP_TK SC_TK
1175 $$ = build_method_invocation ($1, NULL_TREE);
1176 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1177 $$ = java_method_add_stmt (current_function_decl, $$);
1179 | this_or_super OP_TK argument_list CP_TK SC_TK
1181 $$ = build_method_invocation ($1, $3);
1182 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1183 $$ = java_method_add_stmt (current_function_decl, $$);
1185 /* Added, JDK1.1 inner classes. Modified because the rule
1186 'primary' couldn't work. */
1187 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1188 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1189 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1190 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1193 this_or_super: /* Added, simplifies error diagnostics */
1194 THIS_TK
1196 tree wfl = build_wfl_node (this_identifier_node);
1197 EXPR_WFL_LINECOL (wfl) = $1.location;
1198 $$ = wfl;
1200 | SUPER_TK
1202 tree wfl = build_wfl_node (super_identifier_node);
1203 EXPR_WFL_LINECOL (wfl) = $1.location;
1204 $$ = wfl;
1208 /* 19.9 Productions from 9: Interfaces */
1209 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1210 interface_declaration:
1211 INTERFACE_TK identifier
1212 { create_interface (0, $2, NULL_TREE); }
1213 interface_body
1214 | modifiers INTERFACE_TK identifier
1215 { create_interface ($1, $3, NULL_TREE); }
1216 interface_body
1217 | INTERFACE_TK identifier extends_interfaces
1218 { create_interface (0, $2, $3); }
1219 interface_body
1220 | modifiers INTERFACE_TK identifier extends_interfaces
1221 { create_interface ($1, $3, $4); }
1222 interface_body
1223 | INTERFACE_TK identifier error
1224 {yyerror ("'{' expected"); RECOVER;}
1225 | modifiers INTERFACE_TK identifier error
1226 {yyerror ("'{' expected"); RECOVER;}
1229 extends_interfaces:
1230 EXTENDS_TK interface_type
1232 ctxp->interface_number = 1;
1233 $$ = build_tree_list ($2, NULL_TREE);
1235 | extends_interfaces C_TK interface_type
1237 ctxp->interface_number++;
1238 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1240 | EXTENDS_TK error
1241 {yyerror ("Invalid interface type"); RECOVER;}
1242 | extends_interfaces C_TK error
1243 {yyerror ("Missing term"); RECOVER;}
1246 interface_body:
1247 OCB_TK CCB_TK
1248 { $$ = NULL_TREE; }
1249 | OCB_TK interface_member_declarations CCB_TK
1250 { $$ = NULL_TREE; }
1253 interface_member_declarations:
1254 interface_member_declaration
1255 | interface_member_declarations interface_member_declaration
1258 interface_member_declaration:
1259 constant_declaration
1260 | abstract_method_declaration
1261 | class_declaration /* Added, JDK1.1 inner classes */
1262 { end_class_declaration (1); }
1263 | interface_declaration /* Added, JDK1.1 inner interfaces */
1264 { end_class_declaration (1); }
1267 constant_declaration:
1268 field_declaration
1271 abstract_method_declaration:
1272 method_header SC_TK
1274 check_abstract_method_header ($1);
1275 current_function_decl = NULL_TREE; /* FIXME ? */
1277 | method_header error
1278 {yyerror ("';' expected"); RECOVER;}
1281 /* 19.10 Productions from 10: Arrays */
1282 array_initializer:
1283 OCB_TK CCB_TK
1284 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1285 | OCB_TK variable_initializers CCB_TK
1286 { $$ = build_new_array_init ($1.location, $2); }
1287 | OCB_TK variable_initializers C_TK CCB_TK
1288 { $$ = build_new_array_init ($1.location, $2); }
1291 variable_initializers:
1292 variable_initializer
1294 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1295 $1, NULL_TREE);
1297 | variable_initializers C_TK variable_initializer
1299 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1301 | variable_initializers C_TK error
1302 {yyerror ("Missing term"); RECOVER;}
1305 /* 19.11 Production from 14: Blocks and Statements */
1306 block:
1307 OCB_TK CCB_TK
1309 /* Store the location of the `}' when doing xrefs */
1310 if (current_function_decl && flag_emit_xref)
1311 DECL_END_SOURCE_LINE (current_function_decl) =
1312 EXPR_WFL_ADD_COL ($2.location, 1);
1313 $$ = empty_stmt_node;
1315 | block_begin block_statements block_end
1316 { $$ = $3; }
1319 block_begin:
1320 OCB_TK
1321 { enter_block (); }
1324 block_end:
1325 CCB_TK
1327 maybe_absorb_scoping_blocks ();
1328 /* Store the location of the `}' when doing xrefs */
1329 if (current_function_decl && flag_emit_xref)
1330 DECL_END_SOURCE_LINE (current_function_decl) =
1331 EXPR_WFL_ADD_COL ($1.location, 1);
1332 $$ = exit_block ();
1333 if (!BLOCK_SUBBLOCKS ($$))
1334 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1338 block_statements:
1339 block_statement
1340 | block_statements block_statement
1343 block_statement:
1344 local_variable_declaration_statement
1345 | statement
1346 { java_method_add_stmt (current_function_decl, $1); }
1347 | class_declaration /* Added, JDK1.1 local classes */
1349 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1350 end_class_declaration (1);
1354 local_variable_declaration_statement:
1355 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1358 local_variable_declaration:
1359 type variable_declarators
1360 { declare_local_variables (0, $1, $2); }
1361 | final type variable_declarators /* Added, JDK1.1 final locals */
1362 { declare_local_variables ($1, $2, $3); }
1365 statement:
1366 statement_without_trailing_substatement
1367 | labeled_statement
1368 | if_then_statement
1369 | if_then_else_statement
1370 | while_statement
1371 | for_statement
1372 { $$ = exit_block (); }
1375 statement_nsi:
1376 statement_without_trailing_substatement
1377 | labeled_statement_nsi
1378 | if_then_else_statement_nsi
1379 | while_statement_nsi
1380 | for_statement_nsi
1381 { $$ = exit_block (); }
1384 statement_without_trailing_substatement:
1385 block
1386 | empty_statement
1387 | expression_statement
1388 | switch_statement
1389 | do_statement
1390 | break_statement
1391 | continue_statement
1392 | return_statement
1393 | synchronized_statement
1394 | throw_statement
1395 | try_statement
1398 empty_statement:
1399 SC_TK
1400 { $$ = empty_stmt_node; }
1403 label_decl:
1404 identifier REL_CL_TK
1406 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1407 EXPR_WFL_NODE ($1));
1408 pushlevel (2);
1409 push_labeled_block ($$);
1410 PUSH_LABELED_BLOCK ($$);
1414 labeled_statement:
1415 label_decl statement
1416 { $$ = finish_labeled_statement ($1, $2); }
1417 | identifier error
1418 {yyerror ("':' expected"); RECOVER;}
1421 labeled_statement_nsi:
1422 label_decl statement_nsi
1423 { $$ = finish_labeled_statement ($1, $2); }
1426 /* We concentrate here a bunch of error handling rules that we couldn't write
1427 earlier, because expression_statement catches a missing ';'. */
1428 expression_statement:
1429 statement_expression SC_TK
1431 /* We have a statement. Generate a WFL around it so
1432 we can debug it */
1433 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1434 /* We know we have a statement, so set the debug
1435 info to be eventually generate here. */
1436 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1438 | error SC_TK
1440 if (ctxp->prevent_ese != lineno)
1441 yyerror ("Invalid expression statement");
1442 DRECOVER (expr_stmt);
1444 | error OCB_TK
1446 if (ctxp->prevent_ese != lineno)
1447 yyerror ("Invalid expression statement");
1448 DRECOVER (expr_stmt);
1450 | error CCB_TK
1452 if (ctxp->prevent_ese != lineno)
1453 yyerror ("Invalid expression statement");
1454 DRECOVER (expr_stmt);
1456 | this_or_super OP_TK error
1457 {yyerror ("')' expected"); RECOVER;}
1458 | this_or_super OP_TK CP_TK error
1460 parse_ctor_invocation_error ();
1461 RECOVER;
1463 | this_or_super OP_TK argument_list error
1464 {yyerror ("')' expected"); RECOVER;}
1465 | this_or_super OP_TK argument_list CP_TK error
1467 parse_ctor_invocation_error ();
1468 RECOVER;
1470 | name DOT_TK SUPER_TK error
1471 {yyerror ("'(' expected"); RECOVER;}
1472 | name DOT_TK SUPER_TK OP_TK error
1473 {yyerror ("')' expected"); RECOVER;}
1474 | name DOT_TK SUPER_TK OP_TK argument_list error
1475 {yyerror ("')' expected"); RECOVER;}
1476 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1477 {yyerror ("';' expected"); RECOVER;}
1478 | name DOT_TK SUPER_TK OP_TK CP_TK error
1479 {yyerror ("';' expected"); RECOVER;}
1482 statement_expression:
1483 assignment
1484 | pre_increment_expression
1485 | pre_decrement_expression
1486 | post_increment_expression
1487 | post_decrement_expression
1488 | method_invocation
1489 | class_instance_creation_expression
1492 if_then_statement:
1493 IF_TK OP_TK expression CP_TK statement
1495 $$ = build_if_else_statement ($2.location, $3,
1496 $5, NULL_TREE);
1498 | IF_TK error
1499 {yyerror ("'(' expected"); RECOVER;}
1500 | IF_TK OP_TK error
1501 {yyerror ("Missing term"); RECOVER;}
1502 | IF_TK OP_TK expression error
1503 {yyerror ("')' expected"); RECOVER;}
1506 if_then_else_statement:
1507 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1508 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1511 if_then_else_statement_nsi:
1512 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1513 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1516 switch_statement:
1517 switch_expression
1519 enter_block ();
1521 switch_block
1523 /* Make into "proper list" of COMPOUND_EXPRs.
1524 I.e. make the last statment also have its own
1525 COMPOUND_EXPR. */
1526 maybe_absorb_scoping_blocks ();
1527 TREE_OPERAND ($1, 1) = exit_block ();
1528 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1532 switch_expression:
1533 SWITCH_TK OP_TK expression CP_TK
1535 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1536 EXPR_WFL_LINECOL ($$) = $2.location;
1538 | SWITCH_TK error
1539 {yyerror ("'(' expected"); RECOVER;}
1540 | SWITCH_TK OP_TK error
1541 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1542 | SWITCH_TK OP_TK expression CP_TK error
1543 {yyerror ("'{' expected"); RECOVER;}
1546 /* Default assignment is there to avoid type node on switch_block
1547 node. */
1549 switch_block:
1550 OCB_TK CCB_TK
1551 { $$ = NULL_TREE; }
1552 | OCB_TK switch_labels CCB_TK
1553 { $$ = NULL_TREE; }
1554 | OCB_TK switch_block_statement_groups CCB_TK
1555 { $$ = NULL_TREE; }
1556 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1557 { $$ = NULL_TREE; }
1560 switch_block_statement_groups:
1561 switch_block_statement_group
1562 | switch_block_statement_groups switch_block_statement_group
1565 switch_block_statement_group:
1566 switch_labels block_statements
1569 switch_labels:
1570 switch_label
1571 | switch_labels switch_label
1574 switch_label:
1575 CASE_TK constant_expression REL_CL_TK
1577 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1578 EXPR_WFL_LINECOL (lab) = $1.location;
1579 java_method_add_stmt (current_function_decl, lab);
1581 | DEFAULT_TK REL_CL_TK
1583 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1584 EXPR_WFL_LINECOL (lab) = $1.location;
1585 java_method_add_stmt (current_function_decl, lab);
1587 | CASE_TK error
1588 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1589 | CASE_TK constant_expression error
1590 {yyerror ("':' expected"); RECOVER;}
1591 | DEFAULT_TK error
1592 {yyerror ("':' expected"); RECOVER;}
1595 while_expression:
1596 WHILE_TK OP_TK expression CP_TK
1598 tree body = build_loop_body ($2.location, $3, 0);
1599 $$ = build_new_loop (body);
1603 while_statement:
1604 while_expression statement
1605 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1606 | WHILE_TK error
1607 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1608 | WHILE_TK OP_TK error
1609 {yyerror ("Missing term and ')' expected"); RECOVER;}
1610 | WHILE_TK OP_TK expression error
1611 {yyerror ("')' expected"); RECOVER;}
1614 while_statement_nsi:
1615 while_expression statement_nsi
1616 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1619 do_statement_begin:
1620 DO_TK
1622 tree body = build_loop_body (0, NULL_TREE, 1);
1623 $$ = build_new_loop (body);
1625 /* Need error handing here. FIXME */
1628 do_statement:
1629 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1630 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1633 for_statement:
1634 for_begin SC_TK expression SC_TK for_update CP_TK statement
1636 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1637 $3 = build_wfl_node ($3);
1638 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1640 | for_begin SC_TK SC_TK for_update CP_TK statement
1642 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1643 /* We have not condition, so we get rid of the EXIT_EXPR */
1644 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1645 empty_stmt_node;
1647 | for_begin SC_TK error
1648 {yyerror ("Invalid control expression"); RECOVER;}
1649 | for_begin SC_TK expression SC_TK error
1650 {yyerror ("Invalid update expression"); RECOVER;}
1651 | for_begin SC_TK SC_TK error
1652 {yyerror ("Invalid update expression"); RECOVER;}
1655 for_statement_nsi:
1656 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1657 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1658 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1660 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1661 /* We have not condition, so we get rid of the EXIT_EXPR */
1662 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1663 empty_stmt_node;
1667 for_header:
1668 FOR_TK OP_TK
1670 /* This scope defined for local variable that may be
1671 defined within the scope of the for loop */
1672 enter_block ();
1674 | FOR_TK error
1675 {yyerror ("'(' expected"); DRECOVER(for_1);}
1676 | FOR_TK OP_TK error
1677 {yyerror ("Invalid init statement"); RECOVER;}
1680 for_begin:
1681 for_header for_init
1683 /* We now declare the loop body. The loop is
1684 declared as a for loop. */
1685 tree body = build_loop_body (0, NULL_TREE, 0);
1686 $$ = build_new_loop (body);
1687 FOR_LOOP_P ($$) = 1;
1688 /* The loop is added to the current block the for
1689 statement is defined within */
1690 java_method_add_stmt (current_function_decl, $$);
1693 for_init: /* Can be empty */
1694 { $$ = empty_stmt_node; }
1695 | statement_expression_list
1697 /* Init statement recorded within the previously
1698 defined block scope */
1699 $$ = java_method_add_stmt (current_function_decl, $1);
1701 | local_variable_declaration
1703 /* Local variable are recorded within the previously
1704 defined block scope */
1705 $$ = NULL_TREE;
1707 | statement_expression_list error
1708 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1711 for_update: /* Can be empty */
1712 {$$ = empty_stmt_node;}
1713 | statement_expression_list
1714 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1717 statement_expression_list:
1718 statement_expression
1719 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1720 | statement_expression_list C_TK statement_expression
1721 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1722 | statement_expression_list C_TK error
1723 {yyerror ("Missing term"); RECOVER;}
1726 break_statement:
1727 BREAK_TK SC_TK
1728 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1729 | BREAK_TK identifier SC_TK
1730 { $$ = build_bc_statement ($1.location, 1, $2); }
1731 | BREAK_TK error
1732 {yyerror ("Missing term"); RECOVER;}
1733 | BREAK_TK identifier error
1734 {yyerror ("';' expected"); RECOVER;}
1737 continue_statement:
1738 CONTINUE_TK SC_TK
1739 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1740 | CONTINUE_TK identifier SC_TK
1741 { $$ = build_bc_statement ($1.location, 0, $2); }
1742 | CONTINUE_TK error
1743 {yyerror ("Missing term"); RECOVER;}
1744 | CONTINUE_TK identifier error
1745 {yyerror ("';' expected"); RECOVER;}
1748 return_statement:
1749 RETURN_TK SC_TK
1750 { $$ = build_return ($1.location, NULL_TREE); }
1751 | RETURN_TK expression SC_TK
1752 { $$ = build_return ($1.location, $2); }
1753 | RETURN_TK error
1754 {yyerror ("Missing term"); RECOVER;}
1755 | RETURN_TK expression error
1756 {yyerror ("';' expected"); RECOVER;}
1759 throw_statement:
1760 THROW_TK expression SC_TK
1762 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1763 EXPR_WFL_LINECOL ($$) = $1.location;
1765 | THROW_TK error
1766 {yyerror ("Missing term"); RECOVER;}
1767 | THROW_TK expression error
1768 {yyerror ("';' expected"); RECOVER;}
1771 synchronized_statement:
1772 synchronized OP_TK expression CP_TK block
1774 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1775 EXPR_WFL_LINECOL ($$) =
1776 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1778 | synchronized OP_TK expression CP_TK error
1779 {yyerror ("'{' expected"); RECOVER;}
1780 | synchronized error
1781 {yyerror ("'(' expected"); RECOVER;}
1782 | synchronized OP_TK error CP_TK
1783 {yyerror ("Missing term"); RECOVER;}
1784 | synchronized OP_TK error
1785 {yyerror ("Missing term"); RECOVER;}
1788 synchronized:
1789 modifiers
1791 check_modifiers (
1792 "Illegal modifier `%s'. Only `synchronized' was expected here",
1793 $1, ACC_SYNCHRONIZED);
1794 if ($1 != ACC_SYNCHRONIZED)
1795 MODIFIER_WFL (SYNCHRONIZED_TK) =
1796 build_wfl_node (NULL_TREE);
1800 try_statement:
1801 TRY_TK block catches
1802 { $$ = build_try_statement ($1.location, $2, $3); }
1803 | TRY_TK block finally
1804 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1805 | TRY_TK block catches finally
1806 { $$ = build_try_finally_statement
1807 ($1.location, build_try_statement ($1.location,
1808 $2, $3), $4);
1810 | TRY_TK error
1811 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1814 catches:
1815 catch_clause
1816 | catches catch_clause
1818 TREE_CHAIN ($2) = $1;
1819 $$ = $2;
1823 catch_clause:
1824 catch_clause_parameter block
1826 java_method_add_stmt (current_function_decl, $2);
1827 exit_block ();
1828 $$ = $1;
1831 catch_clause_parameter:
1832 CATCH_TK OP_TK formal_parameter CP_TK
1834 /* We add a block to define a scope for
1835 formal_parameter (CCBP). The formal parameter is
1836 declared initialized by the appropriate function
1837 call */
1838 tree ccpb = enter_block ();
1839 tree init = build_assignment (ASSIGN_TK, $2.location,
1840 TREE_PURPOSE ($3),
1841 soft_exceptioninfo_call_node);
1842 declare_local_variables (0, TREE_VALUE ($3),
1843 build_tree_list (TREE_PURPOSE ($3),
1844 init));
1845 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1846 EXPR_WFL_LINECOL ($$) = $1.location;
1848 | CATCH_TK error
1849 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1850 | CATCH_TK OP_TK error
1852 yyerror ("Missing term or ')' expected");
1853 RECOVER; $$ = NULL_TREE;
1855 | CATCH_TK OP_TK error CP_TK /* That's for () */
1856 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1859 finally:
1860 FINALLY_TK block
1861 { $$ = $2; }
1862 | FINALLY_TK error
1863 {yyerror ("'{' expected"); RECOVER; }
1866 /* 19.12 Production from 15: Expressions */
1867 primary:
1868 primary_no_new_array
1869 | array_creation_expression
1872 primary_no_new_array:
1873 literal
1874 | THIS_TK
1875 { $$ = build_this ($1.location); }
1876 | OP_TK expression CP_TK
1877 {$$ = $2;}
1878 | class_instance_creation_expression
1879 | field_access
1880 | method_invocation
1881 | array_access
1882 | type_literals
1883 /* Added, JDK1.1 inner classes. Documentation is wrong
1884 refering to a 'ClassName' (class_name) rule that doesn't
1885 exist. Used name: instead. */
1886 | name DOT_TK THIS_TK
1888 tree wfl = build_wfl_node (this_identifier_node);
1889 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1891 | OP_TK expression error
1892 {yyerror ("')' expected"); RECOVER;}
1893 | name DOT_TK error
1894 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1895 | primitive_type DOT_TK error
1896 {yyerror ("'class' expected" ); RECOVER;}
1897 | VOID_TK DOT_TK error
1898 {yyerror ("'class' expected" ); RECOVER;}
1901 /* Added, JDK1.1 type literals. We can't use `type' directly, so we
1902 broke the rule down a bit. */
1904 array_type_literal:
1905 primitive_type OSB_TK CSB_TK
1907 $$ = build_java_array_type ($1, -1);
1908 CLASS_LOADED_P ($$) = 1;
1910 | name OSB_TK CSB_TK
1911 { $$ = build_unresolved_array_type ($1); }
1912 /* This triggers two reduce/reduce conflict between array_type_literal and
1913 dims. FIXME.
1914 | array_type OSB_TK CSB_TK
1915 { $$ = build_unresolved_array_type ($1); }
1919 type_literals:
1920 name DOT_TK CLASS_TK
1921 { $$ = build_incomplete_class_ref ($2.location, $1); }
1922 | array_type_literal DOT_TK CLASS_TK
1923 { $$ = build_incomplete_class_ref ($2.location, $1); }
1924 | primitive_type DOT_TK CLASS_TK
1925 { $$ = build_class_ref ($1); }
1926 | VOID_TK DOT_TK CLASS_TK
1927 { $$ = build_class_ref (void_type_node); }
1930 class_instance_creation_expression:
1931 NEW_TK class_type OP_TK argument_list CP_TK
1932 { $$ = build_new_invocation ($2, $4); }
1933 | NEW_TK class_type OP_TK CP_TK
1934 { $$ = build_new_invocation ($2, NULL_TREE); }
1935 | anonymous_class_creation
1936 /* Added, JDK1.1 inner classes, modified to use name or
1937 primary instead of primary solely which couldn't work in
1938 all situations. */
1939 | something_dot_new identifier OP_TK CP_TK
1941 tree ctor = build_new_invocation ($2, NULL_TREE);
1942 $$ = make_qualified_primary ($1, ctor,
1943 EXPR_WFL_LINECOL ($1));
1945 | something_dot_new identifier OP_TK CP_TK class_body
1946 | something_dot_new identifier OP_TK argument_list CP_TK
1948 tree ctor = build_new_invocation ($2, $4);
1949 $$ = make_qualified_primary ($1, ctor,
1950 EXPR_WFL_LINECOL ($1));
1952 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1953 | NEW_TK error SC_TK
1954 {yyerror ("'(' expected"); DRECOVER(new_1);}
1955 | NEW_TK class_type error
1956 {yyerror ("'(' expected"); RECOVER;}
1957 | NEW_TK class_type OP_TK error
1958 {yyerror ("')' or term expected"); RECOVER;}
1959 | NEW_TK class_type OP_TK argument_list error
1960 {yyerror ("')' expected"); RECOVER;}
1961 | something_dot_new error
1962 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1963 | something_dot_new identifier error
1964 {yyerror ("'(' expected"); RECOVER;}
1967 /* Created after JDK1.1 rules originally added to
1968 class_instance_creation_expression, but modified to use
1969 'class_type' instead of 'TypeName' (type_name) which is mentionned
1970 in the documentation but doesn't exist. */
1972 anonymous_class_creation:
1973 NEW_TK class_type OP_TK argument_list CP_TK
1974 { create_anonymous_class ($1.location, $2); }
1975 class_body
1977 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
1978 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
1980 end_class_declaration (1);
1982 /* Now we can craft the new expression */
1983 $$ = build_new_invocation (id, $4);
1985 /* Note that we can't possibly be here if
1986 `class_type' is an interface (in which case the
1987 anonymous class extends Object and implements
1988 `class_type', hence its constructor can't have
1989 arguments.) */
1991 /* Otherwise, the innerclass must feature a
1992 constructor matching `argument_list'. Anonymous
1993 classes are a bit special: it's impossible to
1994 define constructor for them, hence constructors
1995 must be generated following the hints provided by
1996 the `new' expression. Whether a super constructor
1997 of that nature exists or not is to be verified
1998 later on in verify_constructor_super.
2000 It's during the expansion of a `new' statement
2001 refering to an anonymous class that a ctor will
2002 be generated for the anonymous class, with the
2003 right arguments. */
2006 | NEW_TK class_type OP_TK CP_TK
2007 { create_anonymous_class ($1.location, $2); }
2008 class_body
2010 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2011 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2013 end_class_declaration (1);
2015 /* Now we can craft the new expression. The
2016 statement doesn't need to be remember so that a
2017 constructor can be generated, since its signature
2018 is already known. */
2019 $$ = build_new_invocation (id, NULL_TREE);
2023 something_dot_new: /* Added, not part of the specs. */
2024 name DOT_TK NEW_TK
2025 { $$ = $1; }
2026 | primary DOT_TK NEW_TK
2027 { $$ = $1; }
2030 argument_list:
2031 expression
2033 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2034 ctxp->formal_parameter_number = 1;
2036 | argument_list C_TK expression
2038 ctxp->formal_parameter_number += 1;
2039 $$ = tree_cons (NULL_TREE, $3, $1);
2041 | argument_list C_TK error
2042 {yyerror ("Missing term"); RECOVER;}
2045 array_creation_expression:
2046 NEW_TK primitive_type dim_exprs
2047 { $$ = build_newarray_node ($2, $3, 0); }
2048 | NEW_TK class_or_interface_type dim_exprs
2049 { $$ = build_newarray_node ($2, $3, 0); }
2050 | NEW_TK primitive_type dim_exprs dims
2051 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
2052 | NEW_TK class_or_interface_type dim_exprs dims
2053 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
2054 /* Added, JDK1.1 anonymous array. Initial documentation rule
2055 modified */
2056 | NEW_TK class_or_interface_type dims array_initializer
2058 char *sig;
2059 while (CURRENT_OSB (ctxp)--)
2060 obstack_1grow (&temporary_obstack, '[');
2061 sig = obstack_finish (&temporary_obstack);
2062 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2063 $2, get_identifier (sig), $4);
2065 | NEW_TK primitive_type dims array_initializer
2067 tree type = $2;
2068 while (CURRENT_OSB (ctxp)--)
2069 type = build_java_array_type (type, -1);
2070 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2071 build_pointer_type (type), NULL_TREE, $4);
2073 | NEW_TK error CSB_TK
2074 {yyerror ("'[' expected"); DRECOVER ("]");}
2075 | NEW_TK error OSB_TK
2076 {yyerror ("']' expected"); RECOVER;}
2079 dim_exprs:
2080 dim_expr
2081 { $$ = build_tree_list (NULL_TREE, $1); }
2082 | dim_exprs dim_expr
2083 { $$ = tree_cons (NULL_TREE, $2, $$); }
2086 dim_expr:
2087 OSB_TK expression CSB_TK
2089 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2091 $2 = build_wfl_node ($2);
2092 TREE_TYPE ($2) = NULL_TREE;
2094 EXPR_WFL_LINECOL ($2) = $1.location;
2095 $$ = $2;
2097 | OSB_TK expression error
2098 {yyerror ("']' expected"); RECOVER;}
2099 | OSB_TK error
2101 yyerror ("Missing term");
2102 yyerror ("']' expected");
2103 RECOVER;
2107 dims:
2108 OSB_TK CSB_TK
2110 int allocate = 0;
2111 /* If not initialized, allocate memory for the osb
2112 numbers stack */
2113 if (!ctxp->osb_limit)
2115 allocate = ctxp->osb_limit = 32;
2116 ctxp->osb_depth = -1;
2118 /* If capacity overflown, reallocate a bigger chunk */
2119 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2120 allocate = ctxp->osb_limit << 1;
2122 if (allocate)
2124 allocate *= sizeof (int);
2125 if (ctxp->osb_number)
2126 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2127 allocate);
2128 else
2129 ctxp->osb_number = (int *)xmalloc (allocate);
2131 ctxp->osb_depth++;
2132 CURRENT_OSB (ctxp) = 1;
2134 | dims OSB_TK CSB_TK
2135 { CURRENT_OSB (ctxp)++; }
2136 | dims OSB_TK error
2137 { yyerror ("']' expected"); RECOVER;}
2140 field_access:
2141 primary DOT_TK identifier
2142 { $$ = make_qualified_primary ($1, $3, $2.location); }
2143 /* FIXME - REWRITE TO:
2144 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2145 | SUPER_TK DOT_TK identifier
2147 tree super_wfl =
2148 build_wfl_node (super_identifier_node);
2149 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2150 $$ = make_qualified_name (super_wfl, $3, $2.location);
2152 | SUPER_TK error
2153 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2156 method_invocation:
2157 name OP_TK CP_TK
2158 { $$ = build_method_invocation ($1, NULL_TREE); }
2159 | name OP_TK argument_list CP_TK
2160 { $$ = build_method_invocation ($1, $3); }
2161 | primary DOT_TK identifier OP_TK CP_TK
2163 if (TREE_CODE ($1) == THIS_EXPR)
2164 $$ = build_this_super_qualified_invocation
2165 (1, $3, NULL_TREE, 0, $2.location);
2166 else
2168 tree invok = build_method_invocation ($3, NULL_TREE);
2169 $$ = make_qualified_primary ($1, invok, $2.location);
2172 | primary DOT_TK identifier OP_TK argument_list CP_TK
2174 if (TREE_CODE ($1) == THIS_EXPR)
2175 $$ = build_this_super_qualified_invocation
2176 (1, $3, $5, 0, $2.location);
2177 else
2179 tree invok = build_method_invocation ($3, $5);
2180 $$ = make_qualified_primary ($1, invok, $2.location);
2183 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2185 $$ = build_this_super_qualified_invocation
2186 (0, $3, NULL_TREE, $1.location, $2.location);
2188 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2190 $$ = build_this_super_qualified_invocation
2191 (0, $3, $5, $1.location, $2.location);
2193 /* Screws up thing. I let it here until I'm convinced it can
2194 be removed. FIXME
2195 | primary DOT_TK error
2196 {yyerror ("'(' expected"); DRECOVER(bad);} */
2197 | SUPER_TK DOT_TK error CP_TK
2198 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2199 | SUPER_TK DOT_TK error DOT_TK
2200 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2203 array_access:
2204 name OSB_TK expression CSB_TK
2205 { $$ = build_array_ref ($2.location, $1, $3); }
2206 | primary_no_new_array OSB_TK expression CSB_TK
2207 { $$ = build_array_ref ($2.location, $1, $3); }
2208 | name OSB_TK error
2210 yyerror ("Missing term and ']' expected");
2211 DRECOVER(array_access);
2213 | name OSB_TK expression error
2215 yyerror ("']' expected");
2216 DRECOVER(array_access);
2218 | primary_no_new_array OSB_TK error
2220 yyerror ("Missing term and ']' expected");
2221 DRECOVER(array_access);
2223 | primary_no_new_array OSB_TK expression error
2225 yyerror ("']' expected");
2226 DRECOVER(array_access);
2230 postfix_expression:
2231 primary
2232 | name
2233 | post_increment_expression
2234 | post_decrement_expression
2237 post_increment_expression:
2238 postfix_expression INCR_TK
2239 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2242 post_decrement_expression:
2243 postfix_expression DECR_TK
2244 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2247 unary_expression:
2248 pre_increment_expression
2249 | pre_decrement_expression
2250 | PLUS_TK unary_expression
2251 {$$ = build_unaryop ($1.token, $1.location, $2); }
2252 | MINUS_TK unary_expression
2253 {$$ = build_unaryop ($1.token, $1.location, $2); }
2254 | unary_expression_not_plus_minus
2255 | PLUS_TK error
2256 {yyerror ("Missing term"); RECOVER}
2257 | MINUS_TK error
2258 {yyerror ("Missing term"); RECOVER}
2261 pre_increment_expression:
2262 INCR_TK unary_expression
2263 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2264 | INCR_TK error
2265 {yyerror ("Missing term"); RECOVER}
2268 pre_decrement_expression:
2269 DECR_TK unary_expression
2270 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2271 | DECR_TK error
2272 {yyerror ("Missing term"); RECOVER}
2275 unary_expression_not_plus_minus:
2276 postfix_expression
2277 | NOT_TK unary_expression
2278 {$$ = build_unaryop ($1.token, $1.location, $2); }
2279 | NEG_TK unary_expression
2280 {$$ = build_unaryop ($1.token, $1.location, $2); }
2281 | cast_expression
2282 | NOT_TK error
2283 {yyerror ("Missing term"); RECOVER}
2284 | NEG_TK error
2285 {yyerror ("Missing term"); RECOVER}
2288 cast_expression: /* Error handling here is potentially weak */
2289 OP_TK primitive_type dims CP_TK unary_expression
2291 tree type = $2;
2292 while (CURRENT_OSB (ctxp)--)
2293 type = build_java_array_type (type, -1);
2294 ctxp->osb_depth--;
2295 $$ = build_cast ($1.location, type, $5);
2297 | OP_TK primitive_type CP_TK unary_expression
2298 { $$ = build_cast ($1.location, $2, $4); }
2299 | OP_TK expression CP_TK unary_expression_not_plus_minus
2300 { $$ = build_cast ($1.location, $2, $4); }
2301 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2303 const char *ptr;
2304 while (CURRENT_OSB (ctxp)--)
2305 obstack_1grow (&temporary_obstack, '[');
2306 ctxp->osb_depth--;
2307 obstack_grow0 (&temporary_obstack,
2308 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2309 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2310 ptr = obstack_finish (&temporary_obstack);
2311 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2312 $$ = build_cast ($1.location, $2, $5);
2314 | OP_TK primitive_type OSB_TK error
2315 {yyerror ("']' expected, invalid type expression");}
2316 | OP_TK error
2318 if (ctxp->prevent_ese != lineno)
2319 yyerror ("Invalid type expression"); RECOVER;
2320 RECOVER;
2322 | OP_TK primitive_type dims CP_TK error
2323 {yyerror ("Missing term"); RECOVER;}
2324 | OP_TK primitive_type CP_TK error
2325 {yyerror ("Missing term"); RECOVER;}
2326 | OP_TK name dims CP_TK error
2327 {yyerror ("Missing term"); RECOVER;}
2330 multiplicative_expression:
2331 unary_expression
2332 | multiplicative_expression MULT_TK unary_expression
2334 $$ = build_binop (BINOP_LOOKUP ($2.token),
2335 $2.location, $1, $3);
2337 | multiplicative_expression DIV_TK unary_expression
2339 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2340 $1, $3);
2342 | multiplicative_expression REM_TK unary_expression
2344 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2345 $1, $3);
2347 | multiplicative_expression MULT_TK error
2348 {yyerror ("Missing term"); RECOVER;}
2349 | multiplicative_expression DIV_TK error
2350 {yyerror ("Missing term"); RECOVER;}
2351 | multiplicative_expression REM_TK error
2352 {yyerror ("Missing term"); RECOVER;}
2355 additive_expression:
2356 multiplicative_expression
2357 | additive_expression PLUS_TK multiplicative_expression
2359 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2360 $1, $3);
2362 | additive_expression MINUS_TK multiplicative_expression
2364 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2365 $1, $3);
2367 | additive_expression PLUS_TK error
2368 {yyerror ("Missing term"); RECOVER;}
2369 | additive_expression MINUS_TK error
2370 {yyerror ("Missing term"); RECOVER;}
2373 shift_expression:
2374 additive_expression
2375 | shift_expression LS_TK additive_expression
2377 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2378 $1, $3);
2380 | shift_expression SRS_TK additive_expression
2382 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2383 $1, $3);
2385 | shift_expression ZRS_TK additive_expression
2387 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2388 $1, $3);
2390 | shift_expression LS_TK error
2391 {yyerror ("Missing term"); RECOVER;}
2392 | shift_expression SRS_TK error
2393 {yyerror ("Missing term"); RECOVER;}
2394 | shift_expression ZRS_TK error
2395 {yyerror ("Missing term"); RECOVER;}
2398 relational_expression:
2399 shift_expression
2400 | relational_expression LT_TK shift_expression
2402 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2403 $1, $3);
2405 | relational_expression GT_TK shift_expression
2407 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2408 $1, $3);
2410 | relational_expression LTE_TK shift_expression
2412 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2413 $1, $3);
2415 | relational_expression GTE_TK shift_expression
2417 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2418 $1, $3);
2420 | relational_expression INSTANCEOF_TK reference_type
2421 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2422 | relational_expression LT_TK error
2423 {yyerror ("Missing term"); RECOVER;}
2424 | relational_expression GT_TK error
2425 {yyerror ("Missing term"); RECOVER;}
2426 | relational_expression LTE_TK error
2427 {yyerror ("Missing term"); RECOVER;}
2428 | relational_expression GTE_TK error
2429 {yyerror ("Missing term"); RECOVER;}
2430 | relational_expression INSTANCEOF_TK error
2431 {yyerror ("Invalid reference type"); RECOVER;}
2434 equality_expression:
2435 relational_expression
2436 | equality_expression EQ_TK relational_expression
2438 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2439 $1, $3);
2441 | equality_expression NEQ_TK relational_expression
2443 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2444 $1, $3);
2446 | equality_expression EQ_TK error
2447 {yyerror ("Missing term"); RECOVER;}
2448 | equality_expression NEQ_TK error
2449 {yyerror ("Missing term"); RECOVER;}
2452 and_expression:
2453 equality_expression
2454 | and_expression AND_TK equality_expression
2456 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2457 $1, $3);
2459 | and_expression AND_TK error
2460 {yyerror ("Missing term"); RECOVER;}
2463 exclusive_or_expression:
2464 and_expression
2465 | exclusive_or_expression XOR_TK and_expression
2467 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2468 $1, $3);
2470 | exclusive_or_expression XOR_TK error
2471 {yyerror ("Missing term"); RECOVER;}
2474 inclusive_or_expression:
2475 exclusive_or_expression
2476 | inclusive_or_expression OR_TK exclusive_or_expression
2478 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2479 $1, $3);
2481 | inclusive_or_expression OR_TK error
2482 {yyerror ("Missing term"); RECOVER;}
2485 conditional_and_expression:
2486 inclusive_or_expression
2487 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2489 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2490 $1, $3);
2492 | conditional_and_expression BOOL_AND_TK error
2493 {yyerror ("Missing term"); RECOVER;}
2496 conditional_or_expression:
2497 conditional_and_expression
2498 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2500 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2501 $1, $3);
2503 | conditional_or_expression BOOL_OR_TK error
2504 {yyerror ("Missing term"); RECOVER;}
2507 conditional_expression: /* Error handling here is weak */
2508 conditional_or_expression
2509 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2511 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2512 EXPR_WFL_LINECOL ($$) = $2.location;
2514 | conditional_or_expression REL_QM_TK REL_CL_TK error
2516 YYERROR_NOW;
2517 yyerror ("Missing term");
2518 DRECOVER (1);
2520 | conditional_or_expression REL_QM_TK error
2521 {yyerror ("Missing term"); DRECOVER (2);}
2522 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2523 {yyerror ("Missing term"); DRECOVER (3);}
2526 assignment_expression:
2527 conditional_expression
2528 | assignment
2531 assignment:
2532 left_hand_side assignment_operator assignment_expression
2533 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2534 | left_hand_side assignment_operator error
2536 if (ctxp->prevent_ese != lineno)
2537 yyerror ("Missing term");
2538 DRECOVER (assign);
2542 left_hand_side:
2543 name
2544 | field_access
2545 | array_access
2548 assignment_operator:
2549 ASSIGN_ANY_TK
2550 | ASSIGN_TK
2553 expression:
2554 assignment_expression
2557 constant_expression:
2558 expression
2564 /* This section of the code deal with save/restoring parser contexts.
2565 Add mode documentation here. FIXME */
2567 /* Helper function. Create a new parser context. With
2568 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2569 context is copied, otherwise, the new context is zeroed. The newly
2570 created context becomes the current one. */
2572 static void
2573 create_new_parser_context (copy_from_previous)
2574 int copy_from_previous;
2576 struct parser_ctxt *new;
2578 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2579 if (copy_from_previous)
2581 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2582 new->saved_data_ctx = 1;
2584 else
2585 bzero ((PTR) new, sizeof (struct parser_ctxt));
2587 new->next = ctxp;
2588 ctxp = new;
2591 /* Create a new parser context and make it the current one. */
2593 void
2594 java_push_parser_context ()
2596 create_new_parser_context (0);
2597 if (ctxp->next)
2599 ctxp->incomplete_class = ctxp->next->incomplete_class;
2600 ctxp->gclass_list = ctxp->next->gclass_list;
2604 void
2605 java_pop_parser_context (generate)
2606 int generate;
2608 tree current;
2609 struct parser_ctxt *toFree, *next;
2611 if (!ctxp)
2612 return;
2614 toFree = ctxp;
2615 next = ctxp->next;
2616 if (next)
2618 next->incomplete_class = ctxp->incomplete_class;
2619 next->gclass_list = ctxp->gclass_list;
2620 lineno = ctxp->lineno;
2621 finput = ctxp->finput;
2622 current_class = ctxp->current_class;
2625 /* Set the single import class file flag to 0 for the current list
2626 of imported things */
2627 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2628 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2630 /* And restore those of the previous context */
2631 if ((ctxp = next)) /* Assignment is really meant here */
2632 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2633 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2635 /* If we pushed a context to parse a class intended to be generated,
2636 we keep it so we can remember the class. What we could actually
2637 do is to just update a list of class names. */
2638 if (generate)
2640 toFree->next = ctxp_for_generation;
2641 ctxp_for_generation = toFree;
2643 else
2644 free (toFree);
2647 /* Create a parser context for the use of saving some global
2648 variables. */
2650 void
2651 java_parser_context_save_global ()
2653 if (!ctxp)
2655 java_push_parser_context ();
2656 ctxp->saved_data_ctx = 1;
2659 /* If this context already stores data, create a new one suitable
2660 for data storage. */
2661 else if (ctxp->saved_data)
2662 create_new_parser_context (1);
2664 ctxp->finput = finput;
2665 ctxp->lineno = lineno;
2666 ctxp->current_class = current_class;
2667 ctxp->filename = input_filename;
2668 ctxp->current_function_decl = current_function_decl;
2669 ctxp->saved_data = 1;
2672 /* Restore some global variables from the previous context. Make the
2673 previous context the current one. */
2675 void
2676 java_parser_context_restore_global ()
2678 finput = ctxp->finput;
2679 lineno = ctxp->lineno;
2680 current_class = ctxp->current_class;
2681 input_filename = ctxp->filename;
2682 current_function_decl = ctxp->current_function_decl;
2683 ctxp->saved_data = 0;
2684 if (ctxp->saved_data_ctx)
2685 java_pop_parser_context (0);
2688 /* Suspend vital data for the current class/function being parsed so
2689 that an other class can be parsed. Used to let local/anonymous
2690 classes be parsed. */
2692 static void
2693 java_parser_context_suspend ()
2695 /* This makes debugging through java_debug_context easier */
2696 static const char *name = "<inner buffer context>";
2698 /* Duplicate the previous context, use it to save the globals we're
2699 interested in */
2700 create_new_parser_context (1);
2701 ctxp->current_function_decl = current_function_decl;
2702 ctxp->current_class = current_class;
2704 /* Then create a new context which inherits all data from the
2705 previous one. This will be the new current context */
2706 create_new_parser_context (1);
2708 /* Help debugging */
2709 ctxp->next->filename = name;
2712 /* Resume vital data for the current class/function being parsed so
2713 that an other class can be parsed. Used to let local/anonymous
2714 classes be parsed. The trick is the data storing file position
2715 informations must be restored to their current value, so parsing
2716 can resume as if no context was ever saved. */
2718 static void
2719 java_parser_context_resume ()
2721 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2722 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2723 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2725 /* We need to inherit the list of classes to complete/generate */
2726 restored->incomplete_class = old->incomplete_class;
2727 restored->gclass_list = old->gclass_list;
2728 restored->classd_list = old->classd_list;
2729 restored->class_list = old->class_list;
2731 /* Restore the current class and function from the saver */
2732 current_class = saver->current_class;
2733 current_function_decl = saver->current_function_decl;
2735 /* Retrive the restored context */
2736 ctxp = restored;
2738 /* Re-installed the data for the parsing to carry on */
2739 bcopy (&old->marker_begining, &ctxp->marker_begining,
2740 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2742 /* Buffer context can now be discarded */
2743 free (saver);
2744 free (old);
2747 /* Add a new anchor node to which all statement(s) initializing static
2748 and non static initialized upon declaration field(s) will be
2749 linked. */
2751 static void
2752 java_parser_context_push_initialized_field ()
2754 tree node;
2756 node = build_tree_list (NULL_TREE, NULL_TREE);
2757 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2758 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2760 node = build_tree_list (NULL_TREE, NULL_TREE);
2761 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2762 CPC_INITIALIZER_LIST (ctxp) = node;
2764 node = build_tree_list (NULL_TREE, NULL_TREE);
2765 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2766 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2769 /* Pop the lists of initialized field. If this lists aren't empty,
2770 remember them so we can use it to create and populate the $finit$
2771 or <clinit> functions. */
2773 static void
2774 java_parser_context_pop_initialized_field ()
2776 tree stmts;
2777 tree class_type = TREE_TYPE (GET_CPC ());
2779 if (CPC_INITIALIZER_LIST (ctxp))
2781 stmts = CPC_INITIALIZER_STMT (ctxp);
2782 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2783 if (stmts && !java_error_count)
2784 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2787 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2789 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2790 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2791 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2792 /* Keep initialization in order to enforce 8.5 */
2793 if (stmts && !java_error_count)
2794 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2797 /* JDK 1.1 instance initializers */
2798 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2800 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2801 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2802 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2803 if (stmts && !java_error_count)
2804 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2808 static tree
2809 reorder_static_initialized (list)
2810 tree list;
2812 /* We have to keep things in order. The alias initializer have to
2813 come first, then the initialized regular field, in reverse to
2814 keep them in lexical order. */
2815 tree marker, previous = NULL_TREE;
2816 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2817 if (TREE_CODE (marker) == TREE_LIST
2818 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2819 break;
2821 /* No static initialized, the list is fine as is */
2822 if (!previous)
2823 list = TREE_CHAIN (marker);
2825 /* No marker? reverse the whole list */
2826 else if (!marker)
2827 list = nreverse (list);
2829 /* Otherwise, reverse what's after the marker and the new reordered
2830 sublist will replace the marker. */
2831 else
2833 TREE_CHAIN (previous) = NULL_TREE;
2834 list = nreverse (list);
2835 list = chainon (TREE_CHAIN (marker), list);
2837 return list;
2840 /* Helper functions to dump the parser context stack. */
2842 #define TAB_CONTEXT(C) \
2843 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2845 static void
2846 java_debug_context_do (tab)
2847 int tab;
2849 struct parser_ctxt *copy = ctxp;
2850 while (copy)
2852 TAB_CONTEXT (tab);
2853 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2854 TAB_CONTEXT (tab);
2855 fprintf (stderr, "filename: %s\n", copy->filename);
2856 TAB_CONTEXT (tab);
2857 fprintf (stderr, "lineno: %d\n", copy->lineno);
2858 TAB_CONTEXT (tab);
2859 fprintf (stderr, "package: %s\n",
2860 (copy->package ?
2861 IDENTIFIER_POINTER (copy->package) : "<none>"));
2862 TAB_CONTEXT (tab);
2863 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2864 TAB_CONTEXT (tab);
2865 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2866 copy = copy->next;
2867 tab += 2;
2871 /* Dump the stacked up parser contexts. Intended to be called from a
2872 debugger. */
2874 void
2875 java_debug_context ()
2877 java_debug_context_do (0);
2882 /* Flag for the error report routine to issue the error the first time
2883 it's called (overriding the default behavior which is to drop the
2884 first invocation and honor the second one, taking advantage of a
2885 richer context. */
2886 static int force_error = 0;
2888 /* Reporting an constructor invocation error. */
2889 static void
2890 parse_ctor_invocation_error ()
2892 if (DECL_CONSTRUCTOR_P (current_function_decl))
2893 yyerror ("Constructor invocation must be first thing in a constructor");
2894 else
2895 yyerror ("Only constructors can invoke constructors");
2898 /* Reporting JDK1.1 features not implemented. */
2900 static tree
2901 parse_jdk1_1_error (msg)
2902 const char *msg;
2904 sorry (": `%s' JDK1.1(TM) feature", msg);
2905 java_error_count++;
2906 return empty_stmt_node;
2909 static int do_warning = 0;
2911 void
2912 yyerror (msg)
2913 const char *msg;
2915 static java_lc elc;
2916 static int prev_lineno;
2917 static const char *prev_msg;
2919 int save_lineno;
2920 char *remainder, *code_from_source;
2921 extern struct obstack temporary_obstack;
2923 if (!force_error && prev_lineno == lineno)
2924 return;
2926 /* Save current error location but report latter, when the context is
2927 richer. */
2928 if (ctxp->java_error_flag == 0)
2930 ctxp->java_error_flag = 1;
2931 elc = ctxp->elc;
2932 /* Do something to use the previous line if we're reaching the
2933 end of the file... */
2934 #ifdef VERBOSE_SKELETON
2935 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2936 #endif
2937 return;
2940 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2941 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2942 return;
2944 ctxp->java_error_flag = 0;
2945 if (do_warning)
2946 java_warning_count++;
2947 else
2948 java_error_count++;
2950 if (elc.col == 0 && msg && msg[1] == ';')
2952 elc.col = ctxp->p_line->char_col-1;
2953 elc.line = ctxp->p_line->lineno;
2956 save_lineno = lineno;
2957 prev_lineno = lineno = elc.line;
2958 prev_msg = msg;
2960 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2961 obstack_grow0 (&temporary_obstack,
2962 code_from_source, strlen (code_from_source));
2963 remainder = obstack_finish (&temporary_obstack);
2964 if (do_warning)
2965 warning ("%s.\n%s", msg, remainder);
2966 else
2967 error ("%s.\n%s", msg, remainder);
2969 /* This allow us to cheaply avoid an extra 'Invalid expression
2970 statement' error report when errors have been already reported on
2971 the same line. This occurs when we report an error but don't have
2972 a synchronization point other than ';', which
2973 expression_statement is the only one to take care of. */
2974 ctxp->prevent_ese = lineno = save_lineno;
2977 static void
2978 issue_warning_error_from_context (cl, msg, ap)
2979 tree cl;
2980 const char *msg;
2981 va_list ap;
2983 const char *saved, *saved_input_filename;
2984 char buffer [4096];
2985 vsprintf (buffer, msg, ap);
2986 force_error = 1;
2988 ctxp->elc.line = EXPR_WFL_LINENO (cl);
2989 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
2990 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
2992 /* We have a CL, that's a good reason for using it if it contains data */
2993 saved = ctxp->filename;
2994 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
2995 ctxp->filename = EXPR_WFL_FILENAME (cl);
2996 saved_input_filename = input_filename;
2997 input_filename = ctxp->filename;
2998 java_error (NULL);
2999 java_error (buffer);
3000 ctxp->filename = saved;
3001 input_filename = saved_input_filename;
3002 force_error = 0;
3005 /* Issue an error message at a current source line CL */
3007 void
3008 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3010 #ifndef ANSI_PROTOTYPES
3011 tree cl;
3012 const char *msg;
3013 #endif
3014 va_list ap;
3016 VA_START (ap, msg);
3017 #ifndef ANSI_PROTOTYPES
3018 cl = va_arg (ap, tree);
3019 msg = va_arg (ap, const char *);
3020 #endif
3021 issue_warning_error_from_context (cl, msg, ap);
3022 va_end (ap);
3025 /* Issue a warning at a current source line CL */
3027 static void
3028 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3030 #ifndef ANSI_PROTOTYPES
3031 tree cl;
3032 const char *msg;
3033 #endif
3034 va_list ap;
3036 VA_START (ap, msg);
3037 #ifndef ANSI_PROTOTYPES
3038 cl = va_arg (ap, tree);
3039 msg = va_arg (ap, const char *);
3040 #endif
3042 force_error = do_warning = 1;
3043 issue_warning_error_from_context (cl, msg, ap);
3044 do_warning = force_error = 0;
3045 va_end (ap);
3048 static tree
3049 find_expr_with_wfl (node)
3050 tree node;
3052 while (node)
3054 char code;
3055 tree to_return;
3057 switch (TREE_CODE (node))
3059 case BLOCK:
3060 node = BLOCK_EXPR_BODY (node);
3061 continue;
3063 case COMPOUND_EXPR:
3064 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3065 if (to_return)
3066 return to_return;
3067 node = TREE_OPERAND (node, 1);
3068 continue;
3070 case LOOP_EXPR:
3071 node = TREE_OPERAND (node, 0);
3072 continue;
3074 case LABELED_BLOCK_EXPR:
3075 node = TREE_OPERAND (node, 1);
3076 continue;
3078 default:
3079 code = TREE_CODE_CLASS (TREE_CODE (node));
3080 if (((code == '1') || (code == '2') || (code == 'e'))
3081 && EXPR_WFL_LINECOL (node))
3082 return node;
3083 return NULL_TREE;
3086 return NULL_TREE;
3089 /* Issue a missing return statement error. Uses METHOD to figure the
3090 last line of the method the error occurs in. */
3092 static void
3093 missing_return_error (method)
3094 tree method;
3096 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3097 parse_error_context (wfl_operator, "Missing return statement");
3100 /* Issue an unreachable statement error. From NODE, find the next
3101 statement to report appropriately. */
3102 static void
3103 unreachable_stmt_error (node)
3104 tree node;
3106 /* Browse node to find the next expression node that has a WFL. Use
3107 the location to report the error */
3108 if (TREE_CODE (node) == COMPOUND_EXPR)
3109 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3110 else
3111 node = find_expr_with_wfl (node);
3113 if (node)
3115 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3116 parse_error_context (wfl_operator, "Unreachable statement");
3118 else
3119 fatal ("Can't get valid statement - unreachable_stmt_error");
3123 java_report_errors ()
3125 if (java_error_count)
3126 fprintf (stderr, "%d error%s",
3127 java_error_count, (java_error_count == 1 ? "" : "s"));
3128 if (java_warning_count)
3129 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3130 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3131 if (java_error_count || java_warning_count)
3132 putc ('\n', stderr);
3133 return java_error_count;
3136 static char *
3137 java_accstring_lookup (flags)
3138 int flags;
3140 static char buffer [80];
3141 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3143 /* Access modifier looked-up first for easier report on forbidden
3144 access. */
3145 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3146 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3147 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3148 if (flags & ACC_STATIC) COPY_RETURN ("static");
3149 if (flags & ACC_FINAL) COPY_RETURN ("final");
3150 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3151 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3152 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3153 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3154 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3155 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3157 buffer [0] = '\0';
3158 return buffer;
3159 #undef COPY_RETURN
3162 /* Issuing error messages upon redefinition of classes, interfaces or
3163 variables. */
3165 static void
3166 classitf_redefinition_error (context, id, decl, cl)
3167 const char *context;
3168 tree id, decl, cl;
3170 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3171 context, IDENTIFIER_POINTER (id),
3172 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3173 /* Here we should point out where its redefined. It's a unicode. FIXME */
3176 static void
3177 variable_redefinition_error (context, name, type, line)
3178 tree context, name, type;
3179 int line;
3181 const char *type_name;
3183 /* Figure a proper name for type. We might haven't resolved it */
3184 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3185 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3186 else
3187 type_name = lang_printable_name (type, 0);
3189 parse_error_context (context,
3190 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3191 IDENTIFIER_POINTER (name),
3192 type_name, IDENTIFIER_POINTER (name), line);
3195 static tree
3196 build_array_from_name (type, type_wfl, name, ret_name)
3197 tree type, type_wfl, name, *ret_name;
3199 int more_dims = 0;
3200 const char *string;
3202 /* Eventually get more dims */
3203 string = IDENTIFIER_POINTER (name);
3204 while (string [more_dims] == '[')
3205 more_dims++;
3207 /* If we have, then craft a new type for this variable */
3208 if (more_dims)
3210 name = get_identifier (&string [more_dims]);
3212 /* If we have a pointer, use its type */
3213 if (TREE_CODE (type) == POINTER_TYPE)
3214 type = TREE_TYPE (type);
3216 /* Building the first dimension of a primitive type uses this
3217 function */
3218 if (JPRIMITIVE_TYPE_P (type))
3220 type = build_java_array_type (type, -1);
3221 CLASS_LOADED_P (type) = 1;
3222 more_dims--;
3224 /* Otherwise, if we have a WFL for this type, use it (the type
3225 is already an array on an unresolved type, and we just keep
3226 on adding dimensions) */
3227 else if (type_wfl)
3228 type = type_wfl;
3230 /* Add all the dimensions */
3231 while (more_dims--)
3232 type = build_unresolved_array_type (type);
3234 /* The type may have been incomplete in the first place */
3235 if (type_wfl)
3236 type = obtain_incomplete_type (type);
3239 if (ret_name)
3240 *ret_name = name;
3241 return type;
3244 /* Build something that the type identifier resolver will identify as
3245 being an array to an unresolved type. TYPE_WFL is a WFL on a
3246 identifier. */
3248 static tree
3249 build_unresolved_array_type (type_or_wfl)
3250 tree type_or_wfl;
3252 const char *ptr;
3254 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3255 just create a array type */
3256 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3258 tree type = build_java_array_type (type_or_wfl, -1);
3259 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3260 return type;
3263 obstack_1grow (&temporary_obstack, '[');
3264 obstack_grow0 (&temporary_obstack,
3265 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3266 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3267 ptr = obstack_finish (&temporary_obstack);
3268 return build_expr_wfl (get_identifier (ptr),
3269 EXPR_WFL_FILENAME (type_or_wfl),
3270 EXPR_WFL_LINENO (type_or_wfl),
3271 EXPR_WFL_COLNO (type_or_wfl));
3274 static void
3275 parser_add_interface (class_decl, interface_decl, wfl)
3276 tree class_decl, interface_decl, wfl;
3278 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3279 parse_error_context (wfl, "Interface `%s' repeated",
3280 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3283 /* Bulk of common class/interface checks. Return 1 if an error was
3284 encountered. TAG is 0 for a class, 1 for an interface. */
3286 static int
3287 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3288 int is_interface, flags;
3289 tree raw_name, qualified_name, decl, cl;
3291 tree node;
3292 int sca = 0; /* Static class allowed */
3293 int icaf = 0; /* Inner class allowed flags */
3294 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3296 if (!quiet_flag)
3297 fprintf (stderr, " %s%s %s",
3298 (CPC_INNER_P () ? "inner" : ""),
3299 (is_interface ? "interface" : "class"),
3300 IDENTIFIER_POINTER (qualified_name));
3302 /* Scope of an interface/class type name:
3303 - Can't be imported by a single type import
3304 - Can't already exists in the package */
3305 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3306 && (node = find_name_in_single_imports (raw_name)))
3308 parse_error_context
3309 (cl, "%s name `%s' clashes with imported type `%s'",
3310 (is_interface ? "Interface" : "Class"),
3311 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3312 return 1;
3314 if (decl && CLASS_COMPLETE_P (decl))
3316 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3317 qualified_name, decl, cl);
3318 return 1;
3321 if (check_inner_class_redefinition (raw_name, cl))
3322 return 1;
3324 /* If public, file name should match class/interface name, except
3325 when dealing with an inner class */
3326 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3328 const char *f;
3330 /* Contains OS dependent assumption on path separator. FIXME */
3331 for (f = &input_filename [strlen (input_filename)];
3332 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3333 f--)
3335 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3336 f++;
3337 if (strncmp (IDENTIFIER_POINTER (raw_name),
3338 f , IDENTIFIER_LENGTH (raw_name)) ||
3339 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3340 parse_error_context
3341 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3342 (is_interface ? "interface" : "class"),
3343 IDENTIFIER_POINTER (qualified_name),
3344 IDENTIFIER_POINTER (raw_name));
3347 /* Static classes can be declared only in top level classes. Note:
3348 once static, a inner class is a top level class. */
3349 if (flags & ACC_STATIC)
3351 /* Catch the specific error of declaring an class inner class
3352 with no toplevel enclosing class. Prevent check_modifiers from
3353 complaining a second time */
3354 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3356 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3357 IDENTIFIER_POINTER (qualified_name));
3358 sca = ACC_STATIC;
3360 /* Else, in the context of a top-level class declaration, let
3361 `check_modifiers' do its job, otherwise, give it a go */
3362 else
3363 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3366 /* Inner classes can be declared private or protected
3367 within their enclosing classes. */
3368 if (CPC_INNER_P ())
3370 /* A class which is local to a block can't be public, private,
3371 protected or static. But it is created final, so allow this
3372 one. */
3373 if (current_function_decl)
3374 icaf = sca = uaaf = ACC_FINAL;
3375 else
3377 check_modifiers_consistency (flags);
3378 icaf = ACC_PRIVATE|ACC_PROTECTED;
3382 if (is_interface)
3384 if (CPC_INNER_P ())
3385 uaaf = INTERFACE_INNER_MODIFIERS;
3386 else
3387 uaaf = INTERFACE_MODIFIERS;
3389 check_modifiers ("Illegal modifier `%s' for interface declaration",
3390 flags, uaaf);
3392 else
3393 check_modifiers ((current_function_decl ?
3394 "Illegal modifier `%s' for local class declaration" :
3395 "Illegal modifier `%s' for class declaration"),
3396 flags, uaaf|sca|icaf);
3397 return 0;
3400 static void
3401 make_nested_class_name (cpc_list)
3402 tree cpc_list;
3404 tree name;
3406 if (!cpc_list)
3407 return;
3408 else
3409 make_nested_class_name (TREE_CHAIN (cpc_list));
3411 /* Pick the qualified name when dealing with the first upmost
3412 enclosing class */
3413 name = (TREE_CHAIN (cpc_list) ?
3414 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3415 obstack_grow (&temporary_obstack,
3416 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3417 /* Why is NO_DOLLAR_IN_LABEL defined? */
3418 #if 0
3419 #ifdef NO_DOLLAR_IN_LABEL
3420 fatal ("make_nested_class_name: Can't use '$' as a separator "
3421 "for inner classes");
3422 #endif
3423 #endif
3424 obstack_1grow (&temporary_obstack, '$');
3427 /* Can't redefine a class already defined in an earlier scope. */
3429 static int
3430 check_inner_class_redefinition (raw_name, cl)
3431 tree raw_name, cl;
3433 tree scope_list;
3435 for (scope_list = GET_CPC_LIST (); scope_list;
3436 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3437 if (raw_name == GET_CPC_UN_NODE (scope_list))
3439 parse_error_context
3440 (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",
3441 IDENTIFIER_POINTER (raw_name));
3442 return 1;
3444 return 0;
3447 static tree
3448 find_as_inner_class (enclosing, name, cl)
3449 tree enclosing, name, cl;
3451 tree qual, to_return;
3452 if (!enclosing)
3453 return NULL_TREE;
3455 name = TYPE_NAME (name);
3457 /* First search: within the scope of `enclosing', search for name */
3458 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3459 qual = EXPR_WFL_QUALIFICATION (cl);
3460 else if (cl)
3461 qual = build_tree_list (cl, NULL_TREE);
3462 else
3463 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3465 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3466 return to_return;
3468 /* We're dealing with a qualified name. Try to resolve thing until
3469 we get something that is an enclosing class. */
3470 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3472 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3474 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3475 qual = TREE_CHAIN (qual))
3477 acc = merge_qualified_name (acc,
3478 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3479 BUILD_PTR_FROM_NAME (ptr, acc);
3480 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3483 /* A NULL qual and a decl means that the search ended
3484 successfully?!? We have to do something then. FIXME */
3486 if (decl)
3487 enclosing = decl;
3488 else
3489 qual = EXPR_WFL_QUALIFICATION (cl);
3491 /* Otherwise, create a qual for the other part of the resolution. */
3492 else
3493 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3495 return find_as_inner_class_do (qual, enclosing);
3498 /* We go inside the list of sub classes and try to find a way
3499 through. */
3501 static tree
3502 find_as_inner_class_do (qual, enclosing)
3503 tree qual, enclosing;
3505 if (!qual)
3506 return NULL_TREE;
3508 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3510 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3511 tree next_enclosing = NULL_TREE;
3512 tree inner_list;
3514 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3515 inner_list; inner_list = TREE_CHAIN (inner_list))
3517 if (TREE_VALUE (inner_list) == name_to_match)
3519 next_enclosing = TREE_PURPOSE (inner_list);
3520 break;
3523 enclosing = next_enclosing;
3526 return (!qual && enclosing ? enclosing : NULL_TREE);
3529 /* Reach all inner classes and tie their unqualified name to a
3530 DECL. */
3532 static void
3533 set_nested_class_simple_name_value (outer, set)
3534 tree outer;
3535 int set;
3537 tree l;
3539 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3540 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3541 TREE_PURPOSE (l) : NULL_TREE);
3544 static void
3545 link_nested_class_to_enclosing ()
3547 if (GET_ENCLOSING_CPC ())
3549 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3550 DECL_INNER_CLASS_LIST (enclosing) =
3551 tree_cons (GET_CPC (), GET_CPC_UN (),
3552 DECL_INNER_CLASS_LIST (enclosing));
3553 enclosing = enclosing;
3557 static tree
3558 maybe_make_nested_class_name (name)
3559 tree name;
3561 tree id = NULL_TREE;
3563 if (CPC_INNER_P ())
3565 make_nested_class_name (GET_CPC_LIST ());
3566 obstack_grow0 (&temporary_obstack,
3567 IDENTIFIER_POINTER (name),
3568 IDENTIFIER_LENGTH (name));
3569 id = get_identifier (obstack_finish (&temporary_obstack));
3570 if (ctxp->package)
3571 QUALIFIED_P (id) = 1;
3573 return id;
3576 /* If DECL is NULL, create and push a new DECL, record the current
3577 line CL and do other maintenance things. */
3579 static tree
3580 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3581 tree decl, raw_name, qualified_name, cl;
3583 if (!decl)
3584 decl = push_class (make_class (), qualified_name);
3586 /* Take care of the file and line business */
3587 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3588 /* If we're emiting xrefs, store the line/col number information */
3589 if (flag_emit_xref)
3590 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3591 else
3592 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3593 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3594 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3595 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3597 PUSH_CPC (decl, raw_name);
3598 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3600 /* Link the declaration to the already seen ones */
3601 TREE_CHAIN (decl) = ctxp->class_list;
3602 ctxp->class_list = decl;
3604 /* Create a new nodes in the global lists */
3605 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
3606 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3608 /* Install a new dependency list element */
3609 create_jdep_list (ctxp);
3611 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3612 IDENTIFIER_POINTER (qualified_name)));
3613 return decl;
3616 static void
3617 add_superinterfaces (decl, interface_list)
3618 tree decl, interface_list;
3620 tree node;
3621 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3622 takes care of ensuring that:
3623 - This is an accessible interface type,
3624 - Circularity detection.
3625 parser_add_interface is then called. If present but not defined,
3626 the check operation is delayed until the super interface gets
3627 defined. */
3628 for (node = interface_list; node; node = TREE_CHAIN (node))
3630 tree current = TREE_PURPOSE (node);
3631 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3632 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3634 if (!parser_check_super_interface (idecl, decl, current))
3635 parser_add_interface (decl, idecl, current);
3637 else
3638 register_incomplete_type (JDEP_INTERFACE,
3639 current, decl, NULL_TREE);
3643 /* Create an interface in pass1 and return its decl. Return the
3644 interface's decl in pass 2. */
3646 static tree
3647 create_interface (flags, id, super)
3648 int flags;
3649 tree id, super;
3651 tree raw_name = EXPR_WFL_NODE (id);
3652 tree q_name = parser_qualified_classname (raw_name);
3653 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3655 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3657 /* Basic checks: scope, redefinition, modifiers */
3658 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3660 PUSH_ERROR ();
3661 return NULL_TREE;
3664 /* Suspend the current parsing context if we're parsing an inner
3665 interface */
3666 if (CPC_INNER_P ())
3667 java_parser_context_suspend ();
3669 /* Push a new context for (static) initialized upon declaration fields */
3670 java_parser_context_push_initialized_field ();
3672 /* Interface modifiers check
3673 - public/abstract allowed (already done at that point)
3674 - abstract is obsolete (comes first, it's a warning, or should be)
3675 - Can't use twice the same (checked in the modifier rule) */
3676 if ((flags & ACC_ABSTRACT) && flag_redundant)
3677 parse_warning_context
3678 (MODIFIER_WFL (ABSTRACT_TK),
3679 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3681 /* Create a new decl if DECL is NULL, otherwise fix it */
3682 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3684 /* Set super info and mark the class a complete */
3685 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3686 object_type_node, ctxp->interface_number);
3687 ctxp->interface_number = 0;
3688 CLASS_COMPLETE_P (decl) = 1;
3689 add_superinterfaces (decl, super);
3691 return decl;
3694 /* Anonymous class counter. Will be reset to 1 every time a non
3695 anonymous class gets created. */
3696 static int anonymous_class_counter = 1;
3698 /* Patch anonymous class CLASS, by either extending or implementing
3699 DEP. */
3701 static void
3702 patch_anonymous_class (type_decl, class_decl, wfl)
3703 tree type_decl, class_decl, wfl;
3705 tree class = TREE_TYPE (class_decl);
3706 tree type = TREE_TYPE (type_decl);
3707 tree binfo = TYPE_BINFO (class);
3709 /* If it's an interface, implement it */
3710 if (CLASS_INTERFACE (type_decl))
3712 tree s_binfo;
3713 int length;
3715 if (parser_check_super_interface (type_decl, class_decl, wfl))
3716 return;
3718 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3719 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3720 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3721 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3722 /* And add the interface */
3723 parser_add_interface (class_decl, type_decl, wfl);
3725 /* Otherwise, it's a type we want to extend */
3726 else
3728 if (parser_check_super (type_decl, class_decl, wfl))
3729 return;
3730 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3734 static tree
3735 create_anonymous_class (location, type_name)
3736 int location;
3737 tree type_name;
3739 char buffer [80];
3740 tree super = NULL_TREE, itf = NULL_TREE;
3741 tree id, type_decl, class;
3743 /* The unqualified name of the anonymous class. It's just a number. */
3744 sprintf (buffer, "%d", anonymous_class_counter++);
3745 id = build_wfl_node (get_identifier (buffer));
3746 EXPR_WFL_LINECOL (id) = location;
3748 /* We know about the type to extend/implement. We go ahead */
3749 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3751 /* Create a class which either implements on extends the designated
3752 class. The class bears an innacessible name. */
3753 if (CLASS_INTERFACE (type_decl))
3755 /* It's OK to modify it here. It's been already used and
3756 shouldn't be reused */
3757 ctxp->interface_number = 1;
3758 /* Interfaces should presented as a list of WFLs */
3759 itf = build_tree_list (type_name, NULL_TREE);
3761 else
3762 super = type_name;
3765 class = create_class (ACC_FINAL, id, super, itf);
3767 /* We didn't know anything about the stuff. We register a dependence. */
3768 if (!type_decl)
3769 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3771 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3772 return class;
3775 /* Create a class in pass1 and return its decl. Return class
3776 interface's decl in pass 2. */
3778 static tree
3779 create_class (flags, id, super, interfaces)
3780 int flags;
3781 tree id, super, interfaces;
3783 tree raw_name = EXPR_WFL_NODE (id);
3784 tree class_id, decl;
3785 tree super_decl_type;
3787 class_id = parser_qualified_classname (raw_name);
3788 decl = IDENTIFIER_CLASS_VALUE (class_id);
3789 EXPR_WFL_NODE (id) = class_id;
3791 /* Basic check: scope, redefinition, modifiers */
3792 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3794 PUSH_ERROR ();
3795 return NULL_TREE;
3798 /* Suspend the current parsing context if we're parsing an inner
3799 class or an anonymous class. */
3800 if (CPC_INNER_P ())
3801 java_parser_context_suspend ();
3802 /* Push a new context for (static) initialized upon declaration fields */
3803 java_parser_context_push_initialized_field ();
3805 /* Class modifier check:
3806 - Allowed modifier (already done at that point)
3807 - abstract AND final forbidden
3808 - Public classes defined in the correct file */
3809 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3810 parse_error_context
3811 (id, "Class `%s' can't be declared both abstract and final",
3812 IDENTIFIER_POINTER (raw_name));
3814 /* Create a new decl if DECL is NULL, otherwise fix it */
3815 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3817 /* If SUPER exists, use it, otherwise use Object */
3818 if (super)
3820 /* Can't extend java.lang.Object */
3821 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3823 parse_error_context (id, "Can't extend `java.lang.Object'");
3824 return NULL_TREE;
3827 super_decl_type =
3828 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3830 else if (TREE_TYPE (decl) != object_type_node)
3831 super_decl_type = object_type_node;
3832 /* We're defining java.lang.Object */
3833 else
3834 super_decl_type = NULL_TREE;
3836 /* Set super info and mark the class a complete */
3837 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3838 ctxp->interface_number);
3839 ctxp->interface_number = 0;
3840 CLASS_COMPLETE_P (decl) = 1;
3841 add_superinterfaces (decl, interfaces);
3843 /* Add the private this$<n> field, Replicate final locals still in
3844 scope as private final fields mangled like val$<local_name>.
3845 This doesn't not occur for top level (static) inner classes. */
3846 if (PURE_INNER_CLASS_DECL_P (decl))
3847 add_inner_class_fields (decl, current_function_decl);
3849 /* If doing xref, store the location at which the inherited class
3850 (if any) was seen. */
3851 if (flag_emit_xref && super)
3852 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3854 /* Eventually sets the @deprecated tag flag */
3855 CHECK_DEPRECATED (decl);
3857 /* Reset the anonymous class counter when declaring non inner classes */
3858 if (!INNER_CLASS_DECL_P (decl))
3859 anonymous_class_counter = 1;
3861 return decl;
3864 /* End a class declaration: register the statements used to create
3865 $finit$ and <clinit>, pop the current class and resume the prior
3866 parser context if necessary. */
3868 static void
3869 end_class_declaration (resume)
3870 int resume;
3872 /* If an error occured, context weren't pushed and won't need to be
3873 popped by a resume. */
3874 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3876 java_parser_context_pop_initialized_field ();
3877 POP_CPC ();
3878 if (resume && no_error_occured)
3879 java_parser_context_resume ();
3881 /* We're ending a class declaration, this is a good time to reset
3882 the interface cout. Note that might have been already done in
3883 create_interface, but if at that time an inner class was being
3884 dealt with, the interface count was reset in a context created
3885 for the sake of handling inner classes declaration. */
3886 ctxp->interface_number = 0;
3889 static void
3890 add_inner_class_fields (class_decl, fct_decl)
3891 tree class_decl;
3892 tree fct_decl;
3894 tree block, marker, f;
3896 f = add_field (TREE_TYPE (class_decl),
3897 build_current_thisn (TREE_TYPE (class_decl)),
3898 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3899 ACC_PRIVATE);
3900 FIELD_THISN (f) = 1;
3902 if (!fct_decl)
3903 return;
3905 for (block = GET_CURRENT_BLOCK (fct_decl);
3906 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3908 tree decl;
3909 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3911 char *name, *pname;
3912 tree wfl, init, list;
3914 /* Avoid non final arguments. */
3915 if (!LOCAL_FINAL (decl))
3916 continue;
3918 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3919 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3920 wfl = build_wfl_node (get_identifier (name));
3921 init = build_wfl_node (get_identifier (pname));
3922 /* Build an initialization for the field: it will be
3923 initialized by a parameter added to $finit$, bearing a
3924 mangled name of the field itself (param$<n>.) The
3925 parameter is provided to $finit$ by the constructor
3926 invoking it (hence the constructor will also feature a
3927 hidden parameter, set to the value of the outer context
3928 local at the time the inner class is created.)
3930 Note: we take into account all possible locals that can
3931 be accessed by the inner class. It's actually not trivial
3932 to minimize these aliases down to the ones really
3933 used. One way to do that would be to expand all regular
3934 methods first, then $finit$ to get a picture of what's
3935 used. It works with the exception that we would have to
3936 go back on all constructor invoked in regular methods to
3937 have their invokation reworked (to include the right amount
3938 of alias initializer parameters.)
3940 The only real way around, I think, is a first pass to
3941 identify locals really used in the inner class. We leave
3942 the flag FIELD_LOCAL_ALIAS_USED around for that future
3943 use.
3945 On the other hand, it only affect local inner classes,
3946 whose constructors (and $finit$ call) will be featuring
3947 unecessary arguments. It's easy for a developper to keep
3948 this number of parameter down by using the `final'
3949 keyword only when necessary. For the time being, we can
3950 issue a warning on unecessary finals. FIXME */
3951 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
3952 wfl, init);
3954 /* Register the field. The TREE_LIST holding the part
3955 initialized/initializer will be marked ARG_FINAL_P so
3956 that the created field can be marked
3957 FIELD_LOCAL_ALIAS. */
3958 list = build_tree_list (wfl, init);
3959 ARG_FINAL_P (list) = 1;
3960 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
3964 if (!CPC_INITIALIZER_STMT (ctxp))
3965 return;
3967 /* If we ever registered an alias field, insert and marker to
3968 remeber where the list ends. The second part of the list (the one
3969 featuring initialized fields) so it can be later reversed to
3970 enforce 8.5. The marker will be removed during that operation. */
3971 marker = build_tree_list (NULL_TREE, NULL_TREE);
3972 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
3973 SET_CPC_INITIALIZER_STMT (ctxp, marker);
3976 /* Can't use lookup_field () since we don't want to load the class and
3977 can't set the CLASS_LOADED_P flag */
3979 static tree
3980 find_field (class, name)
3981 tree class;
3982 tree name;
3984 tree decl;
3985 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
3987 if (DECL_NAME (decl) == name)
3988 return decl;
3990 return NULL_TREE;
3993 /* Wrap around lookup_field that doesn't potentially upset the value
3994 of CLASS */
3996 static tree
3997 lookup_field_wrapper (class, name)
3998 tree class, name;
4000 tree type = class;
4001 tree decl = NULL_TREE;
4002 java_parser_context_save_global ();
4004 /* Last chance: if we're within the context of an inner class, we
4005 might be trying to access a local variable defined in an outer
4006 context. We try to look for it now. */
4007 if (INNER_CLASS_TYPE_P (class))
4009 char *alias_buffer;
4010 tree new_name;
4011 MANGLE_OUTER_LOCAL_VARIABLE_NAME (alias_buffer, name);
4012 new_name = get_identifier (alias_buffer);
4013 decl = lookup_field (&type, new_name);
4014 if (decl && decl != error_mark_node)
4015 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4017 if (!decl || decl == error_mark_node)
4019 type = class;
4020 decl = lookup_field (&type, name);
4023 java_parser_context_restore_global ();
4024 return decl == error_mark_node ? NULL : decl;
4027 /* Find duplicate field within the same class declarations and report
4028 the error. Returns 1 if a duplicated field was found, 0
4029 otherwise. */
4031 static int
4032 duplicate_declaration_error_p (new_field_name, new_type, cl)
4033 tree new_field_name, new_type, cl;
4035 /* This might be modified to work with method decl as well */
4036 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4037 if (decl)
4039 char *t1 = xstrdup (purify_type_name
4040 ((TREE_CODE (new_type) == POINTER_TYPE
4041 && TREE_TYPE (new_type) == NULL_TREE) ?
4042 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4043 lang_printable_name (new_type, 1)));
4044 /* The type may not have been completed by the time we report
4045 the error */
4046 char *t2 = xstrdup (purify_type_name
4047 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4048 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4049 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4050 lang_printable_name (TREE_TYPE (decl), 1)));
4051 parse_error_context
4052 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4053 t1, IDENTIFIER_POINTER (new_field_name),
4054 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4055 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4056 free (t1);
4057 free (t2);
4058 return 1;
4060 return 0;
4063 /* Field registration routine. If TYPE doesn't exist, field
4064 declarations are linked to the undefined TYPE dependency list, to
4065 be later resolved in java_complete_class () */
4067 static void
4068 register_fields (flags, type, variable_list)
4069 int flags;
4070 tree type, variable_list;
4072 tree current, saved_type;
4073 tree class_type = NULL_TREE;
4074 int saved_lineno = lineno;
4075 int must_chain = 0;
4076 tree wfl = NULL_TREE;
4078 if (GET_CPC ())
4079 class_type = TREE_TYPE (GET_CPC ());
4081 if (!class_type || class_type == error_mark_node)
4082 return;
4084 /* If we're adding fields to interfaces, those fields are public,
4085 static, final */
4086 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4088 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4089 flags, ACC_PUBLIC, "interface field(s)");
4090 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4091 flags, ACC_STATIC, "interface field(s)");
4092 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4093 flags, ACC_FINAL, "interface field(s)");
4094 check_modifiers ("Illegal interface member modifier `%s'", flags,
4095 INTERFACE_FIELD_MODIFIERS);
4096 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4099 /* Obtain a suitable type for resolution, if necessary */
4100 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4102 /* If TYPE is fully resolved and we don't have a reference, make one */
4103 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4105 for (current = variable_list, saved_type = type; current;
4106 current = TREE_CHAIN (current), type = saved_type)
4108 tree real_type;
4109 tree field_decl;
4110 tree cl = TREE_PURPOSE (current);
4111 tree init = TREE_VALUE (current);
4112 tree current_name = EXPR_WFL_NODE (cl);
4114 /* Can't declare non-final static fields in inner classes */
4115 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4116 && !(flags & ACC_FINAL))
4117 parse_error_context
4118 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4119 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4120 lang_printable_name (class_type, 0));
4122 /* Process NAME, as it may specify extra dimension(s) for it */
4123 type = build_array_from_name (type, wfl, current_name, &current_name);
4125 /* Type adjustment. We may have just readjusted TYPE because
4126 the variable specified more dimensions. Make sure we have
4127 a reference if we can and don't have one already. Also
4128 change the name if we have an init. */
4129 if (type != saved_type)
4131 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4132 if (init)
4133 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4136 real_type = GET_REAL_TYPE (type);
4137 /* Check for redeclarations */
4138 if (duplicate_declaration_error_p (current_name, real_type, cl))
4139 continue;
4141 /* Set lineno to the line the field was found and create a
4142 declaration for it. Eventually sets the @deprecated tag flag. */
4143 if (flag_emit_xref)
4144 lineno = EXPR_WFL_LINECOL (cl);
4145 else
4146 lineno = EXPR_WFL_LINENO (cl);
4147 field_decl = add_field (class_type, current_name, real_type, flags);
4148 CHECK_DEPRECATED (field_decl);
4150 /* If the couple initializer/initialized is marked ARG_FINAL_P, we
4151 mark the created field FIELD_LOCAL_ALIAS, so that we can
4152 hide parameters to this inner class $finit$ and constructors. */
4153 if (ARG_FINAL_P (current))
4154 FIELD_LOCAL_ALIAS (field_decl) = 1;
4156 /* Check if we must chain. */
4157 if (must_chain)
4158 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4160 /* If we have an initialization value tied to the field */
4161 if (init)
4163 /* The field is declared static */
4164 if (flags & ACC_STATIC)
4166 /* We include the field and its initialization part into
4167 a list used to generate <clinit>. After <clinit> is
4168 walked, field initializations will be processed and
4169 fields initialized with known constants will be taken
4170 out of <clinit> and have their DECL_INITIAL set
4171 appropriately. */
4172 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4173 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4174 if (TREE_OPERAND (init, 1)
4175 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4176 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4178 /* A non-static field declared with an immediate initialization is
4179 to be initialized in <init>, if any. This field is remembered
4180 to be processed at the time of the generation of <init>. */
4181 else
4183 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4184 SET_CPC_INITIALIZER_STMT (ctxp, init);
4186 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4187 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4190 lineno = saved_lineno;
4193 /* Generate $finit$, using the list of initialized fields to populate
4194 its body. $finit$'s parameter(s) list is adjusted to include the
4195 one(s) used to initialized the field(s) caching outer context
4196 local(s). */
4198 static tree
4199 generate_finit (class_type)
4200 tree class_type;
4202 int count = 0;
4203 tree list = TYPE_FINIT_STMT_LIST (class_type);
4204 tree mdecl, current, parms;
4206 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4207 class_type, NULL_TREE,
4208 &count);
4209 CRAFTED_PARAM_LIST_FIXUP (parms);
4210 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4211 finit_identifier_node, parms);
4212 fix_method_argument_names (parms, mdecl);
4213 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4214 mdecl, NULL_TREE);
4215 DECL_FUNCTION_NAP (mdecl) = count;
4216 start_artificial_method_body (mdecl);
4218 for (current = list; current; current = TREE_CHAIN (current))
4219 java_method_add_stmt (mdecl,
4220 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4221 current));
4222 end_artificial_method_body (mdecl);
4223 return mdecl;
4226 static void
4227 add_instance_initializer (mdecl)
4228 tree mdecl;
4230 tree current;
4231 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4232 tree compound = NULL_TREE;
4234 if (stmt_list)
4236 for (current = stmt_list; current; current = TREE_CHAIN (current))
4237 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4239 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4240 NULL_TREE, compound));
4244 /* Shared accros method_declarator and method_header to remember the
4245 patch stage that was reached during the declaration of the method.
4246 A method DECL is built differently is there is no patch
4247 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4248 pending on the currently defined method. */
4250 static int patch_stage;
4252 /* Check the method declaration and add the method to its current
4253 class. If the argument list is known to contain incomplete types,
4254 the method is partially added and the registration will be resume
4255 once the method arguments resolved. If TYPE is NULL, we're dealing
4256 with a constructor. */
4258 static tree
4259 method_header (flags, type, mdecl, throws)
4260 int flags;
4261 tree type, mdecl, throws;
4263 tree meth = TREE_VALUE (mdecl);
4264 tree id = TREE_PURPOSE (mdecl);
4265 tree type_wfl = NULL_TREE;
4266 tree meth_name = NULL_TREE;
4267 tree current, orig_arg, this_class = NULL;
4268 int saved_lineno;
4269 int constructor_ok = 0, must_chain;
4270 int count;
4272 check_modifiers_consistency (flags);
4274 if (GET_CPC ())
4275 this_class = TREE_TYPE (GET_CPC ());
4277 if (!this_class || this_class == error_mark_node)
4278 return NULL_TREE;
4280 /* There are some forbidden modifiers for an abstract method and its
4281 class must be abstract as well. */
4282 if (type && (flags & ACC_ABSTRACT))
4284 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4285 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4286 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4287 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4288 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4289 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4290 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4291 parse_error_context
4292 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4293 IDENTIFIER_POINTER (DECL_NAME (ctxp->current_parsed_class)),
4294 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4297 /* Things to be checked when declaring a constructor */
4298 if (!type)
4300 int ec = java_error_count;
4301 /* 8.6: Constructor declarations: we might be trying to define a
4302 method without specifying a return type. */
4303 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4304 parse_error_context
4305 (id, "Invalid method declaration, return type required");
4306 /* 8.6.3: Constructor modifiers */
4307 else
4309 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4310 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4311 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4312 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4313 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4315 /* If we found error here, we don't consider it's OK to tread
4316 the method definition as a constructor, for the rest of this
4317 function */
4318 if (ec == java_error_count)
4319 constructor_ok = 1;
4322 /* Method declared within the scope of an interface are implicitly
4323 abstract and public. Conflicts with other erroneously provided
4324 modifiers are checked right after. */
4326 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4328 /* If FLAGS isn't set because of a modifier, turn the
4329 corresponding modifier WFL to NULL so we issue a warning on
4330 the obsolete use of the modifier */
4331 if (!(flags & ACC_PUBLIC))
4332 MODIFIER_WFL (PUBLIC_TK) = NULL;
4333 if (!(flags & ACC_ABSTRACT))
4334 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4335 flags |= ACC_PUBLIC;
4336 flags |= ACC_ABSTRACT;
4339 /* Inner class can't declare static methods */
4340 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4342 parse_error_context
4343 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4344 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4345 lang_printable_name (this_class, 0));
4348 /* Modifiers context reset moved up, so abstract method declaration
4349 modifiers can be later checked. */
4351 /* Set constructor returned type to void and method name to <init>,
4352 unless we found an error identifier the constructor (in which
4353 case we retain the original name) */
4354 if (!type)
4356 type = void_type_node;
4357 if (constructor_ok)
4358 meth_name = init_identifier_node;
4360 else
4361 meth_name = EXPR_WFL_NODE (id);
4363 /* Do the returned type resolution and registration if necessary */
4364 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4366 if (meth_name)
4367 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4368 EXPR_WFL_NODE (id) = meth_name;
4369 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4371 if (must_chain)
4373 patch_stage = JDEP_METHOD_RETURN;
4374 register_incomplete_type (patch_stage, type_wfl, id, type);
4375 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4377 else
4378 TREE_TYPE (meth) = type;
4380 saved_lineno = lineno;
4381 /* When defining an abstract or interface method, the curly
4382 bracket at level 1 doesn't exist because there is no function
4383 body */
4384 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4385 EXPR_WFL_LINENO (id));
4387 /* Remember the original argument list */
4388 orig_arg = TYPE_ARG_TYPES (meth);
4390 if (patch_stage) /* includes ret type and/or all args */
4392 jdep *jdep;
4393 meth = add_method_1 (this_class, flags, meth_name, meth);
4394 /* Patch for the return type */
4395 if (patch_stage == JDEP_METHOD_RETURN)
4397 jdep = CLASSD_LAST (ctxp->classd_list);
4398 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4400 /* This is the stop JDEP. METH allows the function's signature
4401 to be computed. */
4402 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4404 else
4405 meth = add_method (this_class, flags, meth_name,
4406 build_java_signature (meth));
4408 /* Remember final parameters */
4409 MARK_FINAL_PARMS (meth, orig_arg);
4411 /* Fix the method argument list so we have the argument name
4412 information */
4413 fix_method_argument_names (orig_arg, meth);
4415 /* Register the parameter number and re-install the current line
4416 number */
4417 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4418 lineno = saved_lineno;
4420 /* Register exception specified by the `throws' keyword for
4421 resolution and set the method decl appropriate field to the list.
4422 Note: the grammar ensures that what we get here are class
4423 types. */
4424 if (throws)
4426 throws = nreverse (throws);
4427 for (current = throws; current; current = TREE_CHAIN (current))
4429 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4430 NULL_TREE, NULL_TREE);
4431 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4432 &TREE_VALUE (current);
4434 DECL_FUNCTION_THROWS (meth) = throws;
4437 /* We set the DECL_NAME to ID so we can track the location where
4438 the function was declared. This allow us to report
4439 redefinition error accurately. When method are verified,
4440 DECL_NAME is reinstalled properly (using the content of the
4441 WFL node ID) (see check_method_redefinition). We don't do that
4442 when Object is being defined. Constructor <init> names will be
4443 reinstalled the same way. */
4444 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4445 DECL_NAME (meth) = id;
4447 /* Set the flag if we correctly processed a constructor */
4448 if (constructor_ok)
4450 DECL_CONSTRUCTOR_P (meth) = 1;
4451 /* Compute and store the number of artificial parameters declared
4452 for this constructor */
4453 for (count = 0, current = TYPE_FIELDS (this_class); current;
4454 current = TREE_CHAIN (current))
4455 if (FIELD_LOCAL_ALIAS (current))
4456 count++;
4457 DECL_FUNCTION_NAP (meth) = count;
4460 /* Eventually set the @deprecated tag flag */
4461 CHECK_DEPRECATED (meth);
4463 /* If doing xref, store column and line number information instead
4464 of the line number only. */
4465 if (flag_emit_xref)
4466 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4468 return meth;
4471 static void
4472 fix_method_argument_names (orig_arg, meth)
4473 tree orig_arg, meth;
4475 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4476 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4478 TREE_PURPOSE (arg) = this_identifier_node;
4479 arg = TREE_CHAIN (arg);
4481 while (orig_arg != end_params_node)
4483 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4484 orig_arg = TREE_CHAIN (orig_arg);
4485 arg = TREE_CHAIN (arg);
4489 /* Complete the method declaration with METHOD_BODY. */
4491 static void
4492 finish_method_declaration (method_body)
4493 tree method_body;
4495 int flags;
4497 if (!current_function_decl)
4498 return;
4500 flags = get_access_flags_from_decl (current_function_decl);
4502 /* 8.4.5 Method Body */
4503 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4505 tree wfl = DECL_NAME (current_function_decl);
4506 parse_error_context (wfl,
4507 "%s method `%s' can't have a body defined",
4508 (METHOD_NATIVE (current_function_decl) ?
4509 "Native" : "Abstract"),
4510 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4511 method_body = NULL_TREE;
4513 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4515 tree wfl = DECL_NAME (current_function_decl);
4516 parse_error_context
4517 (wfl,
4518 "Non native and non abstract method `%s' must have a body defined",
4519 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4520 method_body = NULL_TREE;
4523 if (flag_emit_class_files && method_body
4524 && TREE_CODE (method_body) == NOP_EXPR
4525 && TREE_TYPE (current_function_decl)
4526 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4527 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4529 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4530 maybe_absorb_scoping_blocks ();
4531 /* Exit function's body */
4532 exit_block ();
4533 /* Merge last line of the function with first line, directly in the
4534 function decl. It will be used to emit correct debug info. */
4535 if (!flag_emit_xref)
4536 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4538 /* Since function's argument's list are shared, reset the
4539 ARG_FINAL_P parameter that might have been set on some of this
4540 function parameters. */
4541 UNMARK_FINAL_PARMS (current_function_decl);
4543 /* So we don't have an irrelevant function declaration context for
4544 the next static block we'll see. */
4545 current_function_decl = NULL_TREE;
4548 /* Build a an error message for constructor circularity errors. */
4550 static char *
4551 constructor_circularity_msg (from, to)
4552 tree from, to;
4554 static char string [4096];
4555 char *t = xstrdup (lang_printable_name (from, 0));
4556 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4557 free (t);
4558 return string;
4561 /* Verify a circular call to METH. Return 1 if an error is found, 0
4562 otherwise. */
4564 static int
4565 verify_constructor_circularity (meth, current)
4566 tree meth, current;
4568 static tree list = NULL_TREE;
4569 tree c;
4570 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4572 if (TREE_VALUE (c) == meth)
4574 char *t;
4575 if (list)
4577 tree liste;
4578 list = nreverse (list);
4579 for (liste = list; liste; liste = TREE_CHAIN (liste))
4581 parse_error_context
4582 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4583 constructor_circularity_msg
4584 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4585 java_error_count--;
4588 t = xstrdup (lang_printable_name (meth, 0));
4589 parse_error_context (TREE_PURPOSE (c),
4590 "%s: recursive invocation of constructor `%s'",
4591 constructor_circularity_msg (current, meth), t);
4592 free (t);
4593 list = NULL_TREE;
4594 return 1;
4597 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4599 list = tree_cons (c, current, list);
4600 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4601 return 1;
4602 list = TREE_CHAIN (list);
4604 return 0;
4607 /* Check modifiers that can be declared but exclusively */
4609 static void
4610 check_modifiers_consistency (flags)
4611 int flags;
4613 int acc_count = 0;
4614 tree cl = NULL_TREE;
4616 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4617 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4618 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4619 if (acc_count > 1)
4620 parse_error_context
4621 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4623 acc_count = 0;
4624 cl = NULL_TREE;
4625 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4626 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4627 if (acc_count > 1)
4628 parse_error_context (cl,
4629 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4632 /* Check the methode header METH for abstract specifics features */
4634 static void
4635 check_abstract_method_header (meth)
4636 tree meth;
4638 int flags = get_access_flags_from_decl (meth);
4639 /* DECL_NAME might still be a WFL node */
4640 tree name = GET_METHOD_NAME (meth);
4642 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4643 ACC_ABSTRACT, "abstract method",
4644 IDENTIFIER_POINTER (name));
4645 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4646 ACC_PUBLIC, "abstract method",
4647 IDENTIFIER_POINTER (name));
4649 check_modifiers ("Illegal modifier `%s' for interface method",
4650 flags, INTERFACE_METHOD_MODIFIERS);
4653 /* Create a FUNCTION_TYPE node and start augmenting it with the
4654 declared function arguments. Arguments type that can't be resolved
4655 are left as they are, but the returned node is marked as containing
4656 incomplete types. */
4658 static tree
4659 method_declarator (id, list)
4660 tree id, list;
4662 tree arg_types = NULL_TREE, current, node;
4663 tree meth = make_node (FUNCTION_TYPE);
4664 jdep *jdep;
4666 patch_stage = JDEP_NO_PATCH;
4668 /* If we're dealing with an inner class constructor, we hide the
4669 this$<n> decl in the name field of its parameter declaration. We
4670 also might have to hide the outer context local alias
4671 initializers. Not done when the class is a toplevel class. */
4672 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4673 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4675 tree aliases_list, type, thisn;
4676 /* First the aliases, linked to the regular parameters */
4677 aliases_list =
4678 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4679 TREE_TYPE (GET_CPC ()),
4680 NULL_TREE, NULL);
4681 list = chainon (nreverse (aliases_list), list);
4683 /* Then this$<n> */
4684 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4685 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4686 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4687 list);
4690 for (current = list; current; current = TREE_CHAIN (current))
4692 int must_chain = 0;
4693 tree wfl_name = TREE_PURPOSE (current);
4694 tree type = TREE_VALUE (current);
4695 tree name = EXPR_WFL_NODE (wfl_name);
4696 tree already, arg_node;
4697 tree type_wfl = NULL_TREE;
4698 tree real_type;
4700 /* Obtain a suitable type for resolution, if necessary */
4701 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4703 /* Process NAME, as it may specify extra dimension(s) for it */
4704 type = build_array_from_name (type, type_wfl, name, &name);
4705 EXPR_WFL_NODE (wfl_name) = name;
4707 real_type = GET_REAL_TYPE (type);
4708 if (TREE_CODE (real_type) == RECORD_TYPE)
4710 real_type = promote_type (real_type);
4711 if (TREE_CODE (type) == TREE_LIST)
4712 TREE_PURPOSE (type) = real_type;
4715 /* Check redefinition */
4716 for (already = arg_types; already; already = TREE_CHAIN (already))
4717 if (TREE_PURPOSE (already) == name)
4719 parse_error_context
4720 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4721 IDENTIFIER_POINTER (name),
4722 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4723 break;
4726 /* If we've an incomplete argument type, we know there is a location
4727 to patch when the type get resolved, later. */
4728 jdep = NULL;
4729 if (must_chain)
4731 patch_stage = JDEP_METHOD;
4732 type = register_incomplete_type (patch_stage,
4733 type_wfl, wfl_name, type);
4734 jdep = CLASSD_LAST (ctxp->classd_list);
4735 JDEP_MISC (jdep) = id;
4738 /* The argument node: a name and a (possibly) incomplete type. */
4739 arg_node = build_tree_list (name, real_type);
4740 /* Remeber arguments declared final. */
4741 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4743 if (jdep)
4744 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4745 TREE_CHAIN (arg_node) = arg_types;
4746 arg_types = arg_node;
4748 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4749 node = build_tree_list (id, meth);
4750 return node;
4753 static int
4754 unresolved_type_p (wfl, returned)
4755 tree wfl;
4756 tree *returned;
4759 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4761 if (returned)
4763 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4764 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4765 *returned = TREE_TYPE (decl);
4766 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4767 *returned = TREE_TYPE (GET_CPC ());
4768 else
4769 *returned = NULL_TREE;
4771 return 1;
4773 if (returned)
4774 *returned = wfl;
4775 return 0;
4778 /* From NAME, build a qualified identifier node using the
4779 qualification from the current package definition. */
4781 static tree
4782 parser_qualified_classname (name)
4783 tree name;
4785 tree nested_class_name;
4787 if ((nested_class_name = maybe_make_nested_class_name (name)))
4788 return nested_class_name;
4790 if (ctxp->package)
4791 return merge_qualified_name (ctxp->package, name);
4792 else
4793 return name;
4796 /* Called once the type a interface extends is resolved. Returns 0 if
4797 everything is OK. */
4799 static int
4800 parser_check_super_interface (super_decl, this_decl, this_wfl)
4801 tree super_decl, this_decl, this_wfl;
4803 tree super_type = TREE_TYPE (super_decl);
4805 /* Has to be an interface */
4806 if (!CLASS_INTERFACE (super_decl))
4808 parse_error_context
4809 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4810 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4811 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4812 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4813 "interface" : "class"),
4814 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4815 return 1;
4818 /* Check scope: same package OK, other package: OK if public */
4819 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4820 return 1;
4822 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4823 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4824 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4825 return 0;
4828 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4829 0 if everthing is OK. */
4831 static int
4832 parser_check_super (super_decl, this_decl, wfl)
4833 tree super_decl, this_decl, wfl;
4835 tree super_type = TREE_TYPE (super_decl);
4837 /* SUPER should be a CLASS (neither an array nor an interface) */
4838 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4840 parse_error_context
4841 (wfl, "Class `%s' can't subclass %s `%s'",
4842 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4843 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4844 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4845 return 1;
4848 if (CLASS_FINAL (TYPE_NAME (super_type)))
4850 parse_error_context (wfl, "Can't subclass final classes: %s",
4851 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4852 return 1;
4855 /* Check scope: same package OK, other package: OK if public */
4856 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4857 return 1;
4859 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4860 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4861 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4862 return 0;
4865 /* Create a new dependency list and link it (in a LIFO manner) to the
4866 CTXP list of type dependency list. */
4868 static void
4869 create_jdep_list (ctxp)
4870 struct parser_ctxt *ctxp;
4872 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
4873 new->first = new->last = NULL;
4874 new->next = ctxp->classd_list;
4875 ctxp->classd_list = new;
4878 static jdeplist *
4879 reverse_jdep_list (ctxp)
4880 struct parser_ctxt *ctxp;
4882 register jdeplist *prev = NULL, *current, *next;
4883 for (current = ctxp->classd_list; current; current = next)
4885 next = current->next;
4886 current->next = prev;
4887 prev = current;
4889 return prev;
4892 /* Create a fake pointer based on the ID stored in
4893 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4894 registered again. */
4896 static tree
4897 obtain_incomplete_type (type_name)
4898 tree type_name;
4900 tree ptr, name;
4902 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4903 name = EXPR_WFL_NODE (type_name);
4904 else if (INCOMPLETE_TYPE_P (type_name))
4905 name = TYPE_NAME (type_name);
4906 else
4907 fatal ("invalid type name - obtain_incomplete_type");
4909 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
4910 if (TYPE_NAME (ptr) == name)
4911 break;
4913 if (!ptr)
4915 push_obstacks (&permanent_obstack, &permanent_obstack);
4916 BUILD_PTR_FROM_NAME (ptr, name);
4917 layout_type (ptr);
4918 pop_obstacks ();
4919 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4920 ctxp->incomplete_class = ptr;
4923 return ptr;
4926 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4927 non NULL instead of computing a new fake type based on WFL. The new
4928 dependency is inserted in the current type dependency list, in FIFO
4929 manner. */
4931 static tree
4932 register_incomplete_type (kind, wfl, decl, ptr)
4933 int kind;
4934 tree wfl, decl, ptr;
4936 jdep *new = (jdep *)xmalloc (sizeof (jdep));
4938 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4939 ptr = obtain_incomplete_type (wfl);
4941 JDEP_KIND (new) = kind;
4942 JDEP_DECL (new) = decl;
4943 JDEP_SOLV (new) = ptr;
4944 JDEP_WFL (new) = wfl;
4945 JDEP_CHAIN (new) = NULL;
4946 JDEP_MISC (new) = NULL_TREE;
4947 /* For some dependencies, set the enclosing class of the current
4948 class to be the enclosing context */
4949 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
4950 && GET_ENCLOSING_CPC ())
4951 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
4952 else
4953 JDEP_ENCLOSING (new) = GET_CPC ();
4954 JDEP_GET_PATCH (new) = (tree *)NULL;
4956 JDEP_INSERT (ctxp->classd_list, new);
4958 return ptr;
4961 void
4962 java_check_circular_reference ()
4964 tree current;
4965 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
4967 tree type = TREE_TYPE (current);
4968 if (CLASS_INTERFACE (current))
4970 /* Check all interfaces this class extends */
4971 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
4972 int n, i;
4974 if (!basetype_vec)
4975 return;
4976 n = TREE_VEC_LENGTH (basetype_vec);
4977 for (i = 0; i < n; i++)
4979 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
4980 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
4981 && interface_of_p (type, BINFO_TYPE (vec_elt)))
4982 parse_error_context (lookup_cl (current),
4983 "Cyclic interface inheritance");
4986 else
4987 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
4988 parse_error_context (lookup_cl (current),
4989 "Cyclic class inheritance%s",
4990 (cyclic_inheritance_report ?
4991 cyclic_inheritance_report : ""));
4995 /* Augment the parameter list PARM with parameters crafted to
4996 initialize outer context locals aliases. Through ARTIFICIAL, a
4997 count is kept of the number of crafted parameters. MODE governs
4998 what eventually gets created: something suitable for a function
4999 creation or a function invocation, either the constructor or
5000 $finit$. */
5002 static tree
5003 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5004 int mode;
5005 tree class_type, parm;
5006 int *artificial;
5008 tree field;
5009 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5010 if (FIELD_LOCAL_ALIAS (field))
5012 char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5013 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5015 switch (mode)
5017 case AIPL_FUNCTION_DECLARATION:
5018 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5019 purpose = build_wfl_node (get_identifier (buffer));
5020 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5021 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5022 else
5023 value = TREE_TYPE (field);
5024 break;
5026 case AIPL_FUNCTION_CREATION:
5027 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5028 purpose = get_identifier (buffer);
5029 value = TREE_TYPE (field);
5030 break;
5032 case AIPL_FUNCTION_FINIT_INVOCATION:
5033 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5034 /* Now, this is wrong. purpose should always be the NAME
5035 of something and value its matching value (decl, type,
5036 etc...) FIXME -- but there is a lot to fix. */
5038 /* When invoked for this kind of operation, we already
5039 know whether a field is used or not. */
5040 purpose = TREE_TYPE (field);
5041 value = build_wfl_node (get_identifier (buffer));
5042 break;
5044 case AIPL_FUNCTION_CTOR_INVOCATION:
5045 /* There are two case: the constructor invokation happends
5046 outside the local inner, in which case, locales from the outer
5047 context are directly used.
5049 Otherwise, we fold to using the alias directly. */
5050 if (class_type == current_class)
5051 value = field;
5052 else
5054 name = get_identifier (&buffer[4]);
5055 value = IDENTIFIER_LOCAL_VALUE (name);
5057 break;
5059 parm = tree_cons (purpose, value, parm);
5060 if (artificial)
5061 *artificial +=1;
5063 return parm;
5066 /* Craft a constructor for CLASS_DECL -- what we should do when none
5067 where found. ARGS is non NULL when a special signature must be
5068 enforced. This is the case for anonymous classes. */
5070 static void
5071 craft_constructor (class_decl, args)
5072 tree class_decl, args;
5074 tree class_type = TREE_TYPE (class_decl);
5075 tree parm = NULL_TREE;
5076 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5077 ACC_PUBLIC : 0);
5078 int i = 0, artificial = 0;
5079 tree decl, ctor_name;
5080 char buffer [80];
5082 push_obstacks (&permanent_obstack, &permanent_obstack);
5084 /* The constructor name is <init> unless we're dealing with an
5085 anonymous class, in which case the name will be fixed after having
5086 be expanded. */
5087 if (ANONYMOUS_CLASS_P (class_type))
5088 ctor_name = DECL_NAME (class_decl);
5089 else
5090 ctor_name = init_identifier_node;
5092 /* If we're dealing with an inner class constructor, we hide the
5093 this$<n> decl in the name field of its parameter declaration. */
5094 if (PURE_INNER_CLASS_TYPE_P (class_type))
5096 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5097 parm = tree_cons (build_current_thisn (class_type),
5098 build_pointer_type (type), parm);
5100 /* Some more arguments to be hidden here. The values of the local
5101 variables of the outer context that the inner class needs to see. */
5102 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5103 class_type, parm,
5104 &artificial);
5107 /* Then if there are any args to be enforced, enforce them now */
5108 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5110 sprintf (buffer, "parm%d", i++);
5111 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5114 CRAFTED_PARAM_LIST_FIXUP (parm);
5115 decl = create_artificial_method (class_type, flags, void_type_node,
5116 ctor_name, parm);
5117 fix_method_argument_names (parm, decl);
5118 /* Now, mark the artificial parameters. */
5119 DECL_FUNCTION_NAP (decl) = artificial;
5121 pop_obstacks ();
5122 DECL_CONSTRUCTOR_P (decl) = 1;
5126 /* Fix the constructors. This will be called right after circular
5127 references have been checked. It is necessary to fix constructors
5128 early even if no code generation will take place for that class:
5129 some generated constructor might be required by the class whose
5130 compilation triggered this one to be simply loaded. */
5132 void
5133 java_fix_constructors ()
5135 tree current;
5137 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5139 tree class_type = TREE_TYPE (current);
5140 int saw_ctor = 0;
5141 tree decl;
5143 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5144 continue;
5146 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5148 if (DECL_CONSTRUCTOR_P (decl))
5150 fix_constructors (decl);
5151 saw_ctor = 1;
5155 /* Anonymous class constructor can't be generated that early. */
5156 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5157 craft_constructor (current, NULL_TREE);
5161 /* safe_layout_class just makes sure that we can load a class without
5162 disrupting the current_class, input_file, lineno, etc, information
5163 about the class processed currently. */
5165 void
5166 safe_layout_class (class)
5167 tree class;
5169 tree save_current_class = current_class;
5170 const char *save_input_filename = input_filename;
5171 int save_lineno = lineno;
5173 push_obstacks (&permanent_obstack, &permanent_obstack);
5175 layout_class (class);
5176 pop_obstacks ();
5178 current_class = save_current_class;
5179 input_filename = save_input_filename;
5180 lineno = save_lineno;
5181 CLASS_LOADED_P (class) = 1;
5184 static tree
5185 jdep_resolve_class (dep)
5186 jdep *dep;
5188 tree decl;
5190 if (JDEP_RESOLVED_P (dep))
5191 decl = JDEP_RESOLVED_DECL (dep);
5192 else
5194 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5195 JDEP_DECL (dep), JDEP_WFL (dep));
5196 JDEP_RESOLVED (dep, decl);
5199 if (!decl)
5200 complete_class_report_errors (dep);
5202 if (PURE_INNER_CLASS_DECL_P (decl))
5203 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5204 return decl;
5207 /* Complete unsatisfied class declaration and their dependencies */
5209 void
5210 java_complete_class ()
5212 tree cclass;
5213 jdeplist *cclassd;
5214 int error_found;
5215 tree type;
5217 push_obstacks (&permanent_obstack, &permanent_obstack);
5219 /* Process imports */
5220 process_imports ();
5222 /* Rever things so we have the right order */
5223 ctxp->class_list = nreverse (ctxp->class_list);
5224 ctxp->classd_list = reverse_jdep_list (ctxp);
5226 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5227 cclass && cclassd;
5228 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5230 jdep *dep;
5231 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5233 tree decl;
5234 if (!(decl = jdep_resolve_class (dep)))
5235 continue;
5237 /* Now it's time to patch */
5238 switch (JDEP_KIND (dep))
5240 case JDEP_SUPER:
5241 /* Simply patch super */
5242 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5243 continue;
5244 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5245 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5246 break;
5248 case JDEP_FIELD:
5250 /* We do part of the job done in add_field */
5251 tree field_decl = JDEP_DECL (dep);
5252 tree field_type = TREE_TYPE (decl);
5253 push_obstacks (&permanent_obstack, &permanent_obstack);
5254 if (TREE_CODE (field_type) == RECORD_TYPE)
5255 field_type = promote_type (field_type);
5256 pop_obstacks ();
5257 TREE_TYPE (field_decl) = field_type;
5258 DECL_ALIGN (field_decl) = 0;
5259 DECL_USER_ALIGN (field_decl) = 0;
5260 layout_decl (field_decl, 0);
5261 SOURCE_FRONTEND_DEBUG
5262 (("Completed field/var decl `%s' with `%s'",
5263 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5264 IDENTIFIER_POINTER (DECL_NAME (decl))));
5265 break;
5267 case JDEP_METHOD: /* We start patching a method */
5268 case JDEP_METHOD_RETURN:
5269 error_found = 0;
5270 while (1)
5272 if (decl)
5274 type = TREE_TYPE(decl);
5275 if (TREE_CODE (type) == RECORD_TYPE)
5276 type = promote_type (type);
5277 JDEP_APPLY_PATCH (dep, type);
5278 SOURCE_FRONTEND_DEBUG
5279 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5280 "Completing fct `%s' with ret type `%s'":
5281 "Completing arg `%s' with type `%s'"),
5282 IDENTIFIER_POINTER (EXPR_WFL_NODE
5283 (JDEP_DECL_WFL (dep))),
5284 IDENTIFIER_POINTER (DECL_NAME (decl))));
5286 else
5287 error_found = 1;
5288 dep = JDEP_CHAIN (dep);
5289 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5290 break;
5291 else
5292 decl = jdep_resolve_class (dep);
5294 if (!error_found)
5296 tree mdecl = JDEP_DECL (dep), signature;
5297 push_obstacks (&permanent_obstack, &permanent_obstack);
5298 /* Recompute and reset the signature, check first that
5299 all types are now defined. If they're not,
5300 dont build the signature. */
5301 if (check_method_types_complete (mdecl))
5303 signature = build_java_signature (TREE_TYPE (mdecl));
5304 set_java_signature (TREE_TYPE (mdecl), signature);
5306 pop_obstacks ();
5308 else
5309 continue;
5310 break;
5312 case JDEP_INTERFACE:
5313 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5314 JDEP_WFL (dep)))
5315 continue;
5316 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5317 break;
5319 case JDEP_PARM:
5320 case JDEP_VARIABLE:
5321 type = TREE_TYPE(decl);
5322 if (TREE_CODE (type) == RECORD_TYPE)
5323 type = promote_type (type);
5324 JDEP_APPLY_PATCH (dep, type);
5325 break;
5327 case JDEP_TYPE:
5328 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5329 SOURCE_FRONTEND_DEBUG
5330 (("Completing a random type dependency on a '%s' node",
5331 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5332 break;
5334 case JDEP_EXCEPTION:
5335 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5336 SOURCE_FRONTEND_DEBUG
5337 (("Completing `%s' `throws' argument node",
5338 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5339 break;
5341 case JDEP_ANONYMOUS:
5342 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5343 break;
5345 default:
5346 fatal ("Can't handle patch code %d - java_complete_class",
5347 JDEP_KIND (dep));
5351 pop_obstacks ();
5352 return;
5355 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5356 array. */
5358 static tree
5359 resolve_class (enclosing, class_type, decl, cl)
5360 tree enclosing, class_type, decl, cl;
5362 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5363 const char *base = name;
5364 tree resolved_type = TREE_TYPE (class_type);
5365 tree resolved_type_decl;
5367 if (resolved_type != NULL_TREE)
5369 tree resolved_type_decl = TYPE_NAME (resolved_type);
5370 if (resolved_type_decl == NULL_TREE
5371 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5373 resolved_type_decl = build_decl (TYPE_DECL,
5374 TYPE_NAME (class_type),
5375 resolved_type);
5377 return resolved_type_decl;
5380 /* 1- Check to see if we have an array. If true, find what we really
5381 want to resolve */
5382 while (name[0] == '[')
5383 name++;
5384 if (base != name)
5385 TYPE_NAME (class_type) = get_identifier (name);
5387 /* 2- Resolve the bare type */
5388 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5389 decl, cl)))
5390 return NULL_TREE;
5391 resolved_type = TREE_TYPE (resolved_type_decl);
5393 /* 3- If we have and array, reconstruct the array down to its nesting */
5394 if (base != name)
5396 while (base != name)
5398 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5399 resolved_type = promote_type (resolved_type);
5400 resolved_type = build_java_array_type (resolved_type, -1);
5401 CLASS_LOADED_P (resolved_type) = 1;
5402 name--;
5404 /* Build a fake decl for this, since this is what is expected to
5405 be returned. */
5406 resolved_type_decl =
5407 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5408 /* Figure how those two things are important for error report. FIXME */
5409 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5410 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
5411 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
5413 TREE_TYPE (class_type) = resolved_type;
5414 return resolved_type_decl;
5417 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5418 are used to report error messages. */
5420 tree
5421 do_resolve_class (enclosing, class_type, decl, cl)
5422 tree enclosing, class_type, decl, cl;
5424 tree new_class_decl;
5426 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5427 it is changed by find_in_imports{_on_demand} and (but it doesn't
5428 really matter) qualify_and_find */
5430 /* 0- Search in the current class as an inner class */
5432 /* Maybe some code here should be added to load the class or
5433 something, at least if the class isn't an inner class and ended
5434 being loaded from class file. FIXME. */
5435 while (enclosing)
5437 tree name;
5439 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5440 return new_class_decl;
5442 /* Explore enclosing contexts. */
5443 while (INNER_CLASS_DECL_P (enclosing))
5445 enclosing = DECL_CONTEXT (enclosing);
5446 if ((new_class_decl = find_as_inner_class (enclosing,
5447 class_type, cl)))
5448 return new_class_decl;
5451 /* Now go to the upper classes, bail out if necessary. */
5452 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5453 if (!enclosing || enclosing == object_type_node)
5454 break;
5456 if (TREE_CODE (enclosing) == RECORD_TYPE)
5458 enclosing = TYPE_NAME (enclosing);
5459 continue;
5462 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
5463 BUILD_PTR_FROM_NAME (name, enclosing);
5464 else
5465 name = enclosing;
5466 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5469 /* 1- Check for the type in single imports. This will change
5470 TYPE_NAME() if something relevant is found */
5471 find_in_imports (class_type);
5473 /* 2- And check for the type in the current compilation unit */
5474 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5476 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5477 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5478 load_class (TYPE_NAME (class_type), 0);
5479 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5482 /* 3- Search according to the current package definition */
5483 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5485 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5486 TYPE_NAME (class_type))))
5487 return new_class_decl;
5490 /* 4- Check the import on demands. Don't allow bar.baz to be
5491 imported from foo.* */
5492 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5493 if (find_in_imports_on_demand (class_type))
5494 return NULL_TREE;
5496 /* If found in find_in_imports_on_demant, the type has already been
5497 loaded. */
5498 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5499 return new_class_decl;
5501 /* 5- Try with a name qualified with the package name we've seen so far */
5502 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5504 tree package;
5506 /* If there is a current package (ctxp->package), it's the first
5507 element of package_list and we can skip it. */
5508 for (package = (ctxp->package ?
5509 TREE_CHAIN (package_list) : package_list);
5510 package; package = TREE_CHAIN (package))
5511 if ((new_class_decl = qualify_and_find (class_type,
5512 TREE_PURPOSE (package),
5513 TYPE_NAME (class_type))))
5514 return new_class_decl;
5517 /* 5- Check an other compilation unit that bears the name of type */
5518 load_class (TYPE_NAME (class_type), 0);
5519 if (check_pkg_class_access (TYPE_NAME (class_type),
5520 (cl ? cl : lookup_cl (decl))))
5521 return NULL_TREE;
5523 /* 6- Last call for a resolution */
5524 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5527 static tree
5528 qualify_and_find (class_type, package, name)
5529 tree class_type, package, name;
5531 tree new_qualified = merge_qualified_name (package, name);
5532 tree new_class_decl;
5534 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5535 load_class (new_qualified, 0);
5536 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5538 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5539 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5540 load_class (new_qualified, 0);
5541 TYPE_NAME (class_type) = new_qualified;
5542 return IDENTIFIER_CLASS_VALUE (new_qualified);
5544 return NULL_TREE;
5547 /* Resolve NAME and lay it out (if not done and if not the current
5548 parsed class). Return a decl node. This function is meant to be
5549 called when type resolution is necessary during the walk pass. */
5551 static tree
5552 resolve_and_layout (something, cl)
5553 tree something;
5554 tree cl;
5556 tree decl;
5558 /* Don't do that on the current class */
5559 if (something == current_class)
5560 return TYPE_NAME (current_class);
5562 /* Don't do anything for void and other primitive types */
5563 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5564 return NULL_TREE;
5566 /* Pointer types can be reall pointer types or fake pointers. When
5567 finding a real pointer, recheck for primitive types */
5568 if (TREE_CODE (something) == POINTER_TYPE)
5570 if (TREE_TYPE (something))
5572 something = TREE_TYPE (something);
5573 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5574 return NULL_TREE;
5576 else
5577 something = TYPE_NAME (something);
5580 /* Don't do anything for arrays of primitive types */
5581 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5582 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5583 return NULL_TREE;
5585 /* Something might be a WFL */
5586 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5587 something = EXPR_WFL_NODE (something);
5589 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5590 TYPE_DECL or a real TYPE */
5591 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5592 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5593 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5595 if (!(decl = resolve_no_layout (something, cl)))
5596 return NULL_TREE;
5598 /* Resolve and layout if necessary */
5599 layout_class_methods (TREE_TYPE (decl));
5600 /* Check methods, but only once */
5601 if (CLASS_FROM_SOURCE_P (TREE_TYPE (decl))
5602 && !CLASS_LOADED_P (TREE_TYPE (decl)))
5603 CHECK_METHODS (decl);
5604 if (TREE_TYPE (decl) != current_class && !CLASS_LOADED_P (TREE_TYPE (decl)))
5605 safe_layout_class (TREE_TYPE (decl));
5607 return decl;
5610 /* Resolve a class, returns its decl but doesn't perform any
5611 layout. The current parsing context is saved and restored */
5613 static tree
5614 resolve_no_layout (name, cl)
5615 tree name, cl;
5617 tree ptr, decl;
5618 BUILD_PTR_FROM_NAME (ptr, name);
5619 java_parser_context_save_global ();
5620 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5621 java_parser_context_restore_global ();
5623 return decl;
5626 /* Called when reporting errors. Skip leader '[' in a complex array
5627 type description that failed to be resolved. */
5629 static const char *
5630 purify_type_name (name)
5631 const char *name;
5633 while (*name && *name == '[')
5634 name++;
5635 return name;
5638 /* The type CURRENT refers to can't be found. We print error messages. */
5640 static void
5641 complete_class_report_errors (dep)
5642 jdep *dep;
5644 const char *name;
5646 if (!JDEP_WFL (dep))
5647 return;
5649 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5650 switch (JDEP_KIND (dep))
5652 case JDEP_SUPER:
5653 parse_error_context
5654 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5655 purify_type_name (name),
5656 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5657 break;
5658 case JDEP_FIELD:
5659 parse_error_context
5660 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5661 purify_type_name (name),
5662 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5663 break;
5664 case JDEP_METHOD: /* Covers arguments */
5665 parse_error_context
5666 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5667 purify_type_name (name),
5668 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5669 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5670 break;
5671 case JDEP_METHOD_RETURN: /* Covers return type */
5672 parse_error_context
5673 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5674 purify_type_name (name),
5675 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5676 break;
5677 case JDEP_INTERFACE:
5678 parse_error_context
5679 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5680 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5681 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5682 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5683 break;
5684 case JDEP_VARIABLE:
5685 parse_error_context
5686 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5687 purify_type_name (IDENTIFIER_POINTER
5688 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5689 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5690 break;
5691 case JDEP_EXCEPTION: /* As specified by `throws' */
5692 parse_error_context
5693 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5694 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5695 break;
5696 default:
5697 /* Fix for -Wall. Just break doing nothing. The error will be
5698 caught later */
5699 break;
5703 /* Return a static string containing the DECL prototype string. If
5704 DECL is a constructor, use the class name instead of the form
5705 <init> */
5707 static const char *
5708 get_printable_method_name (decl)
5709 tree decl;
5711 const char *to_return;
5712 tree name = NULL_TREE;
5714 if (DECL_CONSTRUCTOR_P (decl))
5716 name = DECL_NAME (decl);
5717 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
5720 to_return = lang_printable_name (decl, 0);
5721 if (DECL_CONSTRUCTOR_P (decl))
5722 DECL_NAME (decl) = name;
5724 return to_return;
5727 /* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5728 nevertheless needs to be verfied, 1 otherwise. */
5730 static int
5731 reset_method_name (method)
5732 tree method;
5734 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5736 /* NAME is just the plain name when Object is being defined */
5737 if (DECL_CONTEXT (method) != object_type_node)
5738 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5739 init_identifier_node : GET_METHOD_NAME (method));
5740 return 0;
5742 else
5743 return 1;
5746 /* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5748 tree
5749 java_get_real_method_name (method_decl)
5750 tree method_decl;
5752 tree method_name = DECL_NAME (method_decl);
5753 if (DECL_CONSTRUCTOR_P (method_decl))
5754 return init_identifier_node;
5756 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5757 and still can be a constructor. FIXME */
5759 /* Don't confuse method only bearing the name of their class as
5760 constructors */
5761 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5762 && ctxp
5763 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
5764 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5765 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
5766 return init_identifier_node;
5767 else
5768 return EXPR_WFL_NODE (method_name);
5771 /* Track method being redefined inside the same class. As a side
5772 effect, set DECL_NAME to an IDENTIFIER (prior entering this
5773 function it's a FWL, so we can track errors more accurately.) */
5775 static int
5776 check_method_redefinition (class, method)
5777 tree class, method;
5779 tree redef, name;
5780 tree cl = DECL_NAME (method);
5781 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
5782 /* decl name of artificial <clinit> and $finit$ doesn't need to be
5783 fixed and checked */
5785 /* Reset the method name before running the check. If it returns 1,
5786 the method doesn't need to be verified with respect to method
5787 redeclaration and we return 0 */
5788 if (reset_method_name (method))
5789 return 0;
5791 name = DECL_NAME (method);
5792 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5794 if (redef == method)
5795 break;
5796 if (DECL_NAME (redef) == name
5797 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
5799 parse_error_context
5800 (cl, "Duplicate %s declaration `%s'",
5801 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5802 get_printable_method_name (redef));
5803 return 1;
5806 return 0;
5809 static void
5810 check_abstract_method_definitions (do_interface, class_decl, type)
5811 int do_interface;
5812 tree class_decl, type;
5814 tree class = TREE_TYPE (class_decl);
5815 tree method, end_type;
5817 end_type = (do_interface ? object_type_node : type);
5818 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5820 tree other_super, other_method, method_sig, method_name;
5821 int found = 0;
5822 int end_type_reached = 0;
5824 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5825 continue;
5827 /* Now verify that somewhere in between TYPE and CLASS,
5828 abstract method METHOD gets a non abstract definition
5829 that is inherited by CLASS. */
5831 method_sig = build_java_signature (TREE_TYPE (method));
5832 method_name = DECL_NAME (method);
5833 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5834 method_name = EXPR_WFL_NODE (method_name);
5836 other_super = class;
5837 do {
5838 if (other_super == end_type)
5839 end_type_reached = 1;
5841 /* Method search */
5842 for (other_method = TYPE_METHODS (other_super); other_method;
5843 other_method = TREE_CHAIN (other_method))
5845 tree s = build_java_signature (TREE_TYPE (other_method));
5846 tree other_name = DECL_NAME (other_method);
5848 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5849 other_name = EXPR_WFL_NODE (other_name);
5850 if (!DECL_CLINIT_P (other_method)
5851 && !DECL_CONSTRUCTOR_P (other_method)
5852 && method_name == other_name
5853 && method_sig == s
5854 && !METHOD_ABSTRACT (other_method))
5856 found = 1;
5857 break;
5860 other_super = CLASSTYPE_SUPER (other_super);
5861 } while (!end_type_reached);
5863 /* Report that abstract METHOD didn't find an implementation
5864 that CLASS can use. */
5865 if (!found)
5867 char *t = xstrdup (lang_printable_name
5868 (TREE_TYPE (TREE_TYPE (method)), 0));
5869 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5870 tree saved_wfl = NULL_TREE;
5872 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5874 saved_wfl = DECL_NAME (method);
5875 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5878 parse_error_context
5879 (lookup_cl (class_decl),
5880 "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",
5881 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5882 t, lang_printable_name (method, 0),
5883 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5884 "interface" : "class"),
5885 IDENTIFIER_POINTER (ccn),
5886 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5887 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5889 free (t);
5891 if (saved_wfl)
5892 DECL_NAME (method) = saved_wfl;
5897 /* Check that CLASS_DECL somehow implements all inherited abstract
5898 methods. */
5900 static void
5901 java_check_abstract_method_definitions (class_decl)
5902 tree class_decl;
5904 tree class = TREE_TYPE (class_decl);
5905 tree super, vector;
5906 int i;
5908 if (CLASS_ABSTRACT (class_decl))
5909 return;
5911 /* Check for inherited types */
5912 super = class;
5913 do {
5914 super = CLASSTYPE_SUPER (super);
5915 check_abstract_method_definitions (0, class_decl, super);
5916 } while (super != object_type_node);
5918 /* Check for implemented interfaces. */
5919 vector = TYPE_BINFO_BASETYPES (class);
5920 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5922 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5923 check_abstract_method_definitions (1, class_decl, super);
5927 /* Check all the types method DECL uses and return 1 if all of them
5928 are now complete, 0 otherwise. This is used to check whether its
5929 safe to build a method signature or not. */
5931 static int
5932 check_method_types_complete (decl)
5933 tree decl;
5935 tree type = TREE_TYPE (decl);
5936 tree args;
5938 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5939 return 0;
5941 args = TYPE_ARG_TYPES (type);
5942 if (TREE_CODE (type) == METHOD_TYPE)
5943 args = TREE_CHAIN (args);
5944 for (; args != end_params_node; args = TREE_CHAIN (args))
5945 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5946 return 0;
5948 return 1;
5951 /* Check all the methods of CLASS_DECL. Methods are first completed
5952 then checked according to regular method existance rules. If no
5953 constructor for CLASS_DECL were encountered, then build its
5954 declaration. */
5956 static void
5957 java_check_regular_methods (class_decl)
5958 tree class_decl;
5960 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
5961 tree method;
5962 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5963 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
5964 tree mthrows;
5966 /* It is not necessary to check methods defined in java.lang.Object */
5967 if (class == object_type_node)
5968 return;
5970 if (!TYPE_NVIRTUALS (class))
5971 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
5973 /* Should take interfaces into account. FIXME */
5974 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
5976 tree sig;
5977 tree method_wfl = DECL_NAME (method);
5978 int aflags;
5980 /* If we previously found something and its name was saved,
5981 reinstall it now */
5982 if (found && saved_found_wfl)
5984 DECL_NAME (found) = saved_found_wfl;
5985 saved_found_wfl = NULL_TREE;
5988 /* Check for redefinitions */
5989 if (check_method_redefinition (class, method))
5990 continue;
5992 /* If we see one constructor a mark so we don't generate the
5993 default one. Also skip other verifications: constructors
5994 can't be inherited hence hiden or overriden */
5995 if (DECL_CONSTRUCTOR_P (method))
5997 saw_constructor = 1;
5998 continue;
6001 /* We verify things thrown by the method. They must inherits from
6002 java.lang.Throwable */
6003 for (mthrows = DECL_FUNCTION_THROWS (method);
6004 mthrows; mthrows = TREE_CHAIN (mthrows))
6006 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6007 parse_error_context
6008 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6009 IDENTIFIER_POINTER
6010 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6013 sig = build_java_argument_signature (TREE_TYPE (method));
6014 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6016 /* Inner class can't declare static methods */
6017 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6019 char *t = xstrdup (lang_printable_name (class, 0));
6020 parse_error_context
6021 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6022 lang_printable_name (method, 0), t);
6023 free (t);
6026 /* Nothing overrides or it's a private method. */
6027 if (!found)
6028 continue;
6029 if (METHOD_PRIVATE (found))
6031 found = NULL_TREE;
6032 continue;
6035 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6036 We set it temporarily for the sake of the error report. */
6037 saved_found_wfl = DECL_NAME (found);
6038 reset_method_name (found);
6040 /* If `found' is declared in an interface, make sure the
6041 modifier matches. */
6042 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6043 && clinit_identifier_node != DECL_NAME (found)
6044 && !METHOD_PUBLIC (method))
6046 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6047 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6048 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6049 lang_printable_name (method, 0),
6050 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6053 /* Can't override a method with the same name and different return
6054 types. */
6055 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6057 char *t = xstrdup
6058 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6059 parse_error_context
6060 (method_wfl,
6061 "Method `%s' was defined with return type `%s' in class `%s'",
6062 lang_printable_name (found, 0), t,
6063 IDENTIFIER_POINTER
6064 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6065 free (t);
6068 aflags = get_access_flags_from_decl (found);
6069 /* If the method has default, access in an other package, then
6070 issue a warning that the current method doesn't override the
6071 one that was found elsewhere. Do not issue this warning when
6072 the match was found in java.lang.Object. */
6073 if (DECL_CONTEXT (found) != object_type_node
6074 && ((aflags & ACC_VISIBILITY) == 0)
6075 && !class_in_current_package (DECL_CONTEXT (found))
6076 && !DECL_CLINIT_P (found)
6077 && flag_not_overriding)
6079 parse_warning_context
6080 (method_wfl, "Method `%s' in class `%s' does not override the corresponding method in class `%s', which is private to a different package",
6081 lang_printable_name (found, 0),
6082 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6083 IDENTIFIER_POINTER (DECL_NAME
6084 (TYPE_NAME (DECL_CONTEXT (found)))));
6085 continue;
6088 /* Can't override final. Can't override static. */
6089 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6091 /* Static *can* override static */
6092 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6093 continue;
6094 parse_error_context
6095 (method_wfl,
6096 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6097 (METHOD_FINAL (found) ? "Final" : "Static"),
6098 lang_printable_name (found, 0),
6099 (METHOD_FINAL (found) ? "final" : "static"),
6100 IDENTIFIER_POINTER
6101 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6102 continue;
6105 /* Static method can't override instance method. */
6106 if (METHOD_STATIC (method))
6108 parse_error_context
6109 (method_wfl,
6110 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6111 lang_printable_name (found, 0),
6112 IDENTIFIER_POINTER
6113 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6114 continue;
6117 /* - Overriding/hiding public must be public
6118 - Overriding/hiding protected must be protected or public
6119 - If the overriden or hidden method has default (package)
6120 access, then the overriding or hiding method must not be
6121 private; otherwise, a compile-time error occurs. If
6122 `found' belongs to an interface, things have been already
6123 taken care of. */
6124 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6125 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6126 || (METHOD_PROTECTED (found)
6127 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6128 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6129 && METHOD_PRIVATE (method))))
6131 parse_error_context
6132 (method_wfl,
6133 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6134 (METHOD_PUBLIC (method) ? "public" :
6135 (METHOD_PRIVATE (method) ? "private" : "protected")),
6136 IDENTIFIER_POINTER (DECL_NAME
6137 (TYPE_NAME (DECL_CONTEXT (found)))));
6138 continue;
6141 /* Overriding methods must have compatible `throws' clauses on checked
6142 exceptions, if any */
6143 check_throws_clauses (method, method_wfl, found);
6145 /* Inheriting multiple methods with the same signature. FIXME */
6148 /* Don't forget eventual pending found and saved_found_wfl. Take
6149 into account that we might have exited because we saw an
6150 artificial method as the last entry. */
6152 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6153 DECL_NAME (found) = saved_found_wfl;
6155 if (!TYPE_NVIRTUALS (class))
6156 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6158 /* Search for inherited abstract method not yet implemented in this
6159 class. */
6160 java_check_abstract_method_definitions (class_decl);
6162 if (!saw_constructor)
6163 fatal ("No constructor found");
6166 /* Return a non zero value if the `throws' clause of METHOD (if any)
6167 is incompatible with the `throws' clause of FOUND (if any). */
6169 static void
6170 check_throws_clauses (method, method_wfl, found)
6171 tree method, method_wfl, found;
6173 tree mthrows, fthrows;
6175 /* Can't check these things with class loaded from bytecode. FIXME */
6176 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6177 return;
6179 for (mthrows = DECL_FUNCTION_THROWS (method);
6180 mthrows; mthrows = TREE_CHAIN (mthrows))
6182 /* We don't verify unchecked expressions */
6183 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6184 continue;
6185 /* Checked expression must be compatible */
6186 for (fthrows = DECL_FUNCTION_THROWS (found);
6187 fthrows; fthrows = TREE_CHAIN (fthrows))
6188 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6189 break;
6190 if (!fthrows)
6192 parse_error_context
6193 (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'",
6194 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6195 lang_printable_name (found, 0),
6196 IDENTIFIER_POINTER
6197 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6202 /* Check abstract method of interface INTERFACE */
6204 static void
6205 java_check_abstract_methods (interface_decl)
6206 tree interface_decl;
6208 int i, n;
6209 tree method, basetype_vec, found;
6210 tree interface = TREE_TYPE (interface_decl);
6212 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6214 tree method_wfl = DECL_NAME (method);
6216 /* 2- Check for double definition inside the defining interface */
6217 if (check_method_redefinition (interface, method))
6218 continue;
6220 /* 3- Overriding is OK as far as we preserve the return type and
6221 the thrown exceptions (FIXME) */
6222 found = lookup_java_interface_method2 (interface, method);
6223 if (found)
6225 char *t;
6226 tree saved_found_wfl = DECL_NAME (found);
6227 reset_method_name (found);
6228 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6229 parse_error_context
6230 (method_wfl,
6231 "Method `%s' was defined with return type `%s' in class `%s'",
6232 lang_printable_name (found, 0), t,
6233 IDENTIFIER_POINTER
6234 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6235 free (t);
6236 DECL_NAME (found) = saved_found_wfl;
6237 continue;
6241 /* 4- Inherited methods can't differ by their returned types */
6242 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6243 return;
6244 n = TREE_VEC_LENGTH (basetype_vec);
6245 for (i = 0; i < n; i++)
6247 tree sub_interface_method, sub_interface;
6248 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6249 if (!vec_elt)
6250 continue;
6251 sub_interface = BINFO_TYPE (vec_elt);
6252 for (sub_interface_method = TYPE_METHODS (sub_interface);
6253 sub_interface_method;
6254 sub_interface_method = TREE_CHAIN (sub_interface_method))
6256 found = lookup_java_interface_method2 (interface,
6257 sub_interface_method);
6258 if (found && (found != sub_interface_method))
6260 tree saved_found_wfl = DECL_NAME (found);
6261 reset_method_name (found);
6262 parse_error_context
6263 (lookup_cl (sub_interface_method),
6264 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6265 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6266 lang_printable_name (found, 0),
6267 IDENTIFIER_POINTER
6268 (DECL_NAME (TYPE_NAME
6269 (DECL_CONTEXT (sub_interface_method)))),
6270 IDENTIFIER_POINTER
6271 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6272 DECL_NAME (found) = saved_found_wfl;
6278 /* Lookup methods in interfaces using their name and partial
6279 signature. Return a matching method only if their types differ. */
6281 static tree
6282 lookup_java_interface_method2 (class, method_decl)
6283 tree class, method_decl;
6285 int i, n;
6286 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6288 if (!basetype_vec)
6289 return NULL_TREE;
6291 n = TREE_VEC_LENGTH (basetype_vec);
6292 for (i = 0; i < n; i++)
6294 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6295 if ((BINFO_TYPE (vec_elt) != object_type_node)
6296 && (to_return =
6297 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6298 return to_return;
6300 for (i = 0; i < n; i++)
6302 to_return = lookup_java_interface_method2
6303 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6304 if (to_return)
6305 return to_return;
6308 return NULL_TREE;
6311 /* Lookup method using their name and partial signature. Return a
6312 matching method only if their types differ. */
6314 static tree
6315 lookup_java_method2 (clas, method_decl, do_interface)
6316 tree clas, method_decl;
6317 int do_interface;
6319 tree method, method_signature, method_name, method_type, name;
6321 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6322 name = DECL_NAME (method_decl);
6323 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6324 EXPR_WFL_NODE (name) : name);
6325 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6327 while (clas != NULL_TREE)
6329 for (method = TYPE_METHODS (clas);
6330 method != NULL_TREE; method = TREE_CHAIN (method))
6332 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6333 tree name = DECL_NAME (method);
6334 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6335 EXPR_WFL_NODE (name) : name) == method_name
6336 && method_sig == method_signature
6337 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6338 return method;
6340 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6342 return NULL_TREE;
6345 /* Return the line that matches DECL line number, and try its best to
6346 position the column number. Used during error reports. */
6348 static tree
6349 lookup_cl (decl)
6350 tree decl;
6352 static tree cl = NULL_TREE;
6353 char *line, *found;
6355 if (!decl)
6356 return NULL_TREE;
6358 if (cl == NULL_TREE)
6359 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6361 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6362 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6364 line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)),
6365 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6367 found = strstr ((const char *)line,
6368 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6369 if (found)
6370 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6372 return cl;
6375 /* Look for a simple name in the single-type import list */
6377 static tree
6378 find_name_in_single_imports (name)
6379 tree name;
6381 tree node;
6383 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6384 if (TREE_VALUE (node) == name)
6385 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6387 return NULL_TREE;
6390 /* Process all single-type import. */
6392 static int
6393 process_imports ()
6395 tree import;
6396 int error_found;
6398 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6400 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6402 /* Don't load twice something already defined. */
6403 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6404 continue;
6405 QUALIFIED_P (to_be_found) = 1;
6406 load_class (to_be_found, 0);
6407 error_found =
6408 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6409 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6411 parse_error_context (TREE_PURPOSE (import),
6412 "Class or interface `%s' not found in import",
6413 IDENTIFIER_POINTER (to_be_found));
6414 return 1;
6416 if (error_found)
6417 return 1;
6419 return 0;
6422 /* Possibly find and mark a class imported by a single-type import
6423 statement. */
6425 static void
6426 find_in_imports (class_type)
6427 tree class_type;
6429 tree import;
6431 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6432 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6434 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6435 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6439 static int
6440 note_possible_classname (name, len)
6441 const char *name;
6442 int len;
6444 tree node;
6445 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6446 len = len - 5;
6447 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6448 len = len - 6;
6449 else
6450 return 0;
6451 node = ident_subst (name, len, "", '/', '.', "");
6452 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6453 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6454 return 1;
6457 /* Read a import directory, gathering potential match for further type
6458 references. Indifferently reads a filesystem or a ZIP archive
6459 directory. */
6461 static void
6462 read_import_dir (wfl)
6463 tree wfl;
6465 tree package_id = EXPR_WFL_NODE (wfl);
6466 const char *package_name = IDENTIFIER_POINTER (package_id);
6467 int package_length = IDENTIFIER_LENGTH (package_id);
6468 DIR *dirp = NULL;
6469 JCF *saved_jcf = current_jcf;
6471 int found = 0;
6472 int k;
6473 void *entry;
6474 struct buffer filename[1];
6477 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6478 return;
6479 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6481 BUFFER_INIT (filename);
6482 buffer_grow (filename, package_length + 100);
6484 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6486 const char *entry_name = jcf_path_name (entry);
6487 int entry_length = strlen (entry_name);
6488 if (jcf_path_is_zipfile (entry))
6490 ZipFile *zipf;
6491 buffer_grow (filename, entry_length);
6492 memcpy (filename->data, entry_name, entry_length - 1);
6493 filename->data[entry_length-1] = '\0';
6494 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6495 if (zipf == NULL)
6496 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6497 else
6499 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6500 BUFFER_RESET (filename);
6501 for (k = 0; k < package_length; k++)
6503 char ch = package_name[k];
6504 *filename->ptr++ = ch == '.' ? '/' : ch;
6506 *filename->ptr++ = '/';
6508 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6510 const char *current_entry = ZIPDIR_FILENAME (zipd);
6511 int current_entry_len = zipd->filename_length;
6513 if (current_entry_len >= BUFFER_LENGTH (filename)
6514 && strncmp (filename->data, current_entry,
6515 BUFFER_LENGTH (filename)) != 0)
6516 continue;
6517 found |= note_possible_classname (current_entry,
6518 current_entry_len);
6522 else
6524 BUFFER_RESET (filename);
6525 buffer_grow (filename, entry_length + package_length + 4);
6526 strcpy (filename->data, entry_name);
6527 filename->ptr = filename->data + entry_length;
6528 for (k = 0; k < package_length; k++)
6530 char ch = package_name[k];
6531 *filename->ptr++ = ch == '.' ? '/' : ch;
6533 *filename->ptr = '\0';
6535 dirp = opendir (filename->data);
6536 if (dirp == NULL)
6537 continue;
6538 *filename->ptr++ = '/';
6539 for (;;)
6541 int len;
6542 const char *d_name;
6543 struct dirent *direntp = readdir (dirp);
6544 if (!direntp)
6545 break;
6546 d_name = direntp->d_name;
6547 len = strlen (direntp->d_name);
6548 buffer_grow (filename, len+1);
6549 strcpy (filename->ptr, d_name);
6550 found |= note_possible_classname (filename->data + entry_length,
6551 package_length+len+1);
6553 if (dirp)
6554 closedir (dirp);
6558 free (filename->data);
6560 /* Here we should have a unified way of retrieving an entry, to be
6561 indexed. */
6562 if (!found)
6564 static int first = 1;
6565 if (first)
6567 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6568 java_error_count++;
6569 first = 0;
6571 else
6572 parse_error_context (wfl, "Package `%s' not found in import",
6573 package_name);
6574 current_jcf = saved_jcf;
6575 return;
6577 current_jcf = saved_jcf;
6580 /* Possibly find a type in the import on demands specified
6581 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6582 entire list, to detected potential double definitions. */
6584 static int
6585 find_in_imports_on_demand (class_type)
6586 tree class_type;
6588 tree node, import, node_to_use = NULL_TREE;
6589 int seen_once = -1;
6590 tree cl = NULL_TREE;
6592 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6594 const char *id_name;
6595 obstack_grow (&temporary_obstack,
6596 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6597 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6598 obstack_1grow (&temporary_obstack, '.');
6599 obstack_grow0 (&temporary_obstack,
6600 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6601 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6602 id_name = obstack_finish (&temporary_obstack);
6604 node = maybe_get_identifier (id_name);
6605 if (node && IS_A_CLASSFILE_NAME (node))
6607 if (seen_once < 0)
6609 cl = TREE_PURPOSE (import);
6610 seen_once = 1;
6611 node_to_use = node;
6613 else
6615 seen_once++;
6616 parse_error_context
6617 (import, "Type `%s' also potentially defined in package `%s'",
6618 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6619 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6624 if (seen_once == 1)
6626 /* Setup lineno so that it refers to the line of the import (in
6627 case we parse a class file and encounter errors */
6628 tree decl;
6629 int saved_lineno = lineno;
6630 lineno = EXPR_WFL_LINENO (cl);
6631 TYPE_NAME (class_type) = node_to_use;
6632 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6633 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6634 /* If there is no DECL set for the class or if the class isn't
6635 loaded and not seen in source yet, the load */
6636 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6637 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6638 load_class (node_to_use, 0);
6639 lineno = saved_lineno;
6640 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6642 else
6643 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6646 /* Add package NAME to the list of package encountered so far. To
6647 speed up class lookup in do_resolve_class, we make sure a
6648 particular package is added only once. */
6650 static void
6651 register_package (name)
6652 tree name;
6654 static struct hash_table _pht, *pht = NULL;
6656 if (!pht)
6658 hash_table_init (&_pht, hash_newfunc,
6659 java_hash_hash_tree_node, java_hash_compare_tree_node);
6660 pht = &_pht;
6663 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6665 package_list = chainon (package_list, build_tree_list (name, NULL));
6666 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6670 static tree
6671 resolve_package (pkg, next)
6672 tree pkg, *next;
6674 tree current, acc;
6675 tree type_name = NULL_TREE;
6676 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6678 /* The trick is to determine when the package name stops and were
6679 the name of something contained in the package starts. Then we
6680 return a fully qualified name of what we want to get. */
6682 /* Do a quick search on well known package names */
6683 if (!strncmp (name, "java.lang.reflect", 17))
6685 *next =
6686 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6687 type_name = lookup_package_type (name, 17);
6689 else if (!strncmp (name, "java.lang", 9))
6691 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6692 type_name = lookup_package_type (name, 9);
6695 /* If we found something here, return */
6696 if (type_name)
6697 return type_name;
6699 *next = EXPR_WFL_QUALIFICATION (pkg);
6701 /* Try the current package. */
6702 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6703 IDENTIFIER_LENGTH (ctxp->package)))
6705 type_name =
6706 lookup_package_type_and_set_next (name,
6707 IDENTIFIER_LENGTH (ctxp->package),
6708 next );
6709 if (type_name)
6710 return type_name;
6713 /* Search in imported package */
6714 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6716 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6717 int len = IDENTIFIER_LENGTH (current_pkg_name);
6718 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6720 tree left, dummy;
6722 breakdown_qualified (&left, &dummy, current_pkg_name);
6723 len = IDENTIFIER_LENGTH (left);
6724 type_name = lookup_package_type_and_set_next (name, len, next);
6725 if (type_name)
6726 break;
6730 /* Try to progressively construct a type name */
6731 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6732 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6733 current; current = TREE_CHAIN (current))
6735 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6736 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6738 type_name = acc;
6739 /* resolve_package should be used in a loop, hence we
6740 point at this one to naturally process the next one at
6741 the next iteration. */
6742 *next = current;
6743 break;
6746 return type_name;
6749 static tree
6750 lookup_package_type_and_set_next (name, len, next)
6751 const char *name;
6752 int len;
6753 tree *next;
6755 const char *ptr;
6756 tree type_name = lookup_package_type (name, len);
6758 if (!type_name)
6759 return NULL;
6761 ptr = IDENTIFIER_POINTER (type_name);
6762 while (ptr && (ptr = strchr (ptr, '.')))
6764 *next = TREE_CHAIN (*next);
6765 ptr++;
6767 return type_name;
6770 static tree
6771 lookup_package_type (name, from)
6772 const char *name;
6773 int from;
6775 char subname [128];
6776 const char *sub = &name[from+1];
6777 while (*sub != '.' && *sub)
6778 sub++;
6779 strncpy (subname, name, sub-name);
6780 subname [sub-name] = '\0';
6781 return get_identifier (subname);
6784 static void
6785 check_inner_class_access (decl, enclosing_decl, cl)
6786 tree decl, enclosing_decl, cl;
6788 int access = 0;
6790 /* We don't issue an error message when CL is null. CL can be null
6791 as a result of processing a JDEP crafted by source_start_java_method
6792 for the purpose of patching its parm decl. But the error would
6793 have been already trapped when fixing the method's signature.
6794 DECL can also be NULL in case of earlier errors. */
6795 if (!decl || !cl)
6796 return;
6798 /* We grant access to private and protected inner classes if the
6799 location from where we're trying to access DECL is an enclosing
6800 context for DECL or if both have a common enclosing context. */
6801 if (CLASS_PRIVATE (decl))
6802 access = 1;
6803 if (CLASS_PROTECTED (decl))
6804 access = 2;
6805 if (!access)
6806 return;
6808 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6809 TREE_TYPE (decl))
6810 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6811 TREE_TYPE (decl)))
6812 return;
6814 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6815 (access == 1 ? "private" : "protected"),
6816 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6817 lang_printable_name (decl, 0));
6820 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6821 access violations were found, 1 otherwise. */
6823 static int
6824 check_pkg_class_access (class_name, cl)
6825 tree class_name;
6826 tree cl;
6828 tree type;
6830 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6831 return 0;
6833 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6834 return 0;
6836 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6838 /* Access to a private class within the same package is
6839 allowed. */
6840 tree l, r;
6841 breakdown_qualified (&l, &r, class_name);
6842 if (l == ctxp->package)
6843 return 0;
6845 parse_error_context
6846 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
6847 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6848 IDENTIFIER_POINTER (class_name));
6849 return 1;
6851 return 0;
6854 /* Local variable declaration. */
6856 static void
6857 declare_local_variables (modifier, type, vlist)
6858 int modifier;
6859 tree type;
6860 tree vlist;
6862 tree decl, current, saved_type;
6863 tree type_wfl = NULL_TREE;
6864 int must_chain = 0;
6865 int final_p = 0;
6867 /* Push a new block if statements were seen between the last time we
6868 pushed a block and now. Keep a cound of block to close */
6869 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
6871 tree body = GET_CURRENT_BLOCK (current_function_decl);
6872 tree b = enter_block ();
6873 BLOCK_EXPR_ORIGIN (b) = body;
6876 if (modifier)
6878 int i;
6879 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
6880 if (modifier == ACC_FINAL)
6881 final_p = 1;
6882 else
6884 parse_error_context
6885 (ctxp->modifier_ctx [i],
6886 "Only `final' is allowed as a local variables modifier");
6887 return;
6891 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6892 hold the TYPE value if a new incomplete has to be created (as
6893 opposed to being found already existing and reused). */
6894 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6896 /* If TYPE is fully resolved and we don't have a reference, make one */
6897 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6899 /* Go through all the declared variables */
6900 for (current = vlist, saved_type = type; current;
6901 current = TREE_CHAIN (current), type = saved_type)
6903 tree other, real_type;
6904 tree wfl = TREE_PURPOSE (current);
6905 tree name = EXPR_WFL_NODE (wfl);
6906 tree init = TREE_VALUE (current);
6908 /* Process NAME, as it may specify extra dimension(s) for it */
6909 type = build_array_from_name (type, type_wfl, name, &name);
6911 /* Variable redefinition check */
6912 if ((other = lookup_name_in_blocks (name)))
6914 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6915 DECL_SOURCE_LINE (other));
6916 continue;
6919 /* Type adjustment. We may have just readjusted TYPE because
6920 the variable specified more dimensions. Make sure we have
6921 a reference if we can and don't have one already. */
6922 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6924 real_type = GET_REAL_TYPE (type);
6925 /* Never layout this decl. This will be done when its scope
6926 will be entered */
6927 decl = build_decl (VAR_DECL, name, real_type);
6928 LOCAL_FINAL (decl) = final_p;
6929 BLOCK_CHAIN_DECL (decl);
6931 /* If doing xreferencing, replace the line number with the WFL
6932 compound value */
6933 if (flag_emit_xref)
6934 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6936 /* Don't try to use an INIT statement when an error was found */
6937 if (init && java_error_count)
6938 init = NULL_TREE;
6940 /* Add the initialization function to the current function's code */
6941 if (init)
6943 /* Name might have been readjusted */
6944 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6945 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6946 java_method_add_stmt (current_function_decl,
6947 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6948 init));
6951 /* Setup dependency the type of the decl */
6952 if (must_chain)
6954 jdep *dep;
6955 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
6956 dep = CLASSD_LAST (ctxp->classd_list);
6957 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
6960 SOURCE_FRONTEND_DEBUG (("Defined locals"));
6963 /* Called during parsing. Build decls from argument list. */
6965 static void
6966 source_start_java_method (fndecl)
6967 tree fndecl;
6969 tree tem;
6970 tree parm_decl;
6971 int i;
6973 if (!fndecl)
6974 return;
6976 current_function_decl = fndecl;
6978 /* New scope for the function */
6979 enter_block ();
6980 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
6981 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
6983 tree type = TREE_VALUE (tem);
6984 tree name = TREE_PURPOSE (tem);
6986 /* If type is incomplete. Create an incomplete decl and ask for
6987 the decl to be patched later */
6988 if (INCOMPLETE_TYPE_P (type))
6990 jdep *jdep;
6991 tree real_type = GET_REAL_TYPE (type);
6992 parm_decl = build_decl (PARM_DECL, name, real_type);
6993 type = obtain_incomplete_type (type);
6994 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
6995 jdep = CLASSD_LAST (ctxp->classd_list);
6996 JDEP_MISC (jdep) = name;
6997 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
6999 else
7000 parm_decl = build_decl (PARM_DECL, name, type);
7002 /* Remember if a local variable was declared final (via its
7003 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7004 if (ARG_FINAL_P (tem))
7005 LOCAL_FINAL (parm_decl) = 1;
7007 BLOCK_CHAIN_DECL (parm_decl);
7009 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7010 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7011 nreverse (tem);
7012 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7013 DECL_MAX_LOCALS (current_function_decl) = i;
7016 /* Called during parsing. Creates an artificial method declaration. */
7018 static tree
7019 create_artificial_method (class, flags, type, name, args)
7020 tree class;
7021 int flags;
7022 tree type, name, args;
7024 tree mdecl;
7026 java_parser_context_save_global ();
7027 lineno = 0;
7028 mdecl = make_node (FUNCTION_TYPE);
7029 TREE_TYPE (mdecl) = type;
7030 TYPE_ARG_TYPES (mdecl) = args;
7031 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7032 java_parser_context_restore_global ();
7033 DECL_ARTIFICIAL (mdecl) = 1;
7034 return mdecl;
7037 /* Starts the body if an artifical method. */
7039 static void
7040 start_artificial_method_body (mdecl)
7041 tree mdecl;
7043 DECL_SOURCE_LINE (mdecl) = 1;
7044 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7045 source_start_java_method (mdecl);
7046 enter_block ();
7049 static void
7050 end_artificial_method_body (mdecl)
7051 tree mdecl;
7053 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
7054 exit_block ();
7057 /* Called during expansion. Push decls formerly built from argument
7058 list so they're usable during expansion. */
7060 static void
7061 expand_start_java_method (fndecl)
7062 tree fndecl;
7064 tree tem, *ptr;
7066 current_function_decl = fndecl;
7068 if (! quiet_flag)
7069 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
7070 announce_function (fndecl);
7071 if (! quiet_flag)
7072 fprintf (stderr, "]");
7074 pushlevel (1); /* Prepare for a parameter push */
7075 ptr = &DECL_ARGUMENTS (fndecl);
7076 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7077 while (tem)
7079 tree next = TREE_CHAIN (tem);
7080 tree type = TREE_TYPE (tem);
7081 if (PROMOTE_PROTOTYPES
7082 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7083 && INTEGRAL_TYPE_P (type))
7084 type = integer_type_node;
7085 DECL_ARG_TYPE (tem) = type;
7086 layout_decl (tem, 0);
7087 pushdecl (tem);
7088 *ptr = tem;
7089 ptr = &TREE_CHAIN (tem);
7090 tem = next;
7092 *ptr = NULL_TREE;
7093 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7094 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
7097 /* Terminate a function and expand its body. */
7099 static void
7100 source_end_java_method ()
7102 tree fndecl = current_function_decl;
7103 int flag_asynchronous_exceptions = asynchronous_exceptions;
7105 if (!fndecl)
7106 return;
7108 java_parser_context_save_global ();
7109 lineno = ctxp->last_ccb_indent1;
7111 /* Set EH language codes */
7112 java_set_exception_lang_code ();
7114 /* Turn function bodies with only a NOP expr null, so they don't get
7115 generated at all and we won't get warnings when using the -W
7116 -Wall flags. */
7117 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7118 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7120 /* Generate function's code */
7121 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7122 && ! flag_emit_class_files
7123 && ! flag_emit_xref)
7124 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7126 /* pop out of its parameters */
7127 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7128 poplevel (1, 0, 1);
7129 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7131 /* Generate rtl for function exit. */
7132 if (! flag_emit_class_files && ! flag_emit_xref)
7134 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7135 /* Emit catch-finally clauses */
7136 emit_handlers ();
7137 expand_function_end (input_filename, lineno, 0);
7139 /* FIXME: If the current method contains any exception handlers,
7140 force asynchronous_exceptions: this is necessary because signal
7141 handlers in libjava may throw exceptions. This is far from being
7142 a perfect solution, but it's better than doing nothing at all.*/
7143 if (catch_clauses)
7144 asynchronous_exceptions = 1;
7146 /* Run the optimizers and output assembler code for this function. */
7147 rest_of_compilation (fndecl);
7150 current_function_decl = NULL_TREE;
7151 permanent_allocation (1);
7152 java_parser_context_restore_global ();
7153 asynchronous_exceptions = flag_asynchronous_exceptions;
7156 /* Record EXPR in the current function block. Complements compound
7157 expression second operand if necessary. */
7159 tree
7160 java_method_add_stmt (fndecl, expr)
7161 tree fndecl, expr;
7163 if (!GET_CURRENT_BLOCK (fndecl))
7164 return NULL_TREE;
7165 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7168 static tree
7169 add_stmt_to_block (b, type, stmt)
7170 tree b, type, stmt;
7172 tree body = BLOCK_EXPR_BODY (b), c;
7174 if (java_error_count)
7175 return body;
7177 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7178 return body;
7180 BLOCK_EXPR_BODY (b) = c;
7181 TREE_SIDE_EFFECTS (c) = 1;
7182 return c;
7185 /* Add STMT to EXISTING if possible, otherwise create a new
7186 COMPOUND_EXPR and add STMT to it. */
7188 static tree
7189 add_stmt_to_compound (existing, type, stmt)
7190 tree existing, type, stmt;
7192 if (existing)
7193 return build (COMPOUND_EXPR, type, existing, stmt);
7194 else
7195 return stmt;
7198 /* Hold THIS for the scope of the current public method decl. */
7199 static tree current_this;
7201 void java_layout_seen_class_methods ()
7203 tree previous_list = all_class_list;
7204 tree end = NULL_TREE;
7205 tree current;
7207 while (1)
7209 for (current = previous_list;
7210 current != end; current = TREE_CHAIN (current))
7211 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7213 if (previous_list != all_class_list)
7215 end = previous_list;
7216 previous_list = all_class_list;
7218 else
7219 break;
7223 void
7224 java_reorder_fields ()
7226 static tree stop_reordering = NULL_TREE;
7228 tree current;
7229 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7231 current_class = TREE_TYPE (TREE_VALUE (current));
7233 if (current_class == stop_reordering)
7234 break;
7236 /* Reverse the fields, but leave the dummy field in front.
7237 Fields are already ordered for Object and Class */
7238 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7239 && current_class != class_type_node)
7241 /* If the dummy field is there, reverse the right fields and
7242 just layout the type for proper fields offset */
7243 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7245 tree fields = TYPE_FIELDS (current_class);
7246 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7247 TYPE_SIZE (current_class) = NULL_TREE;
7249 /* We don't have a dummy field, we need to layout the class,
7250 after having reversed the fields */
7251 else
7253 TYPE_FIELDS (current_class) =
7254 nreverse (TYPE_FIELDS (current_class));
7255 TYPE_SIZE (current_class) = NULL_TREE;
7259 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7262 /* Layout the methods of all classes loaded in one way on an
7263 other. Check methods of source parsed classes. Then reorder the
7264 fields and layout the classes or the type of all source parsed
7265 classes */
7267 void
7268 java_layout_classes ()
7270 tree current;
7271 int save_error_count = java_error_count;
7273 /* Layout the methods of all classes seen so far */
7274 java_layout_seen_class_methods ();
7275 java_parse_abort_on_error ();
7276 all_class_list = NULL_TREE;
7278 /* Then check the methods of all parsed classes */
7279 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7280 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7281 CHECK_METHODS (TREE_VALUE (current));
7282 java_parse_abort_on_error ();
7284 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7286 current_class = TREE_TYPE (TREE_VALUE (current));
7287 layout_class (current_class);
7289 /* From now on, the class is considered completely loaded */
7290 CLASS_LOADED_P (current_class) = 1;
7292 /* Error reported by the caller */
7293 if (java_error_count)
7294 return;
7297 /* We might have reloaded classes durign the process of laying out
7298 classes for code generation. We must layout the methods of those
7299 late additions, as constructor checks might use them */
7300 java_layout_seen_class_methods ();
7301 java_parse_abort_on_error ();
7304 /* Expand methods in the current set of classes rememebered for
7305 generation. */
7307 static void
7308 java_complete_expand_classes ()
7310 tree current;
7312 do_not_fold = flag_emit_xref;
7314 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7315 if (!INNER_CLASS_DECL_P (current))
7316 java_complete_expand_class (current);
7319 /* Expand the methods found in OUTER, starting first by OUTER's inner
7320 classes, if any. */
7322 static void
7323 java_complete_expand_class (outer)
7324 tree outer;
7326 tree inner_list;
7328 set_nested_class_simple_name_value (outer, 1); /* Set */
7330 /* We need to go after all inner classes and start expanding them,
7331 starting with most nested ones. We have to do that because nested
7332 classes might add functions to outer classes */
7334 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7335 inner_list; inner_list = TREE_CHAIN (inner_list))
7336 java_complete_expand_class (TREE_PURPOSE (inner_list));
7338 java_complete_expand_methods (outer);
7339 set_nested_class_simple_name_value (outer, 0); /* Reset */
7342 /* Expand methods registered in CLASS_DECL. The general idea is that
7343 we expand regular methods first. This allows us get an estimate on
7344 how outer context local alias fields are really used so we can add
7345 to the constructor just enough code to initialize them properly (it
7346 also lets us generate $finit$ correctly.) Then we expand the
7347 constructors and then <clinit>. */
7349 static void
7350 java_complete_expand_methods (class_decl)
7351 tree class_decl;
7353 tree clinit, finit, decl, first_decl;
7355 current_class = TREE_TYPE (class_decl);
7357 /* Initialize a new constant pool */
7358 init_outgoing_cpool ();
7360 /* Pre-expand <clinit> to figure whether we really need it or
7361 not. If we do need it, we pre-expand the static fields so they're
7362 ready to be used somewhere else. <clinit> will be fully expanded
7363 after we processed the constructors. */
7364 first_decl = TYPE_METHODS (current_class);
7365 clinit = maybe_generate_pre_expand_clinit (current_class);
7367 /* Then generate $finit$ (if we need to) because constructor will
7368 try to use it.*/
7369 if (TYPE_FINIT_STMT_LIST (current_class))
7371 finit = generate_finit (current_class);
7372 java_complete_expand_method (finit);
7375 /* Now do the constructors */
7376 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7378 int no_body;
7380 if (!DECL_CONSTRUCTOR_P (decl))
7381 continue;
7383 no_body = !DECL_FUNCTION_BODY (decl);
7384 /* Don't generate debug info on line zero when expanding a
7385 generated constructor. */
7386 if (no_body)
7387 restore_line_number_status (1);
7389 java_complete_expand_method (decl);
7391 if (no_body)
7392 restore_line_number_status (0);
7395 /* First, do the ordinary methods. */
7396 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7398 /* Skip abstract or native methods -- but do handle native
7399 methods when generating JNI stubs. */
7400 if (METHOD_ABSTRACT (decl)
7401 || (! flag_jni && METHOD_NATIVE (decl))
7402 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7403 continue;
7405 if (METHOD_NATIVE (decl))
7407 tree body = build_jni_stub (decl);
7408 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7411 java_complete_expand_method (decl);
7414 /* If there is indeed a <clinit>, fully expand it now */
7415 if (clinit)
7417 /* Prevent the use of `this' inside <clinit> */
7418 ctxp->explicit_constructor_p = 1;
7419 java_complete_expand_method (clinit);
7420 ctxp->explicit_constructor_p = 0;
7423 /* We might have generated a class$ that we now want to expand */
7424 if (TYPE_DOT_CLASS (current_class))
7425 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7427 /* Now verify constructor circularity (stop after the first one we
7428 prove wrong.) */
7429 if (!CLASS_INTERFACE (class_decl))
7430 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7431 if (DECL_CONSTRUCTOR_P (decl)
7432 && verify_constructor_circularity (decl, decl))
7433 break;
7435 /* Save the constant pool. We'll need to restore it later. */
7436 TYPE_CPOOL (current_class) = outgoing_cpool;
7439 /* Hold a list of catch clauses list. The first element of this list is
7440 the list of the catch clauses of the currently analysed try block. */
7441 static tree currently_caught_type_list;
7443 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7444 safely used in some other methods/constructors. */
7446 static tree
7447 maybe_generate_pre_expand_clinit (class_type)
7448 tree class_type;
7450 tree current, mdecl;
7452 if (!TYPE_CLINIT_STMT_LIST (class_type))
7453 return NULL_TREE;
7455 /* Go through all static fields and pre expand them */
7456 for (current = TYPE_FIELDS (class_type); current;
7457 current = TREE_CHAIN (current))
7458 if (FIELD_STATIC (current))
7459 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7461 /* Then build the <clinit> method */
7462 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7463 clinit_identifier_node, end_params_node);
7464 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7465 mdecl, NULL_TREE);
7466 start_artificial_method_body (mdecl);
7468 /* We process the list of assignment we produced as the result of
7469 the declaration of initialized static field and add them as
7470 statement to the <clinit> method. */
7471 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7472 current = TREE_CHAIN (current))
7474 tree stmt = current;
7475 /* We build the assignment expression that will initialize the
7476 field to its value. There are strict rules on static
7477 initializers (8.5). FIXME */
7478 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7479 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7480 java_method_add_stmt (mdecl, stmt);
7483 end_artificial_method_body (mdecl);
7485 /* Now we want to place <clinit> as the last method (because we need
7486 it at least for interface so that it doesn't interfere with the
7487 dispatch table based lookup. */
7488 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7490 current = TREE_CHAIN (TYPE_METHODS (class_type));
7491 TYPE_METHODS (class_type) = current;
7493 while (TREE_CHAIN (current))
7494 current = TREE_CHAIN (current);
7496 TREE_CHAIN (current) = mdecl;
7497 TREE_CHAIN (mdecl) = NULL_TREE;
7500 return mdecl;
7503 /* See whether we could get rid of <clinit>. Criteria are: all static
7504 final fields have constant initial values and the body of <clinit>
7505 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7507 static int
7508 maybe_yank_clinit (mdecl)
7509 tree mdecl;
7511 tree type, current;
7512 tree fbody, bbody;
7514 if (!DECL_CLINIT_P (mdecl))
7515 return 0;
7517 /* If the body isn't empty, then we keep <clinit> */
7518 fbody = DECL_FUNCTION_BODY (mdecl);
7519 if ((bbody = BLOCK_EXPR_BODY (fbody)))
7520 bbody = BLOCK_EXPR_BODY (bbody);
7521 if (bbody && bbody != empty_stmt_node)
7522 return 0;
7524 type = DECL_CONTEXT (mdecl);
7525 current = TYPE_FIELDS (type);
7527 for (current = (current ? TREE_CHAIN (current) : current);
7528 current; current = TREE_CHAIN (current))
7529 if (!(FIELD_STATIC (current) && FIELD_FINAL (current)
7530 && DECL_INITIAL (current) && TREE_CONSTANT (DECL_INITIAL (current))))
7531 break;
7533 if (current)
7534 return 0;
7536 /* Get rid of <clinit> in the class' list of methods */
7537 if (TYPE_METHODS (type) == mdecl)
7538 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7539 else
7540 for (current = TYPE_METHODS (type); current;
7541 current = TREE_CHAIN (current))
7542 if (TREE_CHAIN (current) == mdecl)
7544 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7545 break;
7548 return 1;
7552 /* Complete and expand a method. */
7554 static void
7555 java_complete_expand_method (mdecl)
7556 tree mdecl;
7558 int yank_clinit = 0;
7560 current_function_decl = mdecl;
7561 /* Fix constructors before expanding them */
7562 if (DECL_CONSTRUCTOR_P (mdecl))
7563 fix_constructors (mdecl);
7565 /* Expand functions that have a body */
7566 if (DECL_FUNCTION_BODY (mdecl))
7568 tree fbody = DECL_FUNCTION_BODY (mdecl);
7569 tree block_body = BLOCK_EXPR_BODY (fbody);
7570 tree exception_copy = NULL_TREE;
7571 expand_start_java_method (mdecl);
7572 build_result_decl (mdecl);
7574 current_this
7575 = (!METHOD_STATIC (mdecl) ?
7576 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7578 /* Purge the `throws' list of unchecked exceptions. If we're
7579 doing xref, save a copy of the list and re-install it
7580 later. */
7581 if (flag_emit_xref)
7582 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7584 purge_unchecked_exceptions (mdecl);
7586 /* Install exceptions thrown with `throws' */
7587 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7589 if (block_body != NULL_TREE)
7591 block_body = java_complete_tree (block_body);
7593 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7594 check_for_initialization (block_body);
7595 ctxp->explicit_constructor_p = 0;
7598 BLOCK_EXPR_BODY (fbody) = block_body;
7600 /* If we saw a return but couldn't evaluate it properly, we'll
7601 have an error_mark_node here. */
7602 if (block_body != error_mark_node
7603 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7604 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7605 && !flag_emit_xref)
7606 missing_return_error (current_function_decl);
7608 /* Check wether we could just get rid of clinit, now the picture
7609 is complete. */
7610 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7611 complete_start_java_method (mdecl);
7613 /* Don't go any further if we've found error(s) during the
7614 expansion */
7615 if (!java_error_count && !yank_clinit)
7616 source_end_java_method ();
7617 else
7619 if (java_error_count)
7620 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7621 poplevel (1, 0, 1);
7624 /* Pop the exceptions and sanity check */
7625 POP_EXCEPTIONS();
7626 if (currently_caught_type_list)
7627 fatal ("Exception list non empty - java_complete_expand_method");
7629 if (flag_emit_xref)
7630 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7636 /* This section of the code deals with accessing enclosing context
7637 fields either directly by using the relevant access to this$<n> or
7638 by invoking an access method crafted for that purpose. */
7640 /* Build the necessary access from an inner class to an outer
7641 class. This routine could be optimized to cache previous result
7642 (decl, current_class and returned access). When an access method
7643 needs to be generated, it always takes the form of a read. It might
7644 be later turned into a write by calling outer_field_access_fix. */
7646 static tree
7647 build_outer_field_access (id, decl)
7648 tree id, decl;
7650 tree access = NULL_TREE;
7651 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7653 /* If decl's class is the direct outer class of the current_class,
7654 build the access as `this$<n>.<field>'. Not that we will break
7655 the `private' barrier if we're not emitting bytecodes. */
7656 if (ctx == DECL_CONTEXT (decl)
7657 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7659 tree thisn = build_current_thisn (current_class);
7660 access = make_qualified_primary (build_wfl_node (thisn),
7661 id, EXPR_WFL_LINECOL (id));
7663 /* Otherwise, generate access methods to outer this and access the
7664 field (either using an access method or by direct access.) */
7665 else
7667 int lc = EXPR_WFL_LINECOL (id);
7669 /* Now we chain the required number of calls to the access$0 to
7670 get a hold to the enclosing instance we need, and the we
7671 build the field access. */
7672 access = build_access_to_thisn (ctx, DECL_CONTEXT (decl), lc);
7674 /* If the field is private and we're generating bytecode, then
7675 we generate an access method */
7676 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7678 tree name = build_outer_field_access_methods (decl);
7679 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7680 name, access, NULL_TREE);
7682 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7683 Once again we break the `private' access rule from a foreign
7684 class. */
7685 else
7686 access = make_qualified_primary (access, id, lc);
7688 return resolve_expression_name (access, NULL);
7691 /* Return a non zero value if NODE describes an outer field inner
7692 access. */
7694 static int
7695 outer_field_access_p (type, decl)
7696 tree type, decl;
7698 if (!INNER_CLASS_TYPE_P (type)
7699 || TREE_CODE (decl) != FIELD_DECL
7700 || DECL_CONTEXT (decl) == type)
7701 return 0;
7703 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7704 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7706 if (type == DECL_CONTEXT (decl))
7707 return 1;
7708 if (!DECL_CONTEXT (TYPE_NAME (type)))
7709 break;
7712 return 0;
7715 /* Return a non zero value if NODE represents an outer field inner
7716 access that was been already expanded. As a side effect, it returns
7717 the name of the field being accessed and the argument passed to the
7718 access function, suitable for a regeneration of the access method
7719 call if necessary. */
7721 static int
7722 outer_field_expanded_access_p (node, name, arg_type, arg)
7723 tree node, *name, *arg_type, *arg;
7725 int identified = 0;
7727 if (TREE_CODE (node) != CALL_EXPR)
7728 return 0;
7730 /* Well, gcj generates slightly different tree nodes when compiling
7731 to native or bytecodes. It's the case for function calls. */
7733 if (flag_emit_class_files
7734 && TREE_CODE (node) == CALL_EXPR
7735 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7736 identified = 1;
7737 else if (!flag_emit_class_files)
7739 node = TREE_OPERAND (node, 0);
7741 if (node && TREE_OPERAND (node, 0)
7742 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7744 node = TREE_OPERAND (node, 0);
7745 if (TREE_OPERAND (node, 0)
7746 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7747 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7748 (DECL_NAME (TREE_OPERAND (node, 0)))))
7749 identified = 1;
7753 if (identified && name && arg_type && arg)
7755 tree argument = TREE_OPERAND (node, 1);
7756 *name = DECL_NAME (TREE_OPERAND (node, 0));
7757 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7758 *arg = TREE_VALUE (argument);
7760 return identified;
7763 /* Detect in NODE an outer field read access from an inner class and
7764 transform it into a write with RHS as an argument. This function is
7765 called from the java_complete_lhs when an assignment to a LHS can
7766 be identified. */
7768 static tree
7769 outer_field_access_fix (wfl, node, rhs)
7770 tree wfl, node, rhs;
7772 tree name, arg_type, arg;
7774 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7776 /* At any rate, check whether we're trying to assign a value to
7777 a final. */
7778 tree accessed = (JDECL_P (node) ? node :
7779 (TREE_CODE (node) == COMPONENT_REF ?
7780 TREE_OPERAND (node, 1) : node));
7781 if (check_final_assignment (accessed, wfl))
7782 return error_mark_node;
7784 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7785 arg_type, name, arg, rhs);
7786 return java_complete_tree (node);
7788 return NULL_TREE;
7791 /* Construct the expression that calls an access method:
7792 <type>.access$<n>(<arg1> [, <arg2>]);
7794 ARG2 can be NULL and will be omitted in that case. It will denote a
7795 read access. */
7797 static tree
7798 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7799 int lc;
7800 tree type, access_method_name, arg1, arg2;
7802 tree args, cn, access;
7804 args = arg1 ? arg1 :
7805 build_wfl_node (build_current_thisn (current_class));
7806 args = build_tree_list (NULL_TREE, args);
7808 if (arg2)
7809 args = tree_cons (NULL_TREE, arg2, args);
7811 access = build_method_invocation (build_wfl_node (access_method_name), args);
7812 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7813 return make_qualified_primary (cn, access, lc);
7816 static tree
7817 build_new_access_id ()
7819 static int access_n_counter = 1;
7820 char buffer [128];
7822 sprintf (buffer, "access$%d", access_n_counter++);
7823 return get_identifier (buffer);
7826 /* Create the static access functions for the outer field DECL. We define a
7827 read:
7828 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7829 return inst$.field;
7831 and a write access:
7832 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7833 TREE_TYPE (<field>) value$) {
7834 return inst$.field = value$;
7836 We should have a usage flags on the DECL so we can lazily turn the ones
7837 we're using for code generation. FIXME.
7840 static tree
7841 build_outer_field_access_methods (decl)
7842 tree decl;
7844 tree id, args, stmt, mdecl;
7846 /* Check point, to be removed. FIXME */
7847 if (FIELD_INNER_ACCESS (decl)
7848 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
7849 abort ();
7851 if (FIELD_INNER_ACCESS (decl))
7852 return FIELD_INNER_ACCESS (decl);
7854 push_obstacks (&permanent_obstack, &permanent_obstack);
7856 /* Create the identifier and a function named after it. */
7857 id = build_new_access_id ();
7859 /* The identifier is marked as bearing the name of a generated write
7860 access function for outer field accessed from inner classes. */
7861 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7863 /* Create the read access */
7864 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7865 TREE_CHAIN (args) = end_params_node;
7866 stmt = make_qualified_primary (build_wfl_node (inst_id),
7867 build_wfl_node (DECL_NAME (decl)), 0);
7868 stmt = build_return (0, stmt);
7869 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7870 TREE_TYPE (decl), id, args, stmt);
7871 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7873 /* Create the write access method */
7874 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7875 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
7876 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
7877 stmt = make_qualified_primary (build_wfl_node (inst_id),
7878 build_wfl_node (DECL_NAME (decl)), 0);
7879 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
7880 build_wfl_node (wpv_id)));
7882 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7883 TREE_TYPE (decl), id, args, stmt);
7884 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7885 pop_obstacks ();
7887 /* Return the access name */
7888 return FIELD_INNER_ACCESS (decl) = id;
7891 /* Build an field access method NAME. */
7893 static tree
7894 build_outer_field_access_method (class, type, name, args, body)
7895 tree class, type, name, args, body;
7897 tree saved_current_function_decl, mdecl;
7899 /* Create the method */
7900 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
7901 fix_method_argument_names (args, mdecl);
7902 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7904 /* Attach the method body. */
7905 saved_current_function_decl = current_function_decl;
7906 start_artificial_method_body (mdecl);
7907 java_method_add_stmt (mdecl, body);
7908 end_artificial_method_body (mdecl);
7909 current_function_decl = saved_current_function_decl;
7911 return mdecl;
7915 /* This section deals with building access function necessary for
7916 certain kinds of method invocation from inner classes. */
7918 static tree
7919 build_outer_method_access_method (decl)
7920 tree decl;
7922 tree saved_current_function_decl, mdecl;
7923 tree args = NULL_TREE, call_args = NULL_TREE;
7924 tree carg, id, body, class;
7925 char buffer [80];
7926 int parm_id_count = 0;
7928 /* Test this abort with an access to a private field */
7929 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
7930 abort ();
7932 /* Check the cache first */
7933 if (DECL_FUNCTION_INNER_ACCESS (decl))
7934 return DECL_FUNCTION_INNER_ACCESS (decl);
7936 class = DECL_CONTEXT (decl);
7938 /* Obtain an access identifier and mark it */
7939 id = build_new_access_id ();
7940 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7942 push_obstacks (&permanent_obstack, &permanent_obstack);
7944 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
7945 /* Create the arguments, as much as the original */
7946 for (; carg && carg != end_params_node;
7947 carg = TREE_CHAIN (carg))
7949 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
7950 args = chainon (args, build_tree_list (get_identifier (buffer),
7951 TREE_VALUE (carg)));
7953 args = chainon (args, end_params_node);
7955 /* Create the method */
7956 mdecl = create_artificial_method (class, ACC_STATIC,
7957 TREE_TYPE (TREE_TYPE (decl)), id, args);
7958 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7959 /* There is a potential bug here. We should be able to use
7960 fix_method_argument_names, but then arg names get mixed up and
7961 eventually a constructor will have its this$0 altered and the
7962 outer context won't be assignment properly. The test case is
7963 stub.java FIXME */
7964 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
7966 /* Attach the method body. */
7967 saved_current_function_decl = current_function_decl;
7968 start_artificial_method_body (mdecl);
7970 /* The actual method invocation uses the same args. When invoking a
7971 static methods that way, we don't want to skip the first
7972 argument. */
7973 carg = args;
7974 if (!METHOD_STATIC (decl))
7975 carg = TREE_CHAIN (carg);
7976 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
7977 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
7978 call_args);
7980 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
7981 call_args);
7982 if (!METHOD_STATIC (decl))
7983 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
7984 body, 0);
7985 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
7986 body = build_return (0, body);
7987 java_method_add_stmt (mdecl,body);
7988 end_artificial_method_body (mdecl);
7989 current_function_decl = saved_current_function_decl;
7990 pop_obstacks ();
7992 /* Back tag the access function so it know what it accesses */
7993 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
7995 /* Tag the current method so it knows it has an access generated */
7996 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8000 /* This section of the code deals with building expressions to access
8001 the enclosing instance of an inner class. The enclosing instance is
8002 kept in a generated field called this$<n>, with <n> being the
8003 inner class nesting level (starting from 0.) */
8005 /* Build an access to a given this$<n>, possibly by chaining access
8006 call to others. Access methods to this$<n> are build on the fly if
8007 necessary */
8009 static tree
8010 build_access_to_thisn (from, to, lc)
8011 tree from, to;
8012 int lc;
8014 tree access = NULL_TREE;
8016 while (from != to)
8018 tree access0_wfl, cn;
8020 maybe_build_thisn_access_method (from);
8021 access0_wfl = build_wfl_node (access0_identifier_node);
8022 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8023 EXPR_WFL_LINECOL (access0_wfl) = lc;
8025 if (!access)
8027 access = build_current_thisn (current_class);
8028 access = build_wfl_node (access);
8030 access = build_tree_list (NULL_TREE, access);
8031 access = build_method_invocation (access0_wfl, access);
8032 access = make_qualified_primary (cn, access, lc);
8034 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
8036 return access;
8039 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8040 is returned if nothing needs to be generated. Otherwise, the method
8041 generated and a method decl is returned.
8043 NOTE: These generated methods should be declared in a class file
8044 attribute so that they can't be referred to directly. */
8046 static tree
8047 maybe_build_thisn_access_method (type)
8048 tree type;
8050 tree mdecl, args, stmt, rtype;
8051 tree saved_current_function_decl;
8053 /* If TYPE is a top-level class, no access method is required.
8054 If there already is such an access method, bail out. */
8055 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
8056 return NULL_TREE;
8058 /* We generate the method. The method looks like:
8059 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8061 push_obstacks (&permanent_obstack, &permanent_obstack);
8062 args = build_tree_list (inst_id, build_pointer_type (type));
8063 TREE_CHAIN (args) = end_params_node;
8064 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8065 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8066 access0_identifier_node, args);
8067 fix_method_argument_names (args, mdecl);
8068 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8069 stmt = build_current_thisn (type);
8070 stmt = make_qualified_primary (build_wfl_node (inst_id),
8071 build_wfl_node (stmt), 0);
8072 stmt = build_return (0, stmt);
8074 saved_current_function_decl = current_function_decl;
8075 start_artificial_method_body (mdecl);
8076 java_method_add_stmt (mdecl, stmt);
8077 end_artificial_method_body (mdecl);
8078 current_function_decl = saved_current_function_decl;
8079 pop_obstacks ();
8081 CLASS_ACCESS0_GENERATED_P (type) = 1;
8083 return mdecl;
8086 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8087 the first level of innerclassing. this$1 for the next one, etc...
8088 This function can be invoked with TYPE to NULL, available and then
8089 has to count the parser context. */
8091 static tree
8092 build_current_thisn (type)
8093 tree type;
8095 static int saved_i = -1;
8096 static tree saved_thisn = NULL_TREE;
8098 tree decl;
8099 char buffer [80];
8100 int i = 0;
8102 if (type)
8104 static tree saved_type = NULL_TREE;
8105 static int saved_type_i = 0;
8107 if (type == saved_type)
8108 i = saved_type_i;
8109 else
8111 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8112 decl; decl = DECL_CONTEXT (decl), i++)
8115 saved_type = type;
8116 saved_type_i = i;
8119 else
8120 i = list_length (GET_CPC_LIST ())-2;
8122 if (i == saved_i)
8123 return saved_thisn;
8125 sprintf (buffer, "this$%d", i);
8126 saved_i = i;
8127 saved_thisn = get_identifier (buffer);
8128 return saved_thisn;
8131 /* Return the assignement to the hidden enclosing context `this$<n>'
8132 by the second incoming parameter to the innerclass constructor. The
8133 form used is `this.this$<n> = this$<n>;'. */
8135 static tree
8136 build_thisn_assign ()
8138 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8140 tree thisn = build_current_thisn (current_class);
8141 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8142 build_wfl_node (thisn), 0);
8143 tree rhs = build_wfl_node (thisn);
8144 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8145 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8147 return NULL_TREE;
8151 /* Building the synthetic `class$' used to implement the `.class' 1.1
8152 extension for non primitive types. This method looks like:
8154 static Class class$(String type) throws NoClassDefFoundError
8156 try {return (java.lang.Class.forName (String));}
8157 catch (ClassNotFoundException e) {
8158 throw new NoClassDefFoundError(e.getMessage());}
8159 } */
8161 static tree
8162 build_dot_class_method (class)
8163 tree class;
8165 #define BWF(S) build_wfl_node (get_identifier ((S)))
8166 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8167 tree args, tmp, saved_current_function_decl, mdecl;
8168 tree stmt, throw_stmt, catch, catch_block, try_block;
8169 tree catch_clause_param;
8170 tree class_not_found_exception, no_class_def_found_error;
8172 static tree get_message_wfl, type_parm_wfl;
8174 if (!get_message_wfl)
8176 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8177 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8180 /* Build the arguments */
8181 args = build_tree_list (get_identifier ("type$"),
8182 build_pointer_type (string_type_node));
8183 TREE_CHAIN (args) = end_params_node;
8185 /* Build the qualified name java.lang.Class.forName */
8186 tmp = MQN (MQN (MQN (BWF ("java"),
8187 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8189 /* For things we have to catch and throw */
8190 class_not_found_exception =
8191 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8192 no_class_def_found_error =
8193 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8194 load_class (class_not_found_exception, 1);
8195 load_class (no_class_def_found_error, 1);
8197 /* Create the "class$" function */
8198 mdecl = create_artificial_method (class, ACC_STATIC,
8199 build_pointer_type (class_type_node),
8200 get_identifier ("class$"), args);
8201 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8202 no_class_def_found_error);
8204 /* We start by building the try block. We need to build:
8205 return (java.lang.Class.forName (type)); */
8206 stmt = build_method_invocation (tmp,
8207 build_tree_list (NULL_TREE, type_parm_wfl));
8208 stmt = build_return (0, stmt);
8209 /* Put it in a block. That's the try block */
8210 try_block = build_expr_block (stmt, NULL_TREE);
8212 /* Now onto the catch block. We start by building the expression
8213 throwing a new exception:
8214 throw new NoClassDefFoundError (_.getMessage); */
8215 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8216 get_message_wfl, 0);
8217 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8219 /* Build new NoClassDefFoundError (_.getMessage) */
8220 throw_stmt = build_new_invocation
8221 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8222 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8224 /* Build the throw, (it's too early to use BUILD_THROW) */
8225 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8227 /* Build the catch block to encapsulate all this. We begin by
8228 building an decl for the catch clause parameter and link it to
8229 newly created block, the catch block. */
8230 catch_clause_param =
8231 build_decl (VAR_DECL, wpv_id,
8232 build_pointer_type (class_not_found_exception));
8233 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8235 /* We initialize the variable with the exception handler. */
8236 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8237 soft_exceptioninfo_call_node);
8238 add_stmt_to_block (catch_block, NULL_TREE, catch);
8240 /* We add the statement throwing the new exception */
8241 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8243 /* Build a catch expression for all this */
8244 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8246 /* Build the try/catch sequence */
8247 stmt = build_try_statement (0, try_block, catch_block);
8249 fix_method_argument_names (args, mdecl);
8250 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8251 saved_current_function_decl = current_function_decl;
8252 start_artificial_method_body (mdecl);
8253 java_method_add_stmt (mdecl, stmt);
8254 end_artificial_method_body (mdecl);
8255 current_function_decl = saved_current_function_decl;
8256 TYPE_DOT_CLASS (class) = mdecl;
8258 return mdecl;
8261 static tree
8262 build_dot_class_method_invocation (name)
8263 tree name;
8265 tree s = make_node (STRING_CST);
8266 TREE_STRING_LENGTH (s) = IDENTIFIER_LENGTH (name);
8267 TREE_STRING_POINTER (s) = obstack_alloc (expression_obstack,
8268 TREE_STRING_LENGTH (s)+1);
8269 strcpy (TREE_STRING_POINTER (s), IDENTIFIER_POINTER (name));
8270 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8271 build_tree_list (NULL_TREE, s));
8274 /* This section of the code deals with constructor. */
8276 /* Craft a body for default constructor. Patch existing constructor
8277 bodies with call to super() and field initialization statements if
8278 necessary. */
8280 static void
8281 fix_constructors (mdecl)
8282 tree mdecl;
8284 tree body = DECL_FUNCTION_BODY (mdecl);
8285 tree thisn_assign, compound = NULL_TREE;
8286 tree class_type = DECL_CONTEXT (mdecl);
8288 if (!body)
8290 /* It is an error for the compiler to generate a default
8291 constructor if the superclass doesn't have a constructor that
8292 takes no argument, or the same args for an anonymous class */
8293 if (verify_constructor_super (mdecl))
8295 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8296 tree save = DECL_NAME (mdecl);
8297 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8298 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8299 parse_error_context
8300 (lookup_cl (TYPE_NAME (class_type)),
8301 "No constructor matching `%s' found in class `%s'",
8302 lang_printable_name (mdecl, 0), n);
8303 DECL_NAME (mdecl) = save;
8306 /* The constructor body must be crafted by hand. It's the
8307 constructor we defined when we realize we didn't have the
8308 CLASSNAME() constructor */
8309 start_artificial_method_body (mdecl);
8311 /* We don't generate a super constructor invocation if we're
8312 compiling java.lang.Object. build_super_invocation takes care
8313 of that. */
8314 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8316 /* Insert the instance initializer block right here, after the
8317 super invocation. */
8318 add_instance_initializer (mdecl);
8320 /* Insert an assignment to the this$<n> hidden field, if
8321 necessary */
8322 if ((thisn_assign = build_thisn_assign ()))
8323 java_method_add_stmt (mdecl, thisn_assign);
8325 end_artificial_method_body (mdecl);
8327 /* Search for an explicit constructor invocation */
8328 else
8330 int found = 0;
8331 tree main_block = BLOCK_EXPR_BODY (body);
8333 while (body)
8334 switch (TREE_CODE (body))
8336 case CALL_EXPR:
8337 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8338 body = NULL_TREE;
8339 break;
8340 case COMPOUND_EXPR:
8341 case EXPR_WITH_FILE_LOCATION:
8342 body = TREE_OPERAND (body, 0);
8343 break;
8344 case BLOCK:
8345 body = BLOCK_EXPR_BODY (body);
8346 break;
8347 default:
8348 found = 0;
8349 body = NULL_TREE;
8351 /* The constructor is missing an invocation of super() */
8352 if (!found)
8353 compound = add_stmt_to_compound (compound, NULL_TREE,
8354 build_super_invocation (mdecl));
8356 /* Insert the instance initializer block right here, after the
8357 super invocation. */
8358 add_instance_initializer (mdecl);
8360 /* Generate the assignment to this$<n>, if necessary */
8361 if ((thisn_assign = build_thisn_assign ()))
8362 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8364 /* Fix the constructor main block if we're adding extra stmts */
8365 if (compound)
8367 compound = add_stmt_to_compound (compound, NULL_TREE,
8368 BLOCK_EXPR_BODY (main_block));
8369 BLOCK_EXPR_BODY (main_block) = compound;
8374 /* Browse constructors in the super class, searching for a constructor
8375 that doesn't take any argument. Return 0 if one is found, 1
8376 otherwise. If the current class is an anonymous inner class, look
8377 for something that has the same signature. */
8379 static int
8380 verify_constructor_super (mdecl)
8381 tree mdecl;
8383 tree class = CLASSTYPE_SUPER (current_class);
8384 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8385 tree sdecl;
8387 if (!class)
8388 return 0;
8390 if (ANONYMOUS_CLASS_P (current_class))
8392 tree mdecl_arg_type;
8393 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8394 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8395 if (DECL_CONSTRUCTOR_P (sdecl))
8397 tree m_arg_type;
8398 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8399 if (super_inner)
8400 arg_type = TREE_CHAIN (arg_type);
8401 for (m_arg_type = mdecl_arg_type;
8402 (arg_type != end_params_node
8403 && m_arg_type != end_params_node);
8404 arg_type = TREE_CHAIN (arg_type),
8405 m_arg_type = TREE_CHAIN (m_arg_type))
8406 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
8407 break;
8409 if (arg_type == end_params_node && m_arg_type == end_params_node)
8410 return 0;
8413 else
8415 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8417 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8418 if (super_inner)
8419 arg = TREE_CHAIN (arg);
8420 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8421 return 0;
8424 return 1;
8427 /* Generate code for all context remembered for code generation. */
8429 void
8430 java_expand_classes ()
8432 int save_error_count = 0;
8433 static struct parser_ctxt *saved_ctxp = NULL;
8435 java_parse_abort_on_error ();
8436 if (!(ctxp = ctxp_for_generation))
8437 return;
8438 java_layout_classes ();
8439 java_parse_abort_on_error ();
8441 saved_ctxp = ctxp_for_generation;
8442 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8444 ctxp = ctxp_for_generation;
8445 lang_init_source (2); /* Error msgs have method prototypes */
8446 java_complete_expand_classes (); /* Complete and expand classes */
8447 java_parse_abort_on_error ();
8450 /* Find anonymous classes and expand their constructor, now they
8451 have been fixed. */
8452 for (ctxp_for_generation = saved_ctxp;
8453 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8455 tree current;
8456 ctxp = ctxp_for_generation;
8457 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8459 current_class = TREE_TYPE (current);
8460 if (ANONYMOUS_CLASS_P (current_class))
8462 tree d;
8463 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8465 if (DECL_CONSTRUCTOR_P (d))
8467 restore_line_number_status (1);
8468 reset_method_name (d);
8469 java_complete_expand_method (d);
8470 restore_line_number_status (0);
8471 break; /* We now there are no other ones */
8478 /* If we've found error at that stage, don't try to generate
8479 anything, unless we're emitting xrefs or checking the syntax only
8480 (but not using -fsyntax-only for the purpose of generating
8481 bytecode. */
8482 if (java_error_count && !flag_emit_xref
8483 && (!flag_syntax_only && !flag_emit_class_files))
8484 return;
8486 /* Now things are stable, go for generation of the class data. */
8487 for (ctxp_for_generation = saved_ctxp;
8488 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8490 tree current;
8491 ctxp = ctxp_for_generation;
8492 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8494 current_class = TREE_TYPE (current);
8495 outgoing_cpool = TYPE_CPOOL (current_class);
8496 if (flag_emit_class_files)
8497 write_classfile (current_class);
8498 if (flag_emit_xref)
8499 expand_xref (current_class);
8500 else if (! flag_syntax_only)
8501 finish_class ();
8506 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8507 a tree list node containing RIGHT. Fore coming RIGHTs will be
8508 chained to this hook. LOCATION contains the location of the
8509 separating `.' operator. */
8511 static tree
8512 make_qualified_primary (primary, right, location)
8513 tree primary, right;
8514 int location;
8516 tree wfl;
8518 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8519 wfl = build_wfl_wrap (primary, location);
8520 else
8522 wfl = primary;
8523 /* If wfl wasn't qualified, we build a first anchor */
8524 if (!EXPR_WFL_QUALIFICATION (wfl))
8525 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8528 /* And chain them */
8529 EXPR_WFL_LINECOL (right) = location;
8530 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8531 PRIMARY_P (wfl) = 1;
8532 return wfl;
8535 /* Simple merge of two name separated by a `.' */
8537 static tree
8538 merge_qualified_name (left, right)
8539 tree left, right;
8541 tree node;
8542 if (!left && !right)
8543 return NULL_TREE;
8545 if (!left)
8546 return right;
8548 if (!right)
8549 return left;
8551 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8552 IDENTIFIER_LENGTH (left));
8553 obstack_1grow (&temporary_obstack, '.');
8554 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8555 IDENTIFIER_LENGTH (right));
8556 node = get_identifier (obstack_base (&temporary_obstack));
8557 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8558 QUALIFIED_P (node) = 1;
8559 return node;
8562 /* Merge the two parts of a qualified name into LEFT. Set the
8563 location information of the resulting node to LOCATION, usually
8564 inherited from the location information of the `.' operator. */
8566 static tree
8567 make_qualified_name (left, right, location)
8568 tree left, right;
8569 int location;
8571 #ifdef USE_COMPONENT_REF
8572 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8573 EXPR_WFL_LINECOL (node) = location;
8574 return node;
8575 #else
8576 tree left_id = EXPR_WFL_NODE (left);
8577 tree right_id = EXPR_WFL_NODE (right);
8578 tree wfl, merge;
8580 merge = merge_qualified_name (left_id, right_id);
8582 /* Left wasn't qualified and is now qualified */
8583 if (!QUALIFIED_P (left_id))
8585 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8586 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8587 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8590 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8591 EXPR_WFL_LINECOL (wfl) = location;
8592 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8594 EXPR_WFL_NODE (left) = merge;
8595 return left;
8596 #endif
8599 /* Extract the last identifier component of the qualified in WFL. The
8600 last identifier is removed from the linked list */
8602 static tree
8603 cut_identifier_in_qualified (wfl)
8604 tree wfl;
8606 tree q;
8607 tree previous = NULL_TREE;
8608 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8609 if (!TREE_CHAIN (q))
8611 if (!previous)
8612 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
8613 TREE_CHAIN (previous) = NULL_TREE;
8614 return TREE_PURPOSE (q);
8618 /* Resolve the expression name NAME. Return its decl. */
8620 static tree
8621 resolve_expression_name (id, orig)
8622 tree id;
8623 tree *orig;
8625 tree name = EXPR_WFL_NODE (id);
8626 tree decl;
8628 /* 6.5.5.1: Simple expression names */
8629 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8631 /* 15.13.1: NAME can appear within the scope of a local variable
8632 declaration */
8633 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8634 return decl;
8636 /* 15.13.1: NAME can appear within a class declaration */
8637 else
8639 decl = lookup_field_wrapper (current_class, name);
8640 if (decl)
8642 tree access = NULL_TREE;
8643 int fs = FIELD_STATIC (decl);
8645 /* If we're accessing an outer scope local alias, make
8646 sure we change the name of the field we're going to
8647 build access to. */
8648 if (FIELD_LOCAL_ALIAS_USED (decl))
8649 name = DECL_NAME (decl);
8651 /* Instance variable (8.3.1.1) can't appear within
8652 static method, static initializer or initializer for
8653 a static variable. */
8654 if (!fs && METHOD_STATIC (current_function_decl))
8656 static_ref_err (id, name, current_class);
8657 return error_mark_node;
8659 /* Instance variables can't appear as an argument of
8660 an explicit constructor invocation */
8661 if (!fs && ctxp->explicit_constructor_p)
8663 parse_error_context
8664 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
8665 return error_mark_node;
8668 /* If we're processing an inner class and we're trying
8669 to access a field belonging to an outer class, build
8670 the access to the field */
8671 if (!fs && outer_field_access_p (current_class, decl))
8672 return build_outer_field_access (id, decl);
8674 /* Otherwise build what it takes to access the field */
8675 access = build_field_ref ((fs ? NULL_TREE : current_this),
8676 DECL_CONTEXT (decl), name);
8677 if (fs && !flag_emit_class_files && !flag_emit_xref)
8678 access = build_class_init (DECL_CONTEXT (access), access);
8679 /* We may be asked to save the real field access node */
8680 if (orig)
8681 *orig = access;
8682 /* And we return what we got */
8683 return access;
8685 /* Fall down to error report on undefined variable */
8688 /* 6.5.5.2 Qualified Expression Names */
8689 else
8691 if (orig)
8692 *orig = NULL_TREE;
8693 qualify_ambiguous_name (id);
8694 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8695 /* 15.10.2: Accessing Superclass Members using super */
8696 return resolve_field_access (id, orig, NULL);
8699 /* We've got an error here */
8700 parse_error_context (id, "Undefined variable `%s'",
8701 IDENTIFIER_POINTER (name));
8703 return error_mark_node;
8706 static void
8707 static_ref_err (wfl, field_id, class_type)
8708 tree wfl, field_id, class_type;
8710 parse_error_context
8711 (wfl,
8712 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8713 IDENTIFIER_POINTER (field_id),
8714 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8717 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8718 We return something suitable to generate the field access. We also
8719 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8720 recipient's address can be null. */
8722 static tree
8723 resolve_field_access (qual_wfl, field_decl, field_type)
8724 tree qual_wfl;
8725 tree *field_decl, *field_type;
8727 int is_static = 0;
8728 tree field_ref;
8729 tree decl, where_found, type_found;
8731 if (resolve_qualified_expression_name (qual_wfl, &decl,
8732 &where_found, &type_found))
8733 return error_mark_node;
8735 /* Resolve the LENGTH field of an array here */
8736 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
8737 && type_found && TYPE_ARRAY_P (type_found)
8738 && ! flag_emit_class_files && ! flag_emit_xref)
8740 tree length = build_java_array_length_access (where_found);
8741 field_ref =
8742 build_java_arraynull_check (type_found, length, int_type_node);
8744 /* In case we're dealing with a static array, we need to
8745 initialize its class before the array length can be fetched.
8746 It's also a good time to create a DECL_RTL for the field if
8747 none already exists, otherwise if the field was declared in a
8748 class found in an external file and hasn't been (and won't
8749 be) accessed for its value, none will be created. */
8750 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8752 build_static_field_ref (where_found);
8753 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8756 /* We might have been trying to resolve field.method(). In which
8757 case, the resolution is over and decl is the answer */
8758 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
8759 field_ref = decl;
8760 else if (JDECL_P (decl))
8762 int static_final_found = 0;
8763 if (!type_found)
8764 type_found = DECL_CONTEXT (decl);
8765 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
8766 if (FIELD_FINAL (decl) && FIELD_STATIC (decl)
8767 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
8768 && DECL_INITIAL (decl))
8770 /* When called on a FIELD_DECL of the right (primitive)
8771 type, java_complete_tree will try to substitue the decl
8772 for it's initial value. */
8773 field_ref = java_complete_tree (decl);
8774 static_final_found = 1;
8776 else
8777 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8778 NULL_TREE : where_found),
8779 type_found, DECL_NAME (decl));
8780 if (field_ref == error_mark_node)
8781 return error_mark_node;
8782 if (is_static && !static_final_found
8783 && !flag_emit_class_files && !flag_emit_xref)
8784 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
8786 else
8787 field_ref = decl;
8789 if (field_decl)
8790 *field_decl = decl;
8791 if (field_type)
8792 *field_type = (QUAL_DECL_TYPE (decl) ?
8793 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
8794 return field_ref;
8797 /* If NODE is an access to f static field, strip out the class
8798 initialization part and return the field decl, otherwise, return
8799 NODE. */
8801 static tree
8802 strip_out_static_field_access_decl (node)
8803 tree node;
8805 if (TREE_CODE (node) == COMPOUND_EXPR)
8807 tree op1 = TREE_OPERAND (node, 1);
8808 if (TREE_CODE (op1) == COMPOUND_EXPR)
8810 tree call = TREE_OPERAND (op1, 0);
8811 if (TREE_CODE (call) == CALL_EXPR
8812 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8813 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8814 == soft_initclass_node)
8815 return TREE_OPERAND (op1, 1);
8817 else if (JDECL_P (op1))
8818 return op1;
8820 return node;
8823 /* 6.5.5.2: Qualified Expression Names */
8825 static int
8826 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8827 tree wfl;
8828 tree *found_decl, *type_found, *where_found;
8830 int from_type = 0; /* Field search initiated from a type */
8831 int from_super = 0, from_cast = 0, from_qualified_this = 0;
8832 int previous_call_static = 0;
8833 int is_static;
8834 tree decl = NULL_TREE, type = NULL_TREE, q;
8835 /* For certain for of inner class instantiation */
8836 tree saved_current, saved_this;
8837 #define RESTORE_THIS_AND_CURRENT_CLASS \
8838 { current_class = saved_current; current_this = saved_this;}
8840 *type_found = *where_found = NULL_TREE;
8842 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8844 tree qual_wfl = QUAL_WFL (q);
8845 tree ret_decl; /* for EH checking */
8846 int location; /* for EH checking */
8848 /* 15.10.1 Field Access Using a Primary */
8849 switch (TREE_CODE (qual_wfl))
8851 case CALL_EXPR:
8852 case NEW_CLASS_EXPR:
8853 /* If the access to the function call is a non static field,
8854 build the code to access it. */
8855 if (JDECL_P (decl) && !FIELD_STATIC (decl))
8857 decl = maybe_access_field (decl, *where_found,
8858 DECL_CONTEXT (decl));
8859 if (decl == error_mark_node)
8860 return 1;
8863 /* And code for the function call */
8864 if (complete_function_arguments (qual_wfl))
8865 return 1;
8867 /* We might have to setup a new current class and a new this
8868 for the search of an inner class, relative to the type of
8869 a expression resolved as `decl'. The current values are
8870 saved and restored shortly after */
8871 saved_current = current_class;
8872 saved_this = current_this;
8873 if (decl && TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
8875 current_class = type;
8876 current_this = decl;
8879 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
8880 CALL_USING_SUPER (qual_wfl) = 1;
8881 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
8882 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
8883 *where_found = patch_method_invocation (qual_wfl, decl, type,
8884 &is_static, &ret_decl);
8885 if (*where_found == error_mark_node)
8887 RESTORE_THIS_AND_CURRENT_CLASS;
8888 return 1;
8890 *type_found = type = QUAL_DECL_TYPE (*where_found);
8892 /* If we're creating an inner class instance, check for that
8893 an enclosing instance is in scope */
8894 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
8895 && INNER_ENCLOSING_SCOPE_CHECK (type))
8897 parse_error_context
8898 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
8899 lang_printable_name (type, 0),
8900 (!current_this ? "" :
8901 "; an explicit one must be provided when creating this inner class"));
8902 RESTORE_THIS_AND_CURRENT_CLASS;
8903 return 1;
8906 /* In case we had to change then to resolve a inner class
8907 instantiation using a primary qualified by a `new' */
8908 RESTORE_THIS_AND_CURRENT_CLASS;
8910 /* EH check */
8911 if (location)
8912 check_thrown_exceptions (location, ret_decl);
8914 /* If the previous call was static and this one is too,
8915 build a compound expression to hold the two (because in
8916 that case, previous function calls aren't transported as
8917 forcoming function's argument. */
8918 if (previous_call_static && is_static)
8920 decl = build (COMPOUND_EXPR, type, decl, *where_found);
8921 TREE_SIDE_EFFECTS (decl) = 1;
8923 else
8925 previous_call_static = is_static;
8926 decl = *where_found;
8928 from_type = 0;
8929 continue;
8931 case NEW_ARRAY_EXPR:
8932 case NEW_ANONYMOUS_ARRAY_EXPR:
8933 *where_found = decl = java_complete_tree (qual_wfl);
8934 if (decl == error_mark_node)
8935 return 1;
8936 *type_found = type = QUAL_DECL_TYPE (decl);
8937 CLASS_LOADED_P (type) = 1;
8938 continue;
8940 case CONVERT_EXPR:
8941 *where_found = decl = java_complete_tree (qual_wfl);
8942 if (decl == error_mark_node)
8943 return 1;
8944 *type_found = type = QUAL_DECL_TYPE (decl);
8945 from_cast = 1;
8946 continue;
8948 case CONDITIONAL_EXPR:
8949 case STRING_CST:
8950 case MODIFY_EXPR:
8951 *where_found = decl = java_complete_tree (qual_wfl);
8952 if (decl == error_mark_node)
8953 return 1;
8954 *type_found = type = QUAL_DECL_TYPE (decl);
8955 continue;
8957 case ARRAY_REF:
8958 /* If the access to the function call is a non static field,
8959 build the code to access it. */
8960 if (JDECL_P (decl) && !FIELD_STATIC (decl))
8962 decl = maybe_access_field (decl, *where_found, type);
8963 if (decl == error_mark_node)
8964 return 1;
8966 /* And code for the array reference expression */
8967 decl = java_complete_tree (qual_wfl);
8968 if (decl == error_mark_node)
8969 return 1;
8970 type = QUAL_DECL_TYPE (decl);
8971 continue;
8973 case PLUS_EXPR:
8974 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
8975 return 1;
8976 if ((type = patch_string (decl)))
8977 decl = type;
8978 *where_found = QUAL_RESOLUTION (q) = decl;
8979 *type_found = type = TREE_TYPE (decl);
8980 break;
8982 case CLASS_LITERAL:
8983 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
8984 return 1;
8985 *where_found = QUAL_RESOLUTION (q) = decl;
8986 *type_found = type = TREE_TYPE (decl);
8987 break;
8989 default:
8990 /* Fix for -Wall Just go to the next statement. Don't
8991 continue */
8992 break;
8995 /* If we fall here, we weren't processing a (static) function call. */
8996 previous_call_static = 0;
8998 /* It can be the keyword THIS */
8999 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9001 if (!current_this)
9003 parse_error_context
9004 (wfl, "Keyword `this' used outside allowed context");
9005 return 1;
9007 if (ctxp->explicit_constructor_p)
9009 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9010 return 1;
9012 /* We have to generate code for intermediate acess */
9013 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9015 *where_found = decl = current_this;
9016 *type_found = type = QUAL_DECL_TYPE (decl);
9018 /* We're trying to access the this from somewhere else. Make sure
9019 it's allowed before doing so. */
9020 else
9022 if (!enclosing_context_p (type, current_class))
9024 char *p = xstrdup (lang_printable_name (type, 0));
9025 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9026 p, p,
9027 lang_printable_name (current_class, 0));
9028 free (p);
9029 return 1;
9031 *where_found = decl = build_current_thisn (type);
9032 from_qualified_this = 1;
9035 from_type = 0;
9036 continue;
9039 /* 15.10.2 Accessing Superclass Members using SUPER */
9040 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9042 tree node;
9043 /* Check on the restricted use of SUPER */
9044 if (METHOD_STATIC (current_function_decl)
9045 || current_class == object_type_node)
9047 parse_error_context
9048 (wfl, "Keyword `super' used outside allowed context");
9049 return 1;
9051 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9052 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9053 CLASSTYPE_SUPER (current_class),
9054 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9055 *where_found = decl = java_complete_tree (node);
9056 if (decl == error_mark_node)
9057 return 1;
9058 *type_found = type = QUAL_DECL_TYPE (decl);
9059 from_super = from_type = 1;
9060 continue;
9063 /* 15.13.1: Can't search for field name in packages, so we
9064 assume a variable/class name was meant. */
9065 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9067 tree name = resolve_package (wfl, &q);
9068 if (name)
9070 tree list;
9071 *where_found = decl = resolve_no_layout (name, qual_wfl);
9072 /* We want to be absolutely sure that the class is laid
9073 out. We're going to search something inside it. */
9074 *type_found = type = TREE_TYPE (decl);
9075 layout_class (type);
9076 from_type = 1;
9078 /* Fix them all the way down, if any are left. */
9079 if (q)
9081 list = TREE_CHAIN (q);
9082 while (list)
9084 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9085 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9086 list = TREE_CHAIN (list);
9090 else
9092 if (from_super || from_cast)
9093 parse_error_context
9094 ((from_cast ? qual_wfl : wfl),
9095 "No variable `%s' defined in class `%s'",
9096 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9097 lang_printable_name (type, 0));
9098 else
9099 parse_error_context
9100 (qual_wfl, "Undefined variable or class name: `%s'",
9101 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9102 return 1;
9106 /* We have a type name. It's been already resolved when the
9107 expression was qualified. */
9108 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9110 if (!(decl = QUAL_RESOLUTION (q)))
9111 return 1; /* Error reported already */
9113 /* Sneak preview. If next we see a `new', we're facing a
9114 qualification with resulted in a type being selected
9115 instead of a field. Report the error */
9116 if(TREE_CHAIN (q)
9117 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9119 parse_error_context (qual_wfl, "Undefined variable `%s'",
9120 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9121 return 1;
9124 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9126 parse_error_context
9127 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9128 java_accstring_lookup (get_access_flags_from_decl (decl)),
9129 GET_TYPE_NAME (type),
9130 IDENTIFIER_POINTER (DECL_NAME (decl)),
9131 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9132 return 1;
9134 check_deprecation (qual_wfl, decl);
9136 type = TREE_TYPE (decl);
9137 from_type = 1;
9139 /* We resolve and expression name */
9140 else
9142 tree field_decl = NULL_TREE;
9144 /* If there exists an early resolution, use it. That occurs
9145 only once and we know that there are more things to
9146 come. Don't do that when processing something after SUPER
9147 (we need more thing to be put in place below */
9148 if (!from_super && QUAL_RESOLUTION (q))
9150 decl = QUAL_RESOLUTION (q);
9151 if (!type)
9153 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9155 if (current_this)
9156 *where_found = current_this;
9157 else
9159 static_ref_err (qual_wfl, DECL_NAME (decl),
9160 current_class);
9161 return 1;
9164 else
9166 *where_found = TREE_TYPE (decl);
9167 if (TREE_CODE (*where_found) == POINTER_TYPE)
9168 *where_found = TREE_TYPE (*where_found);
9173 /* We have to search for a field, knowing the type of its
9174 container. The flag FROM_TYPE indicates that we resolved
9175 the last member of the expression as a type name, which
9176 means that for the resolution of this field, we'll look
9177 for other errors than if it was resolved as a member of
9178 an other field. */
9179 else
9181 int is_static;
9182 tree field_decl_type; /* For layout */
9184 if (!from_type && !JREFERENCE_TYPE_P (type))
9186 parse_error_context
9187 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9188 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9189 lang_printable_name (type, 0),
9190 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9191 return 1;
9194 field_decl = lookup_field_wrapper (type,
9195 EXPR_WFL_NODE (qual_wfl));
9197 /* Maybe what we're trying to access an inner class. */
9198 if (!field_decl)
9200 tree ptr, inner_decl;
9202 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9203 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9204 if (inner_decl)
9206 check_inner_class_access (inner_decl, decl, qual_wfl);
9207 type = TREE_TYPE (inner_decl);
9208 decl = inner_decl;
9209 from_type = 1;
9210 continue;
9214 if (field_decl == NULL_TREE)
9216 parse_error_context
9217 (qual_wfl, "No variable `%s' defined in type `%s'",
9218 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9219 GET_TYPE_NAME (type));
9220 return 1;
9222 if (field_decl == error_mark_node)
9223 return 1;
9225 /* Layout the type of field_decl, since we may need
9226 it. Don't do primitive types or loaded classes. The
9227 situation of non primitive arrays may not handled
9228 properly here. FIXME */
9229 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9230 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9231 else
9232 field_decl_type = TREE_TYPE (field_decl);
9233 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9234 && !CLASS_LOADED_P (field_decl_type)
9235 && !TYPE_ARRAY_P (field_decl_type))
9236 resolve_and_layout (field_decl_type, NULL_TREE);
9237 if (TYPE_ARRAY_P (field_decl_type))
9238 CLASS_LOADED_P (field_decl_type) = 1;
9240 /* Check on accessibility here */
9241 if (not_accessible_p (type, field_decl, from_super))
9243 parse_error_context
9244 (qual_wfl,
9245 "Can't access %s field `%s.%s' from `%s'",
9246 java_accstring_lookup
9247 (get_access_flags_from_decl (field_decl)),
9248 GET_TYPE_NAME (type),
9249 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9250 IDENTIFIER_POINTER
9251 (DECL_NAME (TYPE_NAME (current_class))));
9252 return 1;
9254 check_deprecation (qual_wfl, field_decl);
9256 /* There are things to check when fields are accessed
9257 from type. There are no restrictions on a static
9258 declaration of the field when it is accessed from an
9259 interface */
9260 is_static = FIELD_STATIC (field_decl);
9261 if (!from_super && from_type
9262 && !TYPE_INTERFACE_P (type)
9263 && !is_static
9264 && (current_function_decl
9265 && METHOD_STATIC (current_function_decl)))
9267 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9268 return 1;
9270 from_cast = from_super = 0;
9272 /* It's an access from a type but it isn't static, we
9273 make it relative to `this'. */
9274 if (!is_static && from_type)
9275 decl = current_this;
9277 /* If we need to generate something to get a proper
9278 handle on what this field is accessed from, do it
9279 now. */
9280 if (!is_static)
9282 decl = maybe_access_field (decl, *where_found, *type_found);
9283 if (decl == error_mark_node)
9284 return 1;
9287 /* We want to keep the location were found it, and the type
9288 we found. */
9289 *where_found = decl;
9290 *type_found = type;
9292 /* Generate the correct expression for field access from
9293 qualified this */
9294 if (from_qualified_this)
9296 field_decl = build_outer_field_access (qual_wfl, field_decl);
9297 from_qualified_this = 0;
9300 /* This is the decl found and eventually the next one to
9301 search from */
9302 decl = field_decl;
9304 from_type = 0;
9305 type = QUAL_DECL_TYPE (decl);
9307 /* Sneak preview. If decl is qualified by a `new', report
9308 the error here to be accurate on the peculiar construct */
9309 if (TREE_CHAIN (q)
9310 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9311 && !JREFERENCE_TYPE_P (type))
9313 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9314 lang_printable_name (type, 0));
9315 return 1;
9318 /* `q' might have changed due to a after package resolution
9319 re-qualification */
9320 if (!q)
9321 break;
9323 *found_decl = decl;
9324 return 0;
9327 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9328 can't be accessed from REFERENCE (a record type). This should be
9329 used when decl is a field or a method.*/
9331 static int
9332 not_accessible_p (reference, member, from_super)
9333 tree reference, member;
9334 int from_super;
9336 int access_flag = get_access_flags_from_decl (member);
9338 /* Inner classes are processed by check_inner_class_access */
9339 if (INNER_CLASS_TYPE_P (reference))
9340 return 0;
9342 /* Access always granted for members declared public */
9343 if (access_flag & ACC_PUBLIC)
9344 return 0;
9346 /* Check access on protected members */
9347 if (access_flag & ACC_PROTECTED)
9349 /* Access granted if it occurs from within the package
9350 containing the class in which the protected member is
9351 declared */
9352 if (class_in_current_package (DECL_CONTEXT (member)))
9353 return 0;
9355 /* If accessed with the form `super.member', then access is granted */
9356 if (from_super)
9357 return 0;
9359 /* Otherwise, access is granted if occuring from the class where
9360 member is declared or a subclass of it. Find the right
9361 context to perform the check */
9362 if (PURE_INNER_CLASS_TYPE_P (reference))
9364 while (INNER_CLASS_TYPE_P (reference))
9366 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9367 return 0;
9368 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9371 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9372 return 0;
9373 return 1;
9376 /* Check access on private members. Access is granted only if it
9377 occurs from within the class in which it is declared. Exceptions
9378 are accesses from inner-classes. */
9379 if (access_flag & ACC_PRIVATE)
9380 return (current_class == DECL_CONTEXT (member) ? 0 :
9381 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
9383 /* Default access are permitted only when occuring within the
9384 package in which the type (REFERENCE) is declared. In other words,
9385 REFERENCE is defined in the current package */
9386 if (ctxp->package)
9387 return !class_in_current_package (reference);
9389 /* Otherwise, access is granted */
9390 return 0;
9393 /* Test deprecated decl access. */
9394 static void
9395 check_deprecation (wfl, decl)
9396 tree wfl, decl;
9398 const char *file = DECL_SOURCE_FILE (decl);
9399 /* Complain if the field is deprecated and the file it was defined
9400 in isn't compiled at the same time the file which contains its
9401 use is */
9402 if (DECL_DEPRECATED (decl)
9403 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9405 char the [20];
9406 switch (TREE_CODE (decl))
9408 case FUNCTION_DECL:
9409 strcpy (the, "method");
9410 break;
9411 case FIELD_DECL:
9412 strcpy (the, "field");
9413 break;
9414 case TYPE_DECL:
9415 strcpy (the, "class");
9416 break;
9417 default:
9418 fatal ("unexpected DECL code - check_deprecation");
9420 parse_warning_context
9421 (wfl, "The %s `%s' in class `%s' has been deprecated",
9422 the, lang_printable_name (decl, 0),
9423 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9427 /* Returns 1 if class was declared in the current package, 0 otherwise */
9429 static int
9430 class_in_current_package (class)
9431 tree class;
9433 static tree cache = NULL_TREE;
9434 int qualified_flag;
9435 tree left;
9437 if (cache == class)
9438 return 1;
9440 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9442 /* If the current package is empty and the name of CLASS is
9443 qualified, class isn't in the current package. If there is a
9444 current package and the name of the CLASS is not qualified, class
9445 isn't in the current package */
9446 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9447 return 0;
9449 /* If there is not package and the name of CLASS isn't qualified,
9450 they belong to the same unnamed package */
9451 if (!ctxp->package && !qualified_flag)
9452 return 1;
9454 /* Compare the left part of the name of CLASS with the package name */
9455 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9456 if (ctxp->package == left)
9458 cache = class;
9459 return 1;
9461 return 0;
9464 /* This function may generate code to access DECL from WHERE. This is
9465 done only if certain conditions meet. */
9467 static tree
9468 maybe_access_field (decl, where, type)
9469 tree decl, where, type;
9471 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9472 && !FIELD_STATIC (decl))
9473 decl = build_field_ref (where ? where : current_this,
9474 (type ? type : DECL_CONTEXT (decl)),
9475 DECL_NAME (decl));
9476 return decl;
9479 /* Build a method invocation, by patching PATCH. If non NULL
9480 and according to the situation, PRIMARY and WHERE may be
9481 used. IS_STATIC is set to 1 if the invoked function is static. */
9483 static tree
9484 patch_method_invocation (patch, primary, where, is_static, ret_decl)
9485 tree patch, primary, where;
9486 int *is_static;
9487 tree *ret_decl;
9489 tree wfl = TREE_OPERAND (patch, 0);
9490 tree args = TREE_OPERAND (patch, 1);
9491 tree name = EXPR_WFL_NODE (wfl);
9492 tree list;
9493 int is_static_flag = 0;
9494 int is_super_init = 0;
9495 tree this_arg = NULL_TREE;
9497 /* Should be overriden if everything goes well. Otherwise, if
9498 something fails, it should keep this value. It stop the
9499 evaluation of a bogus assignment. See java_complete_tree,
9500 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9501 evaluating an assignment */
9502 TREE_TYPE (patch) = error_mark_node;
9504 /* Since lookup functions are messing with line numbers, save the
9505 context now. */
9506 java_parser_context_save_global ();
9508 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9510 /* Resolution of qualified name, excluding constructors */
9511 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9513 tree identifier, identifier_wfl, type, resolved;
9514 /* Extract the last IDENTIFIER of the qualified
9515 expression. This is a wfl and we will use it's location
9516 data during error report. */
9517 identifier_wfl = cut_identifier_in_qualified (wfl);
9518 identifier = EXPR_WFL_NODE (identifier_wfl);
9520 /* Given the context, IDENTIFIER is syntactically qualified
9521 as a MethodName. We need to qualify what's before */
9522 qualify_ambiguous_name (wfl);
9523 resolved = resolve_field_access (wfl, NULL, NULL);
9525 if (resolved == error_mark_node)
9526 PATCH_METHOD_RETURN_ERROR ();
9528 type = GET_SKIP_TYPE (resolved);
9529 resolve_and_layout (type, NULL_TREE);
9531 if (JPRIMITIVE_TYPE_P (type))
9533 parse_error_context
9534 (identifier_wfl,
9535 "Can't invoke a method on primitive type `%s'",
9536 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9537 PATCH_METHOD_RETURN_ERROR ();
9540 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9541 args = nreverse (args);
9543 /* We're resolving a call from a type */
9544 if (TREE_CODE (resolved) == TYPE_DECL)
9546 if (CLASS_INTERFACE (resolved))
9548 parse_error_context
9549 (identifier_wfl,
9550 "Can't make static reference to method `%s' in interface `%s'",
9551 IDENTIFIER_POINTER (identifier),
9552 IDENTIFIER_POINTER (name));
9553 PATCH_METHOD_RETURN_ERROR ();
9555 if (list && !METHOD_STATIC (list))
9557 char *fct_name = xstrdup (lang_printable_name (list, 0));
9558 parse_error_context
9559 (identifier_wfl,
9560 "Can't make static reference to method `%s %s' in class `%s'",
9561 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9562 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9563 free (fct_name);
9564 PATCH_METHOD_RETURN_ERROR ();
9567 else
9568 this_arg = primary = resolved;
9570 /* IDENTIFIER_WFL will be used to report any problem further */
9571 wfl = identifier_wfl;
9573 /* Resolution of simple names, names generated after a primary: or
9574 constructors */
9575 else
9577 tree class_to_search = NULL_TREE;
9578 int lc; /* Looking for Constructor */
9580 /* We search constructor in their target class */
9581 if (CALL_CONSTRUCTOR_P (patch))
9583 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9584 class_to_search = EXPR_WFL_NODE (wfl);
9585 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9586 this_identifier_node)
9587 class_to_search = NULL_TREE;
9588 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9589 super_identifier_node)
9591 is_super_init = 1;
9592 if (CLASSTYPE_SUPER (current_class))
9593 class_to_search =
9594 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9595 else
9597 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
9598 PATCH_METHOD_RETURN_ERROR ();
9602 /* Class to search is NULL if we're searching the current one */
9603 if (class_to_search)
9605 class_to_search = resolve_and_layout (class_to_search, wfl);
9607 if (!class_to_search)
9609 parse_error_context
9610 (wfl, "Class `%s' not found in type declaration",
9611 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9612 PATCH_METHOD_RETURN_ERROR ();
9615 /* Can't instantiate an abstract class, but we can
9616 invoke it's constructor. It's use within the `new'
9617 context is denied here. */
9618 if (CLASS_ABSTRACT (class_to_search)
9619 && TREE_CODE (patch) == NEW_CLASS_EXPR)
9621 parse_error_context
9622 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9623 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9624 PATCH_METHOD_RETURN_ERROR ();
9627 class_to_search = TREE_TYPE (class_to_search);
9629 else
9630 class_to_search = current_class;
9631 lc = 1;
9633 /* This is a regular search in the local class, unless an
9634 alternate class is specified. */
9635 else
9637 class_to_search = (where ? where : current_class);
9638 lc = 0;
9641 /* NAME is a simple identifier or comes from a primary. Search
9642 in the class whose declaration contain the method being
9643 invoked. */
9644 resolve_and_layout (class_to_search, NULL_TREE);
9646 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
9647 /* Don't continue if no method were found, as the next statement
9648 can't be executed then. */
9649 if (!list)
9650 PATCH_METHOD_RETURN_ERROR ();
9652 /* Check for static reference if non static methods */
9653 if (check_for_static_method_reference (wfl, patch, list,
9654 class_to_search, primary))
9655 PATCH_METHOD_RETURN_ERROR ();
9657 /* Check for inner classes creation from illegal contexts */
9658 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9659 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9660 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9662 parse_error_context
9663 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9664 lang_printable_name (class_to_search, 0),
9665 (!current_this ? "" :
9666 "; an explicit one must be provided when creating this inner class"));
9667 PATCH_METHOD_RETURN_ERROR ();
9670 /* Non static methods are called with the current object extra
9671 argument. If patch a `new TYPE()', the argument is the value
9672 returned by the object allocator. If method is resolved as a
9673 primary, use the primary otherwise use the current THIS. */
9674 args = nreverse (args);
9675 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
9677 this_arg = primary ? primary : current_this;
9679 /* If we're using an access method, things are different.
9680 There are two familly of cases:
9682 1) We're not generating bytecodes:
9684 - LIST is non static. It's invocation is transformed from
9685 x(a1,...,an) into this$<n>.x(a1,....an).
9686 - LIST is static. It's invocation is transformed from
9687 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9689 2) We're generating bytecodes:
9691 - LIST is non static. It's invocation is transformed from
9692 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9693 - LIST is static. It's invocation is transformed from
9694 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9696 Of course, this$<n> can be abitrary complex, ranging from
9697 this$0 (the immediate outer context) to
9698 access$0(access$0(...(this$0))).
9700 maybe_use_access_method returns a non zero value if the
9701 this_arg has to be moved into the (then generated) stub
9702 argument list. In the meantime, the selected function
9703 might have be replaced by a generated stub. */
9704 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9705 args = tree_cons (NULL_TREE, this_arg, args);
9709 /* Merge point of all resolution schemes. If we have nothing, this
9710 is an error, already signaled */
9711 if (!list)
9712 PATCH_METHOD_RETURN_ERROR ();
9714 /* Check accessibility, position the is_static flag, build and
9715 return the call */
9716 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
9718 char *fct_name = xstrdup (lang_printable_name (list, 0));
9719 parse_error_context
9720 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9721 java_accstring_lookup (get_access_flags_from_decl (list)),
9722 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9723 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9724 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9725 free (fct_name);
9726 PATCH_METHOD_RETURN_ERROR ();
9728 check_deprecation (wfl, list);
9730 /* If invoking a innerclass constructor, there are hidden parameters
9731 to pass */
9732 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9733 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9735 /* And make sure we add the accessed local variables to be saved
9736 in field aliases. */
9737 args = build_alias_initializer_parameter_list
9738 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9740 /* We have to reverse things. Find out why. FIXME */
9741 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (list)))
9742 args = nreverse (args);
9744 /* Secretely pass the current_this/primary as a second argument */
9745 if (primary || current_this)
9746 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9747 else
9748 args = tree_cons (NULL_TREE, integer_zero_node, args);
9751 /* This handles the situation where a constructor invocation needs
9752 to have an enclosing context passed as a second parameter (the
9753 constructor is one of an inner class. We extract it from the
9754 current function. */
9755 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9757 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9758 tree extra_arg;
9760 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9762 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9763 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9765 else
9767 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9768 extra_arg =
9769 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9770 extra_arg = java_complete_tree (extra_arg);
9772 args = tree_cons (NULL_TREE, extra_arg, args);
9775 is_static_flag = METHOD_STATIC (list);
9776 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
9777 args = tree_cons (NULL_TREE, this_arg, args);
9779 /* In the context of an explicit constructor invocation, we can't
9780 invoke any method relying on `this'. Exceptions are: we're
9781 invoking a static function, primary exists and is not the current
9782 this, we're creating a new object. */
9783 if (ctxp->explicit_constructor_p
9784 && !is_static_flag
9785 && (!primary || primary == current_this)
9786 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
9788 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9789 PATCH_METHOD_RETURN_ERROR ();
9791 java_parser_context_restore_global ();
9792 if (is_static)
9793 *is_static = is_static_flag;
9794 /* Sometimes, we want the decl of the selected method. Such as for
9795 EH checking */
9796 if (ret_decl)
9797 *ret_decl = list;
9798 patch = patch_invoke (patch, list, args);
9799 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
9801 tree finit_parms, finit_call;
9803 /* Prepare to pass hidden parameters to $finit$, if any. */
9804 finit_parms = build_alias_initializer_parameter_list
9805 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
9807 finit_call =
9808 build_method_invocation (build_wfl_node (finit_identifier_node),
9809 finit_parms);
9811 /* Generate the code used to initialize fields declared with an
9812 initialization statement and build a compound statement along
9813 with the super constructor invocation. */
9814 patch = build (COMPOUND_EXPR, void_type_node, patch,
9815 java_complete_tree (finit_call));
9816 CAN_COMPLETE_NORMALLY (patch) = 1;
9818 return patch;
9821 /* Check that we're not trying to do a static reference to a method in
9822 non static method. Return 1 if it's the case, 0 otherwise. */
9824 static int
9825 check_for_static_method_reference (wfl, node, method, where, primary)
9826 tree wfl, node, method, where, primary;
9828 if (METHOD_STATIC (current_function_decl)
9829 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
9831 char *fct_name = xstrdup (lang_printable_name (method, 0));
9832 parse_error_context
9833 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
9834 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
9835 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
9836 free (fct_name);
9837 return 1;
9839 return 0;
9842 /* Fix the invocation of *MDECL if necessary in the case of a
9843 invocation from an inner class. *THIS_ARG might be modified
9844 appropriately and an alternative access to *MDECL might be
9845 returned. */
9847 static int
9848 maybe_use_access_method (is_super_init, mdecl, this_arg)
9849 int is_super_init;
9850 tree *mdecl, *this_arg;
9852 tree ctx;
9853 tree md = *mdecl, ta = *this_arg;
9854 int to_return = 0;
9855 int non_static_context = !METHOD_STATIC (md);
9857 if (is_super_init
9858 || DECL_CONTEXT (md) == current_class
9859 || !PURE_INNER_CLASS_TYPE_P (current_class)
9860 || DECL_FINIT_P (md))
9861 return 0;
9863 /* If we're calling a method found in an enclosing class, generate
9864 what it takes to retrieve the right this. Don't do that if we're
9865 invoking a static method. */
9867 if (non_static_context)
9869 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
9870 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
9872 ta = build_current_thisn (current_class);
9873 ta = build_wfl_node (ta);
9875 else
9877 tree type = ctx;
9878 while (type)
9880 maybe_build_thisn_access_method (type);
9881 if (inherits_from_p (type, DECL_CONTEXT (md)))
9883 ta = build_access_to_thisn (ctx, type, 0);
9884 break;
9886 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
9887 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
9890 ta = java_complete_tree (ta);
9893 /* We might have to use an access method to get to MD. We can
9894 break the method access rule as far as we're not generating
9895 bytecode */
9896 if (METHOD_PRIVATE (md) && flag_emit_class_files)
9898 md = build_outer_method_access_method (md);
9899 to_return = 1;
9902 *mdecl = md;
9903 *this_arg = ta;
9905 /* Returnin a non zero value indicates we were doing a non static
9906 method invokation that is now a static invocation. It will have
9907 callee displace `this' to insert it in the regular argument
9908 list. */
9909 return (non_static_context && to_return);
9912 /* Patch an invoke expression METHOD and ARGS, based on its invocation
9913 mode. */
9915 static tree
9916 patch_invoke (patch, method, args)
9917 tree patch, method, args;
9919 tree dtable, func;
9920 tree original_call, t, ta;
9921 tree cond = NULL_TREE;
9923 /* Last step for args: convert build-in types. If we're dealing with
9924 a new TYPE() type call, the first argument to the constructor
9925 isn't found in the incoming argument list, but delivered by
9926 `new' */
9927 t = TYPE_ARG_TYPES (TREE_TYPE (method));
9928 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9929 t = TREE_CHAIN (t);
9930 for (ta = args; t != end_params_node && ta;
9931 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
9932 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
9933 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
9934 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
9936 /* Resolve unresolved returned type isses */
9937 t = TREE_TYPE (TREE_TYPE (method));
9938 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
9939 resolve_and_layout (TREE_TYPE (t), NULL);
9941 if (flag_emit_class_files || flag_emit_xref)
9942 func = method;
9943 else
9945 tree signature = build_java_signature (TREE_TYPE (method));
9946 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
9948 case INVOKE_VIRTUAL:
9949 dtable = invoke_build_dtable (0, args);
9950 func = build_invokevirtual (dtable, method);
9951 break;
9953 case INVOKE_NONVIRTUAL:
9954 /* If the object for the method call is null, we throw an
9955 exception. We don't do this if the object is the current
9956 method's `this'. In other cases we just rely on an
9957 optimization pass to eliminate redundant checks. */
9958 if (TREE_VALUE (args) != current_this)
9960 /* We use a SAVE_EXPR here to make sure we only evaluate
9961 the new `self' expression once. */
9962 tree save_arg = save_expr (TREE_VALUE (args));
9963 TREE_VALUE (args) = save_arg;
9964 cond = build (EQ_EXPR, boolean_type_node, save_arg,
9965 null_pointer_node);
9967 /* Fall through. */
9969 case INVOKE_SUPER:
9970 case INVOKE_STATIC:
9971 func = build_known_method_ref (method, TREE_TYPE (method),
9972 DECL_CONTEXT (method),
9973 signature, args);
9974 break;
9976 case INVOKE_INTERFACE:
9977 dtable = invoke_build_dtable (1, args);
9978 func = build_invokeinterface (dtable, method);
9979 break;
9981 default:
9982 fatal ("internal error - unknown invocation_mode result");
9985 /* Ensure self_type is initialized, (invokestatic). FIXME */
9986 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
9989 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
9990 TREE_OPERAND (patch, 0) = func;
9991 TREE_OPERAND (patch, 1) = args;
9992 original_call = patch;
9994 /* We're processing a `new TYPE ()' form. New is called and its
9995 returned value is the first argument to the constructor. We build
9996 a COMPOUND_EXPR and use saved expression so that the overall NEW
9997 expression value is a pointer to a newly created and initialized
9998 class. */
9999 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10001 tree class = DECL_CONTEXT (method);
10002 tree c1, saved_new, size, new;
10003 if (flag_emit_class_files || flag_emit_xref)
10005 TREE_TYPE (patch) = build_pointer_type (class);
10006 return patch;
10008 if (!TYPE_SIZE (class))
10009 safe_layout_class (class);
10010 size = size_in_bytes (class);
10011 new = build (CALL_EXPR, promote_type (class),
10012 build_address_of (alloc_object_node),
10013 tree_cons (NULL_TREE, build_class_ref (class),
10014 build_tree_list (NULL_TREE,
10015 size_in_bytes (class))),
10016 NULL_TREE);
10017 saved_new = save_expr (new);
10018 c1 = build_tree_list (NULL_TREE, saved_new);
10019 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10020 TREE_OPERAND (original_call, 1) = c1;
10021 TREE_SET_CODE (original_call, CALL_EXPR);
10022 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10025 /* If COND is set, then we are building a check to see if the object
10026 is NULL. */
10027 if (cond != NULL_TREE)
10029 /* We have to make the `then' branch a compound expression to
10030 make the types turn out right. This seems bizarre. */
10031 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10032 build (COMPOUND_EXPR, TREE_TYPE (patch),
10033 build (CALL_EXPR, void_type_node,
10034 build_address_of (soft_nullpointer_node),
10035 NULL_TREE, NULL_TREE),
10036 (FLOAT_TYPE_P (TREE_TYPE (patch))
10037 ? build_real (TREE_TYPE (patch), dconst0)
10038 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10039 integer_zero_node))),
10040 patch);
10041 TREE_SIDE_EFFECTS (patch) = 1;
10044 return patch;
10047 static int
10048 invocation_mode (method, super)
10049 tree method;
10050 int super;
10052 int access = get_access_flags_from_decl (method);
10054 if (super)
10055 return INVOKE_SUPER;
10057 if (access & ACC_STATIC)
10058 return INVOKE_STATIC;
10060 /* We have to look for a constructor before we handle nonvirtual
10061 calls; otherwise the constructor will look nonvirtual. */
10062 if (DECL_CONSTRUCTOR_P (method))
10063 return INVOKE_STATIC;
10065 if (access & ACC_FINAL || access & ACC_PRIVATE)
10066 return INVOKE_NONVIRTUAL;
10068 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10069 return INVOKE_NONVIRTUAL;
10071 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10072 return INVOKE_INTERFACE;
10074 return INVOKE_VIRTUAL;
10077 /* Retrieve a refined list of matching methods. It covers the step
10078 15.11.2 (Compile-Time Step 2) */
10080 static tree
10081 lookup_method_invoke (lc, cl, class, name, arg_list)
10082 int lc;
10083 tree cl;
10084 tree class, name, arg_list;
10086 tree atl = end_params_node; /* Arg Type List */
10087 tree method, signature, list, node;
10088 const char *candidates; /* Used for error report */
10089 char *dup;
10091 /* Fix the arguments */
10092 for (node = arg_list; node; node = TREE_CHAIN (node))
10094 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10095 /* Non primitive type may have to be resolved */
10096 if (!JPRIMITIVE_TYPE_P (current_arg))
10097 resolve_and_layout (current_arg, NULL_TREE);
10098 /* And promoted */
10099 if (TREE_CODE (current_arg) == RECORD_TYPE)
10100 current_arg = promote_type (current_arg);
10101 atl = tree_cons (NULL_TREE, current_arg, atl);
10104 /* Presto. If we're dealing with an anonymous class and a
10105 constructor call, generate the right constructor now, since we
10106 know the arguments' types. */
10108 if (lc && ANONYMOUS_CLASS_P (class))
10109 craft_constructor (TYPE_NAME (class), atl);
10111 /* Find all candidates and then refine the list, searching for the
10112 most specific method. */
10113 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10114 list = find_most_specific_methods_list (list);
10115 if (list && !TREE_CHAIN (list))
10116 return TREE_VALUE (list);
10118 /* Issue an error. List candidates if any. Candidates are listed
10119 only if accessible (non accessible methods may end-up here for
10120 the sake of a better error report). */
10121 candidates = NULL;
10122 if (list)
10124 tree current;
10125 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10126 for (current = list; current; current = TREE_CHAIN (current))
10128 tree cm = TREE_VALUE (current);
10129 char string [4096];
10130 if (!cm || not_accessible_p (class, cm, 0))
10131 continue;
10132 sprintf
10133 (string, " `%s' in `%s'%s",
10134 get_printable_method_name (cm),
10135 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10136 (TREE_CHAIN (current) ? "\n" : ""));
10137 obstack_grow (&temporary_obstack, string, strlen (string));
10139 obstack_1grow (&temporary_obstack, '\0');
10140 candidates = obstack_finish (&temporary_obstack);
10142 /* Issue the error message */
10143 method = make_node (FUNCTION_TYPE);
10144 TYPE_ARG_TYPES (method) = atl;
10145 signature = build_java_argument_signature (method);
10146 dup = xstrdup (lang_printable_name (class, 0));
10147 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10148 (lc ? "constructor" : "method"),
10149 (lc ? dup : IDENTIFIER_POINTER (name)),
10150 IDENTIFIER_POINTER (signature), dup,
10151 (candidates ? candidates : ""));
10152 free (dup);
10153 return NULL_TREE;
10156 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10157 when we're looking for a constructor. */
10159 static tree
10160 find_applicable_accessible_methods_list (lc, class, name, arglist)
10161 int lc;
10162 tree class, name, arglist;
10164 static struct hash_table t, *searched_classes = NULL;
10165 static int search_not_done = 0;
10166 tree list = NULL_TREE, all_list = NULL_TREE;
10168 /* Check the hash table to determine if this class has been searched
10169 already. */
10170 if (searched_classes)
10172 if (hash_lookup (searched_classes,
10173 (const hash_table_key) class, FALSE, NULL))
10174 return NULL;
10176 else
10178 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10179 java_hash_compare_tree_node);
10180 searched_classes = &t;
10183 search_not_done++;
10184 hash_lookup (searched_classes,
10185 (const hash_table_key) class, TRUE, NULL);
10187 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10189 load_class (class, 1);
10190 safe_layout_class (class);
10193 /* Search interfaces */
10194 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10195 && CLASS_INTERFACE (TYPE_NAME (class)))
10197 int i, n;
10198 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10199 search_applicable_methods_list (lc, TYPE_METHODS (class),
10200 name, arglist, &list, &all_list);
10201 n = TREE_VEC_LENGTH (basetype_vec);
10202 for (i = 1; i < n; i++)
10204 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10205 tree rlist;
10207 rlist = find_applicable_accessible_methods_list (lc, t, name,
10208 arglist);
10209 list = chainon (rlist, list);
10212 /* Search classes */
10213 else
10215 tree sc = class;
10216 int seen_inner_class = 0;
10217 search_applicable_methods_list (lc, TYPE_METHODS (class),
10218 name, arglist, &list, &all_list);
10220 /* We must search all interfaces of this class */
10221 if (!lc)
10223 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10224 int n = TREE_VEC_LENGTH (basetype_vec), i;
10225 for (i = 1; i < n; i++)
10227 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10228 if (t != object_type_node)
10230 tree rlist
10231 = find_applicable_accessible_methods_list (lc, t,
10232 name, arglist);
10233 list = chainon (rlist, list);
10238 /* Search enclosing context of inner classes before looking
10239 ancestors up. */
10240 while (!lc && INNER_CLASS_TYPE_P (class))
10242 tree rlist;
10243 seen_inner_class = 1;
10244 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
10245 rlist = find_applicable_accessible_methods_list (lc, class,
10246 name, arglist);
10247 list = chainon (rlist, list);
10250 if (!lc && seen_inner_class
10251 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10252 class = CLASSTYPE_SUPER (sc);
10253 else
10254 class = sc;
10256 /* Search superclass */
10257 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10259 tree rlist;
10260 class = CLASSTYPE_SUPER (class);
10261 rlist = find_applicable_accessible_methods_list (lc, class,
10262 name, arglist);
10263 list = chainon (rlist, list);
10267 search_not_done--;
10269 /* We're done. Reset the searched classes list and finally search
10270 java.lang.Object if it wasn't searched already. */
10271 if (!search_not_done)
10273 if (!lc
10274 && TYPE_METHODS (object_type_node)
10275 && !hash_lookup (searched_classes,
10276 (const hash_table_key) object_type_node,
10277 FALSE, NULL))
10279 search_applicable_methods_list (lc,
10280 TYPE_METHODS (object_type_node),
10281 name, arglist, &list, &all_list);
10283 hash_table_free (searched_classes);
10284 searched_classes = NULL;
10287 /* Either return the list obtained or all selected (but
10288 inaccessible) methods for better error report. */
10289 return (!list ? all_list : list);
10292 /* Effectively search for the appropriate method in method */
10294 static void
10295 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10296 int lc;
10297 tree method, name, arglist;
10298 tree *list, *all_list;
10300 for (; method; method = TREE_CHAIN (method))
10302 /* When dealing with constructor, stop here, otherwise search
10303 other classes */
10304 if (lc && !DECL_CONSTRUCTOR_P (method))
10305 continue;
10306 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10307 || (GET_METHOD_NAME (method) != name)))
10308 continue;
10310 if (argument_types_convertible (method, arglist))
10312 /* Retain accessible methods only */
10313 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10314 method, 0))
10315 *list = tree_cons (NULL_TREE, method, *list);
10316 else
10317 /* Also retain all selected method here */
10318 *all_list = tree_cons (NULL_TREE, method, *list);
10323 /* 15.11.2.2 Choose the Most Specific Method */
10325 static tree
10326 find_most_specific_methods_list (list)
10327 tree list;
10329 int max = 0;
10330 int abstract, candidates;
10331 tree current, new_list = NULL_TREE;
10332 for (current = list; current; current = TREE_CHAIN (current))
10334 tree method;
10335 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10337 for (method = list; method; method = TREE_CHAIN (method))
10339 tree method_v, current_v;
10340 /* Don't test a method against itself */
10341 if (method == current)
10342 continue;
10344 method_v = TREE_VALUE (method);
10345 current_v = TREE_VALUE (current);
10347 /* Compare arguments and location where methods where declared */
10348 if (argument_types_convertible (method_v, current_v))
10350 if (valid_method_invocation_conversion_p
10351 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10352 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10353 && enclosing_context_p (DECL_CONTEXT (method_v),
10354 DECL_CONTEXT (current_v))))
10356 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10357 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10358 max = (v > max ? v : max);
10364 /* Review the list and select the maximally specific methods */
10365 for (current = list, abstract = -1, candidates = -1;
10366 current; current = TREE_CHAIN (current))
10367 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10369 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10370 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10371 candidates++;
10374 /* If we have several and they're all abstract, just pick the
10375 closest one. */
10376 if (candidates > 0 && (candidates == abstract))
10378 new_list = nreverse (new_list);
10379 TREE_CHAIN (new_list) = NULL_TREE;
10382 /* We have several, we couldn't find a most specific, all but one are
10383 abstract, we pick the only non abstract one. */
10384 if (candidates > 0 && !max && (candidates == abstract+1))
10386 for (current = new_list; current; current = TREE_CHAIN (current))
10387 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10389 TREE_CHAIN (current) = NULL_TREE;
10390 new_list = current;
10394 /* If we can't find one, lower expectations and try to gather multiple
10395 maximally specific methods */
10396 while (!new_list && max)
10398 while (--max > 0)
10400 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10401 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10405 return new_list;
10408 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10409 converted by method invocation conversion (5.3) to the type of the
10410 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10411 to change less often than M1. */
10413 static int
10414 argument_types_convertible (m1, m2_or_arglist)
10415 tree m1, m2_or_arglist;
10417 static tree m2_arg_value = NULL_TREE;
10418 static tree m2_arg_cache = NULL_TREE;
10420 register tree m1_arg, m2_arg;
10422 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10424 if (m2_arg_value == m2_or_arglist)
10425 m2_arg = m2_arg_cache;
10426 else
10428 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10429 argument types */
10430 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10432 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10433 if (!METHOD_STATIC (m2_or_arglist))
10434 m2_arg = TREE_CHAIN (m2_arg);
10436 else
10437 m2_arg = m2_or_arglist;
10439 m2_arg_value = m2_or_arglist;
10440 m2_arg_cache = m2_arg;
10443 while (m1_arg != end_params_node && m2_arg != end_params_node)
10445 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
10446 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10447 TREE_VALUE (m2_arg)))
10448 break;
10449 m1_arg = TREE_CHAIN (m1_arg);
10450 m2_arg = TREE_CHAIN (m2_arg);
10452 return m1_arg == end_params_node && m2_arg == end_params_node;
10455 /* Qualification routines */
10457 static void
10458 qualify_ambiguous_name (id)
10459 tree id;
10461 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10462 saved_current_class;
10463 int again, super_found = 0, this_found = 0, new_array_found = 0;
10464 int code;
10466 /* We first qualify the first element, then derive qualification of
10467 others based on the first one. If the first element is qualified
10468 by a resolution (field or type), this resolution is stored in the
10469 QUAL_RESOLUTION of the qual element being examined. We need to
10470 save the current_class since the use of SUPER might change the
10471 its value. */
10472 saved_current_class = current_class;
10473 qual = EXPR_WFL_QUALIFICATION (id);
10474 do {
10476 /* Simple qualified expression feature a qual_wfl that is a
10477 WFL. Expression derived from a primary feature more complicated
10478 things like a CALL_EXPR. Expression from primary need to be
10479 worked out to extract the part on which the qualification will
10480 take place. */
10481 qual_wfl = QUAL_WFL (qual);
10482 switch (TREE_CODE (qual_wfl))
10484 case CALL_EXPR:
10485 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10486 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10488 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10489 qual_wfl = QUAL_WFL (qual);
10491 break;
10492 case NEW_ARRAY_EXPR:
10493 case NEW_ANONYMOUS_ARRAY_EXPR:
10494 qual = TREE_CHAIN (qual);
10495 again = new_array_found = 1;
10496 continue;
10497 case CONVERT_EXPR:
10498 break;
10499 case NEW_CLASS_EXPR:
10500 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10501 break;
10502 case ARRAY_REF:
10503 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10504 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10505 break;
10506 case STRING_CST:
10507 qual = TREE_CHAIN (qual);
10508 qual_wfl = QUAL_WFL (qual);
10509 break;
10510 case CLASS_LITERAL:
10511 qual = TREE_CHAIN (qual);
10512 qual_wfl = QUAL_WFL (qual);
10513 break;
10514 default:
10515 /* Fix for -Wall. Just break doing nothing */
10516 break;
10519 ptr_type = current_class;
10520 again = 0;
10521 code = TREE_CODE (qual_wfl);
10523 /* Pos evaluation: non WFL leading expression nodes */
10524 if (code == CONVERT_EXPR
10525 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10526 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10528 else if (code == INTEGER_CST)
10529 name = qual_wfl;
10531 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
10532 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10533 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10535 else if (code == TREE_LIST)
10536 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10538 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10539 || code == PLUS_EXPR)
10541 qual = TREE_CHAIN (qual);
10542 qual_wfl = QUAL_WFL (qual);
10543 again = 1;
10545 else
10547 name = EXPR_WFL_NODE (qual_wfl);
10548 if (!name)
10550 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10551 again = 1;
10555 /* If we have a THIS (from a primary), we set the context accordingly */
10556 if (name == this_identifier_node)
10558 qual = TREE_CHAIN (qual);
10559 qual_wfl = QUAL_WFL (qual);
10560 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10561 again = 1;
10562 else
10563 name = EXPR_WFL_NODE (qual_wfl);
10564 this_found = 1;
10566 /* If we have a SUPER, we set the context accordingly */
10567 if (name == super_identifier_node)
10569 current_class = CLASSTYPE_SUPER (ptr_type);
10570 /* Check that there is such a thing as a super class. If not,
10571 return. The error will be caught later on, during the
10572 resolution */
10573 if (!current_class)
10575 current_class = saved_current_class;
10576 return;
10578 qual = TREE_CHAIN (qual);
10579 /* Do one more interation to set things up */
10580 super_found = again = 1;
10582 } while (again);
10584 /* If name appears within the scope of a local variable declaration
10585 or parameter declaration, then it is an expression name. We don't
10586 carry this test out if we're in the context of the use of SUPER
10587 or THIS */
10588 if (!this_found && !super_found
10589 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10590 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
10592 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10593 QUAL_RESOLUTION (qual) = decl;
10596 /* If within the class/interface NAME was found to be used there
10597 exists a (possibly inherited) field named NAME, then this is an
10598 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10599 address length, it is OK. */
10600 else if ((decl = lookup_field_wrapper (ptr_type, name))
10601 || (new_array_found && name == length_identifier_node))
10603 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10604 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
10607 /* We reclassify NAME as yielding to a type name resolution if:
10608 - NAME is a class/interface declared within the compilation
10609 unit containing NAME,
10610 - NAME is imported via a single-type-import declaration,
10611 - NAME is declared in an another compilation unit of the package
10612 of the compilation unit containing NAME,
10613 - NAME is declared by exactly on type-import-on-demand declaration
10614 of the compilation unit containing NAME.
10615 - NAME is actually a STRING_CST. */
10616 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10617 || (decl = resolve_and_layout (name, NULL_TREE)))
10619 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10620 QUAL_RESOLUTION (qual) = decl;
10623 /* Method call, array references and cast are expression name */
10624 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
10625 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10626 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
10627 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10629 /* Check here that NAME isn't declared by more than one
10630 type-import-on-demand declaration of the compilation unit
10631 containing NAME. FIXME */
10633 /* Otherwise, NAME is reclassified as a package name */
10634 else
10635 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10637 /* Propagate the qualification accross other components of the
10638 qualified name */
10639 for (qual = TREE_CHAIN (qual); qual;
10640 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10642 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10643 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10644 else
10645 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10648 /* Store the global qualification for the ambiguous part of ID back
10649 into ID fields */
10650 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10651 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10652 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10653 RESOLVE_TYPE_NAME_P (id) = 1;
10654 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10655 RESOLVE_PACKAGE_NAME_P (id) = 1;
10657 /* Restore the current class */
10658 current_class = saved_current_class;
10661 static int
10662 breakdown_qualified (left, right, source)
10663 tree *left, *right, source;
10665 char *p = IDENTIFIER_POINTER (source), *base;
10666 int l = IDENTIFIER_LENGTH (source);
10668 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10669 base = p;
10670 p += (l-1);
10671 while (*p != '.' && p != base)
10672 p--;
10674 /* We didn't find a '.'. Return an error */
10675 if (p == base)
10676 return 1;
10678 *p = '\0';
10679 if (right)
10680 *right = get_identifier (p+1);
10681 *left = get_identifier (IDENTIFIER_POINTER (source));
10682 *p = '.';
10684 return 0;
10687 /* Patch tree nodes in a function body. When a BLOCK is found, push
10688 local variable decls if present.
10689 Same as java_complete_lhs, but does resolve static finals to values. */
10691 static tree
10692 java_complete_tree (node)
10693 tree node;
10695 node = java_complete_lhs (node);
10696 if (JDECL_P (node) && FIELD_STATIC (node) && FIELD_FINAL (node)
10697 && DECL_INITIAL (node) != NULL_TREE
10698 && !flag_emit_xref)
10700 tree value = DECL_INITIAL (node);
10701 DECL_INITIAL (node) = NULL_TREE;
10702 push_obstacks (&permanent_obstack, &permanent_obstack);
10703 value = fold_constant_for_init (value, node);
10704 pop_obstacks ();
10705 DECL_INITIAL (node) = value;
10706 if (value != NULL_TREE)
10708 /* fold_constant_for_init sometimes widen the original type
10709 of the constant (i.e. byte to int.) It's not desirable,
10710 especially if NODE is a function argument. */
10711 if (TREE_CODE (value) == INTEGER_CST
10712 && TREE_TYPE (node) != TREE_TYPE (value))
10713 return convert (TREE_TYPE (node), value);
10714 else
10715 return value;
10718 return node;
10721 static tree
10722 java_stabilize_reference (node)
10723 tree node;
10725 if (TREE_CODE (node) == COMPOUND_EXPR)
10727 tree op0 = TREE_OPERAND (node, 0);
10728 tree op1 = TREE_OPERAND (node, 1);
10729 TREE_OPERAND (node, 0) = save_expr (op0);
10730 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10731 return node;
10733 return stabilize_reference (node);
10736 /* Patch tree nodes in a function body. When a BLOCK is found, push
10737 local variable decls if present.
10738 Same as java_complete_tree, but does not resolve static finals to values. */
10740 static tree
10741 java_complete_lhs (node)
10742 tree node;
10744 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
10745 int flag;
10747 /* CONVERT_EXPR always has its type set, even though it needs to be
10748 worked out. */
10749 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10750 return node;
10752 /* The switch block implements cases processing container nodes
10753 first. Contained nodes are always written back. Leaves come
10754 next and return a value. */
10755 switch (TREE_CODE (node))
10757 case BLOCK:
10759 /* 1- Block section.
10760 Set the local values on decl names so we can identify them
10761 faster when they're referenced. At that stage, identifiers
10762 are legal so we don't check for declaration errors. */
10763 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10765 DECL_CONTEXT (cn) = current_function_decl;
10766 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
10768 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
10769 CAN_COMPLETE_NORMALLY (node) = 1;
10770 else
10772 tree stmt = BLOCK_EXPR_BODY (node);
10773 tree *ptr;
10774 int error_seen = 0;
10775 if (TREE_CODE (stmt) == COMPOUND_EXPR)
10777 /* Re-order from (((A; B); C); ...; Z) to
10778 (A; (B; (C ; (...; Z)))).
10779 This makes it easier to scan the statements left-to-right
10780 without using recursion (which might overflow the stack
10781 if the block has many statements. */
10782 for (;;)
10784 tree left = TREE_OPERAND (stmt, 0);
10785 if (TREE_CODE (left) != COMPOUND_EXPR)
10786 break;
10787 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
10788 TREE_OPERAND (left, 1) = stmt;
10789 stmt = left;
10791 BLOCK_EXPR_BODY (node) = stmt;
10794 /* Now do the actual complete, without deep recursion for
10795 long blocks. */
10796 ptr = &BLOCK_EXPR_BODY (node);
10797 while (TREE_CODE (*ptr) == COMPOUND_EXPR
10798 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
10800 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
10801 tree *next = &TREE_OPERAND (*ptr, 1);
10802 TREE_OPERAND (*ptr, 0) = cur;
10803 if (cur == empty_stmt_node)
10805 /* Optimization; makes it easier to detect empty bodies.
10806 Most useful for <clinit> with all-constant initializer. */
10807 *ptr = *next;
10808 continue;
10810 if (TREE_CODE (cur) == ERROR_MARK)
10811 error_seen++;
10812 else if (! CAN_COMPLETE_NORMALLY (cur))
10814 wfl_op2 = *next;
10815 for (;;)
10817 if (TREE_CODE (wfl_op2) == BLOCK)
10818 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
10819 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
10820 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
10821 else
10822 break;
10824 if (TREE_CODE (wfl_op2) != CASE_EXPR
10825 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
10826 unreachable_stmt_error (*ptr);
10828 ptr = next;
10830 *ptr = java_complete_tree (*ptr);
10832 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
10833 return error_mark_node;
10834 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
10836 /* Turn local bindings to null */
10837 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10838 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
10840 TREE_TYPE (node) = void_type_node;
10841 break;
10843 /* 2- They are expressions but ultimately deal with statements */
10845 case THROW_EXPR:
10846 wfl_op1 = TREE_OPERAND (node, 0);
10847 COMPLETE_CHECK_OP_0 (node);
10848 /* 14.19 A throw statement cannot complete normally. */
10849 CAN_COMPLETE_NORMALLY (node) = 0;
10850 return patch_throw_statement (node, wfl_op1);
10852 case SYNCHRONIZED_EXPR:
10853 wfl_op1 = TREE_OPERAND (node, 0);
10854 return patch_synchronized_statement (node, wfl_op1);
10856 case TRY_EXPR:
10857 return patch_try_statement (node);
10859 case TRY_FINALLY_EXPR:
10860 COMPLETE_CHECK_OP_0 (node);
10861 COMPLETE_CHECK_OP_1 (node);
10862 CAN_COMPLETE_NORMALLY (node)
10863 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
10864 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
10865 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
10866 return node;
10868 case CLEANUP_POINT_EXPR:
10869 COMPLETE_CHECK_OP_0 (node);
10870 TREE_TYPE (node) = void_type_node;
10871 CAN_COMPLETE_NORMALLY (node) =
10872 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
10873 return node;
10875 case WITH_CLEANUP_EXPR:
10876 COMPLETE_CHECK_OP_0 (node);
10877 COMPLETE_CHECK_OP_2 (node);
10878 CAN_COMPLETE_NORMALLY (node) =
10879 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
10880 TREE_TYPE (node) = void_type_node;
10881 return node;
10883 case LABELED_BLOCK_EXPR:
10884 PUSH_LABELED_BLOCK (node);
10885 if (LABELED_BLOCK_BODY (node))
10886 COMPLETE_CHECK_OP_1 (node);
10887 TREE_TYPE (node) = void_type_node;
10888 POP_LABELED_BLOCK ();
10890 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
10892 LABELED_BLOCK_BODY (node) = NULL_TREE;
10893 CAN_COMPLETE_NORMALLY (node) = 1;
10895 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
10896 CAN_COMPLETE_NORMALLY (node) = 1;
10897 return node;
10899 case EXIT_BLOCK_EXPR:
10900 /* We don't complete operand 1, because it's the return value of
10901 the EXIT_BLOCK_EXPR which doesn't exist it Java */
10902 return patch_bc_statement (node);
10904 case CASE_EXPR:
10905 cn = java_complete_tree (TREE_OPERAND (node, 0));
10906 if (cn == error_mark_node)
10907 return cn;
10909 /* First, the case expression must be constant. Values of final
10910 fields are accepted. */
10911 cn = fold (cn);
10912 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
10913 && JDECL_P (TREE_OPERAND (cn, 1))
10914 && FIELD_FINAL (TREE_OPERAND (cn, 1))
10915 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
10917 push_obstacks (&permanent_obstack, &permanent_obstack);
10918 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
10919 TREE_OPERAND (cn, 1));
10920 pop_obstacks ();
10923 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
10925 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10926 parse_error_context (node, "Constant expression required");
10927 return error_mark_node;
10930 nn = ctxp->current_loop;
10932 /* It must be assignable to the type of the switch expression. */
10933 if (!try_builtin_assignconv (NULL_TREE,
10934 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
10936 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10937 parse_error_context
10938 (wfl_operator,
10939 "Incompatible type for case. Can't convert `%s' to `int'",
10940 lang_printable_name (TREE_TYPE (cn), 0));
10941 return error_mark_node;
10944 cn = fold (convert (int_type_node, cn));
10946 /* Multiple instance of a case label bearing the same
10947 value is checked during code generation. The case
10948 expression is allright so far. */
10949 TREE_OPERAND (node, 0) = cn;
10950 TREE_TYPE (node) = void_type_node;
10951 CAN_COMPLETE_NORMALLY (node) = 1;
10952 TREE_SIDE_EFFECTS (node) = 1;
10953 break;
10955 case DEFAULT_EXPR:
10956 nn = ctxp->current_loop;
10957 /* Only one default label is allowed per switch statement */
10958 if (SWITCH_HAS_DEFAULT (nn))
10960 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10961 parse_error_context (wfl_operator,
10962 "Duplicate case label: `default'");
10963 return error_mark_node;
10965 else
10966 SWITCH_HAS_DEFAULT (nn) = 1;
10967 TREE_TYPE (node) = void_type_node;
10968 TREE_SIDE_EFFECTS (node) = 1;
10969 CAN_COMPLETE_NORMALLY (node) = 1;
10970 break;
10972 case SWITCH_EXPR:
10973 case LOOP_EXPR:
10974 PUSH_LOOP (node);
10975 /* Check whether the loop was enclosed in a labeled
10976 statement. If not, create one, insert the loop in it and
10977 return the node */
10978 nn = patch_loop_statement (node);
10980 /* Anyways, walk the body of the loop */
10981 if (TREE_CODE (node) == LOOP_EXPR)
10982 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
10983 /* Switch statement: walk the switch expression and the cases */
10984 else
10985 node = patch_switch_statement (node);
10987 if (TREE_OPERAND (node, 0) == error_mark_node)
10988 nn = error_mark_node;
10989 else
10991 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
10992 /* If we returned something different, that's because we
10993 inserted a label. Pop the label too. */
10994 if (nn != node)
10996 if (CAN_COMPLETE_NORMALLY (node))
10997 CAN_COMPLETE_NORMALLY (nn) = 1;
10998 POP_LABELED_BLOCK ();
11001 POP_LOOP ();
11002 return nn;
11004 case EXIT_EXPR:
11005 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11006 return patch_exit_expr (node);
11008 case COND_EXPR:
11009 /* Condition */
11010 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11011 if (TREE_OPERAND (node, 0) == error_mark_node)
11012 return error_mark_node;
11013 /* then-else branches */
11014 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11015 if (TREE_OPERAND (node, 1) == error_mark_node)
11016 return error_mark_node;
11017 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11018 if (TREE_OPERAND (node, 2) == error_mark_node)
11019 return error_mark_node;
11020 return patch_if_else_statement (node);
11021 break;
11023 case CONDITIONAL_EXPR:
11024 /* Condition */
11025 wfl_op1 = TREE_OPERAND (node, 0);
11026 COMPLETE_CHECK_OP_0 (node);
11027 wfl_op2 = TREE_OPERAND (node, 1);
11028 COMPLETE_CHECK_OP_1 (node);
11029 wfl_op3 = TREE_OPERAND (node, 2);
11030 COMPLETE_CHECK_OP_2 (node);
11031 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11033 /* 3- Expression section */
11034 case COMPOUND_EXPR:
11035 wfl_op2 = TREE_OPERAND (node, 1);
11036 TREE_OPERAND (node, 0) = nn =
11037 java_complete_tree (TREE_OPERAND (node, 0));
11038 if (wfl_op2 == empty_stmt_node)
11039 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11040 else
11042 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11044 /* An unreachable condition in a do-while statement
11045 is *not* (technically) an unreachable statement. */
11046 nn = wfl_op2;
11047 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11048 nn = EXPR_WFL_NODE (nn);
11049 if (TREE_CODE (nn) != EXIT_EXPR)
11051 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11052 parse_error_context (wfl_operator, "Unreachable statement");
11055 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11056 if (TREE_OPERAND (node, 1) == error_mark_node)
11057 return error_mark_node;
11058 CAN_COMPLETE_NORMALLY (node)
11059 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11061 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11062 break;
11064 case RETURN_EXPR:
11065 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11066 return patch_return (node);
11068 case EXPR_WITH_FILE_LOCATION:
11069 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11070 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11072 tree wfl = node;
11073 node = resolve_expression_name (node, NULL);
11074 if (node == error_mark_node)
11075 return node;
11076 /* Keep line number information somewhere were it doesn't
11077 disrupt the completion process. */
11078 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11080 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11081 TREE_OPERAND (node, 1) = wfl;
11083 CAN_COMPLETE_NORMALLY (node) = 1;
11085 else
11087 tree body;
11088 int save_lineno = lineno;
11089 lineno = EXPR_WFL_LINENO (node);
11090 body = java_complete_tree (EXPR_WFL_NODE (node));
11091 lineno = save_lineno;
11092 EXPR_WFL_NODE (node) = body;
11093 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11094 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11095 if (body == empty_stmt_node)
11097 /* Optimization; makes it easier to detect empty bodies. */
11098 return body;
11100 if (body == error_mark_node)
11102 /* Its important for the evaluation of assignment that
11103 this mark on the TREE_TYPE is propagated. */
11104 TREE_TYPE (node) = error_mark_node;
11105 return error_mark_node;
11107 else
11108 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11111 break;
11113 case NEW_ARRAY_EXPR:
11114 /* Patch all the dimensions */
11115 flag = 0;
11116 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11118 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11119 tree dim = convert (int_type_node,
11120 java_complete_tree (TREE_VALUE (cn)));
11121 if (dim == error_mark_node)
11123 flag = 1;
11124 continue;
11126 else
11128 TREE_VALUE (cn) = dim;
11129 /* Setup the location of the current dimension, for
11130 later error report. */
11131 TREE_PURPOSE (cn) =
11132 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11133 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11136 /* They complete the array creation expression, if no errors
11137 were found. */
11138 CAN_COMPLETE_NORMALLY (node) = 1;
11139 return (flag ? error_mark_node
11140 : force_evaluation_order (patch_newarray (node)));
11142 case NEW_ANONYMOUS_ARRAY_EXPR:
11143 /* Create the array type if necessary. */
11144 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11146 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11147 if (!(type = resolve_type_during_patch (type)))
11148 return error_mark_node;
11149 type = build_array_from_name (type, NULL_TREE,
11150 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11151 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11153 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11154 ANONYMOUS_ARRAY_INITIALIZER (node));
11155 if (node == error_mark_node)
11156 return error_mark_node;
11157 CAN_COMPLETE_NORMALLY (node) = 1;
11158 return node;
11160 case NEW_CLASS_EXPR:
11161 case CALL_EXPR:
11162 /* Complete function's argument(s) first */
11163 if (complete_function_arguments (node))
11164 return error_mark_node;
11165 else
11167 tree decl, wfl = TREE_OPERAND (node, 0);
11168 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11170 node = patch_method_invocation (node, NULL_TREE,
11171 NULL_TREE, 0, &decl);
11172 if (node == error_mark_node)
11173 return error_mark_node;
11175 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11176 /* If we call this(...), register signature and positions */
11177 if (in_this)
11178 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11179 tree_cons (wfl, decl,
11180 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11181 CAN_COMPLETE_NORMALLY (node) = 1;
11182 return force_evaluation_order (node);
11185 case MODIFY_EXPR:
11186 /* Save potential wfls */
11187 wfl_op1 = TREE_OPERAND (node, 0);
11188 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11190 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11191 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11192 && DECL_INITIAL (nn) != NULL_TREE)
11194 tree value;
11196 push_obstacks (&permanent_obstack, &permanent_obstack);
11197 value = fold_constant_for_init (nn, nn);
11198 pop_obstacks ();
11200 if (value != NULL_TREE)
11202 tree type = TREE_TYPE (value);
11203 if (JPRIMITIVE_TYPE_P (type) ||
11204 (type == string_ptr_type_node && ! flag_emit_class_files))
11205 return empty_stmt_node;
11207 if (! flag_emit_class_files)
11208 DECL_INITIAL (nn) = NULL_TREE;
11210 wfl_op2 = TREE_OPERAND (node, 1);
11212 if (TREE_OPERAND (node, 0) == error_mark_node)
11213 return error_mark_node;
11215 flag = COMPOUND_ASSIGN_P (wfl_op2);
11216 if (flag)
11218 /* This might break when accessing outer field from inner
11219 class. TESTME, FIXME */
11220 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11222 /* Hand stablize the lhs on both places */
11223 TREE_OPERAND (node, 0) = lvalue;
11224 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11225 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11227 /* 15.25.2.a: Left hand is not an array access. FIXME */
11228 /* Now complete the RHS. We write it back later on. */
11229 nn = java_complete_tree (TREE_OPERAND (node, 1));
11231 if ((cn = patch_string (nn)))
11232 nn = cn;
11234 /* The last part of the rewrite for E1 op= E2 is to have
11235 E1 = (T)(E1 op E2), with T being the type of E1. */
11236 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11237 TREE_TYPE (lvalue), nn));
11239 /* 15.25.2.b: Left hand is an array access. FIXME */
11242 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11243 function to complete this RHS. Note that a NEW_ARRAY_INIT
11244 might have been already fully expanded if created as a result
11245 of processing an anonymous array initializer. We avoid doing
11246 the operation twice by testing whether the node already bears
11247 a type. */
11248 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11249 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11250 TREE_OPERAND (node, 1));
11251 /* Otherwise we simply complete the RHS */
11252 else
11253 nn = java_complete_tree (TREE_OPERAND (node, 1));
11255 if (nn == error_mark_node)
11256 return error_mark_node;
11258 /* Write back the RHS as we evaluated it. */
11259 TREE_OPERAND (node, 1) = nn;
11261 /* In case we're handling = with a String as a RHS, we need to
11262 produce a String out of the RHS (it might still be a
11263 STRING_CST or a StringBuffer at this stage */
11264 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11265 TREE_OPERAND (node, 1) = nn;
11267 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11268 TREE_OPERAND (node, 1))))
11270 /* We return error_mark_node if outer_field_access_fix
11271 detects we write into a final. */
11272 if (nn == error_mark_node)
11273 return error_mark_node;
11274 node = nn;
11276 else
11278 node = patch_assignment (node, wfl_op1, wfl_op2);
11279 /* Reorganize the tree if necessary. */
11280 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11281 || JSTRING_P (TREE_TYPE (node))))
11282 node = java_refold (node);
11285 CAN_COMPLETE_NORMALLY (node) = 1;
11286 return node;
11288 case MULT_EXPR:
11289 case PLUS_EXPR:
11290 case MINUS_EXPR:
11291 case LSHIFT_EXPR:
11292 case RSHIFT_EXPR:
11293 case URSHIFT_EXPR:
11294 case BIT_AND_EXPR:
11295 case BIT_XOR_EXPR:
11296 case BIT_IOR_EXPR:
11297 case TRUNC_MOD_EXPR:
11298 case TRUNC_DIV_EXPR:
11299 case RDIV_EXPR:
11300 case TRUTH_ANDIF_EXPR:
11301 case TRUTH_ORIF_EXPR:
11302 case EQ_EXPR:
11303 case NE_EXPR:
11304 case GT_EXPR:
11305 case GE_EXPR:
11306 case LT_EXPR:
11307 case LE_EXPR:
11308 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11309 knows how to handle those cases. */
11310 wfl_op1 = TREE_OPERAND (node, 0);
11311 wfl_op2 = TREE_OPERAND (node, 1);
11313 CAN_COMPLETE_NORMALLY (node) = 1;
11314 /* Don't complete string nodes if dealing with the PLUS operand. */
11315 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11317 nn = java_complete_tree (wfl_op1);
11318 if (nn == error_mark_node)
11319 return error_mark_node;
11321 TREE_OPERAND (node, 0) = nn;
11323 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11325 nn = java_complete_tree (wfl_op2);
11326 if (nn == error_mark_node)
11327 return error_mark_node;
11329 TREE_OPERAND (node, 1) = nn;
11331 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11333 case INSTANCEOF_EXPR:
11334 wfl_op1 = TREE_OPERAND (node, 0);
11335 COMPLETE_CHECK_OP_0 (node);
11336 if (flag_emit_xref)
11338 TREE_TYPE (node) = boolean_type_node;
11339 return node;
11341 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11343 case UNARY_PLUS_EXPR:
11344 case NEGATE_EXPR:
11345 case TRUTH_NOT_EXPR:
11346 case BIT_NOT_EXPR:
11347 case PREDECREMENT_EXPR:
11348 case PREINCREMENT_EXPR:
11349 case POSTDECREMENT_EXPR:
11350 case POSTINCREMENT_EXPR:
11351 case CONVERT_EXPR:
11352 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11353 how to handle those cases. */
11354 wfl_op1 = TREE_OPERAND (node, 0);
11355 CAN_COMPLETE_NORMALLY (node) = 1;
11356 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11357 if (TREE_OPERAND (node, 0) == error_mark_node)
11358 return error_mark_node;
11359 node = patch_unaryop (node, wfl_op1);
11360 CAN_COMPLETE_NORMALLY (node) = 1;
11361 break;
11363 case ARRAY_REF:
11364 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11365 how to handle those cases. */
11366 wfl_op1 = TREE_OPERAND (node, 0);
11367 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11368 if (TREE_OPERAND (node, 0) == error_mark_node)
11369 return error_mark_node;
11370 if (!flag_emit_class_files && !flag_emit_xref)
11371 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
11372 /* The same applies to wfl_op2 */
11373 wfl_op2 = TREE_OPERAND (node, 1);
11374 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11375 if (TREE_OPERAND (node, 1) == error_mark_node)
11376 return error_mark_node;
11377 if (!flag_emit_class_files && !flag_emit_xref)
11378 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
11379 return patch_array_ref (node);
11381 case RECORD_TYPE:
11382 return node;;
11384 case COMPONENT_REF:
11385 /* The first step in the re-write of qualified name handling. FIXME.
11386 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
11387 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11388 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11390 tree name = TREE_OPERAND (node, 1);
11391 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11392 if (field == NULL_TREE)
11394 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11395 return error_mark_node;
11397 if (! FIELD_STATIC (field))
11399 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11400 return error_mark_node;
11402 return field;
11404 else
11405 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
11406 break;
11408 case THIS_EXPR:
11409 /* Can't use THIS in a static environment */
11410 if (!current_this)
11412 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11413 parse_error_context (wfl_operator,
11414 "Keyword `this' used outside allowed context");
11415 TREE_TYPE (node) = error_mark_node;
11416 return error_mark_node;
11418 if (ctxp->explicit_constructor_p)
11420 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11421 parse_error_context
11422 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
11423 TREE_TYPE (node) = error_mark_node;
11424 return error_mark_node;
11426 return current_this;
11428 case CLASS_LITERAL:
11429 CAN_COMPLETE_NORMALLY (node) = 1;
11430 node = patch_incomplete_class_ref (node);
11431 if (node == error_mark_node)
11432 return error_mark_node;
11433 break;
11435 case INSTANCE_INITIALIZERS_EXPR:
11436 in_instance_initializer++;
11437 node = java_complete_tree (TREE_OPERAND (node, 0));
11438 in_instance_initializer--;
11439 if (node != error_mark_node)
11440 TREE_TYPE (node) = void_type_node;
11441 else
11442 return error_mark_node;
11443 break;
11445 default:
11446 CAN_COMPLETE_NORMALLY (node) = 1;
11447 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
11448 and it's time to turn it into the appropriate String object */
11449 if ((nn = patch_string (node)))
11450 node = nn;
11451 else
11452 fatal ("No case for tree code `%s' - java_complete_tree\n",
11453 tree_code_name [TREE_CODE (node)]);
11455 return node;
11458 /* Complete function call's argument. Return a non zero value is an
11459 error was found. */
11461 static int
11462 complete_function_arguments (node)
11463 tree node;
11465 int flag = 0;
11466 tree cn;
11468 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11469 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11471 tree wfl = TREE_VALUE (cn), parm, temp;
11472 parm = java_complete_tree (wfl);
11474 if (parm == error_mark_node)
11476 flag = 1;
11477 continue;
11479 /* If have a string literal that we haven't transformed yet or a
11480 crafted string buffer, as a result of use of the the String
11481 `+' operator. Build `parm.toString()' and expand it. */
11482 if ((temp = patch_string (parm)))
11483 parm = temp;
11484 /* Inline PRIMTYPE.TYPE read access */
11485 parm = maybe_build_primttype_type_ref (parm, wfl);
11487 TREE_VALUE (cn) = parm;
11489 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11490 return flag;
11493 /* Sometimes (for loops and variable initialized during their
11494 declaration), we want to wrap a statement around a WFL and turn it
11495 debugable. */
11497 static tree
11498 build_debugable_stmt (location, stmt)
11499 int location;
11500 tree stmt;
11502 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11504 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11505 EXPR_WFL_LINECOL (stmt) = location;
11507 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11508 return stmt;
11511 static tree
11512 build_expr_block (body, decls)
11513 tree body, decls;
11515 tree node = make_node (BLOCK);
11516 BLOCK_EXPR_DECLS (node) = decls;
11517 BLOCK_EXPR_BODY (node) = body;
11518 if (body)
11519 TREE_TYPE (node) = TREE_TYPE (body);
11520 TREE_SIDE_EFFECTS (node) = 1;
11521 return node;
11524 /* Create a new function block and link it approriately to current
11525 function block chain */
11527 static tree
11528 enter_block ()
11530 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11533 /* Link block B supercontext to the previous block. The current
11534 function DECL is used as supercontext when enter_a_block is called
11535 for the first time for a given function. The current function body
11536 (DECL_FUNCTION_BODY) is set to be block B. */
11538 static tree
11539 enter_a_block (b)
11540 tree b;
11542 tree fndecl = current_function_decl;
11544 if (!fndecl) {
11545 BLOCK_SUPERCONTEXT (b) = current_static_block;
11546 current_static_block = b;
11549 else if (!DECL_FUNCTION_BODY (fndecl))
11551 BLOCK_SUPERCONTEXT (b) = fndecl;
11552 DECL_FUNCTION_BODY (fndecl) = b;
11554 else
11556 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11557 DECL_FUNCTION_BODY (fndecl) = b;
11559 return b;
11562 /* Exit a block by changing the current function body
11563 (DECL_FUNCTION_BODY) to the current block super context, only if
11564 the block being exited isn't the method's top level one. */
11566 static tree
11567 exit_block ()
11569 tree b;
11570 if (current_function_decl)
11572 b = DECL_FUNCTION_BODY (current_function_decl);
11573 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11574 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11576 else
11578 b = current_static_block;
11580 if (BLOCK_SUPERCONTEXT (b))
11581 current_static_block = BLOCK_SUPERCONTEXT (b);
11583 return b;
11586 /* Lookup for NAME in the nested function's blocks, all the way up to
11587 the current toplevel one. It complies with Java's local variable
11588 scoping rules. */
11590 static tree
11591 lookup_name_in_blocks (name)
11592 tree name;
11594 tree b = GET_CURRENT_BLOCK (current_function_decl);
11596 while (b != current_function_decl)
11598 tree current;
11600 /* Paranoid sanity check. To be removed */
11601 if (TREE_CODE (b) != BLOCK)
11602 fatal ("non block expr function body - lookup_name_in_blocks");
11604 for (current = BLOCK_EXPR_DECLS (b); current;
11605 current = TREE_CHAIN (current))
11606 if (DECL_NAME (current) == name)
11607 return current;
11608 b = BLOCK_SUPERCONTEXT (b);
11610 return NULL_TREE;
11613 static void
11614 maybe_absorb_scoping_blocks ()
11616 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
11618 tree b = exit_block ();
11619 java_method_add_stmt (current_function_decl, b);
11620 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11625 /* This section of the source is reserved to build_* functions that
11626 are building incomplete tree nodes and the patch_* functions that
11627 are completing them. */
11629 /* Wrap a non WFL node around a WFL. */
11630 static tree
11631 build_wfl_wrap (node, location)
11632 tree node;
11633 int location;
11635 tree wfl, node_to_insert = node;
11637 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11638 with the way we're processing SUPER. A THIS from a primary as a
11639 different form than a SUPER. Turn THIS into something symbolic */
11640 if (TREE_CODE (node) == THIS_EXPR)
11641 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11642 else
11643 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11645 EXPR_WFL_LINECOL (wfl) = location;
11646 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11647 return wfl;
11651 /* Build a super() constructor invocation. Returns empty_stmt_node if
11652 we're currently dealing with the class java.lang.Object. */
11654 static tree
11655 build_super_invocation (mdecl)
11656 tree mdecl;
11658 if (DECL_CONTEXT (mdecl) == object_type_node)
11659 return empty_stmt_node;
11660 else
11662 tree super_wfl = build_wfl_node (super_identifier_node);
11663 tree a = NULL_TREE, t;
11664 /* If we're dealing with an anonymous class, pass the arguments
11665 of the crafted constructor along. */
11666 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11668 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11669 for (; t != end_params_node; t = TREE_CHAIN (t))
11670 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11672 return build_method_invocation (super_wfl, a);
11676 /* Build a SUPER/THIS qualified method invocation. */
11678 static tree
11679 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11680 int use_this;
11681 tree name, args;
11682 int lloc, rloc;
11684 tree invok;
11685 tree wfl =
11686 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
11687 EXPR_WFL_LINECOL (wfl) = lloc;
11688 invok = build_method_invocation (name, args);
11689 return make_qualified_primary (wfl, invok, rloc);
11692 /* Build an incomplete CALL_EXPR node. */
11694 static tree
11695 build_method_invocation (name, args)
11696 tree name;
11697 tree args;
11699 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11700 TREE_SIDE_EFFECTS (call) = 1;
11701 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11702 return call;
11705 /* Build an incomplete new xxx(...) node. */
11707 static tree
11708 build_new_invocation (name, args)
11709 tree name, args;
11711 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11712 TREE_SIDE_EFFECTS (call) = 1;
11713 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11714 return call;
11717 /* Build an incomplete assignment expression. */
11719 static tree
11720 build_assignment (op, op_location, lhs, rhs)
11721 int op, op_location;
11722 tree lhs, rhs;
11724 tree assignment;
11725 /* Build the corresponding binop if we deal with a Compound
11726 Assignment operator. Mark the binop sub-tree as part of a
11727 Compound Assignment expression */
11728 if (op != ASSIGN_TK)
11730 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11731 COMPOUND_ASSIGN_P (rhs) = 1;
11733 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11734 TREE_SIDE_EFFECTS (assignment) = 1;
11735 EXPR_WFL_LINECOL (assignment) = op_location;
11736 return assignment;
11739 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11741 char *
11742 print_int_node (node)
11743 tree node;
11745 static char buffer [80];
11746 if (TREE_CONSTANT_OVERFLOW (node))
11747 sprintf (buffer, "<overflow>");
11749 if (TREE_INT_CST_HIGH (node) == 0)
11750 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11751 TREE_INT_CST_LOW (node));
11752 else if (TREE_INT_CST_HIGH (node) == -1
11753 && TREE_INT_CST_LOW (node) != 0)
11755 buffer [0] = '-';
11756 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
11757 -TREE_INT_CST_LOW (node));
11759 else
11760 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
11761 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
11763 return buffer;
11766 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
11767 context. */
11769 static int
11770 check_final_assignment (lvalue, wfl)
11771 tree lvalue, wfl;
11773 if (TREE_CODE (lvalue) == COMPOUND_EXPR
11774 && JDECL_P (TREE_OPERAND (lvalue, 1)))
11775 lvalue = TREE_OPERAND (lvalue, 1);
11777 /* When generating class files, references to the `length' field
11778 look a bit different. */
11779 if ((flag_emit_class_files
11780 && TREE_CODE (lvalue) == COMPONENT_REF
11781 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
11782 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
11783 || (TREE_CODE (lvalue) == FIELD_DECL
11784 && FIELD_FINAL (lvalue)
11785 && !DECL_CLINIT_P (current_function_decl)
11786 && !DECL_FINIT_P (current_function_decl)))
11788 parse_error_context
11789 (wfl, "Can't assign a value to the final variable `%s'",
11790 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
11791 return 1;
11793 return 0;
11796 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
11797 read. This is needed to avoid circularities in the implementation
11798 of these fields in libjava. */
11800 static tree
11801 maybe_build_primttype_type_ref (rhs, wfl)
11802 tree rhs, wfl;
11804 tree to_return = NULL_TREE;
11805 tree rhs_type = TREE_TYPE (rhs);
11806 if (TREE_CODE (rhs) == COMPOUND_EXPR)
11808 tree n = TREE_OPERAND (rhs, 1);
11809 if (TREE_CODE (n) == VAR_DECL
11810 && DECL_NAME (n) == TYPE_identifier_node
11811 && rhs_type == class_ptr_type
11812 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
11814 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
11815 if (!strncmp (self_name, "java.lang.", 10))
11816 to_return = build_primtype_type_ref (self_name);
11819 return (to_return ? to_return : rhs );
11822 /* 15.25 Assignment operators. */
11824 static tree
11825 patch_assignment (node, wfl_op1, wfl_op2)
11826 tree node;
11827 tree wfl_op1;
11828 tree wfl_op2;
11830 tree rhs = TREE_OPERAND (node, 1);
11831 tree lvalue = TREE_OPERAND (node, 0), llvalue;
11832 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
11833 int error_found = 0;
11834 int lvalue_from_array = 0;
11836 /* Can't assign to a (blank) final. */
11837 if (check_final_assignment (lvalue, wfl_op1))
11838 error_found = 1;
11840 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11842 /* Lhs can be a named variable */
11843 if (JDECL_P (lvalue))
11845 lhs_type = TREE_TYPE (lvalue);
11847 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
11848 comment on reason why */
11849 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
11851 lhs_type = TREE_TYPE (lvalue);
11852 lvalue_from_array = 1;
11854 /* Or a field access */
11855 else if (TREE_CODE (lvalue) == COMPONENT_REF)
11856 lhs_type = TREE_TYPE (lvalue);
11857 /* Or a function return slot */
11858 else if (TREE_CODE (lvalue) == RESULT_DECL)
11859 lhs_type = TREE_TYPE (lvalue);
11860 /* Otherwise, we might want to try to write into an optimized static
11861 final, this is an of a different nature, reported further on. */
11862 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
11863 && resolve_expression_name (wfl_op1, &llvalue))
11865 if (!error_found && check_final_assignment (llvalue, wfl_op1))
11867 /* What we should do instead is resetting the all the flags
11868 previously set, exchange lvalue for llvalue and continue. */
11869 error_found = 1;
11870 return error_mark_node;
11872 else
11873 lhs_type = TREE_TYPE (lvalue);
11875 else
11877 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
11878 error_found = 1;
11881 rhs_type = TREE_TYPE (rhs);
11882 /* 5.1 Try the assignment conversion for builtin type. */
11883 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
11885 /* 5.2 If it failed, try a reference conversion */
11886 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
11887 lhs_type = promote_type (rhs_type);
11889 /* 15.25.2 If we have a compound assignment, convert RHS into the
11890 type of the LHS */
11891 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11892 new_rhs = convert (lhs_type, rhs);
11894 /* Explicit cast required. This is an error */
11895 if (!new_rhs)
11897 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
11898 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
11899 tree wfl;
11900 char operation [32]; /* Max size known */
11902 /* If the assignment is part of a declaration, we use the WFL of
11903 the declared variable to point out the error and call it a
11904 declaration problem. If the assignment is a genuine =
11905 operator, we call is a operator `=' problem, otherwise we
11906 call it an assignment problem. In both of these last cases,
11907 we use the WFL of the operator to indicate the error. */
11909 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
11911 wfl = wfl_op1;
11912 strcpy (operation, "declaration");
11914 else
11916 wfl = wfl_operator;
11917 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11918 strcpy (operation, "assignment");
11919 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
11920 strcpy (operation, "`return'");
11921 else
11922 strcpy (operation, "`='");
11925 if (!valid_cast_to_p (rhs_type, lhs_type))
11926 parse_error_context
11927 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
11928 operation, t1, t2);
11929 else
11930 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
11931 operation, t1, t2);
11932 free (t1); free (t2);
11933 error_found = 1;
11936 /* Inline read access to java.lang.PRIMTYPE.TYPE */
11937 if (new_rhs)
11938 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
11940 if (error_found)
11941 return error_mark_node;
11943 /* 10.10: Array Store Exception runtime check */
11944 if (!flag_emit_class_files
11945 && !flag_emit_xref
11946 && lvalue_from_array
11947 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
11949 tree check;
11950 tree base = lvalue;
11952 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
11953 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
11954 base = TREE_OPERAND (lvalue, 0);
11955 else
11957 if (flag_bounds_check)
11958 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
11959 else
11960 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
11963 /* Build the invocation of _Jv_CheckArrayStore */
11964 new_rhs = save_expr (new_rhs);
11965 check = build (CALL_EXPR, void_type_node,
11966 build_address_of (soft_checkarraystore_node),
11967 tree_cons (NULL_TREE, base,
11968 build_tree_list (NULL_TREE, new_rhs)),
11969 NULL_TREE);
11970 TREE_SIDE_EFFECTS (check) = 1;
11972 /* We have to decide on an insertion point */
11973 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
11975 tree t;
11976 if (flag_bounds_check)
11978 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
11979 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
11980 build (COMPOUND_EXPR, void_type_node, t, check);
11982 else
11983 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
11984 check, TREE_OPERAND (lvalue, 1));
11986 else
11988 /* Make sure the bound check will happen before the store check */
11989 if (flag_bounds_check)
11990 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
11991 build (COMPOUND_EXPR, void_type_node,
11992 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
11993 else
11994 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
11998 TREE_OPERAND (node, 0) = lvalue;
11999 TREE_OPERAND (node, 1) = new_rhs;
12000 TREE_TYPE (node) = lhs_type;
12001 return node;
12004 /* Check that type SOURCE can be cast into type DEST. If the cast
12005 can't occur at all, return 0 otherwise 1. This function is used to
12006 produce accurate error messages on the reasons why an assignment
12007 failed. */
12009 static tree
12010 try_reference_assignconv (lhs_type, rhs)
12011 tree lhs_type, rhs;
12013 tree new_rhs = NULL_TREE;
12014 tree rhs_type = TREE_TYPE (rhs);
12016 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12018 /* `null' may be assigned to any reference type */
12019 if (rhs == null_pointer_node)
12020 new_rhs = null_pointer_node;
12021 /* Try the reference assignment conversion */
12022 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12023 new_rhs = rhs;
12024 /* This is a magic assignment that we process differently */
12025 else if (rhs == soft_exceptioninfo_call_node)
12026 new_rhs = rhs;
12028 return new_rhs;
12031 /* Check that RHS can be converted into LHS_TYPE by the assignment
12032 conversion (5.2), for the cases of RHS being a builtin type. Return
12033 NULL_TREE if the conversion fails or if because RHS isn't of a
12034 builtin type. Return a converted RHS if the conversion is possible. */
12036 static tree
12037 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12038 tree wfl_op1, lhs_type, rhs;
12040 tree new_rhs = NULL_TREE;
12041 tree rhs_type = TREE_TYPE (rhs);
12043 /* Zero accepted everywhere */
12044 if (TREE_CODE (rhs) == INTEGER_CST
12045 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12046 && JPRIMITIVE_TYPE_P (rhs_type))
12047 new_rhs = convert (lhs_type, rhs);
12049 /* 5.1.1 Try Identity Conversion,
12050 5.1.2 Try Widening Primitive Conversion */
12051 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12052 new_rhs = convert (lhs_type, rhs);
12054 /* Try a narrowing primitive conversion (5.1.3):
12055 - expression is a constant expression of type int AND
12056 - variable is byte, short or char AND
12057 - The value of the expression is representable in the type of the
12058 variable */
12059 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12060 && (lhs_type == byte_type_node || lhs_type == char_type_node
12061 || lhs_type == short_type_node))
12063 if (int_fits_type_p (rhs, lhs_type))
12064 new_rhs = convert (lhs_type, rhs);
12065 else if (wfl_op1) /* Might be called with a NULL */
12066 parse_warning_context
12067 (wfl_op1, "Constant expression `%s' to wide for narrowing primitive conversion to `%s'",
12068 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12069 /* Reported a warning that will turn into an error further
12070 down, so we don't return */
12073 return new_rhs;
12076 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12077 conversion (5.1.1) or widening primitve conversion (5.1.2). Return
12078 0 is the conversion test fails. This implements parts the method
12079 invocation convertion (5.3). */
12081 static int
12082 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12083 tree lhs_type, rhs_type;
12085 /* 5.1.1: This is the identity conversion part. */
12086 if (lhs_type == rhs_type)
12087 return 1;
12089 /* Reject non primitive types */
12090 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
12091 return 0;
12093 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12094 than a char can't be converted into a char. Short can't too, but
12095 the < test below takes care of that */
12096 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12097 return 0;
12099 /* Accept all promoted type here. Note, we can't use <= in the test
12100 below, because we still need to bounce out assignments of short
12101 to char and the likes */
12102 if (lhs_type == int_type_node
12103 && (rhs_type == promoted_byte_type_node
12104 || rhs_type == promoted_short_type_node
12105 || rhs_type == promoted_char_type_node
12106 || rhs_type == promoted_boolean_type_node))
12107 return 1;
12109 /* From here, an integral is widened if its precision is smaller
12110 than the precision of the LHS or if the LHS is a floating point
12111 type, or the RHS is a float and the RHS a double. */
12112 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12113 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12114 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12115 || (rhs_type == float_type_node && lhs_type == double_type_node))
12116 return 1;
12118 return 0;
12121 /* Check that something of SOURCE type can be assigned or cast to
12122 something of DEST type at runtime. Return 1 if the operation is
12123 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12124 were SOURCE is cast into DEST, which borrows a lot of the
12125 assignment check. */
12127 static int
12128 valid_ref_assignconv_cast_p (source, dest, cast)
12129 tree source;
12130 tree dest;
12131 int cast;
12133 /* SOURCE or DEST might be null if not from a declared entity. */
12134 if (!source || !dest)
12135 return 0;
12136 if (JNULLP_TYPE_P (source))
12137 return 1;
12138 if (TREE_CODE (source) == POINTER_TYPE)
12139 source = TREE_TYPE (source);
12140 if (TREE_CODE (dest) == POINTER_TYPE)
12141 dest = TREE_TYPE (dest);
12142 /* Case where SOURCE is a class type */
12143 if (TYPE_CLASS_P (source))
12145 if (TYPE_CLASS_P (dest))
12146 return (source == dest
12147 || inherits_from_p (source, dest)
12148 || (cast && inherits_from_p (dest, source)));
12149 if (TYPE_INTERFACE_P (dest))
12151 /* If doing a cast and SOURCE is final, the operation is
12152 always correct a compile time (because even if SOURCE
12153 does not implement DEST, a subclass of SOURCE might). */
12154 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12155 return 1;
12156 /* Otherwise, SOURCE must implement DEST */
12157 return interface_of_p (dest, source);
12159 /* DEST is an array, cast permited if SOURCE is of Object type */
12160 return (cast && source == object_type_node ? 1 : 0);
12162 if (TYPE_INTERFACE_P (source))
12164 if (TYPE_CLASS_P (dest))
12166 /* If not casting, DEST must be the Object type */
12167 if (!cast)
12168 return dest == object_type_node;
12169 /* We're doing a cast. The cast is always valid is class
12170 DEST is not final, otherwise, DEST must implement SOURCE */
12171 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12172 return 1;
12173 else
12174 return interface_of_p (source, dest);
12176 if (TYPE_INTERFACE_P (dest))
12178 /* If doing a cast, then if SOURCE and DEST contain method
12179 with the same signature but different return type, then
12180 this is a (compile time) error */
12181 if (cast)
12183 tree method_source, method_dest;
12184 tree source_type;
12185 tree source_sig;
12186 tree source_name;
12187 for (method_source = TYPE_METHODS (source); method_source;
12188 method_source = TREE_CHAIN (method_source))
12190 source_sig =
12191 build_java_argument_signature (TREE_TYPE (method_source));
12192 source_type = TREE_TYPE (TREE_TYPE (method_source));
12193 source_name = DECL_NAME (method_source);
12194 for (method_dest = TYPE_METHODS (dest);
12195 method_dest; method_dest = TREE_CHAIN (method_dest))
12196 if (source_sig ==
12197 build_java_argument_signature (TREE_TYPE (method_dest))
12198 && source_name == DECL_NAME (method_dest)
12199 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12200 return 0;
12202 return 1;
12204 else
12205 return source == dest || interface_of_p (dest, source);
12207 else /* Array */
12208 return (cast ?
12209 (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable) : 0);
12211 if (TYPE_ARRAY_P (source))
12213 if (TYPE_CLASS_P (dest))
12214 return dest == object_type_node;
12215 /* Can't cast an array to an interface unless the interface is
12216 java.lang.Cloneable */
12217 if (TYPE_INTERFACE_P (dest))
12218 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable ? 1 : 0);
12219 else /* Arrays */
12221 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12222 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12224 /* In case of severe errors, they turn out null */
12225 if (!dest_element_type || !source_element_type)
12226 return 0;
12227 if (source_element_type == dest_element_type)
12228 return 1;
12229 return valid_ref_assignconv_cast_p (source_element_type,
12230 dest_element_type, cast);
12232 return 0;
12234 return 0;
12237 static int
12238 valid_cast_to_p (source, dest)
12239 tree source;
12240 tree dest;
12242 if (TREE_CODE (source) == POINTER_TYPE)
12243 source = TREE_TYPE (source);
12244 if (TREE_CODE (dest) == POINTER_TYPE)
12245 dest = TREE_TYPE (dest);
12247 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12248 return valid_ref_assignconv_cast_p (source, dest, 1);
12250 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12251 return 1;
12253 return 0;
12256 /* Method invocation conversion test. Return 1 if type SOURCE can be
12257 converted to type DEST through the methond invocation conversion
12258 process (5.3) */
12260 static tree
12261 do_unary_numeric_promotion (arg)
12262 tree arg;
12264 tree type = TREE_TYPE (arg);
12265 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
12266 : TREE_CODE (type) == CHAR_TYPE)
12267 arg = convert (int_type_node, arg);
12268 return arg;
12271 /* Return a non zero value if SOURCE can be converted into DEST using
12272 the method invocation conversion rule (5.3). */
12273 static int
12274 valid_method_invocation_conversion_p (dest, source)
12275 tree dest, source;
12277 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12278 && valid_builtin_assignconv_identity_widening_p (dest, source))
12279 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12280 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12281 && valid_ref_assignconv_cast_p (source, dest, 0)));
12284 /* Build an incomplete binop expression. */
12286 static tree
12287 build_binop (op, op_location, op1, op2)
12288 enum tree_code op;
12289 int op_location;
12290 tree op1, op2;
12292 tree binop = build (op, NULL_TREE, op1, op2);
12293 TREE_SIDE_EFFECTS (binop) = 1;
12294 /* Store the location of the operator, for better error report. The
12295 string of the operator will be rebuild based on the OP value. */
12296 EXPR_WFL_LINECOL (binop) = op_location;
12297 return binop;
12300 /* Build the string of the operator retained by NODE. If NODE is part
12301 of a compound expression, add an '=' at the end of the string. This
12302 function is called when an error needs to be reported on an
12303 operator. The string is returned as a pointer to a static character
12304 buffer. */
12306 static char *
12307 operator_string (node)
12308 tree node;
12310 #define BUILD_OPERATOR_STRING(S) \
12312 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12313 return buffer; \
12316 static char buffer [10];
12317 switch (TREE_CODE (node))
12319 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12320 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12321 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12322 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12323 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12324 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12325 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12326 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12327 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12328 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12329 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12330 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12331 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12332 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12333 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12334 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12335 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12336 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12337 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
12338 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12339 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12340 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12341 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12342 case PREINCREMENT_EXPR: /* Fall through */
12343 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12344 case PREDECREMENT_EXPR: /* Fall through */
12345 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12346 default:
12347 fatal ("unregistered operator %s - operator_string",
12348 tree_code_name [TREE_CODE (node)]);
12350 return NULL;
12351 #undef BUILD_OPERATOR_STRING
12354 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12356 static int
12357 java_decl_equiv (var_acc1, var_acc2)
12358 tree var_acc1, var_acc2;
12360 if (JDECL_P (var_acc1))
12361 return (var_acc1 == var_acc2);
12363 return (TREE_CODE (var_acc1) == COMPONENT_REF
12364 && TREE_CODE (var_acc2) == COMPONENT_REF
12365 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12366 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12367 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12370 /* Return a non zero value if CODE is one of the operators that can be
12371 used in conjunction with the `=' operator in a compound assignment. */
12373 static int
12374 binop_compound_p (code)
12375 enum tree_code code;
12377 int i;
12378 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12379 if (binop_lookup [i] == code)
12380 break;
12382 return i < BINOP_COMPOUND_CANDIDATES;
12385 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12387 static tree
12388 java_refold (t)
12389 tree t;
12391 tree c, b, ns, decl;
12393 if (TREE_CODE (t) != MODIFY_EXPR)
12394 return t;
12396 c = TREE_OPERAND (t, 1);
12397 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12398 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12399 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12400 return t;
12402 /* Now the left branch of the binary operator. */
12403 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12404 if (! (b && TREE_CODE (b) == NOP_EXPR
12405 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12406 return t;
12408 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12409 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12410 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12411 return t;
12413 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12414 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12415 /* It's got to be the an equivalent decl */
12416 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12418 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12419 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12420 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12421 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12422 /* Change the right part of the BINOP_EXPR */
12423 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12426 return t;
12429 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
12430 errors but we modify NODE so that it contains the type computed
12431 according to the expression, when it's fixed. Otherwise, we write
12432 error_mark_node as the type. It allows us to further the analysis
12433 of remaining nodes and detects more errors in certain cases. */
12435 static tree
12436 patch_binop (node, wfl_op1, wfl_op2)
12437 tree node;
12438 tree wfl_op1;
12439 tree wfl_op2;
12441 tree op1 = TREE_OPERAND (node, 0);
12442 tree op2 = TREE_OPERAND (node, 1);
12443 tree op1_type = TREE_TYPE (op1);
12444 tree op2_type = TREE_TYPE (op2);
12445 tree prom_type = NULL_TREE, cn;
12446 int code = TREE_CODE (node);
12448 /* If 1, tell the routine that we have to return error_mark_node
12449 after checking for the initialization of the RHS */
12450 int error_found = 0;
12452 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12454 switch (code)
12456 /* 15.16 Multiplicative operators */
12457 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12458 case RDIV_EXPR: /* 15.16.2 Division Operator / */
12459 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
12460 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
12461 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12463 if (!JPRIMITIVE_TYPE_P (op1_type))
12464 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12465 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12466 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12467 TREE_TYPE (node) = error_mark_node;
12468 error_found = 1;
12469 break;
12471 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12472 /* Change the division operator if necessary */
12473 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12474 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
12476 if (TREE_CODE (prom_type) == INTEGER_TYPE
12477 && flag_use_divide_subroutine
12478 && ! flag_emit_class_files
12479 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12480 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12482 /* This one is more complicated. FLOATs are processed by a
12483 function call to soft_fmod. Duplicate the value of the
12484 COMPOUND_ASSIGN_P flag. */
12485 if (code == TRUNC_MOD_EXPR)
12487 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12488 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
12489 TREE_SIDE_EFFECTS (mod)
12490 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12491 return mod;
12493 break;
12495 /* 15.17 Additive Operators */
12496 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
12498 /* Operation is valid if either one argument is a string
12499 constant, a String object or a StringBuffer crafted for the
12500 purpose of the a previous usage of the String concatenation
12501 operator */
12503 if (TREE_CODE (op1) == STRING_CST
12504 || TREE_CODE (op2) == STRING_CST
12505 || JSTRING_TYPE_P (op1_type)
12506 || JSTRING_TYPE_P (op2_type)
12507 || IS_CRAFTED_STRING_BUFFER_P (op1)
12508 || IS_CRAFTED_STRING_BUFFER_P (op2))
12509 return build_string_concatenation (op1, op2);
12511 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12512 Numeric Types */
12513 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12515 if (!JPRIMITIVE_TYPE_P (op1_type))
12516 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12517 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12518 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12519 TREE_TYPE (node) = error_mark_node;
12520 error_found = 1;
12521 break;
12523 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12524 break;
12526 /* 15.18 Shift Operators */
12527 case LSHIFT_EXPR:
12528 case RSHIFT_EXPR:
12529 case URSHIFT_EXPR:
12530 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12532 if (!JINTEGRAL_TYPE_P (op1_type))
12533 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12534 else
12536 if (JPRIMITIVE_TYPE_P (op2_type))
12537 parse_error_context (wfl_operator,
12538 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
12539 operator_string (node),
12540 lang_printable_name (op2_type, 0));
12541 else
12542 parse_error_context (wfl_operator,
12543 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
12544 operator_string (node),
12545 lang_printable_name (op2_type, 0));
12547 TREE_TYPE (node) = error_mark_node;
12548 error_found = 1;
12549 break;
12552 /* Unary numeric promotion (5.6.1) is performed on each operand
12553 separatly */
12554 op1 = do_unary_numeric_promotion (op1);
12555 op2 = do_unary_numeric_promotion (op2);
12557 /* The type of the shift expression is the type of the promoted
12558 type of the left-hand operand */
12559 prom_type = TREE_TYPE (op1);
12561 /* Shift int only up to 0x1f and long up to 0x3f */
12562 if (prom_type == int_type_node)
12563 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12564 build_int_2 (0x1f, 0)));
12565 else
12566 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12567 build_int_2 (0x3f, 0)));
12569 /* The >>> operator is a >> operating on unsigned quantities */
12570 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
12572 tree to_return;
12573 tree utype = unsigned_type (prom_type);
12574 op1 = convert (utype, op1);
12575 TREE_SET_CODE (node, RSHIFT_EXPR);
12576 TREE_OPERAND (node, 0) = op1;
12577 TREE_OPERAND (node, 1) = op2;
12578 TREE_TYPE (node) = utype;
12579 to_return = convert (prom_type, node);
12580 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12581 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
12582 TREE_SIDE_EFFECTS (to_return)
12583 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12584 return to_return;
12586 break;
12588 /* 15.19.1 Type Comparison Operator instaceof */
12589 case INSTANCEOF_EXPR:
12591 TREE_TYPE (node) = boolean_type_node;
12593 if (!(op2_type = resolve_type_during_patch (op2)))
12594 return error_mark_node;
12596 /* The first operand must be a reference type or the null type */
12597 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12598 error_found = 1; /* Error reported further below */
12600 /* The second operand must be a reference type */
12601 if (!JREFERENCE_TYPE_P (op2_type))
12603 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12604 parse_error_context
12605 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12606 lang_printable_name (op2_type, 0));
12607 error_found = 1;
12610 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12612 /* If the first operand is null, the result is always false */
12613 if (op1 == null_pointer_node)
12614 return boolean_false_node;
12615 else if (flag_emit_class_files)
12617 TREE_OPERAND (node, 1) = op2_type;
12618 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
12619 return node;
12621 /* Otherwise we have to invoke instance of to figure it out */
12622 else
12623 return build_instanceof (op1, op2_type);
12625 /* There is no way the expression operand can be an instance of
12626 the type operand. This is a compile time error. */
12627 else
12629 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
12630 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12631 parse_error_context
12632 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12633 t1, lang_printable_name (op2_type, 0));
12634 free (t1);
12635 error_found = 1;
12638 break;
12640 /* 15.21 Bitwise and Logical Operators */
12641 case BIT_AND_EXPR:
12642 case BIT_XOR_EXPR:
12643 case BIT_IOR_EXPR:
12644 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12645 /* Binary numeric promotion is performed on both operand and the
12646 expression retain that type */
12647 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12649 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12650 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12651 /* The type of the bitwise operator expression is BOOLEAN */
12652 prom_type = boolean_type_node;
12653 else
12655 if (!JINTEGRAL_TYPE_P (op1_type))
12656 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12657 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12658 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12659 TREE_TYPE (node) = error_mark_node;
12660 error_found = 1;
12661 /* Insert a break here if adding thing before the switch's
12662 break for this case */
12664 break;
12666 /* 15.22 Conditional-And Operator */
12667 case TRUTH_ANDIF_EXPR:
12668 /* 15.23 Conditional-Or Operator */
12669 case TRUTH_ORIF_EXPR:
12670 /* Operands must be of BOOLEAN type */
12671 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12672 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12674 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12675 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12676 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12677 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12678 TREE_TYPE (node) = boolean_type_node;
12679 error_found = 1;
12680 break;
12682 /* The type of the conditional operators is BOOLEAN */
12683 prom_type = boolean_type_node;
12684 break;
12686 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12687 case LT_EXPR:
12688 case GT_EXPR:
12689 case LE_EXPR:
12690 case GE_EXPR:
12691 /* The type of each of the operands must be a primitive numeric
12692 type */
12693 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12695 if (!JNUMERIC_TYPE_P (op1_type))
12696 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12697 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12698 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12699 TREE_TYPE (node) = boolean_type_node;
12700 error_found = 1;
12701 break;
12703 /* Binary numeric promotion is performed on the operands */
12704 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12705 /* The type of the relation expression is always BOOLEAN */
12706 prom_type = boolean_type_node;
12707 break;
12709 /* 15.20 Equality Operator */
12710 case EQ_EXPR:
12711 case NE_EXPR:
12712 /* It's time for us to patch the strings. */
12713 if ((cn = patch_string (op1)))
12715 op1 = cn;
12716 op1_type = TREE_TYPE (op1);
12718 if ((cn = patch_string (op2)))
12720 op2 = cn;
12721 op2_type = TREE_TYPE (op2);
12724 /* 15.20.1 Numerical Equality Operators == and != */
12725 /* Binary numeric promotion is performed on the operands */
12726 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
12727 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12729 /* 15.20.2 Boolean Equality Operators == and != */
12730 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
12731 TREE_CODE (op2_type) == BOOLEAN_TYPE)
12732 ; /* Nothing to do here */
12734 /* 15.20.3 Reference Equality Operators == and != */
12735 /* Types have to be either references or the null type. If
12736 they're references, it must be possible to convert either
12737 type to the other by casting conversion. */
12738 else if (op1 == null_pointer_node || op2 == null_pointer_node
12739 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
12740 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
12741 || valid_ref_assignconv_cast_p (op2_type,
12742 op1_type, 1))))
12743 ; /* Nothing to do here */
12745 /* Else we have an error figure what can't be converted into
12746 what and report the error */
12747 else
12749 char *t1;
12750 t1 = xstrdup (lang_printable_name (op1_type, 0));
12751 parse_error_context
12752 (wfl_operator,
12753 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
12754 operator_string (node), t1,
12755 lang_printable_name (op2_type, 0));
12756 free (t1);
12757 TREE_TYPE (node) = boolean_type_node;
12758 error_found = 1;
12759 break;
12761 prom_type = boolean_type_node;
12762 break;
12765 if (error_found)
12766 return error_mark_node;
12768 TREE_OPERAND (node, 0) = op1;
12769 TREE_OPERAND (node, 1) = op2;
12770 TREE_TYPE (node) = prom_type;
12771 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12773 if (flag_emit_xref)
12774 return node;
12776 /* fold does not respect side-effect order as required for Java but not C.
12777 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
12778 * bytecode.
12780 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
12781 : ! TREE_SIDE_EFFECTS (node))
12782 node = fold (node);
12783 return node;
12786 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
12787 zero value, the value of CSTE comes after the valude of STRING */
12789 static tree
12790 do_merge_string_cste (cste, string, string_len, after)
12791 tree cste;
12792 const char *string;
12793 int string_len, after;
12795 int len = TREE_STRING_LENGTH (cste) + string_len;
12796 const char *old = TREE_STRING_POINTER (cste);
12797 TREE_STRING_LENGTH (cste) = len;
12798 TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
12799 if (after)
12801 strcpy (TREE_STRING_POINTER (cste), string);
12802 strcat (TREE_STRING_POINTER (cste), old);
12804 else
12806 strcpy (TREE_STRING_POINTER (cste), old);
12807 strcat (TREE_STRING_POINTER (cste), string);
12809 return cste;
12812 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
12813 new STRING_CST on success, NULL_TREE on failure */
12815 static tree
12816 merge_string_cste (op1, op2, after)
12817 tree op1, op2;
12818 int after;
12820 /* Handle two string constants right away */
12821 if (TREE_CODE (op2) == STRING_CST)
12822 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
12823 TREE_STRING_LENGTH (op2), after);
12825 /* Reasonable integer constant can be treated right away */
12826 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
12828 static const char *boolean_true = "true";
12829 static const char *boolean_false = "false";
12830 static const char *null_pointer = "null";
12831 char ch[3];
12832 const char *string;
12834 if (op2 == boolean_true_node)
12835 string = boolean_true;
12836 else if (op2 == boolean_false_node)
12837 string = boolean_false;
12838 else if (op2 == null_pointer_node)
12839 string = null_pointer;
12840 else if (TREE_TYPE (op2) == char_type_node)
12842 ch[0] = (char )TREE_INT_CST_LOW (op2);
12843 ch[1] = '\0';
12844 string = ch;
12846 else
12847 string = print_int_node (op2);
12849 return do_merge_string_cste (op1, string, strlen (string), after);
12851 return NULL_TREE;
12854 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
12855 has to be a STRING_CST and the other part must be a STRING_CST or a
12856 INTEGRAL constant. Return a new STRING_CST if the operation
12857 succeed, NULL_TREE otherwise.
12859 If the case we want to optimize for space, we might want to return
12860 NULL_TREE for each invocation of this routine. FIXME */
12862 static tree
12863 string_constant_concatenation (op1, op2)
12864 tree op1, op2;
12866 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
12868 tree string, rest;
12869 int invert;
12871 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
12872 rest = (string == op1 ? op2 : op1);
12873 invert = (string == op1 ? 0 : 1 );
12875 /* Walk REST, only if it looks reasonable */
12876 if (TREE_CODE (rest) != STRING_CST
12877 && !IS_CRAFTED_STRING_BUFFER_P (rest)
12878 && !JSTRING_TYPE_P (TREE_TYPE (rest))
12879 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
12881 rest = java_complete_tree (rest);
12882 if (rest == error_mark_node)
12883 return error_mark_node;
12884 rest = fold (rest);
12886 return merge_string_cste (string, rest, invert);
12888 return NULL_TREE;
12891 /* Implement the `+' operator. Does static optimization if possible,
12892 otherwise create (if necessary) and append elements to a
12893 StringBuffer. The StringBuffer will be carried around until it is
12894 used for a function call or an assignment. Then toString() will be
12895 called on it to turn it into a String object. */
12897 static tree
12898 build_string_concatenation (op1, op2)
12899 tree op1, op2;
12901 tree result;
12902 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12904 if (flag_emit_xref)
12905 return build (PLUS_EXPR, string_type_node, op1, op2);
12907 /* Try to do some static optimization */
12908 if ((result = string_constant_concatenation (op1, op2)))
12909 return result;
12911 /* Discard empty strings on either side of the expression */
12912 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
12914 op1 = op2;
12915 op2 = NULL_TREE;
12917 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
12918 op2 = NULL_TREE;
12920 /* If operands are string constant, turn then into object references */
12921 if (TREE_CODE (op1) == STRING_CST)
12922 op1 = patch_string_cst (op1);
12923 if (op2 && TREE_CODE (op2) == STRING_CST)
12924 op2 = patch_string_cst (op2);
12926 /* If either one of the constant is null and the other non null
12927 operand is a String object, return it. */
12928 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
12929 return op1;
12931 /* If OP1 isn't already a StringBuffer, create and
12932 initialize a new one */
12933 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
12935 /* Two solutions here:
12936 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
12937 2) OP1 is something else, we call new StringBuffer().append(OP1). */
12938 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
12939 op1 = BUILD_STRING_BUFFER (op1);
12940 else
12942 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
12943 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
12947 if (op2)
12949 /* OP1 is no longer the last node holding a crafted StringBuffer */
12950 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
12951 /* Create a node for `{new...,xxx}.append (op2)' */
12952 if (op2)
12953 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
12956 /* Mark the last node holding a crafted StringBuffer */
12957 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
12959 TREE_SIDE_EFFECTS (op1) = side_effects;
12960 return op1;
12963 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
12964 StringBuffer. If no string were found to be patched, return
12965 NULL. */
12967 static tree
12968 patch_string (node)
12969 tree node;
12971 if (node == error_mark_node)
12972 return error_mark_node;
12973 if (TREE_CODE (node) == STRING_CST)
12974 return patch_string_cst (node);
12975 else if (IS_CRAFTED_STRING_BUFFER_P (node))
12977 int saved = ctxp->explicit_constructor_p;
12978 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
12979 tree ret;
12980 /* Temporary disable forbid the use of `this'. */
12981 ctxp->explicit_constructor_p = 0;
12982 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
12983 /* String concatenation arguments must be evaluated in order too. */
12984 ret = force_evaluation_order (ret);
12985 /* Restore it at its previous value */
12986 ctxp->explicit_constructor_p = saved;
12987 return ret;
12989 return NULL_TREE;
12992 /* Build the internal representation of a string constant. */
12994 static tree
12995 patch_string_cst (node)
12996 tree node;
12998 int location;
12999 if (! flag_emit_class_files)
13001 push_obstacks (&permanent_obstack, &permanent_obstack);
13002 node = get_identifier (TREE_STRING_POINTER (node));
13003 location = alloc_name_constant (CONSTANT_String, node);
13004 node = build_ref_from_constant_pool (location);
13005 pop_obstacks ();
13007 TREE_TYPE (node) = string_ptr_type_node;
13008 TREE_CONSTANT (node) = 1;
13009 return node;
13012 /* Build an incomplete unary operator expression. */
13014 static tree
13015 build_unaryop (op_token, op_location, op1)
13016 int op_token, op_location;
13017 tree op1;
13019 enum tree_code op;
13020 tree unaryop;
13021 switch (op_token)
13023 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13024 case MINUS_TK: op = NEGATE_EXPR; break;
13025 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13026 case NOT_TK: op = BIT_NOT_EXPR; break;
13027 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13028 op_token);
13031 unaryop = build1 (op, NULL_TREE, op1);
13032 TREE_SIDE_EFFECTS (unaryop) = 1;
13033 /* Store the location of the operator, for better error report. The
13034 string of the operator will be rebuild based on the OP value. */
13035 EXPR_WFL_LINECOL (unaryop) = op_location;
13036 return unaryop;
13039 /* Special case for the ++/-- operators, since they require an extra
13040 argument to build, which is set to NULL and patched
13041 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13043 static tree
13044 build_incdec (op_token, op_location, op1, is_post_p)
13045 int op_token, op_location;
13046 tree op1;
13047 int is_post_p;
13049 static enum tree_code lookup [2][2] =
13051 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13052 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13054 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13055 NULL_TREE, op1, NULL_TREE);
13056 TREE_SIDE_EFFECTS (node) = 1;
13057 /* Store the location of the operator, for better error report. The
13058 string of the operator will be rebuild based on the OP value. */
13059 EXPR_WFL_LINECOL (node) = op_location;
13060 return node;
13063 /* Build an incomplete cast operator, based on the use of the
13064 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13065 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13066 though its type is already set. */
13068 static tree
13069 build_cast (location, type, exp)
13070 int location;
13071 tree type, exp;
13073 tree node = build1 (CONVERT_EXPR, type, exp);
13074 EXPR_WFL_LINECOL (node) = location;
13075 return node;
13078 /* Build an incomplete class reference operator. */
13079 static tree
13080 build_incomplete_class_ref (location, class_name)
13081 int location;
13082 tree class_name;
13084 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13085 EXPR_WFL_LINECOL (node) = location;
13086 return node;
13089 /* Complete an incomplete class reference operator. */
13090 static tree
13091 patch_incomplete_class_ref (node)
13092 tree node;
13094 tree type = TREE_OPERAND (node, 0);
13095 tree ref_type;
13097 if (!(ref_type = resolve_type_during_patch (type)))
13098 return error_mark_node;
13100 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13102 /* A class referenced by `foo.class' is initialized. */
13103 return build_class_init (ref_type, build_class_ref (ref_type));
13106 /* If we're emitting class files and we have to deal with non
13107 primitive types, we invoke (and consider generating) the
13108 synthetic static method `class$'. */
13109 if (!TYPE_DOT_CLASS (current_class))
13110 build_dot_class_method (current_class);
13111 ref_type =
13112 build_dot_class_method_invocation (DECL_NAME (TYPE_NAME (ref_type)));
13113 return java_complete_tree (ref_type);
13116 /* 15.14 Unary operators. We return error_mark_node in case of error,
13117 but preserve the type of NODE if the type is fixed. */
13119 static tree
13120 patch_unaryop (node, wfl_op)
13121 tree node;
13122 tree wfl_op;
13124 tree op = TREE_OPERAND (node, 0);
13125 tree op_type = TREE_TYPE (op);
13126 tree prom_type = NULL_TREE, value, decl;
13127 int outer_field_flag = 0;
13128 int code = TREE_CODE (node);
13129 int error_found = 0;
13131 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13133 switch (code)
13135 /* 15.13.2 Postfix Increment Operator ++ */
13136 case POSTINCREMENT_EXPR:
13137 /* 15.13.3 Postfix Increment Operator -- */
13138 case POSTDECREMENT_EXPR:
13139 /* 15.14.1 Prefix Increment Operator ++ */
13140 case PREINCREMENT_EXPR:
13141 /* 15.14.2 Prefix Decrement Operator -- */
13142 case PREDECREMENT_EXPR:
13143 op = decl = strip_out_static_field_access_decl (op);
13144 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13145 /* We might be trying to change an outer field accessed using
13146 access method. */
13147 if (outer_field_flag)
13149 /* Retrieve the decl of the field we're trying to access. We
13150 do that by first retrieving the function we would call to
13151 access the field. It has been already verified that this
13152 field isn't final */
13153 if (flag_emit_class_files)
13154 decl = TREE_OPERAND (op, 0);
13155 else
13156 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13157 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13159 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13160 else if (!JDECL_P (decl)
13161 && TREE_CODE (decl) != COMPONENT_REF
13162 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13163 && TREE_CODE (decl) != INDIRECT_REF
13164 && !(TREE_CODE (decl) == COMPOUND_EXPR
13165 && TREE_OPERAND (decl, 1)
13166 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13168 tree lvalue;
13169 /* Before screaming, check that we're not in fact trying to
13170 increment a optimized static final access, in which case
13171 we issue an different error message. */
13172 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13173 && resolve_expression_name (wfl_op, &lvalue)
13174 && check_final_assignment (lvalue, wfl_op)))
13175 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13176 operator_string (node));
13177 TREE_TYPE (node) = error_mark_node;
13178 error_found = 1;
13181 if (check_final_assignment (op, wfl_op))
13182 error_found = 1;
13184 /* From now on, we know that op if a variable and that it has a
13185 valid wfl. We use wfl_op to locate errors related to the
13186 ++/-- operand. */
13187 else if (!JNUMERIC_TYPE_P (op_type))
13189 parse_error_context
13190 (wfl_op, "Invalid argument type `%s' to `%s'",
13191 lang_printable_name (op_type, 0), operator_string (node));
13192 TREE_TYPE (node) = error_mark_node;
13193 error_found = 1;
13195 else
13197 /* Before the addition, binary numeric promotion is performed on
13198 both operands, if really necessary */
13199 if (JINTEGRAL_TYPE_P (op_type))
13201 value = build_int_2 (1, 0);
13202 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13204 else
13206 value = build_int_2 (1, 0);
13207 TREE_TYPE (node) =
13208 binary_numeric_promotion (op_type,
13209 TREE_TYPE (value), &op, &value);
13212 /* We remember we might be accessing an outer field */
13213 if (outer_field_flag)
13215 /* We re-generate an access to the field */
13216 value = build (PLUS_EXPR, TREE_TYPE (op),
13217 build_outer_field_access (wfl_op, decl), value);
13219 /* And we patch the original access$() into a write
13220 with plus_op as a rhs */
13221 return outer_field_access_fix (node, op, value);
13224 /* And write back into the node. */
13225 TREE_OPERAND (node, 0) = op;
13226 TREE_OPERAND (node, 1) = value;
13227 /* Convert the overall back into its original type, if
13228 necessary, and return */
13229 if (JINTEGRAL_TYPE_P (op_type))
13230 return fold (node);
13231 else
13232 return fold (convert (op_type, node));
13234 break;
13236 /* 15.14.3 Unary Plus Operator + */
13237 case UNARY_PLUS_EXPR:
13238 /* 15.14.4 Unary Minus Operator - */
13239 case NEGATE_EXPR:
13240 if (!JNUMERIC_TYPE_P (op_type))
13242 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13243 TREE_TYPE (node) = error_mark_node;
13244 error_found = 1;
13246 /* Unary numeric promotion is performed on operand */
13247 else
13249 op = do_unary_numeric_promotion (op);
13250 prom_type = TREE_TYPE (op);
13251 if (code == UNARY_PLUS_EXPR)
13252 return fold (op);
13254 break;
13256 /* 15.14.5 Bitwise Complement Operator ~ */
13257 case BIT_NOT_EXPR:
13258 if (!JINTEGRAL_TYPE_P (op_type))
13260 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13261 TREE_TYPE (node) = error_mark_node;
13262 error_found = 1;
13264 else
13266 op = do_unary_numeric_promotion (op);
13267 prom_type = TREE_TYPE (op);
13269 break;
13271 /* 15.14.6 Logical Complement Operator ! */
13272 case TRUTH_NOT_EXPR:
13273 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13275 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
13276 /* But the type is known. We will report an error if further
13277 attempt of a assignment is made with this rhs */
13278 TREE_TYPE (node) = boolean_type_node;
13279 error_found = 1;
13281 else
13282 prom_type = boolean_type_node;
13283 break;
13285 /* 15.15 Cast Expression */
13286 case CONVERT_EXPR:
13287 value = patch_cast (node, wfl_operator);
13288 if (value == error_mark_node)
13290 /* If this cast is part of an assignment, we tell the code
13291 that deals with it not to complain about a mismatch,
13292 because things have been cast, anyways */
13293 TREE_TYPE (node) = error_mark_node;
13294 error_found = 1;
13296 else
13298 value = fold (value);
13299 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13300 return value;
13302 break;
13305 if (error_found)
13306 return error_mark_node;
13308 /* There are cases where node has been replaced by something else
13309 and we don't end up returning here: UNARY_PLUS_EXPR,
13310 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
13311 TREE_OPERAND (node, 0) = fold (op);
13312 TREE_TYPE (node) = prom_type;
13313 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
13314 return fold (node);
13317 /* Generic type resolution that sometimes takes place during node
13318 patching. Returned the resolved type or generate an error
13319 message. Return the resolved type or NULL_TREE. */
13321 static tree
13322 resolve_type_during_patch (type)
13323 tree type;
13325 if (unresolved_type_p (type, NULL))
13327 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), NULL_TREE);
13328 if (!type_decl)
13330 parse_error_context (type,
13331 "Class `%s' not found in type declaration",
13332 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13333 return NULL_TREE;
13335 else
13337 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13338 return TREE_TYPE (type_decl);
13341 return type;
13343 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
13344 found. Otherwise NODE or something meant to replace it is returned. */
13346 static tree
13347 patch_cast (node, wfl_operator)
13348 tree node;
13349 tree wfl_operator;
13351 tree op = TREE_OPERAND (node, 0);
13352 tree op_type = TREE_TYPE (op);
13353 tree cast_type = TREE_TYPE (node);
13354 char *t1;
13356 /* First resolve OP_TYPE if unresolved */
13357 if (!(cast_type = resolve_type_during_patch (cast_type)))
13358 return error_mark_node;
13360 /* Check on cast that are proven correct at compile time */
13361 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13363 /* Same type */
13364 if (cast_type == op_type)
13365 return node;
13367 /* float and double type are converted to the original type main
13368 variant and then to the target type. */
13369 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13370 op = convert (integer_type_node, op);
13372 /* Try widening/narowwing convertion. Potentially, things need
13373 to be worked out in gcc so we implement the extreme cases
13374 correctly. fold_convert() needs to be fixed. */
13375 return convert (cast_type, op);
13378 /* It's also valid to cast a boolean into a boolean */
13379 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13380 return node;
13382 /* null can be casted to references */
13383 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13384 return build_null_of_type (cast_type);
13386 /* The remaining legal casts involve conversion between reference
13387 types. Check for their compile time correctness. */
13388 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
13389 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
13391 TREE_TYPE (node) = promote_type (cast_type);
13392 /* Now, the case can be determined correct at compile time if
13393 OP_TYPE can be converted into CAST_TYPE by assignment
13394 conversion (5.2) */
13396 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
13398 TREE_SET_CODE (node, NOP_EXPR);
13399 return node;
13402 if (flag_emit_class_files)
13404 TREE_SET_CODE (node, CONVERT_EXPR);
13405 return node;
13408 /* The cast requires a run-time check */
13409 return build (CALL_EXPR, promote_type (cast_type),
13410 build_address_of (soft_checkcast_node),
13411 tree_cons (NULL_TREE, build_class_ref (cast_type),
13412 build_tree_list (NULL_TREE, op)),
13413 NULL_TREE);
13416 /* Any other casts are proven incorrect at compile time */
13417 t1 = xstrdup (lang_printable_name (op_type, 0));
13418 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
13419 t1, lang_printable_name (cast_type, 0));
13420 free (t1);
13421 return error_mark_node;
13424 /* Build a null constant and give it the type TYPE. */
13426 static tree
13427 build_null_of_type (type)
13428 tree type;
13430 tree node = build_int_2 (0, 0);
13431 TREE_TYPE (node) = promote_type (type);
13432 return node;
13435 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13436 a list of indices. */
13437 static tree
13438 build_array_ref (location, array, index)
13439 int location;
13440 tree array, index;
13442 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13443 EXPR_WFL_LINECOL (node) = location;
13444 return node;
13447 /* 15.12 Array Access Expression */
13449 static tree
13450 patch_array_ref (node)
13451 tree node;
13453 tree array = TREE_OPERAND (node, 0);
13454 tree array_type = TREE_TYPE (array);
13455 tree index = TREE_OPERAND (node, 1);
13456 tree index_type = TREE_TYPE (index);
13457 int error_found = 0;
13459 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13461 if (TREE_CODE (array_type) == POINTER_TYPE)
13462 array_type = TREE_TYPE (array_type);
13464 /* The array reference must be an array */
13465 if (!TYPE_ARRAY_P (array_type))
13467 parse_error_context
13468 (wfl_operator,
13469 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13470 lang_printable_name (array_type, 0));
13471 TREE_TYPE (node) = error_mark_node;
13472 error_found = 1;
13475 /* The array index undergoes unary numeric promotion. The promoted
13476 type must be int */
13477 index = do_unary_numeric_promotion (index);
13478 if (TREE_TYPE (index) != int_type_node)
13480 if (valid_cast_to_p (index_type, int_type_node))
13481 parse_error_context (wfl_operator,
13482 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
13483 lang_printable_name (index_type, 0));
13484 else
13485 parse_error_context (wfl_operator,
13486 "Incompatible type for `[]'. Can't convert `%s' to `int'",
13487 lang_printable_name (index_type, 0));
13488 TREE_TYPE (node) = error_mark_node;
13489 error_found = 1;
13492 if (error_found)
13493 return error_mark_node;
13495 array_type = TYPE_ARRAY_ELEMENT (array_type);
13497 if (flag_emit_class_files || flag_emit_xref)
13499 TREE_OPERAND (node, 0) = array;
13500 TREE_OPERAND (node, 1) = index;
13502 else
13504 /* The save_expr is for correct evaluation order. It would be cleaner
13505 to use force_evaluation_order (see comment there), but that is
13506 difficult when we also have to deal with bounds checking. */
13507 if (TREE_SIDE_EFFECTS (index))
13508 array = save_expr (array);
13509 node = build_java_arrayaccess (array, array_type, index);
13510 if (TREE_SIDE_EFFECTS (index))
13511 node = build (COMPOUND_EXPR, array_type, array, node);
13513 TREE_TYPE (node) = array_type;
13514 return node;
13517 /* 15.9 Array Creation Expressions */
13519 static tree
13520 build_newarray_node (type, dims, extra_dims)
13521 tree type;
13522 tree dims;
13523 int extra_dims;
13525 tree node =
13526 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
13527 build_int_2 (extra_dims, 0));
13528 return node;
13531 static tree
13532 patch_newarray (node)
13533 tree node;
13535 tree type = TREE_OPERAND (node, 0);
13536 tree dims = TREE_OPERAND (node, 1);
13537 tree cdim, array_type;
13538 int error_found = 0;
13539 int ndims = 0;
13540 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
13542 /* Dimension types are verified. It's better for the types to be
13543 verified in order. */
13544 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13546 int dim_error = 0;
13547 tree dim = TREE_VALUE (cdim);
13549 /* Dim might have been saved during its evaluation */
13550 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
13552 /* The type of each specified dimension must be an integral type. */
13553 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13554 dim_error = 1;
13556 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13557 promoted type must be int. */
13558 else
13560 dim = do_unary_numeric_promotion (dim);
13561 if (TREE_TYPE (dim) != int_type_node)
13562 dim_error = 1;
13565 /* Report errors on types here */
13566 if (dim_error)
13568 parse_error_context
13569 (TREE_PURPOSE (cdim),
13570 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
13571 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
13572 "Explicit cast needed to" : "Can't"),
13573 lang_printable_name (TREE_TYPE (dim), 0));
13574 error_found = 1;
13577 TREE_PURPOSE (cdim) = NULL_TREE;
13580 /* Resolve array base type if unresolved */
13581 if (!(type = resolve_type_during_patch (type)))
13582 error_found = 1;
13584 if (error_found)
13586 /* We don't want further evaluation of this bogus array creation
13587 operation */
13588 TREE_TYPE (node) = error_mark_node;
13589 return error_mark_node;
13592 /* Set array_type to the actual (promoted) array type of the result. */
13593 if (TREE_CODE (type) == RECORD_TYPE)
13594 type = build_pointer_type (type);
13595 while (--xdims >= 0)
13597 type = promote_type (build_java_array_type (type, -1));
13599 dims = nreverse (dims);
13600 array_type = type;
13601 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13603 type = array_type;
13604 array_type
13605 = build_java_array_type (type,
13606 TREE_CODE (cdim) == INTEGER_CST
13607 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13608 : -1);
13609 array_type = promote_type (array_type);
13611 dims = nreverse (dims);
13613 /* The node is transformed into a function call. Things are done
13614 differently according to the number of dimensions. If the number
13615 of dimension is equal to 1, then the nature of the base type
13616 (primitive or not) matters. */
13617 if (ndims == 1)
13618 return build_new_array (type, TREE_VALUE (dims));
13620 /* Can't reuse what's already written in expr.c because it uses the
13621 JVM stack representation. Provide a build_multianewarray. FIXME */
13622 return build (CALL_EXPR, array_type,
13623 build_address_of (soft_multianewarray_node),
13624 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
13625 tree_cons (NULL_TREE,
13626 build_int_2 (ndims, 0), dims )),
13627 NULL_TREE);
13630 /* 10.6 Array initializer. */
13632 /* Build a wfl for array element that don't have one, so we can
13633 pin-point errors. */
13635 static tree
13636 maybe_build_array_element_wfl (node)
13637 tree node;
13639 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13640 return build_expr_wfl (NULL_TREE, ctxp->filename,
13641 ctxp->elc.line, ctxp->elc.prev_col);
13642 else
13643 return NULL_TREE;
13646 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13647 identification of initialized arrays easier to detect during walk
13648 and expansion. */
13650 static tree
13651 build_new_array_init (location, values)
13652 int location;
13653 tree values;
13655 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13656 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
13657 EXPR_WFL_LINECOL (to_return) = location;
13658 return to_return;
13661 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13662 occurred. Otherwise return NODE after having set its type
13663 appropriately. */
13665 static tree
13666 patch_new_array_init (type, node)
13667 tree type, node;
13669 int error_seen = 0;
13670 tree current, element_type;
13671 HOST_WIDE_INT length;
13672 int all_constant = 1;
13673 tree init = TREE_OPERAND (node, 0);
13675 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13677 parse_error_context (node,
13678 "Invalid array initializer for non-array type `%s'",
13679 lang_printable_name (type, 1));
13680 return error_mark_node;
13682 type = TREE_TYPE (type);
13683 element_type = TYPE_ARRAY_ELEMENT (type);
13685 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13687 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13688 current; length++, current = TREE_CHAIN (current))
13690 tree elt = TREE_VALUE (current);
13691 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
13693 error_seen |= array_constructor_check_entry (element_type, current);
13694 elt = TREE_VALUE (current);
13695 /* When compiling to native code, STRING_CST is converted to
13696 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13697 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
13698 all_constant = 0;
13700 else
13702 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13703 TREE_PURPOSE (current) = NULL_TREE;
13704 all_constant = 0;
13706 if (elt && TREE_CODE (elt) == TREE_LIST
13707 && TREE_VALUE (elt) == error_mark_node)
13708 error_seen = 1;
13711 if (error_seen)
13712 return error_mark_node;
13714 /* Create a new type. We can't reuse the one we have here by
13715 patching its dimension because it originally is of dimension -1
13716 hence reused by gcc. This would prevent triangular arrays. */
13717 type = build_java_array_type (element_type, length);
13718 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13719 TREE_TYPE (node) = promote_type (type);
13720 TREE_CONSTANT (init) = all_constant;
13721 TREE_CONSTANT (node) = all_constant;
13722 return node;
13725 /* Verify that one entry of the initializer element list can be
13726 assigned to the array base type. Report 1 if an error occurred, 0
13727 otherwise. */
13729 static int
13730 array_constructor_check_entry (type, entry)
13731 tree type, entry;
13733 char *array_type_string = NULL; /* For error reports */
13734 tree value, type_value, new_value, wfl_value, patched;
13735 int error_seen = 0;
13737 new_value = NULL_TREE;
13738 wfl_value = TREE_VALUE (entry);
13740 push_obstacks (&permanent_obstack, &permanent_obstack);
13741 value = java_complete_tree (TREE_VALUE (entry));
13742 /* patch_string return error_mark_node if arg is error_mark_node */
13743 if ((patched = patch_string (value)))
13744 value = patched;
13745 if (value == error_mark_node)
13746 return 1;
13748 type_value = TREE_TYPE (value);
13750 /* At anytime, try_builtin_assignconv can report a warning on
13751 constant overflow during narrowing. */
13752 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
13753 new_value = try_builtin_assignconv (wfl_operator, type, value);
13754 if (!new_value && (new_value = try_reference_assignconv (type, value)))
13755 type_value = promote_type (type);
13757 pop_obstacks ();
13758 /* Check and report errors */
13759 if (!new_value)
13761 const char *msg = (!valid_cast_to_p (type_value, type) ?
13762 "Can't" : "Explicit cast needed to");
13763 if (!array_type_string)
13764 array_type_string = xstrdup (lang_printable_name (type, 1));
13765 parse_error_context
13766 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
13767 msg, lang_printable_name (type_value, 1), array_type_string);
13768 error_seen = 1;
13771 if (new_value)
13773 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
13774 TREE_VALUE (entry) = new_value;
13777 if (array_type_string)
13778 free (array_type_string);
13780 TREE_PURPOSE (entry) = NULL_TREE;
13781 return error_seen;
13784 static tree
13785 build_this (location)
13786 int location;
13788 tree node = build_wfl_node (this_identifier_node);
13789 TREE_SET_CODE (node, THIS_EXPR);
13790 EXPR_WFL_LINECOL (node) = location;
13791 return node;
13794 /* 14.15 The return statement. It builds a modify expression that
13795 assigns the returned value to the RESULT_DECL that hold the value
13796 to be returned. */
13798 static tree
13799 build_return (location, op)
13800 int location;
13801 tree op;
13803 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
13804 EXPR_WFL_LINECOL (node) = location;
13805 node = build_debugable_stmt (location, node);
13806 return node;
13809 static tree
13810 patch_return (node)
13811 tree node;
13813 tree return_exp = TREE_OPERAND (node, 0);
13814 tree meth = current_function_decl;
13815 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
13816 int error_found = 0;
13818 TREE_TYPE (node) = error_mark_node;
13819 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13821 /* It's invalid to have a return value within a function that is
13822 declared with the keyword void or that is a constructor */
13823 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
13824 error_found = 1;
13826 /* It's invalid to use a return statement in a static block */
13827 if (DECL_CLINIT_P (current_function_decl))
13828 error_found = 1;
13830 /* It's invalid to have a no return value within a function that
13831 isn't declared with the keyword `void' */
13832 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
13833 error_found = 2;
13835 if (in_instance_initializer)
13836 error_found = 1;
13838 if (error_found)
13840 if (in_instance_initializer)
13841 parse_error_context (wfl_operator,
13842 "`return' inside instance initializer");
13844 else if (DECL_CLINIT_P (current_function_decl))
13845 parse_error_context (wfl_operator,
13846 "`return' inside static initializer");
13848 else if (!DECL_CONSTRUCTOR_P (meth))
13850 char *t = xstrdup (lang_printable_name (mtype, 0));
13851 parse_error_context (wfl_operator,
13852 "`return' with%s value from `%s %s'",
13853 (error_found == 1 ? "" : "out"),
13854 t, lang_printable_name (meth, 0));
13855 free (t);
13857 else
13858 parse_error_context (wfl_operator,
13859 "`return' with value from constructor `%s'",
13860 lang_printable_name (meth, 0));
13861 return error_mark_node;
13864 /* If we have a return_exp, build a modify expression and expand
13865 it. Note: at that point, the assignment is declared valid, but we
13866 may want to carry some more hacks */
13867 if (return_exp)
13869 tree exp = java_complete_tree (return_exp);
13870 tree modify, patched;
13872 /* If the function returned value and EXP are booleans, EXP has
13873 to be converted into the type of DECL_RESULT, which is integer
13874 (see complete_start_java_method) */
13875 if (TREE_TYPE (exp) == boolean_type_node &&
13876 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
13877 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
13879 /* `null' can be assigned to a function returning a reference */
13880 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
13881 exp == null_pointer_node)
13882 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
13884 if ((patched = patch_string (exp)))
13885 exp = patched;
13887 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
13888 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
13889 modify = java_complete_tree (modify);
13891 if (modify != error_mark_node)
13893 TREE_SIDE_EFFECTS (modify) = 1;
13894 TREE_OPERAND (node, 0) = modify;
13896 else
13897 return error_mark_node;
13899 TREE_TYPE (node) = void_type_node;
13900 TREE_SIDE_EFFECTS (node) = 1;
13901 return node;
13904 /* 14.8 The if Statement */
13906 static tree
13907 build_if_else_statement (location, expression, if_body, else_body)
13908 int location;
13909 tree expression, if_body, else_body;
13911 tree node;
13912 if (!else_body)
13913 else_body = empty_stmt_node;
13914 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
13915 EXPR_WFL_LINECOL (node) = location;
13916 node = build_debugable_stmt (location, node);
13917 return node;
13920 static tree
13921 patch_if_else_statement (node)
13922 tree node;
13924 tree expression = TREE_OPERAND (node, 0);
13926 TREE_TYPE (node) = error_mark_node;
13927 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13929 /* The type of expression must be boolean */
13930 if (TREE_TYPE (expression) != boolean_type_node
13931 && TREE_TYPE (expression) != promoted_boolean_type_node)
13933 parse_error_context
13934 (wfl_operator,
13935 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
13936 lang_printable_name (TREE_TYPE (expression), 0));
13937 return error_mark_node;
13940 TREE_TYPE (node) = void_type_node;
13941 TREE_SIDE_EFFECTS (node) = 1;
13942 CAN_COMPLETE_NORMALLY (node)
13943 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
13944 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
13945 return node;
13948 /* 14.6 Labeled Statements */
13950 /* Action taken when a lableled statement is parsed. a new
13951 LABELED_BLOCK_EXPR is created. No statement is attached to the
13952 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
13954 static tree
13955 build_labeled_block (location, label)
13956 int location;
13957 tree label;
13959 tree label_name ;
13960 tree label_decl, node;
13961 if (label == NULL_TREE || label == continue_identifier_node)
13962 label_name = label;
13963 else
13965 label_name = merge_qualified_name (label_id, label);
13966 /* Issue an error if we try to reuse a label that was previously
13967 declared */
13968 if (IDENTIFIER_LOCAL_VALUE (label_name))
13970 EXPR_WFL_LINECOL (wfl_operator) = location;
13971 parse_error_context (wfl_operator,
13972 "Declaration of `%s' shadows a previous label declaration",
13973 IDENTIFIER_POINTER (label));
13974 EXPR_WFL_LINECOL (wfl_operator) =
13975 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
13976 parse_error_context (wfl_operator,
13977 "This is the location of the previous declaration of label `%s'",
13978 IDENTIFIER_POINTER (label));
13979 java_error_count--;
13983 label_decl = create_label_decl (label_name);
13984 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
13985 EXPR_WFL_LINECOL (node) = location;
13986 TREE_SIDE_EFFECTS (node) = 1;
13987 return node;
13990 /* A labeled statement LBE is attached a statement. */
13992 static tree
13993 finish_labeled_statement (lbe, statement)
13994 tree lbe; /* Labeled block expr */
13995 tree statement;
13997 /* In anyways, tie the loop to its statement */
13998 LABELED_BLOCK_BODY (lbe) = statement;
13999 pop_labeled_block ();
14000 POP_LABELED_BLOCK ();
14001 return lbe;
14004 /* 14.10, 14.11, 14.12 Loop Statements */
14006 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14007 list. */
14009 static tree
14010 build_new_loop (loop_body)
14011 tree loop_body;
14013 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14014 TREE_SIDE_EFFECTS (loop) = 1;
14015 PUSH_LOOP (loop);
14016 return loop;
14019 /* Create a loop body according to the following structure:
14020 COMPOUND_EXPR
14021 COMPOUND_EXPR (loop main body)
14022 EXIT_EXPR (this order is for while/for loops.
14023 LABELED_BLOCK_EXPR the order is reversed for do loops)
14024 LABEL_DECL (a continue occuring here branches at the
14025 BODY end of this labeled block)
14026 INCREMENT (if any)
14028 REVERSED, if non zero, tells that the loop condition expr comes
14029 after the body, like in the do-while loop.
14031 To obtain a loop, the loop body structure described above is
14032 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14034 LABELED_BLOCK_EXPR
14035 LABEL_DECL (use this label to exit the loop)
14036 LOOP_EXPR
14037 <structure described above> */
14039 static tree
14040 build_loop_body (location, condition, reversed)
14041 int location;
14042 tree condition;
14043 int reversed;
14045 tree first, second, body;
14047 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14048 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14049 condition = build_debugable_stmt (location, condition);
14050 TREE_SIDE_EFFECTS (condition) = 1;
14052 body = build_labeled_block (0, continue_identifier_node);
14053 first = (reversed ? body : condition);
14054 second = (reversed ? condition : body);
14055 return
14056 build (COMPOUND_EXPR, NULL_TREE,
14057 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14060 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14061 their order) on the current loop. Unlink the current loop from the
14062 loop list. */
14064 static tree
14065 finish_loop_body (location, condition, body, reversed)
14066 int location;
14067 tree condition, body;
14068 int reversed;
14070 tree to_return = ctxp->current_loop;
14071 tree loop_body = LOOP_EXPR_BODY (to_return);
14072 if (condition)
14074 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14075 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14076 The real EXIT_EXPR is one operand further. */
14077 EXPR_WFL_LINECOL (cnode) = location;
14078 /* This one is for accurate error reports */
14079 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14080 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14082 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14083 POP_LOOP ();
14084 return to_return;
14087 /* Tailored version of finish_loop_body for FOR loops, when FOR
14088 loops feature the condition part */
14090 static tree
14091 finish_for_loop (location, condition, update, body)
14092 int location;
14093 tree condition, update, body;
14095 /* Put the condition and the loop body in place */
14096 tree loop = finish_loop_body (location, condition, body, 0);
14097 /* LOOP is the current loop which has been now popped of the loop
14098 stack. Install the update block */
14099 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14100 return loop;
14103 /* Try to find the loop a block might be related to. This comprises
14104 the case where the LOOP_EXPR is found as the second operand of a
14105 COMPOUND_EXPR, because the loop happens to have an initialization
14106 part, then expressed as the first operand of the COMPOUND_EXPR. If
14107 the search finds something, 1 is returned. Otherwise, 0 is
14108 returned. The search is assumed to start from a
14109 LABELED_BLOCK_EXPR's block. */
14111 static tree
14112 search_loop (statement)
14113 tree statement;
14115 if (TREE_CODE (statement) == LOOP_EXPR)
14116 return statement;
14118 if (TREE_CODE (statement) == BLOCK)
14119 statement = BLOCK_SUBBLOCKS (statement);
14120 else
14121 return NULL_TREE;
14123 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14124 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14125 statement = TREE_OPERAND (statement, 1);
14127 return (TREE_CODE (statement) == LOOP_EXPR
14128 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14131 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14132 returned otherwise. */
14134 static int
14135 labeled_block_contains_loop_p (block, loop)
14136 tree block, loop;
14138 if (!block)
14139 return 0;
14141 if (LABELED_BLOCK_BODY (block) == loop)
14142 return 1;
14144 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14145 return 1;
14147 return 0;
14150 /* If the loop isn't surrounded by a labeled statement, create one and
14151 insert LOOP as its body. */
14153 static tree
14154 patch_loop_statement (loop)
14155 tree loop;
14157 tree loop_label;
14159 TREE_TYPE (loop) = void_type_node;
14160 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14161 return loop;
14163 loop_label = build_labeled_block (0, NULL_TREE);
14164 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14165 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14166 LABELED_BLOCK_BODY (loop_label) = loop;
14167 PUSH_LABELED_BLOCK (loop_label);
14168 return loop_label;
14171 /* 14.13, 14.14: break and continue Statements */
14173 /* Build a break or a continue statement. a null NAME indicates an
14174 unlabeled break/continue statement. */
14176 static tree
14177 build_bc_statement (location, is_break, name)
14178 int location, is_break;
14179 tree name;
14181 tree break_continue, label_block_expr = NULL_TREE;
14183 if (name)
14185 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14186 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14187 /* Null means that we don't have a target for this named
14188 break/continue. In this case, we make the target to be the
14189 label name, so that the error can be reported accuratly in
14190 patch_bc_statement. */
14191 label_block_expr = EXPR_WFL_NODE (name);
14193 /* Unlabeled break/continue will be handled during the
14194 break/continue patch operation */
14195 break_continue
14196 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14198 IS_BREAK_STMT_P (break_continue) = is_break;
14199 TREE_SIDE_EFFECTS (break_continue) = 1;
14200 EXPR_WFL_LINECOL (break_continue) = location;
14201 break_continue = build_debugable_stmt (location, break_continue);
14202 return break_continue;
14205 /* Verification of a break/continue statement. */
14207 static tree
14208 patch_bc_statement (node)
14209 tree node;
14211 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14212 tree labeled_block = ctxp->current_labeled_block;
14213 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14215 /* Having an identifier here means that the target is unknown. */
14216 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14218 parse_error_context (wfl_operator, "No label definition found for `%s'",
14219 IDENTIFIER_POINTER (bc_label));
14220 return error_mark_node;
14222 if (! IS_BREAK_STMT_P (node))
14224 /* It's a continue statement. */
14225 for (;; labeled_block = TREE_CHAIN (labeled_block))
14227 if (labeled_block == NULL_TREE)
14229 if (bc_label == NULL_TREE)
14230 parse_error_context (wfl_operator,
14231 "`continue' must be in loop");
14232 else
14233 parse_error_context
14234 (wfl_operator, "continue label `%s' does not name a loop",
14235 IDENTIFIER_POINTER (bc_label));
14236 return error_mark_node;
14238 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14239 == continue_identifier_node)
14240 && (bc_label == NULL_TREE
14241 || TREE_CHAIN (labeled_block) == bc_label))
14243 bc_label = labeled_block;
14244 break;
14248 else if (!bc_label)
14250 for (;; labeled_block = TREE_CHAIN (labeled_block))
14252 if (labeled_block == NULL_TREE)
14254 parse_error_context (wfl_operator,
14255 "`break' must be in loop or switch");
14256 return error_mark_node;
14258 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14259 if (TREE_CODE (target_stmt) == SWITCH_EXPR
14260 || search_loop (target_stmt))
14262 bc_label = labeled_block;
14263 break;
14268 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14269 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14271 /* Our break/continue don't return values. */
14272 TREE_TYPE (node) = void_type_node;
14273 /* Encapsulate the break within a compound statement so that it's
14274 expanded all the times by expand_expr (and not clobbered
14275 sometimes, like after a if statement) */
14276 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14277 TREE_SIDE_EFFECTS (node) = 1;
14278 return node;
14281 /* Process the exit expression belonging to a loop. Its type must be
14282 boolean. */
14284 static tree
14285 patch_exit_expr (node)
14286 tree node;
14288 tree expression = TREE_OPERAND (node, 0);
14289 TREE_TYPE (node) = error_mark_node;
14290 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14292 /* The type of expression must be boolean */
14293 if (TREE_TYPE (expression) != boolean_type_node)
14295 parse_error_context
14296 (wfl_operator,
14297 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
14298 lang_printable_name (TREE_TYPE (expression), 0));
14299 return error_mark_node;
14301 /* Now we know things are allright, invert the condition, fold and
14302 return */
14303 TREE_OPERAND (node, 0) =
14304 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
14306 if (! integer_zerop (TREE_OPERAND (node, 0))
14307 && ctxp->current_loop != NULL_TREE
14308 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14309 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14310 if (! integer_onep (TREE_OPERAND (node, 0)))
14311 CAN_COMPLETE_NORMALLY (node) = 1;
14314 TREE_TYPE (node) = void_type_node;
14315 return node;
14318 /* 14.9 Switch statement */
14320 static tree
14321 patch_switch_statement (node)
14322 tree node;
14324 tree se = TREE_OPERAND (node, 0), se_type;
14326 /* Complete the switch expression */
14327 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14328 se_type = TREE_TYPE (se);
14329 /* The type of the switch expression must be char, byte, short or
14330 int */
14331 if (!JINTEGRAL_TYPE_P (se_type))
14333 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14334 parse_error_context (wfl_operator,
14335 "Incompatible type for `switch'. Can't convert `%s' to `int'",
14336 lang_printable_name (se_type, 0));
14337 /* This is what java_complete_tree will check */
14338 TREE_OPERAND (node, 0) = error_mark_node;
14339 return error_mark_node;
14342 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
14344 /* Ready to return */
14345 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
14347 TREE_TYPE (node) = error_mark_node;
14348 return error_mark_node;
14350 TREE_TYPE (node) = void_type_node;
14351 TREE_SIDE_EFFECTS (node) = 1;
14352 CAN_COMPLETE_NORMALLY (node)
14353 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14354 || ! SWITCH_HAS_DEFAULT (node);
14355 return node;
14358 /* 14.18 The try/catch statements */
14360 static tree
14361 build_try_statement (location, try_block, catches)
14362 int location;
14363 tree try_block, catches;
14365 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
14366 EXPR_WFL_LINECOL (node) = location;
14367 return node;
14370 static tree
14371 build_try_finally_statement (location, try_block, finally)
14372 int location;
14373 tree try_block, finally;
14375 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14376 EXPR_WFL_LINECOL (node) = location;
14377 return node;
14380 static tree
14381 patch_try_statement (node)
14382 tree node;
14384 int error_found = 0;
14385 tree try = TREE_OPERAND (node, 0);
14386 /* Exception handlers are considered in left to right order */
14387 tree catch = nreverse (TREE_OPERAND (node, 1));
14388 tree current, caught_type_list = NULL_TREE;
14390 /* Check catch clauses, if any. Every time we find an error, we try
14391 to process the next catch clause. We process the catch clause before
14392 the try block so that when processing the try block we can check thrown
14393 exceptions againts the caught type list. */
14394 for (current = catch; current; current = TREE_CHAIN (current))
14396 tree carg_decl, carg_type;
14397 tree sub_current, catch_block, catch_clause;
14398 int unreachable;
14400 /* At this point, the structure of the catch clause is
14401 CATCH_EXPR (catch node)
14402 BLOCK (with the decl of the parameter)
14403 COMPOUND_EXPR
14404 MODIFY_EXPR (assignment of the catch parameter)
14405 BLOCK (catch clause block)
14407 catch_clause = TREE_OPERAND (current, 0);
14408 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14409 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14411 /* Catch clauses can't have more than one parameter declared,
14412 but it's already enforced by the grammar. Make sure that the
14413 only parameter of the clause statement in of class Throwable
14414 or a subclass of Throwable, but that was done earlier. The
14415 catch clause parameter type has also been resolved. */
14417 /* Just make sure that the catch clause parameter type inherits
14418 from java.lang.Throwable */
14419 if (!inherits_from_p (carg_type, throwable_type_node))
14421 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14422 parse_error_context (wfl_operator,
14423 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
14424 lang_printable_name (carg_type, 0));
14425 error_found = 1;
14426 continue;
14429 /* Partial check for unreachable catch statement: The catch
14430 clause is reachable iff is no earlier catch block A in
14431 the try statement such that the type of the catch
14432 clause's parameter is the same as or a subclass of the
14433 type of A's parameter */
14434 unreachable = 0;
14435 for (sub_current = catch;
14436 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14438 tree sub_catch_clause, decl;
14439 sub_catch_clause = TREE_OPERAND (sub_current, 0);
14440 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14442 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14444 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14445 parse_error_context
14446 (wfl_operator,
14447 "`catch' not reached because of the catch clause at line %d",
14448 EXPR_WFL_LINENO (sub_current));
14449 unreachable = error_found = 1;
14450 break;
14453 /* Complete the catch clause block */
14454 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14455 if (catch_block == error_mark_node)
14457 error_found = 1;
14458 continue;
14460 if (CAN_COMPLETE_NORMALLY (catch_block))
14461 CAN_COMPLETE_NORMALLY (node) = 1;
14462 TREE_OPERAND (current, 0) = catch_block;
14464 if (unreachable)
14465 continue;
14467 /* Things to do here: the exception must be thrown */
14469 /* Link this type to the caught type list */
14470 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
14473 PUSH_EXCEPTIONS (caught_type_list);
14474 if ((try = java_complete_tree (try)) == error_mark_node)
14475 error_found = 1;
14476 if (CAN_COMPLETE_NORMALLY (try))
14477 CAN_COMPLETE_NORMALLY (node) = 1;
14478 POP_EXCEPTIONS ();
14480 /* Verification ends here */
14481 if (error_found)
14482 return error_mark_node;
14484 TREE_OPERAND (node, 0) = try;
14485 TREE_OPERAND (node, 1) = catch;
14486 TREE_TYPE (node) = void_type_node;
14487 return node;
14490 /* 14.17 The synchronized Statement */
14492 static tree
14493 patch_synchronized_statement (node, wfl_op1)
14494 tree node, wfl_op1;
14496 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
14497 tree block = TREE_OPERAND (node, 1);
14499 tree enter, exit, expr_decl, assignment;
14501 if (expr == error_mark_node)
14503 block = java_complete_tree (block);
14504 return expr;
14507 /* The TYPE of expr must be a reference type */
14508 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
14510 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14511 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
14512 lang_printable_name (TREE_TYPE (expr), 0));
14513 return error_mark_node;
14516 if (flag_emit_xref)
14518 TREE_OPERAND (node, 0) = expr;
14519 TREE_OPERAND (node, 1) = java_complete_tree (block);
14520 CAN_COMPLETE_NORMALLY (node) = 1;
14521 return node;
14524 /* Generate a try-finally for the synchronized statement, except
14525 that the handler that catches all throw exception calls
14526 _Jv_MonitorExit and then rethrow the exception.
14527 The synchronized statement is then implemented as:
14528 TRY
14530 _Jv_MonitorEnter (expression)
14531 synchronized_block
14532 _Jv_MonitorExit (expression)
14534 CATCH_ALL
14536 e = _Jv_exception_info ();
14537 _Jv_MonitorExit (expression)
14538 Throw (e);
14539 } */
14541 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14542 BUILD_MONITOR_ENTER (enter, expr_decl);
14543 BUILD_MONITOR_EXIT (exit, expr_decl);
14544 CAN_COMPLETE_NORMALLY (enter) = 1;
14545 CAN_COMPLETE_NORMALLY (exit) = 1;
14546 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14547 TREE_SIDE_EFFECTS (assignment) = 1;
14548 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14549 build (COMPOUND_EXPR, NULL_TREE,
14550 build (WITH_CLEANUP_EXPR, NULL_TREE,
14551 build (COMPOUND_EXPR, NULL_TREE,
14552 assignment, enter),
14553 NULL_TREE, exit),
14554 block));
14555 node = build_expr_block (node, expr_decl);
14557 return java_complete_tree (node);
14560 /* 14.16 The throw Statement */
14562 static tree
14563 patch_throw_statement (node, wfl_op1)
14564 tree node, wfl_op1;
14566 tree expr = TREE_OPERAND (node, 0);
14567 tree type = TREE_TYPE (expr);
14568 int unchecked_ok = 0, tryblock_throws_ok = 0;
14570 /* Thrown expression must be assignable to java.lang.Throwable */
14571 if (!try_reference_assignconv (throwable_type_node, expr))
14573 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14574 parse_error_context (wfl_operator,
14575 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
14576 lang_printable_name (type, 0));
14577 /* If the thrown expression was a reference, we further the
14578 compile-time check. */
14579 if (!JREFERENCE_TYPE_P (type))
14580 return error_mark_node;
14583 /* At least one of the following must be true */
14585 /* The type of the throw expression is a not checked exception,
14586 i.e. is a unchecked expression. */
14587 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
14589 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14590 /* An instance can't throw a checked excetion unless that exception
14591 is explicitely declared in the `throws' clause of each
14592 constructor. This doesn't apply to anonymous classes, since they
14593 don't have declared constructors. */
14594 if (!unchecked_ok
14595 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14597 tree current;
14598 for (current = TYPE_METHODS (current_class); current;
14599 current = TREE_CHAIN (current))
14600 if (DECL_CONSTRUCTOR_P (current)
14601 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14603 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)",
14604 lang_printable_name (TREE_TYPE (expr), 0));
14605 return error_mark_node;
14609 /* Throw is contained in a try statement and at least one catch
14610 clause can receive the thrown expression or the current method is
14611 declared to throw such an exception. Or, the throw statement is
14612 contained in a method or constructor declaration and the type of
14613 the Expression is assignable to at least one type listed in the
14614 throws clause the declaration. */
14615 if (!unchecked_ok)
14616 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
14617 if (!(unchecked_ok || tryblock_throws_ok))
14619 /* If there is a surrounding try block that has no matching
14620 clatch clause, report it first. A surrounding try block exits
14621 only if there is something after the list of checked
14622 exception thrown by the current function (if any). */
14623 if (IN_TRY_BLOCK_P ())
14624 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
14625 lang_printable_name (type, 0));
14626 /* If we have no surrounding try statement and the method doesn't have
14627 any throws, report it now. FIXME */
14629 /* We report that the exception can't be throw from a try block
14630 in all circumstances but when the `throw' is inside a static
14631 block. */
14632 else if (!EXCEPTIONS_P (currently_caught_type_list)
14633 && !tryblock_throws_ok)
14635 if (DECL_CLINIT_P (current_function_decl))
14636 parse_error_context (wfl_operator,
14637 "Checked exception `%s' can't be thrown in initializer",
14638 lang_printable_name (type, 0));
14639 else
14640 parse_error_context (wfl_operator,
14641 "Checked exception `%s' isn't thrown from a `try' block",
14642 lang_printable_name (type, 0));
14644 /* Otherwise, the current method doesn't have the appropriate
14645 throws declaration */
14646 else
14647 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
14648 lang_printable_name (type, 0));
14649 return error_mark_node;
14652 if (! flag_emit_class_files && ! flag_emit_xref)
14653 BUILD_THROW (node, expr);
14655 /* If doing xrefs, keep the location where the `throw' was seen. */
14656 if (flag_emit_xref)
14657 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
14658 return node;
14661 /* Check that exception said to be thrown by method DECL can be
14662 effectively caught from where DECL is invoked. */
14664 static void
14665 check_thrown_exceptions (location, decl)
14666 int location;
14667 tree decl;
14669 tree throws;
14670 /* For all the unchecked exceptions thrown by DECL */
14671 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14672 throws = TREE_CHAIN (throws))
14673 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
14675 #if 1
14676 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14677 if (DECL_NAME (decl) == get_identifier ("clone"))
14678 continue;
14679 #endif
14680 EXPR_WFL_LINECOL (wfl_operator) = location;
14681 if (DECL_FINIT_P (current_function_decl))
14682 parse_error_context
14683 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14684 lang_printable_name (TREE_VALUE (throws), 0));
14685 else
14687 parse_error_context
14688 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
14689 lang_printable_name (TREE_VALUE (throws), 0),
14690 (DECL_INIT_P (current_function_decl) ?
14691 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14692 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14697 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
14698 try-catch blocks, OR is listed in the `throws' clause of the
14699 current method. */
14701 static int
14702 check_thrown_exceptions_do (exception)
14703 tree exception;
14705 tree list = currently_caught_type_list;
14706 resolve_and_layout (exception, NULL_TREE);
14707 /* First, all the nested try-catch-finally at that stage. The
14708 last element contains `throws' clause exceptions, if any. */
14709 if (IS_UNCHECKED_EXCEPTION_P (exception))
14710 return 1;
14711 while (list)
14713 tree caught;
14714 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14715 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14716 return 1;
14717 list = TREE_CHAIN (list);
14719 return 0;
14722 static void
14723 purge_unchecked_exceptions (mdecl)
14724 tree mdecl;
14726 tree throws = DECL_FUNCTION_THROWS (mdecl);
14727 tree new = NULL_TREE;
14729 while (throws)
14731 tree next = TREE_CHAIN (throws);
14732 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
14734 TREE_CHAIN (throws) = new;
14735 new = throws;
14737 throws = next;
14739 /* List is inverted here, but it doesn't matter */
14740 DECL_FUNCTION_THROWS (mdecl) = new;
14743 /* 15.24 Conditional Operator ?: */
14745 static tree
14746 patch_conditional_expr (node, wfl_cond, wfl_op1)
14747 tree node, wfl_cond, wfl_op1;
14749 tree cond = TREE_OPERAND (node, 0);
14750 tree op1 = TREE_OPERAND (node, 1);
14751 tree op2 = TREE_OPERAND (node, 2);
14752 tree resulting_type = NULL_TREE;
14753 tree t1, t2, patched;
14754 int error_found = 0;
14756 /* Operands of ?: might be StringBuffers crafted as a result of a
14757 string concatenation. Obtain a descent operand here. */
14758 if ((patched = patch_string (op1)))
14759 TREE_OPERAND (node, 1) = op1 = patched;
14760 if ((patched = patch_string (op2)))
14761 TREE_OPERAND (node, 2) = op2 = patched;
14763 t1 = TREE_TYPE (op1);
14764 t2 = TREE_TYPE (op2);
14766 /* The first expression must be a boolean */
14767 if (TREE_TYPE (cond) != boolean_type_node)
14769 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
14770 parse_error_context (wfl_operator,
14771 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
14772 lang_printable_name (TREE_TYPE (cond), 0));
14773 error_found = 1;
14776 /* Second and third can be numeric, boolean (i.e. primitive),
14777 references or null. Anything else results in an error */
14778 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
14779 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
14780 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
14781 || (t1 == boolean_type_node && t2 == boolean_type_node)))
14782 error_found = 1;
14784 /* Determine the type of the conditional expression. Same types are
14785 easy to deal with */
14786 else if (t1 == t2)
14787 resulting_type = t1;
14789 /* There are different rules for numeric types */
14790 else if (JNUMERIC_TYPE_P (t1))
14792 /* if byte/short found, the resulting type is short */
14793 if ((t1 == byte_type_node && t2 == short_type_node)
14794 || (t1 == short_type_node && t2 == byte_type_node))
14795 resulting_type = short_type_node;
14797 /* If t1 is a constant int and t2 is of type byte, short or char
14798 and t1's value fits in t2, then the resulting type is t2 */
14799 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
14800 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
14801 resulting_type = t2;
14803 /* If t2 is a constant int and t1 is of type byte, short or char
14804 and t2's value fits in t1, then the resulting type is t1 */
14805 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
14806 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
14807 resulting_type = t1;
14809 /* Otherwise, binary numeric promotion is applied and the
14810 resulting type is the promoted type of operand 1 and 2 */
14811 else
14812 resulting_type = binary_numeric_promotion (t1, t2,
14813 &TREE_OPERAND (node, 1),
14814 &TREE_OPERAND (node, 2));
14817 /* Cases of a reference and a null type */
14818 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
14819 resulting_type = t1;
14821 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
14822 resulting_type = t2;
14824 /* Last case: different reference types. If a type can be converted
14825 into the other one by assignment conversion, the latter
14826 determines the type of the expression */
14827 else if ((resulting_type = try_reference_assignconv (t1, op2)))
14828 resulting_type = promote_type (t1);
14830 else if ((resulting_type = try_reference_assignconv (t2, op1)))
14831 resulting_type = promote_type (t2);
14833 /* If we don't have any resulting type, we're in trouble */
14834 if (!resulting_type)
14836 char *t = xstrdup (lang_printable_name (t1, 0));
14837 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14838 parse_error_context (wfl_operator,
14839 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
14840 t, lang_printable_name (t2, 0));
14841 free (t);
14842 error_found = 1;
14845 if (error_found)
14847 TREE_TYPE (node) = error_mark_node;
14848 return error_mark_node;
14851 TREE_TYPE (node) = resulting_type;
14852 TREE_SET_CODE (node, COND_EXPR);
14853 CAN_COMPLETE_NORMALLY (node) = 1;
14854 return node;
14857 /* Try to constant fold NODE.
14858 If NODE is not a constant expression, return NULL_EXPR.
14859 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
14861 static tree
14862 fold_constant_for_init (node, context)
14863 tree node;
14864 tree context;
14866 tree op0, op1, val;
14867 enum tree_code code = TREE_CODE (node);
14869 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
14870 return node;
14872 switch (code)
14874 case PLUS_EXPR:
14875 case MINUS_EXPR:
14876 case MULT_EXPR:
14877 case TRUNC_MOD_EXPR:
14878 case RDIV_EXPR:
14879 case LSHIFT_EXPR:
14880 case RSHIFT_EXPR:
14881 case URSHIFT_EXPR:
14882 case BIT_AND_EXPR:
14883 case BIT_XOR_EXPR:
14884 case BIT_IOR_EXPR:
14885 case TRUTH_ANDIF_EXPR:
14886 case TRUTH_ORIF_EXPR:
14887 case EQ_EXPR:
14888 case NE_EXPR:
14889 case GT_EXPR:
14890 case GE_EXPR:
14891 case LT_EXPR:
14892 case LE_EXPR:
14893 op0 = TREE_OPERAND (node, 0);
14894 op1 = TREE_OPERAND (node, 1);
14895 val = fold_constant_for_init (op0, context);
14896 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14897 return NULL_TREE;
14898 TREE_OPERAND (node, 0) = val;
14899 val = fold_constant_for_init (op1, context);
14900 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14901 return NULL_TREE;
14902 TREE_OPERAND (node, 1) = val;
14903 return patch_binop (node, op0, op1);
14905 case UNARY_PLUS_EXPR:
14906 case NEGATE_EXPR:
14907 case TRUTH_NOT_EXPR:
14908 case BIT_NOT_EXPR:
14909 case CONVERT_EXPR:
14910 op0 = TREE_OPERAND (node, 0);
14911 val = fold_constant_for_init (op0, context);
14912 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14913 return NULL_TREE;
14914 TREE_OPERAND (node, 0) = val;
14915 return patch_unaryop (node, op0);
14916 break;
14918 case COND_EXPR:
14919 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
14920 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14921 return NULL_TREE;
14922 TREE_OPERAND (node, 0) = val;
14923 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
14924 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14925 return NULL_TREE;
14926 TREE_OPERAND (node, 1) = val;
14927 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
14928 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14929 return NULL_TREE;
14930 TREE_OPERAND (node, 2) = val;
14931 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
14932 : TREE_OPERAND (node, 2);
14934 case VAR_DECL:
14935 case FIELD_DECL:
14936 if (! FIELD_FINAL (node)
14937 || DECL_INITIAL (node) == NULL_TREE)
14938 return NULL_TREE;
14939 val = DECL_INITIAL (node);
14940 /* Guard against infinite recursion. */
14941 DECL_INITIAL (node) = NULL_TREE;
14942 val = fold_constant_for_init (val, node);
14943 DECL_INITIAL (node) = val;
14944 return val;
14946 case EXPR_WITH_FILE_LOCATION:
14947 /* Compare java_complete_tree and resolve_expression_name. */
14948 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
14949 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
14951 tree name = EXPR_WFL_NODE (node);
14952 tree decl;
14953 if (PRIMARY_P (node))
14954 return NULL_TREE;
14955 else if (! QUALIFIED_P (name))
14957 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
14958 if (decl == NULL_TREE
14959 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
14960 return NULL_TREE;
14961 return fold_constant_for_init (decl, decl);
14963 else
14965 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
14966 qualify_ambiguous_name (node);
14967 if (resolve_field_access (node, &decl, NULL)
14968 && decl != NULL_TREE)
14969 return fold_constant_for_init (decl, decl);
14970 return NULL_TREE;
14973 else
14975 op0 = TREE_OPERAND (node, 0);
14976 val = fold_constant_for_init (op0, context);
14977 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14978 return NULL_TREE;
14979 TREE_OPERAND (node, 0) = val;
14980 return val;
14983 #ifdef USE_COMPONENT_REF
14984 case IDENTIFIER:
14985 case COMPONENT_REF:
14987 #endif
14989 default:
14990 return NULL_TREE;
14994 #ifdef USE_COMPONENT_REF
14995 /* Context is 'T' for TypeName, 'P' for PackageName,
14996 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
14998 tree
14999 resolve_simple_name (name, context)
15000 tree name;
15001 int context;
15005 tree
15006 resolve_qualified_name (name, context)
15007 tree name;
15008 int context;
15011 #endif