2000-10-18 Alexandre Petit-Bianco <apbianco@cygnus.com>
[official-gcc.git] / gcc / java / parse.y
blobb3c2d87cba717bf8efa39a95205621792306bec7
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"
68 #include "ggc.h"
70 #ifndef DIR_SEPARATOR
71 #define DIR_SEPARATOR '/'
72 #endif
74 /* Local function prototypes */
75 static char *java_accstring_lookup PARAMS ((int));
76 static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
77 static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
78 static tree create_class PARAMS ((int, tree, tree, tree));
79 static tree create_interface PARAMS ((int, tree, tree));
80 static void end_class_declaration PARAMS ((int));
81 static tree find_field PARAMS ((tree, tree));
82 static tree lookup_field_wrapper PARAMS ((tree, tree));
83 static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
84 static void register_fields PARAMS ((int, tree, tree));
85 static tree parser_qualified_classname PARAMS ((tree));
86 static int parser_check_super PARAMS ((tree, tree, tree));
87 static int parser_check_super_interface PARAMS ((tree, tree, tree));
88 static void check_modifiers_consistency PARAMS ((int));
89 static tree lookup_cl PARAMS ((tree));
90 static tree lookup_java_method2 PARAMS ((tree, tree, int));
91 static tree method_header PARAMS ((int, tree, tree, tree));
92 static void fix_method_argument_names PARAMS ((tree ,tree));
93 static tree method_declarator PARAMS ((tree, tree));
94 static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
95 ATTRIBUTE_PRINTF_2;
96 static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list));
97 static void parse_ctor_invocation_error PARAMS ((void));
98 static tree parse_jdk1_1_error PARAMS ((const char *));
99 static void complete_class_report_errors PARAMS ((jdep *));
100 static int process_imports PARAMS ((void));
101 static void read_import_dir PARAMS ((tree));
102 static int find_in_imports_on_demand PARAMS ((tree));
103 static void find_in_imports PARAMS ((tree));
104 static void check_inner_class_access PARAMS ((tree, tree, tree));
105 static int check_pkg_class_access PARAMS ((tree, tree));
106 static void register_package PARAMS ((tree));
107 static tree resolve_package PARAMS ((tree, tree *));
108 static tree lookup_package_type PARAMS ((const char *, int));
109 static tree lookup_package_type_and_set_next PARAMS ((const char *, int, tree *));
110 static tree resolve_class PARAMS ((tree, tree, tree, tree));
111 static void declare_local_variables PARAMS ((int, tree, tree));
112 static void source_start_java_method PARAMS ((tree));
113 static void source_end_java_method PARAMS ((void));
114 static void expand_start_java_method PARAMS ((tree));
115 static tree find_name_in_single_imports PARAMS ((tree));
116 static void check_abstract_method_header PARAMS ((tree));
117 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
118 static tree resolve_expression_name PARAMS ((tree, tree *));
119 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
120 static int check_class_interface_creation PARAMS ((int, int, tree,
121 tree, tree, tree));
122 static tree patch_method_invocation PARAMS ((tree, tree, tree,
123 int *, tree *));
124 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
125 static tree resolve_and_layout PARAMS ((tree, tree));
126 static tree qualify_and_find PARAMS ((tree, tree, tree));
127 static tree resolve_no_layout PARAMS ((tree, tree));
128 static int invocation_mode PARAMS ((tree, int));
129 static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
130 tree, tree));
131 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
132 tree *, tree *));
133 static tree find_most_specific_methods_list PARAMS ((tree));
134 static int argument_types_convertible PARAMS ((tree, tree));
135 static tree patch_invoke PARAMS ((tree, tree, tree));
136 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
137 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
138 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
139 static tree obtain_incomplete_type PARAMS ((tree));
140 static tree java_complete_lhs PARAMS ((tree));
141 static tree java_complete_tree PARAMS ((tree));
142 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
143 static int analyze_clinit_body PARAMS ((tree));
144 static int maybe_yank_clinit PARAMS ((tree));
145 static void java_complete_expand_method PARAMS ((tree));
146 static int unresolved_type_p PARAMS ((tree, tree *));
147 static void create_jdep_list PARAMS ((struct parser_ctxt *));
148 static tree build_expr_block PARAMS ((tree, tree));
149 static tree enter_block PARAMS ((void));
150 static tree enter_a_block PARAMS ((tree));
151 static tree exit_block PARAMS ((void));
152 static tree lookup_name_in_blocks PARAMS ((tree));
153 static void maybe_absorb_scoping_blocks PARAMS ((void));
154 static tree build_method_invocation PARAMS ((tree, tree));
155 static tree build_new_invocation PARAMS ((tree, tree));
156 static tree build_assignment PARAMS ((int, int, tree, tree));
157 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
158 static int check_final_assignment PARAMS ((tree ,tree));
159 static tree patch_assignment PARAMS ((tree, tree, tree ));
160 static tree patch_binop PARAMS ((tree, tree, tree));
161 static tree build_unaryop PARAMS ((int, int, tree));
162 static tree build_incdec PARAMS ((int, int, tree, int));
163 static tree patch_unaryop PARAMS ((tree, tree));
164 static tree build_cast PARAMS ((int, tree, tree));
165 static tree build_null_of_type PARAMS ((tree));
166 static tree patch_cast PARAMS ((tree, tree));
167 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
168 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
169 static int valid_cast_to_p PARAMS ((tree, tree));
170 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
171 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
172 static tree try_reference_assignconv PARAMS ((tree, tree));
173 static tree build_unresolved_array_type PARAMS ((tree));
174 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
175 static tree build_array_ref PARAMS ((int, tree, tree));
176 static tree patch_array_ref PARAMS ((tree));
177 static tree make_qualified_name PARAMS ((tree, tree, int));
178 static tree merge_qualified_name PARAMS ((tree, tree));
179 static tree make_qualified_primary PARAMS ((tree, tree, int));
180 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
181 tree *, tree *));
182 static void qualify_ambiguous_name PARAMS ((tree));
183 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
184 static tree build_newarray_node PARAMS ((tree, tree, int));
185 static tree patch_newarray PARAMS ((tree));
186 static tree resolve_type_during_patch PARAMS ((tree));
187 static tree build_this PARAMS ((int));
188 static tree build_wfl_wrap PARAMS ((tree, int));
189 static tree build_return PARAMS ((int, tree));
190 static tree patch_return PARAMS ((tree));
191 static tree maybe_access_field PARAMS ((tree, tree, tree));
192 static int complete_function_arguments PARAMS ((tree));
193 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
194 tree, tree));
195 static int not_accessible_p PARAMS ((tree, tree, int));
196 static void check_deprecation PARAMS ((tree, tree));
197 static int class_in_current_package PARAMS ((tree));
198 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
199 static tree patch_if_else_statement PARAMS ((tree));
200 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
201 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
202 static tree patch_exit_expr PARAMS ((tree));
203 static tree build_labeled_block PARAMS ((int, tree));
204 static tree finish_labeled_statement PARAMS ((tree, tree));
205 static tree build_bc_statement PARAMS ((int, int, tree));
206 static tree patch_bc_statement PARAMS ((tree));
207 static tree patch_loop_statement PARAMS ((tree));
208 static tree build_new_loop PARAMS ((tree));
209 static tree build_loop_body PARAMS ((int, tree, int));
210 static tree finish_loop_body PARAMS ((int, tree, tree, int));
211 static tree build_debugable_stmt PARAMS ((int, tree));
212 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
213 static tree patch_switch_statement PARAMS ((tree));
214 static tree string_constant_concatenation PARAMS ((tree, tree));
215 static tree build_string_concatenation PARAMS ((tree, tree));
216 static tree patch_string_cst PARAMS ((tree));
217 static tree patch_string PARAMS ((tree));
218 static tree build_try_statement PARAMS ((int, tree, tree));
219 static tree build_try_finally_statement PARAMS ((int, tree, tree));
220 static tree patch_try_statement PARAMS ((tree));
221 static tree patch_synchronized_statement PARAMS ((tree, tree));
222 static tree patch_throw_statement PARAMS ((tree, tree));
223 static void check_thrown_exceptions PARAMS ((int, tree));
224 static int check_thrown_exceptions_do PARAMS ((tree));
225 static void purge_unchecked_exceptions PARAMS ((tree));
226 static void check_throws_clauses PARAMS ((tree, tree, tree));
227 static void finish_method_declaration PARAMS ((tree));
228 static tree build_super_invocation PARAMS ((tree));
229 static int verify_constructor_circularity PARAMS ((tree, tree));
230 static char *constructor_circularity_msg PARAMS ((tree, tree));
231 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
232 int, int));
233 static const char *get_printable_method_name PARAMS ((tree));
234 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
235 static tree generate_finit PARAMS ((tree));
236 static void add_instance_initializer PARAMS ((tree));
237 static void fix_constructors PARAMS ((tree));
238 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
239 tree, int *));
240 static void craft_constructor PARAMS ((tree, tree));
241 static int verify_constructor_super PARAMS ((tree));
242 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
243 static void start_artificial_method_body PARAMS ((tree));
244 static void end_artificial_method_body PARAMS ((tree));
245 static int check_method_redefinition PARAMS ((tree, tree));
246 static int reset_method_name PARAMS ((tree));
247 static int check_method_types_complete PARAMS ((tree));
248 static void java_check_regular_methods PARAMS ((tree));
249 static void java_check_abstract_methods PARAMS ((tree));
250 static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
251 static void unreachable_stmt_error PARAMS ((tree));
252 static tree find_expr_with_wfl PARAMS ((tree));
253 static void missing_return_error PARAMS ((tree));
254 static tree build_new_array_init PARAMS ((int, tree));
255 static tree patch_new_array_init PARAMS ((tree, tree));
256 static tree maybe_build_array_element_wfl PARAMS ((tree));
257 static int array_constructor_check_entry PARAMS ((tree, tree));
258 static const char *purify_type_name PARAMS ((const char *));
259 static tree fold_constant_for_init PARAMS ((tree, tree));
260 static tree strip_out_static_field_access_decl PARAMS ((tree));
261 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
262 static void static_ref_err PARAMS ((tree, tree, tree));
263 static void parser_add_interface PARAMS ((tree, tree, tree));
264 static void add_superinterfaces PARAMS ((tree, tree));
265 static tree jdep_resolve_class PARAMS ((jdep *));
266 static int note_possible_classname PARAMS ((const char *, int));
267 static void java_complete_expand_classes PARAMS ((void));
268 static void java_complete_expand_class PARAMS ((tree));
269 static void java_complete_expand_methods PARAMS ((tree));
270 static tree cut_identifier_in_qualified PARAMS ((tree));
271 static tree java_stabilize_reference PARAMS ((tree));
272 static tree do_unary_numeric_promotion PARAMS ((tree));
273 static char * operator_string PARAMS ((tree));
274 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
275 static tree merge_string_cste PARAMS ((tree, tree, int));
276 static tree java_refold PARAMS ((tree));
277 static int java_decl_equiv PARAMS ((tree, tree));
278 static int binop_compound_p PARAMS ((enum tree_code));
279 static tree search_loop PARAMS ((tree));
280 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
281 static int check_abstract_method_definitions PARAMS ((int, tree, tree));
282 static void java_check_abstract_method_definitions PARAMS ((tree));
283 static void java_debug_context_do PARAMS ((int));
284 static void java_parser_context_push_initialized_field PARAMS ((void));
285 static void java_parser_context_pop_initialized_field PARAMS ((void));
286 static tree reorder_static_initialized PARAMS ((tree));
287 static void java_parser_context_suspend PARAMS ((void));
288 static void java_parser_context_resume PARAMS ((void));
290 /* JDK 1.1 work. FIXME */
292 static tree maybe_make_nested_class_name PARAMS ((tree));
293 static void make_nested_class_name PARAMS ((tree));
294 static void set_nested_class_simple_name_value PARAMS ((tree, int));
295 static void link_nested_class_to_enclosing PARAMS ((void));
296 static tree find_as_inner_class PARAMS ((tree, tree, tree));
297 static tree find_as_inner_class_do PARAMS ((tree, tree));
298 static int check_inner_class_redefinition PARAMS ((tree, tree));
300 static tree build_thisn_assign PARAMS ((void));
301 static tree build_current_thisn PARAMS ((tree));
302 static tree build_access_to_thisn PARAMS ((tree, tree, int));
303 static tree maybe_build_thisn_access_method PARAMS ((tree));
305 static tree build_outer_field_access PARAMS ((tree, tree));
306 static tree build_outer_field_access_methods PARAMS ((tree));
307 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
308 tree, tree));
309 static tree build_outer_method_access_method PARAMS ((tree));
310 static tree build_new_access_id PARAMS ((void));
311 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
312 tree, tree));
314 static int outer_field_access_p PARAMS ((tree, tree));
315 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
316 tree *, tree *));
317 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
318 static tree build_incomplete_class_ref PARAMS ((int, tree));
319 static tree patch_incomplete_class_ref PARAMS ((tree));
320 static tree create_anonymous_class PARAMS ((int, tree));
321 static void patch_anonymous_class PARAMS ((tree, tree, tree));
322 static void add_inner_class_fields PARAMS ((tree, tree));
324 static tree build_dot_class_method PARAMS ((tree));
325 static tree build_dot_class_method_invocation PARAMS ((tree));
326 static void create_new_parser_context PARAMS ((int));
327 static void mark_parser_ctxt PARAMS ((void *));
329 /* Number of error found so far. */
330 int java_error_count;
331 /* Number of warning found so far. */
332 int java_warning_count;
333 /* Tell when not to fold, when doing xrefs */
334 int do_not_fold;
335 /* Cyclic inheritance report, as it can be set by layout_class */
336 char *cyclic_inheritance_report;
338 /* Tell when we're within an instance initializer */
339 static int in_instance_initializer;
341 /* The current parser context */
342 struct parser_ctxt *ctxp;
344 /* List of things that were analyzed for which code will be generated */
345 static struct parser_ctxt *ctxp_for_generation = NULL;
347 /* binop_lookup maps token to tree_code. It is used where binary
348 operations are involved and required by the parser. RDIV_EXPR
349 covers both integral/floating point division. The code is changed
350 once the type of both operator is worked out. */
352 static enum tree_code binop_lookup[19] =
354 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
355 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
356 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
357 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
358 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
360 #define BINOP_LOOKUP(VALUE) \
361 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
363 /* This is the end index for binary operators that can also be used
364 in compound assignements. */
365 #define BINOP_COMPOUND_CANDIDATES 11
367 /* The "$L" identifier we use to create labels. */
368 static tree label_id = NULL_TREE;
370 /* The "StringBuffer" identifier used for the String `+' operator. */
371 static tree wfl_string_buffer = NULL_TREE;
373 /* The "append" identifier used for String `+' operator. */
374 static tree wfl_append = NULL_TREE;
376 /* The "toString" identifier used for String `+' operator. */
377 static tree wfl_to_string = NULL_TREE;
379 /* The "java.lang" import qualified name. */
380 static tree java_lang_id = NULL_TREE;
382 /* The generated `inst$' identifier used for generated enclosing
383 instance/field access functions. */
384 static tree inst_id = NULL_TREE;
386 /* The "java.lang.Cloneable" qualified name. */
387 static tree java_lang_cloneable = NULL_TREE;
389 /* The "java.io.Serializable" qualified name. */
390 static tree java_io_serializable = NULL_TREE;
392 /* Context and flag for static blocks */
393 static tree current_static_block = NULL_TREE;
395 /* The generated `write_parm_value$' identifier. */
396 static tree wpv_id;
398 /* The list of all packages we've seen so far */
399 static tree package_list = NULL_TREE;
401 /* Hold THIS for the scope of the current public method decl. */
402 static tree current_this;
404 /* Hold a list of catch clauses list. The first element of this list is
405 the list of the catch clauses of the currently analysed try block. */
406 static tree currently_caught_type_list;
408 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
409 line and point it out. */
410 /* Should point out the one that don't fit. ASCII/unicode, going
411 backward. FIXME */
413 #define check_modifiers(__message, __value, __mask) do { \
414 if ((__value) & ~(__mask)) \
416 int i, remainder = (__value) & ~(__mask); \
417 for (i = 0; i <= 10; i++) \
418 if ((1 << i) & remainder) \
419 parse_error_context (ctxp->modifier_ctx [i], (__message), \
420 java_accstring_lookup (1 << i)); \
422 } while (0)
426 %union {
427 tree node;
428 int sub_token;
429 struct {
430 int token;
431 int location;
432 } operator;
433 int value;
437 #include "lex.c"
440 %pure_parser
442 /* Things defined here have to match the order of what's in the
443 binop_lookup table. */
445 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
446 %token LS_TK SRS_TK ZRS_TK
447 %token AND_TK XOR_TK OR_TK
448 %token BOOL_AND_TK BOOL_OR_TK
449 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
451 /* This maps to the same binop_lookup entry than the token above */
453 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
454 %token REM_ASSIGN_TK
455 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
456 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
459 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
461 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
462 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
463 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
464 %token PAD_TK ABSTRACT_TK MODIFIER_TK
466 /* Keep those two in order, too */
467 %token DECR_TK INCR_TK
469 /* From now one, things can be in any order */
471 %token DEFAULT_TK IF_TK THROW_TK
472 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
473 %token THROWS_TK BREAK_TK IMPORT_TK
474 %token ELSE_TK INSTANCEOF_TK RETURN_TK
475 %token VOID_TK CATCH_TK INTERFACE_TK
476 %token CASE_TK EXTENDS_TK FINALLY_TK
477 %token SUPER_TK WHILE_TK CLASS_TK
478 %token SWITCH_TK CONST_TK TRY_TK
479 %token FOR_TK NEW_TK CONTINUE_TK
480 %token GOTO_TK PACKAGE_TK THIS_TK
482 %token BYTE_TK SHORT_TK INT_TK LONG_TK
483 %token CHAR_TK INTEGRAL_TK
485 %token FLOAT_TK DOUBLE_TK FP_TK
487 %token ID_TK
489 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
491 %token ASSIGN_ANY_TK ASSIGN_TK
492 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
494 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
495 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
497 %type <value> modifiers MODIFIER_TK final synchronized
499 %type <node> super ID_TK identifier
500 %type <node> name simple_name qualified_name
501 %type <node> type_declaration compilation_unit
502 field_declaration method_declaration extends_interfaces
503 interfaces interface_type_list
504 class_member_declaration
505 import_declarations package_declaration
506 type_declarations interface_body
507 interface_member_declaration constant_declaration
508 interface_member_declarations interface_type
509 abstract_method_declaration interface_type_list
510 %type <node> class_body_declaration class_member_declaration
511 static_initializer constructor_declaration block
512 %type <node> class_body_declarations constructor_header
513 %type <node> class_or_interface_type class_type class_type_list
514 constructor_declarator explicit_constructor_invocation
515 %type <node> dim_expr dim_exprs this_or_super throws
517 %type <node> variable_declarator_id variable_declarator
518 variable_declarators variable_initializer
519 variable_initializers constructor_body
520 array_initializer
522 %type <node> class_body block_end constructor_block_end
523 %type <node> statement statement_without_trailing_substatement
524 labeled_statement if_then_statement label_decl
525 if_then_else_statement while_statement for_statement
526 statement_nsi labeled_statement_nsi do_statement
527 if_then_else_statement_nsi while_statement_nsi
528 for_statement_nsi statement_expression_list for_init
529 for_update statement_expression expression_statement
530 primary_no_new_array expression primary
531 array_creation_expression array_type
532 class_instance_creation_expression field_access
533 method_invocation array_access something_dot_new
534 argument_list postfix_expression while_expression
535 post_increment_expression post_decrement_expression
536 unary_expression_not_plus_minus unary_expression
537 pre_increment_expression pre_decrement_expression
538 unary_expression_not_plus_minus cast_expression
539 multiplicative_expression additive_expression
540 shift_expression relational_expression
541 equality_expression and_expression
542 exclusive_or_expression inclusive_or_expression
543 conditional_and_expression conditional_or_expression
544 conditional_expression assignment_expression
545 left_hand_side assignment for_header for_begin
546 constant_expression do_statement_begin empty_statement
547 switch_statement synchronized_statement throw_statement
548 try_statement switch_expression switch_block
549 catches catch_clause catch_clause_parameter finally
550 anonymous_class_creation
551 %type <node> return_statement break_statement continue_statement
553 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
554 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
555 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
556 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
557 %type <operator> ASSIGN_ANY_TK assignment_operator
558 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
559 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
560 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
561 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
562 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
563 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
564 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
565 %type <operator> NEW_TK
567 %type <node> method_body
569 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
570 STRING_LIT_TK NULL_TK VOID_TK
572 %type <node> IF_TK WHILE_TK FOR_TK
574 %type <node> formal_parameter_list formal_parameter
575 method_declarator method_header
577 %type <node> primitive_type reference_type type
578 BOOLEAN_TK INTEGRAL_TK FP_TK
580 /* Added or modified JDK 1.1 rule types */
581 %type <node> type_literals array_type_literal
584 /* 19.2 Production from 2.3: The Syntactic Grammar */
585 goal:
587 /* Register static variables with the garbage
588 collector. */
589 ggc_add_tree_root (&label_id, 1);
590 ggc_add_tree_root (&wfl_string_buffer, 1);
591 ggc_add_tree_root (&wfl_append, 1);
592 ggc_add_tree_root (&wfl_to_string, 1);
593 ggc_add_tree_root (&java_lang_id, 1);
594 ggc_add_tree_root (&inst_id, 1);
595 ggc_add_tree_root (&java_lang_cloneable, 1);
596 ggc_add_tree_root (&java_io_serializable, 1);
597 ggc_add_tree_root (&current_static_block, 1);
598 ggc_add_tree_root (&wpv_id, 1);
599 ggc_add_tree_root (&package_list, 1);
600 ggc_add_tree_root (&current_this, 1);
601 ggc_add_tree_root (&currently_caught_type_list, 1);
602 ggc_add_string_root (&cyclic_inheritance_report, 1);
603 ggc_add_root (&ctxp, 1,
604 sizeof (struct parser_ctxt *),
605 mark_parser_ctxt);
606 ggc_add_root (&ctxp_for_generation, 1,
607 sizeof (struct parser_ctxt *),
608 mark_parser_ctxt);
610 compilation_unit
614 /* 19.3 Productions from 3: Lexical structure */
615 literal:
616 INT_LIT_TK
617 | FP_LIT_TK
618 | BOOL_LIT_TK
619 | CHAR_LIT_TK
620 | STRING_LIT_TK
621 | NULL_TK
624 /* 19.4 Productions from 4: Types, Values and Variables */
625 type:
626 primitive_type
627 | reference_type
630 primitive_type:
631 INTEGRAL_TK
632 | FP_TK
633 | BOOLEAN_TK
636 reference_type:
637 class_or_interface_type
638 | array_type
641 class_or_interface_type:
642 name
645 class_type:
646 class_or_interface_type /* Default rule */
649 interface_type:
650 class_or_interface_type
653 array_type:
654 primitive_type OSB_TK CSB_TK
656 $$ = build_java_array_type ($1, -1);
657 CLASS_LOADED_P ($$) = 1;
659 | name OSB_TK CSB_TK
660 { $$ = build_unresolved_array_type ($1); }
661 | array_type OSB_TK CSB_TK
662 { $$ = build_unresolved_array_type ($1); }
663 | primitive_type OSB_TK error
664 {RULE ("']' expected"); RECOVER;}
665 | array_type OSB_TK error
666 {RULE ("']' expected"); RECOVER;}
669 /* 19.5 Productions from 6: Names */
670 name:
671 simple_name /* Default rule */
672 | qualified_name /* Default rule */
675 simple_name:
676 identifier /* Default rule */
679 qualified_name:
680 name DOT_TK identifier
681 { $$ = make_qualified_name ($1, $3, $2.location); }
684 identifier:
685 ID_TK
688 /* 19.6: Production from 7: Packages */
689 compilation_unit:
690 {$$ = NULL;}
691 | package_declaration
692 | import_declarations
693 | type_declarations
694 | package_declaration import_declarations
695 | package_declaration type_declarations
696 | import_declarations type_declarations
697 | package_declaration import_declarations type_declarations
700 import_declarations:
701 import_declaration
703 $$ = NULL;
705 | import_declarations import_declaration
707 $$ = NULL;
711 type_declarations:
712 type_declaration
713 | type_declarations type_declaration
716 package_declaration:
717 PACKAGE_TK name SC_TK
719 ctxp->package = EXPR_WFL_NODE ($2);
720 register_package (ctxp->package);
722 | PACKAGE_TK error
723 {yyerror ("Missing name"); RECOVER;}
724 | PACKAGE_TK name error
725 {yyerror ("';' expected"); RECOVER;}
728 import_declaration:
729 single_type_import_declaration
730 | type_import_on_demand_declaration
733 single_type_import_declaration:
734 IMPORT_TK name SC_TK
736 tree name = EXPR_WFL_NODE ($2), last_name;
737 int i = IDENTIFIER_LENGTH (name)-1;
738 const char *last = &IDENTIFIER_POINTER (name)[i];
739 while (last != IDENTIFIER_POINTER (name))
741 if (last [0] == '.')
742 break;
743 last--;
745 last_name = get_identifier (++last);
746 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
748 tree err = find_name_in_single_imports (last_name);
749 if (err && err != name)
750 parse_error_context
751 ($2, "Ambiguous class: `%s' and `%s'",
752 IDENTIFIER_POINTER (name),
753 IDENTIFIER_POINTER (err));
754 else
755 REGISTER_IMPORT ($2, last_name);
757 else
758 REGISTER_IMPORT ($2, last_name);
760 | IMPORT_TK error
761 {yyerror ("Missing name"); RECOVER;}
762 | IMPORT_TK name error
763 {yyerror ("';' expected"); RECOVER;}
766 type_import_on_demand_declaration:
767 IMPORT_TK name DOT_TK MULT_TK SC_TK
769 tree name = EXPR_WFL_NODE ($2);
770 /* Don't import java.lang.* twice. */
771 if (name != java_lang_id)
773 read_import_dir ($2);
774 ctxp->import_demand_list =
775 chainon (ctxp->import_demand_list,
776 build_tree_list ($2, NULL_TREE));
779 | IMPORT_TK name DOT_TK error
780 {yyerror ("'*' expected"); RECOVER;}
781 | IMPORT_TK name DOT_TK MULT_TK error
782 {yyerror ("';' expected"); RECOVER;}
785 type_declaration:
786 class_declaration
787 { end_class_declaration (0); }
788 | interface_declaration
789 { end_class_declaration (0); }
790 | empty_statement
791 | error
793 YYERROR_NOW;
794 yyerror ("Class or interface declaration expected");
798 /* 19.7 Shortened from the original:
799 modifiers: modifier | modifiers modifier
800 modifier: any of public... */
801 modifiers:
802 MODIFIER_TK
804 $$ = (1 << $1);
806 | modifiers MODIFIER_TK
808 int acc = (1 << $2);
809 if ($$ & acc)
810 parse_error_context
811 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
812 java_accstring_lookup (acc));
813 else
815 $$ |= acc;
820 /* 19.8.1 Production from $8.1: Class Declaration */
821 class_declaration:
822 modifiers CLASS_TK identifier super interfaces
823 { create_class ($1, $3, $4, $5); }
824 class_body
825 | CLASS_TK identifier super interfaces
826 { create_class (0, $2, $3, $4); }
827 class_body
828 | modifiers CLASS_TK error
829 {yyerror ("Missing class name"); RECOVER;}
830 | CLASS_TK error
831 {yyerror ("Missing class name"); RECOVER;}
832 | CLASS_TK identifier error
834 if (!ctxp->class_err) yyerror ("'{' expected");
835 DRECOVER(class1);
837 | modifiers CLASS_TK identifier error
838 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
841 super:
842 { $$ = NULL; }
843 | EXTENDS_TK class_type
844 { $$ = $2; }
845 | EXTENDS_TK class_type error
846 {yyerror ("'{' expected"); ctxp->class_err=1;}
847 | EXTENDS_TK error
848 {yyerror ("Missing super class name"); ctxp->class_err=1;}
851 interfaces:
852 { $$ = NULL_TREE; }
853 | IMPLEMENTS_TK interface_type_list
854 { $$ = $2; }
855 | IMPLEMENTS_TK error
857 ctxp->class_err=1;
858 yyerror ("Missing interface name");
862 interface_type_list:
863 interface_type
865 ctxp->interface_number = 1;
866 $$ = build_tree_list ($1, NULL_TREE);
868 | interface_type_list C_TK interface_type
870 ctxp->interface_number++;
871 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
873 | interface_type_list C_TK error
874 {yyerror ("Missing interface name"); RECOVER;}
877 class_body:
878 OCB_TK CCB_TK
880 /* Store the location of the `}' when doing xrefs */
881 if (flag_emit_xref)
882 DECL_END_SOURCE_LINE (GET_CPC ()) =
883 EXPR_WFL_ADD_COL ($2.location, 1);
884 $$ = GET_CPC ();
886 | OCB_TK class_body_declarations CCB_TK
888 /* Store the location of the `}' when doing xrefs */
889 if (flag_emit_xref)
890 DECL_END_SOURCE_LINE (GET_CPC ()) =
891 EXPR_WFL_ADD_COL ($3.location, 1);
892 $$ = GET_CPC ();
896 class_body_declarations:
897 class_body_declaration
898 | class_body_declarations class_body_declaration
901 class_body_declaration:
902 class_member_declaration
903 | static_initializer
904 | constructor_declaration
905 | block /* Added, JDK1.1, instance initializer */
907 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
908 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
912 class_member_declaration:
913 field_declaration
914 | method_declaration
915 | class_declaration /* Added, JDK1.1 inner classes */
916 { end_class_declaration (1); }
917 | interface_declaration /* Added, JDK1.1 inner interfaces */
918 { end_class_declaration (1); }
919 | empty_statement
922 /* 19.8.2 Productions from 8.3: Field Declarations */
923 field_declaration:
924 type variable_declarators SC_TK
925 { register_fields (0, $1, $2); }
926 | modifiers type variable_declarators SC_TK
928 check_modifiers
929 ("Illegal modifier `%s' for field declaration",
930 $1, FIELD_MODIFIERS);
931 check_modifiers_consistency ($1);
932 register_fields ($1, $2, $3);
936 variable_declarators:
937 /* Should we use build_decl_list () instead ? FIXME */
938 variable_declarator /* Default rule */
939 | variable_declarators C_TK variable_declarator
940 { $$ = chainon ($1, $3); }
941 | variable_declarators C_TK error
942 {yyerror ("Missing term"); RECOVER;}
945 variable_declarator:
946 variable_declarator_id
947 { $$ = build_tree_list ($1, NULL_TREE); }
948 | variable_declarator_id ASSIGN_TK variable_initializer
950 if (java_error_count)
951 $3 = NULL_TREE;
952 $$ = build_tree_list
953 ($1, build_assignment ($2.token, $2.location, $1, $3));
955 | variable_declarator_id ASSIGN_TK error
957 yyerror ("Missing variable initializer");
958 $$ = build_tree_list ($1, NULL_TREE);
959 RECOVER;
961 | variable_declarator_id ASSIGN_TK variable_initializer error
963 yyerror ("';' expected");
964 $$ = build_tree_list ($1, NULL_TREE);
965 RECOVER;
969 variable_declarator_id:
970 identifier
971 | variable_declarator_id OSB_TK CSB_TK
972 { $$ = build_unresolved_array_type ($1); }
973 | identifier error
974 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
975 | variable_declarator_id OSB_TK error
977 tree node = java_lval.node;
978 if (node && (TREE_CODE (node) == INTEGER_CST
979 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
980 yyerror ("Can't specify array dimension in a declaration");
981 else
982 yyerror ("']' expected");
983 DRECOVER(vdi);
985 | variable_declarator_id CSB_TK error
986 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
989 variable_initializer:
990 expression
991 | array_initializer
994 /* 19.8.3 Productions from 8.4: Method Declarations */
995 method_declaration:
996 method_header
998 current_function_decl = $1;
999 if (current_function_decl
1000 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1001 source_start_java_method (current_function_decl);
1002 else
1003 current_function_decl = NULL_TREE;
1005 method_body
1006 { finish_method_declaration ($3); }
1007 | method_header error
1008 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1011 method_header:
1012 type method_declarator throws
1013 { $$ = method_header (0, $1, $2, $3); }
1014 | VOID_TK method_declarator throws
1015 { $$ = method_header (0, void_type_node, $2, $3); }
1016 | modifiers type method_declarator throws
1017 { $$ = method_header ($1, $2, $3, $4); }
1018 | modifiers VOID_TK method_declarator throws
1019 { $$ = method_header ($1, void_type_node, $3, $4); }
1020 | type error
1022 yyerror ("Invalid method declaration, method name required");
1023 RECOVER;
1025 | modifiers type error
1026 {RECOVER;}
1027 | VOID_TK error
1028 {yyerror ("Identifier expected"); RECOVER;}
1029 | modifiers VOID_TK error
1030 {yyerror ("Identifier expected"); RECOVER;}
1031 | modifiers error
1033 yyerror ("Invalid method declaration, return type required");
1034 RECOVER;
1038 method_declarator:
1039 identifier OP_TK CP_TK
1041 ctxp->formal_parameter_number = 0;
1042 $$ = method_declarator ($1, NULL_TREE);
1044 | identifier OP_TK formal_parameter_list CP_TK
1045 { $$ = method_declarator ($1, $3); }
1046 | method_declarator OSB_TK CSB_TK
1048 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1049 TREE_PURPOSE ($1) =
1050 build_unresolved_array_type (TREE_PURPOSE ($1));
1051 parse_warning_context
1052 (wfl_operator,
1053 "Discouraged form of returned type specification");
1055 | identifier OP_TK error
1056 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1057 | method_declarator OSB_TK error
1058 {yyerror ("']' expected"); RECOVER;}
1061 formal_parameter_list:
1062 formal_parameter
1064 ctxp->formal_parameter_number = 1;
1066 | formal_parameter_list C_TK formal_parameter
1068 ctxp->formal_parameter_number += 1;
1069 $$ = chainon ($1, $3);
1071 | formal_parameter_list C_TK error
1072 { yyerror ("Missing formal parameter term"); RECOVER; }
1075 formal_parameter:
1076 type variable_declarator_id
1078 $$ = build_tree_list ($2, $1);
1080 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1082 $$ = build_tree_list ($3, $2);
1083 ARG_FINAL_P ($$) = 1;
1085 | type error
1087 yyerror ("Missing identifier"); RECOVER;
1088 $$ = NULL_TREE;
1090 | final type error
1092 yyerror ("Missing identifier"); RECOVER;
1093 $$ = NULL_TREE;
1097 final:
1098 modifiers
1100 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1101 $1, ACC_FINAL);
1102 if ($1 != ACC_FINAL)
1103 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1107 throws:
1108 { $$ = NULL_TREE; }
1109 | THROWS_TK class_type_list
1110 { $$ = $2; }
1111 | THROWS_TK error
1112 {yyerror ("Missing class type term"); RECOVER;}
1115 class_type_list:
1116 class_type
1117 { $$ = build_tree_list ($1, $1); }
1118 | class_type_list C_TK class_type
1119 { $$ = tree_cons ($3, $3, $1); }
1120 | class_type_list C_TK error
1121 {yyerror ("Missing class type term"); RECOVER;}
1124 method_body:
1125 block
1126 | SC_TK { $$ = NULL_TREE; }
1129 /* 19.8.4 Productions from 8.5: Static Initializers */
1130 static_initializer:
1131 static block
1133 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1134 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1135 current_static_block = NULL_TREE;
1139 static: /* Test lval.sub_token here */
1140 modifiers
1142 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1143 /* Can't have a static initializer in an innerclass */
1144 if ($1 | ACC_STATIC &&
1145 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1146 parse_error_context
1147 (MODIFIER_WFL (STATIC_TK),
1148 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1149 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1150 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1154 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1155 constructor_declaration:
1156 constructor_header
1158 current_function_decl = $1;
1159 source_start_java_method (current_function_decl);
1161 constructor_body
1162 { finish_method_declaration ($3); }
1165 constructor_header:
1166 constructor_declarator throws
1167 { $$ = method_header (0, NULL_TREE, $1, $2); }
1168 | modifiers constructor_declarator throws
1169 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1172 constructor_declarator:
1173 simple_name OP_TK CP_TK
1175 ctxp->formal_parameter_number = 0;
1176 $$ = method_declarator ($1, NULL_TREE);
1178 | simple_name OP_TK formal_parameter_list CP_TK
1179 { $$ = method_declarator ($1, $3); }
1182 constructor_body:
1183 /* Unlike regular method, we always need a complete (empty)
1184 body so we can safely perform all the required code
1185 addition (super invocation and field initialization) */
1186 block_begin constructor_block_end
1188 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1189 $$ = $2;
1191 | block_begin explicit_constructor_invocation constructor_block_end
1192 { $$ = $3; }
1193 | block_begin block_statements constructor_block_end
1194 { $$ = $3; }
1195 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1196 { $$ = $4; }
1199 constructor_block_end:
1200 block_end
1203 /* Error recovery for that rule moved down expression_statement: rule. */
1204 explicit_constructor_invocation:
1205 this_or_super OP_TK CP_TK SC_TK
1207 $$ = build_method_invocation ($1, NULL_TREE);
1208 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1209 $$ = java_method_add_stmt (current_function_decl, $$);
1211 | this_or_super OP_TK argument_list CP_TK SC_TK
1213 $$ = build_method_invocation ($1, $3);
1214 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1215 $$ = java_method_add_stmt (current_function_decl, $$);
1217 /* Added, JDK1.1 inner classes. Modified because the rule
1218 'primary' couldn't work. */
1219 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1220 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1221 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1222 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1225 this_or_super: /* Added, simplifies error diagnostics */
1226 THIS_TK
1228 tree wfl = build_wfl_node (this_identifier_node);
1229 EXPR_WFL_LINECOL (wfl) = $1.location;
1230 $$ = wfl;
1232 | SUPER_TK
1234 tree wfl = build_wfl_node (super_identifier_node);
1235 EXPR_WFL_LINECOL (wfl) = $1.location;
1236 $$ = wfl;
1240 /* 19.9 Productions from 9: Interfaces */
1241 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1242 interface_declaration:
1243 INTERFACE_TK identifier
1244 { create_interface (0, $2, NULL_TREE); }
1245 interface_body
1246 | modifiers INTERFACE_TK identifier
1247 { create_interface ($1, $3, NULL_TREE); }
1248 interface_body
1249 | INTERFACE_TK identifier extends_interfaces
1250 { create_interface (0, $2, $3); }
1251 interface_body
1252 | modifiers INTERFACE_TK identifier extends_interfaces
1253 { create_interface ($1, $3, $4); }
1254 interface_body
1255 | INTERFACE_TK identifier error
1256 {yyerror ("'{' expected"); RECOVER;}
1257 | modifiers INTERFACE_TK identifier error
1258 {yyerror ("'{' expected"); RECOVER;}
1261 extends_interfaces:
1262 EXTENDS_TK interface_type
1264 ctxp->interface_number = 1;
1265 $$ = build_tree_list ($2, NULL_TREE);
1267 | extends_interfaces C_TK interface_type
1269 ctxp->interface_number++;
1270 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1272 | EXTENDS_TK error
1273 {yyerror ("Invalid interface type"); RECOVER;}
1274 | extends_interfaces C_TK error
1275 {yyerror ("Missing term"); RECOVER;}
1278 interface_body:
1279 OCB_TK CCB_TK
1280 { $$ = NULL_TREE; }
1281 | OCB_TK interface_member_declarations CCB_TK
1282 { $$ = NULL_TREE; }
1285 interface_member_declarations:
1286 interface_member_declaration
1287 | interface_member_declarations interface_member_declaration
1290 interface_member_declaration:
1291 constant_declaration
1292 | abstract_method_declaration
1293 | class_declaration /* Added, JDK1.1 inner classes */
1294 { end_class_declaration (1); }
1295 | interface_declaration /* Added, JDK1.1 inner interfaces */
1296 { end_class_declaration (1); }
1299 constant_declaration:
1300 field_declaration
1303 abstract_method_declaration:
1304 method_header SC_TK
1306 check_abstract_method_header ($1);
1307 current_function_decl = NULL_TREE; /* FIXME ? */
1309 | method_header error
1310 {yyerror ("';' expected"); RECOVER;}
1313 /* 19.10 Productions from 10: Arrays */
1314 array_initializer:
1315 OCB_TK CCB_TK
1316 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1317 | OCB_TK variable_initializers CCB_TK
1318 { $$ = build_new_array_init ($1.location, $2); }
1319 | OCB_TK variable_initializers C_TK CCB_TK
1320 { $$ = build_new_array_init ($1.location, $2); }
1323 variable_initializers:
1324 variable_initializer
1326 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1327 $1, NULL_TREE);
1329 | variable_initializers C_TK variable_initializer
1331 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1333 | variable_initializers C_TK error
1334 {yyerror ("Missing term"); RECOVER;}
1337 /* 19.11 Production from 14: Blocks and Statements */
1338 block:
1339 OCB_TK CCB_TK
1341 /* Store the location of the `}' when doing xrefs */
1342 if (current_function_decl && flag_emit_xref)
1343 DECL_END_SOURCE_LINE (current_function_decl) =
1344 EXPR_WFL_ADD_COL ($2.location, 1);
1345 $$ = empty_stmt_node;
1347 | block_begin block_statements block_end
1348 { $$ = $3; }
1351 block_begin:
1352 OCB_TK
1353 { enter_block (); }
1356 block_end:
1357 CCB_TK
1359 maybe_absorb_scoping_blocks ();
1360 /* Store the location of the `}' when doing xrefs */
1361 if (current_function_decl && flag_emit_xref)
1362 DECL_END_SOURCE_LINE (current_function_decl) =
1363 EXPR_WFL_ADD_COL ($1.location, 1);
1364 $$ = exit_block ();
1365 if (!BLOCK_SUBBLOCKS ($$))
1366 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1370 block_statements:
1371 block_statement
1372 | block_statements block_statement
1375 block_statement:
1376 local_variable_declaration_statement
1377 | statement
1378 { java_method_add_stmt (current_function_decl, $1); }
1379 | class_declaration /* Added, JDK1.1 local classes */
1381 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1382 end_class_declaration (1);
1386 local_variable_declaration_statement:
1387 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1390 local_variable_declaration:
1391 type variable_declarators
1392 { declare_local_variables (0, $1, $2); }
1393 | final type variable_declarators /* Added, JDK1.1 final locals */
1394 { declare_local_variables ($1, $2, $3); }
1397 statement:
1398 statement_without_trailing_substatement
1399 | labeled_statement
1400 | if_then_statement
1401 | if_then_else_statement
1402 | while_statement
1403 | for_statement
1404 { $$ = exit_block (); }
1407 statement_nsi:
1408 statement_without_trailing_substatement
1409 | labeled_statement_nsi
1410 | if_then_else_statement_nsi
1411 | while_statement_nsi
1412 | for_statement_nsi
1413 { $$ = exit_block (); }
1416 statement_without_trailing_substatement:
1417 block
1418 | empty_statement
1419 | expression_statement
1420 | switch_statement
1421 | do_statement
1422 | break_statement
1423 | continue_statement
1424 | return_statement
1425 | synchronized_statement
1426 | throw_statement
1427 | try_statement
1430 empty_statement:
1431 SC_TK
1433 if (flag_extraneous_semicolon)
1435 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1436 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1438 $$ = empty_stmt_node;
1442 label_decl:
1443 identifier REL_CL_TK
1445 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1446 EXPR_WFL_NODE ($1));
1447 pushlevel (2);
1448 push_labeled_block ($$);
1449 PUSH_LABELED_BLOCK ($$);
1453 labeled_statement:
1454 label_decl statement
1455 { $$ = finish_labeled_statement ($1, $2); }
1456 | identifier error
1457 {yyerror ("':' expected"); RECOVER;}
1460 labeled_statement_nsi:
1461 label_decl statement_nsi
1462 { $$ = finish_labeled_statement ($1, $2); }
1465 /* We concentrate here a bunch of error handling rules that we couldn't write
1466 earlier, because expression_statement catches a missing ';'. */
1467 expression_statement:
1468 statement_expression SC_TK
1470 /* We have a statement. Generate a WFL around it so
1471 we can debug it */
1472 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1473 /* We know we have a statement, so set the debug
1474 info to be eventually generate here. */
1475 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1477 | error SC_TK
1479 YYNOT_TWICE yyerror ("Invalid expression statement");
1480 DRECOVER (expr_stmt);
1482 | error OCB_TK
1484 YYNOT_TWICE yyerror ("Invalid expression statement");
1485 DRECOVER (expr_stmt);
1487 | error CCB_TK
1489 YYNOT_TWICE yyerror ("Invalid expression statement");
1490 DRECOVER (expr_stmt);
1492 | this_or_super OP_TK error
1493 {yyerror ("')' expected"); RECOVER;}
1494 | this_or_super OP_TK CP_TK error
1496 parse_ctor_invocation_error ();
1497 RECOVER;
1499 | this_or_super OP_TK argument_list error
1500 {yyerror ("')' expected"); RECOVER;}
1501 | this_or_super OP_TK argument_list CP_TK error
1503 parse_ctor_invocation_error ();
1504 RECOVER;
1506 | name DOT_TK SUPER_TK error
1507 {yyerror ("'(' expected"); RECOVER;}
1508 | name DOT_TK SUPER_TK OP_TK error
1509 {yyerror ("')' expected"); RECOVER;}
1510 | name DOT_TK SUPER_TK OP_TK argument_list error
1511 {yyerror ("')' expected"); RECOVER;}
1512 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1513 {yyerror ("';' expected"); RECOVER;}
1514 | name DOT_TK SUPER_TK OP_TK CP_TK error
1515 {yyerror ("';' expected"); RECOVER;}
1518 statement_expression:
1519 assignment
1520 | pre_increment_expression
1521 | pre_decrement_expression
1522 | post_increment_expression
1523 | post_decrement_expression
1524 | method_invocation
1525 | class_instance_creation_expression
1528 if_then_statement:
1529 IF_TK OP_TK expression CP_TK statement
1531 $$ = build_if_else_statement ($2.location, $3,
1532 $5, NULL_TREE);
1534 | IF_TK error
1535 {yyerror ("'(' expected"); RECOVER;}
1536 | IF_TK OP_TK error
1537 {yyerror ("Missing term"); RECOVER;}
1538 | IF_TK OP_TK expression error
1539 {yyerror ("')' expected"); RECOVER;}
1542 if_then_else_statement:
1543 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1544 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1547 if_then_else_statement_nsi:
1548 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1549 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1552 switch_statement:
1553 switch_expression
1555 enter_block ();
1557 switch_block
1559 /* Make into "proper list" of COMPOUND_EXPRs.
1560 I.e. make the last statment also have its own
1561 COMPOUND_EXPR. */
1562 maybe_absorb_scoping_blocks ();
1563 TREE_OPERAND ($1, 1) = exit_block ();
1564 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1568 switch_expression:
1569 SWITCH_TK OP_TK expression CP_TK
1571 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1572 EXPR_WFL_LINECOL ($$) = $2.location;
1574 | SWITCH_TK error
1575 {yyerror ("'(' expected"); RECOVER;}
1576 | SWITCH_TK OP_TK error
1577 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1578 | SWITCH_TK OP_TK expression CP_TK error
1579 {yyerror ("'{' expected"); RECOVER;}
1582 /* Default assignment is there to avoid type node on switch_block
1583 node. */
1585 switch_block:
1586 OCB_TK CCB_TK
1587 { $$ = NULL_TREE; }
1588 | OCB_TK switch_labels CCB_TK
1589 { $$ = NULL_TREE; }
1590 | OCB_TK switch_block_statement_groups CCB_TK
1591 { $$ = NULL_TREE; }
1592 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1593 { $$ = NULL_TREE; }
1596 switch_block_statement_groups:
1597 switch_block_statement_group
1598 | switch_block_statement_groups switch_block_statement_group
1601 switch_block_statement_group:
1602 switch_labels block_statements
1605 switch_labels:
1606 switch_label
1607 | switch_labels switch_label
1610 switch_label:
1611 CASE_TK constant_expression REL_CL_TK
1613 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1614 EXPR_WFL_LINECOL (lab) = $1.location;
1615 java_method_add_stmt (current_function_decl, lab);
1617 | DEFAULT_TK REL_CL_TK
1619 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1620 EXPR_WFL_LINECOL (lab) = $1.location;
1621 java_method_add_stmt (current_function_decl, lab);
1623 | CASE_TK error
1624 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1625 | CASE_TK constant_expression error
1626 {yyerror ("':' expected"); RECOVER;}
1627 | DEFAULT_TK error
1628 {yyerror ("':' expected"); RECOVER;}
1631 while_expression:
1632 WHILE_TK OP_TK expression CP_TK
1634 tree body = build_loop_body ($2.location, $3, 0);
1635 $$ = build_new_loop (body);
1639 while_statement:
1640 while_expression statement
1641 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1642 | WHILE_TK error
1643 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1644 | WHILE_TK OP_TK error
1645 {yyerror ("Missing term and ')' expected"); RECOVER;}
1646 | WHILE_TK OP_TK expression error
1647 {yyerror ("')' expected"); RECOVER;}
1650 while_statement_nsi:
1651 while_expression statement_nsi
1652 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1655 do_statement_begin:
1656 DO_TK
1658 tree body = build_loop_body (0, NULL_TREE, 1);
1659 $$ = build_new_loop (body);
1661 /* Need error handing here. FIXME */
1664 do_statement:
1665 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1666 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1669 for_statement:
1670 for_begin SC_TK expression SC_TK for_update CP_TK statement
1672 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1673 $3 = build_wfl_node ($3);
1674 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1676 | for_begin SC_TK SC_TK for_update CP_TK statement
1678 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1679 /* We have not condition, so we get rid of the EXIT_EXPR */
1680 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1681 empty_stmt_node;
1683 | for_begin SC_TK error
1684 {yyerror ("Invalid control expression"); RECOVER;}
1685 | for_begin SC_TK expression SC_TK error
1686 {yyerror ("Invalid update expression"); RECOVER;}
1687 | for_begin SC_TK SC_TK error
1688 {yyerror ("Invalid update expression"); RECOVER;}
1691 for_statement_nsi:
1692 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1693 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1694 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1696 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1697 /* We have not condition, so we get rid of the EXIT_EXPR */
1698 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1699 empty_stmt_node;
1703 for_header:
1704 FOR_TK OP_TK
1706 /* This scope defined for local variable that may be
1707 defined within the scope of the for loop */
1708 enter_block ();
1710 | FOR_TK error
1711 {yyerror ("'(' expected"); DRECOVER(for_1);}
1712 | FOR_TK OP_TK error
1713 {yyerror ("Invalid init statement"); RECOVER;}
1716 for_begin:
1717 for_header for_init
1719 /* We now declare the loop body. The loop is
1720 declared as a for loop. */
1721 tree body = build_loop_body (0, NULL_TREE, 0);
1722 $$ = build_new_loop (body);
1723 FOR_LOOP_P ($$) = 1;
1724 /* The loop is added to the current block the for
1725 statement is defined within */
1726 java_method_add_stmt (current_function_decl, $$);
1729 for_init: /* Can be empty */
1730 { $$ = empty_stmt_node; }
1731 | statement_expression_list
1733 /* Init statement recorded within the previously
1734 defined block scope */
1735 $$ = java_method_add_stmt (current_function_decl, $1);
1737 | local_variable_declaration
1739 /* Local variable are recorded within the previously
1740 defined block scope */
1741 $$ = NULL_TREE;
1743 | statement_expression_list error
1744 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1747 for_update: /* Can be empty */
1748 {$$ = empty_stmt_node;}
1749 | statement_expression_list
1750 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1753 statement_expression_list:
1754 statement_expression
1755 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1756 | statement_expression_list C_TK statement_expression
1757 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1758 | statement_expression_list C_TK error
1759 {yyerror ("Missing term"); RECOVER;}
1762 break_statement:
1763 BREAK_TK SC_TK
1764 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1765 | BREAK_TK identifier SC_TK
1766 { $$ = build_bc_statement ($1.location, 1, $2); }
1767 | BREAK_TK error
1768 {yyerror ("Missing term"); RECOVER;}
1769 | BREAK_TK identifier error
1770 {yyerror ("';' expected"); RECOVER;}
1773 continue_statement:
1774 CONTINUE_TK SC_TK
1775 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1776 | CONTINUE_TK identifier SC_TK
1777 { $$ = build_bc_statement ($1.location, 0, $2); }
1778 | CONTINUE_TK error
1779 {yyerror ("Missing term"); RECOVER;}
1780 | CONTINUE_TK identifier error
1781 {yyerror ("';' expected"); RECOVER;}
1784 return_statement:
1785 RETURN_TK SC_TK
1786 { $$ = build_return ($1.location, NULL_TREE); }
1787 | RETURN_TK expression SC_TK
1788 { $$ = build_return ($1.location, $2); }
1789 | RETURN_TK error
1790 {yyerror ("Missing term"); RECOVER;}
1791 | RETURN_TK expression error
1792 {yyerror ("';' expected"); RECOVER;}
1795 throw_statement:
1796 THROW_TK expression SC_TK
1798 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1799 EXPR_WFL_LINECOL ($$) = $1.location;
1801 | THROW_TK error
1802 {yyerror ("Missing term"); RECOVER;}
1803 | THROW_TK expression error
1804 {yyerror ("';' expected"); RECOVER;}
1807 synchronized_statement:
1808 synchronized OP_TK expression CP_TK block
1810 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1811 EXPR_WFL_LINECOL ($$) =
1812 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1814 | synchronized OP_TK expression CP_TK error
1815 {yyerror ("'{' expected"); RECOVER;}
1816 | synchronized error
1817 {yyerror ("'(' expected"); RECOVER;}
1818 | synchronized OP_TK error CP_TK
1819 {yyerror ("Missing term"); RECOVER;}
1820 | synchronized OP_TK error
1821 {yyerror ("Missing term"); RECOVER;}
1824 synchronized:
1825 modifiers
1827 check_modifiers (
1828 "Illegal modifier `%s'. Only `synchronized' was expected here",
1829 $1, ACC_SYNCHRONIZED);
1830 if ($1 != ACC_SYNCHRONIZED)
1831 MODIFIER_WFL (SYNCHRONIZED_TK) =
1832 build_wfl_node (NULL_TREE);
1836 try_statement:
1837 TRY_TK block catches
1838 { $$ = build_try_statement ($1.location, $2, $3); }
1839 | TRY_TK block finally
1840 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1841 | TRY_TK block catches finally
1842 { $$ = build_try_finally_statement
1843 ($1.location, build_try_statement ($1.location,
1844 $2, $3), $4);
1846 | TRY_TK error
1847 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1850 catches:
1851 catch_clause
1852 | catches catch_clause
1854 TREE_CHAIN ($2) = $1;
1855 $$ = $2;
1859 catch_clause:
1860 catch_clause_parameter block
1862 java_method_add_stmt (current_function_decl, $2);
1863 exit_block ();
1864 $$ = $1;
1867 catch_clause_parameter:
1868 CATCH_TK OP_TK formal_parameter CP_TK
1870 /* We add a block to define a scope for
1871 formal_parameter (CCBP). The formal parameter is
1872 declared initialized by the appropriate function
1873 call */
1874 tree ccpb = enter_block ();
1875 tree init = build_assignment (ASSIGN_TK, $2.location,
1876 TREE_PURPOSE ($3),
1877 soft_exceptioninfo_call_node);
1878 declare_local_variables (0, TREE_VALUE ($3),
1879 build_tree_list (TREE_PURPOSE ($3),
1880 init));
1881 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1882 EXPR_WFL_LINECOL ($$) = $1.location;
1884 | CATCH_TK error
1885 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1886 | CATCH_TK OP_TK error
1888 yyerror ("Missing term or ')' expected");
1889 RECOVER; $$ = NULL_TREE;
1891 | CATCH_TK OP_TK error CP_TK /* That's for () */
1892 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1895 finally:
1896 FINALLY_TK block
1897 { $$ = $2; }
1898 | FINALLY_TK error
1899 {yyerror ("'{' expected"); RECOVER; }
1902 /* 19.12 Production from 15: Expressions */
1903 primary:
1904 primary_no_new_array
1905 | array_creation_expression
1908 primary_no_new_array:
1909 literal
1910 | THIS_TK
1911 { $$ = build_this ($1.location); }
1912 | OP_TK expression CP_TK
1913 {$$ = $2;}
1914 | class_instance_creation_expression
1915 | field_access
1916 | method_invocation
1917 | array_access
1918 | type_literals
1919 /* Added, JDK1.1 inner classes. Documentation is wrong
1920 refering to a 'ClassName' (class_name) rule that doesn't
1921 exist. Used name: instead. */
1922 | name DOT_TK THIS_TK
1924 tree wfl = build_wfl_node (this_identifier_node);
1925 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1927 | OP_TK expression error
1928 {yyerror ("')' expected"); RECOVER;}
1929 | name DOT_TK error
1930 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1931 | primitive_type DOT_TK error
1932 {yyerror ("'class' expected" ); RECOVER;}
1933 | VOID_TK DOT_TK error
1934 {yyerror ("'class' expected" ); RECOVER;}
1937 /* Added, JDK1.1 type literals. We can't use `type' directly, so we
1938 broke the rule down a bit. */
1940 array_type_literal:
1941 primitive_type OSB_TK CSB_TK
1943 $$ = build_java_array_type ($1, -1);
1944 CLASS_LOADED_P ($$) = 1;
1946 | name OSB_TK CSB_TK
1947 { $$ = build_unresolved_array_type ($1); }
1948 /* This triggers two reduce/reduce conflict between array_type_literal and
1949 dims. FIXME.
1950 | array_type OSB_TK CSB_TK
1951 { $$ = build_unresolved_array_type ($1); }
1955 type_literals:
1956 name DOT_TK CLASS_TK
1957 { $$ = build_incomplete_class_ref ($2.location, $1); }
1958 | array_type_literal DOT_TK CLASS_TK
1959 { $$ = build_incomplete_class_ref ($2.location, $1); }
1960 | primitive_type DOT_TK CLASS_TK
1961 { $$ = build_class_ref ($1); }
1962 | VOID_TK DOT_TK CLASS_TK
1963 { $$ = build_class_ref (void_type_node); }
1966 class_instance_creation_expression:
1967 NEW_TK class_type OP_TK argument_list CP_TK
1968 { $$ = build_new_invocation ($2, $4); }
1969 | NEW_TK class_type OP_TK CP_TK
1970 { $$ = build_new_invocation ($2, NULL_TREE); }
1971 | anonymous_class_creation
1972 /* Added, JDK1.1 inner classes, modified to use name or
1973 primary instead of primary solely which couldn't work in
1974 all situations. */
1975 | something_dot_new identifier OP_TK CP_TK
1977 tree ctor = build_new_invocation ($2, NULL_TREE);
1978 $$ = make_qualified_primary ($1, ctor,
1979 EXPR_WFL_LINECOL ($1));
1981 | something_dot_new identifier OP_TK CP_TK class_body
1982 | something_dot_new identifier OP_TK argument_list CP_TK
1984 tree ctor = build_new_invocation ($2, $4);
1985 $$ = make_qualified_primary ($1, ctor,
1986 EXPR_WFL_LINECOL ($1));
1988 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1989 | NEW_TK error SC_TK
1990 {yyerror ("'(' expected"); DRECOVER(new_1);}
1991 | NEW_TK class_type error
1992 {yyerror ("'(' expected"); RECOVER;}
1993 | NEW_TK class_type OP_TK error
1994 {yyerror ("')' or term expected"); RECOVER;}
1995 | NEW_TK class_type OP_TK argument_list error
1996 {yyerror ("')' expected"); RECOVER;}
1997 | something_dot_new error
1998 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1999 | something_dot_new identifier error
2000 {yyerror ("'(' expected"); RECOVER;}
2003 /* Created after JDK1.1 rules originally added to
2004 class_instance_creation_expression, but modified to use
2005 'class_type' instead of 'TypeName' (type_name) which is mentionned
2006 in the documentation but doesn't exist. */
2008 anonymous_class_creation:
2009 NEW_TK class_type OP_TK argument_list CP_TK
2010 { create_anonymous_class ($1.location, $2); }
2011 class_body
2013 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2014 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2016 end_class_declaration (1);
2018 /* Now we can craft the new expression */
2019 $$ = build_new_invocation (id, $4);
2021 /* Note that we can't possibly be here if
2022 `class_type' is an interface (in which case the
2023 anonymous class extends Object and implements
2024 `class_type', hence its constructor can't have
2025 arguments.) */
2027 /* Otherwise, the innerclass must feature a
2028 constructor matching `argument_list'. Anonymous
2029 classes are a bit special: it's impossible to
2030 define constructor for them, hence constructors
2031 must be generated following the hints provided by
2032 the `new' expression. Whether a super constructor
2033 of that nature exists or not is to be verified
2034 later on in verify_constructor_super.
2036 It's during the expansion of a `new' statement
2037 refering to an anonymous class that a ctor will
2038 be generated for the anonymous class, with the
2039 right arguments. */
2042 | NEW_TK class_type OP_TK CP_TK
2043 { create_anonymous_class ($1.location, $2); }
2044 class_body
2046 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2047 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2049 end_class_declaration (1);
2051 /* Now we can craft the new expression. The
2052 statement doesn't need to be remember so that a
2053 constructor can be generated, since its signature
2054 is already known. */
2055 $$ = build_new_invocation (id, NULL_TREE);
2059 something_dot_new: /* Added, not part of the specs. */
2060 name DOT_TK NEW_TK
2061 { $$ = $1; }
2062 | primary DOT_TK NEW_TK
2063 { $$ = $1; }
2066 argument_list:
2067 expression
2069 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2070 ctxp->formal_parameter_number = 1;
2072 | argument_list C_TK expression
2074 ctxp->formal_parameter_number += 1;
2075 $$ = tree_cons (NULL_TREE, $3, $1);
2077 | argument_list C_TK error
2078 {yyerror ("Missing term"); RECOVER;}
2081 array_creation_expression:
2082 NEW_TK primitive_type dim_exprs
2083 { $$ = build_newarray_node ($2, $3, 0); }
2084 | NEW_TK class_or_interface_type dim_exprs
2085 { $$ = build_newarray_node ($2, $3, 0); }
2086 | NEW_TK primitive_type dim_exprs dims
2087 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
2088 | NEW_TK class_or_interface_type dim_exprs dims
2089 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
2090 /* Added, JDK1.1 anonymous array. Initial documentation rule
2091 modified */
2092 | NEW_TK class_or_interface_type dims array_initializer
2094 char *sig;
2095 while (CURRENT_OSB (ctxp)--)
2096 obstack_1grow (&temporary_obstack, '[');
2097 sig = obstack_finish (&temporary_obstack);
2098 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2099 $2, get_identifier (sig), $4);
2101 | NEW_TK primitive_type dims array_initializer
2103 tree type = $2;
2104 while (CURRENT_OSB (ctxp)--)
2105 type = build_java_array_type (type, -1);
2106 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2107 build_pointer_type (type), NULL_TREE, $4);
2109 | NEW_TK error CSB_TK
2110 {yyerror ("'[' expected"); DRECOVER ("]");}
2111 | NEW_TK error OSB_TK
2112 {yyerror ("']' expected"); RECOVER;}
2115 dim_exprs:
2116 dim_expr
2117 { $$ = build_tree_list (NULL_TREE, $1); }
2118 | dim_exprs dim_expr
2119 { $$ = tree_cons (NULL_TREE, $2, $$); }
2122 dim_expr:
2123 OSB_TK expression CSB_TK
2125 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2127 $2 = build_wfl_node ($2);
2128 TREE_TYPE ($2) = NULL_TREE;
2130 EXPR_WFL_LINECOL ($2) = $1.location;
2131 $$ = $2;
2133 | OSB_TK expression error
2134 {yyerror ("']' expected"); RECOVER;}
2135 | OSB_TK error
2137 yyerror ("Missing term");
2138 yyerror ("']' expected");
2139 RECOVER;
2143 dims:
2144 OSB_TK CSB_TK
2146 int allocate = 0;
2147 /* If not initialized, allocate memory for the osb
2148 numbers stack */
2149 if (!ctxp->osb_limit)
2151 allocate = ctxp->osb_limit = 32;
2152 ctxp->osb_depth = -1;
2154 /* If capacity overflown, reallocate a bigger chunk */
2155 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2156 allocate = ctxp->osb_limit << 1;
2158 if (allocate)
2160 allocate *= sizeof (int);
2161 if (ctxp->osb_number)
2162 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2163 allocate);
2164 else
2165 ctxp->osb_number = (int *)xmalloc (allocate);
2167 ctxp->osb_depth++;
2168 CURRENT_OSB (ctxp) = 1;
2170 | dims OSB_TK CSB_TK
2171 { CURRENT_OSB (ctxp)++; }
2172 | dims OSB_TK error
2173 { yyerror ("']' expected"); RECOVER;}
2176 field_access:
2177 primary DOT_TK identifier
2178 { $$ = make_qualified_primary ($1, $3, $2.location); }
2179 /* FIXME - REWRITE TO:
2180 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2181 | SUPER_TK DOT_TK identifier
2183 tree super_wfl = build_wfl_node (super_identifier_node);
2184 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2185 $$ = make_qualified_name (super_wfl, $3, $2.location);
2187 | SUPER_TK error
2188 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2191 method_invocation:
2192 name OP_TK CP_TK
2193 { $$ = build_method_invocation ($1, NULL_TREE); }
2194 | name OP_TK argument_list CP_TK
2195 { $$ = build_method_invocation ($1, $3); }
2196 | primary DOT_TK identifier OP_TK CP_TK
2198 if (TREE_CODE ($1) == THIS_EXPR)
2199 $$ = build_this_super_qualified_invocation
2200 (1, $3, NULL_TREE, 0, $2.location);
2201 else
2203 tree invok = build_method_invocation ($3, NULL_TREE);
2204 $$ = make_qualified_primary ($1, invok, $2.location);
2207 | primary DOT_TK identifier OP_TK argument_list CP_TK
2209 if (TREE_CODE ($1) == THIS_EXPR)
2210 $$ = build_this_super_qualified_invocation
2211 (1, $3, $5, 0, $2.location);
2212 else
2214 tree invok = build_method_invocation ($3, $5);
2215 $$ = make_qualified_primary ($1, invok, $2.location);
2218 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2220 $$ = build_this_super_qualified_invocation
2221 (0, $3, NULL_TREE, $1.location, $2.location);
2223 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2225 $$ = build_this_super_qualified_invocation
2226 (0, $3, $5, $1.location, $2.location);
2228 /* Screws up thing. I let it here until I'm convinced it can
2229 be removed. FIXME
2230 | primary DOT_TK error
2231 {yyerror ("'(' expected"); DRECOVER(bad);} */
2232 | SUPER_TK DOT_TK error CP_TK
2233 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2234 | SUPER_TK DOT_TK error DOT_TK
2235 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2238 array_access:
2239 name OSB_TK expression CSB_TK
2240 { $$ = build_array_ref ($2.location, $1, $3); }
2241 | primary_no_new_array OSB_TK expression CSB_TK
2242 { $$ = build_array_ref ($2.location, $1, $3); }
2243 | name OSB_TK error
2245 yyerror ("Missing term and ']' expected");
2246 DRECOVER(array_access);
2248 | name OSB_TK expression error
2250 yyerror ("']' expected");
2251 DRECOVER(array_access);
2253 | primary_no_new_array OSB_TK error
2255 yyerror ("Missing term and ']' expected");
2256 DRECOVER(array_access);
2258 | primary_no_new_array OSB_TK expression error
2260 yyerror ("']' expected");
2261 DRECOVER(array_access);
2265 postfix_expression:
2266 primary
2267 | name
2268 | post_increment_expression
2269 | post_decrement_expression
2272 post_increment_expression:
2273 postfix_expression INCR_TK
2274 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2277 post_decrement_expression:
2278 postfix_expression DECR_TK
2279 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2282 unary_expression:
2283 pre_increment_expression
2284 | pre_decrement_expression
2285 | PLUS_TK unary_expression
2286 {$$ = build_unaryop ($1.token, $1.location, $2); }
2287 | MINUS_TK unary_expression
2288 {$$ = build_unaryop ($1.token, $1.location, $2); }
2289 | unary_expression_not_plus_minus
2290 | PLUS_TK error
2291 {yyerror ("Missing term"); RECOVER}
2292 | MINUS_TK error
2293 {yyerror ("Missing term"); RECOVER}
2296 pre_increment_expression:
2297 INCR_TK unary_expression
2298 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2299 | INCR_TK error
2300 {yyerror ("Missing term"); RECOVER}
2303 pre_decrement_expression:
2304 DECR_TK unary_expression
2305 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2306 | DECR_TK error
2307 {yyerror ("Missing term"); RECOVER}
2310 unary_expression_not_plus_minus:
2311 postfix_expression
2312 | NOT_TK unary_expression
2313 {$$ = build_unaryop ($1.token, $1.location, $2); }
2314 | NEG_TK unary_expression
2315 {$$ = build_unaryop ($1.token, $1.location, $2); }
2316 | cast_expression
2317 | NOT_TK error
2318 {yyerror ("Missing term"); RECOVER}
2319 | NEG_TK error
2320 {yyerror ("Missing term"); RECOVER}
2323 cast_expression: /* Error handling here is potentially weak */
2324 OP_TK primitive_type dims CP_TK unary_expression
2326 tree type = $2;
2327 while (CURRENT_OSB (ctxp)--)
2328 type = build_java_array_type (type, -1);
2329 ctxp->osb_depth--;
2330 $$ = build_cast ($1.location, type, $5);
2332 | OP_TK primitive_type CP_TK unary_expression
2333 { $$ = build_cast ($1.location, $2, $4); }
2334 | OP_TK expression CP_TK unary_expression_not_plus_minus
2335 { $$ = build_cast ($1.location, $2, $4); }
2336 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2338 const char *ptr;
2339 while (CURRENT_OSB (ctxp)--)
2340 obstack_1grow (&temporary_obstack, '[');
2341 ctxp->osb_depth--;
2342 obstack_grow0 (&temporary_obstack,
2343 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2344 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2345 ptr = obstack_finish (&temporary_obstack);
2346 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2347 $$ = build_cast ($1.location, $2, $5);
2349 | OP_TK primitive_type OSB_TK error
2350 {yyerror ("']' expected, invalid type expression");}
2351 | OP_TK error
2353 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2354 RECOVER;
2356 | OP_TK primitive_type dims CP_TK error
2357 {yyerror ("Missing term"); RECOVER;}
2358 | OP_TK primitive_type CP_TK error
2359 {yyerror ("Missing term"); RECOVER;}
2360 | OP_TK name dims CP_TK error
2361 {yyerror ("Missing term"); RECOVER;}
2364 multiplicative_expression:
2365 unary_expression
2366 | multiplicative_expression MULT_TK unary_expression
2368 $$ = build_binop (BINOP_LOOKUP ($2.token),
2369 $2.location, $1, $3);
2371 | multiplicative_expression DIV_TK unary_expression
2373 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2374 $1, $3);
2376 | multiplicative_expression REM_TK unary_expression
2378 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2379 $1, $3);
2381 | multiplicative_expression MULT_TK error
2382 {yyerror ("Missing term"); RECOVER;}
2383 | multiplicative_expression DIV_TK error
2384 {yyerror ("Missing term"); RECOVER;}
2385 | multiplicative_expression REM_TK error
2386 {yyerror ("Missing term"); RECOVER;}
2389 additive_expression:
2390 multiplicative_expression
2391 | additive_expression PLUS_TK multiplicative_expression
2393 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2394 $1, $3);
2396 | additive_expression MINUS_TK multiplicative_expression
2398 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2399 $1, $3);
2401 | additive_expression PLUS_TK error
2402 {yyerror ("Missing term"); RECOVER;}
2403 | additive_expression MINUS_TK error
2404 {yyerror ("Missing term"); RECOVER;}
2407 shift_expression:
2408 additive_expression
2409 | shift_expression LS_TK additive_expression
2411 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2412 $1, $3);
2414 | shift_expression SRS_TK additive_expression
2416 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2417 $1, $3);
2419 | shift_expression ZRS_TK additive_expression
2421 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2422 $1, $3);
2424 | shift_expression LS_TK error
2425 {yyerror ("Missing term"); RECOVER;}
2426 | shift_expression SRS_TK error
2427 {yyerror ("Missing term"); RECOVER;}
2428 | shift_expression ZRS_TK error
2429 {yyerror ("Missing term"); RECOVER;}
2432 relational_expression:
2433 shift_expression
2434 | relational_expression LT_TK shift_expression
2436 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2437 $1, $3);
2439 | relational_expression GT_TK shift_expression
2441 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2442 $1, $3);
2444 | relational_expression LTE_TK shift_expression
2446 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2447 $1, $3);
2449 | relational_expression GTE_TK shift_expression
2451 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2452 $1, $3);
2454 | relational_expression INSTANCEOF_TK reference_type
2455 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2456 | relational_expression LT_TK error
2457 {yyerror ("Missing term"); RECOVER;}
2458 | relational_expression GT_TK error
2459 {yyerror ("Missing term"); RECOVER;}
2460 | relational_expression LTE_TK error
2461 {yyerror ("Missing term"); RECOVER;}
2462 | relational_expression GTE_TK error
2463 {yyerror ("Missing term"); RECOVER;}
2464 | relational_expression INSTANCEOF_TK error
2465 {yyerror ("Invalid reference type"); RECOVER;}
2468 equality_expression:
2469 relational_expression
2470 | equality_expression EQ_TK relational_expression
2472 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2473 $1, $3);
2475 | equality_expression NEQ_TK relational_expression
2477 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2478 $1, $3);
2480 | equality_expression EQ_TK error
2481 {yyerror ("Missing term"); RECOVER;}
2482 | equality_expression NEQ_TK error
2483 {yyerror ("Missing term"); RECOVER;}
2486 and_expression:
2487 equality_expression
2488 | and_expression AND_TK equality_expression
2490 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2491 $1, $3);
2493 | and_expression AND_TK error
2494 {yyerror ("Missing term"); RECOVER;}
2497 exclusive_or_expression:
2498 and_expression
2499 | exclusive_or_expression XOR_TK and_expression
2501 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2502 $1, $3);
2504 | exclusive_or_expression XOR_TK error
2505 {yyerror ("Missing term"); RECOVER;}
2508 inclusive_or_expression:
2509 exclusive_or_expression
2510 | inclusive_or_expression OR_TK exclusive_or_expression
2512 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2513 $1, $3);
2515 | inclusive_or_expression OR_TK error
2516 {yyerror ("Missing term"); RECOVER;}
2519 conditional_and_expression:
2520 inclusive_or_expression
2521 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2523 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2524 $1, $3);
2526 | conditional_and_expression BOOL_AND_TK error
2527 {yyerror ("Missing term"); RECOVER;}
2530 conditional_or_expression:
2531 conditional_and_expression
2532 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2534 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2535 $1, $3);
2537 | conditional_or_expression BOOL_OR_TK error
2538 {yyerror ("Missing term"); RECOVER;}
2541 conditional_expression: /* Error handling here is weak */
2542 conditional_or_expression
2543 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2545 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2546 EXPR_WFL_LINECOL ($$) = $2.location;
2548 | conditional_or_expression REL_QM_TK REL_CL_TK error
2550 YYERROR_NOW;
2551 yyerror ("Missing term");
2552 DRECOVER (1);
2554 | conditional_or_expression REL_QM_TK error
2555 {yyerror ("Missing term"); DRECOVER (2);}
2556 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2557 {yyerror ("Missing term"); DRECOVER (3);}
2560 assignment_expression:
2561 conditional_expression
2562 | assignment
2565 assignment:
2566 left_hand_side assignment_operator assignment_expression
2567 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2568 | left_hand_side assignment_operator error
2570 YYNOT_TWICE yyerror ("Missing term");
2571 DRECOVER (assign);
2575 left_hand_side:
2576 name
2577 | field_access
2578 | array_access
2581 assignment_operator:
2582 ASSIGN_ANY_TK
2583 | ASSIGN_TK
2586 expression:
2587 assignment_expression
2590 constant_expression:
2591 expression
2597 /* This section of the code deal with save/restoring parser contexts.
2598 Add mode documentation here. FIXME */
2600 /* Helper function. Create a new parser context. With
2601 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2602 context is copied, otherwise, the new context is zeroed. The newly
2603 created context becomes the current one. */
2605 static void
2606 create_new_parser_context (copy_from_previous)
2607 int copy_from_previous;
2609 struct parser_ctxt *new;
2611 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2612 if (copy_from_previous)
2614 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2615 new->saved_data_ctx = 1;
2617 else
2618 bzero ((PTR) new, sizeof (struct parser_ctxt));
2620 new->next = ctxp;
2621 ctxp = new;
2624 /* Create a new parser context and make it the current one. */
2626 void
2627 java_push_parser_context ()
2629 create_new_parser_context (0);
2630 if (ctxp->next)
2632 ctxp->incomplete_class = ctxp->next->incomplete_class;
2633 ctxp->gclass_list = ctxp->next->gclass_list;
2637 void
2638 java_pop_parser_context (generate)
2639 int generate;
2641 tree current;
2642 struct parser_ctxt *toFree, *next;
2644 if (!ctxp)
2645 return;
2647 toFree = ctxp;
2648 next = ctxp->next;
2649 if (next)
2651 next->incomplete_class = ctxp->incomplete_class;
2652 next->gclass_list = ctxp->gclass_list;
2653 lineno = ctxp->lineno;
2654 current_class = ctxp->class_type;
2657 /* If the old and new lexers differ, then free the old one. */
2658 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2659 java_destroy_lexer (ctxp->lexer);
2661 /* Set the single import class file flag to 0 for the current list
2662 of imported things */
2663 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2664 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2666 /* And restore those of the previous context */
2667 if ((ctxp = next)) /* Assignment is really meant here */
2668 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2669 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2671 /* If we pushed a context to parse a class intended to be generated,
2672 we keep it so we can remember the class. What we could actually
2673 do is to just update a list of class names. */
2674 if (generate)
2676 toFree->next = ctxp_for_generation;
2677 ctxp_for_generation = toFree;
2679 else
2680 free (toFree);
2683 /* Create a parser context for the use of saving some global
2684 variables. */
2686 void
2687 java_parser_context_save_global ()
2689 if (!ctxp)
2691 java_push_parser_context ();
2692 ctxp->saved_data_ctx = 1;
2695 /* If this context already stores data, create a new one suitable
2696 for data storage. */
2697 else if (ctxp->saved_data)
2698 create_new_parser_context (1);
2700 ctxp->lineno = lineno;
2701 ctxp->class_type = current_class;
2702 ctxp->filename = input_filename;
2703 ctxp->function_decl = current_function_decl;
2704 ctxp->saved_data = 1;
2707 /* Restore some global variables from the previous context. Make the
2708 previous context the current one. */
2710 void
2711 java_parser_context_restore_global ()
2713 lineno = ctxp->lineno;
2714 current_class = ctxp->class_type;
2715 input_filename = ctxp->filename;
2716 if (wfl_operator)
2718 tree s;
2719 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2720 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2722 current_function_decl = ctxp->function_decl;
2723 ctxp->saved_data = 0;
2724 if (ctxp->saved_data_ctx)
2725 java_pop_parser_context (0);
2728 /* Suspend vital data for the current class/function being parsed so
2729 that an other class can be parsed. Used to let local/anonymous
2730 classes be parsed. */
2732 static void
2733 java_parser_context_suspend ()
2735 /* This makes debugging through java_debug_context easier */
2736 static const char *name = "<inner buffer context>";
2738 /* Duplicate the previous context, use it to save the globals we're
2739 interested in */
2740 create_new_parser_context (1);
2741 ctxp->function_decl = current_function_decl;
2742 ctxp->class_type = current_class;
2744 /* Then create a new context which inherits all data from the
2745 previous one. This will be the new current context */
2746 create_new_parser_context (1);
2748 /* Help debugging */
2749 ctxp->next->filename = name;
2752 /* Resume vital data for the current class/function being parsed so
2753 that an other class can be parsed. Used to let local/anonymous
2754 classes be parsed. The trick is the data storing file position
2755 informations must be restored to their current value, so parsing
2756 can resume as if no context was ever saved. */
2758 static void
2759 java_parser_context_resume ()
2761 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2762 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2763 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2765 /* We need to inherit the list of classes to complete/generate */
2766 restored->incomplete_class = old->incomplete_class;
2767 restored->gclass_list = old->gclass_list;
2768 restored->classd_list = old->classd_list;
2769 restored->class_list = old->class_list;
2771 /* Restore the current class and function from the saver */
2772 current_class = saver->class_type;
2773 current_function_decl = saver->function_decl;
2775 /* Retrive the restored context */
2776 ctxp = restored;
2778 /* Re-installed the data for the parsing to carry on */
2779 bcopy (&old->marker_begining, &ctxp->marker_begining,
2780 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2782 /* Buffer context can now be discarded */
2783 free (saver);
2784 free (old);
2787 /* Add a new anchor node to which all statement(s) initializing static
2788 and non static initialized upon declaration field(s) will be
2789 linked. */
2791 static void
2792 java_parser_context_push_initialized_field ()
2794 tree node;
2796 node = build_tree_list (NULL_TREE, NULL_TREE);
2797 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2798 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2800 node = build_tree_list (NULL_TREE, NULL_TREE);
2801 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2802 CPC_INITIALIZER_LIST (ctxp) = node;
2804 node = build_tree_list (NULL_TREE, NULL_TREE);
2805 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2806 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2809 /* Pop the lists of initialized field. If this lists aren't empty,
2810 remember them so we can use it to create and populate the finit$
2811 or <clinit> functions. */
2813 static void
2814 java_parser_context_pop_initialized_field ()
2816 tree stmts;
2817 tree class_type = TREE_TYPE (GET_CPC ());
2819 if (CPC_INITIALIZER_LIST (ctxp))
2821 stmts = CPC_INITIALIZER_STMT (ctxp);
2822 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2823 if (stmts && !java_error_count)
2824 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2827 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2829 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2830 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2831 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2832 /* Keep initialization in order to enforce 8.5 */
2833 if (stmts && !java_error_count)
2834 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2837 /* JDK 1.1 instance initializers */
2838 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2840 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2841 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2842 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2843 if (stmts && !java_error_count)
2844 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2848 static tree
2849 reorder_static_initialized (list)
2850 tree list;
2852 /* We have to keep things in order. The alias initializer have to
2853 come first, then the initialized regular field, in reverse to
2854 keep them in lexical order. */
2855 tree marker, previous = NULL_TREE;
2856 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2857 if (TREE_CODE (marker) == TREE_LIST
2858 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2859 break;
2861 /* No static initialized, the list is fine as is */
2862 if (!previous)
2863 list = TREE_CHAIN (marker);
2865 /* No marker? reverse the whole list */
2866 else if (!marker)
2867 list = nreverse (list);
2869 /* Otherwise, reverse what's after the marker and the new reordered
2870 sublist will replace the marker. */
2871 else
2873 TREE_CHAIN (previous) = NULL_TREE;
2874 list = nreverse (list);
2875 list = chainon (TREE_CHAIN (marker), list);
2877 return list;
2880 /* Helper functions to dump the parser context stack. */
2882 #define TAB_CONTEXT(C) \
2883 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2885 static void
2886 java_debug_context_do (tab)
2887 int tab;
2889 struct parser_ctxt *copy = ctxp;
2890 while (copy)
2892 TAB_CONTEXT (tab);
2893 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2894 TAB_CONTEXT (tab);
2895 fprintf (stderr, "filename: %s\n", copy->filename);
2896 TAB_CONTEXT (tab);
2897 fprintf (stderr, "lineno: %d\n", copy->lineno);
2898 TAB_CONTEXT (tab);
2899 fprintf (stderr, "package: %s\n",
2900 (copy->package ?
2901 IDENTIFIER_POINTER (copy->package) : "<none>"));
2902 TAB_CONTEXT (tab);
2903 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2904 TAB_CONTEXT (tab);
2905 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2906 copy = copy->next;
2907 tab += 2;
2911 /* Dump the stacked up parser contexts. Intended to be called from a
2912 debugger. */
2914 void
2915 java_debug_context ()
2917 java_debug_context_do (0);
2922 /* Flag for the error report routine to issue the error the first time
2923 it's called (overriding the default behavior which is to drop the
2924 first invocation and honor the second one, taking advantage of a
2925 richer context. */
2926 static int force_error = 0;
2928 /* Reporting an constructor invocation error. */
2929 static void
2930 parse_ctor_invocation_error ()
2932 if (DECL_CONSTRUCTOR_P (current_function_decl))
2933 yyerror ("Constructor invocation must be first thing in a constructor");
2934 else
2935 yyerror ("Only constructors can invoke constructors");
2938 /* Reporting JDK1.1 features not implemented. */
2940 static tree
2941 parse_jdk1_1_error (msg)
2942 const char *msg;
2944 sorry (": `%s' JDK1.1(TM) feature", msg);
2945 java_error_count++;
2946 return empty_stmt_node;
2949 static int do_warning = 0;
2951 void
2952 yyerror (msg)
2953 const char *msg;
2955 static java_lc elc;
2956 static int prev_lineno;
2957 static const char *prev_msg;
2959 int save_lineno;
2960 char *remainder, *code_from_source;
2961 extern struct obstack temporary_obstack;
2963 if (!force_error && prev_lineno == lineno)
2964 return;
2966 /* Save current error location but report latter, when the context is
2967 richer. */
2968 if (ctxp->java_error_flag == 0)
2970 ctxp->java_error_flag = 1;
2971 elc = ctxp->elc;
2972 /* Do something to use the previous line if we're reaching the
2973 end of the file... */
2974 #ifdef VERBOSE_SKELETON
2975 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2976 #endif
2977 return;
2980 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2981 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2982 return;
2984 ctxp->java_error_flag = 0;
2985 if (do_warning)
2986 java_warning_count++;
2987 else
2988 java_error_count++;
2990 if (elc.col == 0 && msg && msg[1] == ';')
2992 elc.col = ctxp->p_line->char_col-1;
2993 elc.line = ctxp->p_line->lineno;
2996 save_lineno = lineno;
2997 prev_lineno = lineno = elc.line;
2998 prev_msg = msg;
3000 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3001 obstack_grow0 (&temporary_obstack,
3002 code_from_source, strlen (code_from_source));
3003 remainder = obstack_finish (&temporary_obstack);
3004 if (do_warning)
3005 warning ("%s.\n%s", msg, remainder);
3006 else
3007 error ("%s.\n%s", msg, remainder);
3009 /* This allow us to cheaply avoid an extra 'Invalid expression
3010 statement' error report when errors have been already reported on
3011 the same line. This occurs when we report an error but don't have
3012 a synchronization point other than ';', which
3013 expression_statement is the only one to take care of. */
3014 ctxp->prevent_ese = lineno = save_lineno;
3017 static void
3018 issue_warning_error_from_context (cl, msg, ap)
3019 tree cl;
3020 const char *msg;
3021 va_list ap;
3023 const char *saved, *saved_input_filename;
3024 char buffer [4096];
3025 vsprintf (buffer, msg, ap);
3026 force_error = 1;
3028 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3029 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3030 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3032 /* We have a CL, that's a good reason for using it if it contains data */
3033 saved = ctxp->filename;
3034 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3035 ctxp->filename = EXPR_WFL_FILENAME (cl);
3036 saved_input_filename = input_filename;
3037 input_filename = ctxp->filename;
3038 java_error (NULL);
3039 java_error (buffer);
3040 ctxp->filename = saved;
3041 input_filename = saved_input_filename;
3042 force_error = 0;
3045 /* Issue an error message at a current source line CL */
3047 void
3048 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3050 #ifndef ANSI_PROTOTYPES
3051 tree cl;
3052 const char *msg;
3053 #endif
3054 va_list ap;
3056 VA_START (ap, msg);
3057 #ifndef ANSI_PROTOTYPES
3058 cl = va_arg (ap, tree);
3059 msg = va_arg (ap, const char *);
3060 #endif
3061 issue_warning_error_from_context (cl, msg, ap);
3062 va_end (ap);
3065 /* Issue a warning at a current source line CL */
3067 static void
3068 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3070 #ifndef ANSI_PROTOTYPES
3071 tree cl;
3072 const char *msg;
3073 #endif
3074 va_list ap;
3076 VA_START (ap, msg);
3077 #ifndef ANSI_PROTOTYPES
3078 cl = va_arg (ap, tree);
3079 msg = va_arg (ap, const char *);
3080 #endif
3082 force_error = do_warning = 1;
3083 issue_warning_error_from_context (cl, msg, ap);
3084 do_warning = force_error = 0;
3085 va_end (ap);
3088 static tree
3089 find_expr_with_wfl (node)
3090 tree node;
3092 while (node)
3094 char code;
3095 tree to_return;
3097 switch (TREE_CODE (node))
3099 case BLOCK:
3100 node = BLOCK_EXPR_BODY (node);
3101 continue;
3103 case COMPOUND_EXPR:
3104 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3105 if (to_return)
3106 return to_return;
3107 node = TREE_OPERAND (node, 1);
3108 continue;
3110 case LOOP_EXPR:
3111 node = TREE_OPERAND (node, 0);
3112 continue;
3114 case LABELED_BLOCK_EXPR:
3115 node = TREE_OPERAND (node, 1);
3116 continue;
3118 default:
3119 code = TREE_CODE_CLASS (TREE_CODE (node));
3120 if (((code == '1') || (code == '2') || (code == 'e'))
3121 && EXPR_WFL_LINECOL (node))
3122 return node;
3123 return NULL_TREE;
3126 return NULL_TREE;
3129 /* Issue a missing return statement error. Uses METHOD to figure the
3130 last line of the method the error occurs in. */
3132 static void
3133 missing_return_error (method)
3134 tree method;
3136 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3137 parse_error_context (wfl_operator, "Missing return statement");
3140 /* Issue an unreachable statement error. From NODE, find the next
3141 statement to report appropriately. */
3142 static void
3143 unreachable_stmt_error (node)
3144 tree node;
3146 /* Browse node to find the next expression node that has a WFL. Use
3147 the location to report the error */
3148 if (TREE_CODE (node) == COMPOUND_EXPR)
3149 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3150 else
3151 node = find_expr_with_wfl (node);
3153 if (node)
3155 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3156 parse_error_context (wfl_operator, "Unreachable statement");
3158 else
3159 fatal ("Can't get valid statement - unreachable_stmt_error");
3163 java_report_errors ()
3165 if (java_error_count)
3166 fprintf (stderr, "%d error%s",
3167 java_error_count, (java_error_count == 1 ? "" : "s"));
3168 if (java_warning_count)
3169 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3170 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3171 if (java_error_count || java_warning_count)
3172 putc ('\n', stderr);
3173 return java_error_count;
3176 static char *
3177 java_accstring_lookup (flags)
3178 int flags;
3180 static char buffer [80];
3181 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3183 /* Access modifier looked-up first for easier report on forbidden
3184 access. */
3185 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3186 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3187 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3188 if (flags & ACC_STATIC) COPY_RETURN ("static");
3189 if (flags & ACC_FINAL) COPY_RETURN ("final");
3190 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3191 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3192 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3193 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3194 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3195 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3197 buffer [0] = '\0';
3198 return buffer;
3199 #undef COPY_RETURN
3202 /* Issuing error messages upon redefinition of classes, interfaces or
3203 variables. */
3205 static void
3206 classitf_redefinition_error (context, id, decl, cl)
3207 const char *context;
3208 tree id, decl, cl;
3210 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3211 context, IDENTIFIER_POINTER (id),
3212 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3213 /* Here we should point out where its redefined. It's a unicode. FIXME */
3216 static void
3217 variable_redefinition_error (context, name, type, line)
3218 tree context, name, type;
3219 int line;
3221 const char *type_name;
3223 /* Figure a proper name for type. We might haven't resolved it */
3224 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3225 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3226 else
3227 type_name = lang_printable_name (type, 0);
3229 parse_error_context (context,
3230 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3231 IDENTIFIER_POINTER (name),
3232 type_name, IDENTIFIER_POINTER (name), line);
3235 static tree
3236 build_array_from_name (type, type_wfl, name, ret_name)
3237 tree type, type_wfl, name, *ret_name;
3239 int more_dims = 0;
3240 const char *string;
3242 /* Eventually get more dims */
3243 string = IDENTIFIER_POINTER (name);
3244 while (string [more_dims] == '[')
3245 more_dims++;
3247 /* If we have, then craft a new type for this variable */
3248 if (more_dims)
3250 name = get_identifier (&string [more_dims]);
3252 /* If we have a pointer, use its type */
3253 if (TREE_CODE (type) == POINTER_TYPE)
3254 type = TREE_TYPE (type);
3256 /* Building the first dimension of a primitive type uses this
3257 function */
3258 if (JPRIMITIVE_TYPE_P (type))
3260 type = build_java_array_type (type, -1);
3261 CLASS_LOADED_P (type) = 1;
3262 more_dims--;
3264 /* Otherwise, if we have a WFL for this type, use it (the type
3265 is already an array on an unresolved type, and we just keep
3266 on adding dimensions) */
3267 else if (type_wfl)
3268 type = type_wfl;
3270 /* Add all the dimensions */
3271 while (more_dims--)
3272 type = build_unresolved_array_type (type);
3274 /* The type may have been incomplete in the first place */
3275 if (type_wfl)
3276 type = obtain_incomplete_type (type);
3279 if (ret_name)
3280 *ret_name = name;
3281 return type;
3284 /* Build something that the type identifier resolver will identify as
3285 being an array to an unresolved type. TYPE_WFL is a WFL on a
3286 identifier. */
3288 static tree
3289 build_unresolved_array_type (type_or_wfl)
3290 tree type_or_wfl;
3292 const char *ptr;
3294 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3295 just create a array type */
3296 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3298 tree type = build_java_array_type (type_or_wfl, -1);
3299 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3300 return type;
3303 obstack_1grow (&temporary_obstack, '[');
3304 obstack_grow0 (&temporary_obstack,
3305 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3306 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3307 ptr = obstack_finish (&temporary_obstack);
3308 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3309 return type_or_wfl;
3312 static void
3313 parser_add_interface (class_decl, interface_decl, wfl)
3314 tree class_decl, interface_decl, wfl;
3316 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3317 parse_error_context (wfl, "Interface `%s' repeated",
3318 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3321 /* Bulk of common class/interface checks. Return 1 if an error was
3322 encountered. TAG is 0 for a class, 1 for an interface. */
3324 static int
3325 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3326 int is_interface, flags;
3327 tree raw_name, qualified_name, decl, cl;
3329 tree node;
3330 int sca = 0; /* Static class allowed */
3331 int icaf = 0; /* Inner class allowed flags */
3332 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3334 if (!quiet_flag)
3335 fprintf (stderr, " %s%s %s",
3336 (CPC_INNER_P () ? "inner" : ""),
3337 (is_interface ? "interface" : "class"),
3338 IDENTIFIER_POINTER (qualified_name));
3340 /* Scope of an interface/class type name:
3341 - Can't be imported by a single type import
3342 - Can't already exists in the package */
3343 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3344 && (node = find_name_in_single_imports (raw_name))
3345 && !CPC_INNER_P ())
3347 parse_error_context
3348 (cl, "%s name `%s' clashes with imported type `%s'",
3349 (is_interface ? "Interface" : "Class"),
3350 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3351 return 1;
3353 if (decl && CLASS_COMPLETE_P (decl))
3355 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3356 qualified_name, decl, cl);
3357 return 1;
3360 if (check_inner_class_redefinition (raw_name, cl))
3361 return 1;
3363 /* If public, file name should match class/interface name, except
3364 when dealing with an inner class */
3365 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3367 const char *f;
3369 /* Contains OS dependent assumption on path separator. FIXME */
3370 for (f = &input_filename [strlen (input_filename)];
3371 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3372 f--)
3374 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3375 f++;
3376 if (strncmp (IDENTIFIER_POINTER (raw_name),
3377 f , IDENTIFIER_LENGTH (raw_name)) ||
3378 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3379 parse_error_context
3380 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3381 (is_interface ? "interface" : "class"),
3382 IDENTIFIER_POINTER (qualified_name),
3383 IDENTIFIER_POINTER (raw_name));
3386 /* Static classes can be declared only in top level classes. Note:
3387 once static, a inner class is a top level class. */
3388 if (flags & ACC_STATIC)
3390 /* Catch the specific error of declaring an class inner class
3391 with no toplevel enclosing class. Prevent check_modifiers from
3392 complaining a second time */
3393 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3395 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3396 IDENTIFIER_POINTER (qualified_name));
3397 sca = ACC_STATIC;
3399 /* Else, in the context of a top-level class declaration, let
3400 `check_modifiers' do its job, otherwise, give it a go */
3401 else
3402 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3405 /* Inner classes can be declared private or protected
3406 within their enclosing classes. */
3407 if (CPC_INNER_P ())
3409 /* A class which is local to a block can't be public, private,
3410 protected or static. But it is created final, so allow this
3411 one. */
3412 if (current_function_decl)
3413 icaf = sca = uaaf = ACC_FINAL;
3414 else
3416 check_modifiers_consistency (flags);
3417 icaf = ACC_PRIVATE|ACC_PROTECTED;
3421 if (is_interface)
3423 if (CPC_INNER_P ())
3424 uaaf = INTERFACE_INNER_MODIFIERS;
3425 else
3426 uaaf = INTERFACE_MODIFIERS;
3428 check_modifiers ("Illegal modifier `%s' for interface declaration",
3429 flags, uaaf);
3431 else
3432 check_modifiers ((current_function_decl ?
3433 "Illegal modifier `%s' for local class declaration" :
3434 "Illegal modifier `%s' for class declaration"),
3435 flags, uaaf|sca|icaf);
3436 return 0;
3439 static void
3440 make_nested_class_name (cpc_list)
3441 tree cpc_list;
3443 tree name;
3445 if (!cpc_list)
3446 return;
3447 else
3448 make_nested_class_name (TREE_CHAIN (cpc_list));
3450 /* Pick the qualified name when dealing with the first upmost
3451 enclosing class */
3452 name = (TREE_CHAIN (cpc_list) ?
3453 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3454 obstack_grow (&temporary_obstack,
3455 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3456 /* Why is NO_DOLLAR_IN_LABEL defined? */
3457 #if 0
3458 #ifdef NO_DOLLAR_IN_LABEL
3459 fatal ("make_nested_class_name: Can't use '$' as a separator "
3460 "for inner classes");
3461 #endif
3462 #endif
3463 obstack_1grow (&temporary_obstack, '$');
3466 /* Can't redefine a class already defined in an earlier scope. */
3468 static int
3469 check_inner_class_redefinition (raw_name, cl)
3470 tree raw_name, cl;
3472 tree scope_list;
3474 for (scope_list = GET_CPC_LIST (); scope_list;
3475 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3476 if (raw_name == GET_CPC_UN_NODE (scope_list))
3478 parse_error_context
3479 (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",
3480 IDENTIFIER_POINTER (raw_name));
3481 return 1;
3483 return 0;
3486 static tree
3487 find_as_inner_class (enclosing, name, cl)
3488 tree enclosing, name, cl;
3490 tree qual, to_return;
3491 if (!enclosing)
3492 return NULL_TREE;
3494 name = TYPE_NAME (name);
3496 /* First search: within the scope of `enclosing', search for name */
3497 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3498 qual = EXPR_WFL_QUALIFICATION (cl);
3499 else if (cl)
3500 qual = build_tree_list (cl, NULL_TREE);
3501 else
3502 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3504 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3505 return to_return;
3507 /* We're dealing with a qualified name. Try to resolve thing until
3508 we get something that is an enclosing class. */
3509 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3511 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3513 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3514 qual = TREE_CHAIN (qual))
3516 acc = merge_qualified_name (acc,
3517 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3518 BUILD_PTR_FROM_NAME (ptr, acc);
3519 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3522 /* A NULL qual and a decl means that the search ended
3523 successfully?!? We have to do something then. FIXME */
3525 if (decl)
3526 enclosing = decl;
3527 else
3528 qual = EXPR_WFL_QUALIFICATION (cl);
3530 /* Otherwise, create a qual for the other part of the resolution. */
3531 else
3532 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3534 return find_as_inner_class_do (qual, enclosing);
3537 /* We go inside the list of sub classes and try to find a way
3538 through. */
3540 static tree
3541 find_as_inner_class_do (qual, enclosing)
3542 tree qual, enclosing;
3544 if (!qual)
3545 return NULL_TREE;
3547 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3549 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3550 tree next_enclosing = NULL_TREE;
3551 tree inner_list;
3553 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3554 inner_list; inner_list = TREE_CHAIN (inner_list))
3556 if (TREE_VALUE (inner_list) == name_to_match)
3558 next_enclosing = TREE_PURPOSE (inner_list);
3559 break;
3562 enclosing = next_enclosing;
3565 return (!qual && enclosing ? enclosing : NULL_TREE);
3568 /* Reach all inner classes and tie their unqualified name to a
3569 DECL. */
3571 static void
3572 set_nested_class_simple_name_value (outer, set)
3573 tree outer;
3574 int set;
3576 tree l;
3578 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3579 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3580 TREE_PURPOSE (l) : NULL_TREE);
3583 static void
3584 link_nested_class_to_enclosing ()
3586 if (GET_ENCLOSING_CPC ())
3588 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3589 DECL_INNER_CLASS_LIST (enclosing) =
3590 tree_cons (GET_CPC (), GET_CPC_UN (),
3591 DECL_INNER_CLASS_LIST (enclosing));
3592 enclosing = enclosing;
3596 static tree
3597 maybe_make_nested_class_name (name)
3598 tree name;
3600 tree id = NULL_TREE;
3602 if (CPC_INNER_P ())
3604 make_nested_class_name (GET_CPC_LIST ());
3605 obstack_grow0 (&temporary_obstack,
3606 IDENTIFIER_POINTER (name),
3607 IDENTIFIER_LENGTH (name));
3608 id = get_identifier (obstack_finish (&temporary_obstack));
3609 if (ctxp->package)
3610 QUALIFIED_P (id) = 1;
3612 return id;
3615 /* If DECL is NULL, create and push a new DECL, record the current
3616 line CL and do other maintenance things. */
3618 static tree
3619 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3620 tree decl, raw_name, qualified_name, cl;
3622 if (!decl)
3623 decl = push_class (make_class (), qualified_name);
3625 /* Take care of the file and line business */
3626 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3627 /* If we're emiting xrefs, store the line/col number information */
3628 if (flag_emit_xref)
3629 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3630 else
3631 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3632 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3633 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3634 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3636 PUSH_CPC (decl, raw_name);
3637 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3639 /* Link the declaration to the already seen ones */
3640 TREE_CHAIN (decl) = ctxp->class_list;
3641 ctxp->class_list = decl;
3643 /* Create a new nodes in the global lists */
3644 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
3645 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3647 /* Install a new dependency list element */
3648 create_jdep_list (ctxp);
3650 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3651 IDENTIFIER_POINTER (qualified_name)));
3652 return decl;
3655 static void
3656 add_superinterfaces (decl, interface_list)
3657 tree decl, interface_list;
3659 tree node;
3660 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3661 takes care of ensuring that:
3662 - This is an accessible interface type,
3663 - Circularity detection.
3664 parser_add_interface is then called. If present but not defined,
3665 the check operation is delayed until the super interface gets
3666 defined. */
3667 for (node = interface_list; node; node = TREE_CHAIN (node))
3669 tree current = TREE_PURPOSE (node);
3670 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3671 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3673 if (!parser_check_super_interface (idecl, decl, current))
3674 parser_add_interface (decl, idecl, current);
3676 else
3677 register_incomplete_type (JDEP_INTERFACE,
3678 current, decl, NULL_TREE);
3682 /* Create an interface in pass1 and return its decl. Return the
3683 interface's decl in pass 2. */
3685 static tree
3686 create_interface (flags, id, super)
3687 int flags;
3688 tree id, super;
3690 tree raw_name = EXPR_WFL_NODE (id);
3691 tree q_name = parser_qualified_classname (raw_name);
3692 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3694 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3696 /* Basic checks: scope, redefinition, modifiers */
3697 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3699 PUSH_ERROR ();
3700 return NULL_TREE;
3703 /* Suspend the current parsing context if we're parsing an inner
3704 interface */
3705 if (CPC_INNER_P ())
3706 java_parser_context_suspend ();
3708 /* Push a new context for (static) initialized upon declaration fields */
3709 java_parser_context_push_initialized_field ();
3711 /* Interface modifiers check
3712 - public/abstract allowed (already done at that point)
3713 - abstract is obsolete (comes first, it's a warning, or should be)
3714 - Can't use twice the same (checked in the modifier rule) */
3715 if ((flags & ACC_ABSTRACT) && flag_redundant)
3716 parse_warning_context
3717 (MODIFIER_WFL (ABSTRACT_TK),
3718 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3720 /* Create a new decl if DECL is NULL, otherwise fix it */
3721 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3723 /* Set super info and mark the class a complete */
3724 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3725 object_type_node, ctxp->interface_number);
3726 ctxp->interface_number = 0;
3727 CLASS_COMPLETE_P (decl) = 1;
3728 add_superinterfaces (decl, super);
3730 return decl;
3733 /* Anonymous class counter. Will be reset to 1 every time a non
3734 anonymous class gets created. */
3735 static int anonymous_class_counter = 1;
3737 /* Patch anonymous class CLASS, by either extending or implementing
3738 DEP. */
3740 static void
3741 patch_anonymous_class (type_decl, class_decl, wfl)
3742 tree type_decl, class_decl, wfl;
3744 tree class = TREE_TYPE (class_decl);
3745 tree type = TREE_TYPE (type_decl);
3746 tree binfo = TYPE_BINFO (class);
3748 /* If it's an interface, implement it */
3749 if (CLASS_INTERFACE (type_decl))
3751 tree s_binfo;
3752 int length;
3754 if (parser_check_super_interface (type_decl, class_decl, wfl))
3755 return;
3757 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3758 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3759 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3760 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3761 /* And add the interface */
3762 parser_add_interface (class_decl, type_decl, wfl);
3764 /* Otherwise, it's a type we want to extend */
3765 else
3767 if (parser_check_super (type_decl, class_decl, wfl))
3768 return;
3769 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3773 static tree
3774 create_anonymous_class (location, type_name)
3775 int location;
3776 tree type_name;
3778 char buffer [80];
3779 tree super = NULL_TREE, itf = NULL_TREE;
3780 tree id, type_decl, class;
3782 /* The unqualified name of the anonymous class. It's just a number. */
3783 sprintf (buffer, "%d", anonymous_class_counter++);
3784 id = build_wfl_node (get_identifier (buffer));
3785 EXPR_WFL_LINECOL (id) = location;
3787 /* We know about the type to extend/implement. We go ahead */
3788 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3790 /* Create a class which either implements on extends the designated
3791 class. The class bears an innacessible name. */
3792 if (CLASS_INTERFACE (type_decl))
3794 /* It's OK to modify it here. It's been already used and
3795 shouldn't be reused */
3796 ctxp->interface_number = 1;
3797 /* Interfaces should presented as a list of WFLs */
3798 itf = build_tree_list (type_name, NULL_TREE);
3800 else
3801 super = type_name;
3804 class = create_class (ACC_FINAL, id, super, itf);
3806 /* We didn't know anything about the stuff. We register a dependence. */
3807 if (!type_decl)
3808 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3810 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3811 return class;
3814 /* Create a class in pass1 and return its decl. Return class
3815 interface's decl in pass 2. */
3817 static tree
3818 create_class (flags, id, super, interfaces)
3819 int flags;
3820 tree id, super, interfaces;
3822 tree raw_name = EXPR_WFL_NODE (id);
3823 tree class_id, decl;
3824 tree super_decl_type;
3826 class_id = parser_qualified_classname (raw_name);
3827 decl = IDENTIFIER_CLASS_VALUE (class_id);
3828 EXPR_WFL_NODE (id) = class_id;
3830 /* Basic check: scope, redefinition, modifiers */
3831 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3833 PUSH_ERROR ();
3834 return NULL_TREE;
3837 /* Suspend the current parsing context if we're parsing an inner
3838 class or an anonymous class. */
3839 if (CPC_INNER_P ())
3840 java_parser_context_suspend ();
3841 /* Push a new context for (static) initialized upon declaration fields */
3842 java_parser_context_push_initialized_field ();
3844 /* Class modifier check:
3845 - Allowed modifier (already done at that point)
3846 - abstract AND final forbidden
3847 - Public classes defined in the correct file */
3848 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3849 parse_error_context
3850 (id, "Class `%s' can't be declared both abstract and final",
3851 IDENTIFIER_POINTER (raw_name));
3853 /* Create a new decl if DECL is NULL, otherwise fix it */
3854 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3856 /* If SUPER exists, use it, otherwise use Object */
3857 if (super)
3859 /* Can't extend java.lang.Object */
3860 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3862 parse_error_context (id, "Can't extend `java.lang.Object'");
3863 return NULL_TREE;
3866 super_decl_type =
3867 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3869 else if (TREE_TYPE (decl) != object_type_node)
3870 super_decl_type = object_type_node;
3871 /* We're defining java.lang.Object */
3872 else
3873 super_decl_type = NULL_TREE;
3875 /* Set super info and mark the class a complete */
3876 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3877 ctxp->interface_number);
3878 ctxp->interface_number = 0;
3879 CLASS_COMPLETE_P (decl) = 1;
3880 add_superinterfaces (decl, interfaces);
3882 /* Add the private this$<n> field, Replicate final locals still in
3883 scope as private final fields mangled like val$<local_name>.
3884 This doesn't not occur for top level (static) inner classes. */
3885 if (PURE_INNER_CLASS_DECL_P (decl))
3886 add_inner_class_fields (decl, current_function_decl);
3888 /* If doing xref, store the location at which the inherited class
3889 (if any) was seen. */
3890 if (flag_emit_xref && super)
3891 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3893 /* Eventually sets the @deprecated tag flag */
3894 CHECK_DEPRECATED (decl);
3896 /* Reset the anonymous class counter when declaring non inner classes */
3897 if (!INNER_CLASS_DECL_P (decl))
3898 anonymous_class_counter = 1;
3900 return decl;
3903 /* End a class declaration: register the statements used to create
3904 finit$ and <clinit>, pop the current class and resume the prior
3905 parser context if necessary. */
3907 static void
3908 end_class_declaration (resume)
3909 int resume;
3911 /* If an error occured, context weren't pushed and won't need to be
3912 popped by a resume. */
3913 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3915 java_parser_context_pop_initialized_field ();
3916 POP_CPC ();
3917 if (resume && no_error_occured)
3918 java_parser_context_resume ();
3920 /* We're ending a class declaration, this is a good time to reset
3921 the interface cout. Note that might have been already done in
3922 create_interface, but if at that time an inner class was being
3923 dealt with, the interface count was reset in a context created
3924 for the sake of handling inner classes declaration. */
3925 ctxp->interface_number = 0;
3928 static void
3929 add_inner_class_fields (class_decl, fct_decl)
3930 tree class_decl;
3931 tree fct_decl;
3933 tree block, marker, f;
3935 f = add_field (TREE_TYPE (class_decl),
3936 build_current_thisn (TREE_TYPE (class_decl)),
3937 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3938 ACC_PRIVATE);
3939 FIELD_THISN (f) = 1;
3941 if (!fct_decl)
3942 return;
3944 for (block = GET_CURRENT_BLOCK (fct_decl);
3945 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3947 tree decl;
3948 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3950 tree name, pname;
3951 tree wfl, init, list;
3953 /* Avoid non final arguments. */
3954 if (!LOCAL_FINAL (decl))
3955 continue;
3957 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3958 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3959 wfl = build_wfl_node (name);
3960 init = build_wfl_node (pname);
3961 /* Build an initialization for the field: it will be
3962 initialized by a parameter added to finit$, bearing a
3963 mangled name of the field itself (param$<n>.) The
3964 parameter is provided to finit$ by the constructor
3965 invoking it (hence the constructor will also feature a
3966 hidden parameter, set to the value of the outer context
3967 local at the time the inner class is created.)
3969 Note: we take into account all possible locals that can
3970 be accessed by the inner class. It's actually not trivial
3971 to minimize these aliases down to the ones really
3972 used. One way to do that would be to expand all regular
3973 methods first, then finit$ to get a picture of what's
3974 used. It works with the exception that we would have to
3975 go back on all constructor invoked in regular methods to
3976 have their invokation reworked (to include the right amount
3977 of alias initializer parameters.)
3979 The only real way around, I think, is a first pass to
3980 identify locals really used in the inner class. We leave
3981 the flag FIELD_LOCAL_ALIAS_USED around for that future
3982 use.
3984 On the other hand, it only affect local inner classes,
3985 whose constructors (and finit$ call) will be featuring
3986 unecessary arguments. It's easy for a developper to keep
3987 this number of parameter down by using the `final'
3988 keyword only when necessary. For the time being, we can
3989 issue a warning on unecessary finals. FIXME */
3990 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
3991 wfl, init);
3993 /* Register the field. The TREE_LIST holding the part
3994 initialized/initializer will be marked ARG_FINAL_P so
3995 that the created field can be marked
3996 FIELD_LOCAL_ALIAS. */
3997 list = build_tree_list (wfl, init);
3998 ARG_FINAL_P (list) = 1;
3999 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4003 if (!CPC_INITIALIZER_STMT (ctxp))
4004 return;
4006 /* If we ever registered an alias field, insert and marker to
4007 remeber where the list ends. The second part of the list (the one
4008 featuring initialized fields) so it can be later reversed to
4009 enforce 8.5. The marker will be removed during that operation. */
4010 marker = build_tree_list (NULL_TREE, NULL_TREE);
4011 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4012 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4015 /* Can't use lookup_field () since we don't want to load the class and
4016 can't set the CLASS_LOADED_P flag */
4018 static tree
4019 find_field (class, name)
4020 tree class;
4021 tree name;
4023 tree decl;
4024 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4026 if (DECL_NAME (decl) == name)
4027 return decl;
4029 return NULL_TREE;
4032 /* Wrap around lookup_field that doesn't potentially upset the value
4033 of CLASS */
4035 static tree
4036 lookup_field_wrapper (class, name)
4037 tree class, name;
4039 tree type = class;
4040 tree decl = NULL_TREE;
4041 java_parser_context_save_global ();
4043 /* Last chance: if we're within the context of an inner class, we
4044 might be trying to access a local variable defined in an outer
4045 context. We try to look for it now. */
4046 if (INNER_CLASS_TYPE_P (class))
4048 tree new_name;
4049 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4050 decl = lookup_field (&type, new_name);
4051 if (decl && decl != error_mark_node)
4052 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4054 if (!decl || decl == error_mark_node)
4056 type = class;
4057 decl = lookup_field (&type, name);
4060 java_parser_context_restore_global ();
4061 return decl == error_mark_node ? NULL : decl;
4064 /* Find duplicate field within the same class declarations and report
4065 the error. Returns 1 if a duplicated field was found, 0
4066 otherwise. */
4068 static int
4069 duplicate_declaration_error_p (new_field_name, new_type, cl)
4070 tree new_field_name, new_type, cl;
4072 /* This might be modified to work with method decl as well */
4073 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4074 if (decl)
4076 char *t1 = xstrdup (purify_type_name
4077 ((TREE_CODE (new_type) == POINTER_TYPE
4078 && TREE_TYPE (new_type) == NULL_TREE) ?
4079 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4080 lang_printable_name (new_type, 1)));
4081 /* The type may not have been completed by the time we report
4082 the error */
4083 char *t2 = xstrdup (purify_type_name
4084 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4085 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4086 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4087 lang_printable_name (TREE_TYPE (decl), 1)));
4088 parse_error_context
4089 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4090 t1, IDENTIFIER_POINTER (new_field_name),
4091 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4092 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4093 free (t1);
4094 free (t2);
4095 return 1;
4097 return 0;
4100 /* Field registration routine. If TYPE doesn't exist, field
4101 declarations are linked to the undefined TYPE dependency list, to
4102 be later resolved in java_complete_class () */
4104 static void
4105 register_fields (flags, type, variable_list)
4106 int flags;
4107 tree type, variable_list;
4109 tree current, saved_type;
4110 tree class_type = NULL_TREE;
4111 int saved_lineno = lineno;
4112 int must_chain = 0;
4113 tree wfl = NULL_TREE;
4115 if (GET_CPC ())
4116 class_type = TREE_TYPE (GET_CPC ());
4118 if (!class_type || class_type == error_mark_node)
4119 return;
4121 /* If we're adding fields to interfaces, those fields are public,
4122 static, final */
4123 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4125 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4126 flags, ACC_PUBLIC, "interface field(s)");
4127 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4128 flags, ACC_STATIC, "interface field(s)");
4129 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4130 flags, ACC_FINAL, "interface field(s)");
4131 check_modifiers ("Illegal interface member modifier `%s'", flags,
4132 INTERFACE_FIELD_MODIFIERS);
4133 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4136 /* Obtain a suitable type for resolution, if necessary */
4137 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4139 /* If TYPE is fully resolved and we don't have a reference, make one */
4140 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4142 for (current = variable_list, saved_type = type; current;
4143 current = TREE_CHAIN (current), type = saved_type)
4145 tree real_type;
4146 tree field_decl;
4147 tree cl = TREE_PURPOSE (current);
4148 tree init = TREE_VALUE (current);
4149 tree current_name = EXPR_WFL_NODE (cl);
4151 /* Can't declare non-final static fields in inner classes */
4152 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4153 && !(flags & ACC_FINAL))
4154 parse_error_context
4155 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4156 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4157 lang_printable_name (class_type, 0));
4159 /* Process NAME, as it may specify extra dimension(s) for it */
4160 type = build_array_from_name (type, wfl, current_name, &current_name);
4162 /* Type adjustment. We may have just readjusted TYPE because
4163 the variable specified more dimensions. Make sure we have
4164 a reference if we can and don't have one already. Also
4165 change the name if we have an init. */
4166 if (type != saved_type)
4168 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4169 if (init)
4170 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4173 real_type = GET_REAL_TYPE (type);
4174 /* Check for redeclarations */
4175 if (duplicate_declaration_error_p (current_name, real_type, cl))
4176 continue;
4178 /* Set lineno to the line the field was found and create a
4179 declaration for it. Eventually sets the @deprecated tag flag. */
4180 if (flag_emit_xref)
4181 lineno = EXPR_WFL_LINECOL (cl);
4182 else
4183 lineno = EXPR_WFL_LINENO (cl);
4184 field_decl = add_field (class_type, current_name, real_type, flags);
4185 CHECK_DEPRECATED (field_decl);
4187 /* If the couple initializer/initialized is marked ARG_FINAL_P, we
4188 mark the created field FIELD_LOCAL_ALIAS, so that we can
4189 hide parameters to this inner class finit$ and constructors. */
4190 if (ARG_FINAL_P (current))
4191 FIELD_LOCAL_ALIAS (field_decl) = 1;
4193 /* Check if we must chain. */
4194 if (must_chain)
4195 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4197 /* If we have an initialization value tied to the field */
4198 if (init)
4200 /* The field is declared static */
4201 if (flags & ACC_STATIC)
4203 /* We include the field and its initialization part into
4204 a list used to generate <clinit>. After <clinit> is
4205 walked, field initializations will be processed and
4206 fields initialized with known constants will be taken
4207 out of <clinit> and have their DECL_INITIAL set
4208 appropriately. */
4209 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4210 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4211 if (TREE_OPERAND (init, 1)
4212 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4213 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4215 /* A non-static field declared with an immediate initialization is
4216 to be initialized in <init>, if any. This field is remembered
4217 to be processed at the time of the generation of <init>. */
4218 else
4220 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4221 SET_CPC_INITIALIZER_STMT (ctxp, init);
4223 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4224 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4227 lineno = saved_lineno;
4230 /* Generate finit$, using the list of initialized fields to populate
4231 its body. finit$'s parameter(s) list is adjusted to include the
4232 one(s) used to initialized the field(s) caching outer context
4233 local(s). */
4235 static tree
4236 generate_finit (class_type)
4237 tree class_type;
4239 int count = 0;
4240 tree list = TYPE_FINIT_STMT_LIST (class_type);
4241 tree mdecl, current, parms;
4243 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4244 class_type, NULL_TREE,
4245 &count);
4246 CRAFTED_PARAM_LIST_FIXUP (parms);
4247 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4248 finit_identifier_node, parms);
4249 fix_method_argument_names (parms, mdecl);
4250 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4251 mdecl, NULL_TREE);
4252 DECL_FUNCTION_NAP (mdecl) = count;
4253 start_artificial_method_body (mdecl);
4255 for (current = list; current; current = TREE_CHAIN (current))
4256 java_method_add_stmt (mdecl,
4257 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4258 current));
4259 end_artificial_method_body (mdecl);
4260 return mdecl;
4263 static void
4264 add_instance_initializer (mdecl)
4265 tree mdecl;
4267 tree current;
4268 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4269 tree compound = NULL_TREE;
4271 if (stmt_list)
4273 for (current = stmt_list; current; current = TREE_CHAIN (current))
4274 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4276 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4277 NULL_TREE, compound));
4281 /* Shared accros method_declarator and method_header to remember the
4282 patch stage that was reached during the declaration of the method.
4283 A method DECL is built differently is there is no patch
4284 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4285 pending on the currently defined method. */
4287 static int patch_stage;
4289 /* Check the method declaration and add the method to its current
4290 class. If the argument list is known to contain incomplete types,
4291 the method is partially added and the registration will be resume
4292 once the method arguments resolved. If TYPE is NULL, we're dealing
4293 with a constructor. */
4295 static tree
4296 method_header (flags, type, mdecl, throws)
4297 int flags;
4298 tree type, mdecl, throws;
4300 tree type_wfl = NULL_TREE;
4301 tree meth_name = NULL_TREE;
4302 tree current, orig_arg, this_class = NULL;
4303 tree id, meth;
4304 int saved_lineno;
4305 int constructor_ok = 0, must_chain;
4306 int count;
4308 if (mdecl == error_mark_node)
4309 return error_mark_node;
4310 meth = TREE_VALUE (mdecl);
4311 id = TREE_PURPOSE (mdecl);
4313 check_modifiers_consistency (flags);
4315 if (GET_CPC ())
4316 this_class = TREE_TYPE (GET_CPC ());
4318 if (!this_class || this_class == error_mark_node)
4319 return NULL_TREE;
4321 /* There are some forbidden modifiers for an abstract method and its
4322 class must be abstract as well. */
4323 if (type && (flags & ACC_ABSTRACT))
4325 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4326 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4327 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4328 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4329 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4330 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4331 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4332 parse_error_context
4333 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4334 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4335 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4338 /* Things to be checked when declaring a constructor */
4339 if (!type)
4341 int ec = java_error_count;
4342 /* 8.6: Constructor declarations: we might be trying to define a
4343 method without specifying a return type. */
4344 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4345 parse_error_context
4346 (id, "Invalid method declaration, return type required");
4347 /* 8.6.3: Constructor modifiers */
4348 else
4350 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4351 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4352 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4353 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4354 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4356 /* If we found error here, we don't consider it's OK to tread
4357 the method definition as a constructor, for the rest of this
4358 function */
4359 if (ec == java_error_count)
4360 constructor_ok = 1;
4363 /* Method declared within the scope of an interface are implicitly
4364 abstract and public. Conflicts with other erroneously provided
4365 modifiers are checked right after. */
4367 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4369 /* If FLAGS isn't set because of a modifier, turn the
4370 corresponding modifier WFL to NULL so we issue a warning on
4371 the obsolete use of the modifier */
4372 if (!(flags & ACC_PUBLIC))
4373 MODIFIER_WFL (PUBLIC_TK) = NULL;
4374 if (!(flags & ACC_ABSTRACT))
4375 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4376 flags |= ACC_PUBLIC;
4377 flags |= ACC_ABSTRACT;
4380 /* Inner class can't declare static methods */
4381 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4383 parse_error_context
4384 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4385 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4386 lang_printable_name (this_class, 0));
4389 /* Modifiers context reset moved up, so abstract method declaration
4390 modifiers can be later checked. */
4392 /* Set constructor returned type to void and method name to <init>,
4393 unless we found an error identifier the constructor (in which
4394 case we retain the original name) */
4395 if (!type)
4397 type = void_type_node;
4398 if (constructor_ok)
4399 meth_name = init_identifier_node;
4401 else
4402 meth_name = EXPR_WFL_NODE (id);
4404 /* Do the returned type resolution and registration if necessary */
4405 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4407 if (meth_name)
4408 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4409 EXPR_WFL_NODE (id) = meth_name;
4410 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4412 if (must_chain)
4414 patch_stage = JDEP_METHOD_RETURN;
4415 register_incomplete_type (patch_stage, type_wfl, id, type);
4416 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4418 else
4419 TREE_TYPE (meth) = type;
4421 saved_lineno = lineno;
4422 /* When defining an abstract or interface method, the curly
4423 bracket at level 1 doesn't exist because there is no function
4424 body */
4425 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4426 EXPR_WFL_LINENO (id));
4428 /* Remember the original argument list */
4429 orig_arg = TYPE_ARG_TYPES (meth);
4431 if (patch_stage) /* includes ret type and/or all args */
4433 jdep *jdep;
4434 meth = add_method_1 (this_class, flags, meth_name, meth);
4435 /* Patch for the return type */
4436 if (patch_stage == JDEP_METHOD_RETURN)
4438 jdep = CLASSD_LAST (ctxp->classd_list);
4439 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4441 /* This is the stop JDEP. METH allows the function's signature
4442 to be computed. */
4443 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4445 else
4446 meth = add_method (this_class, flags, meth_name,
4447 build_java_signature (meth));
4449 /* Remember final parameters */
4450 MARK_FINAL_PARMS (meth, orig_arg);
4452 /* Fix the method argument list so we have the argument name
4453 information */
4454 fix_method_argument_names (orig_arg, meth);
4456 /* Register the parameter number and re-install the current line
4457 number */
4458 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4459 lineno = saved_lineno;
4461 /* Register exception specified by the `throws' keyword for
4462 resolution and set the method decl appropriate field to the list.
4463 Note: the grammar ensures that what we get here are class
4464 types. */
4465 if (throws)
4467 throws = nreverse (throws);
4468 for (current = throws; current; current = TREE_CHAIN (current))
4470 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4471 NULL_TREE, NULL_TREE);
4472 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4473 &TREE_VALUE (current);
4475 DECL_FUNCTION_THROWS (meth) = throws;
4478 /* We set the DECL_NAME to ID so we can track the location where
4479 the function was declared. This allow us to report
4480 redefinition error accurately. When method are verified,
4481 DECL_NAME is reinstalled properly (using the content of the
4482 WFL node ID) (see check_method_redefinition). We don't do that
4483 when Object is being defined. Constructor <init> names will be
4484 reinstalled the same way. */
4485 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4486 DECL_NAME (meth) = id;
4488 /* Set the flag if we correctly processed a constructor */
4489 if (constructor_ok)
4491 DECL_CONSTRUCTOR_P (meth) = 1;
4492 /* Compute and store the number of artificial parameters declared
4493 for this constructor */
4494 for (count = 0, current = TYPE_FIELDS (this_class); current;
4495 current = TREE_CHAIN (current))
4496 if (FIELD_LOCAL_ALIAS (current))
4497 count++;
4498 DECL_FUNCTION_NAP (meth) = count;
4501 /* Eventually set the @deprecated tag flag */
4502 CHECK_DEPRECATED (meth);
4504 /* If doing xref, store column and line number information instead
4505 of the line number only. */
4506 if (flag_emit_xref)
4507 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4509 return meth;
4512 static void
4513 fix_method_argument_names (orig_arg, meth)
4514 tree orig_arg, meth;
4516 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4517 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4519 TREE_PURPOSE (arg) = this_identifier_node;
4520 arg = TREE_CHAIN (arg);
4522 while (orig_arg != end_params_node)
4524 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4525 orig_arg = TREE_CHAIN (orig_arg);
4526 arg = TREE_CHAIN (arg);
4530 /* Complete the method declaration with METHOD_BODY. */
4532 static void
4533 finish_method_declaration (method_body)
4534 tree method_body;
4536 int flags;
4538 if (!current_function_decl)
4539 return;
4541 flags = get_access_flags_from_decl (current_function_decl);
4543 /* 8.4.5 Method Body */
4544 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4546 tree wfl = DECL_NAME (current_function_decl);
4547 parse_error_context (wfl,
4548 "%s method `%s' can't have a body defined",
4549 (METHOD_NATIVE (current_function_decl) ?
4550 "Native" : "Abstract"),
4551 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4552 method_body = NULL_TREE;
4554 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4556 tree wfl = DECL_NAME (current_function_decl);
4557 parse_error_context
4558 (wfl,
4559 "Non native and non abstract method `%s' must have a body defined",
4560 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4561 method_body = NULL_TREE;
4564 if (flag_emit_class_files && method_body
4565 && TREE_CODE (method_body) == NOP_EXPR
4566 && TREE_TYPE (current_function_decl)
4567 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4568 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4570 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4571 maybe_absorb_scoping_blocks ();
4572 /* Exit function's body */
4573 exit_block ();
4574 /* Merge last line of the function with first line, directly in the
4575 function decl. It will be used to emit correct debug info. */
4576 if (!flag_emit_xref)
4577 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4579 /* Since function's argument's list are shared, reset the
4580 ARG_FINAL_P parameter that might have been set on some of this
4581 function parameters. */
4582 UNMARK_FINAL_PARMS (current_function_decl);
4584 /* So we don't have an irrelevant function declaration context for
4585 the next static block we'll see. */
4586 current_function_decl = NULL_TREE;
4589 /* Build a an error message for constructor circularity errors. */
4591 static char *
4592 constructor_circularity_msg (from, to)
4593 tree from, to;
4595 static char string [4096];
4596 char *t = xstrdup (lang_printable_name (from, 0));
4597 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4598 free (t);
4599 return string;
4602 /* Verify a circular call to METH. Return 1 if an error is found, 0
4603 otherwise. */
4605 static int
4606 verify_constructor_circularity (meth, current)
4607 tree meth, current;
4609 static tree list = NULL_TREE;
4610 static int initialized_p;
4611 tree c;
4613 /* If we haven't already registered LIST with the garbage collector,
4614 do so now. */
4615 if (!initialized_p)
4617 ggc_add_tree_root (&list, 1);
4618 initialized_p = 1;
4621 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4623 if (TREE_VALUE (c) == meth)
4625 char *t;
4626 if (list)
4628 tree liste;
4629 list = nreverse (list);
4630 for (liste = list; liste; liste = TREE_CHAIN (liste))
4632 parse_error_context
4633 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4634 constructor_circularity_msg
4635 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4636 java_error_count--;
4639 t = xstrdup (lang_printable_name (meth, 0));
4640 parse_error_context (TREE_PURPOSE (c),
4641 "%s: recursive invocation of constructor `%s'",
4642 constructor_circularity_msg (current, meth), t);
4643 free (t);
4644 list = NULL_TREE;
4645 return 1;
4648 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4650 list = tree_cons (c, current, list);
4651 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4652 return 1;
4653 list = TREE_CHAIN (list);
4655 return 0;
4658 /* Check modifiers that can be declared but exclusively */
4660 static void
4661 check_modifiers_consistency (flags)
4662 int flags;
4664 int acc_count = 0;
4665 tree cl = NULL_TREE;
4667 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4668 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4669 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4670 if (acc_count > 1)
4671 parse_error_context
4672 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4674 acc_count = 0;
4675 cl = NULL_TREE;
4676 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4677 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4678 if (acc_count > 1)
4679 parse_error_context (cl,
4680 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4683 /* Check the methode header METH for abstract specifics features */
4685 static void
4686 check_abstract_method_header (meth)
4687 tree meth;
4689 int flags = get_access_flags_from_decl (meth);
4690 /* DECL_NAME might still be a WFL node */
4691 tree name = GET_METHOD_NAME (meth);
4693 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4694 ACC_ABSTRACT, "abstract method",
4695 IDENTIFIER_POINTER (name));
4696 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4697 ACC_PUBLIC, "abstract method",
4698 IDENTIFIER_POINTER (name));
4700 check_modifiers ("Illegal modifier `%s' for interface method",
4701 flags, INTERFACE_METHOD_MODIFIERS);
4704 /* Create a FUNCTION_TYPE node and start augmenting it with the
4705 declared function arguments. Arguments type that can't be resolved
4706 are left as they are, but the returned node is marked as containing
4707 incomplete types. */
4709 static tree
4710 method_declarator (id, list)
4711 tree id, list;
4713 tree arg_types = NULL_TREE, current, node;
4714 tree meth = make_node (FUNCTION_TYPE);
4715 jdep *jdep;
4717 patch_stage = JDEP_NO_PATCH;
4719 if (GET_CPC () == error_mark_node)
4720 return error_mark_node;
4722 /* If we're dealing with an inner class constructor, we hide the
4723 this$<n> decl in the name field of its parameter declaration. We
4724 also might have to hide the outer context local alias
4725 initializers. Not done when the class is a toplevel class. */
4726 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4727 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4729 tree aliases_list, type, thisn;
4730 /* First the aliases, linked to the regular parameters */
4731 aliases_list =
4732 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4733 TREE_TYPE (GET_CPC ()),
4734 NULL_TREE, NULL);
4735 list = chainon (nreverse (aliases_list), list);
4737 /* Then this$<n> */
4738 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4739 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4740 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4741 list);
4744 for (current = list; current; current = TREE_CHAIN (current))
4746 int must_chain = 0;
4747 tree wfl_name = TREE_PURPOSE (current);
4748 tree type = TREE_VALUE (current);
4749 tree name = EXPR_WFL_NODE (wfl_name);
4750 tree already, arg_node;
4751 tree type_wfl = NULL_TREE;
4752 tree real_type;
4754 /* Obtain a suitable type for resolution, if necessary */
4755 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4757 /* Process NAME, as it may specify extra dimension(s) for it */
4758 type = build_array_from_name (type, type_wfl, name, &name);
4759 EXPR_WFL_NODE (wfl_name) = name;
4761 real_type = GET_REAL_TYPE (type);
4762 if (TREE_CODE (real_type) == RECORD_TYPE)
4764 real_type = promote_type (real_type);
4765 if (TREE_CODE (type) == TREE_LIST)
4766 TREE_PURPOSE (type) = real_type;
4769 /* Check redefinition */
4770 for (already = arg_types; already; already = TREE_CHAIN (already))
4771 if (TREE_PURPOSE (already) == name)
4773 parse_error_context
4774 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4775 IDENTIFIER_POINTER (name),
4776 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4777 break;
4780 /* If we've an incomplete argument type, we know there is a location
4781 to patch when the type get resolved, later. */
4782 jdep = NULL;
4783 if (must_chain)
4785 patch_stage = JDEP_METHOD;
4786 type = register_incomplete_type (patch_stage,
4787 type_wfl, wfl_name, type);
4788 jdep = CLASSD_LAST (ctxp->classd_list);
4789 JDEP_MISC (jdep) = id;
4792 /* The argument node: a name and a (possibly) incomplete type. */
4793 arg_node = build_tree_list (name, real_type);
4794 /* Remeber arguments declared final. */
4795 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4797 if (jdep)
4798 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4799 TREE_CHAIN (arg_node) = arg_types;
4800 arg_types = arg_node;
4802 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4803 node = build_tree_list (id, meth);
4804 return node;
4807 static int
4808 unresolved_type_p (wfl, returned)
4809 tree wfl;
4810 tree *returned;
4813 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4815 if (returned)
4817 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4818 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4819 *returned = TREE_TYPE (decl);
4820 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4821 *returned = TREE_TYPE (GET_CPC ());
4822 else
4823 *returned = NULL_TREE;
4825 return 1;
4827 if (returned)
4828 *returned = wfl;
4829 return 0;
4832 /* From NAME, build a qualified identifier node using the
4833 qualification from the current package definition. */
4835 static tree
4836 parser_qualified_classname (name)
4837 tree name;
4839 tree nested_class_name;
4841 if ((nested_class_name = maybe_make_nested_class_name (name)))
4842 return nested_class_name;
4844 if (ctxp->package)
4845 return merge_qualified_name (ctxp->package, name);
4846 else
4847 return name;
4850 /* Called once the type a interface extends is resolved. Returns 0 if
4851 everything is OK. */
4853 static int
4854 parser_check_super_interface (super_decl, this_decl, this_wfl)
4855 tree super_decl, this_decl, this_wfl;
4857 tree super_type = TREE_TYPE (super_decl);
4859 /* Has to be an interface */
4860 if (!CLASS_INTERFACE (super_decl))
4862 parse_error_context
4863 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4864 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4865 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4866 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4867 "interface" : "class"),
4868 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4869 return 1;
4872 /* Check scope: same package OK, other package: OK if public */
4873 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4874 return 1;
4876 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4877 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4878 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4879 return 0;
4882 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4883 0 if everthing is OK. */
4885 static int
4886 parser_check_super (super_decl, this_decl, wfl)
4887 tree super_decl, this_decl, wfl;
4889 tree super_type = TREE_TYPE (super_decl);
4891 /* SUPER should be a CLASS (neither an array nor an interface) */
4892 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4894 parse_error_context
4895 (wfl, "Class `%s' can't subclass %s `%s'",
4896 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4897 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4898 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4899 return 1;
4902 if (CLASS_FINAL (TYPE_NAME (super_type)))
4904 parse_error_context (wfl, "Can't subclass final classes: %s",
4905 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4906 return 1;
4909 /* Check scope: same package OK, other package: OK if public */
4910 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4911 return 1;
4913 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4914 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4915 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4916 return 0;
4919 /* Create a new dependency list and link it (in a LIFO manner) to the
4920 CTXP list of type dependency list. */
4922 static void
4923 create_jdep_list (ctxp)
4924 struct parser_ctxt *ctxp;
4926 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
4927 new->first = new->last = NULL;
4928 new->next = ctxp->classd_list;
4929 ctxp->classd_list = new;
4932 static jdeplist *
4933 reverse_jdep_list (ctxp)
4934 struct parser_ctxt *ctxp;
4936 register jdeplist *prev = NULL, *current, *next;
4937 for (current = ctxp->classd_list; current; current = next)
4939 next = current->next;
4940 current->next = prev;
4941 prev = current;
4943 return prev;
4946 /* Create a fake pointer based on the ID stored in
4947 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4948 registered again. */
4950 static tree
4951 obtain_incomplete_type (type_name)
4952 tree type_name;
4954 tree ptr, name;
4956 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4957 name = EXPR_WFL_NODE (type_name);
4958 else if (INCOMPLETE_TYPE_P (type_name))
4959 name = TYPE_NAME (type_name);
4960 else
4961 fatal ("invalid type name - obtain_incomplete_type");
4963 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
4964 if (TYPE_NAME (ptr) == name)
4965 break;
4967 if (!ptr)
4969 BUILD_PTR_FROM_NAME (ptr, name);
4970 layout_type (ptr);
4971 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4972 ctxp->incomplete_class = ptr;
4975 return ptr;
4978 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4979 non NULL instead of computing a new fake type based on WFL. The new
4980 dependency is inserted in the current type dependency list, in FIFO
4981 manner. */
4983 static tree
4984 register_incomplete_type (kind, wfl, decl, ptr)
4985 int kind;
4986 tree wfl, decl, ptr;
4988 jdep *new = (jdep *)xmalloc (sizeof (jdep));
4990 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4991 ptr = obtain_incomplete_type (wfl);
4993 JDEP_KIND (new) = kind;
4994 JDEP_DECL (new) = decl;
4995 JDEP_SOLV (new) = ptr;
4996 JDEP_WFL (new) = wfl;
4997 JDEP_CHAIN (new) = NULL;
4998 JDEP_MISC (new) = NULL_TREE;
4999 /* For some dependencies, set the enclosing class of the current
5000 class to be the enclosing context */
5001 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
5002 && GET_ENCLOSING_CPC ())
5003 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5004 else
5005 JDEP_ENCLOSING (new) = GET_CPC ();
5006 JDEP_GET_PATCH (new) = (tree *)NULL;
5008 JDEP_INSERT (ctxp->classd_list, new);
5010 return ptr;
5013 void
5014 java_check_circular_reference ()
5016 tree current;
5017 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5019 tree type = TREE_TYPE (current);
5020 if (CLASS_INTERFACE (current))
5022 /* Check all interfaces this class extends */
5023 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5024 int n, i;
5026 if (!basetype_vec)
5027 return;
5028 n = TREE_VEC_LENGTH (basetype_vec);
5029 for (i = 0; i < n; i++)
5031 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5032 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5033 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5034 parse_error_context (lookup_cl (current),
5035 "Cyclic interface inheritance");
5038 else
5039 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5040 parse_error_context (lookup_cl (current),
5041 "Cyclic class inheritance%s",
5042 (cyclic_inheritance_report ?
5043 cyclic_inheritance_report : ""));
5047 /* Augment the parameter list PARM with parameters crafted to
5048 initialize outer context locals aliases. Through ARTIFICIAL, a
5049 count is kept of the number of crafted parameters. MODE governs
5050 what eventually gets created: something suitable for a function
5051 creation or a function invocation, either the constructor or
5052 finit$. */
5054 static tree
5055 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5056 int mode;
5057 tree class_type, parm;
5058 int *artificial;
5060 tree field;
5061 tree additional_parms = NULL_TREE;
5063 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5064 if (FIELD_LOCAL_ALIAS (field))
5066 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5067 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5068 tree mangled_id;
5070 switch (mode)
5072 case AIPL_FUNCTION_DECLARATION:
5073 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5074 &buffer [4]);
5075 purpose = build_wfl_node (mangled_id);
5076 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5077 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5078 else
5079 value = TREE_TYPE (field);
5080 break;
5082 case AIPL_FUNCTION_CREATION:
5083 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5084 &buffer [4]);
5085 value = TREE_TYPE (field);
5086 break;
5088 case AIPL_FUNCTION_FINIT_INVOCATION:
5089 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5090 &buffer [4]);
5091 /* Now, this is wrong. purpose should always be the NAME
5092 of something and value its matching value (decl, type,
5093 etc...) FIXME -- but there is a lot to fix. */
5095 /* When invoked for this kind of operation, we already
5096 know whether a field is used or not. */
5097 purpose = TREE_TYPE (field);
5098 value = build_wfl_node (mangled_id);
5099 break;
5101 case AIPL_FUNCTION_CTOR_INVOCATION:
5102 /* There are two case: the constructor invokation happends
5103 outside the local inner, in which case, locales from the outer
5104 context are directly used.
5106 Otherwise, we fold to using the alias directly. */
5107 if (class_type == current_class)
5108 value = field;
5109 else
5111 name = get_identifier (&buffer[4]);
5112 value = IDENTIFIER_LOCAL_VALUE (name);
5114 break;
5116 additional_parms = tree_cons (purpose, value, additional_parms);
5117 if (artificial)
5118 *artificial +=1;
5120 if (additional_parms)
5122 if (ANONYMOUS_CLASS_P (class_type)
5123 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5124 additional_parms = nreverse (additional_parms);
5125 parm = chainon (additional_parms, parm);
5128 return parm;
5131 /* Craft a constructor for CLASS_DECL -- what we should do when none
5132 where found. ARGS is non NULL when a special signature must be
5133 enforced. This is the case for anonymous classes. */
5135 static void
5136 craft_constructor (class_decl, args)
5137 tree class_decl, args;
5139 tree class_type = TREE_TYPE (class_decl);
5140 tree parm = NULL_TREE;
5141 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5142 ACC_PUBLIC : 0);
5143 int i = 0, artificial = 0;
5144 tree decl, ctor_name;
5145 char buffer [80];
5147 /* The constructor name is <init> unless we're dealing with an
5148 anonymous class, in which case the name will be fixed after having
5149 be expanded. */
5150 if (ANONYMOUS_CLASS_P (class_type))
5151 ctor_name = DECL_NAME (class_decl);
5152 else
5153 ctor_name = init_identifier_node;
5155 /* If we're dealing with an inner class constructor, we hide the
5156 this$<n> decl in the name field of its parameter declaration. */
5157 if (PURE_INNER_CLASS_TYPE_P (class_type))
5159 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5160 parm = tree_cons (build_current_thisn (class_type),
5161 build_pointer_type (type), parm);
5163 /* Some more arguments to be hidden here. The values of the local
5164 variables of the outer context that the inner class needs to see. */
5165 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5166 class_type, parm,
5167 &artificial);
5170 /* Then if there are any args to be enforced, enforce them now */
5171 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5173 sprintf (buffer, "parm%d", i++);
5174 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5177 CRAFTED_PARAM_LIST_FIXUP (parm);
5178 decl = create_artificial_method (class_type, flags, void_type_node,
5179 ctor_name, parm);
5180 fix_method_argument_names (parm, decl);
5181 /* Now, mark the artificial parameters. */
5182 DECL_FUNCTION_NAP (decl) = artificial;
5183 DECL_CONSTRUCTOR_P (decl) = 1;
5187 /* Fix the constructors. This will be called right after circular
5188 references have been checked. It is necessary to fix constructors
5189 early even if no code generation will take place for that class:
5190 some generated constructor might be required by the class whose
5191 compilation triggered this one to be simply loaded. */
5193 void
5194 java_fix_constructors ()
5196 tree current;
5198 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5200 tree class_type = TREE_TYPE (current);
5201 int saw_ctor = 0;
5202 tree decl;
5204 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5205 continue;
5207 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5209 if (DECL_CONSTRUCTOR_P (decl))
5211 fix_constructors (decl);
5212 saw_ctor = 1;
5216 /* Anonymous class constructor can't be generated that early. */
5217 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5218 craft_constructor (current, NULL_TREE);
5222 /* safe_layout_class just makes sure that we can load a class without
5223 disrupting the current_class, input_file, lineno, etc, information
5224 about the class processed currently. */
5226 void
5227 safe_layout_class (class)
5228 tree class;
5230 tree save_current_class = current_class;
5231 const char *save_input_filename = input_filename;
5232 int save_lineno = lineno;
5234 layout_class (class);
5236 current_class = save_current_class;
5237 input_filename = save_input_filename;
5238 lineno = save_lineno;
5239 CLASS_LOADED_P (class) = 1;
5242 static tree
5243 jdep_resolve_class (dep)
5244 jdep *dep;
5246 tree decl;
5248 if (JDEP_RESOLVED_P (dep))
5249 decl = JDEP_RESOLVED_DECL (dep);
5250 else
5252 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5253 JDEP_DECL (dep), JDEP_WFL (dep));
5254 JDEP_RESOLVED (dep, decl);
5257 if (!decl)
5258 complete_class_report_errors (dep);
5259 else if (PURE_INNER_CLASS_DECL_P (decl))
5260 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5261 return decl;
5264 /* Complete unsatisfied class declaration and their dependencies */
5266 void
5267 java_complete_class ()
5269 tree cclass;
5270 jdeplist *cclassd;
5271 int error_found;
5272 tree type;
5274 /* Process imports */
5275 process_imports ();
5277 /* Rever things so we have the right order */
5278 ctxp->class_list = nreverse (ctxp->class_list);
5279 ctxp->classd_list = reverse_jdep_list (ctxp);
5281 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5282 cclass && cclassd;
5283 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5285 jdep *dep;
5286 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5288 tree decl;
5289 if (!(decl = jdep_resolve_class (dep)))
5290 continue;
5292 /* Now it's time to patch */
5293 switch (JDEP_KIND (dep))
5295 case JDEP_SUPER:
5296 /* Simply patch super */
5297 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5298 continue;
5299 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5300 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5301 break;
5303 case JDEP_FIELD:
5305 /* We do part of the job done in add_field */
5306 tree field_decl = JDEP_DECL (dep);
5307 tree field_type = TREE_TYPE (decl);
5308 if (TREE_CODE (field_type) == RECORD_TYPE)
5309 field_type = promote_type (field_type);
5310 TREE_TYPE (field_decl) = field_type;
5311 DECL_ALIGN (field_decl) = 0;
5312 DECL_USER_ALIGN (field_decl) = 0;
5313 layout_decl (field_decl, 0);
5314 SOURCE_FRONTEND_DEBUG
5315 (("Completed field/var decl `%s' with `%s'",
5316 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5317 IDENTIFIER_POINTER (DECL_NAME (decl))));
5318 break;
5320 case JDEP_METHOD: /* We start patching a method */
5321 case JDEP_METHOD_RETURN:
5322 error_found = 0;
5323 while (1)
5325 if (decl)
5327 type = TREE_TYPE(decl);
5328 if (TREE_CODE (type) == RECORD_TYPE)
5329 type = promote_type (type);
5330 JDEP_APPLY_PATCH (dep, type);
5331 SOURCE_FRONTEND_DEBUG
5332 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5333 "Completing fct `%s' with ret type `%s'":
5334 "Completing arg `%s' with type `%s'"),
5335 IDENTIFIER_POINTER (EXPR_WFL_NODE
5336 (JDEP_DECL_WFL (dep))),
5337 IDENTIFIER_POINTER (DECL_NAME (decl))));
5339 else
5340 error_found = 1;
5341 dep = JDEP_CHAIN (dep);
5342 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5343 break;
5344 else
5345 decl = jdep_resolve_class (dep);
5347 if (!error_found)
5349 tree mdecl = JDEP_DECL (dep), signature;
5350 /* Recompute and reset the signature, check first that
5351 all types are now defined. If they're not,
5352 dont build the signature. */
5353 if (check_method_types_complete (mdecl))
5355 signature = build_java_signature (TREE_TYPE (mdecl));
5356 set_java_signature (TREE_TYPE (mdecl), signature);
5359 else
5360 continue;
5361 break;
5363 case JDEP_INTERFACE:
5364 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5365 JDEP_WFL (dep)))
5366 continue;
5367 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5368 break;
5370 case JDEP_PARM:
5371 case JDEP_VARIABLE:
5372 type = TREE_TYPE(decl);
5373 if (TREE_CODE (type) == RECORD_TYPE)
5374 type = promote_type (type);
5375 JDEP_APPLY_PATCH (dep, type);
5376 break;
5378 case JDEP_TYPE:
5379 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5380 SOURCE_FRONTEND_DEBUG
5381 (("Completing a random type dependency on a '%s' node",
5382 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5383 break;
5385 case JDEP_EXCEPTION:
5386 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5387 SOURCE_FRONTEND_DEBUG
5388 (("Completing `%s' `throws' argument node",
5389 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5390 break;
5392 case JDEP_ANONYMOUS:
5393 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5394 break;
5396 default:
5397 fatal ("Can't handle patch code %d - java_complete_class",
5398 JDEP_KIND (dep));
5402 return;
5405 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5406 array. */
5408 static tree
5409 resolve_class (enclosing, class_type, decl, cl)
5410 tree enclosing, class_type, decl, cl;
5412 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5413 const char *base = name;
5414 tree resolved_type = TREE_TYPE (class_type);
5415 tree resolved_type_decl;
5417 if (resolved_type != NULL_TREE)
5419 tree resolved_type_decl = TYPE_NAME (resolved_type);
5420 if (resolved_type_decl == NULL_TREE
5421 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5423 resolved_type_decl = build_decl (TYPE_DECL,
5424 TYPE_NAME (class_type),
5425 resolved_type);
5427 return resolved_type_decl;
5430 /* 1- Check to see if we have an array. If true, find what we really
5431 want to resolve */
5432 while (name[0] == '[')
5433 name++;
5434 if (base != name)
5436 TYPE_NAME (class_type) = get_identifier (name);
5437 WFL_STRIP_BRACKET (cl, cl);
5440 /* 2- Resolve the bare type */
5441 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5442 decl, cl)))
5443 return NULL_TREE;
5444 resolved_type = TREE_TYPE (resolved_type_decl);
5446 /* 3- If we have and array, reconstruct the array down to its nesting */
5447 if (base != name)
5449 while (base != name)
5451 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5452 resolved_type = promote_type (resolved_type);
5453 resolved_type = build_java_array_type (resolved_type, -1);
5454 CLASS_LOADED_P (resolved_type) = 1;
5455 name--;
5457 /* Build a fake decl for this, since this is what is expected to
5458 be returned. */
5459 resolved_type_decl =
5460 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5461 /* Figure how those two things are important for error report. FIXME */
5462 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5463 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
5464 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
5466 TREE_TYPE (class_type) = resolved_type;
5467 return resolved_type_decl;
5470 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5471 are used to report error messages. */
5473 tree
5474 do_resolve_class (enclosing, class_type, decl, cl)
5475 tree enclosing, class_type, decl, cl;
5477 tree new_class_decl;
5479 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5480 it is changed by find_in_imports{_on_demand} and (but it doesn't
5481 really matter) qualify_and_find */
5483 /* 0- Search in the current class as an inner class */
5485 /* Maybe some code here should be added to load the class or
5486 something, at least if the class isn't an inner class and ended
5487 being loaded from class file. FIXME. */
5488 while (enclosing)
5490 tree name;
5492 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5493 return new_class_decl;
5495 /* Explore enclosing contexts. */
5496 while (INNER_CLASS_DECL_P (enclosing))
5498 enclosing = DECL_CONTEXT (enclosing);
5499 if ((new_class_decl = find_as_inner_class (enclosing,
5500 class_type, cl)))
5501 return new_class_decl;
5504 /* Now go to the upper classes, bail out if necessary. */
5505 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5506 if (!enclosing || enclosing == object_type_node)
5507 break;
5509 if (TREE_CODE (enclosing) == RECORD_TYPE)
5511 enclosing = TYPE_NAME (enclosing);
5512 continue;
5515 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
5516 BUILD_PTR_FROM_NAME (name, enclosing);
5517 else
5518 name = enclosing;
5519 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5522 /* 1- Check for the type in single imports. This will change
5523 TYPE_NAME() if something relevant is found */
5524 find_in_imports (class_type);
5526 /* 2- And check for the type in the current compilation unit */
5527 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5529 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5530 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5531 load_class (TYPE_NAME (class_type), 0);
5532 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5535 /* 3- Search according to the current package definition */
5536 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5538 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5539 TYPE_NAME (class_type))))
5540 return new_class_decl;
5543 /* 4- Check the import on demands. Don't allow bar.baz to be
5544 imported from foo.* */
5545 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5546 if (find_in_imports_on_demand (class_type))
5547 return NULL_TREE;
5549 /* If found in find_in_imports_on_demant, the type has already been
5550 loaded. */
5551 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5552 return new_class_decl;
5554 /* 5- Try with a name qualified with the package name we've seen so far */
5555 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5557 tree package;
5559 /* If there is a current package (ctxp->package), it's the first
5560 element of package_list and we can skip it. */
5561 for (package = (ctxp->package ?
5562 TREE_CHAIN (package_list) : package_list);
5563 package; package = TREE_CHAIN (package))
5564 if ((new_class_decl = qualify_and_find (class_type,
5565 TREE_PURPOSE (package),
5566 TYPE_NAME (class_type))))
5567 return new_class_decl;
5570 /* 5- Check an other compilation unit that bears the name of type */
5571 load_class (TYPE_NAME (class_type), 0);
5572 if (check_pkg_class_access (TYPE_NAME (class_type),
5573 (cl ? cl : lookup_cl (decl))))
5574 return NULL_TREE;
5576 /* 6- Last call for a resolution */
5577 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5580 static tree
5581 qualify_and_find (class_type, package, name)
5582 tree class_type, package, name;
5584 tree new_qualified = merge_qualified_name (package, name);
5585 tree new_class_decl;
5587 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5588 load_class (new_qualified, 0);
5589 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5591 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5592 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5593 load_class (new_qualified, 0);
5594 TYPE_NAME (class_type) = new_qualified;
5595 return IDENTIFIER_CLASS_VALUE (new_qualified);
5597 return NULL_TREE;
5600 /* Resolve NAME and lay it out (if not done and if not the current
5601 parsed class). Return a decl node. This function is meant to be
5602 called when type resolution is necessary during the walk pass. */
5604 static tree
5605 resolve_and_layout (something, cl)
5606 tree something;
5607 tree cl;
5609 tree decl, decl_type;
5611 /* Don't do that on the current class */
5612 if (something == current_class)
5613 return TYPE_NAME (current_class);
5615 /* Don't do anything for void and other primitive types */
5616 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5617 return NULL_TREE;
5619 /* Pointer types can be reall pointer types or fake pointers. When
5620 finding a real pointer, recheck for primitive types */
5621 if (TREE_CODE (something) == POINTER_TYPE)
5623 if (TREE_TYPE (something))
5625 something = TREE_TYPE (something);
5626 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5627 return NULL_TREE;
5629 else
5630 something = TYPE_NAME (something);
5633 /* Don't do anything for arrays of primitive types */
5634 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5635 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5636 return NULL_TREE;
5638 /* Something might be a WFL */
5639 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5640 something = EXPR_WFL_NODE (something);
5642 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5643 TYPE_DECL or a real TYPE */
5644 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5645 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5646 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5648 if (!(decl = resolve_no_layout (something, cl)))
5649 return NULL_TREE;
5651 /* Resolve and layout if necessary */
5652 decl_type = TREE_TYPE (decl);
5653 layout_class_methods (decl_type);
5654 /* Check methods */
5655 if (CLASS_FROM_SOURCE_P (decl_type))
5656 java_check_methods (decl);
5657 /* Layout the type if necessary */
5658 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5659 safe_layout_class (decl_type);
5661 return decl;
5664 /* Resolve a class, returns its decl but doesn't perform any
5665 layout. The current parsing context is saved and restored */
5667 static tree
5668 resolve_no_layout (name, cl)
5669 tree name, cl;
5671 tree ptr, decl;
5672 BUILD_PTR_FROM_NAME (ptr, name);
5673 java_parser_context_save_global ();
5674 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5675 java_parser_context_restore_global ();
5677 return decl;
5680 /* Called when reporting errors. Skip leader '[' in a complex array
5681 type description that failed to be resolved. */
5683 static const char *
5684 purify_type_name (name)
5685 const char *name;
5687 while (*name && *name == '[')
5688 name++;
5689 return name;
5692 /* The type CURRENT refers to can't be found. We print error messages. */
5694 static void
5695 complete_class_report_errors (dep)
5696 jdep *dep;
5698 const char *name;
5700 if (!JDEP_WFL (dep))
5701 return;
5703 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5704 switch (JDEP_KIND (dep))
5706 case JDEP_SUPER:
5707 parse_error_context
5708 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5709 purify_type_name (name),
5710 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5711 break;
5712 case JDEP_FIELD:
5713 parse_error_context
5714 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5715 purify_type_name (name),
5716 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5717 break;
5718 case JDEP_METHOD: /* Covers arguments */
5719 parse_error_context
5720 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5721 purify_type_name (name),
5722 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5723 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5724 break;
5725 case JDEP_METHOD_RETURN: /* Covers return type */
5726 parse_error_context
5727 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5728 purify_type_name (name),
5729 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5730 break;
5731 case JDEP_INTERFACE:
5732 parse_error_context
5733 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5734 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5735 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5736 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5737 break;
5738 case JDEP_VARIABLE:
5739 parse_error_context
5740 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5741 purify_type_name (IDENTIFIER_POINTER
5742 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5743 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5744 break;
5745 case JDEP_EXCEPTION: /* As specified by `throws' */
5746 parse_error_context
5747 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5748 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5749 break;
5750 default:
5751 /* Fix for -Wall. Just break doing nothing. The error will be
5752 caught later */
5753 break;
5757 /* Return a static string containing the DECL prototype string. If
5758 DECL is a constructor, use the class name instead of the form
5759 <init> */
5761 static const char *
5762 get_printable_method_name (decl)
5763 tree decl;
5765 const char *to_return;
5766 tree name = NULL_TREE;
5768 if (DECL_CONSTRUCTOR_P (decl))
5770 name = DECL_NAME (decl);
5771 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
5774 to_return = lang_printable_name (decl, 0);
5775 if (DECL_CONSTRUCTOR_P (decl))
5776 DECL_NAME (decl) = name;
5778 return to_return;
5781 /* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5782 nevertheless needs to be verfied, 1 otherwise. */
5784 static int
5785 reset_method_name (method)
5786 tree method;
5788 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5790 /* NAME is just the plain name when Object is being defined */
5791 if (DECL_CONTEXT (method) != object_type_node)
5792 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5793 init_identifier_node : GET_METHOD_NAME (method));
5794 return 0;
5796 else
5797 return 1;
5800 /* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5802 tree
5803 java_get_real_method_name (method_decl)
5804 tree method_decl;
5806 tree method_name = DECL_NAME (method_decl);
5807 if (DECL_CONSTRUCTOR_P (method_decl))
5808 return init_identifier_node;
5810 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5811 and still can be a constructor. FIXME */
5813 /* Don't confuse method only bearing the name of their class as
5814 constructors */
5815 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5816 && ctxp
5817 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
5818 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5819 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
5820 return init_identifier_node;
5821 else
5822 return EXPR_WFL_NODE (method_name);
5825 /* Track method being redefined inside the same class. As a side
5826 effect, set DECL_NAME to an IDENTIFIER (prior entering this
5827 function it's a FWL, so we can track errors more accurately.) */
5829 static int
5830 check_method_redefinition (class, method)
5831 tree class, method;
5833 tree redef, name;
5834 tree cl = DECL_NAME (method);
5835 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
5836 /* decl name of artificial <clinit> and finit$ doesn't need to be
5837 fixed and checked */
5839 /* Reset the method name before running the check. If it returns 1,
5840 the method doesn't need to be verified with respect to method
5841 redeclaration and we return 0 */
5842 if (reset_method_name (method))
5843 return 0;
5845 name = DECL_NAME (method);
5846 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5848 if (redef == method)
5849 break;
5850 if (DECL_NAME (redef) == name
5851 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
5853 parse_error_context
5854 (cl, "Duplicate %s declaration `%s'",
5855 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5856 get_printable_method_name (redef));
5857 return 1;
5860 return 0;
5863 /* Return 1 if check went ok, 0 otherwise. */
5864 static int
5865 check_abstract_method_definitions (do_interface, class_decl, type)
5866 int do_interface;
5867 tree class_decl, type;
5869 tree class = TREE_TYPE (class_decl);
5870 tree method, end_type;
5871 int ok = 1;
5873 end_type = (do_interface ? object_type_node : type);
5874 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5876 tree other_super, other_method, method_sig, method_name;
5877 int found = 0;
5878 int end_type_reached = 0;
5880 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5881 continue;
5883 /* Now verify that somewhere in between TYPE and CLASS,
5884 abstract method METHOD gets a non abstract definition
5885 that is inherited by CLASS. */
5887 method_sig = build_java_signature (TREE_TYPE (method));
5888 method_name = DECL_NAME (method);
5889 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5890 method_name = EXPR_WFL_NODE (method_name);
5892 other_super = class;
5893 do {
5894 if (other_super == end_type)
5895 end_type_reached = 1;
5897 /* Method search */
5898 for (other_method = TYPE_METHODS (other_super); other_method;
5899 other_method = TREE_CHAIN (other_method))
5901 tree s = build_java_signature (TREE_TYPE (other_method));
5902 tree other_name = DECL_NAME (other_method);
5904 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5905 other_name = EXPR_WFL_NODE (other_name);
5906 if (!DECL_CLINIT_P (other_method)
5907 && !DECL_CONSTRUCTOR_P (other_method)
5908 && method_name == other_name
5909 && method_sig == s
5910 && !METHOD_ABSTRACT (other_method))
5912 found = 1;
5913 break;
5916 other_super = CLASSTYPE_SUPER (other_super);
5917 } while (!end_type_reached);
5919 /* Report that abstract METHOD didn't find an implementation
5920 that CLASS can use. */
5921 if (!found)
5923 char *t = xstrdup (lang_printable_name
5924 (TREE_TYPE (TREE_TYPE (method)), 0));
5925 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5926 tree saved_wfl = NULL_TREE;
5928 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5930 saved_wfl = DECL_NAME (method);
5931 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5934 parse_error_context
5935 (lookup_cl (class_decl),
5936 "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",
5937 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5938 t, lang_printable_name (method, 0),
5939 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5940 "interface" : "class"),
5941 IDENTIFIER_POINTER (ccn),
5942 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5943 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5944 ok = 0;
5945 free (t);
5947 if (saved_wfl)
5948 DECL_NAME (method) = saved_wfl;
5952 if (ok && do_interface)
5954 /* Check for implemented interfaces. */
5955 int i;
5956 tree vector = TYPE_BINFO_BASETYPES (type);
5957 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5959 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5960 ok = check_abstract_method_definitions (1, class_decl, super);
5964 return ok;
5967 /* Check that CLASS_DECL somehow implements all inherited abstract
5968 methods. */
5970 static void
5971 java_check_abstract_method_definitions (class_decl)
5972 tree class_decl;
5974 tree class = TREE_TYPE (class_decl);
5975 tree super, vector;
5976 int i;
5978 if (CLASS_ABSTRACT (class_decl))
5979 return;
5981 /* Check for inherited types */
5982 super = class;
5983 do {
5984 super = CLASSTYPE_SUPER (super);
5985 check_abstract_method_definitions (0, class_decl, super);
5986 } while (super != object_type_node);
5988 /* Check for implemented interfaces. */
5989 vector = TYPE_BINFO_BASETYPES (class);
5990 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5992 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5993 check_abstract_method_definitions (1, class_decl, super);
5997 /* Check all the types method DECL uses and return 1 if all of them
5998 are now complete, 0 otherwise. This is used to check whether its
5999 safe to build a method signature or not. */
6001 static int
6002 check_method_types_complete (decl)
6003 tree decl;
6005 tree type = TREE_TYPE (decl);
6006 tree args;
6008 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6009 return 0;
6011 args = TYPE_ARG_TYPES (type);
6012 if (TREE_CODE (type) == METHOD_TYPE)
6013 args = TREE_CHAIN (args);
6014 for (; args != end_params_node; args = TREE_CHAIN (args))
6015 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6016 return 0;
6018 return 1;
6021 /* Visible interface to check methods contained in CLASS_DECL */
6023 void
6024 java_check_methods (class_decl)
6025 tree class_decl;
6027 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6028 return;
6030 if (CLASS_INTERFACE (class_decl))
6031 java_check_abstract_methods (class_decl);
6032 else
6033 java_check_regular_methods (class_decl);
6035 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6038 /* Check all the methods of CLASS_DECL. Methods are first completed
6039 then checked according to regular method existance rules. If no
6040 constructor for CLASS_DECL were encountered, then build its
6041 declaration. */
6043 static void
6044 java_check_regular_methods (class_decl)
6045 tree class_decl;
6047 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6048 tree method;
6049 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6050 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
6051 tree mthrows;
6053 /* It is not necessary to check methods defined in java.lang.Object */
6054 if (class == object_type_node)
6055 return;
6057 if (!TYPE_NVIRTUALS (class))
6058 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6060 /* Should take interfaces into account. FIXME */
6061 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6063 tree sig;
6064 tree method_wfl = DECL_NAME (method);
6065 int aflags;
6067 /* If we previously found something and its name was saved,
6068 reinstall it now */
6069 if (found && saved_found_wfl)
6071 DECL_NAME (found) = saved_found_wfl;
6072 saved_found_wfl = NULL_TREE;
6075 /* Check for redefinitions */
6076 if (check_method_redefinition (class, method))
6077 continue;
6079 /* If we see one constructor a mark so we don't generate the
6080 default one. Also skip other verifications: constructors
6081 can't be inherited hence hiden or overriden */
6082 if (DECL_CONSTRUCTOR_P (method))
6084 saw_constructor = 1;
6085 continue;
6088 /* We verify things thrown by the method. They must inherits from
6089 java.lang.Throwable */
6090 for (mthrows = DECL_FUNCTION_THROWS (method);
6091 mthrows; mthrows = TREE_CHAIN (mthrows))
6093 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6094 parse_error_context
6095 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6096 IDENTIFIER_POINTER
6097 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6100 sig = build_java_argument_signature (TREE_TYPE (method));
6101 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6103 /* Inner class can't declare static methods */
6104 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6106 char *t = xstrdup (lang_printable_name (class, 0));
6107 parse_error_context
6108 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6109 lang_printable_name (method, 0), t);
6110 free (t);
6113 /* Nothing overrides or it's a private method. */
6114 if (!found)
6115 continue;
6116 if (METHOD_PRIVATE (found))
6118 found = NULL_TREE;
6119 continue;
6122 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6123 We set it temporarily for the sake of the error report. */
6124 saved_found_wfl = DECL_NAME (found);
6125 reset_method_name (found);
6127 /* If `found' is declared in an interface, make sure the
6128 modifier matches. */
6129 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6130 && clinit_identifier_node != DECL_NAME (found)
6131 && !METHOD_PUBLIC (method))
6133 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6134 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6135 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6136 lang_printable_name (method, 0),
6137 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6140 /* Can't override a method with the same name and different return
6141 types. */
6142 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6144 char *t = xstrdup
6145 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6146 parse_error_context
6147 (method_wfl,
6148 "Method `%s' was defined with return type `%s' in class `%s'",
6149 lang_printable_name (found, 0), t,
6150 IDENTIFIER_POINTER
6151 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6152 free (t);
6155 aflags = get_access_flags_from_decl (found);
6156 /* If the method has default, access in an other package, then
6157 issue a warning that the current method doesn't override the
6158 one that was found elsewhere. Do not issue this warning when
6159 the match was found in java.lang.Object. */
6160 if (DECL_CONTEXT (found) != object_type_node
6161 && ((aflags & ACC_VISIBILITY) == 0)
6162 && !class_in_current_package (DECL_CONTEXT (found))
6163 && !DECL_CLINIT_P (found)
6164 && flag_not_overriding)
6166 parse_warning_context
6167 (method_wfl, "Method `%s' in class `%s' does not override the corresponding method in class `%s', which is private to a different package",
6168 lang_printable_name (found, 0),
6169 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6170 IDENTIFIER_POINTER (DECL_NAME
6171 (TYPE_NAME (DECL_CONTEXT (found)))));
6172 continue;
6175 /* Can't override final. Can't override static. */
6176 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6178 /* Static *can* override static */
6179 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6180 continue;
6181 parse_error_context
6182 (method_wfl,
6183 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6184 (METHOD_FINAL (found) ? "Final" : "Static"),
6185 lang_printable_name (found, 0),
6186 (METHOD_FINAL (found) ? "final" : "static"),
6187 IDENTIFIER_POINTER
6188 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6189 continue;
6192 /* Static method can't override instance method. */
6193 if (METHOD_STATIC (method))
6195 parse_error_context
6196 (method_wfl,
6197 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6198 lang_printable_name (found, 0),
6199 IDENTIFIER_POINTER
6200 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6201 continue;
6204 /* - Overriding/hiding public must be public
6205 - Overriding/hiding protected must be protected or public
6206 - If the overriden or hidden method has default (package)
6207 access, then the overriding or hiding method must not be
6208 private; otherwise, a compile-time error occurs. If
6209 `found' belongs to an interface, things have been already
6210 taken care of. */
6211 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6212 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6213 || (METHOD_PROTECTED (found)
6214 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6215 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6216 && METHOD_PRIVATE (method))))
6218 parse_error_context
6219 (method_wfl,
6220 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6221 (METHOD_PUBLIC (method) ? "public" :
6222 (METHOD_PRIVATE (method) ? "private" : "protected")),
6223 IDENTIFIER_POINTER (DECL_NAME
6224 (TYPE_NAME (DECL_CONTEXT (found)))));
6225 continue;
6228 /* Overriding methods must have compatible `throws' clauses on checked
6229 exceptions, if any */
6230 check_throws_clauses (method, method_wfl, found);
6232 /* Inheriting multiple methods with the same signature. FIXME */
6235 /* Don't forget eventual pending found and saved_found_wfl. Take
6236 into account that we might have exited because we saw an
6237 artificial method as the last entry. */
6239 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6240 DECL_NAME (found) = saved_found_wfl;
6242 if (!TYPE_NVIRTUALS (class))
6243 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6245 /* Search for inherited abstract method not yet implemented in this
6246 class. */
6247 java_check_abstract_method_definitions (class_decl);
6249 if (!saw_constructor)
6250 fatal ("No constructor found");
6253 /* Return a non zero value if the `throws' clause of METHOD (if any)
6254 is incompatible with the `throws' clause of FOUND (if any). */
6256 static void
6257 check_throws_clauses (method, method_wfl, found)
6258 tree method, method_wfl, found;
6260 tree mthrows, fthrows;
6262 /* Can't check these things with class loaded from bytecode. FIXME */
6263 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6264 return;
6266 for (mthrows = DECL_FUNCTION_THROWS (method);
6267 mthrows; mthrows = TREE_CHAIN (mthrows))
6269 /* We don't verify unchecked expressions */
6270 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6271 continue;
6272 /* Checked expression must be compatible */
6273 for (fthrows = DECL_FUNCTION_THROWS (found);
6274 fthrows; fthrows = TREE_CHAIN (fthrows))
6275 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6276 break;
6277 if (!fthrows)
6279 parse_error_context
6280 (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'",
6281 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6282 lang_printable_name (found, 0),
6283 IDENTIFIER_POINTER
6284 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6289 /* Check abstract method of interface INTERFACE */
6291 static void
6292 java_check_abstract_methods (interface_decl)
6293 tree interface_decl;
6295 int i, n;
6296 tree method, basetype_vec, found;
6297 tree interface = TREE_TYPE (interface_decl);
6299 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6301 tree method_wfl = DECL_NAME (method);
6303 /* 2- Check for double definition inside the defining interface */
6304 if (check_method_redefinition (interface, method))
6305 continue;
6307 /* 3- Overriding is OK as far as we preserve the return type and
6308 the thrown exceptions (FIXME) */
6309 found = lookup_java_interface_method2 (interface, method);
6310 if (found)
6312 char *t;
6313 tree saved_found_wfl = DECL_NAME (found);
6314 reset_method_name (found);
6315 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6316 parse_error_context
6317 (method_wfl,
6318 "Method `%s' was defined with return type `%s' in class `%s'",
6319 lang_printable_name (found, 0), t,
6320 IDENTIFIER_POINTER
6321 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6322 free (t);
6323 DECL_NAME (found) = saved_found_wfl;
6324 continue;
6328 /* 4- Inherited methods can't differ by their returned types */
6329 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6330 return;
6331 n = TREE_VEC_LENGTH (basetype_vec);
6332 for (i = 0; i < n; i++)
6334 tree sub_interface_method, sub_interface;
6335 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6336 if (!vec_elt)
6337 continue;
6338 sub_interface = BINFO_TYPE (vec_elt);
6339 for (sub_interface_method = TYPE_METHODS (sub_interface);
6340 sub_interface_method;
6341 sub_interface_method = TREE_CHAIN (sub_interface_method))
6343 found = lookup_java_interface_method2 (interface,
6344 sub_interface_method);
6345 if (found && (found != sub_interface_method))
6347 tree saved_found_wfl = DECL_NAME (found);
6348 reset_method_name (found);
6349 parse_error_context
6350 (lookup_cl (sub_interface_method),
6351 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6352 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6353 lang_printable_name (found, 0),
6354 IDENTIFIER_POINTER
6355 (DECL_NAME (TYPE_NAME
6356 (DECL_CONTEXT (sub_interface_method)))),
6357 IDENTIFIER_POINTER
6358 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6359 DECL_NAME (found) = saved_found_wfl;
6365 /* Lookup methods in interfaces using their name and partial
6366 signature. Return a matching method only if their types differ. */
6368 static tree
6369 lookup_java_interface_method2 (class, method_decl)
6370 tree class, method_decl;
6372 int i, n;
6373 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6375 if (!basetype_vec)
6376 return NULL_TREE;
6378 n = TREE_VEC_LENGTH (basetype_vec);
6379 for (i = 0; i < n; i++)
6381 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6382 if ((BINFO_TYPE (vec_elt) != object_type_node)
6383 && (to_return =
6384 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6385 return to_return;
6387 for (i = 0; i < n; i++)
6389 to_return = lookup_java_interface_method2
6390 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6391 if (to_return)
6392 return to_return;
6395 return NULL_TREE;
6398 /* Lookup method using their name and partial signature. Return a
6399 matching method only if their types differ. */
6401 static tree
6402 lookup_java_method2 (clas, method_decl, do_interface)
6403 tree clas, method_decl;
6404 int do_interface;
6406 tree method, method_signature, method_name, method_type, name;
6408 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6409 name = DECL_NAME (method_decl);
6410 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6411 EXPR_WFL_NODE (name) : name);
6412 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6414 while (clas != NULL_TREE)
6416 for (method = TYPE_METHODS (clas);
6417 method != NULL_TREE; method = TREE_CHAIN (method))
6419 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6420 tree name = DECL_NAME (method);
6421 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6422 EXPR_WFL_NODE (name) : name) == method_name
6423 && method_sig == method_signature
6424 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6425 return method;
6427 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6429 return NULL_TREE;
6432 /* Return the line that matches DECL line number, and try its best to
6433 position the column number. Used during error reports. */
6435 static tree
6436 lookup_cl (decl)
6437 tree decl;
6439 static tree cl = NULL_TREE;
6440 char *line, *found;
6442 if (!decl)
6443 return NULL_TREE;
6445 if (cl == NULL_TREE)
6447 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6448 ggc_add_tree_root (&cl, 1);
6451 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6452 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6454 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
6455 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6457 found = strstr ((const char *)line,
6458 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6459 if (found)
6460 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6462 return cl;
6465 /* Look for a simple name in the single-type import list */
6467 static tree
6468 find_name_in_single_imports (name)
6469 tree name;
6471 tree node;
6473 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6474 if (TREE_VALUE (node) == name)
6475 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6477 return NULL_TREE;
6480 /* Process all single-type import. */
6482 static int
6483 process_imports ()
6485 tree import;
6486 int error_found;
6488 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6490 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6492 /* Don't load twice something already defined. */
6493 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6494 continue;
6495 QUALIFIED_P (to_be_found) = 1;
6496 load_class (to_be_found, 0);
6497 error_found =
6498 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6499 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6501 parse_error_context (TREE_PURPOSE (import),
6502 "Class or interface `%s' not found in import",
6503 IDENTIFIER_POINTER (to_be_found));
6504 return 1;
6506 if (error_found)
6507 return 1;
6509 return 0;
6512 /* Possibly find and mark a class imported by a single-type import
6513 statement. */
6515 static void
6516 find_in_imports (class_type)
6517 tree class_type;
6519 tree import;
6521 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6522 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6524 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6525 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6529 static int
6530 note_possible_classname (name, len)
6531 const char *name;
6532 int len;
6534 tree node;
6535 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6536 len = len - 5;
6537 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6538 len = len - 6;
6539 else
6540 return 0;
6541 node = ident_subst (name, len, "", '/', '.', "");
6542 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6543 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6544 return 1;
6547 /* Read a import directory, gathering potential match for further type
6548 references. Indifferently reads a filesystem or a ZIP archive
6549 directory. */
6551 static void
6552 read_import_dir (wfl)
6553 tree wfl;
6555 tree package_id = EXPR_WFL_NODE (wfl);
6556 const char *package_name = IDENTIFIER_POINTER (package_id);
6557 int package_length = IDENTIFIER_LENGTH (package_id);
6558 DIR *dirp = NULL;
6559 JCF *saved_jcf = current_jcf;
6561 int found = 0;
6562 int k;
6563 void *entry;
6564 struct buffer filename[1];
6567 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6568 return;
6569 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6571 BUFFER_INIT (filename);
6572 buffer_grow (filename, package_length + 100);
6574 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6576 const char *entry_name = jcf_path_name (entry);
6577 int entry_length = strlen (entry_name);
6578 if (jcf_path_is_zipfile (entry))
6580 ZipFile *zipf;
6581 buffer_grow (filename, entry_length);
6582 memcpy (filename->data, entry_name, entry_length - 1);
6583 filename->data[entry_length-1] = '\0';
6584 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6585 if (zipf == NULL)
6586 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6587 else
6589 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6590 BUFFER_RESET (filename);
6591 for (k = 0; k < package_length; k++)
6593 char ch = package_name[k];
6594 *filename->ptr++ = ch == '.' ? '/' : ch;
6596 *filename->ptr++ = '/';
6598 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6600 const char *current_entry = ZIPDIR_FILENAME (zipd);
6601 int current_entry_len = zipd->filename_length;
6603 if (current_entry_len >= BUFFER_LENGTH (filename)
6604 && strncmp (filename->data, current_entry,
6605 BUFFER_LENGTH (filename)) != 0)
6606 continue;
6607 found |= note_possible_classname (current_entry,
6608 current_entry_len);
6612 else
6614 BUFFER_RESET (filename);
6615 buffer_grow (filename, entry_length + package_length + 4);
6616 strcpy (filename->data, entry_name);
6617 filename->ptr = filename->data + entry_length;
6618 for (k = 0; k < package_length; k++)
6620 char ch = package_name[k];
6621 *filename->ptr++ = ch == '.' ? '/' : ch;
6623 *filename->ptr = '\0';
6625 dirp = opendir (filename->data);
6626 if (dirp == NULL)
6627 continue;
6628 *filename->ptr++ = '/';
6629 for (;;)
6631 int len;
6632 const char *d_name;
6633 struct dirent *direntp = readdir (dirp);
6634 if (!direntp)
6635 break;
6636 d_name = direntp->d_name;
6637 len = strlen (direntp->d_name);
6638 buffer_grow (filename, len+1);
6639 strcpy (filename->ptr, d_name);
6640 found |= note_possible_classname (filename->data + entry_length,
6641 package_length+len+1);
6643 if (dirp)
6644 closedir (dirp);
6648 free (filename->data);
6650 /* Here we should have a unified way of retrieving an entry, to be
6651 indexed. */
6652 if (!found)
6654 static int first = 1;
6655 if (first)
6657 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6658 java_error_count++;
6659 first = 0;
6661 else
6662 parse_error_context (wfl, "Package `%s' not found in import",
6663 package_name);
6664 current_jcf = saved_jcf;
6665 return;
6667 current_jcf = saved_jcf;
6670 /* Possibly find a type in the import on demands specified
6671 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6672 entire list, to detected potential double definitions. */
6674 static int
6675 find_in_imports_on_demand (class_type)
6676 tree class_type;
6678 tree node, import, node_to_use = NULL_TREE;
6679 int seen_once = -1;
6680 tree cl = NULL_TREE;
6682 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6684 const char *id_name;
6685 obstack_grow (&temporary_obstack,
6686 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6687 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6688 obstack_1grow (&temporary_obstack, '.');
6689 obstack_grow0 (&temporary_obstack,
6690 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6691 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6692 id_name = obstack_finish (&temporary_obstack);
6694 node = maybe_get_identifier (id_name);
6695 if (node && IS_A_CLASSFILE_NAME (node))
6697 if (seen_once < 0)
6699 cl = TREE_PURPOSE (import);
6700 seen_once = 1;
6701 node_to_use = node;
6703 else
6705 seen_once++;
6706 parse_error_context
6707 (TREE_PURPOSE (import),
6708 "Type `%s' also potentially defined in package `%s'",
6709 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6710 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6715 if (seen_once == 1)
6717 /* Setup lineno so that it refers to the line of the import (in
6718 case we parse a class file and encounter errors */
6719 tree decl;
6720 int saved_lineno = lineno;
6721 lineno = EXPR_WFL_LINENO (cl);
6722 TYPE_NAME (class_type) = node_to_use;
6723 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6724 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6725 /* If there is no DECL set for the class or if the class isn't
6726 loaded and not seen in source yet, the load */
6727 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6728 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6729 load_class (node_to_use, 0);
6730 lineno = saved_lineno;
6731 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6733 else
6734 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6737 /* Add package NAME to the list of package encountered so far. To
6738 speed up class lookup in do_resolve_class, we make sure a
6739 particular package is added only once. */
6741 static void
6742 register_package (name)
6743 tree name;
6745 static struct hash_table _pht, *pht = NULL;
6747 if (!pht)
6749 hash_table_init (&_pht, hash_newfunc,
6750 java_hash_hash_tree_node, java_hash_compare_tree_node);
6751 pht = &_pht;
6754 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6756 package_list = chainon (package_list, build_tree_list (name, NULL));
6757 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6761 static tree
6762 resolve_package (pkg, next)
6763 tree pkg, *next;
6765 tree current, acc;
6766 tree type_name = NULL_TREE;
6767 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6769 /* The trick is to determine when the package name stops and were
6770 the name of something contained in the package starts. Then we
6771 return a fully qualified name of what we want to get. */
6773 /* Do a quick search on well known package names */
6774 if (!strncmp (name, "java.lang.reflect", 17))
6776 *next =
6777 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6778 type_name = lookup_package_type (name, 17);
6780 else if (!strncmp (name, "java.lang", 9))
6782 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6783 type_name = lookup_package_type (name, 9);
6786 /* If we found something here, return */
6787 if (type_name)
6788 return type_name;
6790 *next = EXPR_WFL_QUALIFICATION (pkg);
6792 /* Try the current package. */
6793 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6794 IDENTIFIER_LENGTH (ctxp->package)))
6796 type_name =
6797 lookup_package_type_and_set_next (name,
6798 IDENTIFIER_LENGTH (ctxp->package),
6799 next );
6800 if (type_name)
6801 return type_name;
6804 /* Search in imported package */
6805 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6807 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6808 int len = IDENTIFIER_LENGTH (current_pkg_name);
6809 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6811 tree left, dummy;
6813 breakdown_qualified (&left, &dummy, current_pkg_name);
6814 len = IDENTIFIER_LENGTH (left);
6815 type_name = lookup_package_type_and_set_next (name, len, next);
6816 if (type_name)
6817 break;
6821 /* Try to progressively construct a type name */
6822 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6823 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6824 current; current = TREE_CHAIN (current))
6826 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6827 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6829 type_name = acc;
6830 /* resolve_package should be used in a loop, hence we
6831 point at this one to naturally process the next one at
6832 the next iteration. */
6833 *next = current;
6834 break;
6837 return type_name;
6840 static tree
6841 lookup_package_type_and_set_next (name, len, next)
6842 const char *name;
6843 int len;
6844 tree *next;
6846 const char *ptr;
6847 tree type_name = lookup_package_type (name, len);
6849 if (!type_name)
6850 return NULL;
6852 ptr = IDENTIFIER_POINTER (type_name);
6853 while (ptr && (ptr = strchr (ptr, '.')))
6855 *next = TREE_CHAIN (*next);
6856 ptr++;
6858 return type_name;
6861 static tree
6862 lookup_package_type (name, from)
6863 const char *name;
6864 int from;
6866 char subname [128];
6867 const char *sub = &name[from+1];
6868 while (*sub != '.' && *sub)
6869 sub++;
6870 strncpy (subname, name, sub-name);
6871 subname [sub-name] = '\0';
6872 return get_identifier (subname);
6875 static void
6876 check_inner_class_access (decl, enclosing_decl, cl)
6877 tree decl, enclosing_decl, cl;
6879 int access = 0;
6881 /* We don't issue an error message when CL is null. CL can be null
6882 as a result of processing a JDEP crafted by source_start_java_method
6883 for the purpose of patching its parm decl. But the error would
6884 have been already trapped when fixing the method's signature.
6885 DECL can also be NULL in case of earlier errors. */
6886 if (!decl || !cl)
6887 return;
6889 /* We grant access to private and protected inner classes if the
6890 location from where we're trying to access DECL is an enclosing
6891 context for DECL or if both have a common enclosing context. */
6892 if (CLASS_PRIVATE (decl))
6893 access = 1;
6894 if (CLASS_PROTECTED (decl))
6895 access = 2;
6896 if (!access)
6897 return;
6899 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6900 TREE_TYPE (decl))
6901 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6902 TREE_TYPE (decl)))
6903 return;
6905 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6906 (access == 1 ? "private" : "protected"),
6907 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6908 lang_printable_name (decl, 0));
6911 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6912 access violations were found, 1 otherwise. */
6914 static int
6915 check_pkg_class_access (class_name, cl)
6916 tree class_name;
6917 tree cl;
6919 tree type;
6921 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6922 return 0;
6924 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6925 return 0;
6927 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6929 /* Access to a private class within the same package is
6930 allowed. */
6931 tree l, r;
6932 breakdown_qualified (&l, &r, class_name);
6933 if (l == ctxp->package)
6934 return 0;
6936 parse_error_context
6937 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
6938 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6939 IDENTIFIER_POINTER (class_name));
6940 return 1;
6942 return 0;
6945 /* Local variable declaration. */
6947 static void
6948 declare_local_variables (modifier, type, vlist)
6949 int modifier;
6950 tree type;
6951 tree vlist;
6953 tree decl, current, saved_type;
6954 tree type_wfl = NULL_TREE;
6955 int must_chain = 0;
6956 int final_p = 0;
6958 /* Push a new block if statements were seen between the last time we
6959 pushed a block and now. Keep a cound of block to close */
6960 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
6962 tree body = GET_CURRENT_BLOCK (current_function_decl);
6963 tree b = enter_block ();
6964 BLOCK_EXPR_ORIGIN (b) = body;
6967 if (modifier)
6969 int i;
6970 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
6971 if (modifier == ACC_FINAL)
6972 final_p = 1;
6973 else
6975 parse_error_context
6976 (ctxp->modifier_ctx [i],
6977 "Only `final' is allowed as a local variables modifier");
6978 return;
6982 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6983 hold the TYPE value if a new incomplete has to be created (as
6984 opposed to being found already existing and reused). */
6985 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6987 /* If TYPE is fully resolved and we don't have a reference, make one */
6988 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6990 /* Go through all the declared variables */
6991 for (current = vlist, saved_type = type; current;
6992 current = TREE_CHAIN (current), type = saved_type)
6994 tree other, real_type;
6995 tree wfl = TREE_PURPOSE (current);
6996 tree name = EXPR_WFL_NODE (wfl);
6997 tree init = TREE_VALUE (current);
6999 /* Process NAME, as it may specify extra dimension(s) for it */
7000 type = build_array_from_name (type, type_wfl, name, &name);
7002 /* Variable redefinition check */
7003 if ((other = lookup_name_in_blocks (name)))
7005 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7006 DECL_SOURCE_LINE (other));
7007 continue;
7010 /* Type adjustment. We may have just readjusted TYPE because
7011 the variable specified more dimensions. Make sure we have
7012 a reference if we can and don't have one already. */
7013 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7015 real_type = GET_REAL_TYPE (type);
7016 /* Never layout this decl. This will be done when its scope
7017 will be entered */
7018 decl = build_decl (VAR_DECL, name, real_type);
7019 LOCAL_FINAL (decl) = final_p;
7020 BLOCK_CHAIN_DECL (decl);
7022 /* If doing xreferencing, replace the line number with the WFL
7023 compound value */
7024 if (flag_emit_xref)
7025 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7027 /* Don't try to use an INIT statement when an error was found */
7028 if (init && java_error_count)
7029 init = NULL_TREE;
7031 /* Add the initialization function to the current function's code */
7032 if (init)
7034 /* Name might have been readjusted */
7035 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7036 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7037 java_method_add_stmt (current_function_decl,
7038 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7039 init));
7042 /* Setup dependency the type of the decl */
7043 if (must_chain)
7045 jdep *dep;
7046 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7047 dep = CLASSD_LAST (ctxp->classd_list);
7048 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7051 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7054 /* Called during parsing. Build decls from argument list. */
7056 static void
7057 source_start_java_method (fndecl)
7058 tree fndecl;
7060 tree tem;
7061 tree parm_decl;
7062 int i;
7064 if (!fndecl)
7065 return;
7067 current_function_decl = fndecl;
7069 /* New scope for the function */
7070 enter_block ();
7071 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7072 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7074 tree type = TREE_VALUE (tem);
7075 tree name = TREE_PURPOSE (tem);
7077 /* If type is incomplete. Create an incomplete decl and ask for
7078 the decl to be patched later */
7079 if (INCOMPLETE_TYPE_P (type))
7081 jdep *jdep;
7082 tree real_type = GET_REAL_TYPE (type);
7083 parm_decl = build_decl (PARM_DECL, name, real_type);
7084 type = obtain_incomplete_type (type);
7085 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7086 jdep = CLASSD_LAST (ctxp->classd_list);
7087 JDEP_MISC (jdep) = name;
7088 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7090 else
7091 parm_decl = build_decl (PARM_DECL, name, type);
7093 /* Remember if a local variable was declared final (via its
7094 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7095 if (ARG_FINAL_P (tem))
7096 LOCAL_FINAL (parm_decl) = 1;
7098 BLOCK_CHAIN_DECL (parm_decl);
7100 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7101 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7102 nreverse (tem);
7103 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7104 DECL_MAX_LOCALS (current_function_decl) = i;
7107 /* Called during parsing. Creates an artificial method declaration. */
7109 static tree
7110 create_artificial_method (class, flags, type, name, args)
7111 tree class;
7112 int flags;
7113 tree type, name, args;
7115 tree mdecl;
7117 java_parser_context_save_global ();
7118 lineno = 0;
7119 mdecl = make_node (FUNCTION_TYPE);
7120 TREE_TYPE (mdecl) = type;
7121 TYPE_ARG_TYPES (mdecl) = args;
7122 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7123 java_parser_context_restore_global ();
7124 DECL_ARTIFICIAL (mdecl) = 1;
7125 return mdecl;
7128 /* Starts the body if an artifical method. */
7130 static void
7131 start_artificial_method_body (mdecl)
7132 tree mdecl;
7134 DECL_SOURCE_LINE (mdecl) = 1;
7135 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7136 source_start_java_method (mdecl);
7137 enter_block ();
7140 static void
7141 end_artificial_method_body (mdecl)
7142 tree mdecl;
7144 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
7145 exit_block ();
7148 /* Called during expansion. Push decls formerly built from argument
7149 list so they're usable during expansion. */
7151 static void
7152 expand_start_java_method (fndecl)
7153 tree fndecl;
7155 tree tem, *ptr;
7157 current_function_decl = fndecl;
7159 if (! quiet_flag)
7160 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
7161 announce_function (fndecl);
7162 if (! quiet_flag)
7163 fprintf (stderr, "]");
7165 pushlevel (1); /* Prepare for a parameter push */
7166 ptr = &DECL_ARGUMENTS (fndecl);
7167 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7168 while (tem)
7170 tree next = TREE_CHAIN (tem);
7171 tree type = TREE_TYPE (tem);
7172 if (PROMOTE_PROTOTYPES
7173 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7174 && INTEGRAL_TYPE_P (type))
7175 type = integer_type_node;
7176 DECL_ARG_TYPE (tem) = type;
7177 layout_decl (tem, 0);
7178 pushdecl (tem);
7179 *ptr = tem;
7180 ptr = &TREE_CHAIN (tem);
7181 tem = next;
7183 *ptr = NULL_TREE;
7184 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7185 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
7188 /* Terminate a function and expand its body. */
7190 static void
7191 source_end_java_method ()
7193 tree fndecl = current_function_decl;
7194 int flag_asynchronous_exceptions = asynchronous_exceptions;
7196 if (!fndecl)
7197 return;
7199 java_parser_context_save_global ();
7200 lineno = ctxp->last_ccb_indent1;
7202 /* Set EH language codes */
7203 java_set_exception_lang_code ();
7205 /* Turn function bodies with only a NOP expr null, so they don't get
7206 generated at all and we won't get warnings when using the -W
7207 -Wall flags. */
7208 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7209 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7211 /* Generate function's code */
7212 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7213 && ! flag_emit_class_files
7214 && ! flag_emit_xref)
7215 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7217 /* pop out of its parameters */
7218 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7219 poplevel (1, 0, 1);
7220 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7222 /* Generate rtl for function exit. */
7223 if (! flag_emit_class_files && ! flag_emit_xref)
7225 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7226 /* Emit catch-finally clauses */
7227 emit_handlers ();
7228 expand_function_end (input_filename, lineno, 0);
7230 /* FIXME: If the current method contains any exception handlers,
7231 force asynchronous_exceptions: this is necessary because signal
7232 handlers in libjava may throw exceptions. This is far from being
7233 a perfect solution, but it's better than doing nothing at all.*/
7234 if (catch_clauses)
7235 asynchronous_exceptions = 1;
7237 /* Run the optimizers and output assembler code for this function. */
7238 rest_of_compilation (fndecl);
7241 current_function_decl = NULL_TREE;
7242 java_parser_context_restore_global ();
7243 asynchronous_exceptions = flag_asynchronous_exceptions;
7246 /* Record EXPR in the current function block. Complements compound
7247 expression second operand if necessary. */
7249 tree
7250 java_method_add_stmt (fndecl, expr)
7251 tree fndecl, expr;
7253 if (!GET_CURRENT_BLOCK (fndecl))
7254 return NULL_TREE;
7255 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7258 static tree
7259 add_stmt_to_block (b, type, stmt)
7260 tree b, type, stmt;
7262 tree body = BLOCK_EXPR_BODY (b), c;
7264 if (java_error_count)
7265 return body;
7267 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7268 return body;
7270 BLOCK_EXPR_BODY (b) = c;
7271 TREE_SIDE_EFFECTS (c) = 1;
7272 return c;
7275 /* Add STMT to EXISTING if possible, otherwise create a new
7276 COMPOUND_EXPR and add STMT to it. */
7278 static tree
7279 add_stmt_to_compound (existing, type, stmt)
7280 tree existing, type, stmt;
7282 if (existing)
7283 return build (COMPOUND_EXPR, type, existing, stmt);
7284 else
7285 return stmt;
7288 void java_layout_seen_class_methods ()
7290 tree previous_list = all_class_list;
7291 tree end = NULL_TREE;
7292 tree current;
7294 while (1)
7296 for (current = previous_list;
7297 current != end; current = TREE_CHAIN (current))
7298 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7300 if (previous_list != all_class_list)
7302 end = previous_list;
7303 previous_list = all_class_list;
7305 else
7306 break;
7310 void
7311 java_reorder_fields ()
7313 static tree stop_reordering = NULL_TREE;
7314 static int initialized_p;
7315 tree current;
7317 /* Register STOP_REORDERING with the garbage collector. */
7318 if (!initialized_p)
7320 ggc_add_tree_root (&stop_reordering, 1);
7321 initialized_p = 1;
7324 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7326 current_class = TREE_TYPE (TREE_VALUE (current));
7328 if (current_class == stop_reordering)
7329 break;
7331 /* Reverse the fields, but leave the dummy field in front.
7332 Fields are already ordered for Object and Class */
7333 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7334 && current_class != class_type_node)
7336 /* If the dummy field is there, reverse the right fields and
7337 just layout the type for proper fields offset */
7338 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7340 tree fields = TYPE_FIELDS (current_class);
7341 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7342 TYPE_SIZE (current_class) = NULL_TREE;
7344 /* We don't have a dummy field, we need to layout the class,
7345 after having reversed the fields */
7346 else
7348 TYPE_FIELDS (current_class) =
7349 nreverse (TYPE_FIELDS (current_class));
7350 TYPE_SIZE (current_class) = NULL_TREE;
7354 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7357 /* Layout the methods of all classes loaded in one way on an
7358 other. Check methods of source parsed classes. Then reorder the
7359 fields and layout the classes or the type of all source parsed
7360 classes */
7362 void
7363 java_layout_classes ()
7365 tree current;
7366 int save_error_count = java_error_count;
7368 /* Layout the methods of all classes seen so far */
7369 java_layout_seen_class_methods ();
7370 java_parse_abort_on_error ();
7371 all_class_list = NULL_TREE;
7373 /* Then check the methods of all parsed classes */
7374 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7375 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7376 java_check_methods (TREE_VALUE (current));
7377 java_parse_abort_on_error ();
7379 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7381 current_class = TREE_TYPE (TREE_VALUE (current));
7382 layout_class (current_class);
7384 /* From now on, the class is considered completely loaded */
7385 CLASS_LOADED_P (current_class) = 1;
7387 /* Error reported by the caller */
7388 if (java_error_count)
7389 return;
7392 /* We might have reloaded classes durign the process of laying out
7393 classes for code generation. We must layout the methods of those
7394 late additions, as constructor checks might use them */
7395 java_layout_seen_class_methods ();
7396 java_parse_abort_on_error ();
7399 /* Expand methods in the current set of classes rememebered for
7400 generation. */
7402 static void
7403 java_complete_expand_classes ()
7405 tree current;
7407 do_not_fold = flag_emit_xref;
7409 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7410 if (!INNER_CLASS_DECL_P (current))
7411 java_complete_expand_class (current);
7414 /* Expand the methods found in OUTER, starting first by OUTER's inner
7415 classes, if any. */
7417 static void
7418 java_complete_expand_class (outer)
7419 tree outer;
7421 tree inner_list;
7423 set_nested_class_simple_name_value (outer, 1); /* Set */
7425 /* We need to go after all inner classes and start expanding them,
7426 starting with most nested ones. We have to do that because nested
7427 classes might add functions to outer classes */
7429 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7430 inner_list; inner_list = TREE_CHAIN (inner_list))
7431 java_complete_expand_class (TREE_PURPOSE (inner_list));
7433 java_complete_expand_methods (outer);
7434 set_nested_class_simple_name_value (outer, 0); /* Reset */
7437 /* Expand methods registered in CLASS_DECL. The general idea is that
7438 we expand regular methods first. This allows us get an estimate on
7439 how outer context local alias fields are really used so we can add
7440 to the constructor just enough code to initialize them properly (it
7441 also lets us generate finit$ correctly.) Then we expand the
7442 constructors and then <clinit>. */
7444 static void
7445 java_complete_expand_methods (class_decl)
7446 tree class_decl;
7448 tree clinit, finit, decl, first_decl;
7450 current_class = TREE_TYPE (class_decl);
7452 /* Initialize a new constant pool */
7453 init_outgoing_cpool ();
7455 /* Pre-expand <clinit> to figure whether we really need it or
7456 not. If we do need it, we pre-expand the static fields so they're
7457 ready to be used somewhere else. <clinit> will be fully expanded
7458 after we processed the constructors. */
7459 first_decl = TYPE_METHODS (current_class);
7460 clinit = maybe_generate_pre_expand_clinit (current_class);
7462 /* Then generate finit$ (if we need to) because constructor will
7463 try to use it.*/
7464 if (TYPE_FINIT_STMT_LIST (current_class))
7466 finit = generate_finit (current_class);
7467 java_complete_expand_method (finit);
7470 /* Now do the constructors */
7471 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7473 int no_body;
7475 if (!DECL_CONSTRUCTOR_P (decl))
7476 continue;
7478 no_body = !DECL_FUNCTION_BODY (decl);
7479 /* Don't generate debug info on line zero when expanding a
7480 generated constructor. */
7481 if (no_body)
7482 restore_line_number_status (1);
7484 java_complete_expand_method (decl);
7486 if (no_body)
7487 restore_line_number_status (0);
7490 /* First, do the ordinary methods. */
7491 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7493 /* Skip abstract or native methods -- but do handle native
7494 methods when generating JNI stubs. */
7495 if (METHOD_ABSTRACT (decl)
7496 || (! flag_jni && METHOD_NATIVE (decl))
7497 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7498 continue;
7500 if (METHOD_NATIVE (decl))
7502 tree body = build_jni_stub (decl);
7503 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7506 java_complete_expand_method (decl);
7509 /* If there is indeed a <clinit>, fully expand it now */
7510 if (clinit)
7512 /* Prevent the use of `this' inside <clinit> */
7513 ctxp->explicit_constructor_p = 1;
7514 java_complete_expand_method (clinit);
7515 ctxp->explicit_constructor_p = 0;
7518 /* We might have generated a class$ that we now want to expand */
7519 if (TYPE_DOT_CLASS (current_class))
7520 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7522 /* Now verify constructor circularity (stop after the first one we
7523 prove wrong.) */
7524 if (!CLASS_INTERFACE (class_decl))
7525 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7526 if (DECL_CONSTRUCTOR_P (decl)
7527 && verify_constructor_circularity (decl, decl))
7528 break;
7530 /* Save the constant pool. We'll need to restore it later. */
7531 TYPE_CPOOL (current_class) = outgoing_cpool;
7534 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7535 safely used in some other methods/constructors. */
7537 static tree
7538 maybe_generate_pre_expand_clinit (class_type)
7539 tree class_type;
7541 tree current, mdecl;
7543 if (!TYPE_CLINIT_STMT_LIST (class_type))
7544 return NULL_TREE;
7546 /* Go through all static fields and pre expand them */
7547 for (current = TYPE_FIELDS (class_type); current;
7548 current = TREE_CHAIN (current))
7549 if (FIELD_STATIC (current))
7550 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7552 /* Then build the <clinit> method */
7553 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7554 clinit_identifier_node, end_params_node);
7555 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7556 mdecl, NULL_TREE);
7557 start_artificial_method_body (mdecl);
7559 /* We process the list of assignment we produced as the result of
7560 the declaration of initialized static field and add them as
7561 statement to the <clinit> method. */
7562 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7563 current = TREE_CHAIN (current))
7565 tree stmt = current;
7566 /* We build the assignment expression that will initialize the
7567 field to its value. There are strict rules on static
7568 initializers (8.5). FIXME */
7569 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7570 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7571 java_method_add_stmt (mdecl, stmt);
7574 end_artificial_method_body (mdecl);
7576 /* Now we want to place <clinit> as the last method (because we need
7577 it at least for interface so that it doesn't interfere with the
7578 dispatch table based lookup. */
7579 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7581 current = TREE_CHAIN (TYPE_METHODS (class_type));
7582 TYPE_METHODS (class_type) = current;
7584 while (TREE_CHAIN (current))
7585 current = TREE_CHAIN (current);
7587 TREE_CHAIN (current) = mdecl;
7588 TREE_CHAIN (mdecl) = NULL_TREE;
7591 return mdecl;
7594 /* Analyzes a method body and look for something that isn't a
7595 MODIFY_EXPR. */
7597 static int
7598 analyze_clinit_body (bbody)
7599 tree bbody;
7601 while (bbody)
7602 switch (TREE_CODE (bbody))
7604 case BLOCK:
7605 bbody = BLOCK_EXPR_BODY (bbody);
7606 break;
7608 case EXPR_WITH_FILE_LOCATION:
7609 bbody = EXPR_WFL_NODE (bbody);
7610 break;
7612 case COMPOUND_EXPR:
7613 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7614 return 1;
7615 bbody = TREE_OPERAND (bbody, 1);
7616 break;
7618 case MODIFY_EXPR:
7619 bbody = NULL_TREE;
7620 break;
7622 default:
7623 bbody = NULL_TREE;
7624 return 1;
7626 return 0;
7630 /* See whether we could get rid of <clinit>. Criteria are: all static
7631 final fields have constant initial values and the body of <clinit>
7632 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7634 static int
7635 maybe_yank_clinit (mdecl)
7636 tree mdecl;
7638 tree type, current;
7639 tree fbody, bbody;
7640 int found = 0;
7642 if (!DECL_CLINIT_P (mdecl))
7643 return 0;
7645 /* If the body isn't empty, then we keep <clinit>. Note that if
7646 we're emitting classfiles, this isn't enough not to rule it
7647 out. */
7648 fbody = DECL_FUNCTION_BODY (mdecl);
7649 bbody = BLOCK_EXPR_BODY (fbody);
7650 if (bbody && bbody != error_mark_node)
7651 bbody = BLOCK_EXPR_BODY (bbody);
7652 else
7653 return 0;
7654 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7655 return 0;
7657 type = DECL_CONTEXT (mdecl);
7658 current = TYPE_FIELDS (type);
7660 for (current = (current ? TREE_CHAIN (current) : current);
7661 current; current = TREE_CHAIN (current))
7663 tree f_init;
7665 /* We're not interested in non static field */
7666 if (!FIELD_STATIC (current))
7667 continue;
7669 /* Anything that isn't String or a basic type is ruled out -- or
7670 if we now how to deal with it (when doing things natively) we
7671 should generated an empty <clinit> so that SUID are computed
7672 correctly. */
7673 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7674 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7675 break;
7677 f_init = DECL_INITIAL (current);
7678 /* If we're emitting native code, we want static final fields to
7679 have constant initializers. If we don't meet these
7680 conditions, we keep <clinit> */
7681 if (!flag_emit_class_files
7682 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7683 break;
7684 /* If we're emitting bytecode, we want static fields to have
7685 constant initializers or no initializer. If we don't meet
7686 these conditions, we keep <clinit> */
7687 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7688 break;
7691 /* Now we analyze the method body and look for something that
7692 isn't a MODIFY_EXPR */
7693 if (bbody == empty_stmt_node)
7694 found = 0;
7695 else
7696 found = analyze_clinit_body (bbody);
7698 if (current || found)
7699 return 0;
7701 /* Get rid of <clinit> in the class' list of methods */
7702 if (TYPE_METHODS (type) == mdecl)
7703 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7704 else
7705 for (current = TYPE_METHODS (type); current;
7706 current = TREE_CHAIN (current))
7707 if (TREE_CHAIN (current) == mdecl)
7709 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7710 break;
7713 return 1;
7717 /* Complete and expand a method. */
7719 static void
7720 java_complete_expand_method (mdecl)
7721 tree mdecl;
7723 int yank_clinit = 0;
7725 current_function_decl = mdecl;
7726 /* Fix constructors before expanding them */
7727 if (DECL_CONSTRUCTOR_P (mdecl))
7728 fix_constructors (mdecl);
7730 /* Expand functions that have a body */
7731 if (DECL_FUNCTION_BODY (mdecl))
7733 tree fbody = DECL_FUNCTION_BODY (mdecl);
7734 tree block_body = BLOCK_EXPR_BODY (fbody);
7735 tree exception_copy = NULL_TREE;
7736 expand_start_java_method (mdecl);
7737 build_result_decl (mdecl);
7739 current_this
7740 = (!METHOD_STATIC (mdecl) ?
7741 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7743 /* Purge the `throws' list of unchecked exceptions. If we're
7744 doing xref, save a copy of the list and re-install it
7745 later. */
7746 if (flag_emit_xref)
7747 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7749 purge_unchecked_exceptions (mdecl);
7751 /* Install exceptions thrown with `throws' */
7752 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7754 if (block_body != NULL_TREE)
7756 block_body = java_complete_tree (block_body);
7758 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7759 check_for_initialization (block_body);
7760 ctxp->explicit_constructor_p = 0;
7763 BLOCK_EXPR_BODY (fbody) = block_body;
7765 /* If we saw a return but couldn't evaluate it properly, we'll
7766 have an error_mark_node here. */
7767 if (block_body != error_mark_node
7768 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7769 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7770 && !flag_emit_xref)
7771 missing_return_error (current_function_decl);
7773 /* Check wether we could just get rid of clinit, now the picture
7774 is complete. */
7775 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7776 complete_start_java_method (mdecl);
7778 /* Don't go any further if we've found error(s) during the
7779 expansion */
7780 if (!java_error_count && !yank_clinit)
7781 source_end_java_method ();
7782 else
7784 if (java_error_count)
7785 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7786 poplevel (1, 0, 1);
7789 /* Pop the exceptions and sanity check */
7790 POP_EXCEPTIONS();
7791 if (currently_caught_type_list)
7792 fatal ("Exception list non empty - java_complete_expand_method");
7794 if (flag_emit_xref)
7795 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7801 /* This section of the code deals with accessing enclosing context
7802 fields either directly by using the relevant access to this$<n> or
7803 by invoking an access method crafted for that purpose. */
7805 /* Build the necessary access from an inner class to an outer
7806 class. This routine could be optimized to cache previous result
7807 (decl, current_class and returned access). When an access method
7808 needs to be generated, it always takes the form of a read. It might
7809 be later turned into a write by calling outer_field_access_fix. */
7811 static tree
7812 build_outer_field_access (id, decl)
7813 tree id, decl;
7815 tree access = NULL_TREE;
7816 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7818 /* If decl's class is the direct outer class of the current_class,
7819 build the access as `this$<n>.<field>'. Note that we will break
7820 the `private' barrier if we're not emitting bytecodes. */
7821 if (ctx == DECL_CONTEXT (decl)
7822 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7824 tree thisn = build_current_thisn (current_class);
7825 access = make_qualified_primary (build_wfl_node (thisn),
7826 id, EXPR_WFL_LINECOL (id));
7828 /* Otherwise, generate access methods to outer this and access the
7829 field (either using an access method or by direct access.) */
7830 else
7832 int lc = EXPR_WFL_LINECOL (id);
7834 /* Now we chain the required number of calls to the access$0 to
7835 get a hold to the enclosing instance we need, and then we
7836 build the field access. */
7837 access = build_access_to_thisn (current_class, DECL_CONTEXT (decl), lc);
7839 /* If the field is private and we're generating bytecode, then
7840 we generate an access method */
7841 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7843 tree name = build_outer_field_access_methods (decl);
7844 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7845 name, access, NULL_TREE);
7847 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7848 Once again we break the `private' access rule from a foreign
7849 class. */
7850 else
7851 access = make_qualified_primary (access, id, lc);
7853 return resolve_expression_name (access, NULL);
7856 /* Return a non zero value if NODE describes an outer field inner
7857 access. */
7859 static int
7860 outer_field_access_p (type, decl)
7861 tree type, decl;
7863 if (!INNER_CLASS_TYPE_P (type)
7864 || TREE_CODE (decl) != FIELD_DECL
7865 || DECL_CONTEXT (decl) == type)
7866 return 0;
7868 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7869 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7871 if (type == DECL_CONTEXT (decl))
7872 return 1;
7873 if (!DECL_CONTEXT (TYPE_NAME (type)))
7874 break;
7877 return 0;
7880 /* Return a non zero value if NODE represents an outer field inner
7881 access that was been already expanded. As a side effect, it returns
7882 the name of the field being accessed and the argument passed to the
7883 access function, suitable for a regeneration of the access method
7884 call if necessary. */
7886 static int
7887 outer_field_expanded_access_p (node, name, arg_type, arg)
7888 tree node, *name, *arg_type, *arg;
7890 int identified = 0;
7892 if (TREE_CODE (node) != CALL_EXPR)
7893 return 0;
7895 /* Well, gcj generates slightly different tree nodes when compiling
7896 to native or bytecodes. It's the case for function calls. */
7898 if (flag_emit_class_files
7899 && TREE_CODE (node) == CALL_EXPR
7900 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7901 identified = 1;
7902 else if (!flag_emit_class_files)
7904 node = TREE_OPERAND (node, 0);
7906 if (node && TREE_OPERAND (node, 0)
7907 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7909 node = TREE_OPERAND (node, 0);
7910 if (TREE_OPERAND (node, 0)
7911 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7912 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7913 (DECL_NAME (TREE_OPERAND (node, 0)))))
7914 identified = 1;
7918 if (identified && name && arg_type && arg)
7920 tree argument = TREE_OPERAND (node, 1);
7921 *name = DECL_NAME (TREE_OPERAND (node, 0));
7922 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7923 *arg = TREE_VALUE (argument);
7925 return identified;
7928 /* Detect in NODE an outer field read access from an inner class and
7929 transform it into a write with RHS as an argument. This function is
7930 called from the java_complete_lhs when an assignment to a LHS can
7931 be identified. */
7933 static tree
7934 outer_field_access_fix (wfl, node, rhs)
7935 tree wfl, node, rhs;
7937 tree name, arg_type, arg;
7939 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7941 /* At any rate, check whether we're trying to assign a value to
7942 a final. */
7943 tree accessed = (JDECL_P (node) ? node :
7944 (TREE_CODE (node) == COMPONENT_REF ?
7945 TREE_OPERAND (node, 1) : node));
7946 if (check_final_assignment (accessed, wfl))
7947 return error_mark_node;
7949 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7950 arg_type, name, arg, rhs);
7951 return java_complete_tree (node);
7953 return NULL_TREE;
7956 /* Construct the expression that calls an access method:
7957 <type>.access$<n>(<arg1> [, <arg2>]);
7959 ARG2 can be NULL and will be omitted in that case. It will denote a
7960 read access. */
7962 static tree
7963 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7964 int lc;
7965 tree type, access_method_name, arg1, arg2;
7967 tree args, cn, access;
7969 args = arg1 ? arg1 :
7970 build_wfl_node (build_current_thisn (current_class));
7971 args = build_tree_list (NULL_TREE, args);
7973 if (arg2)
7974 args = tree_cons (NULL_TREE, arg2, args);
7976 access = build_method_invocation (build_wfl_node (access_method_name), args);
7977 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7978 return make_qualified_primary (cn, access, lc);
7981 static tree
7982 build_new_access_id ()
7984 static int access_n_counter = 1;
7985 char buffer [128];
7987 sprintf (buffer, "access$%d", access_n_counter++);
7988 return get_identifier (buffer);
7991 /* Create the static access functions for the outer field DECL. We define a
7992 read:
7993 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7994 return inst$.field;
7996 and a write access:
7997 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7998 TREE_TYPE (<field>) value$) {
7999 return inst$.field = value$;
8001 We should have a usage flags on the DECL so we can lazily turn the ones
8002 we're using for code generation. FIXME.
8005 static tree
8006 build_outer_field_access_methods (decl)
8007 tree decl;
8009 tree id, args, stmt, mdecl;
8011 /* Check point, to be removed. FIXME */
8012 if (FIELD_INNER_ACCESS (decl)
8013 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
8014 abort ();
8016 if (FIELD_INNER_ACCESS (decl))
8017 return FIELD_INNER_ACCESS (decl);
8019 /* Create the identifier and a function named after it. */
8020 id = build_new_access_id ();
8022 /* The identifier is marked as bearing the name of a generated write
8023 access function for outer field accessed from inner classes. */
8024 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8026 /* Create the read access */
8027 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8028 TREE_CHAIN (args) = end_params_node;
8029 stmt = make_qualified_primary (build_wfl_node (inst_id),
8030 build_wfl_node (DECL_NAME (decl)), 0);
8031 stmt = build_return (0, stmt);
8032 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8033 TREE_TYPE (decl), id, args, stmt);
8034 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8036 /* Create the write access method */
8037 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8038 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8039 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8040 stmt = make_qualified_primary (build_wfl_node (inst_id),
8041 build_wfl_node (DECL_NAME (decl)), 0);
8042 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8043 build_wfl_node (wpv_id)));
8045 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8046 TREE_TYPE (decl), id, args, stmt);
8047 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8049 /* Return the access name */
8050 return FIELD_INNER_ACCESS (decl) = id;
8053 /* Build an field access method NAME. */
8055 static tree
8056 build_outer_field_access_method (class, type, name, args, body)
8057 tree class, type, name, args, body;
8059 tree saved_current_function_decl, mdecl;
8061 /* Create the method */
8062 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8063 fix_method_argument_names (args, mdecl);
8064 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8066 /* Attach the method body. */
8067 saved_current_function_decl = current_function_decl;
8068 start_artificial_method_body (mdecl);
8069 java_method_add_stmt (mdecl, body);
8070 end_artificial_method_body (mdecl);
8071 current_function_decl = saved_current_function_decl;
8073 return mdecl;
8077 /* This section deals with building access function necessary for
8078 certain kinds of method invocation from inner classes. */
8080 static tree
8081 build_outer_method_access_method (decl)
8082 tree decl;
8084 tree saved_current_function_decl, mdecl;
8085 tree args = NULL_TREE, call_args = NULL_TREE;
8086 tree carg, id, body, class;
8087 char buffer [80];
8088 int parm_id_count = 0;
8090 /* Test this abort with an access to a private field */
8091 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8092 abort ();
8094 /* Check the cache first */
8095 if (DECL_FUNCTION_INNER_ACCESS (decl))
8096 return DECL_FUNCTION_INNER_ACCESS (decl);
8098 class = DECL_CONTEXT (decl);
8100 /* Obtain an access identifier and mark it */
8101 id = build_new_access_id ();
8102 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8104 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8105 /* Create the arguments, as much as the original */
8106 for (; carg && carg != end_params_node;
8107 carg = TREE_CHAIN (carg))
8109 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8110 args = chainon (args, build_tree_list (get_identifier (buffer),
8111 TREE_VALUE (carg)));
8113 args = chainon (args, end_params_node);
8115 /* Create the method */
8116 mdecl = create_artificial_method (class, ACC_STATIC,
8117 TREE_TYPE (TREE_TYPE (decl)), id, args);
8118 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8119 /* There is a potential bug here. We should be able to use
8120 fix_method_argument_names, but then arg names get mixed up and
8121 eventually a constructor will have its this$0 altered and the
8122 outer context won't be assignment properly. The test case is
8123 stub.java FIXME */
8124 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8126 /* Attach the method body. */
8127 saved_current_function_decl = current_function_decl;
8128 start_artificial_method_body (mdecl);
8130 /* The actual method invocation uses the same args. When invoking a
8131 static methods that way, we don't want to skip the first
8132 argument. */
8133 carg = args;
8134 if (!METHOD_STATIC (decl))
8135 carg = TREE_CHAIN (carg);
8136 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8137 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8138 call_args);
8140 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8141 call_args);
8142 if (!METHOD_STATIC (decl))
8143 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8144 body, 0);
8145 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8146 body = build_return (0, body);
8147 java_method_add_stmt (mdecl,body);
8148 end_artificial_method_body (mdecl);
8149 current_function_decl = saved_current_function_decl;
8151 /* Back tag the access function so it know what it accesses */
8152 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8154 /* Tag the current method so it knows it has an access generated */
8155 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8159 /* This section of the code deals with building expressions to access
8160 the enclosing instance of an inner class. The enclosing instance is
8161 kept in a generated field called this$<n>, with <n> being the
8162 inner class nesting level (starting from 0.) */
8164 /* Build an access to a given this$<n>, always chaining access call to
8165 others. Access methods to this$<n> are build on the fly if
8166 necessary. This CAN'T be used to solely access this$<n-1> from
8167 this$<n> (which alway yield to special cases and optimization, see
8168 for example build_outer_field_access). */
8170 static tree
8171 build_access_to_thisn (from, to, lc)
8172 tree from, to;
8173 int lc;
8175 tree access = NULL_TREE;
8177 while (from != to)
8179 if (!access)
8181 access = build_current_thisn (from);
8182 access = build_wfl_node (access);
8184 else
8186 tree access0_wfl, cn;
8188 maybe_build_thisn_access_method (from);
8189 access0_wfl = build_wfl_node (access0_identifier_node);
8190 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8191 EXPR_WFL_LINECOL (access0_wfl) = lc;
8192 access = build_tree_list (NULL_TREE, access);
8193 access = build_method_invocation (access0_wfl, access);
8194 access = make_qualified_primary (cn, access, lc);
8197 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
8199 return access;
8202 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8203 is returned if nothing needs to be generated. Otherwise, the method
8204 generated and a method decl is returned.
8206 NOTE: These generated methods should be declared in a class file
8207 attribute so that they can't be referred to directly. */
8209 static tree
8210 maybe_build_thisn_access_method (type)
8211 tree type;
8213 tree mdecl, args, stmt, rtype;
8214 tree saved_current_function_decl;
8216 /* If TYPE is a top-level class, no access method is required.
8217 If there already is such an access method, bail out. */
8218 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
8219 return NULL_TREE;
8221 /* We generate the method. The method looks like:
8222 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8224 args = build_tree_list (inst_id, build_pointer_type (type));
8225 TREE_CHAIN (args) = end_params_node;
8226 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8227 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8228 access0_identifier_node, args);
8229 fix_method_argument_names (args, mdecl);
8230 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8231 stmt = build_current_thisn (type);
8232 stmt = make_qualified_primary (build_wfl_node (inst_id),
8233 build_wfl_node (stmt), 0);
8234 stmt = build_return (0, stmt);
8236 saved_current_function_decl = current_function_decl;
8237 start_artificial_method_body (mdecl);
8238 java_method_add_stmt (mdecl, stmt);
8239 end_artificial_method_body (mdecl);
8240 current_function_decl = saved_current_function_decl;
8242 CLASS_ACCESS0_GENERATED_P (type) = 1;
8244 return mdecl;
8247 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8248 the first level of innerclassing. this$1 for the next one, etc...
8249 This function can be invoked with TYPE to NULL, available and then
8250 has to count the parser context. */
8252 static tree
8253 build_current_thisn (type)
8254 tree type;
8256 static int saved_i = -1;
8257 static tree saved_thisn = NULL_TREE;
8258 static tree saved_type = NULL_TREE;
8259 static int saved_type_i = 0;
8260 static int initialized_p;
8261 tree decl;
8262 char buffer [80];
8263 int i = 0;
8265 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8266 if (!initialized_p)
8268 ggc_add_tree_root (&saved_thisn, 1);
8269 ggc_add_tree_root (&saved_type, 1);
8270 initialized_p = 1;
8273 if (type)
8275 if (type == saved_type)
8276 i = saved_type_i;
8277 else
8279 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8280 decl; decl = DECL_CONTEXT (decl), i++)
8283 saved_type = type;
8284 saved_type_i = i;
8287 else
8288 i = list_length (GET_CPC_LIST ())-2;
8290 if (i == saved_i)
8291 return saved_thisn;
8293 sprintf (buffer, "this$%d", i);
8294 saved_i = i;
8295 saved_thisn = get_identifier (buffer);
8296 return saved_thisn;
8299 /* Return the assignement to the hidden enclosing context `this$<n>'
8300 by the second incoming parameter to the innerclass constructor. The
8301 form used is `this.this$<n> = this$<n>;'. */
8303 static tree
8304 build_thisn_assign ()
8306 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8308 tree thisn = build_current_thisn (current_class);
8309 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8310 build_wfl_node (thisn), 0);
8311 tree rhs = build_wfl_node (thisn);
8312 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8313 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8315 return NULL_TREE;
8319 /* Building the synthetic `class$' used to implement the `.class' 1.1
8320 extension for non primitive types. This method looks like:
8322 static Class class$(String type) throws NoClassDefFoundError
8324 try {return (java.lang.Class.forName (String));}
8325 catch (ClassNotFoundException e) {
8326 throw new NoClassDefFoundError(e.getMessage());}
8327 } */
8329 static tree
8330 build_dot_class_method (class)
8331 tree class;
8333 #define BWF(S) build_wfl_node (get_identifier ((S)))
8334 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8335 tree args, tmp, saved_current_function_decl, mdecl;
8336 tree stmt, throw_stmt, catch, catch_block, try_block;
8337 tree catch_clause_param;
8338 tree class_not_found_exception, no_class_def_found_error;
8340 static tree get_message_wfl, type_parm_wfl;
8342 if (!get_message_wfl)
8344 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8345 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8346 ggc_add_tree_root (&get_message_wfl, 1);
8347 ggc_add_tree_root (&type_parm_wfl, 1);
8350 /* Build the arguments */
8351 args = build_tree_list (get_identifier ("type$"),
8352 build_pointer_type (string_type_node));
8353 TREE_CHAIN (args) = end_params_node;
8355 /* Build the qualified name java.lang.Class.forName */
8356 tmp = MQN (MQN (MQN (BWF ("java"),
8357 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8359 /* For things we have to catch and throw */
8360 class_not_found_exception =
8361 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8362 no_class_def_found_error =
8363 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8364 load_class (class_not_found_exception, 1);
8365 load_class (no_class_def_found_error, 1);
8367 /* Create the "class$" function */
8368 mdecl = create_artificial_method (class, ACC_STATIC,
8369 build_pointer_type (class_type_node),
8370 get_identifier ("class$"), args);
8371 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8372 no_class_def_found_error);
8374 /* We start by building the try block. We need to build:
8375 return (java.lang.Class.forName (type)); */
8376 stmt = build_method_invocation (tmp,
8377 build_tree_list (NULL_TREE, type_parm_wfl));
8378 stmt = build_return (0, stmt);
8379 /* Put it in a block. That's the try block */
8380 try_block = build_expr_block (stmt, NULL_TREE);
8382 /* Now onto the catch block. We start by building the expression
8383 throwing a new exception:
8384 throw new NoClassDefFoundError (_.getMessage); */
8385 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8386 get_message_wfl, 0);
8387 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8389 /* Build new NoClassDefFoundError (_.getMessage) */
8390 throw_stmt = build_new_invocation
8391 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8392 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8394 /* Build the throw, (it's too early to use BUILD_THROW) */
8395 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8397 /* Build the catch block to encapsulate all this. We begin by
8398 building an decl for the catch clause parameter and link it to
8399 newly created block, the catch block. */
8400 catch_clause_param =
8401 build_decl (VAR_DECL, wpv_id,
8402 build_pointer_type (class_not_found_exception));
8403 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8405 /* We initialize the variable with the exception handler. */
8406 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8407 soft_exceptioninfo_call_node);
8408 add_stmt_to_block (catch_block, NULL_TREE, catch);
8410 /* We add the statement throwing the new exception */
8411 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8413 /* Build a catch expression for all this */
8414 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8416 /* Build the try/catch sequence */
8417 stmt = build_try_statement (0, try_block, catch_block);
8419 fix_method_argument_names (args, mdecl);
8420 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8421 saved_current_function_decl = current_function_decl;
8422 start_artificial_method_body (mdecl);
8423 java_method_add_stmt (mdecl, stmt);
8424 end_artificial_method_body (mdecl);
8425 current_function_decl = saved_current_function_decl;
8426 TYPE_DOT_CLASS (class) = mdecl;
8428 return mdecl;
8431 static tree
8432 build_dot_class_method_invocation (type)
8433 tree type;
8435 tree sig_id, s;
8437 if (TYPE_ARRAY_P (type))
8438 sig_id = build_java_signature (type);
8439 else
8440 sig_id = DECL_NAME (TYPE_NAME (type));
8442 s = build_string (IDENTIFIER_LENGTH (sig_id),
8443 IDENTIFIER_POINTER (sig_id));
8444 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8445 build_tree_list (NULL_TREE, s));
8448 /* This section of the code deals with constructor. */
8450 /* Craft a body for default constructor. Patch existing constructor
8451 bodies with call to super() and field initialization statements if
8452 necessary. */
8454 static void
8455 fix_constructors (mdecl)
8456 tree mdecl;
8458 tree body = DECL_FUNCTION_BODY (mdecl);
8459 tree thisn_assign, compound = NULL_TREE;
8460 tree class_type = DECL_CONTEXT (mdecl);
8462 if (!body)
8464 /* It is an error for the compiler to generate a default
8465 constructor if the superclass doesn't have a constructor that
8466 takes no argument, or the same args for an anonymous class */
8467 if (verify_constructor_super (mdecl))
8469 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8470 tree save = DECL_NAME (mdecl);
8471 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8472 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8473 parse_error_context
8474 (lookup_cl (TYPE_NAME (class_type)),
8475 "No constructor matching `%s' found in class `%s'",
8476 lang_printable_name (mdecl, 0), n);
8477 DECL_NAME (mdecl) = save;
8480 /* The constructor body must be crafted by hand. It's the
8481 constructor we defined when we realize we didn't have the
8482 CLASSNAME() constructor */
8483 start_artificial_method_body (mdecl);
8485 /* Insert an assignment to the this$<n> hidden field, if
8486 necessary */
8487 if ((thisn_assign = build_thisn_assign ()))
8488 java_method_add_stmt (mdecl, thisn_assign);
8490 /* We don't generate a super constructor invocation if we're
8491 compiling java.lang.Object. build_super_invocation takes care
8492 of that. */
8493 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8495 /* Insert the instance initializer block right here, after the
8496 super invocation. */
8497 add_instance_initializer (mdecl);
8499 end_artificial_method_body (mdecl);
8501 /* Search for an explicit constructor invocation */
8502 else
8504 int found = 0;
8505 tree main_block = BLOCK_EXPR_BODY (body);
8507 while (body)
8508 switch (TREE_CODE (body))
8510 case CALL_EXPR:
8511 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8512 body = NULL_TREE;
8513 break;
8514 case COMPOUND_EXPR:
8515 case EXPR_WITH_FILE_LOCATION:
8516 body = TREE_OPERAND (body, 0);
8517 break;
8518 case BLOCK:
8519 body = BLOCK_EXPR_BODY (body);
8520 break;
8521 default:
8522 found = 0;
8523 body = NULL_TREE;
8525 /* The constructor is missing an invocation of super() */
8526 if (!found)
8527 compound = add_stmt_to_compound (compound, NULL_TREE,
8528 build_super_invocation (mdecl));
8530 /* Generate the assignment to this$<n>, if necessary */
8531 if ((thisn_assign = build_thisn_assign ()))
8532 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8534 /* Insert the instance initializer block right here, after the
8535 super invocation. */
8536 add_instance_initializer (mdecl);
8538 /* Fix the constructor main block if we're adding extra stmts */
8539 if (compound)
8541 compound = add_stmt_to_compound (compound, NULL_TREE,
8542 BLOCK_EXPR_BODY (main_block));
8543 BLOCK_EXPR_BODY (main_block) = compound;
8548 /* Browse constructors in the super class, searching for a constructor
8549 that doesn't take any argument. Return 0 if one is found, 1
8550 otherwise. If the current class is an anonymous inner class, look
8551 for something that has the same signature. */
8553 static int
8554 verify_constructor_super (mdecl)
8555 tree mdecl;
8557 tree class = CLASSTYPE_SUPER (current_class);
8558 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8559 tree sdecl;
8561 if (!class)
8562 return 0;
8564 if (ANONYMOUS_CLASS_P (current_class))
8566 tree mdecl_arg_type;
8567 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8568 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8569 if (DECL_CONSTRUCTOR_P (sdecl))
8571 tree m_arg_type;
8572 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8573 if (super_inner)
8574 arg_type = TREE_CHAIN (arg_type);
8575 for (m_arg_type = mdecl_arg_type;
8576 (arg_type != end_params_node
8577 && m_arg_type != end_params_node);
8578 arg_type = TREE_CHAIN (arg_type),
8579 m_arg_type = TREE_CHAIN (m_arg_type))
8580 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
8581 break;
8583 if (arg_type == end_params_node && m_arg_type == end_params_node)
8584 return 0;
8587 else
8589 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8591 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8592 if (super_inner)
8593 arg = TREE_CHAIN (arg);
8594 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8595 return 0;
8598 return 1;
8601 /* Generate code for all context remembered for code generation. */
8603 void
8604 java_expand_classes ()
8606 int save_error_count = 0;
8607 static struct parser_ctxt *saved_ctxp = NULL;
8609 java_parse_abort_on_error ();
8610 if (!(ctxp = ctxp_for_generation))
8611 return;
8612 java_layout_classes ();
8613 java_parse_abort_on_error ();
8615 saved_ctxp = ctxp_for_generation;
8616 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8618 ctxp = ctxp_for_generation;
8619 lang_init_source (2); /* Error msgs have method prototypes */
8620 java_complete_expand_classes (); /* Complete and expand classes */
8621 java_parse_abort_on_error ();
8624 /* Find anonymous classes and expand their constructor, now they
8625 have been fixed. */
8626 for (ctxp_for_generation = saved_ctxp;
8627 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8629 tree current;
8630 ctxp = ctxp_for_generation;
8631 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8633 current_class = TREE_TYPE (current);
8634 if (ANONYMOUS_CLASS_P (current_class))
8636 tree d;
8637 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8639 if (DECL_CONSTRUCTOR_P (d))
8641 restore_line_number_status (1);
8642 reset_method_name (d);
8643 java_complete_expand_method (d);
8644 restore_line_number_status (0);
8645 break; /* We now there are no other ones */
8652 /* If we've found error at that stage, don't try to generate
8653 anything, unless we're emitting xrefs or checking the syntax only
8654 (but not using -fsyntax-only for the purpose of generating
8655 bytecode. */
8656 if (java_error_count && !flag_emit_xref
8657 && (!flag_syntax_only && !flag_emit_class_files))
8658 return;
8660 /* Now things are stable, go for generation of the class data. */
8661 for (ctxp_for_generation = saved_ctxp;
8662 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8664 tree current;
8665 ctxp = ctxp_for_generation;
8666 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8668 current_class = TREE_TYPE (current);
8669 outgoing_cpool = TYPE_CPOOL (current_class);
8670 if (flag_emit_class_files)
8671 write_classfile (current_class);
8672 if (flag_emit_xref)
8673 expand_xref (current_class);
8674 else if (! flag_syntax_only)
8675 finish_class ();
8680 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8681 a tree list node containing RIGHT. Fore coming RIGHTs will be
8682 chained to this hook. LOCATION contains the location of the
8683 separating `.' operator. */
8685 static tree
8686 make_qualified_primary (primary, right, location)
8687 tree primary, right;
8688 int location;
8690 tree wfl;
8692 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8693 wfl = build_wfl_wrap (primary, location);
8694 else
8696 wfl = primary;
8697 /* If wfl wasn't qualified, we build a first anchor */
8698 if (!EXPR_WFL_QUALIFICATION (wfl))
8699 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8702 /* And chain them */
8703 EXPR_WFL_LINECOL (right) = location;
8704 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8705 PRIMARY_P (wfl) = 1;
8706 return wfl;
8709 /* Simple merge of two name separated by a `.' */
8711 static tree
8712 merge_qualified_name (left, right)
8713 tree left, right;
8715 tree node;
8716 if (!left && !right)
8717 return NULL_TREE;
8719 if (!left)
8720 return right;
8722 if (!right)
8723 return left;
8725 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8726 IDENTIFIER_LENGTH (left));
8727 obstack_1grow (&temporary_obstack, '.');
8728 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8729 IDENTIFIER_LENGTH (right));
8730 node = get_identifier (obstack_base (&temporary_obstack));
8731 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8732 QUALIFIED_P (node) = 1;
8733 return node;
8736 /* Merge the two parts of a qualified name into LEFT. Set the
8737 location information of the resulting node to LOCATION, usually
8738 inherited from the location information of the `.' operator. */
8740 static tree
8741 make_qualified_name (left, right, location)
8742 tree left, right;
8743 int location;
8745 #ifdef USE_COMPONENT_REF
8746 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8747 EXPR_WFL_LINECOL (node) = location;
8748 return node;
8749 #else
8750 tree left_id = EXPR_WFL_NODE (left);
8751 tree right_id = EXPR_WFL_NODE (right);
8752 tree wfl, merge;
8754 merge = merge_qualified_name (left_id, right_id);
8756 /* Left wasn't qualified and is now qualified */
8757 if (!QUALIFIED_P (left_id))
8759 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8760 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8761 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8764 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8765 EXPR_WFL_LINECOL (wfl) = location;
8766 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8768 EXPR_WFL_NODE (left) = merge;
8769 return left;
8770 #endif
8773 /* Extract the last identifier component of the qualified in WFL. The
8774 last identifier is removed from the linked list */
8776 static tree
8777 cut_identifier_in_qualified (wfl)
8778 tree wfl;
8780 tree q;
8781 tree previous = NULL_TREE;
8782 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8783 if (!TREE_CHAIN (q))
8785 if (!previous)
8786 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
8787 TREE_CHAIN (previous) = NULL_TREE;
8788 return TREE_PURPOSE (q);
8792 /* Resolve the expression name NAME. Return its decl. */
8794 static tree
8795 resolve_expression_name (id, orig)
8796 tree id;
8797 tree *orig;
8799 tree name = EXPR_WFL_NODE (id);
8800 tree decl;
8802 /* 6.5.5.1: Simple expression names */
8803 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8805 /* 15.13.1: NAME can appear within the scope of a local variable
8806 declaration */
8807 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8808 return decl;
8810 /* 15.13.1: NAME can appear within a class declaration */
8811 else
8813 decl = lookup_field_wrapper (current_class, name);
8814 if (decl)
8816 tree access = NULL_TREE;
8817 int fs = FIELD_STATIC (decl);
8819 /* If we're accessing an outer scope local alias, make
8820 sure we change the name of the field we're going to
8821 build access to. */
8822 if (FIELD_LOCAL_ALIAS_USED (decl))
8823 name = DECL_NAME (decl);
8825 /* Instance variable (8.3.1.1) can't appear within
8826 static method, static initializer or initializer for
8827 a static variable. */
8828 if (!fs && METHOD_STATIC (current_function_decl))
8830 static_ref_err (id, name, current_class);
8831 return error_mark_node;
8833 /* Instance variables can't appear as an argument of
8834 an explicit constructor invocation */
8835 if (!fs && ctxp->explicit_constructor_p)
8837 parse_error_context
8838 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
8839 return error_mark_node;
8842 /* If we're processing an inner class and we're trying
8843 to access a field belonging to an outer class, build
8844 the access to the field */
8845 if (!fs && outer_field_access_p (current_class, decl))
8846 return build_outer_field_access (id, decl);
8848 /* Otherwise build what it takes to access the field */
8849 access = build_field_ref ((fs ? NULL_TREE : current_this),
8850 DECL_CONTEXT (decl), name);
8851 if (fs && !flag_emit_class_files && !flag_emit_xref)
8852 access = build_class_init (DECL_CONTEXT (access), access);
8853 /* We may be asked to save the real field access node */
8854 if (orig)
8855 *orig = access;
8856 /* And we return what we got */
8857 return access;
8859 /* Fall down to error report on undefined variable */
8862 /* 6.5.5.2 Qualified Expression Names */
8863 else
8865 if (orig)
8866 *orig = NULL_TREE;
8867 qualify_ambiguous_name (id);
8868 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8869 /* 15.10.2: Accessing Superclass Members using super */
8870 return resolve_field_access (id, orig, NULL);
8873 /* We've got an error here */
8874 parse_error_context (id, "Undefined variable `%s'",
8875 IDENTIFIER_POINTER (name));
8877 return error_mark_node;
8880 static void
8881 static_ref_err (wfl, field_id, class_type)
8882 tree wfl, field_id, class_type;
8884 parse_error_context
8885 (wfl,
8886 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8887 IDENTIFIER_POINTER (field_id),
8888 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8891 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8892 We return something suitable to generate the field access. We also
8893 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8894 recipient's address can be null. */
8896 static tree
8897 resolve_field_access (qual_wfl, field_decl, field_type)
8898 tree qual_wfl;
8899 tree *field_decl, *field_type;
8901 int is_static = 0;
8902 tree field_ref;
8903 tree decl, where_found, type_found;
8905 if (resolve_qualified_expression_name (qual_wfl, &decl,
8906 &where_found, &type_found))
8907 return error_mark_node;
8909 /* Resolve the LENGTH field of an array here */
8910 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
8911 && type_found && TYPE_ARRAY_P (type_found)
8912 && ! flag_emit_class_files && ! flag_emit_xref)
8914 tree length = build_java_array_length_access (where_found);
8915 field_ref =
8916 build_java_arraynull_check (type_found, length, int_type_node);
8918 /* In case we're dealing with a static array, we need to
8919 initialize its class before the array length can be fetched.
8920 It's also a good time to create a DECL_RTL for the field if
8921 none already exists, otherwise if the field was declared in a
8922 class found in an external file and hasn't been (and won't
8923 be) accessed for its value, none will be created. */
8924 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8926 build_static_field_ref (where_found);
8927 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8930 /* We might have been trying to resolve field.method(). In which
8931 case, the resolution is over and decl is the answer */
8932 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
8933 field_ref = decl;
8934 else if (JDECL_P (decl))
8936 int static_final_found = 0;
8937 if (!type_found)
8938 type_found = DECL_CONTEXT (decl);
8939 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
8940 if (FIELD_FINAL (decl) && FIELD_STATIC (decl)
8941 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
8942 && DECL_INITIAL (decl))
8944 /* When called on a FIELD_DECL of the right (primitive)
8945 type, java_complete_tree will try to substitue the decl
8946 for it's initial value. */
8947 field_ref = java_complete_tree (decl);
8948 static_final_found = 1;
8950 else
8951 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8952 NULL_TREE : where_found),
8953 type_found, DECL_NAME (decl));
8954 if (field_ref == error_mark_node)
8955 return error_mark_node;
8956 if (is_static && !static_final_found
8957 && !flag_emit_class_files && !flag_emit_xref)
8958 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
8960 else
8961 field_ref = decl;
8963 if (field_decl)
8964 *field_decl = decl;
8965 if (field_type)
8966 *field_type = (QUAL_DECL_TYPE (decl) ?
8967 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
8968 return field_ref;
8971 /* If NODE is an access to f static field, strip out the class
8972 initialization part and return the field decl, otherwise, return
8973 NODE. */
8975 static tree
8976 strip_out_static_field_access_decl (node)
8977 tree node;
8979 if (TREE_CODE (node) == COMPOUND_EXPR)
8981 tree op1 = TREE_OPERAND (node, 1);
8982 if (TREE_CODE (op1) == COMPOUND_EXPR)
8984 tree call = TREE_OPERAND (op1, 0);
8985 if (TREE_CODE (call) == CALL_EXPR
8986 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8987 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8988 == soft_initclass_node)
8989 return TREE_OPERAND (op1, 1);
8991 else if (JDECL_P (op1))
8992 return op1;
8994 return node;
8997 /* 6.5.5.2: Qualified Expression Names */
8999 static int
9000 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9001 tree wfl;
9002 tree *found_decl, *type_found, *where_found;
9004 int from_type = 0; /* Field search initiated from a type */
9005 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9006 int previous_call_static = 0;
9007 int is_static;
9008 tree decl = NULL_TREE, type = NULL_TREE, q;
9009 /* For certain for of inner class instantiation */
9010 tree saved_current, saved_this;
9011 #define RESTORE_THIS_AND_CURRENT_CLASS \
9012 { current_class = saved_current; current_this = saved_this;}
9014 *type_found = *where_found = NULL_TREE;
9016 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9018 tree qual_wfl = QUAL_WFL (q);
9019 tree ret_decl; /* for EH checking */
9020 int location; /* for EH checking */
9022 /* 15.10.1 Field Access Using a Primary */
9023 switch (TREE_CODE (qual_wfl))
9025 case CALL_EXPR:
9026 case NEW_CLASS_EXPR:
9027 /* If the access to the function call is a non static field,
9028 build the code to access it. */
9029 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9031 decl = maybe_access_field (decl, *where_found,
9032 DECL_CONTEXT (decl));
9033 if (decl == error_mark_node)
9034 return 1;
9037 /* And code for the function call */
9038 if (complete_function_arguments (qual_wfl))
9039 return 1;
9041 /* We might have to setup a new current class and a new this
9042 for the search of an inner class, relative to the type of
9043 a expression resolved as `decl'. The current values are
9044 saved and restored shortly after */
9045 saved_current = current_class;
9046 saved_this = current_this;
9047 if (decl
9048 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9049 || from_qualified_this))
9051 /* If we still have `from_qualified_this', we have the form
9052 <T>.this.f() and we need to build <T>.this */
9053 if (from_qualified_this)
9055 decl = build_access_to_thisn (current_class, type, 0);
9056 decl = java_complete_tree (decl);
9057 type = TREE_TYPE (TREE_TYPE (decl));
9059 current_class = type;
9060 current_this = decl;
9061 from_qualified_this = 0;
9064 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9065 CALL_USING_SUPER (qual_wfl) = 1;
9066 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9067 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9068 *where_found = patch_method_invocation (qual_wfl, decl, type,
9069 &is_static, &ret_decl);
9070 if (*where_found == error_mark_node)
9072 RESTORE_THIS_AND_CURRENT_CLASS;
9073 return 1;
9075 *type_found = type = QUAL_DECL_TYPE (*where_found);
9077 /* If we're creating an inner class instance, check for that
9078 an enclosing instance is in scope */
9079 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9080 && INNER_ENCLOSING_SCOPE_CHECK (type))
9082 parse_error_context
9083 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9084 lang_printable_name (type, 0),
9085 (!current_this ? "" :
9086 "; an explicit one must be provided when creating this inner class"));
9087 RESTORE_THIS_AND_CURRENT_CLASS;
9088 return 1;
9091 /* In case we had to change then to resolve a inner class
9092 instantiation using a primary qualified by a `new' */
9093 RESTORE_THIS_AND_CURRENT_CLASS;
9095 /* EH check. No check on access$<n> functions */
9096 if (location
9097 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9098 (DECL_NAME (current_function_decl)))
9099 check_thrown_exceptions (location, ret_decl);
9101 /* If the previous call was static and this one is too,
9102 build a compound expression to hold the two (because in
9103 that case, previous function calls aren't transported as
9104 forcoming function's argument. */
9105 if (previous_call_static && is_static)
9107 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9108 TREE_SIDE_EFFECTS (decl) = 1;
9110 else
9112 previous_call_static = is_static;
9113 decl = *where_found;
9115 from_type = 0;
9116 continue;
9118 case NEW_ARRAY_EXPR:
9119 case NEW_ANONYMOUS_ARRAY_EXPR:
9120 *where_found = decl = java_complete_tree (qual_wfl);
9121 if (decl == error_mark_node)
9122 return 1;
9123 *type_found = type = QUAL_DECL_TYPE (decl);
9124 CLASS_LOADED_P (type) = 1;
9125 continue;
9127 case CONVERT_EXPR:
9128 *where_found = decl = java_complete_tree (qual_wfl);
9129 if (decl == error_mark_node)
9130 return 1;
9131 *type_found = type = QUAL_DECL_TYPE (decl);
9132 from_cast = 1;
9133 continue;
9135 case CONDITIONAL_EXPR:
9136 case STRING_CST:
9137 case MODIFY_EXPR:
9138 *where_found = decl = java_complete_tree (qual_wfl);
9139 if (decl == error_mark_node)
9140 return 1;
9141 *type_found = type = QUAL_DECL_TYPE (decl);
9142 continue;
9144 case ARRAY_REF:
9145 /* If the access to the function call is a non static field,
9146 build the code to access it. */
9147 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9149 decl = maybe_access_field (decl, *where_found, type);
9150 if (decl == error_mark_node)
9151 return 1;
9153 /* And code for the array reference expression */
9154 decl = java_complete_tree (qual_wfl);
9155 if (decl == error_mark_node)
9156 return 1;
9157 type = QUAL_DECL_TYPE (decl);
9158 continue;
9160 case PLUS_EXPR:
9161 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9162 return 1;
9163 if ((type = patch_string (decl)))
9164 decl = type;
9165 *where_found = QUAL_RESOLUTION (q) = decl;
9166 *type_found = type = TREE_TYPE (decl);
9167 break;
9169 case CLASS_LITERAL:
9170 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9171 return 1;
9172 *where_found = QUAL_RESOLUTION (q) = decl;
9173 *type_found = type = TREE_TYPE (decl);
9174 break;
9176 default:
9177 /* Fix for -Wall Just go to the next statement. Don't
9178 continue */
9179 break;
9182 /* If we fall here, we weren't processing a (static) function call. */
9183 previous_call_static = 0;
9185 /* It can be the keyword THIS */
9186 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9188 if (!current_this)
9190 parse_error_context
9191 (wfl, "Keyword `this' used outside allowed context");
9192 return 1;
9194 if (ctxp->explicit_constructor_p)
9196 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9197 return 1;
9199 /* We have to generate code for intermediate acess */
9200 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9202 *where_found = decl = current_this;
9203 *type_found = type = QUAL_DECL_TYPE (decl);
9205 /* We're trying to access the this from somewhere else. Make sure
9206 it's allowed before doing so. */
9207 else
9209 if (!enclosing_context_p (type, current_class))
9211 char *p = xstrdup (lang_printable_name (type, 0));
9212 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9213 p, p,
9214 lang_printable_name (current_class, 0));
9215 free (p);
9216 return 1;
9218 from_qualified_this = 1;
9219 /* If there's nothing else after that, we need to
9220 produce something now, otherwise, the section of the
9221 code that needs to produce <T>.this will generate
9222 what is necessary. */
9223 if (!TREE_CHAIN (q))
9225 decl = build_access_to_thisn (current_class, type, 0);
9226 *where_found = decl = java_complete_tree (decl);
9227 *type_found = type = TREE_TYPE (decl);
9231 from_type = 0;
9232 continue;
9235 /* 15.10.2 Accessing Superclass Members using SUPER */
9236 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9238 tree node;
9239 /* Check on the restricted use of SUPER */
9240 if (METHOD_STATIC (current_function_decl)
9241 || current_class == object_type_node)
9243 parse_error_context
9244 (wfl, "Keyword `super' used outside allowed context");
9245 return 1;
9247 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9248 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9249 CLASSTYPE_SUPER (current_class),
9250 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9251 *where_found = decl = java_complete_tree (node);
9252 if (decl == error_mark_node)
9253 return 1;
9254 *type_found = type = QUAL_DECL_TYPE (decl);
9255 from_super = from_type = 1;
9256 continue;
9259 /* 15.13.1: Can't search for field name in packages, so we
9260 assume a variable/class name was meant. */
9261 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9263 tree name = resolve_package (wfl, &q);
9264 if (name)
9266 tree list;
9267 *where_found = decl = resolve_no_layout (name, qual_wfl);
9268 /* We want to be absolutely sure that the class is laid
9269 out. We're going to search something inside it. */
9270 *type_found = type = TREE_TYPE (decl);
9271 layout_class (type);
9272 from_type = 1;
9274 /* Fix them all the way down, if any are left. */
9275 if (q)
9277 list = TREE_CHAIN (q);
9278 while (list)
9280 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9281 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9282 list = TREE_CHAIN (list);
9286 else
9288 if (from_super || from_cast)
9289 parse_error_context
9290 ((from_cast ? qual_wfl : wfl),
9291 "No variable `%s' defined in class `%s'",
9292 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9293 lang_printable_name (type, 0));
9294 else
9295 parse_error_context
9296 (qual_wfl, "Undefined variable or class name: `%s'",
9297 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9298 return 1;
9302 /* We have a type name. It's been already resolved when the
9303 expression was qualified. */
9304 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9306 if (!(decl = QUAL_RESOLUTION (q)))
9307 return 1; /* Error reported already */
9309 /* Sneak preview. If next we see a `new', we're facing a
9310 qualification with resulted in a type being selected
9311 instead of a field. Report the error */
9312 if(TREE_CHAIN (q)
9313 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9315 parse_error_context (qual_wfl, "Undefined variable `%s'",
9316 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9317 return 1;
9320 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9322 parse_error_context
9323 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9324 java_accstring_lookup (get_access_flags_from_decl (decl)),
9325 GET_TYPE_NAME (type),
9326 IDENTIFIER_POINTER (DECL_NAME (decl)),
9327 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9328 return 1;
9330 check_deprecation (qual_wfl, decl);
9332 type = TREE_TYPE (decl);
9333 from_type = 1;
9335 /* We resolve and expression name */
9336 else
9338 tree field_decl = NULL_TREE;
9340 /* If there exists an early resolution, use it. That occurs
9341 only once and we know that there are more things to
9342 come. Don't do that when processing something after SUPER
9343 (we need more thing to be put in place below */
9344 if (!from_super && QUAL_RESOLUTION (q))
9346 decl = QUAL_RESOLUTION (q);
9347 if (!type)
9349 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9351 if (current_this)
9352 *where_found = current_this;
9353 else
9355 static_ref_err (qual_wfl, DECL_NAME (decl),
9356 current_class);
9357 return 1;
9359 if (outer_field_access_p (current_class, decl))
9360 decl = build_outer_field_access (qual_wfl, decl);
9362 else
9364 *where_found = TREE_TYPE (decl);
9365 if (TREE_CODE (*where_found) == POINTER_TYPE)
9366 *where_found = TREE_TYPE (*where_found);
9371 /* We have to search for a field, knowing the type of its
9372 container. The flag FROM_TYPE indicates that we resolved
9373 the last member of the expression as a type name, which
9374 means that for the resolution of this field, we'll look
9375 for other errors than if it was resolved as a member of
9376 an other field. */
9377 else
9379 int is_static;
9380 tree field_decl_type; /* For layout */
9382 if (!from_type && !JREFERENCE_TYPE_P (type))
9384 parse_error_context
9385 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9386 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9387 lang_printable_name (type, 0),
9388 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9389 return 1;
9392 field_decl = lookup_field_wrapper (type,
9393 EXPR_WFL_NODE (qual_wfl));
9395 /* Maybe what we're trying to access an inner class. */
9396 if (!field_decl)
9398 tree ptr, inner_decl;
9400 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9401 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9402 if (inner_decl)
9404 check_inner_class_access (inner_decl, decl, qual_wfl);
9405 type = TREE_TYPE (inner_decl);
9406 decl = inner_decl;
9407 from_type = 1;
9408 continue;
9412 if (field_decl == NULL_TREE)
9414 parse_error_context
9415 (qual_wfl, "No variable `%s' defined in type `%s'",
9416 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9417 GET_TYPE_NAME (type));
9418 return 1;
9420 if (field_decl == error_mark_node)
9421 return 1;
9423 /* Layout the type of field_decl, since we may need
9424 it. Don't do primitive types or loaded classes. The
9425 situation of non primitive arrays may not handled
9426 properly here. FIXME */
9427 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9428 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9429 else
9430 field_decl_type = TREE_TYPE (field_decl);
9431 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9432 && !CLASS_LOADED_P (field_decl_type)
9433 && !TYPE_ARRAY_P (field_decl_type))
9434 resolve_and_layout (field_decl_type, NULL_TREE);
9435 if (TYPE_ARRAY_P (field_decl_type))
9436 CLASS_LOADED_P (field_decl_type) = 1;
9438 /* Check on accessibility here */
9439 if (not_accessible_p (type, field_decl, from_super))
9441 parse_error_context
9442 (qual_wfl,
9443 "Can't access %s field `%s.%s' from `%s'",
9444 java_accstring_lookup
9445 (get_access_flags_from_decl (field_decl)),
9446 GET_TYPE_NAME (type),
9447 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9448 IDENTIFIER_POINTER
9449 (DECL_NAME (TYPE_NAME (current_class))));
9450 return 1;
9452 check_deprecation (qual_wfl, field_decl);
9454 /* There are things to check when fields are accessed
9455 from type. There are no restrictions on a static
9456 declaration of the field when it is accessed from an
9457 interface */
9458 is_static = FIELD_STATIC (field_decl);
9459 if (!from_super && from_type
9460 && !TYPE_INTERFACE_P (type)
9461 && !is_static
9462 && (current_function_decl
9463 && METHOD_STATIC (current_function_decl)))
9465 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9466 return 1;
9468 from_cast = from_super = 0;
9470 /* It's an access from a type but it isn't static, we
9471 make it relative to `this'. */
9472 if (!is_static && from_type)
9473 decl = current_this;
9475 /* If we need to generate something to get a proper
9476 handle on what this field is accessed from, do it
9477 now. */
9478 if (!is_static)
9480 decl = maybe_access_field (decl, *where_found, *type_found);
9481 if (decl == error_mark_node)
9482 return 1;
9485 /* We want to keep the location were found it, and the type
9486 we found. */
9487 *where_found = decl;
9488 *type_found = type;
9490 /* Generate the correct expression for field access from
9491 qualified this */
9492 if (from_qualified_this)
9494 field_decl = build_outer_field_access (qual_wfl, field_decl);
9495 from_qualified_this = 0;
9498 /* This is the decl found and eventually the next one to
9499 search from */
9500 decl = field_decl;
9502 from_type = 0;
9503 type = QUAL_DECL_TYPE (decl);
9505 /* Sneak preview. If decl is qualified by a `new', report
9506 the error here to be accurate on the peculiar construct */
9507 if (TREE_CHAIN (q)
9508 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9509 && !JREFERENCE_TYPE_P (type))
9511 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9512 lang_printable_name (type, 0));
9513 return 1;
9516 /* `q' might have changed due to a after package resolution
9517 re-qualification */
9518 if (!q)
9519 break;
9521 *found_decl = decl;
9522 return 0;
9525 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9526 can't be accessed from REFERENCE (a record type). This should be
9527 used when decl is a field or a method.*/
9529 static int
9530 not_accessible_p (reference, member, from_super)
9531 tree reference, member;
9532 int from_super;
9534 int access_flag = get_access_flags_from_decl (member);
9536 /* Inner classes are processed by check_inner_class_access */
9537 if (INNER_CLASS_TYPE_P (reference))
9538 return 0;
9540 /* Access always granted for members declared public */
9541 if (access_flag & ACC_PUBLIC)
9542 return 0;
9544 /* Check access on protected members */
9545 if (access_flag & ACC_PROTECTED)
9547 /* Access granted if it occurs from within the package
9548 containing the class in which the protected member is
9549 declared */
9550 if (class_in_current_package (DECL_CONTEXT (member)))
9551 return 0;
9553 /* If accessed with the form `super.member', then access is granted */
9554 if (from_super)
9555 return 0;
9557 /* Otherwise, access is granted if occuring from the class where
9558 member is declared or a subclass of it. Find the right
9559 context to perform the check */
9560 if (PURE_INNER_CLASS_TYPE_P (reference))
9562 while (INNER_CLASS_TYPE_P (reference))
9564 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9565 return 0;
9566 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9569 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9570 return 0;
9571 return 1;
9574 /* Check access on private members. Access is granted only if it
9575 occurs from within the class in which it is declared. Exceptions
9576 are accesses from inner-classes. */
9577 if (access_flag & ACC_PRIVATE)
9578 return (current_class == DECL_CONTEXT (member) ? 0 :
9579 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
9581 /* Default access are permitted only when occuring within the
9582 package in which the type (REFERENCE) is declared. In other words,
9583 REFERENCE is defined in the current package */
9584 if (ctxp->package)
9585 return !class_in_current_package (reference);
9587 /* Otherwise, access is granted */
9588 return 0;
9591 /* Test deprecated decl access. */
9592 static void
9593 check_deprecation (wfl, decl)
9594 tree wfl, decl;
9596 const char *file = DECL_SOURCE_FILE (decl);
9597 /* Complain if the field is deprecated and the file it was defined
9598 in isn't compiled at the same time the file which contains its
9599 use is */
9600 if (DECL_DEPRECATED (decl)
9601 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9603 char the [20];
9604 switch (TREE_CODE (decl))
9606 case FUNCTION_DECL:
9607 strcpy (the, "method");
9608 break;
9609 case FIELD_DECL:
9610 strcpy (the, "field");
9611 break;
9612 case TYPE_DECL:
9613 strcpy (the, "class");
9614 break;
9615 default:
9616 fatal ("unexpected DECL code - check_deprecation");
9618 parse_warning_context
9619 (wfl, "The %s `%s' in class `%s' has been deprecated",
9620 the, lang_printable_name (decl, 0),
9621 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9625 /* Returns 1 if class was declared in the current package, 0 otherwise */
9627 static int
9628 class_in_current_package (class)
9629 tree class;
9631 static tree cache = NULL_TREE;
9632 int qualified_flag;
9633 tree left;
9635 if (cache == class)
9636 return 1;
9638 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9640 /* If the current package is empty and the name of CLASS is
9641 qualified, class isn't in the current package. If there is a
9642 current package and the name of the CLASS is not qualified, class
9643 isn't in the current package */
9644 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9645 return 0;
9647 /* If there is not package and the name of CLASS isn't qualified,
9648 they belong to the same unnamed package */
9649 if (!ctxp->package && !qualified_flag)
9650 return 1;
9652 /* Compare the left part of the name of CLASS with the package name */
9653 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9654 if (ctxp->package == left)
9656 static int initialized_p;
9657 /* Register CACHE with the garbage collector. */
9658 if (!initialized_p)
9660 ggc_add_tree_root (&cache, 1);
9661 initialized_p = 1;
9664 cache = class;
9665 return 1;
9667 return 0;
9670 /* This function may generate code to access DECL from WHERE. This is
9671 done only if certain conditions meet. */
9673 static tree
9674 maybe_access_field (decl, where, type)
9675 tree decl, where, type;
9677 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9678 && !FIELD_STATIC (decl))
9679 decl = build_field_ref (where ? where : current_this,
9680 (type ? type : DECL_CONTEXT (decl)),
9681 DECL_NAME (decl));
9682 return decl;
9685 /* Build a method invocation, by patching PATCH. If non NULL
9686 and according to the situation, PRIMARY and WHERE may be
9687 used. IS_STATIC is set to 1 if the invoked function is static. */
9689 static tree
9690 patch_method_invocation (patch, primary, where, is_static, ret_decl)
9691 tree patch, primary, where;
9692 int *is_static;
9693 tree *ret_decl;
9695 tree wfl = TREE_OPERAND (patch, 0);
9696 tree args = TREE_OPERAND (patch, 1);
9697 tree name = EXPR_WFL_NODE (wfl);
9698 tree list;
9699 int is_static_flag = 0;
9700 int is_super_init = 0;
9701 tree this_arg = NULL_TREE;
9703 /* Should be overriden if everything goes well. Otherwise, if
9704 something fails, it should keep this value. It stop the
9705 evaluation of a bogus assignment. See java_complete_tree,
9706 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9707 evaluating an assignment */
9708 TREE_TYPE (patch) = error_mark_node;
9710 /* Since lookup functions are messing with line numbers, save the
9711 context now. */
9712 java_parser_context_save_global ();
9714 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9716 /* Resolution of qualified name, excluding constructors */
9717 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9719 tree identifier, identifier_wfl, type, resolved;
9720 /* Extract the last IDENTIFIER of the qualified
9721 expression. This is a wfl and we will use it's location
9722 data during error report. */
9723 identifier_wfl = cut_identifier_in_qualified (wfl);
9724 identifier = EXPR_WFL_NODE (identifier_wfl);
9726 /* Given the context, IDENTIFIER is syntactically qualified
9727 as a MethodName. We need to qualify what's before */
9728 qualify_ambiguous_name (wfl);
9729 resolved = resolve_field_access (wfl, NULL, NULL);
9731 if (resolved == error_mark_node)
9732 PATCH_METHOD_RETURN_ERROR ();
9734 type = GET_SKIP_TYPE (resolved);
9735 resolve_and_layout (type, NULL_TREE);
9737 if (JPRIMITIVE_TYPE_P (type))
9739 parse_error_context
9740 (identifier_wfl,
9741 "Can't invoke a method on primitive type `%s'",
9742 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9743 PATCH_METHOD_RETURN_ERROR ();
9746 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9747 args = nreverse (args);
9749 /* We're resolving a call from a type */
9750 if (TREE_CODE (resolved) == TYPE_DECL)
9752 if (CLASS_INTERFACE (resolved))
9754 parse_error_context
9755 (identifier_wfl,
9756 "Can't make static reference to method `%s' in interface `%s'",
9757 IDENTIFIER_POINTER (identifier),
9758 IDENTIFIER_POINTER (name));
9759 PATCH_METHOD_RETURN_ERROR ();
9761 if (list && !METHOD_STATIC (list))
9763 char *fct_name = xstrdup (lang_printable_name (list, 0));
9764 parse_error_context
9765 (identifier_wfl,
9766 "Can't make static reference to method `%s %s' in class `%s'",
9767 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9768 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9769 free (fct_name);
9770 PATCH_METHOD_RETURN_ERROR ();
9773 else
9774 this_arg = primary = resolved;
9776 /* IDENTIFIER_WFL will be used to report any problem further */
9777 wfl = identifier_wfl;
9779 /* Resolution of simple names, names generated after a primary: or
9780 constructors */
9781 else
9783 tree class_to_search = NULL_TREE;
9784 int lc; /* Looking for Constructor */
9786 /* We search constructor in their target class */
9787 if (CALL_CONSTRUCTOR_P (patch))
9789 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9790 class_to_search = EXPR_WFL_NODE (wfl);
9791 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9792 this_identifier_node)
9793 class_to_search = NULL_TREE;
9794 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9795 super_identifier_node)
9797 is_super_init = 1;
9798 if (CLASSTYPE_SUPER (current_class))
9799 class_to_search =
9800 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9801 else
9803 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
9804 PATCH_METHOD_RETURN_ERROR ();
9808 /* Class to search is NULL if we're searching the current one */
9809 if (class_to_search)
9811 class_to_search = resolve_and_layout (class_to_search, wfl);
9813 if (!class_to_search)
9815 parse_error_context
9816 (wfl, "Class `%s' not found in type declaration",
9817 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9818 PATCH_METHOD_RETURN_ERROR ();
9821 /* Can't instantiate an abstract class, but we can
9822 invoke it's constructor. It's use within the `new'
9823 context is denied here. */
9824 if (CLASS_ABSTRACT (class_to_search)
9825 && TREE_CODE (patch) == NEW_CLASS_EXPR)
9827 parse_error_context
9828 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9829 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9830 PATCH_METHOD_RETURN_ERROR ();
9833 class_to_search = TREE_TYPE (class_to_search);
9835 else
9836 class_to_search = current_class;
9837 lc = 1;
9839 /* This is a regular search in the local class, unless an
9840 alternate class is specified. */
9841 else
9843 class_to_search = (where ? where : current_class);
9844 lc = 0;
9847 /* NAME is a simple identifier or comes from a primary. Search
9848 in the class whose declaration contain the method being
9849 invoked. */
9850 resolve_and_layout (class_to_search, NULL_TREE);
9852 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
9853 /* Don't continue if no method were found, as the next statement
9854 can't be executed then. */
9855 if (!list)
9856 PATCH_METHOD_RETURN_ERROR ();
9858 /* Check for static reference if non static methods */
9859 if (check_for_static_method_reference (wfl, patch, list,
9860 class_to_search, primary))
9861 PATCH_METHOD_RETURN_ERROR ();
9863 /* Check for inner classes creation from illegal contexts */
9864 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9865 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9866 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9868 parse_error_context
9869 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9870 lang_printable_name (class_to_search, 0),
9871 (!current_this ? "" :
9872 "; an explicit one must be provided when creating this inner class"));
9873 PATCH_METHOD_RETURN_ERROR ();
9876 /* Non static methods are called with the current object extra
9877 argument. If patch a `new TYPE()', the argument is the value
9878 returned by the object allocator. If method is resolved as a
9879 primary, use the primary otherwise use the current THIS. */
9880 args = nreverse (args);
9881 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
9883 this_arg = primary ? primary : current_this;
9885 /* If we're using an access method, things are different.
9886 There are two familly of cases:
9888 1) We're not generating bytecodes:
9890 - LIST is non static. It's invocation is transformed from
9891 x(a1,...,an) into this$<n>.x(a1,....an).
9892 - LIST is static. It's invocation is transformed from
9893 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9895 2) We're generating bytecodes:
9897 - LIST is non static. It's invocation is transformed from
9898 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9899 - LIST is static. It's invocation is transformed from
9900 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9902 Of course, this$<n> can be abitrary complex, ranging from
9903 this$0 (the immediate outer context) to
9904 access$0(access$0(...(this$0))).
9906 maybe_use_access_method returns a non zero value if the
9907 this_arg has to be moved into the (then generated) stub
9908 argument list. In the meantime, the selected function
9909 might have be replaced by a generated stub. */
9910 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9911 args = tree_cons (NULL_TREE, this_arg, args);
9915 /* Merge point of all resolution schemes. If we have nothing, this
9916 is an error, already signaled */
9917 if (!list)
9918 PATCH_METHOD_RETURN_ERROR ();
9920 /* Check accessibility, position the is_static flag, build and
9921 return the call */
9922 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
9924 char *fct_name = xstrdup (lang_printable_name (list, 0));
9925 parse_error_context
9926 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9927 java_accstring_lookup (get_access_flags_from_decl (list)),
9928 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9929 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9930 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9931 free (fct_name);
9932 PATCH_METHOD_RETURN_ERROR ();
9934 check_deprecation (wfl, list);
9936 /* If invoking a innerclass constructor, there are hidden parameters
9937 to pass */
9938 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9939 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9941 /* And make sure we add the accessed local variables to be saved
9942 in field aliases. */
9943 args = build_alias_initializer_parameter_list
9944 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9946 /* Secretly pass the current_this/primary as a second argument */
9947 if (primary || current_this)
9948 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9949 else
9950 args = tree_cons (NULL_TREE, integer_zero_node, args);
9953 /* This handles the situation where a constructor invocation needs
9954 to have an enclosing context passed as a second parameter (the
9955 constructor is one of an inner class. We extract it from the
9956 current function. */
9957 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9959 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9960 tree extra_arg;
9962 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9964 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9965 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9967 else
9969 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9970 extra_arg =
9971 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9972 extra_arg = java_complete_tree (extra_arg);
9974 args = tree_cons (NULL_TREE, extra_arg, args);
9977 is_static_flag = METHOD_STATIC (list);
9978 if (! is_static_flag && this_arg != NULL_TREE)
9979 args = tree_cons (NULL_TREE, this_arg, args);
9981 /* In the context of an explicit constructor invocation, we can't
9982 invoke any method relying on `this'. Exceptions are: we're
9983 invoking a static function, primary exists and is not the current
9984 this, we're creating a new object. */
9985 if (ctxp->explicit_constructor_p
9986 && !is_static_flag
9987 && (!primary || primary == current_this)
9988 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
9990 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9991 PATCH_METHOD_RETURN_ERROR ();
9993 java_parser_context_restore_global ();
9994 if (is_static)
9995 *is_static = is_static_flag;
9996 /* Sometimes, we want the decl of the selected method. Such as for
9997 EH checking */
9998 if (ret_decl)
9999 *ret_decl = list;
10000 patch = patch_invoke (patch, list, args);
10001 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10003 tree finit_parms, finit_call;
10005 /* Prepare to pass hidden parameters to finit$, if any. */
10006 finit_parms = build_alias_initializer_parameter_list
10007 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10009 finit_call =
10010 build_method_invocation (build_wfl_node (finit_identifier_node),
10011 finit_parms);
10013 /* Generate the code used to initialize fields declared with an
10014 initialization statement and build a compound statement along
10015 with the super constructor invocation. */
10016 patch = build (COMPOUND_EXPR, void_type_node, patch,
10017 java_complete_tree (finit_call));
10018 CAN_COMPLETE_NORMALLY (patch) = 1;
10020 return patch;
10023 /* Check that we're not trying to do a static reference to a method in
10024 non static method. Return 1 if it's the case, 0 otherwise. */
10026 static int
10027 check_for_static_method_reference (wfl, node, method, where, primary)
10028 tree wfl, node, method, where, primary;
10030 if (METHOD_STATIC (current_function_decl)
10031 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10033 char *fct_name = xstrdup (lang_printable_name (method, 0));
10034 parse_error_context
10035 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10036 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10037 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10038 free (fct_name);
10039 return 1;
10041 return 0;
10044 /* Fix the invocation of *MDECL if necessary in the case of a
10045 invocation from an inner class. *THIS_ARG might be modified
10046 appropriately and an alternative access to *MDECL might be
10047 returned. */
10049 static int
10050 maybe_use_access_method (is_super_init, mdecl, this_arg)
10051 int is_super_init;
10052 tree *mdecl, *this_arg;
10054 tree ctx;
10055 tree md = *mdecl, ta = *this_arg;
10056 int to_return = 0;
10057 int non_static_context = !METHOD_STATIC (md);
10059 if (is_super_init
10060 || DECL_CONTEXT (md) == current_class
10061 || !PURE_INNER_CLASS_TYPE_P (current_class)
10062 || DECL_FINIT_P (md))
10063 return 0;
10065 /* If we're calling a method found in an enclosing class, generate
10066 what it takes to retrieve the right this. Don't do that if we're
10067 invoking a static method. */
10069 if (non_static_context)
10071 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10072 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10074 ta = build_current_thisn (current_class);
10075 ta = build_wfl_node (ta);
10077 else
10079 tree type = ctx;
10080 while (type)
10082 maybe_build_thisn_access_method (type);
10083 if (inherits_from_p (type, DECL_CONTEXT (md)))
10085 ta = build_access_to_thisn (ctx, type, 0);
10086 break;
10088 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10089 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10092 ta = java_complete_tree (ta);
10095 /* We might have to use an access method to get to MD. We can
10096 break the method access rule as far as we're not generating
10097 bytecode */
10098 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10100 md = build_outer_method_access_method (md);
10101 to_return = 1;
10104 *mdecl = md;
10105 *this_arg = ta;
10107 /* Returnin a non zero value indicates we were doing a non static
10108 method invokation that is now a static invocation. It will have
10109 callee displace `this' to insert it in the regular argument
10110 list. */
10111 return (non_static_context && to_return);
10114 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10115 mode. */
10117 static tree
10118 patch_invoke (patch, method, args)
10119 tree patch, method, args;
10121 tree dtable, func;
10122 tree original_call, t, ta;
10123 tree cond = NULL_TREE;
10125 /* Last step for args: convert build-in types. If we're dealing with
10126 a new TYPE() type call, the first argument to the constructor
10127 isn't found in the incoming argument list, but delivered by
10128 `new' */
10129 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10130 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10131 t = TREE_CHAIN (t);
10132 for (ta = args; t != end_params_node && ta;
10133 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10134 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10135 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10136 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10138 /* Resolve unresolved returned type isses */
10139 t = TREE_TYPE (TREE_TYPE (method));
10140 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10141 resolve_and_layout (TREE_TYPE (t), NULL);
10143 if (flag_emit_class_files || flag_emit_xref)
10144 func = method;
10145 else
10147 tree signature = build_java_signature (TREE_TYPE (method));
10148 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10150 case INVOKE_VIRTUAL:
10151 dtable = invoke_build_dtable (0, args);
10152 func = build_invokevirtual (dtable, method);
10153 break;
10155 case INVOKE_NONVIRTUAL:
10156 /* If the object for the method call is null, we throw an
10157 exception. We don't do this if the object is the current
10158 method's `this'. In other cases we just rely on an
10159 optimization pass to eliminate redundant checks. */
10160 if (TREE_VALUE (args) != current_this)
10162 /* We use a SAVE_EXPR here to make sure we only evaluate
10163 the new `self' expression once. */
10164 tree save_arg = save_expr (TREE_VALUE (args));
10165 TREE_VALUE (args) = save_arg;
10166 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10167 null_pointer_node);
10169 /* Fall through. */
10171 case INVOKE_SUPER:
10172 case INVOKE_STATIC:
10173 func = build_known_method_ref (method, TREE_TYPE (method),
10174 DECL_CONTEXT (method),
10175 signature, args);
10176 break;
10178 case INVOKE_INTERFACE:
10179 dtable = invoke_build_dtable (1, args);
10180 func = build_invokeinterface (dtable, method);
10181 break;
10183 default:
10184 fatal ("internal error - unknown invocation_mode result");
10187 /* Ensure self_type is initialized, (invokestatic). FIXME */
10188 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10191 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10192 TREE_OPERAND (patch, 0) = func;
10193 TREE_OPERAND (patch, 1) = args;
10194 original_call = patch;
10196 /* We're processing a `new TYPE ()' form. New is called and its
10197 returned value is the first argument to the constructor. We build
10198 a COMPOUND_EXPR and use saved expression so that the overall NEW
10199 expression value is a pointer to a newly created and initialized
10200 class. */
10201 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10203 tree class = DECL_CONTEXT (method);
10204 tree c1, saved_new, size, new;
10205 if (flag_emit_class_files || flag_emit_xref)
10207 TREE_TYPE (patch) = build_pointer_type (class);
10208 return patch;
10210 if (!TYPE_SIZE (class))
10211 safe_layout_class (class);
10212 size = size_in_bytes (class);
10213 new = build (CALL_EXPR, promote_type (class),
10214 build_address_of (alloc_object_node),
10215 tree_cons (NULL_TREE, build_class_ref (class),
10216 build_tree_list (NULL_TREE,
10217 size_in_bytes (class))),
10218 NULL_TREE);
10219 saved_new = save_expr (new);
10220 c1 = build_tree_list (NULL_TREE, saved_new);
10221 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10222 TREE_OPERAND (original_call, 1) = c1;
10223 TREE_SET_CODE (original_call, CALL_EXPR);
10224 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10227 /* If COND is set, then we are building a check to see if the object
10228 is NULL. */
10229 if (cond != NULL_TREE)
10231 /* We have to make the `then' branch a compound expression to
10232 make the types turn out right. This seems bizarre. */
10233 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10234 build (COMPOUND_EXPR, TREE_TYPE (patch),
10235 build (CALL_EXPR, void_type_node,
10236 build_address_of (soft_nullpointer_node),
10237 NULL_TREE, NULL_TREE),
10238 (FLOAT_TYPE_P (TREE_TYPE (patch))
10239 ? build_real (TREE_TYPE (patch), dconst0)
10240 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10241 integer_zero_node))),
10242 patch);
10243 TREE_SIDE_EFFECTS (patch) = 1;
10246 return patch;
10249 static int
10250 invocation_mode (method, super)
10251 tree method;
10252 int super;
10254 int access = get_access_flags_from_decl (method);
10256 if (super)
10257 return INVOKE_SUPER;
10259 if (access & ACC_STATIC)
10260 return INVOKE_STATIC;
10262 /* We have to look for a constructor before we handle nonvirtual
10263 calls; otherwise the constructor will look nonvirtual. */
10264 if (DECL_CONSTRUCTOR_P (method))
10265 return INVOKE_STATIC;
10267 if (access & ACC_FINAL || access & ACC_PRIVATE)
10268 return INVOKE_NONVIRTUAL;
10270 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10271 return INVOKE_NONVIRTUAL;
10273 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10274 return INVOKE_INTERFACE;
10276 return INVOKE_VIRTUAL;
10279 /* Retrieve a refined list of matching methods. It covers the step
10280 15.11.2 (Compile-Time Step 2) */
10282 static tree
10283 lookup_method_invoke (lc, cl, class, name, arg_list)
10284 int lc;
10285 tree cl;
10286 tree class, name, arg_list;
10288 tree atl = end_params_node; /* Arg Type List */
10289 tree method, signature, list, node;
10290 const char *candidates; /* Used for error report */
10291 char *dup;
10293 /* Fix the arguments */
10294 for (node = arg_list; node; node = TREE_CHAIN (node))
10296 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10297 /* Non primitive type may have to be resolved */
10298 if (!JPRIMITIVE_TYPE_P (current_arg))
10299 resolve_and_layout (current_arg, NULL_TREE);
10300 /* And promoted */
10301 if (TREE_CODE (current_arg) == RECORD_TYPE)
10302 current_arg = promote_type (current_arg);
10303 atl = tree_cons (NULL_TREE, current_arg, atl);
10306 /* Presto. If we're dealing with an anonymous class and a
10307 constructor call, generate the right constructor now, since we
10308 know the arguments' types. */
10310 if (lc && ANONYMOUS_CLASS_P (class))
10311 craft_constructor (TYPE_NAME (class), atl);
10313 /* Find all candidates and then refine the list, searching for the
10314 most specific method. */
10315 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10316 list = find_most_specific_methods_list (list);
10317 if (list && !TREE_CHAIN (list))
10318 return TREE_VALUE (list);
10320 /* Issue an error. List candidates if any. Candidates are listed
10321 only if accessible (non accessible methods may end-up here for
10322 the sake of a better error report). */
10323 candidates = NULL;
10324 if (list)
10326 tree current;
10327 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10328 for (current = list; current; current = TREE_CHAIN (current))
10330 tree cm = TREE_VALUE (current);
10331 char string [4096];
10332 if (!cm || not_accessible_p (class, cm, 0))
10333 continue;
10334 sprintf
10335 (string, " `%s' in `%s'%s",
10336 get_printable_method_name (cm),
10337 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10338 (TREE_CHAIN (current) ? "\n" : ""));
10339 obstack_grow (&temporary_obstack, string, strlen (string));
10341 obstack_1grow (&temporary_obstack, '\0');
10342 candidates = obstack_finish (&temporary_obstack);
10344 /* Issue the error message */
10345 method = make_node (FUNCTION_TYPE);
10346 TYPE_ARG_TYPES (method) = atl;
10347 signature = build_java_argument_signature (method);
10348 dup = xstrdup (lang_printable_name (class, 0));
10349 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10350 (lc ? "constructor" : "method"),
10351 (lc ? dup : IDENTIFIER_POINTER (name)),
10352 IDENTIFIER_POINTER (signature), dup,
10353 (candidates ? candidates : ""));
10354 free (dup);
10355 return NULL_TREE;
10358 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10359 when we're looking for a constructor. */
10361 static tree
10362 find_applicable_accessible_methods_list (lc, class, name, arglist)
10363 int lc;
10364 tree class, name, arglist;
10366 static struct hash_table t, *searched_classes = NULL;
10367 static int search_not_done = 0;
10368 tree list = NULL_TREE, all_list = NULL_TREE;
10370 /* Check the hash table to determine if this class has been searched
10371 already. */
10372 if (searched_classes)
10374 if (hash_lookup (searched_classes,
10375 (const hash_table_key) class, FALSE, NULL))
10376 return NULL;
10378 else
10380 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10381 java_hash_compare_tree_node);
10382 searched_classes = &t;
10385 search_not_done++;
10386 hash_lookup (searched_classes,
10387 (const hash_table_key) class, TRUE, NULL);
10389 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10391 load_class (class, 1);
10392 safe_layout_class (class);
10395 /* Search interfaces */
10396 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10397 && CLASS_INTERFACE (TYPE_NAME (class)))
10399 int i, n;
10400 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10401 search_applicable_methods_list (lc, TYPE_METHODS (class),
10402 name, arglist, &list, &all_list);
10403 n = TREE_VEC_LENGTH (basetype_vec);
10404 for (i = 1; i < n; i++)
10406 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10407 tree rlist;
10409 rlist = find_applicable_accessible_methods_list (lc, t, name,
10410 arglist);
10411 list = chainon (rlist, list);
10414 /* Search classes */
10415 else
10417 tree sc = class;
10418 int seen_inner_class = 0;
10419 search_applicable_methods_list (lc, TYPE_METHODS (class),
10420 name, arglist, &list, &all_list);
10422 /* We must search all interfaces of this class */
10423 if (!lc)
10425 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10426 int n = TREE_VEC_LENGTH (basetype_vec), i;
10427 for (i = 1; i < n; i++)
10429 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10430 if (t != object_type_node)
10432 tree rlist
10433 = find_applicable_accessible_methods_list (lc, t,
10434 name, arglist);
10435 list = chainon (rlist, list);
10440 /* Search enclosing context of inner classes before looking
10441 ancestors up. */
10442 while (!lc && INNER_CLASS_TYPE_P (class))
10444 tree rlist;
10445 seen_inner_class = 1;
10446 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
10447 rlist = find_applicable_accessible_methods_list (lc, class,
10448 name, arglist);
10449 list = chainon (rlist, list);
10452 if (!lc && seen_inner_class
10453 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10454 class = CLASSTYPE_SUPER (sc);
10455 else
10456 class = sc;
10458 /* Search superclass */
10459 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10461 tree rlist;
10462 class = CLASSTYPE_SUPER (class);
10463 rlist = find_applicable_accessible_methods_list (lc, class,
10464 name, arglist);
10465 list = chainon (rlist, list);
10469 search_not_done--;
10471 /* We're done. Reset the searched classes list and finally search
10472 java.lang.Object if it wasn't searched already. */
10473 if (!search_not_done)
10475 if (!lc
10476 && TYPE_METHODS (object_type_node)
10477 && !hash_lookup (searched_classes,
10478 (const hash_table_key) object_type_node,
10479 FALSE, NULL))
10481 search_applicable_methods_list (lc,
10482 TYPE_METHODS (object_type_node),
10483 name, arglist, &list, &all_list);
10485 hash_table_free (searched_classes);
10486 searched_classes = NULL;
10489 /* Either return the list obtained or all selected (but
10490 inaccessible) methods for better error report. */
10491 return (!list ? all_list : list);
10494 /* Effectively search for the appropriate method in method */
10496 static void
10497 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10498 int lc;
10499 tree method, name, arglist;
10500 tree *list, *all_list;
10502 for (; method; method = TREE_CHAIN (method))
10504 /* When dealing with constructor, stop here, otherwise search
10505 other classes */
10506 if (lc && !DECL_CONSTRUCTOR_P (method))
10507 continue;
10508 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10509 || (GET_METHOD_NAME (method) != name)))
10510 continue;
10512 if (argument_types_convertible (method, arglist))
10514 /* Retain accessible methods only */
10515 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10516 method, 0))
10517 *list = tree_cons (NULL_TREE, method, *list);
10518 else
10519 /* Also retain all selected method here */
10520 *all_list = tree_cons (NULL_TREE, method, *list);
10525 /* 15.11.2.2 Choose the Most Specific Method */
10527 static tree
10528 find_most_specific_methods_list (list)
10529 tree list;
10531 int max = 0;
10532 int abstract, candidates;
10533 tree current, new_list = NULL_TREE;
10534 for (current = list; current; current = TREE_CHAIN (current))
10536 tree method;
10537 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10539 for (method = list; method; method = TREE_CHAIN (method))
10541 tree method_v, current_v;
10542 /* Don't test a method against itself */
10543 if (method == current)
10544 continue;
10546 method_v = TREE_VALUE (method);
10547 current_v = TREE_VALUE (current);
10549 /* Compare arguments and location where methods where declared */
10550 if (argument_types_convertible (method_v, current_v))
10552 if (valid_method_invocation_conversion_p
10553 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10554 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10555 && enclosing_context_p (DECL_CONTEXT (method_v),
10556 DECL_CONTEXT (current_v))))
10558 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10559 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10560 max = (v > max ? v : max);
10566 /* Review the list and select the maximally specific methods */
10567 for (current = list, abstract = -1, candidates = -1;
10568 current; current = TREE_CHAIN (current))
10569 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10571 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10572 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10573 candidates++;
10576 /* If we have several and they're all abstract, just pick the
10577 closest one. */
10578 if (candidates > 0 && (candidates == abstract))
10580 new_list = nreverse (new_list);
10581 TREE_CHAIN (new_list) = NULL_TREE;
10584 /* We have several, we couldn't find a most specific, all but one are
10585 abstract, we pick the only non abstract one. */
10586 if (candidates > 0 && !max && (candidates == abstract+1))
10588 for (current = new_list; current; current = TREE_CHAIN (current))
10589 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10591 TREE_CHAIN (current) = NULL_TREE;
10592 new_list = current;
10596 /* If we can't find one, lower expectations and try to gather multiple
10597 maximally specific methods */
10598 while (!new_list && max)
10600 while (--max > 0)
10602 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10603 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10607 return new_list;
10610 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10611 converted by method invocation conversion (5.3) to the type of the
10612 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10613 to change less often than M1. */
10615 static int
10616 argument_types_convertible (m1, m2_or_arglist)
10617 tree m1, m2_or_arglist;
10619 static tree m2_arg_value = NULL_TREE;
10620 static tree m2_arg_cache = NULL_TREE;
10621 static int initialized_p;
10623 register tree m1_arg, m2_arg;
10625 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10626 collector. */
10627 if (!initialized_p)
10629 ggc_add_tree_root (&m2_arg_value, 1);
10630 ggc_add_tree_root (&m2_arg_cache, 1);
10631 initialized_p = 1;
10634 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10636 if (m2_arg_value == m2_or_arglist)
10637 m2_arg = m2_arg_cache;
10638 else
10640 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10641 argument types */
10642 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10644 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10645 if (!METHOD_STATIC (m2_or_arglist))
10646 m2_arg = TREE_CHAIN (m2_arg);
10648 else
10649 m2_arg = m2_or_arglist;
10651 m2_arg_value = m2_or_arglist;
10652 m2_arg_cache = m2_arg;
10655 while (m1_arg != end_params_node && m2_arg != end_params_node)
10657 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
10658 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10659 TREE_VALUE (m2_arg)))
10660 break;
10661 m1_arg = TREE_CHAIN (m1_arg);
10662 m2_arg = TREE_CHAIN (m2_arg);
10664 return m1_arg == end_params_node && m2_arg == end_params_node;
10667 /* Qualification routines */
10669 static void
10670 qualify_ambiguous_name (id)
10671 tree id;
10673 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10674 saved_current_class;
10675 int again, super_found = 0, this_found = 0, new_array_found = 0;
10676 int code;
10678 /* We first qualify the first element, then derive qualification of
10679 others based on the first one. If the first element is qualified
10680 by a resolution (field or type), this resolution is stored in the
10681 QUAL_RESOLUTION of the qual element being examined. We need to
10682 save the current_class since the use of SUPER might change the
10683 its value. */
10684 saved_current_class = current_class;
10685 qual = EXPR_WFL_QUALIFICATION (id);
10686 do {
10688 /* Simple qualified expression feature a qual_wfl that is a
10689 WFL. Expression derived from a primary feature more complicated
10690 things like a CALL_EXPR. Expression from primary need to be
10691 worked out to extract the part on which the qualification will
10692 take place. */
10693 qual_wfl = QUAL_WFL (qual);
10694 switch (TREE_CODE (qual_wfl))
10696 case CALL_EXPR:
10697 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10698 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10700 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10701 qual_wfl = QUAL_WFL (qual);
10703 break;
10704 case NEW_ARRAY_EXPR:
10705 case NEW_ANONYMOUS_ARRAY_EXPR:
10706 qual = TREE_CHAIN (qual);
10707 again = new_array_found = 1;
10708 continue;
10709 case CONVERT_EXPR:
10710 break;
10711 case NEW_CLASS_EXPR:
10712 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10713 break;
10714 case ARRAY_REF:
10715 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10716 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10717 break;
10718 case STRING_CST:
10719 qual = TREE_CHAIN (qual);
10720 qual_wfl = QUAL_WFL (qual);
10721 break;
10722 case CLASS_LITERAL:
10723 qual = TREE_CHAIN (qual);
10724 qual_wfl = QUAL_WFL (qual);
10725 break;
10726 default:
10727 /* Fix for -Wall. Just break doing nothing */
10728 break;
10731 ptr_type = current_class;
10732 again = 0;
10733 code = TREE_CODE (qual_wfl);
10735 /* Pos evaluation: non WFL leading expression nodes */
10736 if (code == CONVERT_EXPR
10737 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10738 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10740 else if (code == INTEGER_CST)
10741 name = qual_wfl;
10743 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
10744 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10745 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10747 else if (code == TREE_LIST)
10748 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10750 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10751 || code == PLUS_EXPR)
10753 qual = TREE_CHAIN (qual);
10754 qual_wfl = QUAL_WFL (qual);
10755 again = 1;
10757 else
10759 name = EXPR_WFL_NODE (qual_wfl);
10760 if (!name)
10762 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10763 again = 1;
10767 /* If we have a THIS (from a primary), we set the context accordingly */
10768 if (name == this_identifier_node)
10770 /* This isn't really elegant. One more added irregularity
10771 before I start using COMPONENT_REF (hopefully very soon.) */
10772 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10773 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10774 EXPR_WITH_FILE_LOCATION
10775 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10776 this_identifier_node)
10778 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10779 qual = EXPR_WFL_QUALIFICATION (qual);
10781 qual = TREE_CHAIN (qual);
10782 qual_wfl = QUAL_WFL (qual);
10783 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10784 again = 1;
10785 else
10786 name = EXPR_WFL_NODE (qual_wfl);
10787 this_found = 1;
10789 /* If we have a SUPER, we set the context accordingly */
10790 if (name == super_identifier_node)
10792 current_class = CLASSTYPE_SUPER (ptr_type);
10793 /* Check that there is such a thing as a super class. If not,
10794 return. The error will be caught later on, during the
10795 resolution */
10796 if (!current_class)
10798 current_class = saved_current_class;
10799 return;
10801 qual = TREE_CHAIN (qual);
10802 /* Do one more interation to set things up */
10803 super_found = again = 1;
10805 } while (again);
10807 /* If name appears within the scope of a local variable declaration
10808 or parameter declaration, then it is an expression name. We don't
10809 carry this test out if we're in the context of the use of SUPER
10810 or THIS */
10811 if (!this_found && !super_found
10812 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10813 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
10815 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10816 QUAL_RESOLUTION (qual) = decl;
10819 /* If within the class/interface NAME was found to be used there
10820 exists a (possibly inherited) field named NAME, then this is an
10821 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10822 address length, it is OK. */
10823 else if ((decl = lookup_field_wrapper (ptr_type, name))
10824 || (new_array_found && name == length_identifier_node))
10826 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10827 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
10830 /* We reclassify NAME as yielding to a type name resolution if:
10831 - NAME is a class/interface declared within the compilation
10832 unit containing NAME,
10833 - NAME is imported via a single-type-import declaration,
10834 - NAME is declared in an another compilation unit of the package
10835 of the compilation unit containing NAME,
10836 - NAME is declared by exactly on type-import-on-demand declaration
10837 of the compilation unit containing NAME.
10838 - NAME is actually a STRING_CST. */
10839 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10840 || (decl = resolve_and_layout (name, NULL_TREE)))
10842 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10843 QUAL_RESOLUTION (qual) = decl;
10846 /* Method call, array references and cast are expression name */
10847 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
10848 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10849 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
10850 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10852 /* Check here that NAME isn't declared by more than one
10853 type-import-on-demand declaration of the compilation unit
10854 containing NAME. FIXME */
10856 /* Otherwise, NAME is reclassified as a package name */
10857 else
10858 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10860 /* Propagate the qualification accross other components of the
10861 qualified name */
10862 for (qual = TREE_CHAIN (qual); qual;
10863 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10865 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10866 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10867 else
10868 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10871 /* Store the global qualification for the ambiguous part of ID back
10872 into ID fields */
10873 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10874 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10875 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10876 RESOLVE_TYPE_NAME_P (id) = 1;
10877 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10878 RESOLVE_PACKAGE_NAME_P (id) = 1;
10880 /* Restore the current class */
10881 current_class = saved_current_class;
10884 static int
10885 breakdown_qualified (left, right, source)
10886 tree *left, *right, source;
10888 char *p, *base;
10889 int l = IDENTIFIER_LENGTH (source);
10891 base = alloca (l + 1);
10892 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10894 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10895 p = base + l - 1;
10896 while (*p != '.' && p != base)
10897 p--;
10899 /* We didn't find a '.'. Return an error */
10900 if (p == base)
10901 return 1;
10903 *p = '\0';
10904 if (right)
10905 *right = get_identifier (p+1);
10906 *left = get_identifier (base);
10908 return 0;
10911 /* Patch tree nodes in a function body. When a BLOCK is found, push
10912 local variable decls if present.
10913 Same as java_complete_lhs, but does resolve static finals to values. */
10915 static tree
10916 java_complete_tree (node)
10917 tree node;
10919 node = java_complete_lhs (node);
10920 if (JDECL_P (node) && FIELD_STATIC (node) && FIELD_FINAL (node)
10921 && DECL_INITIAL (node) != NULL_TREE
10922 && !flag_emit_xref)
10924 tree value = DECL_INITIAL (node);
10925 DECL_INITIAL (node) = NULL_TREE;
10926 value = fold_constant_for_init (value, node);
10927 DECL_INITIAL (node) = value;
10928 if (value != NULL_TREE)
10930 /* fold_constant_for_init sometimes widen the original type
10931 of the constant (i.e. byte to int.) It's not desirable,
10932 especially if NODE is a function argument. */
10933 if (TREE_CODE (value) == INTEGER_CST
10934 && TREE_TYPE (node) != TREE_TYPE (value))
10935 return convert (TREE_TYPE (node), value);
10936 else
10937 return value;
10940 return node;
10943 static tree
10944 java_stabilize_reference (node)
10945 tree node;
10947 if (TREE_CODE (node) == COMPOUND_EXPR)
10949 tree op0 = TREE_OPERAND (node, 0);
10950 tree op1 = TREE_OPERAND (node, 1);
10951 TREE_OPERAND (node, 0) = save_expr (op0);
10952 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10953 return node;
10955 return stabilize_reference (node);
10958 /* Patch tree nodes in a function body. When a BLOCK is found, push
10959 local variable decls if present.
10960 Same as java_complete_tree, but does not resolve static finals to values. */
10962 static tree
10963 java_complete_lhs (node)
10964 tree node;
10966 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
10967 int flag;
10969 /* CONVERT_EXPR always has its type set, even though it needs to be
10970 worked out. */
10971 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10972 return node;
10974 /* The switch block implements cases processing container nodes
10975 first. Contained nodes are always written back. Leaves come
10976 next and return a value. */
10977 switch (TREE_CODE (node))
10979 case BLOCK:
10981 /* 1- Block section.
10982 Set the local values on decl names so we can identify them
10983 faster when they're referenced. At that stage, identifiers
10984 are legal so we don't check for declaration errors. */
10985 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10987 DECL_CONTEXT (cn) = current_function_decl;
10988 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
10990 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
10991 CAN_COMPLETE_NORMALLY (node) = 1;
10992 else
10994 tree stmt = BLOCK_EXPR_BODY (node);
10995 tree *ptr;
10996 int error_seen = 0;
10997 if (TREE_CODE (stmt) == COMPOUND_EXPR)
10999 /* Re-order from (((A; B); C); ...; Z) to
11000 (A; (B; (C ; (...; Z)))).
11001 This makes it easier to scan the statements left-to-right
11002 without using recursion (which might overflow the stack
11003 if the block has many statements. */
11004 for (;;)
11006 tree left = TREE_OPERAND (stmt, 0);
11007 if (TREE_CODE (left) != COMPOUND_EXPR)
11008 break;
11009 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11010 TREE_OPERAND (left, 1) = stmt;
11011 stmt = left;
11013 BLOCK_EXPR_BODY (node) = stmt;
11016 /* Now do the actual complete, without deep recursion for
11017 long blocks. */
11018 ptr = &BLOCK_EXPR_BODY (node);
11019 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11020 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11022 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11023 tree *next = &TREE_OPERAND (*ptr, 1);
11024 TREE_OPERAND (*ptr, 0) = cur;
11025 if (cur == empty_stmt_node)
11027 /* Optimization; makes it easier to detect empty bodies.
11028 Most useful for <clinit> with all-constant initializer. */
11029 *ptr = *next;
11030 continue;
11032 if (TREE_CODE (cur) == ERROR_MARK)
11033 error_seen++;
11034 else if (! CAN_COMPLETE_NORMALLY (cur))
11036 wfl_op2 = *next;
11037 for (;;)
11039 if (TREE_CODE (wfl_op2) == BLOCK)
11040 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11041 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11042 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11043 else
11044 break;
11046 if (TREE_CODE (wfl_op2) != CASE_EXPR
11047 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11048 unreachable_stmt_error (*ptr);
11050 ptr = next;
11052 *ptr = java_complete_tree (*ptr);
11054 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11055 return error_mark_node;
11056 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11058 /* Turn local bindings to null */
11059 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11060 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11062 TREE_TYPE (node) = void_type_node;
11063 break;
11065 /* 2- They are expressions but ultimately deal with statements */
11067 case THROW_EXPR:
11068 wfl_op1 = TREE_OPERAND (node, 0);
11069 COMPLETE_CHECK_OP_0 (node);
11070 /* 14.19 A throw statement cannot complete normally. */
11071 CAN_COMPLETE_NORMALLY (node) = 0;
11072 return patch_throw_statement (node, wfl_op1);
11074 case SYNCHRONIZED_EXPR:
11075 wfl_op1 = TREE_OPERAND (node, 0);
11076 return patch_synchronized_statement (node, wfl_op1);
11078 case TRY_EXPR:
11079 return patch_try_statement (node);
11081 case TRY_FINALLY_EXPR:
11082 COMPLETE_CHECK_OP_0 (node);
11083 COMPLETE_CHECK_OP_1 (node);
11084 CAN_COMPLETE_NORMALLY (node)
11085 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11086 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11087 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11088 return node;
11090 case CLEANUP_POINT_EXPR:
11091 COMPLETE_CHECK_OP_0 (node);
11092 TREE_TYPE (node) = void_type_node;
11093 CAN_COMPLETE_NORMALLY (node) =
11094 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11095 return node;
11097 case WITH_CLEANUP_EXPR:
11098 COMPLETE_CHECK_OP_0 (node);
11099 COMPLETE_CHECK_OP_2 (node);
11100 CAN_COMPLETE_NORMALLY (node) =
11101 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11102 TREE_TYPE (node) = void_type_node;
11103 return node;
11105 case LABELED_BLOCK_EXPR:
11106 PUSH_LABELED_BLOCK (node);
11107 if (LABELED_BLOCK_BODY (node))
11108 COMPLETE_CHECK_OP_1 (node);
11109 TREE_TYPE (node) = void_type_node;
11110 POP_LABELED_BLOCK ();
11112 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11114 LABELED_BLOCK_BODY (node) = NULL_TREE;
11115 CAN_COMPLETE_NORMALLY (node) = 1;
11117 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11118 CAN_COMPLETE_NORMALLY (node) = 1;
11119 return node;
11121 case EXIT_BLOCK_EXPR:
11122 /* We don't complete operand 1, because it's the return value of
11123 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11124 return patch_bc_statement (node);
11126 case CASE_EXPR:
11127 cn = java_complete_tree (TREE_OPERAND (node, 0));
11128 if (cn == error_mark_node)
11129 return cn;
11131 /* First, the case expression must be constant. Values of final
11132 fields are accepted. */
11133 cn = fold (cn);
11134 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11135 && JDECL_P (TREE_OPERAND (cn, 1))
11136 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11137 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11139 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11140 TREE_OPERAND (cn, 1));
11143 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11145 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11146 parse_error_context (node, "Constant expression required");
11147 return error_mark_node;
11150 nn = ctxp->current_loop;
11152 /* It must be assignable to the type of the switch expression. */
11153 if (!try_builtin_assignconv (NULL_TREE,
11154 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11156 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11157 parse_error_context
11158 (wfl_operator,
11159 "Incompatible type for case. Can't convert `%s' to `int'",
11160 lang_printable_name (TREE_TYPE (cn), 0));
11161 return error_mark_node;
11164 cn = fold (convert (int_type_node, cn));
11166 /* Multiple instance of a case label bearing the same
11167 value is checked during code generation. The case
11168 expression is allright so far. */
11169 if (TREE_CODE (cn) == VAR_DECL)
11170 cn = DECL_INITIAL (cn);
11171 TREE_OPERAND (node, 0) = cn;
11172 TREE_TYPE (node) = void_type_node;
11173 CAN_COMPLETE_NORMALLY (node) = 1;
11174 TREE_SIDE_EFFECTS (node) = 1;
11175 break;
11177 case DEFAULT_EXPR:
11178 nn = ctxp->current_loop;
11179 /* Only one default label is allowed per switch statement */
11180 if (SWITCH_HAS_DEFAULT (nn))
11182 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11183 parse_error_context (wfl_operator,
11184 "Duplicate case label: `default'");
11185 return error_mark_node;
11187 else
11188 SWITCH_HAS_DEFAULT (nn) = 1;
11189 TREE_TYPE (node) = void_type_node;
11190 TREE_SIDE_EFFECTS (node) = 1;
11191 CAN_COMPLETE_NORMALLY (node) = 1;
11192 break;
11194 case SWITCH_EXPR:
11195 case LOOP_EXPR:
11196 PUSH_LOOP (node);
11197 /* Check whether the loop was enclosed in a labeled
11198 statement. If not, create one, insert the loop in it and
11199 return the node */
11200 nn = patch_loop_statement (node);
11202 /* Anyways, walk the body of the loop */
11203 if (TREE_CODE (node) == LOOP_EXPR)
11204 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11205 /* Switch statement: walk the switch expression and the cases */
11206 else
11207 node = patch_switch_statement (node);
11209 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11210 nn = error_mark_node;
11211 else
11213 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11214 /* If we returned something different, that's because we
11215 inserted a label. Pop the label too. */
11216 if (nn != node)
11218 if (CAN_COMPLETE_NORMALLY (node))
11219 CAN_COMPLETE_NORMALLY (nn) = 1;
11220 POP_LABELED_BLOCK ();
11223 POP_LOOP ();
11224 return nn;
11226 case EXIT_EXPR:
11227 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11228 return patch_exit_expr (node);
11230 case COND_EXPR:
11231 /* Condition */
11232 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11233 if (TREE_OPERAND (node, 0) == error_mark_node)
11234 return error_mark_node;
11235 /* then-else branches */
11236 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11237 if (TREE_OPERAND (node, 1) == error_mark_node)
11238 return error_mark_node;
11239 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11240 if (TREE_OPERAND (node, 2) == error_mark_node)
11241 return error_mark_node;
11242 return patch_if_else_statement (node);
11243 break;
11245 case CONDITIONAL_EXPR:
11246 /* Condition */
11247 wfl_op1 = TREE_OPERAND (node, 0);
11248 COMPLETE_CHECK_OP_0 (node);
11249 wfl_op2 = TREE_OPERAND (node, 1);
11250 COMPLETE_CHECK_OP_1 (node);
11251 wfl_op3 = TREE_OPERAND (node, 2);
11252 COMPLETE_CHECK_OP_2 (node);
11253 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11255 /* 3- Expression section */
11256 case COMPOUND_EXPR:
11257 wfl_op2 = TREE_OPERAND (node, 1);
11258 TREE_OPERAND (node, 0) = nn =
11259 java_complete_tree (TREE_OPERAND (node, 0));
11260 if (wfl_op2 == empty_stmt_node)
11261 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11262 else
11264 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11266 /* An unreachable condition in a do-while statement
11267 is *not* (technically) an unreachable statement. */
11268 nn = wfl_op2;
11269 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11270 nn = EXPR_WFL_NODE (nn);
11271 if (TREE_CODE (nn) != EXIT_EXPR)
11273 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11274 parse_error_context (wfl_operator, "Unreachable statement");
11277 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11278 if (TREE_OPERAND (node, 1) == error_mark_node)
11279 return error_mark_node;
11280 CAN_COMPLETE_NORMALLY (node)
11281 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11283 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11284 break;
11286 case RETURN_EXPR:
11287 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11288 return patch_return (node);
11290 case EXPR_WITH_FILE_LOCATION:
11291 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11292 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11294 tree wfl = node;
11295 node = resolve_expression_name (node, NULL);
11296 if (node == error_mark_node)
11297 return node;
11298 /* Keep line number information somewhere were it doesn't
11299 disrupt the completion process. */
11300 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11302 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11303 TREE_OPERAND (node, 1) = wfl;
11305 CAN_COMPLETE_NORMALLY (node) = 1;
11307 else
11309 tree body;
11310 int save_lineno = lineno;
11311 lineno = EXPR_WFL_LINENO (node);
11312 body = java_complete_tree (EXPR_WFL_NODE (node));
11313 lineno = save_lineno;
11314 EXPR_WFL_NODE (node) = body;
11315 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11316 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11317 if (body == empty_stmt_node)
11319 /* Optimization; makes it easier to detect empty bodies. */
11320 return body;
11322 if (body == error_mark_node)
11324 /* Its important for the evaluation of assignment that
11325 this mark on the TREE_TYPE is propagated. */
11326 TREE_TYPE (node) = error_mark_node;
11327 return error_mark_node;
11329 else
11330 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11333 break;
11335 case NEW_ARRAY_EXPR:
11336 /* Patch all the dimensions */
11337 flag = 0;
11338 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11340 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11341 tree dim = convert (int_type_node,
11342 java_complete_tree (TREE_VALUE (cn)));
11343 if (dim == error_mark_node)
11345 flag = 1;
11346 continue;
11348 else
11350 TREE_VALUE (cn) = dim;
11351 /* Setup the location of the current dimension, for
11352 later error report. */
11353 TREE_PURPOSE (cn) =
11354 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11355 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11358 /* They complete the array creation expression, if no errors
11359 were found. */
11360 CAN_COMPLETE_NORMALLY (node) = 1;
11361 return (flag ? error_mark_node
11362 : force_evaluation_order (patch_newarray (node)));
11364 case NEW_ANONYMOUS_ARRAY_EXPR:
11365 /* Create the array type if necessary. */
11366 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11368 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11369 if (!(type = resolve_type_during_patch (type)))
11370 return error_mark_node;
11371 type = build_array_from_name (type, NULL_TREE,
11372 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11373 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11375 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11376 ANONYMOUS_ARRAY_INITIALIZER (node));
11377 if (node == error_mark_node)
11378 return error_mark_node;
11379 CAN_COMPLETE_NORMALLY (node) = 1;
11380 return node;
11382 case NEW_CLASS_EXPR:
11383 case CALL_EXPR:
11384 /* Complete function's argument(s) first */
11385 if (complete_function_arguments (node))
11386 return error_mark_node;
11387 else
11389 tree decl, wfl = TREE_OPERAND (node, 0);
11390 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11392 node = patch_method_invocation (node, NULL_TREE,
11393 NULL_TREE, 0, &decl);
11394 if (node == error_mark_node)
11395 return error_mark_node;
11397 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11398 /* If we call this(...), register signature and positions */
11399 if (in_this)
11400 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11401 tree_cons (wfl, decl,
11402 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11403 CAN_COMPLETE_NORMALLY (node) = 1;
11404 return force_evaluation_order (node);
11407 case MODIFY_EXPR:
11408 /* Save potential wfls */
11409 wfl_op1 = TREE_OPERAND (node, 0);
11410 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11412 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11413 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11414 && DECL_INITIAL (nn) != NULL_TREE)
11416 tree value;
11418 value = fold_constant_for_init (nn, nn);
11420 if (value != NULL_TREE)
11422 tree type = TREE_TYPE (value);
11423 if (JPRIMITIVE_TYPE_P (type) ||
11424 (type == string_ptr_type_node && ! flag_emit_class_files))
11425 return empty_stmt_node;
11427 if (! flag_emit_class_files)
11428 DECL_INITIAL (nn) = NULL_TREE;
11430 wfl_op2 = TREE_OPERAND (node, 1);
11432 if (TREE_OPERAND (node, 0) == error_mark_node)
11433 return error_mark_node;
11435 flag = COMPOUND_ASSIGN_P (wfl_op2);
11436 if (flag)
11438 /* This might break when accessing outer field from inner
11439 class. TESTME, FIXME */
11440 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11442 /* Hand stablize the lhs on both places */
11443 TREE_OPERAND (node, 0) = lvalue;
11444 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11445 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11447 /* 15.25.2.a: Left hand is not an array access. FIXME */
11448 /* Now complete the RHS. We write it back later on. */
11449 nn = java_complete_tree (TREE_OPERAND (node, 1));
11451 if ((cn = patch_string (nn)))
11452 nn = cn;
11454 /* The last part of the rewrite for E1 op= E2 is to have
11455 E1 = (T)(E1 op E2), with T being the type of E1. */
11456 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11457 TREE_TYPE (lvalue), nn));
11459 /* 15.25.2.b: Left hand is an array access. FIXME */
11462 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11463 function to complete this RHS. Note that a NEW_ARRAY_INIT
11464 might have been already fully expanded if created as a result
11465 of processing an anonymous array initializer. We avoid doing
11466 the operation twice by testing whether the node already bears
11467 a type. */
11468 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11469 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11470 TREE_OPERAND (node, 1));
11471 /* Otherwise we simply complete the RHS */
11472 else
11473 nn = java_complete_tree (TREE_OPERAND (node, 1));
11475 if (nn == error_mark_node)
11476 return error_mark_node;
11478 /* Write back the RHS as we evaluated it. */
11479 TREE_OPERAND (node, 1) = nn;
11481 /* In case we're handling = with a String as a RHS, we need to
11482 produce a String out of the RHS (it might still be a
11483 STRING_CST or a StringBuffer at this stage */
11484 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11485 TREE_OPERAND (node, 1) = nn;
11487 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11488 TREE_OPERAND (node, 1))))
11490 /* We return error_mark_node if outer_field_access_fix
11491 detects we write into a final. */
11492 if (nn == error_mark_node)
11493 return error_mark_node;
11494 node = nn;
11496 else
11498 node = patch_assignment (node, wfl_op1, wfl_op2);
11499 /* Reorganize the tree if necessary. */
11500 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11501 || JSTRING_P (TREE_TYPE (node))))
11502 node = java_refold (node);
11505 CAN_COMPLETE_NORMALLY (node) = 1;
11506 return node;
11508 case MULT_EXPR:
11509 case PLUS_EXPR:
11510 case MINUS_EXPR:
11511 case LSHIFT_EXPR:
11512 case RSHIFT_EXPR:
11513 case URSHIFT_EXPR:
11514 case BIT_AND_EXPR:
11515 case BIT_XOR_EXPR:
11516 case BIT_IOR_EXPR:
11517 case TRUNC_MOD_EXPR:
11518 case TRUNC_DIV_EXPR:
11519 case RDIV_EXPR:
11520 case TRUTH_ANDIF_EXPR:
11521 case TRUTH_ORIF_EXPR:
11522 case EQ_EXPR:
11523 case NE_EXPR:
11524 case GT_EXPR:
11525 case GE_EXPR:
11526 case LT_EXPR:
11527 case LE_EXPR:
11528 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11529 knows how to handle those cases. */
11530 wfl_op1 = TREE_OPERAND (node, 0);
11531 wfl_op2 = TREE_OPERAND (node, 1);
11533 CAN_COMPLETE_NORMALLY (node) = 1;
11534 /* Don't complete string nodes if dealing with the PLUS operand. */
11535 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11537 nn = java_complete_tree (wfl_op1);
11538 if (nn == error_mark_node)
11539 return error_mark_node;
11541 TREE_OPERAND (node, 0) = nn;
11543 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11545 nn = java_complete_tree (wfl_op2);
11546 if (nn == error_mark_node)
11547 return error_mark_node;
11549 TREE_OPERAND (node, 1) = nn;
11551 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11553 case INSTANCEOF_EXPR:
11554 wfl_op1 = TREE_OPERAND (node, 0);
11555 COMPLETE_CHECK_OP_0 (node);
11556 if (flag_emit_xref)
11558 TREE_TYPE (node) = boolean_type_node;
11559 return node;
11561 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11563 case UNARY_PLUS_EXPR:
11564 case NEGATE_EXPR:
11565 case TRUTH_NOT_EXPR:
11566 case BIT_NOT_EXPR:
11567 case PREDECREMENT_EXPR:
11568 case PREINCREMENT_EXPR:
11569 case POSTDECREMENT_EXPR:
11570 case POSTINCREMENT_EXPR:
11571 case CONVERT_EXPR:
11572 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11573 how to handle those cases. */
11574 wfl_op1 = TREE_OPERAND (node, 0);
11575 CAN_COMPLETE_NORMALLY (node) = 1;
11576 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11577 if (TREE_OPERAND (node, 0) == error_mark_node)
11578 return error_mark_node;
11579 node = patch_unaryop (node, wfl_op1);
11580 CAN_COMPLETE_NORMALLY (node) = 1;
11581 break;
11583 case ARRAY_REF:
11584 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11585 how to handle those cases. */
11586 wfl_op1 = TREE_OPERAND (node, 0);
11587 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11588 if (TREE_OPERAND (node, 0) == error_mark_node)
11589 return error_mark_node;
11590 if (!flag_emit_class_files && !flag_emit_xref)
11591 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
11592 /* The same applies to wfl_op2 */
11593 wfl_op2 = TREE_OPERAND (node, 1);
11594 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11595 if (TREE_OPERAND (node, 1) == error_mark_node)
11596 return error_mark_node;
11597 if (!flag_emit_class_files && !flag_emit_xref)
11598 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
11599 return patch_array_ref (node);
11601 case RECORD_TYPE:
11602 return node;;
11604 case COMPONENT_REF:
11605 /* The first step in the re-write of qualified name handling. FIXME.
11606 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
11607 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11608 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11610 tree name = TREE_OPERAND (node, 1);
11611 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11612 if (field == NULL_TREE)
11614 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11615 return error_mark_node;
11617 if (! FIELD_STATIC (field))
11619 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11620 return error_mark_node;
11622 return field;
11624 else
11625 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
11626 break;
11628 case THIS_EXPR:
11629 /* Can't use THIS in a static environment */
11630 if (!current_this)
11632 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11633 parse_error_context (wfl_operator,
11634 "Keyword `this' used outside allowed context");
11635 TREE_TYPE (node) = error_mark_node;
11636 return error_mark_node;
11638 if (ctxp->explicit_constructor_p)
11640 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11641 parse_error_context
11642 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
11643 TREE_TYPE (node) = error_mark_node;
11644 return error_mark_node;
11646 return current_this;
11648 case CLASS_LITERAL:
11649 CAN_COMPLETE_NORMALLY (node) = 1;
11650 node = patch_incomplete_class_ref (node);
11651 if (node == error_mark_node)
11652 return error_mark_node;
11653 break;
11655 case INSTANCE_INITIALIZERS_EXPR:
11656 in_instance_initializer++;
11657 node = java_complete_tree (TREE_OPERAND (node, 0));
11658 in_instance_initializer--;
11659 if (node != error_mark_node)
11660 TREE_TYPE (node) = void_type_node;
11661 else
11662 return error_mark_node;
11663 break;
11665 default:
11666 CAN_COMPLETE_NORMALLY (node) = 1;
11667 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
11668 and it's time to turn it into the appropriate String object */
11669 if ((nn = patch_string (node)))
11670 node = nn;
11671 else
11672 fatal ("No case for tree code `%s' - java_complete_tree\n",
11673 tree_code_name [TREE_CODE (node)]);
11675 return node;
11678 /* Complete function call's argument. Return a non zero value is an
11679 error was found. */
11681 static int
11682 complete_function_arguments (node)
11683 tree node;
11685 int flag = 0;
11686 tree cn;
11688 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11689 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11691 tree wfl = TREE_VALUE (cn), parm, temp;
11692 parm = java_complete_tree (wfl);
11694 if (parm == error_mark_node)
11696 flag = 1;
11697 continue;
11699 /* If have a string literal that we haven't transformed yet or a
11700 crafted string buffer, as a result of use of the the String
11701 `+' operator. Build `parm.toString()' and expand it. */
11702 if ((temp = patch_string (parm)))
11703 parm = temp;
11704 /* Inline PRIMTYPE.TYPE read access */
11705 parm = maybe_build_primttype_type_ref (parm, wfl);
11707 TREE_VALUE (cn) = parm;
11709 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11710 return flag;
11713 /* Sometimes (for loops and variable initialized during their
11714 declaration), we want to wrap a statement around a WFL and turn it
11715 debugable. */
11717 static tree
11718 build_debugable_stmt (location, stmt)
11719 int location;
11720 tree stmt;
11722 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11724 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11725 EXPR_WFL_LINECOL (stmt) = location;
11727 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11728 return stmt;
11731 static tree
11732 build_expr_block (body, decls)
11733 tree body, decls;
11735 tree node = make_node (BLOCK);
11736 BLOCK_EXPR_DECLS (node) = decls;
11737 BLOCK_EXPR_BODY (node) = body;
11738 if (body)
11739 TREE_TYPE (node) = TREE_TYPE (body);
11740 TREE_SIDE_EFFECTS (node) = 1;
11741 return node;
11744 /* Create a new function block and link it approriately to current
11745 function block chain */
11747 static tree
11748 enter_block ()
11750 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11753 /* Link block B supercontext to the previous block. The current
11754 function DECL is used as supercontext when enter_a_block is called
11755 for the first time for a given function. The current function body
11756 (DECL_FUNCTION_BODY) is set to be block B. */
11758 static tree
11759 enter_a_block (b)
11760 tree b;
11762 tree fndecl = current_function_decl;
11764 if (!fndecl) {
11765 BLOCK_SUPERCONTEXT (b) = current_static_block;
11766 current_static_block = b;
11769 else if (!DECL_FUNCTION_BODY (fndecl))
11771 BLOCK_SUPERCONTEXT (b) = fndecl;
11772 DECL_FUNCTION_BODY (fndecl) = b;
11774 else
11776 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11777 DECL_FUNCTION_BODY (fndecl) = b;
11779 return b;
11782 /* Exit a block by changing the current function body
11783 (DECL_FUNCTION_BODY) to the current block super context, only if
11784 the block being exited isn't the method's top level one. */
11786 static tree
11787 exit_block ()
11789 tree b;
11790 if (current_function_decl)
11792 b = DECL_FUNCTION_BODY (current_function_decl);
11793 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11794 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11796 else
11798 b = current_static_block;
11800 if (BLOCK_SUPERCONTEXT (b))
11801 current_static_block = BLOCK_SUPERCONTEXT (b);
11803 return b;
11806 /* Lookup for NAME in the nested function's blocks, all the way up to
11807 the current toplevel one. It complies with Java's local variable
11808 scoping rules. */
11810 static tree
11811 lookup_name_in_blocks (name)
11812 tree name;
11814 tree b = GET_CURRENT_BLOCK (current_function_decl);
11816 while (b != current_function_decl)
11818 tree current;
11820 /* Paranoid sanity check. To be removed */
11821 if (TREE_CODE (b) != BLOCK)
11822 fatal ("non block expr function body - lookup_name_in_blocks");
11824 for (current = BLOCK_EXPR_DECLS (b); current;
11825 current = TREE_CHAIN (current))
11826 if (DECL_NAME (current) == name)
11827 return current;
11828 b = BLOCK_SUPERCONTEXT (b);
11830 return NULL_TREE;
11833 static void
11834 maybe_absorb_scoping_blocks ()
11836 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
11838 tree b = exit_block ();
11839 java_method_add_stmt (current_function_decl, b);
11840 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11845 /* This section of the source is reserved to build_* functions that
11846 are building incomplete tree nodes and the patch_* functions that
11847 are completing them. */
11849 /* Wrap a non WFL node around a WFL. */
11850 static tree
11851 build_wfl_wrap (node, location)
11852 tree node;
11853 int location;
11855 tree wfl, node_to_insert = node;
11857 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11858 with the way we're processing SUPER. A THIS from a primary as a
11859 different form than a SUPER. Turn THIS into something symbolic */
11860 if (TREE_CODE (node) == THIS_EXPR)
11861 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11862 else
11863 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11865 EXPR_WFL_LINECOL (wfl) = location;
11866 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11867 return wfl;
11871 /* Build a super() constructor invocation. Returns empty_stmt_node if
11872 we're currently dealing with the class java.lang.Object. */
11874 static tree
11875 build_super_invocation (mdecl)
11876 tree mdecl;
11878 if (DECL_CONTEXT (mdecl) == object_type_node)
11879 return empty_stmt_node;
11880 else
11882 tree super_wfl = build_wfl_node (super_identifier_node);
11883 tree a = NULL_TREE, t;
11884 /* If we're dealing with an anonymous class, pass the arguments
11885 of the crafted constructor along. */
11886 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11888 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11889 for (; t != end_params_node; t = TREE_CHAIN (t))
11890 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11892 return build_method_invocation (super_wfl, a);
11896 /* Build a SUPER/THIS qualified method invocation. */
11898 static tree
11899 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11900 int use_this;
11901 tree name, args;
11902 int lloc, rloc;
11904 tree invok;
11905 tree wfl =
11906 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
11907 EXPR_WFL_LINECOL (wfl) = lloc;
11908 invok = build_method_invocation (name, args);
11909 return make_qualified_primary (wfl, invok, rloc);
11912 /* Build an incomplete CALL_EXPR node. */
11914 static tree
11915 build_method_invocation (name, args)
11916 tree name;
11917 tree args;
11919 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11920 TREE_SIDE_EFFECTS (call) = 1;
11921 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11922 return call;
11925 /* Build an incomplete new xxx(...) node. */
11927 static tree
11928 build_new_invocation (name, args)
11929 tree name, args;
11931 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11932 TREE_SIDE_EFFECTS (call) = 1;
11933 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11934 return call;
11937 /* Build an incomplete assignment expression. */
11939 static tree
11940 build_assignment (op, op_location, lhs, rhs)
11941 int op, op_location;
11942 tree lhs, rhs;
11944 tree assignment;
11945 /* Build the corresponding binop if we deal with a Compound
11946 Assignment operator. Mark the binop sub-tree as part of a
11947 Compound Assignment expression */
11948 if (op != ASSIGN_TK)
11950 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11951 COMPOUND_ASSIGN_P (rhs) = 1;
11953 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11954 TREE_SIDE_EFFECTS (assignment) = 1;
11955 EXPR_WFL_LINECOL (assignment) = op_location;
11956 return assignment;
11959 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11961 char *
11962 print_int_node (node)
11963 tree node;
11965 static char buffer [80];
11966 if (TREE_CONSTANT_OVERFLOW (node))
11967 sprintf (buffer, "<overflow>");
11969 if (TREE_INT_CST_HIGH (node) == 0)
11970 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11971 TREE_INT_CST_LOW (node));
11972 else if (TREE_INT_CST_HIGH (node) == -1
11973 && TREE_INT_CST_LOW (node) != 0)
11975 buffer [0] = '-';
11976 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
11977 -TREE_INT_CST_LOW (node));
11979 else
11980 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
11981 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
11983 return buffer;
11986 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
11987 context. */
11989 static int
11990 check_final_assignment (lvalue, wfl)
11991 tree lvalue, wfl;
11993 if (TREE_CODE (lvalue) == COMPOUND_EXPR
11994 && JDECL_P (TREE_OPERAND (lvalue, 1)))
11995 lvalue = TREE_OPERAND (lvalue, 1);
11997 /* When generating class files, references to the `length' field
11998 look a bit different. */
11999 if ((flag_emit_class_files
12000 && TREE_CODE (lvalue) == COMPONENT_REF
12001 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
12002 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
12003 || (TREE_CODE (lvalue) == FIELD_DECL
12004 && FIELD_FINAL (lvalue)
12005 && !DECL_CLINIT_P (current_function_decl)
12006 && !DECL_FINIT_P (current_function_decl)))
12008 parse_error_context
12009 (wfl, "Can't assign a value to the final variable `%s'",
12010 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12011 return 1;
12013 return 0;
12016 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12017 read. This is needed to avoid circularities in the implementation
12018 of these fields in libjava. */
12020 static tree
12021 maybe_build_primttype_type_ref (rhs, wfl)
12022 tree rhs, wfl;
12024 tree to_return = NULL_TREE;
12025 tree rhs_type = TREE_TYPE (rhs);
12026 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12028 tree n = TREE_OPERAND (rhs, 1);
12029 if (TREE_CODE (n) == VAR_DECL
12030 && DECL_NAME (n) == TYPE_identifier_node
12031 && rhs_type == class_ptr_type
12032 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
12034 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
12035 if (!strncmp (self_name, "java.lang.", 10))
12036 to_return = build_primtype_type_ref (self_name);
12039 return (to_return ? to_return : rhs );
12042 /* 15.25 Assignment operators. */
12044 static tree
12045 patch_assignment (node, wfl_op1, wfl_op2)
12046 tree node;
12047 tree wfl_op1;
12048 tree wfl_op2;
12050 tree rhs = TREE_OPERAND (node, 1);
12051 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12052 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12053 int error_found = 0;
12054 int lvalue_from_array = 0;
12056 /* Can't assign to a (blank) final. */
12057 if (check_final_assignment (lvalue, wfl_op1))
12058 error_found = 1;
12060 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12062 /* Lhs can be a named variable */
12063 if (JDECL_P (lvalue))
12065 lhs_type = TREE_TYPE (lvalue);
12067 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12068 comment on reason why */
12069 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12071 lhs_type = TREE_TYPE (lvalue);
12072 lvalue_from_array = 1;
12074 /* Or a field access */
12075 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12076 lhs_type = TREE_TYPE (lvalue);
12077 /* Or a function return slot */
12078 else if (TREE_CODE (lvalue) == RESULT_DECL)
12079 lhs_type = TREE_TYPE (lvalue);
12080 /* Otherwise, we might want to try to write into an optimized static
12081 final, this is an of a different nature, reported further on. */
12082 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12083 && resolve_expression_name (wfl_op1, &llvalue))
12085 if (!error_found && check_final_assignment (llvalue, wfl_op1))
12087 /* What we should do instead is resetting the all the flags
12088 previously set, exchange lvalue for llvalue and continue. */
12089 error_found = 1;
12090 return error_mark_node;
12092 else
12093 lhs_type = TREE_TYPE (lvalue);
12095 else
12097 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12098 error_found = 1;
12101 rhs_type = TREE_TYPE (rhs);
12102 /* 5.1 Try the assignment conversion for builtin type. */
12103 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12105 /* 5.2 If it failed, try a reference conversion */
12106 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12107 lhs_type = promote_type (rhs_type);
12109 /* 15.25.2 If we have a compound assignment, convert RHS into the
12110 type of the LHS */
12111 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12112 new_rhs = convert (lhs_type, rhs);
12114 /* Explicit cast required. This is an error */
12115 if (!new_rhs)
12117 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12118 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12119 tree wfl;
12120 char operation [32]; /* Max size known */
12122 /* If the assignment is part of a declaration, we use the WFL of
12123 the declared variable to point out the error and call it a
12124 declaration problem. If the assignment is a genuine =
12125 operator, we call is a operator `=' problem, otherwise we
12126 call it an assignment problem. In both of these last cases,
12127 we use the WFL of the operator to indicate the error. */
12129 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12131 wfl = wfl_op1;
12132 strcpy (operation, "declaration");
12134 else
12136 wfl = wfl_operator;
12137 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12138 strcpy (operation, "assignment");
12139 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12140 strcpy (operation, "`return'");
12141 else
12142 strcpy (operation, "`='");
12145 if (!valid_cast_to_p (rhs_type, lhs_type))
12146 parse_error_context
12147 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12148 operation, t1, t2);
12149 else
12150 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12151 operation, t1, t2);
12152 free (t1); free (t2);
12153 error_found = 1;
12156 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12157 if (new_rhs)
12158 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
12160 if (error_found)
12161 return error_mark_node;
12163 /* 10.10: Array Store Exception runtime check */
12164 if (!flag_emit_class_files
12165 && !flag_emit_xref
12166 && lvalue_from_array
12167 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12169 tree check;
12170 tree base = lvalue;
12172 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12173 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12174 base = TREE_OPERAND (lvalue, 0);
12175 else
12177 if (flag_bounds_check)
12178 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12179 else
12180 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12183 /* Build the invocation of _Jv_CheckArrayStore */
12184 new_rhs = save_expr (new_rhs);
12185 check = build (CALL_EXPR, void_type_node,
12186 build_address_of (soft_checkarraystore_node),
12187 tree_cons (NULL_TREE, base,
12188 build_tree_list (NULL_TREE, new_rhs)),
12189 NULL_TREE);
12190 TREE_SIDE_EFFECTS (check) = 1;
12192 /* We have to decide on an insertion point */
12193 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12195 tree t;
12196 if (flag_bounds_check)
12198 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12199 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12200 build (COMPOUND_EXPR, void_type_node, t, check);
12202 else
12203 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12204 check, TREE_OPERAND (lvalue, 1));
12206 else
12208 /* Make sure the bound check will happen before the store check */
12209 if (flag_bounds_check)
12210 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12211 build (COMPOUND_EXPR, void_type_node,
12212 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12213 else
12214 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12218 /* Final locals can be used as case values in switch
12219 statement. Prepare them for this eventuality. */
12220 if (TREE_CODE (lvalue) == VAR_DECL
12221 && LOCAL_FINAL (lvalue)
12222 && TREE_CONSTANT (new_rhs)
12223 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12224 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12227 TREE_CONSTANT (lvalue) = 1;
12228 DECL_INITIAL (lvalue) = new_rhs;
12231 TREE_OPERAND (node, 0) = lvalue;
12232 TREE_OPERAND (node, 1) = new_rhs;
12233 TREE_TYPE (node) = lhs_type;
12234 return node;
12237 /* Check that type SOURCE can be cast into type DEST. If the cast
12238 can't occur at all, return 0 otherwise 1. This function is used to
12239 produce accurate error messages on the reasons why an assignment
12240 failed. */
12242 static tree
12243 try_reference_assignconv (lhs_type, rhs)
12244 tree lhs_type, rhs;
12246 tree new_rhs = NULL_TREE;
12247 tree rhs_type = TREE_TYPE (rhs);
12249 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12251 /* `null' may be assigned to any reference type */
12252 if (rhs == null_pointer_node)
12253 new_rhs = null_pointer_node;
12254 /* Try the reference assignment conversion */
12255 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12256 new_rhs = rhs;
12257 /* This is a magic assignment that we process differently */
12258 else if (rhs == soft_exceptioninfo_call_node)
12259 new_rhs = rhs;
12261 return new_rhs;
12264 /* Check that RHS can be converted into LHS_TYPE by the assignment
12265 conversion (5.2), for the cases of RHS being a builtin type. Return
12266 NULL_TREE if the conversion fails or if because RHS isn't of a
12267 builtin type. Return a converted RHS if the conversion is possible. */
12269 static tree
12270 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12271 tree wfl_op1, lhs_type, rhs;
12273 tree new_rhs = NULL_TREE;
12274 tree rhs_type = TREE_TYPE (rhs);
12276 /* Handle boolean specially. */
12277 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12278 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12280 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12281 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12282 new_rhs = rhs;
12285 /* Zero accepted everywhere */
12286 else if (TREE_CODE (rhs) == INTEGER_CST
12287 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12288 && JPRIMITIVE_TYPE_P (rhs_type))
12289 new_rhs = convert (lhs_type, rhs);
12291 /* 5.1.1 Try Identity Conversion,
12292 5.1.2 Try Widening Primitive Conversion */
12293 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12294 new_rhs = convert (lhs_type, rhs);
12296 /* Try a narrowing primitive conversion (5.1.3):
12297 - expression is a constant expression of type int AND
12298 - variable is byte, short or char AND
12299 - The value of the expression is representable in the type of the
12300 variable */
12301 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12302 && (lhs_type == byte_type_node || lhs_type == char_type_node
12303 || lhs_type == short_type_node))
12305 if (int_fits_type_p (rhs, lhs_type))
12306 new_rhs = convert (lhs_type, rhs);
12307 else if (wfl_op1) /* Might be called with a NULL */
12308 parse_warning_context
12309 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12310 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12311 /* Reported a warning that will turn into an error further
12312 down, so we don't return */
12315 return new_rhs;
12318 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12319 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12320 0 is the conversion test fails. This implements parts the method
12321 invocation convertion (5.3). */
12323 static int
12324 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12325 tree lhs_type, rhs_type;
12327 /* 5.1.1: This is the identity conversion part. */
12328 if (lhs_type == rhs_type)
12329 return 1;
12331 /* Reject non primitive types and boolean conversions. */
12332 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12333 return 0;
12335 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12336 than a char can't be converted into a char. Short can't too, but
12337 the < test below takes care of that */
12338 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12339 return 0;
12341 /* Accept all promoted type here. Note, we can't use <= in the test
12342 below, because we still need to bounce out assignments of short
12343 to char and the likes */
12344 if (lhs_type == int_type_node
12345 && (rhs_type == promoted_byte_type_node
12346 || rhs_type == promoted_short_type_node
12347 || rhs_type == promoted_char_type_node
12348 || rhs_type == promoted_boolean_type_node))
12349 return 1;
12351 /* From here, an integral is widened if its precision is smaller
12352 than the precision of the LHS or if the LHS is a floating point
12353 type, or the RHS is a float and the RHS a double. */
12354 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12355 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12356 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12357 || (rhs_type == float_type_node && lhs_type == double_type_node))
12358 return 1;
12360 return 0;
12363 /* Check that something of SOURCE type can be assigned or cast to
12364 something of DEST type at runtime. Return 1 if the operation is
12365 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12366 were SOURCE is cast into DEST, which borrows a lot of the
12367 assignment check. */
12369 static int
12370 valid_ref_assignconv_cast_p (source, dest, cast)
12371 tree source;
12372 tree dest;
12373 int cast;
12375 /* SOURCE or DEST might be null if not from a declared entity. */
12376 if (!source || !dest)
12377 return 0;
12378 if (JNULLP_TYPE_P (source))
12379 return 1;
12380 if (TREE_CODE (source) == POINTER_TYPE)
12381 source = TREE_TYPE (source);
12382 if (TREE_CODE (dest) == POINTER_TYPE)
12383 dest = TREE_TYPE (dest);
12384 /* Case where SOURCE is a class type */
12385 if (TYPE_CLASS_P (source))
12387 if (TYPE_CLASS_P (dest))
12388 return (source == dest
12389 || inherits_from_p (source, dest)
12390 || (cast && inherits_from_p (dest, source)));
12391 if (TYPE_INTERFACE_P (dest))
12393 /* If doing a cast and SOURCE is final, the operation is
12394 always correct a compile time (because even if SOURCE
12395 does not implement DEST, a subclass of SOURCE might). */
12396 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12397 return 1;
12398 /* Otherwise, SOURCE must implement DEST */
12399 return interface_of_p (dest, source);
12401 /* DEST is an array, cast permited if SOURCE is of Object type */
12402 return (cast && source == object_type_node ? 1 : 0);
12404 if (TYPE_INTERFACE_P (source))
12406 if (TYPE_CLASS_P (dest))
12408 /* If not casting, DEST must be the Object type */
12409 if (!cast)
12410 return dest == object_type_node;
12411 /* We're doing a cast. The cast is always valid is class
12412 DEST is not final, otherwise, DEST must implement SOURCE */
12413 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12414 return 1;
12415 else
12416 return interface_of_p (source, dest);
12418 if (TYPE_INTERFACE_P (dest))
12420 /* If doing a cast, then if SOURCE and DEST contain method
12421 with the same signature but different return type, then
12422 this is a (compile time) error */
12423 if (cast)
12425 tree method_source, method_dest;
12426 tree source_type;
12427 tree source_sig;
12428 tree source_name;
12429 for (method_source = TYPE_METHODS (source); method_source;
12430 method_source = TREE_CHAIN (method_source))
12432 source_sig =
12433 build_java_argument_signature (TREE_TYPE (method_source));
12434 source_type = TREE_TYPE (TREE_TYPE (method_source));
12435 source_name = DECL_NAME (method_source);
12436 for (method_dest = TYPE_METHODS (dest);
12437 method_dest; method_dest = TREE_CHAIN (method_dest))
12438 if (source_sig ==
12439 build_java_argument_signature (TREE_TYPE (method_dest))
12440 && source_name == DECL_NAME (method_dest)
12441 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12442 return 0;
12444 return 1;
12446 else
12447 return source == dest || interface_of_p (dest, source);
12449 else
12451 /* Array */
12452 return (cast
12453 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12454 || (DECL_NAME (TYPE_NAME (source))
12455 == java_io_serializable)));
12458 if (TYPE_ARRAY_P (source))
12460 if (TYPE_CLASS_P (dest))
12461 return dest == object_type_node;
12462 /* Can't cast an array to an interface unless the interface is
12463 java.lang.Cloneable or java.io.Serializable. */
12464 if (TYPE_INTERFACE_P (dest))
12465 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12466 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
12467 else /* Arrays */
12469 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12470 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12472 /* In case of severe errors, they turn out null */
12473 if (!dest_element_type || !source_element_type)
12474 return 0;
12475 if (source_element_type == dest_element_type)
12476 return 1;
12477 return valid_ref_assignconv_cast_p (source_element_type,
12478 dest_element_type, cast);
12480 return 0;
12482 return 0;
12485 static int
12486 valid_cast_to_p (source, dest)
12487 tree source;
12488 tree dest;
12490 if (TREE_CODE (source) == POINTER_TYPE)
12491 source = TREE_TYPE (source);
12492 if (TREE_CODE (dest) == POINTER_TYPE)
12493 dest = TREE_TYPE (dest);
12495 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12496 return valid_ref_assignconv_cast_p (source, dest, 1);
12498 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12499 return 1;
12501 else if (TREE_CODE (source) == BOOLEAN_TYPE
12502 && TREE_CODE (dest) == BOOLEAN_TYPE)
12503 return 1;
12505 return 0;
12508 static tree
12509 do_unary_numeric_promotion (arg)
12510 tree arg;
12512 tree type = TREE_TYPE (arg);
12513 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12514 || TREE_CODE (type) == CHAR_TYPE)
12515 arg = convert (int_type_node, arg);
12516 return arg;
12519 /* Return a non zero value if SOURCE can be converted into DEST using
12520 the method invocation conversion rule (5.3). */
12521 static int
12522 valid_method_invocation_conversion_p (dest, source)
12523 tree dest, source;
12525 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12526 && valid_builtin_assignconv_identity_widening_p (dest, source))
12527 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12528 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12529 && valid_ref_assignconv_cast_p (source, dest, 0)));
12532 /* Build an incomplete binop expression. */
12534 static tree
12535 build_binop (op, op_location, op1, op2)
12536 enum tree_code op;
12537 int op_location;
12538 tree op1, op2;
12540 tree binop = build (op, NULL_TREE, op1, op2);
12541 TREE_SIDE_EFFECTS (binop) = 1;
12542 /* Store the location of the operator, for better error report. The
12543 string of the operator will be rebuild based on the OP value. */
12544 EXPR_WFL_LINECOL (binop) = op_location;
12545 return binop;
12548 /* Build the string of the operator retained by NODE. If NODE is part
12549 of a compound expression, add an '=' at the end of the string. This
12550 function is called when an error needs to be reported on an
12551 operator. The string is returned as a pointer to a static character
12552 buffer. */
12554 static char *
12555 operator_string (node)
12556 tree node;
12558 #define BUILD_OPERATOR_STRING(S) \
12560 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12561 return buffer; \
12564 static char buffer [10];
12565 switch (TREE_CODE (node))
12567 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12568 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12569 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12570 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12571 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12572 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12573 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12574 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12575 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12576 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12577 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12578 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12579 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12580 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12581 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12582 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12583 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12584 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12585 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
12586 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12587 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12588 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12589 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12590 case PREINCREMENT_EXPR: /* Fall through */
12591 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12592 case PREDECREMENT_EXPR: /* Fall through */
12593 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12594 default:
12595 fatal ("unregistered operator %s - operator_string",
12596 tree_code_name [TREE_CODE (node)]);
12598 return NULL;
12599 #undef BUILD_OPERATOR_STRING
12602 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12604 static int
12605 java_decl_equiv (var_acc1, var_acc2)
12606 tree var_acc1, var_acc2;
12608 if (JDECL_P (var_acc1))
12609 return (var_acc1 == var_acc2);
12611 return (TREE_CODE (var_acc1) == COMPONENT_REF
12612 && TREE_CODE (var_acc2) == COMPONENT_REF
12613 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12614 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12615 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12618 /* Return a non zero value if CODE is one of the operators that can be
12619 used in conjunction with the `=' operator in a compound assignment. */
12621 static int
12622 binop_compound_p (code)
12623 enum tree_code code;
12625 int i;
12626 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12627 if (binop_lookup [i] == code)
12628 break;
12630 return i < BINOP_COMPOUND_CANDIDATES;
12633 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12635 static tree
12636 java_refold (t)
12637 tree t;
12639 tree c, b, ns, decl;
12641 if (TREE_CODE (t) != MODIFY_EXPR)
12642 return t;
12644 c = TREE_OPERAND (t, 1);
12645 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12646 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12647 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12648 return t;
12650 /* Now the left branch of the binary operator. */
12651 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12652 if (! (b && TREE_CODE (b) == NOP_EXPR
12653 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12654 return t;
12656 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12657 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12658 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12659 return t;
12661 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12662 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12663 /* It's got to be the an equivalent decl */
12664 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12666 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12667 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12668 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12669 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12670 /* Change the right part of the BINOP_EXPR */
12671 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12674 return t;
12677 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
12678 errors but we modify NODE so that it contains the type computed
12679 according to the expression, when it's fixed. Otherwise, we write
12680 error_mark_node as the type. It allows us to further the analysis
12681 of remaining nodes and detects more errors in certain cases. */
12683 static tree
12684 patch_binop (node, wfl_op1, wfl_op2)
12685 tree node;
12686 tree wfl_op1;
12687 tree wfl_op2;
12689 tree op1 = TREE_OPERAND (node, 0);
12690 tree op2 = TREE_OPERAND (node, 1);
12691 tree op1_type = TREE_TYPE (op1);
12692 tree op2_type = TREE_TYPE (op2);
12693 tree prom_type = NULL_TREE, cn;
12694 int code = TREE_CODE (node);
12696 /* If 1, tell the routine that we have to return error_mark_node
12697 after checking for the initialization of the RHS */
12698 int error_found = 0;
12700 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12702 switch (code)
12704 /* 15.16 Multiplicative operators */
12705 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12706 case RDIV_EXPR: /* 15.16.2 Division Operator / */
12707 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
12708 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
12709 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
12711 if (!JNUMERIC_TYPE_P (op1_type))
12712 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12713 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12714 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12715 TREE_TYPE (node) = error_mark_node;
12716 error_found = 1;
12717 break;
12719 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12720 /* Change the division operator if necessary */
12721 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12722 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
12724 if (TREE_CODE (prom_type) == INTEGER_TYPE
12725 && flag_use_divide_subroutine
12726 && ! flag_emit_class_files
12727 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12728 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12730 /* This one is more complicated. FLOATs are processed by a
12731 function call to soft_fmod. Duplicate the value of the
12732 COMPOUND_ASSIGN_P flag. */
12733 if (code == TRUNC_MOD_EXPR)
12735 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12736 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
12737 TREE_SIDE_EFFECTS (mod)
12738 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12739 return mod;
12741 break;
12743 /* 15.17 Additive Operators */
12744 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
12746 /* Operation is valid if either one argument is a string
12747 constant, a String object or a StringBuffer crafted for the
12748 purpose of the a previous usage of the String concatenation
12749 operator */
12751 if (TREE_CODE (op1) == STRING_CST
12752 || TREE_CODE (op2) == STRING_CST
12753 || JSTRING_TYPE_P (op1_type)
12754 || JSTRING_TYPE_P (op2_type)
12755 || IS_CRAFTED_STRING_BUFFER_P (op1)
12756 || IS_CRAFTED_STRING_BUFFER_P (op2))
12757 return build_string_concatenation (op1, op2);
12759 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12760 Numeric Types */
12761 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
12763 if (!JNUMERIC_TYPE_P (op1_type))
12764 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12765 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12766 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12767 TREE_TYPE (node) = error_mark_node;
12768 error_found = 1;
12769 break;
12771 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12772 break;
12774 /* 15.18 Shift Operators */
12775 case LSHIFT_EXPR:
12776 case RSHIFT_EXPR:
12777 case URSHIFT_EXPR:
12778 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12780 if (!JINTEGRAL_TYPE_P (op1_type))
12781 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12782 else
12784 if (JNUMERIC_TYPE_P (op2_type))
12785 parse_error_context (wfl_operator,
12786 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
12787 operator_string (node),
12788 lang_printable_name (op2_type, 0));
12789 else
12790 parse_error_context (wfl_operator,
12791 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
12792 operator_string (node),
12793 lang_printable_name (op2_type, 0));
12795 TREE_TYPE (node) = error_mark_node;
12796 error_found = 1;
12797 break;
12800 /* Unary numeric promotion (5.6.1) is performed on each operand
12801 separatly */
12802 op1 = do_unary_numeric_promotion (op1);
12803 op2 = do_unary_numeric_promotion (op2);
12805 /* The type of the shift expression is the type of the promoted
12806 type of the left-hand operand */
12807 prom_type = TREE_TYPE (op1);
12809 /* Shift int only up to 0x1f and long up to 0x3f */
12810 if (prom_type == int_type_node)
12811 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12812 build_int_2 (0x1f, 0)));
12813 else
12814 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12815 build_int_2 (0x3f, 0)));
12817 /* The >>> operator is a >> operating on unsigned quantities */
12818 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
12820 tree to_return;
12821 tree utype = unsigned_type (prom_type);
12822 op1 = convert (utype, op1);
12823 TREE_SET_CODE (node, RSHIFT_EXPR);
12824 TREE_OPERAND (node, 0) = op1;
12825 TREE_OPERAND (node, 1) = op2;
12826 TREE_TYPE (node) = utype;
12827 to_return = convert (prom_type, node);
12828 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12829 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
12830 TREE_SIDE_EFFECTS (to_return)
12831 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12832 return to_return;
12834 break;
12836 /* 15.19.1 Type Comparison Operator instaceof */
12837 case INSTANCEOF_EXPR:
12839 TREE_TYPE (node) = boolean_type_node;
12841 if (!(op2_type = resolve_type_during_patch (op2)))
12842 return error_mark_node;
12844 /* The first operand must be a reference type or the null type */
12845 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12846 error_found = 1; /* Error reported further below */
12848 /* The second operand must be a reference type */
12849 if (!JREFERENCE_TYPE_P (op2_type))
12851 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12852 parse_error_context
12853 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12854 lang_printable_name (op2_type, 0));
12855 error_found = 1;
12858 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12860 /* If the first operand is null, the result is always false */
12861 if (op1 == null_pointer_node)
12862 return boolean_false_node;
12863 else if (flag_emit_class_files)
12865 TREE_OPERAND (node, 1) = op2_type;
12866 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
12867 return node;
12869 /* Otherwise we have to invoke instance of to figure it out */
12870 else
12871 return build_instanceof (op1, op2_type);
12873 /* There is no way the expression operand can be an instance of
12874 the type operand. This is a compile time error. */
12875 else
12877 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
12878 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12879 parse_error_context
12880 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12881 t1, lang_printable_name (op2_type, 0));
12882 free (t1);
12883 error_found = 1;
12886 break;
12888 /* 15.21 Bitwise and Logical Operators */
12889 case BIT_AND_EXPR:
12890 case BIT_XOR_EXPR:
12891 case BIT_IOR_EXPR:
12892 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12893 /* Binary numeric promotion is performed on both operand and the
12894 expression retain that type */
12895 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12897 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12898 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12899 /* The type of the bitwise operator expression is BOOLEAN */
12900 prom_type = boolean_type_node;
12901 else
12903 if (!JINTEGRAL_TYPE_P (op1_type))
12904 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12905 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12906 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12907 TREE_TYPE (node) = error_mark_node;
12908 error_found = 1;
12909 /* Insert a break here if adding thing before the switch's
12910 break for this case */
12912 break;
12914 /* 15.22 Conditional-And Operator */
12915 case TRUTH_ANDIF_EXPR:
12916 /* 15.23 Conditional-Or Operator */
12917 case TRUTH_ORIF_EXPR:
12918 /* Operands must be of BOOLEAN type */
12919 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12920 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12922 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12923 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12924 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12925 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12926 TREE_TYPE (node) = boolean_type_node;
12927 error_found = 1;
12928 break;
12930 /* The type of the conditional operators is BOOLEAN */
12931 prom_type = boolean_type_node;
12932 break;
12934 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12935 case LT_EXPR:
12936 case GT_EXPR:
12937 case LE_EXPR:
12938 case GE_EXPR:
12939 /* The type of each of the operands must be a primitive numeric
12940 type */
12941 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12943 if (!JNUMERIC_TYPE_P (op1_type))
12944 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12945 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12946 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12947 TREE_TYPE (node) = boolean_type_node;
12948 error_found = 1;
12949 break;
12951 /* Binary numeric promotion is performed on the operands */
12952 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12953 /* The type of the relation expression is always BOOLEAN */
12954 prom_type = boolean_type_node;
12955 break;
12957 /* 15.20 Equality Operator */
12958 case EQ_EXPR:
12959 case NE_EXPR:
12960 /* It's time for us to patch the strings. */
12961 if ((cn = patch_string (op1)))
12963 op1 = cn;
12964 op1_type = TREE_TYPE (op1);
12966 if ((cn = patch_string (op2)))
12968 op2 = cn;
12969 op2_type = TREE_TYPE (op2);
12972 /* 15.20.1 Numerical Equality Operators == and != */
12973 /* Binary numeric promotion is performed on the operands */
12974 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
12975 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12977 /* 15.20.2 Boolean Equality Operators == and != */
12978 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
12979 TREE_CODE (op2_type) == BOOLEAN_TYPE)
12980 ; /* Nothing to do here */
12982 /* 15.20.3 Reference Equality Operators == and != */
12983 /* Types have to be either references or the null type. If
12984 they're references, it must be possible to convert either
12985 type to the other by casting conversion. */
12986 else if (op1 == null_pointer_node || op2 == null_pointer_node
12987 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
12988 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
12989 || valid_ref_assignconv_cast_p (op2_type,
12990 op1_type, 1))))
12991 ; /* Nothing to do here */
12993 /* Else we have an error figure what can't be converted into
12994 what and report the error */
12995 else
12997 char *t1;
12998 t1 = xstrdup (lang_printable_name (op1_type, 0));
12999 parse_error_context
13000 (wfl_operator,
13001 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13002 operator_string (node), t1,
13003 lang_printable_name (op2_type, 0));
13004 free (t1);
13005 TREE_TYPE (node) = boolean_type_node;
13006 error_found = 1;
13007 break;
13009 prom_type = boolean_type_node;
13010 break;
13013 if (error_found)
13014 return error_mark_node;
13016 TREE_OPERAND (node, 0) = op1;
13017 TREE_OPERAND (node, 1) = op2;
13018 TREE_TYPE (node) = prom_type;
13019 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13021 if (flag_emit_xref)
13022 return node;
13024 /* fold does not respect side-effect order as required for Java but not C.
13025 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13026 * bytecode.
13028 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13029 : ! TREE_SIDE_EFFECTS (node))
13030 node = fold (node);
13031 return node;
13034 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13035 zero value, the value of CSTE comes after the valude of STRING */
13037 static tree
13038 do_merge_string_cste (cste, string, string_len, after)
13039 tree cste;
13040 const char *string;
13041 int string_len, after;
13043 const char *old = TREE_STRING_POINTER (cste);
13044 int old_len = TREE_STRING_LENGTH (cste);
13045 int len = old_len + string_len;
13046 char *new;
13048 cste = make_node (STRING_CST);
13049 TREE_STRING_LENGTH (cste) = len;
13050 new = TREE_STRING_POINTER (cste) = ggc_alloc (len+1);
13052 if (after)
13054 memcpy (new, string, string_len);
13055 memcpy (&new [string_len], old, old_len);
13057 else
13059 memcpy (new, old, old_len);
13060 memcpy (&new [old_len], string, string_len);
13062 new [len] = '\0';
13063 return cste;
13066 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13067 new STRING_CST on success, NULL_TREE on failure */
13069 static tree
13070 merge_string_cste (op1, op2, after)
13071 tree op1, op2;
13072 int after;
13074 /* Handle two string constants right away */
13075 if (TREE_CODE (op2) == STRING_CST)
13076 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13077 TREE_STRING_LENGTH (op2), after);
13079 /* Reasonable integer constant can be treated right away */
13080 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13082 static const char *boolean_true = "true";
13083 static const char *boolean_false = "false";
13084 static const char *null_pointer = "null";
13085 char ch[3];
13086 const char *string;
13088 if (op2 == boolean_true_node)
13089 string = boolean_true;
13090 else if (op2 == boolean_false_node)
13091 string = boolean_false;
13092 else if (op2 == null_pointer_node)
13093 string = null_pointer;
13094 else if (TREE_TYPE (op2) == char_type_node)
13096 ch[0] = (char )TREE_INT_CST_LOW (op2);
13097 ch[1] = '\0';
13098 string = ch;
13100 else
13101 string = print_int_node (op2);
13103 return do_merge_string_cste (op1, string, strlen (string), after);
13105 return NULL_TREE;
13108 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13109 has to be a STRING_CST and the other part must be a STRING_CST or a
13110 INTEGRAL constant. Return a new STRING_CST if the operation
13111 succeed, NULL_TREE otherwise.
13113 If the case we want to optimize for space, we might want to return
13114 NULL_TREE for each invocation of this routine. FIXME */
13116 static tree
13117 string_constant_concatenation (op1, op2)
13118 tree op1, op2;
13120 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13122 tree string, rest;
13123 int invert;
13125 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13126 rest = (string == op1 ? op2 : op1);
13127 invert = (string == op1 ? 0 : 1 );
13129 /* Walk REST, only if it looks reasonable */
13130 if (TREE_CODE (rest) != STRING_CST
13131 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13132 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13133 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13135 rest = java_complete_tree (rest);
13136 if (rest == error_mark_node)
13137 return error_mark_node;
13138 rest = fold (rest);
13140 return merge_string_cste (string, rest, invert);
13142 return NULL_TREE;
13145 /* Implement the `+' operator. Does static optimization if possible,
13146 otherwise create (if necessary) and append elements to a
13147 StringBuffer. The StringBuffer will be carried around until it is
13148 used for a function call or an assignment. Then toString() will be
13149 called on it to turn it into a String object. */
13151 static tree
13152 build_string_concatenation (op1, op2)
13153 tree op1, op2;
13155 tree result;
13156 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13158 if (flag_emit_xref)
13159 return build (PLUS_EXPR, string_type_node, op1, op2);
13161 /* Try to do some static optimization */
13162 if ((result = string_constant_concatenation (op1, op2)))
13163 return result;
13165 /* Discard empty strings on either side of the expression */
13166 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13168 op1 = op2;
13169 op2 = NULL_TREE;
13171 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13172 op2 = NULL_TREE;
13174 /* If operands are string constant, turn then into object references */
13175 if (TREE_CODE (op1) == STRING_CST)
13176 op1 = patch_string_cst (op1);
13177 if (op2 && TREE_CODE (op2) == STRING_CST)
13178 op2 = patch_string_cst (op2);
13180 /* If either one of the constant is null and the other non null
13181 operand is a String object, return it. */
13182 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13183 return op1;
13185 /* If OP1 isn't already a StringBuffer, create and
13186 initialize a new one */
13187 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13189 /* Two solutions here:
13190 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13191 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13192 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13193 op1 = BUILD_STRING_BUFFER (op1);
13194 else
13196 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13197 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13201 if (op2)
13203 /* OP1 is no longer the last node holding a crafted StringBuffer */
13204 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13205 /* Create a node for `{new...,xxx}.append (op2)' */
13206 if (op2)
13207 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13210 /* Mark the last node holding a crafted StringBuffer */
13211 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13213 TREE_SIDE_EFFECTS (op1) = side_effects;
13214 return op1;
13217 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13218 StringBuffer. If no string were found to be patched, return
13219 NULL. */
13221 static tree
13222 patch_string (node)
13223 tree node;
13225 if (node == error_mark_node)
13226 return error_mark_node;
13227 if (TREE_CODE (node) == STRING_CST)
13228 return patch_string_cst (node);
13229 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13231 int saved = ctxp->explicit_constructor_p;
13232 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13233 tree ret;
13234 /* Temporary disable forbid the use of `this'. */
13235 ctxp->explicit_constructor_p = 0;
13236 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13237 /* String concatenation arguments must be evaluated in order too. */
13238 ret = force_evaluation_order (ret);
13239 /* Restore it at its previous value */
13240 ctxp->explicit_constructor_p = saved;
13241 return ret;
13243 return NULL_TREE;
13246 /* Build the internal representation of a string constant. */
13248 static tree
13249 patch_string_cst (node)
13250 tree node;
13252 int location;
13253 if (! flag_emit_class_files)
13255 node = get_identifier (TREE_STRING_POINTER (node));
13256 location = alloc_name_constant (CONSTANT_String, node);
13257 node = build_ref_from_constant_pool (location);
13259 TREE_TYPE (node) = string_ptr_type_node;
13260 TREE_CONSTANT (node) = 1;
13261 return node;
13264 /* Build an incomplete unary operator expression. */
13266 static tree
13267 build_unaryop (op_token, op_location, op1)
13268 int op_token, op_location;
13269 tree op1;
13271 enum tree_code op;
13272 tree unaryop;
13273 switch (op_token)
13275 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13276 case MINUS_TK: op = NEGATE_EXPR; break;
13277 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13278 case NOT_TK: op = BIT_NOT_EXPR; break;
13279 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13280 op_token);
13283 unaryop = build1 (op, NULL_TREE, op1);
13284 TREE_SIDE_EFFECTS (unaryop) = 1;
13285 /* Store the location of the operator, for better error report. The
13286 string of the operator will be rebuild based on the OP value. */
13287 EXPR_WFL_LINECOL (unaryop) = op_location;
13288 return unaryop;
13291 /* Special case for the ++/-- operators, since they require an extra
13292 argument to build, which is set to NULL and patched
13293 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13295 static tree
13296 build_incdec (op_token, op_location, op1, is_post_p)
13297 int op_token, op_location;
13298 tree op1;
13299 int is_post_p;
13301 static enum tree_code lookup [2][2] =
13303 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13304 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13306 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13307 NULL_TREE, op1, NULL_TREE);
13308 TREE_SIDE_EFFECTS (node) = 1;
13309 /* Store the location of the operator, for better error report. The
13310 string of the operator will be rebuild based on the OP value. */
13311 EXPR_WFL_LINECOL (node) = op_location;
13312 return node;
13315 /* Build an incomplete cast operator, based on the use of the
13316 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13317 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13318 though its type is already set. */
13320 static tree
13321 build_cast (location, type, exp)
13322 int location;
13323 tree type, exp;
13325 tree node = build1 (CONVERT_EXPR, type, exp);
13326 EXPR_WFL_LINECOL (node) = location;
13327 return node;
13330 /* Build an incomplete class reference operator. */
13331 static tree
13332 build_incomplete_class_ref (location, class_name)
13333 int location;
13334 tree class_name;
13336 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13337 EXPR_WFL_LINECOL (node) = location;
13338 return node;
13341 /* Complete an incomplete class reference operator. */
13342 static tree
13343 patch_incomplete_class_ref (node)
13344 tree node;
13346 tree type = TREE_OPERAND (node, 0);
13347 tree ref_type;
13349 if (!(ref_type = resolve_type_during_patch (type)))
13350 return error_mark_node;
13352 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13354 /* A class referenced by `foo.class' is initialized. */
13355 return build_class_init (ref_type, build_class_ref (ref_type));
13358 /* If we're emitting class files and we have to deal with non
13359 primitive types, we invoke (and consider generating) the
13360 synthetic static method `class$'. */
13361 if (!TYPE_DOT_CLASS (current_class))
13362 build_dot_class_method (current_class);
13363 ref_type = build_dot_class_method_invocation (ref_type);
13364 return java_complete_tree (ref_type);
13367 /* 15.14 Unary operators. We return error_mark_node in case of error,
13368 but preserve the type of NODE if the type is fixed. */
13370 static tree
13371 patch_unaryop (node, wfl_op)
13372 tree node;
13373 tree wfl_op;
13375 tree op = TREE_OPERAND (node, 0);
13376 tree op_type = TREE_TYPE (op);
13377 tree prom_type = NULL_TREE, value, decl;
13378 int outer_field_flag = 0;
13379 int code = TREE_CODE (node);
13380 int error_found = 0;
13382 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13384 switch (code)
13386 /* 15.13.2 Postfix Increment Operator ++ */
13387 case POSTINCREMENT_EXPR:
13388 /* 15.13.3 Postfix Increment Operator -- */
13389 case POSTDECREMENT_EXPR:
13390 /* 15.14.1 Prefix Increment Operator ++ */
13391 case PREINCREMENT_EXPR:
13392 /* 15.14.2 Prefix Decrement Operator -- */
13393 case PREDECREMENT_EXPR:
13394 op = decl = strip_out_static_field_access_decl (op);
13395 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13396 /* We might be trying to change an outer field accessed using
13397 access method. */
13398 if (outer_field_flag)
13400 /* Retrieve the decl of the field we're trying to access. We
13401 do that by first retrieving the function we would call to
13402 access the field. It has been already verified that this
13403 field isn't final */
13404 if (flag_emit_class_files)
13405 decl = TREE_OPERAND (op, 0);
13406 else
13407 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13408 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13410 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13411 else if (!JDECL_P (decl)
13412 && TREE_CODE (decl) != COMPONENT_REF
13413 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13414 && TREE_CODE (decl) != INDIRECT_REF
13415 && !(TREE_CODE (decl) == COMPOUND_EXPR
13416 && TREE_OPERAND (decl, 1)
13417 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13419 tree lvalue;
13420 /* Before screaming, check that we're not in fact trying to
13421 increment a optimized static final access, in which case
13422 we issue an different error message. */
13423 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13424 && resolve_expression_name (wfl_op, &lvalue)
13425 && check_final_assignment (lvalue, wfl_op)))
13426 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13427 operator_string (node));
13428 TREE_TYPE (node) = error_mark_node;
13429 error_found = 1;
13432 if (check_final_assignment (op, wfl_op))
13433 error_found = 1;
13435 /* From now on, we know that op if a variable and that it has a
13436 valid wfl. We use wfl_op to locate errors related to the
13437 ++/-- operand. */
13438 else if (!JNUMERIC_TYPE_P (op_type))
13440 parse_error_context
13441 (wfl_op, "Invalid argument type `%s' to `%s'",
13442 lang_printable_name (op_type, 0), operator_string (node));
13443 TREE_TYPE (node) = error_mark_node;
13444 error_found = 1;
13446 else
13448 /* Before the addition, binary numeric promotion is performed on
13449 both operands, if really necessary */
13450 if (JINTEGRAL_TYPE_P (op_type))
13452 value = build_int_2 (1, 0);
13453 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13455 else
13457 value = build_int_2 (1, 0);
13458 TREE_TYPE (node) =
13459 binary_numeric_promotion (op_type,
13460 TREE_TYPE (value), &op, &value);
13463 /* We remember we might be accessing an outer field */
13464 if (outer_field_flag)
13466 /* We re-generate an access to the field */
13467 value = build (PLUS_EXPR, TREE_TYPE (op),
13468 build_outer_field_access (wfl_op, decl), value);
13470 /* And we patch the original access$() into a write
13471 with plus_op as a rhs */
13472 return outer_field_access_fix (node, op, value);
13475 /* And write back into the node. */
13476 TREE_OPERAND (node, 0) = op;
13477 TREE_OPERAND (node, 1) = value;
13478 /* Convert the overall back into its original type, if
13479 necessary, and return */
13480 if (JINTEGRAL_TYPE_P (op_type))
13481 return fold (node);
13482 else
13483 return fold (convert (op_type, node));
13485 break;
13487 /* 15.14.3 Unary Plus Operator + */
13488 case UNARY_PLUS_EXPR:
13489 /* 15.14.4 Unary Minus Operator - */
13490 case NEGATE_EXPR:
13491 if (!JNUMERIC_TYPE_P (op_type))
13493 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13494 TREE_TYPE (node) = error_mark_node;
13495 error_found = 1;
13497 /* Unary numeric promotion is performed on operand */
13498 else
13500 op = do_unary_numeric_promotion (op);
13501 prom_type = TREE_TYPE (op);
13502 if (code == UNARY_PLUS_EXPR)
13503 return fold (op);
13505 break;
13507 /* 15.14.5 Bitwise Complement Operator ~ */
13508 case BIT_NOT_EXPR:
13509 if (!JINTEGRAL_TYPE_P (op_type))
13511 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13512 TREE_TYPE (node) = error_mark_node;
13513 error_found = 1;
13515 else
13517 op = do_unary_numeric_promotion (op);
13518 prom_type = TREE_TYPE (op);
13520 break;
13522 /* 15.14.6 Logical Complement Operator ! */
13523 case TRUTH_NOT_EXPR:
13524 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13526 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
13527 /* But the type is known. We will report an error if further
13528 attempt of a assignment is made with this rhs */
13529 TREE_TYPE (node) = boolean_type_node;
13530 error_found = 1;
13532 else
13533 prom_type = boolean_type_node;
13534 break;
13536 /* 15.15 Cast Expression */
13537 case CONVERT_EXPR:
13538 value = patch_cast (node, wfl_operator);
13539 if (value == error_mark_node)
13541 /* If this cast is part of an assignment, we tell the code
13542 that deals with it not to complain about a mismatch,
13543 because things have been cast, anyways */
13544 TREE_TYPE (node) = error_mark_node;
13545 error_found = 1;
13547 else
13549 value = fold (value);
13550 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13551 return value;
13553 break;
13556 if (error_found)
13557 return error_mark_node;
13559 /* There are cases where node has been replaced by something else
13560 and we don't end up returning here: UNARY_PLUS_EXPR,
13561 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
13562 TREE_OPERAND (node, 0) = fold (op);
13563 TREE_TYPE (node) = prom_type;
13564 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
13565 return fold (node);
13568 /* Generic type resolution that sometimes takes place during node
13569 patching. Returned the resolved type or generate an error
13570 message. Return the resolved type or NULL_TREE. */
13572 static tree
13573 resolve_type_during_patch (type)
13574 tree type;
13576 if (unresolved_type_p (type, NULL))
13578 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
13579 if (!type_decl)
13581 parse_error_context (type,
13582 "Class `%s' not found in type declaration",
13583 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13584 return NULL_TREE;
13586 else
13588 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13589 return TREE_TYPE (type_decl);
13592 return type;
13594 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
13595 found. Otherwise NODE or something meant to replace it is returned. */
13597 static tree
13598 patch_cast (node, wfl_op)
13599 tree node;
13600 tree wfl_op;
13602 tree op = TREE_OPERAND (node, 0);
13603 tree op_type = TREE_TYPE (op);
13604 tree cast_type = TREE_TYPE (node);
13605 char *t1;
13607 /* First resolve OP_TYPE if unresolved */
13608 if (!(cast_type = resolve_type_during_patch (cast_type)))
13609 return error_mark_node;
13611 /* Check on cast that are proven correct at compile time */
13612 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13614 /* Same type */
13615 if (cast_type == op_type)
13616 return node;
13618 /* float and double type are converted to the original type main
13619 variant and then to the target type. */
13620 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13621 op = convert (integer_type_node, op);
13623 /* Try widening/narowwing convertion. Potentially, things need
13624 to be worked out in gcc so we implement the extreme cases
13625 correctly. fold_convert() needs to be fixed. */
13626 return convert (cast_type, op);
13629 /* It's also valid to cast a boolean into a boolean */
13630 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13631 return node;
13633 /* null can be casted to references */
13634 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13635 return build_null_of_type (cast_type);
13637 /* The remaining legal casts involve conversion between reference
13638 types. Check for their compile time correctness. */
13639 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
13640 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
13642 TREE_TYPE (node) = promote_type (cast_type);
13643 /* Now, the case can be determined correct at compile time if
13644 OP_TYPE can be converted into CAST_TYPE by assignment
13645 conversion (5.2) */
13647 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
13649 TREE_SET_CODE (node, NOP_EXPR);
13650 return node;
13653 if (flag_emit_class_files)
13655 TREE_SET_CODE (node, CONVERT_EXPR);
13656 return node;
13659 /* The cast requires a run-time check */
13660 return build (CALL_EXPR, promote_type (cast_type),
13661 build_address_of (soft_checkcast_node),
13662 tree_cons (NULL_TREE, build_class_ref (cast_type),
13663 build_tree_list (NULL_TREE, op)),
13664 NULL_TREE);
13667 /* Any other casts are proven incorrect at compile time */
13668 t1 = xstrdup (lang_printable_name (op_type, 0));
13669 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
13670 t1, lang_printable_name (cast_type, 0));
13671 free (t1);
13672 return error_mark_node;
13675 /* Build a null constant and give it the type TYPE. */
13677 static tree
13678 build_null_of_type (type)
13679 tree type;
13681 tree node = build_int_2 (0, 0);
13682 TREE_TYPE (node) = promote_type (type);
13683 return node;
13686 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13687 a list of indices. */
13688 static tree
13689 build_array_ref (location, array, index)
13690 int location;
13691 tree array, index;
13693 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13694 EXPR_WFL_LINECOL (node) = location;
13695 return node;
13698 /* 15.12 Array Access Expression */
13700 static tree
13701 patch_array_ref (node)
13702 tree node;
13704 tree array = TREE_OPERAND (node, 0);
13705 tree array_type = TREE_TYPE (array);
13706 tree index = TREE_OPERAND (node, 1);
13707 tree index_type = TREE_TYPE (index);
13708 int error_found = 0;
13710 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13712 if (TREE_CODE (array_type) == POINTER_TYPE)
13713 array_type = TREE_TYPE (array_type);
13715 /* The array reference must be an array */
13716 if (!TYPE_ARRAY_P (array_type))
13718 parse_error_context
13719 (wfl_operator,
13720 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13721 lang_printable_name (array_type, 0));
13722 TREE_TYPE (node) = error_mark_node;
13723 error_found = 1;
13726 /* The array index undergoes unary numeric promotion. The promoted
13727 type must be int */
13728 index = do_unary_numeric_promotion (index);
13729 if (TREE_TYPE (index) != int_type_node)
13731 if (valid_cast_to_p (index_type, int_type_node))
13732 parse_error_context (wfl_operator,
13733 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
13734 lang_printable_name (index_type, 0));
13735 else
13736 parse_error_context (wfl_operator,
13737 "Incompatible type for `[]'. Can't convert `%s' to `int'",
13738 lang_printable_name (index_type, 0));
13739 TREE_TYPE (node) = error_mark_node;
13740 error_found = 1;
13743 if (error_found)
13744 return error_mark_node;
13746 array_type = TYPE_ARRAY_ELEMENT (array_type);
13748 if (flag_emit_class_files || flag_emit_xref)
13750 TREE_OPERAND (node, 0) = array;
13751 TREE_OPERAND (node, 1) = index;
13753 else
13755 /* The save_expr is for correct evaluation order. It would be cleaner
13756 to use force_evaluation_order (see comment there), but that is
13757 difficult when we also have to deal with bounds checking. */
13758 if (TREE_SIDE_EFFECTS (index))
13759 array = save_expr (array);
13760 node = build_java_arrayaccess (array, array_type, index);
13761 if (TREE_SIDE_EFFECTS (index))
13762 node = build (COMPOUND_EXPR, array_type, array, node);
13764 TREE_TYPE (node) = array_type;
13765 return node;
13768 /* 15.9 Array Creation Expressions */
13770 static tree
13771 build_newarray_node (type, dims, extra_dims)
13772 tree type;
13773 tree dims;
13774 int extra_dims;
13776 tree node =
13777 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
13778 build_int_2 (extra_dims, 0));
13779 return node;
13782 static tree
13783 patch_newarray (node)
13784 tree node;
13786 tree type = TREE_OPERAND (node, 0);
13787 tree dims = TREE_OPERAND (node, 1);
13788 tree cdim, array_type;
13789 int error_found = 0;
13790 int ndims = 0;
13791 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
13793 /* Dimension types are verified. It's better for the types to be
13794 verified in order. */
13795 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13797 int dim_error = 0;
13798 tree dim = TREE_VALUE (cdim);
13800 /* Dim might have been saved during its evaluation */
13801 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
13803 /* The type of each specified dimension must be an integral type. */
13804 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13805 dim_error = 1;
13807 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13808 promoted type must be int. */
13809 else
13811 dim = do_unary_numeric_promotion (dim);
13812 if (TREE_TYPE (dim) != int_type_node)
13813 dim_error = 1;
13816 /* Report errors on types here */
13817 if (dim_error)
13819 parse_error_context
13820 (TREE_PURPOSE (cdim),
13821 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
13822 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
13823 "Explicit cast needed to" : "Can't"),
13824 lang_printable_name (TREE_TYPE (dim), 0));
13825 error_found = 1;
13828 TREE_PURPOSE (cdim) = NULL_TREE;
13831 /* Resolve array base type if unresolved */
13832 if (!(type = resolve_type_during_patch (type)))
13833 error_found = 1;
13835 if (error_found)
13837 /* We don't want further evaluation of this bogus array creation
13838 operation */
13839 TREE_TYPE (node) = error_mark_node;
13840 return error_mark_node;
13843 /* Set array_type to the actual (promoted) array type of the result. */
13844 if (TREE_CODE (type) == RECORD_TYPE)
13845 type = build_pointer_type (type);
13846 while (--xdims >= 0)
13848 type = promote_type (build_java_array_type (type, -1));
13850 dims = nreverse (dims);
13851 array_type = type;
13852 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13854 type = array_type;
13855 array_type
13856 = build_java_array_type (type,
13857 TREE_CODE (cdim) == INTEGER_CST
13858 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13859 : -1);
13860 array_type = promote_type (array_type);
13862 dims = nreverse (dims);
13864 /* The node is transformed into a function call. Things are done
13865 differently according to the number of dimensions. If the number
13866 of dimension is equal to 1, then the nature of the base type
13867 (primitive or not) matters. */
13868 if (ndims == 1)
13869 return build_new_array (type, TREE_VALUE (dims));
13871 /* Can't reuse what's already written in expr.c because it uses the
13872 JVM stack representation. Provide a build_multianewarray. FIXME */
13873 return build (CALL_EXPR, array_type,
13874 build_address_of (soft_multianewarray_node),
13875 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
13876 tree_cons (NULL_TREE,
13877 build_int_2 (ndims, 0), dims )),
13878 NULL_TREE);
13881 /* 10.6 Array initializer. */
13883 /* Build a wfl for array element that don't have one, so we can
13884 pin-point errors. */
13886 static tree
13887 maybe_build_array_element_wfl (node)
13888 tree node;
13890 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13891 return build_expr_wfl (NULL_TREE, ctxp->filename,
13892 ctxp->elc.line, ctxp->elc.prev_col);
13893 else
13894 return NULL_TREE;
13897 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13898 identification of initialized arrays easier to detect during walk
13899 and expansion. */
13901 static tree
13902 build_new_array_init (location, values)
13903 int location;
13904 tree values;
13906 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13907 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
13908 EXPR_WFL_LINECOL (to_return) = location;
13909 return to_return;
13912 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13913 occurred. Otherwise return NODE after having set its type
13914 appropriately. */
13916 static tree
13917 patch_new_array_init (type, node)
13918 tree type, node;
13920 int error_seen = 0;
13921 tree current, element_type;
13922 HOST_WIDE_INT length;
13923 int all_constant = 1;
13924 tree init = TREE_OPERAND (node, 0);
13926 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13928 parse_error_context (node,
13929 "Invalid array initializer for non-array type `%s'",
13930 lang_printable_name (type, 1));
13931 return error_mark_node;
13933 type = TREE_TYPE (type);
13934 element_type = TYPE_ARRAY_ELEMENT (type);
13936 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13938 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13939 current; length++, current = TREE_CHAIN (current))
13941 tree elt = TREE_VALUE (current);
13942 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
13944 error_seen |= array_constructor_check_entry (element_type, current);
13945 elt = TREE_VALUE (current);
13946 /* When compiling to native code, STRING_CST is converted to
13947 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13948 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
13949 all_constant = 0;
13951 else
13953 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13954 TREE_PURPOSE (current) = NULL_TREE;
13955 all_constant = 0;
13957 if (elt && TREE_CODE (elt) == TREE_LIST
13958 && TREE_VALUE (elt) == error_mark_node)
13959 error_seen = 1;
13962 if (error_seen)
13963 return error_mark_node;
13965 /* Create a new type. We can't reuse the one we have here by
13966 patching its dimension because it originally is of dimension -1
13967 hence reused by gcc. This would prevent triangular arrays. */
13968 type = build_java_array_type (element_type, length);
13969 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13970 TREE_TYPE (node) = promote_type (type);
13971 TREE_CONSTANT (init) = all_constant;
13972 TREE_CONSTANT (node) = all_constant;
13973 return node;
13976 /* Verify that one entry of the initializer element list can be
13977 assigned to the array base type. Report 1 if an error occurred, 0
13978 otherwise. */
13980 static int
13981 array_constructor_check_entry (type, entry)
13982 tree type, entry;
13984 char *array_type_string = NULL; /* For error reports */
13985 tree value, type_value, new_value, wfl_value, patched;
13986 int error_seen = 0;
13988 new_value = NULL_TREE;
13989 wfl_value = TREE_VALUE (entry);
13991 value = java_complete_tree (TREE_VALUE (entry));
13992 /* patch_string return error_mark_node if arg is error_mark_node */
13993 if ((patched = patch_string (value)))
13994 value = patched;
13995 if (value == error_mark_node)
13996 return 1;
13998 type_value = TREE_TYPE (value);
14000 /* At anytime, try_builtin_assignconv can report a warning on
14001 constant overflow during narrowing. */
14002 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14003 new_value = try_builtin_assignconv (wfl_operator, type, value);
14004 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14005 type_value = promote_type (type);
14007 /* Check and report errors */
14008 if (!new_value)
14010 const char *msg = (!valid_cast_to_p (type_value, type) ?
14011 "Can't" : "Explicit cast needed to");
14012 if (!array_type_string)
14013 array_type_string = xstrdup (lang_printable_name (type, 1));
14014 parse_error_context
14015 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14016 msg, lang_printable_name (type_value, 1), array_type_string);
14017 error_seen = 1;
14020 if (new_value)
14022 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
14023 TREE_VALUE (entry) = new_value;
14026 if (array_type_string)
14027 free (array_type_string);
14029 TREE_PURPOSE (entry) = NULL_TREE;
14030 return error_seen;
14033 static tree
14034 build_this (location)
14035 int location;
14037 tree node = build_wfl_node (this_identifier_node);
14038 TREE_SET_CODE (node, THIS_EXPR);
14039 EXPR_WFL_LINECOL (node) = location;
14040 return node;
14043 /* 14.15 The return statement. It builds a modify expression that
14044 assigns the returned value to the RESULT_DECL that hold the value
14045 to be returned. */
14047 static tree
14048 build_return (location, op)
14049 int location;
14050 tree op;
14052 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14053 EXPR_WFL_LINECOL (node) = location;
14054 node = build_debugable_stmt (location, node);
14055 return node;
14058 static tree
14059 patch_return (node)
14060 tree node;
14062 tree return_exp = TREE_OPERAND (node, 0);
14063 tree meth = current_function_decl;
14064 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14065 int error_found = 0;
14067 TREE_TYPE (node) = error_mark_node;
14068 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14070 /* It's invalid to have a return value within a function that is
14071 declared with the keyword void or that is a constructor */
14072 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14073 error_found = 1;
14075 /* It's invalid to use a return statement in a static block */
14076 if (DECL_CLINIT_P (current_function_decl))
14077 error_found = 1;
14079 /* It's invalid to have a no return value within a function that
14080 isn't declared with the keyword `void' */
14081 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14082 error_found = 2;
14084 if (in_instance_initializer)
14085 error_found = 1;
14087 if (error_found)
14089 if (in_instance_initializer)
14090 parse_error_context (wfl_operator,
14091 "`return' inside instance initializer");
14093 else if (DECL_CLINIT_P (current_function_decl))
14094 parse_error_context (wfl_operator,
14095 "`return' inside static initializer");
14097 else if (!DECL_CONSTRUCTOR_P (meth))
14099 char *t = xstrdup (lang_printable_name (mtype, 0));
14100 parse_error_context (wfl_operator,
14101 "`return' with%s value from `%s %s'",
14102 (error_found == 1 ? "" : "out"),
14103 t, lang_printable_name (meth, 0));
14104 free (t);
14106 else
14107 parse_error_context (wfl_operator,
14108 "`return' with value from constructor `%s'",
14109 lang_printable_name (meth, 0));
14110 return error_mark_node;
14113 /* If we have a return_exp, build a modify expression and expand
14114 it. Note: at that point, the assignment is declared valid, but we
14115 may want to carry some more hacks */
14116 if (return_exp)
14118 tree exp = java_complete_tree (return_exp);
14119 tree modify, patched;
14121 /* If the function returned value and EXP are booleans, EXP has
14122 to be converted into the type of DECL_RESULT, which is integer
14123 (see complete_start_java_method) */
14124 if (TREE_TYPE (exp) == boolean_type_node &&
14125 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14126 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14128 /* `null' can be assigned to a function returning a reference */
14129 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14130 exp == null_pointer_node)
14131 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14133 if ((patched = patch_string (exp)))
14134 exp = patched;
14136 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14137 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14138 modify = java_complete_tree (modify);
14140 if (modify != error_mark_node)
14142 TREE_SIDE_EFFECTS (modify) = 1;
14143 TREE_OPERAND (node, 0) = modify;
14145 else
14146 return error_mark_node;
14148 TREE_TYPE (node) = void_type_node;
14149 TREE_SIDE_EFFECTS (node) = 1;
14150 return node;
14153 /* 14.8 The if Statement */
14155 static tree
14156 build_if_else_statement (location, expression, if_body, else_body)
14157 int location;
14158 tree expression, if_body, else_body;
14160 tree node;
14161 if (!else_body)
14162 else_body = empty_stmt_node;
14163 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14164 EXPR_WFL_LINECOL (node) = location;
14165 node = build_debugable_stmt (location, node);
14166 return node;
14169 static tree
14170 patch_if_else_statement (node)
14171 tree node;
14173 tree expression = TREE_OPERAND (node, 0);
14175 TREE_TYPE (node) = error_mark_node;
14176 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14178 /* The type of expression must be boolean */
14179 if (TREE_TYPE (expression) != boolean_type_node
14180 && TREE_TYPE (expression) != promoted_boolean_type_node)
14182 parse_error_context
14183 (wfl_operator,
14184 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14185 lang_printable_name (TREE_TYPE (expression), 0));
14186 return error_mark_node;
14189 TREE_TYPE (node) = void_type_node;
14190 TREE_SIDE_EFFECTS (node) = 1;
14191 CAN_COMPLETE_NORMALLY (node)
14192 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14193 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
14194 return node;
14197 /* 14.6 Labeled Statements */
14199 /* Action taken when a lableled statement is parsed. a new
14200 LABELED_BLOCK_EXPR is created. No statement is attached to the
14201 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14203 static tree
14204 build_labeled_block (location, label)
14205 int location;
14206 tree label;
14208 tree label_name ;
14209 tree label_decl, node;
14210 if (label == NULL_TREE || label == continue_identifier_node)
14211 label_name = label;
14212 else
14214 label_name = merge_qualified_name (label_id, label);
14215 /* Issue an error if we try to reuse a label that was previously
14216 declared */
14217 if (IDENTIFIER_LOCAL_VALUE (label_name))
14219 EXPR_WFL_LINECOL (wfl_operator) = location;
14220 parse_error_context (wfl_operator,
14221 "Declaration of `%s' shadows a previous label declaration",
14222 IDENTIFIER_POINTER (label));
14223 EXPR_WFL_LINECOL (wfl_operator) =
14224 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14225 parse_error_context (wfl_operator,
14226 "This is the location of the previous declaration of label `%s'",
14227 IDENTIFIER_POINTER (label));
14228 java_error_count--;
14232 label_decl = create_label_decl (label_name);
14233 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14234 EXPR_WFL_LINECOL (node) = location;
14235 TREE_SIDE_EFFECTS (node) = 1;
14236 return node;
14239 /* A labeled statement LBE is attached a statement. */
14241 static tree
14242 finish_labeled_statement (lbe, statement)
14243 tree lbe; /* Labeled block expr */
14244 tree statement;
14246 /* In anyways, tie the loop to its statement */
14247 LABELED_BLOCK_BODY (lbe) = statement;
14248 pop_labeled_block ();
14249 POP_LABELED_BLOCK ();
14250 return lbe;
14253 /* 14.10, 14.11, 14.12 Loop Statements */
14255 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14256 list. */
14258 static tree
14259 build_new_loop (loop_body)
14260 tree loop_body;
14262 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14263 TREE_SIDE_EFFECTS (loop) = 1;
14264 PUSH_LOOP (loop);
14265 return loop;
14268 /* Create a loop body according to the following structure:
14269 COMPOUND_EXPR
14270 COMPOUND_EXPR (loop main body)
14271 EXIT_EXPR (this order is for while/for loops.
14272 LABELED_BLOCK_EXPR the order is reversed for do loops)
14273 LABEL_DECL (a continue occuring here branches at the
14274 BODY end of this labeled block)
14275 INCREMENT (if any)
14277 REVERSED, if non zero, tells that the loop condition expr comes
14278 after the body, like in the do-while loop.
14280 To obtain a loop, the loop body structure described above is
14281 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14283 LABELED_BLOCK_EXPR
14284 LABEL_DECL (use this label to exit the loop)
14285 LOOP_EXPR
14286 <structure described above> */
14288 static tree
14289 build_loop_body (location, condition, reversed)
14290 int location;
14291 tree condition;
14292 int reversed;
14294 tree first, second, body;
14296 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14297 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14298 condition = build_debugable_stmt (location, condition);
14299 TREE_SIDE_EFFECTS (condition) = 1;
14301 body = build_labeled_block (0, continue_identifier_node);
14302 first = (reversed ? body : condition);
14303 second = (reversed ? condition : body);
14304 return
14305 build (COMPOUND_EXPR, NULL_TREE,
14306 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14309 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14310 their order) on the current loop. Unlink the current loop from the
14311 loop list. */
14313 static tree
14314 finish_loop_body (location, condition, body, reversed)
14315 int location;
14316 tree condition, body;
14317 int reversed;
14319 tree to_return = ctxp->current_loop;
14320 tree loop_body = LOOP_EXPR_BODY (to_return);
14321 if (condition)
14323 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14324 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14325 The real EXIT_EXPR is one operand further. */
14326 EXPR_WFL_LINECOL (cnode) = location;
14327 /* This one is for accurate error reports */
14328 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14329 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14331 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14332 POP_LOOP ();
14333 return to_return;
14336 /* Tailored version of finish_loop_body for FOR loops, when FOR
14337 loops feature the condition part */
14339 static tree
14340 finish_for_loop (location, condition, update, body)
14341 int location;
14342 tree condition, update, body;
14344 /* Put the condition and the loop body in place */
14345 tree loop = finish_loop_body (location, condition, body, 0);
14346 /* LOOP is the current loop which has been now popped of the loop
14347 stack. Install the update block */
14348 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14349 return loop;
14352 /* Try to find the loop a block might be related to. This comprises
14353 the case where the LOOP_EXPR is found as the second operand of a
14354 COMPOUND_EXPR, because the loop happens to have an initialization
14355 part, then expressed as the first operand of the COMPOUND_EXPR. If
14356 the search finds something, 1 is returned. Otherwise, 0 is
14357 returned. The search is assumed to start from a
14358 LABELED_BLOCK_EXPR's block. */
14360 static tree
14361 search_loop (statement)
14362 tree statement;
14364 if (TREE_CODE (statement) == LOOP_EXPR)
14365 return statement;
14367 if (TREE_CODE (statement) == BLOCK)
14368 statement = BLOCK_SUBBLOCKS (statement);
14369 else
14370 return NULL_TREE;
14372 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14373 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14374 statement = TREE_OPERAND (statement, 1);
14376 return (TREE_CODE (statement) == LOOP_EXPR
14377 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14380 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14381 returned otherwise. */
14383 static int
14384 labeled_block_contains_loop_p (block, loop)
14385 tree block, loop;
14387 if (!block)
14388 return 0;
14390 if (LABELED_BLOCK_BODY (block) == loop)
14391 return 1;
14393 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14394 return 1;
14396 return 0;
14399 /* If the loop isn't surrounded by a labeled statement, create one and
14400 insert LOOP as its body. */
14402 static tree
14403 patch_loop_statement (loop)
14404 tree loop;
14406 tree loop_label;
14408 TREE_TYPE (loop) = void_type_node;
14409 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14410 return loop;
14412 loop_label = build_labeled_block (0, NULL_TREE);
14413 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14414 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14415 LABELED_BLOCK_BODY (loop_label) = loop;
14416 PUSH_LABELED_BLOCK (loop_label);
14417 return loop_label;
14420 /* 14.13, 14.14: break and continue Statements */
14422 /* Build a break or a continue statement. a null NAME indicates an
14423 unlabeled break/continue statement. */
14425 static tree
14426 build_bc_statement (location, is_break, name)
14427 int location, is_break;
14428 tree name;
14430 tree break_continue, label_block_expr = NULL_TREE;
14432 if (name)
14434 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14435 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14436 /* Null means that we don't have a target for this named
14437 break/continue. In this case, we make the target to be the
14438 label name, so that the error can be reported accuratly in
14439 patch_bc_statement. */
14440 label_block_expr = EXPR_WFL_NODE (name);
14442 /* Unlabeled break/continue will be handled during the
14443 break/continue patch operation */
14444 break_continue
14445 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14447 IS_BREAK_STMT_P (break_continue) = is_break;
14448 TREE_SIDE_EFFECTS (break_continue) = 1;
14449 EXPR_WFL_LINECOL (break_continue) = location;
14450 break_continue = build_debugable_stmt (location, break_continue);
14451 return break_continue;
14454 /* Verification of a break/continue statement. */
14456 static tree
14457 patch_bc_statement (node)
14458 tree node;
14460 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14461 tree labeled_block = ctxp->current_labeled_block;
14462 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14464 /* Having an identifier here means that the target is unknown. */
14465 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14467 parse_error_context (wfl_operator, "No label definition found for `%s'",
14468 IDENTIFIER_POINTER (bc_label));
14469 return error_mark_node;
14471 if (! IS_BREAK_STMT_P (node))
14473 /* It's a continue statement. */
14474 for (;; labeled_block = TREE_CHAIN (labeled_block))
14476 if (labeled_block == NULL_TREE)
14478 if (bc_label == NULL_TREE)
14479 parse_error_context (wfl_operator,
14480 "`continue' must be in loop");
14481 else
14482 parse_error_context
14483 (wfl_operator, "continue label `%s' does not name a loop",
14484 IDENTIFIER_POINTER (bc_label));
14485 return error_mark_node;
14487 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14488 == continue_identifier_node)
14489 && (bc_label == NULL_TREE
14490 || TREE_CHAIN (labeled_block) == bc_label))
14492 bc_label = labeled_block;
14493 break;
14497 else if (!bc_label)
14499 for (;; labeled_block = TREE_CHAIN (labeled_block))
14501 if (labeled_block == NULL_TREE)
14503 parse_error_context (wfl_operator,
14504 "`break' must be in loop or switch");
14505 return error_mark_node;
14507 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14508 if (TREE_CODE (target_stmt) == SWITCH_EXPR
14509 || search_loop (target_stmt))
14511 bc_label = labeled_block;
14512 break;
14517 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14518 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14520 /* Our break/continue don't return values. */
14521 TREE_TYPE (node) = void_type_node;
14522 /* Encapsulate the break within a compound statement so that it's
14523 expanded all the times by expand_expr (and not clobbered
14524 sometimes, like after a if statement) */
14525 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14526 TREE_SIDE_EFFECTS (node) = 1;
14527 return node;
14530 /* Process the exit expression belonging to a loop. Its type must be
14531 boolean. */
14533 static tree
14534 patch_exit_expr (node)
14535 tree node;
14537 tree expression = TREE_OPERAND (node, 0);
14538 TREE_TYPE (node) = error_mark_node;
14539 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14541 /* The type of expression must be boolean */
14542 if (TREE_TYPE (expression) != boolean_type_node)
14544 parse_error_context
14545 (wfl_operator,
14546 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
14547 lang_printable_name (TREE_TYPE (expression), 0));
14548 return error_mark_node;
14550 /* Now we know things are allright, invert the condition, fold and
14551 return */
14552 TREE_OPERAND (node, 0) =
14553 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
14555 if (! integer_zerop (TREE_OPERAND (node, 0))
14556 && ctxp->current_loop != NULL_TREE
14557 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14558 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14559 if (! integer_onep (TREE_OPERAND (node, 0)))
14560 CAN_COMPLETE_NORMALLY (node) = 1;
14563 TREE_TYPE (node) = void_type_node;
14564 return node;
14567 /* 14.9 Switch statement */
14569 static tree
14570 patch_switch_statement (node)
14571 tree node;
14573 tree se = TREE_OPERAND (node, 0), se_type;
14575 /* Complete the switch expression */
14576 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14577 se_type = TREE_TYPE (se);
14578 /* The type of the switch expression must be char, byte, short or
14579 int */
14580 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
14582 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14583 parse_error_context (wfl_operator,
14584 "Incompatible type for `switch'. Can't convert `%s' to `int'",
14585 lang_printable_name (se_type, 0));
14586 /* This is what java_complete_tree will check */
14587 TREE_OPERAND (node, 0) = error_mark_node;
14588 return error_mark_node;
14591 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
14593 /* Ready to return */
14594 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
14596 TREE_TYPE (node) = error_mark_node;
14597 return error_mark_node;
14599 TREE_TYPE (node) = void_type_node;
14600 TREE_SIDE_EFFECTS (node) = 1;
14601 CAN_COMPLETE_NORMALLY (node)
14602 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14603 || ! SWITCH_HAS_DEFAULT (node);
14604 return node;
14607 /* 14.18 The try/catch statements */
14609 static tree
14610 build_try_statement (location, try_block, catches)
14611 int location;
14612 tree try_block, catches;
14614 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
14615 EXPR_WFL_LINECOL (node) = location;
14616 return node;
14619 static tree
14620 build_try_finally_statement (location, try_block, finally)
14621 int location;
14622 tree try_block, finally;
14624 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14625 EXPR_WFL_LINECOL (node) = location;
14626 return node;
14629 static tree
14630 patch_try_statement (node)
14631 tree node;
14633 int error_found = 0;
14634 tree try = TREE_OPERAND (node, 0);
14635 /* Exception handlers are considered in left to right order */
14636 tree catch = nreverse (TREE_OPERAND (node, 1));
14637 tree current, caught_type_list = NULL_TREE;
14639 /* Check catch clauses, if any. Every time we find an error, we try
14640 to process the next catch clause. We process the catch clause before
14641 the try block so that when processing the try block we can check thrown
14642 exceptions againts the caught type list. */
14643 for (current = catch; current; current = TREE_CHAIN (current))
14645 tree carg_decl, carg_type;
14646 tree sub_current, catch_block, catch_clause;
14647 int unreachable;
14649 /* At this point, the structure of the catch clause is
14650 CATCH_EXPR (catch node)
14651 BLOCK (with the decl of the parameter)
14652 COMPOUND_EXPR
14653 MODIFY_EXPR (assignment of the catch parameter)
14654 BLOCK (catch clause block)
14656 catch_clause = TREE_OPERAND (current, 0);
14657 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14658 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14660 /* Catch clauses can't have more than one parameter declared,
14661 but it's already enforced by the grammar. Make sure that the
14662 only parameter of the clause statement in of class Throwable
14663 or a subclass of Throwable, but that was done earlier. The
14664 catch clause parameter type has also been resolved. */
14666 /* Just make sure that the catch clause parameter type inherits
14667 from java.lang.Throwable */
14668 if (!inherits_from_p (carg_type, throwable_type_node))
14670 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14671 parse_error_context (wfl_operator,
14672 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
14673 lang_printable_name (carg_type, 0));
14674 error_found = 1;
14675 continue;
14678 /* Partial check for unreachable catch statement: The catch
14679 clause is reachable iff is no earlier catch block A in
14680 the try statement such that the type of the catch
14681 clause's parameter is the same as or a subclass of the
14682 type of A's parameter */
14683 unreachable = 0;
14684 for (sub_current = catch;
14685 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14687 tree sub_catch_clause, decl;
14688 sub_catch_clause = TREE_OPERAND (sub_current, 0);
14689 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14691 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14693 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14694 parse_error_context
14695 (wfl_operator,
14696 "`catch' not reached because of the catch clause at line %d",
14697 EXPR_WFL_LINENO (sub_current));
14698 unreachable = error_found = 1;
14699 break;
14702 /* Complete the catch clause block */
14703 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14704 if (catch_block == error_mark_node)
14706 error_found = 1;
14707 continue;
14709 if (CAN_COMPLETE_NORMALLY (catch_block))
14710 CAN_COMPLETE_NORMALLY (node) = 1;
14711 TREE_OPERAND (current, 0) = catch_block;
14713 if (unreachable)
14714 continue;
14716 /* Things to do here: the exception must be thrown */
14718 /* Link this type to the caught type list */
14719 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
14722 PUSH_EXCEPTIONS (caught_type_list);
14723 if ((try = java_complete_tree (try)) == error_mark_node)
14724 error_found = 1;
14725 if (CAN_COMPLETE_NORMALLY (try))
14726 CAN_COMPLETE_NORMALLY (node) = 1;
14727 POP_EXCEPTIONS ();
14729 /* Verification ends here */
14730 if (error_found)
14731 return error_mark_node;
14733 TREE_OPERAND (node, 0) = try;
14734 TREE_OPERAND (node, 1) = catch;
14735 TREE_TYPE (node) = void_type_node;
14736 return node;
14739 /* 14.17 The synchronized Statement */
14741 static tree
14742 patch_synchronized_statement (node, wfl_op1)
14743 tree node, wfl_op1;
14745 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
14746 tree block = TREE_OPERAND (node, 1);
14748 tree enter, exit, expr_decl, assignment;
14750 if (expr == error_mark_node)
14752 block = java_complete_tree (block);
14753 return expr;
14756 /* The TYPE of expr must be a reference type */
14757 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
14759 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14760 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
14761 lang_printable_name (TREE_TYPE (expr), 0));
14762 return error_mark_node;
14765 if (flag_emit_xref)
14767 TREE_OPERAND (node, 0) = expr;
14768 TREE_OPERAND (node, 1) = java_complete_tree (block);
14769 CAN_COMPLETE_NORMALLY (node) = 1;
14770 return node;
14773 /* Generate a try-finally for the synchronized statement, except
14774 that the handler that catches all throw exception calls
14775 _Jv_MonitorExit and then rethrow the exception.
14776 The synchronized statement is then implemented as:
14777 TRY
14779 _Jv_MonitorEnter (expression)
14780 synchronized_block
14781 _Jv_MonitorExit (expression)
14783 CATCH_ALL
14785 e = _Jv_exception_info ();
14786 _Jv_MonitorExit (expression)
14787 Throw (e);
14788 } */
14790 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14791 BUILD_MONITOR_ENTER (enter, expr_decl);
14792 BUILD_MONITOR_EXIT (exit, expr_decl);
14793 CAN_COMPLETE_NORMALLY (enter) = 1;
14794 CAN_COMPLETE_NORMALLY (exit) = 1;
14795 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14796 TREE_SIDE_EFFECTS (assignment) = 1;
14797 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14798 build (COMPOUND_EXPR, NULL_TREE,
14799 build (WITH_CLEANUP_EXPR, NULL_TREE,
14800 build (COMPOUND_EXPR, NULL_TREE,
14801 assignment, enter),
14802 NULL_TREE, exit),
14803 block));
14804 node = build_expr_block (node, expr_decl);
14806 return java_complete_tree (node);
14809 /* 14.16 The throw Statement */
14811 static tree
14812 patch_throw_statement (node, wfl_op1)
14813 tree node, wfl_op1;
14815 tree expr = TREE_OPERAND (node, 0);
14816 tree type = TREE_TYPE (expr);
14817 int unchecked_ok = 0, tryblock_throws_ok = 0;
14819 /* Thrown expression must be assignable to java.lang.Throwable */
14820 if (!try_reference_assignconv (throwable_type_node, expr))
14822 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14823 parse_error_context (wfl_operator,
14824 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
14825 lang_printable_name (type, 0));
14826 /* If the thrown expression was a reference, we further the
14827 compile-time check. */
14828 if (!JREFERENCE_TYPE_P (type))
14829 return error_mark_node;
14832 /* At least one of the following must be true */
14834 /* The type of the throw expression is a not checked exception,
14835 i.e. is a unchecked expression. */
14836 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
14838 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14839 /* An instance can't throw a checked excetion unless that exception
14840 is explicitely declared in the `throws' clause of each
14841 constructor. This doesn't apply to anonymous classes, since they
14842 don't have declared constructors. */
14843 if (!unchecked_ok
14844 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14846 tree current;
14847 for (current = TYPE_METHODS (current_class); current;
14848 current = TREE_CHAIN (current))
14849 if (DECL_CONSTRUCTOR_P (current)
14850 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14852 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)",
14853 lang_printable_name (TREE_TYPE (expr), 0));
14854 return error_mark_node;
14858 /* Throw is contained in a try statement and at least one catch
14859 clause can receive the thrown expression or the current method is
14860 declared to throw such an exception. Or, the throw statement is
14861 contained in a method or constructor declaration and the type of
14862 the Expression is assignable to at least one type listed in the
14863 throws clause the declaration. */
14864 if (!unchecked_ok)
14865 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
14866 if (!(unchecked_ok || tryblock_throws_ok))
14868 /* If there is a surrounding try block that has no matching
14869 clatch clause, report it first. A surrounding try block exits
14870 only if there is something after the list of checked
14871 exception thrown by the current function (if any). */
14872 if (IN_TRY_BLOCK_P ())
14873 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
14874 lang_printable_name (type, 0));
14875 /* If we have no surrounding try statement and the method doesn't have
14876 any throws, report it now. FIXME */
14878 /* We report that the exception can't be throw from a try block
14879 in all circumstances but when the `throw' is inside a static
14880 block. */
14881 else if (!EXCEPTIONS_P (currently_caught_type_list)
14882 && !tryblock_throws_ok)
14884 if (DECL_CLINIT_P (current_function_decl))
14885 parse_error_context (wfl_operator,
14886 "Checked exception `%s' can't be thrown in initializer",
14887 lang_printable_name (type, 0));
14888 else
14889 parse_error_context (wfl_operator,
14890 "Checked exception `%s' isn't thrown from a `try' block",
14891 lang_printable_name (type, 0));
14893 /* Otherwise, the current method doesn't have the appropriate
14894 throws declaration */
14895 else
14896 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
14897 lang_printable_name (type, 0));
14898 return error_mark_node;
14901 if (! flag_emit_class_files && ! flag_emit_xref)
14902 BUILD_THROW (node, expr);
14904 /* If doing xrefs, keep the location where the `throw' was seen. */
14905 if (flag_emit_xref)
14906 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
14907 return node;
14910 /* Check that exception said to be thrown by method DECL can be
14911 effectively caught from where DECL is invoked. */
14913 static void
14914 check_thrown_exceptions (location, decl)
14915 int location;
14916 tree decl;
14918 tree throws;
14919 /* For all the unchecked exceptions thrown by DECL */
14920 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14921 throws = TREE_CHAIN (throws))
14922 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
14924 #if 1
14925 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14926 if (DECL_NAME (decl) == get_identifier ("clone"))
14927 continue;
14928 #endif
14929 EXPR_WFL_LINECOL (wfl_operator) = location;
14930 if (DECL_FINIT_P (current_function_decl))
14931 parse_error_context
14932 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14933 lang_printable_name (TREE_VALUE (throws), 0));
14934 else
14936 parse_error_context
14937 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
14938 lang_printable_name (TREE_VALUE (throws), 0),
14939 (DECL_INIT_P (current_function_decl) ?
14940 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14941 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14946 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
14947 try-catch blocks, OR is listed in the `throws' clause of the
14948 current method. */
14950 static int
14951 check_thrown_exceptions_do (exception)
14952 tree exception;
14954 tree list = currently_caught_type_list;
14955 resolve_and_layout (exception, NULL_TREE);
14956 /* First, all the nested try-catch-finally at that stage. The
14957 last element contains `throws' clause exceptions, if any. */
14958 if (IS_UNCHECKED_EXCEPTION_P (exception))
14959 return 1;
14960 while (list)
14962 tree caught;
14963 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14964 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14965 return 1;
14966 list = TREE_CHAIN (list);
14968 return 0;
14971 static void
14972 purge_unchecked_exceptions (mdecl)
14973 tree mdecl;
14975 tree throws = DECL_FUNCTION_THROWS (mdecl);
14976 tree new = NULL_TREE;
14978 while (throws)
14980 tree next = TREE_CHAIN (throws);
14981 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
14983 TREE_CHAIN (throws) = new;
14984 new = throws;
14986 throws = next;
14988 /* List is inverted here, but it doesn't matter */
14989 DECL_FUNCTION_THROWS (mdecl) = new;
14992 /* 15.24 Conditional Operator ?: */
14994 static tree
14995 patch_conditional_expr (node, wfl_cond, wfl_op1)
14996 tree node, wfl_cond, wfl_op1;
14998 tree cond = TREE_OPERAND (node, 0);
14999 tree op1 = TREE_OPERAND (node, 1);
15000 tree op2 = TREE_OPERAND (node, 2);
15001 tree resulting_type = NULL_TREE;
15002 tree t1, t2, patched;
15003 int error_found = 0;
15005 /* Operands of ?: might be StringBuffers crafted as a result of a
15006 string concatenation. Obtain a descent operand here. */
15007 if ((patched = patch_string (op1)))
15008 TREE_OPERAND (node, 1) = op1 = patched;
15009 if ((patched = patch_string (op2)))
15010 TREE_OPERAND (node, 2) = op2 = patched;
15012 t1 = TREE_TYPE (op1);
15013 t2 = TREE_TYPE (op2);
15015 /* The first expression must be a boolean */
15016 if (TREE_TYPE (cond) != boolean_type_node)
15018 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15019 parse_error_context (wfl_operator,
15020 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15021 lang_printable_name (TREE_TYPE (cond), 0));
15022 error_found = 1;
15025 /* Second and third can be numeric, boolean (i.e. primitive),
15026 references or null. Anything else results in an error */
15027 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15028 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15029 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15030 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15031 error_found = 1;
15033 /* Determine the type of the conditional expression. Same types are
15034 easy to deal with */
15035 else if (t1 == t2)
15036 resulting_type = t1;
15038 /* There are different rules for numeric types */
15039 else if (JNUMERIC_TYPE_P (t1))
15041 /* if byte/short found, the resulting type is short */
15042 if ((t1 == byte_type_node && t2 == short_type_node)
15043 || (t1 == short_type_node && t2 == byte_type_node))
15044 resulting_type = short_type_node;
15046 /* If t1 is a constant int and t2 is of type byte, short or char
15047 and t1's value fits in t2, then the resulting type is t2 */
15048 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15049 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15050 resulting_type = t2;
15052 /* If t2 is a constant int and t1 is of type byte, short or char
15053 and t2's value fits in t1, then the resulting type is t1 */
15054 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15055 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15056 resulting_type = t1;
15058 /* Otherwise, binary numeric promotion is applied and the
15059 resulting type is the promoted type of operand 1 and 2 */
15060 else
15061 resulting_type = binary_numeric_promotion (t1, t2,
15062 &TREE_OPERAND (node, 1),
15063 &TREE_OPERAND (node, 2));
15066 /* Cases of a reference and a null type */
15067 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15068 resulting_type = t1;
15070 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15071 resulting_type = t2;
15073 /* Last case: different reference types. If a type can be converted
15074 into the other one by assignment conversion, the latter
15075 determines the type of the expression */
15076 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15077 resulting_type = promote_type (t1);
15079 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15080 resulting_type = promote_type (t2);
15082 /* If we don't have any resulting type, we're in trouble */
15083 if (!resulting_type)
15085 char *t = xstrdup (lang_printable_name (t1, 0));
15086 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15087 parse_error_context (wfl_operator,
15088 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15089 t, lang_printable_name (t2, 0));
15090 free (t);
15091 error_found = 1;
15094 if (error_found)
15096 TREE_TYPE (node) = error_mark_node;
15097 return error_mark_node;
15100 TREE_TYPE (node) = resulting_type;
15101 TREE_SET_CODE (node, COND_EXPR);
15102 CAN_COMPLETE_NORMALLY (node) = 1;
15103 return node;
15106 /* Try to constant fold NODE.
15107 If NODE is not a constant expression, return NULL_EXPR.
15108 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15110 static tree
15111 fold_constant_for_init (node, context)
15112 tree node;
15113 tree context;
15115 tree op0, op1, val;
15116 enum tree_code code = TREE_CODE (node);
15118 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
15119 return node;
15121 switch (code)
15123 case PLUS_EXPR:
15124 case MINUS_EXPR:
15125 case MULT_EXPR:
15126 case TRUNC_MOD_EXPR:
15127 case RDIV_EXPR:
15128 case LSHIFT_EXPR:
15129 case RSHIFT_EXPR:
15130 case URSHIFT_EXPR:
15131 case BIT_AND_EXPR:
15132 case BIT_XOR_EXPR:
15133 case BIT_IOR_EXPR:
15134 case TRUTH_ANDIF_EXPR:
15135 case TRUTH_ORIF_EXPR:
15136 case EQ_EXPR:
15137 case NE_EXPR:
15138 case GT_EXPR:
15139 case GE_EXPR:
15140 case LT_EXPR:
15141 case LE_EXPR:
15142 op0 = TREE_OPERAND (node, 0);
15143 op1 = TREE_OPERAND (node, 1);
15144 val = fold_constant_for_init (op0, context);
15145 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15146 return NULL_TREE;
15147 TREE_OPERAND (node, 0) = val;
15148 val = fold_constant_for_init (op1, context);
15149 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15150 return NULL_TREE;
15151 TREE_OPERAND (node, 1) = val;
15152 return patch_binop (node, op0, op1);
15154 case UNARY_PLUS_EXPR:
15155 case NEGATE_EXPR:
15156 case TRUTH_NOT_EXPR:
15157 case BIT_NOT_EXPR:
15158 case CONVERT_EXPR:
15159 op0 = TREE_OPERAND (node, 0);
15160 val = fold_constant_for_init (op0, context);
15161 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15162 return NULL_TREE;
15163 TREE_OPERAND (node, 0) = val;
15164 return patch_unaryop (node, op0);
15165 break;
15167 case COND_EXPR:
15168 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15169 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15170 return NULL_TREE;
15171 TREE_OPERAND (node, 0) = val;
15172 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15173 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15174 return NULL_TREE;
15175 TREE_OPERAND (node, 1) = val;
15176 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15177 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15178 return NULL_TREE;
15179 TREE_OPERAND (node, 2) = val;
15180 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15181 : TREE_OPERAND (node, 2);
15183 case VAR_DECL:
15184 case FIELD_DECL:
15185 if (! FIELD_FINAL (node)
15186 || DECL_INITIAL (node) == NULL_TREE)
15187 return NULL_TREE;
15188 val = DECL_INITIAL (node);
15189 /* Guard against infinite recursion. */
15190 DECL_INITIAL (node) = NULL_TREE;
15191 val = fold_constant_for_init (val, node);
15192 DECL_INITIAL (node) = val;
15193 return val;
15195 case EXPR_WITH_FILE_LOCATION:
15196 /* Compare java_complete_tree and resolve_expression_name. */
15197 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15198 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15200 tree name = EXPR_WFL_NODE (node);
15201 tree decl;
15202 if (PRIMARY_P (node))
15203 return NULL_TREE;
15204 else if (! QUALIFIED_P (name))
15206 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15207 if (decl == NULL_TREE
15208 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15209 return NULL_TREE;
15210 return fold_constant_for_init (decl, decl);
15212 else
15214 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15215 qualify_ambiguous_name (node);
15216 if (resolve_field_access (node, &decl, NULL)
15217 && decl != NULL_TREE)
15218 return fold_constant_for_init (decl, decl);
15219 return NULL_TREE;
15222 else
15224 op0 = TREE_OPERAND (node, 0);
15225 val = fold_constant_for_init (op0, context);
15226 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15227 return NULL_TREE;
15228 TREE_OPERAND (node, 0) = val;
15229 return val;
15232 #ifdef USE_COMPONENT_REF
15233 case IDENTIFIER:
15234 case COMPONENT_REF:
15236 #endif
15238 default:
15239 return NULL_TREE;
15243 #ifdef USE_COMPONENT_REF
15244 /* Context is 'T' for TypeName, 'P' for PackageName,
15245 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15247 tree
15248 resolve_simple_name (name, context)
15249 tree name;
15250 int context;
15254 tree
15255 resolve_qualified_name (name, context)
15256 tree name;
15257 int context;
15260 #endif
15262 /* Mark P, which is really a `struct parser_ctxt **' for GC. */
15264 static void
15265 mark_parser_ctxt (p)
15266 void *p;
15268 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15269 int i;
15271 if (!pc)
15272 return;
15274 #ifndef JC1_LITE
15275 for (i = 0; i < 11; ++i)
15276 ggc_mark_tree (pc->modifier_ctx[i]);
15277 ggc_mark_tree (pc->class_type);
15278 ggc_mark_tree (pc->function_decl);
15279 ggc_mark_tree (pc->package);
15280 ggc_mark_tree (pc->incomplete_class);
15281 ggc_mark_tree (pc->gclass_list);
15282 ggc_mark_tree (pc->class_list);
15283 ggc_mark_tree (pc->current_parsed_class);
15284 ggc_mark_tree (pc->current_parsed_class_un);
15285 ggc_mark_tree (pc->non_static_initialized);
15286 ggc_mark_tree (pc->static_initialized);
15287 ggc_mark_tree (pc->instance_initializers);
15288 ggc_mark_tree (pc->import_list);
15289 ggc_mark_tree (pc->import_demand_list);
15290 ggc_mark_tree (pc->current_loop);
15291 ggc_mark_tree (pc->current_labeled_block);
15292 #endif /* JC1_LITE */
15294 if (pc->next)
15295 mark_parser_ctxt (&pc->next);