2000-08-29 Alexandre Petit-Bianco <apbianco@cygnus.com>
[official-gcc.git] / gcc / java / parse.y
blobd67b6c7cc71c56476c6c0630a25df0d7a4e4d2e7
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 int 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) % ARRAY_SIZE (binop_lookup)]
360 /* This is the end index for binary operators that can also be used
361 in compound assignements. */
362 #define BINOP_COMPOUND_CANDIDATES 11
364 /* Fake WFL used to report error message. It is initialized once if
365 needed and reused with it's location information is overriden. */
366 tree wfl_operator = NULL_TREE;
368 /* The "$L" identifier we use to create labels. */
369 static tree label_id = NULL_TREE;
371 /* The "StringBuffer" identifier used for the String `+' operator. */
372 static tree wfl_string_buffer = NULL_TREE;
374 /* The "append" identifier used for String `+' operator. */
375 static tree wfl_append = NULL_TREE;
377 /* The "toString" identifier used for String `+' operator. */
378 static tree wfl_to_string = NULL_TREE;
380 /* The "java.lang" import qualified name. */
381 static tree java_lang_id = NULL_TREE;
383 /* The generated `inst$' identifier used for generated enclosing
384 instance/field access functions. */
385 static tree inst_id = NULL_TREE;
387 /* The "java.lang.Cloneable" qualified name. */
388 static tree java_lang_cloneable = NULL_TREE;
390 /* Context and flag for static blocks */
391 static tree current_static_block = NULL_TREE;
393 /* The generated `write_parm_value$' identifier. */
394 static tree wpv_id;
396 /* The list of all packages we've seen so far */
397 static tree package_list = NULL_TREE;
399 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
400 line and point it out. */
401 /* Should point out the one that don't fit. ASCII/unicode, going
402 backward. FIXME */
404 #define check_modifiers(__message, __value, __mask) do { \
405 if ((__value) & ~(__mask)) \
407 int i, remainder = (__value) & ~(__mask); \
408 for (i = 0; i <= 10; i++) \
409 if ((1 << i) & remainder) \
410 parse_error_context (ctxp->modifier_ctx [i], (__message), \
411 java_accstring_lookup (1 << i)); \
413 } while (0)
417 %union {
418 tree node;
419 int sub_token;
420 struct {
421 int token;
422 int location;
423 } operator;
424 int value;
428 #include "lex.c"
431 %pure_parser
433 /* Things defined here have to match the order of what's in the
434 binop_lookup table. */
436 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
437 %token LS_TK SRS_TK ZRS_TK
438 %token AND_TK XOR_TK OR_TK
439 %token BOOL_AND_TK BOOL_OR_TK
440 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
442 /* This maps to the same binop_lookup entry than the token above */
444 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
445 %token REM_ASSIGN_TK
446 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
447 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
450 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
452 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
453 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
454 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
455 %token PAD_TK ABSTRACT_TK MODIFIER_TK
457 /* Keep those two in order, too */
458 %token DECR_TK INCR_TK
460 /* From now one, things can be in any order */
462 %token DEFAULT_TK IF_TK THROW_TK
463 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
464 %token THROWS_TK BREAK_TK IMPORT_TK
465 %token ELSE_TK INSTANCEOF_TK RETURN_TK
466 %token VOID_TK CATCH_TK INTERFACE_TK
467 %token CASE_TK EXTENDS_TK FINALLY_TK
468 %token SUPER_TK WHILE_TK CLASS_TK
469 %token SWITCH_TK CONST_TK TRY_TK
470 %token FOR_TK NEW_TK CONTINUE_TK
471 %token GOTO_TK PACKAGE_TK THIS_TK
473 %token BYTE_TK SHORT_TK INT_TK LONG_TK
474 %token CHAR_TK INTEGRAL_TK
476 %token FLOAT_TK DOUBLE_TK FP_TK
478 %token ID_TK
480 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
482 %token ASSIGN_ANY_TK ASSIGN_TK
483 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
485 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
486 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
488 %type <value> modifiers MODIFIER_TK final synchronized
490 %type <node> super ID_TK identifier
491 %type <node> name simple_name qualified_name
492 %type <node> type_declaration compilation_unit
493 field_declaration method_declaration extends_interfaces
494 interfaces interface_type_list
495 class_member_declaration
496 import_declarations package_declaration
497 type_declarations interface_body
498 interface_member_declaration constant_declaration
499 interface_member_declarations interface_type
500 abstract_method_declaration interface_type_list
501 %type <node> class_body_declaration class_member_declaration
502 static_initializer constructor_declaration block
503 %type <node> class_body_declarations constructor_header
504 %type <node> class_or_interface_type class_type class_type_list
505 constructor_declarator explicit_constructor_invocation
506 %type <node> dim_expr dim_exprs this_or_super throws
508 %type <node> variable_declarator_id variable_declarator
509 variable_declarators variable_initializer
510 variable_initializers constructor_body
511 array_initializer
513 %type <node> class_body block_end constructor_block_end
514 %type <node> statement statement_without_trailing_substatement
515 labeled_statement if_then_statement label_decl
516 if_then_else_statement while_statement for_statement
517 statement_nsi labeled_statement_nsi do_statement
518 if_then_else_statement_nsi while_statement_nsi
519 for_statement_nsi statement_expression_list for_init
520 for_update statement_expression expression_statement
521 primary_no_new_array expression primary
522 array_creation_expression array_type
523 class_instance_creation_expression field_access
524 method_invocation array_access something_dot_new
525 argument_list postfix_expression while_expression
526 post_increment_expression post_decrement_expression
527 unary_expression_not_plus_minus unary_expression
528 pre_increment_expression pre_decrement_expression
529 unary_expression_not_plus_minus cast_expression
530 multiplicative_expression additive_expression
531 shift_expression relational_expression
532 equality_expression and_expression
533 exclusive_or_expression inclusive_or_expression
534 conditional_and_expression conditional_or_expression
535 conditional_expression assignment_expression
536 left_hand_side assignment for_header for_begin
537 constant_expression do_statement_begin empty_statement
538 switch_statement synchronized_statement throw_statement
539 try_statement switch_expression switch_block
540 catches catch_clause catch_clause_parameter finally
541 anonymous_class_creation
542 %type <node> return_statement break_statement continue_statement
544 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
545 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
546 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
547 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
548 %type <operator> ASSIGN_ANY_TK assignment_operator
549 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
550 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
551 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
552 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
553 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
554 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
555 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
556 %type <operator> NEW_TK
558 %type <node> method_body
560 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
561 STRING_LIT_TK NULL_TK VOID_TK
563 %type <node> IF_TK WHILE_TK FOR_TK
565 %type <node> formal_parameter_list formal_parameter
566 method_declarator method_header
568 %type <node> primitive_type reference_type type
569 BOOLEAN_TK INTEGRAL_TK FP_TK
571 /* Added or modified JDK 1.1 rule types */
572 %type <node> type_literals array_type_literal
575 /* 19.2 Production from 2.3: The Syntactic Grammar */
576 goal:
577 compilation_unit
581 /* 19.3 Productions from 3: Lexical structure */
582 literal:
583 INT_LIT_TK
584 | FP_LIT_TK
585 | BOOL_LIT_TK
586 | CHAR_LIT_TK
587 | STRING_LIT_TK
588 | NULL_TK
591 /* 19.4 Productions from 4: Types, Values and Variables */
592 type:
593 primitive_type
594 | reference_type
597 primitive_type:
598 INTEGRAL_TK
599 | FP_TK
600 | BOOLEAN_TK
603 reference_type:
604 class_or_interface_type
605 | array_type
608 class_or_interface_type:
609 name
612 class_type:
613 class_or_interface_type /* Default rule */
616 interface_type:
617 class_or_interface_type
620 array_type:
621 primitive_type OSB_TK CSB_TK
623 $$ = build_java_array_type ($1, -1);
624 CLASS_LOADED_P ($$) = 1;
626 | name OSB_TK CSB_TK
627 { $$ = build_unresolved_array_type ($1); }
628 | array_type OSB_TK CSB_TK
629 { $$ = build_unresolved_array_type ($1); }
630 | primitive_type OSB_TK error
631 {RULE ("']' expected"); RECOVER;}
632 | array_type OSB_TK error
633 {RULE ("']' expected"); RECOVER;}
636 /* 19.5 Productions from 6: Names */
637 name:
638 simple_name /* Default rule */
639 | qualified_name /* Default rule */
642 simple_name:
643 identifier /* Default rule */
646 qualified_name:
647 name DOT_TK identifier
648 { $$ = make_qualified_name ($1, $3, $2.location); }
651 identifier:
652 ID_TK
655 /* 19.6: Production from 7: Packages */
656 compilation_unit:
657 {$$ = NULL;}
658 | package_declaration
659 | import_declarations
660 | type_declarations
661 | package_declaration import_declarations
662 | package_declaration type_declarations
663 | import_declarations type_declarations
664 | package_declaration import_declarations type_declarations
667 import_declarations:
668 import_declaration
670 $$ = NULL;
672 | import_declarations import_declaration
674 $$ = NULL;
678 type_declarations:
679 type_declaration
680 | type_declarations type_declaration
683 package_declaration:
684 PACKAGE_TK name SC_TK
686 ctxp->package = EXPR_WFL_NODE ($2);
687 register_package (ctxp->package);
689 | PACKAGE_TK error
690 {yyerror ("Missing name"); RECOVER;}
691 | PACKAGE_TK name error
692 {yyerror ("';' expected"); RECOVER;}
695 import_declaration:
696 single_type_import_declaration
697 | type_import_on_demand_declaration
700 single_type_import_declaration:
701 IMPORT_TK name SC_TK
703 tree name = EXPR_WFL_NODE ($2), last_name;
704 int i = IDENTIFIER_LENGTH (name)-1;
705 const char *last = &IDENTIFIER_POINTER (name)[i];
706 while (last != IDENTIFIER_POINTER (name))
708 if (last [0] == '.')
709 break;
710 last--;
712 last_name = get_identifier (++last);
713 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
715 tree err = find_name_in_single_imports (last_name);
716 if (err && err != name)
717 parse_error_context
718 ($2, "Ambiguous class: `%s' and `%s'",
719 IDENTIFIER_POINTER (name),
720 IDENTIFIER_POINTER (err));
721 else
722 REGISTER_IMPORT ($2, last_name);
724 else
725 REGISTER_IMPORT ($2, last_name);
727 | IMPORT_TK error
728 {yyerror ("Missing name"); RECOVER;}
729 | IMPORT_TK name error
730 {yyerror ("';' expected"); RECOVER;}
733 type_import_on_demand_declaration:
734 IMPORT_TK name DOT_TK MULT_TK SC_TK
736 tree name = EXPR_WFL_NODE ($2);
737 /* Don't import java.lang.* twice. */
738 if (name != java_lang_id)
740 read_import_dir ($2);
741 ctxp->import_demand_list =
742 chainon (ctxp->import_demand_list,
743 build_tree_list ($2, NULL_TREE));
746 | IMPORT_TK name DOT_TK error
747 {yyerror ("'*' expected"); RECOVER;}
748 | IMPORT_TK name DOT_TK MULT_TK error
749 {yyerror ("';' expected"); RECOVER;}
752 type_declaration:
753 class_declaration
754 { end_class_declaration (0); }
755 | interface_declaration
756 { end_class_declaration (0); }
757 | empty_statement
758 | error
760 YYERROR_NOW;
761 yyerror ("Class or interface declaration expected");
765 /* 19.7 Shortened from the original:
766 modifiers: modifier | modifiers modifier
767 modifier: any of public... */
768 modifiers:
769 MODIFIER_TK
771 $$ = (1 << $1);
773 | modifiers MODIFIER_TK
775 int acc = (1 << $2);
776 if ($$ & acc)
777 parse_error_context
778 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
779 java_accstring_lookup (acc));
780 else
782 $$ |= acc;
787 /* 19.8.1 Production from $8.1: Class Declaration */
788 class_declaration:
789 modifiers CLASS_TK identifier super interfaces
790 { create_class ($1, $3, $4, $5); }
791 class_body
792 | CLASS_TK identifier super interfaces
793 { create_class (0, $2, $3, $4); }
794 class_body
795 | modifiers CLASS_TK error
796 {yyerror ("Missing class name"); RECOVER;}
797 | CLASS_TK error
798 {yyerror ("Missing class name"); RECOVER;}
799 | CLASS_TK identifier error
801 if (!ctxp->class_err) yyerror ("'{' expected");
802 DRECOVER(class1);
804 | modifiers CLASS_TK identifier error
805 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
808 super:
809 { $$ = NULL; }
810 | EXTENDS_TK class_type
811 { $$ = $2; }
812 | EXTENDS_TK class_type error
813 {yyerror ("'{' expected"); ctxp->class_err=1;}
814 | EXTENDS_TK error
815 {yyerror ("Missing super class name"); ctxp->class_err=1;}
818 interfaces:
819 { $$ = NULL_TREE; }
820 | IMPLEMENTS_TK interface_type_list
821 { $$ = $2; }
822 | IMPLEMENTS_TK error
824 ctxp->class_err=1;
825 yyerror ("Missing interface name");
829 interface_type_list:
830 interface_type
832 ctxp->interface_number = 1;
833 $$ = build_tree_list ($1, NULL_TREE);
835 | interface_type_list C_TK interface_type
837 ctxp->interface_number++;
838 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
840 | interface_type_list C_TK error
841 {yyerror ("Missing interface name"); RECOVER;}
844 class_body:
845 OCB_TK CCB_TK
847 /* Store the location of the `}' when doing xrefs */
848 if (flag_emit_xref)
849 DECL_END_SOURCE_LINE (GET_CPC ()) =
850 EXPR_WFL_ADD_COL ($2.location, 1);
851 $$ = GET_CPC ();
853 | OCB_TK class_body_declarations CCB_TK
855 /* Store the location of the `}' when doing xrefs */
856 if (flag_emit_xref)
857 DECL_END_SOURCE_LINE (GET_CPC ()) =
858 EXPR_WFL_ADD_COL ($3.location, 1);
859 $$ = GET_CPC ();
863 class_body_declarations:
864 class_body_declaration
865 | class_body_declarations class_body_declaration
868 class_body_declaration:
869 class_member_declaration
870 | static_initializer
871 | constructor_declaration
872 | block /* Added, JDK1.1, instance initializer */
874 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
875 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
879 class_member_declaration:
880 field_declaration
881 | method_declaration
882 | class_declaration /* Added, JDK1.1 inner classes */
883 { end_class_declaration (1); }
884 | interface_declaration /* Added, JDK1.1 inner interfaces */
885 { end_class_declaration (1); }
886 | empty_statement
889 /* 19.8.2 Productions from 8.3: Field Declarations */
890 field_declaration:
891 type variable_declarators SC_TK
892 { register_fields (0, $1, $2); }
893 | modifiers type variable_declarators SC_TK
895 check_modifiers
896 ("Illegal modifier `%s' for field declaration",
897 $1, FIELD_MODIFIERS);
898 check_modifiers_consistency ($1);
899 register_fields ($1, $2, $3);
903 variable_declarators:
904 /* Should we use build_decl_list () instead ? FIXME */
905 variable_declarator /* Default rule */
906 | variable_declarators C_TK variable_declarator
907 { $$ = chainon ($1, $3); }
908 | variable_declarators C_TK error
909 {yyerror ("Missing term"); RECOVER;}
912 variable_declarator:
913 variable_declarator_id
914 { $$ = build_tree_list ($1, NULL_TREE); }
915 | variable_declarator_id ASSIGN_TK variable_initializer
917 if (java_error_count)
918 $3 = NULL_TREE;
919 $$ = build_tree_list
920 ($1, build_assignment ($2.token, $2.location, $1, $3));
922 | variable_declarator_id ASSIGN_TK error
924 yyerror ("Missing variable initializer");
925 $$ = build_tree_list ($1, NULL_TREE);
926 RECOVER;
928 | variable_declarator_id ASSIGN_TK variable_initializer error
930 yyerror ("';' expected");
931 $$ = build_tree_list ($1, NULL_TREE);
932 RECOVER;
936 variable_declarator_id:
937 identifier
938 | variable_declarator_id OSB_TK CSB_TK
939 { $$ = build_unresolved_array_type ($1); }
940 | identifier error
941 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
942 | variable_declarator_id OSB_TK error
943 {yyerror ("']' expected"); DRECOVER(vdi);}
944 | variable_declarator_id CSB_TK error
945 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
948 variable_initializer:
949 expression
950 | array_initializer
953 /* 19.8.3 Productions from 8.4: Method Declarations */
954 method_declaration:
955 method_header
957 current_function_decl = $1;
958 if (current_function_decl
959 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
960 source_start_java_method (current_function_decl);
961 else
962 current_function_decl = NULL_TREE;
964 method_body
965 { finish_method_declaration ($3); }
966 | method_header error
967 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
970 method_header:
971 type method_declarator throws
972 { $$ = method_header (0, $1, $2, $3); }
973 | VOID_TK method_declarator throws
974 { $$ = method_header (0, void_type_node, $2, $3); }
975 | modifiers type method_declarator throws
976 { $$ = method_header ($1, $2, $3, $4); }
977 | modifiers VOID_TK method_declarator throws
978 { $$ = method_header ($1, void_type_node, $3, $4); }
979 | type error
981 yyerror ("Invalid method declaration, method name required");
982 RECOVER;
984 | modifiers type error
985 {RECOVER;}
986 | VOID_TK error
987 {yyerror ("Identifier expected"); RECOVER;}
988 | modifiers VOID_TK error
989 {yyerror ("Identifier expected"); RECOVER;}
990 | modifiers error
992 yyerror ("Invalid method declaration, return type required");
993 RECOVER;
997 method_declarator:
998 identifier OP_TK CP_TK
1000 ctxp->formal_parameter_number = 0;
1001 $$ = method_declarator ($1, NULL_TREE);
1003 | identifier OP_TK formal_parameter_list CP_TK
1004 { $$ = method_declarator ($1, $3); }
1005 | method_declarator OSB_TK CSB_TK
1007 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1008 TREE_PURPOSE ($1) =
1009 build_unresolved_array_type (TREE_PURPOSE ($1));
1010 parse_warning_context
1011 (wfl_operator,
1012 "Discouraged form of returned type specification");
1014 | identifier OP_TK error
1015 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1016 | method_declarator OSB_TK error
1017 {yyerror ("']' expected"); RECOVER;}
1020 formal_parameter_list:
1021 formal_parameter
1023 ctxp->formal_parameter_number = 1;
1025 | formal_parameter_list C_TK formal_parameter
1027 ctxp->formal_parameter_number += 1;
1028 $$ = chainon ($1, $3);
1030 | formal_parameter_list C_TK error
1031 { yyerror ("Missing formal parameter term"); RECOVER; }
1034 formal_parameter:
1035 type variable_declarator_id
1037 $$ = build_tree_list ($2, $1);
1039 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1041 $$ = build_tree_list ($3, $2);
1042 ARG_FINAL_P ($$) = 1;
1044 | type error
1046 yyerror ("Missing identifier"); RECOVER;
1047 $$ = NULL_TREE;
1049 | final type error
1051 yyerror ("Missing identifier"); RECOVER;
1052 $$ = NULL_TREE;
1056 final:
1057 modifiers
1059 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1060 $1, ACC_FINAL);
1061 if ($1 != ACC_FINAL)
1062 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1066 throws:
1067 { $$ = NULL_TREE; }
1068 | THROWS_TK class_type_list
1069 { $$ = $2; }
1070 | THROWS_TK error
1071 {yyerror ("Missing class type term"); RECOVER;}
1074 class_type_list:
1075 class_type
1076 { $$ = build_tree_list ($1, $1); }
1077 | class_type_list C_TK class_type
1078 { $$ = tree_cons ($3, $3, $1); }
1079 | class_type_list C_TK error
1080 {yyerror ("Missing class type term"); RECOVER;}
1083 method_body:
1084 block
1085 | SC_TK { $$ = NULL_TREE; }
1088 /* 19.8.4 Productions from 8.5: Static Initializers */
1089 static_initializer:
1090 static block
1092 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1093 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1097 static: /* Test lval.sub_token here */
1098 modifiers
1100 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1101 /* Can't have a static initializer in an innerclass */
1102 if ($1 | ACC_STATIC &&
1103 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1104 parse_error_context
1105 (MODIFIER_WFL (STATIC_TK),
1106 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1107 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1108 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1112 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1113 constructor_declaration:
1114 constructor_header
1116 current_function_decl = $1;
1117 source_start_java_method (current_function_decl);
1119 constructor_body
1120 { finish_method_declaration ($3); }
1123 constructor_header:
1124 constructor_declarator throws
1125 { $$ = method_header (0, NULL_TREE, $1, $2); }
1126 | modifiers constructor_declarator throws
1127 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1130 constructor_declarator:
1131 simple_name OP_TK CP_TK
1133 ctxp->formal_parameter_number = 0;
1134 $$ = method_declarator ($1, NULL_TREE);
1136 | simple_name OP_TK formal_parameter_list CP_TK
1137 { $$ = method_declarator ($1, $3); }
1140 constructor_body:
1141 /* Unlike regular method, we always need a complete (empty)
1142 body so we can safely perform all the required code
1143 addition (super invocation and field initialization) */
1144 block_begin constructor_block_end
1146 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1147 $$ = $2;
1149 | block_begin explicit_constructor_invocation constructor_block_end
1150 { $$ = $3; }
1151 | block_begin block_statements constructor_block_end
1152 { $$ = $3; }
1153 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1154 { $$ = $4; }
1157 constructor_block_end:
1158 block_end
1161 /* Error recovery for that rule moved down expression_statement: rule. */
1162 explicit_constructor_invocation:
1163 this_or_super OP_TK CP_TK SC_TK
1165 $$ = build_method_invocation ($1, NULL_TREE);
1166 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1167 $$ = java_method_add_stmt (current_function_decl, $$);
1169 | this_or_super OP_TK argument_list CP_TK SC_TK
1171 $$ = build_method_invocation ($1, $3);
1172 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1173 $$ = java_method_add_stmt (current_function_decl, $$);
1175 /* Added, JDK1.1 inner classes. Modified because the rule
1176 'primary' couldn't work. */
1177 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1178 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1179 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1180 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1183 this_or_super: /* Added, simplifies error diagnostics */
1184 THIS_TK
1186 tree wfl = build_wfl_node (this_identifier_node);
1187 EXPR_WFL_LINECOL (wfl) = $1.location;
1188 $$ = wfl;
1190 | SUPER_TK
1192 tree wfl = build_wfl_node (super_identifier_node);
1193 EXPR_WFL_LINECOL (wfl) = $1.location;
1194 $$ = wfl;
1198 /* 19.9 Productions from 9: Interfaces */
1199 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1200 interface_declaration:
1201 INTERFACE_TK identifier
1202 { create_interface (0, $2, NULL_TREE); }
1203 interface_body
1204 | modifiers INTERFACE_TK identifier
1205 { create_interface ($1, $3, NULL_TREE); }
1206 interface_body
1207 | INTERFACE_TK identifier extends_interfaces
1208 { create_interface (0, $2, $3); }
1209 interface_body
1210 | modifiers INTERFACE_TK identifier extends_interfaces
1211 { create_interface ($1, $3, $4); }
1212 interface_body
1213 | INTERFACE_TK identifier error
1214 {yyerror ("'{' expected"); RECOVER;}
1215 | modifiers INTERFACE_TK identifier error
1216 {yyerror ("'{' expected"); RECOVER;}
1219 extends_interfaces:
1220 EXTENDS_TK interface_type
1222 ctxp->interface_number = 1;
1223 $$ = build_tree_list ($2, NULL_TREE);
1225 | extends_interfaces C_TK interface_type
1227 ctxp->interface_number++;
1228 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1230 | EXTENDS_TK error
1231 {yyerror ("Invalid interface type"); RECOVER;}
1232 | extends_interfaces C_TK error
1233 {yyerror ("Missing term"); RECOVER;}
1236 interface_body:
1237 OCB_TK CCB_TK
1238 { $$ = NULL_TREE; }
1239 | OCB_TK interface_member_declarations CCB_TK
1240 { $$ = NULL_TREE; }
1243 interface_member_declarations:
1244 interface_member_declaration
1245 | interface_member_declarations interface_member_declaration
1248 interface_member_declaration:
1249 constant_declaration
1250 | abstract_method_declaration
1251 | class_declaration /* Added, JDK1.1 inner classes */
1252 { end_class_declaration (1); }
1253 | interface_declaration /* Added, JDK1.1 inner interfaces */
1254 { end_class_declaration (1); }
1257 constant_declaration:
1258 field_declaration
1261 abstract_method_declaration:
1262 method_header SC_TK
1264 check_abstract_method_header ($1);
1265 current_function_decl = NULL_TREE; /* FIXME ? */
1267 | method_header error
1268 {yyerror ("';' expected"); RECOVER;}
1271 /* 19.10 Productions from 10: Arrays */
1272 array_initializer:
1273 OCB_TK CCB_TK
1274 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1275 | OCB_TK variable_initializers CCB_TK
1276 { $$ = build_new_array_init ($1.location, $2); }
1277 | OCB_TK variable_initializers C_TK CCB_TK
1278 { $$ = build_new_array_init ($1.location, $2); }
1281 variable_initializers:
1282 variable_initializer
1284 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1285 $1, NULL_TREE);
1287 | variable_initializers C_TK variable_initializer
1289 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1291 | variable_initializers C_TK error
1292 {yyerror ("Missing term"); RECOVER;}
1295 /* 19.11 Production from 14: Blocks and Statements */
1296 block:
1297 OCB_TK CCB_TK
1299 /* Store the location of the `}' when doing xrefs */
1300 if (current_function_decl && flag_emit_xref)
1301 DECL_END_SOURCE_LINE (current_function_decl) =
1302 EXPR_WFL_ADD_COL ($2.location, 1);
1303 $$ = empty_stmt_node;
1305 | block_begin block_statements block_end
1306 { $$ = $3; }
1309 block_begin:
1310 OCB_TK
1311 { enter_block (); }
1314 block_end:
1315 CCB_TK
1317 maybe_absorb_scoping_blocks ();
1318 /* Store the location of the `}' when doing xrefs */
1319 if (current_function_decl && flag_emit_xref)
1320 DECL_END_SOURCE_LINE (current_function_decl) =
1321 EXPR_WFL_ADD_COL ($1.location, 1);
1322 $$ = exit_block ();
1323 if (!BLOCK_SUBBLOCKS ($$))
1324 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1328 block_statements:
1329 block_statement
1330 | block_statements block_statement
1333 block_statement:
1334 local_variable_declaration_statement
1335 | statement
1336 { java_method_add_stmt (current_function_decl, $1); }
1337 | class_declaration /* Added, JDK1.1 local classes */
1339 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1340 end_class_declaration (1);
1344 local_variable_declaration_statement:
1345 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1348 local_variable_declaration:
1349 type variable_declarators
1350 { declare_local_variables (0, $1, $2); }
1351 | final type variable_declarators /* Added, JDK1.1 final locals */
1352 { declare_local_variables ($1, $2, $3); }
1355 statement:
1356 statement_without_trailing_substatement
1357 | labeled_statement
1358 | if_then_statement
1359 | if_then_else_statement
1360 | while_statement
1361 | for_statement
1362 { $$ = exit_block (); }
1365 statement_nsi:
1366 statement_without_trailing_substatement
1367 | labeled_statement_nsi
1368 | if_then_else_statement_nsi
1369 | while_statement_nsi
1370 | for_statement_nsi
1371 { $$ = exit_block (); }
1374 statement_without_trailing_substatement:
1375 block
1376 | empty_statement
1377 | expression_statement
1378 | switch_statement
1379 | do_statement
1380 | break_statement
1381 | continue_statement
1382 | return_statement
1383 | synchronized_statement
1384 | throw_statement
1385 | try_statement
1388 empty_statement:
1389 SC_TK
1391 if (flag_extraneous_semicolon)
1393 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1394 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1396 $$ = empty_stmt_node;
1400 label_decl:
1401 identifier REL_CL_TK
1403 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1404 EXPR_WFL_NODE ($1));
1405 pushlevel (2);
1406 push_labeled_block ($$);
1407 PUSH_LABELED_BLOCK ($$);
1411 labeled_statement:
1412 label_decl statement
1413 { $$ = finish_labeled_statement ($1, $2); }
1414 | identifier error
1415 {yyerror ("':' expected"); RECOVER;}
1418 labeled_statement_nsi:
1419 label_decl statement_nsi
1420 { $$ = finish_labeled_statement ($1, $2); }
1423 /* We concentrate here a bunch of error handling rules that we couldn't write
1424 earlier, because expression_statement catches a missing ';'. */
1425 expression_statement:
1426 statement_expression SC_TK
1428 /* We have a statement. Generate a WFL around it so
1429 we can debug it */
1430 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1431 /* We know we have a statement, so set the debug
1432 info to be eventually generate here. */
1433 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1435 | error SC_TK
1437 if (ctxp->prevent_ese != lineno)
1438 yyerror ("Invalid expression statement");
1439 DRECOVER (expr_stmt);
1441 | error OCB_TK
1443 if (ctxp->prevent_ese != lineno)
1444 yyerror ("Invalid expression statement");
1445 DRECOVER (expr_stmt);
1447 | error CCB_TK
1449 if (ctxp->prevent_ese != lineno)
1450 yyerror ("Invalid expression statement");
1451 DRECOVER (expr_stmt);
1453 | this_or_super OP_TK error
1454 {yyerror ("')' expected"); RECOVER;}
1455 | this_or_super OP_TK CP_TK error
1457 parse_ctor_invocation_error ();
1458 RECOVER;
1460 | this_or_super OP_TK argument_list error
1461 {yyerror ("')' expected"); RECOVER;}
1462 | this_or_super OP_TK argument_list CP_TK error
1464 parse_ctor_invocation_error ();
1465 RECOVER;
1467 | name DOT_TK SUPER_TK error
1468 {yyerror ("'(' expected"); RECOVER;}
1469 | name DOT_TK SUPER_TK OP_TK error
1470 {yyerror ("')' expected"); RECOVER;}
1471 | name DOT_TK SUPER_TK OP_TK argument_list error
1472 {yyerror ("')' expected"); RECOVER;}
1473 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1474 {yyerror ("';' expected"); RECOVER;}
1475 | name DOT_TK SUPER_TK OP_TK CP_TK error
1476 {yyerror ("';' expected"); RECOVER;}
1479 statement_expression:
1480 assignment
1481 | pre_increment_expression
1482 | pre_decrement_expression
1483 | post_increment_expression
1484 | post_decrement_expression
1485 | method_invocation
1486 | class_instance_creation_expression
1489 if_then_statement:
1490 IF_TK OP_TK expression CP_TK statement
1492 $$ = build_if_else_statement ($2.location, $3,
1493 $5, NULL_TREE);
1495 | IF_TK error
1496 {yyerror ("'(' expected"); RECOVER;}
1497 | IF_TK OP_TK error
1498 {yyerror ("Missing term"); RECOVER;}
1499 | IF_TK OP_TK expression error
1500 {yyerror ("')' expected"); RECOVER;}
1503 if_then_else_statement:
1504 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1505 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1508 if_then_else_statement_nsi:
1509 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1510 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1513 switch_statement:
1514 switch_expression
1516 enter_block ();
1518 switch_block
1520 /* Make into "proper list" of COMPOUND_EXPRs.
1521 I.e. make the last statment also have its own
1522 COMPOUND_EXPR. */
1523 maybe_absorb_scoping_blocks ();
1524 TREE_OPERAND ($1, 1) = exit_block ();
1525 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1529 switch_expression:
1530 SWITCH_TK OP_TK expression CP_TK
1532 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1533 EXPR_WFL_LINECOL ($$) = $2.location;
1535 | SWITCH_TK error
1536 {yyerror ("'(' expected"); RECOVER;}
1537 | SWITCH_TK OP_TK error
1538 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1539 | SWITCH_TK OP_TK expression CP_TK error
1540 {yyerror ("'{' expected"); RECOVER;}
1543 /* Default assignment is there to avoid type node on switch_block
1544 node. */
1546 switch_block:
1547 OCB_TK CCB_TK
1548 { $$ = NULL_TREE; }
1549 | OCB_TK switch_labels CCB_TK
1550 { $$ = NULL_TREE; }
1551 | OCB_TK switch_block_statement_groups CCB_TK
1552 { $$ = NULL_TREE; }
1553 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1554 { $$ = NULL_TREE; }
1557 switch_block_statement_groups:
1558 switch_block_statement_group
1559 | switch_block_statement_groups switch_block_statement_group
1562 switch_block_statement_group:
1563 switch_labels block_statements
1566 switch_labels:
1567 switch_label
1568 | switch_labels switch_label
1571 switch_label:
1572 CASE_TK constant_expression REL_CL_TK
1574 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1575 EXPR_WFL_LINECOL (lab) = $1.location;
1576 java_method_add_stmt (current_function_decl, lab);
1578 | DEFAULT_TK REL_CL_TK
1580 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1581 EXPR_WFL_LINECOL (lab) = $1.location;
1582 java_method_add_stmt (current_function_decl, lab);
1584 | CASE_TK error
1585 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1586 | CASE_TK constant_expression error
1587 {yyerror ("':' expected"); RECOVER;}
1588 | DEFAULT_TK error
1589 {yyerror ("':' expected"); RECOVER;}
1592 while_expression:
1593 WHILE_TK OP_TK expression CP_TK
1595 tree body = build_loop_body ($2.location, $3, 0);
1596 $$ = build_new_loop (body);
1600 while_statement:
1601 while_expression statement
1602 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1603 | WHILE_TK error
1604 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1605 | WHILE_TK OP_TK error
1606 {yyerror ("Missing term and ')' expected"); RECOVER;}
1607 | WHILE_TK OP_TK expression error
1608 {yyerror ("')' expected"); RECOVER;}
1611 while_statement_nsi:
1612 while_expression statement_nsi
1613 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1616 do_statement_begin:
1617 DO_TK
1619 tree body = build_loop_body (0, NULL_TREE, 1);
1620 $$ = build_new_loop (body);
1622 /* Need error handing here. FIXME */
1625 do_statement:
1626 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1627 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1630 for_statement:
1631 for_begin SC_TK expression SC_TK for_update CP_TK statement
1633 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1634 $3 = build_wfl_node ($3);
1635 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1637 | for_begin SC_TK SC_TK for_update CP_TK statement
1639 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1640 /* We have not condition, so we get rid of the EXIT_EXPR */
1641 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1642 empty_stmt_node;
1644 | for_begin SC_TK error
1645 {yyerror ("Invalid control expression"); RECOVER;}
1646 | for_begin SC_TK expression SC_TK error
1647 {yyerror ("Invalid update expression"); RECOVER;}
1648 | for_begin SC_TK SC_TK error
1649 {yyerror ("Invalid update expression"); RECOVER;}
1652 for_statement_nsi:
1653 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1654 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1655 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1657 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1658 /* We have not condition, so we get rid of the EXIT_EXPR */
1659 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1660 empty_stmt_node;
1664 for_header:
1665 FOR_TK OP_TK
1667 /* This scope defined for local variable that may be
1668 defined within the scope of the for loop */
1669 enter_block ();
1671 | FOR_TK error
1672 {yyerror ("'(' expected"); DRECOVER(for_1);}
1673 | FOR_TK OP_TK error
1674 {yyerror ("Invalid init statement"); RECOVER;}
1677 for_begin:
1678 for_header for_init
1680 /* We now declare the loop body. The loop is
1681 declared as a for loop. */
1682 tree body = build_loop_body (0, NULL_TREE, 0);
1683 $$ = build_new_loop (body);
1684 FOR_LOOP_P ($$) = 1;
1685 /* The loop is added to the current block the for
1686 statement is defined within */
1687 java_method_add_stmt (current_function_decl, $$);
1690 for_init: /* Can be empty */
1691 { $$ = empty_stmt_node; }
1692 | statement_expression_list
1694 /* Init statement recorded within the previously
1695 defined block scope */
1696 $$ = java_method_add_stmt (current_function_decl, $1);
1698 | local_variable_declaration
1700 /* Local variable are recorded within the previously
1701 defined block scope */
1702 $$ = NULL_TREE;
1704 | statement_expression_list error
1705 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1708 for_update: /* Can be empty */
1709 {$$ = empty_stmt_node;}
1710 | statement_expression_list
1711 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1714 statement_expression_list:
1715 statement_expression
1716 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1717 | statement_expression_list C_TK statement_expression
1718 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1719 | statement_expression_list C_TK error
1720 {yyerror ("Missing term"); RECOVER;}
1723 break_statement:
1724 BREAK_TK SC_TK
1725 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1726 | BREAK_TK identifier SC_TK
1727 { $$ = build_bc_statement ($1.location, 1, $2); }
1728 | BREAK_TK error
1729 {yyerror ("Missing term"); RECOVER;}
1730 | BREAK_TK identifier error
1731 {yyerror ("';' expected"); RECOVER;}
1734 continue_statement:
1735 CONTINUE_TK SC_TK
1736 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1737 | CONTINUE_TK identifier SC_TK
1738 { $$ = build_bc_statement ($1.location, 0, $2); }
1739 | CONTINUE_TK error
1740 {yyerror ("Missing term"); RECOVER;}
1741 | CONTINUE_TK identifier error
1742 {yyerror ("';' expected"); RECOVER;}
1745 return_statement:
1746 RETURN_TK SC_TK
1747 { $$ = build_return ($1.location, NULL_TREE); }
1748 | RETURN_TK expression SC_TK
1749 { $$ = build_return ($1.location, $2); }
1750 | RETURN_TK error
1751 {yyerror ("Missing term"); RECOVER;}
1752 | RETURN_TK expression error
1753 {yyerror ("';' expected"); RECOVER;}
1756 throw_statement:
1757 THROW_TK expression SC_TK
1759 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1760 EXPR_WFL_LINECOL ($$) = $1.location;
1762 | THROW_TK error
1763 {yyerror ("Missing term"); RECOVER;}
1764 | THROW_TK expression error
1765 {yyerror ("';' expected"); RECOVER;}
1768 synchronized_statement:
1769 synchronized OP_TK expression CP_TK block
1771 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1772 EXPR_WFL_LINECOL ($$) =
1773 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1775 | synchronized OP_TK expression CP_TK error
1776 {yyerror ("'{' expected"); RECOVER;}
1777 | synchronized error
1778 {yyerror ("'(' expected"); RECOVER;}
1779 | synchronized OP_TK error CP_TK
1780 {yyerror ("Missing term"); RECOVER;}
1781 | synchronized OP_TK error
1782 {yyerror ("Missing term"); RECOVER;}
1785 synchronized:
1786 modifiers
1788 check_modifiers (
1789 "Illegal modifier `%s'. Only `synchronized' was expected here",
1790 $1, ACC_SYNCHRONIZED);
1791 if ($1 != ACC_SYNCHRONIZED)
1792 MODIFIER_WFL (SYNCHRONIZED_TK) =
1793 build_wfl_node (NULL_TREE);
1797 try_statement:
1798 TRY_TK block catches
1799 { $$ = build_try_statement ($1.location, $2, $3); }
1800 | TRY_TK block finally
1801 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1802 | TRY_TK block catches finally
1803 { $$ = build_try_finally_statement
1804 ($1.location, build_try_statement ($1.location,
1805 $2, $3), $4);
1807 | TRY_TK error
1808 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1811 catches:
1812 catch_clause
1813 | catches catch_clause
1815 TREE_CHAIN ($2) = $1;
1816 $$ = $2;
1820 catch_clause:
1821 catch_clause_parameter block
1823 java_method_add_stmt (current_function_decl, $2);
1824 exit_block ();
1825 $$ = $1;
1828 catch_clause_parameter:
1829 CATCH_TK OP_TK formal_parameter CP_TK
1831 /* We add a block to define a scope for
1832 formal_parameter (CCBP). The formal parameter is
1833 declared initialized by the appropriate function
1834 call */
1835 tree ccpb = enter_block ();
1836 tree init = build_assignment (ASSIGN_TK, $2.location,
1837 TREE_PURPOSE ($3),
1838 soft_exceptioninfo_call_node);
1839 declare_local_variables (0, TREE_VALUE ($3),
1840 build_tree_list (TREE_PURPOSE ($3),
1841 init));
1842 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1843 EXPR_WFL_LINECOL ($$) = $1.location;
1845 | CATCH_TK error
1846 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1847 | CATCH_TK OP_TK error
1849 yyerror ("Missing term or ')' expected");
1850 RECOVER; $$ = NULL_TREE;
1852 | CATCH_TK OP_TK error CP_TK /* That's for () */
1853 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1856 finally:
1857 FINALLY_TK block
1858 { $$ = $2; }
1859 | FINALLY_TK error
1860 {yyerror ("'{' expected"); RECOVER; }
1863 /* 19.12 Production from 15: Expressions */
1864 primary:
1865 primary_no_new_array
1866 | array_creation_expression
1869 primary_no_new_array:
1870 literal
1871 | THIS_TK
1872 { $$ = build_this ($1.location); }
1873 | OP_TK expression CP_TK
1874 {$$ = $2;}
1875 | class_instance_creation_expression
1876 | field_access
1877 | method_invocation
1878 | array_access
1879 | type_literals
1880 /* Added, JDK1.1 inner classes. Documentation is wrong
1881 refering to a 'ClassName' (class_name) rule that doesn't
1882 exist. Used name: instead. */
1883 | name DOT_TK THIS_TK
1885 tree wfl = build_wfl_node (this_identifier_node);
1886 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1888 | OP_TK expression error
1889 {yyerror ("')' expected"); RECOVER;}
1890 | name DOT_TK error
1891 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1892 | primitive_type DOT_TK error
1893 {yyerror ("'class' expected" ); RECOVER;}
1894 | VOID_TK DOT_TK error
1895 {yyerror ("'class' expected" ); RECOVER;}
1898 /* Added, JDK1.1 type literals. We can't use `type' directly, so we
1899 broke the rule down a bit. */
1901 array_type_literal:
1902 primitive_type OSB_TK CSB_TK
1904 $$ = build_java_array_type ($1, -1);
1905 CLASS_LOADED_P ($$) = 1;
1907 | name OSB_TK CSB_TK
1908 { $$ = build_unresolved_array_type ($1); }
1909 /* This triggers two reduce/reduce conflict between array_type_literal and
1910 dims. FIXME.
1911 | array_type OSB_TK CSB_TK
1912 { $$ = build_unresolved_array_type ($1); }
1916 type_literals:
1917 name DOT_TK CLASS_TK
1918 { $$ = build_incomplete_class_ref ($2.location, $1); }
1919 | array_type_literal DOT_TK CLASS_TK
1920 { $$ = build_incomplete_class_ref ($2.location, $1); }
1921 | primitive_type DOT_TK CLASS_TK
1922 { $$ = build_class_ref ($1); }
1923 | VOID_TK DOT_TK CLASS_TK
1924 { $$ = build_class_ref (void_type_node); }
1927 class_instance_creation_expression:
1928 NEW_TK class_type OP_TK argument_list CP_TK
1929 { $$ = build_new_invocation ($2, $4); }
1930 | NEW_TK class_type OP_TK CP_TK
1931 { $$ = build_new_invocation ($2, NULL_TREE); }
1932 | anonymous_class_creation
1933 /* Added, JDK1.1 inner classes, modified to use name or
1934 primary instead of primary solely which couldn't work in
1935 all situations. */
1936 | something_dot_new identifier OP_TK CP_TK
1938 tree ctor = build_new_invocation ($2, NULL_TREE);
1939 $$ = make_qualified_primary ($1, ctor,
1940 EXPR_WFL_LINECOL ($1));
1942 | something_dot_new identifier OP_TK CP_TK class_body
1943 | something_dot_new identifier OP_TK argument_list CP_TK
1945 tree ctor = build_new_invocation ($2, $4);
1946 $$ = make_qualified_primary ($1, ctor,
1947 EXPR_WFL_LINECOL ($1));
1949 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1950 | NEW_TK error SC_TK
1951 {yyerror ("'(' expected"); DRECOVER(new_1);}
1952 | NEW_TK class_type error
1953 {yyerror ("'(' expected"); RECOVER;}
1954 | NEW_TK class_type OP_TK error
1955 {yyerror ("')' or term expected"); RECOVER;}
1956 | NEW_TK class_type OP_TK argument_list error
1957 {yyerror ("')' expected"); RECOVER;}
1958 | something_dot_new error
1959 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1960 | something_dot_new identifier error
1961 {yyerror ("'(' expected"); RECOVER;}
1964 /* Created after JDK1.1 rules originally added to
1965 class_instance_creation_expression, but modified to use
1966 'class_type' instead of 'TypeName' (type_name) which is mentionned
1967 in the documentation but doesn't exist. */
1969 anonymous_class_creation:
1970 NEW_TK class_type OP_TK argument_list CP_TK
1971 { create_anonymous_class ($1.location, $2); }
1972 class_body
1974 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
1975 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
1977 end_class_declaration (1);
1979 /* Now we can craft the new expression */
1980 $$ = build_new_invocation (id, $4);
1982 /* Note that we can't possibly be here if
1983 `class_type' is an interface (in which case the
1984 anonymous class extends Object and implements
1985 `class_type', hence its constructor can't have
1986 arguments.) */
1988 /* Otherwise, the innerclass must feature a
1989 constructor matching `argument_list'. Anonymous
1990 classes are a bit special: it's impossible to
1991 define constructor for them, hence constructors
1992 must be generated following the hints provided by
1993 the `new' expression. Whether a super constructor
1994 of that nature exists or not is to be verified
1995 later on in verify_constructor_super.
1997 It's during the expansion of a `new' statement
1998 refering to an anonymous class that a ctor will
1999 be generated for the anonymous class, with the
2000 right arguments. */
2003 | NEW_TK class_type OP_TK CP_TK
2004 { create_anonymous_class ($1.location, $2); }
2005 class_body
2007 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2008 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2010 end_class_declaration (1);
2012 /* Now we can craft the new expression. The
2013 statement doesn't need to be remember so that a
2014 constructor can be generated, since its signature
2015 is already known. */
2016 $$ = build_new_invocation (id, NULL_TREE);
2020 something_dot_new: /* Added, not part of the specs. */
2021 name DOT_TK NEW_TK
2022 { $$ = $1; }
2023 | primary DOT_TK NEW_TK
2024 { $$ = $1; }
2027 argument_list:
2028 expression
2030 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2031 ctxp->formal_parameter_number = 1;
2033 | argument_list C_TK expression
2035 ctxp->formal_parameter_number += 1;
2036 $$ = tree_cons (NULL_TREE, $3, $1);
2038 | argument_list C_TK error
2039 {yyerror ("Missing term"); RECOVER;}
2042 array_creation_expression:
2043 NEW_TK primitive_type dim_exprs
2044 { $$ = build_newarray_node ($2, $3, 0); }
2045 | NEW_TK class_or_interface_type dim_exprs
2046 { $$ = build_newarray_node ($2, $3, 0); }
2047 | NEW_TK primitive_type dim_exprs dims
2048 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
2049 | NEW_TK class_or_interface_type dim_exprs dims
2050 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
2051 /* Added, JDK1.1 anonymous array. Initial documentation rule
2052 modified */
2053 | NEW_TK class_or_interface_type dims array_initializer
2055 char *sig;
2056 while (CURRENT_OSB (ctxp)--)
2057 obstack_1grow (&temporary_obstack, '[');
2058 sig = obstack_finish (&temporary_obstack);
2059 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2060 $2, get_identifier (sig), $4);
2062 | NEW_TK primitive_type dims array_initializer
2064 tree type = $2;
2065 while (CURRENT_OSB (ctxp)--)
2066 type = build_java_array_type (type, -1);
2067 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2068 build_pointer_type (type), NULL_TREE, $4);
2070 | NEW_TK error CSB_TK
2071 {yyerror ("'[' expected"); DRECOVER ("]");}
2072 | NEW_TK error OSB_TK
2073 {yyerror ("']' expected"); RECOVER;}
2076 dim_exprs:
2077 dim_expr
2078 { $$ = build_tree_list (NULL_TREE, $1); }
2079 | dim_exprs dim_expr
2080 { $$ = tree_cons (NULL_TREE, $2, $$); }
2083 dim_expr:
2084 OSB_TK expression CSB_TK
2086 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2088 $2 = build_wfl_node ($2);
2089 TREE_TYPE ($2) = NULL_TREE;
2091 EXPR_WFL_LINECOL ($2) = $1.location;
2092 $$ = $2;
2094 | OSB_TK expression error
2095 {yyerror ("']' expected"); RECOVER;}
2096 | OSB_TK error
2098 yyerror ("Missing term");
2099 yyerror ("']' expected");
2100 RECOVER;
2104 dims:
2105 OSB_TK CSB_TK
2107 int allocate = 0;
2108 /* If not initialized, allocate memory for the osb
2109 numbers stack */
2110 if (!ctxp->osb_limit)
2112 allocate = ctxp->osb_limit = 32;
2113 ctxp->osb_depth = -1;
2115 /* If capacity overflown, reallocate a bigger chunk */
2116 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2117 allocate = ctxp->osb_limit << 1;
2119 if (allocate)
2121 allocate *= sizeof (int);
2122 if (ctxp->osb_number)
2123 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2124 allocate);
2125 else
2126 ctxp->osb_number = (int *)xmalloc (allocate);
2128 ctxp->osb_depth++;
2129 CURRENT_OSB (ctxp) = 1;
2131 | dims OSB_TK CSB_TK
2132 { CURRENT_OSB (ctxp)++; }
2133 | dims OSB_TK error
2134 { yyerror ("']' expected"); RECOVER;}
2137 field_access:
2138 primary DOT_TK identifier
2139 { $$ = make_qualified_primary ($1, $3, $2.location); }
2140 /* FIXME - REWRITE TO:
2141 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2142 | SUPER_TK DOT_TK identifier
2144 tree super_wfl =
2145 build_wfl_node (super_identifier_node);
2146 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2147 $$ = make_qualified_name (super_wfl, $3, $2.location);
2149 | SUPER_TK error
2150 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2153 method_invocation:
2154 name OP_TK CP_TK
2155 { $$ = build_method_invocation ($1, NULL_TREE); }
2156 | name OP_TK argument_list CP_TK
2157 { $$ = build_method_invocation ($1, $3); }
2158 | primary DOT_TK identifier OP_TK CP_TK
2160 if (TREE_CODE ($1) == THIS_EXPR)
2161 $$ = build_this_super_qualified_invocation
2162 (1, $3, NULL_TREE, 0, $2.location);
2163 else
2165 tree invok = build_method_invocation ($3, NULL_TREE);
2166 $$ = make_qualified_primary ($1, invok, $2.location);
2169 | primary DOT_TK identifier OP_TK argument_list CP_TK
2171 if (TREE_CODE ($1) == THIS_EXPR)
2172 $$ = build_this_super_qualified_invocation
2173 (1, $3, $5, 0, $2.location);
2174 else
2176 tree invok = build_method_invocation ($3, $5);
2177 $$ = make_qualified_primary ($1, invok, $2.location);
2180 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2182 $$ = build_this_super_qualified_invocation
2183 (0, $3, NULL_TREE, $1.location, $2.location);
2185 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2187 $$ = build_this_super_qualified_invocation
2188 (0, $3, $5, $1.location, $2.location);
2190 /* Screws up thing. I let it here until I'm convinced it can
2191 be removed. FIXME
2192 | primary DOT_TK error
2193 {yyerror ("'(' expected"); DRECOVER(bad);} */
2194 | SUPER_TK DOT_TK error CP_TK
2195 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2196 | SUPER_TK DOT_TK error DOT_TK
2197 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2200 array_access:
2201 name OSB_TK expression CSB_TK
2202 { $$ = build_array_ref ($2.location, $1, $3); }
2203 | primary_no_new_array OSB_TK expression CSB_TK
2204 { $$ = build_array_ref ($2.location, $1, $3); }
2205 | name OSB_TK error
2207 yyerror ("Missing term and ']' expected");
2208 DRECOVER(array_access);
2210 | name OSB_TK expression error
2212 yyerror ("']' expected");
2213 DRECOVER(array_access);
2215 | primary_no_new_array OSB_TK error
2217 yyerror ("Missing term and ']' expected");
2218 DRECOVER(array_access);
2220 | primary_no_new_array OSB_TK expression error
2222 yyerror ("']' expected");
2223 DRECOVER(array_access);
2227 postfix_expression:
2228 primary
2229 | name
2230 | post_increment_expression
2231 | post_decrement_expression
2234 post_increment_expression:
2235 postfix_expression INCR_TK
2236 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2239 post_decrement_expression:
2240 postfix_expression DECR_TK
2241 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2244 unary_expression:
2245 pre_increment_expression
2246 | pre_decrement_expression
2247 | PLUS_TK unary_expression
2248 {$$ = build_unaryop ($1.token, $1.location, $2); }
2249 | MINUS_TK unary_expression
2250 {$$ = build_unaryop ($1.token, $1.location, $2); }
2251 | unary_expression_not_plus_minus
2252 | PLUS_TK error
2253 {yyerror ("Missing term"); RECOVER}
2254 | MINUS_TK error
2255 {yyerror ("Missing term"); RECOVER}
2258 pre_increment_expression:
2259 INCR_TK unary_expression
2260 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2261 | INCR_TK error
2262 {yyerror ("Missing term"); RECOVER}
2265 pre_decrement_expression:
2266 DECR_TK unary_expression
2267 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2268 | DECR_TK error
2269 {yyerror ("Missing term"); RECOVER}
2272 unary_expression_not_plus_minus:
2273 postfix_expression
2274 | NOT_TK unary_expression
2275 {$$ = build_unaryop ($1.token, $1.location, $2); }
2276 | NEG_TK unary_expression
2277 {$$ = build_unaryop ($1.token, $1.location, $2); }
2278 | cast_expression
2279 | NOT_TK error
2280 {yyerror ("Missing term"); RECOVER}
2281 | NEG_TK error
2282 {yyerror ("Missing term"); RECOVER}
2285 cast_expression: /* Error handling here is potentially weak */
2286 OP_TK primitive_type dims CP_TK unary_expression
2288 tree type = $2;
2289 while (CURRENT_OSB (ctxp)--)
2290 type = build_java_array_type (type, -1);
2291 ctxp->osb_depth--;
2292 $$ = build_cast ($1.location, type, $5);
2294 | OP_TK primitive_type CP_TK unary_expression
2295 { $$ = build_cast ($1.location, $2, $4); }
2296 | OP_TK expression CP_TK unary_expression_not_plus_minus
2297 { $$ = build_cast ($1.location, $2, $4); }
2298 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2300 const char *ptr;
2301 while (CURRENT_OSB (ctxp)--)
2302 obstack_1grow (&temporary_obstack, '[');
2303 ctxp->osb_depth--;
2304 obstack_grow0 (&temporary_obstack,
2305 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2306 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2307 ptr = obstack_finish (&temporary_obstack);
2308 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2309 $$ = build_cast ($1.location, $2, $5);
2311 | OP_TK primitive_type OSB_TK error
2312 {yyerror ("']' expected, invalid type expression");}
2313 | OP_TK error
2315 if (ctxp->prevent_ese != lineno)
2316 yyerror ("Invalid type expression"); RECOVER;
2317 RECOVER;
2319 | OP_TK primitive_type dims CP_TK error
2320 {yyerror ("Missing term"); RECOVER;}
2321 | OP_TK primitive_type CP_TK error
2322 {yyerror ("Missing term"); RECOVER;}
2323 | OP_TK name dims CP_TK error
2324 {yyerror ("Missing term"); RECOVER;}
2327 multiplicative_expression:
2328 unary_expression
2329 | multiplicative_expression MULT_TK unary_expression
2331 $$ = build_binop (BINOP_LOOKUP ($2.token),
2332 $2.location, $1, $3);
2334 | multiplicative_expression DIV_TK unary_expression
2336 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2337 $1, $3);
2339 | multiplicative_expression REM_TK unary_expression
2341 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2342 $1, $3);
2344 | multiplicative_expression MULT_TK error
2345 {yyerror ("Missing term"); RECOVER;}
2346 | multiplicative_expression DIV_TK error
2347 {yyerror ("Missing term"); RECOVER;}
2348 | multiplicative_expression REM_TK error
2349 {yyerror ("Missing term"); RECOVER;}
2352 additive_expression:
2353 multiplicative_expression
2354 | additive_expression PLUS_TK multiplicative_expression
2356 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2357 $1, $3);
2359 | additive_expression MINUS_TK multiplicative_expression
2361 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2362 $1, $3);
2364 | additive_expression PLUS_TK error
2365 {yyerror ("Missing term"); RECOVER;}
2366 | additive_expression MINUS_TK error
2367 {yyerror ("Missing term"); RECOVER;}
2370 shift_expression:
2371 additive_expression
2372 | shift_expression LS_TK additive_expression
2374 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2375 $1, $3);
2377 | shift_expression SRS_TK additive_expression
2379 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2380 $1, $3);
2382 | shift_expression ZRS_TK additive_expression
2384 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2385 $1, $3);
2387 | shift_expression LS_TK error
2388 {yyerror ("Missing term"); RECOVER;}
2389 | shift_expression SRS_TK error
2390 {yyerror ("Missing term"); RECOVER;}
2391 | shift_expression ZRS_TK error
2392 {yyerror ("Missing term"); RECOVER;}
2395 relational_expression:
2396 shift_expression
2397 | relational_expression LT_TK shift_expression
2399 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2400 $1, $3);
2402 | relational_expression GT_TK shift_expression
2404 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2405 $1, $3);
2407 | relational_expression LTE_TK shift_expression
2409 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2410 $1, $3);
2412 | relational_expression GTE_TK shift_expression
2414 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2415 $1, $3);
2417 | relational_expression INSTANCEOF_TK reference_type
2418 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2419 | relational_expression LT_TK error
2420 {yyerror ("Missing term"); RECOVER;}
2421 | relational_expression GT_TK error
2422 {yyerror ("Missing term"); RECOVER;}
2423 | relational_expression LTE_TK error
2424 {yyerror ("Missing term"); RECOVER;}
2425 | relational_expression GTE_TK error
2426 {yyerror ("Missing term"); RECOVER;}
2427 | relational_expression INSTANCEOF_TK error
2428 {yyerror ("Invalid reference type"); RECOVER;}
2431 equality_expression:
2432 relational_expression
2433 | equality_expression EQ_TK relational_expression
2435 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2436 $1, $3);
2438 | equality_expression NEQ_TK relational_expression
2440 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2441 $1, $3);
2443 | equality_expression EQ_TK error
2444 {yyerror ("Missing term"); RECOVER;}
2445 | equality_expression NEQ_TK error
2446 {yyerror ("Missing term"); RECOVER;}
2449 and_expression:
2450 equality_expression
2451 | and_expression AND_TK equality_expression
2453 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2454 $1, $3);
2456 | and_expression AND_TK error
2457 {yyerror ("Missing term"); RECOVER;}
2460 exclusive_or_expression:
2461 and_expression
2462 | exclusive_or_expression XOR_TK and_expression
2464 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2465 $1, $3);
2467 | exclusive_or_expression XOR_TK error
2468 {yyerror ("Missing term"); RECOVER;}
2471 inclusive_or_expression:
2472 exclusive_or_expression
2473 | inclusive_or_expression OR_TK exclusive_or_expression
2475 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2476 $1, $3);
2478 | inclusive_or_expression OR_TK error
2479 {yyerror ("Missing term"); RECOVER;}
2482 conditional_and_expression:
2483 inclusive_or_expression
2484 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2486 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2487 $1, $3);
2489 | conditional_and_expression BOOL_AND_TK error
2490 {yyerror ("Missing term"); RECOVER;}
2493 conditional_or_expression:
2494 conditional_and_expression
2495 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2497 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2498 $1, $3);
2500 | conditional_or_expression BOOL_OR_TK error
2501 {yyerror ("Missing term"); RECOVER;}
2504 conditional_expression: /* Error handling here is weak */
2505 conditional_or_expression
2506 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2508 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2509 EXPR_WFL_LINECOL ($$) = $2.location;
2511 | conditional_or_expression REL_QM_TK REL_CL_TK error
2513 YYERROR_NOW;
2514 yyerror ("Missing term");
2515 DRECOVER (1);
2517 | conditional_or_expression REL_QM_TK error
2518 {yyerror ("Missing term"); DRECOVER (2);}
2519 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2520 {yyerror ("Missing term"); DRECOVER (3);}
2523 assignment_expression:
2524 conditional_expression
2525 | assignment
2528 assignment:
2529 left_hand_side assignment_operator assignment_expression
2530 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2531 | left_hand_side assignment_operator error
2533 if (ctxp->prevent_ese != lineno)
2534 yyerror ("Missing term");
2535 DRECOVER (assign);
2539 left_hand_side:
2540 name
2541 | field_access
2542 | array_access
2545 assignment_operator:
2546 ASSIGN_ANY_TK
2547 | ASSIGN_TK
2550 expression:
2551 assignment_expression
2554 constant_expression:
2555 expression
2561 /* This section of the code deal with save/restoring parser contexts.
2562 Add mode documentation here. FIXME */
2564 /* Helper function. Create a new parser context. With
2565 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2566 context is copied, otherwise, the new context is zeroed. The newly
2567 created context becomes the current one. */
2569 static void
2570 create_new_parser_context (copy_from_previous)
2571 int copy_from_previous;
2573 struct parser_ctxt *new;
2575 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2576 if (copy_from_previous)
2578 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2579 new->saved_data_ctx = 1;
2581 else
2582 bzero ((PTR) new, sizeof (struct parser_ctxt));
2584 new->next = ctxp;
2585 ctxp = new;
2588 /* Create a new parser context and make it the current one. */
2590 void
2591 java_push_parser_context ()
2593 create_new_parser_context (0);
2594 if (ctxp->next)
2596 ctxp->incomplete_class = ctxp->next->incomplete_class;
2597 ctxp->gclass_list = ctxp->next->gclass_list;
2601 void
2602 java_pop_parser_context (generate)
2603 int generate;
2605 tree current;
2606 struct parser_ctxt *toFree, *next;
2608 if (!ctxp)
2609 return;
2611 toFree = ctxp;
2612 next = ctxp->next;
2613 if (next)
2615 next->incomplete_class = ctxp->incomplete_class;
2616 next->gclass_list = ctxp->gclass_list;
2617 lineno = ctxp->lineno;
2618 finput = ctxp->finput;
2619 current_class = ctxp->current_class;
2622 /* Set the single import class file flag to 0 for the current list
2623 of imported things */
2624 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2625 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2627 /* And restore those of the previous context */
2628 if ((ctxp = next)) /* Assignment is really meant here */
2629 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2630 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2632 /* If we pushed a context to parse a class intended to be generated,
2633 we keep it so we can remember the class. What we could actually
2634 do is to just update a list of class names. */
2635 if (generate)
2637 toFree->next = ctxp_for_generation;
2638 ctxp_for_generation = toFree;
2640 else
2641 free (toFree);
2644 /* Create a parser context for the use of saving some global
2645 variables. */
2647 void
2648 java_parser_context_save_global ()
2650 if (!ctxp)
2652 java_push_parser_context ();
2653 ctxp->saved_data_ctx = 1;
2656 /* If this context already stores data, create a new one suitable
2657 for data storage. */
2658 else if (ctxp->saved_data)
2659 create_new_parser_context (1);
2661 ctxp->finput = finput;
2662 ctxp->lineno = lineno;
2663 ctxp->current_class = current_class;
2664 ctxp->filename = input_filename;
2665 ctxp->current_function_decl = current_function_decl;
2666 ctxp->saved_data = 1;
2669 /* Restore some global variables from the previous context. Make the
2670 previous context the current one. */
2672 void
2673 java_parser_context_restore_global ()
2675 finput = ctxp->finput;
2676 lineno = ctxp->lineno;
2677 current_class = ctxp->current_class;
2678 input_filename = ctxp->filename;
2679 current_function_decl = ctxp->current_function_decl;
2680 ctxp->saved_data = 0;
2681 if (ctxp->saved_data_ctx)
2682 java_pop_parser_context (0);
2685 /* Suspend vital data for the current class/function being parsed so
2686 that an other class can be parsed. Used to let local/anonymous
2687 classes be parsed. */
2689 static void
2690 java_parser_context_suspend ()
2692 /* This makes debugging through java_debug_context easier */
2693 static const char *name = "<inner buffer context>";
2695 /* Duplicate the previous context, use it to save the globals we're
2696 interested in */
2697 create_new_parser_context (1);
2698 ctxp->current_function_decl = current_function_decl;
2699 ctxp->current_class = current_class;
2701 /* Then create a new context which inherits all data from the
2702 previous one. This will be the new current context */
2703 create_new_parser_context (1);
2705 /* Help debugging */
2706 ctxp->next->filename = name;
2709 /* Resume vital data for the current class/function being parsed so
2710 that an other class can be parsed. Used to let local/anonymous
2711 classes be parsed. The trick is the data storing file position
2712 informations must be restored to their current value, so parsing
2713 can resume as if no context was ever saved. */
2715 static void
2716 java_parser_context_resume ()
2718 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2719 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2720 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2722 /* We need to inherit the list of classes to complete/generate */
2723 restored->incomplete_class = old->incomplete_class;
2724 restored->gclass_list = old->gclass_list;
2725 restored->classd_list = old->classd_list;
2726 restored->class_list = old->class_list;
2728 /* Restore the current class and function from the saver */
2729 current_class = saver->current_class;
2730 current_function_decl = saver->current_function_decl;
2732 /* Retrive the restored context */
2733 ctxp = restored;
2735 /* Re-installed the data for the parsing to carry on */
2736 bcopy (&old->marker_begining, &ctxp->marker_begining,
2737 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2739 /* Buffer context can now be discarded */
2740 free (saver);
2741 free (old);
2744 /* Add a new anchor node to which all statement(s) initializing static
2745 and non static initialized upon declaration field(s) will be
2746 linked. */
2748 static void
2749 java_parser_context_push_initialized_field ()
2751 tree node;
2753 node = build_tree_list (NULL_TREE, NULL_TREE);
2754 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2755 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2757 node = build_tree_list (NULL_TREE, NULL_TREE);
2758 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2759 CPC_INITIALIZER_LIST (ctxp) = node;
2761 node = build_tree_list (NULL_TREE, NULL_TREE);
2762 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2763 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2766 /* Pop the lists of initialized field. If this lists aren't empty,
2767 remember them so we can use it to create and populate the finit$
2768 or <clinit> functions. */
2770 static void
2771 java_parser_context_pop_initialized_field ()
2773 tree stmts;
2774 tree class_type = TREE_TYPE (GET_CPC ());
2776 if (CPC_INITIALIZER_LIST (ctxp))
2778 stmts = CPC_INITIALIZER_STMT (ctxp);
2779 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2780 if (stmts && !java_error_count)
2781 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2784 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2786 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2787 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2788 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2789 /* Keep initialization in order to enforce 8.5 */
2790 if (stmts && !java_error_count)
2791 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2794 /* JDK 1.1 instance initializers */
2795 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2797 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2798 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2799 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2800 if (stmts && !java_error_count)
2801 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2805 static tree
2806 reorder_static_initialized (list)
2807 tree list;
2809 /* We have to keep things in order. The alias initializer have to
2810 come first, then the initialized regular field, in reverse to
2811 keep them in lexical order. */
2812 tree marker, previous = NULL_TREE;
2813 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2814 if (TREE_CODE (marker) == TREE_LIST
2815 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2816 break;
2818 /* No static initialized, the list is fine as is */
2819 if (!previous)
2820 list = TREE_CHAIN (marker);
2822 /* No marker? reverse the whole list */
2823 else if (!marker)
2824 list = nreverse (list);
2826 /* Otherwise, reverse what's after the marker and the new reordered
2827 sublist will replace the marker. */
2828 else
2830 TREE_CHAIN (previous) = NULL_TREE;
2831 list = nreverse (list);
2832 list = chainon (TREE_CHAIN (marker), list);
2834 return list;
2837 /* Helper functions to dump the parser context stack. */
2839 #define TAB_CONTEXT(C) \
2840 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2842 static void
2843 java_debug_context_do (tab)
2844 int tab;
2846 struct parser_ctxt *copy = ctxp;
2847 while (copy)
2849 TAB_CONTEXT (tab);
2850 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2851 TAB_CONTEXT (tab);
2852 fprintf (stderr, "filename: %s\n", copy->filename);
2853 TAB_CONTEXT (tab);
2854 fprintf (stderr, "lineno: %d\n", copy->lineno);
2855 TAB_CONTEXT (tab);
2856 fprintf (stderr, "package: %s\n",
2857 (copy->package ?
2858 IDENTIFIER_POINTER (copy->package) : "<none>"));
2859 TAB_CONTEXT (tab);
2860 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2861 TAB_CONTEXT (tab);
2862 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2863 copy = copy->next;
2864 tab += 2;
2868 /* Dump the stacked up parser contexts. Intended to be called from a
2869 debugger. */
2871 void
2872 java_debug_context ()
2874 java_debug_context_do (0);
2879 /* Flag for the error report routine to issue the error the first time
2880 it's called (overriding the default behavior which is to drop the
2881 first invocation and honor the second one, taking advantage of a
2882 richer context. */
2883 static int force_error = 0;
2885 /* Reporting an constructor invocation error. */
2886 static void
2887 parse_ctor_invocation_error ()
2889 if (DECL_CONSTRUCTOR_P (current_function_decl))
2890 yyerror ("Constructor invocation must be first thing in a constructor");
2891 else
2892 yyerror ("Only constructors can invoke constructors");
2895 /* Reporting JDK1.1 features not implemented. */
2897 static tree
2898 parse_jdk1_1_error (msg)
2899 const char *msg;
2901 sorry (": `%s' JDK1.1(TM) feature", msg);
2902 java_error_count++;
2903 return empty_stmt_node;
2906 static int do_warning = 0;
2908 void
2909 yyerror (msg)
2910 const char *msg;
2912 static java_lc elc;
2913 static int prev_lineno;
2914 static const char *prev_msg;
2916 int save_lineno;
2917 char *remainder, *code_from_source;
2918 extern struct obstack temporary_obstack;
2920 if (!force_error && prev_lineno == lineno)
2921 return;
2923 /* Save current error location but report latter, when the context is
2924 richer. */
2925 if (ctxp->java_error_flag == 0)
2927 ctxp->java_error_flag = 1;
2928 elc = ctxp->elc;
2929 /* Do something to use the previous line if we're reaching the
2930 end of the file... */
2931 #ifdef VERBOSE_SKELETON
2932 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2933 #endif
2934 return;
2937 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2938 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2939 return;
2941 ctxp->java_error_flag = 0;
2942 if (do_warning)
2943 java_warning_count++;
2944 else
2945 java_error_count++;
2947 if (elc.col == 0 && msg && msg[1] == ';')
2949 elc.col = ctxp->p_line->char_col-1;
2950 elc.line = ctxp->p_line->lineno;
2953 save_lineno = lineno;
2954 prev_lineno = lineno = elc.line;
2955 prev_msg = msg;
2957 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2958 obstack_grow0 (&temporary_obstack,
2959 code_from_source, strlen (code_from_source));
2960 remainder = obstack_finish (&temporary_obstack);
2961 if (do_warning)
2962 warning ("%s.\n%s", msg, remainder);
2963 else
2964 error ("%s.\n%s", msg, remainder);
2966 /* This allow us to cheaply avoid an extra 'Invalid expression
2967 statement' error report when errors have been already reported on
2968 the same line. This occurs when we report an error but don't have
2969 a synchronization point other than ';', which
2970 expression_statement is the only one to take care of. */
2971 ctxp->prevent_ese = lineno = save_lineno;
2974 static void
2975 issue_warning_error_from_context (cl, msg, ap)
2976 tree cl;
2977 const char *msg;
2978 va_list ap;
2980 const char *saved, *saved_input_filename;
2981 char buffer [4096];
2982 vsprintf (buffer, msg, ap);
2983 force_error = 1;
2985 ctxp->elc.line = EXPR_WFL_LINENO (cl);
2986 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
2987 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
2989 /* We have a CL, that's a good reason for using it if it contains data */
2990 saved = ctxp->filename;
2991 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
2992 ctxp->filename = EXPR_WFL_FILENAME (cl);
2993 saved_input_filename = input_filename;
2994 input_filename = ctxp->filename;
2995 java_error (NULL);
2996 java_error (buffer);
2997 ctxp->filename = saved;
2998 input_filename = saved_input_filename;
2999 force_error = 0;
3002 /* Issue an error message at a current source line CL */
3004 void
3005 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3007 #ifndef ANSI_PROTOTYPES
3008 tree cl;
3009 const char *msg;
3010 #endif
3011 va_list ap;
3013 VA_START (ap, msg);
3014 #ifndef ANSI_PROTOTYPES
3015 cl = va_arg (ap, tree);
3016 msg = va_arg (ap, const char *);
3017 #endif
3018 issue_warning_error_from_context (cl, msg, ap);
3019 va_end (ap);
3022 /* Issue a warning at a current source line CL */
3024 static void
3025 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3027 #ifndef ANSI_PROTOTYPES
3028 tree cl;
3029 const char *msg;
3030 #endif
3031 va_list ap;
3033 VA_START (ap, msg);
3034 #ifndef ANSI_PROTOTYPES
3035 cl = va_arg (ap, tree);
3036 msg = va_arg (ap, const char *);
3037 #endif
3039 force_error = do_warning = 1;
3040 issue_warning_error_from_context (cl, msg, ap);
3041 do_warning = force_error = 0;
3042 va_end (ap);
3045 static tree
3046 find_expr_with_wfl (node)
3047 tree node;
3049 while (node)
3051 char code;
3052 tree to_return;
3054 switch (TREE_CODE (node))
3056 case BLOCK:
3057 node = BLOCK_EXPR_BODY (node);
3058 continue;
3060 case COMPOUND_EXPR:
3061 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3062 if (to_return)
3063 return to_return;
3064 node = TREE_OPERAND (node, 1);
3065 continue;
3067 case LOOP_EXPR:
3068 node = TREE_OPERAND (node, 0);
3069 continue;
3071 case LABELED_BLOCK_EXPR:
3072 node = TREE_OPERAND (node, 1);
3073 continue;
3075 default:
3076 code = TREE_CODE_CLASS (TREE_CODE (node));
3077 if (((code == '1') || (code == '2') || (code == 'e'))
3078 && EXPR_WFL_LINECOL (node))
3079 return node;
3080 return NULL_TREE;
3083 return NULL_TREE;
3086 /* Issue a missing return statement error. Uses METHOD to figure the
3087 last line of the method the error occurs in. */
3089 static void
3090 missing_return_error (method)
3091 tree method;
3093 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3094 parse_error_context (wfl_operator, "Missing return statement");
3097 /* Issue an unreachable statement error. From NODE, find the next
3098 statement to report appropriately. */
3099 static void
3100 unreachable_stmt_error (node)
3101 tree node;
3103 /* Browse node to find the next expression node that has a WFL. Use
3104 the location to report the error */
3105 if (TREE_CODE (node) == COMPOUND_EXPR)
3106 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3107 else
3108 node = find_expr_with_wfl (node);
3110 if (node)
3112 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3113 parse_error_context (wfl_operator, "Unreachable statement");
3115 else
3116 fatal ("Can't get valid statement - unreachable_stmt_error");
3120 java_report_errors ()
3122 if (java_error_count)
3123 fprintf (stderr, "%d error%s",
3124 java_error_count, (java_error_count == 1 ? "" : "s"));
3125 if (java_warning_count)
3126 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3127 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3128 if (java_error_count || java_warning_count)
3129 putc ('\n', stderr);
3130 return java_error_count;
3133 static char *
3134 java_accstring_lookup (flags)
3135 int flags;
3137 static char buffer [80];
3138 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3140 /* Access modifier looked-up first for easier report on forbidden
3141 access. */
3142 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3143 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3144 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3145 if (flags & ACC_STATIC) COPY_RETURN ("static");
3146 if (flags & ACC_FINAL) COPY_RETURN ("final");
3147 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3148 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3149 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3150 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3151 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3152 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3154 buffer [0] = '\0';
3155 return buffer;
3156 #undef COPY_RETURN
3159 /* Issuing error messages upon redefinition of classes, interfaces or
3160 variables. */
3162 static void
3163 classitf_redefinition_error (context, id, decl, cl)
3164 const char *context;
3165 tree id, decl, cl;
3167 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3168 context, IDENTIFIER_POINTER (id),
3169 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3170 /* Here we should point out where its redefined. It's a unicode. FIXME */
3173 static void
3174 variable_redefinition_error (context, name, type, line)
3175 tree context, name, type;
3176 int line;
3178 const char *type_name;
3180 /* Figure a proper name for type. We might haven't resolved it */
3181 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3182 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3183 else
3184 type_name = lang_printable_name (type, 0);
3186 parse_error_context (context,
3187 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3188 IDENTIFIER_POINTER (name),
3189 type_name, IDENTIFIER_POINTER (name), line);
3192 static tree
3193 build_array_from_name (type, type_wfl, name, ret_name)
3194 tree type, type_wfl, name, *ret_name;
3196 int more_dims = 0;
3197 const char *string;
3199 /* Eventually get more dims */
3200 string = IDENTIFIER_POINTER (name);
3201 while (string [more_dims] == '[')
3202 more_dims++;
3204 /* If we have, then craft a new type for this variable */
3205 if (more_dims)
3207 name = get_identifier (&string [more_dims]);
3209 /* If we have a pointer, use its type */
3210 if (TREE_CODE (type) == POINTER_TYPE)
3211 type = TREE_TYPE (type);
3213 /* Building the first dimension of a primitive type uses this
3214 function */
3215 if (JPRIMITIVE_TYPE_P (type))
3217 type = build_java_array_type (type, -1);
3218 CLASS_LOADED_P (type) = 1;
3219 more_dims--;
3221 /* Otherwise, if we have a WFL for this type, use it (the type
3222 is already an array on an unresolved type, and we just keep
3223 on adding dimensions) */
3224 else if (type_wfl)
3225 type = type_wfl;
3227 /* Add all the dimensions */
3228 while (more_dims--)
3229 type = build_unresolved_array_type (type);
3231 /* The type may have been incomplete in the first place */
3232 if (type_wfl)
3233 type = obtain_incomplete_type (type);
3236 if (ret_name)
3237 *ret_name = name;
3238 return type;
3241 /* Build something that the type identifier resolver will identify as
3242 being an array to an unresolved type. TYPE_WFL is a WFL on a
3243 identifier. */
3245 static tree
3246 build_unresolved_array_type (type_or_wfl)
3247 tree type_or_wfl;
3249 const char *ptr;
3251 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3252 just create a array type */
3253 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3255 tree type = build_java_array_type (type_or_wfl, -1);
3256 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3257 return type;
3260 obstack_1grow (&temporary_obstack, '[');
3261 obstack_grow0 (&temporary_obstack,
3262 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3263 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3264 ptr = obstack_finish (&temporary_obstack);
3265 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3266 return type_or_wfl;
3269 static void
3270 parser_add_interface (class_decl, interface_decl, wfl)
3271 tree class_decl, interface_decl, wfl;
3273 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3274 parse_error_context (wfl, "Interface `%s' repeated",
3275 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3278 /* Bulk of common class/interface checks. Return 1 if an error was
3279 encountered. TAG is 0 for a class, 1 for an interface. */
3281 static int
3282 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3283 int is_interface, flags;
3284 tree raw_name, qualified_name, decl, cl;
3286 tree node;
3287 int sca = 0; /* Static class allowed */
3288 int icaf = 0; /* Inner class allowed flags */
3289 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3291 if (!quiet_flag)
3292 fprintf (stderr, " %s%s %s",
3293 (CPC_INNER_P () ? "inner" : ""),
3294 (is_interface ? "interface" : "class"),
3295 IDENTIFIER_POINTER (qualified_name));
3297 /* Scope of an interface/class type name:
3298 - Can't be imported by a single type import
3299 - Can't already exists in the package */
3300 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3301 && (node = find_name_in_single_imports (raw_name))
3302 && !CPC_INNER_P ())
3304 parse_error_context
3305 (cl, "%s name `%s' clashes with imported type `%s'",
3306 (is_interface ? "Interface" : "Class"),
3307 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3308 return 1;
3310 if (decl && CLASS_COMPLETE_P (decl))
3312 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3313 qualified_name, decl, cl);
3314 return 1;
3317 if (check_inner_class_redefinition (raw_name, cl))
3318 return 1;
3320 /* If public, file name should match class/interface name, except
3321 when dealing with an inner class */
3322 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3324 const char *f;
3326 /* Contains OS dependent assumption on path separator. FIXME */
3327 for (f = &input_filename [strlen (input_filename)];
3328 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3329 f--)
3331 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3332 f++;
3333 if (strncmp (IDENTIFIER_POINTER (raw_name),
3334 f , IDENTIFIER_LENGTH (raw_name)) ||
3335 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3336 parse_error_context
3337 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3338 (is_interface ? "interface" : "class"),
3339 IDENTIFIER_POINTER (qualified_name),
3340 IDENTIFIER_POINTER (raw_name));
3343 /* Static classes can be declared only in top level classes. Note:
3344 once static, a inner class is a top level class. */
3345 if (flags & ACC_STATIC)
3347 /* Catch the specific error of declaring an class inner class
3348 with no toplevel enclosing class. Prevent check_modifiers from
3349 complaining a second time */
3350 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3352 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3353 IDENTIFIER_POINTER (qualified_name));
3354 sca = ACC_STATIC;
3356 /* Else, in the context of a top-level class declaration, let
3357 `check_modifiers' do its job, otherwise, give it a go */
3358 else
3359 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3362 /* Inner classes can be declared private or protected
3363 within their enclosing classes. */
3364 if (CPC_INNER_P ())
3366 /* A class which is local to a block can't be public, private,
3367 protected or static. But it is created final, so allow this
3368 one. */
3369 if (current_function_decl)
3370 icaf = sca = uaaf = ACC_FINAL;
3371 else
3373 check_modifiers_consistency (flags);
3374 icaf = ACC_PRIVATE|ACC_PROTECTED;
3378 if (is_interface)
3380 if (CPC_INNER_P ())
3381 uaaf = INTERFACE_INNER_MODIFIERS;
3382 else
3383 uaaf = INTERFACE_MODIFIERS;
3385 check_modifiers ("Illegal modifier `%s' for interface declaration",
3386 flags, uaaf);
3388 else
3389 check_modifiers ((current_function_decl ?
3390 "Illegal modifier `%s' for local class declaration" :
3391 "Illegal modifier `%s' for class declaration"),
3392 flags, uaaf|sca|icaf);
3393 return 0;
3396 static void
3397 make_nested_class_name (cpc_list)
3398 tree cpc_list;
3400 tree name;
3402 if (!cpc_list)
3403 return;
3404 else
3405 make_nested_class_name (TREE_CHAIN (cpc_list));
3407 /* Pick the qualified name when dealing with the first upmost
3408 enclosing class */
3409 name = (TREE_CHAIN (cpc_list) ?
3410 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3411 obstack_grow (&temporary_obstack,
3412 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3413 /* Why is NO_DOLLAR_IN_LABEL defined? */
3414 #if 0
3415 #ifdef NO_DOLLAR_IN_LABEL
3416 fatal ("make_nested_class_name: Can't use '$' as a separator "
3417 "for inner classes");
3418 #endif
3419 #endif
3420 obstack_1grow (&temporary_obstack, '$');
3423 /* Can't redefine a class already defined in an earlier scope. */
3425 static int
3426 check_inner_class_redefinition (raw_name, cl)
3427 tree raw_name, cl;
3429 tree scope_list;
3431 for (scope_list = GET_CPC_LIST (); scope_list;
3432 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3433 if (raw_name == GET_CPC_UN_NODE (scope_list))
3435 parse_error_context
3436 (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",
3437 IDENTIFIER_POINTER (raw_name));
3438 return 1;
3440 return 0;
3443 static tree
3444 find_as_inner_class (enclosing, name, cl)
3445 tree enclosing, name, cl;
3447 tree qual, to_return;
3448 if (!enclosing)
3449 return NULL_TREE;
3451 name = TYPE_NAME (name);
3453 /* First search: within the scope of `enclosing', search for name */
3454 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3455 qual = EXPR_WFL_QUALIFICATION (cl);
3456 else if (cl)
3457 qual = build_tree_list (cl, NULL_TREE);
3458 else
3459 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3461 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3462 return to_return;
3464 /* We're dealing with a qualified name. Try to resolve thing until
3465 we get something that is an enclosing class. */
3466 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3468 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3470 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3471 qual = TREE_CHAIN (qual))
3473 acc = merge_qualified_name (acc,
3474 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3475 BUILD_PTR_FROM_NAME (ptr, acc);
3476 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3479 /* A NULL qual and a decl means that the search ended
3480 successfully?!? We have to do something then. FIXME */
3482 if (decl)
3483 enclosing = decl;
3484 else
3485 qual = EXPR_WFL_QUALIFICATION (cl);
3487 /* Otherwise, create a qual for the other part of the resolution. */
3488 else
3489 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3491 return find_as_inner_class_do (qual, enclosing);
3494 /* We go inside the list of sub classes and try to find a way
3495 through. */
3497 static tree
3498 find_as_inner_class_do (qual, enclosing)
3499 tree qual, enclosing;
3501 if (!qual)
3502 return NULL_TREE;
3504 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3506 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3507 tree next_enclosing = NULL_TREE;
3508 tree inner_list;
3510 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3511 inner_list; inner_list = TREE_CHAIN (inner_list))
3513 if (TREE_VALUE (inner_list) == name_to_match)
3515 next_enclosing = TREE_PURPOSE (inner_list);
3516 break;
3519 enclosing = next_enclosing;
3522 return (!qual && enclosing ? enclosing : NULL_TREE);
3525 /* Reach all inner classes and tie their unqualified name to a
3526 DECL. */
3528 static void
3529 set_nested_class_simple_name_value (outer, set)
3530 tree outer;
3531 int set;
3533 tree l;
3535 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3536 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3537 TREE_PURPOSE (l) : NULL_TREE);
3540 static void
3541 link_nested_class_to_enclosing ()
3543 if (GET_ENCLOSING_CPC ())
3545 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3546 DECL_INNER_CLASS_LIST (enclosing) =
3547 tree_cons (GET_CPC (), GET_CPC_UN (),
3548 DECL_INNER_CLASS_LIST (enclosing));
3549 enclosing = enclosing;
3553 static tree
3554 maybe_make_nested_class_name (name)
3555 tree name;
3557 tree id = NULL_TREE;
3559 if (CPC_INNER_P ())
3561 make_nested_class_name (GET_CPC_LIST ());
3562 obstack_grow0 (&temporary_obstack,
3563 IDENTIFIER_POINTER (name),
3564 IDENTIFIER_LENGTH (name));
3565 id = get_identifier (obstack_finish (&temporary_obstack));
3566 if (ctxp->package)
3567 QUALIFIED_P (id) = 1;
3569 return id;
3572 /* If DECL is NULL, create and push a new DECL, record the current
3573 line CL and do other maintenance things. */
3575 static tree
3576 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3577 tree decl, raw_name, qualified_name, cl;
3579 if (!decl)
3580 decl = push_class (make_class (), qualified_name);
3582 /* Take care of the file and line business */
3583 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3584 /* If we're emiting xrefs, store the line/col number information */
3585 if (flag_emit_xref)
3586 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3587 else
3588 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3589 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3590 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3591 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3593 PUSH_CPC (decl, raw_name);
3594 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3596 /* Link the declaration to the already seen ones */
3597 TREE_CHAIN (decl) = ctxp->class_list;
3598 ctxp->class_list = decl;
3600 /* Create a new nodes in the global lists */
3601 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
3602 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3604 /* Install a new dependency list element */
3605 create_jdep_list (ctxp);
3607 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3608 IDENTIFIER_POINTER (qualified_name)));
3609 return decl;
3612 static void
3613 add_superinterfaces (decl, interface_list)
3614 tree decl, interface_list;
3616 tree node;
3617 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3618 takes care of ensuring that:
3619 - This is an accessible interface type,
3620 - Circularity detection.
3621 parser_add_interface is then called. If present but not defined,
3622 the check operation is delayed until the super interface gets
3623 defined. */
3624 for (node = interface_list; node; node = TREE_CHAIN (node))
3626 tree current = TREE_PURPOSE (node);
3627 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3628 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3630 if (!parser_check_super_interface (idecl, decl, current))
3631 parser_add_interface (decl, idecl, current);
3633 else
3634 register_incomplete_type (JDEP_INTERFACE,
3635 current, decl, NULL_TREE);
3639 /* Create an interface in pass1 and return its decl. Return the
3640 interface's decl in pass 2. */
3642 static tree
3643 create_interface (flags, id, super)
3644 int flags;
3645 tree id, super;
3647 tree raw_name = EXPR_WFL_NODE (id);
3648 tree q_name = parser_qualified_classname (raw_name);
3649 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3651 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3653 /* Basic checks: scope, redefinition, modifiers */
3654 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3656 PUSH_ERROR ();
3657 return NULL_TREE;
3660 /* Suspend the current parsing context if we're parsing an inner
3661 interface */
3662 if (CPC_INNER_P ())
3663 java_parser_context_suspend ();
3665 /* Push a new context for (static) initialized upon declaration fields */
3666 java_parser_context_push_initialized_field ();
3668 /* Interface modifiers check
3669 - public/abstract allowed (already done at that point)
3670 - abstract is obsolete (comes first, it's a warning, or should be)
3671 - Can't use twice the same (checked in the modifier rule) */
3672 if ((flags & ACC_ABSTRACT) && flag_redundant)
3673 parse_warning_context
3674 (MODIFIER_WFL (ABSTRACT_TK),
3675 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3677 /* Create a new decl if DECL is NULL, otherwise fix it */
3678 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3680 /* Set super info and mark the class a complete */
3681 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3682 object_type_node, ctxp->interface_number);
3683 ctxp->interface_number = 0;
3684 CLASS_COMPLETE_P (decl) = 1;
3685 add_superinterfaces (decl, super);
3687 return decl;
3690 /* Anonymous class counter. Will be reset to 1 every time a non
3691 anonymous class gets created. */
3692 static int anonymous_class_counter = 1;
3694 /* Patch anonymous class CLASS, by either extending or implementing
3695 DEP. */
3697 static void
3698 patch_anonymous_class (type_decl, class_decl, wfl)
3699 tree type_decl, class_decl, wfl;
3701 tree class = TREE_TYPE (class_decl);
3702 tree type = TREE_TYPE (type_decl);
3703 tree binfo = TYPE_BINFO (class);
3705 /* If it's an interface, implement it */
3706 if (CLASS_INTERFACE (type_decl))
3708 tree s_binfo;
3709 int length;
3711 if (parser_check_super_interface (type_decl, class_decl, wfl))
3712 return;
3714 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3715 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3716 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3717 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3718 /* And add the interface */
3719 parser_add_interface (class_decl, type_decl, wfl);
3721 /* Otherwise, it's a type we want to extend */
3722 else
3724 if (parser_check_super (type_decl, class_decl, wfl))
3725 return;
3726 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3730 static tree
3731 create_anonymous_class (location, type_name)
3732 int location;
3733 tree type_name;
3735 char buffer [80];
3736 tree super = NULL_TREE, itf = NULL_TREE;
3737 tree id, type_decl, class;
3739 /* The unqualified name of the anonymous class. It's just a number. */
3740 sprintf (buffer, "%d", anonymous_class_counter++);
3741 id = build_wfl_node (get_identifier (buffer));
3742 EXPR_WFL_LINECOL (id) = location;
3744 /* We know about the type to extend/implement. We go ahead */
3745 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3747 /* Create a class which either implements on extends the designated
3748 class. The class bears an innacessible name. */
3749 if (CLASS_INTERFACE (type_decl))
3751 /* It's OK to modify it here. It's been already used and
3752 shouldn't be reused */
3753 ctxp->interface_number = 1;
3754 /* Interfaces should presented as a list of WFLs */
3755 itf = build_tree_list (type_name, NULL_TREE);
3757 else
3758 super = type_name;
3761 class = create_class (ACC_FINAL, id, super, itf);
3763 /* We didn't know anything about the stuff. We register a dependence. */
3764 if (!type_decl)
3765 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3767 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3768 return class;
3771 /* Create a class in pass1 and return its decl. Return class
3772 interface's decl in pass 2. */
3774 static tree
3775 create_class (flags, id, super, interfaces)
3776 int flags;
3777 tree id, super, interfaces;
3779 tree raw_name = EXPR_WFL_NODE (id);
3780 tree class_id, decl;
3781 tree super_decl_type;
3783 class_id = parser_qualified_classname (raw_name);
3784 decl = IDENTIFIER_CLASS_VALUE (class_id);
3785 EXPR_WFL_NODE (id) = class_id;
3787 /* Basic check: scope, redefinition, modifiers */
3788 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3790 PUSH_ERROR ();
3791 return NULL_TREE;
3794 /* Suspend the current parsing context if we're parsing an inner
3795 class or an anonymous class. */
3796 if (CPC_INNER_P ())
3797 java_parser_context_suspend ();
3798 /* Push a new context for (static) initialized upon declaration fields */
3799 java_parser_context_push_initialized_field ();
3801 /* Class modifier check:
3802 - Allowed modifier (already done at that point)
3803 - abstract AND final forbidden
3804 - Public classes defined in the correct file */
3805 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3806 parse_error_context
3807 (id, "Class `%s' can't be declared both abstract and final",
3808 IDENTIFIER_POINTER (raw_name));
3810 /* Create a new decl if DECL is NULL, otherwise fix it */
3811 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3813 /* If SUPER exists, use it, otherwise use Object */
3814 if (super)
3816 /* Can't extend java.lang.Object */
3817 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3819 parse_error_context (id, "Can't extend `java.lang.Object'");
3820 return NULL_TREE;
3823 super_decl_type =
3824 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3826 else if (TREE_TYPE (decl) != object_type_node)
3827 super_decl_type = object_type_node;
3828 /* We're defining java.lang.Object */
3829 else
3830 super_decl_type = NULL_TREE;
3832 /* Set super info and mark the class a complete */
3833 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3834 ctxp->interface_number);
3835 ctxp->interface_number = 0;
3836 CLASS_COMPLETE_P (decl) = 1;
3837 add_superinterfaces (decl, interfaces);
3839 /* Add the private this$<n> field, Replicate final locals still in
3840 scope as private final fields mangled like val$<local_name>.
3841 This doesn't not occur for top level (static) inner classes. */
3842 if (PURE_INNER_CLASS_DECL_P (decl))
3843 add_inner_class_fields (decl, current_function_decl);
3845 /* If doing xref, store the location at which the inherited class
3846 (if any) was seen. */
3847 if (flag_emit_xref && super)
3848 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3850 /* Eventually sets the @deprecated tag flag */
3851 CHECK_DEPRECATED (decl);
3853 /* Reset the anonymous class counter when declaring non inner classes */
3854 if (!INNER_CLASS_DECL_P (decl))
3855 anonymous_class_counter = 1;
3857 return decl;
3860 /* End a class declaration: register the statements used to create
3861 finit$ and <clinit>, pop the current class and resume the prior
3862 parser context if necessary. */
3864 static void
3865 end_class_declaration (resume)
3866 int resume;
3868 /* If an error occured, context weren't pushed and won't need to be
3869 popped by a resume. */
3870 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3872 java_parser_context_pop_initialized_field ();
3873 POP_CPC ();
3874 if (resume && no_error_occured)
3875 java_parser_context_resume ();
3877 /* We're ending a class declaration, this is a good time to reset
3878 the interface cout. Note that might have been already done in
3879 create_interface, but if at that time an inner class was being
3880 dealt with, the interface count was reset in a context created
3881 for the sake of handling inner classes declaration. */
3882 ctxp->interface_number = 0;
3885 static void
3886 add_inner_class_fields (class_decl, fct_decl)
3887 tree class_decl;
3888 tree fct_decl;
3890 tree block, marker, f;
3892 f = add_field (TREE_TYPE (class_decl),
3893 build_current_thisn (TREE_TYPE (class_decl)),
3894 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3895 ACC_PRIVATE);
3896 FIELD_THISN (f) = 1;
3898 if (!fct_decl)
3899 return;
3901 for (block = GET_CURRENT_BLOCK (fct_decl);
3902 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3904 tree decl;
3905 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3907 char *name, *pname;
3908 tree wfl, init, list;
3910 /* Avoid non final arguments. */
3911 if (!LOCAL_FINAL (decl))
3912 continue;
3914 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3915 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3916 wfl = build_wfl_node (get_identifier (name));
3917 init = build_wfl_node (get_identifier (pname));
3918 /* Build an initialization for the field: it will be
3919 initialized by a parameter added to finit$, bearing a
3920 mangled name of the field itself (param$<n>.) The
3921 parameter is provided to finit$ by the constructor
3922 invoking it (hence the constructor will also feature a
3923 hidden parameter, set to the value of the outer context
3924 local at the time the inner class is created.)
3926 Note: we take into account all possible locals that can
3927 be accessed by the inner class. It's actually not trivial
3928 to minimize these aliases down to the ones really
3929 used. One way to do that would be to expand all regular
3930 methods first, then finit$ to get a picture of what's
3931 used. It works with the exception that we would have to
3932 go back on all constructor invoked in regular methods to
3933 have their invokation reworked (to include the right amount
3934 of alias initializer parameters.)
3936 The only real way around, I think, is a first pass to
3937 identify locals really used in the inner class. We leave
3938 the flag FIELD_LOCAL_ALIAS_USED around for that future
3939 use.
3941 On the other hand, it only affect local inner classes,
3942 whose constructors (and finit$ call) will be featuring
3943 unecessary arguments. It's easy for a developper to keep
3944 this number of parameter down by using the `final'
3945 keyword only when necessary. For the time being, we can
3946 issue a warning on unecessary finals. FIXME */
3947 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
3948 wfl, init);
3950 /* Register the field. The TREE_LIST holding the part
3951 initialized/initializer will be marked ARG_FINAL_P so
3952 that the created field can be marked
3953 FIELD_LOCAL_ALIAS. */
3954 list = build_tree_list (wfl, init);
3955 ARG_FINAL_P (list) = 1;
3956 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
3960 if (!CPC_INITIALIZER_STMT (ctxp))
3961 return;
3963 /* If we ever registered an alias field, insert and marker to
3964 remeber where the list ends. The second part of the list (the one
3965 featuring initialized fields) so it can be later reversed to
3966 enforce 8.5. The marker will be removed during that operation. */
3967 marker = build_tree_list (NULL_TREE, NULL_TREE);
3968 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
3969 SET_CPC_INITIALIZER_STMT (ctxp, marker);
3972 /* Can't use lookup_field () since we don't want to load the class and
3973 can't set the CLASS_LOADED_P flag */
3975 static tree
3976 find_field (class, name)
3977 tree class;
3978 tree name;
3980 tree decl;
3981 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
3983 if (DECL_NAME (decl) == name)
3984 return decl;
3986 return NULL_TREE;
3989 /* Wrap around lookup_field that doesn't potentially upset the value
3990 of CLASS */
3992 static tree
3993 lookup_field_wrapper (class, name)
3994 tree class, name;
3996 tree type = class;
3997 tree decl = NULL_TREE;
3998 java_parser_context_save_global ();
4000 /* Last chance: if we're within the context of an inner class, we
4001 might be trying to access a local variable defined in an outer
4002 context. We try to look for it now. */
4003 if (INNER_CLASS_TYPE_P (class))
4005 char *alias_buffer;
4006 tree new_name;
4007 MANGLE_OUTER_LOCAL_VARIABLE_NAME (alias_buffer, name);
4008 new_name = get_identifier (alias_buffer);
4009 decl = lookup_field (&type, new_name);
4010 if (decl && decl != error_mark_node)
4011 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4013 if (!decl || decl == error_mark_node)
4015 type = class;
4016 decl = lookup_field (&type, name);
4019 java_parser_context_restore_global ();
4020 return decl == error_mark_node ? NULL : decl;
4023 /* Find duplicate field within the same class declarations and report
4024 the error. Returns 1 if a duplicated field was found, 0
4025 otherwise. */
4027 static int
4028 duplicate_declaration_error_p (new_field_name, new_type, cl)
4029 tree new_field_name, new_type, cl;
4031 /* This might be modified to work with method decl as well */
4032 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4033 if (decl)
4035 char *t1 = xstrdup (purify_type_name
4036 ((TREE_CODE (new_type) == POINTER_TYPE
4037 && TREE_TYPE (new_type) == NULL_TREE) ?
4038 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4039 lang_printable_name (new_type, 1)));
4040 /* The type may not have been completed by the time we report
4041 the error */
4042 char *t2 = xstrdup (purify_type_name
4043 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4044 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4045 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4046 lang_printable_name (TREE_TYPE (decl), 1)));
4047 parse_error_context
4048 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4049 t1, IDENTIFIER_POINTER (new_field_name),
4050 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4051 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4052 free (t1);
4053 free (t2);
4054 return 1;
4056 return 0;
4059 /* Field registration routine. If TYPE doesn't exist, field
4060 declarations are linked to the undefined TYPE dependency list, to
4061 be later resolved in java_complete_class () */
4063 static void
4064 register_fields (flags, type, variable_list)
4065 int flags;
4066 tree type, variable_list;
4068 tree current, saved_type;
4069 tree class_type = NULL_TREE;
4070 int saved_lineno = lineno;
4071 int must_chain = 0;
4072 tree wfl = NULL_TREE;
4074 if (GET_CPC ())
4075 class_type = TREE_TYPE (GET_CPC ());
4077 if (!class_type || class_type == error_mark_node)
4078 return;
4080 /* If we're adding fields to interfaces, those fields are public,
4081 static, final */
4082 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4084 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4085 flags, ACC_PUBLIC, "interface field(s)");
4086 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4087 flags, ACC_STATIC, "interface field(s)");
4088 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4089 flags, ACC_FINAL, "interface field(s)");
4090 check_modifiers ("Illegal interface member modifier `%s'", flags,
4091 INTERFACE_FIELD_MODIFIERS);
4092 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4095 /* Obtain a suitable type for resolution, if necessary */
4096 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4098 /* If TYPE is fully resolved and we don't have a reference, make one */
4099 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4101 for (current = variable_list, saved_type = type; current;
4102 current = TREE_CHAIN (current), type = saved_type)
4104 tree real_type;
4105 tree field_decl;
4106 tree cl = TREE_PURPOSE (current);
4107 tree init = TREE_VALUE (current);
4108 tree current_name = EXPR_WFL_NODE (cl);
4110 /* Can't declare non-final static fields in inner classes */
4111 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4112 && !(flags & ACC_FINAL))
4113 parse_error_context
4114 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4115 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4116 lang_printable_name (class_type, 0));
4118 /* Process NAME, as it may specify extra dimension(s) for it */
4119 type = build_array_from_name (type, wfl, current_name, &current_name);
4121 /* Type adjustment. We may have just readjusted TYPE because
4122 the variable specified more dimensions. Make sure we have
4123 a reference if we can and don't have one already. Also
4124 change the name if we have an init. */
4125 if (type != saved_type)
4127 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4128 if (init)
4129 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4132 real_type = GET_REAL_TYPE (type);
4133 /* Check for redeclarations */
4134 if (duplicate_declaration_error_p (current_name, real_type, cl))
4135 continue;
4137 /* Set lineno to the line the field was found and create a
4138 declaration for it. Eventually sets the @deprecated tag flag. */
4139 if (flag_emit_xref)
4140 lineno = EXPR_WFL_LINECOL (cl);
4141 else
4142 lineno = EXPR_WFL_LINENO (cl);
4143 field_decl = add_field (class_type, current_name, real_type, flags);
4144 CHECK_DEPRECATED (field_decl);
4146 /* If the couple initializer/initialized is marked ARG_FINAL_P, we
4147 mark the created field FIELD_LOCAL_ALIAS, so that we can
4148 hide parameters to this inner class finit$ and constructors. */
4149 if (ARG_FINAL_P (current))
4150 FIELD_LOCAL_ALIAS (field_decl) = 1;
4152 /* Check if we must chain. */
4153 if (must_chain)
4154 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4156 /* If we have an initialization value tied to the field */
4157 if (init)
4159 /* The field is declared static */
4160 if (flags & ACC_STATIC)
4162 /* We include the field and its initialization part into
4163 a list used to generate <clinit>. After <clinit> is
4164 walked, field initializations will be processed and
4165 fields initialized with known constants will be taken
4166 out of <clinit> and have their DECL_INITIAL set
4167 appropriately. */
4168 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4169 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4170 if (TREE_OPERAND (init, 1)
4171 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4172 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4174 /* A non-static field declared with an immediate initialization is
4175 to be initialized in <init>, if any. This field is remembered
4176 to be processed at the time of the generation of <init>. */
4177 else
4179 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4180 SET_CPC_INITIALIZER_STMT (ctxp, init);
4182 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4183 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4186 lineno = saved_lineno;
4189 /* Generate finit$, using the list of initialized fields to populate
4190 its body. finit$'s parameter(s) list is adjusted to include the
4191 one(s) used to initialized the field(s) caching outer context
4192 local(s). */
4194 static tree
4195 generate_finit (class_type)
4196 tree class_type;
4198 int count = 0;
4199 tree list = TYPE_FINIT_STMT_LIST (class_type);
4200 tree mdecl, current, parms;
4202 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4203 class_type, NULL_TREE,
4204 &count);
4205 CRAFTED_PARAM_LIST_FIXUP (parms);
4206 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4207 finit_identifier_node, parms);
4208 fix_method_argument_names (parms, mdecl);
4209 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4210 mdecl, NULL_TREE);
4211 DECL_FUNCTION_NAP (mdecl) = count;
4212 start_artificial_method_body (mdecl);
4214 for (current = list; current; current = TREE_CHAIN (current))
4215 java_method_add_stmt (mdecl,
4216 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4217 current));
4218 end_artificial_method_body (mdecl);
4219 return mdecl;
4222 static void
4223 add_instance_initializer (mdecl)
4224 tree mdecl;
4226 tree current;
4227 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4228 tree compound = NULL_TREE;
4230 if (stmt_list)
4232 for (current = stmt_list; current; current = TREE_CHAIN (current))
4233 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4235 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4236 NULL_TREE, compound));
4240 /* Shared accros method_declarator and method_header to remember the
4241 patch stage that was reached during the declaration of the method.
4242 A method DECL is built differently is there is no patch
4243 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4244 pending on the currently defined method. */
4246 static int patch_stage;
4248 /* Check the method declaration and add the method to its current
4249 class. If the argument list is known to contain incomplete types,
4250 the method is partially added and the registration will be resume
4251 once the method arguments resolved. If TYPE is NULL, we're dealing
4252 with a constructor. */
4254 static tree
4255 method_header (flags, type, mdecl, throws)
4256 int flags;
4257 tree type, mdecl, throws;
4259 tree type_wfl = NULL_TREE;
4260 tree meth_name = NULL_TREE;
4261 tree current, orig_arg, this_class = NULL;
4262 tree id, meth;
4263 int saved_lineno;
4264 int constructor_ok = 0, must_chain;
4265 int count;
4267 if (mdecl == error_mark_node)
4268 return error_mark_node;
4269 meth = TREE_VALUE (mdecl);
4270 id = TREE_PURPOSE (mdecl);
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 (GET_CPC ())),
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 (GET_CPC () == error_mark_node)
4669 return error_mark_node;
4671 /* If we're dealing with an inner class constructor, we hide the
4672 this$<n> decl in the name field of its parameter declaration. We
4673 also might have to hide the outer context local alias
4674 initializers. Not done when the class is a toplevel class. */
4675 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4676 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4678 tree aliases_list, type, thisn;
4679 /* First the aliases, linked to the regular parameters */
4680 aliases_list =
4681 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4682 TREE_TYPE (GET_CPC ()),
4683 NULL_TREE, NULL);
4684 list = chainon (nreverse (aliases_list), list);
4686 /* Then this$<n> */
4687 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4688 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4689 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4690 list);
4693 for (current = list; current; current = TREE_CHAIN (current))
4695 int must_chain = 0;
4696 tree wfl_name = TREE_PURPOSE (current);
4697 tree type = TREE_VALUE (current);
4698 tree name = EXPR_WFL_NODE (wfl_name);
4699 tree already, arg_node;
4700 tree type_wfl = NULL_TREE;
4701 tree real_type;
4703 /* Obtain a suitable type for resolution, if necessary */
4704 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4706 /* Process NAME, as it may specify extra dimension(s) for it */
4707 type = build_array_from_name (type, type_wfl, name, &name);
4708 EXPR_WFL_NODE (wfl_name) = name;
4710 real_type = GET_REAL_TYPE (type);
4711 if (TREE_CODE (real_type) == RECORD_TYPE)
4713 real_type = promote_type (real_type);
4714 if (TREE_CODE (type) == TREE_LIST)
4715 TREE_PURPOSE (type) = real_type;
4718 /* Check redefinition */
4719 for (already = arg_types; already; already = TREE_CHAIN (already))
4720 if (TREE_PURPOSE (already) == name)
4722 parse_error_context
4723 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4724 IDENTIFIER_POINTER (name),
4725 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4726 break;
4729 /* If we've an incomplete argument type, we know there is a location
4730 to patch when the type get resolved, later. */
4731 jdep = NULL;
4732 if (must_chain)
4734 patch_stage = JDEP_METHOD;
4735 type = register_incomplete_type (patch_stage,
4736 type_wfl, wfl_name, type);
4737 jdep = CLASSD_LAST (ctxp->classd_list);
4738 JDEP_MISC (jdep) = id;
4741 /* The argument node: a name and a (possibly) incomplete type. */
4742 arg_node = build_tree_list (name, real_type);
4743 /* Remeber arguments declared final. */
4744 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4746 if (jdep)
4747 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4748 TREE_CHAIN (arg_node) = arg_types;
4749 arg_types = arg_node;
4751 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4752 node = build_tree_list (id, meth);
4753 return node;
4756 static int
4757 unresolved_type_p (wfl, returned)
4758 tree wfl;
4759 tree *returned;
4762 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4764 if (returned)
4766 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4767 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4768 *returned = TREE_TYPE (decl);
4769 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4770 *returned = TREE_TYPE (GET_CPC ());
4771 else
4772 *returned = NULL_TREE;
4774 return 1;
4776 if (returned)
4777 *returned = wfl;
4778 return 0;
4781 /* From NAME, build a qualified identifier node using the
4782 qualification from the current package definition. */
4784 static tree
4785 parser_qualified_classname (name)
4786 tree name;
4788 tree nested_class_name;
4790 if ((nested_class_name = maybe_make_nested_class_name (name)))
4791 return nested_class_name;
4793 if (ctxp->package)
4794 return merge_qualified_name (ctxp->package, name);
4795 else
4796 return name;
4799 /* Called once the type a interface extends is resolved. Returns 0 if
4800 everything is OK. */
4802 static int
4803 parser_check_super_interface (super_decl, this_decl, this_wfl)
4804 tree super_decl, this_decl, this_wfl;
4806 tree super_type = TREE_TYPE (super_decl);
4808 /* Has to be an interface */
4809 if (!CLASS_INTERFACE (super_decl))
4811 parse_error_context
4812 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4813 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4814 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4815 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4816 "interface" : "class"),
4817 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4818 return 1;
4821 /* Check scope: same package OK, other package: OK if public */
4822 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4823 return 1;
4825 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4826 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4827 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4828 return 0;
4831 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4832 0 if everthing is OK. */
4834 static int
4835 parser_check_super (super_decl, this_decl, wfl)
4836 tree super_decl, this_decl, wfl;
4838 tree super_type = TREE_TYPE (super_decl);
4840 /* SUPER should be a CLASS (neither an array nor an interface) */
4841 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4843 parse_error_context
4844 (wfl, "Class `%s' can't subclass %s `%s'",
4845 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4846 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4847 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4848 return 1;
4851 if (CLASS_FINAL (TYPE_NAME (super_type)))
4853 parse_error_context (wfl, "Can't subclass final classes: %s",
4854 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4855 return 1;
4858 /* Check scope: same package OK, other package: OK if public */
4859 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4860 return 1;
4862 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4863 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4864 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4865 return 0;
4868 /* Create a new dependency list and link it (in a LIFO manner) to the
4869 CTXP list of type dependency list. */
4871 static void
4872 create_jdep_list (ctxp)
4873 struct parser_ctxt *ctxp;
4875 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
4876 new->first = new->last = NULL;
4877 new->next = ctxp->classd_list;
4878 ctxp->classd_list = new;
4881 static jdeplist *
4882 reverse_jdep_list (ctxp)
4883 struct parser_ctxt *ctxp;
4885 register jdeplist *prev = NULL, *current, *next;
4886 for (current = ctxp->classd_list; current; current = next)
4888 next = current->next;
4889 current->next = prev;
4890 prev = current;
4892 return prev;
4895 /* Create a fake pointer based on the ID stored in
4896 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4897 registered again. */
4899 static tree
4900 obtain_incomplete_type (type_name)
4901 tree type_name;
4903 tree ptr, name;
4905 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4906 name = EXPR_WFL_NODE (type_name);
4907 else if (INCOMPLETE_TYPE_P (type_name))
4908 name = TYPE_NAME (type_name);
4909 else
4910 fatal ("invalid type name - obtain_incomplete_type");
4912 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
4913 if (TYPE_NAME (ptr) == name)
4914 break;
4916 if (!ptr)
4918 push_obstacks (&permanent_obstack, &permanent_obstack);
4919 BUILD_PTR_FROM_NAME (ptr, name);
4920 layout_type (ptr);
4921 pop_obstacks ();
4922 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4923 ctxp->incomplete_class = ptr;
4926 return ptr;
4929 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4930 non NULL instead of computing a new fake type based on WFL. The new
4931 dependency is inserted in the current type dependency list, in FIFO
4932 manner. */
4934 static tree
4935 register_incomplete_type (kind, wfl, decl, ptr)
4936 int kind;
4937 tree wfl, decl, ptr;
4939 jdep *new = (jdep *)xmalloc (sizeof (jdep));
4941 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4942 ptr = obtain_incomplete_type (wfl);
4944 JDEP_KIND (new) = kind;
4945 JDEP_DECL (new) = decl;
4946 JDEP_SOLV (new) = ptr;
4947 JDEP_WFL (new) = wfl;
4948 JDEP_CHAIN (new) = NULL;
4949 JDEP_MISC (new) = NULL_TREE;
4950 /* For some dependencies, set the enclosing class of the current
4951 class to be the enclosing context */
4952 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
4953 && GET_ENCLOSING_CPC ())
4954 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
4955 else
4956 JDEP_ENCLOSING (new) = GET_CPC ();
4957 JDEP_GET_PATCH (new) = (tree *)NULL;
4959 JDEP_INSERT (ctxp->classd_list, new);
4961 return ptr;
4964 void
4965 java_check_circular_reference ()
4967 tree current;
4968 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
4970 tree type = TREE_TYPE (current);
4971 if (CLASS_INTERFACE (current))
4973 /* Check all interfaces this class extends */
4974 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
4975 int n, i;
4977 if (!basetype_vec)
4978 return;
4979 n = TREE_VEC_LENGTH (basetype_vec);
4980 for (i = 0; i < n; i++)
4982 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
4983 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
4984 && interface_of_p (type, BINFO_TYPE (vec_elt)))
4985 parse_error_context (lookup_cl (current),
4986 "Cyclic interface inheritance");
4989 else
4990 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
4991 parse_error_context (lookup_cl (current),
4992 "Cyclic class inheritance%s",
4993 (cyclic_inheritance_report ?
4994 cyclic_inheritance_report : ""));
4998 /* Augment the parameter list PARM with parameters crafted to
4999 initialize outer context locals aliases. Through ARTIFICIAL, a
5000 count is kept of the number of crafted parameters. MODE governs
5001 what eventually gets created: something suitable for a function
5002 creation or a function invocation, either the constructor or
5003 finit$. */
5005 static tree
5006 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5007 int mode;
5008 tree class_type, parm;
5009 int *artificial;
5011 tree field;
5012 tree additional_parms = NULL_TREE;
5014 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5015 if (FIELD_LOCAL_ALIAS (field))
5017 char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5018 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5020 switch (mode)
5022 case AIPL_FUNCTION_DECLARATION:
5023 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5024 purpose = build_wfl_node (get_identifier (buffer));
5025 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5026 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5027 else
5028 value = TREE_TYPE (field);
5029 break;
5031 case AIPL_FUNCTION_CREATION:
5032 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5033 purpose = get_identifier (buffer);
5034 value = TREE_TYPE (field);
5035 break;
5037 case AIPL_FUNCTION_FINIT_INVOCATION:
5038 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5039 /* Now, this is wrong. purpose should always be the NAME
5040 of something and value its matching value (decl, type,
5041 etc...) FIXME -- but there is a lot to fix. */
5043 /* When invoked for this kind of operation, we already
5044 know whether a field is used or not. */
5045 purpose = TREE_TYPE (field);
5046 value = build_wfl_node (get_identifier (buffer));
5047 break;
5049 case AIPL_FUNCTION_CTOR_INVOCATION:
5050 /* There are two case: the constructor invokation happends
5051 outside the local inner, in which case, locales from the outer
5052 context are directly used.
5054 Otherwise, we fold to using the alias directly. */
5055 if (class_type == current_class)
5056 value = field;
5057 else
5059 name = get_identifier (&buffer[4]);
5060 value = IDENTIFIER_LOCAL_VALUE (name);
5062 break;
5064 additional_parms = tree_cons (purpose, value, additional_parms);
5065 if (artificial)
5066 *artificial +=1;
5068 if (additional_parms)
5070 if (ANONYMOUS_CLASS_P (class_type)
5071 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5072 additional_parms = nreverse (additional_parms);
5073 parm = chainon (additional_parms, parm);
5076 return parm;
5079 /* Craft a constructor for CLASS_DECL -- what we should do when none
5080 where found. ARGS is non NULL when a special signature must be
5081 enforced. This is the case for anonymous classes. */
5083 static void
5084 craft_constructor (class_decl, args)
5085 tree class_decl, args;
5087 tree class_type = TREE_TYPE (class_decl);
5088 tree parm = NULL_TREE;
5089 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5090 ACC_PUBLIC : 0);
5091 int i = 0, artificial = 0;
5092 tree decl, ctor_name;
5093 char buffer [80];
5095 push_obstacks (&permanent_obstack, &permanent_obstack);
5097 /* The constructor name is <init> unless we're dealing with an
5098 anonymous class, in which case the name will be fixed after having
5099 be expanded. */
5100 if (ANONYMOUS_CLASS_P (class_type))
5101 ctor_name = DECL_NAME (class_decl);
5102 else
5103 ctor_name = init_identifier_node;
5105 /* If we're dealing with an inner class constructor, we hide the
5106 this$<n> decl in the name field of its parameter declaration. */
5107 if (PURE_INNER_CLASS_TYPE_P (class_type))
5109 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5110 parm = tree_cons (build_current_thisn (class_type),
5111 build_pointer_type (type), parm);
5113 /* Some more arguments to be hidden here. The values of the local
5114 variables of the outer context that the inner class needs to see. */
5115 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5116 class_type, parm,
5117 &artificial);
5120 /* Then if there are any args to be enforced, enforce them now */
5121 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5123 sprintf (buffer, "parm%d", i++);
5124 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5127 CRAFTED_PARAM_LIST_FIXUP (parm);
5128 decl = create_artificial_method (class_type, flags, void_type_node,
5129 ctor_name, parm);
5130 fix_method_argument_names (parm, decl);
5131 /* Now, mark the artificial parameters. */
5132 DECL_FUNCTION_NAP (decl) = artificial;
5134 pop_obstacks ();
5135 DECL_CONSTRUCTOR_P (decl) = 1;
5139 /* Fix the constructors. This will be called right after circular
5140 references have been checked. It is necessary to fix constructors
5141 early even if no code generation will take place for that class:
5142 some generated constructor might be required by the class whose
5143 compilation triggered this one to be simply loaded. */
5145 void
5146 java_fix_constructors ()
5148 tree current;
5150 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5152 tree class_type = TREE_TYPE (current);
5153 int saw_ctor = 0;
5154 tree decl;
5156 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5157 continue;
5159 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5161 if (DECL_CONSTRUCTOR_P (decl))
5163 fix_constructors (decl);
5164 saw_ctor = 1;
5168 /* Anonymous class constructor can't be generated that early. */
5169 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5170 craft_constructor (current, NULL_TREE);
5174 /* safe_layout_class just makes sure that we can load a class without
5175 disrupting the current_class, input_file, lineno, etc, information
5176 about the class processed currently. */
5178 void
5179 safe_layout_class (class)
5180 tree class;
5182 tree save_current_class = current_class;
5183 const char *save_input_filename = input_filename;
5184 int save_lineno = lineno;
5186 push_obstacks (&permanent_obstack, &permanent_obstack);
5188 layout_class (class);
5189 pop_obstacks ();
5191 current_class = save_current_class;
5192 input_filename = save_input_filename;
5193 lineno = save_lineno;
5194 CLASS_LOADED_P (class) = 1;
5197 static tree
5198 jdep_resolve_class (dep)
5199 jdep *dep;
5201 tree decl;
5203 if (JDEP_RESOLVED_P (dep))
5204 decl = JDEP_RESOLVED_DECL (dep);
5205 else
5207 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5208 JDEP_DECL (dep), JDEP_WFL (dep));
5209 JDEP_RESOLVED (dep, decl);
5212 if (!decl)
5213 complete_class_report_errors (dep);
5214 else if (PURE_INNER_CLASS_DECL_P (decl))
5215 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5216 return decl;
5219 /* Complete unsatisfied class declaration and their dependencies */
5221 void
5222 java_complete_class ()
5224 tree cclass;
5225 jdeplist *cclassd;
5226 int error_found;
5227 tree type;
5229 push_obstacks (&permanent_obstack, &permanent_obstack);
5231 /* Process imports */
5232 process_imports ();
5234 /* Rever things so we have the right order */
5235 ctxp->class_list = nreverse (ctxp->class_list);
5236 ctxp->classd_list = reverse_jdep_list (ctxp);
5238 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5239 cclass && cclassd;
5240 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5242 jdep *dep;
5243 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5245 tree decl;
5246 if (!(decl = jdep_resolve_class (dep)))
5247 continue;
5249 /* Now it's time to patch */
5250 switch (JDEP_KIND (dep))
5252 case JDEP_SUPER:
5253 /* Simply patch super */
5254 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5255 continue;
5256 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5257 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5258 break;
5260 case JDEP_FIELD:
5262 /* We do part of the job done in add_field */
5263 tree field_decl = JDEP_DECL (dep);
5264 tree field_type = TREE_TYPE (decl);
5265 push_obstacks (&permanent_obstack, &permanent_obstack);
5266 if (TREE_CODE (field_type) == RECORD_TYPE)
5267 field_type = promote_type (field_type);
5268 pop_obstacks ();
5269 TREE_TYPE (field_decl) = field_type;
5270 DECL_ALIGN (field_decl) = 0;
5271 DECL_USER_ALIGN (field_decl) = 0;
5272 layout_decl (field_decl, 0);
5273 SOURCE_FRONTEND_DEBUG
5274 (("Completed field/var decl `%s' with `%s'",
5275 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5276 IDENTIFIER_POINTER (DECL_NAME (decl))));
5277 break;
5279 case JDEP_METHOD: /* We start patching a method */
5280 case JDEP_METHOD_RETURN:
5281 error_found = 0;
5282 while (1)
5284 if (decl)
5286 type = TREE_TYPE(decl);
5287 if (TREE_CODE (type) == RECORD_TYPE)
5288 type = promote_type (type);
5289 JDEP_APPLY_PATCH (dep, type);
5290 SOURCE_FRONTEND_DEBUG
5291 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5292 "Completing fct `%s' with ret type `%s'":
5293 "Completing arg `%s' with type `%s'"),
5294 IDENTIFIER_POINTER (EXPR_WFL_NODE
5295 (JDEP_DECL_WFL (dep))),
5296 IDENTIFIER_POINTER (DECL_NAME (decl))));
5298 else
5299 error_found = 1;
5300 dep = JDEP_CHAIN (dep);
5301 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5302 break;
5303 else
5304 decl = jdep_resolve_class (dep);
5306 if (!error_found)
5308 tree mdecl = JDEP_DECL (dep), signature;
5309 push_obstacks (&permanent_obstack, &permanent_obstack);
5310 /* Recompute and reset the signature, check first that
5311 all types are now defined. If they're not,
5312 dont build the signature. */
5313 if (check_method_types_complete (mdecl))
5315 signature = build_java_signature (TREE_TYPE (mdecl));
5316 set_java_signature (TREE_TYPE (mdecl), signature);
5318 pop_obstacks ();
5320 else
5321 continue;
5322 break;
5324 case JDEP_INTERFACE:
5325 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5326 JDEP_WFL (dep)))
5327 continue;
5328 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5329 break;
5331 case JDEP_PARM:
5332 case JDEP_VARIABLE:
5333 type = TREE_TYPE(decl);
5334 if (TREE_CODE (type) == RECORD_TYPE)
5335 type = promote_type (type);
5336 JDEP_APPLY_PATCH (dep, type);
5337 break;
5339 case JDEP_TYPE:
5340 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5341 SOURCE_FRONTEND_DEBUG
5342 (("Completing a random type dependency on a '%s' node",
5343 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5344 break;
5346 case JDEP_EXCEPTION:
5347 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5348 SOURCE_FRONTEND_DEBUG
5349 (("Completing `%s' `throws' argument node",
5350 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5351 break;
5353 case JDEP_ANONYMOUS:
5354 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5355 break;
5357 default:
5358 fatal ("Can't handle patch code %d - java_complete_class",
5359 JDEP_KIND (dep));
5363 pop_obstacks ();
5364 return;
5367 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5368 array. */
5370 static tree
5371 resolve_class (enclosing, class_type, decl, cl)
5372 tree enclosing, class_type, decl, cl;
5374 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5375 const char *base = name;
5376 tree resolved_type = TREE_TYPE (class_type);
5377 tree resolved_type_decl;
5379 if (resolved_type != NULL_TREE)
5381 tree resolved_type_decl = TYPE_NAME (resolved_type);
5382 if (resolved_type_decl == NULL_TREE
5383 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5385 resolved_type_decl = build_decl (TYPE_DECL,
5386 TYPE_NAME (class_type),
5387 resolved_type);
5389 return resolved_type_decl;
5392 /* 1- Check to see if we have an array. If true, find what we really
5393 want to resolve */
5394 while (name[0] == '[')
5395 name++;
5396 if (base != name)
5398 TYPE_NAME (class_type) = get_identifier (name);
5399 WFL_STRIP_BRACKET (cl, cl);
5402 /* 2- Resolve the bare type */
5403 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5404 decl, cl)))
5405 return NULL_TREE;
5406 resolved_type = TREE_TYPE (resolved_type_decl);
5408 /* 3- If we have and array, reconstruct the array down to its nesting */
5409 if (base != name)
5411 while (base != name)
5413 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5414 resolved_type = promote_type (resolved_type);
5415 resolved_type = build_java_array_type (resolved_type, -1);
5416 CLASS_LOADED_P (resolved_type) = 1;
5417 name--;
5419 /* Build a fake decl for this, since this is what is expected to
5420 be returned. */
5421 resolved_type_decl =
5422 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5423 /* Figure how those two things are important for error report. FIXME */
5424 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5425 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
5426 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
5428 TREE_TYPE (class_type) = resolved_type;
5429 return resolved_type_decl;
5432 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5433 are used to report error messages. */
5435 tree
5436 do_resolve_class (enclosing, class_type, decl, cl)
5437 tree enclosing, class_type, decl, cl;
5439 tree new_class_decl;
5441 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5442 it is changed by find_in_imports{_on_demand} and (but it doesn't
5443 really matter) qualify_and_find */
5445 /* 0- Search in the current class as an inner class */
5447 /* Maybe some code here should be added to load the class or
5448 something, at least if the class isn't an inner class and ended
5449 being loaded from class file. FIXME. */
5450 while (enclosing)
5452 tree name;
5454 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5455 return new_class_decl;
5457 /* Explore enclosing contexts. */
5458 while (INNER_CLASS_DECL_P (enclosing))
5460 enclosing = DECL_CONTEXT (enclosing);
5461 if ((new_class_decl = find_as_inner_class (enclosing,
5462 class_type, cl)))
5463 return new_class_decl;
5466 /* Now go to the upper classes, bail out if necessary. */
5467 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5468 if (!enclosing || enclosing == object_type_node)
5469 break;
5471 if (TREE_CODE (enclosing) == RECORD_TYPE)
5473 enclosing = TYPE_NAME (enclosing);
5474 continue;
5477 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
5478 BUILD_PTR_FROM_NAME (name, enclosing);
5479 else
5480 name = enclosing;
5481 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5484 /* 1- Check for the type in single imports. This will change
5485 TYPE_NAME() if something relevant is found */
5486 find_in_imports (class_type);
5488 /* 2- And check for the type in the current compilation unit */
5489 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5491 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5492 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5493 load_class (TYPE_NAME (class_type), 0);
5494 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5497 /* 3- Search according to the current package definition */
5498 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5500 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5501 TYPE_NAME (class_type))))
5502 return new_class_decl;
5505 /* 4- Check the import on demands. Don't allow bar.baz to be
5506 imported from foo.* */
5507 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5508 if (find_in_imports_on_demand (class_type))
5509 return NULL_TREE;
5511 /* If found in find_in_imports_on_demant, the type has already been
5512 loaded. */
5513 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5514 return new_class_decl;
5516 /* 5- Try with a name qualified with the package name we've seen so far */
5517 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5519 tree package;
5521 /* If there is a current package (ctxp->package), it's the first
5522 element of package_list and we can skip it. */
5523 for (package = (ctxp->package ?
5524 TREE_CHAIN (package_list) : package_list);
5525 package; package = TREE_CHAIN (package))
5526 if ((new_class_decl = qualify_and_find (class_type,
5527 TREE_PURPOSE (package),
5528 TYPE_NAME (class_type))))
5529 return new_class_decl;
5532 /* 5- Check an other compilation unit that bears the name of type */
5533 load_class (TYPE_NAME (class_type), 0);
5534 if (check_pkg_class_access (TYPE_NAME (class_type),
5535 (cl ? cl : lookup_cl (decl))))
5536 return NULL_TREE;
5538 /* 6- Last call for a resolution */
5539 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5542 static tree
5543 qualify_and_find (class_type, package, name)
5544 tree class_type, package, name;
5546 tree new_qualified = merge_qualified_name (package, name);
5547 tree new_class_decl;
5549 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5550 load_class (new_qualified, 0);
5551 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5553 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5554 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5555 load_class (new_qualified, 0);
5556 TYPE_NAME (class_type) = new_qualified;
5557 return IDENTIFIER_CLASS_VALUE (new_qualified);
5559 return NULL_TREE;
5562 /* Resolve NAME and lay it out (if not done and if not the current
5563 parsed class). Return a decl node. This function is meant to be
5564 called when type resolution is necessary during the walk pass. */
5566 static tree
5567 resolve_and_layout (something, cl)
5568 tree something;
5569 tree cl;
5571 tree decl, decl_type;
5573 /* Don't do that on the current class */
5574 if (something == current_class)
5575 return TYPE_NAME (current_class);
5577 /* Don't do anything for void and other primitive types */
5578 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5579 return NULL_TREE;
5581 /* Pointer types can be reall pointer types or fake pointers. When
5582 finding a real pointer, recheck for primitive types */
5583 if (TREE_CODE (something) == POINTER_TYPE)
5585 if (TREE_TYPE (something))
5587 something = TREE_TYPE (something);
5588 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5589 return NULL_TREE;
5591 else
5592 something = TYPE_NAME (something);
5595 /* Don't do anything for arrays of primitive types */
5596 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5597 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5598 return NULL_TREE;
5600 /* Something might be a WFL */
5601 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5602 something = EXPR_WFL_NODE (something);
5604 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5605 TYPE_DECL or a real TYPE */
5606 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5607 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5608 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5610 if (!(decl = resolve_no_layout (something, cl)))
5611 return NULL_TREE;
5613 /* Resolve and layout if necessary */
5614 decl_type = TREE_TYPE (decl);
5615 layout_class_methods (decl_type);
5616 /* Check methods */
5617 if (CLASS_FROM_SOURCE_P (decl_type))
5618 java_check_methods (decl);
5619 /* Layout the type if necessary */
5620 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5621 safe_layout_class (decl_type);
5623 return decl;
5626 /* Resolve a class, returns its decl but doesn't perform any
5627 layout. The current parsing context is saved and restored */
5629 static tree
5630 resolve_no_layout (name, cl)
5631 tree name, cl;
5633 tree ptr, decl;
5634 BUILD_PTR_FROM_NAME (ptr, name);
5635 java_parser_context_save_global ();
5636 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5637 java_parser_context_restore_global ();
5639 return decl;
5642 /* Called when reporting errors. Skip leader '[' in a complex array
5643 type description that failed to be resolved. */
5645 static const char *
5646 purify_type_name (name)
5647 const char *name;
5649 while (*name && *name == '[')
5650 name++;
5651 return name;
5654 /* The type CURRENT refers to can't be found. We print error messages. */
5656 static void
5657 complete_class_report_errors (dep)
5658 jdep *dep;
5660 const char *name;
5662 if (!JDEP_WFL (dep))
5663 return;
5665 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5666 switch (JDEP_KIND (dep))
5668 case JDEP_SUPER:
5669 parse_error_context
5670 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5671 purify_type_name (name),
5672 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5673 break;
5674 case JDEP_FIELD:
5675 parse_error_context
5676 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5677 purify_type_name (name),
5678 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5679 break;
5680 case JDEP_METHOD: /* Covers arguments */
5681 parse_error_context
5682 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5683 purify_type_name (name),
5684 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5685 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5686 break;
5687 case JDEP_METHOD_RETURN: /* Covers return type */
5688 parse_error_context
5689 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5690 purify_type_name (name),
5691 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5692 break;
5693 case JDEP_INTERFACE:
5694 parse_error_context
5695 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5696 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5697 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5698 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5699 break;
5700 case JDEP_VARIABLE:
5701 parse_error_context
5702 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5703 purify_type_name (IDENTIFIER_POINTER
5704 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5705 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5706 break;
5707 case JDEP_EXCEPTION: /* As specified by `throws' */
5708 parse_error_context
5709 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5710 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5711 break;
5712 default:
5713 /* Fix for -Wall. Just break doing nothing. The error will be
5714 caught later */
5715 break;
5719 /* Return a static string containing the DECL prototype string. If
5720 DECL is a constructor, use the class name instead of the form
5721 <init> */
5723 static const char *
5724 get_printable_method_name (decl)
5725 tree decl;
5727 const char *to_return;
5728 tree name = NULL_TREE;
5730 if (DECL_CONSTRUCTOR_P (decl))
5732 name = DECL_NAME (decl);
5733 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
5736 to_return = lang_printable_name (decl, 0);
5737 if (DECL_CONSTRUCTOR_P (decl))
5738 DECL_NAME (decl) = name;
5740 return to_return;
5743 /* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5744 nevertheless needs to be verfied, 1 otherwise. */
5746 static int
5747 reset_method_name (method)
5748 tree method;
5750 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5752 /* NAME is just the plain name when Object is being defined */
5753 if (DECL_CONTEXT (method) != object_type_node)
5754 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5755 init_identifier_node : GET_METHOD_NAME (method));
5756 return 0;
5758 else
5759 return 1;
5762 /* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5764 tree
5765 java_get_real_method_name (method_decl)
5766 tree method_decl;
5768 tree method_name = DECL_NAME (method_decl);
5769 if (DECL_CONSTRUCTOR_P (method_decl))
5770 return init_identifier_node;
5772 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5773 and still can be a constructor. FIXME */
5775 /* Don't confuse method only bearing the name of their class as
5776 constructors */
5777 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5778 && ctxp
5779 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
5780 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5781 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
5782 return init_identifier_node;
5783 else
5784 return EXPR_WFL_NODE (method_name);
5787 /* Track method being redefined inside the same class. As a side
5788 effect, set DECL_NAME to an IDENTIFIER (prior entering this
5789 function it's a FWL, so we can track errors more accurately.) */
5791 static int
5792 check_method_redefinition (class, method)
5793 tree class, method;
5795 tree redef, name;
5796 tree cl = DECL_NAME (method);
5797 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
5798 /* decl name of artificial <clinit> and finit$ doesn't need to be
5799 fixed and checked */
5801 /* Reset the method name before running the check. If it returns 1,
5802 the method doesn't need to be verified with respect to method
5803 redeclaration and we return 0 */
5804 if (reset_method_name (method))
5805 return 0;
5807 name = DECL_NAME (method);
5808 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5810 if (redef == method)
5811 break;
5812 if (DECL_NAME (redef) == name
5813 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
5815 parse_error_context
5816 (cl, "Duplicate %s declaration `%s'",
5817 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5818 get_printable_method_name (redef));
5819 return 1;
5822 return 0;
5825 /* Return 1 if check went ok, 0 otherwise. */
5826 static int
5827 check_abstract_method_definitions (do_interface, class_decl, type)
5828 int do_interface;
5829 tree class_decl, type;
5831 tree class = TREE_TYPE (class_decl);
5832 tree method, end_type;
5833 int ok = 1;
5835 end_type = (do_interface ? object_type_node : type);
5836 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5838 tree other_super, other_method, method_sig, method_name;
5839 int found = 0;
5840 int end_type_reached = 0;
5842 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5843 continue;
5845 /* Now verify that somewhere in between TYPE and CLASS,
5846 abstract method METHOD gets a non abstract definition
5847 that is inherited by CLASS. */
5849 method_sig = build_java_signature (TREE_TYPE (method));
5850 method_name = DECL_NAME (method);
5851 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5852 method_name = EXPR_WFL_NODE (method_name);
5854 other_super = class;
5855 do {
5856 if (other_super == end_type)
5857 end_type_reached = 1;
5859 /* Method search */
5860 for (other_method = TYPE_METHODS (other_super); other_method;
5861 other_method = TREE_CHAIN (other_method))
5863 tree s = build_java_signature (TREE_TYPE (other_method));
5864 tree other_name = DECL_NAME (other_method);
5866 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5867 other_name = EXPR_WFL_NODE (other_name);
5868 if (!DECL_CLINIT_P (other_method)
5869 && !DECL_CONSTRUCTOR_P (other_method)
5870 && method_name == other_name
5871 && method_sig == s
5872 && !METHOD_ABSTRACT (other_method))
5874 found = 1;
5875 break;
5878 other_super = CLASSTYPE_SUPER (other_super);
5879 } while (!end_type_reached);
5881 /* Report that abstract METHOD didn't find an implementation
5882 that CLASS can use. */
5883 if (!found)
5885 char *t = xstrdup (lang_printable_name
5886 (TREE_TYPE (TREE_TYPE (method)), 0));
5887 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5888 tree saved_wfl = NULL_TREE;
5890 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5892 saved_wfl = DECL_NAME (method);
5893 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5896 parse_error_context
5897 (lookup_cl (class_decl),
5898 "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",
5899 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5900 t, lang_printable_name (method, 0),
5901 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5902 "interface" : "class"),
5903 IDENTIFIER_POINTER (ccn),
5904 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5905 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5906 ok = 0;
5907 free (t);
5909 if (saved_wfl)
5910 DECL_NAME (method) = saved_wfl;
5914 if (ok && do_interface)
5916 /* Check for implemented interfaces. */
5917 int i;
5918 tree vector = TYPE_BINFO_BASETYPES (type);
5919 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5921 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5922 ok = check_abstract_method_definitions (1, class_decl, super);
5926 return ok;
5929 /* Check that CLASS_DECL somehow implements all inherited abstract
5930 methods. */
5932 static void
5933 java_check_abstract_method_definitions (class_decl)
5934 tree class_decl;
5936 tree class = TREE_TYPE (class_decl);
5937 tree super, vector;
5938 int i;
5940 if (CLASS_ABSTRACT (class_decl))
5941 return;
5943 /* Check for inherited types */
5944 super = class;
5945 do {
5946 super = CLASSTYPE_SUPER (super);
5947 check_abstract_method_definitions (0, class_decl, super);
5948 } while (super != object_type_node);
5950 /* Check for implemented interfaces. */
5951 vector = TYPE_BINFO_BASETYPES (class);
5952 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5954 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5955 check_abstract_method_definitions (1, class_decl, super);
5959 /* Check all the types method DECL uses and return 1 if all of them
5960 are now complete, 0 otherwise. This is used to check whether its
5961 safe to build a method signature or not. */
5963 static int
5964 check_method_types_complete (decl)
5965 tree decl;
5967 tree type = TREE_TYPE (decl);
5968 tree args;
5970 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5971 return 0;
5973 args = TYPE_ARG_TYPES (type);
5974 if (TREE_CODE (type) == METHOD_TYPE)
5975 args = TREE_CHAIN (args);
5976 for (; args != end_params_node; args = TREE_CHAIN (args))
5977 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5978 return 0;
5980 return 1;
5983 /* Visible interface to check methods contained in CLASS_DECL */
5985 void
5986 java_check_methods (class_decl)
5987 tree class_decl;
5989 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
5990 return;
5992 if (CLASS_INTERFACE (class_decl))
5993 java_check_abstract_methods (class_decl);
5994 else
5995 java_check_regular_methods (class_decl);
5997 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6000 /* Check all the methods of CLASS_DECL. Methods are first completed
6001 then checked according to regular method existance rules. If no
6002 constructor for CLASS_DECL were encountered, then build its
6003 declaration. */
6005 static void
6006 java_check_regular_methods (class_decl)
6007 tree class_decl;
6009 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6010 tree method;
6011 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6012 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
6013 tree mthrows;
6015 /* It is not necessary to check methods defined in java.lang.Object */
6016 if (class == object_type_node)
6017 return;
6019 if (!TYPE_NVIRTUALS (class))
6020 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6022 /* Should take interfaces into account. FIXME */
6023 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6025 tree sig;
6026 tree method_wfl = DECL_NAME (method);
6027 int aflags;
6029 /* If we previously found something and its name was saved,
6030 reinstall it now */
6031 if (found && saved_found_wfl)
6033 DECL_NAME (found) = saved_found_wfl;
6034 saved_found_wfl = NULL_TREE;
6037 /* Check for redefinitions */
6038 if (check_method_redefinition (class, method))
6039 continue;
6041 /* If we see one constructor a mark so we don't generate the
6042 default one. Also skip other verifications: constructors
6043 can't be inherited hence hiden or overriden */
6044 if (DECL_CONSTRUCTOR_P (method))
6046 saw_constructor = 1;
6047 continue;
6050 /* We verify things thrown by the method. They must inherits from
6051 java.lang.Throwable */
6052 for (mthrows = DECL_FUNCTION_THROWS (method);
6053 mthrows; mthrows = TREE_CHAIN (mthrows))
6055 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6056 parse_error_context
6057 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6058 IDENTIFIER_POINTER
6059 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6062 sig = build_java_argument_signature (TREE_TYPE (method));
6063 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6065 /* Inner class can't declare static methods */
6066 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6068 char *t = xstrdup (lang_printable_name (class, 0));
6069 parse_error_context
6070 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6071 lang_printable_name (method, 0), t);
6072 free (t);
6075 /* Nothing overrides or it's a private method. */
6076 if (!found)
6077 continue;
6078 if (METHOD_PRIVATE (found))
6080 found = NULL_TREE;
6081 continue;
6084 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6085 We set it temporarily for the sake of the error report. */
6086 saved_found_wfl = DECL_NAME (found);
6087 reset_method_name (found);
6089 /* If `found' is declared in an interface, make sure the
6090 modifier matches. */
6091 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6092 && clinit_identifier_node != DECL_NAME (found)
6093 && !METHOD_PUBLIC (method))
6095 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6096 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6097 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6098 lang_printable_name (method, 0),
6099 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6102 /* Can't override a method with the same name and different return
6103 types. */
6104 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6106 char *t = xstrdup
6107 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6108 parse_error_context
6109 (method_wfl,
6110 "Method `%s' was defined with return type `%s' in class `%s'",
6111 lang_printable_name (found, 0), t,
6112 IDENTIFIER_POINTER
6113 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6114 free (t);
6117 aflags = get_access_flags_from_decl (found);
6118 /* If the method has default, access in an other package, then
6119 issue a warning that the current method doesn't override the
6120 one that was found elsewhere. Do not issue this warning when
6121 the match was found in java.lang.Object. */
6122 if (DECL_CONTEXT (found) != object_type_node
6123 && ((aflags & ACC_VISIBILITY) == 0)
6124 && !class_in_current_package (DECL_CONTEXT (found))
6125 && !DECL_CLINIT_P (found)
6126 && flag_not_overriding)
6128 parse_warning_context
6129 (method_wfl, "Method `%s' in class `%s' does not override the corresponding method in class `%s', which is private to a different package",
6130 lang_printable_name (found, 0),
6131 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6132 IDENTIFIER_POINTER (DECL_NAME
6133 (TYPE_NAME (DECL_CONTEXT (found)))));
6134 continue;
6137 /* Can't override final. Can't override static. */
6138 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6140 /* Static *can* override static */
6141 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6142 continue;
6143 parse_error_context
6144 (method_wfl,
6145 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6146 (METHOD_FINAL (found) ? "Final" : "Static"),
6147 lang_printable_name (found, 0),
6148 (METHOD_FINAL (found) ? "final" : "static"),
6149 IDENTIFIER_POINTER
6150 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6151 continue;
6154 /* Static method can't override instance method. */
6155 if (METHOD_STATIC (method))
6157 parse_error_context
6158 (method_wfl,
6159 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6160 lang_printable_name (found, 0),
6161 IDENTIFIER_POINTER
6162 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6163 continue;
6166 /* - Overriding/hiding public must be public
6167 - Overriding/hiding protected must be protected or public
6168 - If the overriden or hidden method has default (package)
6169 access, then the overriding or hiding method must not be
6170 private; otherwise, a compile-time error occurs. If
6171 `found' belongs to an interface, things have been already
6172 taken care of. */
6173 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6174 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6175 || (METHOD_PROTECTED (found)
6176 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6177 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6178 && METHOD_PRIVATE (method))))
6180 parse_error_context
6181 (method_wfl,
6182 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6183 (METHOD_PUBLIC (method) ? "public" :
6184 (METHOD_PRIVATE (method) ? "private" : "protected")),
6185 IDENTIFIER_POINTER (DECL_NAME
6186 (TYPE_NAME (DECL_CONTEXT (found)))));
6187 continue;
6190 /* Overriding methods must have compatible `throws' clauses on checked
6191 exceptions, if any */
6192 check_throws_clauses (method, method_wfl, found);
6194 /* Inheriting multiple methods with the same signature. FIXME */
6197 /* Don't forget eventual pending found and saved_found_wfl. Take
6198 into account that we might have exited because we saw an
6199 artificial method as the last entry. */
6201 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6202 DECL_NAME (found) = saved_found_wfl;
6204 if (!TYPE_NVIRTUALS (class))
6205 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6207 /* Search for inherited abstract method not yet implemented in this
6208 class. */
6209 java_check_abstract_method_definitions (class_decl);
6211 if (!saw_constructor)
6212 fatal ("No constructor found");
6215 /* Return a non zero value if the `throws' clause of METHOD (if any)
6216 is incompatible with the `throws' clause of FOUND (if any). */
6218 static void
6219 check_throws_clauses (method, method_wfl, found)
6220 tree method, method_wfl, found;
6222 tree mthrows, fthrows;
6224 /* Can't check these things with class loaded from bytecode. FIXME */
6225 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6226 return;
6228 for (mthrows = DECL_FUNCTION_THROWS (method);
6229 mthrows; mthrows = TREE_CHAIN (mthrows))
6231 /* We don't verify unchecked expressions */
6232 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6233 continue;
6234 /* Checked expression must be compatible */
6235 for (fthrows = DECL_FUNCTION_THROWS (found);
6236 fthrows; fthrows = TREE_CHAIN (fthrows))
6237 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6238 break;
6239 if (!fthrows)
6241 parse_error_context
6242 (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'",
6243 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6244 lang_printable_name (found, 0),
6245 IDENTIFIER_POINTER
6246 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6251 /* Check abstract method of interface INTERFACE */
6253 static void
6254 java_check_abstract_methods (interface_decl)
6255 tree interface_decl;
6257 int i, n;
6258 tree method, basetype_vec, found;
6259 tree interface = TREE_TYPE (interface_decl);
6261 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6263 tree method_wfl = DECL_NAME (method);
6265 /* 2- Check for double definition inside the defining interface */
6266 if (check_method_redefinition (interface, method))
6267 continue;
6269 /* 3- Overriding is OK as far as we preserve the return type and
6270 the thrown exceptions (FIXME) */
6271 found = lookup_java_interface_method2 (interface, method);
6272 if (found)
6274 char *t;
6275 tree saved_found_wfl = DECL_NAME (found);
6276 reset_method_name (found);
6277 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6278 parse_error_context
6279 (method_wfl,
6280 "Method `%s' was defined with return type `%s' in class `%s'",
6281 lang_printable_name (found, 0), t,
6282 IDENTIFIER_POINTER
6283 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6284 free (t);
6285 DECL_NAME (found) = saved_found_wfl;
6286 continue;
6290 /* 4- Inherited methods can't differ by their returned types */
6291 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6292 return;
6293 n = TREE_VEC_LENGTH (basetype_vec);
6294 for (i = 0; i < n; i++)
6296 tree sub_interface_method, sub_interface;
6297 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6298 if (!vec_elt)
6299 continue;
6300 sub_interface = BINFO_TYPE (vec_elt);
6301 for (sub_interface_method = TYPE_METHODS (sub_interface);
6302 sub_interface_method;
6303 sub_interface_method = TREE_CHAIN (sub_interface_method))
6305 found = lookup_java_interface_method2 (interface,
6306 sub_interface_method);
6307 if (found && (found != sub_interface_method))
6309 tree saved_found_wfl = DECL_NAME (found);
6310 reset_method_name (found);
6311 parse_error_context
6312 (lookup_cl (sub_interface_method),
6313 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6314 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6315 lang_printable_name (found, 0),
6316 IDENTIFIER_POINTER
6317 (DECL_NAME (TYPE_NAME
6318 (DECL_CONTEXT (sub_interface_method)))),
6319 IDENTIFIER_POINTER
6320 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6321 DECL_NAME (found) = saved_found_wfl;
6327 /* Lookup methods in interfaces using their name and partial
6328 signature. Return a matching method only if their types differ. */
6330 static tree
6331 lookup_java_interface_method2 (class, method_decl)
6332 tree class, method_decl;
6334 int i, n;
6335 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6337 if (!basetype_vec)
6338 return NULL_TREE;
6340 n = TREE_VEC_LENGTH (basetype_vec);
6341 for (i = 0; i < n; i++)
6343 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6344 if ((BINFO_TYPE (vec_elt) != object_type_node)
6345 && (to_return =
6346 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6347 return to_return;
6349 for (i = 0; i < n; i++)
6351 to_return = lookup_java_interface_method2
6352 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6353 if (to_return)
6354 return to_return;
6357 return NULL_TREE;
6360 /* Lookup method using their name and partial signature. Return a
6361 matching method only if their types differ. */
6363 static tree
6364 lookup_java_method2 (clas, method_decl, do_interface)
6365 tree clas, method_decl;
6366 int do_interface;
6368 tree method, method_signature, method_name, method_type, name;
6370 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6371 name = DECL_NAME (method_decl);
6372 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6373 EXPR_WFL_NODE (name) : name);
6374 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6376 while (clas != NULL_TREE)
6378 for (method = TYPE_METHODS (clas);
6379 method != NULL_TREE; method = TREE_CHAIN (method))
6381 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6382 tree name = DECL_NAME (method);
6383 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6384 EXPR_WFL_NODE (name) : name) == method_name
6385 && method_sig == method_signature
6386 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6387 return method;
6389 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6391 return NULL_TREE;
6394 /* Return the line that matches DECL line number, and try its best to
6395 position the column number. Used during error reports. */
6397 static tree
6398 lookup_cl (decl)
6399 tree decl;
6401 static tree cl = NULL_TREE;
6402 char *line, *found;
6404 if (!decl)
6405 return NULL_TREE;
6407 if (cl == NULL_TREE)
6408 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6410 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6411 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6413 line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)),
6414 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6416 found = strstr ((const char *)line,
6417 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6418 if (found)
6419 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6421 return cl;
6424 /* Look for a simple name in the single-type import list */
6426 static tree
6427 find_name_in_single_imports (name)
6428 tree name;
6430 tree node;
6432 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6433 if (TREE_VALUE (node) == name)
6434 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6436 return NULL_TREE;
6439 /* Process all single-type import. */
6441 static int
6442 process_imports ()
6444 tree import;
6445 int error_found;
6447 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6449 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6451 /* Don't load twice something already defined. */
6452 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6453 continue;
6454 QUALIFIED_P (to_be_found) = 1;
6455 load_class (to_be_found, 0);
6456 error_found =
6457 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6458 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6460 parse_error_context (TREE_PURPOSE (import),
6461 "Class or interface `%s' not found in import",
6462 IDENTIFIER_POINTER (to_be_found));
6463 return 1;
6465 if (error_found)
6466 return 1;
6468 return 0;
6471 /* Possibly find and mark a class imported by a single-type import
6472 statement. */
6474 static void
6475 find_in_imports (class_type)
6476 tree class_type;
6478 tree import;
6480 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6481 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6483 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6484 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6488 static int
6489 note_possible_classname (name, len)
6490 const char *name;
6491 int len;
6493 tree node;
6494 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6495 len = len - 5;
6496 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6497 len = len - 6;
6498 else
6499 return 0;
6500 node = ident_subst (name, len, "", '/', '.', "");
6501 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6502 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6503 return 1;
6506 /* Read a import directory, gathering potential match for further type
6507 references. Indifferently reads a filesystem or a ZIP archive
6508 directory. */
6510 static void
6511 read_import_dir (wfl)
6512 tree wfl;
6514 tree package_id = EXPR_WFL_NODE (wfl);
6515 const char *package_name = IDENTIFIER_POINTER (package_id);
6516 int package_length = IDENTIFIER_LENGTH (package_id);
6517 DIR *dirp = NULL;
6518 JCF *saved_jcf = current_jcf;
6520 int found = 0;
6521 int k;
6522 void *entry;
6523 struct buffer filename[1];
6526 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6527 return;
6528 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6530 BUFFER_INIT (filename);
6531 buffer_grow (filename, package_length + 100);
6533 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6535 const char *entry_name = jcf_path_name (entry);
6536 int entry_length = strlen (entry_name);
6537 if (jcf_path_is_zipfile (entry))
6539 ZipFile *zipf;
6540 buffer_grow (filename, entry_length);
6541 memcpy (filename->data, entry_name, entry_length - 1);
6542 filename->data[entry_length-1] = '\0';
6543 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6544 if (zipf == NULL)
6545 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6546 else
6548 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6549 BUFFER_RESET (filename);
6550 for (k = 0; k < package_length; k++)
6552 char ch = package_name[k];
6553 *filename->ptr++ = ch == '.' ? '/' : ch;
6555 *filename->ptr++ = '/';
6557 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6559 const char *current_entry = ZIPDIR_FILENAME (zipd);
6560 int current_entry_len = zipd->filename_length;
6562 if (current_entry_len >= BUFFER_LENGTH (filename)
6563 && strncmp (filename->data, current_entry,
6564 BUFFER_LENGTH (filename)) != 0)
6565 continue;
6566 found |= note_possible_classname (current_entry,
6567 current_entry_len);
6571 else
6573 BUFFER_RESET (filename);
6574 buffer_grow (filename, entry_length + package_length + 4);
6575 strcpy (filename->data, entry_name);
6576 filename->ptr = filename->data + entry_length;
6577 for (k = 0; k < package_length; k++)
6579 char ch = package_name[k];
6580 *filename->ptr++ = ch == '.' ? '/' : ch;
6582 *filename->ptr = '\0';
6584 dirp = opendir (filename->data);
6585 if (dirp == NULL)
6586 continue;
6587 *filename->ptr++ = '/';
6588 for (;;)
6590 int len;
6591 const char *d_name;
6592 struct dirent *direntp = readdir (dirp);
6593 if (!direntp)
6594 break;
6595 d_name = direntp->d_name;
6596 len = strlen (direntp->d_name);
6597 buffer_grow (filename, len+1);
6598 strcpy (filename->ptr, d_name);
6599 found |= note_possible_classname (filename->data + entry_length,
6600 package_length+len+1);
6602 if (dirp)
6603 closedir (dirp);
6607 free (filename->data);
6609 /* Here we should have a unified way of retrieving an entry, to be
6610 indexed. */
6611 if (!found)
6613 static int first = 1;
6614 if (first)
6616 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6617 java_error_count++;
6618 first = 0;
6620 else
6621 parse_error_context (wfl, "Package `%s' not found in import",
6622 package_name);
6623 current_jcf = saved_jcf;
6624 return;
6626 current_jcf = saved_jcf;
6629 /* Possibly find a type in the import on demands specified
6630 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6631 entire list, to detected potential double definitions. */
6633 static int
6634 find_in_imports_on_demand (class_type)
6635 tree class_type;
6637 tree node, import, node_to_use = NULL_TREE;
6638 int seen_once = -1;
6639 tree cl = NULL_TREE;
6641 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6643 const char *id_name;
6644 obstack_grow (&temporary_obstack,
6645 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6646 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6647 obstack_1grow (&temporary_obstack, '.');
6648 obstack_grow0 (&temporary_obstack,
6649 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6650 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6651 id_name = obstack_finish (&temporary_obstack);
6653 node = maybe_get_identifier (id_name);
6654 if (node && IS_A_CLASSFILE_NAME (node))
6656 if (seen_once < 0)
6658 cl = TREE_PURPOSE (import);
6659 seen_once = 1;
6660 node_to_use = node;
6662 else
6664 seen_once++;
6665 parse_error_context
6666 (TREE_PURPOSE (import),
6667 "Type `%s' also potentially defined in package `%s'",
6668 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6669 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6674 if (seen_once == 1)
6676 /* Setup lineno so that it refers to the line of the import (in
6677 case we parse a class file and encounter errors */
6678 tree decl;
6679 int saved_lineno = lineno;
6680 lineno = EXPR_WFL_LINENO (cl);
6681 TYPE_NAME (class_type) = node_to_use;
6682 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6683 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6684 /* If there is no DECL set for the class or if the class isn't
6685 loaded and not seen in source yet, the load */
6686 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6687 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6688 load_class (node_to_use, 0);
6689 lineno = saved_lineno;
6690 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6692 else
6693 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6696 /* Add package NAME to the list of package encountered so far. To
6697 speed up class lookup in do_resolve_class, we make sure a
6698 particular package is added only once. */
6700 static void
6701 register_package (name)
6702 tree name;
6704 static struct hash_table _pht, *pht = NULL;
6706 if (!pht)
6708 hash_table_init (&_pht, hash_newfunc,
6709 java_hash_hash_tree_node, java_hash_compare_tree_node);
6710 pht = &_pht;
6713 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6715 package_list = chainon (package_list, build_tree_list (name, NULL));
6716 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6720 static tree
6721 resolve_package (pkg, next)
6722 tree pkg, *next;
6724 tree current, acc;
6725 tree type_name = NULL_TREE;
6726 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6728 /* The trick is to determine when the package name stops and were
6729 the name of something contained in the package starts. Then we
6730 return a fully qualified name of what we want to get. */
6732 /* Do a quick search on well known package names */
6733 if (!strncmp (name, "java.lang.reflect", 17))
6735 *next =
6736 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6737 type_name = lookup_package_type (name, 17);
6739 else if (!strncmp (name, "java.lang", 9))
6741 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6742 type_name = lookup_package_type (name, 9);
6745 /* If we found something here, return */
6746 if (type_name)
6747 return type_name;
6749 *next = EXPR_WFL_QUALIFICATION (pkg);
6751 /* Try the current package. */
6752 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6753 IDENTIFIER_LENGTH (ctxp->package)))
6755 type_name =
6756 lookup_package_type_and_set_next (name,
6757 IDENTIFIER_LENGTH (ctxp->package),
6758 next );
6759 if (type_name)
6760 return type_name;
6763 /* Search in imported package */
6764 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6766 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6767 int len = IDENTIFIER_LENGTH (current_pkg_name);
6768 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6770 tree left, dummy;
6772 breakdown_qualified (&left, &dummy, current_pkg_name);
6773 len = IDENTIFIER_LENGTH (left);
6774 type_name = lookup_package_type_and_set_next (name, len, next);
6775 if (type_name)
6776 break;
6780 /* Try to progressively construct a type name */
6781 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6782 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6783 current; current = TREE_CHAIN (current))
6785 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6786 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6788 type_name = acc;
6789 /* resolve_package should be used in a loop, hence we
6790 point at this one to naturally process the next one at
6791 the next iteration. */
6792 *next = current;
6793 break;
6796 return type_name;
6799 static tree
6800 lookup_package_type_and_set_next (name, len, next)
6801 const char *name;
6802 int len;
6803 tree *next;
6805 const char *ptr;
6806 tree type_name = lookup_package_type (name, len);
6808 if (!type_name)
6809 return NULL;
6811 ptr = IDENTIFIER_POINTER (type_name);
6812 while (ptr && (ptr = strchr (ptr, '.')))
6814 *next = TREE_CHAIN (*next);
6815 ptr++;
6817 return type_name;
6820 static tree
6821 lookup_package_type (name, from)
6822 const char *name;
6823 int from;
6825 char subname [128];
6826 const char *sub = &name[from+1];
6827 while (*sub != '.' && *sub)
6828 sub++;
6829 strncpy (subname, name, sub-name);
6830 subname [sub-name] = '\0';
6831 return get_identifier (subname);
6834 static void
6835 check_inner_class_access (decl, enclosing_decl, cl)
6836 tree decl, enclosing_decl, cl;
6838 int access = 0;
6840 /* We don't issue an error message when CL is null. CL can be null
6841 as a result of processing a JDEP crafted by source_start_java_method
6842 for the purpose of patching its parm decl. But the error would
6843 have been already trapped when fixing the method's signature.
6844 DECL can also be NULL in case of earlier errors. */
6845 if (!decl || !cl)
6846 return;
6848 /* We grant access to private and protected inner classes if the
6849 location from where we're trying to access DECL is an enclosing
6850 context for DECL or if both have a common enclosing context. */
6851 if (CLASS_PRIVATE (decl))
6852 access = 1;
6853 if (CLASS_PROTECTED (decl))
6854 access = 2;
6855 if (!access)
6856 return;
6858 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6859 TREE_TYPE (decl))
6860 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6861 TREE_TYPE (decl)))
6862 return;
6864 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6865 (access == 1 ? "private" : "protected"),
6866 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6867 lang_printable_name (decl, 0));
6870 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6871 access violations were found, 1 otherwise. */
6873 static int
6874 check_pkg_class_access (class_name, cl)
6875 tree class_name;
6876 tree cl;
6878 tree type;
6880 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6881 return 0;
6883 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6884 return 0;
6886 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6888 /* Access to a private class within the same package is
6889 allowed. */
6890 tree l, r;
6891 breakdown_qualified (&l, &r, class_name);
6892 if (l == ctxp->package)
6893 return 0;
6895 parse_error_context
6896 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
6897 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6898 IDENTIFIER_POINTER (class_name));
6899 return 1;
6901 return 0;
6904 /* Local variable declaration. */
6906 static void
6907 declare_local_variables (modifier, type, vlist)
6908 int modifier;
6909 tree type;
6910 tree vlist;
6912 tree decl, current, saved_type;
6913 tree type_wfl = NULL_TREE;
6914 int must_chain = 0;
6915 int final_p = 0;
6917 /* Push a new block if statements were seen between the last time we
6918 pushed a block and now. Keep a cound of block to close */
6919 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
6921 tree body = GET_CURRENT_BLOCK (current_function_decl);
6922 tree b = enter_block ();
6923 BLOCK_EXPR_ORIGIN (b) = body;
6926 if (modifier)
6928 int i;
6929 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
6930 if (modifier == ACC_FINAL)
6931 final_p = 1;
6932 else
6934 parse_error_context
6935 (ctxp->modifier_ctx [i],
6936 "Only `final' is allowed as a local variables modifier");
6937 return;
6941 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6942 hold the TYPE value if a new incomplete has to be created (as
6943 opposed to being found already existing and reused). */
6944 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6946 /* If TYPE is fully resolved and we don't have a reference, make one */
6947 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6949 /* Go through all the declared variables */
6950 for (current = vlist, saved_type = type; current;
6951 current = TREE_CHAIN (current), type = saved_type)
6953 tree other, real_type;
6954 tree wfl = TREE_PURPOSE (current);
6955 tree name = EXPR_WFL_NODE (wfl);
6956 tree init = TREE_VALUE (current);
6958 /* Process NAME, as it may specify extra dimension(s) for it */
6959 type = build_array_from_name (type, type_wfl, name, &name);
6961 /* Variable redefinition check */
6962 if ((other = lookup_name_in_blocks (name)))
6964 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6965 DECL_SOURCE_LINE (other));
6966 continue;
6969 /* Type adjustment. We may have just readjusted TYPE because
6970 the variable specified more dimensions. Make sure we have
6971 a reference if we can and don't have one already. */
6972 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6974 real_type = GET_REAL_TYPE (type);
6975 /* Never layout this decl. This will be done when its scope
6976 will be entered */
6977 decl = build_decl (VAR_DECL, name, real_type);
6978 LOCAL_FINAL (decl) = final_p;
6979 BLOCK_CHAIN_DECL (decl);
6981 /* If doing xreferencing, replace the line number with the WFL
6982 compound value */
6983 if (flag_emit_xref)
6984 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6986 /* Don't try to use an INIT statement when an error was found */
6987 if (init && java_error_count)
6988 init = NULL_TREE;
6990 /* Add the initialization function to the current function's code */
6991 if (init)
6993 /* Name might have been readjusted */
6994 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6995 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6996 java_method_add_stmt (current_function_decl,
6997 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6998 init));
7001 /* Setup dependency the type of the decl */
7002 if (must_chain)
7004 jdep *dep;
7005 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7006 dep = CLASSD_LAST (ctxp->classd_list);
7007 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7010 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7013 /* Called during parsing. Build decls from argument list. */
7015 static void
7016 source_start_java_method (fndecl)
7017 tree fndecl;
7019 tree tem;
7020 tree parm_decl;
7021 int i;
7023 if (!fndecl)
7024 return;
7026 current_function_decl = fndecl;
7028 /* New scope for the function */
7029 enter_block ();
7030 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7031 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7033 tree type = TREE_VALUE (tem);
7034 tree name = TREE_PURPOSE (tem);
7036 /* If type is incomplete. Create an incomplete decl and ask for
7037 the decl to be patched later */
7038 if (INCOMPLETE_TYPE_P (type))
7040 jdep *jdep;
7041 tree real_type = GET_REAL_TYPE (type);
7042 parm_decl = build_decl (PARM_DECL, name, real_type);
7043 type = obtain_incomplete_type (type);
7044 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7045 jdep = CLASSD_LAST (ctxp->classd_list);
7046 JDEP_MISC (jdep) = name;
7047 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7049 else
7050 parm_decl = build_decl (PARM_DECL, name, type);
7052 /* Remember if a local variable was declared final (via its
7053 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7054 if (ARG_FINAL_P (tem))
7055 LOCAL_FINAL (parm_decl) = 1;
7057 BLOCK_CHAIN_DECL (parm_decl);
7059 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7060 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7061 nreverse (tem);
7062 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7063 DECL_MAX_LOCALS (current_function_decl) = i;
7066 /* Called during parsing. Creates an artificial method declaration. */
7068 static tree
7069 create_artificial_method (class, flags, type, name, args)
7070 tree class;
7071 int flags;
7072 tree type, name, args;
7074 tree mdecl;
7076 java_parser_context_save_global ();
7077 lineno = 0;
7078 mdecl = make_node (FUNCTION_TYPE);
7079 TREE_TYPE (mdecl) = type;
7080 TYPE_ARG_TYPES (mdecl) = args;
7081 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7082 java_parser_context_restore_global ();
7083 DECL_ARTIFICIAL (mdecl) = 1;
7084 return mdecl;
7087 /* Starts the body if an artifical method. */
7089 static void
7090 start_artificial_method_body (mdecl)
7091 tree mdecl;
7093 DECL_SOURCE_LINE (mdecl) = 1;
7094 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7095 source_start_java_method (mdecl);
7096 enter_block ();
7099 static void
7100 end_artificial_method_body (mdecl)
7101 tree mdecl;
7103 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
7104 exit_block ();
7107 /* Called during expansion. Push decls formerly built from argument
7108 list so they're usable during expansion. */
7110 static void
7111 expand_start_java_method (fndecl)
7112 tree fndecl;
7114 tree tem, *ptr;
7116 current_function_decl = fndecl;
7118 if (! quiet_flag)
7119 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
7120 announce_function (fndecl);
7121 if (! quiet_flag)
7122 fprintf (stderr, "]");
7124 pushlevel (1); /* Prepare for a parameter push */
7125 ptr = &DECL_ARGUMENTS (fndecl);
7126 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7127 while (tem)
7129 tree next = TREE_CHAIN (tem);
7130 tree type = TREE_TYPE (tem);
7131 if (PROMOTE_PROTOTYPES
7132 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7133 && INTEGRAL_TYPE_P (type))
7134 type = integer_type_node;
7135 DECL_ARG_TYPE (tem) = type;
7136 layout_decl (tem, 0);
7137 pushdecl (tem);
7138 *ptr = tem;
7139 ptr = &TREE_CHAIN (tem);
7140 tem = next;
7142 *ptr = NULL_TREE;
7143 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7144 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
7147 /* Terminate a function and expand its body. */
7149 static void
7150 source_end_java_method ()
7152 tree fndecl = current_function_decl;
7153 int flag_asynchronous_exceptions = asynchronous_exceptions;
7155 if (!fndecl)
7156 return;
7158 java_parser_context_save_global ();
7159 lineno = ctxp->last_ccb_indent1;
7161 /* Set EH language codes */
7162 java_set_exception_lang_code ();
7164 /* Turn function bodies with only a NOP expr null, so they don't get
7165 generated at all and we won't get warnings when using the -W
7166 -Wall flags. */
7167 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7168 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7170 /* Generate function's code */
7171 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7172 && ! flag_emit_class_files
7173 && ! flag_emit_xref)
7174 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7176 /* pop out of its parameters */
7177 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7178 poplevel (1, 0, 1);
7179 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7181 /* Generate rtl for function exit. */
7182 if (! flag_emit_class_files && ! flag_emit_xref)
7184 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7185 /* Emit catch-finally clauses */
7186 emit_handlers ();
7187 expand_function_end (input_filename, lineno, 0);
7189 /* FIXME: If the current method contains any exception handlers,
7190 force asynchronous_exceptions: this is necessary because signal
7191 handlers in libjava may throw exceptions. This is far from being
7192 a perfect solution, but it's better than doing nothing at all.*/
7193 if (catch_clauses)
7194 asynchronous_exceptions = 1;
7196 /* Run the optimizers and output assembler code for this function. */
7197 rest_of_compilation (fndecl);
7200 current_function_decl = NULL_TREE;
7201 permanent_allocation (1);
7202 java_parser_context_restore_global ();
7203 asynchronous_exceptions = flag_asynchronous_exceptions;
7206 /* Record EXPR in the current function block. Complements compound
7207 expression second operand if necessary. */
7209 tree
7210 java_method_add_stmt (fndecl, expr)
7211 tree fndecl, expr;
7213 if (!GET_CURRENT_BLOCK (fndecl))
7214 return NULL_TREE;
7215 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7218 static tree
7219 add_stmt_to_block (b, type, stmt)
7220 tree b, type, stmt;
7222 tree body = BLOCK_EXPR_BODY (b), c;
7224 if (java_error_count)
7225 return body;
7227 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7228 return body;
7230 BLOCK_EXPR_BODY (b) = c;
7231 TREE_SIDE_EFFECTS (c) = 1;
7232 return c;
7235 /* Add STMT to EXISTING if possible, otherwise create a new
7236 COMPOUND_EXPR and add STMT to it. */
7238 static tree
7239 add_stmt_to_compound (existing, type, stmt)
7240 tree existing, type, stmt;
7242 if (existing)
7243 return build (COMPOUND_EXPR, type, existing, stmt);
7244 else
7245 return stmt;
7248 /* Hold THIS for the scope of the current public method decl. */
7249 static tree current_this;
7251 void java_layout_seen_class_methods ()
7253 tree previous_list = all_class_list;
7254 tree end = NULL_TREE;
7255 tree current;
7257 while (1)
7259 for (current = previous_list;
7260 current != end; current = TREE_CHAIN (current))
7261 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7263 if (previous_list != all_class_list)
7265 end = previous_list;
7266 previous_list = all_class_list;
7268 else
7269 break;
7273 void
7274 java_reorder_fields ()
7276 static tree stop_reordering = NULL_TREE;
7278 tree current;
7279 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7281 current_class = TREE_TYPE (TREE_VALUE (current));
7283 if (current_class == stop_reordering)
7284 break;
7286 /* Reverse the fields, but leave the dummy field in front.
7287 Fields are already ordered for Object and Class */
7288 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7289 && current_class != class_type_node)
7291 /* If the dummy field is there, reverse the right fields and
7292 just layout the type for proper fields offset */
7293 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7295 tree fields = TYPE_FIELDS (current_class);
7296 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7297 TYPE_SIZE (current_class) = NULL_TREE;
7299 /* We don't have a dummy field, we need to layout the class,
7300 after having reversed the fields */
7301 else
7303 TYPE_FIELDS (current_class) =
7304 nreverse (TYPE_FIELDS (current_class));
7305 TYPE_SIZE (current_class) = NULL_TREE;
7309 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7312 /* Layout the methods of all classes loaded in one way on an
7313 other. Check methods of source parsed classes. Then reorder the
7314 fields and layout the classes or the type of all source parsed
7315 classes */
7317 void
7318 java_layout_classes ()
7320 tree current;
7321 int save_error_count = java_error_count;
7323 /* Layout the methods of all classes seen so far */
7324 java_layout_seen_class_methods ();
7325 java_parse_abort_on_error ();
7326 all_class_list = NULL_TREE;
7328 /* Then check the methods of all parsed classes */
7329 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7330 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7331 java_check_methods (TREE_VALUE (current));
7332 java_parse_abort_on_error ();
7334 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7336 current_class = TREE_TYPE (TREE_VALUE (current));
7337 layout_class (current_class);
7339 /* From now on, the class is considered completely loaded */
7340 CLASS_LOADED_P (current_class) = 1;
7342 /* Error reported by the caller */
7343 if (java_error_count)
7344 return;
7347 /* We might have reloaded classes durign the process of laying out
7348 classes for code generation. We must layout the methods of those
7349 late additions, as constructor checks might use them */
7350 java_layout_seen_class_methods ();
7351 java_parse_abort_on_error ();
7354 /* Expand methods in the current set of classes rememebered for
7355 generation. */
7357 static void
7358 java_complete_expand_classes ()
7360 tree current;
7362 do_not_fold = flag_emit_xref;
7364 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7365 if (!INNER_CLASS_DECL_P (current))
7366 java_complete_expand_class (current);
7369 /* Expand the methods found in OUTER, starting first by OUTER's inner
7370 classes, if any. */
7372 static void
7373 java_complete_expand_class (outer)
7374 tree outer;
7376 tree inner_list;
7378 set_nested_class_simple_name_value (outer, 1); /* Set */
7380 /* We need to go after all inner classes and start expanding them,
7381 starting with most nested ones. We have to do that because nested
7382 classes might add functions to outer classes */
7384 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7385 inner_list; inner_list = TREE_CHAIN (inner_list))
7386 java_complete_expand_class (TREE_PURPOSE (inner_list));
7388 java_complete_expand_methods (outer);
7389 set_nested_class_simple_name_value (outer, 0); /* Reset */
7392 /* Expand methods registered in CLASS_DECL. The general idea is that
7393 we expand regular methods first. This allows us get an estimate on
7394 how outer context local alias fields are really used so we can add
7395 to the constructor just enough code to initialize them properly (it
7396 also lets us generate finit$ correctly.) Then we expand the
7397 constructors and then <clinit>. */
7399 static void
7400 java_complete_expand_methods (class_decl)
7401 tree class_decl;
7403 tree clinit, finit, decl, first_decl;
7405 current_class = TREE_TYPE (class_decl);
7407 /* Initialize a new constant pool */
7408 init_outgoing_cpool ();
7410 /* Pre-expand <clinit> to figure whether we really need it or
7411 not. If we do need it, we pre-expand the static fields so they're
7412 ready to be used somewhere else. <clinit> will be fully expanded
7413 after we processed the constructors. */
7414 first_decl = TYPE_METHODS (current_class);
7415 clinit = maybe_generate_pre_expand_clinit (current_class);
7417 /* Then generate finit$ (if we need to) because constructor will
7418 try to use it.*/
7419 if (TYPE_FINIT_STMT_LIST (current_class))
7421 finit = generate_finit (current_class);
7422 java_complete_expand_method (finit);
7425 /* Now do the constructors */
7426 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7428 int no_body;
7430 if (!DECL_CONSTRUCTOR_P (decl))
7431 continue;
7433 no_body = !DECL_FUNCTION_BODY (decl);
7434 /* Don't generate debug info on line zero when expanding a
7435 generated constructor. */
7436 if (no_body)
7437 restore_line_number_status (1);
7439 java_complete_expand_method (decl);
7441 if (no_body)
7442 restore_line_number_status (0);
7445 /* First, do the ordinary methods. */
7446 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7448 /* Skip abstract or native methods -- but do handle native
7449 methods when generating JNI stubs. */
7450 if (METHOD_ABSTRACT (decl)
7451 || (! flag_jni && METHOD_NATIVE (decl))
7452 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7453 continue;
7455 if (METHOD_NATIVE (decl))
7457 tree body = build_jni_stub (decl);
7458 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7461 java_complete_expand_method (decl);
7464 /* If there is indeed a <clinit>, fully expand it now */
7465 if (clinit)
7467 /* Prevent the use of `this' inside <clinit> */
7468 ctxp->explicit_constructor_p = 1;
7469 java_complete_expand_method (clinit);
7470 ctxp->explicit_constructor_p = 0;
7473 /* We might have generated a class$ that we now want to expand */
7474 if (TYPE_DOT_CLASS (current_class))
7475 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7477 /* Now verify constructor circularity (stop after the first one we
7478 prove wrong.) */
7479 if (!CLASS_INTERFACE (class_decl))
7480 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7481 if (DECL_CONSTRUCTOR_P (decl)
7482 && verify_constructor_circularity (decl, decl))
7483 break;
7485 /* Save the constant pool. We'll need to restore it later. */
7486 TYPE_CPOOL (current_class) = outgoing_cpool;
7489 /* Hold a list of catch clauses list. The first element of this list is
7490 the list of the catch clauses of the currently analysed try block. */
7491 static tree currently_caught_type_list;
7493 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7494 safely used in some other methods/constructors. */
7496 static tree
7497 maybe_generate_pre_expand_clinit (class_type)
7498 tree class_type;
7500 tree current, mdecl;
7502 if (!TYPE_CLINIT_STMT_LIST (class_type))
7503 return NULL_TREE;
7505 /* Go through all static fields and pre expand them */
7506 for (current = TYPE_FIELDS (class_type); current;
7507 current = TREE_CHAIN (current))
7508 if (FIELD_STATIC (current))
7509 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7511 /* Then build the <clinit> method */
7512 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7513 clinit_identifier_node, end_params_node);
7514 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7515 mdecl, NULL_TREE);
7516 start_artificial_method_body (mdecl);
7518 /* We process the list of assignment we produced as the result of
7519 the declaration of initialized static field and add them as
7520 statement to the <clinit> method. */
7521 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7522 current = TREE_CHAIN (current))
7524 tree stmt = current;
7525 /* We build the assignment expression that will initialize the
7526 field to its value. There are strict rules on static
7527 initializers (8.5). FIXME */
7528 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7529 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7530 java_method_add_stmt (mdecl, stmt);
7533 end_artificial_method_body (mdecl);
7535 /* Now we want to place <clinit> as the last method (because we need
7536 it at least for interface so that it doesn't interfere with the
7537 dispatch table based lookup. */
7538 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7540 current = TREE_CHAIN (TYPE_METHODS (class_type));
7541 TYPE_METHODS (class_type) = current;
7543 while (TREE_CHAIN (current))
7544 current = TREE_CHAIN (current);
7546 TREE_CHAIN (current) = mdecl;
7547 TREE_CHAIN (mdecl) = NULL_TREE;
7550 return mdecl;
7553 /* See whether we could get rid of <clinit>. Criteria are: all static
7554 final fields have constant initial values and the body of <clinit>
7555 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7557 static int
7558 maybe_yank_clinit (mdecl)
7559 tree mdecl;
7561 tree type, current;
7562 tree fbody, bbody;
7564 if (!DECL_CLINIT_P (mdecl))
7565 return 0;
7567 /* If the body isn't empty, then we keep <clinit>. Note that if
7568 we're emitting classfiles, this isn't enough not to rule it
7569 out. */
7570 fbody = DECL_FUNCTION_BODY (mdecl);
7571 if ((bbody = BLOCK_EXPR_BODY (fbody)))
7572 bbody = BLOCK_EXPR_BODY (bbody);
7573 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7574 return 0;
7576 type = DECL_CONTEXT (mdecl);
7577 current = TYPE_FIELDS (type);
7579 for (current = (current ? TREE_CHAIN (current) : current);
7580 current; current = TREE_CHAIN (current))
7582 tree f_init;
7584 /* We're not interested in non static field */
7585 if (!FIELD_STATIC (current))
7586 continue;
7588 /* Anything that isn't String or a basic type is ruled out -- or
7589 if we now how to deal with it (when doing things natively) we
7590 should generated an empty <clinit> so that SUID are computed
7591 correctly. */
7592 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7593 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7594 break;
7596 f_init = DECL_INITIAL (current);
7597 /* If we're emitting native code, we want static final fields to
7598 have constant initializers. If we don't meet these
7599 conditions, we keep <clinit> */
7600 if (!flag_emit_class_files
7601 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7602 break;
7603 /* If we're emitting bytecode, we want static fields to have
7604 constant initializers or no initializer. If we don't meet
7605 these conditions, we keep <clinit> */
7606 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7607 break;
7610 if (current)
7611 return 0;
7613 /* Get rid of <clinit> in the class' list of methods */
7614 if (TYPE_METHODS (type) == mdecl)
7615 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7616 else
7617 for (current = TYPE_METHODS (type); current;
7618 current = TREE_CHAIN (current))
7619 if (TREE_CHAIN (current) == mdecl)
7621 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7622 break;
7625 return 1;
7629 /* Complete and expand a method. */
7631 static void
7632 java_complete_expand_method (mdecl)
7633 tree mdecl;
7635 int yank_clinit = 0;
7637 current_function_decl = mdecl;
7638 /* Fix constructors before expanding them */
7639 if (DECL_CONSTRUCTOR_P (mdecl))
7640 fix_constructors (mdecl);
7642 /* Expand functions that have a body */
7643 if (DECL_FUNCTION_BODY (mdecl))
7645 tree fbody = DECL_FUNCTION_BODY (mdecl);
7646 tree block_body = BLOCK_EXPR_BODY (fbody);
7647 tree exception_copy = NULL_TREE;
7648 expand_start_java_method (mdecl);
7649 build_result_decl (mdecl);
7651 current_this
7652 = (!METHOD_STATIC (mdecl) ?
7653 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7655 /* Purge the `throws' list of unchecked exceptions. If we're
7656 doing xref, save a copy of the list and re-install it
7657 later. */
7658 if (flag_emit_xref)
7659 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7661 purge_unchecked_exceptions (mdecl);
7663 /* Install exceptions thrown with `throws' */
7664 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7666 if (block_body != NULL_TREE)
7668 block_body = java_complete_tree (block_body);
7670 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7671 check_for_initialization (block_body);
7672 ctxp->explicit_constructor_p = 0;
7675 BLOCK_EXPR_BODY (fbody) = block_body;
7677 /* If we saw a return but couldn't evaluate it properly, we'll
7678 have an error_mark_node here. */
7679 if (block_body != error_mark_node
7680 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7681 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7682 && !flag_emit_xref)
7683 missing_return_error (current_function_decl);
7685 /* Check wether we could just get rid of clinit, now the picture
7686 is complete. */
7687 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7688 complete_start_java_method (mdecl);
7690 /* Don't go any further if we've found error(s) during the
7691 expansion */
7692 if (!java_error_count && !yank_clinit)
7693 source_end_java_method ();
7694 else
7696 if (java_error_count)
7697 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7698 poplevel (1, 0, 1);
7701 /* Pop the exceptions and sanity check */
7702 POP_EXCEPTIONS();
7703 if (currently_caught_type_list)
7704 fatal ("Exception list non empty - java_complete_expand_method");
7706 if (flag_emit_xref)
7707 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7713 /* This section of the code deals with accessing enclosing context
7714 fields either directly by using the relevant access to this$<n> or
7715 by invoking an access method crafted for that purpose. */
7717 /* Build the necessary access from an inner class to an outer
7718 class. This routine could be optimized to cache previous result
7719 (decl, current_class and returned access). When an access method
7720 needs to be generated, it always takes the form of a read. It might
7721 be later turned into a write by calling outer_field_access_fix. */
7723 static tree
7724 build_outer_field_access (id, decl)
7725 tree id, decl;
7727 tree access = NULL_TREE;
7728 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7730 /* If decl's class is the direct outer class of the current_class,
7731 build the access as `this$<n>.<field>'. Note that we will break
7732 the `private' barrier if we're not emitting bytecodes. */
7733 if (ctx == DECL_CONTEXT (decl)
7734 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7736 tree thisn = build_current_thisn (current_class);
7737 access = make_qualified_primary (build_wfl_node (thisn),
7738 id, EXPR_WFL_LINECOL (id));
7740 /* Otherwise, generate access methods to outer this and access the
7741 field (either using an access method or by direct access.) */
7742 else
7744 int lc = EXPR_WFL_LINECOL (id);
7746 /* Now we chain the required number of calls to the access$0 to
7747 get a hold to the enclosing instance we need, and then we
7748 build the field access. */
7749 access = build_access_to_thisn (ctx, DECL_CONTEXT (decl), lc);
7751 /* If the field is private and we're generating bytecode, then
7752 we generate an access method */
7753 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7755 tree name = build_outer_field_access_methods (decl);
7756 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7757 name, access, NULL_TREE);
7759 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7760 Once again we break the `private' access rule from a foreign
7761 class. */
7762 else
7763 access = make_qualified_primary (access, id, lc);
7765 return resolve_expression_name (access, NULL);
7768 /* Return a non zero value if NODE describes an outer field inner
7769 access. */
7771 static int
7772 outer_field_access_p (type, decl)
7773 tree type, decl;
7775 if (!INNER_CLASS_TYPE_P (type)
7776 || TREE_CODE (decl) != FIELD_DECL
7777 || DECL_CONTEXT (decl) == type)
7778 return 0;
7780 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7781 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7783 if (type == DECL_CONTEXT (decl))
7784 return 1;
7785 if (!DECL_CONTEXT (TYPE_NAME (type)))
7786 break;
7789 return 0;
7792 /* Return a non zero value if NODE represents an outer field inner
7793 access that was been already expanded. As a side effect, it returns
7794 the name of the field being accessed and the argument passed to the
7795 access function, suitable for a regeneration of the access method
7796 call if necessary. */
7798 static int
7799 outer_field_expanded_access_p (node, name, arg_type, arg)
7800 tree node, *name, *arg_type, *arg;
7802 int identified = 0;
7804 if (TREE_CODE (node) != CALL_EXPR)
7805 return 0;
7807 /* Well, gcj generates slightly different tree nodes when compiling
7808 to native or bytecodes. It's the case for function calls. */
7810 if (flag_emit_class_files
7811 && TREE_CODE (node) == CALL_EXPR
7812 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7813 identified = 1;
7814 else if (!flag_emit_class_files)
7816 node = TREE_OPERAND (node, 0);
7818 if (node && TREE_OPERAND (node, 0)
7819 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7821 node = TREE_OPERAND (node, 0);
7822 if (TREE_OPERAND (node, 0)
7823 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7824 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7825 (DECL_NAME (TREE_OPERAND (node, 0)))))
7826 identified = 1;
7830 if (identified && name && arg_type && arg)
7832 tree argument = TREE_OPERAND (node, 1);
7833 *name = DECL_NAME (TREE_OPERAND (node, 0));
7834 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7835 *arg = TREE_VALUE (argument);
7837 return identified;
7840 /* Detect in NODE an outer field read access from an inner class and
7841 transform it into a write with RHS as an argument. This function is
7842 called from the java_complete_lhs when an assignment to a LHS can
7843 be identified. */
7845 static tree
7846 outer_field_access_fix (wfl, node, rhs)
7847 tree wfl, node, rhs;
7849 tree name, arg_type, arg;
7851 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7853 /* At any rate, check whether we're trying to assign a value to
7854 a final. */
7855 tree accessed = (JDECL_P (node) ? node :
7856 (TREE_CODE (node) == COMPONENT_REF ?
7857 TREE_OPERAND (node, 1) : node));
7858 if (check_final_assignment (accessed, wfl))
7859 return error_mark_node;
7861 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7862 arg_type, name, arg, rhs);
7863 return java_complete_tree (node);
7865 return NULL_TREE;
7868 /* Construct the expression that calls an access method:
7869 <type>.access$<n>(<arg1> [, <arg2>]);
7871 ARG2 can be NULL and will be omitted in that case. It will denote a
7872 read access. */
7874 static tree
7875 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7876 int lc;
7877 tree type, access_method_name, arg1, arg2;
7879 tree args, cn, access;
7881 args = arg1 ? arg1 :
7882 build_wfl_node (build_current_thisn (current_class));
7883 args = build_tree_list (NULL_TREE, args);
7885 if (arg2)
7886 args = tree_cons (NULL_TREE, arg2, args);
7888 access = build_method_invocation (build_wfl_node (access_method_name), args);
7889 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7890 return make_qualified_primary (cn, access, lc);
7893 static tree
7894 build_new_access_id ()
7896 static int access_n_counter = 1;
7897 char buffer [128];
7899 sprintf (buffer, "access$%d", access_n_counter++);
7900 return get_identifier (buffer);
7903 /* Create the static access functions for the outer field DECL. We define a
7904 read:
7905 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7906 return inst$.field;
7908 and a write access:
7909 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7910 TREE_TYPE (<field>) value$) {
7911 return inst$.field = value$;
7913 We should have a usage flags on the DECL so we can lazily turn the ones
7914 we're using for code generation. FIXME.
7917 static tree
7918 build_outer_field_access_methods (decl)
7919 tree decl;
7921 tree id, args, stmt, mdecl;
7923 /* Check point, to be removed. FIXME */
7924 if (FIELD_INNER_ACCESS (decl)
7925 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
7926 abort ();
7928 if (FIELD_INNER_ACCESS (decl))
7929 return FIELD_INNER_ACCESS (decl);
7931 push_obstacks (&permanent_obstack, &permanent_obstack);
7933 /* Create the identifier and a function named after it. */
7934 id = build_new_access_id ();
7936 /* The identifier is marked as bearing the name of a generated write
7937 access function for outer field accessed from inner classes. */
7938 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7940 /* Create the read access */
7941 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7942 TREE_CHAIN (args) = end_params_node;
7943 stmt = make_qualified_primary (build_wfl_node (inst_id),
7944 build_wfl_node (DECL_NAME (decl)), 0);
7945 stmt = build_return (0, stmt);
7946 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7947 TREE_TYPE (decl), id, args, stmt);
7948 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7950 /* Create the write access method */
7951 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7952 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
7953 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
7954 stmt = make_qualified_primary (build_wfl_node (inst_id),
7955 build_wfl_node (DECL_NAME (decl)), 0);
7956 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
7957 build_wfl_node (wpv_id)));
7959 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7960 TREE_TYPE (decl), id, args, stmt);
7961 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7962 pop_obstacks ();
7964 /* Return the access name */
7965 return FIELD_INNER_ACCESS (decl) = id;
7968 /* Build an field access method NAME. */
7970 static tree
7971 build_outer_field_access_method (class, type, name, args, body)
7972 tree class, type, name, args, body;
7974 tree saved_current_function_decl, mdecl;
7976 /* Create the method */
7977 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
7978 fix_method_argument_names (args, mdecl);
7979 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7981 /* Attach the method body. */
7982 saved_current_function_decl = current_function_decl;
7983 start_artificial_method_body (mdecl);
7984 java_method_add_stmt (mdecl, body);
7985 end_artificial_method_body (mdecl);
7986 current_function_decl = saved_current_function_decl;
7988 return mdecl;
7992 /* This section deals with building access function necessary for
7993 certain kinds of method invocation from inner classes. */
7995 static tree
7996 build_outer_method_access_method (decl)
7997 tree decl;
7999 tree saved_current_function_decl, mdecl;
8000 tree args = NULL_TREE, call_args = NULL_TREE;
8001 tree carg, id, body, class;
8002 char buffer [80];
8003 int parm_id_count = 0;
8005 /* Test this abort with an access to a private field */
8006 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8007 abort ();
8009 /* Check the cache first */
8010 if (DECL_FUNCTION_INNER_ACCESS (decl))
8011 return DECL_FUNCTION_INNER_ACCESS (decl);
8013 class = DECL_CONTEXT (decl);
8015 /* Obtain an access identifier and mark it */
8016 id = build_new_access_id ();
8017 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8019 push_obstacks (&permanent_obstack, &permanent_obstack);
8021 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8022 /* Create the arguments, as much as the original */
8023 for (; carg && carg != end_params_node;
8024 carg = TREE_CHAIN (carg))
8026 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8027 args = chainon (args, build_tree_list (get_identifier (buffer),
8028 TREE_VALUE (carg)));
8030 args = chainon (args, end_params_node);
8032 /* Create the method */
8033 mdecl = create_artificial_method (class, ACC_STATIC,
8034 TREE_TYPE (TREE_TYPE (decl)), id, args);
8035 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8036 /* There is a potential bug here. We should be able to use
8037 fix_method_argument_names, but then arg names get mixed up and
8038 eventually a constructor will have its this$0 altered and the
8039 outer context won't be assignment properly. The test case is
8040 stub.java FIXME */
8041 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8043 /* Attach the method body. */
8044 saved_current_function_decl = current_function_decl;
8045 start_artificial_method_body (mdecl);
8047 /* The actual method invocation uses the same args. When invoking a
8048 static methods that way, we don't want to skip the first
8049 argument. */
8050 carg = args;
8051 if (!METHOD_STATIC (decl))
8052 carg = TREE_CHAIN (carg);
8053 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8054 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8055 call_args);
8057 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8058 call_args);
8059 if (!METHOD_STATIC (decl))
8060 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8061 body, 0);
8062 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8063 body = build_return (0, body);
8064 java_method_add_stmt (mdecl,body);
8065 end_artificial_method_body (mdecl);
8066 current_function_decl = saved_current_function_decl;
8067 pop_obstacks ();
8069 /* Back tag the access function so it know what it accesses */
8070 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8072 /* Tag the current method so it knows it has an access generated */
8073 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8077 /* This section of the code deals with building expressions to access
8078 the enclosing instance of an inner class. The enclosing instance is
8079 kept in a generated field called this$<n>, with <n> being the
8080 inner class nesting level (starting from 0.) */
8082 /* Build an access to a given this$<n>, possibly by chaining access
8083 call to others. Access methods to this$<n> are build on the fly if
8084 necessary */
8086 static tree
8087 build_access_to_thisn (from, to, lc)
8088 tree from, to;
8089 int lc;
8091 tree access = NULL_TREE;
8093 while (from != to)
8095 tree access0_wfl, cn;
8097 maybe_build_thisn_access_method (from);
8098 access0_wfl = build_wfl_node (access0_identifier_node);
8099 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8100 EXPR_WFL_LINECOL (access0_wfl) = lc;
8102 if (!access)
8104 access = build_current_thisn (current_class);
8105 access = build_wfl_node (access);
8107 access = build_tree_list (NULL_TREE, access);
8108 access = build_method_invocation (access0_wfl, access);
8109 access = make_qualified_primary (cn, access, lc);
8111 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
8113 return access;
8116 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8117 is returned if nothing needs to be generated. Otherwise, the method
8118 generated and a method decl is returned.
8120 NOTE: These generated methods should be declared in a class file
8121 attribute so that they can't be referred to directly. */
8123 static tree
8124 maybe_build_thisn_access_method (type)
8125 tree type;
8127 tree mdecl, args, stmt, rtype;
8128 tree saved_current_function_decl;
8130 /* If TYPE is a top-level class, no access method is required.
8131 If there already is such an access method, bail out. */
8132 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
8133 return NULL_TREE;
8135 /* We generate the method. The method looks like:
8136 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8138 push_obstacks (&permanent_obstack, &permanent_obstack);
8139 args = build_tree_list (inst_id, build_pointer_type (type));
8140 TREE_CHAIN (args) = end_params_node;
8141 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8142 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8143 access0_identifier_node, args);
8144 fix_method_argument_names (args, mdecl);
8145 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8146 stmt = build_current_thisn (type);
8147 stmt = make_qualified_primary (build_wfl_node (inst_id),
8148 build_wfl_node (stmt), 0);
8149 stmt = build_return (0, stmt);
8151 saved_current_function_decl = current_function_decl;
8152 start_artificial_method_body (mdecl);
8153 java_method_add_stmt (mdecl, stmt);
8154 end_artificial_method_body (mdecl);
8155 current_function_decl = saved_current_function_decl;
8156 pop_obstacks ();
8158 CLASS_ACCESS0_GENERATED_P (type) = 1;
8160 return mdecl;
8163 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8164 the first level of innerclassing. this$1 for the next one, etc...
8165 This function can be invoked with TYPE to NULL, available and then
8166 has to count the parser context. */
8168 static tree
8169 build_current_thisn (type)
8170 tree type;
8172 static int saved_i = -1;
8173 static tree saved_thisn = NULL_TREE;
8175 tree decl;
8176 char buffer [80];
8177 int i = 0;
8179 if (type)
8181 static tree saved_type = NULL_TREE;
8182 static int saved_type_i = 0;
8184 if (type == saved_type)
8185 i = saved_type_i;
8186 else
8188 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8189 decl; decl = DECL_CONTEXT (decl), i++)
8192 saved_type = type;
8193 saved_type_i = i;
8196 else
8197 i = list_length (GET_CPC_LIST ())-2;
8199 if (i == saved_i)
8200 return saved_thisn;
8202 sprintf (buffer, "this$%d", i);
8203 saved_i = i;
8204 saved_thisn = get_identifier (buffer);
8205 return saved_thisn;
8208 /* Return the assignement to the hidden enclosing context `this$<n>'
8209 by the second incoming parameter to the innerclass constructor. The
8210 form used is `this.this$<n> = this$<n>;'. */
8212 static tree
8213 build_thisn_assign ()
8215 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8217 tree thisn = build_current_thisn (current_class);
8218 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8219 build_wfl_node (thisn), 0);
8220 tree rhs = build_wfl_node (thisn);
8221 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8222 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8224 return NULL_TREE;
8228 /* Building the synthetic `class$' used to implement the `.class' 1.1
8229 extension for non primitive types. This method looks like:
8231 static Class class$(String type) throws NoClassDefFoundError
8233 try {return (java.lang.Class.forName (String));}
8234 catch (ClassNotFoundException e) {
8235 throw new NoClassDefFoundError(e.getMessage());}
8236 } */
8238 static tree
8239 build_dot_class_method (class)
8240 tree class;
8242 #define BWF(S) build_wfl_node (get_identifier ((S)))
8243 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8244 tree args, tmp, saved_current_function_decl, mdecl;
8245 tree stmt, throw_stmt, catch, catch_block, try_block;
8246 tree catch_clause_param;
8247 tree class_not_found_exception, no_class_def_found_error;
8249 static tree get_message_wfl, type_parm_wfl;
8251 if (!get_message_wfl)
8253 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8254 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8257 /* Build the arguments */
8258 args = build_tree_list (get_identifier ("type$"),
8259 build_pointer_type (string_type_node));
8260 TREE_CHAIN (args) = end_params_node;
8262 /* Build the qualified name java.lang.Class.forName */
8263 tmp = MQN (MQN (MQN (BWF ("java"),
8264 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8266 /* For things we have to catch and throw */
8267 class_not_found_exception =
8268 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8269 no_class_def_found_error =
8270 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8271 load_class (class_not_found_exception, 1);
8272 load_class (no_class_def_found_error, 1);
8274 /* Create the "class$" function */
8275 mdecl = create_artificial_method (class, ACC_STATIC,
8276 build_pointer_type (class_type_node),
8277 get_identifier ("class$"), args);
8278 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8279 no_class_def_found_error);
8281 /* We start by building the try block. We need to build:
8282 return (java.lang.Class.forName (type)); */
8283 stmt = build_method_invocation (tmp,
8284 build_tree_list (NULL_TREE, type_parm_wfl));
8285 stmt = build_return (0, stmt);
8286 /* Put it in a block. That's the try block */
8287 try_block = build_expr_block (stmt, NULL_TREE);
8289 /* Now onto the catch block. We start by building the expression
8290 throwing a new exception:
8291 throw new NoClassDefFoundError (_.getMessage); */
8292 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8293 get_message_wfl, 0);
8294 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8296 /* Build new NoClassDefFoundError (_.getMessage) */
8297 throw_stmt = build_new_invocation
8298 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8299 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8301 /* Build the throw, (it's too early to use BUILD_THROW) */
8302 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8304 /* Build the catch block to encapsulate all this. We begin by
8305 building an decl for the catch clause parameter and link it to
8306 newly created block, the catch block. */
8307 catch_clause_param =
8308 build_decl (VAR_DECL, wpv_id,
8309 build_pointer_type (class_not_found_exception));
8310 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8312 /* We initialize the variable with the exception handler. */
8313 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8314 soft_exceptioninfo_call_node);
8315 add_stmt_to_block (catch_block, NULL_TREE, catch);
8317 /* We add the statement throwing the new exception */
8318 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8320 /* Build a catch expression for all this */
8321 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8323 /* Build the try/catch sequence */
8324 stmt = build_try_statement (0, try_block, catch_block);
8326 fix_method_argument_names (args, mdecl);
8327 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8328 saved_current_function_decl = current_function_decl;
8329 start_artificial_method_body (mdecl);
8330 java_method_add_stmt (mdecl, stmt);
8331 end_artificial_method_body (mdecl);
8332 current_function_decl = saved_current_function_decl;
8333 TYPE_DOT_CLASS (class) = mdecl;
8335 return mdecl;
8338 static tree
8339 build_dot_class_method_invocation (type)
8340 tree type;
8342 tree sig_id, s;
8344 if (TYPE_ARRAY_P (type))
8345 sig_id = build_java_signature (type);
8346 else
8347 sig_id = DECL_NAME (TYPE_NAME (type));
8349 s = make_node (STRING_CST);
8350 TREE_STRING_LENGTH (s) = IDENTIFIER_LENGTH (sig_id);
8351 TREE_STRING_POINTER (s) = obstack_alloc (expression_obstack,
8352 TREE_STRING_LENGTH (s)+1);
8353 strcpy (TREE_STRING_POINTER (s), IDENTIFIER_POINTER (sig_id));
8354 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8355 build_tree_list (NULL_TREE, s));
8358 /* This section of the code deals with constructor. */
8360 /* Craft a body for default constructor. Patch existing constructor
8361 bodies with call to super() and field initialization statements if
8362 necessary. */
8364 static void
8365 fix_constructors (mdecl)
8366 tree mdecl;
8368 tree body = DECL_FUNCTION_BODY (mdecl);
8369 tree thisn_assign, compound = NULL_TREE;
8370 tree class_type = DECL_CONTEXT (mdecl);
8372 if (!body)
8374 /* It is an error for the compiler to generate a default
8375 constructor if the superclass doesn't have a constructor that
8376 takes no argument, or the same args for an anonymous class */
8377 if (verify_constructor_super (mdecl))
8379 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8380 tree save = DECL_NAME (mdecl);
8381 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8382 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8383 parse_error_context
8384 (lookup_cl (TYPE_NAME (class_type)),
8385 "No constructor matching `%s' found in class `%s'",
8386 lang_printable_name (mdecl, 0), n);
8387 DECL_NAME (mdecl) = save;
8390 /* The constructor body must be crafted by hand. It's the
8391 constructor we defined when we realize we didn't have the
8392 CLASSNAME() constructor */
8393 start_artificial_method_body (mdecl);
8395 /* Insert an assignment to the this$<n> hidden field, if
8396 necessary */
8397 if ((thisn_assign = build_thisn_assign ()))
8398 java_method_add_stmt (mdecl, thisn_assign);
8400 /* We don't generate a super constructor invocation if we're
8401 compiling java.lang.Object. build_super_invocation takes care
8402 of that. */
8403 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8405 /* Insert the instance initializer block right here, after the
8406 super invocation. */
8407 add_instance_initializer (mdecl);
8409 end_artificial_method_body (mdecl);
8411 /* Search for an explicit constructor invocation */
8412 else
8414 int found = 0;
8415 tree main_block = BLOCK_EXPR_BODY (body);
8417 while (body)
8418 switch (TREE_CODE (body))
8420 case CALL_EXPR:
8421 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8422 body = NULL_TREE;
8423 break;
8424 case COMPOUND_EXPR:
8425 case EXPR_WITH_FILE_LOCATION:
8426 body = TREE_OPERAND (body, 0);
8427 break;
8428 case BLOCK:
8429 body = BLOCK_EXPR_BODY (body);
8430 break;
8431 default:
8432 found = 0;
8433 body = NULL_TREE;
8435 /* The constructor is missing an invocation of super() */
8436 if (!found)
8437 compound = add_stmt_to_compound (compound, NULL_TREE,
8438 build_super_invocation (mdecl));
8440 /* Generate the assignment to this$<n>, if necessary */
8441 if ((thisn_assign = build_thisn_assign ()))
8442 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8444 /* Insert the instance initializer block right here, after the
8445 super invocation. */
8446 add_instance_initializer (mdecl);
8448 /* Fix the constructor main block if we're adding extra stmts */
8449 if (compound)
8451 compound = add_stmt_to_compound (compound, NULL_TREE,
8452 BLOCK_EXPR_BODY (main_block));
8453 BLOCK_EXPR_BODY (main_block) = compound;
8458 /* Browse constructors in the super class, searching for a constructor
8459 that doesn't take any argument. Return 0 if one is found, 1
8460 otherwise. If the current class is an anonymous inner class, look
8461 for something that has the same signature. */
8463 static int
8464 verify_constructor_super (mdecl)
8465 tree mdecl;
8467 tree class = CLASSTYPE_SUPER (current_class);
8468 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8469 tree sdecl;
8471 if (!class)
8472 return 0;
8474 if (ANONYMOUS_CLASS_P (current_class))
8476 tree mdecl_arg_type;
8477 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8478 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8479 if (DECL_CONSTRUCTOR_P (sdecl))
8481 tree m_arg_type;
8482 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8483 if (super_inner)
8484 arg_type = TREE_CHAIN (arg_type);
8485 for (m_arg_type = mdecl_arg_type;
8486 (arg_type != end_params_node
8487 && m_arg_type != end_params_node);
8488 arg_type = TREE_CHAIN (arg_type),
8489 m_arg_type = TREE_CHAIN (m_arg_type))
8490 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
8491 break;
8493 if (arg_type == end_params_node && m_arg_type == end_params_node)
8494 return 0;
8497 else
8499 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8501 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8502 if (super_inner)
8503 arg = TREE_CHAIN (arg);
8504 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8505 return 0;
8508 return 1;
8511 /* Generate code for all context remembered for code generation. */
8513 void
8514 java_expand_classes ()
8516 int save_error_count = 0;
8517 static struct parser_ctxt *saved_ctxp = NULL;
8519 java_parse_abort_on_error ();
8520 if (!(ctxp = ctxp_for_generation))
8521 return;
8522 java_layout_classes ();
8523 java_parse_abort_on_error ();
8525 saved_ctxp = ctxp_for_generation;
8526 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8528 ctxp = ctxp_for_generation;
8529 lang_init_source (2); /* Error msgs have method prototypes */
8530 java_complete_expand_classes (); /* Complete and expand classes */
8531 java_parse_abort_on_error ();
8534 /* Find anonymous classes and expand their constructor, now they
8535 have been fixed. */
8536 for (ctxp_for_generation = saved_ctxp;
8537 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8539 tree current;
8540 ctxp = ctxp_for_generation;
8541 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8543 current_class = TREE_TYPE (current);
8544 if (ANONYMOUS_CLASS_P (current_class))
8546 tree d;
8547 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8549 if (DECL_CONSTRUCTOR_P (d))
8551 restore_line_number_status (1);
8552 reset_method_name (d);
8553 java_complete_expand_method (d);
8554 restore_line_number_status (0);
8555 break; /* We now there are no other ones */
8562 /* If we've found error at that stage, don't try to generate
8563 anything, unless we're emitting xrefs or checking the syntax only
8564 (but not using -fsyntax-only for the purpose of generating
8565 bytecode. */
8566 if (java_error_count && !flag_emit_xref
8567 && (!flag_syntax_only && !flag_emit_class_files))
8568 return;
8570 /* Now things are stable, go for generation of the class data. */
8571 for (ctxp_for_generation = saved_ctxp;
8572 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8574 tree current;
8575 ctxp = ctxp_for_generation;
8576 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8578 current_class = TREE_TYPE (current);
8579 outgoing_cpool = TYPE_CPOOL (current_class);
8580 if (flag_emit_class_files)
8581 write_classfile (current_class);
8582 if (flag_emit_xref)
8583 expand_xref (current_class);
8584 else if (! flag_syntax_only)
8585 finish_class ();
8590 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8591 a tree list node containing RIGHT. Fore coming RIGHTs will be
8592 chained to this hook. LOCATION contains the location of the
8593 separating `.' operator. */
8595 static tree
8596 make_qualified_primary (primary, right, location)
8597 tree primary, right;
8598 int location;
8600 tree wfl;
8602 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8603 wfl = build_wfl_wrap (primary, location);
8604 else
8606 wfl = primary;
8607 /* If wfl wasn't qualified, we build a first anchor */
8608 if (!EXPR_WFL_QUALIFICATION (wfl))
8609 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8612 /* And chain them */
8613 EXPR_WFL_LINECOL (right) = location;
8614 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8615 PRIMARY_P (wfl) = 1;
8616 return wfl;
8619 /* Simple merge of two name separated by a `.' */
8621 static tree
8622 merge_qualified_name (left, right)
8623 tree left, right;
8625 tree node;
8626 if (!left && !right)
8627 return NULL_TREE;
8629 if (!left)
8630 return right;
8632 if (!right)
8633 return left;
8635 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8636 IDENTIFIER_LENGTH (left));
8637 obstack_1grow (&temporary_obstack, '.');
8638 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8639 IDENTIFIER_LENGTH (right));
8640 node = get_identifier (obstack_base (&temporary_obstack));
8641 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8642 QUALIFIED_P (node) = 1;
8643 return node;
8646 /* Merge the two parts of a qualified name into LEFT. Set the
8647 location information of the resulting node to LOCATION, usually
8648 inherited from the location information of the `.' operator. */
8650 static tree
8651 make_qualified_name (left, right, location)
8652 tree left, right;
8653 int location;
8655 #ifdef USE_COMPONENT_REF
8656 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8657 EXPR_WFL_LINECOL (node) = location;
8658 return node;
8659 #else
8660 tree left_id = EXPR_WFL_NODE (left);
8661 tree right_id = EXPR_WFL_NODE (right);
8662 tree wfl, merge;
8664 merge = merge_qualified_name (left_id, right_id);
8666 /* Left wasn't qualified and is now qualified */
8667 if (!QUALIFIED_P (left_id))
8669 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8670 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8671 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8674 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8675 EXPR_WFL_LINECOL (wfl) = location;
8676 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8678 EXPR_WFL_NODE (left) = merge;
8679 return left;
8680 #endif
8683 /* Extract the last identifier component of the qualified in WFL. The
8684 last identifier is removed from the linked list */
8686 static tree
8687 cut_identifier_in_qualified (wfl)
8688 tree wfl;
8690 tree q;
8691 tree previous = NULL_TREE;
8692 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8693 if (!TREE_CHAIN (q))
8695 if (!previous)
8696 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
8697 TREE_CHAIN (previous) = NULL_TREE;
8698 return TREE_PURPOSE (q);
8702 /* Resolve the expression name NAME. Return its decl. */
8704 static tree
8705 resolve_expression_name (id, orig)
8706 tree id;
8707 tree *orig;
8709 tree name = EXPR_WFL_NODE (id);
8710 tree decl;
8712 /* 6.5.5.1: Simple expression names */
8713 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8715 /* 15.13.1: NAME can appear within the scope of a local variable
8716 declaration */
8717 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8718 return decl;
8720 /* 15.13.1: NAME can appear within a class declaration */
8721 else
8723 decl = lookup_field_wrapper (current_class, name);
8724 if (decl)
8726 tree access = NULL_TREE;
8727 int fs = FIELD_STATIC (decl);
8729 /* If we're accessing an outer scope local alias, make
8730 sure we change the name of the field we're going to
8731 build access to. */
8732 if (FIELD_LOCAL_ALIAS_USED (decl))
8733 name = DECL_NAME (decl);
8735 /* Instance variable (8.3.1.1) can't appear within
8736 static method, static initializer or initializer for
8737 a static variable. */
8738 if (!fs && METHOD_STATIC (current_function_decl))
8740 static_ref_err (id, name, current_class);
8741 return error_mark_node;
8743 /* Instance variables can't appear as an argument of
8744 an explicit constructor invocation */
8745 if (!fs && ctxp->explicit_constructor_p)
8747 parse_error_context
8748 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
8749 return error_mark_node;
8752 /* If we're processing an inner class and we're trying
8753 to access a field belonging to an outer class, build
8754 the access to the field */
8755 if (!fs && outer_field_access_p (current_class, decl))
8756 return build_outer_field_access (id, decl);
8758 /* Otherwise build what it takes to access the field */
8759 access = build_field_ref ((fs ? NULL_TREE : current_this),
8760 DECL_CONTEXT (decl), name);
8761 if (fs && !flag_emit_class_files && !flag_emit_xref)
8762 access = build_class_init (DECL_CONTEXT (access), access);
8763 /* We may be asked to save the real field access node */
8764 if (orig)
8765 *orig = access;
8766 /* And we return what we got */
8767 return access;
8769 /* Fall down to error report on undefined variable */
8772 /* 6.5.5.2 Qualified Expression Names */
8773 else
8775 if (orig)
8776 *orig = NULL_TREE;
8777 qualify_ambiguous_name (id);
8778 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8779 /* 15.10.2: Accessing Superclass Members using super */
8780 return resolve_field_access (id, orig, NULL);
8783 /* We've got an error here */
8784 parse_error_context (id, "Undefined variable `%s'",
8785 IDENTIFIER_POINTER (name));
8787 return error_mark_node;
8790 static void
8791 static_ref_err (wfl, field_id, class_type)
8792 tree wfl, field_id, class_type;
8794 parse_error_context
8795 (wfl,
8796 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8797 IDENTIFIER_POINTER (field_id),
8798 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8801 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8802 We return something suitable to generate the field access. We also
8803 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8804 recipient's address can be null. */
8806 static tree
8807 resolve_field_access (qual_wfl, field_decl, field_type)
8808 tree qual_wfl;
8809 tree *field_decl, *field_type;
8811 int is_static = 0;
8812 tree field_ref;
8813 tree decl, where_found, type_found;
8815 if (resolve_qualified_expression_name (qual_wfl, &decl,
8816 &where_found, &type_found))
8817 return error_mark_node;
8819 /* Resolve the LENGTH field of an array here */
8820 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
8821 && type_found && TYPE_ARRAY_P (type_found)
8822 && ! flag_emit_class_files && ! flag_emit_xref)
8824 tree length = build_java_array_length_access (where_found);
8825 field_ref =
8826 build_java_arraynull_check (type_found, length, int_type_node);
8828 /* In case we're dealing with a static array, we need to
8829 initialize its class before the array length can be fetched.
8830 It's also a good time to create a DECL_RTL for the field if
8831 none already exists, otherwise if the field was declared in a
8832 class found in an external file and hasn't been (and won't
8833 be) accessed for its value, none will be created. */
8834 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8836 build_static_field_ref (where_found);
8837 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8840 /* We might have been trying to resolve field.method(). In which
8841 case, the resolution is over and decl is the answer */
8842 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
8843 field_ref = decl;
8844 else if (JDECL_P (decl))
8846 int static_final_found = 0;
8847 if (!type_found)
8848 type_found = DECL_CONTEXT (decl);
8849 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
8850 if (FIELD_FINAL (decl) && FIELD_STATIC (decl)
8851 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
8852 && DECL_INITIAL (decl))
8854 /* When called on a FIELD_DECL of the right (primitive)
8855 type, java_complete_tree will try to substitue the decl
8856 for it's initial value. */
8857 field_ref = java_complete_tree (decl);
8858 static_final_found = 1;
8860 else
8861 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8862 NULL_TREE : where_found),
8863 type_found, DECL_NAME (decl));
8864 if (field_ref == error_mark_node)
8865 return error_mark_node;
8866 if (is_static && !static_final_found
8867 && !flag_emit_class_files && !flag_emit_xref)
8868 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
8870 else
8871 field_ref = decl;
8873 if (field_decl)
8874 *field_decl = decl;
8875 if (field_type)
8876 *field_type = (QUAL_DECL_TYPE (decl) ?
8877 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
8878 return field_ref;
8881 /* If NODE is an access to f static field, strip out the class
8882 initialization part and return the field decl, otherwise, return
8883 NODE. */
8885 static tree
8886 strip_out_static_field_access_decl (node)
8887 tree node;
8889 if (TREE_CODE (node) == COMPOUND_EXPR)
8891 tree op1 = TREE_OPERAND (node, 1);
8892 if (TREE_CODE (op1) == COMPOUND_EXPR)
8894 tree call = TREE_OPERAND (op1, 0);
8895 if (TREE_CODE (call) == CALL_EXPR
8896 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8897 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8898 == soft_initclass_node)
8899 return TREE_OPERAND (op1, 1);
8901 else if (JDECL_P (op1))
8902 return op1;
8904 return node;
8907 /* 6.5.5.2: Qualified Expression Names */
8909 static int
8910 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8911 tree wfl;
8912 tree *found_decl, *type_found, *where_found;
8914 int from_type = 0; /* Field search initiated from a type */
8915 int from_super = 0, from_cast = 0, from_qualified_this = 0;
8916 int previous_call_static = 0;
8917 int is_static;
8918 tree decl = NULL_TREE, type = NULL_TREE, q;
8919 /* For certain for of inner class instantiation */
8920 tree saved_current, saved_this;
8921 #define RESTORE_THIS_AND_CURRENT_CLASS \
8922 { current_class = saved_current; current_this = saved_this;}
8924 *type_found = *where_found = NULL_TREE;
8926 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8928 tree qual_wfl = QUAL_WFL (q);
8929 tree ret_decl; /* for EH checking */
8930 int location; /* for EH checking */
8932 /* 15.10.1 Field Access Using a Primary */
8933 switch (TREE_CODE (qual_wfl))
8935 case CALL_EXPR:
8936 case NEW_CLASS_EXPR:
8937 /* If the access to the function call is a non static field,
8938 build the code to access it. */
8939 if (JDECL_P (decl) && !FIELD_STATIC (decl))
8941 decl = maybe_access_field (decl, *where_found,
8942 DECL_CONTEXT (decl));
8943 if (decl == error_mark_node)
8944 return 1;
8947 /* And code for the function call */
8948 if (complete_function_arguments (qual_wfl))
8949 return 1;
8951 /* We might have to setup a new current class and a new this
8952 for the search of an inner class, relative to the type of
8953 a expression resolved as `decl'. The current values are
8954 saved and restored shortly after */
8955 saved_current = current_class;
8956 saved_this = current_this;
8957 if (decl && TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
8959 current_class = type;
8960 current_this = decl;
8963 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
8964 CALL_USING_SUPER (qual_wfl) = 1;
8965 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
8966 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
8967 *where_found = patch_method_invocation (qual_wfl, decl, type,
8968 &is_static, &ret_decl);
8969 if (*where_found == error_mark_node)
8971 RESTORE_THIS_AND_CURRENT_CLASS;
8972 return 1;
8974 *type_found = type = QUAL_DECL_TYPE (*where_found);
8976 /* If we're creating an inner class instance, check for that
8977 an enclosing instance is in scope */
8978 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
8979 && INNER_ENCLOSING_SCOPE_CHECK (type))
8981 parse_error_context
8982 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
8983 lang_printable_name (type, 0),
8984 (!current_this ? "" :
8985 "; an explicit one must be provided when creating this inner class"));
8986 RESTORE_THIS_AND_CURRENT_CLASS;
8987 return 1;
8990 /* In case we had to change then to resolve a inner class
8991 instantiation using a primary qualified by a `new' */
8992 RESTORE_THIS_AND_CURRENT_CLASS;
8994 /* EH check. No check on access$<n> functions */
8995 if (location
8996 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
8997 (DECL_NAME (current_function_decl)))
8998 check_thrown_exceptions (location, ret_decl);
9000 /* If the previous call was static and this one is too,
9001 build a compound expression to hold the two (because in
9002 that case, previous function calls aren't transported as
9003 forcoming function's argument. */
9004 if (previous_call_static && is_static)
9006 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9007 TREE_SIDE_EFFECTS (decl) = 1;
9009 else
9011 previous_call_static = is_static;
9012 decl = *where_found;
9014 from_type = 0;
9015 continue;
9017 case NEW_ARRAY_EXPR:
9018 case NEW_ANONYMOUS_ARRAY_EXPR:
9019 *where_found = decl = java_complete_tree (qual_wfl);
9020 if (decl == error_mark_node)
9021 return 1;
9022 *type_found = type = QUAL_DECL_TYPE (decl);
9023 CLASS_LOADED_P (type) = 1;
9024 continue;
9026 case CONVERT_EXPR:
9027 *where_found = decl = java_complete_tree (qual_wfl);
9028 if (decl == error_mark_node)
9029 return 1;
9030 *type_found = type = QUAL_DECL_TYPE (decl);
9031 from_cast = 1;
9032 continue;
9034 case CONDITIONAL_EXPR:
9035 case STRING_CST:
9036 case MODIFY_EXPR:
9037 *where_found = decl = java_complete_tree (qual_wfl);
9038 if (decl == error_mark_node)
9039 return 1;
9040 *type_found = type = QUAL_DECL_TYPE (decl);
9041 continue;
9043 case ARRAY_REF:
9044 /* If the access to the function call is a non static field,
9045 build the code to access it. */
9046 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9048 decl = maybe_access_field (decl, *where_found, type);
9049 if (decl == error_mark_node)
9050 return 1;
9052 /* And code for the array reference expression */
9053 decl = java_complete_tree (qual_wfl);
9054 if (decl == error_mark_node)
9055 return 1;
9056 type = QUAL_DECL_TYPE (decl);
9057 continue;
9059 case PLUS_EXPR:
9060 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9061 return 1;
9062 if ((type = patch_string (decl)))
9063 decl = type;
9064 *where_found = QUAL_RESOLUTION (q) = decl;
9065 *type_found = type = TREE_TYPE (decl);
9066 break;
9068 case CLASS_LITERAL:
9069 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9070 return 1;
9071 *where_found = QUAL_RESOLUTION (q) = decl;
9072 *type_found = type = TREE_TYPE (decl);
9073 break;
9075 default:
9076 /* Fix for -Wall Just go to the next statement. Don't
9077 continue */
9078 break;
9081 /* If we fall here, we weren't processing a (static) function call. */
9082 previous_call_static = 0;
9084 /* It can be the keyword THIS */
9085 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9087 if (!current_this)
9089 parse_error_context
9090 (wfl, "Keyword `this' used outside allowed context");
9091 return 1;
9093 if (ctxp->explicit_constructor_p)
9095 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9096 return 1;
9098 /* We have to generate code for intermediate acess */
9099 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9101 *where_found = decl = current_this;
9102 *type_found = type = QUAL_DECL_TYPE (decl);
9104 /* We're trying to access the this from somewhere else. Make sure
9105 it's allowed before doing so. */
9106 else
9108 if (!enclosing_context_p (type, current_class))
9110 char *p = xstrdup (lang_printable_name (type, 0));
9111 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9112 p, p,
9113 lang_printable_name (current_class, 0));
9114 free (p);
9115 return 1;
9117 *where_found = decl = build_current_thisn (type);
9118 from_qualified_this = 1;
9121 from_type = 0;
9122 continue;
9125 /* 15.10.2 Accessing Superclass Members using SUPER */
9126 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9128 tree node;
9129 /* Check on the restricted use of SUPER */
9130 if (METHOD_STATIC (current_function_decl)
9131 || current_class == object_type_node)
9133 parse_error_context
9134 (wfl, "Keyword `super' used outside allowed context");
9135 return 1;
9137 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9138 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9139 CLASSTYPE_SUPER (current_class),
9140 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9141 *where_found = decl = java_complete_tree (node);
9142 if (decl == error_mark_node)
9143 return 1;
9144 *type_found = type = QUAL_DECL_TYPE (decl);
9145 from_super = from_type = 1;
9146 continue;
9149 /* 15.13.1: Can't search for field name in packages, so we
9150 assume a variable/class name was meant. */
9151 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9153 tree name = resolve_package (wfl, &q);
9154 if (name)
9156 tree list;
9157 *where_found = decl = resolve_no_layout (name, qual_wfl);
9158 /* We want to be absolutely sure that the class is laid
9159 out. We're going to search something inside it. */
9160 *type_found = type = TREE_TYPE (decl);
9161 layout_class (type);
9162 from_type = 1;
9164 /* Fix them all the way down, if any are left. */
9165 if (q)
9167 list = TREE_CHAIN (q);
9168 while (list)
9170 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9171 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9172 list = TREE_CHAIN (list);
9176 else
9178 if (from_super || from_cast)
9179 parse_error_context
9180 ((from_cast ? qual_wfl : wfl),
9181 "No variable `%s' defined in class `%s'",
9182 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9183 lang_printable_name (type, 0));
9184 else
9185 parse_error_context
9186 (qual_wfl, "Undefined variable or class name: `%s'",
9187 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9188 return 1;
9192 /* We have a type name. It's been already resolved when the
9193 expression was qualified. */
9194 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9196 if (!(decl = QUAL_RESOLUTION (q)))
9197 return 1; /* Error reported already */
9199 /* Sneak preview. If next we see a `new', we're facing a
9200 qualification with resulted in a type being selected
9201 instead of a field. Report the error */
9202 if(TREE_CHAIN (q)
9203 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9205 parse_error_context (qual_wfl, "Undefined variable `%s'",
9206 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9207 return 1;
9210 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9212 parse_error_context
9213 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9214 java_accstring_lookup (get_access_flags_from_decl (decl)),
9215 GET_TYPE_NAME (type),
9216 IDENTIFIER_POINTER (DECL_NAME (decl)),
9217 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9218 return 1;
9220 check_deprecation (qual_wfl, decl);
9222 type = TREE_TYPE (decl);
9223 from_type = 1;
9225 /* We resolve and expression name */
9226 else
9228 tree field_decl = NULL_TREE;
9230 /* If there exists an early resolution, use it. That occurs
9231 only once and we know that there are more things to
9232 come. Don't do that when processing something after SUPER
9233 (we need more thing to be put in place below */
9234 if (!from_super && QUAL_RESOLUTION (q))
9236 decl = QUAL_RESOLUTION (q);
9237 if (!type)
9239 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9241 if (current_this)
9242 *where_found = current_this;
9243 else
9245 static_ref_err (qual_wfl, DECL_NAME (decl),
9246 current_class);
9247 return 1;
9249 if (outer_field_access_p (current_class, decl))
9250 decl = build_outer_field_access (qual_wfl, decl);
9252 else
9254 *where_found = TREE_TYPE (decl);
9255 if (TREE_CODE (*where_found) == POINTER_TYPE)
9256 *where_found = TREE_TYPE (*where_found);
9261 /* We have to search for a field, knowing the type of its
9262 container. The flag FROM_TYPE indicates that we resolved
9263 the last member of the expression as a type name, which
9264 means that for the resolution of this field, we'll look
9265 for other errors than if it was resolved as a member of
9266 an other field. */
9267 else
9269 int is_static;
9270 tree field_decl_type; /* For layout */
9272 if (!from_type && !JREFERENCE_TYPE_P (type))
9274 parse_error_context
9275 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9276 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9277 lang_printable_name (type, 0),
9278 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9279 return 1;
9282 field_decl = lookup_field_wrapper (type,
9283 EXPR_WFL_NODE (qual_wfl));
9285 /* Maybe what we're trying to access an inner class. */
9286 if (!field_decl)
9288 tree ptr, inner_decl;
9290 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9291 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9292 if (inner_decl)
9294 check_inner_class_access (inner_decl, decl, qual_wfl);
9295 type = TREE_TYPE (inner_decl);
9296 decl = inner_decl;
9297 from_type = 1;
9298 continue;
9302 if (field_decl == NULL_TREE)
9304 parse_error_context
9305 (qual_wfl, "No variable `%s' defined in type `%s'",
9306 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9307 GET_TYPE_NAME (type));
9308 return 1;
9310 if (field_decl == error_mark_node)
9311 return 1;
9313 /* Layout the type of field_decl, since we may need
9314 it. Don't do primitive types or loaded classes. The
9315 situation of non primitive arrays may not handled
9316 properly here. FIXME */
9317 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9318 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9319 else
9320 field_decl_type = TREE_TYPE (field_decl);
9321 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9322 && !CLASS_LOADED_P (field_decl_type)
9323 && !TYPE_ARRAY_P (field_decl_type))
9324 resolve_and_layout (field_decl_type, NULL_TREE);
9325 if (TYPE_ARRAY_P (field_decl_type))
9326 CLASS_LOADED_P (field_decl_type) = 1;
9328 /* Check on accessibility here */
9329 if (not_accessible_p (type, field_decl, from_super))
9331 parse_error_context
9332 (qual_wfl,
9333 "Can't access %s field `%s.%s' from `%s'",
9334 java_accstring_lookup
9335 (get_access_flags_from_decl (field_decl)),
9336 GET_TYPE_NAME (type),
9337 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9338 IDENTIFIER_POINTER
9339 (DECL_NAME (TYPE_NAME (current_class))));
9340 return 1;
9342 check_deprecation (qual_wfl, field_decl);
9344 /* There are things to check when fields are accessed
9345 from type. There are no restrictions on a static
9346 declaration of the field when it is accessed from an
9347 interface */
9348 is_static = FIELD_STATIC (field_decl);
9349 if (!from_super && from_type
9350 && !TYPE_INTERFACE_P (type)
9351 && !is_static
9352 && (current_function_decl
9353 && METHOD_STATIC (current_function_decl)))
9355 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9356 return 1;
9358 from_cast = from_super = 0;
9360 /* It's an access from a type but it isn't static, we
9361 make it relative to `this'. */
9362 if (!is_static && from_type)
9363 decl = current_this;
9365 /* If we need to generate something to get a proper
9366 handle on what this field is accessed from, do it
9367 now. */
9368 if (!is_static)
9370 decl = maybe_access_field (decl, *where_found, *type_found);
9371 if (decl == error_mark_node)
9372 return 1;
9375 /* We want to keep the location were found it, and the type
9376 we found. */
9377 *where_found = decl;
9378 *type_found = type;
9380 /* Generate the correct expression for field access from
9381 qualified this */
9382 if (from_qualified_this)
9384 field_decl = build_outer_field_access (qual_wfl, field_decl);
9385 from_qualified_this = 0;
9388 /* This is the decl found and eventually the next one to
9389 search from */
9390 decl = field_decl;
9392 from_type = 0;
9393 type = QUAL_DECL_TYPE (decl);
9395 /* Sneak preview. If decl is qualified by a `new', report
9396 the error here to be accurate on the peculiar construct */
9397 if (TREE_CHAIN (q)
9398 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9399 && !JREFERENCE_TYPE_P (type))
9401 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9402 lang_printable_name (type, 0));
9403 return 1;
9406 /* `q' might have changed due to a after package resolution
9407 re-qualification */
9408 if (!q)
9409 break;
9411 *found_decl = decl;
9412 return 0;
9415 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9416 can't be accessed from REFERENCE (a record type). This should be
9417 used when decl is a field or a method.*/
9419 static int
9420 not_accessible_p (reference, member, from_super)
9421 tree reference, member;
9422 int from_super;
9424 int access_flag = get_access_flags_from_decl (member);
9426 /* Inner classes are processed by check_inner_class_access */
9427 if (INNER_CLASS_TYPE_P (reference))
9428 return 0;
9430 /* Access always granted for members declared public */
9431 if (access_flag & ACC_PUBLIC)
9432 return 0;
9434 /* Check access on protected members */
9435 if (access_flag & ACC_PROTECTED)
9437 /* Access granted if it occurs from within the package
9438 containing the class in which the protected member is
9439 declared */
9440 if (class_in_current_package (DECL_CONTEXT (member)))
9441 return 0;
9443 /* If accessed with the form `super.member', then access is granted */
9444 if (from_super)
9445 return 0;
9447 /* Otherwise, access is granted if occuring from the class where
9448 member is declared or a subclass of it. Find the right
9449 context to perform the check */
9450 if (PURE_INNER_CLASS_TYPE_P (reference))
9452 while (INNER_CLASS_TYPE_P (reference))
9454 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9455 return 0;
9456 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9459 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9460 return 0;
9461 return 1;
9464 /* Check access on private members. Access is granted only if it
9465 occurs from within the class in which it is declared. Exceptions
9466 are accesses from inner-classes. */
9467 if (access_flag & ACC_PRIVATE)
9468 return (current_class == DECL_CONTEXT (member) ? 0 :
9469 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
9471 /* Default access are permitted only when occuring within the
9472 package in which the type (REFERENCE) is declared. In other words,
9473 REFERENCE is defined in the current package */
9474 if (ctxp->package)
9475 return !class_in_current_package (reference);
9477 /* Otherwise, access is granted */
9478 return 0;
9481 /* Test deprecated decl access. */
9482 static void
9483 check_deprecation (wfl, decl)
9484 tree wfl, decl;
9486 const char *file = DECL_SOURCE_FILE (decl);
9487 /* Complain if the field is deprecated and the file it was defined
9488 in isn't compiled at the same time the file which contains its
9489 use is */
9490 if (DECL_DEPRECATED (decl)
9491 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9493 char the [20];
9494 switch (TREE_CODE (decl))
9496 case FUNCTION_DECL:
9497 strcpy (the, "method");
9498 break;
9499 case FIELD_DECL:
9500 strcpy (the, "field");
9501 break;
9502 case TYPE_DECL:
9503 strcpy (the, "class");
9504 break;
9505 default:
9506 fatal ("unexpected DECL code - check_deprecation");
9508 parse_warning_context
9509 (wfl, "The %s `%s' in class `%s' has been deprecated",
9510 the, lang_printable_name (decl, 0),
9511 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9515 /* Returns 1 if class was declared in the current package, 0 otherwise */
9517 static int
9518 class_in_current_package (class)
9519 tree class;
9521 static tree cache = NULL_TREE;
9522 int qualified_flag;
9523 tree left;
9525 if (cache == class)
9526 return 1;
9528 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9530 /* If the current package is empty and the name of CLASS is
9531 qualified, class isn't in the current package. If there is a
9532 current package and the name of the CLASS is not qualified, class
9533 isn't in the current package */
9534 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9535 return 0;
9537 /* If there is not package and the name of CLASS isn't qualified,
9538 they belong to the same unnamed package */
9539 if (!ctxp->package && !qualified_flag)
9540 return 1;
9542 /* Compare the left part of the name of CLASS with the package name */
9543 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9544 if (ctxp->package == left)
9546 cache = class;
9547 return 1;
9549 return 0;
9552 /* This function may generate code to access DECL from WHERE. This is
9553 done only if certain conditions meet. */
9555 static tree
9556 maybe_access_field (decl, where, type)
9557 tree decl, where, type;
9559 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9560 && !FIELD_STATIC (decl))
9561 decl = build_field_ref (where ? where : current_this,
9562 (type ? type : DECL_CONTEXT (decl)),
9563 DECL_NAME (decl));
9564 return decl;
9567 /* Build a method invocation, by patching PATCH. If non NULL
9568 and according to the situation, PRIMARY and WHERE may be
9569 used. IS_STATIC is set to 1 if the invoked function is static. */
9571 static tree
9572 patch_method_invocation (patch, primary, where, is_static, ret_decl)
9573 tree patch, primary, where;
9574 int *is_static;
9575 tree *ret_decl;
9577 tree wfl = TREE_OPERAND (patch, 0);
9578 tree args = TREE_OPERAND (patch, 1);
9579 tree name = EXPR_WFL_NODE (wfl);
9580 tree list;
9581 int is_static_flag = 0;
9582 int is_super_init = 0;
9583 tree this_arg = NULL_TREE;
9585 /* Should be overriden if everything goes well. Otherwise, if
9586 something fails, it should keep this value. It stop the
9587 evaluation of a bogus assignment. See java_complete_tree,
9588 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9589 evaluating an assignment */
9590 TREE_TYPE (patch) = error_mark_node;
9592 /* Since lookup functions are messing with line numbers, save the
9593 context now. */
9594 java_parser_context_save_global ();
9596 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9598 /* Resolution of qualified name, excluding constructors */
9599 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9601 tree identifier, identifier_wfl, type, resolved;
9602 /* Extract the last IDENTIFIER of the qualified
9603 expression. This is a wfl and we will use it's location
9604 data during error report. */
9605 identifier_wfl = cut_identifier_in_qualified (wfl);
9606 identifier = EXPR_WFL_NODE (identifier_wfl);
9608 /* Given the context, IDENTIFIER is syntactically qualified
9609 as a MethodName. We need to qualify what's before */
9610 qualify_ambiguous_name (wfl);
9611 resolved = resolve_field_access (wfl, NULL, NULL);
9613 if (resolved == error_mark_node)
9614 PATCH_METHOD_RETURN_ERROR ();
9616 type = GET_SKIP_TYPE (resolved);
9617 resolve_and_layout (type, NULL_TREE);
9619 if (JPRIMITIVE_TYPE_P (type))
9621 parse_error_context
9622 (identifier_wfl,
9623 "Can't invoke a method on primitive type `%s'",
9624 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9625 PATCH_METHOD_RETURN_ERROR ();
9628 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9629 args = nreverse (args);
9631 /* We're resolving a call from a type */
9632 if (TREE_CODE (resolved) == TYPE_DECL)
9634 if (CLASS_INTERFACE (resolved))
9636 parse_error_context
9637 (identifier_wfl,
9638 "Can't make static reference to method `%s' in interface `%s'",
9639 IDENTIFIER_POINTER (identifier),
9640 IDENTIFIER_POINTER (name));
9641 PATCH_METHOD_RETURN_ERROR ();
9643 if (list && !METHOD_STATIC (list))
9645 char *fct_name = xstrdup (lang_printable_name (list, 0));
9646 parse_error_context
9647 (identifier_wfl,
9648 "Can't make static reference to method `%s %s' in class `%s'",
9649 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9650 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9651 free (fct_name);
9652 PATCH_METHOD_RETURN_ERROR ();
9655 else
9656 this_arg = primary = resolved;
9658 /* IDENTIFIER_WFL will be used to report any problem further */
9659 wfl = identifier_wfl;
9661 /* Resolution of simple names, names generated after a primary: or
9662 constructors */
9663 else
9665 tree class_to_search = NULL_TREE;
9666 int lc; /* Looking for Constructor */
9668 /* We search constructor in their target class */
9669 if (CALL_CONSTRUCTOR_P (patch))
9671 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9672 class_to_search = EXPR_WFL_NODE (wfl);
9673 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9674 this_identifier_node)
9675 class_to_search = NULL_TREE;
9676 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9677 super_identifier_node)
9679 is_super_init = 1;
9680 if (CLASSTYPE_SUPER (current_class))
9681 class_to_search =
9682 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9683 else
9685 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
9686 PATCH_METHOD_RETURN_ERROR ();
9690 /* Class to search is NULL if we're searching the current one */
9691 if (class_to_search)
9693 class_to_search = resolve_and_layout (class_to_search, wfl);
9695 if (!class_to_search)
9697 parse_error_context
9698 (wfl, "Class `%s' not found in type declaration",
9699 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9700 PATCH_METHOD_RETURN_ERROR ();
9703 /* Can't instantiate an abstract class, but we can
9704 invoke it's constructor. It's use within the `new'
9705 context is denied here. */
9706 if (CLASS_ABSTRACT (class_to_search)
9707 && TREE_CODE (patch) == NEW_CLASS_EXPR)
9709 parse_error_context
9710 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9711 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9712 PATCH_METHOD_RETURN_ERROR ();
9715 class_to_search = TREE_TYPE (class_to_search);
9717 else
9718 class_to_search = current_class;
9719 lc = 1;
9721 /* This is a regular search in the local class, unless an
9722 alternate class is specified. */
9723 else
9725 class_to_search = (where ? where : current_class);
9726 lc = 0;
9729 /* NAME is a simple identifier or comes from a primary. Search
9730 in the class whose declaration contain the method being
9731 invoked. */
9732 resolve_and_layout (class_to_search, NULL_TREE);
9734 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
9735 /* Don't continue if no method were found, as the next statement
9736 can't be executed then. */
9737 if (!list)
9738 PATCH_METHOD_RETURN_ERROR ();
9740 /* Check for static reference if non static methods */
9741 if (check_for_static_method_reference (wfl, patch, list,
9742 class_to_search, primary))
9743 PATCH_METHOD_RETURN_ERROR ();
9745 /* Check for inner classes creation from illegal contexts */
9746 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9747 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9748 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9750 parse_error_context
9751 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9752 lang_printable_name (class_to_search, 0),
9753 (!current_this ? "" :
9754 "; an explicit one must be provided when creating this inner class"));
9755 PATCH_METHOD_RETURN_ERROR ();
9758 /* Non static methods are called with the current object extra
9759 argument. If patch a `new TYPE()', the argument is the value
9760 returned by the object allocator. If method is resolved as a
9761 primary, use the primary otherwise use the current THIS. */
9762 args = nreverse (args);
9763 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
9765 this_arg = primary ? primary : current_this;
9767 /* If we're using an access method, things are different.
9768 There are two familly of cases:
9770 1) We're not generating bytecodes:
9772 - LIST is non static. It's invocation is transformed from
9773 x(a1,...,an) into this$<n>.x(a1,....an).
9774 - LIST is static. It's invocation is transformed from
9775 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9777 2) We're generating bytecodes:
9779 - LIST is non static. It's invocation is transformed from
9780 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9781 - LIST is static. It's invocation is transformed from
9782 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9784 Of course, this$<n> can be abitrary complex, ranging from
9785 this$0 (the immediate outer context) to
9786 access$0(access$0(...(this$0))).
9788 maybe_use_access_method returns a non zero value if the
9789 this_arg has to be moved into the (then generated) stub
9790 argument list. In the meantime, the selected function
9791 might have be replaced by a generated stub. */
9792 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9793 args = tree_cons (NULL_TREE, this_arg, args);
9797 /* Merge point of all resolution schemes. If we have nothing, this
9798 is an error, already signaled */
9799 if (!list)
9800 PATCH_METHOD_RETURN_ERROR ();
9802 /* Check accessibility, position the is_static flag, build and
9803 return the call */
9804 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
9806 char *fct_name = xstrdup (lang_printable_name (list, 0));
9807 parse_error_context
9808 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9809 java_accstring_lookup (get_access_flags_from_decl (list)),
9810 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9811 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9812 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9813 free (fct_name);
9814 PATCH_METHOD_RETURN_ERROR ();
9816 check_deprecation (wfl, list);
9818 /* If invoking a innerclass constructor, there are hidden parameters
9819 to pass */
9820 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9821 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9823 /* And make sure we add the accessed local variables to be saved
9824 in field aliases. */
9825 args = build_alias_initializer_parameter_list
9826 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9828 /* Secretly pass the current_this/primary as a second argument */
9829 if (primary || current_this)
9830 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9831 else
9832 args = tree_cons (NULL_TREE, integer_zero_node, args);
9835 /* This handles the situation where a constructor invocation needs
9836 to have an enclosing context passed as a second parameter (the
9837 constructor is one of an inner class. We extract it from the
9838 current function. */
9839 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9841 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9842 tree extra_arg;
9844 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9846 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9847 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9849 else
9851 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9852 extra_arg =
9853 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9854 extra_arg = java_complete_tree (extra_arg);
9856 args = tree_cons (NULL_TREE, extra_arg, args);
9859 is_static_flag = METHOD_STATIC (list);
9860 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
9861 args = tree_cons (NULL_TREE, this_arg, args);
9863 /* In the context of an explicit constructor invocation, we can't
9864 invoke any method relying on `this'. Exceptions are: we're
9865 invoking a static function, primary exists and is not the current
9866 this, we're creating a new object. */
9867 if (ctxp->explicit_constructor_p
9868 && !is_static_flag
9869 && (!primary || primary == current_this)
9870 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
9872 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9873 PATCH_METHOD_RETURN_ERROR ();
9875 java_parser_context_restore_global ();
9876 if (is_static)
9877 *is_static = is_static_flag;
9878 /* Sometimes, we want the decl of the selected method. Such as for
9879 EH checking */
9880 if (ret_decl)
9881 *ret_decl = list;
9882 patch = patch_invoke (patch, list, args);
9883 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
9885 tree finit_parms, finit_call;
9887 /* Prepare to pass hidden parameters to finit$, if any. */
9888 finit_parms = build_alias_initializer_parameter_list
9889 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
9891 finit_call =
9892 build_method_invocation (build_wfl_node (finit_identifier_node),
9893 finit_parms);
9895 /* Generate the code used to initialize fields declared with an
9896 initialization statement and build a compound statement along
9897 with the super constructor invocation. */
9898 patch = build (COMPOUND_EXPR, void_type_node, patch,
9899 java_complete_tree (finit_call));
9900 CAN_COMPLETE_NORMALLY (patch) = 1;
9902 return patch;
9905 /* Check that we're not trying to do a static reference to a method in
9906 non static method. Return 1 if it's the case, 0 otherwise. */
9908 static int
9909 check_for_static_method_reference (wfl, node, method, where, primary)
9910 tree wfl, node, method, where, primary;
9912 if (METHOD_STATIC (current_function_decl)
9913 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
9915 char *fct_name = xstrdup (lang_printable_name (method, 0));
9916 parse_error_context
9917 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
9918 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
9919 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
9920 free (fct_name);
9921 return 1;
9923 return 0;
9926 /* Fix the invocation of *MDECL if necessary in the case of a
9927 invocation from an inner class. *THIS_ARG might be modified
9928 appropriately and an alternative access to *MDECL might be
9929 returned. */
9931 static int
9932 maybe_use_access_method (is_super_init, mdecl, this_arg)
9933 int is_super_init;
9934 tree *mdecl, *this_arg;
9936 tree ctx;
9937 tree md = *mdecl, ta = *this_arg;
9938 int to_return = 0;
9939 int non_static_context = !METHOD_STATIC (md);
9941 if (is_super_init
9942 || DECL_CONTEXT (md) == current_class
9943 || !PURE_INNER_CLASS_TYPE_P (current_class)
9944 || DECL_FINIT_P (md))
9945 return 0;
9947 /* If we're calling a method found in an enclosing class, generate
9948 what it takes to retrieve the right this. Don't do that if we're
9949 invoking a static method. */
9951 if (non_static_context)
9953 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
9954 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
9956 ta = build_current_thisn (current_class);
9957 ta = build_wfl_node (ta);
9959 else
9961 tree type = ctx;
9962 while (type)
9964 maybe_build_thisn_access_method (type);
9965 if (inherits_from_p (type, DECL_CONTEXT (md)))
9967 ta = build_access_to_thisn (ctx, type, 0);
9968 break;
9970 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
9971 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
9974 ta = java_complete_tree (ta);
9977 /* We might have to use an access method to get to MD. We can
9978 break the method access rule as far as we're not generating
9979 bytecode */
9980 if (METHOD_PRIVATE (md) && flag_emit_class_files)
9982 md = build_outer_method_access_method (md);
9983 to_return = 1;
9986 *mdecl = md;
9987 *this_arg = ta;
9989 /* Returnin a non zero value indicates we were doing a non static
9990 method invokation that is now a static invocation. It will have
9991 callee displace `this' to insert it in the regular argument
9992 list. */
9993 return (non_static_context && to_return);
9996 /* Patch an invoke expression METHOD and ARGS, based on its invocation
9997 mode. */
9999 static tree
10000 patch_invoke (patch, method, args)
10001 tree patch, method, args;
10003 tree dtable, func;
10004 tree original_call, t, ta;
10005 tree cond = NULL_TREE;
10007 /* Last step for args: convert build-in types. If we're dealing with
10008 a new TYPE() type call, the first argument to the constructor
10009 isn't found in the incoming argument list, but delivered by
10010 `new' */
10011 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10012 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10013 t = TREE_CHAIN (t);
10014 for (ta = args; t != end_params_node && ta;
10015 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10016 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10017 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10018 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10020 /* Resolve unresolved returned type isses */
10021 t = TREE_TYPE (TREE_TYPE (method));
10022 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10023 resolve_and_layout (TREE_TYPE (t), NULL);
10025 if (flag_emit_class_files || flag_emit_xref)
10026 func = method;
10027 else
10029 tree signature = build_java_signature (TREE_TYPE (method));
10030 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10032 case INVOKE_VIRTUAL:
10033 dtable = invoke_build_dtable (0, args);
10034 func = build_invokevirtual (dtable, method);
10035 break;
10037 case INVOKE_NONVIRTUAL:
10038 /* If the object for the method call is null, we throw an
10039 exception. We don't do this if the object is the current
10040 method's `this'. In other cases we just rely on an
10041 optimization pass to eliminate redundant checks. */
10042 if (TREE_VALUE (args) != current_this)
10044 /* We use a SAVE_EXPR here to make sure we only evaluate
10045 the new `self' expression once. */
10046 tree save_arg = save_expr (TREE_VALUE (args));
10047 TREE_VALUE (args) = save_arg;
10048 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10049 null_pointer_node);
10051 /* Fall through. */
10053 case INVOKE_SUPER:
10054 case INVOKE_STATIC:
10055 func = build_known_method_ref (method, TREE_TYPE (method),
10056 DECL_CONTEXT (method),
10057 signature, args);
10058 break;
10060 case INVOKE_INTERFACE:
10061 dtable = invoke_build_dtable (1, args);
10062 func = build_invokeinterface (dtable, method);
10063 break;
10065 default:
10066 fatal ("internal error - unknown invocation_mode result");
10069 /* Ensure self_type is initialized, (invokestatic). FIXME */
10070 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10073 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10074 TREE_OPERAND (patch, 0) = func;
10075 TREE_OPERAND (patch, 1) = args;
10076 original_call = patch;
10078 /* We're processing a `new TYPE ()' form. New is called and its
10079 returned value is the first argument to the constructor. We build
10080 a COMPOUND_EXPR and use saved expression so that the overall NEW
10081 expression value is a pointer to a newly created and initialized
10082 class. */
10083 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10085 tree class = DECL_CONTEXT (method);
10086 tree c1, saved_new, size, new;
10087 if (flag_emit_class_files || flag_emit_xref)
10089 TREE_TYPE (patch) = build_pointer_type (class);
10090 return patch;
10092 if (!TYPE_SIZE (class))
10093 safe_layout_class (class);
10094 size = size_in_bytes (class);
10095 new = build (CALL_EXPR, promote_type (class),
10096 build_address_of (alloc_object_node),
10097 tree_cons (NULL_TREE, build_class_ref (class),
10098 build_tree_list (NULL_TREE,
10099 size_in_bytes (class))),
10100 NULL_TREE);
10101 saved_new = save_expr (new);
10102 c1 = build_tree_list (NULL_TREE, saved_new);
10103 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10104 TREE_OPERAND (original_call, 1) = c1;
10105 TREE_SET_CODE (original_call, CALL_EXPR);
10106 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10109 /* If COND is set, then we are building a check to see if the object
10110 is NULL. */
10111 if (cond != NULL_TREE)
10113 /* We have to make the `then' branch a compound expression to
10114 make the types turn out right. This seems bizarre. */
10115 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10116 build (COMPOUND_EXPR, TREE_TYPE (patch),
10117 build (CALL_EXPR, void_type_node,
10118 build_address_of (soft_nullpointer_node),
10119 NULL_TREE, NULL_TREE),
10120 (FLOAT_TYPE_P (TREE_TYPE (patch))
10121 ? build_real (TREE_TYPE (patch), dconst0)
10122 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10123 integer_zero_node))),
10124 patch);
10125 TREE_SIDE_EFFECTS (patch) = 1;
10128 return patch;
10131 static int
10132 invocation_mode (method, super)
10133 tree method;
10134 int super;
10136 int access = get_access_flags_from_decl (method);
10138 if (super)
10139 return INVOKE_SUPER;
10141 if (access & ACC_STATIC)
10142 return INVOKE_STATIC;
10144 /* We have to look for a constructor before we handle nonvirtual
10145 calls; otherwise the constructor will look nonvirtual. */
10146 if (DECL_CONSTRUCTOR_P (method))
10147 return INVOKE_STATIC;
10149 if (access & ACC_FINAL || access & ACC_PRIVATE)
10150 return INVOKE_NONVIRTUAL;
10152 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10153 return INVOKE_NONVIRTUAL;
10155 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10156 return INVOKE_INTERFACE;
10158 return INVOKE_VIRTUAL;
10161 /* Retrieve a refined list of matching methods. It covers the step
10162 15.11.2 (Compile-Time Step 2) */
10164 static tree
10165 lookup_method_invoke (lc, cl, class, name, arg_list)
10166 int lc;
10167 tree cl;
10168 tree class, name, arg_list;
10170 tree atl = end_params_node; /* Arg Type List */
10171 tree method, signature, list, node;
10172 const char *candidates; /* Used for error report */
10173 char *dup;
10175 /* Fix the arguments */
10176 for (node = arg_list; node; node = TREE_CHAIN (node))
10178 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10179 /* Non primitive type may have to be resolved */
10180 if (!JPRIMITIVE_TYPE_P (current_arg))
10181 resolve_and_layout (current_arg, NULL_TREE);
10182 /* And promoted */
10183 if (TREE_CODE (current_arg) == RECORD_TYPE)
10184 current_arg = promote_type (current_arg);
10185 atl = tree_cons (NULL_TREE, current_arg, atl);
10188 /* Presto. If we're dealing with an anonymous class and a
10189 constructor call, generate the right constructor now, since we
10190 know the arguments' types. */
10192 if (lc && ANONYMOUS_CLASS_P (class))
10193 craft_constructor (TYPE_NAME (class), atl);
10195 /* Find all candidates and then refine the list, searching for the
10196 most specific method. */
10197 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10198 list = find_most_specific_methods_list (list);
10199 if (list && !TREE_CHAIN (list))
10200 return TREE_VALUE (list);
10202 /* Issue an error. List candidates if any. Candidates are listed
10203 only if accessible (non accessible methods may end-up here for
10204 the sake of a better error report). */
10205 candidates = NULL;
10206 if (list)
10208 tree current;
10209 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10210 for (current = list; current; current = TREE_CHAIN (current))
10212 tree cm = TREE_VALUE (current);
10213 char string [4096];
10214 if (!cm || not_accessible_p (class, cm, 0))
10215 continue;
10216 sprintf
10217 (string, " `%s' in `%s'%s",
10218 get_printable_method_name (cm),
10219 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10220 (TREE_CHAIN (current) ? "\n" : ""));
10221 obstack_grow (&temporary_obstack, string, strlen (string));
10223 obstack_1grow (&temporary_obstack, '\0');
10224 candidates = obstack_finish (&temporary_obstack);
10226 /* Issue the error message */
10227 method = make_node (FUNCTION_TYPE);
10228 TYPE_ARG_TYPES (method) = atl;
10229 signature = build_java_argument_signature (method);
10230 dup = xstrdup (lang_printable_name (class, 0));
10231 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10232 (lc ? "constructor" : "method"),
10233 (lc ? dup : IDENTIFIER_POINTER (name)),
10234 IDENTIFIER_POINTER (signature), dup,
10235 (candidates ? candidates : ""));
10236 free (dup);
10237 return NULL_TREE;
10240 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10241 when we're looking for a constructor. */
10243 static tree
10244 find_applicable_accessible_methods_list (lc, class, name, arglist)
10245 int lc;
10246 tree class, name, arglist;
10248 static struct hash_table t, *searched_classes = NULL;
10249 static int search_not_done = 0;
10250 tree list = NULL_TREE, all_list = NULL_TREE;
10252 /* Check the hash table to determine if this class has been searched
10253 already. */
10254 if (searched_classes)
10256 if (hash_lookup (searched_classes,
10257 (const hash_table_key) class, FALSE, NULL))
10258 return NULL;
10260 else
10262 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10263 java_hash_compare_tree_node);
10264 searched_classes = &t;
10267 search_not_done++;
10268 hash_lookup (searched_classes,
10269 (const hash_table_key) class, TRUE, NULL);
10271 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10273 load_class (class, 1);
10274 safe_layout_class (class);
10277 /* Search interfaces */
10278 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10279 && CLASS_INTERFACE (TYPE_NAME (class)))
10281 int i, n;
10282 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10283 search_applicable_methods_list (lc, TYPE_METHODS (class),
10284 name, arglist, &list, &all_list);
10285 n = TREE_VEC_LENGTH (basetype_vec);
10286 for (i = 1; i < n; i++)
10288 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10289 tree rlist;
10291 rlist = find_applicable_accessible_methods_list (lc, t, name,
10292 arglist);
10293 list = chainon (rlist, list);
10296 /* Search classes */
10297 else
10299 tree sc = class;
10300 int seen_inner_class = 0;
10301 search_applicable_methods_list (lc, TYPE_METHODS (class),
10302 name, arglist, &list, &all_list);
10304 /* We must search all interfaces of this class */
10305 if (!lc)
10307 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10308 int n = TREE_VEC_LENGTH (basetype_vec), i;
10309 for (i = 1; i < n; i++)
10311 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10312 if (t != object_type_node)
10314 tree rlist
10315 = find_applicable_accessible_methods_list (lc, t,
10316 name, arglist);
10317 list = chainon (rlist, list);
10322 /* Search enclosing context of inner classes before looking
10323 ancestors up. */
10324 while (!lc && INNER_CLASS_TYPE_P (class))
10326 tree rlist;
10327 seen_inner_class = 1;
10328 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
10329 rlist = find_applicable_accessible_methods_list (lc, class,
10330 name, arglist);
10331 list = chainon (rlist, list);
10334 if (!lc && seen_inner_class
10335 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10336 class = CLASSTYPE_SUPER (sc);
10337 else
10338 class = sc;
10340 /* Search superclass */
10341 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10343 tree rlist;
10344 class = CLASSTYPE_SUPER (class);
10345 rlist = find_applicable_accessible_methods_list (lc, class,
10346 name, arglist);
10347 list = chainon (rlist, list);
10351 search_not_done--;
10353 /* We're done. Reset the searched classes list and finally search
10354 java.lang.Object if it wasn't searched already. */
10355 if (!search_not_done)
10357 if (!lc
10358 && TYPE_METHODS (object_type_node)
10359 && !hash_lookup (searched_classes,
10360 (const hash_table_key) object_type_node,
10361 FALSE, NULL))
10363 search_applicable_methods_list (lc,
10364 TYPE_METHODS (object_type_node),
10365 name, arglist, &list, &all_list);
10367 hash_table_free (searched_classes);
10368 searched_classes = NULL;
10371 /* Either return the list obtained or all selected (but
10372 inaccessible) methods for better error report. */
10373 return (!list ? all_list : list);
10376 /* Effectively search for the appropriate method in method */
10378 static void
10379 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10380 int lc;
10381 tree method, name, arglist;
10382 tree *list, *all_list;
10384 for (; method; method = TREE_CHAIN (method))
10386 /* When dealing with constructor, stop here, otherwise search
10387 other classes */
10388 if (lc && !DECL_CONSTRUCTOR_P (method))
10389 continue;
10390 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10391 || (GET_METHOD_NAME (method) != name)))
10392 continue;
10394 if (argument_types_convertible (method, arglist))
10396 /* Retain accessible methods only */
10397 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10398 method, 0))
10399 *list = tree_cons (NULL_TREE, method, *list);
10400 else
10401 /* Also retain all selected method here */
10402 *all_list = tree_cons (NULL_TREE, method, *list);
10407 /* 15.11.2.2 Choose the Most Specific Method */
10409 static tree
10410 find_most_specific_methods_list (list)
10411 tree list;
10413 int max = 0;
10414 int abstract, candidates;
10415 tree current, new_list = NULL_TREE;
10416 for (current = list; current; current = TREE_CHAIN (current))
10418 tree method;
10419 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10421 for (method = list; method; method = TREE_CHAIN (method))
10423 tree method_v, current_v;
10424 /* Don't test a method against itself */
10425 if (method == current)
10426 continue;
10428 method_v = TREE_VALUE (method);
10429 current_v = TREE_VALUE (current);
10431 /* Compare arguments and location where methods where declared */
10432 if (argument_types_convertible (method_v, current_v))
10434 if (valid_method_invocation_conversion_p
10435 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10436 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10437 && enclosing_context_p (DECL_CONTEXT (method_v),
10438 DECL_CONTEXT (current_v))))
10440 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10441 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10442 max = (v > max ? v : max);
10448 /* Review the list and select the maximally specific methods */
10449 for (current = list, abstract = -1, candidates = -1;
10450 current; current = TREE_CHAIN (current))
10451 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10453 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10454 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10455 candidates++;
10458 /* If we have several and they're all abstract, just pick the
10459 closest one. */
10460 if (candidates > 0 && (candidates == abstract))
10462 new_list = nreverse (new_list);
10463 TREE_CHAIN (new_list) = NULL_TREE;
10466 /* We have several, we couldn't find a most specific, all but one are
10467 abstract, we pick the only non abstract one. */
10468 if (candidates > 0 && !max && (candidates == abstract+1))
10470 for (current = new_list; current; current = TREE_CHAIN (current))
10471 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10473 TREE_CHAIN (current) = NULL_TREE;
10474 new_list = current;
10478 /* If we can't find one, lower expectations and try to gather multiple
10479 maximally specific methods */
10480 while (!new_list && max)
10482 while (--max > 0)
10484 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10485 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10489 return new_list;
10492 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10493 converted by method invocation conversion (5.3) to the type of the
10494 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10495 to change less often than M1. */
10497 static int
10498 argument_types_convertible (m1, m2_or_arglist)
10499 tree m1, m2_or_arglist;
10501 static tree m2_arg_value = NULL_TREE;
10502 static tree m2_arg_cache = NULL_TREE;
10504 register tree m1_arg, m2_arg;
10506 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10508 if (m2_arg_value == m2_or_arglist)
10509 m2_arg = m2_arg_cache;
10510 else
10512 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10513 argument types */
10514 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10516 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10517 if (!METHOD_STATIC (m2_or_arglist))
10518 m2_arg = TREE_CHAIN (m2_arg);
10520 else
10521 m2_arg = m2_or_arglist;
10523 m2_arg_value = m2_or_arglist;
10524 m2_arg_cache = m2_arg;
10527 while (m1_arg != end_params_node && m2_arg != end_params_node)
10529 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
10530 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10531 TREE_VALUE (m2_arg)))
10532 break;
10533 m1_arg = TREE_CHAIN (m1_arg);
10534 m2_arg = TREE_CHAIN (m2_arg);
10536 return m1_arg == end_params_node && m2_arg == end_params_node;
10539 /* Qualification routines */
10541 static void
10542 qualify_ambiguous_name (id)
10543 tree id;
10545 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10546 saved_current_class;
10547 int again, super_found = 0, this_found = 0, new_array_found = 0;
10548 int code;
10550 /* We first qualify the first element, then derive qualification of
10551 others based on the first one. If the first element is qualified
10552 by a resolution (field or type), this resolution is stored in the
10553 QUAL_RESOLUTION of the qual element being examined. We need to
10554 save the current_class since the use of SUPER might change the
10555 its value. */
10556 saved_current_class = current_class;
10557 qual = EXPR_WFL_QUALIFICATION (id);
10558 do {
10560 /* Simple qualified expression feature a qual_wfl that is a
10561 WFL. Expression derived from a primary feature more complicated
10562 things like a CALL_EXPR. Expression from primary need to be
10563 worked out to extract the part on which the qualification will
10564 take place. */
10565 qual_wfl = QUAL_WFL (qual);
10566 switch (TREE_CODE (qual_wfl))
10568 case CALL_EXPR:
10569 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10570 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10572 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10573 qual_wfl = QUAL_WFL (qual);
10575 break;
10576 case NEW_ARRAY_EXPR:
10577 case NEW_ANONYMOUS_ARRAY_EXPR:
10578 qual = TREE_CHAIN (qual);
10579 again = new_array_found = 1;
10580 continue;
10581 case CONVERT_EXPR:
10582 break;
10583 case NEW_CLASS_EXPR:
10584 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10585 break;
10586 case ARRAY_REF:
10587 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10588 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10589 break;
10590 case STRING_CST:
10591 qual = TREE_CHAIN (qual);
10592 qual_wfl = QUAL_WFL (qual);
10593 break;
10594 case CLASS_LITERAL:
10595 qual = TREE_CHAIN (qual);
10596 qual_wfl = QUAL_WFL (qual);
10597 break;
10598 default:
10599 /* Fix for -Wall. Just break doing nothing */
10600 break;
10603 ptr_type = current_class;
10604 again = 0;
10605 code = TREE_CODE (qual_wfl);
10607 /* Pos evaluation: non WFL leading expression nodes */
10608 if (code == CONVERT_EXPR
10609 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10610 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10612 else if (code == INTEGER_CST)
10613 name = qual_wfl;
10615 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
10616 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10617 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10619 else if (code == TREE_LIST)
10620 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10622 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10623 || code == PLUS_EXPR)
10625 qual = TREE_CHAIN (qual);
10626 qual_wfl = QUAL_WFL (qual);
10627 again = 1;
10629 else
10631 name = EXPR_WFL_NODE (qual_wfl);
10632 if (!name)
10634 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10635 again = 1;
10639 /* If we have a THIS (from a primary), we set the context accordingly */
10640 if (name == this_identifier_node)
10642 qual = TREE_CHAIN (qual);
10643 qual_wfl = QUAL_WFL (qual);
10644 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10645 again = 1;
10646 else
10647 name = EXPR_WFL_NODE (qual_wfl);
10648 this_found = 1;
10650 /* If we have a SUPER, we set the context accordingly */
10651 if (name == super_identifier_node)
10653 current_class = CLASSTYPE_SUPER (ptr_type);
10654 /* Check that there is such a thing as a super class. If not,
10655 return. The error will be caught later on, during the
10656 resolution */
10657 if (!current_class)
10659 current_class = saved_current_class;
10660 return;
10662 qual = TREE_CHAIN (qual);
10663 /* Do one more interation to set things up */
10664 super_found = again = 1;
10666 } while (again);
10668 /* If name appears within the scope of a local variable declaration
10669 or parameter declaration, then it is an expression name. We don't
10670 carry this test out if we're in the context of the use of SUPER
10671 or THIS */
10672 if (!this_found && !super_found
10673 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10674 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
10676 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10677 QUAL_RESOLUTION (qual) = decl;
10680 /* If within the class/interface NAME was found to be used there
10681 exists a (possibly inherited) field named NAME, then this is an
10682 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10683 address length, it is OK. */
10684 else if ((decl = lookup_field_wrapper (ptr_type, name))
10685 || (new_array_found && name == length_identifier_node))
10687 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10688 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
10691 /* We reclassify NAME as yielding to a type name resolution if:
10692 - NAME is a class/interface declared within the compilation
10693 unit containing NAME,
10694 - NAME is imported via a single-type-import declaration,
10695 - NAME is declared in an another compilation unit of the package
10696 of the compilation unit containing NAME,
10697 - NAME is declared by exactly on type-import-on-demand declaration
10698 of the compilation unit containing NAME.
10699 - NAME is actually a STRING_CST. */
10700 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10701 || (decl = resolve_and_layout (name, NULL_TREE)))
10703 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10704 QUAL_RESOLUTION (qual) = decl;
10707 /* Method call, array references and cast are expression name */
10708 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
10709 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10710 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
10711 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10713 /* Check here that NAME isn't declared by more than one
10714 type-import-on-demand declaration of the compilation unit
10715 containing NAME. FIXME */
10717 /* Otherwise, NAME is reclassified as a package name */
10718 else
10719 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10721 /* Propagate the qualification accross other components of the
10722 qualified name */
10723 for (qual = TREE_CHAIN (qual); qual;
10724 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10726 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10727 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10728 else
10729 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10732 /* Store the global qualification for the ambiguous part of ID back
10733 into ID fields */
10734 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10735 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10736 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10737 RESOLVE_TYPE_NAME_P (id) = 1;
10738 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10739 RESOLVE_PACKAGE_NAME_P (id) = 1;
10741 /* Restore the current class */
10742 current_class = saved_current_class;
10745 static int
10746 breakdown_qualified (left, right, source)
10747 tree *left, *right, source;
10749 char *p = IDENTIFIER_POINTER (source), *base;
10750 int l = IDENTIFIER_LENGTH (source);
10752 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10753 base = p;
10754 p += (l-1);
10755 while (*p != '.' && p != base)
10756 p--;
10758 /* We didn't find a '.'. Return an error */
10759 if (p == base)
10760 return 1;
10762 *p = '\0';
10763 if (right)
10764 *right = get_identifier (p+1);
10765 *left = get_identifier (IDENTIFIER_POINTER (source));
10766 *p = '.';
10768 return 0;
10771 /* Patch tree nodes in a function body. When a BLOCK is found, push
10772 local variable decls if present.
10773 Same as java_complete_lhs, but does resolve static finals to values. */
10775 static tree
10776 java_complete_tree (node)
10777 tree node;
10779 node = java_complete_lhs (node);
10780 if (JDECL_P (node) && FIELD_STATIC (node) && FIELD_FINAL (node)
10781 && DECL_INITIAL (node) != NULL_TREE
10782 && !flag_emit_xref)
10784 tree value = DECL_INITIAL (node);
10785 DECL_INITIAL (node) = NULL_TREE;
10786 push_obstacks (&permanent_obstack, &permanent_obstack);
10787 value = fold_constant_for_init (value, node);
10788 pop_obstacks ();
10789 DECL_INITIAL (node) = value;
10790 if (value != NULL_TREE)
10792 /* fold_constant_for_init sometimes widen the original type
10793 of the constant (i.e. byte to int.) It's not desirable,
10794 especially if NODE is a function argument. */
10795 if (TREE_CODE (value) == INTEGER_CST
10796 && TREE_TYPE (node) != TREE_TYPE (value))
10797 return convert (TREE_TYPE (node), value);
10798 else
10799 return value;
10802 return node;
10805 static tree
10806 java_stabilize_reference (node)
10807 tree node;
10809 if (TREE_CODE (node) == COMPOUND_EXPR)
10811 tree op0 = TREE_OPERAND (node, 0);
10812 tree op1 = TREE_OPERAND (node, 1);
10813 TREE_OPERAND (node, 0) = save_expr (op0);
10814 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10815 return node;
10817 return stabilize_reference (node);
10820 /* Patch tree nodes in a function body. When a BLOCK is found, push
10821 local variable decls if present.
10822 Same as java_complete_tree, but does not resolve static finals to values. */
10824 static tree
10825 java_complete_lhs (node)
10826 tree node;
10828 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
10829 int flag;
10831 /* CONVERT_EXPR always has its type set, even though it needs to be
10832 worked out. */
10833 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10834 return node;
10836 /* The switch block implements cases processing container nodes
10837 first. Contained nodes are always written back. Leaves come
10838 next and return a value. */
10839 switch (TREE_CODE (node))
10841 case BLOCK:
10843 /* 1- Block section.
10844 Set the local values on decl names so we can identify them
10845 faster when they're referenced. At that stage, identifiers
10846 are legal so we don't check for declaration errors. */
10847 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10849 DECL_CONTEXT (cn) = current_function_decl;
10850 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
10852 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
10853 CAN_COMPLETE_NORMALLY (node) = 1;
10854 else
10856 tree stmt = BLOCK_EXPR_BODY (node);
10857 tree *ptr;
10858 int error_seen = 0;
10859 if (TREE_CODE (stmt) == COMPOUND_EXPR)
10861 /* Re-order from (((A; B); C); ...; Z) to
10862 (A; (B; (C ; (...; Z)))).
10863 This makes it easier to scan the statements left-to-right
10864 without using recursion (which might overflow the stack
10865 if the block has many statements. */
10866 for (;;)
10868 tree left = TREE_OPERAND (stmt, 0);
10869 if (TREE_CODE (left) != COMPOUND_EXPR)
10870 break;
10871 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
10872 TREE_OPERAND (left, 1) = stmt;
10873 stmt = left;
10875 BLOCK_EXPR_BODY (node) = stmt;
10878 /* Now do the actual complete, without deep recursion for
10879 long blocks. */
10880 ptr = &BLOCK_EXPR_BODY (node);
10881 while (TREE_CODE (*ptr) == COMPOUND_EXPR
10882 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
10884 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
10885 tree *next = &TREE_OPERAND (*ptr, 1);
10886 TREE_OPERAND (*ptr, 0) = cur;
10887 if (cur == empty_stmt_node)
10889 /* Optimization; makes it easier to detect empty bodies.
10890 Most useful for <clinit> with all-constant initializer. */
10891 *ptr = *next;
10892 continue;
10894 if (TREE_CODE (cur) == ERROR_MARK)
10895 error_seen++;
10896 else if (! CAN_COMPLETE_NORMALLY (cur))
10898 wfl_op2 = *next;
10899 for (;;)
10901 if (TREE_CODE (wfl_op2) == BLOCK)
10902 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
10903 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
10904 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
10905 else
10906 break;
10908 if (TREE_CODE (wfl_op2) != CASE_EXPR
10909 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
10910 unreachable_stmt_error (*ptr);
10912 ptr = next;
10914 *ptr = java_complete_tree (*ptr);
10916 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
10917 return error_mark_node;
10918 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
10920 /* Turn local bindings to null */
10921 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10922 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
10924 TREE_TYPE (node) = void_type_node;
10925 break;
10927 /* 2- They are expressions but ultimately deal with statements */
10929 case THROW_EXPR:
10930 wfl_op1 = TREE_OPERAND (node, 0);
10931 COMPLETE_CHECK_OP_0 (node);
10932 /* 14.19 A throw statement cannot complete normally. */
10933 CAN_COMPLETE_NORMALLY (node) = 0;
10934 return patch_throw_statement (node, wfl_op1);
10936 case SYNCHRONIZED_EXPR:
10937 wfl_op1 = TREE_OPERAND (node, 0);
10938 return patch_synchronized_statement (node, wfl_op1);
10940 case TRY_EXPR:
10941 return patch_try_statement (node);
10943 case TRY_FINALLY_EXPR:
10944 COMPLETE_CHECK_OP_0 (node);
10945 COMPLETE_CHECK_OP_1 (node);
10946 CAN_COMPLETE_NORMALLY (node)
10947 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
10948 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
10949 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
10950 return node;
10952 case CLEANUP_POINT_EXPR:
10953 COMPLETE_CHECK_OP_0 (node);
10954 TREE_TYPE (node) = void_type_node;
10955 CAN_COMPLETE_NORMALLY (node) =
10956 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
10957 return node;
10959 case WITH_CLEANUP_EXPR:
10960 COMPLETE_CHECK_OP_0 (node);
10961 COMPLETE_CHECK_OP_2 (node);
10962 CAN_COMPLETE_NORMALLY (node) =
10963 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
10964 TREE_TYPE (node) = void_type_node;
10965 return node;
10967 case LABELED_BLOCK_EXPR:
10968 PUSH_LABELED_BLOCK (node);
10969 if (LABELED_BLOCK_BODY (node))
10970 COMPLETE_CHECK_OP_1 (node);
10971 TREE_TYPE (node) = void_type_node;
10972 POP_LABELED_BLOCK ();
10974 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
10976 LABELED_BLOCK_BODY (node) = NULL_TREE;
10977 CAN_COMPLETE_NORMALLY (node) = 1;
10979 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
10980 CAN_COMPLETE_NORMALLY (node) = 1;
10981 return node;
10983 case EXIT_BLOCK_EXPR:
10984 /* We don't complete operand 1, because it's the return value of
10985 the EXIT_BLOCK_EXPR which doesn't exist it Java */
10986 return patch_bc_statement (node);
10988 case CASE_EXPR:
10989 cn = java_complete_tree (TREE_OPERAND (node, 0));
10990 if (cn == error_mark_node)
10991 return cn;
10993 /* First, the case expression must be constant. Values of final
10994 fields are accepted. */
10995 cn = fold (cn);
10996 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
10997 && JDECL_P (TREE_OPERAND (cn, 1))
10998 && FIELD_FINAL (TREE_OPERAND (cn, 1))
10999 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11001 push_obstacks (&permanent_obstack, &permanent_obstack);
11002 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11003 TREE_OPERAND (cn, 1));
11004 pop_obstacks ();
11007 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11009 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11010 parse_error_context (node, "Constant expression required");
11011 return error_mark_node;
11014 nn = ctxp->current_loop;
11016 /* It must be assignable to the type of the switch expression. */
11017 if (!try_builtin_assignconv (NULL_TREE,
11018 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11020 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11021 parse_error_context
11022 (wfl_operator,
11023 "Incompatible type for case. Can't convert `%s' to `int'",
11024 lang_printable_name (TREE_TYPE (cn), 0));
11025 return error_mark_node;
11028 cn = fold (convert (int_type_node, cn));
11030 /* Multiple instance of a case label bearing the same
11031 value is checked during code generation. The case
11032 expression is allright so far. */
11033 if (TREE_CODE (cn) == VAR_DECL)
11034 cn = DECL_INITIAL (cn);
11035 TREE_OPERAND (node, 0) = cn;
11036 TREE_TYPE (node) = void_type_node;
11037 CAN_COMPLETE_NORMALLY (node) = 1;
11038 TREE_SIDE_EFFECTS (node) = 1;
11039 break;
11041 case DEFAULT_EXPR:
11042 nn = ctxp->current_loop;
11043 /* Only one default label is allowed per switch statement */
11044 if (SWITCH_HAS_DEFAULT (nn))
11046 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11047 parse_error_context (wfl_operator,
11048 "Duplicate case label: `default'");
11049 return error_mark_node;
11051 else
11052 SWITCH_HAS_DEFAULT (nn) = 1;
11053 TREE_TYPE (node) = void_type_node;
11054 TREE_SIDE_EFFECTS (node) = 1;
11055 CAN_COMPLETE_NORMALLY (node) = 1;
11056 break;
11058 case SWITCH_EXPR:
11059 case LOOP_EXPR:
11060 PUSH_LOOP (node);
11061 /* Check whether the loop was enclosed in a labeled
11062 statement. If not, create one, insert the loop in it and
11063 return the node */
11064 nn = patch_loop_statement (node);
11066 /* Anyways, walk the body of the loop */
11067 if (TREE_CODE (node) == LOOP_EXPR)
11068 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11069 /* Switch statement: walk the switch expression and the cases */
11070 else
11071 node = patch_switch_statement (node);
11073 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11074 nn = error_mark_node;
11075 else
11077 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11078 /* If we returned something different, that's because we
11079 inserted a label. Pop the label too. */
11080 if (nn != node)
11082 if (CAN_COMPLETE_NORMALLY (node))
11083 CAN_COMPLETE_NORMALLY (nn) = 1;
11084 POP_LABELED_BLOCK ();
11087 POP_LOOP ();
11088 return nn;
11090 case EXIT_EXPR:
11091 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11092 return patch_exit_expr (node);
11094 case COND_EXPR:
11095 /* Condition */
11096 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11097 if (TREE_OPERAND (node, 0) == error_mark_node)
11098 return error_mark_node;
11099 /* then-else branches */
11100 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11101 if (TREE_OPERAND (node, 1) == error_mark_node)
11102 return error_mark_node;
11103 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11104 if (TREE_OPERAND (node, 2) == error_mark_node)
11105 return error_mark_node;
11106 return patch_if_else_statement (node);
11107 break;
11109 case CONDITIONAL_EXPR:
11110 /* Condition */
11111 wfl_op1 = TREE_OPERAND (node, 0);
11112 COMPLETE_CHECK_OP_0 (node);
11113 wfl_op2 = TREE_OPERAND (node, 1);
11114 COMPLETE_CHECK_OP_1 (node);
11115 wfl_op3 = TREE_OPERAND (node, 2);
11116 COMPLETE_CHECK_OP_2 (node);
11117 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11119 /* 3- Expression section */
11120 case COMPOUND_EXPR:
11121 wfl_op2 = TREE_OPERAND (node, 1);
11122 TREE_OPERAND (node, 0) = nn =
11123 java_complete_tree (TREE_OPERAND (node, 0));
11124 if (wfl_op2 == empty_stmt_node)
11125 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11126 else
11128 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11130 /* An unreachable condition in a do-while statement
11131 is *not* (technically) an unreachable statement. */
11132 nn = wfl_op2;
11133 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11134 nn = EXPR_WFL_NODE (nn);
11135 if (TREE_CODE (nn) != EXIT_EXPR)
11137 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11138 parse_error_context (wfl_operator, "Unreachable statement");
11141 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11142 if (TREE_OPERAND (node, 1) == error_mark_node)
11143 return error_mark_node;
11144 CAN_COMPLETE_NORMALLY (node)
11145 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11147 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11148 break;
11150 case RETURN_EXPR:
11151 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11152 return patch_return (node);
11154 case EXPR_WITH_FILE_LOCATION:
11155 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11156 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11158 tree wfl = node;
11159 node = resolve_expression_name (node, NULL);
11160 if (node == error_mark_node)
11161 return node;
11162 /* Keep line number information somewhere were it doesn't
11163 disrupt the completion process. */
11164 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11166 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11167 TREE_OPERAND (node, 1) = wfl;
11169 CAN_COMPLETE_NORMALLY (node) = 1;
11171 else
11173 tree body;
11174 int save_lineno = lineno;
11175 lineno = EXPR_WFL_LINENO (node);
11176 body = java_complete_tree (EXPR_WFL_NODE (node));
11177 lineno = save_lineno;
11178 EXPR_WFL_NODE (node) = body;
11179 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11180 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11181 if (body == empty_stmt_node)
11183 /* Optimization; makes it easier to detect empty bodies. */
11184 return body;
11186 if (body == error_mark_node)
11188 /* Its important for the evaluation of assignment that
11189 this mark on the TREE_TYPE is propagated. */
11190 TREE_TYPE (node) = error_mark_node;
11191 return error_mark_node;
11193 else
11194 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11197 break;
11199 case NEW_ARRAY_EXPR:
11200 /* Patch all the dimensions */
11201 flag = 0;
11202 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11204 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11205 tree dim = convert (int_type_node,
11206 java_complete_tree (TREE_VALUE (cn)));
11207 if (dim == error_mark_node)
11209 flag = 1;
11210 continue;
11212 else
11214 TREE_VALUE (cn) = dim;
11215 /* Setup the location of the current dimension, for
11216 later error report. */
11217 TREE_PURPOSE (cn) =
11218 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11219 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11222 /* They complete the array creation expression, if no errors
11223 were found. */
11224 CAN_COMPLETE_NORMALLY (node) = 1;
11225 return (flag ? error_mark_node
11226 : force_evaluation_order (patch_newarray (node)));
11228 case NEW_ANONYMOUS_ARRAY_EXPR:
11229 /* Create the array type if necessary. */
11230 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11232 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11233 if (!(type = resolve_type_during_patch (type)))
11234 return error_mark_node;
11235 type = build_array_from_name (type, NULL_TREE,
11236 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11237 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11239 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11240 ANONYMOUS_ARRAY_INITIALIZER (node));
11241 if (node == error_mark_node)
11242 return error_mark_node;
11243 CAN_COMPLETE_NORMALLY (node) = 1;
11244 return node;
11246 case NEW_CLASS_EXPR:
11247 case CALL_EXPR:
11248 /* Complete function's argument(s) first */
11249 if (complete_function_arguments (node))
11250 return error_mark_node;
11251 else
11253 tree decl, wfl = TREE_OPERAND (node, 0);
11254 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11256 node = patch_method_invocation (node, NULL_TREE,
11257 NULL_TREE, 0, &decl);
11258 if (node == error_mark_node)
11259 return error_mark_node;
11261 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11262 /* If we call this(...), register signature and positions */
11263 if (in_this)
11264 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11265 tree_cons (wfl, decl,
11266 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11267 CAN_COMPLETE_NORMALLY (node) = 1;
11268 return force_evaluation_order (node);
11271 case MODIFY_EXPR:
11272 /* Save potential wfls */
11273 wfl_op1 = TREE_OPERAND (node, 0);
11274 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11276 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11277 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11278 && DECL_INITIAL (nn) != NULL_TREE)
11280 tree value;
11282 push_obstacks (&permanent_obstack, &permanent_obstack);
11283 value = fold_constant_for_init (nn, nn);
11284 pop_obstacks ();
11286 if (value != NULL_TREE)
11288 tree type = TREE_TYPE (value);
11289 if (JPRIMITIVE_TYPE_P (type) ||
11290 (type == string_ptr_type_node && ! flag_emit_class_files))
11291 return empty_stmt_node;
11293 if (! flag_emit_class_files)
11294 DECL_INITIAL (nn) = NULL_TREE;
11296 wfl_op2 = TREE_OPERAND (node, 1);
11298 if (TREE_OPERAND (node, 0) == error_mark_node)
11299 return error_mark_node;
11301 flag = COMPOUND_ASSIGN_P (wfl_op2);
11302 if (flag)
11304 /* This might break when accessing outer field from inner
11305 class. TESTME, FIXME */
11306 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11308 /* Hand stablize the lhs on both places */
11309 TREE_OPERAND (node, 0) = lvalue;
11310 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11311 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11313 /* 15.25.2.a: Left hand is not an array access. FIXME */
11314 /* Now complete the RHS. We write it back later on. */
11315 nn = java_complete_tree (TREE_OPERAND (node, 1));
11317 if ((cn = patch_string (nn)))
11318 nn = cn;
11320 /* The last part of the rewrite for E1 op= E2 is to have
11321 E1 = (T)(E1 op E2), with T being the type of E1. */
11322 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11323 TREE_TYPE (lvalue), nn));
11325 /* 15.25.2.b: Left hand is an array access. FIXME */
11328 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11329 function to complete this RHS. Note that a NEW_ARRAY_INIT
11330 might have been already fully expanded if created as a result
11331 of processing an anonymous array initializer. We avoid doing
11332 the operation twice by testing whether the node already bears
11333 a type. */
11334 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11335 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11336 TREE_OPERAND (node, 1));
11337 /* Otherwise we simply complete the RHS */
11338 else
11339 nn = java_complete_tree (TREE_OPERAND (node, 1));
11341 if (nn == error_mark_node)
11342 return error_mark_node;
11344 /* Write back the RHS as we evaluated it. */
11345 TREE_OPERAND (node, 1) = nn;
11347 /* In case we're handling = with a String as a RHS, we need to
11348 produce a String out of the RHS (it might still be a
11349 STRING_CST or a StringBuffer at this stage */
11350 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11351 TREE_OPERAND (node, 1) = nn;
11353 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11354 TREE_OPERAND (node, 1))))
11356 /* We return error_mark_node if outer_field_access_fix
11357 detects we write into a final. */
11358 if (nn == error_mark_node)
11359 return error_mark_node;
11360 node = nn;
11362 else
11364 node = patch_assignment (node, wfl_op1, wfl_op2);
11365 /* Reorganize the tree if necessary. */
11366 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11367 || JSTRING_P (TREE_TYPE (node))))
11368 node = java_refold (node);
11371 CAN_COMPLETE_NORMALLY (node) = 1;
11372 return node;
11374 case MULT_EXPR:
11375 case PLUS_EXPR:
11376 case MINUS_EXPR:
11377 case LSHIFT_EXPR:
11378 case RSHIFT_EXPR:
11379 case URSHIFT_EXPR:
11380 case BIT_AND_EXPR:
11381 case BIT_XOR_EXPR:
11382 case BIT_IOR_EXPR:
11383 case TRUNC_MOD_EXPR:
11384 case TRUNC_DIV_EXPR:
11385 case RDIV_EXPR:
11386 case TRUTH_ANDIF_EXPR:
11387 case TRUTH_ORIF_EXPR:
11388 case EQ_EXPR:
11389 case NE_EXPR:
11390 case GT_EXPR:
11391 case GE_EXPR:
11392 case LT_EXPR:
11393 case LE_EXPR:
11394 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11395 knows how to handle those cases. */
11396 wfl_op1 = TREE_OPERAND (node, 0);
11397 wfl_op2 = TREE_OPERAND (node, 1);
11399 CAN_COMPLETE_NORMALLY (node) = 1;
11400 /* Don't complete string nodes if dealing with the PLUS operand. */
11401 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11403 nn = java_complete_tree (wfl_op1);
11404 if (nn == error_mark_node)
11405 return error_mark_node;
11407 TREE_OPERAND (node, 0) = nn;
11409 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11411 nn = java_complete_tree (wfl_op2);
11412 if (nn == error_mark_node)
11413 return error_mark_node;
11415 TREE_OPERAND (node, 1) = nn;
11417 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11419 case INSTANCEOF_EXPR:
11420 wfl_op1 = TREE_OPERAND (node, 0);
11421 COMPLETE_CHECK_OP_0 (node);
11422 if (flag_emit_xref)
11424 TREE_TYPE (node) = boolean_type_node;
11425 return node;
11427 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11429 case UNARY_PLUS_EXPR:
11430 case NEGATE_EXPR:
11431 case TRUTH_NOT_EXPR:
11432 case BIT_NOT_EXPR:
11433 case PREDECREMENT_EXPR:
11434 case PREINCREMENT_EXPR:
11435 case POSTDECREMENT_EXPR:
11436 case POSTINCREMENT_EXPR:
11437 case CONVERT_EXPR:
11438 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11439 how to handle those cases. */
11440 wfl_op1 = TREE_OPERAND (node, 0);
11441 CAN_COMPLETE_NORMALLY (node) = 1;
11442 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11443 if (TREE_OPERAND (node, 0) == error_mark_node)
11444 return error_mark_node;
11445 node = patch_unaryop (node, wfl_op1);
11446 CAN_COMPLETE_NORMALLY (node) = 1;
11447 break;
11449 case ARRAY_REF:
11450 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11451 how to handle those cases. */
11452 wfl_op1 = TREE_OPERAND (node, 0);
11453 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11454 if (TREE_OPERAND (node, 0) == error_mark_node)
11455 return error_mark_node;
11456 if (!flag_emit_class_files && !flag_emit_xref)
11457 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
11458 /* The same applies to wfl_op2 */
11459 wfl_op2 = TREE_OPERAND (node, 1);
11460 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11461 if (TREE_OPERAND (node, 1) == error_mark_node)
11462 return error_mark_node;
11463 if (!flag_emit_class_files && !flag_emit_xref)
11464 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
11465 return patch_array_ref (node);
11467 case RECORD_TYPE:
11468 return node;;
11470 case COMPONENT_REF:
11471 /* The first step in the re-write of qualified name handling. FIXME.
11472 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
11473 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11474 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11476 tree name = TREE_OPERAND (node, 1);
11477 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11478 if (field == NULL_TREE)
11480 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11481 return error_mark_node;
11483 if (! FIELD_STATIC (field))
11485 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11486 return error_mark_node;
11488 return field;
11490 else
11491 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
11492 break;
11494 case THIS_EXPR:
11495 /* Can't use THIS in a static environment */
11496 if (!current_this)
11498 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11499 parse_error_context (wfl_operator,
11500 "Keyword `this' used outside allowed context");
11501 TREE_TYPE (node) = error_mark_node;
11502 return error_mark_node;
11504 if (ctxp->explicit_constructor_p)
11506 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11507 parse_error_context
11508 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
11509 TREE_TYPE (node) = error_mark_node;
11510 return error_mark_node;
11512 return current_this;
11514 case CLASS_LITERAL:
11515 CAN_COMPLETE_NORMALLY (node) = 1;
11516 node = patch_incomplete_class_ref (node);
11517 if (node == error_mark_node)
11518 return error_mark_node;
11519 break;
11521 case INSTANCE_INITIALIZERS_EXPR:
11522 in_instance_initializer++;
11523 node = java_complete_tree (TREE_OPERAND (node, 0));
11524 in_instance_initializer--;
11525 if (node != error_mark_node)
11526 TREE_TYPE (node) = void_type_node;
11527 else
11528 return error_mark_node;
11529 break;
11531 default:
11532 CAN_COMPLETE_NORMALLY (node) = 1;
11533 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
11534 and it's time to turn it into the appropriate String object */
11535 if ((nn = patch_string (node)))
11536 node = nn;
11537 else
11538 fatal ("No case for tree code `%s' - java_complete_tree\n",
11539 tree_code_name [TREE_CODE (node)]);
11541 return node;
11544 /* Complete function call's argument. Return a non zero value is an
11545 error was found. */
11547 static int
11548 complete_function_arguments (node)
11549 tree node;
11551 int flag = 0;
11552 tree cn;
11554 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11555 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11557 tree wfl = TREE_VALUE (cn), parm, temp;
11558 parm = java_complete_tree (wfl);
11560 if (parm == error_mark_node)
11562 flag = 1;
11563 continue;
11565 /* If have a string literal that we haven't transformed yet or a
11566 crafted string buffer, as a result of use of the the String
11567 `+' operator. Build `parm.toString()' and expand it. */
11568 if ((temp = patch_string (parm)))
11569 parm = temp;
11570 /* Inline PRIMTYPE.TYPE read access */
11571 parm = maybe_build_primttype_type_ref (parm, wfl);
11573 TREE_VALUE (cn) = parm;
11575 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11576 return flag;
11579 /* Sometimes (for loops and variable initialized during their
11580 declaration), we want to wrap a statement around a WFL and turn it
11581 debugable. */
11583 static tree
11584 build_debugable_stmt (location, stmt)
11585 int location;
11586 tree stmt;
11588 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11590 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11591 EXPR_WFL_LINECOL (stmt) = location;
11593 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11594 return stmt;
11597 static tree
11598 build_expr_block (body, decls)
11599 tree body, decls;
11601 tree node = make_node (BLOCK);
11602 BLOCK_EXPR_DECLS (node) = decls;
11603 BLOCK_EXPR_BODY (node) = body;
11604 if (body)
11605 TREE_TYPE (node) = TREE_TYPE (body);
11606 TREE_SIDE_EFFECTS (node) = 1;
11607 return node;
11610 /* Create a new function block and link it approriately to current
11611 function block chain */
11613 static tree
11614 enter_block ()
11616 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11619 /* Link block B supercontext to the previous block. The current
11620 function DECL is used as supercontext when enter_a_block is called
11621 for the first time for a given function. The current function body
11622 (DECL_FUNCTION_BODY) is set to be block B. */
11624 static tree
11625 enter_a_block (b)
11626 tree b;
11628 tree fndecl = current_function_decl;
11630 if (!fndecl) {
11631 BLOCK_SUPERCONTEXT (b) = current_static_block;
11632 current_static_block = b;
11635 else if (!DECL_FUNCTION_BODY (fndecl))
11637 BLOCK_SUPERCONTEXT (b) = fndecl;
11638 DECL_FUNCTION_BODY (fndecl) = b;
11640 else
11642 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11643 DECL_FUNCTION_BODY (fndecl) = b;
11645 return b;
11648 /* Exit a block by changing the current function body
11649 (DECL_FUNCTION_BODY) to the current block super context, only if
11650 the block being exited isn't the method's top level one. */
11652 static tree
11653 exit_block ()
11655 tree b;
11656 if (current_function_decl)
11658 b = DECL_FUNCTION_BODY (current_function_decl);
11659 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11660 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11662 else
11664 b = current_static_block;
11666 if (BLOCK_SUPERCONTEXT (b))
11667 current_static_block = BLOCK_SUPERCONTEXT (b);
11669 return b;
11672 /* Lookup for NAME in the nested function's blocks, all the way up to
11673 the current toplevel one. It complies with Java's local variable
11674 scoping rules. */
11676 static tree
11677 lookup_name_in_blocks (name)
11678 tree name;
11680 tree b = GET_CURRENT_BLOCK (current_function_decl);
11682 while (b != current_function_decl)
11684 tree current;
11686 /* Paranoid sanity check. To be removed */
11687 if (TREE_CODE (b) != BLOCK)
11688 fatal ("non block expr function body - lookup_name_in_blocks");
11690 for (current = BLOCK_EXPR_DECLS (b); current;
11691 current = TREE_CHAIN (current))
11692 if (DECL_NAME (current) == name)
11693 return current;
11694 b = BLOCK_SUPERCONTEXT (b);
11696 return NULL_TREE;
11699 static void
11700 maybe_absorb_scoping_blocks ()
11702 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
11704 tree b = exit_block ();
11705 java_method_add_stmt (current_function_decl, b);
11706 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11711 /* This section of the source is reserved to build_* functions that
11712 are building incomplete tree nodes and the patch_* functions that
11713 are completing them. */
11715 /* Wrap a non WFL node around a WFL. */
11716 static tree
11717 build_wfl_wrap (node, location)
11718 tree node;
11719 int location;
11721 tree wfl, node_to_insert = node;
11723 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11724 with the way we're processing SUPER. A THIS from a primary as a
11725 different form than a SUPER. Turn THIS into something symbolic */
11726 if (TREE_CODE (node) == THIS_EXPR)
11727 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11728 else
11729 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11731 EXPR_WFL_LINECOL (wfl) = location;
11732 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11733 return wfl;
11737 /* Build a super() constructor invocation. Returns empty_stmt_node if
11738 we're currently dealing with the class java.lang.Object. */
11740 static tree
11741 build_super_invocation (mdecl)
11742 tree mdecl;
11744 if (DECL_CONTEXT (mdecl) == object_type_node)
11745 return empty_stmt_node;
11746 else
11748 tree super_wfl = build_wfl_node (super_identifier_node);
11749 tree a = NULL_TREE, t;
11750 /* If we're dealing with an anonymous class, pass the arguments
11751 of the crafted constructor along. */
11752 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11754 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11755 for (; t != end_params_node; t = TREE_CHAIN (t))
11756 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11758 return build_method_invocation (super_wfl, a);
11762 /* Build a SUPER/THIS qualified method invocation. */
11764 static tree
11765 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11766 int use_this;
11767 tree name, args;
11768 int lloc, rloc;
11770 tree invok;
11771 tree wfl =
11772 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
11773 EXPR_WFL_LINECOL (wfl) = lloc;
11774 invok = build_method_invocation (name, args);
11775 return make_qualified_primary (wfl, invok, rloc);
11778 /* Build an incomplete CALL_EXPR node. */
11780 static tree
11781 build_method_invocation (name, args)
11782 tree name;
11783 tree args;
11785 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11786 TREE_SIDE_EFFECTS (call) = 1;
11787 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11788 return call;
11791 /* Build an incomplete new xxx(...) node. */
11793 static tree
11794 build_new_invocation (name, args)
11795 tree name, args;
11797 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11798 TREE_SIDE_EFFECTS (call) = 1;
11799 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11800 return call;
11803 /* Build an incomplete assignment expression. */
11805 static tree
11806 build_assignment (op, op_location, lhs, rhs)
11807 int op, op_location;
11808 tree lhs, rhs;
11810 tree assignment;
11811 /* Build the corresponding binop if we deal with a Compound
11812 Assignment operator. Mark the binop sub-tree as part of a
11813 Compound Assignment expression */
11814 if (op != ASSIGN_TK)
11816 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11817 COMPOUND_ASSIGN_P (rhs) = 1;
11819 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11820 TREE_SIDE_EFFECTS (assignment) = 1;
11821 EXPR_WFL_LINECOL (assignment) = op_location;
11822 return assignment;
11825 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11827 char *
11828 print_int_node (node)
11829 tree node;
11831 static char buffer [80];
11832 if (TREE_CONSTANT_OVERFLOW (node))
11833 sprintf (buffer, "<overflow>");
11835 if (TREE_INT_CST_HIGH (node) == 0)
11836 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11837 TREE_INT_CST_LOW (node));
11838 else if (TREE_INT_CST_HIGH (node) == -1
11839 && TREE_INT_CST_LOW (node) != 0)
11841 buffer [0] = '-';
11842 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
11843 -TREE_INT_CST_LOW (node));
11845 else
11846 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
11847 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
11849 return buffer;
11852 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
11853 context. */
11855 static int
11856 check_final_assignment (lvalue, wfl)
11857 tree lvalue, wfl;
11859 if (TREE_CODE (lvalue) == COMPOUND_EXPR
11860 && JDECL_P (TREE_OPERAND (lvalue, 1)))
11861 lvalue = TREE_OPERAND (lvalue, 1);
11863 /* When generating class files, references to the `length' field
11864 look a bit different. */
11865 if ((flag_emit_class_files
11866 && TREE_CODE (lvalue) == COMPONENT_REF
11867 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
11868 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
11869 || (TREE_CODE (lvalue) == FIELD_DECL
11870 && FIELD_FINAL (lvalue)
11871 && !DECL_CLINIT_P (current_function_decl)
11872 && !DECL_FINIT_P (current_function_decl)))
11874 parse_error_context
11875 (wfl, "Can't assign a value to the final variable `%s'",
11876 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
11877 return 1;
11879 return 0;
11882 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
11883 read. This is needed to avoid circularities in the implementation
11884 of these fields in libjava. */
11886 static tree
11887 maybe_build_primttype_type_ref (rhs, wfl)
11888 tree rhs, wfl;
11890 tree to_return = NULL_TREE;
11891 tree rhs_type = TREE_TYPE (rhs);
11892 if (TREE_CODE (rhs) == COMPOUND_EXPR)
11894 tree n = TREE_OPERAND (rhs, 1);
11895 if (TREE_CODE (n) == VAR_DECL
11896 && DECL_NAME (n) == TYPE_identifier_node
11897 && rhs_type == class_ptr_type
11898 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
11900 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
11901 if (!strncmp (self_name, "java.lang.", 10))
11902 to_return = build_primtype_type_ref (self_name);
11905 return (to_return ? to_return : rhs );
11908 /* 15.25 Assignment operators. */
11910 static tree
11911 patch_assignment (node, wfl_op1, wfl_op2)
11912 tree node;
11913 tree wfl_op1;
11914 tree wfl_op2;
11916 tree rhs = TREE_OPERAND (node, 1);
11917 tree lvalue = TREE_OPERAND (node, 0), llvalue;
11918 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
11919 int error_found = 0;
11920 int lvalue_from_array = 0;
11922 /* Can't assign to a (blank) final. */
11923 if (check_final_assignment (lvalue, wfl_op1))
11924 error_found = 1;
11926 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11928 /* Lhs can be a named variable */
11929 if (JDECL_P (lvalue))
11931 lhs_type = TREE_TYPE (lvalue);
11933 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
11934 comment on reason why */
11935 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
11937 lhs_type = TREE_TYPE (lvalue);
11938 lvalue_from_array = 1;
11940 /* Or a field access */
11941 else if (TREE_CODE (lvalue) == COMPONENT_REF)
11942 lhs_type = TREE_TYPE (lvalue);
11943 /* Or a function return slot */
11944 else if (TREE_CODE (lvalue) == RESULT_DECL)
11945 lhs_type = TREE_TYPE (lvalue);
11946 /* Otherwise, we might want to try to write into an optimized static
11947 final, this is an of a different nature, reported further on. */
11948 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
11949 && resolve_expression_name (wfl_op1, &llvalue))
11951 if (!error_found && check_final_assignment (llvalue, wfl_op1))
11953 /* What we should do instead is resetting the all the flags
11954 previously set, exchange lvalue for llvalue and continue. */
11955 error_found = 1;
11956 return error_mark_node;
11958 else
11959 lhs_type = TREE_TYPE (lvalue);
11961 else
11963 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
11964 error_found = 1;
11967 rhs_type = TREE_TYPE (rhs);
11968 /* 5.1 Try the assignment conversion for builtin type. */
11969 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
11971 /* 5.2 If it failed, try a reference conversion */
11972 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
11973 lhs_type = promote_type (rhs_type);
11975 /* 15.25.2 If we have a compound assignment, convert RHS into the
11976 type of the LHS */
11977 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11978 new_rhs = convert (lhs_type, rhs);
11980 /* Explicit cast required. This is an error */
11981 if (!new_rhs)
11983 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
11984 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
11985 tree wfl;
11986 char operation [32]; /* Max size known */
11988 /* If the assignment is part of a declaration, we use the WFL of
11989 the declared variable to point out the error and call it a
11990 declaration problem. If the assignment is a genuine =
11991 operator, we call is a operator `=' problem, otherwise we
11992 call it an assignment problem. In both of these last cases,
11993 we use the WFL of the operator to indicate the error. */
11995 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
11997 wfl = wfl_op1;
11998 strcpy (operation, "declaration");
12000 else
12002 wfl = wfl_operator;
12003 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12004 strcpy (operation, "assignment");
12005 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12006 strcpy (operation, "`return'");
12007 else
12008 strcpy (operation, "`='");
12011 if (!valid_cast_to_p (rhs_type, lhs_type))
12012 parse_error_context
12013 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12014 operation, t1, t2);
12015 else
12016 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12017 operation, t1, t2);
12018 free (t1); free (t2);
12019 error_found = 1;
12022 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12023 if (new_rhs)
12024 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
12026 if (error_found)
12027 return error_mark_node;
12029 /* 10.10: Array Store Exception runtime check */
12030 if (!flag_emit_class_files
12031 && !flag_emit_xref
12032 && lvalue_from_array
12033 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12035 tree check;
12036 tree base = lvalue;
12038 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12039 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12040 base = TREE_OPERAND (lvalue, 0);
12041 else
12043 if (flag_bounds_check)
12044 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12045 else
12046 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12049 /* Build the invocation of _Jv_CheckArrayStore */
12050 new_rhs = save_expr (new_rhs);
12051 check = build (CALL_EXPR, void_type_node,
12052 build_address_of (soft_checkarraystore_node),
12053 tree_cons (NULL_TREE, base,
12054 build_tree_list (NULL_TREE, new_rhs)),
12055 NULL_TREE);
12056 TREE_SIDE_EFFECTS (check) = 1;
12058 /* We have to decide on an insertion point */
12059 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12061 tree t;
12062 if (flag_bounds_check)
12064 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12065 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12066 build (COMPOUND_EXPR, void_type_node, t, check);
12068 else
12069 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12070 check, TREE_OPERAND (lvalue, 1));
12072 else
12074 /* Make sure the bound check will happen before the store check */
12075 if (flag_bounds_check)
12076 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12077 build (COMPOUND_EXPR, void_type_node,
12078 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12079 else
12080 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12084 /* Final locals can be used as case values in switch
12085 statement. Prepare them for this eventuality. */
12086 if (TREE_CODE (lvalue) == VAR_DECL
12087 && LOCAL_FINAL (lvalue)
12088 && TREE_CONSTANT (new_rhs)
12089 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12090 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12093 TREE_CONSTANT (lvalue) = 1;
12094 DECL_INITIAL (lvalue) = new_rhs;
12097 TREE_OPERAND (node, 0) = lvalue;
12098 TREE_OPERAND (node, 1) = new_rhs;
12099 TREE_TYPE (node) = lhs_type;
12100 return node;
12103 /* Check that type SOURCE can be cast into type DEST. If the cast
12104 can't occur at all, return 0 otherwise 1. This function is used to
12105 produce accurate error messages on the reasons why an assignment
12106 failed. */
12108 static tree
12109 try_reference_assignconv (lhs_type, rhs)
12110 tree lhs_type, rhs;
12112 tree new_rhs = NULL_TREE;
12113 tree rhs_type = TREE_TYPE (rhs);
12115 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12117 /* `null' may be assigned to any reference type */
12118 if (rhs == null_pointer_node)
12119 new_rhs = null_pointer_node;
12120 /* Try the reference assignment conversion */
12121 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12122 new_rhs = rhs;
12123 /* This is a magic assignment that we process differently */
12124 else if (rhs == soft_exceptioninfo_call_node)
12125 new_rhs = rhs;
12127 return new_rhs;
12130 /* Check that RHS can be converted into LHS_TYPE by the assignment
12131 conversion (5.2), for the cases of RHS being a builtin type. Return
12132 NULL_TREE if the conversion fails or if because RHS isn't of a
12133 builtin type. Return a converted RHS if the conversion is possible. */
12135 static tree
12136 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12137 tree wfl_op1, lhs_type, rhs;
12139 tree new_rhs = NULL_TREE;
12140 tree rhs_type = TREE_TYPE (rhs);
12142 /* Zero accepted everywhere */
12143 if (TREE_CODE (rhs) == INTEGER_CST
12144 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12145 && JPRIMITIVE_TYPE_P (rhs_type))
12146 new_rhs = convert (lhs_type, rhs);
12148 /* 5.1.1 Try Identity Conversion,
12149 5.1.2 Try Widening Primitive Conversion */
12150 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12151 new_rhs = convert (lhs_type, rhs);
12153 /* Try a narrowing primitive conversion (5.1.3):
12154 - expression is a constant expression of type int AND
12155 - variable is byte, short or char AND
12156 - The value of the expression is representable in the type of the
12157 variable */
12158 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12159 && (lhs_type == byte_type_node || lhs_type == char_type_node
12160 || lhs_type == short_type_node))
12162 if (int_fits_type_p (rhs, lhs_type))
12163 new_rhs = convert (lhs_type, rhs);
12164 else if (wfl_op1) /* Might be called with a NULL */
12165 parse_warning_context
12166 (wfl_op1, "Constant expression `%s' to wide for narrowing primitive conversion to `%s'",
12167 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12168 /* Reported a warning that will turn into an error further
12169 down, so we don't return */
12172 return new_rhs;
12175 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12176 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12177 0 is the conversion test fails. This implements parts the method
12178 invocation convertion (5.3). */
12180 static int
12181 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12182 tree lhs_type, rhs_type;
12184 /* 5.1.1: This is the identity conversion part. */
12185 if (lhs_type == rhs_type)
12186 return 1;
12188 /* Reject non primitive types */
12189 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
12190 return 0;
12192 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12193 than a char can't be converted into a char. Short can't too, but
12194 the < test below takes care of that */
12195 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12196 return 0;
12198 /* Accept all promoted type here. Note, we can't use <= in the test
12199 below, because we still need to bounce out assignments of short
12200 to char and the likes */
12201 if (lhs_type == int_type_node
12202 && (rhs_type == promoted_byte_type_node
12203 || rhs_type == promoted_short_type_node
12204 || rhs_type == promoted_char_type_node
12205 || rhs_type == promoted_boolean_type_node))
12206 return 1;
12208 /* From here, an integral is widened if its precision is smaller
12209 than the precision of the LHS or if the LHS is a floating point
12210 type, or the RHS is a float and the RHS a double. */
12211 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12212 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12213 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12214 || (rhs_type == float_type_node && lhs_type == double_type_node))
12215 return 1;
12217 return 0;
12220 /* Check that something of SOURCE type can be assigned or cast to
12221 something of DEST type at runtime. Return 1 if the operation is
12222 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12223 were SOURCE is cast into DEST, which borrows a lot of the
12224 assignment check. */
12226 static int
12227 valid_ref_assignconv_cast_p (source, dest, cast)
12228 tree source;
12229 tree dest;
12230 int cast;
12232 /* SOURCE or DEST might be null if not from a declared entity. */
12233 if (!source || !dest)
12234 return 0;
12235 if (JNULLP_TYPE_P (source))
12236 return 1;
12237 if (TREE_CODE (source) == POINTER_TYPE)
12238 source = TREE_TYPE (source);
12239 if (TREE_CODE (dest) == POINTER_TYPE)
12240 dest = TREE_TYPE (dest);
12241 /* Case where SOURCE is a class type */
12242 if (TYPE_CLASS_P (source))
12244 if (TYPE_CLASS_P (dest))
12245 return (source == dest
12246 || inherits_from_p (source, dest)
12247 || (cast && inherits_from_p (dest, source)));
12248 if (TYPE_INTERFACE_P (dest))
12250 /* If doing a cast and SOURCE is final, the operation is
12251 always correct a compile time (because even if SOURCE
12252 does not implement DEST, a subclass of SOURCE might). */
12253 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12254 return 1;
12255 /* Otherwise, SOURCE must implement DEST */
12256 return interface_of_p (dest, source);
12258 /* DEST is an array, cast permited if SOURCE is of Object type */
12259 return (cast && source == object_type_node ? 1 : 0);
12261 if (TYPE_INTERFACE_P (source))
12263 if (TYPE_CLASS_P (dest))
12265 /* If not casting, DEST must be the Object type */
12266 if (!cast)
12267 return dest == object_type_node;
12268 /* We're doing a cast. The cast is always valid is class
12269 DEST is not final, otherwise, DEST must implement SOURCE */
12270 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12271 return 1;
12272 else
12273 return interface_of_p (source, dest);
12275 if (TYPE_INTERFACE_P (dest))
12277 /* If doing a cast, then if SOURCE and DEST contain method
12278 with the same signature but different return type, then
12279 this is a (compile time) error */
12280 if (cast)
12282 tree method_source, method_dest;
12283 tree source_type;
12284 tree source_sig;
12285 tree source_name;
12286 for (method_source = TYPE_METHODS (source); method_source;
12287 method_source = TREE_CHAIN (method_source))
12289 source_sig =
12290 build_java_argument_signature (TREE_TYPE (method_source));
12291 source_type = TREE_TYPE (TREE_TYPE (method_source));
12292 source_name = DECL_NAME (method_source);
12293 for (method_dest = TYPE_METHODS (dest);
12294 method_dest; method_dest = TREE_CHAIN (method_dest))
12295 if (source_sig ==
12296 build_java_argument_signature (TREE_TYPE (method_dest))
12297 && source_name == DECL_NAME (method_dest)
12298 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12299 return 0;
12301 return 1;
12303 else
12304 return source == dest || interface_of_p (dest, source);
12306 else /* Array */
12307 return (cast ?
12308 (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable) : 0);
12310 if (TYPE_ARRAY_P (source))
12312 if (TYPE_CLASS_P (dest))
12313 return dest == object_type_node;
12314 /* Can't cast an array to an interface unless the interface is
12315 java.lang.Cloneable */
12316 if (TYPE_INTERFACE_P (dest))
12317 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable ? 1 : 0);
12318 else /* Arrays */
12320 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12321 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12323 /* In case of severe errors, they turn out null */
12324 if (!dest_element_type || !source_element_type)
12325 return 0;
12326 if (source_element_type == dest_element_type)
12327 return 1;
12328 return valid_ref_assignconv_cast_p (source_element_type,
12329 dest_element_type, cast);
12331 return 0;
12333 return 0;
12336 static int
12337 valid_cast_to_p (source, dest)
12338 tree source;
12339 tree dest;
12341 if (TREE_CODE (source) == POINTER_TYPE)
12342 source = TREE_TYPE (source);
12343 if (TREE_CODE (dest) == POINTER_TYPE)
12344 dest = TREE_TYPE (dest);
12346 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12347 return valid_ref_assignconv_cast_p (source, dest, 1);
12349 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12350 return 1;
12352 return 0;
12355 /* Method invocation conversion test. Return 1 if type SOURCE can be
12356 converted to type DEST through the methond invocation conversion
12357 process (5.3) */
12359 static tree
12360 do_unary_numeric_promotion (arg)
12361 tree arg;
12363 tree type = TREE_TYPE (arg);
12364 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
12365 : TREE_CODE (type) == CHAR_TYPE)
12366 arg = convert (int_type_node, arg);
12367 return arg;
12370 /* Return a non zero value if SOURCE can be converted into DEST using
12371 the method invocation conversion rule (5.3). */
12372 static int
12373 valid_method_invocation_conversion_p (dest, source)
12374 tree dest, source;
12376 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12377 && valid_builtin_assignconv_identity_widening_p (dest, source))
12378 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12379 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12380 && valid_ref_assignconv_cast_p (source, dest, 0)));
12383 /* Build an incomplete binop expression. */
12385 static tree
12386 build_binop (op, op_location, op1, op2)
12387 enum tree_code op;
12388 int op_location;
12389 tree op1, op2;
12391 tree binop = build (op, NULL_TREE, op1, op2);
12392 TREE_SIDE_EFFECTS (binop) = 1;
12393 /* Store the location of the operator, for better error report. The
12394 string of the operator will be rebuild based on the OP value. */
12395 EXPR_WFL_LINECOL (binop) = op_location;
12396 return binop;
12399 /* Build the string of the operator retained by NODE. If NODE is part
12400 of a compound expression, add an '=' at the end of the string. This
12401 function is called when an error needs to be reported on an
12402 operator. The string is returned as a pointer to a static character
12403 buffer. */
12405 static char *
12406 operator_string (node)
12407 tree node;
12409 #define BUILD_OPERATOR_STRING(S) \
12411 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12412 return buffer; \
12415 static char buffer [10];
12416 switch (TREE_CODE (node))
12418 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12419 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12420 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12421 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12422 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12423 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12424 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12425 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12426 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12427 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12428 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12429 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12430 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12431 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12432 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12433 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12434 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12435 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12436 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
12437 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12438 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12439 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12440 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12441 case PREINCREMENT_EXPR: /* Fall through */
12442 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12443 case PREDECREMENT_EXPR: /* Fall through */
12444 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12445 default:
12446 fatal ("unregistered operator %s - operator_string",
12447 tree_code_name [TREE_CODE (node)]);
12449 return NULL;
12450 #undef BUILD_OPERATOR_STRING
12453 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12455 static int
12456 java_decl_equiv (var_acc1, var_acc2)
12457 tree var_acc1, var_acc2;
12459 if (JDECL_P (var_acc1))
12460 return (var_acc1 == var_acc2);
12462 return (TREE_CODE (var_acc1) == COMPONENT_REF
12463 && TREE_CODE (var_acc2) == COMPONENT_REF
12464 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12465 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12466 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12469 /* Return a non zero value if CODE is one of the operators that can be
12470 used in conjunction with the `=' operator in a compound assignment. */
12472 static int
12473 binop_compound_p (code)
12474 enum tree_code code;
12476 int i;
12477 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12478 if (binop_lookup [i] == code)
12479 break;
12481 return i < BINOP_COMPOUND_CANDIDATES;
12484 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12486 static tree
12487 java_refold (t)
12488 tree t;
12490 tree c, b, ns, decl;
12492 if (TREE_CODE (t) != MODIFY_EXPR)
12493 return t;
12495 c = TREE_OPERAND (t, 1);
12496 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12497 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12498 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12499 return t;
12501 /* Now the left branch of the binary operator. */
12502 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12503 if (! (b && TREE_CODE (b) == NOP_EXPR
12504 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12505 return t;
12507 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12508 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12509 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12510 return t;
12512 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12513 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12514 /* It's got to be the an equivalent decl */
12515 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12517 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12518 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12519 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12520 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12521 /* Change the right part of the BINOP_EXPR */
12522 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12525 return t;
12528 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
12529 errors but we modify NODE so that it contains the type computed
12530 according to the expression, when it's fixed. Otherwise, we write
12531 error_mark_node as the type. It allows us to further the analysis
12532 of remaining nodes and detects more errors in certain cases. */
12534 static tree
12535 patch_binop (node, wfl_op1, wfl_op2)
12536 tree node;
12537 tree wfl_op1;
12538 tree wfl_op2;
12540 tree op1 = TREE_OPERAND (node, 0);
12541 tree op2 = TREE_OPERAND (node, 1);
12542 tree op1_type = TREE_TYPE (op1);
12543 tree op2_type = TREE_TYPE (op2);
12544 tree prom_type = NULL_TREE, cn;
12545 int code = TREE_CODE (node);
12547 /* If 1, tell the routine that we have to return error_mark_node
12548 after checking for the initialization of the RHS */
12549 int error_found = 0;
12551 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12553 switch (code)
12555 /* 15.16 Multiplicative operators */
12556 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12557 case RDIV_EXPR: /* 15.16.2 Division Operator / */
12558 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
12559 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
12560 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12562 if (!JPRIMITIVE_TYPE_P (op1_type))
12563 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12564 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12565 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12566 TREE_TYPE (node) = error_mark_node;
12567 error_found = 1;
12568 break;
12570 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12571 /* Change the division operator if necessary */
12572 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12573 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
12575 if (TREE_CODE (prom_type) == INTEGER_TYPE
12576 && flag_use_divide_subroutine
12577 && ! flag_emit_class_files
12578 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12579 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12581 /* This one is more complicated. FLOATs are processed by a
12582 function call to soft_fmod. Duplicate the value of the
12583 COMPOUND_ASSIGN_P flag. */
12584 if (code == TRUNC_MOD_EXPR)
12586 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12587 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
12588 TREE_SIDE_EFFECTS (mod)
12589 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12590 return mod;
12592 break;
12594 /* 15.17 Additive Operators */
12595 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
12597 /* Operation is valid if either one argument is a string
12598 constant, a String object or a StringBuffer crafted for the
12599 purpose of the a previous usage of the String concatenation
12600 operator */
12602 if (TREE_CODE (op1) == STRING_CST
12603 || TREE_CODE (op2) == STRING_CST
12604 || JSTRING_TYPE_P (op1_type)
12605 || JSTRING_TYPE_P (op2_type)
12606 || IS_CRAFTED_STRING_BUFFER_P (op1)
12607 || IS_CRAFTED_STRING_BUFFER_P (op2))
12608 return build_string_concatenation (op1, op2);
12610 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12611 Numeric Types */
12612 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12614 if (!JPRIMITIVE_TYPE_P (op1_type))
12615 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12616 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12617 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12618 TREE_TYPE (node) = error_mark_node;
12619 error_found = 1;
12620 break;
12622 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12623 break;
12625 /* 15.18 Shift Operators */
12626 case LSHIFT_EXPR:
12627 case RSHIFT_EXPR:
12628 case URSHIFT_EXPR:
12629 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12631 if (!JINTEGRAL_TYPE_P (op1_type))
12632 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12633 else
12635 if (JPRIMITIVE_TYPE_P (op2_type))
12636 parse_error_context (wfl_operator,
12637 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
12638 operator_string (node),
12639 lang_printable_name (op2_type, 0));
12640 else
12641 parse_error_context (wfl_operator,
12642 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
12643 operator_string (node),
12644 lang_printable_name (op2_type, 0));
12646 TREE_TYPE (node) = error_mark_node;
12647 error_found = 1;
12648 break;
12651 /* Unary numeric promotion (5.6.1) is performed on each operand
12652 separatly */
12653 op1 = do_unary_numeric_promotion (op1);
12654 op2 = do_unary_numeric_promotion (op2);
12656 /* The type of the shift expression is the type of the promoted
12657 type of the left-hand operand */
12658 prom_type = TREE_TYPE (op1);
12660 /* Shift int only up to 0x1f and long up to 0x3f */
12661 if (prom_type == int_type_node)
12662 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12663 build_int_2 (0x1f, 0)));
12664 else
12665 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12666 build_int_2 (0x3f, 0)));
12668 /* The >>> operator is a >> operating on unsigned quantities */
12669 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
12671 tree to_return;
12672 tree utype = unsigned_type (prom_type);
12673 op1 = convert (utype, op1);
12674 TREE_SET_CODE (node, RSHIFT_EXPR);
12675 TREE_OPERAND (node, 0) = op1;
12676 TREE_OPERAND (node, 1) = op2;
12677 TREE_TYPE (node) = utype;
12678 to_return = convert (prom_type, node);
12679 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12680 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
12681 TREE_SIDE_EFFECTS (to_return)
12682 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12683 return to_return;
12685 break;
12687 /* 15.19.1 Type Comparison Operator instaceof */
12688 case INSTANCEOF_EXPR:
12690 TREE_TYPE (node) = boolean_type_node;
12692 if (!(op2_type = resolve_type_during_patch (op2)))
12693 return error_mark_node;
12695 /* The first operand must be a reference type or the null type */
12696 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12697 error_found = 1; /* Error reported further below */
12699 /* The second operand must be a reference type */
12700 if (!JREFERENCE_TYPE_P (op2_type))
12702 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12703 parse_error_context
12704 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12705 lang_printable_name (op2_type, 0));
12706 error_found = 1;
12709 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12711 /* If the first operand is null, the result is always false */
12712 if (op1 == null_pointer_node)
12713 return boolean_false_node;
12714 else if (flag_emit_class_files)
12716 TREE_OPERAND (node, 1) = op2_type;
12717 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
12718 return node;
12720 /* Otherwise we have to invoke instance of to figure it out */
12721 else
12722 return build_instanceof (op1, op2_type);
12724 /* There is no way the expression operand can be an instance of
12725 the type operand. This is a compile time error. */
12726 else
12728 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
12729 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12730 parse_error_context
12731 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12732 t1, lang_printable_name (op2_type, 0));
12733 free (t1);
12734 error_found = 1;
12737 break;
12739 /* 15.21 Bitwise and Logical Operators */
12740 case BIT_AND_EXPR:
12741 case BIT_XOR_EXPR:
12742 case BIT_IOR_EXPR:
12743 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12744 /* Binary numeric promotion is performed on both operand and the
12745 expression retain that type */
12746 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12748 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12749 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12750 /* The type of the bitwise operator expression is BOOLEAN */
12751 prom_type = boolean_type_node;
12752 else
12754 if (!JINTEGRAL_TYPE_P (op1_type))
12755 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12756 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12757 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12758 TREE_TYPE (node) = error_mark_node;
12759 error_found = 1;
12760 /* Insert a break here if adding thing before the switch's
12761 break for this case */
12763 break;
12765 /* 15.22 Conditional-And Operator */
12766 case TRUTH_ANDIF_EXPR:
12767 /* 15.23 Conditional-Or Operator */
12768 case TRUTH_ORIF_EXPR:
12769 /* Operands must be of BOOLEAN type */
12770 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12771 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12773 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12774 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12775 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12776 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12777 TREE_TYPE (node) = boolean_type_node;
12778 error_found = 1;
12779 break;
12781 /* The type of the conditional operators is BOOLEAN */
12782 prom_type = boolean_type_node;
12783 break;
12785 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12786 case LT_EXPR:
12787 case GT_EXPR:
12788 case LE_EXPR:
12789 case GE_EXPR:
12790 /* The type of each of the operands must be a primitive numeric
12791 type */
12792 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12794 if (!JNUMERIC_TYPE_P (op1_type))
12795 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12796 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12797 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12798 TREE_TYPE (node) = boolean_type_node;
12799 error_found = 1;
12800 break;
12802 /* Binary numeric promotion is performed on the operands */
12803 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12804 /* The type of the relation expression is always BOOLEAN */
12805 prom_type = boolean_type_node;
12806 break;
12808 /* 15.20 Equality Operator */
12809 case EQ_EXPR:
12810 case NE_EXPR:
12811 /* It's time for us to patch the strings. */
12812 if ((cn = patch_string (op1)))
12814 op1 = cn;
12815 op1_type = TREE_TYPE (op1);
12817 if ((cn = patch_string (op2)))
12819 op2 = cn;
12820 op2_type = TREE_TYPE (op2);
12823 /* 15.20.1 Numerical Equality Operators == and != */
12824 /* Binary numeric promotion is performed on the operands */
12825 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
12826 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12828 /* 15.20.2 Boolean Equality Operators == and != */
12829 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
12830 TREE_CODE (op2_type) == BOOLEAN_TYPE)
12831 ; /* Nothing to do here */
12833 /* 15.20.3 Reference Equality Operators == and != */
12834 /* Types have to be either references or the null type. If
12835 they're references, it must be possible to convert either
12836 type to the other by casting conversion. */
12837 else if (op1 == null_pointer_node || op2 == null_pointer_node
12838 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
12839 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
12840 || valid_ref_assignconv_cast_p (op2_type,
12841 op1_type, 1))))
12842 ; /* Nothing to do here */
12844 /* Else we have an error figure what can't be converted into
12845 what and report the error */
12846 else
12848 char *t1;
12849 t1 = xstrdup (lang_printable_name (op1_type, 0));
12850 parse_error_context
12851 (wfl_operator,
12852 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
12853 operator_string (node), t1,
12854 lang_printable_name (op2_type, 0));
12855 free (t1);
12856 TREE_TYPE (node) = boolean_type_node;
12857 error_found = 1;
12858 break;
12860 prom_type = boolean_type_node;
12861 break;
12864 if (error_found)
12865 return error_mark_node;
12867 TREE_OPERAND (node, 0) = op1;
12868 TREE_OPERAND (node, 1) = op2;
12869 TREE_TYPE (node) = prom_type;
12870 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12872 if (flag_emit_xref)
12873 return node;
12875 /* fold does not respect side-effect order as required for Java but not C.
12876 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
12877 * bytecode.
12879 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
12880 : ! TREE_SIDE_EFFECTS (node))
12881 node = fold (node);
12882 return node;
12885 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
12886 zero value, the value of CSTE comes after the valude of STRING */
12888 static tree
12889 do_merge_string_cste (cste, string, string_len, after)
12890 tree cste;
12891 const char *string;
12892 int string_len, after;
12894 int len = TREE_STRING_LENGTH (cste) + string_len;
12895 const char *old = TREE_STRING_POINTER (cste);
12896 TREE_STRING_LENGTH (cste) = len;
12897 TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
12898 if (after)
12900 strcpy (TREE_STRING_POINTER (cste), string);
12901 strcat (TREE_STRING_POINTER (cste), old);
12903 else
12905 strcpy (TREE_STRING_POINTER (cste), old);
12906 strcat (TREE_STRING_POINTER (cste), string);
12908 return cste;
12911 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
12912 new STRING_CST on success, NULL_TREE on failure */
12914 static tree
12915 merge_string_cste (op1, op2, after)
12916 tree op1, op2;
12917 int after;
12919 /* Handle two string constants right away */
12920 if (TREE_CODE (op2) == STRING_CST)
12921 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
12922 TREE_STRING_LENGTH (op2), after);
12924 /* Reasonable integer constant can be treated right away */
12925 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
12927 static const char *boolean_true = "true";
12928 static const char *boolean_false = "false";
12929 static const char *null_pointer = "null";
12930 char ch[3];
12931 const char *string;
12933 if (op2 == boolean_true_node)
12934 string = boolean_true;
12935 else if (op2 == boolean_false_node)
12936 string = boolean_false;
12937 else if (op2 == null_pointer_node)
12938 string = null_pointer;
12939 else if (TREE_TYPE (op2) == char_type_node)
12941 ch[0] = (char )TREE_INT_CST_LOW (op2);
12942 ch[1] = '\0';
12943 string = ch;
12945 else
12946 string = print_int_node (op2);
12948 return do_merge_string_cste (op1, string, strlen (string), after);
12950 return NULL_TREE;
12953 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
12954 has to be a STRING_CST and the other part must be a STRING_CST or a
12955 INTEGRAL constant. Return a new STRING_CST if the operation
12956 succeed, NULL_TREE otherwise.
12958 If the case we want to optimize for space, we might want to return
12959 NULL_TREE for each invocation of this routine. FIXME */
12961 static tree
12962 string_constant_concatenation (op1, op2)
12963 tree op1, op2;
12965 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
12967 tree string, rest;
12968 int invert;
12970 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
12971 rest = (string == op1 ? op2 : op1);
12972 invert = (string == op1 ? 0 : 1 );
12974 /* Walk REST, only if it looks reasonable */
12975 if (TREE_CODE (rest) != STRING_CST
12976 && !IS_CRAFTED_STRING_BUFFER_P (rest)
12977 && !JSTRING_TYPE_P (TREE_TYPE (rest))
12978 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
12980 rest = java_complete_tree (rest);
12981 if (rest == error_mark_node)
12982 return error_mark_node;
12983 rest = fold (rest);
12985 return merge_string_cste (string, rest, invert);
12987 return NULL_TREE;
12990 /* Implement the `+' operator. Does static optimization if possible,
12991 otherwise create (if necessary) and append elements to a
12992 StringBuffer. The StringBuffer will be carried around until it is
12993 used for a function call or an assignment. Then toString() will be
12994 called on it to turn it into a String object. */
12996 static tree
12997 build_string_concatenation (op1, op2)
12998 tree op1, op2;
13000 tree result;
13001 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13003 if (flag_emit_xref)
13004 return build (PLUS_EXPR, string_type_node, op1, op2);
13006 /* Try to do some static optimization */
13007 if ((result = string_constant_concatenation (op1, op2)))
13008 return result;
13010 /* Discard empty strings on either side of the expression */
13011 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13013 op1 = op2;
13014 op2 = NULL_TREE;
13016 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13017 op2 = NULL_TREE;
13019 /* If operands are string constant, turn then into object references */
13020 if (TREE_CODE (op1) == STRING_CST)
13021 op1 = patch_string_cst (op1);
13022 if (op2 && TREE_CODE (op2) == STRING_CST)
13023 op2 = patch_string_cst (op2);
13025 /* If either one of the constant is null and the other non null
13026 operand is a String object, return it. */
13027 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13028 return op1;
13030 /* If OP1 isn't already a StringBuffer, create and
13031 initialize a new one */
13032 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13034 /* Two solutions here:
13035 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13036 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13037 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13038 op1 = BUILD_STRING_BUFFER (op1);
13039 else
13041 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13042 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13046 if (op2)
13048 /* OP1 is no longer the last node holding a crafted StringBuffer */
13049 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13050 /* Create a node for `{new...,xxx}.append (op2)' */
13051 if (op2)
13052 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13055 /* Mark the last node holding a crafted StringBuffer */
13056 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13058 TREE_SIDE_EFFECTS (op1) = side_effects;
13059 return op1;
13062 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13063 StringBuffer. If no string were found to be patched, return
13064 NULL. */
13066 static tree
13067 patch_string (node)
13068 tree node;
13070 if (node == error_mark_node)
13071 return error_mark_node;
13072 if (TREE_CODE (node) == STRING_CST)
13073 return patch_string_cst (node);
13074 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13076 int saved = ctxp->explicit_constructor_p;
13077 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13078 tree ret;
13079 /* Temporary disable forbid the use of `this'. */
13080 ctxp->explicit_constructor_p = 0;
13081 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13082 /* String concatenation arguments must be evaluated in order too. */
13083 ret = force_evaluation_order (ret);
13084 /* Restore it at its previous value */
13085 ctxp->explicit_constructor_p = saved;
13086 return ret;
13088 return NULL_TREE;
13091 /* Build the internal representation of a string constant. */
13093 static tree
13094 patch_string_cst (node)
13095 tree node;
13097 int location;
13098 if (! flag_emit_class_files)
13100 push_obstacks (&permanent_obstack, &permanent_obstack);
13101 node = get_identifier (TREE_STRING_POINTER (node));
13102 location = alloc_name_constant (CONSTANT_String, node);
13103 node = build_ref_from_constant_pool (location);
13104 pop_obstacks ();
13106 TREE_TYPE (node) = string_ptr_type_node;
13107 TREE_CONSTANT (node) = 1;
13108 return node;
13111 /* Build an incomplete unary operator expression. */
13113 static tree
13114 build_unaryop (op_token, op_location, op1)
13115 int op_token, op_location;
13116 tree op1;
13118 enum tree_code op;
13119 tree unaryop;
13120 switch (op_token)
13122 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13123 case MINUS_TK: op = NEGATE_EXPR; break;
13124 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13125 case NOT_TK: op = BIT_NOT_EXPR; break;
13126 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13127 op_token);
13130 unaryop = build1 (op, NULL_TREE, op1);
13131 TREE_SIDE_EFFECTS (unaryop) = 1;
13132 /* Store the location of the operator, for better error report. The
13133 string of the operator will be rebuild based on the OP value. */
13134 EXPR_WFL_LINECOL (unaryop) = op_location;
13135 return unaryop;
13138 /* Special case for the ++/-- operators, since they require an extra
13139 argument to build, which is set to NULL and patched
13140 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13142 static tree
13143 build_incdec (op_token, op_location, op1, is_post_p)
13144 int op_token, op_location;
13145 tree op1;
13146 int is_post_p;
13148 static enum tree_code lookup [2][2] =
13150 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13151 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13153 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13154 NULL_TREE, op1, NULL_TREE);
13155 TREE_SIDE_EFFECTS (node) = 1;
13156 /* Store the location of the operator, for better error report. The
13157 string of the operator will be rebuild based on the OP value. */
13158 EXPR_WFL_LINECOL (node) = op_location;
13159 return node;
13162 /* Build an incomplete cast operator, based on the use of the
13163 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13164 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13165 though its type is already set. */
13167 static tree
13168 build_cast (location, type, exp)
13169 int location;
13170 tree type, exp;
13172 tree node = build1 (CONVERT_EXPR, type, exp);
13173 EXPR_WFL_LINECOL (node) = location;
13174 return node;
13177 /* Build an incomplete class reference operator. */
13178 static tree
13179 build_incomplete_class_ref (location, class_name)
13180 int location;
13181 tree class_name;
13183 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13184 EXPR_WFL_LINECOL (node) = location;
13185 return node;
13188 /* Complete an incomplete class reference operator. */
13189 static tree
13190 patch_incomplete_class_ref (node)
13191 tree node;
13193 tree type = TREE_OPERAND (node, 0);
13194 tree ref_type;
13196 if (!(ref_type = resolve_type_during_patch (type)))
13197 return error_mark_node;
13199 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13201 /* A class referenced by `foo.class' is initialized. */
13202 return build_class_init (ref_type, build_class_ref (ref_type));
13205 /* If we're emitting class files and we have to deal with non
13206 primitive types, we invoke (and consider generating) the
13207 synthetic static method `class$'. */
13208 if (!TYPE_DOT_CLASS (current_class))
13209 build_dot_class_method (current_class);
13210 ref_type = build_dot_class_method_invocation (ref_type);
13211 return java_complete_tree (ref_type);
13214 /* 15.14 Unary operators. We return error_mark_node in case of error,
13215 but preserve the type of NODE if the type is fixed. */
13217 static tree
13218 patch_unaryop (node, wfl_op)
13219 tree node;
13220 tree wfl_op;
13222 tree op = TREE_OPERAND (node, 0);
13223 tree op_type = TREE_TYPE (op);
13224 tree prom_type = NULL_TREE, value, decl;
13225 int outer_field_flag = 0;
13226 int code = TREE_CODE (node);
13227 int error_found = 0;
13229 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13231 switch (code)
13233 /* 15.13.2 Postfix Increment Operator ++ */
13234 case POSTINCREMENT_EXPR:
13235 /* 15.13.3 Postfix Increment Operator -- */
13236 case POSTDECREMENT_EXPR:
13237 /* 15.14.1 Prefix Increment Operator ++ */
13238 case PREINCREMENT_EXPR:
13239 /* 15.14.2 Prefix Decrement Operator -- */
13240 case PREDECREMENT_EXPR:
13241 op = decl = strip_out_static_field_access_decl (op);
13242 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13243 /* We might be trying to change an outer field accessed using
13244 access method. */
13245 if (outer_field_flag)
13247 /* Retrieve the decl of the field we're trying to access. We
13248 do that by first retrieving the function we would call to
13249 access the field. It has been already verified that this
13250 field isn't final */
13251 if (flag_emit_class_files)
13252 decl = TREE_OPERAND (op, 0);
13253 else
13254 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13255 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13257 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13258 else if (!JDECL_P (decl)
13259 && TREE_CODE (decl) != COMPONENT_REF
13260 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13261 && TREE_CODE (decl) != INDIRECT_REF
13262 && !(TREE_CODE (decl) == COMPOUND_EXPR
13263 && TREE_OPERAND (decl, 1)
13264 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13266 tree lvalue;
13267 /* Before screaming, check that we're not in fact trying to
13268 increment a optimized static final access, in which case
13269 we issue an different error message. */
13270 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13271 && resolve_expression_name (wfl_op, &lvalue)
13272 && check_final_assignment (lvalue, wfl_op)))
13273 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13274 operator_string (node));
13275 TREE_TYPE (node) = error_mark_node;
13276 error_found = 1;
13279 if (check_final_assignment (op, wfl_op))
13280 error_found = 1;
13282 /* From now on, we know that op if a variable and that it has a
13283 valid wfl. We use wfl_op to locate errors related to the
13284 ++/-- operand. */
13285 else if (!JNUMERIC_TYPE_P (op_type))
13287 parse_error_context
13288 (wfl_op, "Invalid argument type `%s' to `%s'",
13289 lang_printable_name (op_type, 0), operator_string (node));
13290 TREE_TYPE (node) = error_mark_node;
13291 error_found = 1;
13293 else
13295 /* Before the addition, binary numeric promotion is performed on
13296 both operands, if really necessary */
13297 if (JINTEGRAL_TYPE_P (op_type))
13299 value = build_int_2 (1, 0);
13300 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13302 else
13304 value = build_int_2 (1, 0);
13305 TREE_TYPE (node) =
13306 binary_numeric_promotion (op_type,
13307 TREE_TYPE (value), &op, &value);
13310 /* We remember we might be accessing an outer field */
13311 if (outer_field_flag)
13313 /* We re-generate an access to the field */
13314 value = build (PLUS_EXPR, TREE_TYPE (op),
13315 build_outer_field_access (wfl_op, decl), value);
13317 /* And we patch the original access$() into a write
13318 with plus_op as a rhs */
13319 return outer_field_access_fix (node, op, value);
13322 /* And write back into the node. */
13323 TREE_OPERAND (node, 0) = op;
13324 TREE_OPERAND (node, 1) = value;
13325 /* Convert the overall back into its original type, if
13326 necessary, and return */
13327 if (JINTEGRAL_TYPE_P (op_type))
13328 return fold (node);
13329 else
13330 return fold (convert (op_type, node));
13332 break;
13334 /* 15.14.3 Unary Plus Operator + */
13335 case UNARY_PLUS_EXPR:
13336 /* 15.14.4 Unary Minus Operator - */
13337 case NEGATE_EXPR:
13338 if (!JNUMERIC_TYPE_P (op_type))
13340 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13341 TREE_TYPE (node) = error_mark_node;
13342 error_found = 1;
13344 /* Unary numeric promotion is performed on operand */
13345 else
13347 op = do_unary_numeric_promotion (op);
13348 prom_type = TREE_TYPE (op);
13349 if (code == UNARY_PLUS_EXPR)
13350 return fold (op);
13352 break;
13354 /* 15.14.5 Bitwise Complement Operator ~ */
13355 case BIT_NOT_EXPR:
13356 if (!JINTEGRAL_TYPE_P (op_type))
13358 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13359 TREE_TYPE (node) = error_mark_node;
13360 error_found = 1;
13362 else
13364 op = do_unary_numeric_promotion (op);
13365 prom_type = TREE_TYPE (op);
13367 break;
13369 /* 15.14.6 Logical Complement Operator ! */
13370 case TRUTH_NOT_EXPR:
13371 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13373 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
13374 /* But the type is known. We will report an error if further
13375 attempt of a assignment is made with this rhs */
13376 TREE_TYPE (node) = boolean_type_node;
13377 error_found = 1;
13379 else
13380 prom_type = boolean_type_node;
13381 break;
13383 /* 15.15 Cast Expression */
13384 case CONVERT_EXPR:
13385 value = patch_cast (node, wfl_operator);
13386 if (value == error_mark_node)
13388 /* If this cast is part of an assignment, we tell the code
13389 that deals with it not to complain about a mismatch,
13390 because things have been cast, anyways */
13391 TREE_TYPE (node) = error_mark_node;
13392 error_found = 1;
13394 else
13396 value = fold (value);
13397 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13398 return value;
13400 break;
13403 if (error_found)
13404 return error_mark_node;
13406 /* There are cases where node has been replaced by something else
13407 and we don't end up returning here: UNARY_PLUS_EXPR,
13408 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
13409 TREE_OPERAND (node, 0) = fold (op);
13410 TREE_TYPE (node) = prom_type;
13411 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
13412 return fold (node);
13415 /* Generic type resolution that sometimes takes place during node
13416 patching. Returned the resolved type or generate an error
13417 message. Return the resolved type or NULL_TREE. */
13419 static tree
13420 resolve_type_during_patch (type)
13421 tree type;
13423 if (unresolved_type_p (type, NULL))
13425 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
13426 if (!type_decl)
13428 parse_error_context (type,
13429 "Class `%s' not found in type declaration",
13430 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13431 return NULL_TREE;
13433 else
13435 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13436 return TREE_TYPE (type_decl);
13439 return type;
13441 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
13442 found. Otherwise NODE or something meant to replace it is returned. */
13444 static tree
13445 patch_cast (node, wfl_operator)
13446 tree node;
13447 tree wfl_operator;
13449 tree op = TREE_OPERAND (node, 0);
13450 tree op_type = TREE_TYPE (op);
13451 tree cast_type = TREE_TYPE (node);
13452 char *t1;
13454 /* First resolve OP_TYPE if unresolved */
13455 if (!(cast_type = resolve_type_during_patch (cast_type)))
13456 return error_mark_node;
13458 /* Check on cast that are proven correct at compile time */
13459 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13461 /* Same type */
13462 if (cast_type == op_type)
13463 return node;
13465 /* float and double type are converted to the original type main
13466 variant and then to the target type. */
13467 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13468 op = convert (integer_type_node, op);
13470 /* Try widening/narowwing convertion. Potentially, things need
13471 to be worked out in gcc so we implement the extreme cases
13472 correctly. fold_convert() needs to be fixed. */
13473 return convert (cast_type, op);
13476 /* It's also valid to cast a boolean into a boolean */
13477 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13478 return node;
13480 /* null can be casted to references */
13481 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13482 return build_null_of_type (cast_type);
13484 /* The remaining legal casts involve conversion between reference
13485 types. Check for their compile time correctness. */
13486 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
13487 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
13489 TREE_TYPE (node) = promote_type (cast_type);
13490 /* Now, the case can be determined correct at compile time if
13491 OP_TYPE can be converted into CAST_TYPE by assignment
13492 conversion (5.2) */
13494 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
13496 TREE_SET_CODE (node, NOP_EXPR);
13497 return node;
13500 if (flag_emit_class_files)
13502 TREE_SET_CODE (node, CONVERT_EXPR);
13503 return node;
13506 /* The cast requires a run-time check */
13507 return build (CALL_EXPR, promote_type (cast_type),
13508 build_address_of (soft_checkcast_node),
13509 tree_cons (NULL_TREE, build_class_ref (cast_type),
13510 build_tree_list (NULL_TREE, op)),
13511 NULL_TREE);
13514 /* Any other casts are proven incorrect at compile time */
13515 t1 = xstrdup (lang_printable_name (op_type, 0));
13516 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
13517 t1, lang_printable_name (cast_type, 0));
13518 free (t1);
13519 return error_mark_node;
13522 /* Build a null constant and give it the type TYPE. */
13524 static tree
13525 build_null_of_type (type)
13526 tree type;
13528 tree node = build_int_2 (0, 0);
13529 TREE_TYPE (node) = promote_type (type);
13530 return node;
13533 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13534 a list of indices. */
13535 static tree
13536 build_array_ref (location, array, index)
13537 int location;
13538 tree array, index;
13540 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13541 EXPR_WFL_LINECOL (node) = location;
13542 return node;
13545 /* 15.12 Array Access Expression */
13547 static tree
13548 patch_array_ref (node)
13549 tree node;
13551 tree array = TREE_OPERAND (node, 0);
13552 tree array_type = TREE_TYPE (array);
13553 tree index = TREE_OPERAND (node, 1);
13554 tree index_type = TREE_TYPE (index);
13555 int error_found = 0;
13557 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13559 if (TREE_CODE (array_type) == POINTER_TYPE)
13560 array_type = TREE_TYPE (array_type);
13562 /* The array reference must be an array */
13563 if (!TYPE_ARRAY_P (array_type))
13565 parse_error_context
13566 (wfl_operator,
13567 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13568 lang_printable_name (array_type, 0));
13569 TREE_TYPE (node) = error_mark_node;
13570 error_found = 1;
13573 /* The array index undergoes unary numeric promotion. The promoted
13574 type must be int */
13575 index = do_unary_numeric_promotion (index);
13576 if (TREE_TYPE (index) != int_type_node)
13578 if (valid_cast_to_p (index_type, int_type_node))
13579 parse_error_context (wfl_operator,
13580 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
13581 lang_printable_name (index_type, 0));
13582 else
13583 parse_error_context (wfl_operator,
13584 "Incompatible type for `[]'. Can't convert `%s' to `int'",
13585 lang_printable_name (index_type, 0));
13586 TREE_TYPE (node) = error_mark_node;
13587 error_found = 1;
13590 if (error_found)
13591 return error_mark_node;
13593 array_type = TYPE_ARRAY_ELEMENT (array_type);
13595 if (flag_emit_class_files || flag_emit_xref)
13597 TREE_OPERAND (node, 0) = array;
13598 TREE_OPERAND (node, 1) = index;
13600 else
13602 /* The save_expr is for correct evaluation order. It would be cleaner
13603 to use force_evaluation_order (see comment there), but that is
13604 difficult when we also have to deal with bounds checking. */
13605 if (TREE_SIDE_EFFECTS (index))
13606 array = save_expr (array);
13607 node = build_java_arrayaccess (array, array_type, index);
13608 if (TREE_SIDE_EFFECTS (index))
13609 node = build (COMPOUND_EXPR, array_type, array, node);
13611 TREE_TYPE (node) = array_type;
13612 return node;
13615 /* 15.9 Array Creation Expressions */
13617 static tree
13618 build_newarray_node (type, dims, extra_dims)
13619 tree type;
13620 tree dims;
13621 int extra_dims;
13623 tree node =
13624 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
13625 build_int_2 (extra_dims, 0));
13626 return node;
13629 static tree
13630 patch_newarray (node)
13631 tree node;
13633 tree type = TREE_OPERAND (node, 0);
13634 tree dims = TREE_OPERAND (node, 1);
13635 tree cdim, array_type;
13636 int error_found = 0;
13637 int ndims = 0;
13638 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
13640 /* Dimension types are verified. It's better for the types to be
13641 verified in order. */
13642 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13644 int dim_error = 0;
13645 tree dim = TREE_VALUE (cdim);
13647 /* Dim might have been saved during its evaluation */
13648 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
13650 /* The type of each specified dimension must be an integral type. */
13651 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13652 dim_error = 1;
13654 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13655 promoted type must be int. */
13656 else
13658 dim = do_unary_numeric_promotion (dim);
13659 if (TREE_TYPE (dim) != int_type_node)
13660 dim_error = 1;
13663 /* Report errors on types here */
13664 if (dim_error)
13666 parse_error_context
13667 (TREE_PURPOSE (cdim),
13668 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
13669 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
13670 "Explicit cast needed to" : "Can't"),
13671 lang_printable_name (TREE_TYPE (dim), 0));
13672 error_found = 1;
13675 TREE_PURPOSE (cdim) = NULL_TREE;
13678 /* Resolve array base type if unresolved */
13679 if (!(type = resolve_type_during_patch (type)))
13680 error_found = 1;
13682 if (error_found)
13684 /* We don't want further evaluation of this bogus array creation
13685 operation */
13686 TREE_TYPE (node) = error_mark_node;
13687 return error_mark_node;
13690 /* Set array_type to the actual (promoted) array type of the result. */
13691 if (TREE_CODE (type) == RECORD_TYPE)
13692 type = build_pointer_type (type);
13693 while (--xdims >= 0)
13695 type = promote_type (build_java_array_type (type, -1));
13697 dims = nreverse (dims);
13698 array_type = type;
13699 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13701 type = array_type;
13702 array_type
13703 = build_java_array_type (type,
13704 TREE_CODE (cdim) == INTEGER_CST
13705 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13706 : -1);
13707 array_type = promote_type (array_type);
13709 dims = nreverse (dims);
13711 /* The node is transformed into a function call. Things are done
13712 differently according to the number of dimensions. If the number
13713 of dimension is equal to 1, then the nature of the base type
13714 (primitive or not) matters. */
13715 if (ndims == 1)
13716 return build_new_array (type, TREE_VALUE (dims));
13718 /* Can't reuse what's already written in expr.c because it uses the
13719 JVM stack representation. Provide a build_multianewarray. FIXME */
13720 return build (CALL_EXPR, array_type,
13721 build_address_of (soft_multianewarray_node),
13722 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
13723 tree_cons (NULL_TREE,
13724 build_int_2 (ndims, 0), dims )),
13725 NULL_TREE);
13728 /* 10.6 Array initializer. */
13730 /* Build a wfl for array element that don't have one, so we can
13731 pin-point errors. */
13733 static tree
13734 maybe_build_array_element_wfl (node)
13735 tree node;
13737 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13738 return build_expr_wfl (NULL_TREE, ctxp->filename,
13739 ctxp->elc.line, ctxp->elc.prev_col);
13740 else
13741 return NULL_TREE;
13744 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13745 identification of initialized arrays easier to detect during walk
13746 and expansion. */
13748 static tree
13749 build_new_array_init (location, values)
13750 int location;
13751 tree values;
13753 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13754 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
13755 EXPR_WFL_LINECOL (to_return) = location;
13756 return to_return;
13759 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13760 occurred. Otherwise return NODE after having set its type
13761 appropriately. */
13763 static tree
13764 patch_new_array_init (type, node)
13765 tree type, node;
13767 int error_seen = 0;
13768 tree current, element_type;
13769 HOST_WIDE_INT length;
13770 int all_constant = 1;
13771 tree init = TREE_OPERAND (node, 0);
13773 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13775 parse_error_context (node,
13776 "Invalid array initializer for non-array type `%s'",
13777 lang_printable_name (type, 1));
13778 return error_mark_node;
13780 type = TREE_TYPE (type);
13781 element_type = TYPE_ARRAY_ELEMENT (type);
13783 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13785 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13786 current; length++, current = TREE_CHAIN (current))
13788 tree elt = TREE_VALUE (current);
13789 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
13791 error_seen |= array_constructor_check_entry (element_type, current);
13792 elt = TREE_VALUE (current);
13793 /* When compiling to native code, STRING_CST is converted to
13794 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13795 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
13796 all_constant = 0;
13798 else
13800 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13801 TREE_PURPOSE (current) = NULL_TREE;
13802 all_constant = 0;
13804 if (elt && TREE_CODE (elt) == TREE_LIST
13805 && TREE_VALUE (elt) == error_mark_node)
13806 error_seen = 1;
13809 if (error_seen)
13810 return error_mark_node;
13812 /* Create a new type. We can't reuse the one we have here by
13813 patching its dimension because it originally is of dimension -1
13814 hence reused by gcc. This would prevent triangular arrays. */
13815 type = build_java_array_type (element_type, length);
13816 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13817 TREE_TYPE (node) = promote_type (type);
13818 TREE_CONSTANT (init) = all_constant;
13819 TREE_CONSTANT (node) = all_constant;
13820 return node;
13823 /* Verify that one entry of the initializer element list can be
13824 assigned to the array base type. Report 1 if an error occurred, 0
13825 otherwise. */
13827 static int
13828 array_constructor_check_entry (type, entry)
13829 tree type, entry;
13831 char *array_type_string = NULL; /* For error reports */
13832 tree value, type_value, new_value, wfl_value, patched;
13833 int error_seen = 0;
13835 new_value = NULL_TREE;
13836 wfl_value = TREE_VALUE (entry);
13838 push_obstacks (&permanent_obstack, &permanent_obstack);
13839 value = java_complete_tree (TREE_VALUE (entry));
13840 /* patch_string return error_mark_node if arg is error_mark_node */
13841 if ((patched = patch_string (value)))
13842 value = patched;
13843 if (value == error_mark_node)
13844 return 1;
13846 type_value = TREE_TYPE (value);
13848 /* At anytime, try_builtin_assignconv can report a warning on
13849 constant overflow during narrowing. */
13850 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
13851 new_value = try_builtin_assignconv (wfl_operator, type, value);
13852 if (!new_value && (new_value = try_reference_assignconv (type, value)))
13853 type_value = promote_type (type);
13855 pop_obstacks ();
13856 /* Check and report errors */
13857 if (!new_value)
13859 const char *msg = (!valid_cast_to_p (type_value, type) ?
13860 "Can't" : "Explicit cast needed to");
13861 if (!array_type_string)
13862 array_type_string = xstrdup (lang_printable_name (type, 1));
13863 parse_error_context
13864 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
13865 msg, lang_printable_name (type_value, 1), array_type_string);
13866 error_seen = 1;
13869 if (new_value)
13871 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
13872 TREE_VALUE (entry) = new_value;
13875 if (array_type_string)
13876 free (array_type_string);
13878 TREE_PURPOSE (entry) = NULL_TREE;
13879 return error_seen;
13882 static tree
13883 build_this (location)
13884 int location;
13886 tree node = build_wfl_node (this_identifier_node);
13887 TREE_SET_CODE (node, THIS_EXPR);
13888 EXPR_WFL_LINECOL (node) = location;
13889 return node;
13892 /* 14.15 The return statement. It builds a modify expression that
13893 assigns the returned value to the RESULT_DECL that hold the value
13894 to be returned. */
13896 static tree
13897 build_return (location, op)
13898 int location;
13899 tree op;
13901 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
13902 EXPR_WFL_LINECOL (node) = location;
13903 node = build_debugable_stmt (location, node);
13904 return node;
13907 static tree
13908 patch_return (node)
13909 tree node;
13911 tree return_exp = TREE_OPERAND (node, 0);
13912 tree meth = current_function_decl;
13913 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
13914 int error_found = 0;
13916 TREE_TYPE (node) = error_mark_node;
13917 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13919 /* It's invalid to have a return value within a function that is
13920 declared with the keyword void or that is a constructor */
13921 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
13922 error_found = 1;
13924 /* It's invalid to use a return statement in a static block */
13925 if (DECL_CLINIT_P (current_function_decl))
13926 error_found = 1;
13928 /* It's invalid to have a no return value within a function that
13929 isn't declared with the keyword `void' */
13930 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
13931 error_found = 2;
13933 if (in_instance_initializer)
13934 error_found = 1;
13936 if (error_found)
13938 if (in_instance_initializer)
13939 parse_error_context (wfl_operator,
13940 "`return' inside instance initializer");
13942 else if (DECL_CLINIT_P (current_function_decl))
13943 parse_error_context (wfl_operator,
13944 "`return' inside static initializer");
13946 else if (!DECL_CONSTRUCTOR_P (meth))
13948 char *t = xstrdup (lang_printable_name (mtype, 0));
13949 parse_error_context (wfl_operator,
13950 "`return' with%s value from `%s %s'",
13951 (error_found == 1 ? "" : "out"),
13952 t, lang_printable_name (meth, 0));
13953 free (t);
13955 else
13956 parse_error_context (wfl_operator,
13957 "`return' with value from constructor `%s'",
13958 lang_printable_name (meth, 0));
13959 return error_mark_node;
13962 /* If we have a return_exp, build a modify expression and expand
13963 it. Note: at that point, the assignment is declared valid, but we
13964 may want to carry some more hacks */
13965 if (return_exp)
13967 tree exp = java_complete_tree (return_exp);
13968 tree modify, patched;
13970 /* If the function returned value and EXP are booleans, EXP has
13971 to be converted into the type of DECL_RESULT, which is integer
13972 (see complete_start_java_method) */
13973 if (TREE_TYPE (exp) == boolean_type_node &&
13974 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
13975 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
13977 /* `null' can be assigned to a function returning a reference */
13978 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
13979 exp == null_pointer_node)
13980 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
13982 if ((patched = patch_string (exp)))
13983 exp = patched;
13985 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
13986 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
13987 modify = java_complete_tree (modify);
13989 if (modify != error_mark_node)
13991 TREE_SIDE_EFFECTS (modify) = 1;
13992 TREE_OPERAND (node, 0) = modify;
13994 else
13995 return error_mark_node;
13997 TREE_TYPE (node) = void_type_node;
13998 TREE_SIDE_EFFECTS (node) = 1;
13999 return node;
14002 /* 14.8 The if Statement */
14004 static tree
14005 build_if_else_statement (location, expression, if_body, else_body)
14006 int location;
14007 tree expression, if_body, else_body;
14009 tree node;
14010 if (!else_body)
14011 else_body = empty_stmt_node;
14012 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14013 EXPR_WFL_LINECOL (node) = location;
14014 node = build_debugable_stmt (location, node);
14015 return node;
14018 static tree
14019 patch_if_else_statement (node)
14020 tree node;
14022 tree expression = TREE_OPERAND (node, 0);
14024 TREE_TYPE (node) = error_mark_node;
14025 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14027 /* The type of expression must be boolean */
14028 if (TREE_TYPE (expression) != boolean_type_node
14029 && TREE_TYPE (expression) != promoted_boolean_type_node)
14031 parse_error_context
14032 (wfl_operator,
14033 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14034 lang_printable_name (TREE_TYPE (expression), 0));
14035 return error_mark_node;
14038 TREE_TYPE (node) = void_type_node;
14039 TREE_SIDE_EFFECTS (node) = 1;
14040 CAN_COMPLETE_NORMALLY (node)
14041 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14042 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
14043 return node;
14046 /* 14.6 Labeled Statements */
14048 /* Action taken when a lableled statement is parsed. a new
14049 LABELED_BLOCK_EXPR is created. No statement is attached to the
14050 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14052 static tree
14053 build_labeled_block (location, label)
14054 int location;
14055 tree label;
14057 tree label_name ;
14058 tree label_decl, node;
14059 if (label == NULL_TREE || label == continue_identifier_node)
14060 label_name = label;
14061 else
14063 label_name = merge_qualified_name (label_id, label);
14064 /* Issue an error if we try to reuse a label that was previously
14065 declared */
14066 if (IDENTIFIER_LOCAL_VALUE (label_name))
14068 EXPR_WFL_LINECOL (wfl_operator) = location;
14069 parse_error_context (wfl_operator,
14070 "Declaration of `%s' shadows a previous label declaration",
14071 IDENTIFIER_POINTER (label));
14072 EXPR_WFL_LINECOL (wfl_operator) =
14073 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14074 parse_error_context (wfl_operator,
14075 "This is the location of the previous declaration of label `%s'",
14076 IDENTIFIER_POINTER (label));
14077 java_error_count--;
14081 label_decl = create_label_decl (label_name);
14082 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14083 EXPR_WFL_LINECOL (node) = location;
14084 TREE_SIDE_EFFECTS (node) = 1;
14085 return node;
14088 /* A labeled statement LBE is attached a statement. */
14090 static tree
14091 finish_labeled_statement (lbe, statement)
14092 tree lbe; /* Labeled block expr */
14093 tree statement;
14095 /* In anyways, tie the loop to its statement */
14096 LABELED_BLOCK_BODY (lbe) = statement;
14097 pop_labeled_block ();
14098 POP_LABELED_BLOCK ();
14099 return lbe;
14102 /* 14.10, 14.11, 14.12 Loop Statements */
14104 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14105 list. */
14107 static tree
14108 build_new_loop (loop_body)
14109 tree loop_body;
14111 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14112 TREE_SIDE_EFFECTS (loop) = 1;
14113 PUSH_LOOP (loop);
14114 return loop;
14117 /* Create a loop body according to the following structure:
14118 COMPOUND_EXPR
14119 COMPOUND_EXPR (loop main body)
14120 EXIT_EXPR (this order is for while/for loops.
14121 LABELED_BLOCK_EXPR the order is reversed for do loops)
14122 LABEL_DECL (a continue occuring here branches at the
14123 BODY end of this labeled block)
14124 INCREMENT (if any)
14126 REVERSED, if non zero, tells that the loop condition expr comes
14127 after the body, like in the do-while loop.
14129 To obtain a loop, the loop body structure described above is
14130 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14132 LABELED_BLOCK_EXPR
14133 LABEL_DECL (use this label to exit the loop)
14134 LOOP_EXPR
14135 <structure described above> */
14137 static tree
14138 build_loop_body (location, condition, reversed)
14139 int location;
14140 tree condition;
14141 int reversed;
14143 tree first, second, body;
14145 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14146 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14147 condition = build_debugable_stmt (location, condition);
14148 TREE_SIDE_EFFECTS (condition) = 1;
14150 body = build_labeled_block (0, continue_identifier_node);
14151 first = (reversed ? body : condition);
14152 second = (reversed ? condition : body);
14153 return
14154 build (COMPOUND_EXPR, NULL_TREE,
14155 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14158 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14159 their order) on the current loop. Unlink the current loop from the
14160 loop list. */
14162 static tree
14163 finish_loop_body (location, condition, body, reversed)
14164 int location;
14165 tree condition, body;
14166 int reversed;
14168 tree to_return = ctxp->current_loop;
14169 tree loop_body = LOOP_EXPR_BODY (to_return);
14170 if (condition)
14172 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14173 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14174 The real EXIT_EXPR is one operand further. */
14175 EXPR_WFL_LINECOL (cnode) = location;
14176 /* This one is for accurate error reports */
14177 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14178 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14180 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14181 POP_LOOP ();
14182 return to_return;
14185 /* Tailored version of finish_loop_body for FOR loops, when FOR
14186 loops feature the condition part */
14188 static tree
14189 finish_for_loop (location, condition, update, body)
14190 int location;
14191 tree condition, update, body;
14193 /* Put the condition and the loop body in place */
14194 tree loop = finish_loop_body (location, condition, body, 0);
14195 /* LOOP is the current loop which has been now popped of the loop
14196 stack. Install the update block */
14197 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14198 return loop;
14201 /* Try to find the loop a block might be related to. This comprises
14202 the case where the LOOP_EXPR is found as the second operand of a
14203 COMPOUND_EXPR, because the loop happens to have an initialization
14204 part, then expressed as the first operand of the COMPOUND_EXPR. If
14205 the search finds something, 1 is returned. Otherwise, 0 is
14206 returned. The search is assumed to start from a
14207 LABELED_BLOCK_EXPR's block. */
14209 static tree
14210 search_loop (statement)
14211 tree statement;
14213 if (TREE_CODE (statement) == LOOP_EXPR)
14214 return statement;
14216 if (TREE_CODE (statement) == BLOCK)
14217 statement = BLOCK_SUBBLOCKS (statement);
14218 else
14219 return NULL_TREE;
14221 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14222 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14223 statement = TREE_OPERAND (statement, 1);
14225 return (TREE_CODE (statement) == LOOP_EXPR
14226 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14229 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14230 returned otherwise. */
14232 static int
14233 labeled_block_contains_loop_p (block, loop)
14234 tree block, loop;
14236 if (!block)
14237 return 0;
14239 if (LABELED_BLOCK_BODY (block) == loop)
14240 return 1;
14242 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14243 return 1;
14245 return 0;
14248 /* If the loop isn't surrounded by a labeled statement, create one and
14249 insert LOOP as its body. */
14251 static tree
14252 patch_loop_statement (loop)
14253 tree loop;
14255 tree loop_label;
14257 TREE_TYPE (loop) = void_type_node;
14258 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14259 return loop;
14261 loop_label = build_labeled_block (0, NULL_TREE);
14262 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14263 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14264 LABELED_BLOCK_BODY (loop_label) = loop;
14265 PUSH_LABELED_BLOCK (loop_label);
14266 return loop_label;
14269 /* 14.13, 14.14: break and continue Statements */
14271 /* Build a break or a continue statement. a null NAME indicates an
14272 unlabeled break/continue statement. */
14274 static tree
14275 build_bc_statement (location, is_break, name)
14276 int location, is_break;
14277 tree name;
14279 tree break_continue, label_block_expr = NULL_TREE;
14281 if (name)
14283 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14284 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14285 /* Null means that we don't have a target for this named
14286 break/continue. In this case, we make the target to be the
14287 label name, so that the error can be reported accuratly in
14288 patch_bc_statement. */
14289 label_block_expr = EXPR_WFL_NODE (name);
14291 /* Unlabeled break/continue will be handled during the
14292 break/continue patch operation */
14293 break_continue
14294 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14296 IS_BREAK_STMT_P (break_continue) = is_break;
14297 TREE_SIDE_EFFECTS (break_continue) = 1;
14298 EXPR_WFL_LINECOL (break_continue) = location;
14299 break_continue = build_debugable_stmt (location, break_continue);
14300 return break_continue;
14303 /* Verification of a break/continue statement. */
14305 static tree
14306 patch_bc_statement (node)
14307 tree node;
14309 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14310 tree labeled_block = ctxp->current_labeled_block;
14311 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14313 /* Having an identifier here means that the target is unknown. */
14314 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14316 parse_error_context (wfl_operator, "No label definition found for `%s'",
14317 IDENTIFIER_POINTER (bc_label));
14318 return error_mark_node;
14320 if (! IS_BREAK_STMT_P (node))
14322 /* It's a continue statement. */
14323 for (;; labeled_block = TREE_CHAIN (labeled_block))
14325 if (labeled_block == NULL_TREE)
14327 if (bc_label == NULL_TREE)
14328 parse_error_context (wfl_operator,
14329 "`continue' must be in loop");
14330 else
14331 parse_error_context
14332 (wfl_operator, "continue label `%s' does not name a loop",
14333 IDENTIFIER_POINTER (bc_label));
14334 return error_mark_node;
14336 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14337 == continue_identifier_node)
14338 && (bc_label == NULL_TREE
14339 || TREE_CHAIN (labeled_block) == bc_label))
14341 bc_label = labeled_block;
14342 break;
14346 else if (!bc_label)
14348 for (;; labeled_block = TREE_CHAIN (labeled_block))
14350 if (labeled_block == NULL_TREE)
14352 parse_error_context (wfl_operator,
14353 "`break' must be in loop or switch");
14354 return error_mark_node;
14356 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14357 if (TREE_CODE (target_stmt) == SWITCH_EXPR
14358 || search_loop (target_stmt))
14360 bc_label = labeled_block;
14361 break;
14366 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14367 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14369 /* Our break/continue don't return values. */
14370 TREE_TYPE (node) = void_type_node;
14371 /* Encapsulate the break within a compound statement so that it's
14372 expanded all the times by expand_expr (and not clobbered
14373 sometimes, like after a if statement) */
14374 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14375 TREE_SIDE_EFFECTS (node) = 1;
14376 return node;
14379 /* Process the exit expression belonging to a loop. Its type must be
14380 boolean. */
14382 static tree
14383 patch_exit_expr (node)
14384 tree node;
14386 tree expression = TREE_OPERAND (node, 0);
14387 TREE_TYPE (node) = error_mark_node;
14388 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14390 /* The type of expression must be boolean */
14391 if (TREE_TYPE (expression) != boolean_type_node)
14393 parse_error_context
14394 (wfl_operator,
14395 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
14396 lang_printable_name (TREE_TYPE (expression), 0));
14397 return error_mark_node;
14399 /* Now we know things are allright, invert the condition, fold and
14400 return */
14401 TREE_OPERAND (node, 0) =
14402 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
14404 if (! integer_zerop (TREE_OPERAND (node, 0))
14405 && ctxp->current_loop != NULL_TREE
14406 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14407 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14408 if (! integer_onep (TREE_OPERAND (node, 0)))
14409 CAN_COMPLETE_NORMALLY (node) = 1;
14412 TREE_TYPE (node) = void_type_node;
14413 return node;
14416 /* 14.9 Switch statement */
14418 static tree
14419 patch_switch_statement (node)
14420 tree node;
14422 tree se = TREE_OPERAND (node, 0), se_type;
14424 /* Complete the switch expression */
14425 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14426 se_type = TREE_TYPE (se);
14427 /* The type of the switch expression must be char, byte, short or
14428 int */
14429 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
14431 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14432 parse_error_context (wfl_operator,
14433 "Incompatible type for `switch'. Can't convert `%s' to `int'",
14434 lang_printable_name (se_type, 0));
14435 /* This is what java_complete_tree will check */
14436 TREE_OPERAND (node, 0) = error_mark_node;
14437 return error_mark_node;
14440 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
14442 /* Ready to return */
14443 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
14445 TREE_TYPE (node) = error_mark_node;
14446 return error_mark_node;
14448 TREE_TYPE (node) = void_type_node;
14449 TREE_SIDE_EFFECTS (node) = 1;
14450 CAN_COMPLETE_NORMALLY (node)
14451 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14452 || ! SWITCH_HAS_DEFAULT (node);
14453 return node;
14456 /* 14.18 The try/catch statements */
14458 static tree
14459 build_try_statement (location, try_block, catches)
14460 int location;
14461 tree try_block, catches;
14463 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
14464 EXPR_WFL_LINECOL (node) = location;
14465 return node;
14468 static tree
14469 build_try_finally_statement (location, try_block, finally)
14470 int location;
14471 tree try_block, finally;
14473 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14474 EXPR_WFL_LINECOL (node) = location;
14475 return node;
14478 static tree
14479 patch_try_statement (node)
14480 tree node;
14482 int error_found = 0;
14483 tree try = TREE_OPERAND (node, 0);
14484 /* Exception handlers are considered in left to right order */
14485 tree catch = nreverse (TREE_OPERAND (node, 1));
14486 tree current, caught_type_list = NULL_TREE;
14488 /* Check catch clauses, if any. Every time we find an error, we try
14489 to process the next catch clause. We process the catch clause before
14490 the try block so that when processing the try block we can check thrown
14491 exceptions againts the caught type list. */
14492 for (current = catch; current; current = TREE_CHAIN (current))
14494 tree carg_decl, carg_type;
14495 tree sub_current, catch_block, catch_clause;
14496 int unreachable;
14498 /* At this point, the structure of the catch clause is
14499 CATCH_EXPR (catch node)
14500 BLOCK (with the decl of the parameter)
14501 COMPOUND_EXPR
14502 MODIFY_EXPR (assignment of the catch parameter)
14503 BLOCK (catch clause block)
14505 catch_clause = TREE_OPERAND (current, 0);
14506 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14507 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14509 /* Catch clauses can't have more than one parameter declared,
14510 but it's already enforced by the grammar. Make sure that the
14511 only parameter of the clause statement in of class Throwable
14512 or a subclass of Throwable, but that was done earlier. The
14513 catch clause parameter type has also been resolved. */
14515 /* Just make sure that the catch clause parameter type inherits
14516 from java.lang.Throwable */
14517 if (!inherits_from_p (carg_type, throwable_type_node))
14519 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14520 parse_error_context (wfl_operator,
14521 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
14522 lang_printable_name (carg_type, 0));
14523 error_found = 1;
14524 continue;
14527 /* Partial check for unreachable catch statement: The catch
14528 clause is reachable iff is no earlier catch block A in
14529 the try statement such that the type of the catch
14530 clause's parameter is the same as or a subclass of the
14531 type of A's parameter */
14532 unreachable = 0;
14533 for (sub_current = catch;
14534 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14536 tree sub_catch_clause, decl;
14537 sub_catch_clause = TREE_OPERAND (sub_current, 0);
14538 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14540 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14542 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14543 parse_error_context
14544 (wfl_operator,
14545 "`catch' not reached because of the catch clause at line %d",
14546 EXPR_WFL_LINENO (sub_current));
14547 unreachable = error_found = 1;
14548 break;
14551 /* Complete the catch clause block */
14552 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14553 if (catch_block == error_mark_node)
14555 error_found = 1;
14556 continue;
14558 if (CAN_COMPLETE_NORMALLY (catch_block))
14559 CAN_COMPLETE_NORMALLY (node) = 1;
14560 TREE_OPERAND (current, 0) = catch_block;
14562 if (unreachable)
14563 continue;
14565 /* Things to do here: the exception must be thrown */
14567 /* Link this type to the caught type list */
14568 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
14571 PUSH_EXCEPTIONS (caught_type_list);
14572 if ((try = java_complete_tree (try)) == error_mark_node)
14573 error_found = 1;
14574 if (CAN_COMPLETE_NORMALLY (try))
14575 CAN_COMPLETE_NORMALLY (node) = 1;
14576 POP_EXCEPTIONS ();
14578 /* Verification ends here */
14579 if (error_found)
14580 return error_mark_node;
14582 TREE_OPERAND (node, 0) = try;
14583 TREE_OPERAND (node, 1) = catch;
14584 TREE_TYPE (node) = void_type_node;
14585 return node;
14588 /* 14.17 The synchronized Statement */
14590 static tree
14591 patch_synchronized_statement (node, wfl_op1)
14592 tree node, wfl_op1;
14594 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
14595 tree block = TREE_OPERAND (node, 1);
14597 tree enter, exit, expr_decl, assignment;
14599 if (expr == error_mark_node)
14601 block = java_complete_tree (block);
14602 return expr;
14605 /* The TYPE of expr must be a reference type */
14606 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
14608 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14609 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
14610 lang_printable_name (TREE_TYPE (expr), 0));
14611 return error_mark_node;
14614 if (flag_emit_xref)
14616 TREE_OPERAND (node, 0) = expr;
14617 TREE_OPERAND (node, 1) = java_complete_tree (block);
14618 CAN_COMPLETE_NORMALLY (node) = 1;
14619 return node;
14622 /* Generate a try-finally for the synchronized statement, except
14623 that the handler that catches all throw exception calls
14624 _Jv_MonitorExit and then rethrow the exception.
14625 The synchronized statement is then implemented as:
14626 TRY
14628 _Jv_MonitorEnter (expression)
14629 synchronized_block
14630 _Jv_MonitorExit (expression)
14632 CATCH_ALL
14634 e = _Jv_exception_info ();
14635 _Jv_MonitorExit (expression)
14636 Throw (e);
14637 } */
14639 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14640 BUILD_MONITOR_ENTER (enter, expr_decl);
14641 BUILD_MONITOR_EXIT (exit, expr_decl);
14642 CAN_COMPLETE_NORMALLY (enter) = 1;
14643 CAN_COMPLETE_NORMALLY (exit) = 1;
14644 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14645 TREE_SIDE_EFFECTS (assignment) = 1;
14646 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14647 build (COMPOUND_EXPR, NULL_TREE,
14648 build (WITH_CLEANUP_EXPR, NULL_TREE,
14649 build (COMPOUND_EXPR, NULL_TREE,
14650 assignment, enter),
14651 NULL_TREE, exit),
14652 block));
14653 node = build_expr_block (node, expr_decl);
14655 return java_complete_tree (node);
14658 /* 14.16 The throw Statement */
14660 static tree
14661 patch_throw_statement (node, wfl_op1)
14662 tree node, wfl_op1;
14664 tree expr = TREE_OPERAND (node, 0);
14665 tree type = TREE_TYPE (expr);
14666 int unchecked_ok = 0, tryblock_throws_ok = 0;
14668 /* Thrown expression must be assignable to java.lang.Throwable */
14669 if (!try_reference_assignconv (throwable_type_node, expr))
14671 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14672 parse_error_context (wfl_operator,
14673 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
14674 lang_printable_name (type, 0));
14675 /* If the thrown expression was a reference, we further the
14676 compile-time check. */
14677 if (!JREFERENCE_TYPE_P (type))
14678 return error_mark_node;
14681 /* At least one of the following must be true */
14683 /* The type of the throw expression is a not checked exception,
14684 i.e. is a unchecked expression. */
14685 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
14687 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14688 /* An instance can't throw a checked excetion unless that exception
14689 is explicitely declared in the `throws' clause of each
14690 constructor. This doesn't apply to anonymous classes, since they
14691 don't have declared constructors. */
14692 if (!unchecked_ok
14693 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14695 tree current;
14696 for (current = TYPE_METHODS (current_class); current;
14697 current = TREE_CHAIN (current))
14698 if (DECL_CONSTRUCTOR_P (current)
14699 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14701 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)",
14702 lang_printable_name (TREE_TYPE (expr), 0));
14703 return error_mark_node;
14707 /* Throw is contained in a try statement and at least one catch
14708 clause can receive the thrown expression or the current method is
14709 declared to throw such an exception. Or, the throw statement is
14710 contained in a method or constructor declaration and the type of
14711 the Expression is assignable to at least one type listed in the
14712 throws clause the declaration. */
14713 if (!unchecked_ok)
14714 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
14715 if (!(unchecked_ok || tryblock_throws_ok))
14717 /* If there is a surrounding try block that has no matching
14718 clatch clause, report it first. A surrounding try block exits
14719 only if there is something after the list of checked
14720 exception thrown by the current function (if any). */
14721 if (IN_TRY_BLOCK_P ())
14722 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
14723 lang_printable_name (type, 0));
14724 /* If we have no surrounding try statement and the method doesn't have
14725 any throws, report it now. FIXME */
14727 /* We report that the exception can't be throw from a try block
14728 in all circumstances but when the `throw' is inside a static
14729 block. */
14730 else if (!EXCEPTIONS_P (currently_caught_type_list)
14731 && !tryblock_throws_ok)
14733 if (DECL_CLINIT_P (current_function_decl))
14734 parse_error_context (wfl_operator,
14735 "Checked exception `%s' can't be thrown in initializer",
14736 lang_printable_name (type, 0));
14737 else
14738 parse_error_context (wfl_operator,
14739 "Checked exception `%s' isn't thrown from a `try' block",
14740 lang_printable_name (type, 0));
14742 /* Otherwise, the current method doesn't have the appropriate
14743 throws declaration */
14744 else
14745 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
14746 lang_printable_name (type, 0));
14747 return error_mark_node;
14750 if (! flag_emit_class_files && ! flag_emit_xref)
14751 BUILD_THROW (node, expr);
14753 /* If doing xrefs, keep the location where the `throw' was seen. */
14754 if (flag_emit_xref)
14755 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
14756 return node;
14759 /* Check that exception said to be thrown by method DECL can be
14760 effectively caught from where DECL is invoked. */
14762 static void
14763 check_thrown_exceptions (location, decl)
14764 int location;
14765 tree decl;
14767 tree throws;
14768 /* For all the unchecked exceptions thrown by DECL */
14769 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14770 throws = TREE_CHAIN (throws))
14771 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
14773 #if 1
14774 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14775 if (DECL_NAME (decl) == get_identifier ("clone"))
14776 continue;
14777 #endif
14778 EXPR_WFL_LINECOL (wfl_operator) = location;
14779 if (DECL_FINIT_P (current_function_decl))
14780 parse_error_context
14781 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14782 lang_printable_name (TREE_VALUE (throws), 0));
14783 else
14785 parse_error_context
14786 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
14787 lang_printable_name (TREE_VALUE (throws), 0),
14788 (DECL_INIT_P (current_function_decl) ?
14789 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14790 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14795 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
14796 try-catch blocks, OR is listed in the `throws' clause of the
14797 current method. */
14799 static int
14800 check_thrown_exceptions_do (exception)
14801 tree exception;
14803 tree list = currently_caught_type_list;
14804 resolve_and_layout (exception, NULL_TREE);
14805 /* First, all the nested try-catch-finally at that stage. The
14806 last element contains `throws' clause exceptions, if any. */
14807 if (IS_UNCHECKED_EXCEPTION_P (exception))
14808 return 1;
14809 while (list)
14811 tree caught;
14812 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14813 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14814 return 1;
14815 list = TREE_CHAIN (list);
14817 return 0;
14820 static void
14821 purge_unchecked_exceptions (mdecl)
14822 tree mdecl;
14824 tree throws = DECL_FUNCTION_THROWS (mdecl);
14825 tree new = NULL_TREE;
14827 while (throws)
14829 tree next = TREE_CHAIN (throws);
14830 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
14832 TREE_CHAIN (throws) = new;
14833 new = throws;
14835 throws = next;
14837 /* List is inverted here, but it doesn't matter */
14838 DECL_FUNCTION_THROWS (mdecl) = new;
14841 /* 15.24 Conditional Operator ?: */
14843 static tree
14844 patch_conditional_expr (node, wfl_cond, wfl_op1)
14845 tree node, wfl_cond, wfl_op1;
14847 tree cond = TREE_OPERAND (node, 0);
14848 tree op1 = TREE_OPERAND (node, 1);
14849 tree op2 = TREE_OPERAND (node, 2);
14850 tree resulting_type = NULL_TREE;
14851 tree t1, t2, patched;
14852 int error_found = 0;
14854 /* Operands of ?: might be StringBuffers crafted as a result of a
14855 string concatenation. Obtain a descent operand here. */
14856 if ((patched = patch_string (op1)))
14857 TREE_OPERAND (node, 1) = op1 = patched;
14858 if ((patched = patch_string (op2)))
14859 TREE_OPERAND (node, 2) = op2 = patched;
14861 t1 = TREE_TYPE (op1);
14862 t2 = TREE_TYPE (op2);
14864 /* The first expression must be a boolean */
14865 if (TREE_TYPE (cond) != boolean_type_node)
14867 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
14868 parse_error_context (wfl_operator,
14869 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
14870 lang_printable_name (TREE_TYPE (cond), 0));
14871 error_found = 1;
14874 /* Second and third can be numeric, boolean (i.e. primitive),
14875 references or null. Anything else results in an error */
14876 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
14877 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
14878 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
14879 || (t1 == boolean_type_node && t2 == boolean_type_node)))
14880 error_found = 1;
14882 /* Determine the type of the conditional expression. Same types are
14883 easy to deal with */
14884 else if (t1 == t2)
14885 resulting_type = t1;
14887 /* There are different rules for numeric types */
14888 else if (JNUMERIC_TYPE_P (t1))
14890 /* if byte/short found, the resulting type is short */
14891 if ((t1 == byte_type_node && t2 == short_type_node)
14892 || (t1 == short_type_node && t2 == byte_type_node))
14893 resulting_type = short_type_node;
14895 /* If t1 is a constant int and t2 is of type byte, short or char
14896 and t1's value fits in t2, then the resulting type is t2 */
14897 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
14898 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
14899 resulting_type = t2;
14901 /* If t2 is a constant int and t1 is of type byte, short or char
14902 and t2's value fits in t1, then the resulting type is t1 */
14903 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
14904 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
14905 resulting_type = t1;
14907 /* Otherwise, binary numeric promotion is applied and the
14908 resulting type is the promoted type of operand 1 and 2 */
14909 else
14910 resulting_type = binary_numeric_promotion (t1, t2,
14911 &TREE_OPERAND (node, 1),
14912 &TREE_OPERAND (node, 2));
14915 /* Cases of a reference and a null type */
14916 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
14917 resulting_type = t1;
14919 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
14920 resulting_type = t2;
14922 /* Last case: different reference types. If a type can be converted
14923 into the other one by assignment conversion, the latter
14924 determines the type of the expression */
14925 else if ((resulting_type = try_reference_assignconv (t1, op2)))
14926 resulting_type = promote_type (t1);
14928 else if ((resulting_type = try_reference_assignconv (t2, op1)))
14929 resulting_type = promote_type (t2);
14931 /* If we don't have any resulting type, we're in trouble */
14932 if (!resulting_type)
14934 char *t = xstrdup (lang_printable_name (t1, 0));
14935 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14936 parse_error_context (wfl_operator,
14937 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
14938 t, lang_printable_name (t2, 0));
14939 free (t);
14940 error_found = 1;
14943 if (error_found)
14945 TREE_TYPE (node) = error_mark_node;
14946 return error_mark_node;
14949 TREE_TYPE (node) = resulting_type;
14950 TREE_SET_CODE (node, COND_EXPR);
14951 CAN_COMPLETE_NORMALLY (node) = 1;
14952 return node;
14955 /* Try to constant fold NODE.
14956 If NODE is not a constant expression, return NULL_EXPR.
14957 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
14959 static tree
14960 fold_constant_for_init (node, context)
14961 tree node;
14962 tree context;
14964 tree op0, op1, val;
14965 enum tree_code code = TREE_CODE (node);
14967 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
14968 return node;
14970 switch (code)
14972 case PLUS_EXPR:
14973 case MINUS_EXPR:
14974 case MULT_EXPR:
14975 case TRUNC_MOD_EXPR:
14976 case RDIV_EXPR:
14977 case LSHIFT_EXPR:
14978 case RSHIFT_EXPR:
14979 case URSHIFT_EXPR:
14980 case BIT_AND_EXPR:
14981 case BIT_XOR_EXPR:
14982 case BIT_IOR_EXPR:
14983 case TRUTH_ANDIF_EXPR:
14984 case TRUTH_ORIF_EXPR:
14985 case EQ_EXPR:
14986 case NE_EXPR:
14987 case GT_EXPR:
14988 case GE_EXPR:
14989 case LT_EXPR:
14990 case LE_EXPR:
14991 op0 = TREE_OPERAND (node, 0);
14992 op1 = TREE_OPERAND (node, 1);
14993 val = fold_constant_for_init (op0, context);
14994 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14995 return NULL_TREE;
14996 TREE_OPERAND (node, 0) = val;
14997 val = fold_constant_for_init (op1, context);
14998 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14999 return NULL_TREE;
15000 TREE_OPERAND (node, 1) = val;
15001 return patch_binop (node, op0, op1);
15003 case UNARY_PLUS_EXPR:
15004 case NEGATE_EXPR:
15005 case TRUTH_NOT_EXPR:
15006 case BIT_NOT_EXPR:
15007 case CONVERT_EXPR:
15008 op0 = TREE_OPERAND (node, 0);
15009 val = fold_constant_for_init (op0, context);
15010 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15011 return NULL_TREE;
15012 TREE_OPERAND (node, 0) = val;
15013 return patch_unaryop (node, op0);
15014 break;
15016 case COND_EXPR:
15017 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15018 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15019 return NULL_TREE;
15020 TREE_OPERAND (node, 0) = val;
15021 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15022 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15023 return NULL_TREE;
15024 TREE_OPERAND (node, 1) = val;
15025 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15026 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15027 return NULL_TREE;
15028 TREE_OPERAND (node, 2) = val;
15029 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15030 : TREE_OPERAND (node, 2);
15032 case VAR_DECL:
15033 case FIELD_DECL:
15034 if (! FIELD_FINAL (node)
15035 || DECL_INITIAL (node) == NULL_TREE)
15036 return NULL_TREE;
15037 val = DECL_INITIAL (node);
15038 /* Guard against infinite recursion. */
15039 DECL_INITIAL (node) = NULL_TREE;
15040 val = fold_constant_for_init (val, node);
15041 DECL_INITIAL (node) = val;
15042 return val;
15044 case EXPR_WITH_FILE_LOCATION:
15045 /* Compare java_complete_tree and resolve_expression_name. */
15046 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15047 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15049 tree name = EXPR_WFL_NODE (node);
15050 tree decl;
15051 if (PRIMARY_P (node))
15052 return NULL_TREE;
15053 else if (! QUALIFIED_P (name))
15055 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15056 if (decl == NULL_TREE
15057 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15058 return NULL_TREE;
15059 return fold_constant_for_init (decl, decl);
15061 else
15063 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15064 qualify_ambiguous_name (node);
15065 if (resolve_field_access (node, &decl, NULL)
15066 && decl != NULL_TREE)
15067 return fold_constant_for_init (decl, decl);
15068 return NULL_TREE;
15071 else
15073 op0 = TREE_OPERAND (node, 0);
15074 val = fold_constant_for_init (op0, context);
15075 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15076 return NULL_TREE;
15077 TREE_OPERAND (node, 0) = val;
15078 return val;
15081 #ifdef USE_COMPONENT_REF
15082 case IDENTIFIER:
15083 case COMPONENT_REF:
15085 #endif
15087 default:
15088 return NULL_TREE;
15092 #ifdef USE_COMPONENT_REF
15093 /* Context is 'T' for TypeName, 'P' for PackageName,
15094 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15096 tree
15097 resolve_simple_name (name, context)
15098 tree name;
15099 int context;
15103 tree
15104 resolve_qualified_name (name, context)
15105 tree name;
15106 int context;
15109 #endif