tree-inline.c (estimate_num_insns_1): Handle VEC_COND_EXPR.
[official-gcc.git] / gcc / java / parse.y
blobb4facbba592e50bd88313a88ec607bea1a6eeabb
1 /* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
5 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc. */
28 /* This file parses java source code and issues a tree node image
29 suitable for code generation (byte code and targeted CPU assembly
30 language).
32 The grammar conforms to the Java grammar described in "The Java(TM)
33 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
34 1996, ISBN 0-201-63451-1"
36 The following modifications were brought to the original grammar:
38 method_body: added the rule '| block SC_TK'
39 static_initializer: added the rule 'static block SC_TK'.
41 Note: All the extra rules described above should go away when the
42 empty_statement rule will work.
44 statement_nsi: 'nsi' should be read no_short_if.
46 Some rules have been modified to support JDK1.1 inner classes
47 definitions and other extensions. */
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include <dirent.h>
55 #include "tree.h"
56 #include "rtl.h"
57 #include "real.h"
58 #include "obstack.h"
59 #include "toplev.h"
60 #include "pretty-print.h"
61 #include "diagnostic.h"
62 #include "flags.h"
63 #include "java-tree.h"
64 #include "jcf.h"
65 #include "lex.h"
66 #include "parse.h"
67 #include "zipfile.h"
68 #include "convert.h"
69 #include "buffer.h"
70 #include "function.h"
71 #include "except.h"
72 #include "ggc.h"
73 #include "debug.h"
74 #include "tree-inline.h"
75 #include "cgraph.h"
76 #include "target.h"
78 /* Local function prototypes */
79 static char *java_accstring_lookup (int);
80 static const char *accessibility_string (int);
81 static void classitf_redefinition_error (const char *,tree, tree, tree);
82 static void variable_redefinition_error (tree, tree, tree, int);
83 static tree create_class (int, tree, tree, tree);
84 static tree create_interface (int, tree, tree);
85 static void end_class_declaration (int);
86 static tree find_field (tree, tree);
87 static tree lookup_field_wrapper (tree, tree);
88 static int duplicate_declaration_error_p (tree, tree, tree);
89 static void register_fields (int, tree, tree);
90 static tree parser_qualified_classname (tree);
91 static int parser_check_super (tree, tree, tree);
92 static int parser_check_super_interface (tree, tree, tree);
93 static void check_modifiers_consistency (int);
94 static tree lookup_cl (tree);
95 static tree lookup_java_method2 (tree, tree, int);
96 static tree method_header (int, tree, tree, tree);
97 static void fix_method_argument_names (tree ,tree);
98 static tree method_declarator (tree, tree);
99 static void parse_warning_context (tree cl, const char *msgid, ...);
100 #ifdef USE_MAPPED_LOCATION
101 static void issue_warning_error_from_context
102 (source_location, const char *msgid, va_list *);
103 #else
104 static void issue_warning_error_from_context
105 (tree, const char *msgid, va_list *);
106 #endif
107 static void parse_ctor_invocation_error (void);
108 static tree parse_jdk1_1_error (const char *);
109 static void complete_class_report_errors (jdep *);
110 static int process_imports (void);
111 static void read_import_dir (tree);
112 static int find_in_imports_on_demand (tree, tree);
113 static void find_in_imports (tree, tree);
114 static void check_inner_class_access (tree, tree, tree);
115 static int check_pkg_class_access (tree, tree, bool, tree);
116 static void register_package (tree);
117 static tree resolve_package (tree, tree *, tree *);
118 static tree resolve_class (tree, tree, tree, tree);
119 static void declare_local_variables (int, tree, tree);
120 static void dump_java_tree (enum tree_dump_index, tree);
121 static void source_start_java_method (tree);
122 static void source_end_java_method (void);
123 static tree find_name_in_single_imports (tree);
124 static void check_abstract_method_header (tree);
125 static tree lookup_java_interface_method2 (tree, tree);
126 static tree resolve_expression_name (tree, tree *);
127 static tree maybe_create_class_interface_decl (tree, tree, tree, tree);
128 static int check_class_interface_creation (int, int, tree, tree, tree, tree);
129 static tree patch_method_invocation (tree, tree, tree, int, int *, tree *);
130 static tree resolve_and_layout (tree, tree);
131 static tree qualify_and_find (tree, tree, tree);
132 static tree resolve_no_layout (tree, tree);
133 static int invocation_mode (tree, int);
134 static tree find_applicable_accessible_methods_list (int, tree, tree, tree);
135 static void search_applicable_methods_list (int, tree, tree, tree, tree *, tree *);
136 static tree find_most_specific_methods_list (tree);
137 static int argument_types_convertible (tree, tree);
138 static tree patch_invoke (tree, tree, tree);
139 static int maybe_use_access_method (int, tree *, tree *);
140 static tree lookup_method_invoke (int, tree, tree, tree, tree);
141 static tree register_incomplete_type (int, tree, tree, tree);
142 static tree check_inner_circular_reference (tree, tree);
143 static tree check_circular_reference (tree);
144 static tree obtain_incomplete_type (tree);
145 static tree java_complete_lhs (tree);
146 static tree java_complete_tree (tree);
147 static tree maybe_generate_pre_expand_clinit (tree);
148 static int analyze_clinit_body (tree, tree);
149 static int maybe_yank_clinit (tree);
150 static void start_complete_expand_method (tree);
151 static void java_complete_expand_method (tree);
152 static void java_expand_method_bodies (tree);
153 static int unresolved_type_p (tree, tree *);
154 static void create_jdep_list (struct parser_ctxt *);
155 static tree build_expr_block (tree, tree);
156 static tree enter_block (void);
157 static tree exit_block (void);
158 static tree lookup_name_in_blocks (tree);
159 static void maybe_absorb_scoping_blocks (void);
160 static tree build_method_invocation (tree, tree);
161 static tree build_new_invocation (tree, tree);
162 static tree build_assignment (int, int, tree, tree);
163 static tree build_binop (enum tree_code, int, tree, tree);
164 static tree patch_assignment (tree, tree);
165 static tree patch_binop (tree, tree, tree);
166 static tree build_unaryop (int, int, tree);
167 static tree build_incdec (int, int, tree, int);
168 static tree patch_unaryop (tree, tree);
169 static tree build_cast (int, tree, tree);
170 static tree build_null_of_type (tree);
171 static tree patch_cast (tree, tree);
172 static int valid_ref_assignconv_cast_p (tree, tree, int);
173 static int valid_builtin_assignconv_identity_widening_p (tree, tree);
174 static int valid_cast_to_p (tree, tree);
175 static int valid_method_invocation_conversion_p (tree, tree);
176 static tree try_builtin_assignconv (tree, tree, tree);
177 static tree try_reference_assignconv (tree, tree);
178 static tree build_unresolved_array_type (tree);
179 static int build_type_name_from_array_name (tree, tree *);
180 static tree build_array_from_name (tree, tree, tree, tree *);
181 static tree build_array_ref (int, tree, tree);
182 static tree patch_array_ref (tree);
183 #ifdef USE_MAPPED_LOCATION
184 static tree make_qualified_name (tree, tree, source_location);
185 #else
186 static tree make_qualified_name (tree, tree, int);
187 #endif
188 static tree merge_qualified_name (tree, tree);
189 static tree make_qualified_primary (tree, tree, int);
190 static int resolve_qualified_expression_name (tree, tree *, tree *, tree *);
191 static void qualify_ambiguous_name (tree);
192 static tree resolve_field_access (tree, tree *, tree *);
193 static tree build_newarray_node (tree, tree, int);
194 static tree patch_newarray (tree);
195 static tree resolve_type_during_patch (tree);
196 static tree build_this (int);
197 static tree build_wfl_wrap (tree, int);
198 static tree build_return (int, tree);
199 static tree patch_return (tree);
200 static tree maybe_access_field (tree, tree, tree);
201 static int complete_function_arguments (tree);
202 static int check_for_static_method_reference (tree, tree, tree, tree, tree);
203 static int not_accessible_p (tree, tree, tree, int);
204 static void check_deprecation (tree, tree);
205 static int class_in_current_package (tree);
206 static tree build_if_else_statement (int, tree, tree, tree);
207 static tree patch_if_else_statement (tree);
208 static tree add_stmt_to_block (tree, tree, tree);
209 static tree patch_exit_expr (tree);
210 static tree build_labeled_block (int, tree);
211 static tree finish_labeled_statement (tree, tree);
212 static tree build_bc_statement (int, int, tree);
213 static tree patch_bc_statement (tree);
214 static tree patch_loop_statement (tree);
215 static tree build_new_loop (tree);
216 static tree build_loop_body (int, tree, int);
217 static tree finish_loop_body (int, tree, tree, int);
218 static tree build_debugable_stmt (int, tree);
219 static tree finish_for_loop (int, tree, tree, tree);
220 static tree patch_switch_statement (tree);
221 static tree string_constant_concatenation (tree, tree);
222 static tree build_string_concatenation (tree, tree);
223 static tree patch_string_cst (tree);
224 static tree patch_string (tree);
225 static tree encapsulate_with_try_catch (int, tree, tree, tree);
226 #ifdef USE_MAPPED_LOCATION
227 static tree build_assertion (source_location, tree, tree);
228 #else
229 static tree build_assertion (int, tree, tree);
230 #endif
231 static tree build_try_statement (int, tree, tree);
232 static tree build_try_finally_statement (int, tree, tree);
233 static tree patch_try_statement (tree);
234 static tree patch_synchronized_statement (tree, tree);
235 static tree patch_throw_statement (tree, tree);
236 #ifdef USE_MAPPED_LOCATION
237 static void check_thrown_exceptions (source_location, tree, tree);
238 #else
239 static void check_thrown_exceptions (int, tree, tree);
240 #endif
241 static int check_thrown_exceptions_do (tree);
242 static void purge_unchecked_exceptions (tree);
243 static bool ctors_unchecked_throws_clause_p (tree);
244 static void check_concrete_throws_clauses (tree, tree, tree, tree);
245 static void check_throws_clauses (tree, tree, tree);
246 static void finish_method_declaration (tree);
247 static tree build_super_invocation (tree);
248 static int verify_constructor_circularity (tree, tree);
249 static char *constructor_circularity_msg (tree, tree);
250 static tree build_this_super_qualified_invocation (int, tree, tree, int, int);
251 static const char *get_printable_method_name (tree);
252 static tree patch_conditional_expr (tree, tree, tree);
253 static tree generate_finit (tree);
254 static tree generate_instinit (tree);
255 static tree build_instinit_invocation (tree);
256 static void fix_constructors (tree);
257 static tree build_alias_initializer_parameter_list (int, tree, tree, int *);
258 static tree craft_constructor (tree, tree);
259 static int verify_constructor_super (tree);
260 static tree create_artificial_method (tree, int, tree, tree, tree);
261 static void start_artificial_method_body (tree);
262 static void end_artificial_method_body (tree);
263 static int check_method_redefinition (tree, tree);
264 static int check_method_types_complete (tree);
265 static bool hack_is_accessible_p (tree, tree);
266 static void java_check_regular_methods (tree);
267 static void check_interface_throws_clauses (tree, tree);
268 static void java_check_abstract_methods (tree);
269 static void unreachable_stmt_error (tree);
270 static int not_accessible_field_error (tree, tree);
271 static tree find_expr_with_wfl (tree);
272 static void missing_return_error (tree);
273 static tree build_new_array_init (int, tree);
274 static tree patch_new_array_init (tree, tree);
275 static tree maybe_build_array_element_wfl (tree);
276 static int array_constructor_check_entry (tree, tree);
277 static const char *purify_type_name (const char *);
278 static tree fold_constant_for_init (tree, tree);
279 static jdeplist *reverse_jdep_list (struct parser_ctxt *);
280 static void static_ref_err (tree, tree, tree);
281 static void parser_add_interface (tree, tree, tree);
282 static void add_superinterfaces (tree, tree);
283 static tree jdep_resolve_class (jdep *);
284 static int note_possible_classname (const char *, int);
285 static void java_complete_expand_classes (void);
286 static void java_complete_expand_class (tree);
287 static void java_complete_expand_methods (tree);
288 static tree cut_identifier_in_qualified (tree);
289 static tree java_stabilize_reference (tree);
290 static tree do_unary_numeric_promotion (tree);
291 static char * operator_string (tree);
292 static tree do_merge_string_cste (tree, const char *, int, int);
293 static tree merge_string_cste (tree, tree, int);
294 static tree java_refold (tree);
295 static int java_decl_equiv (tree, tree);
296 static int binop_compound_p (enum tree_code);
297 static tree search_loop (tree);
298 static int labeled_block_contains_loop_p (tree, tree);
299 static int check_abstract_method_definitions (int, tree, tree);
300 static void java_check_abstract_method_definitions (tree);
301 static void java_debug_context_do (int);
302 static void java_parser_context_push_initialized_field (void);
303 static void java_parser_context_pop_initialized_field (void);
304 static tree reorder_static_initialized (tree);
305 static void java_parser_context_suspend (void);
306 static void java_parser_context_resume (void);
307 static int pop_current_osb (struct parser_ctxt *);
309 /* JDK 1.1 work. FIXME */
311 static tree maybe_make_nested_class_name (tree);
312 static int make_nested_class_name (tree);
313 static void link_nested_class_to_enclosing (void);
314 static tree resolve_inner_class (htab_t, tree, tree *, tree *, tree);
315 static tree find_as_inner_class (tree, tree, tree);
316 static tree find_as_inner_class_do (tree, tree);
317 static int check_inner_class_redefinition (tree, tree);
319 static tree build_thisn_assign (void);
320 static tree build_current_thisn (tree);
321 static tree build_access_to_thisn (tree, tree, int);
322 static tree maybe_build_thisn_access_method (tree);
324 static tree build_outer_field_access (tree, tree);
325 static tree build_outer_field_access_methods (tree);
326 static tree build_outer_field_access_expr (int, tree, tree,
327 tree, tree);
328 static tree build_outer_method_access_method (tree);
329 static tree build_new_access_id (void);
330 static tree build_outer_field_access_method (tree, tree, tree,
331 tree, tree);
333 static int outer_field_access_p (tree, tree);
334 static int outer_field_expanded_access_p (tree, tree *,
335 tree *, tree *);
336 static tree outer_field_access_fix (tree, tree, tree);
337 static tree build_incomplete_class_ref (int, tree);
338 static tree patch_incomplete_class_ref (tree);
339 static tree create_anonymous_class (tree);
340 static void patch_anonymous_class (tree, tree, tree);
341 static void add_inner_class_fields (tree, tree);
343 static tree build_dot_class_method (tree);
344 static tree build_dot_class_method_invocation (tree, tree);
345 static void create_new_parser_context (int);
346 static tree maybe_build_class_init_for_field (tree, tree);
348 static int emit_test_initialization (void **, void *);
350 static char *string_convert_int_cst (tree);
352 /* Number of error found so far. */
353 int java_error_count;
354 /* Number of warning found so far. */
355 int java_warning_count;
356 /* Cyclic inheritance report, as it can be set by layout_class */
357 const char *cyclic_inheritance_report;
359 /* The current parser context */
360 struct parser_ctxt *ctxp;
362 /* List of things that were analyzed for which code will be generated */
363 struct parser_ctxt *ctxp_for_generation = NULL;
364 struct parser_ctxt *ctxp_for_generation_last = NULL;
366 /* binop_lookup maps token to tree_code. It is used where binary
367 operations are involved and required by the parser. RDIV_EXPR
368 covers both integral/floating point division. The code is changed
369 once the type of both operator is worked out. */
371 static const enum tree_code binop_lookup[19] =
373 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
374 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
375 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
376 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
377 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
379 #define BINOP_LOOKUP(VALUE) \
380 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
382 /* This is the end index for binary operators that can also be used
383 in compound assignments. */
384 #define BINOP_COMPOUND_CANDIDATES 11
386 /* The "$L" identifier we use to create labels. */
387 static GTY(()) tree label_id;
389 /* The "StringBuffer" identifier used for the String `+' operator. */
390 static GTY(()) tree wfl_string_buffer;
392 /* The "append" identifier used for String `+' operator. */
393 static GTY(()) tree wfl_append;
395 /* The "toString" identifier used for String `+' operator. */
396 static GTY(()) tree wfl_to_string;
398 /* The "java.lang" import qualified name. */
399 static GTY(()) tree java_lang_id;
401 /* The generated `inst$' identifier used for generated enclosing
402 instance/field access functions. */
403 static GTY(()) tree inst_id;
405 /* Context and flag for static blocks */
406 static GTY(()) tree current_static_block;
408 /* The generated `write_parm_value$' identifier. */
409 static GTY(()) tree wpv_id;
411 /* The list of all packages we've seen so far */
412 static GTY(()) tree package_list;
414 /* Hold THIS for the scope of the current method decl. */
415 static GTY(()) tree current_this;
417 /* Hold a list of catch clauses list. The first element of this list is
418 the list of the catch clauses of the currently analyzed try block. */
419 static GTY(()) tree currently_caught_type_list;
421 /* This holds a linked list of all the case labels for the current
422 switch statement. It is only used when checking to see if there
423 are duplicate labels. FIXME: probably this should just be attached
424 to the switch itself; then it could be referenced via
425 `ctxp->current_loop'. */
426 static GTY(()) tree case_label_list;
428 /* Anonymous class counter. Will be reset to 1 every time a non
429 anonymous class gets created. */
430 static int anonymous_class_counter = 1;
432 static GTY(()) tree src_parse_roots[1];
434 /* All classes seen from source code */
435 #define gclass_list src_parse_roots[0]
437 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
438 line and point it out. */
439 /* Should point out the one that don't fit. ASCII/unicode, going
440 backward. FIXME */
442 #define check_modifiers(__message, __value, __mask) do { \
443 if ((__value) & ~(__mask)) \
445 size_t i, remainder = (__value) & ~(__mask); \
446 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++) \
447 if ((1 << i) & remainder) \
448 parse_error_context (ctxp->modifier_ctx [i], (__message), \
449 java_accstring_lookup (1 << i)); \
451 } while (0)
455 %union {
456 tree node;
457 int sub_token;
458 struct {
459 int token;
460 #ifdef USE_MAPPED_LOCATION
461 source_location location;
462 #else
463 int location;
464 #endif
465 } operator;
466 int value;
470 #ifdef USE_MAPPED_LOCATION
471 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
472 SET_EXPR_LOCATION(EXPR, (TOKEN).location)
473 #else
474 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
475 (EXPR_WFL_LINECOL (EXPR) = (TOKEN).location)
476 #endif
478 #include "lex.c"
481 %pure_parser
483 /* Things defined here have to match the order of what's in the
484 binop_lookup table. */
486 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
487 %token LS_TK SRS_TK ZRS_TK
488 %token AND_TK XOR_TK OR_TK
489 %token BOOL_AND_TK BOOL_OR_TK
490 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
492 /* This maps to the same binop_lookup entry than the token above */
494 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
495 %token REM_ASSIGN_TK
496 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
497 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
500 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
502 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
503 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
504 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
505 %token PAD_TK ABSTRACT_TK STRICT_TK
506 %token MODIFIER_TK
508 /* Keep those two in order, too */
509 %token DECR_TK INCR_TK
511 /* From now one, things can be in any order */
513 %token DEFAULT_TK IF_TK THROW_TK
514 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
515 %token THROWS_TK BREAK_TK IMPORT_TK
516 %token ELSE_TK INSTANCEOF_TK RETURN_TK
517 %token VOID_TK CATCH_TK INTERFACE_TK
518 %token CASE_TK EXTENDS_TK FINALLY_TK
519 %token SUPER_TK WHILE_TK CLASS_TK
520 %token SWITCH_TK CONST_TK TRY_TK
521 %token FOR_TK NEW_TK CONTINUE_TK
522 %token GOTO_TK PACKAGE_TK THIS_TK
523 %token ASSERT_TK
525 %token BYTE_TK SHORT_TK INT_TK LONG_TK
526 %token CHAR_TK INTEGRAL_TK
528 %token FLOAT_TK DOUBLE_TK FP_TK
530 %token ID_TK
532 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
534 %token ASSIGN_ANY_TK ASSIGN_TK
535 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
537 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
538 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
540 %type <value> modifiers MODIFIER_TK final synchronized
542 %type <node> super ID_TK identifier
543 %type <node> name simple_name qualified_name
544 %type <node> type_declaration compilation_unit
545 field_declaration method_declaration extends_interfaces
546 interfaces interface_type_list
547 import_declarations package_declaration
548 type_declarations interface_body
549 interface_member_declaration constant_declaration
550 interface_member_declarations interface_type
551 abstract_method_declaration
552 %type <node> class_body_declaration class_member_declaration
553 static_initializer constructor_declaration block
554 %type <node> class_body_declarations constructor_header
555 %type <node> class_or_interface_type class_type class_type_list
556 constructor_declarator explicit_constructor_invocation
557 %type <node> dim_expr dim_exprs this_or_super throws
559 %type <node> variable_declarator_id variable_declarator
560 variable_declarators variable_initializer
561 variable_initializers constructor_body
562 array_initializer
564 %type <node> class_body block_end constructor_block_end
565 %type <node> statement statement_without_trailing_substatement
566 labeled_statement if_then_statement label_decl
567 if_then_else_statement while_statement for_statement
568 statement_nsi labeled_statement_nsi do_statement
569 if_then_else_statement_nsi while_statement_nsi
570 for_statement_nsi statement_expression_list for_init
571 for_update statement_expression expression_statement
572 primary_no_new_array expression primary array_type
573 array_creation_initialized array_creation_uninitialized
574 class_instance_creation_expression field_access
575 method_invocation array_access something_dot_new
576 argument_list postfix_expression while_expression
577 post_increment_expression post_decrement_expression
578 unary_expression_not_plus_minus unary_expression
579 pre_increment_expression pre_decrement_expression
580 cast_expression
581 multiplicative_expression additive_expression
582 shift_expression relational_expression
583 equality_expression and_expression
584 exclusive_or_expression inclusive_or_expression
585 conditional_and_expression conditional_or_expression
586 conditional_expression assignment_expression
587 left_hand_side assignment for_header for_begin
588 constant_expression do_statement_begin empty_statement
589 switch_statement synchronized_statement throw_statement
590 try_statement assert_statement
591 switch_expression switch_block
592 catches catch_clause catch_clause_parameter finally
593 anonymous_class_creation trap_overflow_corner_case
594 %type <node> return_statement break_statement continue_statement
596 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
597 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
598 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
599 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
600 %type <operator> ASSIGN_ANY_TK assignment_operator
601 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
602 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
603 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
604 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
605 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
606 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
607 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
608 %type <operator> NEW_TK ASSERT_TK
610 %type <node> method_body
612 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
613 STRING_LIT_TK NULL_TK VOID_TK
615 %type <node> IF_TK WHILE_TK FOR_TK
617 %type <node> formal_parameter_list formal_parameter
618 method_declarator method_header
620 %type <node> primitive_type reference_type type
621 BOOLEAN_TK INTEGRAL_TK FP_TK
623 /* Added or modified JDK 1.1 rule types */
624 %type <node> type_literals
627 /* 19.2 Production from 2.3: The Syntactic Grammar */
628 goal: compilation_unit
632 /* 19.3 Productions from 3: Lexical structure */
633 literal:
634 INT_LIT_TK
635 | FP_LIT_TK
636 | BOOL_LIT_TK
637 | CHAR_LIT_TK
638 | STRING_LIT_TK
639 | NULL_TK
642 /* 19.4 Productions from 4: Types, Values and Variables */
643 type:
644 primitive_type
645 | reference_type
648 primitive_type:
649 INTEGRAL_TK
650 | FP_TK
651 | BOOLEAN_TK
654 reference_type:
655 class_or_interface_type
656 | array_type
659 class_or_interface_type:
660 name
663 class_type:
664 class_or_interface_type /* Default rule */
667 interface_type:
668 class_or_interface_type
671 array_type:
672 primitive_type dims
674 int osb = pop_current_osb (ctxp);
675 tree t = build_java_array_type (($1), -1);
676 while (--osb)
677 t = build_unresolved_array_type (t);
678 $$ = t;
680 | name dims
682 int osb = pop_current_osb (ctxp);
683 tree t = $1;
684 while (osb--)
685 t = build_unresolved_array_type (t);
686 $$ = t;
690 /* 19.5 Productions from 6: Names */
691 name:
692 simple_name /* Default rule */
693 | qualified_name /* Default rule */
696 simple_name:
697 identifier /* Default rule */
700 qualified_name:
701 name DOT_TK identifier
702 { $$ = make_qualified_name ($1, $3, $2.location); }
705 identifier:
706 ID_TK
709 /* 19.6: Production from 7: Packages */
710 compilation_unit:
711 {$$ = NULL;}
712 | package_declaration
713 | import_declarations
714 | type_declarations
715 | package_declaration import_declarations
716 | package_declaration type_declarations
717 | import_declarations type_declarations
718 | package_declaration import_declarations type_declarations
721 import_declarations:
722 import_declaration
724 $$ = NULL;
726 | import_declarations import_declaration
728 $$ = NULL;
732 type_declarations:
733 type_declaration
734 | type_declarations type_declaration
737 package_declaration:
738 PACKAGE_TK name SC_TK
740 ctxp->package = EXPR_WFL_NODE ($2);
741 register_package (ctxp->package);
743 | PACKAGE_TK error
744 {yyerror ("Missing name"); RECOVER;}
745 | PACKAGE_TK name error
746 {yyerror ("';' expected"); RECOVER;}
749 import_declaration:
750 single_type_import_declaration
751 | type_import_on_demand_declaration
754 single_type_import_declaration:
755 IMPORT_TK name SC_TK
757 tree name = EXPR_WFL_NODE ($2), last_name;
758 int i = IDENTIFIER_LENGTH (name)-1;
759 const char *last = &IDENTIFIER_POINTER (name)[i];
760 while (last != IDENTIFIER_POINTER (name))
762 if (last [0] == '.')
763 break;
764 last--;
766 last_name = get_identifier (++last);
767 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
769 tree err = find_name_in_single_imports (last_name);
770 if (err && err != name)
771 parse_error_context
772 ($2, "Ambiguous class: %qs and %qs",
773 IDENTIFIER_POINTER (name),
774 IDENTIFIER_POINTER (err));
775 else
776 REGISTER_IMPORT ($2, last_name);
778 else
779 REGISTER_IMPORT ($2, last_name);
781 | IMPORT_TK error
782 {yyerror ("Missing name"); RECOVER;}
783 | IMPORT_TK name error
784 {yyerror ("';' expected"); RECOVER;}
787 type_import_on_demand_declaration:
788 IMPORT_TK name DOT_TK MULT_TK SC_TK
790 tree name = EXPR_WFL_NODE ($2);
791 tree it;
792 /* Search for duplicates. */
793 for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
794 if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
795 break;
796 /* Don't import the same thing more than once, just ignore
797 duplicates (7.5.2) */
798 if (! it)
800 read_import_dir ($2);
801 ctxp->import_demand_list =
802 chainon (ctxp->import_demand_list,
803 build_tree_list ($2, NULL_TREE));
806 | IMPORT_TK name DOT_TK error
807 {yyerror ("'*' expected"); RECOVER;}
808 | IMPORT_TK name DOT_TK MULT_TK error
809 {yyerror ("';' expected"); RECOVER;}
812 type_declaration:
813 class_declaration
814 { end_class_declaration (0); }
815 | interface_declaration
816 { end_class_declaration (0); }
817 | empty_statement
818 | error
820 YYERROR_NOW;
821 yyerror ("Class or interface declaration expected");
825 /* 19.7 Shortened from the original:
826 modifiers: modifier | modifiers modifier
827 modifier: any of public... */
828 modifiers:
829 MODIFIER_TK
831 $$ = (1 << $1);
833 | modifiers MODIFIER_TK
835 int acc = (1 << $2);
836 if ($$ & acc)
837 parse_error_context
838 (ctxp->modifier_ctx [$2], "Modifier %qs declared twice",
839 java_accstring_lookup (acc));
840 else
842 $$ |= acc;
847 /* 19.8.1 Production from $8.1: Class Declaration */
848 class_declaration:
849 modifiers CLASS_TK identifier super interfaces
850 { create_class ($1, $3, $4, $5); }
851 class_body
853 | CLASS_TK identifier super interfaces
854 { create_class (0, $2, $3, $4); }
855 class_body
857 | modifiers CLASS_TK error
858 { yyerror ("Missing class name"); RECOVER; }
859 | CLASS_TK error
860 { yyerror ("Missing class name"); RECOVER; }
861 | CLASS_TK identifier error
863 if (!ctxp->class_err) yyerror ("'{' expected");
864 DRECOVER(class1);
866 | modifiers CLASS_TK identifier error
867 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
870 super:
871 { $$ = NULL; }
872 | EXTENDS_TK class_type
873 { $$ = $2; }
874 | EXTENDS_TK class_type error
875 {yyerror ("'{' expected"); ctxp->class_err=1;}
876 | EXTENDS_TK error
877 {yyerror ("Missing super class name"); ctxp->class_err=1;}
880 interfaces:
881 { $$ = NULL_TREE; }
882 | IMPLEMENTS_TK interface_type_list
883 { $$ = $2; }
884 | IMPLEMENTS_TK error
886 ctxp->class_err=1;
887 yyerror ("Missing interface name");
891 interface_type_list:
892 interface_type
894 ctxp->interface_number = 1;
895 $$ = build_tree_list ($1, NULL_TREE);
897 | interface_type_list C_TK interface_type
899 ctxp->interface_number++;
900 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
902 | interface_type_list C_TK error
903 {yyerror ("Missing interface name"); RECOVER;}
906 class_body:
907 OCB_TK CCB_TK
909 $$ = GET_CPC ();
911 | OCB_TK class_body_declarations CCB_TK
913 $$ = GET_CPC ();
917 class_body_declarations:
918 class_body_declaration
919 | class_body_declarations class_body_declaration
922 class_body_declaration:
923 class_member_declaration
924 | static_initializer
925 | constructor_declaration
926 | block /* Added, JDK1.1, instance initializer */
928 if (!IS_EMPTY_STMT ($1))
930 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
931 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
936 class_member_declaration:
937 field_declaration
938 | method_declaration
939 | class_declaration /* Added, JDK1.1 inner classes */
940 { end_class_declaration (1); }
941 | interface_declaration /* Added, JDK1.1 inner interfaces */
942 { end_class_declaration (1); }
943 | empty_statement
946 /* 19.8.2 Productions from 8.3: Field Declarations */
947 field_declaration:
948 type variable_declarators SC_TK
949 { register_fields (0, $1, $2); }
950 | modifiers type variable_declarators SC_TK
952 check_modifiers
953 ("Illegal modifier %qs for field declaration",
954 $1, FIELD_MODIFIERS);
955 check_modifiers_consistency ($1);
956 register_fields ($1, $2, $3);
960 variable_declarators:
961 /* Should we use build_decl_list () instead ? FIXME */
962 variable_declarator /* Default rule */
963 | variable_declarators C_TK variable_declarator
964 { $$ = chainon ($1, $3); }
965 | variable_declarators C_TK error
966 {yyerror ("Missing term"); RECOVER;}
969 variable_declarator:
970 variable_declarator_id
971 { $$ = build_tree_list ($1, NULL_TREE); }
972 | variable_declarator_id ASSIGN_TK variable_initializer
974 if (java_error_count)
975 $3 = NULL_TREE;
976 $$ = build_tree_list
977 ($1, build_assignment ($2.token, $2.location, $1, $3));
979 | variable_declarator_id ASSIGN_TK error
981 yyerror ("Missing variable initializer");
982 $$ = build_tree_list ($1, NULL_TREE);
983 RECOVER;
985 | variable_declarator_id ASSIGN_TK variable_initializer error
987 yyerror ("';' expected");
988 $$ = build_tree_list ($1, NULL_TREE);
989 RECOVER;
993 variable_declarator_id:
994 identifier
995 | variable_declarator_id OSB_TK CSB_TK
996 { $$ = build_unresolved_array_type ($1); }
997 | identifier error
998 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
999 | variable_declarator_id OSB_TK error
1001 yyerror ("']' expected");
1002 DRECOVER(vdi);
1004 | variable_declarator_id CSB_TK error
1005 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1008 variable_initializer:
1009 expression
1010 | array_initializer
1013 /* 19.8.3 Productions from 8.4: Method Declarations */
1014 method_declaration:
1015 method_header
1017 current_function_decl = $1;
1018 if (current_function_decl
1019 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1020 source_start_java_method (current_function_decl);
1021 else
1022 current_function_decl = NULL_TREE;
1024 method_body
1025 { finish_method_declaration ($3); }
1026 | method_header error
1027 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1030 method_header:
1031 type method_declarator throws
1032 { $$ = method_header (0, $1, $2, $3); }
1033 | VOID_TK method_declarator throws
1034 { $$ = method_header (0, void_type_node, $2, $3); }
1035 | modifiers type method_declarator throws
1036 { $$ = method_header ($1, $2, $3, $4); }
1037 | modifiers VOID_TK method_declarator throws
1038 { $$ = method_header ($1, void_type_node, $3, $4); }
1039 | type error
1041 yyerror ("Invalid method declaration, method name required");
1042 RECOVER;
1044 | modifiers type error
1046 yyerror ("Identifier expected");
1047 RECOVER;
1049 | VOID_TK error
1051 yyerror ("Identifier expected");
1052 RECOVER;
1054 | modifiers VOID_TK error
1056 yyerror ("Identifier expected");
1057 RECOVER;
1059 | modifiers error
1061 yyerror ("Invalid method declaration, return type required");
1062 RECOVER;
1066 method_declarator:
1067 identifier OP_TK CP_TK
1069 ctxp->formal_parameter_number = 0;
1070 $$ = method_declarator ($1, NULL_TREE);
1072 | identifier OP_TK formal_parameter_list CP_TK
1073 { $$ = method_declarator ($1, $3); }
1074 | method_declarator OSB_TK CSB_TK
1076 SET_EXPR_LOCATION_FROM_TOKEN (wfl_operator, $2);
1077 TREE_PURPOSE ($1) =
1078 build_unresolved_array_type (TREE_PURPOSE ($1));
1079 parse_warning_context
1080 (wfl_operator,
1081 "Discouraged form of returned type specification");
1083 | identifier OP_TK error
1084 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1085 | method_declarator OSB_TK error
1086 {yyerror ("']' expected"); RECOVER;}
1089 formal_parameter_list:
1090 formal_parameter
1092 ctxp->formal_parameter_number = 1;
1094 | formal_parameter_list C_TK formal_parameter
1096 ctxp->formal_parameter_number += 1;
1097 $$ = chainon ($1, $3);
1099 | formal_parameter_list C_TK error
1100 { yyerror ("Missing formal parameter term"); RECOVER; }
1103 formal_parameter:
1104 type variable_declarator_id
1106 $$ = build_tree_list ($2, $1);
1108 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1110 $$ = build_tree_list ($3, $2);
1111 ARG_FINAL_P ($$) = 1;
1113 | type error
1115 yyerror ("Missing identifier"); RECOVER;
1116 $$ = NULL_TREE;
1118 | final type error
1120 yyerror ("Missing identifier"); RECOVER;
1121 $$ = NULL_TREE;
1125 final:
1126 modifiers
1128 check_modifiers ("Illegal modifier %qs. Only %<final%> was expected here",
1129 $1, ACC_FINAL);
1130 if ($1 != ACC_FINAL)
1131 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1135 throws:
1136 { $$ = NULL_TREE; }
1137 | THROWS_TK class_type_list
1138 { $$ = $2; }
1139 | THROWS_TK error
1140 {yyerror ("Missing class type term"); RECOVER;}
1143 class_type_list:
1144 class_type
1145 { $$ = build_tree_list ($1, $1); }
1146 | class_type_list C_TK class_type
1147 { $$ = tree_cons ($3, $3, $1); }
1148 | class_type_list C_TK error
1149 {yyerror ("Missing class type term"); RECOVER;}
1152 method_body:
1153 block
1154 | SC_TK { $$ = NULL_TREE; }
1157 /* 19.8.4 Productions from 8.5: Static Initializers */
1158 static_initializer:
1159 static block
1161 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1162 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1163 current_static_block = NULL_TREE;
1167 static: /* Test lval.sub_token here */
1168 modifiers
1170 check_modifiers ("Illegal modifier %qs for static initializer", $1, ACC_STATIC);
1171 /* Can't have a static initializer in an innerclass */
1172 if ($1 | ACC_STATIC &&
1173 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1174 parse_error_context
1175 (MODIFIER_WFL (STATIC_TK),
1176 "Can't define static initializer in class %qs. Static initializer can only be defined in top-level classes",
1177 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1178 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1182 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1183 constructor_declaration:
1184 constructor_header
1186 current_function_decl = $1;
1187 source_start_java_method (current_function_decl);
1189 constructor_body
1190 { finish_method_declaration ($3); }
1193 constructor_header:
1194 constructor_declarator throws
1195 { $$ = method_header (0, NULL_TREE, $1, $2); }
1196 | modifiers constructor_declarator throws
1197 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1200 constructor_declarator:
1201 simple_name OP_TK CP_TK
1203 ctxp->formal_parameter_number = 0;
1204 $$ = method_declarator ($1, NULL_TREE);
1206 | simple_name OP_TK formal_parameter_list CP_TK
1207 { $$ = method_declarator ($1, $3); }
1210 constructor_body:
1211 /* Unlike regular method, we always need a complete (empty)
1212 body so we can safely perform all the required code
1213 addition (super invocation and field initialization) */
1214 block_begin constructor_block_end
1216 BLOCK_EXPR_BODY ($2) = build_java_empty_stmt ();
1217 $$ = $2;
1219 | block_begin explicit_constructor_invocation constructor_block_end
1220 { $$ = $3; }
1221 | block_begin block_statements constructor_block_end
1222 { $$ = $3; }
1223 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1224 { $$ = $4; }
1227 constructor_block_end:
1228 block_end
1231 /* Error recovery for that rule moved down expression_statement: rule. */
1232 explicit_constructor_invocation:
1233 this_or_super OP_TK CP_TK SC_TK
1235 $$ = build_method_invocation ($1, NULL_TREE);
1236 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1237 $$ = java_method_add_stmt (current_function_decl, $$);
1239 | this_or_super OP_TK argument_list CP_TK SC_TK
1241 $$ = build_method_invocation ($1, $3);
1242 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1243 $$ = java_method_add_stmt (current_function_decl, $$);
1245 /* Added, JDK1.1 inner classes. Modified because the rule
1246 'primary' couldn't work. */
1247 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1248 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1249 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1250 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1253 this_or_super: /* Added, simplifies error diagnostics */
1254 THIS_TK
1256 tree wfl = build_wfl_node (this_identifier_node);
1257 SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1258 $$ = wfl;
1260 | SUPER_TK
1262 tree wfl = build_wfl_node (super_identifier_node);
1263 SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1264 $$ = wfl;
1268 /* 19.9 Productions from 9: Interfaces */
1269 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1270 interface_declaration:
1271 INTERFACE_TK identifier
1272 { create_interface (0, $2, NULL_TREE); }
1273 interface_body
1274 { ; }
1275 | modifiers INTERFACE_TK identifier
1276 { create_interface ($1, $3, NULL_TREE); }
1277 interface_body
1278 { ; }
1279 | INTERFACE_TK identifier extends_interfaces
1280 { create_interface (0, $2, $3); }
1281 interface_body
1282 { ; }
1283 | modifiers INTERFACE_TK identifier extends_interfaces
1284 { create_interface ($1, $3, $4); }
1285 interface_body
1286 { ; }
1287 | INTERFACE_TK identifier error
1288 { yyerror ("'{' expected"); RECOVER; }
1289 | modifiers INTERFACE_TK identifier error
1290 { yyerror ("'{' expected"); RECOVER; }
1293 extends_interfaces:
1294 EXTENDS_TK interface_type
1296 ctxp->interface_number = 1;
1297 $$ = build_tree_list ($2, NULL_TREE);
1299 | extends_interfaces C_TK interface_type
1301 ctxp->interface_number++;
1302 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1304 | EXTENDS_TK error
1305 {yyerror ("Invalid interface type"); RECOVER;}
1306 | extends_interfaces C_TK error
1307 {yyerror ("Missing term"); RECOVER;}
1310 interface_body:
1311 OCB_TK CCB_TK
1312 { $$ = NULL_TREE; }
1313 | OCB_TK interface_member_declarations CCB_TK
1314 { $$ = NULL_TREE; }
1317 interface_member_declarations:
1318 interface_member_declaration
1319 | interface_member_declarations interface_member_declaration
1322 interface_member_declaration:
1323 constant_declaration
1324 | abstract_method_declaration
1325 | class_declaration /* Added, JDK1.1 inner classes */
1326 { end_class_declaration (1); }
1327 | interface_declaration /* Added, JDK1.1 inner interfaces */
1328 { end_class_declaration (1); }
1331 constant_declaration:
1332 field_declaration
1335 abstract_method_declaration:
1336 method_header SC_TK
1338 check_abstract_method_header ($1);
1339 current_function_decl = NULL_TREE; /* FIXME ? */
1341 | method_header error
1342 {yyerror ("';' expected"); RECOVER;}
1345 /* 19.10 Productions from 10: Arrays */
1346 array_initializer:
1347 OCB_TK CCB_TK
1348 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1349 | OCB_TK C_TK CCB_TK
1350 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1351 | OCB_TK variable_initializers CCB_TK
1352 { $$ = build_new_array_init ($1.location, $2); }
1353 | OCB_TK variable_initializers C_TK CCB_TK
1354 { $$ = build_new_array_init ($1.location, $2); }
1357 variable_initializers:
1358 variable_initializer
1360 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1361 $1, NULL_TREE);
1363 | variable_initializers C_TK variable_initializer
1365 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1367 | variable_initializers C_TK error
1368 {yyerror ("Missing term"); RECOVER;}
1371 /* 19.11 Production from 14: Blocks and Statements */
1372 block:
1373 block_begin block_end
1374 { $$ = $2; }
1375 | block_begin block_statements block_end
1376 { $$ = $3; }
1379 block_begin:
1380 OCB_TK
1381 { enter_block (); }
1384 block_end:
1385 CCB_TK
1387 maybe_absorb_scoping_blocks ();
1388 $$ = exit_block ();
1389 if (!BLOCK_SUBBLOCKS ($$))
1390 BLOCK_SUBBLOCKS ($$) = build_java_empty_stmt ();
1394 block_statements:
1395 block_statement
1396 | block_statements block_statement
1399 block_statement:
1400 local_variable_declaration_statement
1401 | statement
1402 { java_method_add_stmt (current_function_decl, $1); }
1403 | class_declaration /* Added, JDK1.1 local classes */
1405 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1406 end_class_declaration (1);
1410 local_variable_declaration_statement:
1411 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1414 local_variable_declaration:
1415 type variable_declarators
1416 { declare_local_variables (0, $1, $2); }
1417 | final type variable_declarators /* Added, JDK1.1 final locals */
1418 { declare_local_variables ($1, $2, $3); }
1421 statement:
1422 statement_without_trailing_substatement
1423 | labeled_statement
1424 | if_then_statement
1425 | if_then_else_statement
1426 | while_statement
1427 | for_statement
1428 { $$ = exit_block (); }
1431 statement_nsi:
1432 statement_without_trailing_substatement
1433 | labeled_statement_nsi
1434 | if_then_else_statement_nsi
1435 | while_statement_nsi
1436 | for_statement_nsi
1437 { $$ = exit_block (); }
1440 statement_without_trailing_substatement:
1441 block
1442 | empty_statement
1443 | expression_statement
1444 | switch_statement
1445 | do_statement
1446 | break_statement
1447 | continue_statement
1448 | return_statement
1449 | synchronized_statement
1450 | throw_statement
1451 | try_statement
1452 | assert_statement
1455 empty_statement:
1456 SC_TK
1458 if (flag_extraneous_semicolon
1459 && ! current_static_block
1460 && (! current_function_decl ||
1461 /* Verify we're not in a inner class declaration */
1462 (GET_CPC () != TYPE_NAME
1463 (DECL_CONTEXT (current_function_decl)))))
1466 #ifdef USE_MAPPED_LOCATION
1467 SET_EXPR_LOCATION (wfl_operator, input_location);
1468 #else
1469 EXPR_WFL_SET_LINECOL (wfl_operator, input_line, -1);
1470 #endif
1471 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1473 $$ = build_java_empty_stmt ();
1477 label_decl:
1478 identifier REL_CL_TK
1480 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1481 EXPR_WFL_NODE ($1));
1482 pushlevel (2);
1483 push_labeled_block ($$);
1484 PUSH_LABELED_BLOCK ($$);
1488 labeled_statement:
1489 label_decl statement
1490 { $$ = finish_labeled_statement ($1, $2); }
1491 | identifier error
1492 {yyerror ("':' expected"); RECOVER;}
1495 labeled_statement_nsi:
1496 label_decl statement_nsi
1497 { $$ = finish_labeled_statement ($1, $2); }
1500 /* We concentrate here a bunch of error handling rules that we couldn't write
1501 earlier, because expression_statement catches a missing ';'. */
1502 expression_statement:
1503 statement_expression SC_TK
1505 /* We have a statement. Generate a WFL around it so
1506 we can debug it */
1507 #ifdef USE_MAPPED_LOCATION
1508 $$ = expr_add_location ($1, input_location, 1);
1509 #else
1510 $$ = build_expr_wfl ($1, input_filename, input_line, 0);
1511 JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1512 #endif
1513 /* We know we have a statement, so set the debug
1514 info to be eventually generate here. */
1516 | error SC_TK
1518 YYNOT_TWICE yyerror ("Invalid expression statement");
1519 DRECOVER (expr_stmt);
1521 | error OCB_TK
1523 YYNOT_TWICE yyerror ("Invalid expression statement");
1524 DRECOVER (expr_stmt);
1526 | error CCB_TK
1528 YYNOT_TWICE yyerror ("Invalid expression statement");
1529 DRECOVER (expr_stmt);
1531 | this_or_super OP_TK error
1532 {yyerror ("')' expected"); RECOVER;}
1533 | this_or_super OP_TK CP_TK error
1535 parse_ctor_invocation_error ();
1536 RECOVER;
1538 | this_or_super OP_TK argument_list error
1539 {yyerror ("')' expected"); RECOVER;}
1540 | this_or_super OP_TK argument_list CP_TK error
1542 parse_ctor_invocation_error ();
1543 RECOVER;
1545 | name DOT_TK SUPER_TK error
1546 {yyerror ("'(' expected"); RECOVER;}
1547 | name DOT_TK SUPER_TK OP_TK error
1548 {yyerror ("')' expected"); RECOVER;}
1549 | name DOT_TK SUPER_TK OP_TK argument_list error
1550 {yyerror ("')' expected"); RECOVER;}
1551 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1552 {yyerror ("';' expected"); RECOVER;}
1553 | name DOT_TK SUPER_TK OP_TK CP_TK error
1554 {yyerror ("';' expected"); RECOVER;}
1557 statement_expression:
1558 assignment
1559 | pre_increment_expression
1560 | pre_decrement_expression
1561 | post_increment_expression
1562 | post_decrement_expression
1563 | method_invocation
1564 | class_instance_creation_expression
1567 if_then_statement:
1568 IF_TK OP_TK expression CP_TK statement
1570 $$ = build_if_else_statement ($2.location, $3,
1571 $5, NULL_TREE);
1573 | IF_TK error
1574 {yyerror ("'(' expected"); RECOVER;}
1575 | IF_TK OP_TK error
1576 {yyerror ("Missing term"); RECOVER;}
1577 | IF_TK OP_TK expression error
1578 {yyerror ("')' expected"); RECOVER;}
1581 if_then_else_statement:
1582 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1583 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1586 if_then_else_statement_nsi:
1587 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1588 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1591 switch_statement:
1592 switch_expression
1594 enter_block ();
1596 switch_block
1598 /* Make into "proper list" of COMPOUND_EXPRs.
1599 I.e. make the last statement also have its own
1600 COMPOUND_EXPR. */
1601 maybe_absorb_scoping_blocks ();
1602 TREE_OPERAND ($1, 1) = exit_block ();
1603 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1607 switch_expression:
1608 SWITCH_TK OP_TK expression CP_TK
1610 $$ = build3 (SWITCH_EXPR, NULL_TREE, $3,
1611 NULL_TREE, NULL_TREE);
1612 SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
1614 | SWITCH_TK error
1615 {yyerror ("'(' expected"); RECOVER;}
1616 | SWITCH_TK OP_TK error
1617 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1618 | SWITCH_TK OP_TK expression CP_TK error
1619 {yyerror ("'{' expected"); RECOVER;}
1622 /* Default assignment is there to avoid type node on switch_block
1623 node. */
1625 switch_block:
1626 OCB_TK CCB_TK
1627 { $$ = NULL_TREE; }
1628 | OCB_TK switch_labels CCB_TK
1629 { $$ = NULL_TREE; }
1630 | OCB_TK switch_block_statement_groups CCB_TK
1631 { $$ = NULL_TREE; }
1632 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1633 { $$ = NULL_TREE; }
1636 switch_block_statement_groups:
1637 switch_block_statement_group
1638 | switch_block_statement_groups switch_block_statement_group
1641 switch_block_statement_group:
1642 switch_labels block_statements
1645 switch_labels:
1646 switch_label
1647 | switch_labels switch_label
1650 switch_label:
1651 CASE_TK constant_expression REL_CL_TK
1653 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1654 SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1655 java_method_add_stmt (current_function_decl, lab);
1657 | DEFAULT_TK REL_CL_TK
1659 tree lab = make_node (DEFAULT_EXPR);
1660 SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1661 java_method_add_stmt (current_function_decl, lab);
1663 | CASE_TK error
1664 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1665 | CASE_TK constant_expression error
1666 {yyerror ("':' expected"); RECOVER;}
1667 | DEFAULT_TK error
1668 {yyerror ("':' expected"); RECOVER;}
1671 while_expression:
1672 WHILE_TK OP_TK expression CP_TK
1674 tree body = build_loop_body ($2.location, $3, 0);
1675 $$ = build_new_loop (body);
1679 while_statement:
1680 while_expression statement
1681 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1682 | WHILE_TK error
1683 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1684 | WHILE_TK OP_TK error
1685 {yyerror ("Missing term and ')' expected"); RECOVER;}
1686 | WHILE_TK OP_TK expression error
1687 {yyerror ("')' expected"); RECOVER;}
1690 while_statement_nsi:
1691 while_expression statement_nsi
1692 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1695 do_statement_begin:
1696 DO_TK
1698 tree body = build_loop_body (0, NULL_TREE, 1);
1699 $$ = build_new_loop (body);
1701 /* Need error handing here. FIXME */
1704 do_statement:
1705 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1706 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1709 for_statement:
1710 for_begin SC_TK expression SC_TK for_update CP_TK statement
1712 if (CONSTANT_CLASS_P ($3))
1713 $3 = build_wfl_node ($3);
1714 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1716 | for_begin SC_TK SC_TK for_update CP_TK statement
1718 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1719 /* We have not condition, so we get rid of the EXIT_EXPR */
1720 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1721 build_java_empty_stmt ();
1723 | for_begin SC_TK error
1724 {yyerror ("Invalid control expression"); RECOVER;}
1725 | for_begin SC_TK expression SC_TK error
1726 {yyerror ("Invalid update expression"); RECOVER;}
1727 | for_begin SC_TK SC_TK error
1728 {yyerror ("Invalid update expression"); RECOVER;}
1731 for_statement_nsi:
1732 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1733 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1734 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1736 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1737 /* We have not condition, so we get rid of the EXIT_EXPR */
1738 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1739 build_java_empty_stmt ();
1743 for_header:
1744 FOR_TK OP_TK
1746 /* This scope defined for local variable that may be
1747 defined within the scope of the for loop */
1748 enter_block ();
1750 | FOR_TK error
1751 {yyerror ("'(' expected"); DRECOVER(for_1);}
1752 | FOR_TK OP_TK error
1753 {yyerror ("Invalid init statement"); RECOVER;}
1756 for_begin:
1757 for_header for_init
1759 /* We now declare the loop body. The loop is
1760 declared as a for loop. */
1761 tree body = build_loop_body (0, NULL_TREE, 0);
1762 $$ = build_new_loop (body);
1763 FOR_LOOP_P ($$) = 1;
1764 /* The loop is added to the current block the for
1765 statement is defined within */
1766 java_method_add_stmt (current_function_decl, $$);
1769 for_init: /* Can be empty */
1770 { $$ = build_java_empty_stmt (); }
1771 | statement_expression_list
1773 /* Init statement recorded within the previously
1774 defined block scope */
1775 $$ = java_method_add_stmt (current_function_decl, $1);
1777 | local_variable_declaration
1779 /* Local variable are recorded within the previously
1780 defined block scope */
1781 $$ = NULL_TREE;
1783 | statement_expression_list error
1784 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1787 for_update: /* Can be empty */
1788 {$$ = build_java_empty_stmt ();}
1789 | statement_expression_list
1790 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1793 statement_expression_list:
1794 statement_expression
1795 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1796 | statement_expression_list C_TK statement_expression
1797 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1798 | statement_expression_list C_TK error
1799 {yyerror ("Missing term"); RECOVER;}
1802 break_statement:
1803 BREAK_TK SC_TK
1804 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1805 | BREAK_TK identifier SC_TK
1806 { $$ = build_bc_statement ($1.location, 1, $2); }
1807 | BREAK_TK error
1808 {yyerror ("Missing term"); RECOVER;}
1809 | BREAK_TK identifier error
1810 {yyerror ("';' expected"); RECOVER;}
1813 continue_statement:
1814 CONTINUE_TK SC_TK
1815 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1816 | CONTINUE_TK identifier SC_TK
1817 { $$ = build_bc_statement ($1.location, 0, $2); }
1818 | CONTINUE_TK error
1819 {yyerror ("Missing term"); RECOVER;}
1820 | CONTINUE_TK identifier error
1821 {yyerror ("';' expected"); RECOVER;}
1824 return_statement:
1825 RETURN_TK SC_TK
1826 { $$ = build_return ($1.location, NULL_TREE); }
1827 | RETURN_TK expression SC_TK
1828 { $$ = build_return ($1.location, $2); }
1829 | RETURN_TK error
1830 {yyerror ("Missing term"); RECOVER;}
1831 | RETURN_TK expression error
1832 {yyerror ("';' expected"); RECOVER;}
1835 throw_statement:
1836 THROW_TK expression SC_TK
1838 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1839 SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1841 | THROW_TK error
1842 {yyerror ("Missing term"); RECOVER;}
1843 | THROW_TK expression error
1844 {yyerror ("';' expected"); RECOVER;}
1847 assert_statement:
1848 ASSERT_TK expression REL_CL_TK expression SC_TK
1850 $$ = build_assertion ($1.location, $2, $4);
1852 | ASSERT_TK expression SC_TK
1854 $$ = build_assertion ($1.location, $2, NULL_TREE);
1856 | ASSERT_TK error
1857 {yyerror ("Missing term"); RECOVER;}
1858 | ASSERT_TK expression error
1859 {yyerror ("';' expected"); RECOVER;}
1862 synchronized_statement:
1863 synchronized OP_TK expression CP_TK block
1865 $$ = build2 (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1866 EXPR_WFL_LINECOL ($$) =
1867 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1869 | synchronized OP_TK expression CP_TK error
1870 {yyerror ("'{' expected"); RECOVER;}
1871 | synchronized error
1872 {yyerror ("'(' expected"); RECOVER;}
1873 | synchronized OP_TK error CP_TK
1874 {yyerror ("Missing term"); RECOVER;}
1875 | synchronized OP_TK error
1876 {yyerror ("Missing term"); RECOVER;}
1879 synchronized:
1880 modifiers
1882 check_modifiers (
1883 "Illegal modifier %qs. Only %<synchronized%> was expected here",
1884 $1, ACC_SYNCHRONIZED);
1885 if ($1 != ACC_SYNCHRONIZED)
1886 MODIFIER_WFL (SYNCHRONIZED_TK) =
1887 build_wfl_node (NULL_TREE);
1891 try_statement:
1892 TRY_TK block catches
1893 { $$ = build_try_statement ($1.location, $2, $3); }
1894 | TRY_TK block finally
1895 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1896 | TRY_TK block catches finally
1897 { $$ = build_try_finally_statement
1898 ($1.location, build_try_statement ($1.location,
1899 $2, $3), $4);
1901 | TRY_TK error
1902 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1905 catches:
1906 catch_clause
1907 | catches catch_clause
1909 TREE_CHAIN ($2) = $1;
1910 $$ = $2;
1914 catch_clause:
1915 catch_clause_parameter block
1917 java_method_add_stmt (current_function_decl, $2);
1918 exit_block ();
1919 $$ = $1;
1923 catch_clause_parameter:
1924 CATCH_TK OP_TK formal_parameter CP_TK
1926 /* We add a block to define a scope for
1927 formal_parameter (CCBP). The formal parameter is
1928 declared initialized by the appropriate function
1929 call */
1930 tree ccpb;
1931 tree init;
1932 if ($3)
1934 ccpb = enter_block ();
1935 init = build_assignment
1936 (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1937 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1938 declare_local_variables (0, TREE_VALUE ($3),
1939 build_tree_list
1940 (TREE_PURPOSE ($3), init));
1941 $$ = build1 (JAVA_CATCH_EXPR, NULL_TREE, ccpb);
1942 SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1944 else
1946 $$ = error_mark_node;
1949 | CATCH_TK error
1950 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1951 | CATCH_TK OP_TK error
1953 yyerror ("Missing term or ')' expected");
1954 RECOVER; $$ = NULL_TREE;
1956 | CATCH_TK OP_TK error CP_TK /* That's for () */
1957 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1960 finally:
1961 FINALLY_TK block
1962 { $$ = $2; }
1963 | FINALLY_TK error
1964 {yyerror ("'{' expected"); RECOVER; }
1967 /* 19.12 Production from 15: Expressions */
1968 primary:
1969 primary_no_new_array
1970 | array_creation_uninitialized
1971 | array_creation_initialized
1974 primary_no_new_array:
1975 literal
1976 | THIS_TK
1977 { $$ = build_this ($1.location); }
1978 | OP_TK expression CP_TK
1979 {$$ = $2;}
1980 | class_instance_creation_expression
1981 | field_access
1982 | method_invocation
1983 | array_access
1984 | type_literals
1985 /* Added, JDK1.1 inner classes. Documentation is wrong
1986 referring to a 'ClassName' (class_name) rule that doesn't
1987 exist. Used name: instead. */
1988 | name DOT_TK THIS_TK
1990 tree wfl = build_wfl_node (this_identifier_node);
1991 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1993 | OP_TK expression error
1994 {yyerror ("')' expected"); RECOVER;}
1995 | name DOT_TK error
1996 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1997 | primitive_type DOT_TK error
1998 {yyerror ("'class' expected" ); RECOVER;}
1999 | VOID_TK DOT_TK error
2000 {yyerror ("'class' expected" ); RECOVER;}
2003 type_literals:
2004 name DOT_TK CLASS_TK
2005 { $$ = build_incomplete_class_ref ($2.location, $1); }
2006 | array_type DOT_TK CLASS_TK
2007 { $$ = build_incomplete_class_ref ($2.location, $1); }
2008 | primitive_type DOT_TK CLASS_TK
2009 { $$ = build_incomplete_class_ref ($2.location, $1); }
2010 | VOID_TK DOT_TK CLASS_TK
2012 $$ = build_incomplete_class_ref ($2.location,
2013 void_type_node);
2017 class_instance_creation_expression:
2018 NEW_TK class_type OP_TK argument_list CP_TK
2019 { $$ = build_new_invocation ($2, $4); }
2020 | NEW_TK class_type OP_TK CP_TK
2021 { $$ = build_new_invocation ($2, NULL_TREE); }
2022 | anonymous_class_creation
2023 /* Added, JDK1.1 inner classes, modified to use name or
2024 primary instead of primary solely which couldn't work in
2025 all situations. */
2026 | something_dot_new identifier OP_TK CP_TK
2028 tree ctor = build_new_invocation ($2, NULL_TREE);
2029 $$ = make_qualified_primary ($1, ctor,
2030 EXPR_WFL_LINECOL ($1));
2032 | something_dot_new identifier OP_TK CP_TK class_body
2033 | something_dot_new identifier OP_TK argument_list CP_TK
2035 tree ctor = build_new_invocation ($2, $4);
2036 $$ = make_qualified_primary ($1, ctor,
2037 EXPR_WFL_LINECOL ($1));
2039 | something_dot_new identifier OP_TK argument_list CP_TK class_body
2040 | NEW_TK error SC_TK
2041 {$$ = NULL_TREE; yyerror ("'(' expected"); DRECOVER(new_1);}
2042 | NEW_TK class_type error
2043 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2044 | NEW_TK class_type OP_TK error
2045 {$$ = NULL_TREE; yyerror ("')' or term expected"); RECOVER;}
2046 | NEW_TK class_type OP_TK argument_list error
2047 {$$ = NULL_TREE; yyerror ("')' expected"); RECOVER;}
2048 | something_dot_new error
2050 $$ = NULL_TREE;
2051 YYERROR_NOW;
2052 yyerror ("Identifier expected");
2053 RECOVER;
2055 | something_dot_new identifier error
2056 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2059 /* Created after JDK1.1 rules originally added to
2060 class_instance_creation_expression, but modified to use
2061 'class_type' instead of 'TypeName' (type_name) which is mentioned
2062 in the documentation but doesn't exist. */
2064 anonymous_class_creation:
2065 NEW_TK class_type OP_TK argument_list CP_TK
2066 { create_anonymous_class ($2); }
2067 class_body
2069 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2070 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2072 end_class_declaration (1);
2074 /* Now we can craft the new expression */
2075 $$ = build_new_invocation (id, $4);
2077 /* Note that we can't possibly be here if
2078 `class_type' is an interface (in which case the
2079 anonymous class extends Object and implements
2080 `class_type', hence its constructor can't have
2081 arguments.) */
2083 /* Otherwise, the innerclass must feature a
2084 constructor matching `argument_list'. Anonymous
2085 classes are a bit special: it's impossible to
2086 define constructor for them, hence constructors
2087 must be generated following the hints provided by
2088 the `new' expression. Whether a super constructor
2089 of that nature exists or not is to be verified
2090 later on in verify_constructor_super.
2092 It's during the expansion of a `new' statement
2093 referring to an anonymous class that a ctor will
2094 be generated for the anonymous class, with the
2095 right arguments. */
2098 | NEW_TK class_type OP_TK CP_TK
2099 { create_anonymous_class ($2); }
2100 class_body
2102 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2103 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2105 end_class_declaration (1);
2107 /* Now we can craft the new expression. The
2108 statement doesn't need to be remember so that a
2109 constructor can be generated, since its signature
2110 is already known. */
2111 $$ = build_new_invocation (id, NULL_TREE);
2115 something_dot_new: /* Added, not part of the specs. */
2116 name DOT_TK NEW_TK
2117 { $$ = $1; }
2118 | primary DOT_TK NEW_TK
2119 { $$ = $1; }
2122 argument_list:
2123 expression
2125 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2126 ctxp->formal_parameter_number = 1;
2128 | argument_list C_TK expression
2130 ctxp->formal_parameter_number += 1;
2131 $$ = tree_cons (NULL_TREE, $3, $1);
2133 | argument_list C_TK error
2134 {yyerror ("Missing term"); RECOVER;}
2137 array_creation_uninitialized:
2138 NEW_TK primitive_type dim_exprs
2139 { $$ = build_newarray_node ($2, $3, 0); }
2140 | NEW_TK class_or_interface_type dim_exprs
2141 { $$ = build_newarray_node ($2, $3, 0); }
2142 | NEW_TK primitive_type dim_exprs dims
2143 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2144 | NEW_TK class_or_interface_type dim_exprs dims
2145 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2146 | NEW_TK error CSB_TK
2147 {yyerror ("'[' expected"); DRECOVER ("]");}
2148 | NEW_TK error OSB_TK
2149 {yyerror ("']' expected"); RECOVER;}
2152 array_creation_initialized:
2153 /* Added, JDK1.1 anonymous array. Initial documentation rule
2154 modified */
2155 NEW_TK class_or_interface_type dims array_initializer
2157 char *sig;
2158 int osb = pop_current_osb (ctxp);
2159 while (osb--)
2160 obstack_grow (&temporary_obstack, "[]", 2);
2161 obstack_1grow (&temporary_obstack, '\0');
2162 sig = obstack_finish (&temporary_obstack);
2163 $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2164 $2, get_identifier (sig), $4);
2166 | NEW_TK primitive_type dims array_initializer
2168 int osb = pop_current_osb (ctxp);
2169 tree type = $2;
2170 while (osb--)
2171 type = build_java_array_type (type, -1);
2172 $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2173 build_pointer_type (type), NULL_TREE, $4);
2175 | NEW_TK error CSB_TK
2176 {yyerror ("'[' expected"); DRECOVER ("]");}
2177 | NEW_TK error OSB_TK
2178 {yyerror ("']' expected"); RECOVER;}
2181 dim_exprs:
2182 dim_expr
2183 { $$ = build_tree_list (NULL_TREE, $1); }
2184 | dim_exprs dim_expr
2185 { $$ = tree_cons (NULL_TREE, $2, $$); }
2188 dim_expr:
2189 OSB_TK expression CSB_TK
2191 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2193 $2 = build_wfl_node ($2);
2194 TREE_TYPE ($2) = NULL_TREE;
2196 EXPR_WFL_LINECOL ($2) = $1.location;
2197 $$ = $2;
2199 | OSB_TK expression error
2200 {yyerror ("']' expected"); RECOVER;}
2201 | OSB_TK error
2203 yyerror ("Missing term");
2204 yyerror ("']' expected");
2205 RECOVER;
2209 dims:
2210 OSB_TK CSB_TK
2212 int allocate = 0;
2213 /* If not initialized, allocate memory for the osb
2214 numbers stack */
2215 if (!ctxp->osb_limit)
2217 allocate = ctxp->osb_limit = 32;
2218 ctxp->osb_depth = -1;
2220 /* If capacity overflown, reallocate a bigger chunk */
2221 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2222 allocate = ctxp->osb_limit << 1;
2224 if (allocate)
2226 allocate *= sizeof (int);
2227 if (ctxp->osb_number)
2228 ctxp->osb_number = xrealloc (ctxp->osb_number,
2229 allocate);
2230 else
2231 ctxp->osb_number = xmalloc (allocate);
2233 ctxp->osb_depth++;
2234 CURRENT_OSB (ctxp) = 1;
2236 | dims OSB_TK CSB_TK
2237 { CURRENT_OSB (ctxp)++; }
2238 | dims OSB_TK error
2239 { yyerror ("']' expected"); RECOVER;}
2242 field_access:
2243 primary DOT_TK identifier
2244 { $$ = make_qualified_primary ($1, $3, $2.location); }
2245 /* FIXME - REWRITE TO:
2246 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2247 | SUPER_TK DOT_TK identifier
2249 tree super_wfl = build_wfl_node (super_identifier_node);
2250 SET_EXPR_LOCATION_FROM_TOKEN (super_wfl, $1);
2251 $$ = make_qualified_name (super_wfl, $3, $2.location);
2253 | SUPER_TK error
2254 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2257 method_invocation:
2258 name OP_TK CP_TK
2259 { $$ = build_method_invocation ($1, NULL_TREE); }
2260 | name OP_TK argument_list CP_TK
2261 { $$ = build_method_invocation ($1, $3); }
2262 | primary DOT_TK identifier OP_TK CP_TK
2264 if (TREE_CODE ($1) == THIS_EXPR)
2265 $$ = build_this_super_qualified_invocation
2266 (1, $3, NULL_TREE, 0, $2.location);
2267 else
2269 tree invok = build_method_invocation ($3, NULL_TREE);
2270 $$ = make_qualified_primary ($1, invok, $2.location);
2273 | primary DOT_TK identifier OP_TK argument_list CP_TK
2275 if (TREE_CODE ($1) == THIS_EXPR)
2276 $$ = build_this_super_qualified_invocation
2277 (1, $3, $5, 0, $2.location);
2278 else
2280 tree invok = build_method_invocation ($3, $5);
2281 $$ = make_qualified_primary ($1, invok, $2.location);
2284 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2286 $$ = build_this_super_qualified_invocation
2287 (0, $3, NULL_TREE, $1.location, $2.location);
2289 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2291 $$ = build_this_super_qualified_invocation
2292 (0, $3, $5, $1.location, $2.location);
2294 /* Screws up thing. I let it here until I'm convinced it can
2295 be removed. FIXME
2296 | primary DOT_TK error
2297 {yyerror ("'(' expected"); DRECOVER(bad);} */
2298 | SUPER_TK DOT_TK error CP_TK
2299 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2300 | SUPER_TK DOT_TK error DOT_TK
2301 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2304 array_access:
2305 name OSB_TK expression CSB_TK
2306 { $$ = build_array_ref ($2.location, $1, $3); }
2307 | primary_no_new_array OSB_TK expression CSB_TK
2308 { $$ = build_array_ref ($2.location, $1, $3); }
2309 | array_creation_initialized OSB_TK expression CSB_TK
2310 { $$ = build_array_ref ($2.location, $1, $3); }
2311 | name OSB_TK error
2313 yyerror ("Missing term and ']' expected");
2314 DRECOVER(array_access);
2316 | name OSB_TK expression error
2318 yyerror ("']' expected");
2319 DRECOVER(array_access);
2321 | primary_no_new_array OSB_TK error
2323 yyerror ("Missing term and ']' expected");
2324 DRECOVER(array_access);
2326 | primary_no_new_array OSB_TK expression error
2328 yyerror ("']' expected");
2329 DRECOVER(array_access);
2331 | array_creation_initialized OSB_TK error
2333 yyerror ("Missing term and ']' expected");
2334 DRECOVER(array_access);
2336 | array_creation_initialized OSB_TK expression error
2338 yyerror ("']' expected");
2339 DRECOVER(array_access);
2343 postfix_expression:
2344 primary
2345 | name
2346 | post_increment_expression
2347 | post_decrement_expression
2350 post_increment_expression:
2351 postfix_expression INCR_TK
2352 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2355 post_decrement_expression:
2356 postfix_expression DECR_TK
2357 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2360 trap_overflow_corner_case:
2361 pre_increment_expression
2362 | pre_decrement_expression
2363 | PLUS_TK unary_expression
2364 {$$ = build_unaryop ($1.token, $1.location, $2); }
2365 | unary_expression_not_plus_minus
2366 | PLUS_TK error
2367 {yyerror ("Missing term"); RECOVER}
2370 unary_expression:
2371 trap_overflow_corner_case
2373 if ($1)
2374 error_if_numeric_overflow ($1);
2375 $$ = $1;
2377 | MINUS_TK trap_overflow_corner_case
2378 {$$ = build_unaryop ($1.token, $1.location, $2); }
2379 | MINUS_TK error
2380 {yyerror ("Missing term"); RECOVER}
2383 pre_increment_expression:
2384 INCR_TK unary_expression
2385 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2386 | INCR_TK error
2387 {yyerror ("Missing term"); RECOVER}
2390 pre_decrement_expression:
2391 DECR_TK unary_expression
2392 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2393 | DECR_TK error
2394 {yyerror ("Missing term"); RECOVER}
2397 unary_expression_not_plus_minus:
2398 postfix_expression
2399 | NOT_TK unary_expression
2400 {$$ = build_unaryop ($1.token, $1.location, $2); }
2401 | NEG_TK unary_expression
2402 {$$ = build_unaryop ($1.token, $1.location, $2); }
2403 | cast_expression
2404 | NOT_TK error
2405 {yyerror ("Missing term"); RECOVER}
2406 | NEG_TK error
2407 {yyerror ("Missing term"); RECOVER}
2410 cast_expression: /* Error handling here is potentially weak */
2411 OP_TK primitive_type dims CP_TK unary_expression
2413 tree type = $2;
2414 int osb = pop_current_osb (ctxp);
2415 while (osb--)
2416 type = build_java_array_type (type, -1);
2417 $$ = build_cast ($1.location, type, $5);
2419 | OP_TK primitive_type CP_TK unary_expression
2420 { $$ = build_cast ($1.location, $2, $4); }
2421 | OP_TK expression CP_TK unary_expression_not_plus_minus
2422 { $$ = build_cast ($1.location, $2, $4); }
2423 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2425 const char *ptr;
2426 int osb = pop_current_osb (ctxp);
2427 obstack_grow (&temporary_obstack,
2428 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2429 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2430 while (osb--)
2431 obstack_grow (&temporary_obstack, "[]", 2);
2432 obstack_1grow (&temporary_obstack, '\0');
2433 ptr = obstack_finish (&temporary_obstack);
2434 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2435 $$ = build_cast ($1.location, $2, $5);
2437 | OP_TK primitive_type OSB_TK error
2438 {yyerror ("']' expected, invalid type expression");}
2439 | OP_TK error
2441 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2442 RECOVER;
2444 | OP_TK primitive_type dims CP_TK error
2445 {yyerror ("Missing term"); RECOVER;}
2446 | OP_TK primitive_type CP_TK error
2447 {yyerror ("Missing term"); RECOVER;}
2448 | OP_TK name dims CP_TK error
2449 {yyerror ("Missing term"); RECOVER;}
2452 multiplicative_expression:
2453 unary_expression
2454 | multiplicative_expression MULT_TK unary_expression
2456 $$ = build_binop (BINOP_LOOKUP ($2.token),
2457 $2.location, $1, $3);
2459 | multiplicative_expression DIV_TK unary_expression
2461 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2462 $1, $3);
2464 | multiplicative_expression REM_TK unary_expression
2466 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2467 $1, $3);
2469 | multiplicative_expression MULT_TK error
2470 {yyerror ("Missing term"); RECOVER;}
2471 | multiplicative_expression DIV_TK error
2472 {yyerror ("Missing term"); RECOVER;}
2473 | multiplicative_expression REM_TK error
2474 {yyerror ("Missing term"); RECOVER;}
2477 additive_expression:
2478 multiplicative_expression
2479 | additive_expression PLUS_TK multiplicative_expression
2481 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2482 $1, $3);
2484 | additive_expression MINUS_TK multiplicative_expression
2486 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2487 $1, $3);
2489 | additive_expression PLUS_TK error
2490 {yyerror ("Missing term"); RECOVER;}
2491 | additive_expression MINUS_TK error
2492 {yyerror ("Missing term"); RECOVER;}
2495 shift_expression:
2496 additive_expression
2497 | shift_expression LS_TK additive_expression
2499 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2500 $1, $3);
2502 | shift_expression SRS_TK additive_expression
2504 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2505 $1, $3);
2507 | shift_expression ZRS_TK additive_expression
2509 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2510 $1, $3);
2512 | shift_expression LS_TK error
2513 {yyerror ("Missing term"); RECOVER;}
2514 | shift_expression SRS_TK error
2515 {yyerror ("Missing term"); RECOVER;}
2516 | shift_expression ZRS_TK error
2517 {yyerror ("Missing term"); RECOVER;}
2520 relational_expression:
2521 shift_expression
2522 | relational_expression LT_TK shift_expression
2524 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2525 $1, $3);
2527 | relational_expression GT_TK shift_expression
2529 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2530 $1, $3);
2532 | relational_expression LTE_TK shift_expression
2534 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2535 $1, $3);
2537 | relational_expression GTE_TK shift_expression
2539 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2540 $1, $3);
2542 | relational_expression INSTANCEOF_TK reference_type
2543 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2544 | relational_expression LT_TK error
2545 {yyerror ("Missing term"); RECOVER;}
2546 | relational_expression GT_TK error
2547 {yyerror ("Missing term"); RECOVER;}
2548 | relational_expression LTE_TK error
2549 {yyerror ("Missing term"); RECOVER;}
2550 | relational_expression GTE_TK error
2551 {yyerror ("Missing term"); RECOVER;}
2552 | relational_expression INSTANCEOF_TK error
2553 {yyerror ("Invalid reference type"); RECOVER;}
2556 equality_expression:
2557 relational_expression
2558 | equality_expression EQ_TK relational_expression
2560 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2561 $1, $3);
2563 | equality_expression NEQ_TK relational_expression
2565 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2566 $1, $3);
2568 | equality_expression EQ_TK error
2569 {yyerror ("Missing term"); RECOVER;}
2570 | equality_expression NEQ_TK error
2571 {yyerror ("Missing term"); RECOVER;}
2574 and_expression:
2575 equality_expression
2576 | and_expression AND_TK equality_expression
2578 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2579 $1, $3);
2581 | and_expression AND_TK error
2582 {yyerror ("Missing term"); RECOVER;}
2585 exclusive_or_expression:
2586 and_expression
2587 | exclusive_or_expression XOR_TK and_expression
2589 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2590 $1, $3);
2592 | exclusive_or_expression XOR_TK error
2593 {yyerror ("Missing term"); RECOVER;}
2596 inclusive_or_expression:
2597 exclusive_or_expression
2598 | inclusive_or_expression OR_TK exclusive_or_expression
2600 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2601 $1, $3);
2603 | inclusive_or_expression OR_TK error
2604 {yyerror ("Missing term"); RECOVER;}
2607 conditional_and_expression:
2608 inclusive_or_expression
2609 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2611 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2612 $1, $3);
2614 | conditional_and_expression BOOL_AND_TK error
2615 {yyerror ("Missing term"); RECOVER;}
2618 conditional_or_expression:
2619 conditional_and_expression
2620 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2622 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2623 $1, $3);
2625 | conditional_or_expression BOOL_OR_TK error
2626 {yyerror ("Missing term"); RECOVER;}
2629 conditional_expression: /* Error handling here is weak */
2630 conditional_or_expression
2631 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2633 $$ = build3 (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2634 SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
2636 | conditional_or_expression REL_QM_TK REL_CL_TK error
2638 YYERROR_NOW;
2639 yyerror ("Missing term");
2640 DRECOVER (1);
2642 | conditional_or_expression REL_QM_TK error
2643 {yyerror ("Missing term"); DRECOVER (2);}
2644 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2645 {yyerror ("Missing term"); DRECOVER (3);}
2648 assignment_expression:
2649 conditional_expression
2650 | assignment
2653 assignment:
2654 left_hand_side assignment_operator assignment_expression
2655 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2656 | left_hand_side assignment_operator error
2658 YYNOT_TWICE yyerror ("Missing term");
2659 DRECOVER (assign);
2663 left_hand_side:
2664 name
2665 | field_access
2666 | array_access
2669 assignment_operator:
2670 ASSIGN_ANY_TK
2671 | ASSIGN_TK
2674 expression:
2675 assignment_expression
2678 constant_expression:
2679 expression
2684 /* Helper function to retrieve an OSB count. Should be used when the
2685 `dims:' rule is being used. */
2687 static int
2688 pop_current_osb (struct parser_ctxt *ctxp)
2690 int to_return;
2692 if (ctxp->osb_depth < 0)
2693 abort ();
2695 to_return = CURRENT_OSB (ctxp);
2696 ctxp->osb_depth--;
2698 return to_return;
2703 /* This section of the code deal with save/restoring parser contexts.
2704 Add mode documentation here. FIXME */
2706 /* Helper function. Create a new parser context. With
2707 COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2708 context is copied, otherwise, the new context is zeroed. The newly
2709 created context becomes the current one. */
2711 static void
2712 create_new_parser_context (int copy_from_previous)
2714 struct parser_ctxt *new;
2716 new = ggc_alloc (sizeof (struct parser_ctxt));
2717 if (copy_from_previous)
2719 memcpy (new, ctxp, sizeof (struct parser_ctxt));
2720 /* This flag, indicating the context saves global values,
2721 should only be set by java_parser_context_save_global. */
2722 new->saved_data_ctx = 0;
2724 else
2725 memset (new, 0, sizeof (struct parser_ctxt));
2727 new->next = ctxp;
2728 ctxp = new;
2731 /* Create a new parser context and make it the current one. */
2733 void
2734 java_push_parser_context (void)
2736 create_new_parser_context (0);
2739 void
2740 java_pop_parser_context (int generate)
2742 tree current;
2743 struct parser_ctxt *next;
2745 if (!ctxp)
2746 return;
2748 next = ctxp->next;
2749 if (next)
2751 input_location = ctxp->save_location;
2752 current_class = ctxp->class_type;
2755 /* If the old and new lexers differ, then free the old one. */
2756 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2757 java_destroy_lexer (ctxp->lexer);
2759 /* Set the single import class file flag to 0 for the current list
2760 of imported things */
2761 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2762 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2764 /* If we pushed a context to parse a class intended to be generated,
2765 we keep it so we can remember the class. What we could actually
2766 do is to just update a list of class names. */
2767 if (generate)
2769 if (ctxp_for_generation_last == NULL)
2770 ctxp_for_generation = ctxp;
2771 else
2772 ctxp_for_generation_last->next = ctxp;
2773 ctxp->next = NULL;
2774 ctxp_for_generation_last = ctxp;
2777 /* And restore those of the previous context */
2778 if ((ctxp = next)) /* Assignment is really meant here */
2779 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2780 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2783 /* Create a parser context for the use of saving some global
2784 variables. */
2786 void
2787 java_parser_context_save_global (void)
2789 if (!ctxp)
2791 java_push_parser_context ();
2792 ctxp->saved_data_ctx = 1;
2795 /* If this context already stores data, create a new one suitable
2796 for data storage. */
2797 else if (ctxp->saved_data)
2799 create_new_parser_context (1);
2800 ctxp->saved_data_ctx = 1;
2803 ctxp->save_location = input_location;
2804 ctxp->class_type = current_class;
2805 ctxp->function_decl = current_function_decl;
2806 ctxp->saved_data = 1;
2809 /* Restore some global variables from the previous context. Make the
2810 previous context the current one. */
2812 void
2813 java_parser_context_restore_global (void)
2815 input_location = ctxp->save_location;
2816 current_class = ctxp->class_type;
2817 if (wfl_operator)
2818 #ifdef USE_MAPPED_LOCATION
2819 SET_EXPR_LOCATION (wfl_operator, ctxp->save_location);
2820 #else
2821 EXPR_WFL_FILENAME_NODE (wfl_operator) = get_identifier (input_filename);
2822 #endif
2823 current_function_decl = ctxp->function_decl;
2824 ctxp->saved_data = 0;
2825 if (ctxp->saved_data_ctx)
2826 java_pop_parser_context (0);
2829 /* Suspend vital data for the current class/function being parsed so
2830 that an other class can be parsed. Used to let local/anonymous
2831 classes be parsed. */
2833 static void
2834 java_parser_context_suspend (void)
2836 /* This makes debugging through java_debug_context easier */
2837 static const char *const name = "<inner buffer context>";
2839 /* Duplicate the previous context, use it to save the globals we're
2840 interested in */
2841 create_new_parser_context (1);
2842 ctxp->function_decl = current_function_decl;
2843 ctxp->class_type = current_class;
2845 /* Then create a new context which inherits all data from the
2846 previous one. This will be the new current context */
2847 create_new_parser_context (1);
2849 /* Help debugging */
2850 ctxp->next->filename = name;
2853 /* Resume vital data for the current class/function being parsed so
2854 that an other class can be parsed. Used to let local/anonymous
2855 classes be parsed. The trick is the data storing file position
2856 informations must be restored to their current value, so parsing
2857 can resume as if no context was ever saved. */
2859 static void
2860 java_parser_context_resume (void)
2862 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2863 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2864 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2866 /* We need to inherit the list of classes to complete/generate */
2867 restored->classd_list = old->classd_list;
2868 restored->class_list = old->class_list;
2870 /* Restore the current class and function from the saver */
2871 current_class = saver->class_type;
2872 current_function_decl = saver->function_decl;
2874 /* Retrieve the restored context */
2875 ctxp = restored;
2877 /* Re-installed the data for the parsing to carry on */
2878 memcpy (&ctxp->marker_begining, &old->marker_begining,
2879 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2882 /* Add a new anchor node to which all statement(s) initializing static
2883 and non static initialized upon declaration field(s) will be
2884 linked. */
2886 static void
2887 java_parser_context_push_initialized_field (void)
2889 tree node;
2891 node = build_tree_list (NULL_TREE, NULL_TREE);
2892 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2893 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2895 node = build_tree_list (NULL_TREE, NULL_TREE);
2896 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2897 CPC_INITIALIZER_LIST (ctxp) = node;
2899 node = build_tree_list (NULL_TREE, NULL_TREE);
2900 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2901 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2904 /* Pop the lists of initialized field. If this lists aren't empty,
2905 remember them so we can use it to create and populate the finit$
2906 or <clinit> functions. */
2908 static void
2909 java_parser_context_pop_initialized_field (void)
2911 tree stmts;
2912 tree class_type = TREE_TYPE (GET_CPC ());
2914 if (CPC_INITIALIZER_LIST (ctxp))
2916 stmts = CPC_INITIALIZER_STMT (ctxp);
2917 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2918 if (stmts && !java_error_count)
2919 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2922 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2924 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2925 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2926 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2927 /* Keep initialization in order to enforce 8.5 */
2928 if (stmts && !java_error_count)
2929 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2932 /* JDK 1.1 instance initializers */
2933 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2935 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2936 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2937 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2938 if (stmts && !java_error_count)
2939 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2943 static tree
2944 reorder_static_initialized (tree list)
2946 /* We have to keep things in order. The alias initializer have to
2947 come first, then the initialized regular field, in reverse to
2948 keep them in lexical order. */
2949 tree marker, previous = NULL_TREE;
2950 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2951 if (TREE_CODE (marker) == TREE_LIST
2952 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2953 break;
2955 /* No static initialized, the list is fine as is */
2956 if (!previous)
2957 list = TREE_CHAIN (marker);
2959 /* No marker? reverse the whole list */
2960 else if (!marker)
2961 list = nreverse (list);
2963 /* Otherwise, reverse what's after the marker and the new reordered
2964 sublist will replace the marker. */
2965 else
2967 TREE_CHAIN (previous) = NULL_TREE;
2968 list = nreverse (list);
2969 list = chainon (TREE_CHAIN (marker), list);
2971 return list;
2974 /* Helper functions to dump the parser context stack. */
2976 #define TAB_CONTEXT(C) \
2977 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2979 static void
2980 java_debug_context_do (int tab)
2982 struct parser_ctxt *copy = ctxp;
2983 while (copy)
2985 TAB_CONTEXT (tab);
2986 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2987 TAB_CONTEXT (tab);
2988 fprintf (stderr, "filename: %s\n", copy->filename);
2989 TAB_CONTEXT (tab);
2990 fprintf (stderr, "package: %s\n",
2991 (copy->package ?
2992 IDENTIFIER_POINTER (copy->package) : "<none>"));
2993 TAB_CONTEXT (tab);
2994 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2995 TAB_CONTEXT (tab);
2996 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2997 copy = copy->next;
2998 tab += 2;
3002 /* Dump the stacked up parser contexts. Intended to be called from a
3003 debugger. */
3005 void
3006 java_debug_context (void)
3008 java_debug_context_do (0);
3013 /* Flag for the error report routine to issue the error the first time
3014 it's called (overriding the default behavior which is to drop the
3015 first invocation and honor the second one, taking advantage of a
3016 richer context. */
3017 static int force_error = 0;
3019 /* Reporting an constructor invocation error. */
3020 static void
3021 parse_ctor_invocation_error (void)
3023 if (DECL_CONSTRUCTOR_P (current_function_decl))
3024 yyerror ("Constructor invocation must be first thing in a constructor");
3025 else
3026 yyerror ("Only constructors can invoke constructors");
3029 /* Reporting JDK1.1 features not implemented. */
3031 static tree
3032 parse_jdk1_1_error (const char *msg)
3034 sorry (": %qs JDK1.1(TM) feature", msg);
3035 java_error_count++;
3036 return build_java_empty_stmt ();
3039 static int do_warning = 0;
3041 void
3042 yyerror (const char *msgid)
3044 #ifdef USE_MAPPED_LOCATION
3045 static source_location elc;
3046 expanded_location xloc = expand_location (input_location);
3047 int current_line = xloc.line;
3048 #else
3049 static java_lc elc;
3050 int save_lineno;
3051 int current_line = input_line;
3052 #endif
3053 static int prev_lineno;
3054 static const char *prev_msg;
3056 char *remainder, *code_from_source;
3058 if (!force_error && prev_lineno == current_line)
3059 return;
3060 #ifndef USE_MAPPED_LOCATION
3061 current_line = ctxp->lexer->token_start.line;
3062 #endif
3064 /* Save current error location but report latter, when the context is
3065 richer. */
3066 if (ctxp->java_error_flag == 0)
3068 ctxp->java_error_flag = 1;
3069 #ifdef USE_MAPPED_LOCATION
3070 elc = input_location;
3071 #else
3072 elc = ctxp->lexer->token_start;
3073 #endif
3074 /* Do something to use the previous line if we're reaching the
3075 end of the file... */
3076 #ifdef VERBOSE_SKELETON
3077 printf ("* Error detected (%s)\n", (msgid ? msgid : "(null)"));
3078 #endif
3079 return;
3082 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3083 if (!force_error && msgid == prev_msg && prev_lineno == current_line)
3084 return;
3086 ctxp->java_error_flag = 0;
3087 if (do_warning)
3088 java_warning_count++;
3089 else
3090 java_error_count++;
3092 #if 0 /* FIXME */
3093 if (elc.col == 0 && msgid && msgid[1] == ';')
3094 elc = ctxp->prev_line_end;
3095 #endif
3097 prev_msg = msgid;
3099 #ifdef USE_MAPPED_LOCATION
3100 prev_lineno = current_line;
3101 code_from_source = java_get_line_col (xloc.file, current_line, xloc.column);
3102 #else
3103 save_lineno = input_line;
3104 prev_lineno = input_line = current_line;
3105 code_from_source = java_get_line_col (input_filename, current_line,
3106 ctxp->lexer->token_start.col);
3107 #endif
3110 obstack_grow0 (&temporary_obstack,
3111 code_from_source, strlen (code_from_source));
3112 remainder = obstack_finish (&temporary_obstack);
3113 if (do_warning)
3114 warning (0, "%s.\n%s", msgid, remainder);
3115 else
3116 error ("%s.\n%s", msgid, remainder);
3118 /* This allow us to cheaply avoid an extra 'Invalid expression
3119 statement' error report when errors have been already reported on
3120 the same line. This occurs when we report an error but don't have
3121 a synchronization point other than ';', which
3122 expression_statement is the only one to take care of. */
3123 #ifndef USE_MAPPED_LOCATION
3124 input_line = save_lineno;
3125 #endif
3126 ctxp->prevent_ese = input_line;
3129 static void
3130 issue_warning_error_from_context (
3131 #ifdef USE_MAPPED_LOCATION
3132 source_location cl,
3133 #else
3134 tree cl,
3135 #endif
3136 const char *msgid, va_list *ap)
3138 #ifdef USE_MAPPED_LOCATION
3139 source_location saved_location = input_location;
3140 expanded_location xloc = expand_location (cl);
3141 #else
3142 java_lc save_lc = ctxp->lexer->token_start;
3143 const char *saved = ctxp->filename, *saved_input_filename;
3144 #endif
3145 char buffer [4096];
3146 text_info text;
3148 text.err_no = errno;
3149 text.args_ptr = ap;
3150 text.format_spec = msgid;
3151 pp_format_text (global_dc->printer, &text);
3152 strncpy (buffer, pp_formatted_text (global_dc->printer), sizeof (buffer) - 1);
3153 buffer[sizeof (buffer) - 1] = '\0';
3154 pp_clear_output_area (global_dc->printer);
3156 force_error = 1;
3158 #ifdef USE_MAPPED_LOCATION
3159 if (xloc.file != NULL)
3161 ctxp->filename = xloc.file;
3162 input_location = cl;
3164 #else
3165 ctxp->lexer->token_start.line = EXPR_WFL_LINENO (cl);
3166 ctxp->lexer->token_start.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1
3167 : EXPR_WFL_COLNO (cl) == 0xffe ? -2
3168 : EXPR_WFL_COLNO (cl));
3170 /* We have a CL, that's a good reason for using it if it contains data */
3171 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3172 ctxp->filename = EXPR_WFL_FILENAME (cl);
3173 saved_input_filename = input_filename;
3174 input_filename = ctxp->filename;
3175 #endif
3176 java_error (NULL);
3177 java_error (buffer);
3178 #ifdef USE_MAPPED_LOCATION
3179 input_location = saved_location;
3180 #else
3181 ctxp->filename = saved;
3182 input_filename = saved_input_filename;
3183 ctxp->lexer->token_start = save_lc;
3184 #endif
3185 force_error = 0;
3188 /* Issue an error message at a current source line CL.
3189 FUTURE/FIXME: change cl to be a source_location. */
3191 void
3192 parse_error_context (tree cl, const char *msgid, ...)
3194 va_list ap;
3195 va_start (ap, msgid);
3196 #ifdef USE_MAPPED_LOCATION
3197 issue_warning_error_from_context (EXPR_LOCATION (cl), msgid, &ap);
3198 #else
3199 issue_warning_error_from_context (cl, msgid, &ap);
3200 #endif
3201 va_end (ap);
3204 /* Issue a warning at a current source line CL.
3205 FUTURE/FIXME: change cl to be a source_location. */
3207 static void
3208 parse_warning_context (tree cl, const char *msgid, ...)
3210 va_list ap;
3211 va_start (ap, msgid);
3213 do_warning = 1;
3214 #ifdef USE_MAPPED_LOCATION
3215 issue_warning_error_from_context (EXPR_LOCATION (cl), msgid, &ap);
3216 #else
3217 issue_warning_error_from_context (cl, msgid, &ap);
3218 #endif
3219 do_warning = 0;
3220 va_end (ap);
3223 static tree
3224 find_expr_with_wfl (tree node)
3226 while (node)
3228 enum tree_code_class code;
3229 tree to_return;
3231 switch (TREE_CODE (node))
3233 case BLOCK:
3234 node = BLOCK_EXPR_BODY (node);
3235 continue;
3237 case COMPOUND_EXPR:
3238 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3239 if (to_return)
3240 return to_return;
3241 node = TREE_OPERAND (node, 1);
3242 continue;
3244 case LOOP_EXPR:
3245 node = TREE_OPERAND (node, 0);
3246 continue;
3248 case LABELED_BLOCK_EXPR:
3249 node = LABELED_BLOCK_BODY (node);
3250 continue;
3252 default:
3253 code = TREE_CODE_CLASS (TREE_CODE (node));
3254 if (((code == tcc_unary) || (code == tcc_binary)
3255 || (code == tcc_expression))
3256 && EXPR_WFL_LINECOL (node))
3257 return node;
3258 return NULL_TREE;
3261 return NULL_TREE;
3264 /* Issue a missing return statement error. Uses METHOD to figure the
3265 last line of the method the error occurs in. */
3267 static void
3268 missing_return_error (tree method)
3270 #ifdef USE_MAPPED_LOCATION
3271 SET_EXPR_LOCATION (wfl_operator, DECL_FUNCTION_LAST_LINE (method));
3272 #else
3273 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_FUNCTION_LAST_LINE (method), -2);
3274 #endif
3275 parse_error_context (wfl_operator, "Missing return statement");
3278 /* Issue an unreachable statement error. From NODE, find the next
3279 statement to report appropriately. */
3280 static void
3281 unreachable_stmt_error (tree node)
3283 /* Browse node to find the next expression node that has a WFL. Use
3284 the location to report the error */
3285 if (TREE_CODE (node) == COMPOUND_EXPR)
3286 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3287 else
3288 node = find_expr_with_wfl (node);
3290 if (node)
3292 #ifdef USE_MAPPED_LOCATION
3293 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
3294 #else
3295 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3296 #endif
3297 parse_error_context (wfl_operator, "Unreachable statement");
3299 else
3300 abort ();
3303 static int
3304 not_accessible_field_error (tree wfl, tree decl)
3306 parse_error_context
3307 (wfl, "Can't access %s field %<%s.%s%> from %qs",
3308 accessibility_string (get_access_flags_from_decl (decl)),
3309 GET_TYPE_NAME (DECL_CONTEXT (decl)),
3310 IDENTIFIER_POINTER (DECL_NAME (decl)),
3311 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
3312 return 1;
3316 java_report_errors (void)
3318 if (java_error_count)
3319 fprintf (stderr, "%d error%s",
3320 java_error_count, (java_error_count == 1 ? "" : "s"));
3321 if (java_warning_count)
3322 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3323 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3324 if (java_error_count || java_warning_count)
3325 putc ('\n', stderr);
3326 return java_error_count;
3329 static char *
3330 java_accstring_lookup (int flags)
3332 static char buffer [80];
3333 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3335 /* Access modifier looked-up first for easier report on forbidden
3336 access. */
3337 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3338 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3339 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3340 if (flags & ACC_STATIC) COPY_RETURN ("static");
3341 if (flags & ACC_FINAL) COPY_RETURN ("final");
3342 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3343 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3344 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3345 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3346 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3347 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3349 buffer [0] = '\0';
3350 return buffer;
3351 #undef COPY_RETURN
3354 /* Returns a string denoting the accessibility of a class or a member as
3355 indicated by FLAGS. We need a separate function from
3356 java_accstring_lookup, as the latter can return spurious "static", etc.
3357 if package-private access is defined (in which case none of the
3358 relevant access control bits in FLAGS is set). */
3360 static const char *
3361 accessibility_string (int flags)
3363 if (flags & ACC_PRIVATE) return "private";
3364 if (flags & ACC_PROTECTED) return "protected";
3365 if (flags & ACC_PUBLIC) return "public";
3367 return "package-private";
3370 /* Issuing error messages upon redefinition of classes, interfaces or
3371 variables. */
3373 static void
3374 classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3376 parse_error_context (cl, "%s %qs already defined in %s:%d",
3377 context, IDENTIFIER_POINTER (id),
3378 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3379 /* Here we should point out where its redefined. It's a unicode. FIXME */
3382 static void
3383 variable_redefinition_error (tree context, tree name, tree type, int line)
3385 const char *type_name;
3387 /* Figure a proper name for type. We might haven't resolved it */
3388 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3389 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3390 else
3391 type_name = lang_printable_name (type, 0);
3393 parse_error_context (context,
3394 "Variable %qs is already defined in this method and was declared %<%s %s%> at line %d",
3395 IDENTIFIER_POINTER (name),
3396 type_name, IDENTIFIER_POINTER (name), line);
3399 /* If ANAME is terminated with `[]', it indicates an array. This
3400 function returns the number of `[]' found and if this number is
3401 greater than zero, it extracts the array type name and places it in
3402 the node pointed to by TRIMMED unless TRIMMED is null. */
3404 static int
3405 build_type_name_from_array_name (tree aname, tree *trimmed)
3407 const char *name = IDENTIFIER_POINTER (aname);
3408 int len = IDENTIFIER_LENGTH (aname);
3409 int array_dims;
3411 STRING_STRIP_BRACKETS (name, len, array_dims);
3413 if (array_dims && trimmed)
3414 *trimmed = get_identifier_with_length (name, len);
3416 return array_dims;
3419 static tree
3420 build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3422 int more_dims = 0;
3424 /* Eventually get more dims */
3425 more_dims = build_type_name_from_array_name (name, &name);
3427 /* If we have, then craft a new type for this variable */
3428 if (more_dims)
3430 tree save = type;
3432 /* If we have a pointer, use its type */
3433 if (TREE_CODE (type) == POINTER_TYPE)
3434 type = TREE_TYPE (type);
3436 /* Building the first dimension of a primitive type uses this
3437 function */
3438 if (JPRIMITIVE_TYPE_P (type))
3440 type = build_java_array_type (type, -1);
3441 more_dims--;
3443 /* Otherwise, if we have a WFL for this type, use it (the type
3444 is already an array on an unresolved type, and we just keep
3445 on adding dimensions) */
3446 else if (type_wfl)
3448 type = type_wfl;
3449 more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3450 NULL);
3453 /* Add all the dimensions */
3454 while (more_dims--)
3455 type = build_unresolved_array_type (type);
3457 /* The type may have been incomplete in the first place */
3458 if (type_wfl)
3459 type = obtain_incomplete_type (type);
3462 if (ret_name)
3463 *ret_name = name;
3464 return type;
3467 /* Build something that the type identifier resolver will identify as
3468 being an array to an unresolved type. TYPE_WFL is a WFL on a
3469 identifier. */
3471 static tree
3472 build_unresolved_array_type (tree type_or_wfl)
3474 const char *ptr;
3475 tree wfl;
3477 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3478 just create a array type */
3479 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3480 return build_java_array_type (type_or_wfl, -1);
3482 obstack_grow (&temporary_obstack,
3483 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3484 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3485 obstack_grow0 (&temporary_obstack, "[]", 2);
3486 ptr = obstack_finish (&temporary_obstack);
3487 #ifdef USE_MAPPED_LOCATION
3488 wfl = build_expr_wfl (get_identifier (ptr), EXPR_LOCATION (type_or_wfl));
3489 #else
3490 wfl = build_expr_wfl (get_identifier (ptr),
3491 EXPR_WFL_FILENAME (type_or_wfl),
3492 EXPR_WFL_LINENO (type_or_wfl),
3493 EXPR_WFL_COLNO (type_or_wfl));
3494 #endif
3495 /* Re-install the existing qualifications so that the type can be
3496 resolved properly. */
3497 EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3498 return wfl;
3501 static void
3502 parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3504 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3505 parse_error_context (wfl, "Interface %qs repeated",
3506 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3509 /* Bulk of common class/interface checks. Return 1 if an error was
3510 encountered. TAG is 0 for a class, 1 for an interface. */
3512 static int
3513 check_class_interface_creation (int is_interface, int flags, tree raw_name,
3514 tree qualified_name, tree decl, tree cl)
3516 tree node;
3517 int sca = 0; /* Static class allowed */
3518 int icaf = 0; /* Inner class allowed flags */
3519 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3521 if (!quiet_flag)
3522 fprintf (stderr, " %s%s %s",
3523 (CPC_INNER_P () ? "inner" : ""),
3524 (is_interface ? "interface" : "class"),
3525 IDENTIFIER_POINTER (qualified_name));
3527 /* Scope of an interface/class type name:
3528 - Can't be imported by a single type import
3529 - Can't already exists in the package */
3530 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3531 && (node = find_name_in_single_imports (raw_name))
3532 && !CPC_INNER_P ())
3534 parse_error_context
3535 (cl, "%s name %qs clashes with imported type %qs",
3536 (is_interface ? "Interface" : "Class"),
3537 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3538 return 1;
3540 if (decl && CLASS_COMPLETE_P (decl))
3542 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3543 qualified_name, decl, cl);
3544 return 1;
3547 if (check_inner_class_redefinition (raw_name, cl))
3548 return 1;
3550 /* If public, file name should match class/interface name, except
3551 when dealing with an inner class */
3552 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3554 const char *fname = input_filename;
3555 const char *f;
3557 for (f = fname + strlen (fname);
3558 f != fname && ! IS_DIR_SEPARATOR (*f);
3559 f--)
3561 if (IS_DIR_SEPARATOR (*f))
3562 f++;
3563 if (strncmp (IDENTIFIER_POINTER (raw_name),
3564 f , IDENTIFIER_LENGTH (raw_name)) ||
3565 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3566 parse_error_context
3567 (cl, "Public %s %qs must be defined in a file called %<%s.java%>",
3568 (is_interface ? "interface" : "class"),
3569 IDENTIFIER_POINTER (qualified_name),
3570 IDENTIFIER_POINTER (raw_name));
3573 /* Static classes can be declared only in top level classes. Note:
3574 once static, a inner class is a top level class. */
3575 if (flags & ACC_STATIC)
3577 /* Catch the specific error of declaring an class inner class
3578 with no toplevel enclosing class. Prevent check_modifiers from
3579 complaining a second time */
3580 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3582 parse_error_context (cl, "Inner class %qs can't be static. Static classes can only occur in interfaces and top-level classes",
3583 IDENTIFIER_POINTER (qualified_name));
3584 sca = ACC_STATIC;
3586 /* Else, in the context of a top-level class declaration, let
3587 `check_modifiers' do its job, otherwise, give it a go */
3588 else
3589 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3592 /* Inner classes can be declared private or protected
3593 within their enclosing classes. */
3594 if (CPC_INNER_P ())
3596 /* A class which is local to a block can't be public, private,
3597 protected or static. But it is created final, so allow this
3598 one. */
3599 if (current_function_decl)
3600 icaf = sca = uaaf = ACC_FINAL;
3601 else
3603 check_modifiers_consistency (flags);
3604 icaf = ACC_PROTECTED;
3605 if (! CLASS_INTERFACE (GET_CPC ()))
3606 icaf |= ACC_PRIVATE;
3610 if (is_interface)
3612 if (CPC_INNER_P ())
3613 uaaf = INTERFACE_INNER_MODIFIERS;
3614 else
3615 uaaf = INTERFACE_MODIFIERS;
3617 check_modifiers ("Illegal modifier %qs for interface declaration",
3618 flags, uaaf);
3620 else
3621 check_modifiers ((current_function_decl ?
3622 "Illegal modifier %qs for local class declaration" :
3623 "Illegal modifier %qs for class declaration"),
3624 flags, uaaf|sca|icaf);
3625 return 0;
3628 /* Construct a nested class name. If the final component starts with
3629 a digit, return true. Otherwise return false. */
3630 static int
3631 make_nested_class_name (tree cpc_list)
3633 tree name;
3635 if (!cpc_list)
3636 return 0;
3638 make_nested_class_name (TREE_CHAIN (cpc_list));
3640 /* Pick the qualified name when dealing with the first upmost
3641 enclosing class */
3642 name = (TREE_CHAIN (cpc_list)
3643 ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3644 obstack_grow (&temporary_obstack,
3645 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3646 obstack_1grow (&temporary_obstack, '$');
3648 return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3651 /* Can't redefine a class already defined in an earlier scope. */
3653 static int
3654 check_inner_class_redefinition (tree raw_name, tree cl)
3656 tree scope_list;
3658 for (scope_list = GET_CPC_LIST (); scope_list;
3659 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3660 if (raw_name == GET_CPC_UN_NODE (scope_list))
3662 parse_error_context
3663 (cl, "The class name %qs is already defined in this scope. An inner class may not have the same simple name as any of its enclosing classes",
3664 IDENTIFIER_POINTER (raw_name));
3665 return 1;
3667 return 0;
3670 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3671 we remember ENCLOSING and SUPER. */
3673 static tree
3674 resolve_inner_class (htab_t circularity_hash, tree cl, tree *enclosing,
3675 tree *super, tree class_type)
3677 tree local_enclosing = *enclosing;
3678 tree local_super = NULL_TREE;
3680 while (local_enclosing)
3682 tree intermediate, decl;
3684 *htab_find_slot (circularity_hash, local_enclosing, INSERT) =
3685 local_enclosing;
3687 if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3688 return decl;
3690 intermediate = local_enclosing;
3691 /* Explore enclosing contexts. */
3692 while (INNER_CLASS_DECL_P (intermediate))
3694 intermediate = DECL_CONTEXT (intermediate);
3695 if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3696 return decl;
3699 /* Now go to the upper classes, bail out if necessary. We will
3700 analyze the returned SUPER and act accordingly (see
3701 do_resolve_class). */
3702 if (JPRIMITIVE_TYPE_P (TREE_TYPE (local_enclosing))
3703 || TREE_TYPE (local_enclosing) == void_type_node)
3705 parse_error_context (cl, "Qualifier must be a reference");
3706 local_enclosing = NULL_TREE;
3707 break;
3709 local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3710 if (!local_super || local_super == object_type_node)
3711 break;
3713 if (TREE_CODE (local_super) == POINTER_TYPE)
3714 local_super = do_resolve_class (NULL, NULL, local_super, NULL, NULL);
3715 else
3716 local_super = TYPE_NAME (local_super);
3718 /* We may not have checked for circular inheritance yet, so do so
3719 here to prevent an infinite loop. */
3720 if (htab_find (circularity_hash, local_super) != NULL)
3722 if (!cl)
3723 cl = lookup_cl (local_enclosing);
3725 parse_error_context
3726 (cl, "Cyclic inheritance involving %s",
3727 IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3728 local_enclosing = NULL_TREE;
3730 else
3731 local_enclosing = local_super;
3734 /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3735 *super = local_super;
3736 *enclosing = local_enclosing;
3738 return NULL_TREE;
3741 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3742 qualified. */
3744 static tree
3745 find_as_inner_class (tree enclosing, tree name, tree cl)
3747 tree qual, to_return;
3748 if (!enclosing)
3749 return NULL_TREE;
3751 name = TYPE_NAME (name);
3753 /* First search: within the scope of `enclosing', search for name */
3754 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3755 qual = EXPR_WFL_QUALIFICATION (cl);
3756 else if (cl)
3757 qual = build_tree_list (cl, NULL_TREE);
3758 else
3759 qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3761 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3762 return to_return;
3764 /* We're dealing with a qualified name. Try to resolve thing until
3765 we get something that is an enclosing class. */
3766 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3768 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3770 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3771 qual = TREE_CHAIN (qual))
3773 acc = merge_qualified_name (acc,
3774 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3775 BUILD_PTR_FROM_NAME (ptr, acc);
3776 decl = do_resolve_class (NULL_TREE, NULL_TREE, ptr, NULL_TREE, cl);
3779 /* A NULL qual and a decl means that the search ended
3780 successfully?!? We have to do something then. FIXME */
3782 if (decl)
3783 enclosing = decl;
3784 else
3785 qual = EXPR_WFL_QUALIFICATION (cl);
3787 /* Otherwise, create a qual for the other part of the resolution. */
3788 else
3789 qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3791 return find_as_inner_class_do (qual, enclosing);
3794 /* We go inside the list of sub classes and try to find a way
3795 through. */
3797 static tree
3798 find_as_inner_class_do (tree qual, tree enclosing)
3800 if (!qual)
3801 return NULL_TREE;
3803 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3805 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3806 tree next_enclosing = NULL_TREE;
3807 tree inner_list;
3809 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3810 inner_list; inner_list = TREE_CHAIN (inner_list))
3812 if (TREE_VALUE (inner_list) == name_to_match)
3814 next_enclosing = TREE_PURPOSE (inner_list);
3815 break;
3818 enclosing = next_enclosing;
3821 return (!qual && enclosing ? enclosing : NULL_TREE);
3824 static void
3825 link_nested_class_to_enclosing (void)
3827 if (GET_ENCLOSING_CPC ())
3829 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3830 DECL_INNER_CLASS_LIST (enclosing) =
3831 tree_cons (GET_CPC (), GET_CPC_UN (),
3832 DECL_INNER_CLASS_LIST (enclosing));
3836 static tree
3837 maybe_make_nested_class_name (tree name)
3839 tree id = NULL_TREE;
3841 if (CPC_INNER_P ())
3843 /* If we're in a function, we must append a number to create the
3844 nested class name. However, we don't do this if the class we
3845 are constructing is anonymous, because in that case we'll
3846 already have a number as the class name. */
3847 if (! make_nested_class_name (GET_CPC_LIST ())
3848 && current_function_decl != NULL_TREE
3849 && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3851 char buf[10];
3852 sprintf (buf, "%d", anonymous_class_counter);
3853 ++anonymous_class_counter;
3854 obstack_grow (&temporary_obstack, buf, strlen (buf));
3855 obstack_1grow (&temporary_obstack, '$');
3857 obstack_grow0 (&temporary_obstack,
3858 IDENTIFIER_POINTER (name),
3859 IDENTIFIER_LENGTH (name));
3860 id = get_identifier (obstack_finish (&temporary_obstack));
3861 if (ctxp->package)
3862 QUALIFIED_P (id) = 1;
3864 return id;
3867 /* If DECL is NULL, create and push a new DECL, record the current
3868 line CL and do other maintenance things. */
3870 static tree
3871 maybe_create_class_interface_decl (tree decl, tree raw_name,
3872 tree qualified_name, tree cl)
3874 if (!decl)
3875 decl = push_class (make_class (), qualified_name);
3877 /* Take care of the file and line business */
3878 #ifdef USE_MAPPED_LOCATION
3879 DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (cl);
3880 #else
3881 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3882 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3883 #endif
3884 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3885 CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3886 #ifdef USE_MAPPED_LOCATION
3888 tree tmp = maybe_get_identifier (EXPR_FILENAME (cl));
3889 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3890 tmp && IS_A_COMMAND_LINE_FILENAME_P (tmp);
3892 #else
3893 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3894 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3895 #endif
3897 PUSH_CPC (decl, raw_name);
3898 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3900 /* Link the declaration to the already seen ones */
3901 TREE_CHAIN (decl) = ctxp->class_list;
3902 ctxp->class_list = decl;
3904 /* Create a new nodes in the global lists */
3905 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3906 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3908 /* Install a new dependency list element */
3909 create_jdep_list (ctxp);
3911 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3912 IDENTIFIER_POINTER (qualified_name)));
3913 return decl;
3916 static void
3917 add_superinterfaces (tree decl, tree interface_list)
3919 tree node;
3920 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3921 takes care of ensuring that:
3922 - This is an accessible interface type,
3923 - Circularity detection.
3924 parser_add_interface is then called. If present but not defined,
3925 the check operation is delayed until the super interface gets
3926 defined. */
3927 for (node = interface_list; node; node = TREE_CHAIN (node))
3929 tree current = TREE_PURPOSE (node);
3930 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3931 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3933 if (!parser_check_super_interface (idecl, decl, current))
3934 parser_add_interface (decl, idecl, current);
3936 else
3937 register_incomplete_type (JDEP_INTERFACE,
3938 current, decl, NULL_TREE);
3942 /* Create an interface in pass1 and return its decl. Return the
3943 interface's decl in pass 2. */
3945 static tree
3946 create_interface (int flags, tree id, tree super)
3948 tree raw_name = EXPR_WFL_NODE (id);
3949 tree q_name = parser_qualified_classname (raw_name);
3950 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3952 /* Certain syntax errors are making SUPER be like ID. Avoid this
3953 case. */
3954 if (ctxp->class_err && id == super)
3955 super = NULL;
3957 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3959 /* Basic checks: scope, redefinition, modifiers */
3960 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3962 PUSH_ERROR ();
3963 return NULL_TREE;
3966 /* Suspend the current parsing context if we're parsing an inner
3967 interface */
3968 if (CPC_INNER_P ())
3970 java_parser_context_suspend ();
3971 /* Interface members are public. */
3972 if (CLASS_INTERFACE (GET_CPC ()))
3973 flags |= ACC_PUBLIC;
3976 /* Push a new context for (static) initialized upon declaration fields */
3977 java_parser_context_push_initialized_field ();
3979 /* Interface modifiers check
3980 - public/abstract allowed (already done at that point)
3981 - abstract is obsolete (comes first, it's a warning, or should be)
3982 - Can't use twice the same (checked in the modifier rule) */
3983 if ((flags & ACC_ABSTRACT) && flag_redundant)
3984 parse_warning_context
3985 (MODIFIER_WFL (ABSTRACT_TK),
3986 "Redundant use of %<abstract%> modifier. Interface %qs is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3988 /* Create a new decl if DECL is NULL, otherwise fix it */
3989 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3991 /* Interfaces are always abstract. */
3992 flags |= ACC_ABSTRACT;
3994 /* Inner interfaces are always static. */
3995 if (INNER_CLASS_DECL_P (decl))
3996 flags |= ACC_STATIC;
3998 /* Set super info and mark the class a complete */
3999 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
4000 object_type_node, ctxp->interface_number);
4001 ctxp->interface_number = 0;
4002 CLASS_COMPLETE_P (decl) = 1;
4003 add_superinterfaces (decl, super);
4005 /* Eventually sets the @deprecated tag flag */
4006 CHECK_DEPRECATED (decl);
4008 return decl;
4011 /* Patch anonymous class CLASS, by either extending or implementing
4012 DEP. */
4014 static void
4015 patch_anonymous_class (tree type_decl, tree class_decl, tree wfl)
4017 tree class = TREE_TYPE (class_decl);
4018 tree type = TREE_TYPE (type_decl);
4019 tree binfo = TYPE_BINFO (class);
4021 /* If it's an interface, implement it */
4022 if (CLASS_INTERFACE (type_decl))
4024 if (parser_check_super_interface (type_decl, class_decl, wfl))
4025 return;
4027 if (!VEC_space (tree, BINFO_BASE_BINFOS (binfo), 1))
4029 /* Extend the binfo - by reallocating and copying it. */
4030 tree new_binfo;
4031 tree base_binfo;
4032 int i;
4034 new_binfo = make_tree_binfo ((BINFO_N_BASE_BINFOS (binfo) + 1) * 2);
4035 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4036 BINFO_BASE_APPEND (new_binfo, base_binfo);
4037 CLASS_HAS_SUPER_FLAG (new_binfo) = CLASS_HAS_SUPER_FLAG (binfo);
4038 BINFO_VTABLE (new_binfo) = BINFO_VTABLE (binfo);
4039 TYPE_BINFO (class) = new_binfo;
4042 /* And add the interface */
4043 parser_add_interface (class_decl, type_decl, wfl);
4045 /* Otherwise, it's a type we want to extend */
4046 else
4048 if (parser_check_super (type_decl, class_decl, wfl))
4049 return;
4050 BINFO_TYPE (BINFO_BASE_BINFO (binfo, 0)) = type;
4054 /* Create an anonymous class which extends/implements TYPE_NAME, and return
4055 its decl. */
4057 static tree
4058 create_anonymous_class (tree type_name)
4060 char buffer [80];
4061 tree super = NULL_TREE, itf = NULL_TREE;
4062 tree id, type_decl, class;
4064 /* The unqualified name of the anonymous class. It's just a number. */
4065 sprintf (buffer, "%d", anonymous_class_counter++);
4066 id = build_wfl_node (get_identifier (buffer));
4067 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL (type_name);
4069 /* We know about the type to extend/implement. We go ahead */
4070 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
4072 /* Create a class which either implements on extends the designated
4073 class. The class bears an inaccessible name. */
4074 if (CLASS_INTERFACE (type_decl))
4076 /* It's OK to modify it here. It's been already used and
4077 shouldn't be reused */
4078 ctxp->interface_number = 1;
4079 /* Interfaces should presented as a list of WFLs */
4080 itf = build_tree_list (type_name, NULL_TREE);
4082 else
4083 super = type_name;
4086 class = create_class (ACC_FINAL, id, super, itf);
4088 /* We didn't know anything about the stuff. We register a dependence. */
4089 if (!type_decl)
4090 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
4092 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
4093 return class;
4096 /* Create a class in pass1 and return its decl. Return class
4097 interface's decl in pass 2. */
4099 static tree
4100 create_class (int flags, tree id, tree super, tree interfaces)
4102 tree raw_name = EXPR_WFL_NODE (id);
4103 tree class_id, decl;
4104 tree super_decl_type;
4106 /* Certain syntax errors are making SUPER be like ID. Avoid this
4107 case. */
4108 if (ctxp->class_err && id == super)
4109 super = NULL;
4111 class_id = parser_qualified_classname (raw_name);
4112 decl = IDENTIFIER_CLASS_VALUE (class_id);
4113 EXPR_WFL_NODE (id) = class_id;
4115 /* Basic check: scope, redefinition, modifiers */
4116 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
4118 PUSH_ERROR ();
4119 return NULL_TREE;
4122 /* Suspend the current parsing context if we're parsing an inner
4123 class or an anonymous class. */
4124 if (CPC_INNER_P ())
4126 java_parser_context_suspend ();
4127 /* Interface members are public. */
4128 if (CLASS_INTERFACE (GET_CPC ()))
4129 flags |= ACC_PUBLIC;
4132 /* Push a new context for (static) initialized upon declaration fields */
4133 java_parser_context_push_initialized_field ();
4135 /* Class modifier check:
4136 - Allowed modifier (already done at that point)
4137 - abstract AND final forbidden
4138 - Public classes defined in the correct file */
4139 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4140 parse_error_context
4141 (id, "Class %qs can't be declared both abstract and final",
4142 IDENTIFIER_POINTER (raw_name));
4144 /* Create a new decl if DECL is NULL, otherwise fix it */
4145 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4147 /* If SUPER exists, use it, otherwise use Object */
4148 if (super)
4150 /* java.lang.Object can't extend anything. */
4151 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4153 parse_error_context (id, "%<java.lang.Object%> can't extend anything");
4154 return NULL_TREE;
4157 super_decl_type =
4158 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4160 else if (TREE_TYPE (decl) != object_type_node)
4161 super_decl_type = object_type_node;
4162 /* We're defining java.lang.Object */
4163 else
4164 super_decl_type = NULL_TREE;
4166 /* A class nested in an interface is implicitly static. */
4167 if (INNER_CLASS_DECL_P (decl)
4168 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4170 flags |= ACC_STATIC;
4173 /* Set super info and mark the class as complete. */
4174 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4175 ctxp->interface_number);
4176 ctxp->interface_number = 0;
4177 CLASS_COMPLETE_P (decl) = 1;
4178 add_superinterfaces (decl, interfaces);
4180 /* TYPE_VFIELD' is a compiler-generated field used to point to
4181 virtual function tables. In gcj, every class has a common base
4182 virtual function table in java.lang.object. */
4183 TYPE_VFIELD (TREE_TYPE (decl)) = TYPE_VFIELD (object_type_node);
4185 /* We keep the compilation unit imports in the class so that
4186 they can be used later to resolve type dependencies that
4187 aren't necessary to solve now. */
4188 TYPE_IMPORT_LIST (TREE_TYPE (decl)) = ctxp->import_list;
4189 TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (decl)) = ctxp->import_demand_list;
4191 /* Add the private this$<n> field, Replicate final locals still in
4192 scope as private final fields mangled like val$<local_name>.
4193 This does not occur for top level (static) inner classes. */
4194 if (PURE_INNER_CLASS_DECL_P (decl))
4195 add_inner_class_fields (decl, current_function_decl);
4197 /* Eventually sets the @deprecated tag flag */
4198 CHECK_DEPRECATED (decl);
4200 /* Reset the anonymous class counter when declaring non inner classes */
4201 if (!INNER_CLASS_DECL_P (decl))
4202 anonymous_class_counter = 1;
4204 return decl;
4207 /* End a class declaration: register the statements used to create
4208 finit$ and <clinit>, pop the current class and resume the prior
4209 parser context if necessary. */
4211 static void
4212 end_class_declaration (int resume)
4214 /* If an error occurred, context weren't pushed and won't need to be
4215 popped by a resume. */
4216 int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4218 if (GET_CPC () != error_mark_node)
4219 dump_java_tree (TDI_class, GET_CPC ());
4221 java_parser_context_pop_initialized_field ();
4222 POP_CPC ();
4223 if (resume && no_error_occurred)
4224 java_parser_context_resume ();
4226 /* We're ending a class declaration, this is a good time to reset
4227 the interface cout. Note that might have been already done in
4228 create_interface, but if at that time an inner class was being
4229 dealt with, the interface count was reset in a context created
4230 for the sake of handling inner classes declaration. */
4231 ctxp->interface_number = 0;
4234 static void
4235 add_inner_class_fields (tree class_decl, tree fct_decl)
4237 tree block, marker, f;
4239 f = add_field (TREE_TYPE (class_decl),
4240 build_current_thisn (TREE_TYPE (class_decl)),
4241 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4242 ACC_PRIVATE);
4243 FIELD_THISN (f) = 1;
4245 if (!fct_decl)
4246 return;
4248 for (block = GET_CURRENT_BLOCK (fct_decl);
4249 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4251 tree decl;
4252 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4254 tree name, pname;
4255 tree wfl, init, list;
4257 /* Avoid non final arguments. */
4258 if (!LOCAL_FINAL_P (decl))
4259 continue;
4261 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4262 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4263 wfl = build_wfl_node (name);
4264 init = build_wfl_node (pname);
4265 /* Build an initialization for the field: it will be
4266 initialized by a parameter added to finit$, bearing a
4267 mangled name of the field itself (param$<n>.) The
4268 parameter is provided to finit$ by the constructor
4269 invoking it (hence the constructor will also feature a
4270 hidden parameter, set to the value of the outer context
4271 local at the time the inner class is created.)
4273 Note: we take into account all possible locals that can
4274 be accessed by the inner class. It's actually not trivial
4275 to minimize these aliases down to the ones really
4276 used. One way to do that would be to expand all regular
4277 methods first, then finit$ to get a picture of what's
4278 used. It works with the exception that we would have to
4279 go back on all constructor invoked in regular methods to
4280 have their invocation reworked (to include the right amount
4281 of alias initializer parameters.)
4283 The only real way around, I think, is a first pass to
4284 identify locals really used in the inner class. We leave
4285 the flag FIELD_LOCAL_ALIAS_USED around for that future
4286 use.
4288 On the other hand, it only affect local inner classes,
4289 whose constructors (and finit$ call) will be featuring
4290 unnecessary arguments. It's easy for a developer to keep
4291 this number of parameter down by using the `final'
4292 keyword only when necessary. For the time being, we can
4293 issue a warning on unnecessary finals. FIXME */
4294 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4295 wfl, init);
4297 /* Register the field. The TREE_LIST holding the part
4298 initialized/initializer will be marked ARG_FINAL_P so
4299 that the created field can be marked
4300 FIELD_LOCAL_ALIAS. */
4301 list = build_tree_list (wfl, init);
4302 ARG_FINAL_P (list) = 1;
4303 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4307 if (!CPC_INITIALIZER_STMT (ctxp))
4308 return;
4310 /* If we ever registered an alias field, insert and marker to
4311 remember where the list ends. The second part of the list (the one
4312 featuring initialized fields) so it can be later reversed to
4313 enforce 8.5. The marker will be removed during that operation. */
4314 marker = build_tree_list (NULL_TREE, NULL_TREE);
4315 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4316 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4319 /* Can't use lookup_field () since we don't want to load the class and
4320 can't set the CLASS_LOADED_P flag */
4322 static tree
4323 find_field (tree class, tree name)
4325 tree decl;
4326 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4328 if (DECL_NAME (decl) == name)
4329 return decl;
4331 return NULL_TREE;
4334 /* Wrap around lookup_field that doesn't potentially upset the value
4335 of CLASS */
4337 static tree
4338 lookup_field_wrapper (tree class, tree name)
4340 tree type = class;
4341 tree decl = NULL_TREE;
4342 java_parser_context_save_global ();
4344 /* Last chance: if we're within the context of an inner class, we
4345 might be trying to access a local variable defined in an outer
4346 context. We try to look for it now. */
4347 if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4349 tree new_name;
4350 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4351 decl = lookup_field (&type, new_name);
4352 if (decl && decl != error_mark_node)
4353 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4355 if (!decl || decl == error_mark_node)
4357 type = class;
4358 decl = lookup_field (&type, name);
4361 /* If the field still hasn't been found, try the next enclosing context. */
4362 if (!decl && INNER_CLASS_TYPE_P (class))
4364 tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4365 decl = lookup_field_wrapper (outer_type, name);
4368 java_parser_context_restore_global ();
4369 return decl == error_mark_node ? NULL : decl;
4372 /* Find duplicate field within the same class declarations and report
4373 the error. Returns 1 if a duplicated field was found, 0
4374 otherwise. */
4376 static int
4377 duplicate_declaration_error_p (tree new_field_name, tree new_type, tree cl)
4379 /* This might be modified to work with method decl as well */
4380 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4381 if (decl)
4383 char *t1 = xstrdup (purify_type_name
4384 ((TREE_CODE (new_type) == POINTER_TYPE
4385 && TREE_TYPE (new_type) == NULL_TREE) ?
4386 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4387 lang_printable_name (new_type, 1)));
4388 /* The type may not have been completed by the time we report
4389 the error */
4390 char *t2 = xstrdup (purify_type_name
4391 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4392 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4393 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4394 lang_printable_name (TREE_TYPE (decl), 1)));
4395 parse_error_context
4396 (cl, "Duplicate variable declaration: %<%s %s%> was %<%s %s%> (%s:%d)",
4397 t1, IDENTIFIER_POINTER (new_field_name),
4398 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4399 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4400 free (t1);
4401 free (t2);
4402 return 1;
4404 return 0;
4407 /* Field registration routine. If TYPE doesn't exist, field
4408 declarations are linked to the undefined TYPE dependency list, to
4409 be later resolved in java_complete_class () */
4411 static void
4412 register_fields (int flags, tree type, tree variable_list)
4414 tree current, saved_type;
4415 tree class_type = NULL_TREE;
4416 location_t saved_location = input_location;
4417 int must_chain = 0;
4418 tree wfl = NULL_TREE;
4420 if (GET_CPC ())
4421 class_type = TREE_TYPE (GET_CPC ());
4423 if (!class_type || class_type == error_mark_node)
4424 return;
4426 /* If we're adding fields to interfaces, those fields are public,
4427 static, final */
4428 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4430 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4431 flags, ACC_PUBLIC, "interface field(s)");
4432 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4433 flags, ACC_STATIC, "interface field(s)");
4434 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4435 flags, ACC_FINAL, "interface field(s)");
4436 check_modifiers ("Illegal interface member modifier %qs", flags,
4437 INTERFACE_FIELD_MODIFIERS);
4438 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4441 /* Obtain a suitable type for resolution, if necessary */
4442 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4444 /* If TYPE is fully resolved and we don't have a reference, make one */
4445 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4447 for (current = variable_list, saved_type = type; current;
4448 current = TREE_CHAIN (current), type = saved_type)
4450 tree real_type;
4451 tree field_decl;
4452 tree cl = TREE_PURPOSE (current);
4453 tree init = TREE_VALUE (current);
4454 tree current_name = EXPR_WFL_NODE (cl);
4456 /* Can't declare non-final static fields in inner classes */
4457 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4458 && !(flags & ACC_FINAL))
4459 parse_error_context
4460 (cl, "Field %qs can't be static in inner class %qs unless it is final",
4461 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4462 lang_printable_name (class_type, 0));
4464 /* Process NAME, as it may specify extra dimension(s) for it */
4465 type = build_array_from_name (type, wfl, current_name, &current_name);
4467 /* Type adjustment. We may have just readjusted TYPE because
4468 the variable specified more dimensions. Make sure we have
4469 a reference if we can and don't have one already. Also
4470 change the name if we have an init. */
4471 if (type != saved_type)
4473 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4474 if (init)
4475 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4478 real_type = GET_REAL_TYPE (type);
4479 /* Check for redeclarations */
4480 if (duplicate_declaration_error_p (current_name, real_type, cl))
4481 continue;
4483 /* Set input_line to the line the field was found and create a
4484 declaration for it. Eventually sets the @deprecated tag flag. */
4485 #ifdef USE_MAPPED_LOCATION
4486 input_location = EXPR_LOCATION (cl);
4487 #else
4488 input_line = EXPR_WFL_LINENO (cl);
4489 #endif
4490 field_decl = add_field (class_type, current_name, real_type, flags);
4491 CHECK_DEPRECATED_NO_RESET (field_decl);
4493 /* If the field denotes a final instance variable, then we
4494 allocate a LANG_DECL_SPECIFIC part to keep track of its
4495 initialization. We also mark whether the field was
4496 initialized upon its declaration. We don't do that if the
4497 created field is an alias to a final local. */
4498 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4500 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4501 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4504 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4505 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4506 hide parameters to this inner class finit$ and
4507 constructors. It also means that the field isn't final per
4508 say. */
4509 if (ARG_FINAL_P (current))
4511 FIELD_LOCAL_ALIAS (field_decl) = 1;
4512 FIELD_FINAL (field_decl) = 0;
4515 /* Check if we must chain. */
4516 if (must_chain)
4517 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4519 /* If we have an initialization value tied to the field */
4520 if (init)
4522 /* The field is declared static */
4523 if (flags & ACC_STATIC)
4525 /* We include the field and its initialization part into
4526 a list used to generate <clinit>. After <clinit> is
4527 walked, field initializations will be processed and
4528 fields initialized with known constants will be taken
4529 out of <clinit> and have their DECL_INITIAL set
4530 appropriately. */
4531 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4532 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4533 if (TREE_OPERAND (init, 1)
4534 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4535 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4537 /* A non-static field declared with an immediate initialization is
4538 to be initialized in <init>, if any. This field is remembered
4539 to be processed at the time of the generation of <init>. */
4540 else
4542 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4543 SET_CPC_INITIALIZER_STMT (ctxp, init);
4545 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4546 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4550 CLEAR_DEPRECATED;
4551 input_location = saved_location;
4554 /* Generate finit$, using the list of initialized fields to populate
4555 its body. finit$'s parameter(s) list is adjusted to include the
4556 one(s) used to initialized the field(s) caching outer context
4557 local(s). */
4559 static tree
4560 generate_finit (tree class_type)
4562 int count = 0;
4563 tree list = TYPE_FINIT_STMT_LIST (class_type);
4564 tree mdecl, current, parms;
4566 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4567 class_type, NULL_TREE,
4568 &count);
4569 CRAFTED_PARAM_LIST_FIXUP (parms);
4570 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4571 finit_identifier_node, parms);
4572 fix_method_argument_names (parms, mdecl);
4573 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4574 mdecl, NULL_TREE);
4575 DECL_FUNCTION_NAP (mdecl) = count;
4576 start_artificial_method_body (mdecl);
4578 for (current = list; current; current = TREE_CHAIN (current))
4579 java_method_add_stmt (mdecl,
4580 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4581 current));
4582 end_artificial_method_body (mdecl);
4583 return mdecl;
4586 /* Generate a function to run the instance initialization code. The
4587 private method is called `instinit$'. Unless we're dealing with an
4588 anonymous class, we determine whether all ctors of CLASS_TYPE
4589 declare a checked exception in their `throws' clause in order to
4590 see whether it's necessary to encapsulate the instance initializer
4591 statements in a try/catch/rethrow sequence. */
4593 static tree
4594 generate_instinit (tree class_type)
4596 tree current;
4597 tree compound = NULL_TREE;
4598 tree parms = tree_cons (this_identifier_node,
4599 build_pointer_type (class_type), end_params_node);
4600 tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4601 void_type_node,
4602 instinit_identifier_node, parms);
4604 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4605 mdecl, NULL_TREE);
4607 /* Gather all the statements in a compound */
4608 for (current = TYPE_II_STMT_LIST (class_type);
4609 current; current = TREE_CHAIN (current))
4610 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4612 /* We need to encapsulate COMPOUND by a try/catch statement to
4613 rethrow exceptions that might occur in the instance initializer.
4614 We do that only if all ctors of CLASS_TYPE are set to catch a
4615 checked exception. This doesn't apply to anonymous classes (since
4616 they don't have declared ctors.) */
4617 if (!ANONYMOUS_CLASS_P (class_type) &&
4618 ctors_unchecked_throws_clause_p (class_type))
4620 compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4621 build1 (THROW_EXPR, NULL_TREE,
4622 build_wfl_node (wpv_id)));
4623 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4624 exception_type_node);
4627 start_artificial_method_body (mdecl);
4628 java_method_add_stmt (mdecl, compound);
4629 end_artificial_method_body (mdecl);
4631 return mdecl;
4634 /* FIXME */
4635 static tree
4636 build_instinit_invocation (tree class_type)
4638 tree to_return = NULL_TREE;
4640 if (TYPE_II_STMT_LIST (class_type))
4642 tree parm = build_tree_list (NULL_TREE,
4643 build_wfl_node (this_identifier_node));
4644 to_return =
4645 build_method_invocation (build_wfl_node (instinit_identifier_node),
4646 parm);
4648 return to_return;
4651 /* Shared across method_declarator and method_header to remember the
4652 patch stage that was reached during the declaration of the method.
4653 A method DECL is built differently is there is no patch
4654 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4655 pending on the currently defined method. */
4657 static int patch_stage;
4659 /* Check the method declaration and add the method to its current
4660 class. If the argument list is known to contain incomplete types,
4661 the method is partially added and the registration will be resume
4662 once the method arguments resolved. If TYPE is NULL, we're dealing
4663 with a constructor. */
4665 static tree
4666 method_header (int flags, tree type, tree mdecl, tree throws)
4668 tree type_wfl = NULL_TREE;
4669 tree meth_name = NULL_TREE;
4670 tree current, orig_arg, this_class = NULL;
4671 tree id, meth;
4672 location_t saved_location;
4673 int constructor_ok = 0, must_chain;
4674 int count;
4676 if (mdecl == error_mark_node)
4677 return error_mark_node;
4678 meth = TREE_VALUE (mdecl);
4679 id = TREE_PURPOSE (mdecl);
4681 check_modifiers_consistency (flags);
4683 if (GET_CPC ())
4684 this_class = TREE_TYPE (GET_CPC ());
4686 if (!this_class || this_class == error_mark_node)
4687 return NULL_TREE;
4689 /* There are some forbidden modifiers for an abstract method and its
4690 class must be abstract as well. */
4691 if (type && (flags & ACC_ABSTRACT))
4693 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4694 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4695 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4696 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4697 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4698 ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4699 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4700 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4701 parse_error_context
4702 (id,
4703 "Class %qs must be declared abstract to define abstract method %qs",
4704 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4705 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4708 /* A native method can't be strictfp. */
4709 if ((flags & ACC_NATIVE) && (flags & ACC_STRICT))
4710 parse_error_context (id, "native method %qs can't be strictfp",
4711 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4712 /* No such thing as a transient or volatile method. */
4713 if ((flags & ACC_TRANSIENT))
4714 parse_error_context (id, "method %qs can't be transient",
4715 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4716 if ((flags & ACC_VOLATILE))
4717 parse_error_context (id, "method %qs can't be volatile",
4718 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4720 /* Things to be checked when declaring a constructor */
4721 if (!type)
4723 int ec = java_error_count;
4724 /* 8.6: Constructor declarations: we might be trying to define a
4725 method without specifying a return type. */
4726 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4727 parse_error_context
4728 (id, "Invalid method declaration, return type required");
4729 /* 8.6.3: Constructor modifiers */
4730 else
4732 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4733 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4734 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4735 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4736 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4737 JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4739 /* If we found error here, we don't consider it's OK to tread
4740 the method definition as a constructor, for the rest of this
4741 function */
4742 if (ec == java_error_count)
4743 constructor_ok = 1;
4746 /* Method declared within the scope of an interface are implicitly
4747 abstract and public. Conflicts with other erroneously provided
4748 modifiers are checked right after. */
4750 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4752 /* If FLAGS isn't set because of a modifier, turn the
4753 corresponding modifier WFL to NULL so we issue a warning on
4754 the obsolete use of the modifier */
4755 if (!(flags & ACC_PUBLIC))
4756 MODIFIER_WFL (PUBLIC_TK) = NULL;
4757 if (!(flags & ACC_ABSTRACT))
4758 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4759 flags |= ACC_PUBLIC;
4760 flags |= ACC_ABSTRACT;
4763 /* Inner class can't declare static methods */
4764 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4766 parse_error_context
4767 (id, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
4768 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4769 lang_printable_name (this_class, 0));
4772 /* Modifiers context reset moved up, so abstract method declaration
4773 modifiers can be later checked. */
4775 /* Set constructor returned type to void and method name to <init>,
4776 unless we found an error identifier the constructor (in which
4777 case we retain the original name) */
4778 if (!type)
4780 type = void_type_node;
4781 if (constructor_ok)
4782 meth_name = init_identifier_node;
4784 else
4785 meth_name = EXPR_WFL_NODE (id);
4787 /* Do the returned type resolution and registration if necessary */
4788 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4790 if (meth_name)
4791 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4792 EXPR_WFL_NODE (id) = meth_name;
4793 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4795 if (must_chain)
4797 patch_stage = JDEP_METHOD_RETURN;
4798 register_incomplete_type (patch_stage, type_wfl, id, type);
4799 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4801 else
4802 TREE_TYPE (meth) = type;
4804 saved_location = input_location;
4805 /* When defining an abstract or interface method, the curly
4806 bracket at level 1 doesn't exist because there is no function
4807 body */
4808 #ifdef USE_MAPPED_LOCATION
4809 input_location = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4810 EXPR_LOCATION (id));
4811 #else
4812 input_line = (ctxp->first_ccb_indent1 ? (int) ctxp->first_ccb_indent1 :
4813 EXPR_WFL_LINENO (id));
4814 #endif
4816 /* Remember the original argument list */
4817 orig_arg = TYPE_ARG_TYPES (meth);
4819 if (patch_stage) /* includes ret type and/or all args */
4821 jdep *jdep;
4822 meth = add_method_1 (this_class, flags, meth_name, meth);
4823 /* Patch for the return type */
4824 if (patch_stage == JDEP_METHOD_RETURN)
4826 jdep = CLASSD_LAST (ctxp->classd_list);
4827 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4829 /* This is the stop JDEP. METH allows the function's signature
4830 to be computed. */
4831 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4833 else
4834 meth = add_method (this_class, flags, meth_name,
4835 build_java_signature (meth));
4837 /* Remember final parameters */
4838 MARK_FINAL_PARMS (meth, orig_arg);
4840 /* Fix the method argument list so we have the argument name
4841 information */
4842 fix_method_argument_names (orig_arg, meth);
4844 /* Register the parameter number and re-install the current line
4845 number */
4846 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4847 input_location = saved_location;
4849 /* Register exception specified by the `throws' keyword for
4850 resolution and set the method decl appropriate field to the list.
4851 Note: the grammar ensures that what we get here are class
4852 types. */
4853 if (throws)
4855 throws = nreverse (throws);
4856 for (current = throws; current; current = TREE_CHAIN (current))
4858 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4859 NULL_TREE, NULL_TREE);
4860 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4861 &TREE_VALUE (current);
4863 DECL_FUNCTION_THROWS (meth) = throws;
4866 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4867 DECL_FUNCTION_WFL (meth) = id;
4869 /* Set the flag if we correctly processed a constructor */
4870 if (constructor_ok)
4872 DECL_CONSTRUCTOR_P (meth) = 1;
4873 /* Compute and store the number of artificial parameters declared
4874 for this constructor */
4875 for (count = 0, current = TYPE_FIELDS (this_class); current;
4876 current = TREE_CHAIN (current))
4877 if (FIELD_LOCAL_ALIAS (current))
4878 count++;
4879 DECL_FUNCTION_NAP (meth) = count;
4882 /* Eventually set the @deprecated tag flag */
4883 CHECK_DEPRECATED (meth);
4885 return meth;
4888 static void
4889 fix_method_argument_names (tree orig_arg, tree meth)
4891 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4892 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4894 TREE_PURPOSE (arg) = this_identifier_node;
4895 arg = TREE_CHAIN (arg);
4897 while (orig_arg != end_params_node)
4899 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4900 orig_arg = TREE_CHAIN (orig_arg);
4901 arg = TREE_CHAIN (arg);
4905 /* Complete the method declaration with METHOD_BODY. */
4907 static void
4908 finish_method_declaration (tree method_body)
4910 int flags;
4912 if (!current_function_decl)
4913 return;
4915 flags = get_access_flags_from_decl (current_function_decl);
4917 /* 8.4.5 Method Body */
4918 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4920 tree name = DECL_NAME (current_function_decl);
4921 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4922 "%s method %qs can't have a body defined",
4923 (METHOD_NATIVE (current_function_decl) ?
4924 "Native" : "Abstract"),
4925 IDENTIFIER_POINTER (name));
4926 method_body = NULL_TREE;
4928 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4930 tree name = DECL_NAME (current_function_decl);
4931 parse_error_context
4932 (DECL_FUNCTION_WFL (current_function_decl),
4933 "Non native and non abstract method %qs must have a body defined",
4934 IDENTIFIER_POINTER (name));
4935 method_body = NULL_TREE;
4938 if (flag_emit_class_files && method_body
4939 && TREE_CODE (method_body) == NOP_EXPR
4940 && TREE_TYPE (current_function_decl)
4941 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4942 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4944 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4945 maybe_absorb_scoping_blocks ();
4946 /* Exit function's body */
4947 exit_block ();
4948 /* Merge last line of the function with first line, directly in the
4949 function decl. It will be used to emit correct debug info. */
4950 DECL_FUNCTION_LAST_LINE (current_function_decl) = ctxp->last_ccb_indent1;
4952 /* Since function's argument's list are shared, reset the
4953 ARG_FINAL_P parameter that might have been set on some of this
4954 function parameters. */
4955 UNMARK_FINAL_PARMS (current_function_decl);
4957 /* So we don't have an irrelevant function declaration context for
4958 the next static block we'll see. */
4959 current_function_decl = NULL_TREE;
4962 /* Build a an error message for constructor circularity errors. */
4964 static char *
4965 constructor_circularity_msg (tree from, tree to)
4967 static char string [4096];
4968 char *t = xstrdup (lang_printable_name (from, 2));
4969 sprintf (string, "'%s' invokes '%s'", t, lang_printable_name (to, 2));
4970 free (t);
4971 return string;
4974 /* Verify a circular call to METH. Return 1 if an error is found, 0
4975 otherwise. */
4977 static GTY(()) tree vcc_list;
4978 static int
4979 verify_constructor_circularity (tree meth, tree current)
4981 tree c;
4983 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4985 if (TREE_VALUE (c) == meth)
4987 char *t;
4988 if (vcc_list)
4990 tree liste;
4991 vcc_list = nreverse (vcc_list);
4992 for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
4994 parse_error_context
4995 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4996 constructor_circularity_msg
4997 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4998 java_error_count--;
5001 t = xstrdup (lang_printable_name (meth, 2));
5002 parse_error_context (TREE_PURPOSE (c),
5003 "%s: recursive invocation of constructor %qs",
5004 constructor_circularity_msg (current, meth), t);
5005 free (t);
5006 vcc_list = NULL_TREE;
5007 return 1;
5010 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5012 vcc_list = tree_cons (c, current, vcc_list);
5013 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
5014 return 1;
5015 vcc_list = TREE_CHAIN (vcc_list);
5017 return 0;
5020 /* Check modifiers that can be declared but exclusively */
5022 static void
5023 check_modifiers_consistency (int flags)
5025 int acc_count = 0;
5026 tree cl = NULL_TREE;
5028 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
5029 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
5030 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
5031 if (acc_count > 1)
5032 parse_error_context
5033 (cl, "Inconsistent member declaration. At most one of %<public%>, %<private%>, or %<protected%> may be specified");
5035 acc_count = 0;
5036 cl = NULL_TREE;
5037 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
5038 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
5039 if (acc_count > 1)
5040 parse_error_context (cl,
5041 "Inconsistent member declaration. At most one of %<final%> or %<volatile%> may be specified");
5044 /* Check the methode header METH for abstract specifics features */
5046 static void
5047 check_abstract_method_header (tree meth)
5049 int flags = get_access_flags_from_decl (meth);
5051 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
5052 ACC_ABSTRACT, "abstract method",
5053 IDENTIFIER_POINTER (DECL_NAME (meth)));
5054 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
5055 ACC_PUBLIC, "abstract method",
5056 IDENTIFIER_POINTER (DECL_NAME (meth)));
5058 check_modifiers ("Illegal modifier %qs for interface method",
5059 flags, INTERFACE_METHOD_MODIFIERS);
5062 /* Create a FUNCTION_TYPE node and start augmenting it with the
5063 declared function arguments. Arguments type that can't be resolved
5064 are left as they are, but the returned node is marked as containing
5065 incomplete types. */
5067 static tree
5068 method_declarator (tree id, tree list)
5070 tree arg_types = NULL_TREE, current, node;
5071 tree meth = make_node (FUNCTION_TYPE);
5072 jdep *jdep;
5074 patch_stage = JDEP_NO_PATCH;
5076 if (GET_CPC () == error_mark_node)
5077 return error_mark_node;
5079 /* If we're dealing with an inner class constructor, we hide the
5080 this$<n> decl in the name field of its parameter declaration. We
5081 also might have to hide the outer context local alias
5082 initializers. Not done when the class is a toplevel class. */
5083 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
5084 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
5086 tree aliases_list, type, thisn;
5087 /* First the aliases, linked to the regular parameters */
5088 aliases_list =
5089 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
5090 TREE_TYPE (GET_CPC ()),
5091 NULL_TREE, NULL);
5092 list = chainon (nreverse (aliases_list), list);
5094 /* Then this$<n> */
5095 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
5096 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
5097 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
5098 list);
5101 for (current = list; current; current = TREE_CHAIN (current))
5103 int must_chain = 0;
5104 tree wfl_name = TREE_PURPOSE (current);
5105 tree type = TREE_VALUE (current);
5106 tree name = EXPR_WFL_NODE (wfl_name);
5107 tree already, arg_node;
5108 tree type_wfl = NULL_TREE;
5109 tree real_type;
5111 /* Obtain a suitable type for resolution, if necessary */
5112 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5114 /* Process NAME, as it may specify extra dimension(s) for it */
5115 type = build_array_from_name (type, type_wfl, name, &name);
5116 EXPR_WFL_NODE (wfl_name) = name;
5118 real_type = GET_REAL_TYPE (type);
5119 if (TREE_CODE (real_type) == RECORD_TYPE)
5121 real_type = promote_type (real_type);
5122 if (TREE_CODE (type) == TREE_LIST)
5123 TREE_PURPOSE (type) = real_type;
5126 /* Check redefinition */
5127 for (already = arg_types; already; already = TREE_CHAIN (already))
5128 if (TREE_PURPOSE (already) == name)
5130 parse_error_context
5131 (wfl_name, "Variable %qs is used more than once in the argument list of method %qs",
5132 IDENTIFIER_POINTER (name),
5133 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5134 break;
5137 /* If we've an incomplete argument type, we know there is a location
5138 to patch when the type get resolved, later. */
5139 jdep = NULL;
5140 if (must_chain)
5142 patch_stage = JDEP_METHOD;
5143 type = register_incomplete_type (patch_stage,
5144 type_wfl, wfl_name, type);
5145 jdep = CLASSD_LAST (ctxp->classd_list);
5146 JDEP_MISC (jdep) = id;
5149 /* The argument node: a name and a (possibly) incomplete type. */
5150 arg_node = build_tree_list (name, real_type);
5151 /* Remember arguments declared final. */
5152 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5154 if (jdep)
5155 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5156 TREE_CHAIN (arg_node) = arg_types;
5157 arg_types = arg_node;
5159 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5160 node = build_tree_list (id, meth);
5161 return node;
5164 static int
5165 unresolved_type_p (tree wfl, tree *returned)
5167 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5169 if (returned)
5171 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5172 if (decl && current_class && (decl == TYPE_NAME (current_class)))
5173 *returned = TREE_TYPE (decl);
5174 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5175 *returned = TREE_TYPE (GET_CPC ());
5176 else
5177 *returned = NULL_TREE;
5179 return 1;
5181 if (returned)
5182 *returned = wfl;
5183 return 0;
5186 /* From NAME, build a qualified identifier node using the
5187 qualification from the current package definition. */
5189 static tree
5190 parser_qualified_classname (tree name)
5192 tree nested_class_name;
5194 if ((nested_class_name = maybe_make_nested_class_name (name)))
5195 return nested_class_name;
5197 if (ctxp->package)
5198 return merge_qualified_name (ctxp->package, name);
5199 else
5200 return name;
5203 /* Called once the type a interface extends is resolved. Returns 0 if
5204 everything is OK. */
5206 static int
5207 parser_check_super_interface (tree super_decl, tree this_decl, tree this_wfl)
5209 tree super_type = TREE_TYPE (super_decl);
5211 /* Has to be an interface */
5212 if (!CLASS_INTERFACE (super_decl))
5214 parse_error_context
5215 (this_wfl, "%s %qs can't implement/extend %s %qs",
5216 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5217 "Interface" : "Class"),
5218 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5219 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5220 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5221 return 1;
5224 /* Check top-level interface access. Inner classes are subject to member
5225 access rules (6.6.1). */
5226 if (! INNER_CLASS_P (super_type)
5227 && check_pkg_class_access (DECL_NAME (super_decl),
5228 NULL_TREE, true, this_decl))
5229 return 1;
5231 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5232 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5233 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5234 return 0;
5237 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5238 0 if everything is OK. */
5240 static int
5241 parser_check_super (tree super_decl, tree this_decl, tree wfl)
5243 tree super_type = TREE_TYPE (super_decl);
5245 /* SUPER should be a CLASS (neither an array nor an interface) */
5246 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5248 parse_error_context
5249 (wfl, "Class %qs can't subclass %s %qs",
5250 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5251 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5252 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5253 return 1;
5256 if (CLASS_FINAL (TYPE_NAME (super_type)))
5258 parse_error_context (wfl, "Can't subclass final classes: %s",
5259 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5260 return 1;
5263 /* Check top-level class scope. Inner classes are subject to member access
5264 rules (6.6.1). */
5265 if (! INNER_CLASS_P (super_type)
5266 && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true, NULL_TREE)))
5267 return 1;
5269 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5270 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5271 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5272 return 0;
5275 /* Create a new dependency list and link it (in a LIFO manner) to the
5276 CTXP list of type dependency list. */
5278 static void
5279 create_jdep_list (struct parser_ctxt *ctxp)
5281 jdeplist *new = xmalloc (sizeof (jdeplist));
5282 new->first = new->last = NULL;
5283 new->next = ctxp->classd_list;
5284 ctxp->classd_list = new;
5287 static jdeplist *
5288 reverse_jdep_list (struct parser_ctxt *ctxp)
5290 jdeplist *prev = NULL, *current, *next;
5291 for (current = ctxp->classd_list; current; current = next)
5293 next = current->next;
5294 current->next = prev;
5295 prev = current;
5297 return prev;
5300 /* Create a fake pointer based on the ID stored in
5301 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5302 registered again. */
5304 static tree
5305 obtain_incomplete_type (tree type_name)
5307 tree ptr = NULL_TREE, name;
5309 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5310 name = EXPR_WFL_NODE (type_name);
5311 else if (INCOMPLETE_TYPE_P (type_name))
5312 name = TYPE_NAME (type_name);
5313 else
5314 abort ();
5316 /* Workaround from build_pointer_type for incomplete types. */
5317 BUILD_PTR_FROM_NAME (ptr, name);
5318 TYPE_MODE (ptr) = ptr_mode;
5319 layout_type (ptr);
5321 return ptr;
5324 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5325 non NULL instead of computing a new fake type based on WFL. The new
5326 dependency is inserted in the current type dependency list, in FIFO
5327 manner. */
5329 static tree
5330 register_incomplete_type (int kind, tree wfl, tree decl, tree ptr)
5332 jdep *new = xmalloc (sizeof (jdep));
5334 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5335 ptr = obtain_incomplete_type (wfl);
5337 JDEP_KIND (new) = kind;
5338 JDEP_DECL (new) = decl;
5339 JDEP_TO_RESOLVE (new) = ptr;
5340 JDEP_WFL (new) = wfl;
5341 JDEP_CHAIN (new) = NULL;
5342 JDEP_MISC (new) = NULL_TREE;
5343 /* For some dependencies, set the enclosing class of the current
5344 class to be the enclosing context */
5345 if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS || kind == JDEP_SUPER)
5346 && GET_ENCLOSING_CPC ())
5347 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5348 else
5349 JDEP_ENCLOSING (new) = GET_CPC ();
5350 JDEP_GET_PATCH (new) = (tree *)NULL;
5352 JDEP_INSERT (ctxp->classd_list, new);
5354 return ptr;
5357 /* This checks for circular references with innerclasses. We start
5358 from SOURCE and should never reach TARGET. Extended/implemented
5359 types in SOURCE have their enclosing context checked not to reach
5360 TARGET. When the last enclosing context of SOURCE is reached, its
5361 extended/implemented types are also checked not to reach TARGET.
5362 In case of error, WFL of the offending type is returned; NULL_TREE
5363 otherwise. */
5365 static tree
5366 check_inner_circular_reference (tree source, tree target)
5368 tree base_binfo;
5369 tree ctx, cl;
5370 int i;
5372 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (source), i, base_binfo); i++)
5374 tree su;
5376 /* We can end up with a NULL_TREE or an incomplete type here if
5377 we encountered previous type resolution errors. It's safe to
5378 simply ignore these cases. */
5379 su = BINFO_TYPE (base_binfo);
5380 if (INCOMPLETE_TYPE_P (su))
5381 continue;
5383 if (inherits_from_p (su, target))
5384 return lookup_cl (TYPE_NAME (su));
5386 for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5388 /* An enclosing context shouldn't be TARGET */
5389 if (ctx == TYPE_NAME (target))
5390 return lookup_cl (TYPE_NAME (su));
5392 /* When we reach the enclosing last context, start a check
5393 on it, with the same target */
5394 if (! DECL_CONTEXT (ctx) &&
5395 (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5396 return cl;
5399 return NULL_TREE;
5402 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5403 offending type if a circularity is detected. NULL_TREE is returned
5404 otherwise. TYPE can be an interface or a class. */
5406 static tree
5407 check_circular_reference (tree type)
5409 tree base_binfo;
5410 int i;
5412 if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
5413 return NULL_TREE;
5415 if (! CLASS_INTERFACE (TYPE_NAME (type)))
5417 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5418 return lookup_cl (TYPE_NAME (type));
5419 return NULL_TREE;
5422 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo); i++)
5424 if (BINFO_TYPE (base_binfo) != object_type_node
5425 && interface_of_p (type, BINFO_TYPE (base_binfo)))
5426 return lookup_cl (TYPE_NAME (BINFO_TYPE (base_binfo)));
5428 return NULL_TREE;
5431 void
5432 java_check_circular_reference (void)
5434 tree current;
5435 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5437 tree type = TREE_TYPE (current);
5438 tree cl;
5440 cl = check_circular_reference (type);
5441 if (! cl)
5442 cl = check_inner_circular_reference (type, type);
5443 if (cl)
5444 parse_error_context (cl, "Cyclic class inheritance%s",
5445 (cyclic_inheritance_report ?
5446 cyclic_inheritance_report : ""));
5450 /* Augment the parameter list PARM with parameters crafted to
5451 initialize outer context locals aliases. Through ARTIFICIAL, a
5452 count is kept of the number of crafted parameters. MODE governs
5453 what eventually gets created: something suitable for a function
5454 creation or a function invocation, either the constructor or
5455 finit$. */
5457 static tree
5458 build_alias_initializer_parameter_list (int mode, tree class_type, tree parm,
5459 int *artificial)
5461 tree field;
5462 tree additional_parms = NULL_TREE;
5464 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5465 if (FIELD_LOCAL_ALIAS (field))
5467 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5468 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5469 tree mangled_id;
5471 switch (mode)
5473 case AIPL_FUNCTION_DECLARATION:
5474 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5475 &buffer [4]);
5476 purpose = build_wfl_node (mangled_id);
5477 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5478 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5479 else
5480 value = TREE_TYPE (field);
5481 break;
5483 case AIPL_FUNCTION_CREATION:
5484 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5485 &buffer [4]);
5486 value = TREE_TYPE (field);
5487 break;
5489 case AIPL_FUNCTION_FINIT_INVOCATION:
5490 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5491 &buffer [4]);
5492 /* Now, this is wrong. purpose should always be the NAME
5493 of something and value its matching value (decl, type,
5494 etc...) FIXME -- but there is a lot to fix. */
5496 /* When invoked for this kind of operation, we already
5497 know whether a field is used or not. */
5498 purpose = TREE_TYPE (field);
5499 value = build_wfl_node (mangled_id);
5500 break;
5502 case AIPL_FUNCTION_CTOR_INVOCATION:
5503 /* There are two case: the constructor invocation happens
5504 outside the local inner, in which case, locales from the outer
5505 context are directly used.
5507 Otherwise, we fold to using the alias directly. */
5508 if (class_type == current_class)
5509 value = field;
5510 else
5512 name = get_identifier (&buffer[4]);
5513 value = IDENTIFIER_LOCAL_VALUE (name);
5515 break;
5517 additional_parms = tree_cons (purpose, value, additional_parms);
5518 if (artificial)
5519 *artificial +=1;
5521 if (additional_parms)
5523 if (ANONYMOUS_CLASS_P (class_type)
5524 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5525 additional_parms = nreverse (additional_parms);
5526 parm = chainon (additional_parms, parm);
5529 return parm;
5532 /* Craft a constructor for CLASS_DECL -- what we should do when none
5533 where found. ARGS is non NULL when a special signature must be
5534 enforced. This is the case for anonymous classes. */
5536 static tree
5537 craft_constructor (tree class_decl, tree args)
5539 tree class_type = TREE_TYPE (class_decl);
5540 tree parm = NULL_TREE;
5541 /* Inherit access flags for the constructor from its enclosing class. */
5542 int valid_ctor_flags = ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE;
5543 int flags = (get_access_flags_from_decl (class_decl) & valid_ctor_flags);
5544 int i = 0, artificial = 0;
5545 tree decl, ctor_name;
5546 char buffer [80];
5548 ctor_name = init_identifier_node;
5550 /* If we're dealing with an inner class constructor, we hide the
5551 this$<n> decl in the name field of its parameter declaration. */
5552 if (PURE_INNER_CLASS_TYPE_P (class_type))
5554 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5555 parm = tree_cons (build_current_thisn (class_type),
5556 build_pointer_type (type), parm);
5558 /* Some more arguments to be hidden here. The values of the local
5559 variables of the outer context that the inner class needs to see. */
5560 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5561 class_type, parm,
5562 &artificial);
5565 /* Then if there are any args to be enforced, enforce them now */
5566 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5568 /* If we see a `void *', we need to change it to Object. */
5569 if (TREE_VALUE (args) == TREE_TYPE (null_pointer_node))
5570 TREE_VALUE (args) = object_ptr_type_node;
5572 sprintf (buffer, "parm%d", i++);
5573 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5576 CRAFTED_PARAM_LIST_FIXUP (parm);
5577 decl = create_artificial_method (class_type, flags, void_type_node,
5578 ctor_name, parm);
5579 fix_method_argument_names (parm, decl);
5580 /* Now, mark the artificial parameters. */
5581 DECL_FUNCTION_NAP (decl) = artificial;
5582 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5583 DECL_INLINE (decl) = 1;
5584 return decl;
5588 /* Fix the constructors. This will be called right after circular
5589 references have been checked. It is necessary to fix constructors
5590 early even if no code generation will take place for that class:
5591 some generated constructor might be required by the class whose
5592 compilation triggered this one to be simply loaded. */
5594 void
5595 java_fix_constructors (void)
5597 tree current;
5599 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5601 tree class_type = TREE_TYPE (current);
5602 int saw_ctor = 0;
5603 tree decl;
5605 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5606 continue;
5608 output_class = current_class = class_type;
5609 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5611 if (DECL_CONSTRUCTOR_P (decl))
5613 fix_constructors (decl);
5614 saw_ctor = 1;
5618 /* Anonymous class constructor can't be generated that early. */
5619 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5620 craft_constructor (current, NULL_TREE);
5624 /* safe_layout_class just makes sure that we can load a class without
5625 disrupting the current_class, input_file, input_line, etc, information
5626 about the class processed currently. */
5628 void
5629 safe_layout_class (tree class)
5631 tree save_current_class = current_class;
5632 location_t save_location = input_location;
5634 layout_class (class);
5636 current_class = save_current_class;
5637 input_location = save_location;
5640 static tree
5641 jdep_resolve_class (jdep *dep)
5643 tree decl;
5645 if (JDEP_RESOLVED_P (dep))
5646 decl = JDEP_RESOLVED_DECL (dep);
5647 else
5649 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5650 JDEP_DECL (dep), JDEP_WFL (dep));
5651 JDEP_RESOLVED (dep, decl);
5652 /* If there is no WFL, that's ok. We generate this warning
5653 elsewhere. */
5654 if (decl && JDEP_WFL (dep) != NULL_TREE)
5655 check_deprecation (JDEP_WFL (dep), decl);
5658 if (!decl)
5659 complete_class_report_errors (dep);
5660 else if (INNER_CLASS_DECL_P (decl))
5662 tree inner = TREE_TYPE (decl);
5663 if (! CLASS_LOADED_P (inner))
5665 safe_layout_class (inner);
5666 if (TYPE_SIZE (inner) == error_mark_node)
5667 TYPE_SIZE (inner) = NULL_TREE;
5669 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5671 return decl;
5674 /* Complete unsatisfied class declaration and their dependencies */
5676 void
5677 java_complete_class (void)
5679 tree cclass;
5680 jdeplist *cclassd;
5681 int error_found;
5682 tree type;
5684 /* Process imports */
5685 process_imports ();
5687 /* Reverse things so we have the right order */
5688 ctxp->class_list = nreverse (ctxp->class_list);
5689 ctxp->classd_list = reverse_jdep_list (ctxp);
5691 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5692 cclass && cclassd;
5693 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5695 jdep *dep;
5697 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5699 tree decl;
5700 if (!(decl = jdep_resolve_class (dep)))
5701 continue;
5703 /* Now it's time to patch */
5704 switch (JDEP_KIND (dep))
5706 case JDEP_SUPER:
5707 /* Simply patch super */
5708 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5709 continue;
5710 BINFO_TYPE (BINFO_BASE_BINFO
5711 (TYPE_BINFO (TREE_TYPE (JDEP_DECL (dep))), 0))
5712 = TREE_TYPE (decl);
5713 break;
5715 case JDEP_FIELD:
5717 /* We do part of the job done in add_field */
5718 tree field_decl = JDEP_DECL (dep);
5719 tree field_type = TREE_TYPE (decl);
5720 if (TREE_CODE (field_type) == RECORD_TYPE)
5721 field_type = promote_type (field_type);
5722 TREE_TYPE (field_decl) = field_type;
5723 DECL_ALIGN (field_decl) = 0;
5724 DECL_USER_ALIGN (field_decl) = 0;
5725 layout_decl (field_decl, 0);
5726 SOURCE_FRONTEND_DEBUG
5727 (("Completed field/var decl '%s' with '%s'",
5728 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5729 IDENTIFIER_POINTER (DECL_NAME (decl))));
5730 break;
5732 case JDEP_METHOD: /* We start patching a method */
5733 case JDEP_METHOD_RETURN:
5734 error_found = 0;
5735 while (1)
5737 if (decl)
5739 type = TREE_TYPE(decl);
5740 if (TREE_CODE (type) == RECORD_TYPE)
5741 type = promote_type (type);
5742 JDEP_APPLY_PATCH (dep, type);
5743 SOURCE_FRONTEND_DEBUG
5744 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5745 "Completing fct '%s' with ret type '%s'":
5746 "Completing arg '%s' with type '%s'"),
5747 IDENTIFIER_POINTER (EXPR_WFL_NODE
5748 (JDEP_DECL_WFL (dep))),
5749 IDENTIFIER_POINTER (DECL_NAME (decl))));
5751 else
5752 error_found = 1;
5753 dep = JDEP_CHAIN (dep);
5754 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5755 break;
5756 else
5757 decl = jdep_resolve_class (dep);
5759 if (!error_found)
5761 tree mdecl = JDEP_DECL (dep), signature;
5762 /* Recompute and reset the signature, check first that
5763 all types are now defined. If they're not,
5764 don't build the signature. */
5765 if (check_method_types_complete (mdecl))
5767 signature = build_java_signature (TREE_TYPE (mdecl));
5768 set_java_signature (TREE_TYPE (mdecl), signature);
5771 else
5772 continue;
5773 break;
5775 case JDEP_INTERFACE:
5776 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5777 JDEP_WFL (dep)))
5778 continue;
5779 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5780 break;
5782 case JDEP_PARM:
5783 case JDEP_VARIABLE:
5784 type = TREE_TYPE(decl);
5785 if (TREE_CODE (type) == RECORD_TYPE)
5786 type = promote_type (type);
5787 JDEP_APPLY_PATCH (dep, type);
5788 break;
5790 case JDEP_TYPE:
5791 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5792 SOURCE_FRONTEND_DEBUG
5793 (("Completing a random type dependency on a '%s' node",
5794 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5795 break;
5797 case JDEP_EXCEPTION:
5798 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5799 SOURCE_FRONTEND_DEBUG
5800 (("Completing '%s' 'throws' argument node",
5801 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5802 break;
5804 case JDEP_ANONYMOUS:
5805 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5806 break;
5808 default:
5809 abort ();
5813 return;
5816 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5817 array. */
5819 static tree
5820 resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5822 tree tname = TYPE_NAME (class_type);
5823 tree resolved_type = TREE_TYPE (class_type);
5824 int array_dims = 0;
5825 tree resolved_type_decl;
5827 if (resolved_type != NULL_TREE)
5829 tree resolved_type_decl = TYPE_NAME (resolved_type);
5830 if (resolved_type_decl == NULL_TREE
5831 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5833 resolved_type_decl = build_decl (TYPE_DECL,
5834 TYPE_NAME (class_type),
5835 resolved_type);
5837 return resolved_type_decl;
5840 /* 1- Check to see if we have an array. If true, find what we really
5841 want to resolve */
5842 if ((array_dims = build_type_name_from_array_name (tname,
5843 &TYPE_NAME (class_type))))
5844 WFL_STRIP_BRACKET (cl, cl);
5846 /* 2- Resolve the bare type */
5847 if (!(resolved_type_decl = do_resolve_class (enclosing, NULL_TREE, class_type,
5848 decl, cl)))
5849 return NULL_TREE;
5850 resolved_type = TREE_TYPE (resolved_type_decl);
5852 /* 3- If we have an array, reconstruct the array down to its nesting */
5853 if (array_dims)
5855 for (; array_dims; array_dims--)
5856 resolved_type = build_java_array_type (resolved_type, -1);
5857 resolved_type_decl = TYPE_NAME (resolved_type);
5859 TREE_TYPE (class_type) = resolved_type;
5860 return resolved_type_decl;
5863 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5864 are used to report error messages; CL must either be NULL_TREE or a
5865 WFL wrapping a class. Do not try to replace TYPE_NAME (class_type)
5866 by a variable, since it is changed by find_in_imports{_on_demand}
5867 and (but it doesn't really matter) qualify_and_find. */
5869 tree
5870 do_resolve_class (tree enclosing, tree import_type, tree class_type, tree decl,
5871 tree cl)
5873 tree new_class_decl = NULL_TREE, super = NULL_TREE;
5874 tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5875 tree decl_result;
5876 htab_t circularity_hash;
5878 if (QUALIFIED_P (TYPE_NAME (class_type)))
5880 /* If the type name is of the form `Q . Id', then Q is either a
5881 package name or a class name. First we try to find Q as a
5882 class and then treat Id as a member type. If we can't find Q
5883 as a class then we fall through. */
5884 tree q, left, left_type, right;
5885 if (split_qualified_name (&left, &right, TYPE_NAME (class_type)) == 0)
5887 BUILD_PTR_FROM_NAME (left_type, left);
5888 q = do_resolve_class (enclosing, import_type, left_type, decl, cl);
5889 if (q)
5891 enclosing = q;
5892 saved_enclosing_type = TREE_TYPE (q);
5893 BUILD_PTR_FROM_NAME (class_type, right);
5898 if (enclosing)
5900 /* This hash table is used to register the classes we're going
5901 through when searching the current class as an inner class, in
5902 order to detect circular references. Remember to free it before
5903 returning the section 0- of this function. */
5904 circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
5905 NULL);
5907 /* 0- Search in the current class as an inner class.
5908 Maybe some code here should be added to load the class or
5909 something, at least if the class isn't an inner class and ended
5910 being loaded from class file. FIXME. */
5911 while (enclosing)
5913 new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5914 &super, class_type);
5915 if (new_class_decl)
5916 break;
5918 /* If we haven't found anything because SUPER reached Object and
5919 ENCLOSING happens to be an innerclass, try the enclosing context. */
5920 if ((!super || super == object_type_node) &&
5921 enclosing && INNER_CLASS_DECL_P (enclosing))
5922 enclosing = DECL_CONTEXT (enclosing);
5923 else
5924 enclosing = NULL_TREE;
5927 htab_delete (circularity_hash);
5929 if (new_class_decl)
5930 return new_class_decl;
5933 /* 1- Check for the type in single imports. Look at enclosing classes and,
5934 if we're laying out a superclass, at the import list for the subclass.
5935 This will change TYPE_NAME() if something relevant is found. */
5936 if (import_type && TYPE_IMPORT_LIST (import_type))
5937 find_in_imports (import_type, class_type);
5938 find_in_imports (saved_enclosing_type, class_type);
5940 /* 2- And check for the type in the current compilation unit */
5941 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5943 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
5944 load_class (TYPE_NAME (class_type), 0);
5945 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5948 /* 3- Search according to the current package definition */
5949 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5951 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5952 TYPE_NAME (class_type))))
5953 return new_class_decl;
5956 /* 4- Check the import on demands. Don't allow bar.baz to be
5957 imported from foo.* */
5958 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5960 if (import_type
5961 && TYPE_IMPORT_DEMAND_LIST (import_type)
5962 && find_in_imports_on_demand (import_type, class_type))
5963 return NULL_TREE;
5964 if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5965 return NULL_TREE;
5968 /* If found in find_in_imports_on_demand, the type has already been
5969 loaded. */
5970 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5971 return new_class_decl;
5973 /* 5- Try with a name qualified with the package name we've seen so far */
5974 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5976 tree package;
5978 /* If there is a current package (ctxp->package), it's the first
5979 element of package_list and we can skip it. */
5980 for (package = (ctxp->package ?
5981 TREE_CHAIN (package_list) : package_list);
5982 package; package = TREE_CHAIN (package))
5983 if ((new_class_decl = qualify_and_find (class_type,
5984 TREE_PURPOSE (package),
5985 TYPE_NAME (class_type))))
5986 return new_class_decl;
5989 /* 5- Check another compilation unit that bears the name of type */
5990 load_class (TYPE_NAME (class_type), 0);
5992 if (!cl)
5993 cl = lookup_cl (decl);
5995 /* If we don't have a value for CL, then we're being called recursively.
5996 We can't check package access just yet, but it will be taken care of
5997 by the caller. */
5998 if (cl)
6000 if (check_pkg_class_access (TYPE_NAME (class_type), cl, true, NULL_TREE))
6001 return NULL_TREE;
6004 /* 6- Last call for a resolution */
6005 decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6007 /* The final lookup might have registered a.b.c into a.b$c If we
6008 failed at the first lookup, progressively change the name if
6009 applicable and use the matching DECL instead. */
6010 if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
6012 char *separator;
6013 tree name = TYPE_NAME (class_type);
6014 char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
6016 strcpy (namebuffer, IDENTIFIER_POINTER (name));
6018 do {
6020 /* Reach the last '.', and if applicable, replace it by a `$' and
6021 see if this exists as a type. */
6022 if ((separator = strrchr (namebuffer, '.')))
6024 *separator = '$';
6025 name = get_identifier (namebuffer);
6026 decl_result = IDENTIFIER_CLASS_VALUE (name);
6028 } while (!decl_result && separator);
6030 return decl_result;
6033 static tree
6034 qualify_and_find (tree class_type, tree package, tree name)
6036 tree new_qualified = merge_qualified_name (package, name);
6037 tree new_class_decl;
6039 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
6040 load_class (new_qualified, 0);
6041 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
6043 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
6044 load_class (TREE_TYPE (new_class_decl), 0);
6045 TYPE_NAME (class_type) = new_qualified;
6046 return IDENTIFIER_CLASS_VALUE (new_qualified);
6048 return NULL_TREE;
6051 /* Resolve NAME and lay it out (if not done and if not the current
6052 parsed class). Return a decl node. This function is meant to be
6053 called when type resolution is necessary during the walk pass. */
6055 static tree
6056 resolve_and_layout (tree something, tree cl)
6058 tree decl, decl_type;
6060 /* Don't do that on the current class */
6061 if (something == current_class)
6062 return TYPE_NAME (current_class);
6064 /* Don't do anything for void and other primitive types */
6065 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6066 return NULL_TREE;
6068 /* Pointer types can be reall pointer types or fake pointers. When
6069 finding a real pointer, recheck for primitive types */
6070 if (TREE_CODE (something) == POINTER_TYPE)
6072 if (TREE_TYPE (something))
6074 something = TREE_TYPE (something);
6075 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6076 return NULL_TREE;
6078 else
6079 something = TYPE_NAME (something);
6082 /* Don't do anything for arrays of primitive types */
6083 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
6084 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
6085 return NULL_TREE;
6087 /* Something might be a WFL */
6088 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
6089 something = EXPR_WFL_NODE (something);
6091 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
6092 TYPE_DECL or a real TYPE */
6093 else if (TREE_CODE (something) != IDENTIFIER_NODE)
6094 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
6095 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
6097 if (!(decl = resolve_no_layout (something, cl)))
6098 return NULL_TREE;
6100 /* Resolve and layout if necessary */
6101 decl_type = TREE_TYPE (decl);
6102 layout_class_methods (decl_type);
6103 /* Check methods */
6104 if (CLASS_FROM_SOURCE_P (decl_type))
6105 java_check_methods (decl);
6106 /* Layout the type if necessary */
6107 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6108 safe_layout_class (decl_type);
6110 return decl;
6113 /* Resolve a class, returns its decl but doesn't perform any
6114 layout. The current parsing context is saved and restored */
6116 static tree
6117 resolve_no_layout (tree name, tree cl)
6119 tree ptr, decl;
6120 BUILD_PTR_FROM_NAME (ptr, name);
6121 java_parser_context_save_global ();
6122 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6123 java_parser_context_restore_global ();
6125 return decl;
6128 /* Called when reporting errors. Skip the '[]'s in a complex array
6129 type description that failed to be resolved. purify_type_name can't
6130 use an identifier tree. */
6132 static const char *
6133 purify_type_name (const char *name)
6135 int len = strlen (name);
6136 int bracket_found;
6138 STRING_STRIP_BRACKETS (name, len, bracket_found);
6139 if (bracket_found)
6141 char *stripped_name = xmemdup (name, len, len+1);
6142 stripped_name [len] = '\0';
6143 return stripped_name;
6145 return name;
6148 /* The type CURRENT refers to can't be found. We print error messages. */
6150 static void
6151 complete_class_report_errors (jdep *dep)
6153 const char *name;
6155 if (!JDEP_WFL (dep))
6156 return;
6158 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6159 switch (JDEP_KIND (dep))
6161 case JDEP_SUPER:
6162 parse_error_context
6163 (JDEP_WFL (dep), "Superclass %qs of class %qs not found",
6164 purify_type_name (name),
6165 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6166 break;
6167 case JDEP_FIELD:
6168 parse_error_context
6169 (JDEP_WFL (dep), "Type %qs not found in declaration of field %qs",
6170 purify_type_name (name),
6171 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6172 break;
6173 case JDEP_METHOD: /* Covers arguments */
6174 parse_error_context
6175 (JDEP_WFL (dep), "Type %qs not found in the declaration of the argument %qs of method %qs",
6176 purify_type_name (name),
6177 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6178 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6179 break;
6180 case JDEP_METHOD_RETURN: /* Covers return type */
6181 parse_error_context
6182 (JDEP_WFL (dep), "Type %qs not found in the declaration of the return type of method %qs",
6183 purify_type_name (name),
6184 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6185 break;
6186 case JDEP_INTERFACE:
6187 parse_error_context
6188 (JDEP_WFL (dep), "Superinterface %qs of %s %qs not found",
6189 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6190 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6191 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6192 break;
6193 case JDEP_VARIABLE:
6194 parse_error_context
6195 (JDEP_WFL (dep), "Type %qs not found in the declaration of the local variable %qs",
6196 purify_type_name (IDENTIFIER_POINTER
6197 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6198 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6199 break;
6200 case JDEP_EXCEPTION: /* As specified by `throws' */
6201 parse_error_context
6202 (JDEP_WFL (dep), "Class %qs not found in %<throws%>",
6203 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6204 break;
6205 default:
6206 /* Fix for -Wall. Just break doing nothing. The error will be
6207 caught later */
6208 break;
6212 /* Return a static string containing the DECL prototype string. If
6213 DECL is a constructor, use the class name instead of the form
6214 <init> */
6216 static const char *
6217 get_printable_method_name (tree decl)
6219 const char *to_return;
6220 tree name = NULL_TREE;
6222 if (DECL_CONSTRUCTOR_P (decl))
6224 name = DECL_NAME (decl);
6225 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6228 to_return = lang_printable_name (decl, 2);
6229 if (DECL_CONSTRUCTOR_P (decl))
6230 DECL_NAME (decl) = name;
6232 return to_return;
6235 /* Track method being redefined inside the same class. As a side
6236 effect, set DECL_NAME to an IDENTIFIER (prior entering this
6237 function it's a FWL, so we can track errors more accurately.) */
6239 static int
6240 check_method_redefinition (tree class, tree method)
6242 tree redef, sig;
6244 /* There's no need to verify <clinit> and finit$ and instinit$ */
6245 if (DECL_CLINIT_P (method)
6246 || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6247 return 0;
6249 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6250 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6252 if (redef == method)
6253 break;
6254 if (DECL_NAME (redef) == DECL_NAME (method)
6255 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6256 && !DECL_ARTIFICIAL (method))
6258 parse_error_context
6259 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration %qs",
6260 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6261 get_printable_method_name (redef));
6262 return 1;
6265 return 0;
6268 /* Return 1 if check went ok, 0 otherwise. */
6269 static int
6270 check_abstract_method_definitions (int do_interface, tree class_decl,
6271 tree type)
6273 tree class = TREE_TYPE (class_decl);
6274 tree method, end_type;
6275 int ok = 1;
6277 end_type = (do_interface ? object_type_node : type);
6278 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6280 tree other_super, other_method, method_sig, method_name;
6281 int found = 0;
6282 int end_type_reached = 0;
6284 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6285 continue;
6287 /* Now verify that somewhere in between TYPE and CLASS,
6288 abstract method METHOD gets a non abstract definition
6289 that is inherited by CLASS. */
6291 method_sig = build_java_signature (TREE_TYPE (method));
6292 method_name = DECL_NAME (method);
6293 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6294 method_name = EXPR_WFL_NODE (method_name);
6296 other_super = class;
6297 do {
6298 if (other_super == end_type)
6299 end_type_reached = 1;
6301 /* Method search */
6302 for (other_method = TYPE_METHODS (other_super); other_method;
6303 other_method = TREE_CHAIN (other_method))
6305 tree s = build_java_signature (TREE_TYPE (other_method));
6306 tree other_name = DECL_NAME (other_method);
6308 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6309 other_name = EXPR_WFL_NODE (other_name);
6310 if (!DECL_CLINIT_P (other_method)
6311 && !DECL_CONSTRUCTOR_P (other_method)
6312 && method_name == other_name
6313 && method_sig == s
6314 && !METHOD_ABSTRACT (other_method))
6316 found = 1;
6317 break;
6320 other_super = CLASSTYPE_SUPER (other_super);
6321 } while (!end_type_reached);
6323 /* Report that abstract METHOD didn't find an implementation
6324 that CLASS can use. */
6325 if (!found)
6327 char *t = xstrdup (lang_printable_name
6328 (TREE_TYPE (TREE_TYPE (method)), 0));
6329 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6331 parse_error_context
6332 (lookup_cl (class_decl),
6333 "Class %qs doesn't define the abstract method %<%s %s%> from %s %<%s%>. This method must be defined or %s %qs must be declared abstract",
6334 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6335 t, lang_printable_name (method, 2),
6336 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6337 "interface" : "class"),
6338 IDENTIFIER_POINTER (ccn),
6339 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6340 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6341 ok = 0;
6342 free (t);
6346 if (ok && do_interface)
6348 /* Check for implemented interfaces. */
6349 int i;
6350 tree base_binfo;
6352 for (i = 1;
6353 ok && BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo);
6354 i++)
6355 ok = check_abstract_method_definitions (1, class_decl,
6356 BINFO_TYPE (base_binfo));
6359 return ok;
6362 /* Check that CLASS_DECL somehow implements all inherited abstract
6363 methods. */
6365 static void
6366 java_check_abstract_method_definitions (tree class_decl)
6368 tree class = TREE_TYPE (class_decl);
6369 tree super, base_binfo;
6370 int i;
6372 if (CLASS_ABSTRACT (class_decl))
6373 return;
6375 /* Check for inherited types */
6376 super = class;
6377 do {
6378 super = CLASSTYPE_SUPER (super);
6379 check_abstract_method_definitions (0, class_decl, super);
6380 } while (super != object_type_node);
6382 /* Check for implemented interfaces. */
6383 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6384 check_abstract_method_definitions (1, class_decl, BINFO_TYPE (base_binfo));
6387 /* Check all the types method DECL uses and return 1 if all of them
6388 are now complete, 0 otherwise. This is used to check whether its
6389 safe to build a method signature or not. */
6391 static int
6392 check_method_types_complete (tree decl)
6394 tree type = TREE_TYPE (decl);
6395 tree args;
6397 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6398 return 0;
6400 args = TYPE_ARG_TYPES (type);
6401 if (TREE_CODE (type) == METHOD_TYPE)
6402 args = TREE_CHAIN (args);
6403 for (; args != end_params_node; args = TREE_CHAIN (args))
6404 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6405 return 0;
6407 return 1;
6410 /* Visible interface to check methods contained in CLASS_DECL */
6412 void
6413 java_check_methods (tree class_decl)
6415 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6416 return;
6418 if (CLASS_INTERFACE (class_decl))
6419 java_check_abstract_methods (class_decl);
6420 else
6421 java_check_regular_methods (class_decl);
6423 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6426 /* Like not_accessible_p, but doesn't refer to the current class at
6427 all. */
6428 static bool
6429 hack_is_accessible_p (tree member, tree from_where)
6431 int flags = get_access_flags_from_decl (member);
6433 if (from_where == DECL_CONTEXT (member)
6434 || (flags & ACC_PUBLIC))
6435 return true;
6437 if ((flags & ACC_PROTECTED))
6439 if (inherits_from_p (from_where, DECL_CONTEXT (member)))
6440 return true;
6443 if ((flags & ACC_PRIVATE))
6444 return false;
6446 /* Package private, or protected. */
6447 return in_same_package (TYPE_NAME (from_where),
6448 TYPE_NAME (DECL_CONTEXT (member)));
6451 /* Check all the methods of CLASS_DECL. Methods are first completed
6452 then checked according to regular method existence rules. If no
6453 constructor for CLASS_DECL were encountered, then build its
6454 declaration. */
6455 static void
6456 java_check_regular_methods (tree class_decl)
6458 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6459 tree method;
6460 tree class = TREE_TYPE (class_decl);
6461 tree found = NULL_TREE;
6462 tree mthrows;
6464 /* It is not necessary to check methods defined in java.lang.Object */
6465 if (class == object_type_node)
6466 return;
6468 if (!TYPE_NVIRTUALS (class))
6469 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6471 /* Should take interfaces into account. FIXME */
6472 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6474 tree sig;
6475 tree method_wfl = DECL_FUNCTION_WFL (method);
6476 int aflags;
6478 /* Check for redefinitions */
6479 if (check_method_redefinition (class, method))
6480 continue;
6482 /* We verify things thrown by the method. They must inherit from
6483 java.lang.Throwable. */
6484 for (mthrows = DECL_FUNCTION_THROWS (method);
6485 mthrows; mthrows = TREE_CHAIN (mthrows))
6487 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6488 parse_error_context
6489 (TREE_PURPOSE (mthrows), "Class %qs in %<throws%> clause must be a subclass of class %<java.lang.Throwable%>",
6490 IDENTIFIER_POINTER
6491 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6494 /* If we see one constructor a mark so we don't generate the
6495 default one. Also skip other verifications: constructors
6496 can't be inherited hence hidden or overridden. */
6497 if (DECL_CONSTRUCTOR_P (method))
6499 saw_constructor = 1;
6500 continue;
6503 sig = build_java_argument_signature (TREE_TYPE (method));
6504 found = lookup_argument_method_generic (class, DECL_NAME (method), sig,
6505 SEARCH_SUPER | SEARCH_INTERFACE);
6507 /* Inner class can't declare static methods */
6508 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6510 char *t = xstrdup (lang_printable_name (class, 0));
6511 parse_error_context
6512 (method_wfl, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
6513 lang_printable_name (method, 2), t);
6514 free (t);
6517 /* Nothing overrides or it's a private method. */
6518 if (!found)
6519 continue;
6520 if (METHOD_PRIVATE (found))
6522 found = NULL_TREE;
6523 continue;
6526 /* If `found' is declared in an interface, make sure the
6527 modifier matches. */
6528 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6529 && clinit_identifier_node != DECL_NAME (found)
6530 && !METHOD_PUBLIC (method))
6532 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6533 parse_error_context (method_wfl, "Class %qs must override %qs with a public method in order to implement interface %qs",
6534 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6535 lang_printable_name (method, 2),
6536 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6539 /* Can't override a method with the same name and different return
6540 types. */
6541 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6543 char *t = xstrdup
6544 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6545 parse_error_context
6546 (method_wfl,
6547 "Method %qs was defined with return type %qs in class %qs",
6548 lang_printable_name (found, 2), t,
6549 IDENTIFIER_POINTER
6550 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6551 free (t);
6554 aflags = get_access_flags_from_decl (found);
6556 /* Can't override final. Can't override static. */
6557 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6559 /* Static *can* override static */
6560 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6561 continue;
6562 parse_error_context
6563 (method_wfl,
6564 "%s methods can't be overridden. Method %qs is %s in class %qs",
6565 (METHOD_FINAL (found) ? "Final" : "Static"),
6566 lang_printable_name (found, 2),
6567 (METHOD_FINAL (found) ? "final" : "static"),
6568 IDENTIFIER_POINTER
6569 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6570 continue;
6573 /* Static method can't override instance method. */
6574 if (METHOD_STATIC (method))
6576 parse_error_context
6577 (method_wfl,
6578 "Instance methods can't be overridden by a static method. Method %qs is an instance method in class %qs",
6579 lang_printable_name (found, 2),
6580 IDENTIFIER_POINTER
6581 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6582 continue;
6585 /* - Overriding/hiding public must be public
6586 - Overriding/hiding protected must be protected or public
6587 - If the overridden or hidden method has default (package)
6588 access, then the overriding or hiding method must not be
6589 private; otherwise, a compile-time error occurs. If
6590 `found' belongs to an interface, things have been already
6591 taken care of. */
6592 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6593 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6594 || (METHOD_PROTECTED (found)
6595 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6596 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6597 && METHOD_PRIVATE (method))))
6599 parse_error_context
6600 (method_wfl,
6601 "Methods can't be overridden to be more private. Method %qs is not %s in class %qs", lang_printable_name (method, 2),
6602 (METHOD_PUBLIC (method) ? "public" :
6603 (METHOD_PRIVATE (method) ? "private" : "protected")),
6604 IDENTIFIER_POINTER (DECL_NAME
6605 (TYPE_NAME (DECL_CONTEXT (found)))));
6606 continue;
6609 /* Check this method against all the other implementations it
6610 overrides. Here we only check the class hierarchy; the rest
6611 of the checking is done later. If this method is just a
6612 Miranda method, we can skip the check. */
6613 if (! METHOD_INVISIBLE (method))
6614 check_concrete_throws_clauses (class, method, DECL_NAME (method), sig);
6617 /* The above throws clause check only looked at superclasses. Now
6618 we must also make sure that all methods declared in interfaces
6619 have compatible throws clauses. FIXME: there are more efficient
6620 ways to organize this checking; we should implement one. */
6621 check_interface_throws_clauses (class, class);
6623 if (!TYPE_NVIRTUALS (class))
6624 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6626 /* Search for inherited abstract method not yet implemented in this
6627 class. */
6628 java_check_abstract_method_definitions (class_decl);
6630 if (!saw_constructor)
6631 abort ();
6634 /* Check to make sure that all the methods in all the interfaces
6635 implemented by CLASS_DECL are compatible with the concrete
6636 implementations available in CHECK_CLASS_DECL. */
6637 static void
6638 check_interface_throws_clauses (tree check_class_decl, tree class_decl)
6640 for (; class_decl != NULL_TREE; class_decl = CLASSTYPE_SUPER (class_decl))
6642 int i;
6644 if (! CLASS_LOADED_P (class_decl))
6646 if (CLASS_FROM_SOURCE_P (class_decl))
6647 safe_layout_class (class_decl);
6648 else
6649 load_class (class_decl, 1);
6652 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (class_decl)) - 1; i > 0; --i)
6654 tree interface
6655 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (class_decl), i));
6656 tree iface_method;
6658 for (iface_method = TYPE_METHODS (interface);
6659 iface_method != NULL_TREE;
6660 iface_method = TREE_CHAIN (iface_method))
6662 tree sig, method;
6664 /* First look for a concrete method implemented or
6665 inherited by this class. No need to search
6666 interfaces here, since we're already looking through
6667 all of them. */
6668 sig = build_java_argument_signature (TREE_TYPE (iface_method));
6669 method
6670 = lookup_argument_method_generic (check_class_decl,
6671 DECL_NAME (iface_method),
6672 sig, SEARCH_VISIBLE);
6673 /* If we don't find an implementation, that is ok. Any
6674 potential errors from that are diagnosed elsewhere.
6675 Also, multiple inheritance with conflicting throws
6676 clauses is fine in the absence of a concrete
6677 implementation. */
6678 if (method != NULL_TREE && !METHOD_ABSTRACT (method)
6679 && !METHOD_INVISIBLE (iface_method))
6681 tree method_wfl = DECL_FUNCTION_WFL (method);
6682 check_throws_clauses (method, method_wfl, iface_method);
6686 /* Now check superinterfaces. */
6687 check_interface_throws_clauses (check_class_decl, interface);
6692 /* Check throws clauses of a method against the clauses of all the
6693 methods it overrides. We do this by searching up the class
6694 hierarchy, examining all matching accessible methods. */
6695 static void
6696 check_concrete_throws_clauses (tree class, tree self_method,
6697 tree name, tree signature)
6699 tree method = lookup_argument_method_generic (class, name, signature,
6700 SEARCH_SUPER | SEARCH_VISIBLE);
6701 while (method != NULL_TREE)
6703 if (! METHOD_INVISIBLE (method) && hack_is_accessible_p (method, class))
6704 check_throws_clauses (self_method, DECL_FUNCTION_WFL (self_method),
6705 method);
6707 method = lookup_argument_method_generic (DECL_CONTEXT (method),
6708 name, signature,
6709 SEARCH_SUPER | SEARCH_VISIBLE);
6713 /* Generate an error if the `throws' clause of METHOD (if any) is
6714 incompatible with the `throws' clause of FOUND (if any). */
6715 static void
6716 check_throws_clauses (tree method, tree method_wfl, tree found)
6718 tree mthrows;
6720 for (mthrows = DECL_FUNCTION_THROWS (method);
6721 mthrows; mthrows = TREE_CHAIN (mthrows))
6723 tree fthrows;
6725 /* We don't verify unchecked expressions */
6726 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6727 continue;
6728 /* Checked expression must be compatible */
6729 for (fthrows = DECL_FUNCTION_THROWS (found);
6730 fthrows; fthrows = TREE_CHAIN (fthrows))
6732 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6733 break;
6735 if (!fthrows)
6737 parse_error_context
6738 (method_wfl, "Invalid checked exception class %qs in %<throws%> clause. The exception must be a subclass of an exception thrown by %qs from class %qs",
6739 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6740 lang_printable_name (found, 2),
6741 IDENTIFIER_POINTER
6742 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6747 /* Check abstract method of interface INTERFACE */
6748 static void
6749 java_check_abstract_methods (tree interface_decl)
6751 int i;
6752 tree method, found;
6753 tree interface = TREE_TYPE (interface_decl);
6754 tree base_binfo;
6756 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6758 /* 2- Check for double definition inside the defining interface */
6759 if (check_method_redefinition (interface, method))
6760 continue;
6762 /* 3- Overriding is OK as far as we preserve the return type. */
6763 found = lookup_java_interface_method2 (interface, method);
6764 if (found)
6766 char *t;
6767 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6768 parse_error_context
6769 (DECL_FUNCTION_WFL (found),
6770 "Method %qs was defined with return type %qs in class %qs",
6771 lang_printable_name (found, 2), t,
6772 IDENTIFIER_POINTER
6773 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6774 free (t);
6775 continue;
6779 /* 4- Inherited methods can't differ by their returned types */
6780 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (interface), i, base_binfo); i++)
6782 tree sub_interface_method, sub_interface;
6784 sub_interface = BINFO_TYPE (base_binfo);
6785 for (sub_interface_method = TYPE_METHODS (sub_interface);
6786 sub_interface_method;
6787 sub_interface_method = TREE_CHAIN (sub_interface_method))
6789 found = lookup_java_interface_method2 (interface,
6790 sub_interface_method);
6791 if (found && (found != sub_interface_method))
6793 parse_error_context
6794 (lookup_cl (sub_interface_method),
6795 "Interface %qs inherits method %qs from interface %qs. This method is redefined with a different return type in interface %qs",
6796 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6797 lang_printable_name (found, 2),
6798 IDENTIFIER_POINTER
6799 (DECL_NAME (TYPE_NAME
6800 (DECL_CONTEXT (sub_interface_method)))),
6801 IDENTIFIER_POINTER
6802 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6808 /* Lookup methods in interfaces using their name and partial
6809 signature. Return a matching method only if their types differ. */
6811 static tree
6812 lookup_java_interface_method2 (tree class, tree method_decl)
6814 int i;
6815 tree base_binfo;
6816 tree to_return;
6818 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6820 if ((BINFO_TYPE (base_binfo) != object_type_node)
6821 && (to_return =
6822 lookup_java_method2 (BINFO_TYPE (base_binfo), method_decl, 1)))
6823 return to_return;
6825 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6827 to_return = lookup_java_interface_method2
6828 (BINFO_TYPE (base_binfo), method_decl);
6829 if (to_return)
6830 return to_return;
6833 return NULL_TREE;
6836 /* Lookup method using their name and partial signature. Return a
6837 matching method only if their types differ. */
6839 static tree
6840 lookup_java_method2 (tree clas, tree method_decl, int do_interface)
6842 tree method, method_signature, method_name, method_type, name;
6844 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6845 name = DECL_NAME (method_decl);
6846 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6847 EXPR_WFL_NODE (name) : name);
6848 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6850 while (clas != NULL_TREE)
6852 for (method = TYPE_METHODS (clas);
6853 method != NULL_TREE; method = TREE_CHAIN (method))
6855 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6856 tree name = DECL_NAME (method);
6857 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6858 EXPR_WFL_NODE (name) : name) == method_name
6859 && method_sig == method_signature
6860 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6861 return method;
6863 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6865 return NULL_TREE;
6868 /* Return the line that matches DECL line number, and try its best to
6869 position the column number. Used during error reports.
6870 FUTURE/FIXME: return source_location instead of node. */
6872 static GTY(()) tree cl_v;
6873 static tree
6874 lookup_cl (tree decl)
6876 #ifndef USE_MAPPED_LOCATION
6877 char *line, *found;
6878 #endif
6880 if (!decl)
6881 return NULL_TREE;
6883 if (cl_v == NULL_TREE)
6885 cl_v = build_unknown_wfl (NULL_TREE);
6888 #ifdef USE_MAPPED_LOCATION
6889 SET_EXPR_LOCATION (cl_v, DECL_SOURCE_LOCATION (decl));
6890 #else
6891 EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6892 EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE (decl), -1);
6894 line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6895 EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6897 found = strstr ((const char *)line,
6898 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6899 if (found)
6900 EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6901 #endif
6903 return cl_v;
6906 /* Look for a simple name in the single-type import list */
6908 static tree
6909 find_name_in_single_imports (tree name)
6911 tree node;
6913 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6914 if (TREE_VALUE (node) == name)
6915 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6917 return NULL_TREE;
6920 /* Process all single-type import. */
6922 static int
6923 process_imports (void)
6925 tree import;
6926 int error_found;
6928 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6930 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6931 char *original_name;
6933 /* Don't load twice something already defined. */
6934 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6935 continue;
6937 original_name = xmemdup (IDENTIFIER_POINTER (to_be_found),
6938 IDENTIFIER_LENGTH (to_be_found),
6939 IDENTIFIER_LENGTH (to_be_found) + 1);
6941 while (1)
6943 tree left;
6945 QUALIFIED_P (to_be_found) = 1;
6946 load_class (to_be_found, 0);
6947 error_found =
6948 check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true, NULL_TREE);
6950 /* We found it, we can bail out */
6951 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6953 check_deprecation (TREE_PURPOSE (import),
6954 IDENTIFIER_CLASS_VALUE (to_be_found));
6955 break;
6958 /* We haven't found it. Maybe we're trying to access an
6959 inner class. The only way for us to know is to try again
6960 after having dropped a qualifier. If we can't break it further,
6961 we have an error. */
6962 if (split_qualified_name (&left, NULL, to_be_found))
6963 break;
6965 to_be_found = left;
6967 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6969 parse_error_context (TREE_PURPOSE (import),
6970 "Class or interface %qs not found in import",
6971 original_name);
6972 error_found = 1;
6975 free (original_name);
6976 if (error_found)
6977 return 1;
6979 return 0;
6982 /* Possibly find and mark a class imported by a single-type import
6983 statement. */
6985 static void
6986 find_in_imports (tree enclosing_type, tree class_type)
6988 tree import;
6989 if (enclosing_type && TYPE_IMPORT_LIST (enclosing_type))
6990 import = TYPE_IMPORT_LIST (enclosing_type);
6991 else
6992 import = ctxp->import_list;
6994 while (import)
6996 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6998 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6999 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
7000 return;
7002 import = TREE_CHAIN (import);
7006 static int
7007 note_possible_classname (const char *name, int len)
7009 tree node;
7010 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
7011 len = len - 5;
7012 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
7013 len = len - 6;
7014 else
7015 return 0;
7016 node = ident_subst (name, len, "", '/', '.', "");
7017 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
7018 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
7019 return 1;
7022 /* Read a import directory, gathering potential match for further type
7023 references. Indifferently reads a filesystem or a ZIP archive
7024 directory. */
7026 static void
7027 read_import_dir (tree wfl)
7029 tree package_id = EXPR_WFL_NODE (wfl);
7030 const char *package_name = IDENTIFIER_POINTER (package_id);
7031 int package_length = IDENTIFIER_LENGTH (package_id);
7032 DIR *dirp = NULL;
7033 JCF *saved_jcf = current_jcf;
7035 int found = 0;
7036 int k;
7037 void *entry;
7038 struct buffer filename[1];
7040 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
7041 return;
7042 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
7044 BUFFER_INIT (filename);
7045 buffer_grow (filename, package_length + 100);
7047 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
7049 const char *entry_name = jcf_path_name (entry);
7050 int entry_length = strlen (entry_name);
7051 if (jcf_path_is_zipfile (entry))
7053 ZipFile *zipf;
7054 buffer_grow (filename, entry_length);
7055 memcpy (filename->data, entry_name, entry_length - 1);
7056 filename->data[entry_length-1] = '\0';
7057 zipf = opendir_in_zip ((const char *) filename->data, jcf_path_is_system (entry));
7058 if (zipf == NULL)
7059 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
7060 else
7062 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
7063 BUFFER_RESET (filename);
7064 for (k = 0; k < package_length; k++)
7066 char ch = package_name[k];
7067 *filename->ptr++ = ch == '.' ? '/' : ch;
7069 *filename->ptr++ = '/';
7071 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
7073 const char *current_entry = ZIPDIR_FILENAME (zipd);
7074 int current_entry_len = zipd->filename_length;
7076 if (current_entry_len >= BUFFER_LENGTH (filename)
7077 && strncmp ((const char *) filename->data, current_entry,
7078 BUFFER_LENGTH (filename)) != 0)
7079 continue;
7080 found |= note_possible_classname (current_entry,
7081 current_entry_len);
7085 else
7087 BUFFER_RESET (filename);
7088 buffer_grow (filename, entry_length + package_length + 4);
7089 strcpy ((char *) filename->data, entry_name);
7090 filename->ptr = filename->data + entry_length;
7091 for (k = 0; k < package_length; k++)
7093 char ch = package_name[k];
7094 *filename->ptr++ = ch == '.' ? '/' : ch;
7096 *filename->ptr = '\0';
7098 dirp = opendir ((const char *) filename->data);
7099 if (dirp == NULL)
7100 continue;
7101 *filename->ptr++ = '/';
7102 for (;;)
7104 int len;
7105 const char *d_name;
7106 struct dirent *direntp = readdir (dirp);
7107 if (!direntp)
7108 break;
7109 d_name = direntp->d_name;
7110 len = strlen (direntp->d_name);
7111 buffer_grow (filename, len+1);
7112 strcpy ((char *) filename->ptr, d_name);
7113 found |= note_possible_classname ((const char *) filename->data + entry_length,
7114 package_length+len+1);
7116 if (dirp)
7117 closedir (dirp);
7121 free (filename->data);
7123 /* Here we should have a unified way of retrieving an entry, to be
7124 indexed. */
7125 if (!found)
7127 static int first = 1;
7128 if (first)
7130 error ("Can't find default package %qs. Check the CLASSPATH environment variable and the access to the archives", package_name);
7131 java_error_count++;
7132 first = 0;
7134 else
7135 parse_error_context (wfl, "Package %qs not found in import",
7136 package_name);
7137 current_jcf = saved_jcf;
7138 return;
7140 current_jcf = saved_jcf;
7143 /* Possibly find a type in the import on demands specified
7144 types. Returns 1 if an error occurred, 0 otherwise. Run through the
7145 entire list, to detected potential double definitions. */
7147 static int
7148 find_in_imports_on_demand (tree enclosing_type, tree class_type)
7150 tree class_type_name = TYPE_NAME (class_type);
7151 tree cl = NULL_TREE;
7152 int seen_once = -1; /* -1 when not set, 1 if seen once, >1 otherwise. */
7153 int to_return = -1; /* -1 when not set, 0 or 1 otherwise */
7154 tree node;
7155 tree import;
7157 if (enclosing_type && TYPE_IMPORT_DEMAND_LIST (enclosing_type))
7158 import = TYPE_IMPORT_DEMAND_LIST (enclosing_type);
7159 else
7160 import = ctxp->import_demand_list;
7162 for (; import; import = TREE_CHAIN (import))
7164 location_t saved_location = input_location;
7165 int access_check;
7166 const char *id_name;
7167 tree decl, type_name_copy;
7169 obstack_grow (&temporary_obstack,
7170 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
7171 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
7172 obstack_1grow (&temporary_obstack, '.');
7173 obstack_grow0 (&temporary_obstack,
7174 IDENTIFIER_POINTER (class_type_name),
7175 IDENTIFIER_LENGTH (class_type_name));
7176 id_name = obstack_finish (&temporary_obstack);
7178 if (! (node = maybe_get_identifier (id_name)))
7179 continue;
7181 /* Setup input_line so that it refers to the line of the import (in
7182 case we parse a class file and encounter errors */
7183 #ifdef USE_MAPPED_LOCATION
7184 input_location = EXPR_LOCATION (TREE_PURPOSE (import));
7185 #else
7186 input_line = EXPR_WFL_LINENO (TREE_PURPOSE (import));
7187 #endif
7189 type_name_copy = TYPE_NAME (class_type);
7190 TYPE_NAME (class_type) = node;
7191 QUALIFIED_P (node) = 1;
7192 decl = IDENTIFIER_CLASS_VALUE (node);
7193 access_check = -1;
7194 /* If there is no DECL set for the class or if the class isn't
7195 loaded and not seen in source yet, then load */
7196 if (!decl || ! CLASS_LOADED_P (TREE_TYPE (decl)))
7198 load_class (node, 0);
7199 decl = IDENTIFIER_CLASS_VALUE (node);
7201 if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7202 access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7203 false, NULL_TREE);
7204 else
7205 /* 6.6.1: Inner classes are subject to member access rules. */
7206 access_check = 0;
7208 input_location = saved_location;
7210 /* If the loaded class is not accessible or couldn't be loaded,
7211 we restore the original TYPE_NAME and process the next
7212 import. */
7213 if (access_check || !decl)
7215 TYPE_NAME (class_type) = type_name_copy;
7216 continue;
7219 /* If the loaded class is accessible, we keep a tab on it to
7220 detect and report multiple inclusions. */
7221 if (IS_A_CLASSFILE_NAME (node))
7223 if (seen_once < 0)
7225 cl = TREE_PURPOSE (import);
7226 seen_once = 1;
7228 else if (seen_once >= 0)
7230 tree location = (cl ? cl : TREE_PURPOSE (import));
7231 tree package = (cl ? EXPR_WFL_NODE (cl) :
7232 EXPR_WFL_NODE (TREE_PURPOSE (import)));
7233 seen_once++;
7234 parse_error_context
7235 (location,
7236 "Type %qs also potentially defined in package %qs",
7237 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7238 IDENTIFIER_POINTER (package));
7241 to_return = access_check;
7244 if (seen_once == 1)
7245 return to_return;
7246 else
7247 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7250 /* Add package NAME to the list of packages encountered so far. To
7251 speed up class lookup in do_resolve_class, we make sure a
7252 particular package is added only once. */
7254 static void
7255 register_package (tree name)
7257 static htab_t pht;
7258 void **e;
7260 if (pht == NULL)
7261 pht = htab_create (50, htab_hash_pointer, htab_eq_pointer, NULL);
7263 e = htab_find_slot (pht, name, INSERT);
7264 if (*e == NULL)
7266 package_list = chainon (package_list, build_tree_list (name, NULL));
7267 *e = name;
7271 static tree
7272 resolve_package (tree pkg, tree *next, tree *type_name)
7274 tree current;
7275 tree decl = NULL_TREE;
7276 *type_name = NULL_TREE;
7278 /* The trick is to determine when the package name stops and were
7279 the name of something contained in the package starts. Then we
7280 return a fully qualified name of what we want to get. */
7282 *next = EXPR_WFL_QUALIFICATION (pkg);
7284 /* Try to progressively construct a type name */
7285 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7286 for (current = EXPR_WFL_QUALIFICATION (pkg);
7287 current; current = TREE_CHAIN (current))
7289 /* If we don't have what we're expecting, exit now. TYPE_NAME
7290 will be null and the error caught later. */
7291 if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7292 break;
7293 *type_name =
7294 merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7295 if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7297 /* resolve_package should be used in a loop, hence we
7298 point at this one to naturally process the next one at
7299 the next iteration. */
7300 *next = current;
7301 break;
7304 return decl;
7308 /* Check accessibility of inner classes according to member access rules.
7309 DECL is the inner class, ENCLOSING_DECL is the class from which the
7310 access is being attempted. */
7312 static void
7313 check_inner_class_access (tree decl, tree enclosing_decl, tree cl)
7315 const char *access;
7316 tree enclosing_decl_type;
7318 /* We don't issue an error message when CL is null. CL can be null
7319 as a result of processing a JDEP crafted by source_start_java_method
7320 for the purpose of patching its parm decl. But the error would
7321 have been already trapped when fixing the method's signature.
7322 DECL can also be NULL in case of earlier errors. */
7323 if (!decl || !cl)
7324 return;
7326 enclosing_decl_type = TREE_TYPE (enclosing_decl);
7328 if (CLASS_PRIVATE (decl))
7330 /* Access is permitted only within the body of the top-level
7331 class in which DECL is declared. */
7332 tree top_level = decl;
7333 while (DECL_CONTEXT (top_level))
7334 top_level = DECL_CONTEXT (top_level);
7335 while (DECL_CONTEXT (enclosing_decl))
7336 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7337 if (top_level == enclosing_decl)
7338 return;
7339 access = "private";
7341 else if (CLASS_PROTECTED (decl))
7343 tree decl_context;
7344 /* Access is permitted from within the same package... */
7345 if (in_same_package (decl, enclosing_decl))
7346 return;
7348 /* ... or from within the body of a subtype of the context in which
7349 DECL is declared. */
7350 decl_context = DECL_CONTEXT (decl);
7351 while (enclosing_decl)
7353 if (CLASS_INTERFACE (decl))
7355 if (interface_of_p (TREE_TYPE (decl_context),
7356 enclosing_decl_type))
7357 return;
7359 else
7361 /* Eww. The order of the arguments is different!! */
7362 if (inherits_from_p (enclosing_decl_type,
7363 TREE_TYPE (decl_context)))
7364 return;
7366 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7368 access = "protected";
7370 else if (! CLASS_PUBLIC (decl))
7372 /* Access is permitted only from within the same package as DECL. */
7373 if (in_same_package (decl, enclosing_decl))
7374 return;
7375 access = "non-public";
7377 else
7378 /* Class is public. */
7379 return;
7381 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7382 (CLASS_INTERFACE (decl) ? "interface" : "class"),
7383 lang_printable_name (decl, 2), access);
7386 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7387 foreign package, it must be PUBLIC. Return 0 if no access
7388 violations were found, 1 otherwise. If VERBOSE is true and an error
7389 was found, it is reported and accounted for. If CL is NULL then
7390 look it up with THIS_DECL. */
7392 static int
7393 check_pkg_class_access (tree class_name, tree cl, bool verbose, tree this_decl)
7395 tree type;
7397 if (!IDENTIFIER_CLASS_VALUE (class_name))
7398 return 0;
7400 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7401 return 0;
7403 if (!CLASS_PUBLIC (TYPE_NAME (type)))
7405 /* Access to a private class within the same package is
7406 allowed. */
7407 tree l, r;
7408 split_qualified_name (&l, &r, class_name);
7409 if (!QUALIFIED_P (class_name) && !ctxp->package)
7410 /* Both in the empty package. */
7411 return 0;
7412 if (l == ctxp->package)
7413 /* Both in the same package. */
7414 return 0;
7416 if (verbose)
7417 parse_error_context
7418 (cl == NULL ? lookup_cl (this_decl): cl,
7419 "Can't access %s %qs. Only public classes and interfaces in other packages can be accessed",
7420 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7421 IDENTIFIER_POINTER (class_name));
7422 return 1;
7424 return 0;
7427 /* Local variable declaration. */
7429 static void
7430 declare_local_variables (int modifier, tree type, tree vlist)
7432 tree decl, current, saved_type;
7433 tree type_wfl = NULL_TREE;
7434 int must_chain = 0;
7435 int final_p = 0;
7437 /* Push a new block if statements were seen between the last time we
7438 pushed a block and now. Keep a count of blocks to close */
7439 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7441 tree b = enter_block ();
7442 BLOCK_IS_IMPLICIT (b) = 1;
7445 if (modifier)
7447 size_t i;
7448 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7449 if (1 << i & modifier)
7450 break;
7451 if (modifier == ACC_FINAL)
7452 final_p = 1;
7453 else
7455 parse_error_context
7456 (ctxp->modifier_ctx [i],
7457 "Only %<final%> is allowed as a local variables modifier");
7458 return;
7462 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7463 hold the TYPE value if a new incomplete has to be created (as
7464 opposed to being found already existing and reused). */
7465 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7467 /* If TYPE is fully resolved and we don't have a reference, make one */
7468 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7470 /* Go through all the declared variables */
7471 for (current = vlist, saved_type = type; current;
7472 current = TREE_CHAIN (current), type = saved_type)
7474 tree other, real_type;
7475 tree wfl = TREE_PURPOSE (current);
7476 tree name = EXPR_WFL_NODE (wfl);
7477 tree init = TREE_VALUE (current);
7479 /* Process NAME, as it may specify extra dimension(s) for it */
7480 type = build_array_from_name (type, type_wfl, name, &name);
7482 /* Variable redefinition check */
7483 if ((other = lookup_name_in_blocks (name)))
7485 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7486 DECL_SOURCE_LINE (other));
7487 continue;
7490 /* Type adjustment. We may have just readjusted TYPE because
7491 the variable specified more dimensions. Make sure we have
7492 a reference if we can and don't have one already. */
7493 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7495 real_type = GET_REAL_TYPE (type);
7496 /* Never layout this decl. This will be done when its scope
7497 will be entered */
7498 decl = build_decl (VAR_DECL, name, real_type);
7499 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7500 DECL_FINAL (decl) = final_p;
7501 BLOCK_CHAIN_DECL (decl);
7503 /* Don't try to use an INIT statement when an error was found */
7504 if (init && java_error_count)
7505 init = NULL_TREE;
7507 /* Remember it if this is an initialized-upon-declaration final
7508 variable. */
7509 if (init && final_p)
7511 DECL_LOCAL_FINAL_IUD (decl) = 1;
7514 /* Add the initialization function to the current function's code */
7515 if (init)
7517 /* Name might have been readjusted */
7518 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7519 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7520 java_method_add_stmt (current_function_decl,
7521 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7522 init));
7525 /* Setup dependency the type of the decl */
7526 if (must_chain)
7528 jdep *dep;
7529 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7530 dep = CLASSD_LAST (ctxp->classd_list);
7531 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7534 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7537 /* Called during parsing. Build decls from argument list. */
7539 static void
7540 source_start_java_method (tree fndecl)
7542 tree tem;
7543 tree parm_decl;
7544 int i;
7546 if (!fndecl)
7547 return;
7549 current_function_decl = fndecl;
7551 /* New scope for the function */
7552 enter_block ();
7553 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7554 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7556 tree type = TREE_VALUE (tem);
7557 tree name = TREE_PURPOSE (tem);
7559 /* If type is incomplete. Create an incomplete decl and ask for
7560 the decl to be patched later */
7561 if (INCOMPLETE_TYPE_P (type))
7563 jdep *jdep;
7564 tree real_type = GET_REAL_TYPE (type);
7565 parm_decl = build_decl (PARM_DECL, name, real_type);
7566 type = obtain_incomplete_type (type);
7567 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7568 jdep = CLASSD_LAST (ctxp->classd_list);
7569 JDEP_MISC (jdep) = name;
7570 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7572 else
7573 parm_decl = build_decl (PARM_DECL, name, type);
7575 /* Remember if a local variable was declared final (via its
7576 TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7577 if (ARG_FINAL_P (tem))
7579 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7580 DECL_FINAL (parm_decl) = 1;
7583 BLOCK_CHAIN_DECL (parm_decl);
7585 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7586 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7587 nreverse (tem);
7588 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7589 DECL_MAX_LOCALS (current_function_decl) = i;
7592 /* Called during parsing. Creates an artificial method declaration. */
7594 static tree
7595 create_artificial_method (tree class, int flags, tree type,
7596 tree name, tree args)
7598 tree mdecl;
7599 location_t save_location = input_location;
7601 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (class));
7602 mdecl = make_node (FUNCTION_TYPE);
7603 TREE_TYPE (mdecl) = type;
7604 TYPE_ARG_TYPES (mdecl) = args;
7605 /* We used to compute the signature of MDECL here and then use
7606 add_method(), but that failed because our caller might modify
7607 the type of the returned method, which trashes the cache in
7608 get_type_from_signature(). */
7609 mdecl = add_method_1 (class, flags, name, mdecl);
7610 input_location = save_location;
7611 DECL_ARTIFICIAL (mdecl) = 1;
7612 return mdecl;
7615 /* Starts the body if an artificial method. */
7617 static void
7618 start_artificial_method_body (tree mdecl)
7620 #ifdef USE_MAPPED_LOCATION
7621 DECL_SOURCE_LOCATION (mdecl) = ctxp->file_start_location;
7622 DECL_FUNCTION_LAST_LINE (mdecl) = ctxp->file_start_location;
7623 #else
7624 DECL_SOURCE_LINE (mdecl) = 1;
7625 DECL_FUNCTION_LAST_LINE (mdecl) = 1;
7626 #endif
7627 source_start_java_method (mdecl);
7628 enter_block ();
7631 static void
7632 end_artificial_method_body (tree mdecl)
7634 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7635 It has to be evaluated first. (if mdecl is current_function_decl,
7636 we have an undefined behavior if no temporary variable is used.) */
7637 tree b = exit_block ();
7638 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7639 exit_block ();
7642 /* Dump a tree of some kind. This is a convenience wrapper for the
7643 dump_* functions in tree-dump.c. */
7644 static void
7645 dump_java_tree (enum tree_dump_index phase, tree t)
7647 FILE *stream;
7648 int flags;
7650 stream = dump_begin (phase, &flags);
7651 flags |= TDF_SLIM;
7652 if (stream)
7654 dump_node (t, flags, stream);
7655 dump_end (phase, stream);
7659 /* Terminate a function and expand its body. */
7661 static void
7662 source_end_java_method (void)
7664 tree fndecl = current_function_decl;
7666 if (!fndecl)
7667 return;
7669 java_parser_context_save_global ();
7670 #ifdef USE_MAPPED_LOCATION
7671 input_location = ctxp->last_ccb_indent1;
7672 #else
7673 input_line = ctxp->last_ccb_indent1;
7674 #endif
7676 /* Turn function bodies with only a NOP expr null, so they don't get
7677 generated at all and we won't get warnings when using the -W
7678 -Wall flags. */
7679 if (IS_EMPTY_STMT (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))))
7680 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7682 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7683 && ! flag_emit_class_files)
7684 finish_method (fndecl);
7686 current_function_decl = NULL_TREE;
7687 java_parser_context_restore_global ();
7688 current_function_decl = NULL_TREE;
7691 /* Record EXPR in the current function block. Complements compound
7692 expression second operand if necessary. */
7694 tree
7695 java_method_add_stmt (tree fndecl, tree expr)
7697 if (!GET_CURRENT_BLOCK (fndecl))
7698 return NULL_TREE;
7699 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7702 static tree
7703 add_stmt_to_block (tree b, tree type, tree stmt)
7705 tree body = BLOCK_EXPR_BODY (b), c;
7707 if (java_error_count)
7708 return body;
7710 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7711 return body;
7713 BLOCK_EXPR_BODY (b) = c;
7714 TREE_SIDE_EFFECTS (c) = 1;
7715 return c;
7718 /* Lays out the methods for the classes seen so far. */
7720 void
7721 java_layout_seen_class_methods (void)
7723 tree previous_list = all_class_list;
7724 tree end = NULL_TREE;
7725 tree current;
7727 while (1)
7729 for (current = previous_list;
7730 current != end; current = TREE_CHAIN (current))
7732 tree decl = TREE_VALUE (current);
7733 tree cls = TREE_TYPE (decl);
7735 input_location = DECL_SOURCE_LOCATION (decl);
7737 if (! CLASS_LOADED_P (cls))
7738 load_class (cls, 0);
7740 layout_class_methods (cls);
7743 /* Note that new classes might have been added while laying out
7744 methods, changing the value of all_class_list. */
7746 if (previous_list != all_class_list)
7748 end = previous_list;
7749 previous_list = all_class_list;
7751 else
7752 break;
7756 static GTY(()) tree stop_reordering;
7757 void
7758 java_reorder_fields (void)
7760 tree current;
7762 for (current = gclass_list; current; current = TREE_CHAIN (current))
7764 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7766 if (current_class == stop_reordering)
7767 break;
7769 /* Reverse the fields, but leave the dummy field in front.
7770 Fields are already ordered for Object and Class */
7771 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7772 && current_class != class_type_node)
7774 /* If the dummy field is there, reverse the right fields and
7775 just layout the type for proper fields offset */
7776 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7778 tree fields = TYPE_FIELDS (current_class);
7779 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7780 TYPE_SIZE (current_class) = NULL_TREE;
7782 /* We don't have a dummy field, we need to layout the class,
7783 after having reversed the fields */
7784 else
7786 TYPE_FIELDS (current_class) =
7787 nreverse (TYPE_FIELDS (current_class));
7788 TYPE_SIZE (current_class) = NULL_TREE;
7792 /* There are cases were gclass_list will be empty. */
7793 if (gclass_list)
7794 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7797 /* Layout the methods of all classes loaded in one way or another.
7798 Check methods of source parsed classes. Then reorder the
7799 fields and layout the classes or the type of all source parsed
7800 classes */
7802 void
7803 java_layout_classes (void)
7805 tree current;
7806 int save_error_count = java_error_count;
7808 /* Layout the methods of all classes seen so far */
7809 java_layout_seen_class_methods ();
7810 java_parse_abort_on_error ();
7811 all_class_list = NULL_TREE;
7813 /* Then check the methods of all parsed classes */
7814 for (current = gclass_list; current; current = TREE_CHAIN (current))
7815 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7816 java_check_methods (TREE_VALUE (current));
7817 java_parse_abort_on_error ();
7819 for (current = gclass_list; current; current = TREE_CHAIN (current))
7821 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7822 layout_class (current_class);
7824 /* Error reported by the caller */
7825 if (java_error_count)
7826 return;
7829 /* We might have reloaded classes durign the process of laying out
7830 classes for code generation. We must layout the methods of those
7831 late additions, as constructor checks might use them */
7832 java_layout_seen_class_methods ();
7833 java_parse_abort_on_error ();
7836 /* Expand methods in the current set of classes remembered for
7837 generation. */
7839 static void
7840 java_complete_expand_classes (void)
7842 tree current;
7844 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7845 if (!INNER_CLASS_DECL_P (current))
7846 java_complete_expand_class (current);
7849 /* Expand the methods found in OUTER, starting first by OUTER's inner
7850 classes, if any. */
7852 static void
7853 java_complete_expand_class (tree outer)
7855 tree inner_list;
7857 /* We need to go after all inner classes and start expanding them,
7858 starting with most nested ones. We have to do that because nested
7859 classes might add functions to outer classes */
7861 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7862 inner_list; inner_list = TREE_CHAIN (inner_list))
7863 java_complete_expand_class (TREE_PURPOSE (inner_list));
7865 java_complete_expand_methods (outer);
7868 /* Expand methods registered in CLASS_DECL. The general idea is that
7869 we expand regular methods first. This allows us get an estimate on
7870 how outer context local alias fields are really used so we can add
7871 to the constructor just enough code to initialize them properly (it
7872 also lets us generate finit$ correctly.) Then we expand the
7873 constructors and then <clinit>. */
7875 static void
7876 java_complete_expand_methods (tree class_decl)
7878 tree clinit, decl, first_decl;
7880 output_class = current_class = TREE_TYPE (class_decl);
7882 /* Pre-expand <clinit> to figure whether we really need it or
7883 not. If we do need it, we pre-expand the static fields so they're
7884 ready to be used somewhere else. <clinit> will be fully expanded
7885 after we processed the constructors. */
7886 first_decl = TYPE_METHODS (current_class);
7887 clinit = maybe_generate_pre_expand_clinit (current_class);
7889 /* Then generate finit$ (if we need to) because constructors will
7890 try to use it.*/
7891 if (TYPE_FINIT_STMT_LIST (current_class))
7892 java_complete_expand_method (generate_finit (current_class));
7894 /* Then generate instinit$ (if we need to) because constructors will
7895 try to use it. */
7896 if (TYPE_II_STMT_LIST (current_class))
7897 java_complete_expand_method (generate_instinit (current_class));
7899 /* Now do the constructors */
7900 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7902 if (!DECL_CONSTRUCTOR_P (decl))
7903 continue;
7904 java_complete_expand_method (decl);
7907 /* First, do the ordinary methods. */
7908 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7910 /* Ctors aren't part of this batch. */
7911 if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7912 continue;
7914 /* Skip abstract or native methods -- but do handle native
7915 methods when generating JNI stubs. */
7916 if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7918 DECL_FUNCTION_BODY (decl) = NULL_TREE;
7919 continue;
7922 if (METHOD_NATIVE (decl))
7924 tree body;
7925 current_function_decl = decl;
7926 body = build_jni_stub (decl);
7927 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7930 java_complete_expand_method (decl);
7933 /* If there is indeed a <clinit>, fully expand it now */
7934 if (clinit)
7936 /* Prevent the use of `this' inside <clinit> */
7937 ctxp->explicit_constructor_p = 1;
7938 java_complete_expand_method (clinit);
7939 ctxp->explicit_constructor_p = 0;
7942 /* We might have generated a class$ that we now want to expand */
7943 if (TYPE_DOT_CLASS (current_class))
7944 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7946 /* Now verify constructor circularity (stop after the first one we
7947 prove wrong.) */
7948 if (!CLASS_INTERFACE (class_decl))
7949 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7950 if (DECL_CONSTRUCTOR_P (decl)
7951 && verify_constructor_circularity (decl, decl))
7952 break;
7955 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7956 safely used in some other methods/constructors. */
7958 static tree
7959 maybe_generate_pre_expand_clinit (tree class_type)
7961 tree current, mdecl;
7963 if (!TYPE_CLINIT_STMT_LIST (class_type))
7964 return NULL_TREE;
7966 /* Go through all static fields and pre expand them */
7967 for (current = TYPE_FIELDS (class_type); current;
7968 current = TREE_CHAIN (current))
7969 if (FIELD_STATIC (current))
7970 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7972 /* Then build the <clinit> method */
7973 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7974 clinit_identifier_node, end_params_node);
7975 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7976 mdecl, NULL_TREE);
7977 start_artificial_method_body (mdecl);
7979 /* We process the list of assignment we produced as the result of
7980 the declaration of initialized static field and add them as
7981 statement to the <clinit> method. */
7982 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7983 current = TREE_CHAIN (current))
7985 tree stmt = current;
7986 /* We build the assignment expression that will initialize the
7987 field to its value. There are strict rules on static
7988 initializers (8.5). FIXME */
7989 if (TREE_CODE (stmt) != BLOCK && !IS_EMPTY_STMT (stmt))
7990 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7991 java_method_add_stmt (mdecl, stmt);
7994 end_artificial_method_body (mdecl);
7996 /* Now we want to place <clinit> as the last method (because we need
7997 it at least for interface so that it doesn't interfere with the
7998 dispatch table based lookup. */
7999 if (TREE_CHAIN (TYPE_METHODS (class_type)))
8001 current = TREE_CHAIN (TYPE_METHODS (class_type));
8002 TYPE_METHODS (class_type) = current;
8004 while (TREE_CHAIN (current))
8005 current = TREE_CHAIN (current);
8007 TREE_CHAIN (current) = mdecl;
8008 TREE_CHAIN (mdecl) = NULL_TREE;
8011 return mdecl;
8014 /* Analyzes a method body and look for something that isn't a
8015 MODIFY_EXPR with a constant value. */
8017 static int
8018 analyze_clinit_body (tree this_class, tree bbody)
8020 while (bbody)
8021 switch (TREE_CODE (bbody))
8023 case BLOCK:
8024 bbody = BLOCK_EXPR_BODY (bbody);
8025 break;
8027 case EXPR_WITH_FILE_LOCATION:
8028 bbody = EXPR_WFL_NODE (bbody);
8029 break;
8031 case COMPOUND_EXPR:
8032 if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
8033 return 1;
8034 bbody = TREE_OPERAND (bbody, 1);
8035 break;
8037 case MODIFY_EXPR:
8038 /* If we're generating to class file and we're dealing with an
8039 array initialization, we return 1 to keep <clinit> */
8040 if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
8041 && flag_emit_class_files)
8042 return 1;
8044 /* There are a few cases where we're required to keep
8045 <clinit>:
8046 - If this is an assignment whose operand is not constant,
8047 - If this is an assignment to a non-initialized field,
8048 - If this field is not a member of the current class.
8050 return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
8051 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
8052 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
8054 default:
8055 return 1;
8057 return 0;
8061 /* See whether we could get rid of <clinit>. Criteria are: all static
8062 final fields have constant initial values and the body of <clinit>
8063 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
8065 static int
8066 maybe_yank_clinit (tree mdecl)
8068 tree type, current;
8069 tree fbody, bbody;
8071 if (!DECL_CLINIT_P (mdecl))
8072 return 0;
8074 /* If the body isn't empty, then we keep <clinit>. Note that if
8075 we're emitting classfiles, this isn't enough not to rule it
8076 out. */
8077 fbody = DECL_FUNCTION_BODY (mdecl);
8078 bbody = BLOCK_EXPR_BODY (fbody);
8079 if (bbody && bbody != error_mark_node)
8080 bbody = BLOCK_EXPR_BODY (bbody);
8081 else
8082 return 0;
8083 if (bbody && ! flag_emit_class_files && !IS_EMPTY_STMT (bbody))
8084 return 0;
8086 type = DECL_CONTEXT (mdecl);
8087 current = TYPE_FIELDS (type);
8089 for (current = (current ? TREE_CHAIN (current) : current);
8090 current; current = TREE_CHAIN (current))
8092 tree f_init;
8094 /* We're not interested in non-static fields. */
8095 if (!FIELD_STATIC (current))
8096 continue;
8098 /* Nor in fields without initializers. */
8099 f_init = DECL_INITIAL (current);
8100 if (f_init == NULL_TREE)
8101 continue;
8103 /* Anything that isn't String or a basic type is ruled out -- or
8104 if we know how to deal with it (when doing things natively) we
8105 should generated an empty <clinit> so that SUID are computed
8106 correctly. */
8107 if (! JSTRING_TYPE_P (TREE_TYPE (current))
8108 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
8109 return 0;
8111 if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
8112 return 0;
8115 /* Now we analyze the method body and look for something that
8116 isn't a MODIFY_EXPR */
8117 if (bbody && !IS_EMPTY_STMT (bbody) && analyze_clinit_body (type, bbody))
8118 return 0;
8120 /* Get rid of <clinit> in the class' list of methods */
8121 if (TYPE_METHODS (type) == mdecl)
8122 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
8123 else
8124 for (current = TYPE_METHODS (type); current;
8125 current = TREE_CHAIN (current))
8126 if (TREE_CHAIN (current) == mdecl)
8128 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
8129 break;
8132 return 1;
8135 /* Install the argument from MDECL. Suitable to completion and
8136 expansion of mdecl's body. */
8138 void
8139 start_complete_expand_method (tree mdecl)
8141 tree tem;
8143 pushlevel (1); /* Prepare for a parameter push */
8144 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
8145 DECL_ARGUMENTS (mdecl) = tem;
8147 for (; tem; tem = TREE_CHAIN (tem))
8149 /* TREE_CHAIN (tem) will change after pushdecl. */
8150 tree next = TREE_CHAIN (tem);
8151 tree type = TREE_TYPE (tem);
8152 if (targetm.calls.promote_prototypes (type)
8153 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8154 && INTEGRAL_TYPE_P (type))
8155 type = integer_type_node;
8156 DECL_ARG_TYPE (tem) = type;
8157 layout_decl (tem, 0);
8158 pushdecl (tem);
8159 /* Re-install the next so that the list is kept and the loop
8160 advances. */
8161 TREE_CHAIN (tem) = next;
8163 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8164 input_location = DECL_SOURCE_LOCATION (mdecl);
8165 build_result_decl (mdecl);
8169 /* Complete and expand a method. */
8171 static void
8172 java_complete_expand_method (tree mdecl)
8174 tree fbody, block_body, exception_copy;
8176 current_function_decl = mdecl;
8177 /* Fix constructors before expanding them */
8178 if (DECL_CONSTRUCTOR_P (mdecl))
8179 fix_constructors (mdecl);
8181 /* Expand functions that have a body */
8182 if (!DECL_FUNCTION_BODY (mdecl))
8183 return;
8185 fbody = DECL_FUNCTION_BODY (mdecl);
8186 block_body = BLOCK_EXPR_BODY (fbody);
8187 exception_copy = NULL_TREE;
8189 current_function_decl = mdecl;
8191 if (! quiet_flag)
8192 fprintf (stderr, " [%s.",
8193 lang_printable_name (DECL_CONTEXT (mdecl), 0));
8194 announce_function (mdecl);
8195 if (! quiet_flag)
8196 fprintf (stderr, "]");
8198 /* Prepare the function for tree completion */
8199 start_complete_expand_method (mdecl);
8201 /* Install the current this */
8202 current_this = (!METHOD_STATIC (mdecl) ?
8203 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8205 /* Purge the `throws' list of unchecked exceptions (we save a copy
8206 of the list and re-install it later.) */
8207 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8208 purge_unchecked_exceptions (mdecl);
8210 /* Install exceptions thrown with `throws' */
8211 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8213 if (block_body != NULL_TREE)
8215 block_body = java_complete_tree (block_body);
8217 /* Before we check initialization, attached all class initialization
8218 variable to the block_body */
8219 htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8220 attach_init_test_initialization_flags, block_body);
8222 if (! METHOD_NATIVE (mdecl))
8224 check_for_initialization (block_body, mdecl);
8226 /* Go through all the flags marking the initialization of
8227 static variables and see whether they're definitively
8228 assigned, in which case the type is remembered as
8229 definitively initialized in MDECL. */
8230 if (STATIC_CLASS_INIT_OPT_P ())
8232 /* Always register the context as properly initialized in
8233 MDECL. This used with caution helps removing extra
8234 initialization of self. */
8235 if (METHOD_STATIC (mdecl))
8237 *(htab_find_slot
8238 (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8239 DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8243 ctxp->explicit_constructor_p = 0;
8246 BLOCK_EXPR_BODY (fbody) = block_body;
8248 /* If we saw a return but couldn't evaluate it properly, we'll have
8249 an error_mark_node here. */
8250 if (block_body != error_mark_node
8251 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8252 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE)
8253 missing_return_error (current_function_decl);
8255 /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8256 maybe_yank_clinit (mdecl);
8258 /* Pop the current level, with special measures if we found errors. */
8259 if (java_error_count)
8260 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8261 poplevel (1, 0, 1);
8263 /* Pop the exceptions and sanity check */
8264 POP_EXCEPTIONS();
8265 if (currently_caught_type_list)
8266 abort ();
8268 /* Restore the copy of the list of exceptions. */
8269 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8272 /* For with each class for which there's code to generate. */
8274 static void
8275 java_expand_method_bodies (tree class)
8277 tree decl;
8278 for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8280 tree block;
8282 if (! DECL_FUNCTION_BODY (decl))
8283 continue;
8285 current_function_decl = decl;
8287 block = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8289 /* Save the function body for gimplify and inlining. */
8290 DECL_SAVED_TREE (decl) = block;
8292 /* It's time to assign the variable flagging static class
8293 initialization based on which classes invoked static methods
8294 are definitely initializing. This should be flagged. */
8295 if (STATIC_CLASS_INIT_OPT_P ())
8297 tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8298 for (; list != NULL_TREE; list = TREE_CHAIN (list))
8300 /* Executed for each statement calling a static function.
8301 LIST is a TREE_LIST whose PURPOSE is the called function
8302 and VALUE is a compound whose second operand can be patched
8303 with static class initialization flag assignments. */
8305 tree called_method = TREE_PURPOSE (list);
8306 tree compound = TREE_VALUE (list);
8307 tree assignment_compound_list
8308 = build_tree_list (called_method, NULL);
8310 /* For each class definitely initialized in
8311 CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8312 assignment to the class initialization flag. */
8313 htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8314 emit_test_initialization,
8315 assignment_compound_list);
8317 if (TREE_VALUE (assignment_compound_list))
8318 TREE_OPERAND (compound, 1)
8319 = TREE_VALUE (assignment_compound_list);
8323 /* Expand the function body. */
8324 source_end_java_method ();
8330 /* This section of the code deals with accessing enclosing context
8331 fields either directly by using the relevant access to this$<n> or
8332 by invoking an access method crafted for that purpose. */
8334 /* Build the necessary access from an inner class to an outer
8335 class. This routine could be optimized to cache previous result
8336 (decl, current_class and returned access). When an access method
8337 needs to be generated, it always takes the form of a read. It might
8338 be later turned into a write by calling outer_field_access_fix. */
8340 static tree
8341 build_outer_field_access (tree id, tree decl)
8343 tree access = NULL_TREE;
8344 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8345 tree decl_ctx = DECL_CONTEXT (decl);
8347 /* If the immediate enclosing context of the current class is the
8348 field decl's class or inherits from it; build the access as
8349 `this$<n>.<field>'. Note that we will break the `private' barrier
8350 if we're not emitting bytecodes. */
8351 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8352 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8354 tree thisn = build_current_thisn (current_class);
8355 access = make_qualified_primary (build_wfl_node (thisn),
8356 id, EXPR_WFL_LINECOL (id));
8358 /* Otherwise, generate access methods to outer this and access the
8359 field (either using an access method or by direct access.) */
8360 else
8362 int lc = EXPR_WFL_LINECOL (id);
8364 /* Now we chain the required number of calls to the access$0 to
8365 get a hold to the enclosing instance we need, and then we
8366 build the field access. */
8367 access = build_access_to_thisn (current_class, decl_ctx, lc);
8369 /* If the field is private and we're generating bytecode, then
8370 we generate an access method */
8371 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8373 tree name = build_outer_field_access_methods (decl);
8374 access = build_outer_field_access_expr (lc, decl_ctx,
8375 name, access, NULL_TREE);
8377 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8378 Once again we break the `private' access rule from a foreign
8379 class. */
8380 else
8381 access = make_qualified_primary (access, id, lc);
8383 return resolve_expression_name (access, NULL);
8386 /* Return a nonzero value if NODE describes an outer field inner
8387 access. */
8389 static int
8390 outer_field_access_p (tree type, tree decl)
8392 if (!INNER_CLASS_TYPE_P (type)
8393 || TREE_CODE (decl) != FIELD_DECL
8394 || DECL_CONTEXT (decl) == type)
8395 return 0;
8397 /* If the inner class extends the declaration context of the field
8398 we're trying to access, then this isn't an outer field access */
8399 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8400 return 0;
8402 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8403 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8405 if (type == DECL_CONTEXT (decl))
8406 return 1;
8408 if (!DECL_CONTEXT (TYPE_NAME (type)))
8410 /* Before we give up, see whether the field is inherited from
8411 the enclosing context we're considering. */
8412 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8413 return 1;
8414 break;
8418 return 0;
8421 /* Return a nonzero value if NODE represents an outer field inner
8422 access that was been already expanded. As a side effect, it returns
8423 the name of the field being accessed and the argument passed to the
8424 access function, suitable for a regeneration of the access method
8425 call if necessary. */
8427 static int
8428 outer_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8429 tree *arg)
8431 int identified = 0;
8433 if (TREE_CODE (node) != CALL_EXPR)
8434 return 0;
8436 /* Well, gcj generates slightly different tree nodes when compiling
8437 to native or bytecodes. It's the case for function calls. */
8439 if (flag_emit_class_files
8440 && TREE_CODE (node) == CALL_EXPR
8441 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8442 identified = 1;
8443 else if (!flag_emit_class_files)
8445 node = TREE_OPERAND (node, 0);
8447 if (node && TREE_OPERAND (node, 0)
8448 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8450 node = TREE_OPERAND (node, 0);
8451 if (TREE_OPERAND (node, 0)
8452 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8453 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8454 (DECL_NAME (TREE_OPERAND (node, 0)))))
8455 identified = 1;
8459 if (identified && name && arg_type && arg)
8461 tree argument = TREE_OPERAND (node, 1);
8462 *name = DECL_NAME (TREE_OPERAND (node, 0));
8463 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8464 *arg = TREE_VALUE (argument);
8466 return identified;
8469 /* Detect in NODE an outer field read access from an inner class and
8470 transform it into a write with RHS as an argument. This function is
8471 called from the java_complete_lhs when an assignment to a LHS can
8472 be identified. */
8474 static tree
8475 outer_field_access_fix (tree wfl, tree node, tree rhs)
8477 tree name, arg_type, arg;
8479 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8481 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8482 arg_type, name, arg, rhs);
8483 return java_complete_tree (node);
8485 return NULL_TREE;
8488 /* Construct the expression that calls an access method:
8489 <type>.access$<n>(<arg1> [, <arg2>]);
8491 ARG2 can be NULL and will be omitted in that case. It will denote a
8492 read access. */
8494 static tree
8495 build_outer_field_access_expr (int lc, tree type, tree access_method_name,
8496 tree arg1, tree arg2)
8498 tree args, cn, access;
8500 args = arg1 ? arg1 :
8501 build_wfl_node (build_current_thisn (current_class));
8502 args = build_tree_list (NULL_TREE, args);
8504 if (arg2)
8505 args = tree_cons (NULL_TREE, arg2, args);
8507 access = build_method_invocation (build_wfl_node (access_method_name), args);
8508 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8509 return make_qualified_primary (cn, access, lc);
8512 static tree
8513 build_new_access_id (void)
8515 static int access_n_counter = 1;
8516 char buffer [128];
8518 sprintf (buffer, "access$%d", access_n_counter++);
8519 return get_identifier (buffer);
8522 /* Create the static access functions for the outer field DECL. We define a
8523 read:
8524 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8525 return inst$.field;
8527 and a write access:
8528 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8529 TREE_TYPE (<field>) value$) {
8530 return inst$.field = value$;
8532 We should have a usage flags on the DECL so we can lazily turn the ones
8533 we're using for code generation. FIXME.
8536 static tree
8537 build_outer_field_access_methods (tree decl)
8539 tree id, args, stmt, mdecl;
8541 if (FIELD_INNER_ACCESS_P (decl))
8542 return FIELD_INNER_ACCESS (decl);
8544 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8546 /* Create the identifier and a function named after it. */
8547 id = build_new_access_id ();
8549 /* The identifier is marked as bearing the name of a generated write
8550 access function for outer field accessed from inner classes. */
8551 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8553 /* Create the read access */
8554 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8555 TREE_CHAIN (args) = end_params_node;
8556 stmt = make_qualified_primary (build_wfl_node (inst_id),
8557 build_wfl_node (DECL_NAME (decl)), 0);
8558 stmt = build_return (0, stmt);
8559 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8560 TREE_TYPE (decl), id, args, stmt);
8561 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8563 /* Create the write access method. No write access for final variable */
8564 if (!FIELD_FINAL (decl))
8566 args = build_tree_list (inst_id,
8567 build_pointer_type (DECL_CONTEXT (decl)));
8568 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8569 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8570 stmt = make_qualified_primary (build_wfl_node (inst_id),
8571 build_wfl_node (DECL_NAME (decl)), 0);
8572 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8573 build_wfl_node (wpv_id)));
8574 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8575 TREE_TYPE (decl), id,
8576 args, stmt);
8578 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8580 /* Return the access name */
8581 return FIELD_INNER_ACCESS (decl) = id;
8584 /* Build an field access method NAME. */
8586 static tree
8587 build_outer_field_access_method (tree class, tree type, tree name,
8588 tree args, tree body)
8590 tree saved_current_function_decl, mdecl;
8592 /* Create the method */
8593 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8594 fix_method_argument_names (args, mdecl);
8595 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8597 /* Attach the method body. */
8598 saved_current_function_decl = current_function_decl;
8599 start_artificial_method_body (mdecl);
8600 java_method_add_stmt (mdecl, body);
8601 end_artificial_method_body (mdecl);
8602 current_function_decl = saved_current_function_decl;
8604 return mdecl;
8608 /* This section deals with building access function necessary for
8609 certain kinds of method invocation from inner classes. */
8611 static tree
8612 build_outer_method_access_method (tree decl)
8614 tree saved_current_function_decl, mdecl;
8615 tree args = NULL_TREE, call_args = NULL_TREE;
8616 tree carg, id, body, class;
8617 char buffer [80];
8618 int parm_id_count = 0;
8620 /* Test this abort with an access to a private field */
8621 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8622 abort ();
8624 /* Check the cache first */
8625 if (DECL_FUNCTION_INNER_ACCESS (decl))
8626 return DECL_FUNCTION_INNER_ACCESS (decl);
8628 class = DECL_CONTEXT (decl);
8630 /* Obtain an access identifier and mark it */
8631 id = build_new_access_id ();
8632 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8634 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8635 /* Create the arguments, as much as the original */
8636 for (; carg && carg != end_params_node;
8637 carg = TREE_CHAIN (carg))
8639 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8640 args = chainon (args, build_tree_list (get_identifier (buffer),
8641 TREE_VALUE (carg)));
8643 args = chainon (args, end_params_node);
8645 /* Create the method */
8646 mdecl = create_artificial_method (class, ACC_STATIC,
8647 TREE_TYPE (TREE_TYPE (decl)), id, args);
8648 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8649 /* There is a potential bug here. We should be able to use
8650 fix_method_argument_names, but then arg names get mixed up and
8651 eventually a constructor will have its this$0 altered and the
8652 outer context won't be assignment properly. The testcase is
8653 stub.java FIXME */
8654 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8656 /* Attach the method body. */
8657 saved_current_function_decl = current_function_decl;
8658 start_artificial_method_body (mdecl);
8660 /* The actual method invocation uses the same args. When invoking a
8661 static methods that way, we don't want to skip the first
8662 argument. */
8663 carg = args;
8664 if (!METHOD_STATIC (decl))
8665 carg = TREE_CHAIN (carg);
8666 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8667 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8668 call_args);
8670 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8671 call_args);
8672 if (!METHOD_STATIC (decl))
8673 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8674 body, 0);
8675 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8676 body = build_return (0, body);
8677 java_method_add_stmt (mdecl,body);
8678 end_artificial_method_body (mdecl);
8679 current_function_decl = saved_current_function_decl;
8681 /* Back tag the access function so it know what it accesses */
8682 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8684 /* Tag the current method so it knows it has an access generated */
8685 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8689 /* This section of the code deals with building expressions to access
8690 the enclosing instance of an inner class. The enclosing instance is
8691 kept in a generated field called this$<n>, with <n> being the
8692 inner class nesting level (starting from 0.) */
8694 /* Build an access to a given this$<n>, always chaining access call to
8695 others. Access methods to this$<n> are build on the fly if
8696 necessary. This CAN'T be used to solely access this$<n-1> from
8697 this$<n> (which alway yield to special cases and optimization, see
8698 for example build_outer_field_access). */
8700 static tree
8701 build_access_to_thisn (tree from, tree to, int lc)
8703 tree access = NULL_TREE;
8705 while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8707 if (!access)
8709 access = build_current_thisn (from);
8710 access = build_wfl_node (access);
8712 else
8714 tree access0_wfl, cn;
8716 maybe_build_thisn_access_method (from);
8717 access0_wfl = build_wfl_node (access0_identifier_node);
8718 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8719 EXPR_WFL_LINECOL (access0_wfl) = lc;
8720 access = build_tree_list (NULL_TREE, access);
8721 access = build_method_invocation (access0_wfl, access);
8722 access = make_qualified_primary (cn, access, lc);
8725 /* If FROM isn't an inner class, that's fine, we've done enough.
8726 What we're looking for can be accessed from there. */
8727 from = DECL_CONTEXT (TYPE_NAME (from));
8728 if (!from)
8729 break;
8730 from = TREE_TYPE (from);
8732 return access;
8735 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8736 is returned if nothing needs to be generated. Otherwise, the method
8737 generated and a method decl is returned.
8739 NOTE: These generated methods should be declared in a class file
8740 attribute so that they can't be referred to directly. */
8742 static tree
8743 maybe_build_thisn_access_method (tree type)
8745 tree mdecl, args, stmt, rtype;
8746 tree saved_current_function_decl;
8748 /* If TYPE is a top-level class, no access method is required.
8749 If there already is such an access method, bail out. */
8750 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8751 return NULL_TREE;
8753 /* We generate the method. The method looks like:
8754 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8756 args = build_tree_list (inst_id, build_pointer_type (type));
8757 TREE_CHAIN (args) = end_params_node;
8758 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8759 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8760 access0_identifier_node, args);
8761 fix_method_argument_names (args, mdecl);
8762 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8763 stmt = build_current_thisn (type);
8764 stmt = make_qualified_primary (build_wfl_node (inst_id),
8765 build_wfl_node (stmt), 0);
8766 stmt = build_return (0, stmt);
8768 saved_current_function_decl = current_function_decl;
8769 start_artificial_method_body (mdecl);
8770 java_method_add_stmt (mdecl, stmt);
8771 end_artificial_method_body (mdecl);
8772 current_function_decl = saved_current_function_decl;
8774 CLASS_ACCESS0_GENERATED_P (type) = 1;
8776 return mdecl;
8779 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8780 the first level of innerclassing. this$1 for the next one, etc...
8781 This function can be invoked with TYPE to NULL, available and then
8782 has to count the parser context. */
8784 static GTY(()) tree saved_thisn;
8785 static GTY(()) tree saved_type;
8787 static tree
8788 build_current_thisn (tree type)
8790 static int saved_i = -1;
8791 static int saved_type_i = 0;
8792 tree decl;
8793 char buffer [24];
8794 int i = 0;
8796 if (type)
8798 if (type == saved_type)
8799 i = saved_type_i;
8800 else
8802 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8803 decl; decl = DECL_CONTEXT (decl), i++)
8806 saved_type = type;
8807 saved_type_i = i;
8810 else
8811 i = list_length (GET_CPC_LIST ())-2;
8813 if (i == saved_i)
8814 return saved_thisn;
8816 sprintf (buffer, "this$%d", i);
8817 saved_i = i;
8818 saved_thisn = get_identifier (buffer);
8819 return saved_thisn;
8822 /* Return the assignment to the hidden enclosing context `this$<n>'
8823 by the second incoming parameter to the innerclass constructor. The
8824 form used is `this.this$<n> = this$<n>;'. */
8826 static tree
8827 build_thisn_assign (void)
8829 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8831 tree thisn = build_current_thisn (current_class);
8832 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8833 build_wfl_node (thisn), 0);
8834 tree rhs = build_wfl_node (thisn);
8835 #ifdef USE_MAPPED_LOCATION
8836 SET_EXPR_LOCATION (lhs, input_location);
8837 #else
8838 EXPR_WFL_SET_LINECOL (lhs, input_line, 0);
8839 #endif
8840 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8842 return NULL_TREE;
8846 /* Building the synthetic `class$' used to implement the `.class' 1.1
8847 extension for non primitive types. This method looks like:
8849 static Class class$(String type) throws NoClassDefFoundError
8851 try {return (java.lang.Class.forName (String));}
8852 catch (ClassNotFoundException e) {
8853 throw new NoClassDefFoundError(e.getMessage());}
8854 } */
8856 static GTY(()) tree get_message_wfl;
8857 static GTY(()) tree type_parm_wfl;
8859 static tree
8860 build_dot_class_method (tree class)
8862 #define BWF(S) build_wfl_node (get_identifier ((S)))
8863 #ifdef USE_MAPPED_LOCATION
8864 #define MQN(X,Y) make_qualified_name ((X), (Y), UNKNOWN_LOCATION)
8865 #else
8866 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8867 #endif
8868 tree args, tmp, saved_current_function_decl, mdecl, qual_name;
8869 tree stmt, throw_stmt;
8871 if (!get_message_wfl)
8873 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8874 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8877 /* Build the arguments */
8878 args = build_tree_list (get_identifier ("type$"),
8879 build_pointer_type (string_type_node));
8880 TREE_CHAIN (args) = end_params_node;
8882 /* Build the qualified name java.lang.Class.forName */
8883 tmp = MQN (MQN (MQN (BWF ("java"),
8884 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8886 /* Create the "class$" function */
8887 mdecl = create_artificial_method (class, ACC_STATIC,
8888 build_pointer_type (class_type_node),
8889 classdollar_identifier_node, args);
8890 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8891 BWF ("NoClassDefFoundError"));
8892 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE, qual_name);
8893 register_incomplete_type (JDEP_EXCEPTION, qual_name, NULL_TREE, NULL_TREE);
8894 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
8895 &TREE_VALUE (DECL_FUNCTION_THROWS (mdecl));
8897 /* We start by building the try block. We need to build:
8898 return (java.lang.Class.forName (type)); */
8899 stmt = build_method_invocation (tmp,
8900 build_tree_list (NULL_TREE, type_parm_wfl));
8901 stmt = build_return (0, stmt);
8903 /* Now onto the catch block. We start by building the expression
8904 throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8905 #ifdef USE_MAPPED_LOCATION
8906 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8907 get_message_wfl, UNKNOWN_LOCATION);
8908 #else
8909 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8910 get_message_wfl, 0);
8911 #endif
8912 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8914 /* Build new NoClassDefFoundError (_.getMessage) */
8915 throw_stmt = build_new_invocation
8916 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8917 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8919 /* Build the throw, (it's too early to use BUILD_THROW) */
8920 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8922 /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8923 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8924 BWF ("ClassNotFoundException"));
8925 stmt = encapsulate_with_try_catch (0, qual_name, stmt, throw_stmt);
8927 fix_method_argument_names (args, mdecl);
8928 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8929 saved_current_function_decl = current_function_decl;
8930 start_artificial_method_body (mdecl);
8931 java_method_add_stmt (mdecl, stmt);
8932 end_artificial_method_body (mdecl);
8933 current_function_decl = saved_current_function_decl;
8934 TYPE_DOT_CLASS (class) = mdecl;
8936 return mdecl;
8939 static tree
8940 build_dot_class_method_invocation (tree this_class, tree type)
8942 tree dot_class_method = TYPE_DOT_CLASS (this_class);
8943 tree sig_id, s, t;
8945 if (TYPE_ARRAY_P (type))
8946 sig_id = build_java_signature (type);
8947 else
8948 sig_id = DECL_NAME (TYPE_NAME (type));
8950 /* Ensure that the proper name separator is used */
8951 sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8952 IDENTIFIER_LENGTH (sig_id));
8954 s = build_string (IDENTIFIER_LENGTH (sig_id),
8955 IDENTIFIER_POINTER (sig_id));
8956 t = build_method_invocation (build_wfl_node (DECL_NAME (dot_class_method)),
8957 build_tree_list (NULL_TREE, s));
8958 if (DECL_CONTEXT (dot_class_method) != this_class)
8960 tree class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (dot_class_method)));
8961 t = make_qualified_primary (build_wfl_node (class_name), t, 0);
8963 return t;
8966 /* This section of the code deals with constructor. */
8968 /* Craft a body for default constructor. Patch existing constructor
8969 bodies with call to super() and field initialization statements if
8970 necessary. */
8972 static void
8973 fix_constructors (tree mdecl)
8975 tree iii; /* Instance Initializer Invocation */
8976 tree *bodyp = &DECL_FUNCTION_BODY (mdecl);
8977 tree thisn_assign, compound = NULL_TREE;
8978 tree class_type = DECL_CONTEXT (mdecl);
8980 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8981 return;
8982 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8984 if (!*bodyp)
8986 /* It is an error for the compiler to generate a default
8987 constructor if the superclass doesn't have a constructor that
8988 takes no argument, or the same args for an anonymous class */
8989 if (verify_constructor_super (mdecl))
8991 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8992 tree save = DECL_NAME (mdecl);
8993 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8994 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8995 parse_error_context
8996 (lookup_cl (TYPE_NAME (class_type)),
8997 "No constructor matching %qs found in class %qs",
8998 lang_printable_name (mdecl, 2), n);
8999 DECL_NAME (mdecl) = save;
9002 /* The constructor body must be crafted by hand. It's the
9003 constructor we defined when we realize we didn't have the
9004 CLASSNAME() constructor */
9005 start_artificial_method_body (mdecl);
9007 /* Insert an assignment to the this$<n> hidden field, if
9008 necessary */
9009 if ((thisn_assign = build_thisn_assign ()))
9010 java_method_add_stmt (mdecl, thisn_assign);
9012 /* We don't generate a super constructor invocation if we're
9013 compiling java.lang.Object. build_super_invocation takes care
9014 of that. */
9015 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
9017 /* FIXME */
9018 if ((iii = build_instinit_invocation (class_type)))
9019 java_method_add_stmt (mdecl, iii);
9021 end_artificial_method_body (mdecl);
9023 /* Search for an explicit constructor invocation */
9024 else
9026 int found = 0;
9027 int invokes_this = 0;
9028 tree main_block = BLOCK_EXPR_BODY (*bodyp);
9030 while (*bodyp)
9032 tree body = *bodyp;
9033 switch (TREE_CODE (body))
9035 case CALL_EXPR:
9036 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
9037 if (CALL_THIS_CONSTRUCTOR_P (body))
9038 invokes_this = 1;
9039 break;
9040 case COMPOUND_EXPR:
9041 case EXPR_WITH_FILE_LOCATION:
9042 bodyp = &TREE_OPERAND (body, 0);
9043 continue;
9044 case BLOCK:
9045 bodyp = &BLOCK_EXPR_BODY (body);
9046 continue;
9047 default:
9048 break;
9050 break;
9053 /* Generate the assignment to this$<n>, if necessary */
9054 if ((thisn_assign = build_thisn_assign ()))
9055 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
9057 /* The constructor is missing an invocation of super() */
9058 if (!found)
9059 compound = add_stmt_to_compound (compound, NULL_TREE,
9060 build_super_invocation (mdecl));
9061 /* Explicit super() invocation should take place before the
9062 instance initializer blocks. */
9063 else
9065 compound = add_stmt_to_compound (compound, NULL_TREE, *bodyp);
9066 *bodyp = build_java_empty_stmt ();
9069 DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
9071 /* Insert the instance initializer block right after. */
9072 if (!invokes_this && (iii = build_instinit_invocation (class_type)))
9073 compound = add_stmt_to_compound (compound, NULL_TREE, iii);
9075 /* Fix the constructor main block if we're adding extra stmts */
9076 if (compound)
9078 compound = add_stmt_to_compound (compound, NULL_TREE,
9079 BLOCK_EXPR_BODY (main_block));
9080 BLOCK_EXPR_BODY (main_block) = compound;
9085 /* Browse constructors in the super class, searching for a constructor
9086 that doesn't take any argument. Return 0 if one is found, 1
9087 otherwise. If the current class is an anonymous inner class, look
9088 for something that has the same signature. */
9090 static int
9091 verify_constructor_super (tree mdecl)
9093 tree class = CLASSTYPE_SUPER (current_class);
9094 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
9095 tree sdecl;
9097 if (!class)
9098 return 0;
9100 if (ANONYMOUS_CLASS_P (current_class))
9102 tree mdecl_arg_type;
9103 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
9104 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9105 if (DECL_CONSTRUCTOR_P (sdecl))
9107 tree m_arg_type;
9108 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9109 if (super_inner)
9110 arg_type = TREE_CHAIN (arg_type);
9111 for (m_arg_type = mdecl_arg_type;
9112 (arg_type != end_params_node
9113 && m_arg_type != end_params_node);
9114 arg_type = TREE_CHAIN (arg_type),
9115 m_arg_type = TREE_CHAIN (m_arg_type))
9116 if (!valid_method_invocation_conversion_p
9117 (TREE_VALUE (arg_type),
9118 TREE_VALUE (m_arg_type)))
9119 break;
9121 if (arg_type == end_params_node && m_arg_type == end_params_node)
9122 return 0;
9125 else
9127 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9129 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9130 if (super_inner)
9131 arg = TREE_CHAIN (arg);
9132 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
9133 return 0;
9136 return 1;
9139 /* Generate code for all context remembered for code generation. */
9141 static GTY(()) tree reversed_class_list;
9142 void
9143 java_expand_classes (void)
9145 int save_error_count = 0;
9146 static struct parser_ctxt *cur_ctxp = NULL;
9147 location_t save_location;
9149 java_parse_abort_on_error ();
9150 if (!(ctxp = ctxp_for_generation))
9151 return;
9152 java_layout_classes ();
9153 java_parse_abort_on_error ();
9154 save_location = input_location;
9156 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9158 tree current;
9159 for (current = cur_ctxp->class_list;
9160 current;
9161 current = TREE_CHAIN (current))
9162 gen_indirect_dispatch_tables (TREE_TYPE (current));
9165 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9167 ctxp = cur_ctxp;
9168 input_location = ctxp->file_start_location;
9169 lang_init_source (2); /* Error msgs have method prototypes */
9170 java_complete_expand_classes (); /* Complete and expand classes */
9171 java_parse_abort_on_error ();
9173 input_location = save_location;
9175 /* Find anonymous classes and expand their constructor. This extra pass is
9176 necessary because the constructor itself is only generated when the
9177 method in which it is defined is expanded. */
9178 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9180 tree current;
9181 ctxp = cur_ctxp;
9182 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9184 output_class = current_class = TREE_TYPE (current);
9185 if (ANONYMOUS_CLASS_P (current_class))
9187 tree d;
9188 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9190 if (DECL_CONSTRUCTOR_P (d))
9192 java_complete_expand_method (d);
9193 break; /* There is only one constructor. */
9200 /* Expanding the constructors of anonymous classes generates access
9201 methods. Scan all the methods looking for null DECL_RESULTs --
9202 this will be the case if a method hasn't been expanded. */
9203 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9205 tree current;
9206 ctxp = cur_ctxp;
9207 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9209 tree d;
9210 output_class = current_class = TREE_TYPE (current);
9211 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9213 if (DECL_RESULT (d) == NULL_TREE)
9214 java_complete_expand_method (d);
9219 /* ??? Instead of all this we could iterate around the list of
9220 classes until there were no more un-expanded methods. It would
9221 take a little longer -- one pass over the whole list of methods
9222 -- but it would be simpler. Like this: */
9223 #if 0
9225 int something_changed;
9229 something_changed = 0;
9230 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9232 tree current;
9233 ctxp = cur_ctxp;
9234 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9236 tree d;
9237 output_class = current_class = TREE_TYPE (current);
9238 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9240 if (DECL_RESULT (d) == NULL_TREE)
9242 something_changed = 1;
9243 java_complete_expand_method (d);
9249 while (something_changed);
9251 #endif
9253 /* If we've found error at that stage, don't try to generate
9254 anything, unless we're checking the syntax only
9255 (but not using -fsyntax-only for the purpose of generating
9256 bytecode). */
9257 if (java_error_count
9258 && (!flag_syntax_only && !flag_emit_class_files))
9259 return;
9261 /* Now things are stable, go for generation of the class data. */
9263 /* We pessimistically marked all methods and fields external until
9264 we knew what set of classes we were planning to compile. Now mark
9265 those that will be generated locally as not external. */
9266 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9268 tree current;
9269 ctxp = cur_ctxp;
9270 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9271 java_mark_class_local (TREE_TYPE (current));
9274 /* Compile the classes. */
9275 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9277 tree current;
9278 reversed_class_list = NULL;
9280 ctxp = cur_ctxp;
9282 /* We write out the classes in reverse order. This ensures that
9283 inner classes are written before their containing classes,
9284 which is important for parallel builds. Otherwise, the
9285 class file for the outer class may be found, but the class
9286 file for the inner class may not be present. In that
9287 situation, the compiler cannot fall back to the original
9288 source, having already read the outer class, so we must
9289 prevent that situation. */
9290 for (current = ctxp->class_list;
9291 current;
9292 current = TREE_CHAIN (current))
9293 reversed_class_list
9294 = tree_cons (NULL_TREE, current, reversed_class_list);
9296 for (current = reversed_class_list;
9297 current;
9298 current = TREE_CHAIN (current))
9300 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
9301 if (flag_emit_class_files)
9302 write_classfile (current_class);
9303 else if (! flag_syntax_only)
9304 java_expand_method_bodies (current_class);
9309 void
9310 java_finish_classes (void)
9312 static struct parser_ctxt *cur_ctxp = NULL;
9313 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9315 tree current;
9316 ctxp = cur_ctxp;
9317 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9319 output_class = current_class = TREE_TYPE (current);
9320 finish_class ();
9325 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9326 a tree list node containing RIGHT. Fore coming RIGHTs will be
9327 chained to this hook. LOCATION contains the location of the
9328 separating `.' operator. */
9330 static tree
9331 make_qualified_primary (tree primary, tree right, int location)
9333 tree wfl;
9335 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9336 wfl = build_wfl_wrap (primary, location);
9337 else
9339 wfl = primary;
9340 /* If wfl wasn't qualified, we build a first anchor */
9341 if (!EXPR_WFL_QUALIFICATION (wfl))
9342 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9345 /* And chain them */
9346 EXPR_WFL_LINECOL (right) = location;
9347 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9348 PRIMARY_P (wfl) = 1;
9349 return wfl;
9352 /* Simple merge of two name separated by a `.' */
9354 static tree
9355 merge_qualified_name (tree left, tree right)
9357 tree node;
9358 if (!left && !right)
9359 return NULL_TREE;
9361 if (!left)
9362 return right;
9364 if (!right)
9365 return left;
9367 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9368 IDENTIFIER_LENGTH (left));
9369 obstack_1grow (&temporary_obstack, '.');
9370 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9371 IDENTIFIER_LENGTH (right));
9372 node = get_identifier (obstack_base (&temporary_obstack));
9373 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9374 QUALIFIED_P (node) = 1;
9375 return node;
9378 /* Merge the two parts of a qualified name into LEFT. Set the
9379 location information of the resulting node to LOCATION, usually
9380 inherited from the location information of the `.' operator. */
9382 static tree
9383 make_qualified_name (tree left, tree right,
9384 #ifdef USE_MAPPED_LOCATION
9385 source_location location
9386 #else
9387 int location
9388 #endif
9391 #ifdef USE_COMPONENT_REF
9392 tree node = build3 (COMPONENT_REF, NULL_TREE, left, right, NULL_TREE);
9393 SET_EXPR_LOCATION (node, location);
9394 return node;
9395 #else
9396 tree left_id = EXPR_WFL_NODE (left);
9397 tree right_id = EXPR_WFL_NODE (right);
9398 tree wfl, merge;
9400 merge = merge_qualified_name (left_id, right_id);
9402 /* Left wasn't qualified and is now qualified */
9403 #ifdef USE_MAPPED_LOCATION
9404 if (!QUALIFIED_P (left_id))
9406 tree wfl = build_expr_wfl (left_id, EXPR_LOCATION (left));
9407 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9410 wfl = build_expr_wfl (right_id, location);
9411 #else
9412 if (!QUALIFIED_P (left_id))
9414 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9415 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9416 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9419 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9420 EXPR_WFL_LINECOL (wfl) = location;
9421 #endif
9422 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9423 EXPR_WFL_NODE (left) = merge;
9424 return left;
9425 #endif
9428 /* Extract the last identifier component of the qualified in WFL. The
9429 last identifier is removed from the linked list */
9431 static tree
9432 cut_identifier_in_qualified (tree wfl)
9434 tree q;
9435 tree previous = NULL_TREE;
9436 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9437 if (!TREE_CHAIN (q))
9439 if (!previous)
9440 /* Operating on a non qualified qualified WFL. */
9441 abort ();
9443 TREE_CHAIN (previous) = NULL_TREE;
9444 return TREE_PURPOSE (q);
9448 /* Resolve the expression name NAME. Return its decl. */
9450 static tree
9451 resolve_expression_name (tree id, tree *orig)
9453 tree name = EXPR_WFL_NODE (id);
9454 tree decl;
9456 /* 6.5.5.1: Simple expression names */
9457 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9459 /* 15.13.1: NAME can appear within the scope of a local variable
9460 declaration */
9461 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9462 return decl;
9464 /* 15.13.1: NAME can appear within a class declaration */
9465 else
9467 decl = lookup_field_wrapper (current_class, name);
9468 if (decl)
9470 tree access = NULL_TREE;
9471 int fs = FIELD_STATIC (decl);
9473 /* If we're accessing an outer scope local alias, make
9474 sure we change the name of the field we're going to
9475 build access to. */
9476 if (FIELD_LOCAL_ALIAS_USED (decl))
9477 name = DECL_NAME (decl);
9479 check_deprecation (id, decl);
9481 /* Instance variable (8.3.1.1) can't appear within
9482 static method, static initializer or initializer for
9483 a static variable. */
9484 if (!fs && METHOD_STATIC (current_function_decl))
9486 static_ref_err (id, name, current_class);
9487 return error_mark_node;
9489 /* Instance variables can't appear as an argument of
9490 an explicit constructor invocation */
9491 if (!fs && ctxp->explicit_constructor_p
9492 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9494 parse_error_context
9495 (id, "Can't reference %qs before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9496 return error_mark_node;
9499 /* If we're processing an inner class and we're trying
9500 to access a field belonging to an outer class, build
9501 the access to the field */
9502 if (!fs && outer_field_access_p (current_class, decl))
9504 if (CLASS_STATIC (TYPE_NAME (current_class)))
9506 static_ref_err (id, DECL_NAME (decl), current_class);
9507 return error_mark_node;
9509 access = build_outer_field_access (id, decl);
9510 if (orig)
9511 *orig = access;
9512 return access;
9515 /* Otherwise build what it takes to access the field */
9516 access = build_field_ref ((fs ? NULL_TREE : current_this),
9517 DECL_CONTEXT (decl), name);
9518 if (fs)
9519 access = maybe_build_class_init_for_field (decl, access);
9520 /* We may be asked to save the real field access node */
9521 if (orig)
9522 *orig = access;
9523 /* Last check: can we access the field? */
9524 if (not_accessible_p (current_class, decl, NULL_TREE, 0))
9526 not_accessible_field_error (id, decl);
9527 return error_mark_node;
9529 /* And we return what we got */
9530 return access;
9532 /* Fall down to error report on undefined variable */
9535 /* 6.5.5.2 Qualified Expression Names */
9536 else
9538 if (orig)
9539 *orig = NULL_TREE;
9540 qualify_ambiguous_name (id);
9541 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9542 /* 15.10.2: Accessing Superclass Members using super */
9543 return resolve_field_access (id, orig, NULL);
9546 /* We've got an error here */
9547 if (INNER_CLASS_TYPE_P (current_class))
9548 parse_error_context (id,
9549 "Local variable %qs can't be accessed from within the inner class %qs unless it is declared final",
9550 IDENTIFIER_POINTER (name),
9551 IDENTIFIER_POINTER (DECL_NAME
9552 (TYPE_NAME (current_class))));
9553 else
9554 parse_error_context (id, "Undefined variable %qs",
9555 IDENTIFIER_POINTER (name));
9557 return error_mark_node;
9560 static void
9561 static_ref_err (tree wfl, tree field_id, tree class_type)
9563 parse_error_context
9564 (wfl,
9565 "Can't make a static reference to nonstatic variable %qs in class %qs",
9566 IDENTIFIER_POINTER (field_id),
9567 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9570 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9571 We return something suitable to generate the field access. We also
9572 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9573 recipient's address can be null. */
9575 static tree
9576 resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9578 int is_static = 0;
9579 tree field_ref;
9580 tree decl = NULL_TREE, where_found, type_found;
9582 if (resolve_qualified_expression_name (qual_wfl, &decl,
9583 &where_found, &type_found))
9584 return error_mark_node;
9586 /* Resolve the LENGTH field of an array here */
9587 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9588 && type_found && TYPE_ARRAY_P (type_found)
9589 && ! flag_emit_class_files)
9591 tree length = build_java_array_length_access (where_found);
9592 field_ref = length;
9594 /* In case we're dealing with a static array, we need to
9595 initialize its class before the array length can be fetched.
9596 It's also a good time to create a DECL_RTL for the field if
9597 none already exists, otherwise if the field was declared in a
9598 class found in an external file and hasn't been (and won't
9599 be) accessed for its value, none will be created. */
9600 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9602 build_static_field_ref (where_found);
9603 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9606 /* We might have been trying to resolve field.method(). In which
9607 case, the resolution is over and decl is the answer */
9608 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9609 field_ref = decl;
9610 else if (JDECL_P (decl))
9612 if (!type_found)
9613 type_found = DECL_CONTEXT (decl);
9614 is_static = FIELD_STATIC (decl);
9615 field_ref = build_field_ref ((is_static ?
9616 NULL_TREE : where_found),
9617 type_found, DECL_NAME (decl));
9618 if (field_ref == error_mark_node)
9619 return error_mark_node;
9620 if (is_static)
9621 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9623 /* If we're looking at a static field, we may need to generate a
9624 class initialization for it. This can happen when the access
9625 looks like `field.ref', where `field' is a static field in an
9626 interface we implement. */
9627 if (!flag_emit_class_files
9628 && TREE_CODE (where_found) == VAR_DECL
9629 && FIELD_STATIC (where_found))
9631 build_static_field_ref (where_found);
9632 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9635 else
9636 field_ref = decl;
9638 if (field_decl)
9639 *field_decl = decl;
9640 if (field_type)
9641 *field_type = (QUAL_DECL_TYPE (decl) ?
9642 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9643 return field_ref;
9646 /* If NODE is an access to a static field, strip out the class
9647 initialization part and return the field decl, otherwise, return
9648 NODE. */
9650 tree
9651 extract_field_decl (tree node)
9653 if (TREE_CODE (node) == COMPOUND_EXPR)
9655 tree op1 = TREE_OPERAND (node, 1);
9656 if (TREE_CODE (op1) == COMPOUND_EXPR)
9658 tree call = TREE_OPERAND (op1, 0);
9659 if (TREE_CODE (call) == CALL_EXPR
9660 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9661 && (TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9662 == soft_initclass_node))
9663 return TREE_OPERAND (op1, 1);
9665 else if (JDECL_P (op1))
9666 return op1;
9668 return node;
9671 /* 6.5.5.2: Qualified Expression Names */
9673 static int
9674 resolve_qualified_expression_name (tree wfl, tree *found_decl,
9675 tree *where_found, tree *type_found)
9677 int from_type = 0; /* Field search initiated from a type */
9678 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9679 int previous_call_static = 0;
9680 int is_static;
9681 tree decl = NULL_TREE, type = NULL_TREE, q;
9682 /* For certain for of inner class instantiation */
9683 tree saved_current, saved_this;
9684 #define RESTORE_THIS_AND_CURRENT_CLASS \
9685 { current_class = saved_current; current_this = saved_this;}
9687 *type_found = *where_found = NULL_TREE;
9689 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9691 tree qual_wfl = QUAL_WFL (q);
9692 tree ret_decl; /* for EH checking */
9693 #ifdef USE_MAPPED_LOCATION
9694 source_location location; /* for EH checking */
9695 #else
9696 int location; /* for EH checking */
9697 #endif
9699 /* 15.10.1 Field Access Using a Primary */
9700 switch (TREE_CODE (qual_wfl))
9702 case CALL_EXPR:
9703 case NEW_CLASS_EXPR:
9704 /* If the access to the function call is a non static field,
9705 build the code to access it. */
9706 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9708 decl = maybe_access_field (decl, *where_found,
9709 DECL_CONTEXT (decl));
9710 if (decl == error_mark_node)
9711 return 1;
9714 /* And code for the function call */
9715 if (complete_function_arguments (qual_wfl))
9716 return 1;
9718 /* We might have to setup a new current class and a new this
9719 for the search of an inner class, relative to the type of
9720 a expression resolved as `decl'. The current values are
9721 saved and restored shortly after */
9722 saved_current = current_class;
9723 saved_this = current_this;
9724 if (decl
9725 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9726 || from_qualified_this))
9728 /* If we still have `from_qualified_this', we have the form
9729 <T>.this.f() and we need to build <T>.this */
9730 if (from_qualified_this)
9732 decl = build_access_to_thisn (current_class, type, 0);
9733 decl = java_complete_tree (decl);
9734 type = TREE_TYPE (TREE_TYPE (decl));
9736 current_class = type;
9737 current_this = decl;
9738 from_qualified_this = 0;
9741 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9742 CALL_USING_SUPER (qual_wfl) = 1;
9743 #ifdef USE_MAPPED_LOCATION
9744 location = (TREE_CODE (qual_wfl) == CALL_EXPR
9745 ? EXPR_LOCATION (TREE_OPERAND (qual_wfl, 0))
9746 : UNKNOWN_LOCATION);
9747 #else
9748 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9749 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9750 #endif
9751 *where_found = patch_method_invocation (qual_wfl, decl, type,
9752 from_super,
9753 &is_static, &ret_decl);
9754 from_super = 0;
9755 if (*where_found == error_mark_node)
9757 RESTORE_THIS_AND_CURRENT_CLASS;
9758 return 1;
9760 *type_found = type = QUAL_DECL_TYPE (*where_found);
9762 *where_found = force_evaluation_order (*where_found);
9764 /* If we're creating an inner class instance, check for that
9765 an enclosing instance is in scope */
9766 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9767 && INNER_ENCLOSING_SCOPE_CHECK (type))
9769 parse_error_context
9770 (qual_wfl, "No enclosing instance for inner class %qs is in scope%s",
9771 lang_printable_name (type, 0),
9772 (!current_this ? "" :
9773 "; an explicit one must be provided when creating this inner class"));
9774 RESTORE_THIS_AND_CURRENT_CLASS;
9775 return 1;
9778 /* In case we had to change then to resolve a inner class
9779 instantiation using a primary qualified by a `new' */
9780 RESTORE_THIS_AND_CURRENT_CLASS;
9782 #ifdef USE_MAPPED_LOCATION
9783 if (location != UNKNOWN_LOCATION)
9784 #else
9785 if (location)
9786 #endif
9788 tree arguments = NULL_TREE;
9789 if (TREE_CODE (qual_wfl) == CALL_EXPR
9790 && TREE_OPERAND (qual_wfl, 1) != NULL_TREE)
9791 arguments = TREE_VALUE (TREE_OPERAND (qual_wfl, 1));
9792 check_thrown_exceptions (location, ret_decl, arguments);
9795 /* If the previous call was static and this one is too,
9796 build a compound expression to hold the two (because in
9797 that case, previous function calls aren't transported as
9798 forcoming function's argument. */
9799 if (previous_call_static && is_static)
9801 /* We must set CAN_COMPLETE_NORMALLY for the first call
9802 since it is done nowhere else. */
9803 CAN_COMPLETE_NORMALLY (decl) = 1;
9804 decl = build2 (COMPOUND_EXPR, TREE_TYPE (*where_found),
9805 decl, *where_found);
9806 TREE_SIDE_EFFECTS (decl) = 1;
9808 else
9810 previous_call_static = is_static;
9811 decl = *where_found;
9813 from_type = 0;
9814 continue;
9816 case NEW_ARRAY_EXPR:
9817 case NEW_ANONYMOUS_ARRAY_EXPR:
9818 *where_found = decl = java_complete_tree (qual_wfl);
9819 if (decl == error_mark_node)
9820 return 1;
9821 *type_found = type = QUAL_DECL_TYPE (decl);
9822 continue;
9824 case CONVERT_EXPR:
9825 *where_found = decl = java_complete_tree (qual_wfl);
9826 if (decl == error_mark_node)
9827 return 1;
9828 *type_found = type = QUAL_DECL_TYPE (decl);
9829 from_cast = 1;
9830 continue;
9832 case CONDITIONAL_EXPR:
9833 case STRING_CST:
9834 case MODIFY_EXPR:
9835 *where_found = decl = java_complete_tree (qual_wfl);
9836 if (decl == error_mark_node)
9837 return 1;
9838 *type_found = type = QUAL_DECL_TYPE (decl);
9839 continue;
9841 case ARRAY_REF:
9842 /* If the access to the function call is a non static field,
9843 build the code to access it. */
9844 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9846 decl = maybe_access_field (decl, *where_found, type);
9847 if (decl == error_mark_node)
9848 return 1;
9850 /* And code for the array reference expression */
9851 decl = java_complete_tree (qual_wfl);
9852 if (decl == error_mark_node)
9853 return 1;
9854 type = QUAL_DECL_TYPE (decl);
9855 continue;
9857 case PLUS_EXPR:
9858 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9859 return 1;
9860 if ((type = patch_string (decl)))
9861 decl = type;
9862 *where_found = QUAL_RESOLUTION (q) = decl;
9863 *type_found = type = TREE_TYPE (decl);
9864 break;
9866 case CLASS_LITERAL:
9867 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9868 return 1;
9869 *where_found = QUAL_RESOLUTION (q) = decl;
9870 *type_found = type = TREE_TYPE (decl);
9871 break;
9873 default:
9874 /* Fix for -Wall Just go to the next statement. Don't
9875 continue */
9876 break;
9879 /* If we fall here, we weren't processing a (static) function call. */
9880 previous_call_static = 0;
9882 /* It can be the keyword THIS */
9883 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9884 && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9886 if (!current_this)
9888 parse_error_context
9889 (wfl, "Keyword %<this%> used outside allowed context");
9890 return 1;
9892 if (ctxp->explicit_constructor_p
9893 && type == current_class)
9895 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
9896 return 1;
9898 /* We have to generate code for intermediate access */
9899 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9901 *where_found = decl = current_this;
9902 *type_found = type = QUAL_DECL_TYPE (decl);
9904 /* We're trying to access the this from somewhere else. Make sure
9905 it's allowed before doing so. */
9906 else
9908 if (!enclosing_context_p (type, current_class))
9910 char *p = xstrdup (lang_printable_name (type, 0));
9911 parse_error_context (qual_wfl, "Can't use variable %<%s.this%>: type %qs isn't an outer type of type %qs",
9912 p, p,
9913 lang_printable_name (current_class, 0));
9914 free (p);
9915 return 1;
9917 from_qualified_this = 1;
9918 /* If there's nothing else after that, we need to
9919 produce something now, otherwise, the section of the
9920 code that needs to produce <T>.this will generate
9921 what is necessary. */
9922 if (!TREE_CHAIN (q))
9924 decl = build_access_to_thisn (current_class, type, 0);
9925 *where_found = decl = java_complete_tree (decl);
9926 *type_found = type = TREE_TYPE (decl);
9930 from_type = 0;
9931 continue;
9934 /* 15.10.2 Accessing Superclass Members using SUPER */
9935 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9936 && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9938 tree node;
9939 /* Check on the restricted use of SUPER */
9940 if (METHOD_STATIC (current_function_decl)
9941 || current_class == object_type_node)
9943 parse_error_context
9944 (wfl, "Keyword %<super%> used outside allowed context");
9945 return 1;
9947 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9948 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9949 CLASSTYPE_SUPER (current_class),
9950 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9951 *where_found = decl = java_complete_tree (node);
9952 if (decl == error_mark_node)
9953 return 1;
9954 *type_found = type = QUAL_DECL_TYPE (decl);
9955 from_super = from_type = 1;
9956 continue;
9959 /* 15.13.1: Can't search for field name in packages, so we
9960 assume a variable/class name was meant. */
9961 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9963 tree name;
9964 if ((decl = resolve_package (wfl, &q, &name)))
9966 tree list;
9967 *where_found = decl;
9969 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
9971 /* We want to be absolutely sure that the class is laid
9972 out. We're going to search something inside it. */
9973 *type_found = type = TREE_TYPE (decl);
9974 layout_class (type);
9975 from_type = 1;
9977 /* Fix them all the way down, if any are left. */
9978 if (q)
9980 list = TREE_CHAIN (q);
9981 while (list)
9983 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9984 list = TREE_CHAIN (list);
9988 else
9990 if (from_super || from_cast)
9991 parse_error_context
9992 ((from_cast ? qual_wfl : wfl),
9993 "No variable %qs defined in class %qs",
9994 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9995 lang_printable_name (type, 0));
9996 else
9997 parse_error_context
9998 (qual_wfl, "Undefined variable or class name: %qs",
9999 IDENTIFIER_POINTER (name));
10000 return 1;
10004 /* We have a type name. It's been already resolved when the
10005 expression was qualified. */
10006 else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
10008 decl = QUAL_RESOLUTION (q);
10010 /* Sneak preview. If next we see a `new', we're facing a
10011 qualification which resulted in a type being selected
10012 instead of a field. Report the error. */
10013 if(TREE_CHAIN (q)
10014 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
10016 parse_error_context (qual_wfl, "Undefined variable %qs",
10017 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10018 return 1;
10021 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10023 check_deprecation (qual_wfl, decl);
10025 type = TREE_TYPE (decl);
10026 from_type = 1;
10028 /* We resolve an expression name */
10029 else
10031 tree field_decl = NULL_TREE;
10033 /* If there exists an early resolution, use it. That occurs
10034 only once and we know that there are more things to
10035 come. Don't do that when processing something after SUPER
10036 (we need more thing to be put in place below */
10037 if (!from_super && QUAL_RESOLUTION (q))
10039 decl = QUAL_RESOLUTION (q);
10040 if (!type)
10042 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
10044 if (current_this)
10045 *where_found = current_this;
10046 else
10048 static_ref_err (qual_wfl, DECL_NAME (decl),
10049 current_class);
10050 return 1;
10052 if (outer_field_access_p (current_class, decl))
10053 decl = build_outer_field_access (qual_wfl, decl);
10055 else
10057 *where_found = TREE_TYPE (decl);
10058 if (TREE_CODE (*where_found) == POINTER_TYPE)
10059 *where_found = TREE_TYPE (*where_found);
10064 /* Report and error if we're using a numerical literal as a
10065 qualifier. It can only be an INTEGER_CST. */
10066 else if (TREE_CODE (qual_wfl) == INTEGER_CST)
10068 parse_error_context
10069 (wfl, "Can't use type %qs as a qualifier",
10070 lang_printable_name (TREE_TYPE (qual_wfl), 0));
10071 return 1;
10074 /* We have to search for a field, knowing the type of its
10075 container. The flag FROM_TYPE indicates that we resolved
10076 the last member of the expression as a type name, which
10077 means that for the resolution of this field, we'll look
10078 for other errors than if it was resolved as a member of
10079 an other field. */
10080 else
10082 int is_static;
10083 tree field_decl_type; /* For layout */
10085 if (!from_type && !JREFERENCE_TYPE_P (type))
10087 parse_error_context
10088 (qual_wfl, "Attempt to reference field %qs in %<%s %s%>",
10089 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10090 lang_printable_name (type, 0),
10091 IDENTIFIER_POINTER (DECL_NAME (decl)));
10092 return 1;
10095 field_decl = lookup_field_wrapper (type,
10096 EXPR_WFL_NODE (qual_wfl));
10098 /* Maybe what we're trying to access to is an inner
10099 class, only if decl is a TYPE_DECL. */
10100 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
10102 tree ptr, inner_decl;
10104 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
10105 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
10106 if (inner_decl)
10108 check_inner_class_access (inner_decl, decl, qual_wfl);
10109 type = TREE_TYPE (inner_decl);
10110 decl = inner_decl;
10111 from_type = 1;
10112 continue;
10116 if (field_decl == NULL_TREE)
10118 parse_error_context
10119 (qual_wfl, "No variable %qs defined in type %qs",
10120 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10121 GET_TYPE_NAME (type));
10122 return 1;
10124 if (field_decl == error_mark_node)
10125 return 1;
10127 /* Layout the type of field_decl, since we may need
10128 it. Don't do primitive types or loaded classes. The
10129 situation of non primitive arrays may not handled
10130 properly here. FIXME */
10131 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10132 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
10133 else
10134 field_decl_type = TREE_TYPE (field_decl);
10135 if (!JPRIMITIVE_TYPE_P (field_decl_type)
10136 && !CLASS_LOADED_P (field_decl_type)
10137 && !TYPE_ARRAY_P (field_decl_type))
10138 resolve_and_layout (field_decl_type, NULL_TREE);
10140 /* Check on accessibility here */
10141 if (not_accessible_p (current_class, field_decl,
10142 *type_found, from_super))
10143 return not_accessible_field_error (qual_wfl,field_decl);
10144 check_deprecation (qual_wfl, field_decl);
10146 /* There are things to check when fields are accessed
10147 from type. There are no restrictions on a static
10148 declaration of the field when it is accessed from an
10149 interface */
10150 is_static = FIELD_STATIC (field_decl);
10151 if (!from_super && from_type
10152 && !TYPE_INTERFACE_P (type)
10153 && !is_static
10154 && (current_function_decl
10155 && METHOD_STATIC (current_function_decl)))
10157 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
10158 return 1;
10160 from_cast = from_super = 0;
10162 /* It's an access from a type but it isn't static, we
10163 make it relative to `this'. */
10164 if (!is_static && from_type)
10165 decl = current_this;
10167 /* If we need to generate something to get a proper
10168 handle on what this field is accessed from, do it
10169 now. */
10170 if (!is_static)
10172 decl = maybe_access_field (decl, *where_found, *type_found);
10173 if (decl == error_mark_node)
10174 return 1;
10177 /* We want to keep the location were found it, and the type
10178 we found. */
10179 *where_found = decl;
10180 *type_found = type;
10182 /* Generate the correct expression for field access from
10183 qualified this */
10184 if (from_qualified_this)
10186 field_decl = build_outer_field_access (qual_wfl, field_decl);
10187 from_qualified_this = 0;
10190 /* This is the decl found and eventually the next one to
10191 search from */
10192 decl = field_decl;
10194 from_type = 0;
10195 type = QUAL_DECL_TYPE (decl);
10197 /* Sneak preview. If decl is qualified by a `new', report
10198 the error here to be accurate on the peculiar construct */
10199 if (TREE_CHAIN (q)
10200 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
10201 && !JREFERENCE_TYPE_P (type))
10203 parse_error_context (qual_wfl, "Attempt to reference field %<new%> in a %qs",
10204 lang_printable_name (type, 0));
10205 return 1;
10208 /* `q' might have changed due to a after package resolution
10209 re-qualification */
10210 if (!q)
10211 break;
10213 *found_decl = decl;
10214 return 0;
10217 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
10218 can't be accessed from REFERENCE (a record type). If MEMBER
10219 features a protected access, we then use WHERE which, if non null,
10220 holds the type of MEMBER's access that is checked against
10221 6.6.2.1. This function should be used when decl is a field or a
10222 method. */
10224 static int
10225 not_accessible_p (tree reference, tree member, tree where, int from_super)
10227 int access_flag = get_access_flags_from_decl (member);
10228 bool is_static = false;
10230 if (TREE_CODE (member) == FIELD_DECL ||
10231 TREE_CODE (member) == VAR_DECL)
10232 is_static = FIELD_STATIC (member);
10233 else
10234 is_static = METHOD_STATIC (member);
10236 /* Access always granted for members declared public */
10237 if (access_flag & ACC_PUBLIC)
10238 return 0;
10240 /* Check access on protected members */
10241 if (access_flag & ACC_PROTECTED)
10243 /* Access granted if it occurs from within the package
10244 containing the class in which the protected member is
10245 declared */
10246 if (class_in_current_package (DECL_CONTEXT (member)))
10247 return 0;
10249 /* If accessed with the form `super.member', then access is granted */
10250 if (from_super)
10251 return 0;
10253 /* If WHERE is active, access was made through a qualifier. For
10254 non-static members, access is granted if the type of the qualifier
10255 is or is a sublass of the type the access is made from (6.6.2.1.) */
10256 if (where && !is_static)
10258 while (reference)
10260 if (inherits_from_p (where, reference))
10261 return 0;
10262 if (INNER_CLASS_TYPE_P (reference))
10263 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10264 else
10265 break;
10267 return 1;
10270 /* Otherwise, access is granted if occurring from within the class
10271 where member is declared, or a subclass of it. */
10272 while (reference)
10274 if (inherits_from_p (reference, DECL_CONTEXT (member)))
10275 return 0;
10276 if (INNER_CLASS_TYPE_P (reference))
10277 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10278 else
10279 break;
10281 return 1;
10284 /* Check access on private members. Access is granted only if it
10285 occurs from within the class in which it is declared -- that does
10286 it for innerclasses too. */
10287 if (access_flag & ACC_PRIVATE)
10289 if (reference == DECL_CONTEXT (member) ||
10290 common_enclosing_context_p (DECL_CONTEXT (member), reference))
10291 return 0;
10292 return 1;
10295 /* Default access is permitted only when occurring from within the
10296 package in which the context (MEMBER) is declared. */
10297 return !class_in_current_package (DECL_CONTEXT (member));
10300 /* Test deprecated decl access. */
10301 static void
10302 check_deprecation (tree wfl, tree decl)
10304 const char *file;
10305 tree elt;
10307 if (! warn_deprecated)
10308 return;
10310 /* We want to look at the element type of arrays here, so we strip
10311 all surrounding array types. */
10312 if (TYPE_ARRAY_P (TREE_TYPE (decl)))
10314 elt = TREE_TYPE (decl);
10315 while (TYPE_ARRAY_P (elt))
10316 elt = TYPE_ARRAY_ELEMENT (elt);
10317 /* We'll end up with a pointer type, so we use TREE_TYPE to go
10318 to the record. */
10319 decl = TYPE_NAME (TREE_TYPE (elt));
10321 file = DECL_SOURCE_FILE (decl);
10323 /* Complain if the field is deprecated and the file it was defined
10324 in isn't compiled at the same time the file which contains its
10325 use is */
10326 if (DECL_DEPRECATED (decl)
10327 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10329 const char *the;
10330 switch (TREE_CODE (decl))
10332 case FUNCTION_DECL:
10333 the = "method";
10334 break;
10335 case FIELD_DECL:
10336 case VAR_DECL:
10337 the = "field";
10338 break;
10339 case TYPE_DECL:
10340 parse_warning_context (wfl, "The class %qs has been deprecated",
10341 IDENTIFIER_POINTER (DECL_NAME (decl)));
10342 return;
10343 default:
10344 abort ();
10346 /* Don't issue a message if the context as been deprecated as a
10347 whole. */
10348 if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10349 parse_warning_context
10350 (wfl, "The %s %qs in class %qs has been deprecated",
10351 the, lang_printable_name (decl, 0),
10352 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10356 /* Returns 1 if class was declared in the current package, 0 otherwise */
10358 static GTY(()) tree cicp_cache;
10359 static int
10360 class_in_current_package (tree class)
10362 int qualified_flag;
10363 tree left;
10365 if (cicp_cache == class)
10366 return 1;
10368 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10370 /* If the current package is empty and the name of CLASS is
10371 qualified, class isn't in the current package. If there is a
10372 current package and the name of the CLASS is not qualified, class
10373 isn't in the current package */
10374 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10375 return 0;
10377 /* If there is not package and the name of CLASS isn't qualified,
10378 they belong to the same unnamed package */
10379 if (!ctxp->package && !qualified_flag)
10380 return 1;
10382 /* Compare the left part of the name of CLASS with the package name */
10383 split_qualified_name (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10384 if (ctxp->package == left)
10386 cicp_cache = class;
10387 return 1;
10389 return 0;
10392 /* This function may generate code to access DECL from WHERE. This is
10393 done only if certain conditions meet. */
10395 static tree
10396 maybe_access_field (tree decl, tree where, tree type)
10398 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10399 && !FIELD_STATIC (decl))
10400 decl = build_field_ref (where ? where : current_this,
10401 (type ? type : DECL_CONTEXT (decl)),
10402 DECL_NAME (decl));
10403 return decl;
10406 /* Build a method invocation, by patching PATCH. If non NULL
10407 and according to the situation, PRIMARY and WHERE may be
10408 used. IS_STATIC is set to 1 if the invoked function is static. */
10410 static tree
10411 patch_method_invocation (tree patch, tree primary, tree where, int from_super,
10412 int *is_static, tree *ret_decl)
10414 tree wfl = TREE_OPERAND (patch, 0);
10415 tree args = TREE_OPERAND (patch, 1);
10416 tree name = EXPR_WFL_NODE (wfl);
10417 tree list;
10418 int is_static_flag = 0;
10419 int is_super_init = 0;
10420 tree this_arg = NULL_TREE;
10421 int is_array_clone_call = 0;
10423 /* Should be overridden if everything goes well. Otherwise, if
10424 something fails, it should keep this value. It stop the
10425 evaluation of a bogus assignment. See java_complete_tree,
10426 MODIFY_EXPR: for the reasons why we sometimes want to keep on
10427 evaluating an assignment */
10428 TREE_TYPE (patch) = error_mark_node;
10430 /* Since lookup functions are messing with line numbers, save the
10431 context now. */
10432 java_parser_context_save_global ();
10434 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10436 /* Resolution of qualified name, excluding constructors */
10437 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10439 tree identifier, identifier_wfl, type, resolved;
10440 /* Extract the last IDENTIFIER of the qualified
10441 expression. This is a wfl and we will use it's location
10442 data during error report. */
10443 identifier_wfl = cut_identifier_in_qualified (wfl);
10444 identifier = EXPR_WFL_NODE (identifier_wfl);
10446 /* Given the context, IDENTIFIER is syntactically qualified
10447 as a MethodName. We need to qualify what's before */
10448 qualify_ambiguous_name (wfl);
10449 resolved = resolve_field_access (wfl, NULL, NULL);
10451 if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10452 && FIELD_FINAL (resolved)
10453 && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10454 && !flag_emit_class_files)
10455 resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10457 if (resolved == error_mark_node)
10458 PATCH_METHOD_RETURN_ERROR ();
10460 type = GET_SKIP_TYPE (resolved);
10461 resolve_and_layout (type, NULL_TREE);
10463 if (JPRIMITIVE_TYPE_P (type))
10465 parse_error_context
10466 (identifier_wfl,
10467 "Can't invoke a method on primitive type %qs",
10468 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10469 PATCH_METHOD_RETURN_ERROR ();
10472 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10473 args = nreverse (args);
10475 /* We're resolving a call from a type */
10476 if (TREE_CODE (resolved) == TYPE_DECL)
10478 if (CLASS_INTERFACE (resolved))
10480 parse_error_context
10481 (identifier_wfl,
10482 "Can't make static reference to method %qs in interface %qs",
10483 IDENTIFIER_POINTER (identifier),
10484 IDENTIFIER_POINTER (name));
10485 PATCH_METHOD_RETURN_ERROR ();
10487 if (list && !METHOD_STATIC (list))
10489 char *fct_name = xstrdup (lang_printable_name (list, 2));
10490 parse_error_context
10491 (identifier_wfl,
10492 "Can't make static reference to method %<%s %s%> in class %qs",
10493 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10494 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10495 free (fct_name);
10496 PATCH_METHOD_RETURN_ERROR ();
10499 else
10500 this_arg = primary = resolved;
10502 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10503 is_array_clone_call = 1;
10505 /* IDENTIFIER_WFL will be used to report any problem further */
10506 wfl = identifier_wfl;
10508 /* Resolution of simple names, names generated after a primary: or
10509 constructors */
10510 else
10512 tree class_to_search = NULL_TREE;
10513 int lc; /* Looking for Constructor */
10515 /* We search constructor in their target class */
10516 if (CALL_CONSTRUCTOR_P (patch))
10518 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10519 class_to_search = EXPR_WFL_NODE (wfl);
10520 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10521 this_identifier_node)
10522 class_to_search = NULL_TREE;
10523 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10524 super_identifier_node)
10526 is_super_init = 1;
10527 if (CLASSTYPE_SUPER (current_class))
10528 class_to_search =
10529 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10530 else
10532 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10533 PATCH_METHOD_RETURN_ERROR ();
10537 /* Class to search is NULL if we're searching the current one */
10538 if (class_to_search)
10540 class_to_search = resolve_and_layout (class_to_search, wfl);
10542 if (!class_to_search)
10544 parse_error_context
10545 (wfl, "Class %qs not found in type declaration",
10546 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10547 PATCH_METHOD_RETURN_ERROR ();
10550 /* Can't instantiate an abstract class, but we can
10551 invoke it's constructor. It's use within the `new'
10552 context is denied here. */
10553 if (CLASS_ABSTRACT (class_to_search)
10554 && TREE_CODE (patch) == NEW_CLASS_EXPR)
10556 parse_error_context
10557 (wfl, "Class %qs is an abstract class. It can't be instantiated",
10558 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10559 PATCH_METHOD_RETURN_ERROR ();
10562 class_to_search = TREE_TYPE (class_to_search);
10564 else
10565 class_to_search = current_class;
10566 lc = 1;
10568 /* This is a regular search in the local class, unless an
10569 alternate class is specified. */
10570 else
10572 if (where != NULL_TREE)
10573 class_to_search = where;
10574 else if (QUALIFIED_P (name))
10575 class_to_search = current_class;
10576 else
10578 class_to_search = current_class;
10580 for (;;)
10582 if (has_method (class_to_search, name))
10583 break;
10584 if (! INNER_CLASS_TYPE_P (class_to_search))
10586 parse_error_context (wfl,
10587 "No method named %qs in scope",
10588 IDENTIFIER_POINTER (name));
10589 PATCH_METHOD_RETURN_ERROR ();
10591 class_to_search
10592 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10595 lc = 0;
10598 /* NAME is a simple identifier or comes from a primary. Search
10599 in the class whose declaration contain the method being
10600 invoked. */
10601 resolve_and_layout (class_to_search, NULL_TREE);
10603 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10604 /* Don't continue if no method were found, as the next statement
10605 can't be executed then. */
10606 if (!list)
10607 PATCH_METHOD_RETURN_ERROR ();
10609 if (TYPE_ARRAY_P (class_to_search)
10610 && DECL_NAME (list) == get_identifier ("clone"))
10611 is_array_clone_call = 1;
10613 /* Check for static reference if non static methods */
10614 if (check_for_static_method_reference (wfl, patch, list,
10615 class_to_search, primary))
10616 PATCH_METHOD_RETURN_ERROR ();
10618 /* Check for inner classes creation from illegal contexts */
10619 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10620 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10621 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10622 && !DECL_INIT_P (current_function_decl))
10624 parse_error_context
10625 (wfl, "No enclosing instance for inner class %qs is in scope%s",
10626 lang_printable_name (class_to_search, 0),
10627 (!current_this ? "" :
10628 "; an explicit one must be provided when creating this inner class"));
10629 PATCH_METHOD_RETURN_ERROR ();
10632 /* Non static methods are called with the current object extra
10633 argument. If patch a `new TYPE()', the argument is the value
10634 returned by the object allocator. If method is resolved as a
10635 primary, use the primary otherwise use the current THIS. */
10636 args = nreverse (args);
10637 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10639 this_arg = primary ? primary : current_this;
10641 /* If we're using an access method, things are different.
10642 There are two family of cases:
10644 1) We're not generating bytecodes:
10646 - LIST is non static. It's invocation is transformed from
10647 x(a1,...,an) into this$<n>.x(a1,....an).
10648 - LIST is static. It's invocation is transformed from
10649 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10651 2) We're generating bytecodes:
10653 - LIST is non static. It's invocation is transformed from
10654 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10655 - LIST is static. It's invocation is transformed from
10656 x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10658 Of course, this$<n> can be arbitrarily complex, ranging from
10659 this$0 (the immediate outer context) to
10660 access$0(access$0(...(this$0))).
10662 maybe_use_access_method returns a nonzero value if the
10663 this_arg has to be moved into the (then generated) stub
10664 argument list. In the meantime, the selected function
10665 might have be replaced by a generated stub. */
10666 if (!primary &&
10667 maybe_use_access_method (is_super_init, &list, &this_arg))
10669 args = tree_cons (NULL_TREE, this_arg, args);
10670 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10675 /* Merge point of all resolution schemes. If we have nothing, this
10676 is an error, already signaled */
10677 if (!list)
10678 PATCH_METHOD_RETURN_ERROR ();
10680 /* Check accessibility, position the is_static flag, build and
10681 return the call */
10682 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10683 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10684 NULL_TREE), from_super)
10685 /* Calls to clone() on array types are permitted as a special-case. */
10686 && !is_array_clone_call)
10688 const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10689 const char *const access =
10690 accessibility_string (get_access_flags_from_decl (list));
10691 const char *const klass =
10692 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10693 const char *const refklass =
10694 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10695 const char *const what = (DECL_CONSTRUCTOR_P (list)
10696 ? "constructor" : "method");
10697 parse_error_context (wfl,
10698 "Can't access %s %s %<%s.%s%> from %qs",
10699 access, what, klass, fct_name, refklass);
10700 PATCH_METHOD_RETURN_ERROR ();
10703 /* Deprecation check: check whether the method being invoked or the
10704 instance-being-created's type are deprecated. */
10705 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10706 check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10707 check_deprecation (wfl, list);
10709 /* If invoking a innerclass constructor, there are hidden parameters
10710 to pass */
10711 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10712 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10714 /* And make sure we add the accessed local variables to be saved
10715 in field aliases. */
10716 args = build_alias_initializer_parameter_list
10717 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10719 /* Secretly pass the current_this/primary as a second argument */
10720 if (primary || current_this)
10722 tree extra_arg;
10723 tree this_type = (current_this ?
10724 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10725 /* Method's (list) enclosing context */
10726 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10727 /* If we have a primary, use it. */
10728 if (primary)
10729 extra_arg = primary;
10730 /* The current `this' is an inner class but isn't a direct
10731 enclosing context for the inner class we're trying to
10732 create. Build an access to the proper enclosing context
10733 and use it. */
10734 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10735 && this_type != TREE_TYPE (mec))
10738 extra_arg = build_access_to_thisn (current_class,
10739 TREE_TYPE (mec), 0);
10740 extra_arg = java_complete_tree (extra_arg);
10742 /* Otherwise, just use the current `this' as an enclosing
10743 context. */
10744 else
10745 extra_arg = current_this;
10746 args = tree_cons (NULL_TREE, extra_arg, args);
10748 else
10749 args = tree_cons (NULL_TREE, integer_zero_node, args);
10752 /* This handles the situation where a constructor invocation needs
10753 to have an enclosing context passed as a second parameter (the
10754 constructor is one of an inner class). */
10755 if ((is_super_init ||
10756 (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10757 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10759 tree dest = TYPE_NAME (DECL_CONTEXT (list));
10760 tree extra_arg =
10761 build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10762 extra_arg = java_complete_tree (extra_arg);
10763 args = tree_cons (NULL_TREE, extra_arg, args);
10766 is_static_flag = METHOD_STATIC (list);
10767 if (! is_static_flag && this_arg != NULL_TREE)
10768 args = tree_cons (NULL_TREE, this_arg, args);
10770 /* In the context of an explicit constructor invocation, we can't
10771 invoke any method relying on `this'. Exceptions are: we're
10772 invoking a static function, primary exists and is not the current
10773 this, we're creating a new object. */
10774 if (ctxp->explicit_constructor_p
10775 && !is_static_flag
10776 && (!primary || primary == current_this)
10777 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10779 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
10780 PATCH_METHOD_RETURN_ERROR ();
10782 java_parser_context_restore_global ();
10783 if (is_static)
10784 *is_static = is_static_flag;
10785 /* Sometimes, we want the decl of the selected method. Such as for
10786 EH checking */
10787 if (ret_decl)
10788 *ret_decl = list;
10789 patch = patch_invoke (patch, list, args);
10791 /* Now is a good time to insert the call to finit$ */
10792 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10794 tree finit_parms, finit_call;
10796 /* Prepare to pass hidden parameters to finit$, if any. */
10797 finit_parms = build_alias_initializer_parameter_list
10798 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10800 finit_call =
10801 build_method_invocation (build_wfl_node (finit_identifier_node),
10802 finit_parms);
10804 /* Generate the code used to initialize fields declared with an
10805 initialization statement and build a compound statement along
10806 with the super constructor invocation. */
10807 CAN_COMPLETE_NORMALLY (patch) = 1;
10808 patch = build2 (COMPOUND_EXPR, void_type_node, patch,
10809 java_complete_tree (finit_call));
10811 return patch;
10814 /* Check that we're not trying to do a static reference to a method in
10815 non static method. Return 1 if it's the case, 0 otherwise. */
10817 static int
10818 check_for_static_method_reference (tree wfl, tree node, tree method,
10819 tree where, tree primary)
10821 if (METHOD_STATIC (current_function_decl)
10822 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10824 char *fct_name = xstrdup (lang_printable_name (method, 0));
10825 parse_error_context
10826 (wfl, "Can't make static reference to method %<%s %s%> in class %qs",
10827 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10828 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10829 free (fct_name);
10830 return 1;
10832 return 0;
10835 /* Fix the invocation of *MDECL if necessary in the case of a
10836 invocation from an inner class. *THIS_ARG might be modified
10837 appropriately and an alternative access to *MDECL might be
10838 returned. */
10840 static int
10841 maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10843 tree ctx;
10844 tree md = *mdecl, ta = *this_arg;
10845 int to_return = 0;
10846 int non_static_context = !METHOD_STATIC (md);
10848 if (is_super_init
10849 || DECL_CONTEXT (md) == current_class
10850 || !PURE_INNER_CLASS_TYPE_P (current_class)
10851 || DECL_FINIT_P (md)
10852 || DECL_INSTINIT_P (md))
10853 return 0;
10855 /* If we're calling a method found in an enclosing class, generate
10856 what it takes to retrieve the right this. Don't do that if we're
10857 invoking a static method. Note that if MD's type is unrelated to
10858 CURRENT_CLASS, then the current this can be used. */
10860 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10862 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10863 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10865 ta = build_current_thisn (current_class);
10866 ta = build_wfl_node (ta);
10868 else
10870 tree type = ctx;
10871 while (type)
10873 maybe_build_thisn_access_method (type);
10874 if (inherits_from_p (type, DECL_CONTEXT (md)))
10876 ta = build_access_to_thisn (ctx, type, 0);
10877 break;
10879 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10880 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10883 ta = java_complete_tree (ta);
10886 /* We might have to use an access method to get to MD. We can
10887 break the method access rule as far as we're not generating
10888 bytecode */
10889 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10891 md = build_outer_method_access_method (md);
10892 to_return = 1;
10895 *mdecl = md;
10896 *this_arg = ta;
10898 /* Returning a nonzero value indicates we were doing a non static
10899 method invocation that is now a static invocation. It will have
10900 callee displace `this' to insert it in the regular argument
10901 list. */
10902 return (non_static_context && to_return);
10905 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10906 mode. */
10908 static tree
10909 patch_invoke (tree patch, tree method, tree args)
10911 tree dtable, func;
10912 tree original_call, t, ta;
10913 tree check = NULL_TREE;
10915 /* Last step for args: convert build-in types. If we're dealing with
10916 a new TYPE() type call, the first argument to the constructor
10917 isn't found in the incoming argument list, but delivered by
10918 `new' */
10919 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10920 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10921 t = TREE_CHAIN (t);
10922 for (ta = args; t != end_params_node && ta;
10923 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10924 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10925 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10926 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10928 /* Resolve unresolved returned type issues */
10929 t = TREE_TYPE (TREE_TYPE (method));
10930 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10931 resolve_and_layout (TREE_TYPE (t), NULL);
10933 if (flag_emit_class_files)
10934 func = method;
10935 else
10937 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10939 case INVOKE_VIRTUAL:
10940 dtable = invoke_build_dtable (0, args);
10941 func = build_invokevirtual (dtable, method);
10942 break;
10944 case INVOKE_NONVIRTUAL:
10945 /* If the object for the method call is null, we throw an
10946 exception. We don't do this if the object is the current
10947 method's `this'. In other cases we just rely on an
10948 optimization pass to eliminate redundant checks. */
10949 if (TREE_VALUE (args) != current_this)
10951 /* We use a save_expr here to make sure we only evaluate
10952 the new `self' expression once. */
10953 tree save_arg = save_expr (TREE_VALUE (args));
10954 TREE_VALUE (args) = save_arg;
10955 check = java_check_reference (save_arg, 1);
10957 /* Fall through. */
10959 case INVOKE_SUPER:
10960 case INVOKE_STATIC:
10962 tree signature = build_java_signature (TREE_TYPE (method));
10963 func = build_known_method_ref (method, TREE_TYPE (method),
10964 DECL_CONTEXT (method),
10965 signature, args);
10967 break;
10969 case INVOKE_INTERFACE:
10970 dtable = invoke_build_dtable (1, args);
10971 func = build_invokeinterface (dtable, method);
10972 break;
10974 default:
10975 abort ();
10978 /* Ensure self_type is initialized, (invokestatic). FIXME */
10979 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10982 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10983 TREE_OPERAND (patch, 0) = func;
10984 TREE_OPERAND (patch, 1) = args;
10985 patch = check_for_builtin (method, patch);
10986 original_call = patch;
10988 /* We're processing a `new TYPE ()' form. New is called and its
10989 returned value is the first argument to the constructor. We build
10990 a COMPOUND_EXPR and use saved expression so that the overall NEW
10991 expression value is a pointer to a newly created and initialized
10992 class. */
10993 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10995 tree class = DECL_CONTEXT (method);
10996 tree c1, saved_new, new;
10997 tree alloc_node;
10999 if (flag_emit_class_files)
11001 TREE_TYPE (patch) = build_pointer_type (class);
11002 return patch;
11004 if (!TYPE_SIZE (class))
11005 safe_layout_class (class);
11006 alloc_node =
11007 (class_has_finalize_method (class) ? alloc_object_node
11008 : alloc_no_finalizer_node);
11009 new = build3 (CALL_EXPR, promote_type (class),
11010 build_address_of (alloc_node),
11011 build_tree_list (NULL_TREE, build_class_ref (class)),
11012 NULL_TREE);
11013 saved_new = save_expr (new);
11014 c1 = build_tree_list (NULL_TREE, saved_new);
11015 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
11016 TREE_OPERAND (original_call, 1) = c1;
11017 TREE_SET_CODE (original_call, CALL_EXPR);
11018 patch = build2 (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
11021 /* If CHECK is set, then we are building a check to see if the object
11022 is NULL. */
11023 if (check != NULL_TREE)
11025 /* We have to call force_evaluation_order now because creating a
11026 COMPOUND_EXPR wraps the arg list in a way that makes it
11027 unrecognizable by force_evaluation_order later. Yuk. */
11028 patch = build2 (COMPOUND_EXPR, TREE_TYPE (patch), check,
11029 force_evaluation_order (patch));
11030 TREE_SIDE_EFFECTS (patch) = 1;
11033 /* In order to be able to modify PATCH later, we SAVE_EXPR it and
11034 put it as the first expression of a COMPOUND_EXPR. The second
11035 expression being an empty statement to be later patched if
11036 necessary. We remember a TREE_LIST (the PURPOSE is the method,
11037 the VALUE is the compound) in a hashtable and return a
11038 COMPOUND_EXPR built so that the result of the evaluation of the
11039 original PATCH node is returned. */
11040 if (STATIC_CLASS_INIT_OPT_P ()
11041 && current_function_decl && METHOD_STATIC (method))
11043 tree list;
11044 tree fndecl = current_function_decl;
11045 /* We have to call force_evaluation_order now because creating a
11046 COMPOUND_EXPR wraps the arg list in a way that makes it
11047 unrecognizable by force_evaluation_order later. Yuk. */
11048 tree save = force_evaluation_order (patch);
11049 tree type = TREE_TYPE (patch);
11051 patch = build2 (COMPOUND_EXPR, type, save, build_java_empty_stmt ());
11052 list = tree_cons (method, patch,
11053 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
11055 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
11057 patch = build2 (COMPOUND_EXPR, type, patch, save);
11060 return patch;
11063 static int
11064 invocation_mode (tree method, int super)
11066 int access = get_access_flags_from_decl (method);
11068 if (super)
11069 return INVOKE_SUPER;
11071 if (access & ACC_STATIC)
11072 return INVOKE_STATIC;
11074 /* We have to look for a constructor before we handle nonvirtual
11075 calls; otherwise the constructor will look nonvirtual. */
11076 if (DECL_CONSTRUCTOR_P (method))
11077 return INVOKE_STATIC;
11079 if (access & ACC_PRIVATE)
11080 return INVOKE_NONVIRTUAL;
11082 /* Binary compatibility: just because it's final today, that doesn't
11083 mean it'll be final tomorrow. */
11084 if (! flag_indirect_dispatch
11085 || DECL_CONTEXT (method) == object_type_node)
11087 if (access & ACC_FINAL)
11088 return INVOKE_NONVIRTUAL;
11090 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
11091 return INVOKE_NONVIRTUAL;
11094 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
11095 return INVOKE_INTERFACE;
11097 return INVOKE_VIRTUAL;
11100 /* Retrieve a refined list of matching methods. It covers the step
11101 15.11.2 (Compile-Time Step 2) */
11103 static tree
11104 lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
11106 tree atl = end_params_node; /* Arg Type List */
11107 tree method, signature, list, node;
11108 const char *candidates; /* Used for error report */
11109 char *dup;
11111 /* Fix the arguments */
11112 for (node = arg_list; node; node = TREE_CHAIN (node))
11114 tree current_arg = TREE_TYPE (TREE_VALUE (node));
11115 /* Non primitive type may have to be resolved */
11116 if (!JPRIMITIVE_TYPE_P (current_arg))
11117 resolve_and_layout (current_arg, NULL_TREE);
11118 /* And promoted */
11119 if (TREE_CODE (current_arg) == RECORD_TYPE)
11120 current_arg = promote_type (current_arg);
11121 atl = tree_cons (NULL_TREE, current_arg, atl);
11124 /* Presto. If we're dealing with an anonymous class and a
11125 constructor call, generate the right constructor now, since we
11126 know the arguments' types. */
11128 if (lc && ANONYMOUS_CLASS_P (class))
11130 tree mdecl = craft_constructor (TYPE_NAME (class), atl);
11131 /* The anonymous class may have already been laid out, so make sure
11132 the new constructor is laid out here. */
11133 layout_class_method (class, CLASSTYPE_SUPER (class), mdecl, NULL_TREE);
11136 /* Find all candidates and then refine the list, searching for the
11137 most specific method. */
11138 list = find_applicable_accessible_methods_list (lc, class, name, atl);
11139 list = find_most_specific_methods_list (list);
11140 if (list && !TREE_CHAIN (list))
11141 return TREE_VALUE (list);
11143 /* Issue an error. List candidates if any. Candidates are listed
11144 only if accessible (non accessible methods may end-up here for
11145 the sake of a better error report). */
11146 candidates = NULL;
11147 if (list)
11149 tree current;
11150 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
11151 for (current = list; current; current = TREE_CHAIN (current))
11153 tree cm = TREE_VALUE (current);
11154 char string [4096];
11155 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
11156 continue;
11157 sprintf
11158 (string, " '%s' in '%s'%s",
11159 get_printable_method_name (cm),
11160 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
11161 (TREE_CHAIN (current) ? "\n" : ""));
11162 obstack_grow (&temporary_obstack, string, strlen (string));
11164 obstack_1grow (&temporary_obstack, '\0');
11165 candidates = obstack_finish (&temporary_obstack);
11167 /* Issue the error message */
11168 method = make_node (FUNCTION_TYPE);
11169 TYPE_ARG_TYPES (method) = atl;
11170 signature = build_java_argument_signature (method);
11171 dup = xstrdup (lang_printable_name (class, 0));
11172 parse_error_context (cl, "Can't find %s %<%s(%s)%> in type %qs%s",
11173 (lc ? "constructor" : "method"),
11174 (lc ? dup : IDENTIFIER_POINTER (name)),
11175 IDENTIFIER_POINTER (signature), dup,
11176 (candidates ? candidates : ""));
11177 free (dup);
11178 return NULL_TREE;
11181 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
11182 when we're looking for a constructor. */
11184 static tree
11185 find_applicable_accessible_methods_list (int lc, tree class, tree name,
11186 tree arglist)
11188 static htab_t searched_classes;
11189 static int search_not_done = 0;
11190 tree list = NULL_TREE, all_list = NULL_TREE;
11191 tree base_binfo;
11192 int i;
11194 /* Check the hash table to determine if this class has been searched
11195 already. */
11196 if (searched_classes)
11198 if (htab_find (searched_classes, class) != NULL)
11199 return NULL;
11201 else
11203 searched_classes = htab_create (10, htab_hash_pointer,
11204 htab_eq_pointer, NULL);
11207 search_not_done++;
11208 *htab_find_slot (searched_classes, class, INSERT) = class;
11210 if (!CLASS_LOADED_P (class))
11212 load_class (class, 1);
11213 safe_layout_class (class);
11216 /* Search interfaces */
11217 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
11218 && CLASS_INTERFACE (TYPE_NAME (class)))
11220 search_applicable_methods_list (lc, TYPE_METHODS (class),
11221 name, arglist, &list, &all_list);
11222 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11224 tree t = BINFO_TYPE (base_binfo);
11225 tree rlist;
11227 rlist = find_applicable_accessible_methods_list (lc, t, name,
11228 arglist);
11229 list = chainon (rlist, list);
11232 /* Search classes */
11233 else
11235 search_applicable_methods_list (lc, TYPE_METHODS (class),
11236 name, arglist, &list, &all_list);
11238 /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11239 that we only search in class. Note that we should have found
11240 something at this point. */
11241 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11243 lc = 1;
11244 if (!list)
11245 abort ();
11248 /* We must search all interfaces of this class */
11249 if (!lc)
11251 for (i = 1;
11252 BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11254 tree t = BINFO_TYPE (base_binfo);
11255 if (t != object_type_node)
11257 tree rlist
11258 = find_applicable_accessible_methods_list (lc, t,
11259 name, arglist);
11260 list = chainon (rlist, list);
11265 /* Search superclass */
11266 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11268 tree rlist;
11269 class = CLASSTYPE_SUPER (class);
11270 rlist = find_applicable_accessible_methods_list (lc, class,
11271 name, arglist);
11272 list = chainon (rlist, list);
11276 search_not_done--;
11278 /* We're done. Reset the searched classes list and finally search
11279 java.lang.Object if it wasn't searched already. */
11280 if (!search_not_done)
11282 if (!lc
11283 && TYPE_METHODS (object_type_node)
11284 && htab_find (searched_classes, object_type_node) == NULL)
11286 search_applicable_methods_list (lc,
11287 TYPE_METHODS (object_type_node),
11288 name, arglist, &list, &all_list);
11290 htab_delete (searched_classes);
11291 searched_classes = NULL;
11294 /* Either return the list obtained or all selected (but
11295 inaccessible) methods for better error report. */
11296 return (!list ? all_list : list);
11299 /* Effectively search for the appropriate method in method */
11301 static void
11302 search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
11303 tree *list, tree *all_list)
11305 for (; method; method = TREE_CHAIN (method))
11307 /* When dealing with constructor, stop here, otherwise search
11308 other classes */
11309 if (lc && !DECL_CONSTRUCTOR_P (method))
11310 continue;
11311 else if (!lc && (DECL_CONSTRUCTOR_P (method)
11312 || (DECL_NAME (method) != name)))
11313 continue;
11315 if (argument_types_convertible (method, arglist))
11317 /* Retain accessible methods only */
11318 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11319 method, NULL_TREE, 0))
11320 *list = tree_cons (NULL_TREE, method, *list);
11321 else
11322 /* Also retain all selected method here */
11323 *all_list = tree_cons (NULL_TREE, method, *list);
11328 /* 15.11.2.2 Choose the Most Specific Method */
11330 static tree
11331 find_most_specific_methods_list (tree list)
11333 int max = 0;
11334 int abstract, candidates;
11335 tree current, new_list = NULL_TREE;
11336 for (current = list; current; current = TREE_CHAIN (current))
11338 tree method;
11339 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11341 for (method = list; method; method = TREE_CHAIN (method))
11343 tree method_v, current_v;
11344 /* Don't test a method against itself */
11345 if (method == current)
11346 continue;
11348 method_v = TREE_VALUE (method);
11349 current_v = TREE_VALUE (current);
11351 /* Compare arguments and location where methods where declared */
11352 if (argument_types_convertible (method_v, current_v))
11354 if (valid_method_invocation_conversion_p
11355 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
11356 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
11357 && enclosing_context_p (DECL_CONTEXT (method_v),
11358 DECL_CONTEXT (current_v))))
11360 int v = (DECL_SPECIFIC_COUNT (current_v) +=
11361 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
11362 max = (v > max ? v : max);
11368 /* Review the list and select the maximally specific methods */
11369 for (current = list, abstract = -1, candidates = -1;
11370 current; current = TREE_CHAIN (current))
11371 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11373 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11374 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11375 candidates++;
11378 /* If we have several and they're all abstract, just pick the
11379 closest one. */
11380 if (candidates > 0 && candidates == abstract)
11382 /* FIXME: merge the throws clauses. There is no convenient way
11383 to do this in gcj right now, since ideally we'd like to
11384 introduce a new METHOD_DECL here, but that is really not
11385 possible. */
11386 new_list = nreverse (new_list);
11387 TREE_CHAIN (new_list) = NULL_TREE;
11388 return new_list;
11391 /* We have several (we couldn't find a most specific), all but one
11392 are abstract, we pick the only non abstract one. */
11393 if (candidates > 0 && (candidates == abstract+1))
11395 for (current = new_list; current; current = TREE_CHAIN (current))
11396 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11398 TREE_CHAIN (current) = NULL_TREE;
11399 new_list = current;
11403 /* If we can't find one, lower expectations and try to gather multiple
11404 maximally specific methods */
11405 while (!new_list && max)
11407 while (--max > 0)
11409 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11410 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11414 return new_list;
11417 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11418 converted by method invocation conversion (5.3) to the type of the
11419 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11420 to change less often than M1. */
11422 static GTY(()) tree m2_arg_value;
11423 static GTY(()) tree m2_arg_cache;
11425 static int
11426 argument_types_convertible (tree m1, tree m2_or_arglist)
11428 tree m1_arg, m2_arg;
11430 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11432 if (m2_arg_value == m2_or_arglist)
11433 m2_arg = m2_arg_cache;
11434 else
11436 /* M2_OR_ARGLIST can be a function DECL or a raw list of
11437 argument types */
11438 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11440 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11441 if (!METHOD_STATIC (m2_or_arglist))
11442 m2_arg = TREE_CHAIN (m2_arg);
11444 else
11445 m2_arg = m2_or_arglist;
11447 m2_arg_value = m2_or_arglist;
11448 m2_arg_cache = m2_arg;
11451 while (m1_arg != end_params_node && m2_arg != end_params_node)
11453 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11454 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11455 TREE_VALUE (m2_arg)))
11456 break;
11457 m1_arg = TREE_CHAIN (m1_arg);
11458 m2_arg = TREE_CHAIN (m2_arg);
11460 return m1_arg == end_params_node && m2_arg == end_params_node;
11463 /* Qualification routines */
11465 /* Given a name x.y.z, look up x locally. If it's found, save the
11466 decl. If it's not found, mark the name as RESOLVE_PACKAGE_NAME_P,
11467 so that we later try and load the appropriate classes. */
11468 static void
11469 qualify_ambiguous_name (tree id)
11471 tree name, decl;
11473 /* We inspect the first item of the qualification list. As a sanity
11474 check, make sure that it is an identfier node. */
11475 tree qual = EXPR_WFL_QUALIFICATION (id);
11476 tree qual_wfl = QUAL_WFL (qual);
11478 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11479 return;
11481 name = EXPR_WFL_NODE (qual_wfl);
11483 /* If we don't have an identifier, or we have a 'this' or 'super',
11484 then field access processing is all we need : there is nothing
11485 for us to do. */
11486 if (!name || TREE_CODE (name) != IDENTIFIER_NODE ||
11487 name == this_identifier_node ||
11488 name == super_identifier_node)
11489 return;
11491 /* If name appears within the scope of a local variable declaration
11492 or parameter declaration, or is a field within an enclosing
11493 class, then it is an expression name. Save the decl and let
11494 resolve_field_access do it's work. */
11495 if ((decl = IDENTIFIER_LOCAL_VALUE (name)) ||
11496 (decl = lookup_field_wrapper (current_class, name)))
11498 QUAL_RESOLUTION (qual) = decl;
11499 return;
11502 /* If name is a known class name (either declared or imported), mark
11503 us as a type name. */
11504 if ((decl = resolve_and_layout (name, NULL_TREE)))
11506 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11507 QUAL_RESOLUTION (qual) = decl;
11510 /* Check here that NAME isn't declared by more than one
11511 type-import-on-demand declaration of the compilation unit
11512 containing NAME. FIXME */
11514 /* We couldn't find a declaration for the name. Assume for now that
11515 we have a qualified class name that needs to be loaded from an
11516 external class file. */
11517 else
11518 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11520 /* Propagate the qualification across other components of the
11521 qualified name */
11522 for (qual = TREE_CHAIN (qual); qual;
11523 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11525 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11526 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11529 /* Store the global qualification for the ambiguous part of ID back
11530 into ID fields */
11531 if (RESOLVE_TYPE_NAME_P (qual_wfl))
11532 RESOLVE_TYPE_NAME_P (id) = 1;
11533 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11534 RESOLVE_PACKAGE_NAME_P (id) = 1;
11537 /* Patch tree nodes in a function body. When a BLOCK is found, push
11538 local variable decls if present.
11539 Same as java_complete_lhs, but does resolve static finals to values. */
11541 static tree
11542 java_complete_tree (tree node)
11544 node = java_complete_lhs (node);
11545 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11546 && DECL_INITIAL (node) != NULL_TREE)
11548 tree value = fold_constant_for_init (node, node);
11549 if (value != NULL_TREE)
11550 return value;
11552 return node;
11555 static tree
11556 java_stabilize_reference (tree node)
11558 if (TREE_CODE (node) == COMPOUND_EXPR)
11560 tree op0 = TREE_OPERAND (node, 0);
11561 tree op1 = TREE_OPERAND (node, 1);
11562 TREE_OPERAND (node, 0) = save_expr (op0);
11563 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11564 return node;
11566 return stabilize_reference (node);
11569 /* Patch tree nodes in a function body. When a BLOCK is found, push
11570 local variable decls if present.
11571 Same as java_complete_tree, but does not resolve static finals to values. */
11573 static tree
11574 java_complete_lhs (tree node)
11576 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11577 int flag;
11579 /* CONVERT_EXPR always has its type set, even though it needs to be
11580 worked out. */
11581 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11582 return node;
11584 /* The switch block implements cases processing container nodes
11585 first. Contained nodes are always written back. Leaves come
11586 next and return a value. */
11587 switch (TREE_CODE (node))
11589 case BLOCK:
11591 /* 1- Block section.
11592 Set the local values on decl names so we can identify them
11593 faster when they're referenced. At that stage, identifiers
11594 are legal so we don't check for declaration errors. */
11595 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11597 DECL_CONTEXT (cn) = current_function_decl;
11598 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11600 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11601 CAN_COMPLETE_NORMALLY (node) = 1;
11602 else
11604 tree stmt = BLOCK_EXPR_BODY (node);
11605 tree *ptr;
11606 int error_seen = 0;
11607 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11609 /* Re-order from (((A; B); C); ...; Z) to
11610 (A; (B; (C ; (...; Z)))).
11611 This makes it easier to scan the statements left-to-right
11612 without using recursion (which might overflow the stack
11613 if the block has many statements. */
11614 for (;;)
11616 tree left = TREE_OPERAND (stmt, 0);
11617 if (TREE_CODE (left) != COMPOUND_EXPR)
11618 break;
11619 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11620 TREE_OPERAND (left, 1) = stmt;
11621 stmt = left;
11623 BLOCK_EXPR_BODY (node) = stmt;
11626 /* Now do the actual complete, without deep recursion for
11627 long blocks. */
11628 ptr = &BLOCK_EXPR_BODY (node);
11629 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11630 && !IS_EMPTY_STMT (TREE_OPERAND (*ptr, 1)))
11632 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11633 tree *next = &TREE_OPERAND (*ptr, 1);
11634 TREE_OPERAND (*ptr, 0) = cur;
11635 if (IS_EMPTY_STMT (cur))
11637 /* Optimization; makes it easier to detect empty bodies.
11638 Most useful for <clinit> with all-constant initializer. */
11639 *ptr = *next;
11640 continue;
11642 if (TREE_CODE (cur) == ERROR_MARK)
11643 error_seen++;
11644 else if (! CAN_COMPLETE_NORMALLY (cur))
11646 wfl_op2 = *next;
11647 for (;;)
11649 if (TREE_CODE (wfl_op2) == BLOCK)
11650 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11651 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11652 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11653 else
11654 break;
11656 if (TREE_CODE (wfl_op2) != CASE_EXPR
11657 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11658 unreachable_stmt_error (*ptr);
11660 if (TREE_TYPE (*ptr) == NULL_TREE)
11661 TREE_TYPE (*ptr) = void_type_node;
11662 ptr = next;
11664 *ptr = java_complete_tree (*ptr);
11666 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11667 return error_mark_node;
11668 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11670 /* Turn local bindings to null */
11671 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11672 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11674 TREE_TYPE (node) = void_type_node;
11675 break;
11677 /* 2- They are expressions but ultimately deal with statements */
11679 case THROW_EXPR:
11680 wfl_op1 = TREE_OPERAND (node, 0);
11681 COMPLETE_CHECK_OP_0 (node);
11682 /* 14.19 A throw statement cannot complete normally. */
11683 CAN_COMPLETE_NORMALLY (node) = 0;
11684 return patch_throw_statement (node, wfl_op1);
11686 case SYNCHRONIZED_EXPR:
11687 wfl_op1 = TREE_OPERAND (node, 0);
11688 return patch_synchronized_statement (node, wfl_op1);
11690 case TRY_EXPR:
11691 return patch_try_statement (node);
11693 case TRY_FINALLY_EXPR:
11694 COMPLETE_CHECK_OP_0 (node);
11695 COMPLETE_CHECK_OP_1 (node);
11696 if (IS_EMPTY_STMT (TREE_OPERAND (node, 0)))
11697 /* Reduce try/finally nodes with an empty try block. */
11698 return TREE_OPERAND (node, 1);
11699 if (IS_EMPTY_STMT (TREE_OPERAND (node, 1)))
11700 /* Likewise for an empty finally block. */
11701 return TREE_OPERAND (node, 0);
11702 CAN_COMPLETE_NORMALLY (node)
11703 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11704 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11705 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11706 return node;
11708 case LABELED_BLOCK_EXPR:
11709 PUSH_LABELED_BLOCK (node);
11710 if (LABELED_BLOCK_BODY (node))
11711 COMPLETE_CHECK_OP_1 (node);
11712 TREE_TYPE (node) = void_type_node;
11713 POP_LABELED_BLOCK ();
11715 if (IS_EMPTY_STMT (LABELED_BLOCK_BODY (node)))
11717 LABELED_BLOCK_BODY (node) = NULL_TREE;
11718 CAN_COMPLETE_NORMALLY (node) = 1;
11720 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11721 CAN_COMPLETE_NORMALLY (node) = 1;
11722 return node;
11724 case EXIT_BLOCK_EXPR:
11725 return patch_bc_statement (node);
11727 case CASE_EXPR:
11728 cn = java_complete_tree (TREE_OPERAND (node, 0));
11729 if (cn == error_mark_node)
11730 return cn;
11732 /* First, the case expression must be constant. Values of final
11733 fields are accepted. */
11734 cn = fold (cn);
11735 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11736 && JDECL_P (TREE_OPERAND (cn, 1))
11737 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11738 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11740 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11741 TREE_OPERAND (cn, 1));
11743 /* Accept final locals too. */
11744 else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn)
11745 && DECL_INITIAL (cn))
11746 cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11748 if (!TREE_CONSTANT (cn))
11750 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11751 parse_error_context (node, "Constant expression required");
11752 return error_mark_node;
11755 nn = ctxp->current_loop;
11757 /* It must be assignable to the type of the switch expression. */
11758 if (!try_builtin_assignconv (NULL_TREE,
11759 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11761 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11762 parse_error_context
11763 (wfl_operator,
11764 "Incompatible type for case. Can't convert %qs to %<int%>",
11765 lang_printable_name (TREE_TYPE (cn), 0));
11766 return error_mark_node;
11769 cn = fold (convert (int_type_node, cn));
11770 TREE_CONSTANT_OVERFLOW (cn) = 0;
11771 CAN_COMPLETE_NORMALLY (cn) = 1;
11773 /* Save the label on a list so that we can later check for
11774 duplicates. */
11775 case_label_list = tree_cons (node, cn, case_label_list);
11777 /* Multiple instance of a case label bearing the same value is
11778 checked later. The case expression is all right so far. */
11779 if (TREE_CODE (cn) == VAR_DECL)
11780 cn = DECL_INITIAL (cn);
11781 TREE_OPERAND (node, 0) = cn;
11782 TREE_TYPE (node) = void_type_node;
11783 CAN_COMPLETE_NORMALLY (node) = 1;
11784 TREE_SIDE_EFFECTS (node) = 1;
11785 break;
11787 case DEFAULT_EXPR:
11788 nn = ctxp->current_loop;
11789 /* Only one default label is allowed per switch statement */
11790 if (SWITCH_HAS_DEFAULT (nn))
11792 #ifdef USE_MAPPED_LOCATION
11793 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
11794 #else
11795 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11796 #endif
11797 parse_error_context (wfl_operator,
11798 "Duplicate case label: %<default%>");
11799 return error_mark_node;
11801 else
11802 SWITCH_HAS_DEFAULT (nn) = 1;
11803 TREE_TYPE (node) = void_type_node;
11804 TREE_SIDE_EFFECTS (node) = 1;
11805 CAN_COMPLETE_NORMALLY (node) = 1;
11806 break;
11808 case SWITCH_EXPR:
11809 case LOOP_EXPR:
11810 PUSH_LOOP (node);
11811 /* Check whether the loop was enclosed in a labeled
11812 statement. If not, create one, insert the loop in it and
11813 return the node */
11814 nn = patch_loop_statement (node);
11816 /* Anyways, walk the body of the loop */
11817 if (TREE_CODE (node) == LOOP_EXPR)
11818 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11819 /* Switch statement: walk the switch expression and the cases */
11820 else
11821 node = patch_switch_statement (node);
11823 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11824 nn = error_mark_node;
11825 else
11827 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11828 /* If we returned something different, that's because we
11829 inserted a label. Pop the label too. */
11830 if (nn != node)
11832 if (CAN_COMPLETE_NORMALLY (node))
11833 CAN_COMPLETE_NORMALLY (nn) = 1;
11834 POP_LABELED_BLOCK ();
11837 POP_LOOP ();
11838 return nn;
11840 case EXIT_EXPR:
11841 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11842 return patch_exit_expr (node);
11844 case COND_EXPR:
11845 /* Condition */
11846 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11847 if (TREE_OPERAND (node, 0) == error_mark_node)
11848 return error_mark_node;
11849 /* then-else branches */
11850 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11851 if (TREE_OPERAND (node, 1) == error_mark_node)
11852 return error_mark_node;
11854 /* This is a special case due to build_assertion(). When
11855 assertions are disabled we build a COND_EXPR in which
11856 Operand 1 is the body of the assertion. If that happens to
11857 be a string concatenation we'll need to patch it here. */
11858 tree patched = patch_string (TREE_OPERAND (node, 1));
11859 if (patched)
11860 TREE_OPERAND (node, 1) = patched;
11862 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11863 if (TREE_OPERAND (node, 2) == error_mark_node)
11864 return error_mark_node;
11865 return patch_if_else_statement (node);
11866 break;
11868 case CONDITIONAL_EXPR:
11869 /* Condition */
11870 wfl_op1 = TREE_OPERAND (node, 0);
11871 COMPLETE_CHECK_OP_0 (node);
11872 wfl_op2 = TREE_OPERAND (node, 1);
11873 COMPLETE_CHECK_OP_1 (node);
11874 wfl_op3 = TREE_OPERAND (node, 2);
11875 COMPLETE_CHECK_OP_2 (node);
11876 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11878 /* 3- Expression section */
11879 case COMPOUND_EXPR:
11880 wfl_op2 = TREE_OPERAND (node, 1);
11881 TREE_OPERAND (node, 0) = nn =
11882 java_complete_tree (TREE_OPERAND (node, 0));
11883 if (IS_EMPTY_STMT (wfl_op2))
11884 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11885 else
11887 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11889 /* An unreachable condition in a do-while statement
11890 is *not* (technically) an unreachable statement. */
11891 nn = wfl_op2;
11892 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11893 nn = EXPR_WFL_NODE (nn);
11894 /* NN can be NULL_TREE exactly when UPDATE is, in
11895 finish_for_loop. */
11896 if (nn != NULL_TREE && TREE_CODE (nn) != EXIT_EXPR)
11898 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11899 if (SUPPRESS_UNREACHABLE_ERROR (nn))
11901 /* Perhaps this warning should have an
11902 associated flag. The code being compiled is
11903 pedantically correct, but useless. */
11904 parse_warning_context (wfl_operator,
11905 "Unreachable statement");
11907 else
11908 parse_error_context (wfl_operator,
11909 "Unreachable statement");
11912 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11913 if (TREE_OPERAND (node, 1) == error_mark_node)
11914 return error_mark_node;
11915 /* Even though we might allow the case where the first
11916 operand doesn't return normally, we still should compute
11917 CAN_COMPLETE_NORMALLY correctly. */
11918 CAN_COMPLETE_NORMALLY (node)
11919 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11920 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11922 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11923 break;
11925 case RETURN_EXPR:
11926 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11927 return patch_return (node);
11929 case EXPR_WITH_FILE_LOCATION:
11930 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11931 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11933 node = resolve_expression_name (node, NULL);
11934 if (node == error_mark_node)
11935 return node;
11936 CAN_COMPLETE_NORMALLY (node) = 1;
11938 else
11940 tree body;
11941 location_t save_location = input_location;
11942 #ifdef USE_MAPPED_LOCATION
11943 input_location = EXPR_LOCATION (node);
11944 if (input_location == UNKNOWN_LOCATION)
11945 input_location = save_location;
11946 #else
11947 input_line = EXPR_WFL_LINENO (node);
11948 #endif
11949 body = java_complete_tree (EXPR_WFL_NODE (node));
11950 input_location = save_location;
11951 EXPR_WFL_NODE (node) = body;
11952 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11953 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11954 if (IS_EMPTY_STMT (body) || TREE_CONSTANT (body))
11956 /* Makes it easier to constant fold, detect empty bodies. */
11957 return body;
11959 if (body == error_mark_node)
11961 /* Its important for the evaluation of assignment that
11962 this mark on the TREE_TYPE is propagated. */
11963 TREE_TYPE (node) = error_mark_node;
11964 return error_mark_node;
11966 else
11967 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11970 break;
11972 case NEW_ARRAY_EXPR:
11973 /* Patch all the dimensions */
11974 flag = 0;
11975 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11977 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11978 tree dim = convert (int_type_node,
11979 java_complete_tree (TREE_VALUE (cn)));
11980 if (dim == error_mark_node)
11982 flag = 1;
11983 continue;
11985 else
11987 TREE_VALUE (cn) = dim;
11988 /* Setup the location of the current dimension, for
11989 later error report. */
11990 #ifdef USE_MAPPED_LOCATION
11991 TREE_PURPOSE (cn) = expr_add_location (NULL_TREE, location, 0);
11992 #else
11993 TREE_PURPOSE (cn) =
11994 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11995 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11996 #endif
11999 /* They complete the array creation expression, if no errors
12000 were found. */
12001 CAN_COMPLETE_NORMALLY (node) = 1;
12002 return (flag ? error_mark_node
12003 : force_evaluation_order (patch_newarray (node)));
12005 case NEW_ANONYMOUS_ARRAY_EXPR:
12006 /* Create the array type if necessary. */
12007 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
12009 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
12010 if (!(type = resolve_type_during_patch (type)))
12011 return error_mark_node;
12012 type = build_array_from_name (type, NULL_TREE,
12013 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
12014 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
12016 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
12017 ANONYMOUS_ARRAY_INITIALIZER (node));
12018 if (node == error_mark_node)
12019 return error_mark_node;
12020 CAN_COMPLETE_NORMALLY (node) = 1;
12021 return node;
12023 case NEW_CLASS_EXPR:
12024 case CALL_EXPR:
12025 /* Complete function's argument(s) first */
12026 if (complete_function_arguments (node))
12027 return error_mark_node;
12028 else
12030 tree decl, wfl = TREE_OPERAND (node, 0);
12031 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
12032 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
12033 super_identifier_node);
12034 tree arguments;
12035 #ifdef USE_MAPPED_LOCATION
12036 source_location location = EXPR_LOCATION (node);
12037 #else
12038 int location = EXPR_WFL_LINECOL (node);
12039 #endif
12041 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12042 from_super, 0, &decl);
12043 if (node == error_mark_node)
12044 return error_mark_node;
12046 if (TREE_CODE (node) == CALL_EXPR
12047 && TREE_OPERAND (node, 1) != NULL_TREE)
12048 arguments = TREE_VALUE (TREE_OPERAND (node, 1));
12049 else
12050 arguments = NULL_TREE;
12051 check_thrown_exceptions (location, decl, arguments);
12052 /* If we call this(...), register signature and positions */
12053 if (in_this)
12054 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12055 tree_cons (wfl, decl,
12056 DECL_CONSTRUCTOR_CALLS (current_function_decl));
12057 CAN_COMPLETE_NORMALLY (node) = 1;
12058 return force_evaluation_order (node);
12061 case MODIFY_EXPR:
12062 /* Save potential wfls */
12063 wfl_op1 = TREE_OPERAND (node, 0);
12064 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12066 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12067 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12068 && DECL_INITIAL (nn) != NULL_TREE)
12070 tree value;
12072 value = fold_constant_for_init (nn, nn);
12074 /* When we have a primitype type, or a string and we're not
12075 emitting a class file, we actually don't want to generate
12076 anything for the assignment. */
12077 if (value != NULL_TREE &&
12078 (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
12079 (TREE_TYPE (value) == string_ptr_type_node &&
12080 ! flag_emit_class_files)))
12082 /* Prepare node for patch_assignment */
12083 TREE_OPERAND (node, 1) = value;
12084 /* Call patch assignment to verify the assignment */
12085 if (patch_assignment (node, wfl_op1) == error_mark_node)
12086 return error_mark_node;
12087 /* Set DECL_INITIAL properly (a conversion might have
12088 been decided by patch_assignment) and return the
12089 empty statement. */
12090 else
12092 tree patched = patch_string (TREE_OPERAND (node, 1));
12093 if (patched)
12094 DECL_INITIAL (nn) = patched;
12095 else
12096 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12097 DECL_FIELD_FINAL_IUD (nn) = 1;
12098 return build_java_empty_stmt ();
12101 if (! flag_emit_class_files)
12102 DECL_INITIAL (nn) = NULL_TREE;
12104 wfl_op2 = TREE_OPERAND (node, 1);
12106 if (TREE_OPERAND (node, 0) == error_mark_node)
12107 return error_mark_node;
12109 flag = COMPOUND_ASSIGN_P (wfl_op2);
12110 if (flag)
12112 /* This might break when accessing outer field from inner
12113 class. TESTME, FIXME */
12114 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12116 /* Hand stabilize the lhs on both places */
12117 TREE_OPERAND (node, 0) = lvalue;
12118 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12119 (flag_emit_class_files ? lvalue : save_expr (lvalue));
12121 /* 15.25.2.a: Left hand is not an array access. FIXME */
12122 /* Now complete the RHS. We write it back later on. */
12123 nn = java_complete_tree (TREE_OPERAND (node, 1));
12125 if ((cn = patch_string (nn)))
12126 nn = cn;
12128 /* The last part of the rewrite for E1 op= E2 is to have
12129 E1 = (T)(E1 op E2), with T being the type of E1. */
12130 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12131 TREE_TYPE (lvalue), nn));
12133 /* If the assignment is compound and has reference type,
12134 then ensure the LHS has type String and nothing else. */
12135 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12136 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12137 parse_error_context (wfl_op2,
12138 "Incompatible type for %<+=%>. Can't convert %qs to %<java.lang.String%>",
12139 lang_printable_name (TREE_TYPE (lvalue), 0));
12141 /* 15.25.2.b: Left hand is an array access. FIXME */
12144 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12145 function to complete this RHS. Note that a NEW_ARRAY_INIT
12146 might have been already fully expanded if created as a result
12147 of processing an anonymous array initializer. We avoid doing
12148 the operation twice by testing whether the node already bears
12149 a type. */
12150 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12151 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12152 TREE_OPERAND (node, 1));
12153 /* Otherwise we simply complete the RHS */
12154 else
12155 nn = java_complete_tree (TREE_OPERAND (node, 1));
12157 if (nn == error_mark_node)
12158 return error_mark_node;
12160 /* Write back the RHS as we evaluated it. */
12161 TREE_OPERAND (node, 1) = nn;
12163 /* In case we're handling = with a String as a RHS, we need to
12164 produce a String out of the RHS (it might still be a
12165 STRING_CST or a StringBuffer at this stage */
12166 if ((nn = patch_string (TREE_OPERAND (node, 1))))
12167 TREE_OPERAND (node, 1) = nn;
12169 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12170 TREE_OPERAND (node, 1))))
12172 /* We return error_mark_node if outer_field_access_fix
12173 detects we write into a final. */
12174 if (nn == error_mark_node)
12175 return error_mark_node;
12176 node = nn;
12178 else
12180 node = patch_assignment (node, wfl_op1);
12181 if (node == error_mark_node)
12182 return error_mark_node;
12183 /* Reorganize the tree if necessary. */
12184 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12185 || JSTRING_P (TREE_TYPE (node))))
12186 node = java_refold (node);
12189 /* Seek to set DECL_INITIAL to a proper value, since it might have
12190 undergone a conversion in patch_assignment. We do that only when
12191 it's necessary to have DECL_INITIAL properly set. */
12192 nn = TREE_OPERAND (node, 0);
12193 if (TREE_CODE (nn) == VAR_DECL
12194 && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12195 && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12196 && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12197 || TREE_TYPE (nn) == string_ptr_type_node))
12198 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12200 CAN_COMPLETE_NORMALLY (node) = 1;
12201 return node;
12203 case MULT_EXPR:
12204 case PLUS_EXPR:
12205 case MINUS_EXPR:
12206 case LSHIFT_EXPR:
12207 case RSHIFT_EXPR:
12208 case URSHIFT_EXPR:
12209 case BIT_AND_EXPR:
12210 case BIT_XOR_EXPR:
12211 case BIT_IOR_EXPR:
12212 case TRUNC_MOD_EXPR:
12213 case TRUNC_DIV_EXPR:
12214 case RDIV_EXPR:
12215 case TRUTH_ANDIF_EXPR:
12216 case TRUTH_ORIF_EXPR:
12217 case EQ_EXPR:
12218 case NE_EXPR:
12219 case GT_EXPR:
12220 case GE_EXPR:
12221 case LT_EXPR:
12222 case LE_EXPR:
12223 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12224 knows how to handle those cases. */
12225 wfl_op1 = TREE_OPERAND (node, 0);
12226 wfl_op2 = TREE_OPERAND (node, 1);
12228 CAN_COMPLETE_NORMALLY (node) = 1;
12229 /* Don't complete string nodes if dealing with the PLUS operand. */
12230 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12232 nn = java_complete_tree (wfl_op1);
12233 if (nn == error_mark_node)
12234 return error_mark_node;
12236 TREE_OPERAND (node, 0) = nn;
12238 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12240 nn = java_complete_tree (wfl_op2);
12241 if (nn == error_mark_node)
12242 return error_mark_node;
12244 TREE_OPERAND (node, 1) = nn;
12246 return patch_binop (node, wfl_op1, wfl_op2);
12248 case INSTANCEOF_EXPR:
12249 wfl_op1 = TREE_OPERAND (node, 0);
12250 COMPLETE_CHECK_OP_0 (node);
12251 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
12253 case UNARY_PLUS_EXPR:
12254 case NEGATE_EXPR:
12255 case TRUTH_NOT_EXPR:
12256 case BIT_NOT_EXPR:
12257 case PREDECREMENT_EXPR:
12258 case PREINCREMENT_EXPR:
12259 case POSTDECREMENT_EXPR:
12260 case POSTINCREMENT_EXPR:
12261 case CONVERT_EXPR:
12262 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12263 how to handle those cases. */
12264 wfl_op1 = TREE_OPERAND (node, 0);
12265 CAN_COMPLETE_NORMALLY (node) = 1;
12266 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12267 if (TREE_OPERAND (node, 0) == error_mark_node)
12268 return error_mark_node;
12269 node = patch_unaryop (node, wfl_op1);
12270 CAN_COMPLETE_NORMALLY (node) = 1;
12271 break;
12273 case ARRAY_REF:
12274 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12275 how to handle those cases. */
12276 wfl_op1 = TREE_OPERAND (node, 0);
12277 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12278 if (TREE_OPERAND (node, 0) == error_mark_node)
12279 return error_mark_node;
12280 if (!flag_emit_class_files)
12281 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12282 /* The same applies to wfl_op2 */
12283 wfl_op2 = TREE_OPERAND (node, 1);
12284 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12285 if (TREE_OPERAND (node, 1) == error_mark_node)
12286 return error_mark_node;
12287 if (!flag_emit_class_files)
12288 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12289 return patch_array_ref (node);
12291 case RECORD_TYPE:
12292 return node;;
12294 case COMPONENT_REF:
12295 /* The first step in the re-write of qualified name handling. FIXME.
12296 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12297 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12298 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12300 tree name = TREE_OPERAND (node, 1);
12301 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12302 if (field == NULL_TREE)
12304 error ("missing static field %qs", IDENTIFIER_POINTER (name));
12305 return error_mark_node;
12307 if (! FIELD_STATIC (field))
12309 error ("not a static field %qs", IDENTIFIER_POINTER (name));
12310 return error_mark_node;
12312 return field;
12314 else
12315 abort ();
12316 break;
12318 case THIS_EXPR:
12319 /* Can't use THIS in a static environment */
12320 if (!current_this)
12322 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12323 parse_error_context (wfl_operator,
12324 "Keyword %<this%> used outside allowed context");
12325 TREE_TYPE (node) = error_mark_node;
12326 return error_mark_node;
12328 if (ctxp->explicit_constructor_p)
12330 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12331 parse_error_context
12332 (wfl_operator, "Can't reference %<this%> or %<super%> before the superclass constructor has been called");
12333 TREE_TYPE (node) = error_mark_node;
12334 return error_mark_node;
12336 return current_this;
12338 case CLASS_LITERAL:
12339 CAN_COMPLETE_NORMALLY (node) = 1;
12340 node = patch_incomplete_class_ref (node);
12341 if (node == error_mark_node)
12342 return error_mark_node;
12343 break;
12345 default:
12346 CAN_COMPLETE_NORMALLY (node) = 1;
12347 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12348 and it's time to turn it into the appropriate String object */
12349 if ((nn = patch_string (node)))
12350 node = nn;
12351 else
12352 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12354 return node;
12357 /* Complete function call's argument. Return a nonzero value is an
12358 error was found. */
12360 static int
12361 complete_function_arguments (tree node)
12363 int flag = 0;
12364 tree cn;
12366 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12367 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12369 tree wfl = TREE_VALUE (cn), parm, temp;
12370 parm = java_complete_tree (wfl);
12372 if (parm == error_mark_node)
12374 flag = 1;
12375 continue;
12377 /* If we have a string literal that we haven't transformed yet or a
12378 crafted string buffer, as a result of the use of the String
12379 `+' operator. Build `parm.toString()' and expand it. */
12380 if ((temp = patch_string (parm)))
12381 parm = temp;
12383 TREE_VALUE (cn) = parm;
12385 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12386 return flag;
12389 /* Sometimes (for loops and variable initialized during their
12390 declaration), we want to wrap a statement around a WFL and turn it
12391 debugable. */
12393 static tree
12394 build_debugable_stmt (int location, tree stmt)
12396 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12398 #ifdef USE_MAPPED_LOCATION
12399 stmt = expr_add_location (stmt, location, 1);
12400 #else
12401 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12402 EXPR_WFL_LINECOL (stmt) = location;
12403 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12404 #endif
12406 return stmt;
12409 static tree
12410 build_expr_block (tree body, tree decls)
12412 tree node = make_node (BLOCK);
12413 BLOCK_EXPR_DECLS (node) = decls;
12414 BLOCK_EXPR_BODY (node) = body;
12415 if (body)
12416 TREE_TYPE (node) = TREE_TYPE (body);
12417 TREE_SIDE_EFFECTS (node) = 1;
12418 return node;
12421 /* Create a new function block and link it appropriately to current
12422 function block chain */
12424 static tree
12425 enter_block (void)
12427 tree b = build_expr_block (NULL_TREE, NULL_TREE);
12429 /* Link block B supercontext to the previous block. The current
12430 function DECL is used as supercontext when enter_a_block is called
12431 for the first time for a given function. The current function body
12432 (DECL_FUNCTION_BODY) is set to be block B. */
12434 tree fndecl = current_function_decl;
12436 if (!fndecl) {
12437 BLOCK_SUPERCONTEXT (b) = current_static_block;
12438 current_static_block = b;
12441 else if (!DECL_FUNCTION_BODY (fndecl))
12443 BLOCK_SUPERCONTEXT (b) = fndecl;
12444 DECL_FUNCTION_BODY (fndecl) = b;
12446 else
12448 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12449 DECL_FUNCTION_BODY (fndecl) = b;
12451 return b;
12454 /* Exit a block by changing the current function body
12455 (DECL_FUNCTION_BODY) to the current block super context, only if
12456 the block being exited isn't the method's top level one. */
12458 static tree
12459 exit_block (void)
12461 tree b;
12462 if (current_function_decl)
12464 b = DECL_FUNCTION_BODY (current_function_decl);
12465 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12466 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12468 else
12470 b = current_static_block;
12472 if (BLOCK_SUPERCONTEXT (b))
12473 current_static_block = BLOCK_SUPERCONTEXT (b);
12475 return b;
12478 /* Lookup for NAME in the nested function's blocks, all the way up to
12479 the current toplevel one. It complies with Java's local variable
12480 scoping rules. */
12482 static tree
12483 lookup_name_in_blocks (tree name)
12485 tree b = GET_CURRENT_BLOCK (current_function_decl);
12487 while (b != current_function_decl)
12489 tree current;
12491 /* Paranoid sanity check. To be removed */
12492 if (TREE_CODE (b) != BLOCK)
12493 abort ();
12495 for (current = BLOCK_EXPR_DECLS (b); current;
12496 current = TREE_CHAIN (current))
12497 if (DECL_NAME (current) == name)
12498 return current;
12499 b = BLOCK_SUPERCONTEXT (b);
12501 return NULL_TREE;
12504 static void
12505 maybe_absorb_scoping_blocks (void)
12507 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12509 tree b = exit_block ();
12510 java_method_add_stmt (current_function_decl, b);
12511 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", input_line));
12516 /* This section of the source is reserved to build_* functions that
12517 are building incomplete tree nodes and the patch_* functions that
12518 are completing them. */
12520 /* Wrap a non WFL node around a WFL. */
12522 static tree
12523 build_wfl_wrap (tree node, int location)
12525 tree wfl, node_to_insert = node;
12527 /* We want to process THIS . xxx symbolically, to keep it consistent
12528 with the way we're processing SUPER. A THIS from a primary as a
12529 different form than a SUPER. Turn THIS into something symbolic */
12530 if (TREE_CODE (node) == THIS_EXPR)
12531 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12532 else
12533 #ifdef USE_MAPPED_LOCATION
12534 wfl = build_unknown_wfl (NULL_TREE);
12536 SET_EXPR_LOCATION (wfl, location);
12537 #else
12538 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12540 EXPR_WFL_LINECOL (wfl) = location;
12541 #endif
12542 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12543 return wfl;
12546 /* Build a super() constructor invocation. Returns an empty statement if
12547 we're currently dealing with the class java.lang.Object. */
12549 static tree
12550 build_super_invocation (tree mdecl)
12552 if (DECL_CONTEXT (mdecl) == object_type_node)
12553 return build_java_empty_stmt ();
12554 else
12556 tree super_wfl = build_wfl_node (super_identifier_node);
12557 tree a = NULL_TREE, t;
12559 /* This is called after parsing is done, so the parser context
12560 won't be accurate. Set location info from current_class decl. */
12561 tree class_wfl = lookup_cl (TYPE_NAME (current_class));
12562 EXPR_WFL_LINECOL (super_wfl) = EXPR_WFL_LINECOL (class_wfl);
12564 /* If we're dealing with an anonymous class, pass the arguments
12565 of the crafted constructor along. */
12566 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12568 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12569 for (; t != end_params_node; t = TREE_CHAIN (t))
12570 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12572 return build_method_invocation (super_wfl, a);
12576 /* Build a SUPER/THIS qualified method invocation. */
12578 static tree
12579 build_this_super_qualified_invocation (int use_this, tree name, tree args,
12580 int lloc, int rloc)
12582 tree invok;
12583 tree wfl =
12584 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12585 EXPR_WFL_LINECOL (wfl) = lloc;
12586 invok = build_method_invocation (name, args);
12587 return make_qualified_primary (wfl, invok, rloc);
12590 /* Build an incomplete CALL_EXPR node. */
12592 static tree
12593 build_method_invocation (tree name, tree args)
12595 tree call = build3 (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12596 TREE_SIDE_EFFECTS (call) = 1;
12597 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12598 return call;
12601 /* Build an incomplete new xxx(...) node. */
12603 static tree
12604 build_new_invocation (tree name, tree args)
12606 tree call = build3 (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12607 TREE_SIDE_EFFECTS (call) = 1;
12608 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12609 return call;
12612 /* Build an incomplete assignment expression. */
12614 static tree
12615 build_assignment (int op, int op_location, tree lhs, tree rhs)
12617 tree assignment;
12618 /* Build the corresponding binop if we deal with a Compound
12619 Assignment operator. Mark the binop sub-tree as part of a
12620 Compound Assignment expression */
12621 if (op != ASSIGN_TK)
12623 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12624 COMPOUND_ASSIGN_P (rhs) = 1;
12626 assignment = build2 (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12627 TREE_SIDE_EFFECTS (assignment) = 1;
12628 EXPR_WFL_LINECOL (assignment) = op_location;
12629 return assignment;
12632 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12633 the buffer. This is used only for string conversion. */
12634 static char *
12635 string_convert_int_cst (tree node)
12637 /* Long.MIN_VALUE is -9223372036854775808, 20 characters. */
12638 static char buffer[21];
12640 unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12641 unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12642 char *p = buffer + sizeof (buffer);
12643 int neg = 0;
12645 unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12646 << (HOST_BITS_PER_WIDE_INT - 1));
12648 *--p = '\0';
12650 /* If negative, note the fact and negate the value. */
12651 if ((hi & hibit))
12653 lo = ~lo;
12654 hi = ~hi;
12655 if (++lo == 0)
12656 ++hi;
12657 neg = 1;
12660 /* Divide by 10 until there are no bits left. */
12663 unsigned HOST_WIDE_INT acc = 0;
12664 unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12665 unsigned int i;
12667 /* Use long division to compute the result and the remainder. */
12668 for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12670 /* Shift a bit into accumulator. */
12671 acc <<= 1;
12672 if ((hi & hibit))
12673 acc |= 1;
12675 /* Shift the value. */
12676 hi <<= 1;
12677 if ((lo & hibit))
12678 hi |= 1;
12679 lo <<= 1;
12681 /* Shift the correct bit into the result. */
12682 outhi <<= 1;
12683 if ((outlo & hibit))
12684 outhi |= 1;
12685 outlo <<= 1;
12686 if (acc >= 10)
12688 acc -= 10;
12689 outlo |= 1;
12693 /* '0' == 060 in Java, but might not be here (think EBCDIC). */
12694 *--p = '\060' + acc;
12696 hi = outhi;
12697 lo = outlo;
12699 while (hi || lo);
12701 if (neg)
12702 *--p = '\055'; /* '-' == 055 in Java, but might not be here. */
12704 return p;
12707 /* Print an INTEGER_CST node in a static buffer, and return the
12708 buffer. This is used only for error handling. */
12709 char *
12710 print_int_node (tree node)
12712 static char buffer [80];
12713 if (TREE_CONSTANT_OVERFLOW (node))
12714 sprintf (buffer, "<overflow>");
12716 if (TREE_INT_CST_HIGH (node) == 0)
12717 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12718 TREE_INT_CST_LOW (node));
12719 else if (TREE_INT_CST_HIGH (node) == -1
12720 && TREE_INT_CST_LOW (node) != 0)
12721 sprintf (buffer, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
12722 -TREE_INT_CST_LOW (node));
12723 else
12724 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12725 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12727 return buffer;
12731 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12732 context. */
12734 /* 15.25 Assignment operators. */
12736 static tree
12737 patch_assignment (tree node, tree wfl_op1)
12739 tree rhs = TREE_OPERAND (node, 1);
12740 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12741 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12742 int error_found = 0;
12743 int lvalue_from_array = 0;
12744 int is_return = 0;
12746 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12748 /* Lhs can be a named variable */
12749 if (JDECL_P (lvalue))
12751 lhs_type = TREE_TYPE (lvalue);
12753 /* Or Lhs can be an array access. */
12754 else if (TREE_CODE (lvalue) == ARRAY_REF)
12756 lhs_type = TREE_TYPE (lvalue);
12757 lvalue_from_array = 1;
12759 /* Or a field access */
12760 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12761 lhs_type = TREE_TYPE (lvalue);
12762 /* Or a function return slot */
12763 else if (TREE_CODE (lvalue) == RESULT_DECL)
12765 /* If the return type is an integral type, then we create the
12766 RESULT_DECL with a promoted type, but we need to do these
12767 checks against the unpromoted type to ensure type safety. So
12768 here we look at the real type, not the type of the decl we
12769 are modifying. */
12770 lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12771 is_return = 1;
12773 /* Otherwise, we might want to try to write into an optimized static
12774 final, this is an of a different nature, reported further on. */
12775 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12776 && resolve_expression_name (wfl_op1, &llvalue))
12778 lhs_type = TREE_TYPE (lvalue);
12780 else
12782 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12783 error_found = 1;
12786 rhs_type = TREE_TYPE (rhs);
12788 /* 5.1 Try the assignment conversion for builtin type. */
12789 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12791 /* 5.2 If it failed, try a reference conversion */
12792 if (!new_rhs)
12793 new_rhs = try_reference_assignconv (lhs_type, rhs);
12795 /* 15.25.2 If we have a compound assignment, convert RHS into the
12796 type of the LHS */
12797 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12798 new_rhs = convert (lhs_type, rhs);
12800 /* Explicit cast required. This is an error */
12801 if (!new_rhs)
12803 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12804 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12805 tree wfl;
12806 char operation [32]; /* Max size known */
12808 /* If the assignment is part of a declaration, we use the WFL of
12809 the declared variable to point out the error and call it a
12810 declaration problem. If the assignment is a genuine =
12811 operator, we call is a operator `=' problem, otherwise we
12812 call it an assignment problem. In both of these last cases,
12813 we use the WFL of the operator to indicate the error. */
12815 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12817 wfl = wfl_op1;
12818 strcpy (operation, "declaration");
12820 else
12822 wfl = wfl_operator;
12823 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12824 strcpy (operation, "assignment");
12825 else if (is_return)
12826 strcpy (operation, "'return'");
12827 else
12828 strcpy (operation, "'='");
12831 if (!valid_cast_to_p (rhs_type, lhs_type))
12832 parse_error_context
12833 (wfl, "Incompatible type for %s. Can't convert %qs to %qs",
12834 operation, t1, t2);
12835 else
12836 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert %qs to %qs",
12837 operation, t1, t2);
12838 free (t1); free (t2);
12839 error_found = 1;
12842 if (error_found)
12843 return error_mark_node;
12845 /* If we're processing a `return' statement, promote the actual type
12846 to the promoted type. */
12847 if (is_return)
12848 new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12850 /* 10.10: Array Store Exception runtime check */
12851 if (!flag_emit_class_files
12852 && lvalue_from_array
12853 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12855 tree array, store_check, base, index_expr;
12857 /* Save RHS so that it doesn't get re-evaluated by the store check. */
12858 new_rhs = save_expr (new_rhs);
12860 /* Get the INDIRECT_REF. */
12861 array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12862 /* Get the array pointer expr. */
12863 array = TREE_OPERAND (array, 0);
12864 store_check = build_java_arraystore_check (array, new_rhs);
12866 index_expr = TREE_OPERAND (lvalue, 1);
12868 if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12870 /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12871 happen before the store check, so prepare to insert the store
12872 check within the second operand of the existing COMPOUND_EXPR. */
12873 base = index_expr;
12875 else
12876 base = lvalue;
12878 index_expr = TREE_OPERAND (base, 1);
12879 TREE_OPERAND (base, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (index_expr),
12880 store_check, index_expr);
12883 /* Final locals can be used as case values in switch
12884 statement. Prepare them for this eventuality. */
12885 if (TREE_CODE (lvalue) == VAR_DECL
12886 && DECL_FINAL (lvalue)
12887 && TREE_CONSTANT (new_rhs)
12888 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12889 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12892 TREE_CONSTANT (lvalue) = 1;
12893 TREE_INVARIANT (lvalue) = 1;
12894 DECL_INITIAL (lvalue) = new_rhs;
12897 /* Copy the rhs if it's a reference. */
12898 if (! flag_check_references && ! flag_emit_class_files && optimize > 0)
12900 switch (TREE_CODE (new_rhs))
12902 case ARRAY_REF:
12903 case INDIRECT_REF:
12904 case COMPONENT_REF:
12905 /* Transform a = foo.bar
12906 into a = ({int tmp; tmp = foo.bar;}).
12907 We need to ensure that if a read from memory fails
12908 because of a NullPointerException, a destination variable
12909 will remain unchanged. An explicit temporary does what
12910 we need.
12912 If flag_check_references is set, this is unnecessary
12913 because we'll check each reference before doing any
12914 reads. If optimize is not set the result will never be
12915 written to a stack slot that contains the LHS. */
12917 tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"),
12918 TREE_TYPE (new_rhs));
12919 tree block = make_node (BLOCK);
12920 tree assignment
12921 = build2 (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
12922 DECL_CONTEXT (tmp) = current_function_decl;
12923 TREE_TYPE (block) = TREE_TYPE (new_rhs);
12924 BLOCK_VARS (block) = tmp;
12925 BLOCK_EXPR_BODY (block) = assignment;
12926 TREE_SIDE_EFFECTS (block) = 1;
12927 new_rhs = block;
12929 break;
12930 default:
12931 break;
12935 TREE_OPERAND (node, 0) = lvalue;
12936 TREE_OPERAND (node, 1) = new_rhs;
12937 TREE_TYPE (node) = lhs_type;
12938 return node;
12941 /* Check that type SOURCE can be cast into type DEST. If the cast
12942 can't occur at all, return NULL; otherwise, return a possibly
12943 modified rhs. */
12945 static tree
12946 try_reference_assignconv (tree lhs_type, tree rhs)
12948 tree new_rhs = NULL_TREE;
12949 tree rhs_type = TREE_TYPE (rhs);
12951 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12953 /* `null' may be assigned to any reference type */
12954 if (rhs == null_pointer_node)
12955 new_rhs = null_pointer_node;
12956 /* Try the reference assignment conversion */
12957 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12958 new_rhs = rhs;
12959 /* This is a magic assignment that we process differently */
12960 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
12961 new_rhs = rhs;
12963 return new_rhs;
12966 /* Check that RHS can be converted into LHS_TYPE by the assignment
12967 conversion (5.2), for the cases of RHS being a builtin type. Return
12968 NULL_TREE if the conversion fails or if because RHS isn't of a
12969 builtin type. Return a converted RHS if the conversion is possible. */
12971 static tree
12972 try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
12974 tree new_rhs = NULL_TREE;
12975 tree rhs_type = TREE_TYPE (rhs);
12977 /* Handle boolean specially. */
12978 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12979 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12981 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12982 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12983 new_rhs = rhs;
12986 /* 5.1.1 Try Identity Conversion,
12987 5.1.2 Try Widening Primitive Conversion */
12988 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12989 new_rhs = convert (lhs_type, rhs);
12991 /* Try a narrowing primitive conversion (5.1.3):
12992 - expression is a constant expression of type byte, short, char,
12993 or int, AND
12994 - variable is byte, short or char AND
12995 - The value of the expression is representable in the type of the
12996 variable */
12997 else if ((rhs_type == byte_type_node || rhs_type == short_type_node
12998 || rhs_type == char_type_node || rhs_type == int_type_node)
12999 && TREE_CONSTANT (rhs)
13000 && (lhs_type == byte_type_node || lhs_type == char_type_node
13001 || lhs_type == short_type_node))
13003 if (int_fits_type_p (rhs, lhs_type))
13004 new_rhs = convert (lhs_type, rhs);
13005 else if (wfl_op1) /* Might be called with a NULL */
13006 parse_warning_context
13007 (wfl_op1,
13008 "Constant expression %qs too wide for narrowing primitive conversion to %qs",
13009 print_int_node (rhs), lang_printable_name (lhs_type, 0));
13010 /* Reported a warning that will turn into an error further
13011 down, so we don't return */
13014 return new_rhs;
13017 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
13018 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
13019 0 is the conversion test fails. This implements parts the method
13020 invocation conversion (5.3). */
13022 static int
13023 valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
13025 /* 5.1.1: This is the identity conversion part. */
13026 if (lhs_type == rhs_type)
13027 return 1;
13029 /* Reject non primitive types and boolean conversions. */
13030 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
13031 return 0;
13033 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
13034 than a char can't be converted into a char. Short can't too, but
13035 the < test below takes care of that */
13036 if (lhs_type == char_type_node && rhs_type == byte_type_node)
13037 return 0;
13039 /* Accept all promoted type here. Note, we can't use <= in the test
13040 below, because we still need to bounce out assignments of short
13041 to char and the likes */
13042 if (lhs_type == int_type_node
13043 && (rhs_type == promoted_byte_type_node
13044 || rhs_type == promoted_short_type_node
13045 || rhs_type == promoted_char_type_node
13046 || rhs_type == promoted_boolean_type_node))
13047 return 1;
13049 /* From here, an integral is widened if its precision is smaller
13050 than the precision of the LHS or if the LHS is a floating point
13051 type, or the RHS is a float and the RHS a double. */
13052 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13053 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13054 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13055 || (rhs_type == float_type_node && lhs_type == double_type_node))
13056 return 1;
13058 return 0;
13061 /* Check that something of SOURCE type can be assigned or cast to
13062 something of DEST type at runtime. Return 1 if the operation is
13063 valid, 0 otherwise. If CAST is set to 1, we're treating the case
13064 were SOURCE is cast into DEST, which borrows a lot of the
13065 assignment check. */
13067 static int
13068 valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
13070 /* SOURCE or DEST might be null if not from a declared entity. */
13071 if (!source || !dest)
13072 return 0;
13073 if (JNULLP_TYPE_P (source))
13074 return 1;
13075 if (TREE_CODE (source) == POINTER_TYPE)
13076 source = TREE_TYPE (source);
13077 if (TREE_CODE (dest) == POINTER_TYPE)
13078 dest = TREE_TYPE (dest);
13080 /* If source and dest are being compiled from bytecode, they may need to
13081 be loaded. */
13082 if (CLASS_P (source) && !CLASS_LOADED_P (source))
13084 load_class (source, 1);
13085 safe_layout_class (source);
13087 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13089 load_class (dest, 1);
13090 safe_layout_class (dest);
13093 /* Case where SOURCE is a class type */
13094 if (TYPE_CLASS_P (source))
13096 if (TYPE_CLASS_P (dest))
13097 return (source == dest
13098 || inherits_from_p (source, dest)
13099 || (cast && inherits_from_p (dest, source)));
13100 if (TYPE_INTERFACE_P (dest))
13102 /* If doing a cast and SOURCE is final, the operation is
13103 always correct a compile time (because even if SOURCE
13104 does not implement DEST, a subclass of SOURCE might). */
13105 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13106 return 1;
13107 /* Otherwise, SOURCE must implement DEST */
13108 return interface_of_p (dest, source);
13110 /* DEST is an array, cast permitted if SOURCE is of Object type */
13111 return (cast && source == object_type_node ? 1 : 0);
13113 if (TYPE_INTERFACE_P (source))
13115 if (TYPE_CLASS_P (dest))
13117 /* If not casting, DEST must be the Object type */
13118 if (!cast)
13119 return dest == object_type_node;
13120 /* We're doing a cast. The cast is always valid is class
13121 DEST is not final, otherwise, DEST must implement SOURCE */
13122 else if (!CLASS_FINAL (TYPE_NAME (dest)))
13123 return 1;
13124 else
13125 return interface_of_p (source, dest);
13127 if (TYPE_INTERFACE_P (dest))
13129 /* If doing a cast, then if SOURCE and DEST contain method
13130 with the same signature but different return type, then
13131 this is a (compile time) error */
13132 if (cast)
13134 tree method_source, method_dest;
13135 tree source_type;
13136 tree source_sig;
13137 tree source_name;
13138 for (method_source = TYPE_METHODS (source); method_source;
13139 method_source = TREE_CHAIN (method_source))
13141 source_sig =
13142 build_java_argument_signature (TREE_TYPE (method_source));
13143 source_type = TREE_TYPE (TREE_TYPE (method_source));
13144 source_name = DECL_NAME (method_source);
13145 for (method_dest = TYPE_METHODS (dest);
13146 method_dest; method_dest = TREE_CHAIN (method_dest))
13147 if (source_sig ==
13148 build_java_argument_signature (TREE_TYPE (method_dest))
13149 && source_name == DECL_NAME (method_dest)
13150 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13151 return 0;
13153 return 1;
13155 else
13156 return source == dest || interface_of_p (dest, source);
13158 else
13160 /* Array */
13161 return (cast
13162 && (DECL_NAME (TYPE_NAME (source))
13163 == java_lang_cloneable_identifier_node
13164 || (DECL_NAME (TYPE_NAME (source))
13165 == java_io_serializable_identifier_node)));
13168 if (TYPE_ARRAY_P (source))
13170 if (TYPE_CLASS_P (dest))
13171 return dest == object_type_node;
13172 /* Can't cast an array to an interface unless the interface is
13173 java.lang.Cloneable or java.io.Serializable. */
13174 if (TYPE_INTERFACE_P (dest))
13175 return (DECL_NAME (TYPE_NAME (dest))
13176 == java_lang_cloneable_identifier_node
13177 || (DECL_NAME (TYPE_NAME (dest))
13178 == java_io_serializable_identifier_node));
13179 else /* Arrays */
13181 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13182 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13184 /* In case of severe errors, they turn out null */
13185 if (!dest_element_type || !source_element_type)
13186 return 0;
13187 if (source_element_type == dest_element_type)
13188 return 1;
13189 return valid_ref_assignconv_cast_p (source_element_type,
13190 dest_element_type, cast);
13192 return 0;
13194 return 0;
13197 static int
13198 valid_cast_to_p (tree source, tree dest)
13200 if (TREE_CODE (source) == POINTER_TYPE)
13201 source = TREE_TYPE (source);
13202 if (TREE_CODE (dest) == POINTER_TYPE)
13203 dest = TREE_TYPE (dest);
13205 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13206 return valid_ref_assignconv_cast_p (source, dest, 1);
13208 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13209 return 1;
13211 else if (TREE_CODE (source) == BOOLEAN_TYPE
13212 && TREE_CODE (dest) == BOOLEAN_TYPE)
13213 return 1;
13215 return 0;
13218 static tree
13219 do_unary_numeric_promotion (tree arg)
13221 tree type = TREE_TYPE (arg);
13222 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13223 || TREE_CODE (type) == CHAR_TYPE)
13224 arg = convert (int_type_node, arg);
13225 return arg;
13228 /* Return a nonzero value if SOURCE can be converted into DEST using
13229 the method invocation conversion rule (5.3). */
13230 static int
13231 valid_method_invocation_conversion_p (tree dest, tree source)
13233 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13234 && valid_builtin_assignconv_identity_widening_p (dest, source))
13235 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13236 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13237 && valid_ref_assignconv_cast_p (source, dest, 0)));
13240 /* Build an incomplete binop expression. */
13242 static tree
13243 build_binop (enum tree_code op, int op_location, tree op1, tree op2)
13245 tree binop = build2 (op, NULL_TREE, op1, op2);
13246 TREE_SIDE_EFFECTS (binop) = 1;
13247 /* Store the location of the operator, for better error report. The
13248 string of the operator will be rebuild based on the OP value. */
13249 EXPR_WFL_LINECOL (binop) = op_location;
13250 return binop;
13253 /* Build the string of the operator retained by NODE. If NODE is part
13254 of a compound expression, add an '=' at the end of the string. This
13255 function is called when an error needs to be reported on an
13256 operator. The string is returned as a pointer to a static character
13257 buffer. */
13259 static char *
13260 operator_string (tree node)
13262 #define BUILD_OPERATOR_STRING(S) \
13264 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13265 return buffer; \
13268 static char buffer [10];
13269 switch (TREE_CODE (node))
13271 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13272 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13273 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13274 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13275 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13276 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13277 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13278 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13279 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13280 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13281 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13282 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13283 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13284 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13285 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13286 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13287 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13288 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13289 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13290 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13291 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13292 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13293 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13294 case PREINCREMENT_EXPR: /* Fall through */
13295 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13296 case PREDECREMENT_EXPR: /* Fall through */
13297 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13298 default:
13299 internal_error ("unregistered operator %s",
13300 tree_code_name [TREE_CODE (node)]);
13302 return NULL;
13303 #undef BUILD_OPERATOR_STRING
13306 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13308 static int
13309 java_decl_equiv (tree var_acc1, tree var_acc2)
13311 if (JDECL_P (var_acc1))
13312 return (var_acc1 == var_acc2);
13314 return (TREE_CODE (var_acc1) == COMPONENT_REF
13315 && TREE_CODE (var_acc2) == COMPONENT_REF
13316 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13317 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13318 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13321 /* Return a nonzero value if CODE is one of the operators that can be
13322 used in conjunction with the `=' operator in a compound assignment. */
13324 static int
13325 binop_compound_p (enum tree_code code)
13327 int i;
13328 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13329 if (binop_lookup [i] == code)
13330 break;
13332 return i < BINOP_COMPOUND_CANDIDATES;
13335 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13337 static tree
13338 java_refold (tree t)
13340 tree c, b, ns, decl;
13342 if (TREE_CODE (t) != MODIFY_EXPR)
13343 return t;
13345 c = TREE_OPERAND (t, 1);
13346 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13347 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13348 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13349 return t;
13351 /* Now the left branch of the binary operator. */
13352 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13353 if (! (b && TREE_CODE (b) == NOP_EXPR
13354 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13355 return t;
13357 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13358 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13359 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13360 return t;
13362 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13363 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13364 /* It's got to be the an equivalent decl */
13365 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13367 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13368 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13369 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13370 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13371 /* Change the right part of the BINOP_EXPR */
13372 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13375 return t;
13378 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13379 errors but we modify NODE so that it contains the type computed
13380 according to the expression, when it's fixed. Otherwise, we write
13381 error_mark_node as the type. It allows us to further the analysis
13382 of remaining nodes and detects more errors in certain cases. */
13384 static tree
13385 patch_binop (tree node, tree wfl_op1, tree wfl_op2)
13387 tree op1 = TREE_OPERAND (node, 0);
13388 tree op2 = TREE_OPERAND (node, 1);
13389 tree op1_type = TREE_TYPE (op1);
13390 tree op2_type = TREE_TYPE (op2);
13391 tree prom_type = NULL_TREE, cn;
13392 enum tree_code code = TREE_CODE (node);
13394 /* If 1, tell the routine that we have to return error_mark_node
13395 after checking for the initialization of the RHS */
13396 int error_found = 0;
13398 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13400 /* If either op<n>_type are NULL, this might be early signs of an
13401 error situation, unless it's too early to tell (in case we're
13402 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13403 correctly so the error can be later on reported accurately. */
13404 if (! (code == PLUS_EXPR || code == NE_EXPR
13405 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13407 tree n;
13408 if (! op1_type)
13410 n = java_complete_tree (op1);
13411 op1_type = TREE_TYPE (n);
13413 if (! op2_type)
13415 n = java_complete_tree (op2);
13416 op2_type = TREE_TYPE (n);
13420 switch (code)
13422 /* 15.16 Multiplicative operators */
13423 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13424 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13425 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13426 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13427 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13429 if (!JNUMERIC_TYPE_P (op1_type))
13430 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13431 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13432 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13433 TREE_TYPE (node) = error_mark_node;
13434 error_found = 1;
13435 break;
13437 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13439 /* Detect integral division by zero */
13440 if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13441 && TREE_CODE (prom_type) == INTEGER_TYPE
13442 && (op2 == integer_zero_node || op2 == long_zero_node ||
13443 (TREE_CODE (op2) == INTEGER_CST &&
13444 ! TREE_INT_CST_LOW (op2) && ! TREE_INT_CST_HIGH (op2))))
13446 parse_warning_context
13447 (wfl_operator,
13448 "Evaluating this expression will result in an arithmetic exception being thrown");
13449 TREE_CONSTANT (node) = 0;
13450 TREE_INVARIANT (node) = 0;
13453 /* Change the division operator if necessary */
13454 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13455 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13457 /* Before divisions as is disappear, try to simplify and bail if
13458 applicable, otherwise we won't perform even simple
13459 simplifications like (1-1)/3. We can't do that with floating
13460 point number, folds can't handle them at this stage. */
13461 if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13462 && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13464 TREE_TYPE (node) = prom_type;
13465 node = fold (node);
13466 if (TREE_CODE (node) != code)
13467 return node;
13470 if (TREE_CODE (prom_type) == INTEGER_TYPE
13471 && flag_use_divide_subroutine
13472 && ! flag_emit_class_files
13473 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13474 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13476 /* This one is more complicated. FLOATs are processed by a
13477 function call to soft_fmod. Duplicate the value of the
13478 COMPOUND_ASSIGN_P flag. */
13479 if (code == TRUNC_MOD_EXPR)
13481 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13482 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13483 return mod;
13485 break;
13487 /* 15.17 Additive Operators */
13488 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13490 /* Operation is valid if either one argument is a string
13491 constant, a String object or a StringBuffer crafted for the
13492 purpose of the a previous usage of the String concatenation
13493 operator */
13495 if (TREE_CODE (op1) == STRING_CST
13496 || TREE_CODE (op2) == STRING_CST
13497 || JSTRING_TYPE_P (op1_type)
13498 || JSTRING_TYPE_P (op2_type)
13499 || IS_CRAFTED_STRING_BUFFER_P (op1)
13500 || IS_CRAFTED_STRING_BUFFER_P (op2))
13501 return build_string_concatenation (op1, op2);
13503 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13504 Numeric Types */
13505 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13507 if (!JNUMERIC_TYPE_P (op1_type))
13508 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13509 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13510 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13511 TREE_TYPE (node) = error_mark_node;
13512 error_found = 1;
13513 break;
13515 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13516 break;
13518 /* 15.18 Shift Operators */
13519 case LSHIFT_EXPR:
13520 case RSHIFT_EXPR:
13521 case URSHIFT_EXPR:
13522 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13524 if (!JINTEGRAL_TYPE_P (op1_type))
13525 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13526 else
13528 if (JNUMERIC_TYPE_P (op2_type))
13529 parse_error_context (wfl_operator,
13530 "Incompatible type for %qs. Explicit cast needed to convert shift distance from %qs to integral",
13531 operator_string (node),
13532 lang_printable_name (op2_type, 0));
13533 else
13534 parse_error_context (wfl_operator,
13535 "Incompatible type for %qs. Can't convert shift distance from %qs to integral",
13536 operator_string (node),
13537 lang_printable_name (op2_type, 0));
13539 TREE_TYPE (node) = error_mark_node;
13540 error_found = 1;
13541 break;
13544 /* Unary numeric promotion (5.6.1) is performed on each operand
13545 separately */
13546 op1 = do_unary_numeric_promotion (op1);
13547 op2 = do_unary_numeric_promotion (op2);
13549 /* If the right hand side is of type `long', first cast it to
13550 `int'. */
13551 if (TREE_TYPE (op2) == long_type_node)
13552 op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13554 /* The type of the shift expression is the type of the promoted
13555 type of the left-hand operand */
13556 prom_type = TREE_TYPE (op1);
13558 /* Shift int only up to 0x1f and long up to 0x3f */
13559 if (prom_type == int_type_node)
13560 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13561 build_int_cst (NULL_TREE, 0x1f)));
13562 else
13563 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13564 build_int_cst (NULL_TREE, 0x3f)));
13566 /* The >>> operator is a >> operating on unsigned quantities */
13567 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13569 tree to_return;
13570 tree utype = java_unsigned_type (prom_type);
13571 op1 = convert (utype, op1);
13572 TREE_SET_CODE (node, RSHIFT_EXPR);
13573 TREE_OPERAND (node, 0) = op1;
13574 TREE_OPERAND (node, 1) = op2;
13575 TREE_TYPE (node) = utype;
13576 to_return = convert (prom_type, node);
13577 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13578 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13579 TREE_SIDE_EFFECTS (to_return)
13580 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13581 return to_return;
13583 break;
13585 /* 15.19.1 Type Comparison Operator instanceof */
13586 case INSTANCEOF_EXPR:
13588 TREE_TYPE (node) = boolean_type_node;
13590 /* OP1_TYPE might be NULL when OP1 is a string constant. */
13591 if ((cn = patch_string (op1)))
13593 op1 = cn;
13594 op1_type = TREE_TYPE (op1);
13596 if (op1_type == NULL_TREE)
13597 abort ();
13599 if (!(op2_type = resolve_type_during_patch (op2)))
13600 return error_mark_node;
13602 /* The first operand must be a reference type or the null type */
13603 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13604 error_found = 1; /* Error reported further below */
13606 /* The second operand must be a reference type */
13607 if (!JREFERENCE_TYPE_P (op2_type))
13609 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13610 parse_error_context
13611 (wfl_operator, "Invalid argument %qs for %<instanceof%>",
13612 lang_printable_name (op2_type, 0));
13613 error_found = 1;
13616 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13618 /* If the first operand is null, the result is always false */
13619 if (op1 == null_pointer_node)
13620 return boolean_false_node;
13621 else if (flag_emit_class_files)
13623 TREE_OPERAND (node, 1) = op2_type;
13624 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13625 return node;
13627 /* Otherwise we have to invoke instance of to figure it out */
13628 else
13629 return build_instanceof (op1, op2_type);
13631 /* There is no way the expression operand can be an instance of
13632 the type operand. This is a compile time error. */
13633 else
13635 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13636 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13637 parse_error_context
13638 (wfl_operator, "Impossible for %qs to be instance of %qs",
13639 t1, lang_printable_name (op2_type, 0));
13640 free (t1);
13641 error_found = 1;
13644 break;
13646 /* 15.21 Bitwise and Logical Operators */
13647 case BIT_AND_EXPR:
13648 case BIT_XOR_EXPR:
13649 case BIT_IOR_EXPR:
13650 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13651 /* Binary numeric promotion is performed on both operand and the
13652 expression retain that type */
13653 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13655 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13656 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13657 /* The type of the bitwise operator expression is BOOLEAN */
13658 prom_type = boolean_type_node;
13659 else
13661 if (!JINTEGRAL_TYPE_P (op1_type))
13662 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13663 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13664 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13665 TREE_TYPE (node) = error_mark_node;
13666 error_found = 1;
13667 /* Insert a break here if adding thing before the switch's
13668 break for this case */
13670 break;
13672 /* 15.22 Conditional-And Operator */
13673 case TRUTH_ANDIF_EXPR:
13674 /* 15.23 Conditional-Or Operator */
13675 case TRUTH_ORIF_EXPR:
13676 /* Operands must be of BOOLEAN type */
13677 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13678 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13680 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13681 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13682 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13683 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13684 TREE_TYPE (node) = boolean_type_node;
13685 error_found = 1;
13686 break;
13688 else if (integer_zerop (op1))
13690 return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13692 else if (integer_onep (op1))
13694 return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13696 /* The type of the conditional operators is BOOLEAN */
13697 prom_type = boolean_type_node;
13698 break;
13700 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13701 case LT_EXPR:
13702 case GT_EXPR:
13703 case LE_EXPR:
13704 case GE_EXPR:
13705 /* The type of each of the operands must be a primitive numeric
13706 type */
13707 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13709 if (!JNUMERIC_TYPE_P (op1_type))
13710 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13711 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13712 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13713 TREE_TYPE (node) = boolean_type_node;
13714 error_found = 1;
13715 break;
13717 /* Binary numeric promotion is performed on the operands */
13718 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13719 /* The type of the relation expression is always BOOLEAN */
13720 prom_type = boolean_type_node;
13721 break;
13723 /* 15.20 Equality Operator */
13724 case EQ_EXPR:
13725 case NE_EXPR:
13726 /* It's time for us to patch the strings. */
13727 if ((cn = patch_string (op1)))
13729 op1 = cn;
13730 op1_type = TREE_TYPE (op1);
13732 if ((cn = patch_string (op2)))
13734 op2 = cn;
13735 op2_type = TREE_TYPE (op2);
13738 /* 15.20.1 Numerical Equality Operators == and != */
13739 /* Binary numeric promotion is performed on the operands */
13740 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13741 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13743 /* 15.20.2 Boolean Equality Operators == and != */
13744 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13745 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13746 ; /* Nothing to do here */
13748 /* 15.20.3 Reference Equality Operators == and != */
13749 /* Types have to be either references or the null type. If
13750 they're references, it must be possible to convert either
13751 type to the other by casting conversion. */
13752 else if ((op1 == null_pointer_node && op2 == null_pointer_node)
13753 || (op1 == null_pointer_node && JREFERENCE_TYPE_P (op2_type))
13754 || (JREFERENCE_TYPE_P (op1_type) && op2 == null_pointer_node)
13755 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13756 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13757 || valid_ref_assignconv_cast_p (op2_type,
13758 op1_type, 1))))
13759 ; /* Nothing to do here */
13761 /* Else we have an error figure what can't be converted into
13762 what and report the error */
13763 else
13765 char *t1;
13766 t1 = xstrdup (lang_printable_name (op1_type, 0));
13767 parse_error_context
13768 (wfl_operator,
13769 "Incompatible type for %qs. Can't convert %qs to %qs",
13770 operator_string (node), t1,
13771 lang_printable_name (op2_type, 0));
13772 free (t1);
13773 TREE_TYPE (node) = boolean_type_node;
13774 error_found = 1;
13775 break;
13777 prom_type = boolean_type_node;
13778 break;
13779 default:
13780 abort ();
13783 if (error_found)
13784 return error_mark_node;
13786 TREE_OPERAND (node, 0) = op1;
13787 TREE_OPERAND (node, 1) = op2;
13788 TREE_TYPE (node) = prom_type;
13789 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13791 /* fold does not respect side-effect order as required for Java but not C.
13792 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13793 * bytecode.
13795 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13796 : ! TREE_SIDE_EFFECTS (node))
13797 node = fold (node);
13798 return node;
13801 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13802 zero value, the value of CSTE comes after the valude of STRING */
13804 static tree
13805 do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13807 const char *old = TREE_STRING_POINTER (cste);
13808 int old_len = TREE_STRING_LENGTH (cste);
13809 int len = old_len + string_len;
13810 char *new = alloca (len+1);
13812 if (after)
13814 memcpy (new, string, string_len);
13815 memcpy (&new [string_len], old, old_len);
13817 else
13819 memcpy (new, old, old_len);
13820 memcpy (&new [old_len], string, string_len);
13822 new [len] = '\0';
13823 return build_string (len, new);
13826 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13827 new STRING_CST on success, NULL_TREE on failure. */
13829 static tree
13830 merge_string_cste (tree op1, tree op2, int after)
13832 /* Handle two string constants right away. */
13833 if (TREE_CODE (op2) == STRING_CST)
13834 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13835 TREE_STRING_LENGTH (op2), after);
13837 /* Reasonable integer constant can be treated right away. */
13838 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13840 static const char *const boolean_true = "true";
13841 static const char *const boolean_false = "false";
13842 static const char *const null_pointer = "null";
13843 char ch[4];
13844 const char *string;
13846 if (op2 == boolean_true_node)
13847 string = boolean_true;
13848 else if (op2 == boolean_false_node)
13849 string = boolean_false;
13850 else if (op2 == null_pointer_node
13851 || (integer_zerop (op2)
13852 && TREE_CODE (TREE_TYPE (op2)) == POINTER_TYPE))
13853 /* FIXME: null is not a compile-time constant, so it is only safe to
13854 merge if the overall expression is non-constant. However, this
13855 code always merges without checking the overall expression. */
13856 string = null_pointer;
13857 else if (TREE_TYPE (op2) == char_type_node)
13859 /* Convert the character into UTF-8. */
13860 unsigned int c = (unsigned int) TREE_INT_CST_LOW (op2);
13861 unsigned char *p = (unsigned char *) ch;
13862 if (0x01 <= c && c <= 0x7f)
13863 *p++ = (unsigned char) c;
13864 else if (c < 0x7ff)
13866 *p++ = (unsigned char) (c >> 6 | 0xc0);
13867 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13869 else
13871 *p++ = (unsigned char) (c >> 12 | 0xe0);
13872 *p++ = (unsigned char) (((c >> 6) & 0x3f) | 0x80);
13873 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13875 *p = '\0';
13877 string = ch;
13879 else
13880 string = string_convert_int_cst (op2);
13882 return do_merge_string_cste (op1, string, strlen (string), after);
13884 return NULL_TREE;
13887 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13888 has to be a STRING_CST and the other part must be a STRING_CST or a
13889 INTEGRAL constant. Return a new STRING_CST if the operation
13890 succeed, NULL_TREE otherwise.
13892 If the case we want to optimize for space, we might want to return
13893 NULL_TREE for each invocation of this routine. FIXME */
13895 static tree
13896 string_constant_concatenation (tree op1, tree op2)
13898 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13900 tree string, rest;
13901 int invert;
13903 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13904 rest = (string == op1 ? op2 : op1);
13905 invert = (string == op1 ? 0 : 1 );
13907 /* Walk REST, only if it looks reasonable */
13908 if (TREE_CODE (rest) != STRING_CST
13909 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13910 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13911 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13913 rest = java_complete_tree (rest);
13914 if (rest == error_mark_node)
13915 return error_mark_node;
13916 rest = fold (rest);
13918 return merge_string_cste (string, rest, invert);
13920 return NULL_TREE;
13923 /* Implement the `+' operator. Does static optimization if possible,
13924 otherwise create (if necessary) and append elements to a
13925 StringBuffer. The StringBuffer will be carried around until it is
13926 used for a function call or an assignment. Then toString() will be
13927 called on it to turn it into a String object. */
13929 static tree
13930 build_string_concatenation (tree op1, tree op2)
13932 tree result;
13933 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13935 /* Try to do some static optimization */
13936 if ((result = string_constant_concatenation (op1, op2)))
13937 return result;
13939 /* Discard empty strings on either side of the expression */
13940 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13942 op1 = op2;
13943 op2 = NULL_TREE;
13945 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13946 op2 = NULL_TREE;
13948 /* If operands are string constant, turn then into object references */
13949 if (TREE_CODE (op1) == STRING_CST)
13950 op1 = patch_string_cst (op1);
13951 if (op2 && TREE_CODE (op2) == STRING_CST)
13952 op2 = patch_string_cst (op2);
13954 /* If either one of the constant is null and the other non null
13955 operand is a String constant, return it. */
13956 if ((TREE_CODE (op1) == STRING_CST) && !op2)
13957 return op1;
13959 /* If OP1 isn't already a StringBuffer, create and
13960 initialize a new one */
13961 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13963 /* Two solutions here:
13964 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13965 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13966 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13967 op1 = BUILD_STRING_BUFFER (op1);
13968 else
13970 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13971 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13975 if (op2)
13977 /* OP1 is no longer the last node holding a crafted StringBuffer */
13978 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13979 /* Create a node for `{new...,xxx}.append (op2)' */
13980 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13983 /* Mark the last node holding a crafted StringBuffer */
13984 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13986 TREE_SIDE_EFFECTS (op1) = side_effects;
13987 return op1;
13990 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13991 StringBuffer. If no string were found to be patched, return
13992 NULL. */
13994 static tree
13995 patch_string (tree node)
13997 if (node == error_mark_node)
13998 return error_mark_node;
13999 if (TREE_CODE (node) == STRING_CST)
14000 return patch_string_cst (node);
14001 else if (IS_CRAFTED_STRING_BUFFER_P (node))
14003 int saved = ctxp->explicit_constructor_p;
14004 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
14005 tree ret;
14006 /* Temporary disable forbid the use of `this'. */
14007 ctxp->explicit_constructor_p = 0;
14008 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
14009 /* String concatenation arguments must be evaluated in order too. */
14010 ret = force_evaluation_order (ret);
14011 /* Restore it at its previous value */
14012 ctxp->explicit_constructor_p = saved;
14013 return ret;
14015 return NULL_TREE;
14018 /* Build the internal representation of a string constant. */
14020 static tree
14021 patch_string_cst (tree node)
14023 int location;
14024 if (! flag_emit_class_files)
14026 node = get_identifier (TREE_STRING_POINTER (node));
14027 location = alloc_name_constant (CONSTANT_String, node);
14028 node = build_ref_from_constant_pool (location);
14030 TREE_CONSTANT (node) = 1;
14031 TREE_INVARIANT (node) = 1;
14033 /* ??? Guessing that the class file code can't handle casts. */
14034 if (! flag_emit_class_files)
14035 node = convert (string_ptr_type_node, node);
14036 else
14037 TREE_TYPE (node) = string_ptr_type_node;
14039 return node;
14042 /* Build an incomplete unary operator expression. */
14044 static tree
14045 build_unaryop (int op_token, int op_location, tree op1)
14047 enum tree_code op;
14048 tree unaryop;
14049 switch (op_token)
14051 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14052 case MINUS_TK: op = NEGATE_EXPR; break;
14053 case NEG_TK: op = TRUTH_NOT_EXPR; break;
14054 case NOT_TK: op = BIT_NOT_EXPR; break;
14055 default: abort ();
14058 unaryop = build1 (op, NULL_TREE, op1);
14059 TREE_SIDE_EFFECTS (unaryop) = 1;
14060 /* Store the location of the operator, for better error report. The
14061 string of the operator will be rebuild based on the OP value. */
14062 EXPR_WFL_LINECOL (unaryop) = op_location;
14063 return unaryop;
14066 /* Special case for the ++/-- operators, since they require an extra
14067 argument to build, which is set to NULL and patched
14068 later. IS_POST_P is 1 if the operator, 0 otherwise. */
14070 static tree
14071 build_incdec (int op_token, int op_location, tree op1, int is_post_p)
14073 static const enum tree_code lookup [2][2] =
14075 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14076 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14078 tree node = build2 (lookup [is_post_p][(op_token - DECR_TK)],
14079 NULL_TREE, op1, NULL_TREE);
14080 TREE_SIDE_EFFECTS (node) = 1;
14081 /* Store the location of the operator, for better error report. The
14082 string of the operator will be rebuild based on the OP value. */
14083 EXPR_WFL_LINECOL (node) = op_location;
14084 return node;
14087 /* Build an incomplete cast operator, based on the use of the
14088 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14089 set. java_complete_tree is trained to walk a CONVERT_EXPR even
14090 though its type is already set. */
14092 static tree
14093 build_cast (int location, tree type, tree exp)
14095 tree node = build1 (CONVERT_EXPR, type, exp);
14096 EXPR_WFL_LINECOL (node) = location;
14097 return node;
14100 /* Build an incomplete class reference operator. */
14101 static tree
14102 build_incomplete_class_ref (int location, tree class_name)
14104 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14105 tree class_decl = GET_CPC ();
14106 tree this_class = TREE_TYPE (class_decl);
14108 /* Generate the synthetic static method `class$'. (Previously we
14109 deferred this, causing different method tables to be emitted
14110 for native code and bytecode.) */
14111 if (!TYPE_DOT_CLASS (this_class)
14112 && !JPRIMITIVE_TYPE_P (class_name)
14113 && !(TREE_CODE (class_name) == VOID_TYPE))
14115 tree cpc_list = GET_CPC_LIST();
14116 tree cpc = cpc_list;
14117 tree target_class;
14119 /* For inner classes, add a 'class$' method to their outermost
14120 context, creating it if necessary. */
14122 while (GET_NEXT_ENCLOSING_CPC(cpc))
14123 cpc = GET_NEXT_ENCLOSING_CPC(cpc);
14124 class_decl = TREE_VALUE (cpc);
14126 target_class = TREE_TYPE (class_decl);
14128 if (CLASS_INTERFACE (TYPE_NAME (target_class)))
14130 /* For interfaces, adding a static 'class$' method directly
14131 is illegal. So create an inner class to contain the new
14132 method. Empirically this matches the behavior of javac. */
14133 tree t, inner;
14134 /* We want the generated inner class inside the outermost class. */
14135 GET_CPC_LIST() = cpc;
14136 t = build_wfl_node (DECL_NAME (TYPE_NAME (object_type_node)));
14137 inner = create_anonymous_class (t);
14138 target_class = TREE_TYPE (inner);
14139 end_class_declaration (1);
14140 GET_CPC_LIST() = cpc_list;
14143 if (TYPE_DOT_CLASS (target_class) == NULL_TREE)
14144 build_dot_class_method (target_class);
14146 if (this_class != target_class)
14147 TYPE_DOT_CLASS (this_class) = TYPE_DOT_CLASS (target_class);
14150 EXPR_WFL_LINECOL (node) = location;
14151 return node;
14154 /* Complete an incomplete class reference operator. */
14155 static tree
14156 patch_incomplete_class_ref (tree node)
14158 tree type = TREE_OPERAND (node, 0);
14159 tree ref_type;
14161 if (!(ref_type = resolve_type_during_patch (type)))
14162 return error_mark_node;
14164 /* If we're not emitting class files and we know ref_type is a
14165 compiled class, build a direct reference. */
14166 if ((! flag_emit_class_files && is_compiled_class (ref_type))
14167 || JPRIMITIVE_TYPE_P (ref_type)
14168 || TREE_CODE (ref_type) == VOID_TYPE)
14170 tree dot = build_class_ref (ref_type);
14171 /* A class referenced by `foo.class' is initialized. */
14172 if (!flag_emit_class_files)
14173 dot = build_class_init (ref_type, dot);
14174 return java_complete_tree (dot);
14177 /* If we're emitting class files and we have to deal with non
14178 primitive types, we invoke the synthetic static method `class$'. */
14179 ref_type = build_dot_class_method_invocation (current_class, ref_type);
14180 return java_complete_tree (ref_type);
14183 /* 15.14 Unary operators. We return error_mark_node in case of error,
14184 but preserve the type of NODE if the type is fixed. */
14186 static tree
14187 patch_unaryop (tree node, tree wfl_op)
14189 tree op = TREE_OPERAND (node, 0);
14190 tree op_type = TREE_TYPE (op);
14191 tree prom_type = NULL_TREE, value, decl;
14192 int outer_field_flag = 0;
14193 int code = TREE_CODE (node);
14194 int error_found = 0;
14196 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14198 switch (code)
14200 /* 15.13.2 Postfix Increment Operator ++ */
14201 case POSTINCREMENT_EXPR:
14202 /* 15.13.3 Postfix Increment Operator -- */
14203 case POSTDECREMENT_EXPR:
14204 /* 15.14.1 Prefix Increment Operator ++ */
14205 case PREINCREMENT_EXPR:
14206 /* 15.14.2 Prefix Decrement Operator -- */
14207 case PREDECREMENT_EXPR:
14208 op = decl = extract_field_decl (op);
14209 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
14210 /* We might be trying to change an outer field accessed using
14211 access method. */
14212 if (outer_field_flag)
14214 /* Retrieve the decl of the field we're trying to access. We
14215 do that by first retrieving the function we would call to
14216 access the field. It has been already verified that this
14217 field isn't final */
14218 if (flag_emit_class_files)
14219 decl = TREE_OPERAND (op, 0);
14220 else
14221 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14222 decl = DECL_FUNCTION_ACCESS_DECL (decl);
14224 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14225 else if (!JDECL_P (decl)
14226 && TREE_CODE (decl) != COMPONENT_REF
14227 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14228 && TREE_CODE (decl) != INDIRECT_REF
14229 && !(TREE_CODE (decl) == COMPOUND_EXPR
14230 && TREE_OPERAND (decl, 1)
14231 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14233 TREE_TYPE (node) = error_mark_node;
14234 error_found = 1;
14237 /* From now on, we know that op if a variable and that it has a
14238 valid wfl. We use wfl_op to locate errors related to the
14239 ++/-- operand. */
14240 if (!JNUMERIC_TYPE_P (op_type))
14242 parse_error_context
14243 (wfl_op, "Invalid argument type %qs to %qs",
14244 lang_printable_name (op_type, 0), operator_string (node));
14245 TREE_TYPE (node) = error_mark_node;
14246 error_found = 1;
14248 else
14250 /* Before the addition, binary numeric promotion is performed on
14251 both operands, if really necessary */
14252 if (JINTEGRAL_TYPE_P (op_type))
14254 value = build_int_cst (op_type, 1);
14255 TREE_TYPE (node) = op_type;
14257 else
14259 value = build_int_cst (NULL_TREE, 1);
14260 TREE_TYPE (node) =
14261 binary_numeric_promotion (op_type,
14262 TREE_TYPE (value), &op, &value);
14265 /* We remember we might be accessing an outer field */
14266 if (outer_field_flag)
14268 /* We re-generate an access to the field */
14269 value = build2 (PLUS_EXPR, TREE_TYPE (op),
14270 build_outer_field_access (wfl_op, decl), value);
14272 /* And we patch the original access$() into a write
14273 with plus_op as a rhs */
14274 return outer_field_access_fix (node, op, value);
14277 /* And write back into the node. */
14278 TREE_OPERAND (node, 0) = op;
14279 TREE_OPERAND (node, 1) = value;
14280 /* Convert the overall back into its original type, if
14281 necessary, and return */
14282 if (JINTEGRAL_TYPE_P (op_type))
14283 return fold (node);
14284 else
14285 return fold (convert (op_type, node));
14287 break;
14289 /* 15.14.3 Unary Plus Operator + */
14290 case UNARY_PLUS_EXPR:
14291 /* 15.14.4 Unary Minus Operator - */
14292 case NEGATE_EXPR:
14293 if (!JNUMERIC_TYPE_P (op_type))
14295 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14296 TREE_TYPE (node) = error_mark_node;
14297 error_found = 1;
14299 /* Unary numeric promotion is performed on operand */
14300 else
14302 op = do_unary_numeric_promotion (op);
14303 prom_type = TREE_TYPE (op);
14304 if (code == UNARY_PLUS_EXPR)
14305 return fold (op);
14307 break;
14309 /* 15.14.5 Bitwise Complement Operator ~ */
14310 case BIT_NOT_EXPR:
14311 if (!JINTEGRAL_TYPE_P (op_type))
14313 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14314 TREE_TYPE (node) = error_mark_node;
14315 error_found = 1;
14317 else
14319 op = do_unary_numeric_promotion (op);
14320 prom_type = TREE_TYPE (op);
14322 break;
14324 /* 15.14.6 Logical Complement Operator ! */
14325 case TRUTH_NOT_EXPR:
14326 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14328 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14329 /* But the type is known. We will report an error if further
14330 attempt of a assignment is made with this rhs */
14331 TREE_TYPE (node) = boolean_type_node;
14332 error_found = 1;
14334 else
14335 prom_type = boolean_type_node;
14336 break;
14338 /* 15.15 Cast Expression */
14339 case CONVERT_EXPR:
14340 value = patch_cast (node, wfl_operator);
14341 if (value == error_mark_node)
14343 /* If this cast is part of an assignment, we tell the code
14344 that deals with it not to complain about a mismatch,
14345 because things have been cast, anyways */
14346 TREE_TYPE (node) = error_mark_node;
14347 error_found = 1;
14349 else
14351 value = fold (value);
14352 return value;
14354 break;
14357 if (error_found)
14358 return error_mark_node;
14360 /* There are cases where node has been replaced by something else
14361 and we don't end up returning here: UNARY_PLUS_EXPR,
14362 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14363 TREE_OPERAND (node, 0) = fold (op);
14364 TREE_TYPE (node) = prom_type;
14365 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14366 return fold (node);
14369 /* Generic type resolution that sometimes takes place during node
14370 patching. Returned the resolved type or generate an error
14371 message. Return the resolved type or NULL_TREE. */
14373 static tree
14374 resolve_type_during_patch (tree type)
14376 if (unresolved_type_p (type, NULL))
14378 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14379 if (!type_decl)
14381 parse_error_context (type,
14382 "Class %qs not found in type declaration",
14383 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14384 return NULL_TREE;
14387 check_deprecation (type, type_decl);
14389 return TREE_TYPE (type_decl);
14391 return type;
14394 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14395 found. Otherwise NODE or something meant to replace it is returned. */
14397 static tree
14398 patch_cast (tree node, tree wfl_op)
14400 tree op = TREE_OPERAND (node, 0);
14401 tree cast_type = TREE_TYPE (node);
14402 tree patched, op_type;
14403 char *t1;
14405 /* Some string patching might be necessary at this stage */
14406 if ((patched = patch_string (op)))
14407 TREE_OPERAND (node, 0) = op = patched;
14408 op_type = TREE_TYPE (op);
14410 /* First resolve OP_TYPE if unresolved */
14411 if (!(cast_type = resolve_type_during_patch (cast_type)))
14412 return error_mark_node;
14414 /* Check on cast that are proven correct at compile time */
14415 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14417 /* Same type */
14418 if (cast_type == op_type)
14419 return node;
14421 /* A narrowing conversion from a floating-point number to an
14422 integral type requires special handling (5.1.3). */
14423 if (JFLOAT_TYPE_P (op_type) && JINTEGRAL_TYPE_P (cast_type))
14424 if (cast_type != long_type_node)
14425 op = convert (integer_type_node, op);
14427 /* Try widening/narrowing conversion. Potentially, things need
14428 to be worked out in gcc so we implement the extreme cases
14429 correctly. fold_convert() needs to be fixed. */
14430 return convert (cast_type, op);
14433 /* It's also valid to cast a boolean into a boolean */
14434 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14435 return node;
14437 /* null can be casted to references */
14438 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14439 return build_null_of_type (cast_type);
14441 /* The remaining legal casts involve conversion between reference
14442 types. Check for their compile time correctness. */
14443 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14444 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14446 TREE_TYPE (node) = promote_type (cast_type);
14447 /* Now, the case can be determined correct at compile time if
14448 OP_TYPE can be converted into CAST_TYPE by assignment
14449 conversion (5.2) */
14451 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14453 TREE_SET_CODE (node, NOP_EXPR);
14454 return node;
14457 if (flag_emit_class_files)
14459 TREE_SET_CODE (node, CONVERT_EXPR);
14460 return node;
14463 /* The cast requires a run-time check */
14464 return build3 (CALL_EXPR, promote_type (cast_type),
14465 build_address_of (soft_checkcast_node),
14466 tree_cons (NULL_TREE, build_class_ref (cast_type),
14467 build_tree_list (NULL_TREE, op)),
14468 NULL_TREE);
14471 /* Any other casts are proven incorrect at compile time */
14472 t1 = xstrdup (lang_printable_name (op_type, 0));
14473 parse_error_context (wfl_op, "Invalid cast from %qs to %qs",
14474 t1, lang_printable_name (cast_type, 0));
14475 free (t1);
14476 return error_mark_node;
14479 /* Build a null constant and give it the type TYPE. */
14481 static tree
14482 build_null_of_type (tree type)
14484 tree node = build_int_cst (promote_type (type), 0);
14485 return node;
14488 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14489 a list of indices. */
14490 static tree
14491 build_array_ref (int location, tree array, tree index)
14493 tree node = build4 (ARRAY_REF, NULL_TREE, array, index,
14494 NULL_TREE, NULL_TREE);
14495 EXPR_WFL_LINECOL (node) = location;
14496 return node;
14499 /* 15.12 Array Access Expression */
14501 static tree
14502 patch_array_ref (tree node)
14504 tree array = TREE_OPERAND (node, 0);
14505 tree array_type = TREE_TYPE (array);
14506 tree index = TREE_OPERAND (node, 1);
14507 tree index_type = TREE_TYPE (index);
14508 int error_found = 0;
14510 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14512 if (TREE_CODE (array_type) == POINTER_TYPE)
14513 array_type = TREE_TYPE (array_type);
14515 /* The array reference must be an array */
14516 if (!TYPE_ARRAY_P (array_type))
14518 parse_error_context
14519 (wfl_operator,
14520 "%<[]%> can only be applied to arrays. It can't be applied to %qs",
14521 lang_printable_name (array_type, 0));
14522 TREE_TYPE (node) = error_mark_node;
14523 error_found = 1;
14526 /* The array index undergoes unary numeric promotion. The promoted
14527 type must be int */
14528 index = do_unary_numeric_promotion (index);
14529 if (TREE_TYPE (index) != int_type_node)
14531 if (valid_cast_to_p (index_type, int_type_node))
14532 parse_error_context (wfl_operator,
14533 "Incompatible type for %<[]%>. Explicit cast needed to convert %qs to %<int%>",
14534 lang_printable_name (index_type, 0));
14535 else
14536 parse_error_context (wfl_operator,
14537 "Incompatible type for %<[]%>. Can't convert %qs to %<int%>",
14538 lang_printable_name (index_type, 0));
14539 TREE_TYPE (node) = error_mark_node;
14540 error_found = 1;
14543 if (error_found)
14544 return error_mark_node;
14546 array_type = TYPE_ARRAY_ELEMENT (array_type);
14548 if (flag_emit_class_files)
14550 TREE_OPERAND (node, 0) = array;
14551 TREE_OPERAND (node, 1) = index;
14553 else
14554 node = build_java_arrayaccess (array, array_type, index);
14555 TREE_TYPE (node) = array_type;
14556 return node;
14559 /* 15.9 Array Creation Expressions */
14561 static tree
14562 build_newarray_node (tree type, tree dims, int extra_dims)
14564 tree node = build3 (NEW_ARRAY_EXPR, NULL_TREE, type,
14565 nreverse (dims),
14566 build_int_cst (NULL_TREE, extra_dims));
14567 return node;
14570 static tree
14571 patch_newarray (tree node)
14573 tree type = TREE_OPERAND (node, 0);
14574 tree dims = TREE_OPERAND (node, 1);
14575 tree cdim, array_type;
14576 int error_found = 0;
14577 int ndims = 0;
14578 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14580 /* Dimension types are verified. It's better for the types to be
14581 verified in order. */
14582 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14584 int dim_error = 0;
14585 tree dim = TREE_VALUE (cdim);
14587 /* Dim might have been saved during its evaluation */
14588 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14590 /* The type of each specified dimension must be an integral type. */
14591 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14592 dim_error = 1;
14594 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14595 promoted type must be int. */
14596 else
14598 dim = do_unary_numeric_promotion (dim);
14599 if (TREE_TYPE (dim) != int_type_node)
14600 dim_error = 1;
14603 /* Report errors on types here */
14604 if (dim_error)
14606 parse_error_context
14607 (TREE_PURPOSE (cdim),
14608 "Incompatible type for dimension in array creation expression. %s convert %qs to %<int%>",
14609 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14610 "Explicit cast needed to" : "Can't"),
14611 lang_printable_name (TREE_TYPE (dim), 0));
14612 error_found = 1;
14615 TREE_PURPOSE (cdim) = NULL_TREE;
14618 /* Resolve array base type if unresolved */
14619 if (!(type = resolve_type_during_patch (type)))
14620 error_found = 1;
14622 if (error_found)
14624 /* We don't want further evaluation of this bogus array creation
14625 operation */
14626 TREE_TYPE (node) = error_mark_node;
14627 return error_mark_node;
14630 /* Set array_type to the actual (promoted) array type of the result. */
14631 if (TREE_CODE (type) == RECORD_TYPE)
14632 type = build_pointer_type (type);
14633 while (--xdims >= 0)
14635 type = promote_type (build_java_array_type (type, -1));
14637 dims = nreverse (dims);
14638 array_type = type;
14639 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14641 type = array_type;
14642 array_type
14643 = build_java_array_type (type,
14644 TREE_CODE (cdim) == INTEGER_CST
14645 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14646 : -1);
14647 array_type = promote_type (array_type);
14649 dims = nreverse (dims);
14651 /* The node is transformed into a function call. Things are done
14652 differently according to the number of dimensions. If the number
14653 of dimension is equal to 1, then the nature of the base type
14654 (primitive or not) matters. */
14655 if (ndims == 1)
14656 return build_new_array (type, TREE_VALUE (dims));
14658 /* Can't reuse what's already written in expr.c because it uses the
14659 JVM stack representation. Provide a build_multianewarray. FIXME */
14660 return build3 (CALL_EXPR, array_type,
14661 build_address_of (soft_multianewarray_node),
14662 tree_cons (NULL_TREE,
14663 build_class_ref (TREE_TYPE (array_type)),
14664 tree_cons (NULL_TREE,
14665 build_int_cst (NULL_TREE, ndims),
14666 dims)),
14667 NULL_TREE);
14670 /* 10.6 Array initializer. */
14672 /* Build a wfl for array element that don't have one, so we can
14673 pin-point errors. */
14675 static tree
14676 maybe_build_array_element_wfl (tree node)
14678 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14680 /* FIXME - old code used "prev_lc.line" and "elc.prev_col */
14681 return build_expr_wfl (NULL_TREE,
14682 #ifdef USE_MAPPED_LOCATION
14683 input_location
14684 #else
14685 ctxp->filename,
14686 ctxp->lexer->token_start.line,
14687 ctxp->lexer->token_start.col
14688 #endif
14691 else
14692 return NULL_TREE;
14695 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14696 identification of initialized arrays easier to detect during walk
14697 and expansion. */
14699 static tree
14700 build_new_array_init (int location, tree values)
14702 tree constructor = build_constructor (NULL_TREE, values);
14703 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14704 EXPR_WFL_LINECOL (to_return) = location;
14705 return to_return;
14708 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14709 occurred. Otherwise return NODE after having set its type
14710 appropriately. */
14712 static tree
14713 patch_new_array_init (tree type, tree node)
14715 int error_seen = 0;
14716 tree current, element_type;
14717 HOST_WIDE_INT length;
14718 int all_constant = 1;
14719 tree init = TREE_OPERAND (node, 0);
14721 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14723 parse_error_context (node,
14724 "Invalid array initializer for non-array type %qs",
14725 lang_printable_name (type, 1));
14726 return error_mark_node;
14728 type = TREE_TYPE (type);
14729 element_type = TYPE_ARRAY_ELEMENT (type);
14731 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14733 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14734 current; length++, current = TREE_CHAIN (current))
14736 tree elt = TREE_VALUE (current);
14737 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14739 error_seen |= array_constructor_check_entry (element_type, current);
14740 elt = TREE_VALUE (current);
14741 /* When compiling to native code, STRING_CST is converted to
14742 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14743 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14744 all_constant = 0;
14746 else
14748 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14749 TREE_PURPOSE (current) = NULL_TREE;
14750 all_constant = 0;
14752 if (elt && TREE_CODE (elt) == TREE_LIST
14753 && TREE_VALUE (elt) == error_mark_node)
14754 error_seen = 1;
14757 if (error_seen)
14758 return error_mark_node;
14760 /* Create a new type. We can't reuse the one we have here by
14761 patching its dimension because it originally is of dimension -1
14762 hence reused by gcc. This would prevent triangular arrays. */
14763 type = build_java_array_type (element_type, length);
14764 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14765 TREE_TYPE (node) = promote_type (type);
14766 TREE_CONSTANT (init) = all_constant;
14767 TREE_INVARIANT (init) = all_constant;
14768 TREE_CONSTANT (node) = all_constant;
14769 TREE_INVARIANT (node) = all_constant;
14770 return node;
14773 /* Verify that one entry of the initializer element list can be
14774 assigned to the array base type. Report 1 if an error occurred, 0
14775 otherwise. */
14777 static int
14778 array_constructor_check_entry (tree type, tree entry)
14780 char *array_type_string = NULL; /* For error reports */
14781 tree value, type_value, new_value, wfl_value, patched;
14782 int error_seen = 0;
14784 new_value = NULL_TREE;
14785 wfl_value = TREE_VALUE (entry);
14787 value = java_complete_tree (TREE_VALUE (entry));
14788 /* patch_string return error_mark_node if arg is error_mark_node */
14789 if ((patched = patch_string (value)))
14790 value = patched;
14791 if (value == error_mark_node)
14792 return 1;
14794 type_value = TREE_TYPE (value);
14796 /* At anytime, try_builtin_assignconv can report a warning on
14797 constant overflow during narrowing. */
14798 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14799 new_value = try_builtin_assignconv (wfl_operator, type, value);
14800 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14801 type_value = promote_type (type);
14803 /* Check and report errors */
14804 if (!new_value)
14806 const char *const msg = (!valid_cast_to_p (type_value, type) ?
14807 "Can't" : "Explicit cast needed to");
14808 if (!array_type_string)
14809 array_type_string = xstrdup (lang_printable_name (type, 1));
14810 parse_error_context
14811 (wfl_operator, "Incompatible type for array. %s convert %qs to %qs",
14812 msg, lang_printable_name (type_value, 1), array_type_string);
14813 error_seen = 1;
14816 if (new_value)
14817 TREE_VALUE (entry) = new_value;
14819 if (array_type_string)
14820 free (array_type_string);
14822 TREE_PURPOSE (entry) = NULL_TREE;
14823 return error_seen;
14826 static tree
14827 build_this (int location)
14829 tree node = build_wfl_node (this_identifier_node);
14830 TREE_SET_CODE (node, THIS_EXPR);
14831 EXPR_WFL_LINECOL (node) = location;
14832 return node;
14835 /* 14.15 The return statement. It builds a modify expression that
14836 assigns the returned value to the RESULT_DECL that hold the value
14837 to be returned. */
14839 static tree
14840 build_return (int location, tree op)
14842 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14843 EXPR_WFL_LINECOL (node) = location;
14844 node = build_debugable_stmt (location, node);
14845 return node;
14848 static tree
14849 patch_return (tree node)
14851 tree return_exp = TREE_OPERAND (node, 0);
14852 tree meth = current_function_decl;
14853 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14854 int error_found = 0;
14856 TREE_TYPE (node) = error_mark_node;
14857 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14859 /* It's invalid to have a return value within a function that is
14860 declared with the keyword void or that is a constructor */
14861 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14862 error_found = 1;
14864 /* It's invalid to use a return statement in a static block */
14865 if (DECL_CLINIT_P (current_function_decl))
14866 error_found = 1;
14868 /* It's invalid to have a no return value within a function that
14869 isn't declared with the keyword `void' */
14870 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14871 error_found = 2;
14873 if (DECL_INSTINIT_P (current_function_decl))
14874 error_found = 1;
14876 if (error_found)
14878 if (DECL_INSTINIT_P (current_function_decl))
14879 parse_error_context (wfl_operator,
14880 "%<return%> inside instance initializer");
14882 else if (DECL_CLINIT_P (current_function_decl))
14883 parse_error_context (wfl_operator,
14884 "%<return%> inside static initializer");
14886 else if (!DECL_CONSTRUCTOR_P (meth))
14888 char *t = xstrdup (lang_printable_name (mtype, 0));
14889 parse_error_context (wfl_operator,
14890 "%<return%> with%s value from %<%s %s%>",
14891 (error_found == 1 ? "" : "out"),
14892 t, lang_printable_name (meth, 2));
14893 free (t);
14895 else
14896 parse_error_context (wfl_operator,
14897 "%<return%> with value from constructor %qs",
14898 lang_printable_name (meth, 2));
14899 return error_mark_node;
14902 /* If we have a return_exp, build a modify expression and expand
14903 it. Note: at that point, the assignment is declared valid, but we
14904 may want to carry some more hacks */
14905 if (return_exp)
14907 tree exp = java_complete_tree (return_exp);
14908 tree modify, patched;
14910 if ((patched = patch_string (exp)))
14911 exp = patched;
14913 modify = build2 (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14914 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14915 modify = java_complete_tree (modify);
14917 if (modify != error_mark_node)
14919 TREE_SIDE_EFFECTS (modify) = 1;
14920 TREE_OPERAND (node, 0) = modify;
14922 else
14923 return error_mark_node;
14925 TREE_TYPE (node) = void_type_node;
14926 TREE_SIDE_EFFECTS (node) = 1;
14927 return node;
14930 /* 14.8 The if Statement */
14932 static tree
14933 build_if_else_statement (int location, tree expression, tree if_body,
14934 tree else_body)
14936 tree node;
14937 if (!else_body)
14938 else_body = build_java_empty_stmt ();
14939 node = build3 (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14940 EXPR_WFL_LINECOL (node) = location;
14941 node = build_debugable_stmt (location, node);
14942 return node;
14945 static tree
14946 patch_if_else_statement (tree node)
14948 tree expression = TREE_OPERAND (node, 0);
14949 int can_complete_normally
14950 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14951 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
14953 TREE_TYPE (node) = error_mark_node;
14954 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14956 /* The type of expression must be boolean */
14957 if (TREE_TYPE (expression) != boolean_type_node
14958 && TREE_TYPE (expression) != promoted_boolean_type_node)
14960 parse_error_context
14961 (wfl_operator,
14962 "Incompatible type for %<if%>. Can't convert %qs to %<boolean%>",
14963 lang_printable_name (TREE_TYPE (expression), 0));
14964 return error_mark_node;
14967 TREE_TYPE (node) = void_type_node;
14968 TREE_SIDE_EFFECTS (node) = 1;
14969 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14970 return node;
14973 /* 14.6 Labeled Statements */
14975 /* Action taken when a labeled statement is parsed. a new
14976 LABELED_BLOCK_EXPR is created. No statement is attached to the
14977 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14979 static tree
14980 build_labeled_block (int location, tree label)
14982 tree label_name ;
14983 tree label_decl, node;
14984 if (label == NULL_TREE || label == continue_identifier_node)
14985 label_name = label;
14986 else
14988 label_name = merge_qualified_name (label_id, label);
14989 /* Issue an error if we try to reuse a label that was previously
14990 declared */
14991 if (IDENTIFIER_LOCAL_VALUE (label_name))
14993 EXPR_WFL_LINECOL (wfl_operator) = location;
14994 parse_error_context (wfl_operator,
14995 "Declaration of %qs shadows a previous label declaration",
14996 IDENTIFIER_POINTER (label));
14997 EXPR_WFL_LINECOL (wfl_operator) =
14998 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14999 parse_error_context (wfl_operator,
15000 "This is the location of the previous declaration of label %qs",
15001 IDENTIFIER_POINTER (label));
15002 java_error_count--;
15006 label_decl = create_label_decl (label_name);
15007 node = build2 (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
15008 EXPR_WFL_LINECOL (node) = location;
15009 TREE_SIDE_EFFECTS (node) = 1;
15010 return node;
15013 /* A labeled statement LBE is attached a statement. */
15015 static tree
15016 finish_labeled_statement (tree lbe, /* Labeled block expr */
15017 tree statement)
15019 /* In anyways, tie the loop to its statement */
15020 LABELED_BLOCK_BODY (lbe) = statement;
15021 pop_labeled_block ();
15022 POP_LABELED_BLOCK ();
15023 return lbe;
15026 /* 14.10, 14.11, 14.12 Loop Statements */
15028 /* Create an empty LOOP_EXPR and make it the last in the nested loop
15029 list. */
15031 static tree
15032 build_new_loop (tree loop_body)
15034 tree loop = build1 (LOOP_EXPR, NULL_TREE, loop_body);
15035 TREE_SIDE_EFFECTS (loop) = 1;
15036 PUSH_LOOP (loop);
15037 return loop;
15040 /* Create a loop body according to the following structure:
15041 COMPOUND_EXPR
15042 COMPOUND_EXPR (loop main body)
15043 EXIT_EXPR (this order is for while/for loops.
15044 LABELED_BLOCK_EXPR the order is reversed for do loops)
15045 LABEL_DECL (a continue occurring here branches at the
15046 BODY end of this labeled block)
15047 INCREMENT (if any)
15049 REVERSED, if nonzero, tells that the loop condition expr comes
15050 after the body, like in the do-while loop.
15052 To obtain a loop, the loop body structure described above is
15053 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15055 LABELED_BLOCK_EXPR
15056 LABEL_DECL (use this label to exit the loop)
15057 LOOP_EXPR
15058 <structure described above> */
15060 static tree
15061 build_loop_body (int location, tree condition, int reversed)
15063 tree first, second, body;
15065 condition = build1 (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15066 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15067 condition = build_debugable_stmt (location, condition);
15068 TREE_SIDE_EFFECTS (condition) = 1;
15070 body = build_labeled_block (0, continue_identifier_node);
15071 first = (reversed ? body : condition);
15072 second = (reversed ? condition : body);
15073 return build2 (COMPOUND_EXPR, NULL_TREE,
15074 build2 (COMPOUND_EXPR, NULL_TREE, first, second),
15075 build_java_empty_stmt ());
15078 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15079 their order) on the current loop. Unlink the current loop from the
15080 loop list. */
15082 static tree
15083 finish_loop_body (int location, tree condition, tree body, int reversed)
15085 tree to_return = ctxp->current_loop;
15086 tree loop_body = LOOP_EXPR_BODY (to_return);
15087 if (condition)
15089 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15090 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15091 The real EXIT_EXPR is one operand further. */
15092 EXPR_WFL_LINECOL (cnode) = location;
15093 if (TREE_CODE (cnode) == EXPR_WITH_FILE_LOCATION)
15095 cnode = EXPR_WFL_NODE (cnode);
15096 /* This one is for accurate error reports */
15097 EXPR_WFL_LINECOL (cnode) = location;
15099 TREE_OPERAND (cnode, 0) = condition;
15101 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15102 POP_LOOP ();
15103 return to_return;
15106 /* Tailored version of finish_loop_body for FOR loops, when FOR
15107 loops feature the condition part */
15109 static tree
15110 finish_for_loop (int location, tree condition, tree update, tree body)
15112 /* Put the condition and the loop body in place */
15113 tree loop = finish_loop_body (location, condition, body, 0);
15114 /* LOOP is the current loop which has been now popped of the loop
15115 stack. Mark the update block as reachable and install it. We do
15116 this because the (current interpretation of the) JLS requires
15117 that the update expression be considered reachable even if the
15118 for loop's body doesn't complete normally. */
15119 if (update != NULL_TREE && !IS_EMPTY_STMT (update))
15121 tree up2 = update;
15122 if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15123 up2 = EXPR_WFL_NODE (up2);
15124 /* It is possible for the update expression to be an
15125 EXPR_WFL_NODE wrapping nothing. */
15126 if (up2 != NULL_TREE && !IS_EMPTY_STMT (up2))
15128 /* Try to detect constraint violations. These would be
15129 programming errors somewhere. */
15130 if (! EXPR_P (up2) || TREE_CODE (up2) == LOOP_EXPR)
15131 abort ();
15132 SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15135 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15136 return loop;
15139 /* Try to find the loop a block might be related to. This comprises
15140 the case where the LOOP_EXPR is found as the second operand of a
15141 COMPOUND_EXPR, because the loop happens to have an initialization
15142 part, then expressed as the first operand of the COMPOUND_EXPR. If
15143 the search finds something, 1 is returned. Otherwise, 0 is
15144 returned. The search is assumed to start from a
15145 LABELED_BLOCK_EXPR's block. */
15147 static tree
15148 search_loop (tree statement)
15150 if (TREE_CODE (statement) == LOOP_EXPR)
15151 return statement;
15153 if (TREE_CODE (statement) == BLOCK)
15154 statement = BLOCK_SUBBLOCKS (statement);
15155 else
15156 return NULL_TREE;
15158 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15159 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15160 statement = TREE_OPERAND (statement, 1);
15162 return (TREE_CODE (statement) == LOOP_EXPR
15163 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15166 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15167 returned otherwise. */
15169 static int
15170 labeled_block_contains_loop_p (tree block, tree loop)
15172 if (!block)
15173 return 0;
15175 if (LABELED_BLOCK_BODY (block) == loop)
15176 return 1;
15178 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15179 return 1;
15181 return 0;
15184 /* If the loop isn't surrounded by a labeled statement, create one and
15185 insert LOOP as its body. */
15187 static tree
15188 patch_loop_statement (tree loop)
15190 tree loop_label;
15192 TREE_TYPE (loop) = void_type_node;
15193 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15194 return loop;
15196 loop_label = build_labeled_block (0, NULL_TREE);
15197 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15198 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15199 LABELED_BLOCK_BODY (loop_label) = loop;
15200 PUSH_LABELED_BLOCK (loop_label);
15201 return loop_label;
15204 /* 14.13, 14.14: break and continue Statements */
15206 /* Build a break or a continue statement. a null NAME indicates an
15207 unlabeled break/continue statement. */
15209 static tree
15210 build_bc_statement (int location, int is_break, tree name)
15212 tree break_continue, label_block_expr = NULL_TREE;
15214 if (name)
15216 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15217 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15218 /* Null means that we don't have a target for this named
15219 break/continue. In this case, we make the target to be the
15220 label name, so that the error can be reported accurately in
15221 patch_bc_statement. */
15222 label_block_expr = EXPR_WFL_NODE (name);
15224 /* Unlabeled break/continue will be handled during the
15225 break/continue patch operation */
15226 break_continue = build1 (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr);
15228 IS_BREAK_STMT_P (break_continue) = is_break;
15229 TREE_SIDE_EFFECTS (break_continue) = 1;
15230 EXPR_WFL_LINECOL (break_continue) = location;
15231 break_continue = build_debugable_stmt (location, break_continue);
15232 return break_continue;
15235 /* Verification of a break/continue statement. */
15237 static tree
15238 patch_bc_statement (tree node)
15240 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15241 tree labeled_block = ctxp->current_labeled_block;
15242 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15244 /* Having an identifier here means that the target is unknown. */
15245 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15247 parse_error_context (wfl_operator, "No label definition found for %qs",
15248 IDENTIFIER_POINTER (bc_label));
15249 return error_mark_node;
15251 if (! IS_BREAK_STMT_P (node))
15253 /* It's a continue statement. */
15254 for (;; labeled_block = TREE_CHAIN (labeled_block))
15256 if (labeled_block == NULL_TREE)
15258 if (bc_label == NULL_TREE)
15259 parse_error_context (wfl_operator,
15260 "%<continue%> must be in loop");
15261 else
15262 parse_error_context
15263 (wfl_operator, "continue label %qs does not name a loop",
15264 IDENTIFIER_POINTER (bc_label));
15265 return error_mark_node;
15267 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15268 == continue_identifier_node)
15269 && (bc_label == NULL_TREE
15270 || TREE_CHAIN (labeled_block) == bc_label))
15272 bc_label = labeled_block;
15273 break;
15277 else if (!bc_label)
15279 for (;; labeled_block = TREE_CHAIN (labeled_block))
15281 if (labeled_block == NULL_TREE)
15283 parse_error_context (wfl_operator,
15284 "%<break%> must be in loop or switch");
15285 return error_mark_node;
15287 target_stmt = LABELED_BLOCK_BODY (labeled_block);
15288 if (TREE_CODE (target_stmt) == SWITCH_EXPR
15289 || search_loop (target_stmt))
15291 bc_label = labeled_block;
15292 break;
15297 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15298 CAN_COMPLETE_NORMALLY (bc_label) = 1;
15300 /* Our break/continue don't return values. */
15301 TREE_TYPE (node) = void_type_node;
15302 /* Encapsulate the break within a compound statement so that it's
15303 expanded all the times by expand_expr (and not clobbered
15304 sometimes, like after a if statement) */
15305 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15306 TREE_SIDE_EFFECTS (node) = 1;
15307 return node;
15310 /* Process the exit expression belonging to a loop. Its type must be
15311 boolean. */
15313 static tree
15314 patch_exit_expr (tree node)
15316 tree expression = TREE_OPERAND (node, 0);
15317 TREE_TYPE (node) = error_mark_node;
15318 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15320 /* The type of expression must be boolean */
15321 if (TREE_TYPE (expression) != boolean_type_node)
15323 parse_error_context
15324 (wfl_operator,
15325 "Incompatible type for loop conditional. Can't convert %qs to %<boolean%>",
15326 lang_printable_name (TREE_TYPE (expression), 0));
15327 return error_mark_node;
15329 /* Now we know things are allright, invert the condition, fold and
15330 return */
15331 TREE_OPERAND (node, 0) =
15332 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15334 if (! integer_zerop (TREE_OPERAND (node, 0))
15335 && ctxp->current_loop != NULL_TREE
15336 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15337 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15338 if (! integer_onep (TREE_OPERAND (node, 0)))
15339 CAN_COMPLETE_NORMALLY (node) = 1;
15342 TREE_TYPE (node) = void_type_node;
15343 return node;
15346 /* 14.9 Switch statement */
15348 static tree
15349 patch_switch_statement (tree node)
15351 tree se = TREE_OPERAND (node, 0), se_type;
15352 tree save, iter;
15354 /* Complete the switch expression */
15355 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15356 se_type = TREE_TYPE (se);
15357 /* The type of the switch expression must be char, byte, short or
15358 int */
15359 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15361 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15362 parse_error_context (wfl_operator,
15363 "Incompatible type for %<switch%>. Can't convert %qs to %<int%>",
15364 lang_printable_name (se_type, 0));
15365 /* This is what java_complete_tree will check */
15366 TREE_OPERAND (node, 0) = error_mark_node;
15367 return error_mark_node;
15370 /* Save and restore the outer case label list. */
15371 save = case_label_list;
15372 case_label_list = NULL_TREE;
15374 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15376 /* See if we've found a duplicate label. We can't leave this until
15377 code generation, because in `--syntax-only' and `-C' modes we
15378 don't do ordinary code generation. */
15379 for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15381 HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15382 tree subiter;
15383 for (subiter = TREE_CHAIN (iter);
15384 subiter != NULL_TREE;
15385 subiter = TREE_CHAIN (subiter))
15387 HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15388 if (val == subval)
15390 EXPR_WFL_LINECOL (wfl_operator)
15391 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15392 /* The case_label_list is in reverse order, so print the
15393 outer label first. */
15394 parse_error_context (wfl_operator, "duplicate case label: %<"
15395 HOST_WIDE_INT_PRINT_DEC "%>", subval);
15396 EXPR_WFL_LINECOL (wfl_operator)
15397 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15398 parse_error_context (wfl_operator, "original label is here");
15400 break;
15405 case_label_list = save;
15407 /* Ready to return */
15408 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15410 TREE_TYPE (node) = error_mark_node;
15411 return error_mark_node;
15413 TREE_TYPE (node) = void_type_node;
15414 TREE_SIDE_EFFECTS (node) = 1;
15415 CAN_COMPLETE_NORMALLY (node)
15416 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15417 || ! SWITCH_HAS_DEFAULT (node);
15418 return node;
15421 /* Assertions. */
15423 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15424 might be NULL_TREE. */
15425 static tree
15426 build_assertion (
15427 #ifdef USE_MAPPED_LOCATION
15428 source_location location,
15429 #else
15430 int location,
15431 #endif
15432 tree condition, tree value)
15434 tree node;
15435 tree klass = GET_CPC ();
15437 if (! enable_assertions (klass))
15439 condition = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15440 boolean_false_node, condition);
15441 if (value == NULL_TREE)
15442 value = build_java_empty_stmt ();
15443 return build_if_else_statement (location, condition,
15444 value, NULL_TREE);
15447 if (! CLASS_USES_ASSERTIONS (klass))
15449 tree field, classdollar, id, call;
15450 tree class_type = TREE_TYPE (klass);
15452 field = add_field (class_type,
15453 get_identifier ("$assertionsDisabled"),
15454 boolean_type_node,
15455 ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15456 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15457 FIELD_SYNTHETIC (field) = 1;
15459 classdollar = build_incomplete_class_ref (location, class_type);
15461 /* Call CLASS.desiredAssertionStatus(). */
15462 id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15463 call = build3 (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15464 call = make_qualified_primary (classdollar, call, location);
15465 TREE_SIDE_EFFECTS (call) = 1;
15467 /* Invert to obtain !CLASS.desiredAssertionStatus(). This may
15468 seem odd, but we do it to generate code identical to that of
15469 the JDK. */
15470 call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15471 TREE_SIDE_EFFECTS (call) = 1;
15472 DECL_INITIAL (field) = call;
15474 /* Record the initializer in the initializer statement list. */
15475 call = build2 (MODIFY_EXPR, NULL_TREE, field, call);
15476 TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15477 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15478 MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15480 CLASS_USES_ASSERTIONS (klass) = 1;
15483 if (value != NULL_TREE)
15484 value = tree_cons (NULL_TREE, value, NULL_TREE);
15486 node = build_wfl_node (get_identifier ("java"));
15487 node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15488 location);
15489 node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15490 location);
15492 node = build3 (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15493 TREE_SIDE_EFFECTS (node) = 1;
15494 /* It is too early to use BUILD_THROW. */
15495 node = build1 (THROW_EXPR, NULL_TREE, node);
15496 TREE_SIDE_EFFECTS (node) = 1;
15498 /* We invert the condition; if we just put NODE as the `else' part
15499 then we generate weird-looking bytecode. */
15500 condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15501 /* Check $assertionsDisabled. */
15502 condition
15503 = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15504 build1 (TRUTH_NOT_EXPR, NULL_TREE,
15505 build_wfl_node (get_identifier ("$assertionsDisabled"))),
15506 condition);
15507 node = build_if_else_statement (location, condition, node, NULL_TREE);
15508 return node;
15511 /* 14.18 The try/catch statements */
15513 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15514 catches TYPE and executes CATCH_STMTS. */
15516 static tree
15517 encapsulate_with_try_catch (int location, tree type_or_name, tree try_stmts,
15518 tree catch_stmts)
15520 tree try_block, catch_clause_param, catch_block, catch;
15522 /* First build a try block */
15523 try_block = build_expr_block (try_stmts, NULL_TREE);
15525 /* Build a catch block: we need a catch clause parameter */
15526 if (TREE_CODE (type_or_name) == EXPR_WITH_FILE_LOCATION)
15528 tree catch_type = obtain_incomplete_type (type_or_name);
15529 jdep *dep;
15530 catch_clause_param = build_decl (VAR_DECL, wpv_id, catch_type);
15531 register_incomplete_type (JDEP_VARIABLE, type_or_name,
15532 catch_clause_param, catch_type);
15533 dep = CLASSD_LAST (ctxp->classd_list);
15534 JDEP_GET_PATCH (dep) = &TREE_TYPE (catch_clause_param);
15536 else
15537 catch_clause_param = build_decl (VAR_DECL, wpv_id,
15538 build_pointer_type (type_or_name));
15540 /* And a block */
15541 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15543 /* Initialize the variable and store in the block */
15544 catch = build2 (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15545 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15546 add_stmt_to_block (catch_block, NULL_TREE, catch);
15548 /* Add the catch statements */
15549 add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15551 /* Now we can build a JAVA_CATCH_EXPR */
15552 catch_block = build1 (JAVA_CATCH_EXPR, NULL_TREE, catch_block);
15554 return build_try_statement (location, try_block, catch_block);
15557 static tree
15558 build_try_statement (int location, tree try_block, tree catches)
15560 tree node = build2 (TRY_EXPR, NULL_TREE, try_block, catches);
15561 EXPR_WFL_LINECOL (node) = location;
15562 return node;
15565 static tree
15566 build_try_finally_statement (int location, tree try_block, tree finally)
15568 tree node = build2 (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15569 EXPR_WFL_LINECOL (node) = location;
15570 return node;
15573 static tree
15574 patch_try_statement (tree node)
15576 int error_found = 0;
15577 tree try = TREE_OPERAND (node, 0);
15578 /* Exception handlers are considered in left to right order */
15579 tree catch = nreverse (TREE_OPERAND (node, 1));
15580 tree current, caught_type_list = NULL_TREE;
15582 /* Check catch clauses, if any. Every time we find an error, we try
15583 to process the next catch clause. We process the catch clause before
15584 the try block so that when processing the try block we can check thrown
15585 exceptions against the caught type list. */
15586 for (current = catch; current; current = TREE_CHAIN (current))
15588 tree carg_decl, carg_type;
15589 tree sub_current, catch_block, catch_clause;
15590 int unreachable;
15592 /* At this point, the structure of the catch clause is
15593 JAVA_CATCH_EXPR (catch node)
15594 BLOCK (with the decl of the parameter)
15595 COMPOUND_EXPR
15596 MODIFY_EXPR (assignment of the catch parameter)
15597 BLOCK (catch clause block)
15599 catch_clause = TREE_OPERAND (current, 0);
15600 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15601 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15603 /* Catch clauses can't have more than one parameter declared,
15604 but it's already enforced by the grammar. Make sure that the
15605 only parameter of the clause statement in of class Throwable
15606 or a subclass of Throwable, but that was done earlier. The
15607 catch clause parameter type has also been resolved. */
15609 /* Just make sure that the catch clause parameter type inherits
15610 from java.lang.Throwable */
15611 if (!inherits_from_p (carg_type, throwable_type_node))
15613 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15614 parse_error_context (wfl_operator,
15615 "Can't catch class %qs. Catch clause parameter type must be a subclass of class %<java.lang.Throwable%>",
15616 lang_printable_name (carg_type, 0));
15617 error_found = 1;
15618 continue;
15621 /* Partial check for unreachable catch statement: The catch
15622 clause is reachable iff is no earlier catch block A in
15623 the try statement such that the type of the catch
15624 clause's parameter is the same as or a subclass of the
15625 type of A's parameter */
15626 unreachable = 0;
15627 for (sub_current = catch;
15628 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15630 tree sub_catch_clause, decl;
15631 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15632 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15634 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15636 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15637 parse_error_context
15638 (wfl_operator,
15639 "%<catch%> not reached because of the catch clause at line %d",
15640 EXPR_WFL_LINENO (sub_current));
15641 unreachable = error_found = 1;
15642 break;
15645 /* Complete the catch clause block */
15646 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15647 if (catch_block == error_mark_node)
15649 error_found = 1;
15650 continue;
15652 if (CAN_COMPLETE_NORMALLY (catch_block))
15653 CAN_COMPLETE_NORMALLY (node) = 1;
15654 TREE_OPERAND (current, 0) = catch_block;
15656 if (unreachable)
15657 continue;
15659 /* Things to do here: the exception must be thrown */
15661 /* Link this type to the caught type list */
15662 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15665 PUSH_EXCEPTIONS (caught_type_list);
15666 if ((try = java_complete_tree (try)) == error_mark_node)
15667 error_found = 1;
15668 if (CAN_COMPLETE_NORMALLY (try))
15669 CAN_COMPLETE_NORMALLY (node) = 1;
15670 POP_EXCEPTIONS ();
15672 /* Verification ends here */
15673 if (error_found)
15674 return error_mark_node;
15676 TREE_OPERAND (node, 0) = try;
15677 TREE_OPERAND (node, 1) = catch;
15678 TREE_TYPE (node) = void_type_node;
15679 return node;
15682 /* 14.17 The synchronized Statement */
15684 static tree
15685 patch_synchronized_statement (tree node, tree wfl_op1)
15687 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15688 tree block = TREE_OPERAND (node, 1);
15690 tree tmp, enter, exit, expr_decl, assignment;
15692 if (expr == error_mark_node)
15694 block = java_complete_tree (block);
15695 return expr;
15698 /* We might be trying to synchronize on a STRING_CST */
15699 if ((tmp = patch_string (expr)))
15700 expr = tmp;
15702 /* The TYPE of expr must be a reference type */
15703 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15705 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15706 parse_error_context (wfl_operator, "Incompatible type for %<synchronized%>. Can't convert %qs to %<java.lang.Object%>",
15707 lang_printable_name (TREE_TYPE (expr), 0));
15708 return error_mark_node;
15711 /* Generate a try-finally for the synchronized statement, except
15712 that the handler that catches all throw exception calls
15713 _Jv_MonitorExit and then rethrow the exception.
15714 The synchronized statement is then implemented as:
15717 _Jv_MonitorEnter (expression)
15718 synchronized_block
15719 _Jv_MonitorExit (expression)
15721 CATCH_ALL
15723 e = _Jv_exception_info ();
15724 _Jv_MonitorExit (expression)
15725 Throw (e);
15726 } */
15728 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15729 BUILD_MONITOR_ENTER (enter, expr_decl);
15730 BUILD_MONITOR_EXIT (exit, expr_decl);
15731 CAN_COMPLETE_NORMALLY (enter) = 1;
15732 CAN_COMPLETE_NORMALLY (exit) = 1;
15733 assignment = build2 (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15734 TREE_SIDE_EFFECTS (assignment) = 1;
15735 node = build2 (COMPOUND_EXPR, NULL_TREE,
15736 build2 (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15737 build2 (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15738 node = build_expr_block (node, expr_decl);
15740 return java_complete_tree (node);
15743 /* 14.16 The throw Statement */
15745 static tree
15746 patch_throw_statement (tree node, tree wfl_op1)
15748 tree expr = TREE_OPERAND (node, 0);
15749 tree type = TREE_TYPE (expr);
15750 int unchecked_ok = 0, tryblock_throws_ok = 0;
15752 /* Thrown expression must be assignable to java.lang.Throwable */
15753 if (!try_reference_assignconv (throwable_type_node, expr))
15755 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15756 parse_error_context (wfl_operator,
15757 "Can't throw %qs; it must be a subclass of class %<java.lang.Throwable%>",
15758 lang_printable_name (type, 0));
15759 /* If the thrown expression was a reference, we further the
15760 compile-time check. */
15761 if (!JREFERENCE_TYPE_P (type))
15762 return error_mark_node;
15765 /* At least one of the following must be true */
15767 /* The type of the throw expression is a not checked exception,
15768 i.e. is a unchecked expression. */
15769 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15771 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15772 /* An instance can't throw a checked exception unless that exception
15773 is explicitly declared in the `throws' clause of each
15774 constructor. This doesn't apply to anonymous classes, since they
15775 don't have declared constructors. */
15776 if (!unchecked_ok
15777 && DECL_INSTINIT_P (current_function_decl)
15778 && !ANONYMOUS_CLASS_P (current_class))
15780 tree current;
15781 for (current = TYPE_METHODS (current_class); current;
15782 current = TREE_CHAIN (current))
15783 if (DECL_CONSTRUCTOR_P (current)
15784 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15786 parse_error_context (wfl_operator, "Checked exception %qs can't be thrown in instance initializer (not all declared constructor are declaring it in their %<throws%> clause)",
15787 lang_printable_name (TREE_TYPE (expr), 0));
15788 return error_mark_node;
15792 /* Throw is contained in a try statement and at least one catch
15793 clause can receive the thrown expression or the current method is
15794 declared to throw such an exception. Or, the throw statement is
15795 contained in a method or constructor declaration and the type of
15796 the Expression is assignable to at least one type listed in the
15797 throws clause the declaration. */
15798 if (!unchecked_ok)
15799 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15800 if (!(unchecked_ok || tryblock_throws_ok))
15802 /* If there is a surrounding try block that has no matching
15803 clatch clause, report it first. A surrounding try block exits
15804 only if there is something after the list of checked
15805 exception thrown by the current function (if any). */
15806 if (IN_TRY_BLOCK_P ())
15807 parse_error_context (wfl_operator, "Checked exception %qs can't be caught by any of the catch clause(s) of the surrounding %<try%> block",
15808 lang_printable_name (type, 0));
15809 /* If we have no surrounding try statement and the method doesn't have
15810 any throws, report it now. FIXME */
15812 /* We report that the exception can't be throw from a try block
15813 in all circumstances but when the `throw' is inside a static
15814 block. */
15815 else if (!EXCEPTIONS_P (currently_caught_type_list)
15816 && !tryblock_throws_ok)
15818 if (DECL_CLINIT_P (current_function_decl))
15819 parse_error_context (wfl_operator,
15820 "Checked exception %qs can't be thrown in initializer",
15821 lang_printable_name (type, 0));
15822 else
15823 parse_error_context (wfl_operator,
15824 "Checked exception %qs isn't thrown from a %<try%> block",
15825 lang_printable_name (type, 0));
15827 /* Otherwise, the current method doesn't have the appropriate
15828 throws declaration */
15829 else
15830 parse_error_context (wfl_operator, "Checked exception %qs doesn't match any of current method's %<throws%> declaration(s)",
15831 lang_printable_name (type, 0));
15832 return error_mark_node;
15835 if (! flag_emit_class_files)
15836 BUILD_THROW (node, expr);
15838 return node;
15841 /* Check that exception said to be thrown by method DECL can be
15842 effectively caught from where DECL is invoked. THIS_EXPR is the
15843 expression that computes `this' for the method call. */
15844 static void
15845 check_thrown_exceptions (
15846 #ifdef USE_MAPPED_LOCATION
15847 source_location location,
15848 #else
15850 int location,
15851 #endif
15852 tree decl, tree this_expr)
15854 tree throws;
15855 int is_array_call = 0;
15857 /* Skip check within generated methods, such as access$<n>. */
15858 if (OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (current_function_decl)))
15859 return;
15861 if (this_expr != NULL_TREE
15862 && TREE_CODE (TREE_TYPE (this_expr)) == POINTER_TYPE
15863 && TYPE_ARRAY_P (TREE_TYPE (TREE_TYPE (this_expr))))
15864 is_array_call = 1;
15866 /* For all the unchecked exceptions thrown by DECL. */
15867 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15868 throws = TREE_CHAIN (throws))
15869 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15871 /* Suppress errors about cloning arrays. */
15872 if (is_array_call && DECL_NAME (decl) == get_identifier ("clone"))
15873 continue;
15875 #ifdef USE_MAPPED_LOCATION
15876 SET_EXPR_LOCATION (wfl_operator, location);
15877 #else
15878 EXPR_WFL_LINECOL (wfl_operator) = location;
15879 #endif
15880 if (DECL_FINIT_P (current_function_decl))
15881 parse_error_context
15882 (wfl_operator, "Exception %qs can't be thrown in initializer",
15883 lang_printable_name (TREE_VALUE (throws), 0));
15884 else
15886 parse_error_context
15887 (wfl_operator, "Exception %qs must be caught, or it must be declared in the %<throws%> clause of %qs",
15888 lang_printable_name (TREE_VALUE (throws), 0),
15889 (DECL_INIT_P (current_function_decl) ?
15890 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15891 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15896 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15897 try-catch blocks, OR is listed in the `throws' clause of the
15898 current method. */
15900 static int
15901 check_thrown_exceptions_do (tree exception)
15903 tree list = currently_caught_type_list;
15904 resolve_and_layout (exception, NULL_TREE);
15905 /* First, all the nested try-catch-finally at that stage. The
15906 last element contains `throws' clause exceptions, if any. */
15907 if (IS_UNCHECKED_EXCEPTION_P (exception))
15908 return 1;
15909 while (list)
15911 tree caught;
15912 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15913 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15914 return 1;
15915 list = TREE_CHAIN (list);
15917 return 0;
15920 static void
15921 purge_unchecked_exceptions (tree mdecl)
15923 tree throws = DECL_FUNCTION_THROWS (mdecl);
15924 tree new = NULL_TREE;
15926 while (throws)
15928 tree next = TREE_CHAIN (throws);
15929 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15931 TREE_CHAIN (throws) = new;
15932 new = throws;
15934 throws = next;
15936 /* List is inverted here, but it doesn't matter */
15937 DECL_FUNCTION_THROWS (mdecl) = new;
15940 /* This function goes over all of CLASS_TYPE ctors and checks whether
15941 each of them features at least one unchecked exception in its
15942 `throws' clause. If it's the case, it returns `true', `false'
15943 otherwise. */
15945 static bool
15946 ctors_unchecked_throws_clause_p (tree class_type)
15948 tree current;
15950 for (current = TYPE_METHODS (class_type); current;
15951 current = TREE_CHAIN (current))
15953 bool ctu = false; /* Ctor Throws Unchecked */
15954 if (DECL_CONSTRUCTOR_P (current))
15956 tree throws;
15957 for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
15958 throws = TREE_CHAIN (throws))
15959 if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
15960 ctu = true;
15962 /* We return false as we found one ctor that is unfit. */
15963 if (!ctu && DECL_CONSTRUCTOR_P (current))
15964 return false;
15966 /* All ctors feature at least one unchecked exception in their
15967 `throws' clause. */
15968 return true;
15971 /* 15.24 Conditional Operator ?: */
15973 static tree
15974 patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
15976 tree cond = TREE_OPERAND (node, 0);
15977 tree op1 = TREE_OPERAND (node, 1);
15978 tree op2 = TREE_OPERAND (node, 2);
15979 tree resulting_type = NULL_TREE;
15980 tree t1, t2, patched;
15981 int error_found = 0;
15983 /* The condition and operands of ?: might be StringBuffers crafted
15984 as a result of a string concatenation. Obtain decent ones here. */
15985 if ((patched = patch_string (cond)))
15986 TREE_OPERAND (node, 0) = cond = patched;
15987 if ((patched = patch_string (op1)))
15988 TREE_OPERAND (node, 1) = op1 = patched;
15989 if ((patched = patch_string (op2)))
15990 TREE_OPERAND (node, 2) = op2 = patched;
15992 t1 = TREE_TYPE (op1);
15993 t2 = TREE_TYPE (op2);
15995 /* The first expression must be a boolean */
15996 if (TREE_TYPE (cond) != boolean_type_node)
15998 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15999 parse_error_context (wfl_operator,
16000 "Incompatible type for %<?:%>. Can't convert %qs to %<boolean%>",
16001 lang_printable_name (TREE_TYPE (cond), 0));
16002 error_found = 1;
16005 /* Second and third can be numeric, boolean (i.e. primitive),
16006 references or null. Anything else results in an error */
16007 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
16008 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
16009 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
16010 || (t1 == boolean_type_node && t2 == boolean_type_node)))
16011 error_found = 1;
16013 /* Determine the type of the conditional expression. Same types are
16014 easy to deal with */
16015 else if (t1 == t2)
16016 resulting_type = t1;
16018 /* There are different rules for numeric types */
16019 else if (JNUMERIC_TYPE_P (t1))
16021 /* if byte/short found, the resulting type is short */
16022 if ((t1 == byte_type_node && t2 == short_type_node)
16023 || (t1 == short_type_node && t2 == byte_type_node))
16024 resulting_type = short_type_node;
16026 /* If t1 is a constant int and t2 is of type byte, short or char
16027 and t1's value fits in t2, then the resulting type is t2 */
16028 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
16029 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
16030 resulting_type = t2;
16032 /* If t2 is a constant int and t1 is of type byte, short or char
16033 and t2's value fits in t1, then the resulting type is t1 */
16034 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
16035 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
16036 resulting_type = t1;
16038 /* Otherwise, binary numeric promotion is applied and the
16039 resulting type is the promoted type of operand 1 and 2 */
16040 else
16041 resulting_type = binary_numeric_promotion (t1, t2,
16042 &TREE_OPERAND (node, 1),
16043 &TREE_OPERAND (node, 2));
16046 /* Cases of a reference and a null type */
16047 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
16048 resulting_type = t1;
16050 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
16051 resulting_type = t2;
16053 /* Last case: different reference types. If a type can be converted
16054 into the other one by assignment conversion, the latter
16055 determines the type of the expression */
16056 else if ((resulting_type = try_reference_assignconv (t1, op2)))
16057 resulting_type = promote_type (t1);
16059 else if ((resulting_type = try_reference_assignconv (t2, op1)))
16060 resulting_type = promote_type (t2);
16062 /* If we don't have any resulting type, we're in trouble */
16063 if (!resulting_type)
16065 char *t = xstrdup (lang_printable_name (t1, 0));
16066 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16067 parse_error_context (wfl_operator,
16068 "Incompatible type for %<?:%>. Can't convert %qs to %qs",
16069 t, lang_printable_name (t2, 0));
16070 free (t);
16071 error_found = 1;
16074 if (error_found)
16076 TREE_TYPE (node) = error_mark_node;
16077 return error_mark_node;
16080 TREE_TYPE (node) = resulting_type;
16081 TREE_SET_CODE (node, COND_EXPR);
16082 CAN_COMPLETE_NORMALLY (node) = 1;
16083 return node;
16086 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16088 static tree
16089 maybe_build_class_init_for_field (tree decl, tree expr)
16091 tree clas = DECL_CONTEXT (decl);
16092 if (flag_emit_class_files)
16093 return expr;
16095 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16096 && FIELD_FINAL (decl))
16098 tree init = DECL_INITIAL (decl);
16099 if (init != NULL_TREE)
16100 init = fold_constant_for_init (init, decl);
16101 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16102 return expr;
16105 return build_class_init (clas, expr);
16108 /* Try to constant fold NODE.
16109 If NODE is not a constant expression, return NULL_EXPR.
16110 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16112 static tree
16113 fold_constant_for_init (tree node, tree context)
16115 tree op0, op1, val;
16116 enum tree_code code = TREE_CODE (node);
16118 switch (code)
16120 case INTEGER_CST:
16121 if (node == null_pointer_node)
16122 return NULL_TREE;
16123 case STRING_CST:
16124 case REAL_CST:
16125 return node;
16127 case PLUS_EXPR:
16128 case MINUS_EXPR:
16129 case MULT_EXPR:
16130 case TRUNC_MOD_EXPR:
16131 case RDIV_EXPR:
16132 case LSHIFT_EXPR:
16133 case RSHIFT_EXPR:
16134 case URSHIFT_EXPR:
16135 case BIT_AND_EXPR:
16136 case BIT_XOR_EXPR:
16137 case BIT_IOR_EXPR:
16138 case TRUTH_ANDIF_EXPR:
16139 case TRUTH_ORIF_EXPR:
16140 case EQ_EXPR:
16141 case NE_EXPR:
16142 case GT_EXPR:
16143 case GE_EXPR:
16144 case LT_EXPR:
16145 case LE_EXPR:
16146 op0 = TREE_OPERAND (node, 0);
16147 op1 = TREE_OPERAND (node, 1);
16148 val = fold_constant_for_init (op0, context);
16149 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16150 return NULL_TREE;
16151 TREE_OPERAND (node, 0) = val;
16152 val = fold_constant_for_init (op1, context);
16153 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16154 return NULL_TREE;
16155 TREE_OPERAND (node, 1) = val;
16156 return patch_binop (node, op0, op1);
16158 case UNARY_PLUS_EXPR:
16159 case NEGATE_EXPR:
16160 case TRUTH_NOT_EXPR:
16161 case BIT_NOT_EXPR:
16162 case CONVERT_EXPR:
16163 op0 = TREE_OPERAND (node, 0);
16164 val = fold_constant_for_init (op0, context);
16165 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16166 return NULL_TREE;
16167 TREE_OPERAND (node, 0) = val;
16168 val = patch_unaryop (node, op0);
16169 if (! TREE_CONSTANT (val))
16170 return NULL_TREE;
16171 return val;
16173 break;
16175 case COND_EXPR:
16176 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16177 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16178 return NULL_TREE;
16179 TREE_OPERAND (node, 0) = val;
16180 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16181 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16182 return NULL_TREE;
16183 TREE_OPERAND (node, 1) = val;
16184 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16185 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16186 return NULL_TREE;
16187 TREE_OPERAND (node, 2) = val;
16188 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
16189 : TREE_OPERAND (node, 2);
16191 case VAR_DECL:
16192 case FIELD_DECL:
16193 if (! FIELD_FINAL (node)
16194 || DECL_INITIAL (node) == NULL_TREE)
16195 return NULL_TREE;
16196 val = DECL_INITIAL (node);
16197 /* Guard against infinite recursion. */
16198 DECL_INITIAL (node) = NULL_TREE;
16199 val = fold_constant_for_init (val, node);
16200 if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16201 val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16202 DECL_INITIAL (node) = val;
16203 return val;
16205 case EXPR_WITH_FILE_LOCATION:
16206 /* Compare java_complete_tree and resolve_expression_name. */
16207 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16208 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16210 tree name = EXPR_WFL_NODE (node);
16211 tree decl;
16212 if (PRIMARY_P (node))
16213 return NULL_TREE;
16214 else if (! QUALIFIED_P (name))
16216 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16217 if (decl == NULL_TREE
16218 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16219 return NULL_TREE;
16220 return fold_constant_for_init (decl, decl);
16222 else
16224 tree r = NULL_TREE;
16225 /* Install the proper context for the field resolution. */
16226 tree saved_current_class = current_class;
16227 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
16228 current_class = DECL_CONTEXT (context);
16229 qualify_ambiguous_name (node);
16230 r = resolve_field_access (node, &decl, NULL);
16231 /* Restore prior context. */
16232 current_class = saved_current_class;
16233 if (r != error_mark_node && decl != NULL_TREE)
16234 return fold_constant_for_init (decl, decl);
16235 return NULL_TREE;
16238 else
16240 op0 = TREE_OPERAND (node, 0);
16241 val = fold_constant_for_init (op0, context);
16242 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16243 return NULL_TREE;
16244 TREE_OPERAND (node, 0) = val;
16245 return val;
16248 #ifdef USE_COMPONENT_REF
16249 case IDENTIFIER:
16250 case COMPONENT_REF:
16252 #endif
16254 default:
16255 return NULL_TREE;
16259 #ifdef USE_COMPONENT_REF
16260 /* Context is 'T' for TypeName, 'P' for PackageName,
16261 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16263 tree
16264 resolve_simple_name (tree name, int context)
16268 tree
16269 resolve_qualified_name (tree name, int context)
16272 #endif
16274 void
16275 init_src_parse (void)
16277 /* Sanity check; we've been bit by this before. */
16278 if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16279 abort ();
16284 /* This section deals with the functions that are called when tables
16285 recording class initialization information are traversed. */
16287 /* This function is called for each class that is known definitely
16288 initialized when a given static method was called. This function
16289 augments a compound expression (INFO) storing all assignment to
16290 initialized static class flags if a flag already existed, otherwise
16291 a new one is created. */
16293 static int
16294 emit_test_initialization (void **entry_p, void *info)
16296 tree l = (tree) info;
16297 tree decl, init;
16298 tree key = (tree) *entry_p;
16299 tree *ite;
16300 htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16302 /* If we haven't found a flag and we're dealing with self registered
16303 with current_function_decl, then don't do anything. Self is
16304 always added as definitely initialized but this information is
16305 valid only if used outside the current function. */
16306 if (current_function_decl == TREE_PURPOSE (l)
16307 && java_treetreehash_find (cf_ht, key) == NULL)
16308 return true;
16310 ite = java_treetreehash_new (cf_ht, key);
16312 /* If we don't have a variable, create one and install it. */
16313 if (*ite == NULL)
16315 tree block;
16317 decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16318 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16319 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16320 DECL_CONTEXT (decl) = current_function_decl;
16321 DECL_INITIAL (decl) = boolean_true_node;
16322 /* Don't emit any symbolic debugging info for this decl. */
16323 DECL_IGNORED_P (decl) = 1;
16325 /* The trick is to find the right context for it. */
16326 block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16327 TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16328 BLOCK_EXPR_DECLS (block) = decl;
16329 *ite = decl;
16331 else
16332 decl = *ite;
16334 /* Now simply augment the compound that holds all the assignments
16335 pertaining to this method invocation. */
16336 init = build2 (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16337 TREE_SIDE_EFFECTS (init) = 1;
16338 TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16339 TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16341 return true;
16344 #ifdef __XGETTEXT__
16345 /* Depending on the version of Bison used to compile this grammar,
16346 it may issue generic diagnostics spelled "syntax error" or
16347 "parse error". To prevent this from changing the translation
16348 template randomly, we list all the variants of this particular
16349 diagnostic here. Translators: there is no fine distinction
16350 between diagnostics with "syntax error" in them, and diagnostics
16351 with "parse error" in them. It's okay to give them both the same
16352 translation. */
16353 const char d1[] = N_("syntax error");
16354 const char d2[] = N_("parse error");
16355 const char d3[] = N_("syntax error; also virtual memory exhausted");
16356 const char d4[] = N_("parse error; also virtual memory exhausted");
16357 const char d5[] = N_("syntax error: cannot back up");
16358 const char d6[] = N_("parse error: cannot back up");
16359 #endif
16361 #include "gt-java-parse.h"
16362 #include "gtype-java.h"