PR target/4198
[official-gcc.git] / gcc / java / parse.y
blob7410224320002aa5dd22ccb63d340d19ffde3421
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;
365 /* binop_lookup maps token to tree_code. It is used where binary
366 operations are involved and required by the parser. RDIV_EXPR
367 covers both integral/floating point division. The code is changed
368 once the type of both operator is worked out. */
370 static const enum tree_code binop_lookup[19] =
372 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
373 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
374 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
375 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
376 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
378 #define BINOP_LOOKUP(VALUE) \
379 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
381 /* This is the end index for binary operators that can also be used
382 in compound assignments. */
383 #define BINOP_COMPOUND_CANDIDATES 11
385 /* The "$L" identifier we use to create labels. */
386 static GTY(()) tree label_id;
388 /* The "StringBuffer" identifier used for the String `+' operator. */
389 static GTY(()) tree wfl_string_buffer;
391 /* The "append" identifier used for String `+' operator. */
392 static GTY(()) tree wfl_append;
394 /* The "toString" identifier used for String `+' operator. */
395 static GTY(()) tree wfl_to_string;
397 /* The "java.lang" import qualified name. */
398 static GTY(()) tree java_lang_id;
400 /* The generated `inst$' identifier used for generated enclosing
401 instance/field access functions. */
402 static GTY(()) tree inst_id;
404 /* Context and flag for static blocks */
405 static GTY(()) tree current_static_block;
407 /* The generated `write_parm_value$' identifier. */
408 static GTY(()) tree wpv_id;
410 /* The list of all packages we've seen so far */
411 static GTY(()) tree package_list;
413 /* Hold THIS for the scope of the current method decl. */
414 static GTY(()) tree current_this;
416 /* Hold a list of catch clauses list. The first element of this list is
417 the list of the catch clauses of the currently analyzed try block. */
418 static GTY(()) tree currently_caught_type_list;
420 /* This holds a linked list of all the case labels for the current
421 switch statement. It is only used when checking to see if there
422 are duplicate labels. FIXME: probably this should just be attached
423 to the switch itself; then it could be referenced via
424 `ctxp->current_loop'. */
425 static GTY(()) tree case_label_list;
427 /* Anonymous class counter. Will be reset to 1 every time a non
428 anonymous class gets created. */
429 static int anonymous_class_counter = 1;
431 static GTY(()) tree src_parse_roots[1];
433 /* All classes seen from source code */
434 #define gclass_list src_parse_roots[0]
436 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
437 line and point it out. */
438 /* Should point out the one that don't fit. ASCII/unicode, going
439 backward. FIXME */
441 #define check_modifiers(__message, __value, __mask) do { \
442 if ((__value) & ~(__mask)) \
444 size_t i, remainder = (__value) & ~(__mask); \
445 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++) \
446 if ((1 << i) & remainder) \
447 parse_error_context (ctxp->modifier_ctx [i], (__message), \
448 java_accstring_lookup (1 << i)); \
450 } while (0)
454 %union {
455 tree node;
456 int sub_token;
457 struct {
458 int token;
459 #ifdef USE_MAPPED_LOCATION
460 source_location location;
461 #else
462 int location;
463 #endif
464 } operator;
465 int value;
469 #ifdef USE_MAPPED_LOCATION
470 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
471 SET_EXPR_LOCATION(EXPR, (TOKEN).location)
472 #else
473 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
474 (EXPR_WFL_LINECOL (EXPR) = (TOKEN).location)
475 #endif
477 #include "lex.c"
480 %pure_parser
482 /* Things defined here have to match the order of what's in the
483 binop_lookup table. */
485 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
486 %token LS_TK SRS_TK ZRS_TK
487 %token AND_TK XOR_TK OR_TK
488 %token BOOL_AND_TK BOOL_OR_TK
489 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
491 /* This maps to the same binop_lookup entry than the token above */
493 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
494 %token REM_ASSIGN_TK
495 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
496 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
499 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
501 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
502 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
503 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
504 %token PAD_TK ABSTRACT_TK STRICT_TK
505 %token MODIFIER_TK
507 /* Keep those two in order, too */
508 %token DECR_TK INCR_TK
510 /* From now one, things can be in any order */
512 %token DEFAULT_TK IF_TK THROW_TK
513 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
514 %token THROWS_TK BREAK_TK IMPORT_TK
515 %token ELSE_TK INSTANCEOF_TK RETURN_TK
516 %token VOID_TK CATCH_TK INTERFACE_TK
517 %token CASE_TK EXTENDS_TK FINALLY_TK
518 %token SUPER_TK WHILE_TK CLASS_TK
519 %token SWITCH_TK CONST_TK TRY_TK
520 %token FOR_TK NEW_TK CONTINUE_TK
521 %token GOTO_TK PACKAGE_TK THIS_TK
522 %token ASSERT_TK
524 %token BYTE_TK SHORT_TK INT_TK LONG_TK
525 %token CHAR_TK INTEGRAL_TK
527 %token FLOAT_TK DOUBLE_TK FP_TK
529 %token ID_TK
531 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
533 %token ASSIGN_ANY_TK ASSIGN_TK
534 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
536 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
537 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
539 %type <value> modifiers MODIFIER_TK final synchronized
541 %type <node> super ID_TK identifier
542 %type <node> name simple_name qualified_name
543 %type <node> type_declaration compilation_unit
544 field_declaration method_declaration extends_interfaces
545 interfaces interface_type_list
546 import_declarations package_declaration
547 type_declarations interface_body
548 interface_member_declaration constant_declaration
549 interface_member_declarations interface_type
550 abstract_method_declaration
551 %type <node> class_body_declaration class_member_declaration
552 static_initializer constructor_declaration block
553 %type <node> class_body_declarations constructor_header
554 %type <node> class_or_interface_type class_type class_type_list
555 constructor_declarator explicit_constructor_invocation
556 %type <node> dim_expr dim_exprs this_or_super throws
558 %type <node> variable_declarator_id variable_declarator
559 variable_declarators variable_initializer
560 variable_initializers constructor_body
561 array_initializer
563 %type <node> class_body block_end constructor_block_end
564 %type <node> statement statement_without_trailing_substatement
565 labeled_statement if_then_statement label_decl
566 if_then_else_statement while_statement for_statement
567 statement_nsi labeled_statement_nsi do_statement
568 if_then_else_statement_nsi while_statement_nsi
569 for_statement_nsi statement_expression_list for_init
570 for_update statement_expression expression_statement
571 primary_no_new_array expression primary array_type
572 array_creation_initialized array_creation_uninitialized
573 class_instance_creation_expression field_access
574 method_invocation array_access something_dot_new
575 argument_list postfix_expression while_expression
576 post_increment_expression post_decrement_expression
577 unary_expression_not_plus_minus unary_expression
578 pre_increment_expression pre_decrement_expression
579 cast_expression
580 multiplicative_expression additive_expression
581 shift_expression relational_expression
582 equality_expression and_expression
583 exclusive_or_expression inclusive_or_expression
584 conditional_and_expression conditional_or_expression
585 conditional_expression assignment_expression
586 left_hand_side assignment for_header for_begin
587 constant_expression do_statement_begin empty_statement
588 switch_statement synchronized_statement throw_statement
589 try_statement assert_statement
590 switch_expression switch_block
591 catches catch_clause catch_clause_parameter finally
592 anonymous_class_creation trap_overflow_corner_case
593 %type <node> return_statement break_statement continue_statement
595 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
596 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
597 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
598 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
599 %type <operator> ASSIGN_ANY_TK assignment_operator
600 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
601 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
602 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
603 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
604 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
605 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
606 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
607 %type <operator> NEW_TK ASSERT_TK
609 %type <node> method_body
611 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
612 STRING_LIT_TK NULL_TK VOID_TK
614 %type <node> IF_TK WHILE_TK FOR_TK
616 %type <node> formal_parameter_list formal_parameter
617 method_declarator method_header
619 %type <node> primitive_type reference_type type
620 BOOLEAN_TK INTEGRAL_TK FP_TK
622 /* Added or modified JDK 1.1 rule types */
623 %type <node> type_literals
626 /* 19.2 Production from 2.3: The Syntactic Grammar */
627 goal: compilation_unit
631 /* 19.3 Productions from 3: Lexical structure */
632 literal:
633 INT_LIT_TK
634 | FP_LIT_TK
635 | BOOL_LIT_TK
636 | CHAR_LIT_TK
637 | STRING_LIT_TK
638 | NULL_TK
641 /* 19.4 Productions from 4: Types, Values and Variables */
642 type:
643 primitive_type
644 | reference_type
647 primitive_type:
648 INTEGRAL_TK
649 | FP_TK
650 | BOOLEAN_TK
653 reference_type:
654 class_or_interface_type
655 | array_type
658 class_or_interface_type:
659 name
662 class_type:
663 class_or_interface_type /* Default rule */
666 interface_type:
667 class_or_interface_type
670 array_type:
671 primitive_type dims
673 int osb = pop_current_osb (ctxp);
674 tree t = build_java_array_type (($1), -1);
675 while (--osb)
676 t = build_unresolved_array_type (t);
677 $$ = t;
679 | name dims
681 int osb = pop_current_osb (ctxp);
682 tree t = $1;
683 while (osb--)
684 t = build_unresolved_array_type (t);
685 $$ = t;
689 /* 19.5 Productions from 6: Names */
690 name:
691 simple_name /* Default rule */
692 | qualified_name /* Default rule */
695 simple_name:
696 identifier /* Default rule */
699 qualified_name:
700 name DOT_TK identifier
701 { $$ = make_qualified_name ($1, $3, $2.location); }
704 identifier:
705 ID_TK
708 /* 19.6: Production from 7: Packages */
709 compilation_unit:
710 {$$ = NULL;}
711 | package_declaration
712 | import_declarations
713 | type_declarations
714 | package_declaration import_declarations
715 | package_declaration type_declarations
716 | import_declarations type_declarations
717 | package_declaration import_declarations type_declarations
720 import_declarations:
721 import_declaration
723 $$ = NULL;
725 | import_declarations import_declaration
727 $$ = NULL;
731 type_declarations:
732 type_declaration
733 | type_declarations type_declaration
736 package_declaration:
737 PACKAGE_TK name SC_TK
739 ctxp->package = EXPR_WFL_NODE ($2);
740 register_package (ctxp->package);
742 | PACKAGE_TK error
743 {yyerror ("Missing name"); RECOVER;}
744 | PACKAGE_TK name error
745 {yyerror ("';' expected"); RECOVER;}
748 import_declaration:
749 single_type_import_declaration
750 | type_import_on_demand_declaration
753 single_type_import_declaration:
754 IMPORT_TK name SC_TK
756 tree name = EXPR_WFL_NODE ($2), last_name;
757 int i = IDENTIFIER_LENGTH (name)-1;
758 const char *last = &IDENTIFIER_POINTER (name)[i];
759 while (last != IDENTIFIER_POINTER (name))
761 if (last [0] == '.')
762 break;
763 last--;
765 last_name = get_identifier (++last);
766 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
768 tree err = find_name_in_single_imports (last_name);
769 if (err && err != name)
770 parse_error_context
771 ($2, "Ambiguous class: %qs and %qs",
772 IDENTIFIER_POINTER (name),
773 IDENTIFIER_POINTER (err));
774 else
775 REGISTER_IMPORT ($2, last_name);
777 else
778 REGISTER_IMPORT ($2, last_name);
780 | IMPORT_TK error
781 {yyerror ("Missing name"); RECOVER;}
782 | IMPORT_TK name error
783 {yyerror ("';' expected"); RECOVER;}
786 type_import_on_demand_declaration:
787 IMPORT_TK name DOT_TK MULT_TK SC_TK
789 tree name = EXPR_WFL_NODE ($2);
790 tree it;
791 /* Search for duplicates. */
792 for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
793 if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
794 break;
795 /* Don't import the same thing more than once, just ignore
796 duplicates (7.5.2) */
797 if (! it)
799 read_import_dir ($2);
800 ctxp->import_demand_list =
801 chainon (ctxp->import_demand_list,
802 build_tree_list ($2, NULL_TREE));
805 | IMPORT_TK name DOT_TK error
806 {yyerror ("'*' expected"); RECOVER;}
807 | IMPORT_TK name DOT_TK MULT_TK error
808 {yyerror ("';' expected"); RECOVER;}
811 type_declaration:
812 class_declaration
813 { end_class_declaration (0); }
814 | interface_declaration
815 { end_class_declaration (0); }
816 | empty_statement
817 | error
819 YYERROR_NOW;
820 yyerror ("Class or interface declaration expected");
824 /* 19.7 Shortened from the original:
825 modifiers: modifier | modifiers modifier
826 modifier: any of public... */
827 modifiers:
828 MODIFIER_TK
830 $$ = (1 << $1);
832 | modifiers MODIFIER_TK
834 int acc = (1 << $2);
835 if ($$ & acc)
836 parse_error_context
837 (ctxp->modifier_ctx [$2], "Modifier %qs declared twice",
838 java_accstring_lookup (acc));
839 else
841 $$ |= acc;
846 /* 19.8.1 Production from $8.1: Class Declaration */
847 class_declaration:
848 modifiers CLASS_TK identifier super interfaces
849 { create_class ($1, $3, $4, $5); }
850 class_body
852 | CLASS_TK identifier super interfaces
853 { create_class (0, $2, $3, $4); }
854 class_body
856 | modifiers CLASS_TK error
857 { yyerror ("Missing class name"); RECOVER; }
858 | CLASS_TK error
859 { yyerror ("Missing class name"); RECOVER; }
860 | CLASS_TK identifier error
862 if (!ctxp->class_err) yyerror ("'{' expected");
863 DRECOVER(class1);
865 | modifiers CLASS_TK identifier error
866 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
869 super:
870 { $$ = NULL; }
871 | EXTENDS_TK class_type
872 { $$ = $2; }
873 | EXTENDS_TK class_type error
874 {yyerror ("'{' expected"); ctxp->class_err=1;}
875 | EXTENDS_TK error
876 {yyerror ("Missing super class name"); ctxp->class_err=1;}
879 interfaces:
880 { $$ = NULL_TREE; }
881 | IMPLEMENTS_TK interface_type_list
882 { $$ = $2; }
883 | IMPLEMENTS_TK error
885 ctxp->class_err=1;
886 yyerror ("Missing interface name");
890 interface_type_list:
891 interface_type
893 ctxp->interface_number = 1;
894 $$ = build_tree_list ($1, NULL_TREE);
896 | interface_type_list C_TK interface_type
898 ctxp->interface_number++;
899 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
901 | interface_type_list C_TK error
902 {yyerror ("Missing interface name"); RECOVER;}
905 class_body:
906 OCB_TK CCB_TK
908 $$ = GET_CPC ();
910 | OCB_TK class_body_declarations CCB_TK
912 $$ = GET_CPC ();
916 class_body_declarations:
917 class_body_declaration
918 | class_body_declarations class_body_declaration
921 class_body_declaration:
922 class_member_declaration
923 | static_initializer
924 | constructor_declaration
925 | block /* Added, JDK1.1, instance initializer */
927 if (!IS_EMPTY_STMT ($1))
929 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
930 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
935 class_member_declaration:
936 field_declaration
937 | method_declaration
938 | class_declaration /* Added, JDK1.1 inner classes */
939 { end_class_declaration (1); }
940 | interface_declaration /* Added, JDK1.1 inner interfaces */
941 { end_class_declaration (1); }
942 | empty_statement
945 /* 19.8.2 Productions from 8.3: Field Declarations */
946 field_declaration:
947 type variable_declarators SC_TK
948 { register_fields (0, $1, $2); }
949 | modifiers type variable_declarators SC_TK
951 check_modifiers
952 ("Illegal modifier %qs for field declaration",
953 $1, FIELD_MODIFIERS);
954 check_modifiers_consistency ($1);
955 register_fields ($1, $2, $3);
959 variable_declarators:
960 /* Should we use build_decl_list () instead ? FIXME */
961 variable_declarator /* Default rule */
962 | variable_declarators C_TK variable_declarator
963 { $$ = chainon ($1, $3); }
964 | variable_declarators C_TK error
965 {yyerror ("Missing term"); RECOVER;}
968 variable_declarator:
969 variable_declarator_id
970 { $$ = build_tree_list ($1, NULL_TREE); }
971 | variable_declarator_id ASSIGN_TK variable_initializer
973 if (java_error_count)
974 $3 = NULL_TREE;
975 $$ = build_tree_list
976 ($1, build_assignment ($2.token, $2.location, $1, $3));
978 | variable_declarator_id ASSIGN_TK error
980 yyerror ("Missing variable initializer");
981 $$ = build_tree_list ($1, NULL_TREE);
982 RECOVER;
984 | variable_declarator_id ASSIGN_TK variable_initializer error
986 yyerror ("';' expected");
987 $$ = build_tree_list ($1, NULL_TREE);
988 RECOVER;
992 variable_declarator_id:
993 identifier
994 | variable_declarator_id OSB_TK CSB_TK
995 { $$ = build_unresolved_array_type ($1); }
996 | identifier error
997 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
998 | variable_declarator_id OSB_TK error
1000 yyerror ("']' expected");
1001 DRECOVER(vdi);
1003 | variable_declarator_id CSB_TK error
1004 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1007 variable_initializer:
1008 expression
1009 | array_initializer
1012 /* 19.8.3 Productions from 8.4: Method Declarations */
1013 method_declaration:
1014 method_header
1016 current_function_decl = $1;
1017 if (current_function_decl
1018 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1019 source_start_java_method (current_function_decl);
1020 else
1021 current_function_decl = NULL_TREE;
1023 method_body
1024 { finish_method_declaration ($3); }
1025 | method_header error
1026 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1029 method_header:
1030 type method_declarator throws
1031 { $$ = method_header (0, $1, $2, $3); }
1032 | VOID_TK method_declarator throws
1033 { $$ = method_header (0, void_type_node, $2, $3); }
1034 | modifiers type method_declarator throws
1035 { $$ = method_header ($1, $2, $3, $4); }
1036 | modifiers VOID_TK method_declarator throws
1037 { $$ = method_header ($1, void_type_node, $3, $4); }
1038 | type error
1040 yyerror ("Invalid method declaration, method name required");
1041 RECOVER;
1043 | modifiers type error
1045 yyerror ("Identifier expected");
1046 RECOVER;
1048 | VOID_TK error
1050 yyerror ("Identifier expected");
1051 RECOVER;
1053 | modifiers VOID_TK error
1055 yyerror ("Identifier expected");
1056 RECOVER;
1058 | modifiers error
1060 yyerror ("Invalid method declaration, return type required");
1061 RECOVER;
1065 method_declarator:
1066 identifier OP_TK CP_TK
1068 ctxp->formal_parameter_number = 0;
1069 $$ = method_declarator ($1, NULL_TREE);
1071 | identifier OP_TK formal_parameter_list CP_TK
1072 { $$ = method_declarator ($1, $3); }
1073 | method_declarator OSB_TK CSB_TK
1075 SET_EXPR_LOCATION_FROM_TOKEN (wfl_operator, $2);
1076 TREE_PURPOSE ($1) =
1077 build_unresolved_array_type (TREE_PURPOSE ($1));
1078 parse_warning_context
1079 (wfl_operator,
1080 "Discouraged form of returned type specification");
1082 | identifier OP_TK error
1083 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1084 | method_declarator OSB_TK error
1085 {yyerror ("']' expected"); RECOVER;}
1088 formal_parameter_list:
1089 formal_parameter
1091 ctxp->formal_parameter_number = 1;
1093 | formal_parameter_list C_TK formal_parameter
1095 ctxp->formal_parameter_number += 1;
1096 $$ = chainon ($1, $3);
1098 | formal_parameter_list C_TK error
1099 { yyerror ("Missing formal parameter term"); RECOVER; }
1102 formal_parameter:
1103 type variable_declarator_id
1105 $$ = build_tree_list ($2, $1);
1107 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1109 $$ = build_tree_list ($3, $2);
1110 ARG_FINAL_P ($$) = 1;
1112 | type error
1114 yyerror ("Missing identifier"); RECOVER;
1115 $$ = NULL_TREE;
1117 | final type error
1119 yyerror ("Missing identifier"); RECOVER;
1120 $$ = NULL_TREE;
1124 final:
1125 modifiers
1127 check_modifiers ("Illegal modifier %qs. Only %<final%> was expected here",
1128 $1, ACC_FINAL);
1129 if ($1 != ACC_FINAL)
1130 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1134 throws:
1135 { $$ = NULL_TREE; }
1136 | THROWS_TK class_type_list
1137 { $$ = $2; }
1138 | THROWS_TK error
1139 {yyerror ("Missing class type term"); RECOVER;}
1142 class_type_list:
1143 class_type
1144 { $$ = build_tree_list ($1, $1); }
1145 | class_type_list C_TK class_type
1146 { $$ = tree_cons ($3, $3, $1); }
1147 | class_type_list C_TK error
1148 {yyerror ("Missing class type term"); RECOVER;}
1151 method_body:
1152 block
1153 | SC_TK { $$ = NULL_TREE; }
1156 /* 19.8.4 Productions from 8.5: Static Initializers */
1157 static_initializer:
1158 static block
1160 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1161 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1162 current_static_block = NULL_TREE;
1166 static: /* Test lval.sub_token here */
1167 modifiers
1169 check_modifiers ("Illegal modifier %qs for static initializer", $1, ACC_STATIC);
1170 /* Can't have a static initializer in an innerclass */
1171 if ($1 | ACC_STATIC &&
1172 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1173 parse_error_context
1174 (MODIFIER_WFL (STATIC_TK),
1175 "Can't define static initializer in class %qs. Static initializer can only be defined in top-level classes",
1176 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1177 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1181 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1182 constructor_declaration:
1183 constructor_header
1185 current_function_decl = $1;
1186 source_start_java_method (current_function_decl);
1188 constructor_body
1189 { finish_method_declaration ($3); }
1192 constructor_header:
1193 constructor_declarator throws
1194 { $$ = method_header (0, NULL_TREE, $1, $2); }
1195 | modifiers constructor_declarator throws
1196 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1199 constructor_declarator:
1200 simple_name OP_TK CP_TK
1202 ctxp->formal_parameter_number = 0;
1203 $$ = method_declarator ($1, NULL_TREE);
1205 | simple_name OP_TK formal_parameter_list CP_TK
1206 { $$ = method_declarator ($1, $3); }
1209 constructor_body:
1210 /* Unlike regular method, we always need a complete (empty)
1211 body so we can safely perform all the required code
1212 addition (super invocation and field initialization) */
1213 block_begin constructor_block_end
1215 BLOCK_EXPR_BODY ($2) = build_java_empty_stmt ();
1216 $$ = $2;
1218 | block_begin explicit_constructor_invocation constructor_block_end
1219 { $$ = $3; }
1220 | block_begin block_statements constructor_block_end
1221 { $$ = $3; }
1222 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1223 { $$ = $4; }
1226 constructor_block_end:
1227 block_end
1230 /* Error recovery for that rule moved down expression_statement: rule. */
1231 explicit_constructor_invocation:
1232 this_or_super OP_TK CP_TK SC_TK
1234 $$ = build_method_invocation ($1, NULL_TREE);
1235 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1236 $$ = java_method_add_stmt (current_function_decl, $$);
1238 | this_or_super OP_TK argument_list CP_TK SC_TK
1240 $$ = build_method_invocation ($1, $3);
1241 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1242 $$ = java_method_add_stmt (current_function_decl, $$);
1244 /* Added, JDK1.1 inner classes. Modified because the rule
1245 'primary' couldn't work. */
1246 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1247 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1248 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1249 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1252 this_or_super: /* Added, simplifies error diagnostics */
1253 THIS_TK
1255 tree wfl = build_wfl_node (this_identifier_node);
1256 SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1257 $$ = wfl;
1259 | SUPER_TK
1261 tree wfl = build_wfl_node (super_identifier_node);
1262 SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1263 $$ = wfl;
1267 /* 19.9 Productions from 9: Interfaces */
1268 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1269 interface_declaration:
1270 INTERFACE_TK identifier
1271 { create_interface (0, $2, NULL_TREE); }
1272 interface_body
1273 { ; }
1274 | modifiers INTERFACE_TK identifier
1275 { create_interface ($1, $3, NULL_TREE); }
1276 interface_body
1277 { ; }
1278 | INTERFACE_TK identifier extends_interfaces
1279 { create_interface (0, $2, $3); }
1280 interface_body
1281 { ; }
1282 | modifiers INTERFACE_TK identifier extends_interfaces
1283 { create_interface ($1, $3, $4); }
1284 interface_body
1285 { ; }
1286 | INTERFACE_TK identifier error
1287 { yyerror ("'{' expected"); RECOVER; }
1288 | modifiers INTERFACE_TK identifier error
1289 { yyerror ("'{' expected"); RECOVER; }
1292 extends_interfaces:
1293 EXTENDS_TK interface_type
1295 ctxp->interface_number = 1;
1296 $$ = build_tree_list ($2, NULL_TREE);
1298 | extends_interfaces C_TK interface_type
1300 ctxp->interface_number++;
1301 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1303 | EXTENDS_TK error
1304 {yyerror ("Invalid interface type"); RECOVER;}
1305 | extends_interfaces C_TK error
1306 {yyerror ("Missing term"); RECOVER;}
1309 interface_body:
1310 OCB_TK CCB_TK
1311 { $$ = NULL_TREE; }
1312 | OCB_TK interface_member_declarations CCB_TK
1313 { $$ = NULL_TREE; }
1316 interface_member_declarations:
1317 interface_member_declaration
1318 | interface_member_declarations interface_member_declaration
1321 interface_member_declaration:
1322 constant_declaration
1323 | abstract_method_declaration
1324 | class_declaration /* Added, JDK1.1 inner classes */
1325 { end_class_declaration (1); }
1326 | interface_declaration /* Added, JDK1.1 inner interfaces */
1327 { end_class_declaration (1); }
1330 constant_declaration:
1331 field_declaration
1334 abstract_method_declaration:
1335 method_header SC_TK
1337 check_abstract_method_header ($1);
1338 current_function_decl = NULL_TREE; /* FIXME ? */
1340 | method_header error
1341 {yyerror ("';' expected"); RECOVER;}
1344 /* 19.10 Productions from 10: Arrays */
1345 array_initializer:
1346 OCB_TK CCB_TK
1347 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1348 | OCB_TK C_TK CCB_TK
1349 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1350 | OCB_TK variable_initializers CCB_TK
1351 { $$ = build_new_array_init ($1.location, $2); }
1352 | OCB_TK variable_initializers C_TK CCB_TK
1353 { $$ = build_new_array_init ($1.location, $2); }
1356 variable_initializers:
1357 variable_initializer
1359 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1360 $1, NULL_TREE);
1362 | variable_initializers C_TK variable_initializer
1364 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1366 | variable_initializers C_TK error
1367 {yyerror ("Missing term"); RECOVER;}
1370 /* 19.11 Production from 14: Blocks and Statements */
1371 block:
1372 block_begin block_end
1373 { $$ = $2; }
1374 | block_begin block_statements block_end
1375 { $$ = $3; }
1378 block_begin:
1379 OCB_TK
1380 { enter_block (); }
1383 block_end:
1384 CCB_TK
1386 maybe_absorb_scoping_blocks ();
1387 $$ = exit_block ();
1388 if (!BLOCK_SUBBLOCKS ($$))
1389 BLOCK_SUBBLOCKS ($$) = build_java_empty_stmt ();
1393 block_statements:
1394 block_statement
1395 | block_statements block_statement
1398 block_statement:
1399 local_variable_declaration_statement
1400 | statement
1401 { java_method_add_stmt (current_function_decl, $1); }
1402 | class_declaration /* Added, JDK1.1 local classes */
1404 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1405 end_class_declaration (1);
1409 local_variable_declaration_statement:
1410 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1413 local_variable_declaration:
1414 type variable_declarators
1415 { declare_local_variables (0, $1, $2); }
1416 | final type variable_declarators /* Added, JDK1.1 final locals */
1417 { declare_local_variables ($1, $2, $3); }
1420 statement:
1421 statement_without_trailing_substatement
1422 | labeled_statement
1423 | if_then_statement
1424 | if_then_else_statement
1425 | while_statement
1426 | for_statement
1427 { $$ = exit_block (); }
1430 statement_nsi:
1431 statement_without_trailing_substatement
1432 | labeled_statement_nsi
1433 | if_then_else_statement_nsi
1434 | while_statement_nsi
1435 | for_statement_nsi
1436 { $$ = exit_block (); }
1439 statement_without_trailing_substatement:
1440 block
1441 | empty_statement
1442 | expression_statement
1443 | switch_statement
1444 | do_statement
1445 | break_statement
1446 | continue_statement
1447 | return_statement
1448 | synchronized_statement
1449 | throw_statement
1450 | try_statement
1451 | assert_statement
1454 empty_statement:
1455 SC_TK
1457 if (flag_extraneous_semicolon
1458 && ! current_static_block
1459 && (! current_function_decl ||
1460 /* Verify we're not in a inner class declaration */
1461 (GET_CPC () != TYPE_NAME
1462 (DECL_CONTEXT (current_function_decl)))))
1465 #ifdef USE_MAPPED_LOCATION
1466 SET_EXPR_LOCATION (wfl_operator, input_location);
1467 #else
1468 EXPR_WFL_SET_LINECOL (wfl_operator, input_line, -1);
1469 #endif
1470 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1472 $$ = build_java_empty_stmt ();
1476 label_decl:
1477 identifier REL_CL_TK
1479 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1480 EXPR_WFL_NODE ($1));
1481 pushlevel (2);
1482 push_labeled_block ($$);
1483 PUSH_LABELED_BLOCK ($$);
1487 labeled_statement:
1488 label_decl statement
1489 { $$ = finish_labeled_statement ($1, $2); }
1490 | identifier error
1491 {yyerror ("':' expected"); RECOVER;}
1494 labeled_statement_nsi:
1495 label_decl statement_nsi
1496 { $$ = finish_labeled_statement ($1, $2); }
1499 /* We concentrate here a bunch of error handling rules that we couldn't write
1500 earlier, because expression_statement catches a missing ';'. */
1501 expression_statement:
1502 statement_expression SC_TK
1504 /* We have a statement. Generate a WFL around it so
1505 we can debug it */
1506 #ifdef USE_MAPPED_LOCATION
1507 $$ = expr_add_location ($1, input_location, 1);
1508 #else
1509 $$ = build_expr_wfl ($1, input_filename, input_line, 0);
1510 JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1511 #endif
1512 /* We know we have a statement, so set the debug
1513 info to be eventually generate here. */
1515 | error SC_TK
1517 YYNOT_TWICE yyerror ("Invalid expression statement");
1518 DRECOVER (expr_stmt);
1520 | error OCB_TK
1522 YYNOT_TWICE yyerror ("Invalid expression statement");
1523 DRECOVER (expr_stmt);
1525 | error CCB_TK
1527 YYNOT_TWICE yyerror ("Invalid expression statement");
1528 DRECOVER (expr_stmt);
1530 | this_or_super OP_TK error
1531 {yyerror ("')' expected"); RECOVER;}
1532 | this_or_super OP_TK CP_TK error
1534 parse_ctor_invocation_error ();
1535 RECOVER;
1537 | this_or_super OP_TK argument_list error
1538 {yyerror ("')' expected"); RECOVER;}
1539 | this_or_super OP_TK argument_list CP_TK error
1541 parse_ctor_invocation_error ();
1542 RECOVER;
1544 | name DOT_TK SUPER_TK error
1545 {yyerror ("'(' expected"); RECOVER;}
1546 | name DOT_TK SUPER_TK OP_TK error
1547 {yyerror ("')' expected"); RECOVER;}
1548 | name DOT_TK SUPER_TK OP_TK argument_list error
1549 {yyerror ("')' expected"); RECOVER;}
1550 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1551 {yyerror ("';' expected"); RECOVER;}
1552 | name DOT_TK SUPER_TK OP_TK CP_TK error
1553 {yyerror ("';' expected"); RECOVER;}
1556 statement_expression:
1557 assignment
1558 | pre_increment_expression
1559 | pre_decrement_expression
1560 | post_increment_expression
1561 | post_decrement_expression
1562 | method_invocation
1563 | class_instance_creation_expression
1566 if_then_statement:
1567 IF_TK OP_TK expression CP_TK statement
1569 $$ = build_if_else_statement ($2.location, $3,
1570 $5, NULL_TREE);
1572 | IF_TK error
1573 {yyerror ("'(' expected"); RECOVER;}
1574 | IF_TK OP_TK error
1575 {yyerror ("Missing term"); RECOVER;}
1576 | IF_TK OP_TK expression error
1577 {yyerror ("')' expected"); RECOVER;}
1580 if_then_else_statement:
1581 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1582 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1585 if_then_else_statement_nsi:
1586 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1587 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1590 switch_statement:
1591 switch_expression
1593 enter_block ();
1595 switch_block
1597 /* Make into "proper list" of COMPOUND_EXPRs.
1598 I.e. make the last statement also have its own
1599 COMPOUND_EXPR. */
1600 maybe_absorb_scoping_blocks ();
1601 TREE_OPERAND ($1, 1) = exit_block ();
1602 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1606 switch_expression:
1607 SWITCH_TK OP_TK expression CP_TK
1609 $$ = build3 (SWITCH_EXPR, NULL_TREE, $3,
1610 NULL_TREE, NULL_TREE);
1611 SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
1613 | SWITCH_TK error
1614 {yyerror ("'(' expected"); RECOVER;}
1615 | SWITCH_TK OP_TK error
1616 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1617 | SWITCH_TK OP_TK expression CP_TK error
1618 {yyerror ("'{' expected"); RECOVER;}
1621 /* Default assignment is there to avoid type node on switch_block
1622 node. */
1624 switch_block:
1625 OCB_TK CCB_TK
1626 { $$ = NULL_TREE; }
1627 | OCB_TK switch_labels CCB_TK
1628 { $$ = NULL_TREE; }
1629 | OCB_TK switch_block_statement_groups CCB_TK
1630 { $$ = NULL_TREE; }
1631 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1632 { $$ = NULL_TREE; }
1635 switch_block_statement_groups:
1636 switch_block_statement_group
1637 | switch_block_statement_groups switch_block_statement_group
1640 switch_block_statement_group:
1641 switch_labels block_statements
1644 switch_labels:
1645 switch_label
1646 | switch_labels switch_label
1649 switch_label:
1650 CASE_TK constant_expression REL_CL_TK
1652 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1653 SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1654 java_method_add_stmt (current_function_decl, lab);
1656 | DEFAULT_TK REL_CL_TK
1658 tree lab = make_node (DEFAULT_EXPR);
1659 SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1660 java_method_add_stmt (current_function_decl, lab);
1662 | CASE_TK error
1663 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1664 | CASE_TK constant_expression error
1665 {yyerror ("':' expected"); RECOVER;}
1666 | DEFAULT_TK error
1667 {yyerror ("':' expected"); RECOVER;}
1670 while_expression:
1671 WHILE_TK OP_TK expression CP_TK
1673 tree body = build_loop_body ($2.location, $3, 0);
1674 $$ = build_new_loop (body);
1678 while_statement:
1679 while_expression statement
1680 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1681 | WHILE_TK error
1682 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1683 | WHILE_TK OP_TK error
1684 {yyerror ("Missing term and ')' expected"); RECOVER;}
1685 | WHILE_TK OP_TK expression error
1686 {yyerror ("')' expected"); RECOVER;}
1689 while_statement_nsi:
1690 while_expression statement_nsi
1691 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1694 do_statement_begin:
1695 DO_TK
1697 tree body = build_loop_body (0, NULL_TREE, 1);
1698 $$ = build_new_loop (body);
1700 /* Need error handing here. FIXME */
1703 do_statement:
1704 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1705 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1708 for_statement:
1709 for_begin SC_TK expression SC_TK for_update CP_TK statement
1711 if (CONSTANT_CLASS_P ($3))
1712 $3 = build_wfl_node ($3);
1713 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1715 | for_begin SC_TK SC_TK for_update CP_TK statement
1717 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1718 /* We have not condition, so we get rid of the EXIT_EXPR */
1719 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1720 build_java_empty_stmt ();
1722 | for_begin SC_TK error
1723 {yyerror ("Invalid control expression"); RECOVER;}
1724 | for_begin SC_TK expression SC_TK error
1725 {yyerror ("Invalid update expression"); RECOVER;}
1726 | for_begin SC_TK SC_TK error
1727 {yyerror ("Invalid update expression"); RECOVER;}
1730 for_statement_nsi:
1731 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1732 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1733 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1735 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1736 /* We have not condition, so we get rid of the EXIT_EXPR */
1737 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1738 build_java_empty_stmt ();
1742 for_header:
1743 FOR_TK OP_TK
1745 /* This scope defined for local variable that may be
1746 defined within the scope of the for loop */
1747 enter_block ();
1749 | FOR_TK error
1750 {yyerror ("'(' expected"); DRECOVER(for_1);}
1751 | FOR_TK OP_TK error
1752 {yyerror ("Invalid init statement"); RECOVER;}
1755 for_begin:
1756 for_header for_init
1758 /* We now declare the loop body. The loop is
1759 declared as a for loop. */
1760 tree body = build_loop_body (0, NULL_TREE, 0);
1761 $$ = build_new_loop (body);
1762 FOR_LOOP_P ($$) = 1;
1763 /* The loop is added to the current block the for
1764 statement is defined within */
1765 java_method_add_stmt (current_function_decl, $$);
1768 for_init: /* Can be empty */
1769 { $$ = build_java_empty_stmt (); }
1770 | statement_expression_list
1772 /* Init statement recorded within the previously
1773 defined block scope */
1774 $$ = java_method_add_stmt (current_function_decl, $1);
1776 | local_variable_declaration
1778 /* Local variable are recorded within the previously
1779 defined block scope */
1780 $$ = NULL_TREE;
1782 | statement_expression_list error
1783 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1786 for_update: /* Can be empty */
1787 {$$ = build_java_empty_stmt ();}
1788 | statement_expression_list
1789 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1792 statement_expression_list:
1793 statement_expression
1794 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1795 | statement_expression_list C_TK statement_expression
1796 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1797 | statement_expression_list C_TK error
1798 {yyerror ("Missing term"); RECOVER;}
1801 break_statement:
1802 BREAK_TK SC_TK
1803 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1804 | BREAK_TK identifier SC_TK
1805 { $$ = build_bc_statement ($1.location, 1, $2); }
1806 | BREAK_TK error
1807 {yyerror ("Missing term"); RECOVER;}
1808 | BREAK_TK identifier error
1809 {yyerror ("';' expected"); RECOVER;}
1812 continue_statement:
1813 CONTINUE_TK SC_TK
1814 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1815 | CONTINUE_TK identifier SC_TK
1816 { $$ = build_bc_statement ($1.location, 0, $2); }
1817 | CONTINUE_TK error
1818 {yyerror ("Missing term"); RECOVER;}
1819 | CONTINUE_TK identifier error
1820 {yyerror ("';' expected"); RECOVER;}
1823 return_statement:
1824 RETURN_TK SC_TK
1825 { $$ = build_return ($1.location, NULL_TREE); }
1826 | RETURN_TK expression SC_TK
1827 { $$ = build_return ($1.location, $2); }
1828 | RETURN_TK error
1829 {yyerror ("Missing term"); RECOVER;}
1830 | RETURN_TK expression error
1831 {yyerror ("';' expected"); RECOVER;}
1834 throw_statement:
1835 THROW_TK expression SC_TK
1837 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1838 SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1840 | THROW_TK error
1841 {yyerror ("Missing term"); RECOVER;}
1842 | THROW_TK expression error
1843 {yyerror ("';' expected"); RECOVER;}
1846 assert_statement:
1847 ASSERT_TK expression REL_CL_TK expression SC_TK
1849 $$ = build_assertion ($1.location, $2, $4);
1851 | ASSERT_TK expression SC_TK
1853 $$ = build_assertion ($1.location, $2, NULL_TREE);
1855 | ASSERT_TK error
1856 {yyerror ("Missing term"); RECOVER;}
1857 | ASSERT_TK expression error
1858 {yyerror ("';' expected"); RECOVER;}
1861 synchronized_statement:
1862 synchronized OP_TK expression CP_TK block
1864 $$ = build2 (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1865 EXPR_WFL_LINECOL ($$) =
1866 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1868 | synchronized OP_TK expression CP_TK error
1869 {yyerror ("'{' expected"); RECOVER;}
1870 | synchronized error
1871 {yyerror ("'(' expected"); RECOVER;}
1872 | synchronized OP_TK error CP_TK
1873 {yyerror ("Missing term"); RECOVER;}
1874 | synchronized OP_TK error
1875 {yyerror ("Missing term"); RECOVER;}
1878 synchronized:
1879 modifiers
1881 check_modifiers (
1882 "Illegal modifier %qs. Only %<synchronized%> was expected here",
1883 $1, ACC_SYNCHRONIZED);
1884 if ($1 != ACC_SYNCHRONIZED)
1885 MODIFIER_WFL (SYNCHRONIZED_TK) =
1886 build_wfl_node (NULL_TREE);
1890 try_statement:
1891 TRY_TK block catches
1892 { $$ = build_try_statement ($1.location, $2, $3); }
1893 | TRY_TK block finally
1894 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1895 | TRY_TK block catches finally
1896 { $$ = build_try_finally_statement
1897 ($1.location, build_try_statement ($1.location,
1898 $2, $3), $4);
1900 | TRY_TK error
1901 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1904 catches:
1905 catch_clause
1906 | catches catch_clause
1908 TREE_CHAIN ($2) = $1;
1909 $$ = $2;
1913 catch_clause:
1914 catch_clause_parameter block
1916 java_method_add_stmt (current_function_decl, $2);
1917 exit_block ();
1918 $$ = $1;
1922 catch_clause_parameter:
1923 CATCH_TK OP_TK formal_parameter CP_TK
1925 /* We add a block to define a scope for
1926 formal_parameter (CCBP). The formal parameter is
1927 declared initialized by the appropriate function
1928 call */
1929 tree ccpb;
1930 tree init;
1931 if ($3)
1933 ccpb = enter_block ();
1934 init = build_assignment
1935 (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1936 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1937 declare_local_variables (0, TREE_VALUE ($3),
1938 build_tree_list
1939 (TREE_PURPOSE ($3), init));
1940 $$ = build1 (JAVA_CATCH_EXPR, NULL_TREE, ccpb);
1941 SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1943 else
1945 $$ = error_mark_node;
1948 | CATCH_TK error
1949 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1950 | CATCH_TK OP_TK error
1952 yyerror ("Missing term or ')' expected");
1953 RECOVER; $$ = NULL_TREE;
1955 | CATCH_TK OP_TK error CP_TK /* That's for () */
1956 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1959 finally:
1960 FINALLY_TK block
1961 { $$ = $2; }
1962 | FINALLY_TK error
1963 {yyerror ("'{' expected"); RECOVER; }
1966 /* 19.12 Production from 15: Expressions */
1967 primary:
1968 primary_no_new_array
1969 | array_creation_uninitialized
1970 | array_creation_initialized
1973 primary_no_new_array:
1974 literal
1975 | THIS_TK
1976 { $$ = build_this ($1.location); }
1977 | OP_TK expression CP_TK
1978 {$$ = $2;}
1979 | class_instance_creation_expression
1980 | field_access
1981 | method_invocation
1982 | array_access
1983 | type_literals
1984 /* Added, JDK1.1 inner classes. Documentation is wrong
1985 referring to a 'ClassName' (class_name) rule that doesn't
1986 exist. Used name: instead. */
1987 | name DOT_TK THIS_TK
1989 tree wfl = build_wfl_node (this_identifier_node);
1990 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1992 | OP_TK expression error
1993 {yyerror ("')' expected"); RECOVER;}
1994 | name DOT_TK error
1995 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1996 | primitive_type DOT_TK error
1997 {yyerror ("'class' expected" ); RECOVER;}
1998 | VOID_TK DOT_TK error
1999 {yyerror ("'class' expected" ); RECOVER;}
2002 type_literals:
2003 name DOT_TK CLASS_TK
2004 { $$ = build_incomplete_class_ref ($2.location, $1); }
2005 | array_type DOT_TK CLASS_TK
2006 { $$ = build_incomplete_class_ref ($2.location, $1); }
2007 | primitive_type DOT_TK CLASS_TK
2008 { $$ = build_incomplete_class_ref ($2.location, $1); }
2009 | VOID_TK DOT_TK CLASS_TK
2011 $$ = build_incomplete_class_ref ($2.location,
2012 void_type_node);
2016 class_instance_creation_expression:
2017 NEW_TK class_type OP_TK argument_list CP_TK
2018 { $$ = build_new_invocation ($2, $4); }
2019 | NEW_TK class_type OP_TK CP_TK
2020 { $$ = build_new_invocation ($2, NULL_TREE); }
2021 | anonymous_class_creation
2022 /* Added, JDK1.1 inner classes, modified to use name or
2023 primary instead of primary solely which couldn't work in
2024 all situations. */
2025 | something_dot_new identifier OP_TK CP_TK
2027 tree ctor = build_new_invocation ($2, NULL_TREE);
2028 $$ = make_qualified_primary ($1, ctor,
2029 EXPR_WFL_LINECOL ($1));
2031 | something_dot_new identifier OP_TK CP_TK class_body
2032 | something_dot_new identifier OP_TK argument_list CP_TK
2034 tree ctor = build_new_invocation ($2, $4);
2035 $$ = make_qualified_primary ($1, ctor,
2036 EXPR_WFL_LINECOL ($1));
2038 | something_dot_new identifier OP_TK argument_list CP_TK class_body
2039 | NEW_TK error SC_TK
2040 {$$ = NULL_TREE; yyerror ("'(' expected"); DRECOVER(new_1);}
2041 | NEW_TK class_type error
2042 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2043 | NEW_TK class_type OP_TK error
2044 {$$ = NULL_TREE; yyerror ("')' or term expected"); RECOVER;}
2045 | NEW_TK class_type OP_TK argument_list error
2046 {$$ = NULL_TREE; yyerror ("')' expected"); RECOVER;}
2047 | something_dot_new error
2049 $$ = NULL_TREE;
2050 YYERROR_NOW;
2051 yyerror ("Identifier expected");
2052 RECOVER;
2054 | something_dot_new identifier error
2055 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2058 /* Created after JDK1.1 rules originally added to
2059 class_instance_creation_expression, but modified to use
2060 'class_type' instead of 'TypeName' (type_name) which is mentioned
2061 in the documentation but doesn't exist. */
2063 anonymous_class_creation:
2064 NEW_TK class_type OP_TK argument_list CP_TK
2065 { create_anonymous_class ($2); }
2066 class_body
2068 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2069 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2071 end_class_declaration (1);
2073 /* Now we can craft the new expression */
2074 $$ = build_new_invocation (id, $4);
2076 /* Note that we can't possibly be here if
2077 `class_type' is an interface (in which case the
2078 anonymous class extends Object and implements
2079 `class_type', hence its constructor can't have
2080 arguments.) */
2082 /* Otherwise, the innerclass must feature a
2083 constructor matching `argument_list'. Anonymous
2084 classes are a bit special: it's impossible to
2085 define constructor for them, hence constructors
2086 must be generated following the hints provided by
2087 the `new' expression. Whether a super constructor
2088 of that nature exists or not is to be verified
2089 later on in verify_constructor_super.
2091 It's during the expansion of a `new' statement
2092 referring to an anonymous class that a ctor will
2093 be generated for the anonymous class, with the
2094 right arguments. */
2097 | NEW_TK class_type OP_TK CP_TK
2098 { create_anonymous_class ($2); }
2099 class_body
2101 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2102 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2104 end_class_declaration (1);
2106 /* Now we can craft the new expression. The
2107 statement doesn't need to be remember so that a
2108 constructor can be generated, since its signature
2109 is already known. */
2110 $$ = build_new_invocation (id, NULL_TREE);
2114 something_dot_new: /* Added, not part of the specs. */
2115 name DOT_TK NEW_TK
2116 { $$ = $1; }
2117 | primary DOT_TK NEW_TK
2118 { $$ = $1; }
2121 argument_list:
2122 expression
2124 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2125 ctxp->formal_parameter_number = 1;
2127 | argument_list C_TK expression
2129 ctxp->formal_parameter_number += 1;
2130 $$ = tree_cons (NULL_TREE, $3, $1);
2132 | argument_list C_TK error
2133 {yyerror ("Missing term"); RECOVER;}
2136 array_creation_uninitialized:
2137 NEW_TK primitive_type dim_exprs
2138 { $$ = build_newarray_node ($2, $3, 0); }
2139 | NEW_TK class_or_interface_type dim_exprs
2140 { $$ = build_newarray_node ($2, $3, 0); }
2141 | NEW_TK primitive_type dim_exprs dims
2142 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2143 | NEW_TK class_or_interface_type dim_exprs dims
2144 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2145 | NEW_TK error CSB_TK
2146 {yyerror ("'[' expected"); DRECOVER ("]");}
2147 | NEW_TK error OSB_TK
2148 {yyerror ("']' expected"); RECOVER;}
2151 array_creation_initialized:
2152 /* Added, JDK1.1 anonymous array. Initial documentation rule
2153 modified */
2154 NEW_TK class_or_interface_type dims array_initializer
2156 char *sig;
2157 int osb = pop_current_osb (ctxp);
2158 while (osb--)
2159 obstack_grow (&temporary_obstack, "[]", 2);
2160 obstack_1grow (&temporary_obstack, '\0');
2161 sig = obstack_finish (&temporary_obstack);
2162 $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2163 $2, get_identifier (sig), $4);
2165 | NEW_TK primitive_type dims array_initializer
2167 int osb = pop_current_osb (ctxp);
2168 tree type = $2;
2169 while (osb--)
2170 type = build_java_array_type (type, -1);
2171 $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2172 build_pointer_type (type), NULL_TREE, $4);
2174 | NEW_TK error CSB_TK
2175 {yyerror ("'[' expected"); DRECOVER ("]");}
2176 | NEW_TK error OSB_TK
2177 {yyerror ("']' expected"); RECOVER;}
2180 dim_exprs:
2181 dim_expr
2182 { $$ = build_tree_list (NULL_TREE, $1); }
2183 | dim_exprs dim_expr
2184 { $$ = tree_cons (NULL_TREE, $2, $$); }
2187 dim_expr:
2188 OSB_TK expression CSB_TK
2190 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2192 $2 = build_wfl_node ($2);
2193 TREE_TYPE ($2) = NULL_TREE;
2195 EXPR_WFL_LINECOL ($2) = $1.location;
2196 $$ = $2;
2198 | OSB_TK expression error
2199 {yyerror ("']' expected"); RECOVER;}
2200 | OSB_TK error
2202 yyerror ("Missing term");
2203 yyerror ("']' expected");
2204 RECOVER;
2208 dims:
2209 OSB_TK CSB_TK
2211 int allocate = 0;
2212 /* If not initialized, allocate memory for the osb
2213 numbers stack */
2214 if (!ctxp->osb_limit)
2216 allocate = ctxp->osb_limit = 32;
2217 ctxp->osb_depth = -1;
2219 /* If capacity overflown, reallocate a bigger chunk */
2220 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2221 allocate = ctxp->osb_limit << 1;
2223 if (allocate)
2225 allocate *= sizeof (int);
2226 if (ctxp->osb_number)
2227 ctxp->osb_number = xrealloc (ctxp->osb_number,
2228 allocate);
2229 else
2230 ctxp->osb_number = xmalloc (allocate);
2232 ctxp->osb_depth++;
2233 CURRENT_OSB (ctxp) = 1;
2235 | dims OSB_TK CSB_TK
2236 { CURRENT_OSB (ctxp)++; }
2237 | dims OSB_TK error
2238 { yyerror ("']' expected"); RECOVER;}
2241 field_access:
2242 primary DOT_TK identifier
2243 { $$ = make_qualified_primary ($1, $3, $2.location); }
2244 /* FIXME - REWRITE TO:
2245 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2246 | SUPER_TK DOT_TK identifier
2248 tree super_wfl = build_wfl_node (super_identifier_node);
2249 SET_EXPR_LOCATION_FROM_TOKEN (super_wfl, $1);
2250 $$ = make_qualified_name (super_wfl, $3, $2.location);
2252 | SUPER_TK error
2253 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2256 method_invocation:
2257 name OP_TK CP_TK
2258 { $$ = build_method_invocation ($1, NULL_TREE); }
2259 | name OP_TK argument_list CP_TK
2260 { $$ = build_method_invocation ($1, $3); }
2261 | primary DOT_TK identifier OP_TK CP_TK
2263 if (TREE_CODE ($1) == THIS_EXPR)
2264 $$ = build_this_super_qualified_invocation
2265 (1, $3, NULL_TREE, 0, $2.location);
2266 else
2268 tree invok = build_method_invocation ($3, NULL_TREE);
2269 $$ = make_qualified_primary ($1, invok, $2.location);
2272 | primary DOT_TK identifier OP_TK argument_list CP_TK
2274 if (TREE_CODE ($1) == THIS_EXPR)
2275 $$ = build_this_super_qualified_invocation
2276 (1, $3, $5, 0, $2.location);
2277 else
2279 tree invok = build_method_invocation ($3, $5);
2280 $$ = make_qualified_primary ($1, invok, $2.location);
2283 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2285 $$ = build_this_super_qualified_invocation
2286 (0, $3, NULL_TREE, $1.location, $2.location);
2288 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2290 $$ = build_this_super_qualified_invocation
2291 (0, $3, $5, $1.location, $2.location);
2293 /* Screws up thing. I let it here until I'm convinced it can
2294 be removed. FIXME
2295 | primary DOT_TK error
2296 {yyerror ("'(' expected"); DRECOVER(bad);} */
2297 | SUPER_TK DOT_TK error CP_TK
2298 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2299 | SUPER_TK DOT_TK error DOT_TK
2300 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2303 array_access:
2304 name OSB_TK expression CSB_TK
2305 { $$ = build_array_ref ($2.location, $1, $3); }
2306 | primary_no_new_array OSB_TK expression CSB_TK
2307 { $$ = build_array_ref ($2.location, $1, $3); }
2308 | array_creation_initialized OSB_TK expression CSB_TK
2309 { $$ = build_array_ref ($2.location, $1, $3); }
2310 | name OSB_TK error
2312 yyerror ("Missing term and ']' expected");
2313 DRECOVER(array_access);
2315 | name OSB_TK expression error
2317 yyerror ("']' expected");
2318 DRECOVER(array_access);
2320 | primary_no_new_array OSB_TK error
2322 yyerror ("Missing term and ']' expected");
2323 DRECOVER(array_access);
2325 | primary_no_new_array OSB_TK expression error
2327 yyerror ("']' expected");
2328 DRECOVER(array_access);
2330 | array_creation_initialized OSB_TK error
2332 yyerror ("Missing term and ']' expected");
2333 DRECOVER(array_access);
2335 | array_creation_initialized OSB_TK expression error
2337 yyerror ("']' expected");
2338 DRECOVER(array_access);
2342 postfix_expression:
2343 primary
2344 | name
2345 | post_increment_expression
2346 | post_decrement_expression
2349 post_increment_expression:
2350 postfix_expression INCR_TK
2351 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2354 post_decrement_expression:
2355 postfix_expression DECR_TK
2356 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2359 trap_overflow_corner_case:
2360 pre_increment_expression
2361 | pre_decrement_expression
2362 | PLUS_TK unary_expression
2363 {$$ = build_unaryop ($1.token, $1.location, $2); }
2364 | unary_expression_not_plus_minus
2365 | PLUS_TK error
2366 {yyerror ("Missing term"); RECOVER}
2369 unary_expression:
2370 trap_overflow_corner_case
2372 if ($1)
2373 error_if_numeric_overflow ($1);
2374 $$ = $1;
2376 | MINUS_TK trap_overflow_corner_case
2377 {$$ = build_unaryop ($1.token, $1.location, $2); }
2378 | MINUS_TK error
2379 {yyerror ("Missing term"); RECOVER}
2382 pre_increment_expression:
2383 INCR_TK unary_expression
2384 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2385 | INCR_TK error
2386 {yyerror ("Missing term"); RECOVER}
2389 pre_decrement_expression:
2390 DECR_TK unary_expression
2391 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2392 | DECR_TK error
2393 {yyerror ("Missing term"); RECOVER}
2396 unary_expression_not_plus_minus:
2397 postfix_expression
2398 | NOT_TK unary_expression
2399 {$$ = build_unaryop ($1.token, $1.location, $2); }
2400 | NEG_TK unary_expression
2401 {$$ = build_unaryop ($1.token, $1.location, $2); }
2402 | cast_expression
2403 | NOT_TK error
2404 {yyerror ("Missing term"); RECOVER}
2405 | NEG_TK error
2406 {yyerror ("Missing term"); RECOVER}
2409 cast_expression: /* Error handling here is potentially weak */
2410 OP_TK primitive_type dims CP_TK unary_expression
2412 tree type = $2;
2413 int osb = pop_current_osb (ctxp);
2414 while (osb--)
2415 type = build_java_array_type (type, -1);
2416 $$ = build_cast ($1.location, type, $5);
2418 | OP_TK primitive_type CP_TK unary_expression
2419 { $$ = build_cast ($1.location, $2, $4); }
2420 | OP_TK expression CP_TK unary_expression_not_plus_minus
2421 { $$ = build_cast ($1.location, $2, $4); }
2422 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2424 const char *ptr;
2425 int osb = pop_current_osb (ctxp);
2426 obstack_grow (&temporary_obstack,
2427 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2428 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2429 while (osb--)
2430 obstack_grow (&temporary_obstack, "[]", 2);
2431 obstack_1grow (&temporary_obstack, '\0');
2432 ptr = obstack_finish (&temporary_obstack);
2433 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2434 $$ = build_cast ($1.location, $2, $5);
2436 | OP_TK primitive_type OSB_TK error
2437 {yyerror ("']' expected, invalid type expression");}
2438 | OP_TK error
2440 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2441 RECOVER;
2443 | OP_TK primitive_type dims CP_TK error
2444 {yyerror ("Missing term"); RECOVER;}
2445 | OP_TK primitive_type CP_TK error
2446 {yyerror ("Missing term"); RECOVER;}
2447 | OP_TK name dims CP_TK error
2448 {yyerror ("Missing term"); RECOVER;}
2451 multiplicative_expression:
2452 unary_expression
2453 | multiplicative_expression MULT_TK unary_expression
2455 $$ = build_binop (BINOP_LOOKUP ($2.token),
2456 $2.location, $1, $3);
2458 | multiplicative_expression DIV_TK unary_expression
2460 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2461 $1, $3);
2463 | multiplicative_expression REM_TK unary_expression
2465 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2466 $1, $3);
2468 | multiplicative_expression MULT_TK error
2469 {yyerror ("Missing term"); RECOVER;}
2470 | multiplicative_expression DIV_TK error
2471 {yyerror ("Missing term"); RECOVER;}
2472 | multiplicative_expression REM_TK error
2473 {yyerror ("Missing term"); RECOVER;}
2476 additive_expression:
2477 multiplicative_expression
2478 | additive_expression PLUS_TK multiplicative_expression
2480 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2481 $1, $3);
2483 | additive_expression MINUS_TK multiplicative_expression
2485 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2486 $1, $3);
2488 | additive_expression PLUS_TK error
2489 {yyerror ("Missing term"); RECOVER;}
2490 | additive_expression MINUS_TK error
2491 {yyerror ("Missing term"); RECOVER;}
2494 shift_expression:
2495 additive_expression
2496 | shift_expression LS_TK additive_expression
2498 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2499 $1, $3);
2501 | shift_expression SRS_TK additive_expression
2503 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2504 $1, $3);
2506 | shift_expression ZRS_TK additive_expression
2508 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2509 $1, $3);
2511 | shift_expression LS_TK error
2512 {yyerror ("Missing term"); RECOVER;}
2513 | shift_expression SRS_TK error
2514 {yyerror ("Missing term"); RECOVER;}
2515 | shift_expression ZRS_TK error
2516 {yyerror ("Missing term"); RECOVER;}
2519 relational_expression:
2520 shift_expression
2521 | relational_expression LT_TK shift_expression
2523 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2524 $1, $3);
2526 | relational_expression GT_TK shift_expression
2528 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2529 $1, $3);
2531 | relational_expression LTE_TK shift_expression
2533 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2534 $1, $3);
2536 | relational_expression GTE_TK shift_expression
2538 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2539 $1, $3);
2541 | relational_expression INSTANCEOF_TK reference_type
2542 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2543 | relational_expression LT_TK error
2544 {yyerror ("Missing term"); RECOVER;}
2545 | relational_expression GT_TK error
2546 {yyerror ("Missing term"); RECOVER;}
2547 | relational_expression LTE_TK error
2548 {yyerror ("Missing term"); RECOVER;}
2549 | relational_expression GTE_TK error
2550 {yyerror ("Missing term"); RECOVER;}
2551 | relational_expression INSTANCEOF_TK error
2552 {yyerror ("Invalid reference type"); RECOVER;}
2555 equality_expression:
2556 relational_expression
2557 | equality_expression EQ_TK relational_expression
2559 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2560 $1, $3);
2562 | equality_expression NEQ_TK relational_expression
2564 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2565 $1, $3);
2567 | equality_expression EQ_TK error
2568 {yyerror ("Missing term"); RECOVER;}
2569 | equality_expression NEQ_TK error
2570 {yyerror ("Missing term"); RECOVER;}
2573 and_expression:
2574 equality_expression
2575 | and_expression AND_TK equality_expression
2577 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2578 $1, $3);
2580 | and_expression AND_TK error
2581 {yyerror ("Missing term"); RECOVER;}
2584 exclusive_or_expression:
2585 and_expression
2586 | exclusive_or_expression XOR_TK and_expression
2588 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2589 $1, $3);
2591 | exclusive_or_expression XOR_TK error
2592 {yyerror ("Missing term"); RECOVER;}
2595 inclusive_or_expression:
2596 exclusive_or_expression
2597 | inclusive_or_expression OR_TK exclusive_or_expression
2599 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2600 $1, $3);
2602 | inclusive_or_expression OR_TK error
2603 {yyerror ("Missing term"); RECOVER;}
2606 conditional_and_expression:
2607 inclusive_or_expression
2608 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2610 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2611 $1, $3);
2613 | conditional_and_expression BOOL_AND_TK error
2614 {yyerror ("Missing term"); RECOVER;}
2617 conditional_or_expression:
2618 conditional_and_expression
2619 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2621 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2622 $1, $3);
2624 | conditional_or_expression BOOL_OR_TK error
2625 {yyerror ("Missing term"); RECOVER;}
2628 conditional_expression: /* Error handling here is weak */
2629 conditional_or_expression
2630 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2632 $$ = build3 (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2633 SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
2635 | conditional_or_expression REL_QM_TK REL_CL_TK error
2637 YYERROR_NOW;
2638 yyerror ("Missing term");
2639 DRECOVER (1);
2641 | conditional_or_expression REL_QM_TK error
2642 {yyerror ("Missing term"); DRECOVER (2);}
2643 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2644 {yyerror ("Missing term"); DRECOVER (3);}
2647 assignment_expression:
2648 conditional_expression
2649 | assignment
2652 assignment:
2653 left_hand_side assignment_operator assignment_expression
2654 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2655 | left_hand_side assignment_operator error
2657 YYNOT_TWICE yyerror ("Missing term");
2658 DRECOVER (assign);
2662 left_hand_side:
2663 name
2664 | field_access
2665 | array_access
2668 assignment_operator:
2669 ASSIGN_ANY_TK
2670 | ASSIGN_TK
2673 expression:
2674 assignment_expression
2677 constant_expression:
2678 expression
2683 /* Helper function to retrieve an OSB count. Should be used when the
2684 `dims:' rule is being used. */
2686 static int
2687 pop_current_osb (struct parser_ctxt *ctxp)
2689 int to_return;
2691 if (ctxp->osb_depth < 0)
2692 abort ();
2694 to_return = CURRENT_OSB (ctxp);
2695 ctxp->osb_depth--;
2697 return to_return;
2702 /* This section of the code deal with save/restoring parser contexts.
2703 Add mode documentation here. FIXME */
2705 /* Helper function. Create a new parser context. With
2706 COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2707 context is copied, otherwise, the new context is zeroed. The newly
2708 created context becomes the current one. */
2710 static void
2711 create_new_parser_context (int copy_from_previous)
2713 struct parser_ctxt *new;
2715 new = ggc_alloc (sizeof (struct parser_ctxt));
2716 if (copy_from_previous)
2718 memcpy (new, ctxp, sizeof (struct parser_ctxt));
2719 /* This flag, indicating the context saves global values,
2720 should only be set by java_parser_context_save_global. */
2721 new->saved_data_ctx = 0;
2723 else
2724 memset (new, 0, sizeof (struct parser_ctxt));
2726 new->next = ctxp;
2727 ctxp = new;
2730 /* Create a new parser context and make it the current one. */
2732 void
2733 java_push_parser_context (void)
2735 create_new_parser_context (0);
2738 void
2739 java_pop_parser_context (int generate)
2741 tree current;
2742 struct parser_ctxt *next;
2744 if (!ctxp)
2745 return;
2747 next = ctxp->next;
2748 if (next)
2750 input_location = ctxp->save_location;
2751 current_class = ctxp->class_type;
2754 /* If the old and new lexers differ, then free the old one. */
2755 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2756 java_destroy_lexer (ctxp->lexer);
2758 /* Set the single import class file flag to 0 for the current list
2759 of imported things */
2760 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2761 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2763 /* If we pushed a context to parse a class intended to be generated,
2764 we keep it so we can remember the class. What we could actually
2765 do is to just update a list of class names. */
2766 if (generate)
2768 ctxp->next = ctxp_for_generation;
2769 ctxp_for_generation = ctxp;
2772 /* And restore those of the previous context */
2773 if ((ctxp = next)) /* Assignment is really meant here */
2774 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2775 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2778 /* Create a parser context for the use of saving some global
2779 variables. */
2781 void
2782 java_parser_context_save_global (void)
2784 if (!ctxp)
2786 java_push_parser_context ();
2787 ctxp->saved_data_ctx = 1;
2790 /* If this context already stores data, create a new one suitable
2791 for data storage. */
2792 else if (ctxp->saved_data)
2794 create_new_parser_context (1);
2795 ctxp->saved_data_ctx = 1;
2798 ctxp->save_location = input_location;
2799 ctxp->class_type = current_class;
2800 ctxp->function_decl = current_function_decl;
2801 ctxp->saved_data = 1;
2804 /* Restore some global variables from the previous context. Make the
2805 previous context the current one. */
2807 void
2808 java_parser_context_restore_global (void)
2810 input_location = ctxp->save_location;
2811 current_class = ctxp->class_type;
2812 if (wfl_operator)
2813 #ifdef USE_MAPPED_LOCATION
2814 SET_EXPR_LOCATION (wfl_operator, ctxp->save_location);
2815 #else
2816 EXPR_WFL_FILENAME_NODE (wfl_operator) = get_identifier (input_filename);
2817 #endif
2818 current_function_decl = ctxp->function_decl;
2819 ctxp->saved_data = 0;
2820 if (ctxp->saved_data_ctx)
2821 java_pop_parser_context (0);
2824 /* Suspend vital data for the current class/function being parsed so
2825 that an other class can be parsed. Used to let local/anonymous
2826 classes be parsed. */
2828 static void
2829 java_parser_context_suspend (void)
2831 /* This makes debugging through java_debug_context easier */
2832 static const char *const name = "<inner buffer context>";
2834 /* Duplicate the previous context, use it to save the globals we're
2835 interested in */
2836 create_new_parser_context (1);
2837 ctxp->function_decl = current_function_decl;
2838 ctxp->class_type = current_class;
2840 /* Then create a new context which inherits all data from the
2841 previous one. This will be the new current context */
2842 create_new_parser_context (1);
2844 /* Help debugging */
2845 ctxp->next->filename = name;
2848 /* Resume vital data for the current class/function being parsed so
2849 that an other class can be parsed. Used to let local/anonymous
2850 classes be parsed. The trick is the data storing file position
2851 informations must be restored to their current value, so parsing
2852 can resume as if no context was ever saved. */
2854 static void
2855 java_parser_context_resume (void)
2857 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2858 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2859 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2861 /* We need to inherit the list of classes to complete/generate */
2862 restored->classd_list = old->classd_list;
2863 restored->class_list = old->class_list;
2865 /* Restore the current class and function from the saver */
2866 current_class = saver->class_type;
2867 current_function_decl = saver->function_decl;
2869 /* Retrieve the restored context */
2870 ctxp = restored;
2872 /* Re-installed the data for the parsing to carry on */
2873 memcpy (&ctxp->marker_begining, &old->marker_begining,
2874 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2877 /* Add a new anchor node to which all statement(s) initializing static
2878 and non static initialized upon declaration field(s) will be
2879 linked. */
2881 static void
2882 java_parser_context_push_initialized_field (void)
2884 tree node;
2886 node = build_tree_list (NULL_TREE, NULL_TREE);
2887 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2888 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2890 node = build_tree_list (NULL_TREE, NULL_TREE);
2891 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2892 CPC_INITIALIZER_LIST (ctxp) = node;
2894 node = build_tree_list (NULL_TREE, NULL_TREE);
2895 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2896 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2899 /* Pop the lists of initialized field. If this lists aren't empty,
2900 remember them so we can use it to create and populate the finit$
2901 or <clinit> functions. */
2903 static void
2904 java_parser_context_pop_initialized_field (void)
2906 tree stmts;
2907 tree class_type = TREE_TYPE (GET_CPC ());
2909 if (CPC_INITIALIZER_LIST (ctxp))
2911 stmts = CPC_INITIALIZER_STMT (ctxp);
2912 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2913 if (stmts && !java_error_count)
2914 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2917 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2919 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2920 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2921 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2922 /* Keep initialization in order to enforce 8.5 */
2923 if (stmts && !java_error_count)
2924 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2927 /* JDK 1.1 instance initializers */
2928 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2930 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2931 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2932 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2933 if (stmts && !java_error_count)
2934 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2938 static tree
2939 reorder_static_initialized (tree list)
2941 /* We have to keep things in order. The alias initializer have to
2942 come first, then the initialized regular field, in reverse to
2943 keep them in lexical order. */
2944 tree marker, previous = NULL_TREE;
2945 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2946 if (TREE_CODE (marker) == TREE_LIST
2947 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2948 break;
2950 /* No static initialized, the list is fine as is */
2951 if (!previous)
2952 list = TREE_CHAIN (marker);
2954 /* No marker? reverse the whole list */
2955 else if (!marker)
2956 list = nreverse (list);
2958 /* Otherwise, reverse what's after the marker and the new reordered
2959 sublist will replace the marker. */
2960 else
2962 TREE_CHAIN (previous) = NULL_TREE;
2963 list = nreverse (list);
2964 list = chainon (TREE_CHAIN (marker), list);
2966 return list;
2969 /* Helper functions to dump the parser context stack. */
2971 #define TAB_CONTEXT(C) \
2972 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2974 static void
2975 java_debug_context_do (int tab)
2977 struct parser_ctxt *copy = ctxp;
2978 while (copy)
2980 TAB_CONTEXT (tab);
2981 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2982 TAB_CONTEXT (tab);
2983 fprintf (stderr, "filename: %s\n", copy->filename);
2984 TAB_CONTEXT (tab);
2985 fprintf (stderr, "package: %s\n",
2986 (copy->package ?
2987 IDENTIFIER_POINTER (copy->package) : "<none>"));
2988 TAB_CONTEXT (tab);
2989 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2990 TAB_CONTEXT (tab);
2991 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2992 copy = copy->next;
2993 tab += 2;
2997 /* Dump the stacked up parser contexts. Intended to be called from a
2998 debugger. */
3000 void
3001 java_debug_context (void)
3003 java_debug_context_do (0);
3008 /* Flag for the error report routine to issue the error the first time
3009 it's called (overriding the default behavior which is to drop the
3010 first invocation and honor the second one, taking advantage of a
3011 richer context. */
3012 static int force_error = 0;
3014 /* Reporting an constructor invocation error. */
3015 static void
3016 parse_ctor_invocation_error (void)
3018 if (DECL_CONSTRUCTOR_P (current_function_decl))
3019 yyerror ("Constructor invocation must be first thing in a constructor");
3020 else
3021 yyerror ("Only constructors can invoke constructors");
3024 /* Reporting JDK1.1 features not implemented. */
3026 static tree
3027 parse_jdk1_1_error (const char *msg)
3029 sorry (": %qs JDK1.1(TM) feature", msg);
3030 java_error_count++;
3031 return build_java_empty_stmt ();
3034 static int do_warning = 0;
3036 void
3037 yyerror (const char *msgid)
3039 #ifdef USE_MAPPED_LOCATION
3040 static source_location elc;
3041 expanded_location xloc = expand_location (input_location);
3042 int current_line = xloc.line;
3043 #else
3044 static java_lc elc;
3045 int save_lineno;
3046 int current_line = input_line;
3047 #endif
3048 static int prev_lineno;
3049 static const char *prev_msg;
3051 char *remainder, *code_from_source;
3053 if (!force_error && prev_lineno == current_line)
3054 return;
3055 #ifndef USE_MAPPED_LOCATION
3056 current_line = ctxp->lexer->token_start.line;
3057 #endif
3059 /* Save current error location but report latter, when the context is
3060 richer. */
3061 if (ctxp->java_error_flag == 0)
3063 ctxp->java_error_flag = 1;
3064 #ifdef USE_MAPPED_LOCATION
3065 elc = input_location;
3066 #else
3067 elc = ctxp->lexer->token_start;
3068 #endif
3069 /* Do something to use the previous line if we're reaching the
3070 end of the file... */
3071 #ifdef VERBOSE_SKELETON
3072 printf ("* Error detected (%s)\n", (msgid ? msgid : "(null)"));
3073 #endif
3074 return;
3077 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3078 if (!force_error && msgid == prev_msg && prev_lineno == current_line)
3079 return;
3081 ctxp->java_error_flag = 0;
3082 if (do_warning)
3083 java_warning_count++;
3084 else
3085 java_error_count++;
3087 #if 0 /* FIXME */
3088 if (elc.col == 0 && msgid && msgid[1] == ';')
3089 elc = ctxp->prev_line_end;
3090 #endif
3092 prev_msg = msgid;
3094 #ifdef USE_MAPPED_LOCATION
3095 prev_lineno = current_line;
3096 code_from_source = java_get_line_col (xloc.file, current_line, xloc.column);
3097 #else
3098 save_lineno = input_line;
3099 prev_lineno = input_line = current_line;
3100 code_from_source = java_get_line_col (input_filename, current_line,
3101 ctxp->lexer->token_start.col);
3102 #endif
3105 obstack_grow0 (&temporary_obstack,
3106 code_from_source, strlen (code_from_source));
3107 remainder = obstack_finish (&temporary_obstack);
3108 if (do_warning)
3109 warning ("%s.\n%s", msgid, remainder);
3110 else
3111 error ("%s.\n%s", msgid, remainder);
3113 /* This allow us to cheaply avoid an extra 'Invalid expression
3114 statement' error report when errors have been already reported on
3115 the same line. This occurs when we report an error but don't have
3116 a synchronization point other than ';', which
3117 expression_statement is the only one to take care of. */
3118 #ifndef USE_MAPPED_LOCATION
3119 input_line = save_lineno;
3120 #endif
3121 ctxp->prevent_ese = input_line;
3124 static void
3125 issue_warning_error_from_context (
3126 #ifdef USE_MAPPED_LOCATION
3127 source_location cl,
3128 #else
3129 tree cl,
3130 #endif
3131 const char *msgid, va_list *ap)
3133 #ifdef USE_MAPPED_LOCATION
3134 source_location saved_location = input_location;
3135 expanded_location xloc = expand_location (cl);
3136 #else
3137 java_lc save_lc = ctxp->lexer->token_start;
3138 const char *saved = ctxp->filename, *saved_input_filename;
3139 #endif
3140 char buffer [4096];
3141 text_info text;
3143 text.err_no = errno;
3144 text.args_ptr = ap;
3145 text.format_spec = msgid;
3146 pp_format_text (global_dc->printer, &text);
3147 strncpy (buffer, pp_formatted_text (global_dc->printer), sizeof (buffer) - 1);
3148 buffer[sizeof (buffer) - 1] = '\0';
3149 pp_clear_output_area (global_dc->printer);
3151 force_error = 1;
3153 #ifdef USE_MAPPED_LOCATION
3154 if (xloc.file != NULL)
3156 ctxp->filename = xloc.file;
3157 input_location = cl;
3159 #else
3160 ctxp->lexer->token_start.line = EXPR_WFL_LINENO (cl);
3161 ctxp->lexer->token_start.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1
3162 : EXPR_WFL_COLNO (cl) == 0xffe ? -2
3163 : EXPR_WFL_COLNO (cl));
3165 /* We have a CL, that's a good reason for using it if it contains data */
3166 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3167 ctxp->filename = EXPR_WFL_FILENAME (cl);
3168 saved_input_filename = input_filename;
3169 input_filename = ctxp->filename;
3170 #endif
3171 java_error (NULL);
3172 java_error (buffer);
3173 #ifdef USE_MAPPED_LOCATION
3174 input_location = saved_location;
3175 #else
3176 ctxp->filename = saved;
3177 input_filename = saved_input_filename;
3178 ctxp->lexer->token_start = save_lc;
3179 #endif
3180 force_error = 0;
3183 /* Issue an error message at a current source line CL.
3184 FUTURE/FIXME: change cl to be a source_location. */
3186 void
3187 parse_error_context (tree cl, const char *msgid, ...)
3189 va_list ap;
3190 va_start (ap, msgid);
3191 #ifdef USE_MAPPED_LOCATION
3192 issue_warning_error_from_context (EXPR_LOCATION (cl), msgid, &ap);
3193 #else
3194 issue_warning_error_from_context (cl, msgid, &ap);
3195 #endif
3196 va_end (ap);
3199 /* Issue a warning at a current source line CL.
3200 FUTURE/FIXME: change cl to be a source_location. */
3202 static void
3203 parse_warning_context (tree cl, const char *msgid, ...)
3205 va_list ap;
3206 va_start (ap, msgid);
3208 do_warning = 1;
3209 #ifdef USE_MAPPED_LOCATION
3210 issue_warning_error_from_context (EXPR_LOCATION (cl), msgid, &ap);
3211 #else
3212 issue_warning_error_from_context (cl, msgid, &ap);
3213 #endif
3214 do_warning = 0;
3215 va_end (ap);
3218 static tree
3219 find_expr_with_wfl (tree node)
3221 while (node)
3223 enum tree_code_class code;
3224 tree to_return;
3226 switch (TREE_CODE (node))
3228 case BLOCK:
3229 node = BLOCK_EXPR_BODY (node);
3230 continue;
3232 case COMPOUND_EXPR:
3233 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3234 if (to_return)
3235 return to_return;
3236 node = TREE_OPERAND (node, 1);
3237 continue;
3239 case LOOP_EXPR:
3240 node = TREE_OPERAND (node, 0);
3241 continue;
3243 case LABELED_BLOCK_EXPR:
3244 node = LABELED_BLOCK_BODY (node);
3245 continue;
3247 default:
3248 code = TREE_CODE_CLASS (TREE_CODE (node));
3249 if (((code == tcc_unary) || (code == tcc_binary)
3250 || (code == tcc_expression))
3251 && EXPR_WFL_LINECOL (node))
3252 return node;
3253 return NULL_TREE;
3256 return NULL_TREE;
3259 /* Issue a missing return statement error. Uses METHOD to figure the
3260 last line of the method the error occurs in. */
3262 static void
3263 missing_return_error (tree method)
3265 #ifdef USE_MAPPED_LOCATION
3266 SET_EXPR_LOCATION (wfl_operator, DECL_FUNCTION_LAST_LINE (method));
3267 #else
3268 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_FUNCTION_LAST_LINE (method), -2);
3269 #endif
3270 parse_error_context (wfl_operator, "Missing return statement");
3273 /* Issue an unreachable statement error. From NODE, find the next
3274 statement to report appropriately. */
3275 static void
3276 unreachable_stmt_error (tree node)
3278 /* Browse node to find the next expression node that has a WFL. Use
3279 the location to report the error */
3280 if (TREE_CODE (node) == COMPOUND_EXPR)
3281 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3282 else
3283 node = find_expr_with_wfl (node);
3285 if (node)
3287 #ifdef USE_MAPPED_LOCATION
3288 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
3289 #else
3290 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3291 #endif
3292 parse_error_context (wfl_operator, "Unreachable statement");
3294 else
3295 abort ();
3298 static int
3299 not_accessible_field_error (tree wfl, tree decl)
3301 parse_error_context
3302 (wfl, "Can't access %s field %<%s.%s%> from %qs",
3303 accessibility_string (get_access_flags_from_decl (decl)),
3304 GET_TYPE_NAME (DECL_CONTEXT (decl)),
3305 IDENTIFIER_POINTER (DECL_NAME (decl)),
3306 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
3307 return 1;
3311 java_report_errors (void)
3313 if (java_error_count)
3314 fprintf (stderr, "%d error%s",
3315 java_error_count, (java_error_count == 1 ? "" : "s"));
3316 if (java_warning_count)
3317 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3318 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3319 if (java_error_count || java_warning_count)
3320 putc ('\n', stderr);
3321 return java_error_count;
3324 static char *
3325 java_accstring_lookup (int flags)
3327 static char buffer [80];
3328 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3330 /* Access modifier looked-up first for easier report on forbidden
3331 access. */
3332 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3333 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3334 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3335 if (flags & ACC_STATIC) COPY_RETURN ("static");
3336 if (flags & ACC_FINAL) COPY_RETURN ("final");
3337 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3338 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3339 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3340 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3341 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3342 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3344 buffer [0] = '\0';
3345 return buffer;
3346 #undef COPY_RETURN
3349 /* Returns a string denoting the accessibility of a class or a member as
3350 indicated by FLAGS. We need a separate function from
3351 java_accstring_lookup, as the latter can return spurious "static", etc.
3352 if package-private access is defined (in which case none of the
3353 relevant access control bits in FLAGS is set). */
3355 static const char *
3356 accessibility_string (int flags)
3358 if (flags & ACC_PRIVATE) return "private";
3359 if (flags & ACC_PROTECTED) return "protected";
3360 if (flags & ACC_PUBLIC) return "public";
3362 return "package-private";
3365 /* Issuing error messages upon redefinition of classes, interfaces or
3366 variables. */
3368 static void
3369 classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3371 parse_error_context (cl, "%s %qs already defined in %s:%d",
3372 context, IDENTIFIER_POINTER (id),
3373 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3374 /* Here we should point out where its redefined. It's a unicode. FIXME */
3377 static void
3378 variable_redefinition_error (tree context, tree name, tree type, int line)
3380 const char *type_name;
3382 /* Figure a proper name for type. We might haven't resolved it */
3383 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3384 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3385 else
3386 type_name = lang_printable_name (type, 0);
3388 parse_error_context (context,
3389 "Variable %qs is already defined in this method and was declared %<%s %s%> at line %d",
3390 IDENTIFIER_POINTER (name),
3391 type_name, IDENTIFIER_POINTER (name), line);
3394 /* If ANAME is terminated with `[]', it indicates an array. This
3395 function returns the number of `[]' found and if this number is
3396 greater than zero, it extracts the array type name and places it in
3397 the node pointed to by TRIMMED unless TRIMMED is null. */
3399 static int
3400 build_type_name_from_array_name (tree aname, tree *trimmed)
3402 const char *name = IDENTIFIER_POINTER (aname);
3403 int len = IDENTIFIER_LENGTH (aname);
3404 int array_dims;
3406 STRING_STRIP_BRACKETS (name, len, array_dims);
3408 if (array_dims && trimmed)
3409 *trimmed = get_identifier_with_length (name, len);
3411 return array_dims;
3414 static tree
3415 build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3417 int more_dims = 0;
3419 /* Eventually get more dims */
3420 more_dims = build_type_name_from_array_name (name, &name);
3422 /* If we have, then craft a new type for this variable */
3423 if (more_dims)
3425 tree save = type;
3427 /* If we have a pointer, use its type */
3428 if (TREE_CODE (type) == POINTER_TYPE)
3429 type = TREE_TYPE (type);
3431 /* Building the first dimension of a primitive type uses this
3432 function */
3433 if (JPRIMITIVE_TYPE_P (type))
3435 type = build_java_array_type (type, -1);
3436 more_dims--;
3438 /* Otherwise, if we have a WFL for this type, use it (the type
3439 is already an array on an unresolved type, and we just keep
3440 on adding dimensions) */
3441 else if (type_wfl)
3443 type = type_wfl;
3444 more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3445 NULL);
3448 /* Add all the dimensions */
3449 while (more_dims--)
3450 type = build_unresolved_array_type (type);
3452 /* The type may have been incomplete in the first place */
3453 if (type_wfl)
3454 type = obtain_incomplete_type (type);
3457 if (ret_name)
3458 *ret_name = name;
3459 return type;
3462 /* Build something that the type identifier resolver will identify as
3463 being an array to an unresolved type. TYPE_WFL is a WFL on a
3464 identifier. */
3466 static tree
3467 build_unresolved_array_type (tree type_or_wfl)
3469 const char *ptr;
3470 tree wfl;
3472 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3473 just create a array type */
3474 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3475 return build_java_array_type (type_or_wfl, -1);
3477 obstack_grow (&temporary_obstack,
3478 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3479 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3480 obstack_grow0 (&temporary_obstack, "[]", 2);
3481 ptr = obstack_finish (&temporary_obstack);
3482 #ifdef USE_MAPPED_LOCATION
3483 wfl = build_expr_wfl (get_identifier (ptr), EXPR_LOCATION (type_or_wfl));
3484 #else
3485 wfl = build_expr_wfl (get_identifier (ptr),
3486 EXPR_WFL_FILENAME (type_or_wfl),
3487 EXPR_WFL_LINENO (type_or_wfl),
3488 EXPR_WFL_COLNO (type_or_wfl));
3489 #endif
3490 /* Re-install the existing qualifications so that the type can be
3491 resolved properly. */
3492 EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3493 return wfl;
3496 static void
3497 parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3499 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3500 parse_error_context (wfl, "Interface %qs repeated",
3501 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3504 /* Bulk of common class/interface checks. Return 1 if an error was
3505 encountered. TAG is 0 for a class, 1 for an interface. */
3507 static int
3508 check_class_interface_creation (int is_interface, int flags, tree raw_name,
3509 tree qualified_name, tree decl, tree cl)
3511 tree node;
3512 int sca = 0; /* Static class allowed */
3513 int icaf = 0; /* Inner class allowed flags */
3514 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3516 if (!quiet_flag)
3517 fprintf (stderr, " %s%s %s",
3518 (CPC_INNER_P () ? "inner" : ""),
3519 (is_interface ? "interface" : "class"),
3520 IDENTIFIER_POINTER (qualified_name));
3522 /* Scope of an interface/class type name:
3523 - Can't be imported by a single type import
3524 - Can't already exists in the package */
3525 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3526 && (node = find_name_in_single_imports (raw_name))
3527 && !CPC_INNER_P ())
3529 parse_error_context
3530 (cl, "%s name %qs clashes with imported type %qs",
3531 (is_interface ? "Interface" : "Class"),
3532 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3533 return 1;
3535 if (decl && CLASS_COMPLETE_P (decl))
3537 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3538 qualified_name, decl, cl);
3539 return 1;
3542 if (check_inner_class_redefinition (raw_name, cl))
3543 return 1;
3545 /* If public, file name should match class/interface name, except
3546 when dealing with an inner class */
3547 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3549 const char *fname = input_filename;
3550 const char *f;
3552 for (f = fname + strlen (fname);
3553 f != fname && ! IS_DIR_SEPARATOR (*f);
3554 f--)
3556 if (IS_DIR_SEPARATOR (*f))
3557 f++;
3558 if (strncmp (IDENTIFIER_POINTER (raw_name),
3559 f , IDENTIFIER_LENGTH (raw_name)) ||
3560 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3561 parse_error_context
3562 (cl, "Public %s %qs must be defined in a file called %<%s.java%>",
3563 (is_interface ? "interface" : "class"),
3564 IDENTIFIER_POINTER (qualified_name),
3565 IDENTIFIER_POINTER (raw_name));
3568 /* Static classes can be declared only in top level classes. Note:
3569 once static, a inner class is a top level class. */
3570 if (flags & ACC_STATIC)
3572 /* Catch the specific error of declaring an class inner class
3573 with no toplevel enclosing class. Prevent check_modifiers from
3574 complaining a second time */
3575 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3577 parse_error_context (cl, "Inner class %qs can't be static. Static classes can only occur in interfaces and top-level classes",
3578 IDENTIFIER_POINTER (qualified_name));
3579 sca = ACC_STATIC;
3581 /* Else, in the context of a top-level class declaration, let
3582 `check_modifiers' do its job, otherwise, give it a go */
3583 else
3584 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3587 /* Inner classes can be declared private or protected
3588 within their enclosing classes. */
3589 if (CPC_INNER_P ())
3591 /* A class which is local to a block can't be public, private,
3592 protected or static. But it is created final, so allow this
3593 one. */
3594 if (current_function_decl)
3595 icaf = sca = uaaf = ACC_FINAL;
3596 else
3598 check_modifiers_consistency (flags);
3599 icaf = ACC_PROTECTED;
3600 if (! CLASS_INTERFACE (GET_CPC ()))
3601 icaf |= ACC_PRIVATE;
3605 if (is_interface)
3607 if (CPC_INNER_P ())
3608 uaaf = INTERFACE_INNER_MODIFIERS;
3609 else
3610 uaaf = INTERFACE_MODIFIERS;
3612 check_modifiers ("Illegal modifier %qs for interface declaration",
3613 flags, uaaf);
3615 else
3616 check_modifiers ((current_function_decl ?
3617 "Illegal modifier %qs for local class declaration" :
3618 "Illegal modifier %qs for class declaration"),
3619 flags, uaaf|sca|icaf);
3620 return 0;
3623 /* Construct a nested class name. If the final component starts with
3624 a digit, return true. Otherwise return false. */
3625 static int
3626 make_nested_class_name (tree cpc_list)
3628 tree name;
3630 if (!cpc_list)
3631 return 0;
3633 make_nested_class_name (TREE_CHAIN (cpc_list));
3635 /* Pick the qualified name when dealing with the first upmost
3636 enclosing class */
3637 name = (TREE_CHAIN (cpc_list)
3638 ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3639 obstack_grow (&temporary_obstack,
3640 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3641 obstack_1grow (&temporary_obstack, '$');
3643 return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3646 /* Can't redefine a class already defined in an earlier scope. */
3648 static int
3649 check_inner_class_redefinition (tree raw_name, tree cl)
3651 tree scope_list;
3653 for (scope_list = GET_CPC_LIST (); scope_list;
3654 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3655 if (raw_name == GET_CPC_UN_NODE (scope_list))
3657 parse_error_context
3658 (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",
3659 IDENTIFIER_POINTER (raw_name));
3660 return 1;
3662 return 0;
3665 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3666 we remember ENCLOSING and SUPER. */
3668 static tree
3669 resolve_inner_class (htab_t circularity_hash, tree cl, tree *enclosing,
3670 tree *super, tree class_type)
3672 tree local_enclosing = *enclosing;
3673 tree local_super = NULL_TREE;
3675 while (local_enclosing)
3677 tree intermediate, decl;
3679 *htab_find_slot (circularity_hash, local_enclosing, INSERT) =
3680 local_enclosing;
3682 if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3683 return decl;
3685 intermediate = local_enclosing;
3686 /* Explore enclosing contexts. */
3687 while (INNER_CLASS_DECL_P (intermediate))
3689 intermediate = DECL_CONTEXT (intermediate);
3690 if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3691 return decl;
3694 /* Now go to the upper classes, bail out if necessary. We will
3695 analyze the returned SUPER and act accordingly (see
3696 do_resolve_class). */
3697 if (JPRIMITIVE_TYPE_P (TREE_TYPE (local_enclosing))
3698 || TREE_TYPE (local_enclosing) == void_type_node)
3700 parse_error_context (cl, "Qualifier must be a reference");
3701 local_enclosing = NULL_TREE;
3702 break;
3704 local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3705 if (!local_super || local_super == object_type_node)
3706 break;
3708 if (TREE_CODE (local_super) == POINTER_TYPE)
3709 local_super = do_resolve_class (NULL, local_super, NULL, NULL);
3710 else
3711 local_super = TYPE_NAME (local_super);
3713 /* We may not have checked for circular inheritance yet, so do so
3714 here to prevent an infinite loop. */
3715 if (htab_find (circularity_hash, local_super) != NULL)
3717 if (!cl)
3718 cl = lookup_cl (local_enclosing);
3720 parse_error_context
3721 (cl, "Cyclic inheritance involving %s",
3722 IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3723 local_enclosing = NULL_TREE;
3725 else
3726 local_enclosing = local_super;
3729 /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3730 *super = local_super;
3731 *enclosing = local_enclosing;
3733 return NULL_TREE;
3736 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3737 qualified. */
3739 static tree
3740 find_as_inner_class (tree enclosing, tree name, tree cl)
3742 tree qual, to_return;
3743 if (!enclosing)
3744 return NULL_TREE;
3746 name = TYPE_NAME (name);
3748 /* First search: within the scope of `enclosing', search for name */
3749 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3750 qual = EXPR_WFL_QUALIFICATION (cl);
3751 else if (cl)
3752 qual = build_tree_list (cl, NULL_TREE);
3753 else
3754 qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3756 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3757 return to_return;
3759 /* We're dealing with a qualified name. Try to resolve thing until
3760 we get something that is an enclosing class. */
3761 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3763 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3765 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3766 qual = TREE_CHAIN (qual))
3768 acc = merge_qualified_name (acc,
3769 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3770 BUILD_PTR_FROM_NAME (ptr, acc);
3771 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3774 /* A NULL qual and a decl means that the search ended
3775 successfully?!? We have to do something then. FIXME */
3777 if (decl)
3778 enclosing = decl;
3779 else
3780 qual = EXPR_WFL_QUALIFICATION (cl);
3782 /* Otherwise, create a qual for the other part of the resolution. */
3783 else
3784 qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3786 return find_as_inner_class_do (qual, enclosing);
3789 /* We go inside the list of sub classes and try to find a way
3790 through. */
3792 static tree
3793 find_as_inner_class_do (tree qual, tree enclosing)
3795 if (!qual)
3796 return NULL_TREE;
3798 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3800 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3801 tree next_enclosing = NULL_TREE;
3802 tree inner_list;
3804 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3805 inner_list; inner_list = TREE_CHAIN (inner_list))
3807 if (TREE_VALUE (inner_list) == name_to_match)
3809 next_enclosing = TREE_PURPOSE (inner_list);
3810 break;
3813 enclosing = next_enclosing;
3816 return (!qual && enclosing ? enclosing : NULL_TREE);
3819 static void
3820 link_nested_class_to_enclosing (void)
3822 if (GET_ENCLOSING_CPC ())
3824 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3825 DECL_INNER_CLASS_LIST (enclosing) =
3826 tree_cons (GET_CPC (), GET_CPC_UN (),
3827 DECL_INNER_CLASS_LIST (enclosing));
3831 static tree
3832 maybe_make_nested_class_name (tree name)
3834 tree id = NULL_TREE;
3836 if (CPC_INNER_P ())
3838 /* If we're in a function, we must append a number to create the
3839 nested class name. However, we don't do this if the class we
3840 are constructing is anonymous, because in that case we'll
3841 already have a number as the class name. */
3842 if (! make_nested_class_name (GET_CPC_LIST ())
3843 && current_function_decl != NULL_TREE
3844 && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3846 char buf[10];
3847 sprintf (buf, "%d", anonymous_class_counter);
3848 ++anonymous_class_counter;
3849 obstack_grow (&temporary_obstack, buf, strlen (buf));
3850 obstack_1grow (&temporary_obstack, '$');
3852 obstack_grow0 (&temporary_obstack,
3853 IDENTIFIER_POINTER (name),
3854 IDENTIFIER_LENGTH (name));
3855 id = get_identifier (obstack_finish (&temporary_obstack));
3856 if (ctxp->package)
3857 QUALIFIED_P (id) = 1;
3859 return id;
3862 /* If DECL is NULL, create and push a new DECL, record the current
3863 line CL and do other maintenance things. */
3865 static tree
3866 maybe_create_class_interface_decl (tree decl, tree raw_name,
3867 tree qualified_name, tree cl)
3869 if (!decl)
3870 decl = push_class (make_class (), qualified_name);
3872 /* Take care of the file and line business */
3873 #ifdef USE_MAPPED_LOCATION
3874 DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (cl);
3875 #else
3876 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3877 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3878 #endif
3879 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3880 CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3881 #ifdef USE_MAPPED_LOCATION
3883 tree tmp = maybe_get_identifier (EXPR_FILENAME (cl));
3884 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3885 tmp && IS_A_COMMAND_LINE_FILENAME_P (tmp);
3887 #else
3888 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3889 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3890 #endif
3892 PUSH_CPC (decl, raw_name);
3893 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3895 /* Link the declaration to the already seen ones */
3896 TREE_CHAIN (decl) = ctxp->class_list;
3897 ctxp->class_list = decl;
3899 /* Create a new nodes in the global lists */
3900 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3901 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3903 /* Install a new dependency list element */
3904 create_jdep_list (ctxp);
3906 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3907 IDENTIFIER_POINTER (qualified_name)));
3908 return decl;
3911 static void
3912 add_superinterfaces (tree decl, tree interface_list)
3914 tree node;
3915 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3916 takes care of ensuring that:
3917 - This is an accessible interface type,
3918 - Circularity detection.
3919 parser_add_interface is then called. If present but not defined,
3920 the check operation is delayed until the super interface gets
3921 defined. */
3922 for (node = interface_list; node; node = TREE_CHAIN (node))
3924 tree current = TREE_PURPOSE (node);
3925 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3926 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3928 if (!parser_check_super_interface (idecl, decl, current))
3929 parser_add_interface (decl, idecl, current);
3931 else
3932 register_incomplete_type (JDEP_INTERFACE,
3933 current, decl, NULL_TREE);
3937 /* Create an interface in pass1 and return its decl. Return the
3938 interface's decl in pass 2. */
3940 static tree
3941 create_interface (int flags, tree id, tree super)
3943 tree raw_name = EXPR_WFL_NODE (id);
3944 tree q_name = parser_qualified_classname (raw_name);
3945 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3947 /* Certain syntax errors are making SUPER be like ID. Avoid this
3948 case. */
3949 if (ctxp->class_err && id == super)
3950 super = NULL;
3952 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3954 /* Basic checks: scope, redefinition, modifiers */
3955 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3957 PUSH_ERROR ();
3958 return NULL_TREE;
3961 /* Suspend the current parsing context if we're parsing an inner
3962 interface */
3963 if (CPC_INNER_P ())
3965 java_parser_context_suspend ();
3966 /* Interface members are public. */
3967 if (CLASS_INTERFACE (GET_CPC ()))
3968 flags |= ACC_PUBLIC;
3971 /* Push a new context for (static) initialized upon declaration fields */
3972 java_parser_context_push_initialized_field ();
3974 /* Interface modifiers check
3975 - public/abstract allowed (already done at that point)
3976 - abstract is obsolete (comes first, it's a warning, or should be)
3977 - Can't use twice the same (checked in the modifier rule) */
3978 if ((flags & ACC_ABSTRACT) && flag_redundant)
3979 parse_warning_context
3980 (MODIFIER_WFL (ABSTRACT_TK),
3981 "Redundant use of %<abstract%> modifier. Interface %qs is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3983 /* Create a new decl if DECL is NULL, otherwise fix it */
3984 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3986 /* Interfaces are always abstract. */
3987 flags |= ACC_ABSTRACT;
3989 /* Inner interfaces are always static. */
3990 if (INNER_CLASS_DECL_P (decl))
3991 flags |= ACC_STATIC;
3993 /* Set super info and mark the class a complete */
3994 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3995 object_type_node, ctxp->interface_number);
3996 ctxp->interface_number = 0;
3997 CLASS_COMPLETE_P (decl) = 1;
3998 add_superinterfaces (decl, super);
4000 /* Eventually sets the @deprecated tag flag */
4001 CHECK_DEPRECATED (decl);
4003 return decl;
4006 /* Patch anonymous class CLASS, by either extending or implementing
4007 DEP. */
4009 static void
4010 patch_anonymous_class (tree type_decl, tree class_decl, tree wfl)
4012 tree class = TREE_TYPE (class_decl);
4013 tree type = TREE_TYPE (type_decl);
4014 tree binfo = TYPE_BINFO (class);
4016 /* If it's an interface, implement it */
4017 if (CLASS_INTERFACE (type_decl))
4019 if (parser_check_super_interface (type_decl, class_decl, wfl))
4020 return;
4022 if (!VEC_space (tree, BINFO_BASE_BINFOS (binfo), 1))
4024 /* Extend the binfo - by reallocating and copying it. */
4025 tree new_binfo;
4026 tree base_binfo;
4027 int i;
4029 new_binfo = make_tree_binfo ((BINFO_N_BASE_BINFOS (binfo) + 1) * 2);
4030 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4031 BINFO_BASE_APPEND (new_binfo, base_binfo);
4032 CLASS_HAS_SUPER_FLAG (new_binfo) = CLASS_HAS_SUPER_FLAG (binfo);
4033 BINFO_VTABLE (new_binfo) = BINFO_VTABLE (binfo);
4034 TYPE_BINFO (class) = new_binfo;
4037 /* And add the interface */
4038 parser_add_interface (class_decl, type_decl, wfl);
4040 /* Otherwise, it's a type we want to extend */
4041 else
4043 if (parser_check_super (type_decl, class_decl, wfl))
4044 return;
4045 BINFO_TYPE (BINFO_BASE_BINFO (binfo, 0)) = type;
4049 /* Create an anonymous class which extends/implements TYPE_NAME, and return
4050 its decl. */
4052 static tree
4053 create_anonymous_class (tree type_name)
4055 char buffer [80];
4056 tree super = NULL_TREE, itf = NULL_TREE;
4057 tree id, type_decl, class;
4059 /* The unqualified name of the anonymous class. It's just a number. */
4060 sprintf (buffer, "%d", anonymous_class_counter++);
4061 id = build_wfl_node (get_identifier (buffer));
4062 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL (type_name);
4064 /* We know about the type to extend/implement. We go ahead */
4065 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
4067 /* Create a class which either implements on extends the designated
4068 class. The class bears an inaccessible name. */
4069 if (CLASS_INTERFACE (type_decl))
4071 /* It's OK to modify it here. It's been already used and
4072 shouldn't be reused */
4073 ctxp->interface_number = 1;
4074 /* Interfaces should presented as a list of WFLs */
4075 itf = build_tree_list (type_name, NULL_TREE);
4077 else
4078 super = type_name;
4081 class = create_class (ACC_FINAL, id, super, itf);
4083 /* We didn't know anything about the stuff. We register a dependence. */
4084 if (!type_decl)
4085 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
4087 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
4088 return class;
4091 /* Create a class in pass1 and return its decl. Return class
4092 interface's decl in pass 2. */
4094 static tree
4095 create_class (int flags, tree id, tree super, tree interfaces)
4097 tree raw_name = EXPR_WFL_NODE (id);
4098 tree class_id, decl;
4099 tree super_decl_type;
4101 /* Certain syntax errors are making SUPER be like ID. Avoid this
4102 case. */
4103 if (ctxp->class_err && id == super)
4104 super = NULL;
4106 class_id = parser_qualified_classname (raw_name);
4107 decl = IDENTIFIER_CLASS_VALUE (class_id);
4108 EXPR_WFL_NODE (id) = class_id;
4110 /* Basic check: scope, redefinition, modifiers */
4111 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
4113 PUSH_ERROR ();
4114 return NULL_TREE;
4117 /* Suspend the current parsing context if we're parsing an inner
4118 class or an anonymous class. */
4119 if (CPC_INNER_P ())
4121 java_parser_context_suspend ();
4122 /* Interface members are public. */
4123 if (CLASS_INTERFACE (GET_CPC ()))
4124 flags |= ACC_PUBLIC;
4127 /* Push a new context for (static) initialized upon declaration fields */
4128 java_parser_context_push_initialized_field ();
4130 /* Class modifier check:
4131 - Allowed modifier (already done at that point)
4132 - abstract AND final forbidden
4133 - Public classes defined in the correct file */
4134 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4135 parse_error_context
4136 (id, "Class %qs can't be declared both abstract and final",
4137 IDENTIFIER_POINTER (raw_name));
4139 /* Create a new decl if DECL is NULL, otherwise fix it */
4140 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4142 /* If SUPER exists, use it, otherwise use Object */
4143 if (super)
4145 /* java.lang.Object can't extend anything. */
4146 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4148 parse_error_context (id, "%<java.lang.Object%> can't extend anything");
4149 return NULL_TREE;
4152 super_decl_type =
4153 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4155 else if (TREE_TYPE (decl) != object_type_node)
4156 super_decl_type = object_type_node;
4157 /* We're defining java.lang.Object */
4158 else
4159 super_decl_type = NULL_TREE;
4161 /* A class nested in an interface is implicitly static. */
4162 if (INNER_CLASS_DECL_P (decl)
4163 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4165 flags |= ACC_STATIC;
4168 /* Set super info and mark the class as complete. */
4169 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4170 ctxp->interface_number);
4171 ctxp->interface_number = 0;
4172 CLASS_COMPLETE_P (decl) = 1;
4173 add_superinterfaces (decl, interfaces);
4175 /* TYPE_VFIELD' is a compiler-generated field used to point to
4176 virtual function tables. In gcj, every class has a common base
4177 virtual function table in java.lang.object. */
4178 TYPE_VFIELD (TREE_TYPE (decl)) = TYPE_VFIELD (object_type_node);
4180 /* Add the private this$<n> field, Replicate final locals still in
4181 scope as private final fields mangled like val$<local_name>.
4182 This does not occur for top level (static) inner classes. */
4183 if (PURE_INNER_CLASS_DECL_P (decl))
4184 add_inner_class_fields (decl, current_function_decl);
4186 /* Eventually sets the @deprecated tag flag */
4187 CHECK_DEPRECATED (decl);
4189 /* Reset the anonymous class counter when declaring non inner classes */
4190 if (!INNER_CLASS_DECL_P (decl))
4191 anonymous_class_counter = 1;
4193 return decl;
4196 /* End a class declaration: register the statements used to create
4197 finit$ and <clinit>, pop the current class and resume the prior
4198 parser context if necessary. */
4200 static void
4201 end_class_declaration (int resume)
4203 /* If an error occurred, context weren't pushed and won't need to be
4204 popped by a resume. */
4205 int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4207 if (GET_CPC () != error_mark_node)
4208 dump_java_tree (TDI_class, GET_CPC ());
4210 java_parser_context_pop_initialized_field ();
4211 POP_CPC ();
4212 if (resume && no_error_occurred)
4213 java_parser_context_resume ();
4215 /* We're ending a class declaration, this is a good time to reset
4216 the interface cout. Note that might have been already done in
4217 create_interface, but if at that time an inner class was being
4218 dealt with, the interface count was reset in a context created
4219 for the sake of handling inner classes declaration. */
4220 ctxp->interface_number = 0;
4223 static void
4224 add_inner_class_fields (tree class_decl, tree fct_decl)
4226 tree block, marker, f;
4228 f = add_field (TREE_TYPE (class_decl),
4229 build_current_thisn (TREE_TYPE (class_decl)),
4230 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4231 ACC_PRIVATE);
4232 FIELD_THISN (f) = 1;
4234 if (!fct_decl)
4235 return;
4237 for (block = GET_CURRENT_BLOCK (fct_decl);
4238 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4240 tree decl;
4241 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4243 tree name, pname;
4244 tree wfl, init, list;
4246 /* Avoid non final arguments. */
4247 if (!LOCAL_FINAL_P (decl))
4248 continue;
4250 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4251 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4252 wfl = build_wfl_node (name);
4253 init = build_wfl_node (pname);
4254 /* Build an initialization for the field: it will be
4255 initialized by a parameter added to finit$, bearing a
4256 mangled name of the field itself (param$<n>.) The
4257 parameter is provided to finit$ by the constructor
4258 invoking it (hence the constructor will also feature a
4259 hidden parameter, set to the value of the outer context
4260 local at the time the inner class is created.)
4262 Note: we take into account all possible locals that can
4263 be accessed by the inner class. It's actually not trivial
4264 to minimize these aliases down to the ones really
4265 used. One way to do that would be to expand all regular
4266 methods first, then finit$ to get a picture of what's
4267 used. It works with the exception that we would have to
4268 go back on all constructor invoked in regular methods to
4269 have their invocation reworked (to include the right amount
4270 of alias initializer parameters.)
4272 The only real way around, I think, is a first pass to
4273 identify locals really used in the inner class. We leave
4274 the flag FIELD_LOCAL_ALIAS_USED around for that future
4275 use.
4277 On the other hand, it only affect local inner classes,
4278 whose constructors (and finit$ call) will be featuring
4279 unnecessary arguments. It's easy for a developer to keep
4280 this number of parameter down by using the `final'
4281 keyword only when necessary. For the time being, we can
4282 issue a warning on unnecessary finals. FIXME */
4283 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4284 wfl, init);
4286 /* Register the field. The TREE_LIST holding the part
4287 initialized/initializer will be marked ARG_FINAL_P so
4288 that the created field can be marked
4289 FIELD_LOCAL_ALIAS. */
4290 list = build_tree_list (wfl, init);
4291 ARG_FINAL_P (list) = 1;
4292 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4296 if (!CPC_INITIALIZER_STMT (ctxp))
4297 return;
4299 /* If we ever registered an alias field, insert and marker to
4300 remember where the list ends. The second part of the list (the one
4301 featuring initialized fields) so it can be later reversed to
4302 enforce 8.5. The marker will be removed during that operation. */
4303 marker = build_tree_list (NULL_TREE, NULL_TREE);
4304 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4305 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4308 /* Can't use lookup_field () since we don't want to load the class and
4309 can't set the CLASS_LOADED_P flag */
4311 static tree
4312 find_field (tree class, tree name)
4314 tree decl;
4315 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4317 if (DECL_NAME (decl) == name)
4318 return decl;
4320 return NULL_TREE;
4323 /* Wrap around lookup_field that doesn't potentially upset the value
4324 of CLASS */
4326 static tree
4327 lookup_field_wrapper (tree class, tree name)
4329 tree type = class;
4330 tree decl = NULL_TREE;
4331 java_parser_context_save_global ();
4333 /* Last chance: if we're within the context of an inner class, we
4334 might be trying to access a local variable defined in an outer
4335 context. We try to look for it now. */
4336 if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4338 tree new_name;
4339 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4340 decl = lookup_field (&type, new_name);
4341 if (decl && decl != error_mark_node)
4342 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4344 if (!decl || decl == error_mark_node)
4346 type = class;
4347 decl = lookup_field (&type, name);
4350 /* If the field still hasn't been found, try the next enclosing context. */
4351 if (!decl && INNER_CLASS_TYPE_P (class))
4353 tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4354 decl = lookup_field_wrapper (outer_type, name);
4357 java_parser_context_restore_global ();
4358 return decl == error_mark_node ? NULL : decl;
4361 /* Find duplicate field within the same class declarations and report
4362 the error. Returns 1 if a duplicated field was found, 0
4363 otherwise. */
4365 static int
4366 duplicate_declaration_error_p (tree new_field_name, tree new_type, tree cl)
4368 /* This might be modified to work with method decl as well */
4369 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4370 if (decl)
4372 char *t1 = xstrdup (purify_type_name
4373 ((TREE_CODE (new_type) == POINTER_TYPE
4374 && TREE_TYPE (new_type) == NULL_TREE) ?
4375 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4376 lang_printable_name (new_type, 1)));
4377 /* The type may not have been completed by the time we report
4378 the error */
4379 char *t2 = xstrdup (purify_type_name
4380 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4381 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4382 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4383 lang_printable_name (TREE_TYPE (decl), 1)));
4384 parse_error_context
4385 (cl, "Duplicate variable declaration: %<%s %s%> was %<%s %s%> (%s:%d)",
4386 t1, IDENTIFIER_POINTER (new_field_name),
4387 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4388 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4389 free (t1);
4390 free (t2);
4391 return 1;
4393 return 0;
4396 /* Field registration routine. If TYPE doesn't exist, field
4397 declarations are linked to the undefined TYPE dependency list, to
4398 be later resolved in java_complete_class () */
4400 static void
4401 register_fields (int flags, tree type, tree variable_list)
4403 tree current, saved_type;
4404 tree class_type = NULL_TREE;
4405 location_t saved_location = input_location;
4406 int must_chain = 0;
4407 tree wfl = NULL_TREE;
4409 if (GET_CPC ())
4410 class_type = TREE_TYPE (GET_CPC ());
4412 if (!class_type || class_type == error_mark_node)
4413 return;
4415 /* If we're adding fields to interfaces, those fields are public,
4416 static, final */
4417 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4419 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4420 flags, ACC_PUBLIC, "interface field(s)");
4421 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4422 flags, ACC_STATIC, "interface field(s)");
4423 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4424 flags, ACC_FINAL, "interface field(s)");
4425 check_modifiers ("Illegal interface member modifier %qs", flags,
4426 INTERFACE_FIELD_MODIFIERS);
4427 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4430 /* Obtain a suitable type for resolution, if necessary */
4431 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4433 /* If TYPE is fully resolved and we don't have a reference, make one */
4434 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4436 for (current = variable_list, saved_type = type; current;
4437 current = TREE_CHAIN (current), type = saved_type)
4439 tree real_type;
4440 tree field_decl;
4441 tree cl = TREE_PURPOSE (current);
4442 tree init = TREE_VALUE (current);
4443 tree current_name = EXPR_WFL_NODE (cl);
4445 /* Can't declare non-final static fields in inner classes */
4446 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4447 && !(flags & ACC_FINAL))
4448 parse_error_context
4449 (cl, "Field %qs can't be static in inner class %qs unless it is final",
4450 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4451 lang_printable_name (class_type, 0));
4453 /* Process NAME, as it may specify extra dimension(s) for it */
4454 type = build_array_from_name (type, wfl, current_name, &current_name);
4456 /* Type adjustment. We may have just readjusted TYPE because
4457 the variable specified more dimensions. Make sure we have
4458 a reference if we can and don't have one already. Also
4459 change the name if we have an init. */
4460 if (type != saved_type)
4462 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4463 if (init)
4464 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4467 real_type = GET_REAL_TYPE (type);
4468 /* Check for redeclarations */
4469 if (duplicate_declaration_error_p (current_name, real_type, cl))
4470 continue;
4472 /* Set input_line to the line the field was found and create a
4473 declaration for it. Eventually sets the @deprecated tag flag. */
4474 #ifdef USE_MAPPED_LOCATION
4475 input_location = EXPR_LOCATION (cl);
4476 #else
4477 input_line = EXPR_WFL_LINENO (cl);
4478 #endif
4479 field_decl = add_field (class_type, current_name, real_type, flags);
4480 CHECK_DEPRECATED_NO_RESET (field_decl);
4482 /* If the field denotes a final instance variable, then we
4483 allocate a LANG_DECL_SPECIFIC part to keep track of its
4484 initialization. We also mark whether the field was
4485 initialized upon its declaration. We don't do that if the
4486 created field is an alias to a final local. */
4487 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4489 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4490 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4493 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4494 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4495 hide parameters to this inner class finit$ and
4496 constructors. It also means that the field isn't final per
4497 say. */
4498 if (ARG_FINAL_P (current))
4500 FIELD_LOCAL_ALIAS (field_decl) = 1;
4501 FIELD_FINAL (field_decl) = 0;
4504 /* Check if we must chain. */
4505 if (must_chain)
4506 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4508 /* If we have an initialization value tied to the field */
4509 if (init)
4511 /* The field is declared static */
4512 if (flags & ACC_STATIC)
4514 /* We include the field and its initialization part into
4515 a list used to generate <clinit>. After <clinit> is
4516 walked, field initializations will be processed and
4517 fields initialized with known constants will be taken
4518 out of <clinit> and have their DECL_INITIAL set
4519 appropriately. */
4520 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4521 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4522 if (TREE_OPERAND (init, 1)
4523 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4524 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4526 /* A non-static field declared with an immediate initialization is
4527 to be initialized in <init>, if any. This field is remembered
4528 to be processed at the time of the generation of <init>. */
4529 else
4531 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4532 SET_CPC_INITIALIZER_STMT (ctxp, init);
4534 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4535 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4539 CLEAR_DEPRECATED;
4540 input_location = saved_location;
4543 /* Generate finit$, using the list of initialized fields to populate
4544 its body. finit$'s parameter(s) list is adjusted to include the
4545 one(s) used to initialized the field(s) caching outer context
4546 local(s). */
4548 static tree
4549 generate_finit (tree class_type)
4551 int count = 0;
4552 tree list = TYPE_FINIT_STMT_LIST (class_type);
4553 tree mdecl, current, parms;
4555 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4556 class_type, NULL_TREE,
4557 &count);
4558 CRAFTED_PARAM_LIST_FIXUP (parms);
4559 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4560 finit_identifier_node, parms);
4561 fix_method_argument_names (parms, mdecl);
4562 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4563 mdecl, NULL_TREE);
4564 DECL_FUNCTION_NAP (mdecl) = count;
4565 start_artificial_method_body (mdecl);
4567 for (current = list; current; current = TREE_CHAIN (current))
4568 java_method_add_stmt (mdecl,
4569 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4570 current));
4571 end_artificial_method_body (mdecl);
4572 return mdecl;
4575 /* Generate a function to run the instance initialization code. The
4576 private method is called `instinit$'. Unless we're dealing with an
4577 anonymous class, we determine whether all ctors of CLASS_TYPE
4578 declare a checked exception in their `throws' clause in order to
4579 see whether it's necessary to encapsulate the instance initializer
4580 statements in a try/catch/rethrow sequence. */
4582 static tree
4583 generate_instinit (tree class_type)
4585 tree current;
4586 tree compound = NULL_TREE;
4587 tree parms = tree_cons (this_identifier_node,
4588 build_pointer_type (class_type), end_params_node);
4589 tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4590 void_type_node,
4591 instinit_identifier_node, parms);
4593 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4594 mdecl, NULL_TREE);
4596 /* Gather all the statements in a compound */
4597 for (current = TYPE_II_STMT_LIST (class_type);
4598 current; current = TREE_CHAIN (current))
4599 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4601 /* We need to encapsulate COMPOUND by a try/catch statement to
4602 rethrow exceptions that might occur in the instance initializer.
4603 We do that only if all ctors of CLASS_TYPE are set to catch a
4604 checked exception. This doesn't apply to anonymous classes (since
4605 they don't have declared ctors.) */
4606 if (!ANONYMOUS_CLASS_P (class_type) &&
4607 ctors_unchecked_throws_clause_p (class_type))
4609 compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4610 build1 (THROW_EXPR, NULL_TREE,
4611 build_wfl_node (wpv_id)));
4612 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4613 exception_type_node);
4616 start_artificial_method_body (mdecl);
4617 java_method_add_stmt (mdecl, compound);
4618 end_artificial_method_body (mdecl);
4620 return mdecl;
4623 /* FIXME */
4624 static tree
4625 build_instinit_invocation (tree class_type)
4627 tree to_return = NULL_TREE;
4629 if (TYPE_II_STMT_LIST (class_type))
4631 tree parm = build_tree_list (NULL_TREE,
4632 build_wfl_node (this_identifier_node));
4633 to_return =
4634 build_method_invocation (build_wfl_node (instinit_identifier_node),
4635 parm);
4637 return to_return;
4640 /* Shared across method_declarator and method_header to remember the
4641 patch stage that was reached during the declaration of the method.
4642 A method DECL is built differently is there is no patch
4643 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4644 pending on the currently defined method. */
4646 static int patch_stage;
4648 /* Check the method declaration and add the method to its current
4649 class. If the argument list is known to contain incomplete types,
4650 the method is partially added and the registration will be resume
4651 once the method arguments resolved. If TYPE is NULL, we're dealing
4652 with a constructor. */
4654 static tree
4655 method_header (int flags, tree type, tree mdecl, tree throws)
4657 tree type_wfl = NULL_TREE;
4658 tree meth_name = NULL_TREE;
4659 tree current, orig_arg, this_class = NULL;
4660 tree id, meth;
4661 location_t saved_location;
4662 int constructor_ok = 0, must_chain;
4663 int count;
4665 if (mdecl == error_mark_node)
4666 return error_mark_node;
4667 meth = TREE_VALUE (mdecl);
4668 id = TREE_PURPOSE (mdecl);
4670 check_modifiers_consistency (flags);
4672 if (GET_CPC ())
4673 this_class = TREE_TYPE (GET_CPC ());
4675 if (!this_class || this_class == error_mark_node)
4676 return NULL_TREE;
4678 /* There are some forbidden modifiers for an abstract method and its
4679 class must be abstract as well. */
4680 if (type && (flags & ACC_ABSTRACT))
4682 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4683 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4684 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4685 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4686 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4687 ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4688 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4689 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4690 parse_error_context
4691 (id,
4692 "Class %qs must be declared abstract to define abstract method %qs",
4693 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4694 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4697 /* A native method can't be strictfp. */
4698 if ((flags & ACC_NATIVE) && (flags & ACC_STRICT))
4699 parse_error_context (id, "native method %qs can't be strictfp",
4700 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4701 /* No such thing as a transient or volatile method. */
4702 if ((flags & ACC_TRANSIENT))
4703 parse_error_context (id, "method %qs can't be transient",
4704 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4705 if ((flags & ACC_VOLATILE))
4706 parse_error_context (id, "method %qs can't be volatile",
4707 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4709 /* Things to be checked when declaring a constructor */
4710 if (!type)
4712 int ec = java_error_count;
4713 /* 8.6: Constructor declarations: we might be trying to define a
4714 method without specifying a return type. */
4715 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4716 parse_error_context
4717 (id, "Invalid method declaration, return type required");
4718 /* 8.6.3: Constructor modifiers */
4719 else
4721 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4722 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4723 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4724 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4725 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4726 JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4728 /* If we found error here, we don't consider it's OK to tread
4729 the method definition as a constructor, for the rest of this
4730 function */
4731 if (ec == java_error_count)
4732 constructor_ok = 1;
4735 /* Method declared within the scope of an interface are implicitly
4736 abstract and public. Conflicts with other erroneously provided
4737 modifiers are checked right after. */
4739 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4741 /* If FLAGS isn't set because of a modifier, turn the
4742 corresponding modifier WFL to NULL so we issue a warning on
4743 the obsolete use of the modifier */
4744 if (!(flags & ACC_PUBLIC))
4745 MODIFIER_WFL (PUBLIC_TK) = NULL;
4746 if (!(flags & ACC_ABSTRACT))
4747 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4748 flags |= ACC_PUBLIC;
4749 flags |= ACC_ABSTRACT;
4752 /* Inner class can't declare static methods */
4753 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4755 parse_error_context
4756 (id, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
4757 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4758 lang_printable_name (this_class, 0));
4761 /* Modifiers context reset moved up, so abstract method declaration
4762 modifiers can be later checked. */
4764 /* Set constructor returned type to void and method name to <init>,
4765 unless we found an error identifier the constructor (in which
4766 case we retain the original name) */
4767 if (!type)
4769 type = void_type_node;
4770 if (constructor_ok)
4771 meth_name = init_identifier_node;
4773 else
4774 meth_name = EXPR_WFL_NODE (id);
4776 /* Do the returned type resolution and registration if necessary */
4777 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4779 if (meth_name)
4780 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4781 EXPR_WFL_NODE (id) = meth_name;
4782 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4784 if (must_chain)
4786 patch_stage = JDEP_METHOD_RETURN;
4787 register_incomplete_type (patch_stage, type_wfl, id, type);
4788 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4790 else
4791 TREE_TYPE (meth) = type;
4793 saved_location = input_location;
4794 /* When defining an abstract or interface method, the curly
4795 bracket at level 1 doesn't exist because there is no function
4796 body */
4797 #ifdef USE_MAPPED_LOCATION
4798 input_location = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4799 EXPR_LOCATION (id));
4800 #else
4801 input_line = (ctxp->first_ccb_indent1 ? (int) ctxp->first_ccb_indent1 :
4802 EXPR_WFL_LINENO (id));
4803 #endif
4805 /* Remember the original argument list */
4806 orig_arg = TYPE_ARG_TYPES (meth);
4808 if (patch_stage) /* includes ret type and/or all args */
4810 jdep *jdep;
4811 meth = add_method_1 (this_class, flags, meth_name, meth);
4812 /* Patch for the return type */
4813 if (patch_stage == JDEP_METHOD_RETURN)
4815 jdep = CLASSD_LAST (ctxp->classd_list);
4816 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4818 /* This is the stop JDEP. METH allows the function's signature
4819 to be computed. */
4820 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4822 else
4823 meth = add_method (this_class, flags, meth_name,
4824 build_java_signature (meth));
4826 /* Remember final parameters */
4827 MARK_FINAL_PARMS (meth, orig_arg);
4829 /* Fix the method argument list so we have the argument name
4830 information */
4831 fix_method_argument_names (orig_arg, meth);
4833 /* Register the parameter number and re-install the current line
4834 number */
4835 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4836 input_location = saved_location;
4838 /* Register exception specified by the `throws' keyword for
4839 resolution and set the method decl appropriate field to the list.
4840 Note: the grammar ensures that what we get here are class
4841 types. */
4842 if (throws)
4844 throws = nreverse (throws);
4845 for (current = throws; current; current = TREE_CHAIN (current))
4847 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4848 NULL_TREE, NULL_TREE);
4849 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4850 &TREE_VALUE (current);
4852 DECL_FUNCTION_THROWS (meth) = throws;
4855 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4856 DECL_FUNCTION_WFL (meth) = id;
4858 /* Set the flag if we correctly processed a constructor */
4859 if (constructor_ok)
4861 DECL_CONSTRUCTOR_P (meth) = 1;
4862 /* Compute and store the number of artificial parameters declared
4863 for this constructor */
4864 for (count = 0, current = TYPE_FIELDS (this_class); current;
4865 current = TREE_CHAIN (current))
4866 if (FIELD_LOCAL_ALIAS (current))
4867 count++;
4868 DECL_FUNCTION_NAP (meth) = count;
4871 /* Eventually set the @deprecated tag flag */
4872 CHECK_DEPRECATED (meth);
4874 return meth;
4877 static void
4878 fix_method_argument_names (tree orig_arg, tree meth)
4880 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4881 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4883 TREE_PURPOSE (arg) = this_identifier_node;
4884 arg = TREE_CHAIN (arg);
4886 while (orig_arg != end_params_node)
4888 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4889 orig_arg = TREE_CHAIN (orig_arg);
4890 arg = TREE_CHAIN (arg);
4894 /* Complete the method declaration with METHOD_BODY. */
4896 static void
4897 finish_method_declaration (tree method_body)
4899 int flags;
4901 if (!current_function_decl)
4902 return;
4904 flags = get_access_flags_from_decl (current_function_decl);
4906 /* 8.4.5 Method Body */
4907 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4909 tree name = DECL_NAME (current_function_decl);
4910 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4911 "%s method %qs can't have a body defined",
4912 (METHOD_NATIVE (current_function_decl) ?
4913 "Native" : "Abstract"),
4914 IDENTIFIER_POINTER (name));
4915 method_body = NULL_TREE;
4917 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4919 tree name = DECL_NAME (current_function_decl);
4920 parse_error_context
4921 (DECL_FUNCTION_WFL (current_function_decl),
4922 "Non native and non abstract method %qs must have a body defined",
4923 IDENTIFIER_POINTER (name));
4924 method_body = NULL_TREE;
4927 if (flag_emit_class_files && method_body
4928 && TREE_CODE (method_body) == NOP_EXPR
4929 && TREE_TYPE (current_function_decl)
4930 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4931 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4933 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4934 maybe_absorb_scoping_blocks ();
4935 /* Exit function's body */
4936 exit_block ();
4937 /* Merge last line of the function with first line, directly in the
4938 function decl. It will be used to emit correct debug info. */
4939 DECL_FUNCTION_LAST_LINE (current_function_decl) = ctxp->last_ccb_indent1;
4941 /* Since function's argument's list are shared, reset the
4942 ARG_FINAL_P parameter that might have been set on some of this
4943 function parameters. */
4944 UNMARK_FINAL_PARMS (current_function_decl);
4946 /* So we don't have an irrelevant function declaration context for
4947 the next static block we'll see. */
4948 current_function_decl = NULL_TREE;
4951 /* Build a an error message for constructor circularity errors. */
4953 static char *
4954 constructor_circularity_msg (tree from, tree to)
4956 static char string [4096];
4957 char *t = xstrdup (lang_printable_name (from, 2));
4958 sprintf (string, "'%s' invokes '%s'", t, lang_printable_name (to, 2));
4959 free (t);
4960 return string;
4963 /* Verify a circular call to METH. Return 1 if an error is found, 0
4964 otherwise. */
4966 static GTY(()) tree vcc_list;
4967 static int
4968 verify_constructor_circularity (tree meth, tree current)
4970 tree c;
4972 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4974 if (TREE_VALUE (c) == meth)
4976 char *t;
4977 if (vcc_list)
4979 tree liste;
4980 vcc_list = nreverse (vcc_list);
4981 for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
4983 parse_error_context
4984 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4985 constructor_circularity_msg
4986 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4987 java_error_count--;
4990 t = xstrdup (lang_printable_name (meth, 2));
4991 parse_error_context (TREE_PURPOSE (c),
4992 "%s: recursive invocation of constructor %qs",
4993 constructor_circularity_msg (current, meth), t);
4994 free (t);
4995 vcc_list = NULL_TREE;
4996 return 1;
4999 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5001 vcc_list = tree_cons (c, current, vcc_list);
5002 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
5003 return 1;
5004 vcc_list = TREE_CHAIN (vcc_list);
5006 return 0;
5009 /* Check modifiers that can be declared but exclusively */
5011 static void
5012 check_modifiers_consistency (int flags)
5014 int acc_count = 0;
5015 tree cl = NULL_TREE;
5017 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
5018 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
5019 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
5020 if (acc_count > 1)
5021 parse_error_context
5022 (cl, "Inconsistent member declaration. At most one of %<public%>, %<private%>, or %<protected%> may be specified");
5024 acc_count = 0;
5025 cl = NULL_TREE;
5026 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
5027 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
5028 if (acc_count > 1)
5029 parse_error_context (cl,
5030 "Inconsistent member declaration. At most one of %<final%> or %<volatile%> may be specified");
5033 /* Check the methode header METH for abstract specifics features */
5035 static void
5036 check_abstract_method_header (tree meth)
5038 int flags = get_access_flags_from_decl (meth);
5040 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
5041 ACC_ABSTRACT, "abstract method",
5042 IDENTIFIER_POINTER (DECL_NAME (meth)));
5043 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
5044 ACC_PUBLIC, "abstract method",
5045 IDENTIFIER_POINTER (DECL_NAME (meth)));
5047 check_modifiers ("Illegal modifier %qs for interface method",
5048 flags, INTERFACE_METHOD_MODIFIERS);
5051 /* Create a FUNCTION_TYPE node and start augmenting it with the
5052 declared function arguments. Arguments type that can't be resolved
5053 are left as they are, but the returned node is marked as containing
5054 incomplete types. */
5056 static tree
5057 method_declarator (tree id, tree list)
5059 tree arg_types = NULL_TREE, current, node;
5060 tree meth = make_node (FUNCTION_TYPE);
5061 jdep *jdep;
5063 patch_stage = JDEP_NO_PATCH;
5065 if (GET_CPC () == error_mark_node)
5066 return error_mark_node;
5068 /* If we're dealing with an inner class constructor, we hide the
5069 this$<n> decl in the name field of its parameter declaration. We
5070 also might have to hide the outer context local alias
5071 initializers. Not done when the class is a toplevel class. */
5072 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
5073 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
5075 tree aliases_list, type, thisn;
5076 /* First the aliases, linked to the regular parameters */
5077 aliases_list =
5078 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
5079 TREE_TYPE (GET_CPC ()),
5080 NULL_TREE, NULL);
5081 list = chainon (nreverse (aliases_list), list);
5083 /* Then this$<n> */
5084 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
5085 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
5086 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
5087 list);
5090 for (current = list; current; current = TREE_CHAIN (current))
5092 int must_chain = 0;
5093 tree wfl_name = TREE_PURPOSE (current);
5094 tree type = TREE_VALUE (current);
5095 tree name = EXPR_WFL_NODE (wfl_name);
5096 tree already, arg_node;
5097 tree type_wfl = NULL_TREE;
5098 tree real_type;
5100 /* Obtain a suitable type for resolution, if necessary */
5101 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5103 /* Process NAME, as it may specify extra dimension(s) for it */
5104 type = build_array_from_name (type, type_wfl, name, &name);
5105 EXPR_WFL_NODE (wfl_name) = name;
5107 real_type = GET_REAL_TYPE (type);
5108 if (TREE_CODE (real_type) == RECORD_TYPE)
5110 real_type = promote_type (real_type);
5111 if (TREE_CODE (type) == TREE_LIST)
5112 TREE_PURPOSE (type) = real_type;
5115 /* Check redefinition */
5116 for (already = arg_types; already; already = TREE_CHAIN (already))
5117 if (TREE_PURPOSE (already) == name)
5119 parse_error_context
5120 (wfl_name, "Variable %qs is used more than once in the argument list of method %qs",
5121 IDENTIFIER_POINTER (name),
5122 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5123 break;
5126 /* If we've an incomplete argument type, we know there is a location
5127 to patch when the type get resolved, later. */
5128 jdep = NULL;
5129 if (must_chain)
5131 patch_stage = JDEP_METHOD;
5132 type = register_incomplete_type (patch_stage,
5133 type_wfl, wfl_name, type);
5134 jdep = CLASSD_LAST (ctxp->classd_list);
5135 JDEP_MISC (jdep) = id;
5138 /* The argument node: a name and a (possibly) incomplete type. */
5139 arg_node = build_tree_list (name, real_type);
5140 /* Remember arguments declared final. */
5141 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5143 if (jdep)
5144 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5145 TREE_CHAIN (arg_node) = arg_types;
5146 arg_types = arg_node;
5148 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5149 node = build_tree_list (id, meth);
5150 return node;
5153 static int
5154 unresolved_type_p (tree wfl, tree *returned)
5156 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5158 if (returned)
5160 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5161 if (decl && current_class && (decl == TYPE_NAME (current_class)))
5162 *returned = TREE_TYPE (decl);
5163 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5164 *returned = TREE_TYPE (GET_CPC ());
5165 else
5166 *returned = NULL_TREE;
5168 return 1;
5170 if (returned)
5171 *returned = wfl;
5172 return 0;
5175 /* From NAME, build a qualified identifier node using the
5176 qualification from the current package definition. */
5178 static tree
5179 parser_qualified_classname (tree name)
5181 tree nested_class_name;
5183 if ((nested_class_name = maybe_make_nested_class_name (name)))
5184 return nested_class_name;
5186 if (ctxp->package)
5187 return merge_qualified_name (ctxp->package, name);
5188 else
5189 return name;
5192 /* Called once the type a interface extends is resolved. Returns 0 if
5193 everything is OK. */
5195 static int
5196 parser_check_super_interface (tree super_decl, tree this_decl, tree this_wfl)
5198 tree super_type = TREE_TYPE (super_decl);
5200 /* Has to be an interface */
5201 if (!CLASS_INTERFACE (super_decl))
5203 parse_error_context
5204 (this_wfl, "%s %qs can't implement/extend %s %qs",
5205 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5206 "Interface" : "Class"),
5207 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5208 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5209 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5210 return 1;
5213 /* Check top-level interface access. Inner classes are subject to member
5214 access rules (6.6.1). */
5215 if (! INNER_CLASS_P (super_type)
5216 && check_pkg_class_access (DECL_NAME (super_decl),
5217 NULL_TREE, true, this_decl))
5218 return 1;
5220 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5221 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5222 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5223 return 0;
5226 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5227 0 if everything is OK. */
5229 static int
5230 parser_check_super (tree super_decl, tree this_decl, tree wfl)
5232 tree super_type = TREE_TYPE (super_decl);
5234 /* SUPER should be a CLASS (neither an array nor an interface) */
5235 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5237 parse_error_context
5238 (wfl, "Class %qs can't subclass %s %qs",
5239 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5240 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5241 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5242 return 1;
5245 if (CLASS_FINAL (TYPE_NAME (super_type)))
5247 parse_error_context (wfl, "Can't subclass final classes: %s",
5248 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5249 return 1;
5252 /* Check top-level class scope. Inner classes are subject to member access
5253 rules (6.6.1). */
5254 if (! INNER_CLASS_P (super_type)
5255 && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true, NULL_TREE)))
5256 return 1;
5258 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5259 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5260 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5261 return 0;
5264 /* Create a new dependency list and link it (in a LIFO manner) to the
5265 CTXP list of type dependency list. */
5267 static void
5268 create_jdep_list (struct parser_ctxt *ctxp)
5270 jdeplist *new = xmalloc (sizeof (jdeplist));
5271 new->first = new->last = NULL;
5272 new->next = ctxp->classd_list;
5273 ctxp->classd_list = new;
5276 static jdeplist *
5277 reverse_jdep_list (struct parser_ctxt *ctxp)
5279 jdeplist *prev = NULL, *current, *next;
5280 for (current = ctxp->classd_list; current; current = next)
5282 next = current->next;
5283 current->next = prev;
5284 prev = current;
5286 return prev;
5289 /* Create a fake pointer based on the ID stored in
5290 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5291 registered again. */
5293 static tree
5294 obtain_incomplete_type (tree type_name)
5296 tree ptr = NULL_TREE, name;
5298 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5299 name = EXPR_WFL_NODE (type_name);
5300 else if (INCOMPLETE_TYPE_P (type_name))
5301 name = TYPE_NAME (type_name);
5302 else
5303 abort ();
5305 /* Workaround from build_pointer_type for incomplete types. */
5306 BUILD_PTR_FROM_NAME (ptr, name);
5307 TYPE_MODE (ptr) = ptr_mode;
5308 layout_type (ptr);
5310 return ptr;
5313 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5314 non NULL instead of computing a new fake type based on WFL. The new
5315 dependency is inserted in the current type dependency list, in FIFO
5316 manner. */
5318 static tree
5319 register_incomplete_type (int kind, tree wfl, tree decl, tree ptr)
5321 jdep *new = xmalloc (sizeof (jdep));
5323 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5324 ptr = obtain_incomplete_type (wfl);
5326 JDEP_KIND (new) = kind;
5327 JDEP_DECL (new) = decl;
5328 JDEP_TO_RESOLVE (new) = ptr;
5329 JDEP_WFL (new) = wfl;
5330 JDEP_CHAIN (new) = NULL;
5331 JDEP_MISC (new) = NULL_TREE;
5332 /* For some dependencies, set the enclosing class of the current
5333 class to be the enclosing context */
5334 if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS || kind == JDEP_SUPER)
5335 && GET_ENCLOSING_CPC ())
5336 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5337 else
5338 JDEP_ENCLOSING (new) = GET_CPC ();
5339 JDEP_GET_PATCH (new) = (tree *)NULL;
5341 JDEP_INSERT (ctxp->classd_list, new);
5343 return ptr;
5346 /* This checks for circular references with innerclasses. We start
5347 from SOURCE and should never reach TARGET. Extended/implemented
5348 types in SOURCE have their enclosing context checked not to reach
5349 TARGET. When the last enclosing context of SOURCE is reached, its
5350 extended/implemented types are also checked not to reach TARGET.
5351 In case of error, WFL of the offending type is returned; NULL_TREE
5352 otherwise. */
5354 static tree
5355 check_inner_circular_reference (tree source, tree target)
5357 tree base_binfo;
5358 tree ctx, cl;
5359 int i;
5361 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (source), i, base_binfo); i++)
5363 tree su;
5365 /* We can end up with a NULL_TREE or an incomplete type here if
5366 we encountered previous type resolution errors. It's safe to
5367 simply ignore these cases. */
5368 su = BINFO_TYPE (base_binfo);
5369 if (INCOMPLETE_TYPE_P (su))
5370 continue;
5372 if (inherits_from_p (su, target))
5373 return lookup_cl (TYPE_NAME (su));
5375 for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5377 /* An enclosing context shouldn't be TARGET */
5378 if (ctx == TYPE_NAME (target))
5379 return lookup_cl (TYPE_NAME (su));
5381 /* When we reach the enclosing last context, start a check
5382 on it, with the same target */
5383 if (! DECL_CONTEXT (ctx) &&
5384 (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5385 return cl;
5388 return NULL_TREE;
5391 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5392 offending type if a circularity is detected. NULL_TREE is returned
5393 otherwise. TYPE can be an interface or a class. */
5395 static tree
5396 check_circular_reference (tree type)
5398 tree base_binfo;
5399 int i;
5401 if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
5402 return NULL_TREE;
5404 if (! CLASS_INTERFACE (TYPE_NAME (type)))
5406 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5407 return lookup_cl (TYPE_NAME (type));
5408 return NULL_TREE;
5411 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo); i++)
5413 if (BINFO_TYPE (base_binfo) != object_type_node
5414 && interface_of_p (type, BINFO_TYPE (base_binfo)))
5415 return lookup_cl (TYPE_NAME (BINFO_TYPE (base_binfo)));
5417 return NULL_TREE;
5420 void
5421 java_check_circular_reference (void)
5423 tree current;
5424 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5426 tree type = TREE_TYPE (current);
5427 tree cl;
5429 cl = check_circular_reference (type);
5430 if (! cl)
5431 cl = check_inner_circular_reference (type, type);
5432 if (cl)
5433 parse_error_context (cl, "Cyclic class inheritance%s",
5434 (cyclic_inheritance_report ?
5435 cyclic_inheritance_report : ""));
5439 /* Augment the parameter list PARM with parameters crafted to
5440 initialize outer context locals aliases. Through ARTIFICIAL, a
5441 count is kept of the number of crafted parameters. MODE governs
5442 what eventually gets created: something suitable for a function
5443 creation or a function invocation, either the constructor or
5444 finit$. */
5446 static tree
5447 build_alias_initializer_parameter_list (int mode, tree class_type, tree parm,
5448 int *artificial)
5450 tree field;
5451 tree additional_parms = NULL_TREE;
5453 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5454 if (FIELD_LOCAL_ALIAS (field))
5456 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5457 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5458 tree mangled_id;
5460 switch (mode)
5462 case AIPL_FUNCTION_DECLARATION:
5463 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5464 &buffer [4]);
5465 purpose = build_wfl_node (mangled_id);
5466 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5467 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5468 else
5469 value = TREE_TYPE (field);
5470 break;
5472 case AIPL_FUNCTION_CREATION:
5473 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5474 &buffer [4]);
5475 value = TREE_TYPE (field);
5476 break;
5478 case AIPL_FUNCTION_FINIT_INVOCATION:
5479 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5480 &buffer [4]);
5481 /* Now, this is wrong. purpose should always be the NAME
5482 of something and value its matching value (decl, type,
5483 etc...) FIXME -- but there is a lot to fix. */
5485 /* When invoked for this kind of operation, we already
5486 know whether a field is used or not. */
5487 purpose = TREE_TYPE (field);
5488 value = build_wfl_node (mangled_id);
5489 break;
5491 case AIPL_FUNCTION_CTOR_INVOCATION:
5492 /* There are two case: the constructor invocation happens
5493 outside the local inner, in which case, locales from the outer
5494 context are directly used.
5496 Otherwise, we fold to using the alias directly. */
5497 if (class_type == current_class)
5498 value = field;
5499 else
5501 name = get_identifier (&buffer[4]);
5502 value = IDENTIFIER_LOCAL_VALUE (name);
5504 break;
5506 additional_parms = tree_cons (purpose, value, additional_parms);
5507 if (artificial)
5508 *artificial +=1;
5510 if (additional_parms)
5512 if (ANONYMOUS_CLASS_P (class_type)
5513 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5514 additional_parms = nreverse (additional_parms);
5515 parm = chainon (additional_parms, parm);
5518 return parm;
5521 /* Craft a constructor for CLASS_DECL -- what we should do when none
5522 where found. ARGS is non NULL when a special signature must be
5523 enforced. This is the case for anonymous classes. */
5525 static tree
5526 craft_constructor (tree class_decl, tree args)
5528 tree class_type = TREE_TYPE (class_decl);
5529 tree parm = NULL_TREE;
5530 /* Inherit access flags for the constructor from its enclosing class. */
5531 int valid_ctor_flags = ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE;
5532 int flags = (get_access_flags_from_decl (class_decl) & valid_ctor_flags);
5533 int i = 0, artificial = 0;
5534 tree decl, ctor_name;
5535 char buffer [80];
5537 ctor_name = init_identifier_node;
5539 /* If we're dealing with an inner class constructor, we hide the
5540 this$<n> decl in the name field of its parameter declaration. */
5541 if (PURE_INNER_CLASS_TYPE_P (class_type))
5543 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5544 parm = tree_cons (build_current_thisn (class_type),
5545 build_pointer_type (type), parm);
5547 /* Some more arguments to be hidden here. The values of the local
5548 variables of the outer context that the inner class needs to see. */
5549 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5550 class_type, parm,
5551 &artificial);
5554 /* Then if there are any args to be enforced, enforce them now */
5555 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5557 /* If we see a `void *', we need to change it to Object. */
5558 if (TREE_VALUE (args) == TREE_TYPE (null_pointer_node))
5559 TREE_VALUE (args) = object_ptr_type_node;
5561 sprintf (buffer, "parm%d", i++);
5562 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5565 CRAFTED_PARAM_LIST_FIXUP (parm);
5566 decl = create_artificial_method (class_type, flags, void_type_node,
5567 ctor_name, parm);
5568 fix_method_argument_names (parm, decl);
5569 /* Now, mark the artificial parameters. */
5570 DECL_FUNCTION_NAP (decl) = artificial;
5571 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5572 DECL_INLINE (decl) = 1;
5573 return decl;
5577 /* Fix the constructors. This will be called right after circular
5578 references have been checked. It is necessary to fix constructors
5579 early even if no code generation will take place for that class:
5580 some generated constructor might be required by the class whose
5581 compilation triggered this one to be simply loaded. */
5583 void
5584 java_fix_constructors (void)
5586 tree current;
5588 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5590 tree class_type = TREE_TYPE (current);
5591 int saw_ctor = 0;
5592 tree decl;
5594 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5595 continue;
5597 output_class = current_class = class_type;
5598 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5600 if (DECL_CONSTRUCTOR_P (decl))
5602 fix_constructors (decl);
5603 saw_ctor = 1;
5607 /* Anonymous class constructor can't be generated that early. */
5608 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5609 craft_constructor (current, NULL_TREE);
5613 /* safe_layout_class just makes sure that we can load a class without
5614 disrupting the current_class, input_file, input_line, etc, information
5615 about the class processed currently. */
5617 void
5618 safe_layout_class (tree class)
5620 tree save_current_class = current_class;
5621 location_t save_location = input_location;
5623 layout_class (class);
5625 current_class = save_current_class;
5626 input_location = save_location;
5629 static tree
5630 jdep_resolve_class (jdep *dep)
5632 tree decl;
5634 if (JDEP_RESOLVED_P (dep))
5635 decl = JDEP_RESOLVED_DECL (dep);
5636 else
5638 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5639 JDEP_DECL (dep), JDEP_WFL (dep));
5640 JDEP_RESOLVED (dep, decl);
5641 /* If there is no WFL, that's ok. We generate this warning
5642 elsewhere. */
5643 if (decl && JDEP_WFL (dep) != NULL_TREE)
5644 check_deprecation (JDEP_WFL (dep), decl);
5647 if (!decl)
5648 complete_class_report_errors (dep);
5649 else if (INNER_CLASS_DECL_P (decl))
5651 tree inner = TREE_TYPE (decl);
5652 if (! CLASS_LOADED_P (inner))
5654 safe_layout_class (inner);
5655 if (TYPE_SIZE (inner) == error_mark_node)
5656 TYPE_SIZE (inner) = NULL_TREE;
5658 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5660 return decl;
5663 /* Complete unsatisfied class declaration and their dependencies */
5665 void
5666 java_complete_class (void)
5668 tree cclass;
5669 jdeplist *cclassd;
5670 int error_found;
5671 tree type;
5673 /* Process imports */
5674 process_imports ();
5676 /* Reverse things so we have the right order */
5677 ctxp->class_list = nreverse (ctxp->class_list);
5678 ctxp->classd_list = reverse_jdep_list (ctxp);
5680 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5681 cclass && cclassd;
5682 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5684 jdep *dep;
5686 /* We keep the compilation unit imports in the class so that
5687 they can be used later to resolve type dependencies that
5688 aren't necessary to solve now. */
5689 TYPE_IMPORT_LIST (TREE_TYPE (cclass)) = ctxp->import_list;
5690 TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (cclass)) = ctxp->import_demand_list;
5692 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5694 tree decl;
5695 if (!(decl = jdep_resolve_class (dep)))
5696 continue;
5698 /* Now it's time to patch */
5699 switch (JDEP_KIND (dep))
5701 case JDEP_SUPER:
5702 /* Simply patch super */
5703 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5704 continue;
5705 BINFO_TYPE (BINFO_BASE_BINFO
5706 (TYPE_BINFO (TREE_TYPE (JDEP_DECL (dep))), 0))
5707 = TREE_TYPE (decl);
5708 break;
5710 case JDEP_FIELD:
5712 /* We do part of the job done in add_field */
5713 tree field_decl = JDEP_DECL (dep);
5714 tree field_type = TREE_TYPE (decl);
5715 if (TREE_CODE (field_type) == RECORD_TYPE)
5716 field_type = promote_type (field_type);
5717 TREE_TYPE (field_decl) = field_type;
5718 DECL_ALIGN (field_decl) = 0;
5719 DECL_USER_ALIGN (field_decl) = 0;
5720 layout_decl (field_decl, 0);
5721 SOURCE_FRONTEND_DEBUG
5722 (("Completed field/var decl '%s' with '%s'",
5723 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5724 IDENTIFIER_POINTER (DECL_NAME (decl))));
5725 break;
5727 case JDEP_METHOD: /* We start patching a method */
5728 case JDEP_METHOD_RETURN:
5729 error_found = 0;
5730 while (1)
5732 if (decl)
5734 type = TREE_TYPE(decl);
5735 if (TREE_CODE (type) == RECORD_TYPE)
5736 type = promote_type (type);
5737 JDEP_APPLY_PATCH (dep, type);
5738 SOURCE_FRONTEND_DEBUG
5739 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5740 "Completing fct '%s' with ret type '%s'":
5741 "Completing arg '%s' with type '%s'"),
5742 IDENTIFIER_POINTER (EXPR_WFL_NODE
5743 (JDEP_DECL_WFL (dep))),
5744 IDENTIFIER_POINTER (DECL_NAME (decl))));
5746 else
5747 error_found = 1;
5748 dep = JDEP_CHAIN (dep);
5749 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5750 break;
5751 else
5752 decl = jdep_resolve_class (dep);
5754 if (!error_found)
5756 tree mdecl = JDEP_DECL (dep), signature;
5757 /* Recompute and reset the signature, check first that
5758 all types are now defined. If they're not,
5759 don't build the signature. */
5760 if (check_method_types_complete (mdecl))
5762 signature = build_java_signature (TREE_TYPE (mdecl));
5763 set_java_signature (TREE_TYPE (mdecl), signature);
5766 else
5767 continue;
5768 break;
5770 case JDEP_INTERFACE:
5771 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5772 JDEP_WFL (dep)))
5773 continue;
5774 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5775 break;
5777 case JDEP_PARM:
5778 case JDEP_VARIABLE:
5779 type = TREE_TYPE(decl);
5780 if (TREE_CODE (type) == RECORD_TYPE)
5781 type = promote_type (type);
5782 JDEP_APPLY_PATCH (dep, type);
5783 break;
5785 case JDEP_TYPE:
5786 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5787 SOURCE_FRONTEND_DEBUG
5788 (("Completing a random type dependency on a '%s' node",
5789 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5790 break;
5792 case JDEP_EXCEPTION:
5793 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5794 SOURCE_FRONTEND_DEBUG
5795 (("Completing '%s' 'throws' argument node",
5796 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5797 break;
5799 case JDEP_ANONYMOUS:
5800 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5801 break;
5803 default:
5804 abort ();
5808 return;
5811 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5812 array. */
5814 static tree
5815 resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5817 tree tname = TYPE_NAME (class_type);
5818 tree resolved_type = TREE_TYPE (class_type);
5819 int array_dims = 0;
5820 tree resolved_type_decl;
5822 if (resolved_type != NULL_TREE)
5824 tree resolved_type_decl = TYPE_NAME (resolved_type);
5825 if (resolved_type_decl == NULL_TREE
5826 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5828 resolved_type_decl = build_decl (TYPE_DECL,
5829 TYPE_NAME (class_type),
5830 resolved_type);
5832 return resolved_type_decl;
5835 /* 1- Check to see if we have an array. If true, find what we really
5836 want to resolve */
5837 if ((array_dims = build_type_name_from_array_name (tname,
5838 &TYPE_NAME (class_type))))
5839 WFL_STRIP_BRACKET (cl, cl);
5841 /* 2- Resolve the bare type */
5842 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5843 decl, cl)))
5844 return NULL_TREE;
5845 resolved_type = TREE_TYPE (resolved_type_decl);
5847 /* 3- If we have an array, reconstruct the array down to its nesting */
5848 if (array_dims)
5850 for (; array_dims; array_dims--)
5851 resolved_type = build_java_array_type (resolved_type, -1);
5852 resolved_type_decl = TYPE_NAME (resolved_type);
5854 TREE_TYPE (class_type) = resolved_type;
5855 return resolved_type_decl;
5858 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5859 are used to report error messages; CL must either be NULL_TREE or a
5860 WFL wrapping a class. Do not try to replace TYPE_NAME (class_type)
5861 by a variable, since it is changed by find_in_imports{_on_demand}
5862 and (but it doesn't really matter) qualify_and_find. */
5864 tree
5865 do_resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5867 tree new_class_decl = NULL_TREE, super = NULL_TREE;
5868 tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5869 tree decl_result;
5870 htab_t circularity_hash;
5872 if (QUALIFIED_P (TYPE_NAME (class_type)))
5874 /* If the type name is of the form `Q . Id', then Q is either a
5875 package name or a class name. First we try to find Q as a
5876 class and then treat Id as a member type. If we can't find Q
5877 as a class then we fall through. */
5878 tree q, left, left_type, right;
5879 if (split_qualified_name (&left, &right, TYPE_NAME (class_type)) == 0)
5881 BUILD_PTR_FROM_NAME (left_type, left);
5882 q = do_resolve_class (enclosing, left_type, decl, cl);
5883 if (q)
5885 enclosing = q;
5886 saved_enclosing_type = TREE_TYPE (q);
5887 BUILD_PTR_FROM_NAME (class_type, right);
5892 if (enclosing)
5894 /* This hash table is used to register the classes we're going
5895 through when searching the current class as an inner class, in
5896 order to detect circular references. Remember to free it before
5897 returning the section 0- of this function. */
5898 circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
5899 NULL);
5901 /* 0- Search in the current class as an inner class.
5902 Maybe some code here should be added to load the class or
5903 something, at least if the class isn't an inner class and ended
5904 being loaded from class file. FIXME. */
5905 while (enclosing)
5907 new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5908 &super, class_type);
5909 if (new_class_decl)
5910 break;
5912 /* If we haven't found anything because SUPER reached Object and
5913 ENCLOSING happens to be an innerclass, try the enclosing context. */
5914 if ((!super || super == object_type_node) &&
5915 enclosing && INNER_CLASS_DECL_P (enclosing))
5916 enclosing = DECL_CONTEXT (enclosing);
5917 else
5918 enclosing = NULL_TREE;
5921 htab_delete (circularity_hash);
5923 if (new_class_decl)
5924 return new_class_decl;
5927 /* 1- Check for the type in single imports. This will change
5928 TYPE_NAME() if something relevant is found */
5929 find_in_imports (saved_enclosing_type, class_type);
5931 /* 2- And check for the type in the current compilation unit */
5932 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5934 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
5935 load_class (TYPE_NAME (class_type), 0);
5936 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5939 /* 3- Search according to the current package definition */
5940 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5942 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5943 TYPE_NAME (class_type))))
5944 return new_class_decl;
5947 /* 4- Check the import on demands. Don't allow bar.baz to be
5948 imported from foo.* */
5949 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5950 if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5951 return NULL_TREE;
5953 /* If found in find_in_imports_on_demand, the type has already been
5954 loaded. */
5955 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5956 return new_class_decl;
5958 /* 5- Try with a name qualified with the package name we've seen so far */
5959 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5961 tree package;
5963 /* If there is a current package (ctxp->package), it's the first
5964 element of package_list and we can skip it. */
5965 for (package = (ctxp->package ?
5966 TREE_CHAIN (package_list) : package_list);
5967 package; package = TREE_CHAIN (package))
5968 if ((new_class_decl = qualify_and_find (class_type,
5969 TREE_PURPOSE (package),
5970 TYPE_NAME (class_type))))
5971 return new_class_decl;
5974 /* 5- Check another compilation unit that bears the name of type */
5975 load_class (TYPE_NAME (class_type), 0);
5977 if (!cl)
5978 cl = lookup_cl (decl);
5980 /* If we don't have a value for CL, then we're being called recursively.
5981 We can't check package access just yet, but it will be taken care of
5982 by the caller. */
5983 if (cl)
5985 if (check_pkg_class_access (TYPE_NAME (class_type), cl, true, NULL_TREE))
5986 return NULL_TREE;
5989 /* 6- Last call for a resolution */
5990 decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5992 /* The final lookup might have registered a.b.c into a.b$c If we
5993 failed at the first lookup, progressively change the name if
5994 applicable and use the matching DECL instead. */
5995 if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
5997 char *separator;
5998 tree name = TYPE_NAME (class_type);
5999 char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
6001 strcpy (namebuffer, IDENTIFIER_POINTER (name));
6003 do {
6005 /* Reach the last '.', and if applicable, replace it by a `$' and
6006 see if this exists as a type. */
6007 if ((separator = strrchr (namebuffer, '.')))
6009 *separator = '$';
6010 name = get_identifier (namebuffer);
6011 decl_result = IDENTIFIER_CLASS_VALUE (name);
6013 } while (!decl_result && separator);
6015 return decl_result;
6018 static tree
6019 qualify_and_find (tree class_type, tree package, tree name)
6021 tree new_qualified = merge_qualified_name (package, name);
6022 tree new_class_decl;
6024 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
6025 load_class (new_qualified, 0);
6026 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
6028 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
6029 load_class (TREE_TYPE (new_class_decl), 0);
6030 TYPE_NAME (class_type) = new_qualified;
6031 return IDENTIFIER_CLASS_VALUE (new_qualified);
6033 return NULL_TREE;
6036 /* Resolve NAME and lay it out (if not done and if not the current
6037 parsed class). Return a decl node. This function is meant to be
6038 called when type resolution is necessary during the walk pass. */
6040 static tree
6041 resolve_and_layout (tree something, tree cl)
6043 tree decl, decl_type;
6045 /* Don't do that on the current class */
6046 if (something == current_class)
6047 return TYPE_NAME (current_class);
6049 /* Don't do anything for void and other primitive types */
6050 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6051 return NULL_TREE;
6053 /* Pointer types can be reall pointer types or fake pointers. When
6054 finding a real pointer, recheck for primitive types */
6055 if (TREE_CODE (something) == POINTER_TYPE)
6057 if (TREE_TYPE (something))
6059 something = TREE_TYPE (something);
6060 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6061 return NULL_TREE;
6063 else
6064 something = TYPE_NAME (something);
6067 /* Don't do anything for arrays of primitive types */
6068 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
6069 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
6070 return NULL_TREE;
6072 /* Something might be a WFL */
6073 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
6074 something = EXPR_WFL_NODE (something);
6076 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
6077 TYPE_DECL or a real TYPE */
6078 else if (TREE_CODE (something) != IDENTIFIER_NODE)
6079 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
6080 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
6082 if (!(decl = resolve_no_layout (something, cl)))
6083 return NULL_TREE;
6085 /* Resolve and layout if necessary */
6086 decl_type = TREE_TYPE (decl);
6087 layout_class_methods (decl_type);
6088 /* Check methods */
6089 if (CLASS_FROM_SOURCE_P (decl_type))
6090 java_check_methods (decl);
6091 /* Layout the type if necessary */
6092 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6093 safe_layout_class (decl_type);
6095 return decl;
6098 /* Resolve a class, returns its decl but doesn't perform any
6099 layout. The current parsing context is saved and restored */
6101 static tree
6102 resolve_no_layout (tree name, tree cl)
6104 tree ptr, decl;
6105 BUILD_PTR_FROM_NAME (ptr, name);
6106 java_parser_context_save_global ();
6107 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6108 java_parser_context_restore_global ();
6110 return decl;
6113 /* Called when reporting errors. Skip the '[]'s in a complex array
6114 type description that failed to be resolved. purify_type_name can't
6115 use an identifier tree. */
6117 static const char *
6118 purify_type_name (const char *name)
6120 int len = strlen (name);
6121 int bracket_found;
6123 STRING_STRIP_BRACKETS (name, len, bracket_found);
6124 if (bracket_found)
6126 char *stripped_name = xmemdup (name, len, len+1);
6127 stripped_name [len] = '\0';
6128 return stripped_name;
6130 return name;
6133 /* The type CURRENT refers to can't be found. We print error messages. */
6135 static void
6136 complete_class_report_errors (jdep *dep)
6138 const char *name;
6140 if (!JDEP_WFL (dep))
6141 return;
6143 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6144 switch (JDEP_KIND (dep))
6146 case JDEP_SUPER:
6147 parse_error_context
6148 (JDEP_WFL (dep), "Superclass %qs of class %qs not found",
6149 purify_type_name (name),
6150 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6151 break;
6152 case JDEP_FIELD:
6153 parse_error_context
6154 (JDEP_WFL (dep), "Type %qs not found in declaration of field %qs",
6155 purify_type_name (name),
6156 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6157 break;
6158 case JDEP_METHOD: /* Covers arguments */
6159 parse_error_context
6160 (JDEP_WFL (dep), "Type %qs not found in the declaration of the argument %qs of method %qs",
6161 purify_type_name (name),
6162 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6163 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6164 break;
6165 case JDEP_METHOD_RETURN: /* Covers return type */
6166 parse_error_context
6167 (JDEP_WFL (dep), "Type %qs not found in the declaration of the return type of method %qs",
6168 purify_type_name (name),
6169 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6170 break;
6171 case JDEP_INTERFACE:
6172 parse_error_context
6173 (JDEP_WFL (dep), "Superinterface %qs of %s %qs not found",
6174 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6175 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6176 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6177 break;
6178 case JDEP_VARIABLE:
6179 parse_error_context
6180 (JDEP_WFL (dep), "Type %qs not found in the declaration of the local variable %qs",
6181 purify_type_name (IDENTIFIER_POINTER
6182 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6183 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6184 break;
6185 case JDEP_EXCEPTION: /* As specified by `throws' */
6186 parse_error_context
6187 (JDEP_WFL (dep), "Class %qs not found in %<throws%>",
6188 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6189 break;
6190 default:
6191 /* Fix for -Wall. Just break doing nothing. The error will be
6192 caught later */
6193 break;
6197 /* Return a static string containing the DECL prototype string. If
6198 DECL is a constructor, use the class name instead of the form
6199 <init> */
6201 static const char *
6202 get_printable_method_name (tree decl)
6204 const char *to_return;
6205 tree name = NULL_TREE;
6207 if (DECL_CONSTRUCTOR_P (decl))
6209 name = DECL_NAME (decl);
6210 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6213 to_return = lang_printable_name (decl, 2);
6214 if (DECL_CONSTRUCTOR_P (decl))
6215 DECL_NAME (decl) = name;
6217 return to_return;
6220 /* Track method being redefined inside the same class. As a side
6221 effect, set DECL_NAME to an IDENTIFIER (prior entering this
6222 function it's a FWL, so we can track errors more accurately.) */
6224 static int
6225 check_method_redefinition (tree class, tree method)
6227 tree redef, sig;
6229 /* There's no need to verify <clinit> and finit$ and instinit$ */
6230 if (DECL_CLINIT_P (method)
6231 || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6232 return 0;
6234 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6235 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6237 if (redef == method)
6238 break;
6239 if (DECL_NAME (redef) == DECL_NAME (method)
6240 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6241 && !DECL_ARTIFICIAL (method))
6243 parse_error_context
6244 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration %qs",
6245 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6246 get_printable_method_name (redef));
6247 return 1;
6250 return 0;
6253 /* Return 1 if check went ok, 0 otherwise. */
6254 static int
6255 check_abstract_method_definitions (int do_interface, tree class_decl,
6256 tree type)
6258 tree class = TREE_TYPE (class_decl);
6259 tree method, end_type;
6260 int ok = 1;
6262 end_type = (do_interface ? object_type_node : type);
6263 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6265 tree other_super, other_method, method_sig, method_name;
6266 int found = 0;
6267 int end_type_reached = 0;
6269 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6270 continue;
6272 /* Now verify that somewhere in between TYPE and CLASS,
6273 abstract method METHOD gets a non abstract definition
6274 that is inherited by CLASS. */
6276 method_sig = build_java_signature (TREE_TYPE (method));
6277 method_name = DECL_NAME (method);
6278 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6279 method_name = EXPR_WFL_NODE (method_name);
6281 other_super = class;
6282 do {
6283 if (other_super == end_type)
6284 end_type_reached = 1;
6286 /* Method search */
6287 for (other_method = TYPE_METHODS (other_super); other_method;
6288 other_method = TREE_CHAIN (other_method))
6290 tree s = build_java_signature (TREE_TYPE (other_method));
6291 tree other_name = DECL_NAME (other_method);
6293 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6294 other_name = EXPR_WFL_NODE (other_name);
6295 if (!DECL_CLINIT_P (other_method)
6296 && !DECL_CONSTRUCTOR_P (other_method)
6297 && method_name == other_name
6298 && method_sig == s
6299 && !METHOD_ABSTRACT (other_method))
6301 found = 1;
6302 break;
6305 other_super = CLASSTYPE_SUPER (other_super);
6306 } while (!end_type_reached);
6308 /* Report that abstract METHOD didn't find an implementation
6309 that CLASS can use. */
6310 if (!found)
6312 char *t = xstrdup (lang_printable_name
6313 (TREE_TYPE (TREE_TYPE (method)), 0));
6314 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6316 parse_error_context
6317 (lookup_cl (class_decl),
6318 "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",
6319 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6320 t, lang_printable_name (method, 2),
6321 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6322 "interface" : "class"),
6323 IDENTIFIER_POINTER (ccn),
6324 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6325 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6326 ok = 0;
6327 free (t);
6331 if (ok && do_interface)
6333 /* Check for implemented interfaces. */
6334 int i;
6335 tree base_binfo;
6337 for (i = 1;
6338 ok && BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo);
6339 i++)
6340 ok = check_abstract_method_definitions (1, class_decl,
6341 BINFO_TYPE (base_binfo));
6344 return ok;
6347 /* Check that CLASS_DECL somehow implements all inherited abstract
6348 methods. */
6350 static void
6351 java_check_abstract_method_definitions (tree class_decl)
6353 tree class = TREE_TYPE (class_decl);
6354 tree super, base_binfo;
6355 int i;
6357 if (CLASS_ABSTRACT (class_decl))
6358 return;
6360 /* Check for inherited types */
6361 super = class;
6362 do {
6363 super = CLASSTYPE_SUPER (super);
6364 check_abstract_method_definitions (0, class_decl, super);
6365 } while (super != object_type_node);
6367 /* Check for implemented interfaces. */
6368 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6369 check_abstract_method_definitions (1, class_decl, BINFO_TYPE (base_binfo));
6372 /* Check all the types method DECL uses and return 1 if all of them
6373 are now complete, 0 otherwise. This is used to check whether its
6374 safe to build a method signature or not. */
6376 static int
6377 check_method_types_complete (tree decl)
6379 tree type = TREE_TYPE (decl);
6380 tree args;
6382 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6383 return 0;
6385 args = TYPE_ARG_TYPES (type);
6386 if (TREE_CODE (type) == METHOD_TYPE)
6387 args = TREE_CHAIN (args);
6388 for (; args != end_params_node; args = TREE_CHAIN (args))
6389 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6390 return 0;
6392 return 1;
6395 /* Visible interface to check methods contained in CLASS_DECL */
6397 void
6398 java_check_methods (tree class_decl)
6400 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6401 return;
6403 if (CLASS_INTERFACE (class_decl))
6404 java_check_abstract_methods (class_decl);
6405 else
6406 java_check_regular_methods (class_decl);
6408 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6411 /* Like not_accessible_p, but doesn't refer to the current class at
6412 all. */
6413 static bool
6414 hack_is_accessible_p (tree member, tree from_where)
6416 int flags = get_access_flags_from_decl (member);
6418 if (from_where == DECL_CONTEXT (member)
6419 || (flags & ACC_PUBLIC))
6420 return true;
6422 if ((flags & ACC_PROTECTED))
6424 if (inherits_from_p (from_where, DECL_CONTEXT (member)))
6425 return true;
6428 if ((flags & ACC_PRIVATE))
6429 return false;
6431 /* Package private, or protected. */
6432 return in_same_package (TYPE_NAME (from_where),
6433 TYPE_NAME (DECL_CONTEXT (member)));
6436 /* Check all the methods of CLASS_DECL. Methods are first completed
6437 then checked according to regular method existence rules. If no
6438 constructor for CLASS_DECL were encountered, then build its
6439 declaration. */
6440 static void
6441 java_check_regular_methods (tree class_decl)
6443 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6444 tree method;
6445 tree class = TREE_TYPE (class_decl);
6446 tree found = NULL_TREE;
6447 tree mthrows;
6449 /* It is not necessary to check methods defined in java.lang.Object */
6450 if (class == object_type_node)
6451 return;
6453 if (!TYPE_NVIRTUALS (class))
6454 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6456 /* Should take interfaces into account. FIXME */
6457 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6459 tree sig;
6460 tree method_wfl = DECL_FUNCTION_WFL (method);
6461 int aflags;
6463 /* Check for redefinitions */
6464 if (check_method_redefinition (class, method))
6465 continue;
6467 /* We verify things thrown by the method. They must inherit from
6468 java.lang.Throwable. */
6469 for (mthrows = DECL_FUNCTION_THROWS (method);
6470 mthrows; mthrows = TREE_CHAIN (mthrows))
6472 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6473 parse_error_context
6474 (TREE_PURPOSE (mthrows), "Class %qs in %<throws%> clause must be a subclass of class %<java.lang.Throwable%>",
6475 IDENTIFIER_POINTER
6476 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6479 /* If we see one constructor a mark so we don't generate the
6480 default one. Also skip other verifications: constructors
6481 can't be inherited hence hidden or overridden. */
6482 if (DECL_CONSTRUCTOR_P (method))
6484 saw_constructor = 1;
6485 continue;
6488 sig = build_java_argument_signature (TREE_TYPE (method));
6489 found = lookup_argument_method_generic (class, DECL_NAME (method), sig,
6490 SEARCH_SUPER | SEARCH_INTERFACE);
6492 /* Inner class can't declare static methods */
6493 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6495 char *t = xstrdup (lang_printable_name (class, 0));
6496 parse_error_context
6497 (method_wfl, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
6498 lang_printable_name (method, 2), t);
6499 free (t);
6502 /* Nothing overrides or it's a private method. */
6503 if (!found)
6504 continue;
6505 if (METHOD_PRIVATE (found))
6507 found = NULL_TREE;
6508 continue;
6511 /* If `found' is declared in an interface, make sure the
6512 modifier matches. */
6513 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6514 && clinit_identifier_node != DECL_NAME (found)
6515 && !METHOD_PUBLIC (method))
6517 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6518 parse_error_context (method_wfl, "Class %qs must override %qs with a public method in order to implement interface %qs",
6519 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6520 lang_printable_name (method, 2),
6521 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6524 /* Can't override a method with the same name and different return
6525 types. */
6526 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6528 char *t = xstrdup
6529 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6530 parse_error_context
6531 (method_wfl,
6532 "Method %qs was defined with return type %qs in class %qs",
6533 lang_printable_name (found, 2), t,
6534 IDENTIFIER_POINTER
6535 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6536 free (t);
6539 aflags = get_access_flags_from_decl (found);
6541 /* Can't override final. Can't override static. */
6542 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6544 /* Static *can* override static */
6545 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6546 continue;
6547 parse_error_context
6548 (method_wfl,
6549 "%s methods can't be overridden. Method %qs is %s in class %qs",
6550 (METHOD_FINAL (found) ? "Final" : "Static"),
6551 lang_printable_name (found, 2),
6552 (METHOD_FINAL (found) ? "final" : "static"),
6553 IDENTIFIER_POINTER
6554 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6555 continue;
6558 /* Static method can't override instance method. */
6559 if (METHOD_STATIC (method))
6561 parse_error_context
6562 (method_wfl,
6563 "Instance methods can't be overridden by a static method. Method %qs is an instance method in class %qs",
6564 lang_printable_name (found, 2),
6565 IDENTIFIER_POINTER
6566 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6567 continue;
6570 /* - Overriding/hiding public must be public
6571 - Overriding/hiding protected must be protected or public
6572 - If the overridden or hidden method has default (package)
6573 access, then the overriding or hiding method must not be
6574 private; otherwise, a compile-time error occurs. If
6575 `found' belongs to an interface, things have been already
6576 taken care of. */
6577 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6578 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6579 || (METHOD_PROTECTED (found)
6580 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6581 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6582 && METHOD_PRIVATE (method))))
6584 parse_error_context
6585 (method_wfl,
6586 "Methods can't be overridden to be more private. Method %qs is not %s in class %qs", lang_printable_name (method, 2),
6587 (METHOD_PUBLIC (method) ? "public" :
6588 (METHOD_PRIVATE (method) ? "private" : "protected")),
6589 IDENTIFIER_POINTER (DECL_NAME
6590 (TYPE_NAME (DECL_CONTEXT (found)))));
6591 continue;
6594 /* Check this method against all the other implementations it
6595 overrides. Here we only check the class hierarchy; the rest
6596 of the checking is done later. If this method is just a
6597 Miranda method, we can skip the check. */
6598 if (! METHOD_INVISIBLE (method))
6599 check_concrete_throws_clauses (class, method, DECL_NAME (method), sig);
6602 /* The above throws clause check only looked at superclasses. Now
6603 we must also make sure that all methods declared in interfaces
6604 have compatible throws clauses. FIXME: there are more efficient
6605 ways to organize this checking; we should implement one. */
6606 check_interface_throws_clauses (class, class);
6608 if (!TYPE_NVIRTUALS (class))
6609 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6611 /* Search for inherited abstract method not yet implemented in this
6612 class. */
6613 java_check_abstract_method_definitions (class_decl);
6615 if (!saw_constructor)
6616 abort ();
6619 /* Check to make sure that all the methods in all the interfaces
6620 implemented by CLASS_DECL are compatible with the concrete
6621 implementations available in CHECK_CLASS_DECL. */
6622 static void
6623 check_interface_throws_clauses (tree check_class_decl, tree class_decl)
6625 for (; class_decl != NULL_TREE; class_decl = CLASSTYPE_SUPER (class_decl))
6627 int i;
6629 if (! CLASS_LOADED_P (class_decl))
6631 if (CLASS_FROM_SOURCE_P (class_decl))
6632 safe_layout_class (class_decl);
6633 else
6634 load_class (class_decl, 1);
6637 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (class_decl)) - 1; i > 0; --i)
6639 tree interface
6640 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (class_decl), i));
6641 tree iface_method;
6643 for (iface_method = TYPE_METHODS (interface);
6644 iface_method != NULL_TREE;
6645 iface_method = TREE_CHAIN (iface_method))
6647 tree sig, method;
6649 /* First look for a concrete method implemented or
6650 inherited by this class. No need to search
6651 interfaces here, since we're already looking through
6652 all of them. */
6653 sig = build_java_argument_signature (TREE_TYPE (iface_method));
6654 method
6655 = lookup_argument_method_generic (check_class_decl,
6656 DECL_NAME (iface_method),
6657 sig, SEARCH_VISIBLE);
6658 /* If we don't find an implementation, that is ok. Any
6659 potential errors from that are diagnosed elsewhere.
6660 Also, multiple inheritance with conflicting throws
6661 clauses is fine in the absence of a concrete
6662 implementation. */
6663 if (method != NULL_TREE && !METHOD_ABSTRACT (method)
6664 && !METHOD_INVISIBLE (iface_method))
6666 tree method_wfl = DECL_FUNCTION_WFL (method);
6667 check_throws_clauses (method, method_wfl, iface_method);
6671 /* Now check superinterfaces. */
6672 check_interface_throws_clauses (check_class_decl, interface);
6677 /* Check throws clauses of a method against the clauses of all the
6678 methods it overrides. We do this by searching up the class
6679 hierarchy, examining all matching accessible methods. */
6680 static void
6681 check_concrete_throws_clauses (tree class, tree self_method,
6682 tree name, tree signature)
6684 tree method = lookup_argument_method_generic (class, name, signature,
6685 SEARCH_SUPER | SEARCH_VISIBLE);
6686 while (method != NULL_TREE)
6688 if (! METHOD_INVISIBLE (method) && hack_is_accessible_p (method, class))
6689 check_throws_clauses (self_method, DECL_FUNCTION_WFL (self_method),
6690 method);
6692 method = lookup_argument_method_generic (DECL_CONTEXT (method),
6693 name, signature,
6694 SEARCH_SUPER | SEARCH_VISIBLE);
6698 /* Generate an error if the `throws' clause of METHOD (if any) is
6699 incompatible with the `throws' clause of FOUND (if any). */
6700 static void
6701 check_throws_clauses (tree method, tree method_wfl, tree found)
6703 tree mthrows;
6705 for (mthrows = DECL_FUNCTION_THROWS (method);
6706 mthrows; mthrows = TREE_CHAIN (mthrows))
6708 tree fthrows;
6710 /* We don't verify unchecked expressions */
6711 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6712 continue;
6713 /* Checked expression must be compatible */
6714 for (fthrows = DECL_FUNCTION_THROWS (found);
6715 fthrows; fthrows = TREE_CHAIN (fthrows))
6717 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6718 break;
6720 if (!fthrows)
6722 parse_error_context
6723 (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",
6724 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6725 lang_printable_name (found, 2),
6726 IDENTIFIER_POINTER
6727 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6732 /* Check abstract method of interface INTERFACE */
6733 static void
6734 java_check_abstract_methods (tree interface_decl)
6736 int i;
6737 tree method, found;
6738 tree interface = TREE_TYPE (interface_decl);
6739 tree base_binfo;
6741 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6743 /* 2- Check for double definition inside the defining interface */
6744 if (check_method_redefinition (interface, method))
6745 continue;
6747 /* 3- Overriding is OK as far as we preserve the return type. */
6748 found = lookup_java_interface_method2 (interface, method);
6749 if (found)
6751 char *t;
6752 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6753 parse_error_context
6754 (DECL_FUNCTION_WFL (found),
6755 "Method %qs was defined with return type %qs in class %qs",
6756 lang_printable_name (found, 2), t,
6757 IDENTIFIER_POINTER
6758 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6759 free (t);
6760 continue;
6764 /* 4- Inherited methods can't differ by their returned types */
6765 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (interface), i, base_binfo); i++)
6767 tree sub_interface_method, sub_interface;
6769 sub_interface = BINFO_TYPE (base_binfo);
6770 for (sub_interface_method = TYPE_METHODS (sub_interface);
6771 sub_interface_method;
6772 sub_interface_method = TREE_CHAIN (sub_interface_method))
6774 found = lookup_java_interface_method2 (interface,
6775 sub_interface_method);
6776 if (found && (found != sub_interface_method))
6778 parse_error_context
6779 (lookup_cl (sub_interface_method),
6780 "Interface %qs inherits method %qs from interface %qs. This method is redefined with a different return type in interface %qs",
6781 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6782 lang_printable_name (found, 2),
6783 IDENTIFIER_POINTER
6784 (DECL_NAME (TYPE_NAME
6785 (DECL_CONTEXT (sub_interface_method)))),
6786 IDENTIFIER_POINTER
6787 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6793 /* Lookup methods in interfaces using their name and partial
6794 signature. Return a matching method only if their types differ. */
6796 static tree
6797 lookup_java_interface_method2 (tree class, tree method_decl)
6799 int i;
6800 tree base_binfo;
6801 tree to_return;
6803 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6805 if ((BINFO_TYPE (base_binfo) != object_type_node)
6806 && (to_return =
6807 lookup_java_method2 (BINFO_TYPE (base_binfo), method_decl, 1)))
6808 return to_return;
6810 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6812 to_return = lookup_java_interface_method2
6813 (BINFO_TYPE (base_binfo), method_decl);
6814 if (to_return)
6815 return to_return;
6818 return NULL_TREE;
6821 /* Lookup method using their name and partial signature. Return a
6822 matching method only if their types differ. */
6824 static tree
6825 lookup_java_method2 (tree clas, tree method_decl, int do_interface)
6827 tree method, method_signature, method_name, method_type, name;
6829 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6830 name = DECL_NAME (method_decl);
6831 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6832 EXPR_WFL_NODE (name) : name);
6833 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6835 while (clas != NULL_TREE)
6837 for (method = TYPE_METHODS (clas);
6838 method != NULL_TREE; method = TREE_CHAIN (method))
6840 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6841 tree name = DECL_NAME (method);
6842 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6843 EXPR_WFL_NODE (name) : name) == method_name
6844 && method_sig == method_signature
6845 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6846 return method;
6848 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6850 return NULL_TREE;
6853 /* Return the line that matches DECL line number, and try its best to
6854 position the column number. Used during error reports.
6855 FUTURE/FIXME: return source_location instead of node. */
6857 static GTY(()) tree cl_v;
6858 static tree
6859 lookup_cl (tree decl)
6861 #ifndef USE_MAPPED_LOCATION
6862 char *line, *found;
6863 #endif
6865 if (!decl)
6866 return NULL_TREE;
6868 if (cl_v == NULL_TREE)
6870 cl_v = build_unknown_wfl (NULL_TREE);
6873 #ifdef USE_MAPPED_LOCATION
6874 SET_EXPR_LOCATION (cl_v, DECL_SOURCE_LOCATION (decl));
6875 #else
6876 EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6877 EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE (decl), -1);
6879 line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6880 EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6882 found = strstr ((const char *)line,
6883 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6884 if (found)
6885 EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6886 #endif
6888 return cl_v;
6891 /* Look for a simple name in the single-type import list */
6893 static tree
6894 find_name_in_single_imports (tree name)
6896 tree node;
6898 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6899 if (TREE_VALUE (node) == name)
6900 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6902 return NULL_TREE;
6905 /* Process all single-type import. */
6907 static int
6908 process_imports (void)
6910 tree import;
6911 int error_found;
6913 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6915 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6916 char *original_name;
6918 /* Don't load twice something already defined. */
6919 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6920 continue;
6922 original_name = xmemdup (IDENTIFIER_POINTER (to_be_found),
6923 IDENTIFIER_LENGTH (to_be_found),
6924 IDENTIFIER_LENGTH (to_be_found) + 1);
6926 while (1)
6928 tree left;
6930 QUALIFIED_P (to_be_found) = 1;
6931 load_class (to_be_found, 0);
6932 error_found =
6933 check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true, NULL_TREE);
6935 /* We found it, we can bail out */
6936 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6938 check_deprecation (TREE_PURPOSE (import),
6939 IDENTIFIER_CLASS_VALUE (to_be_found));
6940 break;
6943 /* We haven't found it. Maybe we're trying to access an
6944 inner class. The only way for us to know is to try again
6945 after having dropped a qualifier. If we can't break it further,
6946 we have an error. */
6947 if (split_qualified_name (&left, NULL, to_be_found))
6948 break;
6950 to_be_found = left;
6952 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6954 parse_error_context (TREE_PURPOSE (import),
6955 "Class or interface %qs not found in import",
6956 original_name);
6957 error_found = 1;
6960 free (original_name);
6961 if (error_found)
6962 return 1;
6964 return 0;
6967 /* Possibly find and mark a class imported by a single-type import
6968 statement. */
6970 static void
6971 find_in_imports (tree enclosing_type, tree class_type)
6973 tree import = (enclosing_type ? TYPE_IMPORT_LIST (enclosing_type) :
6974 ctxp->import_list);
6975 while (import)
6977 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6979 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6980 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6981 return;
6983 import = TREE_CHAIN (import);
6987 static int
6988 note_possible_classname (const char *name, int len)
6990 tree node;
6991 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6992 len = len - 5;
6993 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6994 len = len - 6;
6995 else
6996 return 0;
6997 node = ident_subst (name, len, "", '/', '.', "");
6998 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6999 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
7000 return 1;
7003 /* Read a import directory, gathering potential match for further type
7004 references. Indifferently reads a filesystem or a ZIP archive
7005 directory. */
7007 static void
7008 read_import_dir (tree wfl)
7010 tree package_id = EXPR_WFL_NODE (wfl);
7011 const char *package_name = IDENTIFIER_POINTER (package_id);
7012 int package_length = IDENTIFIER_LENGTH (package_id);
7013 DIR *dirp = NULL;
7014 JCF *saved_jcf = current_jcf;
7016 int found = 0;
7017 int k;
7018 void *entry;
7019 struct buffer filename[1];
7021 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
7022 return;
7023 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
7025 BUFFER_INIT (filename);
7026 buffer_grow (filename, package_length + 100);
7028 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
7030 const char *entry_name = jcf_path_name (entry);
7031 int entry_length = strlen (entry_name);
7032 if (jcf_path_is_zipfile (entry))
7034 ZipFile *zipf;
7035 buffer_grow (filename, entry_length);
7036 memcpy (filename->data, entry_name, entry_length - 1);
7037 filename->data[entry_length-1] = '\0';
7038 zipf = opendir_in_zip ((const char *) filename->data, jcf_path_is_system (entry));
7039 if (zipf == NULL)
7040 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
7041 else
7043 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
7044 BUFFER_RESET (filename);
7045 for (k = 0; k < package_length; k++)
7047 char ch = package_name[k];
7048 *filename->ptr++ = ch == '.' ? '/' : ch;
7050 *filename->ptr++ = '/';
7052 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
7054 const char *current_entry = ZIPDIR_FILENAME (zipd);
7055 int current_entry_len = zipd->filename_length;
7057 if (current_entry_len >= BUFFER_LENGTH (filename)
7058 && strncmp ((const char *) filename->data, current_entry,
7059 BUFFER_LENGTH (filename)) != 0)
7060 continue;
7061 found |= note_possible_classname (current_entry,
7062 current_entry_len);
7066 else
7068 BUFFER_RESET (filename);
7069 buffer_grow (filename, entry_length + package_length + 4);
7070 strcpy ((char *) filename->data, entry_name);
7071 filename->ptr = filename->data + entry_length;
7072 for (k = 0; k < package_length; k++)
7074 char ch = package_name[k];
7075 *filename->ptr++ = ch == '.' ? '/' : ch;
7077 *filename->ptr = '\0';
7079 dirp = opendir ((const char *) filename->data);
7080 if (dirp == NULL)
7081 continue;
7082 *filename->ptr++ = '/';
7083 for (;;)
7085 int len;
7086 const char *d_name;
7087 struct dirent *direntp = readdir (dirp);
7088 if (!direntp)
7089 break;
7090 d_name = direntp->d_name;
7091 len = strlen (direntp->d_name);
7092 buffer_grow (filename, len+1);
7093 strcpy ((char *) filename->ptr, d_name);
7094 found |= note_possible_classname ((const char *) filename->data + entry_length,
7095 package_length+len+1);
7097 if (dirp)
7098 closedir (dirp);
7102 free (filename->data);
7104 /* Here we should have a unified way of retrieving an entry, to be
7105 indexed. */
7106 if (!found)
7108 static int first = 1;
7109 if (first)
7111 error ("Can't find default package %qs. Check the CLASSPATH environment variable and the access to the archives", package_name);
7112 java_error_count++;
7113 first = 0;
7115 else
7116 parse_error_context (wfl, "Package %qs not found in import",
7117 package_name);
7118 current_jcf = saved_jcf;
7119 return;
7121 current_jcf = saved_jcf;
7124 /* Possibly find a type in the import on demands specified
7125 types. Returns 1 if an error occurred, 0 otherwise. Run through the
7126 entire list, to detected potential double definitions. */
7128 static int
7129 find_in_imports_on_demand (tree enclosing_type, tree class_type)
7131 tree class_type_name = TYPE_NAME (class_type);
7132 tree import = (enclosing_type ? TYPE_IMPORT_DEMAND_LIST (enclosing_type) :
7133 ctxp->import_demand_list);
7134 tree cl = NULL_TREE;
7135 int seen_once = -1; /* -1 when not set, 1 if seen once, >1 otherwise. */
7136 int to_return = -1; /* -1 when not set, 0 or 1 otherwise */
7137 tree node;
7139 for (; import; import = TREE_CHAIN (import))
7141 location_t saved_location = input_location;
7142 int access_check;
7143 const char *id_name;
7144 tree decl, type_name_copy;
7146 obstack_grow (&temporary_obstack,
7147 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
7148 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
7149 obstack_1grow (&temporary_obstack, '.');
7150 obstack_grow0 (&temporary_obstack,
7151 IDENTIFIER_POINTER (class_type_name),
7152 IDENTIFIER_LENGTH (class_type_name));
7153 id_name = obstack_finish (&temporary_obstack);
7155 if (! (node = maybe_get_identifier (id_name)))
7156 continue;
7158 /* Setup input_line so that it refers to the line of the import (in
7159 case we parse a class file and encounter errors */
7160 #ifdef USE_MAPPED_LOCATION
7161 input_location = EXPR_LOCATION (TREE_PURPOSE (import));
7162 #else
7163 input_line = EXPR_WFL_LINENO (TREE_PURPOSE (import));
7164 #endif
7166 type_name_copy = TYPE_NAME (class_type);
7167 TYPE_NAME (class_type) = node;
7168 QUALIFIED_P (node) = 1;
7169 decl = IDENTIFIER_CLASS_VALUE (node);
7170 access_check = -1;
7171 /* If there is no DECL set for the class or if the class isn't
7172 loaded and not seen in source yet, then load */
7173 if (!decl || ! CLASS_LOADED_P (TREE_TYPE (decl)))
7175 load_class (node, 0);
7176 decl = IDENTIFIER_CLASS_VALUE (node);
7178 if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7179 access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7180 false, NULL_TREE);
7181 else
7182 /* 6.6.1: Inner classes are subject to member access rules. */
7183 access_check = 0;
7185 input_location = saved_location;
7187 /* If the loaded class is not accessible or couldn't be loaded,
7188 we restore the original TYPE_NAME and process the next
7189 import. */
7190 if (access_check || !decl)
7192 TYPE_NAME (class_type) = type_name_copy;
7193 continue;
7196 /* If the loaded class is accessible, we keep a tab on it to
7197 detect and report multiple inclusions. */
7198 if (IS_A_CLASSFILE_NAME (node))
7200 if (seen_once < 0)
7202 cl = TREE_PURPOSE (import);
7203 seen_once = 1;
7205 else if (seen_once >= 0)
7207 tree location = (cl ? cl : TREE_PURPOSE (import));
7208 tree package = (cl ? EXPR_WFL_NODE (cl) :
7209 EXPR_WFL_NODE (TREE_PURPOSE (import)));
7210 seen_once++;
7211 parse_error_context
7212 (location,
7213 "Type %qs also potentially defined in package %qs",
7214 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7215 IDENTIFIER_POINTER (package));
7218 to_return = access_check;
7221 if (seen_once == 1)
7222 return to_return;
7223 else
7224 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7227 /* Add package NAME to the list of packages encountered so far. To
7228 speed up class lookup in do_resolve_class, we make sure a
7229 particular package is added only once. */
7231 static void
7232 register_package (tree name)
7234 static htab_t pht;
7235 void **e;
7237 if (pht == NULL)
7238 pht = htab_create (50, htab_hash_pointer, htab_eq_pointer, NULL);
7240 e = htab_find_slot (pht, name, INSERT);
7241 if (*e == NULL)
7243 package_list = chainon (package_list, build_tree_list (name, NULL));
7244 *e = name;
7248 static tree
7249 resolve_package (tree pkg, tree *next, tree *type_name)
7251 tree current;
7252 tree decl = NULL_TREE;
7253 *type_name = NULL_TREE;
7255 /* The trick is to determine when the package name stops and were
7256 the name of something contained in the package starts. Then we
7257 return a fully qualified name of what we want to get. */
7259 *next = EXPR_WFL_QUALIFICATION (pkg);
7261 /* Try to progressively construct a type name */
7262 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7263 for (current = EXPR_WFL_QUALIFICATION (pkg);
7264 current; current = TREE_CHAIN (current))
7266 /* If we don't have what we're expecting, exit now. TYPE_NAME
7267 will be null and the error caught later. */
7268 if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7269 break;
7270 *type_name =
7271 merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7272 if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7274 /* resolve_package should be used in a loop, hence we
7275 point at this one to naturally process the next one at
7276 the next iteration. */
7277 *next = current;
7278 break;
7281 return decl;
7285 /* Check accessibility of inner classes according to member access rules.
7286 DECL is the inner class, ENCLOSING_DECL is the class from which the
7287 access is being attempted. */
7289 static void
7290 check_inner_class_access (tree decl, tree enclosing_decl, tree cl)
7292 const char *access;
7293 tree enclosing_decl_type;
7295 /* We don't issue an error message when CL is null. CL can be null
7296 as a result of processing a JDEP crafted by source_start_java_method
7297 for the purpose of patching its parm decl. But the error would
7298 have been already trapped when fixing the method's signature.
7299 DECL can also be NULL in case of earlier errors. */
7300 if (!decl || !cl)
7301 return;
7303 enclosing_decl_type = TREE_TYPE (enclosing_decl);
7305 if (CLASS_PRIVATE (decl))
7307 /* Access is permitted only within the body of the top-level
7308 class in which DECL is declared. */
7309 tree top_level = decl;
7310 while (DECL_CONTEXT (top_level))
7311 top_level = DECL_CONTEXT (top_level);
7312 while (DECL_CONTEXT (enclosing_decl))
7313 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7314 if (top_level == enclosing_decl)
7315 return;
7316 access = "private";
7318 else if (CLASS_PROTECTED (decl))
7320 tree decl_context;
7321 /* Access is permitted from within the same package... */
7322 if (in_same_package (decl, enclosing_decl))
7323 return;
7325 /* ... or from within the body of a subtype of the context in which
7326 DECL is declared. */
7327 decl_context = DECL_CONTEXT (decl);
7328 while (enclosing_decl)
7330 if (CLASS_INTERFACE (decl))
7332 if (interface_of_p (TREE_TYPE (decl_context),
7333 enclosing_decl_type))
7334 return;
7336 else
7338 /* Eww. The order of the arguments is different!! */
7339 if (inherits_from_p (enclosing_decl_type,
7340 TREE_TYPE (decl_context)))
7341 return;
7343 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7345 access = "protected";
7347 else if (! CLASS_PUBLIC (decl))
7349 /* Access is permitted only from within the same package as DECL. */
7350 if (in_same_package (decl, enclosing_decl))
7351 return;
7352 access = "non-public";
7354 else
7355 /* Class is public. */
7356 return;
7358 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7359 (CLASS_INTERFACE (decl) ? "interface" : "class"),
7360 lang_printable_name (decl, 2), access);
7363 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7364 foreign package, it must be PUBLIC. Return 0 if no access
7365 violations were found, 1 otherwise. If VERBOSE is true and an error
7366 was found, it is reported and accounted for. If CL is NULL then
7367 look it up with THIS_DECL. */
7369 static int
7370 check_pkg_class_access (tree class_name, tree cl, bool verbose, tree this_decl)
7372 tree type;
7374 if (!IDENTIFIER_CLASS_VALUE (class_name))
7375 return 0;
7377 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7378 return 0;
7380 if (!CLASS_PUBLIC (TYPE_NAME (type)))
7382 /* Access to a private class within the same package is
7383 allowed. */
7384 tree l, r;
7385 split_qualified_name (&l, &r, class_name);
7386 if (!QUALIFIED_P (class_name) && !ctxp->package)
7387 /* Both in the empty package. */
7388 return 0;
7389 if (l == ctxp->package)
7390 /* Both in the same package. */
7391 return 0;
7393 if (verbose)
7394 parse_error_context
7395 (cl == NULL ? lookup_cl (this_decl): cl,
7396 "Can't access %s %qs. Only public classes and interfaces in other packages can be accessed",
7397 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7398 IDENTIFIER_POINTER (class_name));
7399 return 1;
7401 return 0;
7404 /* Local variable declaration. */
7406 static void
7407 declare_local_variables (int modifier, tree type, tree vlist)
7409 tree decl, current, saved_type;
7410 tree type_wfl = NULL_TREE;
7411 int must_chain = 0;
7412 int final_p = 0;
7414 /* Push a new block if statements were seen between the last time we
7415 pushed a block and now. Keep a count of blocks to close */
7416 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7418 tree b = enter_block ();
7419 BLOCK_IS_IMPLICIT (b) = 1;
7422 if (modifier)
7424 size_t i;
7425 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7426 if (1 << i & modifier)
7427 break;
7428 if (modifier == ACC_FINAL)
7429 final_p = 1;
7430 else
7432 parse_error_context
7433 (ctxp->modifier_ctx [i],
7434 "Only %<final%> is allowed as a local variables modifier");
7435 return;
7439 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7440 hold the TYPE value if a new incomplete has to be created (as
7441 opposed to being found already existing and reused). */
7442 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7444 /* If TYPE is fully resolved and we don't have a reference, make one */
7445 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7447 /* Go through all the declared variables */
7448 for (current = vlist, saved_type = type; current;
7449 current = TREE_CHAIN (current), type = saved_type)
7451 tree other, real_type;
7452 tree wfl = TREE_PURPOSE (current);
7453 tree name = EXPR_WFL_NODE (wfl);
7454 tree init = TREE_VALUE (current);
7456 /* Process NAME, as it may specify extra dimension(s) for it */
7457 type = build_array_from_name (type, type_wfl, name, &name);
7459 /* Variable redefinition check */
7460 if ((other = lookup_name_in_blocks (name)))
7462 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7463 DECL_SOURCE_LINE (other));
7464 continue;
7467 /* Type adjustment. We may have just readjusted TYPE because
7468 the variable specified more dimensions. Make sure we have
7469 a reference if we can and don't have one already. */
7470 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7472 real_type = GET_REAL_TYPE (type);
7473 /* Never layout this decl. This will be done when its scope
7474 will be entered */
7475 decl = build_decl (VAR_DECL, name, real_type);
7476 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7477 DECL_FINAL (decl) = final_p;
7478 BLOCK_CHAIN_DECL (decl);
7480 /* Don't try to use an INIT statement when an error was found */
7481 if (init && java_error_count)
7482 init = NULL_TREE;
7484 /* Remember it if this is an initialized-upon-declaration final
7485 variable. */
7486 if (init && final_p)
7488 DECL_LOCAL_FINAL_IUD (decl) = 1;
7491 /* Add the initialization function to the current function's code */
7492 if (init)
7494 /* Name might have been readjusted */
7495 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7496 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7497 java_method_add_stmt (current_function_decl,
7498 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7499 init));
7502 /* Setup dependency the type of the decl */
7503 if (must_chain)
7505 jdep *dep;
7506 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7507 dep = CLASSD_LAST (ctxp->classd_list);
7508 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7511 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7514 /* Called during parsing. Build decls from argument list. */
7516 static void
7517 source_start_java_method (tree fndecl)
7519 tree tem;
7520 tree parm_decl;
7521 int i;
7523 if (!fndecl)
7524 return;
7526 current_function_decl = fndecl;
7528 /* New scope for the function */
7529 enter_block ();
7530 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7531 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7533 tree type = TREE_VALUE (tem);
7534 tree name = TREE_PURPOSE (tem);
7536 /* If type is incomplete. Create an incomplete decl and ask for
7537 the decl to be patched later */
7538 if (INCOMPLETE_TYPE_P (type))
7540 jdep *jdep;
7541 tree real_type = GET_REAL_TYPE (type);
7542 parm_decl = build_decl (PARM_DECL, name, real_type);
7543 type = obtain_incomplete_type (type);
7544 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7545 jdep = CLASSD_LAST (ctxp->classd_list);
7546 JDEP_MISC (jdep) = name;
7547 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7549 else
7550 parm_decl = build_decl (PARM_DECL, name, type);
7552 /* Remember if a local variable was declared final (via its
7553 TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7554 if (ARG_FINAL_P (tem))
7556 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7557 DECL_FINAL (parm_decl) = 1;
7560 BLOCK_CHAIN_DECL (parm_decl);
7562 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7563 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7564 nreverse (tem);
7565 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7566 DECL_MAX_LOCALS (current_function_decl) = i;
7569 /* Called during parsing. Creates an artificial method declaration. */
7571 static tree
7572 create_artificial_method (tree class, int flags, tree type,
7573 tree name, tree args)
7575 tree mdecl;
7576 location_t save_location = input_location;
7578 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (class));
7579 mdecl = make_node (FUNCTION_TYPE);
7580 TREE_TYPE (mdecl) = type;
7581 TYPE_ARG_TYPES (mdecl) = args;
7582 /* We used to compute the signature of MDECL here and then use
7583 add_method(), but that failed because our caller might modify
7584 the type of the returned method, which trashes the cache in
7585 get_type_from_signature(). */
7586 mdecl = add_method_1 (class, flags, name, mdecl);
7587 input_location = save_location;
7588 DECL_ARTIFICIAL (mdecl) = 1;
7589 return mdecl;
7592 /* Starts the body if an artificial method. */
7594 static void
7595 start_artificial_method_body (tree mdecl)
7597 #ifdef USE_MAPPED_LOCATION
7598 DECL_SOURCE_LOCATION (mdecl) = ctxp->file_start_location;
7599 DECL_FUNCTION_LAST_LINE (mdecl) = ctxp->file_start_location;
7600 #else
7601 DECL_SOURCE_LINE (mdecl) = 1;
7602 DECL_FUNCTION_LAST_LINE (mdecl) = 1;
7603 #endif
7604 source_start_java_method (mdecl);
7605 enter_block ();
7608 static void
7609 end_artificial_method_body (tree mdecl)
7611 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7612 It has to be evaluated first. (if mdecl is current_function_decl,
7613 we have an undefined behavior if no temporary variable is used.) */
7614 tree b = exit_block ();
7615 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7616 exit_block ();
7619 /* Dump a tree of some kind. This is a convenience wrapper for the
7620 dump_* functions in tree-dump.c. */
7621 static void
7622 dump_java_tree (enum tree_dump_index phase, tree t)
7624 FILE *stream;
7625 int flags;
7627 stream = dump_begin (phase, &flags);
7628 flags |= TDF_SLIM;
7629 if (stream)
7631 dump_node (t, flags, stream);
7632 dump_end (phase, stream);
7636 /* Terminate a function and expand its body. */
7638 static void
7639 source_end_java_method (void)
7641 tree fndecl = current_function_decl;
7643 if (!fndecl)
7644 return;
7646 java_parser_context_save_global ();
7647 #ifdef USE_MAPPED_LOCATION
7648 input_location = ctxp->last_ccb_indent1;
7649 #else
7650 input_line = ctxp->last_ccb_indent1;
7651 #endif
7653 /* Turn function bodies with only a NOP expr null, so they don't get
7654 generated at all and we won't get warnings when using the -W
7655 -Wall flags. */
7656 if (IS_EMPTY_STMT (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))))
7657 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7659 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7660 && ! flag_emit_class_files)
7661 finish_method (fndecl);
7663 current_function_decl = NULL_TREE;
7664 java_parser_context_restore_global ();
7665 current_function_decl = NULL_TREE;
7668 /* Record EXPR in the current function block. Complements compound
7669 expression second operand if necessary. */
7671 tree
7672 java_method_add_stmt (tree fndecl, tree expr)
7674 if (!GET_CURRENT_BLOCK (fndecl))
7675 return NULL_TREE;
7676 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7679 static tree
7680 add_stmt_to_block (tree b, tree type, tree stmt)
7682 tree body = BLOCK_EXPR_BODY (b), c;
7684 if (java_error_count)
7685 return body;
7687 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7688 return body;
7690 BLOCK_EXPR_BODY (b) = c;
7691 TREE_SIDE_EFFECTS (c) = 1;
7692 return c;
7695 /* Lays out the methods for the classes seen so far. */
7697 void
7698 java_layout_seen_class_methods (void)
7700 tree previous_list = all_class_list;
7701 tree end = NULL_TREE;
7702 tree current;
7704 while (1)
7706 for (current = previous_list;
7707 current != end; current = TREE_CHAIN (current))
7709 tree decl = TREE_VALUE (current);
7710 tree cls = TREE_TYPE (decl);
7712 input_location = DECL_SOURCE_LOCATION (decl);
7714 if (! CLASS_LOADED_P (cls))
7715 load_class (cls, 0);
7717 layout_class_methods (cls);
7720 /* Note that new classes might have been added while laying out
7721 methods, changing the value of all_class_list. */
7723 if (previous_list != all_class_list)
7725 end = previous_list;
7726 previous_list = all_class_list;
7728 else
7729 break;
7733 static GTY(()) tree stop_reordering;
7734 void
7735 java_reorder_fields (void)
7737 tree current;
7739 for (current = gclass_list; current; current = TREE_CHAIN (current))
7741 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7743 if (current_class == stop_reordering)
7744 break;
7746 /* Reverse the fields, but leave the dummy field in front.
7747 Fields are already ordered for Object and Class */
7748 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7749 && current_class != class_type_node)
7751 /* If the dummy field is there, reverse the right fields and
7752 just layout the type for proper fields offset */
7753 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7755 tree fields = TYPE_FIELDS (current_class);
7756 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7757 TYPE_SIZE (current_class) = NULL_TREE;
7759 /* We don't have a dummy field, we need to layout the class,
7760 after having reversed the fields */
7761 else
7763 TYPE_FIELDS (current_class) =
7764 nreverse (TYPE_FIELDS (current_class));
7765 TYPE_SIZE (current_class) = NULL_TREE;
7769 /* There are cases were gclass_list will be empty. */
7770 if (gclass_list)
7771 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7774 /* Layout the methods of all classes loaded in one way or another.
7775 Check methods of source parsed classes. Then reorder the
7776 fields and layout the classes or the type of all source parsed
7777 classes */
7779 void
7780 java_layout_classes (void)
7782 tree current;
7783 int save_error_count = java_error_count;
7785 /* Layout the methods of all classes seen so far */
7786 java_layout_seen_class_methods ();
7787 java_parse_abort_on_error ();
7788 all_class_list = NULL_TREE;
7790 /* Then check the methods of all parsed classes */
7791 for (current = gclass_list; current; current = TREE_CHAIN (current))
7792 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7793 java_check_methods (TREE_VALUE (current));
7794 java_parse_abort_on_error ();
7796 for (current = gclass_list; current; current = TREE_CHAIN (current))
7798 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7799 layout_class (current_class);
7801 /* Error reported by the caller */
7802 if (java_error_count)
7803 return;
7806 /* We might have reloaded classes durign the process of laying out
7807 classes for code generation. We must layout the methods of those
7808 late additions, as constructor checks might use them */
7809 java_layout_seen_class_methods ();
7810 java_parse_abort_on_error ();
7813 /* Expand methods in the current set of classes remembered for
7814 generation. */
7816 static void
7817 java_complete_expand_classes (void)
7819 tree current;
7821 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7822 if (!INNER_CLASS_DECL_P (current))
7823 java_complete_expand_class (current);
7826 /* Expand the methods found in OUTER, starting first by OUTER's inner
7827 classes, if any. */
7829 static void
7830 java_complete_expand_class (tree outer)
7832 tree inner_list;
7834 /* We need to go after all inner classes and start expanding them,
7835 starting with most nested ones. We have to do that because nested
7836 classes might add functions to outer classes */
7838 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7839 inner_list; inner_list = TREE_CHAIN (inner_list))
7840 java_complete_expand_class (TREE_PURPOSE (inner_list));
7842 java_complete_expand_methods (outer);
7845 /* Expand methods registered in CLASS_DECL. The general idea is that
7846 we expand regular methods first. This allows us get an estimate on
7847 how outer context local alias fields are really used so we can add
7848 to the constructor just enough code to initialize them properly (it
7849 also lets us generate finit$ correctly.) Then we expand the
7850 constructors and then <clinit>. */
7852 static void
7853 java_complete_expand_methods (tree class_decl)
7855 tree clinit, decl, first_decl;
7857 output_class = current_class = TREE_TYPE (class_decl);
7859 /* Pre-expand <clinit> to figure whether we really need it or
7860 not. If we do need it, we pre-expand the static fields so they're
7861 ready to be used somewhere else. <clinit> will be fully expanded
7862 after we processed the constructors. */
7863 first_decl = TYPE_METHODS (current_class);
7864 clinit = maybe_generate_pre_expand_clinit (current_class);
7866 /* Then generate finit$ (if we need to) because constructors will
7867 try to use it.*/
7868 if (TYPE_FINIT_STMT_LIST (current_class))
7869 java_complete_expand_method (generate_finit (current_class));
7871 /* Then generate instinit$ (if we need to) because constructors will
7872 try to use it. */
7873 if (TYPE_II_STMT_LIST (current_class))
7874 java_complete_expand_method (generate_instinit (current_class));
7876 /* Now do the constructors */
7877 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7879 if (!DECL_CONSTRUCTOR_P (decl))
7880 continue;
7881 java_complete_expand_method (decl);
7884 /* First, do the ordinary methods. */
7885 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7887 /* Ctors aren't part of this batch. */
7888 if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7889 continue;
7891 /* Skip abstract or native methods -- but do handle native
7892 methods when generating JNI stubs. */
7893 if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7895 DECL_FUNCTION_BODY (decl) = NULL_TREE;
7896 continue;
7899 if (METHOD_NATIVE (decl))
7901 tree body;
7902 current_function_decl = decl;
7903 body = build_jni_stub (decl);
7904 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7907 java_complete_expand_method (decl);
7910 /* If there is indeed a <clinit>, fully expand it now */
7911 if (clinit)
7913 /* Prevent the use of `this' inside <clinit> */
7914 ctxp->explicit_constructor_p = 1;
7915 java_complete_expand_method (clinit);
7916 ctxp->explicit_constructor_p = 0;
7919 /* We might have generated a class$ that we now want to expand */
7920 if (TYPE_DOT_CLASS (current_class))
7921 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7923 /* Now verify constructor circularity (stop after the first one we
7924 prove wrong.) */
7925 if (!CLASS_INTERFACE (class_decl))
7926 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7927 if (DECL_CONSTRUCTOR_P (decl)
7928 && verify_constructor_circularity (decl, decl))
7929 break;
7932 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7933 safely used in some other methods/constructors. */
7935 static tree
7936 maybe_generate_pre_expand_clinit (tree class_type)
7938 tree current, mdecl;
7940 if (!TYPE_CLINIT_STMT_LIST (class_type))
7941 return NULL_TREE;
7943 /* Go through all static fields and pre expand them */
7944 for (current = TYPE_FIELDS (class_type); current;
7945 current = TREE_CHAIN (current))
7946 if (FIELD_STATIC (current))
7947 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7949 /* Then build the <clinit> method */
7950 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7951 clinit_identifier_node, end_params_node);
7952 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7953 mdecl, NULL_TREE);
7954 start_artificial_method_body (mdecl);
7956 /* We process the list of assignment we produced as the result of
7957 the declaration of initialized static field and add them as
7958 statement to the <clinit> method. */
7959 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7960 current = TREE_CHAIN (current))
7962 tree stmt = current;
7963 /* We build the assignment expression that will initialize the
7964 field to its value. There are strict rules on static
7965 initializers (8.5). FIXME */
7966 if (TREE_CODE (stmt) != BLOCK && !IS_EMPTY_STMT (stmt))
7967 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7968 java_method_add_stmt (mdecl, stmt);
7971 end_artificial_method_body (mdecl);
7973 /* Now we want to place <clinit> as the last method (because we need
7974 it at least for interface so that it doesn't interfere with the
7975 dispatch table based lookup. */
7976 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7978 current = TREE_CHAIN (TYPE_METHODS (class_type));
7979 TYPE_METHODS (class_type) = current;
7981 while (TREE_CHAIN (current))
7982 current = TREE_CHAIN (current);
7984 TREE_CHAIN (current) = mdecl;
7985 TREE_CHAIN (mdecl) = NULL_TREE;
7988 return mdecl;
7991 /* Analyzes a method body and look for something that isn't a
7992 MODIFY_EXPR with a constant value. */
7994 static int
7995 analyze_clinit_body (tree this_class, tree bbody)
7997 while (bbody)
7998 switch (TREE_CODE (bbody))
8000 case BLOCK:
8001 bbody = BLOCK_EXPR_BODY (bbody);
8002 break;
8004 case EXPR_WITH_FILE_LOCATION:
8005 bbody = EXPR_WFL_NODE (bbody);
8006 break;
8008 case COMPOUND_EXPR:
8009 if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
8010 return 1;
8011 bbody = TREE_OPERAND (bbody, 1);
8012 break;
8014 case MODIFY_EXPR:
8015 /* If we're generating to class file and we're dealing with an
8016 array initialization, we return 1 to keep <clinit> */
8017 if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
8018 && flag_emit_class_files)
8019 return 1;
8021 /* There are a few cases where we're required to keep
8022 <clinit>:
8023 - If this is an assignment whose operand is not constant,
8024 - If this is an assignment to a non-initialized field,
8025 - If this field is not a member of the current class.
8027 return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
8028 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
8029 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
8031 default:
8032 return 1;
8034 return 0;
8038 /* See whether we could get rid of <clinit>. Criteria are: all static
8039 final fields have constant initial values and the body of <clinit>
8040 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
8042 static int
8043 maybe_yank_clinit (tree mdecl)
8045 tree type, current;
8046 tree fbody, bbody;
8048 if (!DECL_CLINIT_P (mdecl))
8049 return 0;
8051 /* If the body isn't empty, then we keep <clinit>. Note that if
8052 we're emitting classfiles, this isn't enough not to rule it
8053 out. */
8054 fbody = DECL_FUNCTION_BODY (mdecl);
8055 bbody = BLOCK_EXPR_BODY (fbody);
8056 if (bbody && bbody != error_mark_node)
8057 bbody = BLOCK_EXPR_BODY (bbody);
8058 else
8059 return 0;
8060 if (bbody && ! flag_emit_class_files && !IS_EMPTY_STMT (bbody))
8061 return 0;
8063 type = DECL_CONTEXT (mdecl);
8064 current = TYPE_FIELDS (type);
8066 for (current = (current ? TREE_CHAIN (current) : current);
8067 current; current = TREE_CHAIN (current))
8069 tree f_init;
8071 /* We're not interested in non-static fields. */
8072 if (!FIELD_STATIC (current))
8073 continue;
8075 /* Nor in fields without initializers. */
8076 f_init = DECL_INITIAL (current);
8077 if (f_init == NULL_TREE)
8078 continue;
8080 /* Anything that isn't String or a basic type is ruled out -- or
8081 if we know how to deal with it (when doing things natively) we
8082 should generated an empty <clinit> so that SUID are computed
8083 correctly. */
8084 if (! JSTRING_TYPE_P (TREE_TYPE (current))
8085 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
8086 return 0;
8088 if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
8089 return 0;
8092 /* Now we analyze the method body and look for something that
8093 isn't a MODIFY_EXPR */
8094 if (bbody && !IS_EMPTY_STMT (bbody) && analyze_clinit_body (type, bbody))
8095 return 0;
8097 /* Get rid of <clinit> in the class' list of methods */
8098 if (TYPE_METHODS (type) == mdecl)
8099 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
8100 else
8101 for (current = TYPE_METHODS (type); current;
8102 current = TREE_CHAIN (current))
8103 if (TREE_CHAIN (current) == mdecl)
8105 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
8106 break;
8109 return 1;
8112 /* Install the argument from MDECL. Suitable to completion and
8113 expansion of mdecl's body. */
8115 void
8116 start_complete_expand_method (tree mdecl)
8118 tree tem;
8120 pushlevel (1); /* Prepare for a parameter push */
8121 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
8122 DECL_ARGUMENTS (mdecl) = tem;
8124 for (; tem; tem = TREE_CHAIN (tem))
8126 /* TREE_CHAIN (tem) will change after pushdecl. */
8127 tree next = TREE_CHAIN (tem);
8128 tree type = TREE_TYPE (tem);
8129 if (targetm.calls.promote_prototypes (type)
8130 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8131 && INTEGRAL_TYPE_P (type))
8132 type = integer_type_node;
8133 DECL_ARG_TYPE (tem) = type;
8134 layout_decl (tem, 0);
8135 pushdecl (tem);
8136 /* Re-install the next so that the list is kept and the loop
8137 advances. */
8138 TREE_CHAIN (tem) = next;
8140 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8141 input_location = DECL_SOURCE_LOCATION (mdecl);
8142 build_result_decl (mdecl);
8146 /* Complete and expand a method. */
8148 static void
8149 java_complete_expand_method (tree mdecl)
8151 tree fbody, block_body, exception_copy;
8153 current_function_decl = mdecl;
8154 /* Fix constructors before expanding them */
8155 if (DECL_CONSTRUCTOR_P (mdecl))
8156 fix_constructors (mdecl);
8158 /* Expand functions that have a body */
8159 if (!DECL_FUNCTION_BODY (mdecl))
8160 return;
8162 fbody = DECL_FUNCTION_BODY (mdecl);
8163 block_body = BLOCK_EXPR_BODY (fbody);
8164 exception_copy = NULL_TREE;
8166 current_function_decl = mdecl;
8168 if (! quiet_flag)
8169 fprintf (stderr, " [%s.",
8170 lang_printable_name (DECL_CONTEXT (mdecl), 0));
8171 announce_function (mdecl);
8172 if (! quiet_flag)
8173 fprintf (stderr, "]");
8175 /* Prepare the function for tree completion */
8176 start_complete_expand_method (mdecl);
8178 /* Install the current this */
8179 current_this = (!METHOD_STATIC (mdecl) ?
8180 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8182 /* Purge the `throws' list of unchecked exceptions (we save a copy
8183 of the list and re-install it later.) */
8184 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8185 purge_unchecked_exceptions (mdecl);
8187 /* Install exceptions thrown with `throws' */
8188 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8190 if (block_body != NULL_TREE)
8192 block_body = java_complete_tree (block_body);
8194 /* Before we check initialization, attached all class initialization
8195 variable to the block_body */
8196 htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8197 attach_init_test_initialization_flags, block_body);
8199 if (! METHOD_NATIVE (mdecl))
8201 check_for_initialization (block_body, mdecl);
8203 /* Go through all the flags marking the initialization of
8204 static variables and see whether they're definitively
8205 assigned, in which case the type is remembered as
8206 definitively initialized in MDECL. */
8207 if (STATIC_CLASS_INIT_OPT_P ())
8209 /* Always register the context as properly initialized in
8210 MDECL. This used with caution helps removing extra
8211 initialization of self. */
8212 if (METHOD_STATIC (mdecl))
8214 *(htab_find_slot
8215 (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8216 DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8220 ctxp->explicit_constructor_p = 0;
8223 BLOCK_EXPR_BODY (fbody) = block_body;
8225 /* If we saw a return but couldn't evaluate it properly, we'll have
8226 an error_mark_node here. */
8227 if (block_body != error_mark_node
8228 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8229 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE)
8230 missing_return_error (current_function_decl);
8232 /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8233 maybe_yank_clinit (mdecl);
8235 /* Pop the current level, with special measures if we found errors. */
8236 if (java_error_count)
8237 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8238 poplevel (1, 0, 1);
8240 /* Pop the exceptions and sanity check */
8241 POP_EXCEPTIONS();
8242 if (currently_caught_type_list)
8243 abort ();
8245 /* Restore the copy of the list of exceptions. */
8246 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8249 /* For with each class for which there's code to generate. */
8251 static void
8252 java_expand_method_bodies (tree class)
8254 tree decl;
8255 for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8257 tree block;
8259 if (! DECL_FUNCTION_BODY (decl))
8260 continue;
8262 current_function_decl = decl;
8264 block = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8266 /* Save the function body for gimplify and inlining. */
8267 DECL_SAVED_TREE (decl) = block;
8269 /* It's time to assign the variable flagging static class
8270 initialization based on which classes invoked static methods
8271 are definitely initializing. This should be flagged. */
8272 if (STATIC_CLASS_INIT_OPT_P ())
8274 tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8275 for (; list != NULL_TREE; list = TREE_CHAIN (list))
8277 /* Executed for each statement calling a static function.
8278 LIST is a TREE_LIST whose PURPOSE is the called function
8279 and VALUE is a compound whose second operand can be patched
8280 with static class initialization flag assignments. */
8282 tree called_method = TREE_PURPOSE (list);
8283 tree compound = TREE_VALUE (list);
8284 tree assignment_compound_list
8285 = build_tree_list (called_method, NULL);
8287 /* For each class definitely initialized in
8288 CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8289 assignment to the class initialization flag. */
8290 htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8291 emit_test_initialization,
8292 assignment_compound_list);
8294 if (TREE_VALUE (assignment_compound_list))
8295 TREE_OPERAND (compound, 1)
8296 = TREE_VALUE (assignment_compound_list);
8300 /* Expand the function body. */
8301 source_end_java_method ();
8307 /* This section of the code deals with accessing enclosing context
8308 fields either directly by using the relevant access to this$<n> or
8309 by invoking an access method crafted for that purpose. */
8311 /* Build the necessary access from an inner class to an outer
8312 class. This routine could be optimized to cache previous result
8313 (decl, current_class and returned access). When an access method
8314 needs to be generated, it always takes the form of a read. It might
8315 be later turned into a write by calling outer_field_access_fix. */
8317 static tree
8318 build_outer_field_access (tree id, tree decl)
8320 tree access = NULL_TREE;
8321 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8322 tree decl_ctx = DECL_CONTEXT (decl);
8324 /* If the immediate enclosing context of the current class is the
8325 field decl's class or inherits from it; build the access as
8326 `this$<n>.<field>'. Note that we will break the `private' barrier
8327 if we're not emitting bytecodes. */
8328 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8329 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8331 tree thisn = build_current_thisn (current_class);
8332 access = make_qualified_primary (build_wfl_node (thisn),
8333 id, EXPR_WFL_LINECOL (id));
8335 /* Otherwise, generate access methods to outer this and access the
8336 field (either using an access method or by direct access.) */
8337 else
8339 int lc = EXPR_WFL_LINECOL (id);
8341 /* Now we chain the required number of calls to the access$0 to
8342 get a hold to the enclosing instance we need, and then we
8343 build the field access. */
8344 access = build_access_to_thisn (current_class, decl_ctx, lc);
8346 /* If the field is private and we're generating bytecode, then
8347 we generate an access method */
8348 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8350 tree name = build_outer_field_access_methods (decl);
8351 access = build_outer_field_access_expr (lc, decl_ctx,
8352 name, access, NULL_TREE);
8354 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8355 Once again we break the `private' access rule from a foreign
8356 class. */
8357 else
8358 access = make_qualified_primary (access, id, lc);
8360 return resolve_expression_name (access, NULL);
8363 /* Return a nonzero value if NODE describes an outer field inner
8364 access. */
8366 static int
8367 outer_field_access_p (tree type, tree decl)
8369 if (!INNER_CLASS_TYPE_P (type)
8370 || TREE_CODE (decl) != FIELD_DECL
8371 || DECL_CONTEXT (decl) == type)
8372 return 0;
8374 /* If the inner class extends the declaration context of the field
8375 we're trying to access, then this isn't an outer field access */
8376 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8377 return 0;
8379 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8380 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8382 if (type == DECL_CONTEXT (decl))
8383 return 1;
8385 if (!DECL_CONTEXT (TYPE_NAME (type)))
8387 /* Before we give up, see whether the field is inherited from
8388 the enclosing context we're considering. */
8389 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8390 return 1;
8391 break;
8395 return 0;
8398 /* Return a nonzero value if NODE represents an outer field inner
8399 access that was been already expanded. As a side effect, it returns
8400 the name of the field being accessed and the argument passed to the
8401 access function, suitable for a regeneration of the access method
8402 call if necessary. */
8404 static int
8405 outer_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8406 tree *arg)
8408 int identified = 0;
8410 if (TREE_CODE (node) != CALL_EXPR)
8411 return 0;
8413 /* Well, gcj generates slightly different tree nodes when compiling
8414 to native or bytecodes. It's the case for function calls. */
8416 if (flag_emit_class_files
8417 && TREE_CODE (node) == CALL_EXPR
8418 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8419 identified = 1;
8420 else if (!flag_emit_class_files)
8422 node = TREE_OPERAND (node, 0);
8424 if (node && TREE_OPERAND (node, 0)
8425 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8427 node = TREE_OPERAND (node, 0);
8428 if (TREE_OPERAND (node, 0)
8429 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8430 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8431 (DECL_NAME (TREE_OPERAND (node, 0)))))
8432 identified = 1;
8436 if (identified && name && arg_type && arg)
8438 tree argument = TREE_OPERAND (node, 1);
8439 *name = DECL_NAME (TREE_OPERAND (node, 0));
8440 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8441 *arg = TREE_VALUE (argument);
8443 return identified;
8446 /* Detect in NODE an outer field read access from an inner class and
8447 transform it into a write with RHS as an argument. This function is
8448 called from the java_complete_lhs when an assignment to a LHS can
8449 be identified. */
8451 static tree
8452 outer_field_access_fix (tree wfl, tree node, tree rhs)
8454 tree name, arg_type, arg;
8456 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8458 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8459 arg_type, name, arg, rhs);
8460 return java_complete_tree (node);
8462 return NULL_TREE;
8465 /* Construct the expression that calls an access method:
8466 <type>.access$<n>(<arg1> [, <arg2>]);
8468 ARG2 can be NULL and will be omitted in that case. It will denote a
8469 read access. */
8471 static tree
8472 build_outer_field_access_expr (int lc, tree type, tree access_method_name,
8473 tree arg1, tree arg2)
8475 tree args, cn, access;
8477 args = arg1 ? arg1 :
8478 build_wfl_node (build_current_thisn (current_class));
8479 args = build_tree_list (NULL_TREE, args);
8481 if (arg2)
8482 args = tree_cons (NULL_TREE, arg2, args);
8484 access = build_method_invocation (build_wfl_node (access_method_name), args);
8485 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8486 return make_qualified_primary (cn, access, lc);
8489 static tree
8490 build_new_access_id (void)
8492 static int access_n_counter = 1;
8493 char buffer [128];
8495 sprintf (buffer, "access$%d", access_n_counter++);
8496 return get_identifier (buffer);
8499 /* Create the static access functions for the outer field DECL. We define a
8500 read:
8501 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8502 return inst$.field;
8504 and a write access:
8505 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8506 TREE_TYPE (<field>) value$) {
8507 return inst$.field = value$;
8509 We should have a usage flags on the DECL so we can lazily turn the ones
8510 we're using for code generation. FIXME.
8513 static tree
8514 build_outer_field_access_methods (tree decl)
8516 tree id, args, stmt, mdecl;
8518 if (FIELD_INNER_ACCESS_P (decl))
8519 return FIELD_INNER_ACCESS (decl);
8521 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8523 /* Create the identifier and a function named after it. */
8524 id = build_new_access_id ();
8526 /* The identifier is marked as bearing the name of a generated write
8527 access function for outer field accessed from inner classes. */
8528 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8530 /* Create the read access */
8531 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8532 TREE_CHAIN (args) = end_params_node;
8533 stmt = make_qualified_primary (build_wfl_node (inst_id),
8534 build_wfl_node (DECL_NAME (decl)), 0);
8535 stmt = build_return (0, stmt);
8536 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8537 TREE_TYPE (decl), id, args, stmt);
8538 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8540 /* Create the write access method. No write access for final variable */
8541 if (!FIELD_FINAL (decl))
8543 args = build_tree_list (inst_id,
8544 build_pointer_type (DECL_CONTEXT (decl)));
8545 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8546 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8547 stmt = make_qualified_primary (build_wfl_node (inst_id),
8548 build_wfl_node (DECL_NAME (decl)), 0);
8549 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8550 build_wfl_node (wpv_id)));
8551 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8552 TREE_TYPE (decl), id,
8553 args, stmt);
8555 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8557 /* Return the access name */
8558 return FIELD_INNER_ACCESS (decl) = id;
8561 /* Build an field access method NAME. */
8563 static tree
8564 build_outer_field_access_method (tree class, tree type, tree name,
8565 tree args, tree body)
8567 tree saved_current_function_decl, mdecl;
8569 /* Create the method */
8570 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8571 fix_method_argument_names (args, mdecl);
8572 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8574 /* Attach the method body. */
8575 saved_current_function_decl = current_function_decl;
8576 start_artificial_method_body (mdecl);
8577 java_method_add_stmt (mdecl, body);
8578 end_artificial_method_body (mdecl);
8579 current_function_decl = saved_current_function_decl;
8581 return mdecl;
8585 /* This section deals with building access function necessary for
8586 certain kinds of method invocation from inner classes. */
8588 static tree
8589 build_outer_method_access_method (tree decl)
8591 tree saved_current_function_decl, mdecl;
8592 tree args = NULL_TREE, call_args = NULL_TREE;
8593 tree carg, id, body, class;
8594 char buffer [80];
8595 int parm_id_count = 0;
8597 /* Test this abort with an access to a private field */
8598 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8599 abort ();
8601 /* Check the cache first */
8602 if (DECL_FUNCTION_INNER_ACCESS (decl))
8603 return DECL_FUNCTION_INNER_ACCESS (decl);
8605 class = DECL_CONTEXT (decl);
8607 /* Obtain an access identifier and mark it */
8608 id = build_new_access_id ();
8609 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8611 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8612 /* Create the arguments, as much as the original */
8613 for (; carg && carg != end_params_node;
8614 carg = TREE_CHAIN (carg))
8616 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8617 args = chainon (args, build_tree_list (get_identifier (buffer),
8618 TREE_VALUE (carg)));
8620 args = chainon (args, end_params_node);
8622 /* Create the method */
8623 mdecl = create_artificial_method (class, ACC_STATIC,
8624 TREE_TYPE (TREE_TYPE (decl)), id, args);
8625 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8626 /* There is a potential bug here. We should be able to use
8627 fix_method_argument_names, but then arg names get mixed up and
8628 eventually a constructor will have its this$0 altered and the
8629 outer context won't be assignment properly. The testcase is
8630 stub.java FIXME */
8631 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8633 /* Attach the method body. */
8634 saved_current_function_decl = current_function_decl;
8635 start_artificial_method_body (mdecl);
8637 /* The actual method invocation uses the same args. When invoking a
8638 static methods that way, we don't want to skip the first
8639 argument. */
8640 carg = args;
8641 if (!METHOD_STATIC (decl))
8642 carg = TREE_CHAIN (carg);
8643 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8644 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8645 call_args);
8647 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8648 call_args);
8649 if (!METHOD_STATIC (decl))
8650 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8651 body, 0);
8652 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8653 body = build_return (0, body);
8654 java_method_add_stmt (mdecl,body);
8655 end_artificial_method_body (mdecl);
8656 current_function_decl = saved_current_function_decl;
8658 /* Back tag the access function so it know what it accesses */
8659 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8661 /* Tag the current method so it knows it has an access generated */
8662 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8666 /* This section of the code deals with building expressions to access
8667 the enclosing instance of an inner class. The enclosing instance is
8668 kept in a generated field called this$<n>, with <n> being the
8669 inner class nesting level (starting from 0.) */
8671 /* Build an access to a given this$<n>, always chaining access call to
8672 others. Access methods to this$<n> are build on the fly if
8673 necessary. This CAN'T be used to solely access this$<n-1> from
8674 this$<n> (which alway yield to special cases and optimization, see
8675 for example build_outer_field_access). */
8677 static tree
8678 build_access_to_thisn (tree from, tree to, int lc)
8680 tree access = NULL_TREE;
8682 while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8684 if (!access)
8686 access = build_current_thisn (from);
8687 access = build_wfl_node (access);
8689 else
8691 tree access0_wfl, cn;
8693 maybe_build_thisn_access_method (from);
8694 access0_wfl = build_wfl_node (access0_identifier_node);
8695 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8696 EXPR_WFL_LINECOL (access0_wfl) = lc;
8697 access = build_tree_list (NULL_TREE, access);
8698 access = build_method_invocation (access0_wfl, access);
8699 access = make_qualified_primary (cn, access, lc);
8702 /* If FROM isn't an inner class, that's fine, we've done enough.
8703 What we're looking for can be accessed from there. */
8704 from = DECL_CONTEXT (TYPE_NAME (from));
8705 if (!from)
8706 break;
8707 from = TREE_TYPE (from);
8709 return access;
8712 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8713 is returned if nothing needs to be generated. Otherwise, the method
8714 generated and a method decl is returned.
8716 NOTE: These generated methods should be declared in a class file
8717 attribute so that they can't be referred to directly. */
8719 static tree
8720 maybe_build_thisn_access_method (tree type)
8722 tree mdecl, args, stmt, rtype;
8723 tree saved_current_function_decl;
8725 /* If TYPE is a top-level class, no access method is required.
8726 If there already is such an access method, bail out. */
8727 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8728 return NULL_TREE;
8730 /* We generate the method. The method looks like:
8731 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8733 args = build_tree_list (inst_id, build_pointer_type (type));
8734 TREE_CHAIN (args) = end_params_node;
8735 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8736 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8737 access0_identifier_node, args);
8738 fix_method_argument_names (args, mdecl);
8739 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8740 stmt = build_current_thisn (type);
8741 stmt = make_qualified_primary (build_wfl_node (inst_id),
8742 build_wfl_node (stmt), 0);
8743 stmt = build_return (0, stmt);
8745 saved_current_function_decl = current_function_decl;
8746 start_artificial_method_body (mdecl);
8747 java_method_add_stmt (mdecl, stmt);
8748 end_artificial_method_body (mdecl);
8749 current_function_decl = saved_current_function_decl;
8751 CLASS_ACCESS0_GENERATED_P (type) = 1;
8753 return mdecl;
8756 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8757 the first level of innerclassing. this$1 for the next one, etc...
8758 This function can be invoked with TYPE to NULL, available and then
8759 has to count the parser context. */
8761 static GTY(()) tree saved_thisn;
8762 static GTY(()) tree saved_type;
8764 static tree
8765 build_current_thisn (tree type)
8767 static int saved_i = -1;
8768 static int saved_type_i = 0;
8769 tree decl;
8770 char buffer [24];
8771 int i = 0;
8773 if (type)
8775 if (type == saved_type)
8776 i = saved_type_i;
8777 else
8779 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8780 decl; decl = DECL_CONTEXT (decl), i++)
8783 saved_type = type;
8784 saved_type_i = i;
8787 else
8788 i = list_length (GET_CPC_LIST ())-2;
8790 if (i == saved_i)
8791 return saved_thisn;
8793 sprintf (buffer, "this$%d", i);
8794 saved_i = i;
8795 saved_thisn = get_identifier (buffer);
8796 return saved_thisn;
8799 /* Return the assignment to the hidden enclosing context `this$<n>'
8800 by the second incoming parameter to the innerclass constructor. The
8801 form used is `this.this$<n> = this$<n>;'. */
8803 static tree
8804 build_thisn_assign (void)
8806 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8808 tree thisn = build_current_thisn (current_class);
8809 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8810 build_wfl_node (thisn), 0);
8811 tree rhs = build_wfl_node (thisn);
8812 #ifdef USE_MAPPED_LOCATION
8813 SET_EXPR_LOCATION (lhs, input_location);
8814 #else
8815 EXPR_WFL_SET_LINECOL (lhs, input_line, 0);
8816 #endif
8817 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8819 return NULL_TREE;
8823 /* Building the synthetic `class$' used to implement the `.class' 1.1
8824 extension for non primitive types. This method looks like:
8826 static Class class$(String type) throws NoClassDefFoundError
8828 try {return (java.lang.Class.forName (String));}
8829 catch (ClassNotFoundException e) {
8830 throw new NoClassDefFoundError(e.getMessage());}
8831 } */
8833 static GTY(()) tree get_message_wfl;
8834 static GTY(()) tree type_parm_wfl;
8836 static tree
8837 build_dot_class_method (tree class)
8839 #define BWF(S) build_wfl_node (get_identifier ((S)))
8840 #ifdef USE_MAPPED_LOCATION
8841 #define MQN(X,Y) make_qualified_name ((X), (Y), UNKNOWN_LOCATION)
8842 #else
8843 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8844 #endif
8845 tree args, tmp, saved_current_function_decl, mdecl, qual_name;
8846 tree stmt, throw_stmt;
8848 if (!get_message_wfl)
8850 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8851 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8854 /* Build the arguments */
8855 args = build_tree_list (get_identifier ("type$"),
8856 build_pointer_type (string_type_node));
8857 TREE_CHAIN (args) = end_params_node;
8859 /* Build the qualified name java.lang.Class.forName */
8860 tmp = MQN (MQN (MQN (BWF ("java"),
8861 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8863 /* Create the "class$" function */
8864 mdecl = create_artificial_method (class, ACC_STATIC,
8865 build_pointer_type (class_type_node),
8866 classdollar_identifier_node, args);
8867 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8868 BWF ("NoClassDefFoundError"));
8869 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE, qual_name);
8870 register_incomplete_type (JDEP_EXCEPTION, qual_name, NULL_TREE, NULL_TREE);
8871 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
8872 &TREE_VALUE (DECL_FUNCTION_THROWS (mdecl));
8874 /* We start by building the try block. We need to build:
8875 return (java.lang.Class.forName (type)); */
8876 stmt = build_method_invocation (tmp,
8877 build_tree_list (NULL_TREE, type_parm_wfl));
8878 stmt = build_return (0, stmt);
8880 /* Now onto the catch block. We start by building the expression
8881 throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8882 #ifdef USE_MAPPED_LOCATION
8883 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8884 get_message_wfl, UNKNOWN_LOCATION);
8885 #else
8886 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8887 get_message_wfl, 0);
8888 #endif
8889 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8891 /* Build new NoClassDefFoundError (_.getMessage) */
8892 throw_stmt = build_new_invocation
8893 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8894 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8896 /* Build the throw, (it's too early to use BUILD_THROW) */
8897 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8899 /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8900 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8901 BWF ("ClassNotFoundException"));
8902 stmt = encapsulate_with_try_catch (0, qual_name, stmt, throw_stmt);
8904 fix_method_argument_names (args, mdecl);
8905 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8906 saved_current_function_decl = current_function_decl;
8907 start_artificial_method_body (mdecl);
8908 java_method_add_stmt (mdecl, stmt);
8909 end_artificial_method_body (mdecl);
8910 current_function_decl = saved_current_function_decl;
8911 TYPE_DOT_CLASS (class) = mdecl;
8913 return mdecl;
8916 static tree
8917 build_dot_class_method_invocation (tree this_class, tree type)
8919 tree dot_class_method = TYPE_DOT_CLASS (this_class);
8920 tree sig_id, s, t;
8922 if (TYPE_ARRAY_P (type))
8923 sig_id = build_java_signature (type);
8924 else
8925 sig_id = DECL_NAME (TYPE_NAME (type));
8927 /* Ensure that the proper name separator is used */
8928 sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8929 IDENTIFIER_LENGTH (sig_id));
8931 s = build_string (IDENTIFIER_LENGTH (sig_id),
8932 IDENTIFIER_POINTER (sig_id));
8933 t = build_method_invocation (build_wfl_node (DECL_NAME (dot_class_method)),
8934 build_tree_list (NULL_TREE, s));
8935 if (DECL_CONTEXT (dot_class_method) != this_class)
8937 tree class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (dot_class_method)));
8938 t = make_qualified_primary (build_wfl_node (class_name), t, 0);
8940 return t;
8943 /* This section of the code deals with constructor. */
8945 /* Craft a body for default constructor. Patch existing constructor
8946 bodies with call to super() and field initialization statements if
8947 necessary. */
8949 static void
8950 fix_constructors (tree mdecl)
8952 tree iii; /* Instance Initializer Invocation */
8953 tree *bodyp = &DECL_FUNCTION_BODY (mdecl);
8954 tree thisn_assign, compound = NULL_TREE;
8955 tree class_type = DECL_CONTEXT (mdecl);
8957 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8958 return;
8959 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8961 if (!*bodyp)
8963 /* It is an error for the compiler to generate a default
8964 constructor if the superclass doesn't have a constructor that
8965 takes no argument, or the same args for an anonymous class */
8966 if (verify_constructor_super (mdecl))
8968 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8969 tree save = DECL_NAME (mdecl);
8970 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8971 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8972 parse_error_context
8973 (lookup_cl (TYPE_NAME (class_type)),
8974 "No constructor matching %qs found in class %qs",
8975 lang_printable_name (mdecl, 2), n);
8976 DECL_NAME (mdecl) = save;
8979 /* The constructor body must be crafted by hand. It's the
8980 constructor we defined when we realize we didn't have the
8981 CLASSNAME() constructor */
8982 start_artificial_method_body (mdecl);
8984 /* Insert an assignment to the this$<n> hidden field, if
8985 necessary */
8986 if ((thisn_assign = build_thisn_assign ()))
8987 java_method_add_stmt (mdecl, thisn_assign);
8989 /* We don't generate a super constructor invocation if we're
8990 compiling java.lang.Object. build_super_invocation takes care
8991 of that. */
8992 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8994 /* FIXME */
8995 if ((iii = build_instinit_invocation (class_type)))
8996 java_method_add_stmt (mdecl, iii);
8998 end_artificial_method_body (mdecl);
9000 /* Search for an explicit constructor invocation */
9001 else
9003 int found = 0;
9004 int invokes_this = 0;
9005 tree main_block = BLOCK_EXPR_BODY (*bodyp);
9007 while (*bodyp)
9009 tree body = *bodyp;
9010 switch (TREE_CODE (body))
9012 case CALL_EXPR:
9013 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
9014 if (CALL_THIS_CONSTRUCTOR_P (body))
9015 invokes_this = 1;
9016 break;
9017 case COMPOUND_EXPR:
9018 case EXPR_WITH_FILE_LOCATION:
9019 bodyp = &TREE_OPERAND (body, 0);
9020 continue;
9021 case BLOCK:
9022 bodyp = &BLOCK_EXPR_BODY (body);
9023 continue;
9024 default:
9025 break;
9027 break;
9030 /* Generate the assignment to this$<n>, if necessary */
9031 if ((thisn_assign = build_thisn_assign ()))
9032 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
9034 /* The constructor is missing an invocation of super() */
9035 if (!found)
9036 compound = add_stmt_to_compound (compound, NULL_TREE,
9037 build_super_invocation (mdecl));
9038 /* Explicit super() invocation should take place before the
9039 instance initializer blocks. */
9040 else
9042 compound = add_stmt_to_compound (compound, NULL_TREE, *bodyp);
9043 *bodyp = build_java_empty_stmt ();
9046 DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
9048 /* Insert the instance initializer block right after. */
9049 if (!invokes_this && (iii = build_instinit_invocation (class_type)))
9050 compound = add_stmt_to_compound (compound, NULL_TREE, iii);
9052 /* Fix the constructor main block if we're adding extra stmts */
9053 if (compound)
9055 compound = add_stmt_to_compound (compound, NULL_TREE,
9056 BLOCK_EXPR_BODY (main_block));
9057 BLOCK_EXPR_BODY (main_block) = compound;
9062 /* Browse constructors in the super class, searching for a constructor
9063 that doesn't take any argument. Return 0 if one is found, 1
9064 otherwise. If the current class is an anonymous inner class, look
9065 for something that has the same signature. */
9067 static int
9068 verify_constructor_super (tree mdecl)
9070 tree class = CLASSTYPE_SUPER (current_class);
9071 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
9072 tree sdecl;
9074 if (!class)
9075 return 0;
9077 if (ANONYMOUS_CLASS_P (current_class))
9079 tree mdecl_arg_type;
9080 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
9081 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9082 if (DECL_CONSTRUCTOR_P (sdecl))
9084 tree m_arg_type;
9085 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9086 if (super_inner)
9087 arg_type = TREE_CHAIN (arg_type);
9088 for (m_arg_type = mdecl_arg_type;
9089 (arg_type != end_params_node
9090 && m_arg_type != end_params_node);
9091 arg_type = TREE_CHAIN (arg_type),
9092 m_arg_type = TREE_CHAIN (m_arg_type))
9093 if (!valid_method_invocation_conversion_p
9094 (TREE_VALUE (arg_type),
9095 TREE_VALUE (m_arg_type)))
9096 break;
9098 if (arg_type == end_params_node && m_arg_type == end_params_node)
9099 return 0;
9102 else
9104 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9106 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9107 if (super_inner)
9108 arg = TREE_CHAIN (arg);
9109 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
9110 return 0;
9113 return 1;
9116 /* Generate code for all context remembered for code generation. */
9118 static GTY(()) tree reversed_class_list;
9119 void
9120 java_expand_classes (void)
9122 int save_error_count = 0;
9123 static struct parser_ctxt *cur_ctxp = NULL;
9124 location_t save_location;
9126 java_parse_abort_on_error ();
9127 if (!(ctxp = ctxp_for_generation))
9128 return;
9129 java_layout_classes ();
9130 java_parse_abort_on_error ();
9131 save_location = input_location;
9133 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9135 tree current;
9136 for (current = cur_ctxp->class_list;
9137 current;
9138 current = TREE_CHAIN (current))
9139 gen_indirect_dispatch_tables (TREE_TYPE (current));
9142 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9144 ctxp = cur_ctxp;
9145 input_location = ctxp->file_start_location;
9146 lang_init_source (2); /* Error msgs have method prototypes */
9147 java_complete_expand_classes (); /* Complete and expand classes */
9148 java_parse_abort_on_error ();
9150 input_location = save_location;
9152 /* Find anonymous classes and expand their constructor. This extra pass is
9153 necessary because the constructor itself is only generated when the
9154 method in which it is defined is expanded. */
9155 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9157 tree current;
9158 ctxp = cur_ctxp;
9159 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9161 output_class = current_class = TREE_TYPE (current);
9162 if (ANONYMOUS_CLASS_P (current_class))
9164 tree d;
9165 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9167 if (DECL_CONSTRUCTOR_P (d))
9169 java_complete_expand_method (d);
9170 break; /* There is only one constructor. */
9177 /* Expanding the constructors of anonymous classes generates access
9178 methods. Scan all the methods looking for null DECL_RESULTs --
9179 this will be the case if a method hasn't been expanded. */
9180 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9182 tree current;
9183 ctxp = cur_ctxp;
9184 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9186 tree d;
9187 output_class = current_class = TREE_TYPE (current);
9188 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9190 if (DECL_RESULT (d) == NULL_TREE)
9191 java_complete_expand_method (d);
9196 /* ??? Instead of all this we could iterate around the list of
9197 classes until there were no more un-expanded methods. It would
9198 take a little longer -- one pass over the whole list of methods
9199 -- but it would be simpler. Like this: */
9200 #if 0
9202 int something_changed;
9206 something_changed = 0;
9207 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9209 tree current;
9210 ctxp = cur_ctxp;
9211 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9213 tree d;
9214 output_class = current_class = TREE_TYPE (current);
9215 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9217 if (DECL_RESULT (d) == NULL_TREE)
9219 something_changed = 1;
9220 java_complete_expand_method (d);
9226 while (something_changed);
9228 #endif
9230 /* If we've found error at that stage, don't try to generate
9231 anything, unless we're checking the syntax only
9232 (but not using -fsyntax-only for the purpose of generating
9233 bytecode). */
9234 if (java_error_count
9235 && (!flag_syntax_only && !flag_emit_class_files))
9236 return;
9238 /* Now things are stable, go for generation of the class data. */
9240 /* We pessimistically marked all methods and fields external until
9241 we knew what set of classes we were planning to compile. Now mark
9242 those that will be generated locally as not external. */
9243 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9245 tree current;
9246 ctxp = cur_ctxp;
9247 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9248 java_mark_class_local (TREE_TYPE (current));
9251 /* Compile the classes. */
9252 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9254 tree current;
9255 reversed_class_list = NULL;
9257 ctxp = cur_ctxp;
9259 /* We write out the classes in reverse order. This ensures that
9260 inner classes are written before their containing classes,
9261 which is important for parallel builds. Otherwise, the
9262 class file for the outer class may be found, but the class
9263 file for the inner class may not be present. In that
9264 situation, the compiler cannot fall back to the original
9265 source, having already read the outer class, so we must
9266 prevent that situation. */
9267 for (current = ctxp->class_list;
9268 current;
9269 current = TREE_CHAIN (current))
9270 reversed_class_list
9271 = tree_cons (NULL_TREE, current, reversed_class_list);
9273 for (current = reversed_class_list;
9274 current;
9275 current = TREE_CHAIN (current))
9277 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
9278 if (flag_emit_class_files)
9279 write_classfile (current_class);
9280 else if (! flag_syntax_only)
9281 java_expand_method_bodies (current_class);
9286 void
9287 java_finish_classes (void)
9289 static struct parser_ctxt *cur_ctxp = NULL;
9290 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9292 tree current;
9293 ctxp = cur_ctxp;
9294 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9296 output_class = current_class = TREE_TYPE (current);
9297 finish_class ();
9302 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9303 a tree list node containing RIGHT. Fore coming RIGHTs will be
9304 chained to this hook. LOCATION contains the location of the
9305 separating `.' operator. */
9307 static tree
9308 make_qualified_primary (tree primary, tree right, int location)
9310 tree wfl;
9312 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9313 wfl = build_wfl_wrap (primary, location);
9314 else
9316 wfl = primary;
9317 /* If wfl wasn't qualified, we build a first anchor */
9318 if (!EXPR_WFL_QUALIFICATION (wfl))
9319 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9322 /* And chain them */
9323 EXPR_WFL_LINECOL (right) = location;
9324 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9325 PRIMARY_P (wfl) = 1;
9326 return wfl;
9329 /* Simple merge of two name separated by a `.' */
9331 static tree
9332 merge_qualified_name (tree left, tree right)
9334 tree node;
9335 if (!left && !right)
9336 return NULL_TREE;
9338 if (!left)
9339 return right;
9341 if (!right)
9342 return left;
9344 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9345 IDENTIFIER_LENGTH (left));
9346 obstack_1grow (&temporary_obstack, '.');
9347 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9348 IDENTIFIER_LENGTH (right));
9349 node = get_identifier (obstack_base (&temporary_obstack));
9350 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9351 QUALIFIED_P (node) = 1;
9352 return node;
9355 /* Merge the two parts of a qualified name into LEFT. Set the
9356 location information of the resulting node to LOCATION, usually
9357 inherited from the location information of the `.' operator. */
9359 static tree
9360 make_qualified_name (tree left, tree right,
9361 #ifdef USE_MAPPED_LOCATION
9362 source_location location
9363 #else
9364 int location
9365 #endif
9368 #ifdef USE_COMPONENT_REF
9369 tree node = build3 (COMPONENT_REF, NULL_TREE, left, right, NULL_TREE);
9370 SET_EXPR_LOCATION (node, location);
9371 return node;
9372 #else
9373 tree left_id = EXPR_WFL_NODE (left);
9374 tree right_id = EXPR_WFL_NODE (right);
9375 tree wfl, merge;
9377 merge = merge_qualified_name (left_id, right_id);
9379 /* Left wasn't qualified and is now qualified */
9380 #ifdef USE_MAPPED_LOCATION
9381 if (!QUALIFIED_P (left_id))
9383 tree wfl = build_expr_wfl (left_id, EXPR_LOCATION (left));
9384 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9387 wfl = build_expr_wfl (right_id, location);
9388 #else
9389 if (!QUALIFIED_P (left_id))
9391 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9392 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9393 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9396 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9397 EXPR_WFL_LINECOL (wfl) = location;
9398 #endif
9399 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9400 EXPR_WFL_NODE (left) = merge;
9401 return left;
9402 #endif
9405 /* Extract the last identifier component of the qualified in WFL. The
9406 last identifier is removed from the linked list */
9408 static tree
9409 cut_identifier_in_qualified (tree wfl)
9411 tree q;
9412 tree previous = NULL_TREE;
9413 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9414 if (!TREE_CHAIN (q))
9416 if (!previous)
9417 /* Operating on a non qualified qualified WFL. */
9418 abort ();
9420 TREE_CHAIN (previous) = NULL_TREE;
9421 return TREE_PURPOSE (q);
9425 /* Resolve the expression name NAME. Return its decl. */
9427 static tree
9428 resolve_expression_name (tree id, tree *orig)
9430 tree name = EXPR_WFL_NODE (id);
9431 tree decl;
9433 /* 6.5.5.1: Simple expression names */
9434 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9436 /* 15.13.1: NAME can appear within the scope of a local variable
9437 declaration */
9438 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9439 return decl;
9441 /* 15.13.1: NAME can appear within a class declaration */
9442 else
9444 decl = lookup_field_wrapper (current_class, name);
9445 if (decl)
9447 tree access = NULL_TREE;
9448 int fs = FIELD_STATIC (decl);
9450 /* If we're accessing an outer scope local alias, make
9451 sure we change the name of the field we're going to
9452 build access to. */
9453 if (FIELD_LOCAL_ALIAS_USED (decl))
9454 name = DECL_NAME (decl);
9456 check_deprecation (id, decl);
9458 /* Instance variable (8.3.1.1) can't appear within
9459 static method, static initializer or initializer for
9460 a static variable. */
9461 if (!fs && METHOD_STATIC (current_function_decl))
9463 static_ref_err (id, name, current_class);
9464 return error_mark_node;
9466 /* Instance variables can't appear as an argument of
9467 an explicit constructor invocation */
9468 if (!fs && ctxp->explicit_constructor_p
9469 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9471 parse_error_context
9472 (id, "Can't reference %qs before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9473 return error_mark_node;
9476 /* If we're processing an inner class and we're trying
9477 to access a field belonging to an outer class, build
9478 the access to the field */
9479 if (!fs && outer_field_access_p (current_class, decl))
9481 if (CLASS_STATIC (TYPE_NAME (current_class)))
9483 static_ref_err (id, DECL_NAME (decl), current_class);
9484 return error_mark_node;
9486 access = build_outer_field_access (id, decl);
9487 if (orig)
9488 *orig = access;
9489 return access;
9492 /* Otherwise build what it takes to access the field */
9493 access = build_field_ref ((fs ? NULL_TREE : current_this),
9494 DECL_CONTEXT (decl), name);
9495 if (fs)
9496 access = maybe_build_class_init_for_field (decl, access);
9497 /* We may be asked to save the real field access node */
9498 if (orig)
9499 *orig = access;
9500 /* Last check: can we access the field? */
9501 if (not_accessible_p (current_class, decl, NULL_TREE, 0))
9503 not_accessible_field_error (id, decl);
9504 return error_mark_node;
9506 /* And we return what we got */
9507 return access;
9509 /* Fall down to error report on undefined variable */
9512 /* 6.5.5.2 Qualified Expression Names */
9513 else
9515 if (orig)
9516 *orig = NULL_TREE;
9517 qualify_ambiguous_name (id);
9518 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9519 /* 15.10.2: Accessing Superclass Members using super */
9520 return resolve_field_access (id, orig, NULL);
9523 /* We've got an error here */
9524 if (INNER_CLASS_TYPE_P (current_class))
9525 parse_error_context (id,
9526 "Local variable %qs can't be accessed from within the inner class %qs unless it is declared final",
9527 IDENTIFIER_POINTER (name),
9528 IDENTIFIER_POINTER (DECL_NAME
9529 (TYPE_NAME (current_class))));
9530 else
9531 parse_error_context (id, "Undefined variable %qs",
9532 IDENTIFIER_POINTER (name));
9534 return error_mark_node;
9537 static void
9538 static_ref_err (tree wfl, tree field_id, tree class_type)
9540 parse_error_context
9541 (wfl,
9542 "Can't make a static reference to nonstatic variable %qs in class %qs",
9543 IDENTIFIER_POINTER (field_id),
9544 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9547 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9548 We return something suitable to generate the field access. We also
9549 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9550 recipient's address can be null. */
9552 static tree
9553 resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9555 int is_static = 0;
9556 tree field_ref;
9557 tree decl = NULL_TREE, where_found, type_found;
9559 if (resolve_qualified_expression_name (qual_wfl, &decl,
9560 &where_found, &type_found))
9561 return error_mark_node;
9563 /* Resolve the LENGTH field of an array here */
9564 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9565 && type_found && TYPE_ARRAY_P (type_found)
9566 && ! flag_emit_class_files)
9568 tree length = build_java_array_length_access (where_found);
9569 field_ref = length;
9571 /* In case we're dealing with a static array, we need to
9572 initialize its class before the array length can be fetched.
9573 It's also a good time to create a DECL_RTL for the field if
9574 none already exists, otherwise if the field was declared in a
9575 class found in an external file and hasn't been (and won't
9576 be) accessed for its value, none will be created. */
9577 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9579 build_static_field_ref (where_found);
9580 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9583 /* We might have been trying to resolve field.method(). In which
9584 case, the resolution is over and decl is the answer */
9585 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9586 field_ref = decl;
9587 else if (JDECL_P (decl))
9589 if (!type_found)
9590 type_found = DECL_CONTEXT (decl);
9591 is_static = FIELD_STATIC (decl);
9592 field_ref = build_field_ref ((is_static ?
9593 NULL_TREE : where_found),
9594 type_found, DECL_NAME (decl));
9595 if (field_ref == error_mark_node)
9596 return error_mark_node;
9597 if (is_static)
9598 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9600 /* If we're looking at a static field, we may need to generate a
9601 class initialization for it. This can happen when the access
9602 looks like `field.ref', where `field' is a static field in an
9603 interface we implement. */
9604 if (!flag_emit_class_files
9605 && TREE_CODE (where_found) == VAR_DECL
9606 && FIELD_STATIC (where_found))
9608 build_static_field_ref (where_found);
9609 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9612 else
9613 field_ref = decl;
9615 if (field_decl)
9616 *field_decl = decl;
9617 if (field_type)
9618 *field_type = (QUAL_DECL_TYPE (decl) ?
9619 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9620 return field_ref;
9623 /* If NODE is an access to a static field, strip out the class
9624 initialization part and return the field decl, otherwise, return
9625 NODE. */
9627 tree
9628 extract_field_decl (tree node)
9630 if (TREE_CODE (node) == COMPOUND_EXPR)
9632 tree op1 = TREE_OPERAND (node, 1);
9633 if (TREE_CODE (op1) == COMPOUND_EXPR)
9635 tree call = TREE_OPERAND (op1, 0);
9636 if (TREE_CODE (call) == CALL_EXPR
9637 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9638 && (TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9639 == soft_initclass_node))
9640 return TREE_OPERAND (op1, 1);
9642 else if (JDECL_P (op1))
9643 return op1;
9645 return node;
9648 /* 6.5.5.2: Qualified Expression Names */
9650 static int
9651 resolve_qualified_expression_name (tree wfl, tree *found_decl,
9652 tree *where_found, tree *type_found)
9654 int from_type = 0; /* Field search initiated from a type */
9655 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9656 int previous_call_static = 0;
9657 int is_static;
9658 tree decl = NULL_TREE, type = NULL_TREE, q;
9659 /* For certain for of inner class instantiation */
9660 tree saved_current, saved_this;
9661 #define RESTORE_THIS_AND_CURRENT_CLASS \
9662 { current_class = saved_current; current_this = saved_this;}
9664 *type_found = *where_found = NULL_TREE;
9666 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9668 tree qual_wfl = QUAL_WFL (q);
9669 tree ret_decl; /* for EH checking */
9670 #ifdef USE_MAPPED_LOCATION
9671 source_location location; /* for EH checking */
9672 #else
9673 int location; /* for EH checking */
9674 #endif
9676 /* 15.10.1 Field Access Using a Primary */
9677 switch (TREE_CODE (qual_wfl))
9679 case CALL_EXPR:
9680 case NEW_CLASS_EXPR:
9681 /* If the access to the function call is a non static field,
9682 build the code to access it. */
9683 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9685 decl = maybe_access_field (decl, *where_found,
9686 DECL_CONTEXT (decl));
9687 if (decl == error_mark_node)
9688 return 1;
9691 /* And code for the function call */
9692 if (complete_function_arguments (qual_wfl))
9693 return 1;
9695 /* We might have to setup a new current class and a new this
9696 for the search of an inner class, relative to the type of
9697 a expression resolved as `decl'. The current values are
9698 saved and restored shortly after */
9699 saved_current = current_class;
9700 saved_this = current_this;
9701 if (decl
9702 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9703 || from_qualified_this))
9705 /* If we still have `from_qualified_this', we have the form
9706 <T>.this.f() and we need to build <T>.this */
9707 if (from_qualified_this)
9709 decl = build_access_to_thisn (current_class, type, 0);
9710 decl = java_complete_tree (decl);
9711 type = TREE_TYPE (TREE_TYPE (decl));
9713 current_class = type;
9714 current_this = decl;
9715 from_qualified_this = 0;
9718 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9719 CALL_USING_SUPER (qual_wfl) = 1;
9720 #ifdef USE_MAPPED_LOCATION
9721 location = (TREE_CODE (qual_wfl) == CALL_EXPR
9722 ? EXPR_LOCATION (TREE_OPERAND (qual_wfl, 0))
9723 : UNKNOWN_LOCATION);
9724 #else
9725 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9726 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9727 #endif
9728 *where_found = patch_method_invocation (qual_wfl, decl, type,
9729 from_super,
9730 &is_static, &ret_decl);
9731 from_super = 0;
9732 if (*where_found == error_mark_node)
9734 RESTORE_THIS_AND_CURRENT_CLASS;
9735 return 1;
9737 *type_found = type = QUAL_DECL_TYPE (*where_found);
9739 *where_found = force_evaluation_order (*where_found);
9741 /* If we're creating an inner class instance, check for that
9742 an enclosing instance is in scope */
9743 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9744 && INNER_ENCLOSING_SCOPE_CHECK (type))
9746 parse_error_context
9747 (qual_wfl, "No enclosing instance for inner class %qs is in scope%s",
9748 lang_printable_name (type, 0),
9749 (!current_this ? "" :
9750 "; an explicit one must be provided when creating this inner class"));
9751 RESTORE_THIS_AND_CURRENT_CLASS;
9752 return 1;
9755 /* In case we had to change then to resolve a inner class
9756 instantiation using a primary qualified by a `new' */
9757 RESTORE_THIS_AND_CURRENT_CLASS;
9759 #ifdef USE_MAPPED_LOCATION
9760 if (location != UNKNOWN_LOCATION)
9761 #else
9762 if (location)
9763 #endif
9765 tree arguments = NULL_TREE;
9766 if (TREE_CODE (qual_wfl) == CALL_EXPR
9767 && TREE_OPERAND (qual_wfl, 1) != NULL_TREE)
9768 arguments = TREE_VALUE (TREE_OPERAND (qual_wfl, 1));
9769 check_thrown_exceptions (location, ret_decl, arguments);
9772 /* If the previous call was static and this one is too,
9773 build a compound expression to hold the two (because in
9774 that case, previous function calls aren't transported as
9775 forcoming function's argument. */
9776 if (previous_call_static && is_static)
9778 /* We must set CAN_COMPLETE_NORMALLY for the first call
9779 since it is done nowhere else. */
9780 CAN_COMPLETE_NORMALLY (decl) = 1;
9781 decl = build2 (COMPOUND_EXPR, TREE_TYPE (*where_found),
9782 decl, *where_found);
9783 TREE_SIDE_EFFECTS (decl) = 1;
9785 else
9787 previous_call_static = is_static;
9788 decl = *where_found;
9790 from_type = 0;
9791 continue;
9793 case NEW_ARRAY_EXPR:
9794 case NEW_ANONYMOUS_ARRAY_EXPR:
9795 *where_found = decl = java_complete_tree (qual_wfl);
9796 if (decl == error_mark_node)
9797 return 1;
9798 *type_found = type = QUAL_DECL_TYPE (decl);
9799 continue;
9801 case CONVERT_EXPR:
9802 *where_found = decl = java_complete_tree (qual_wfl);
9803 if (decl == error_mark_node)
9804 return 1;
9805 *type_found = type = QUAL_DECL_TYPE (decl);
9806 from_cast = 1;
9807 continue;
9809 case CONDITIONAL_EXPR:
9810 case STRING_CST:
9811 case MODIFY_EXPR:
9812 *where_found = decl = java_complete_tree (qual_wfl);
9813 if (decl == error_mark_node)
9814 return 1;
9815 *type_found = type = QUAL_DECL_TYPE (decl);
9816 continue;
9818 case ARRAY_REF:
9819 /* If the access to the function call is a non static field,
9820 build the code to access it. */
9821 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9823 decl = maybe_access_field (decl, *where_found, type);
9824 if (decl == error_mark_node)
9825 return 1;
9827 /* And code for the array reference expression */
9828 decl = java_complete_tree (qual_wfl);
9829 if (decl == error_mark_node)
9830 return 1;
9831 type = QUAL_DECL_TYPE (decl);
9832 continue;
9834 case PLUS_EXPR:
9835 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9836 return 1;
9837 if ((type = patch_string (decl)))
9838 decl = type;
9839 *where_found = QUAL_RESOLUTION (q) = decl;
9840 *type_found = type = TREE_TYPE (decl);
9841 break;
9843 case CLASS_LITERAL:
9844 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9845 return 1;
9846 *where_found = QUAL_RESOLUTION (q) = decl;
9847 *type_found = type = TREE_TYPE (decl);
9848 break;
9850 default:
9851 /* Fix for -Wall Just go to the next statement. Don't
9852 continue */
9853 break;
9856 /* If we fall here, we weren't processing a (static) function call. */
9857 previous_call_static = 0;
9859 /* It can be the keyword THIS */
9860 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9861 && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9863 if (!current_this)
9865 parse_error_context
9866 (wfl, "Keyword %<this%> used outside allowed context");
9867 return 1;
9869 if (ctxp->explicit_constructor_p
9870 && type == current_class)
9872 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
9873 return 1;
9875 /* We have to generate code for intermediate access */
9876 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9878 *where_found = decl = current_this;
9879 *type_found = type = QUAL_DECL_TYPE (decl);
9881 /* We're trying to access the this from somewhere else. Make sure
9882 it's allowed before doing so. */
9883 else
9885 if (!enclosing_context_p (type, current_class))
9887 char *p = xstrdup (lang_printable_name (type, 0));
9888 parse_error_context (qual_wfl, "Can't use variable %<%s.this%>: type %qs isn't an outer type of type %qs",
9889 p, p,
9890 lang_printable_name (current_class, 0));
9891 free (p);
9892 return 1;
9894 from_qualified_this = 1;
9895 /* If there's nothing else after that, we need to
9896 produce something now, otherwise, the section of the
9897 code that needs to produce <T>.this will generate
9898 what is necessary. */
9899 if (!TREE_CHAIN (q))
9901 decl = build_access_to_thisn (current_class, type, 0);
9902 *where_found = decl = java_complete_tree (decl);
9903 *type_found = type = TREE_TYPE (decl);
9907 from_type = 0;
9908 continue;
9911 /* 15.10.2 Accessing Superclass Members using SUPER */
9912 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9913 && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9915 tree node;
9916 /* Check on the restricted use of SUPER */
9917 if (METHOD_STATIC (current_function_decl)
9918 || current_class == object_type_node)
9920 parse_error_context
9921 (wfl, "Keyword %<super%> used outside allowed context");
9922 return 1;
9924 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9925 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9926 CLASSTYPE_SUPER (current_class),
9927 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9928 *where_found = decl = java_complete_tree (node);
9929 if (decl == error_mark_node)
9930 return 1;
9931 *type_found = type = QUAL_DECL_TYPE (decl);
9932 from_super = from_type = 1;
9933 continue;
9936 /* 15.13.1: Can't search for field name in packages, so we
9937 assume a variable/class name was meant. */
9938 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9940 tree name;
9941 if ((decl = resolve_package (wfl, &q, &name)))
9943 tree list;
9944 *where_found = decl;
9946 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
9948 /* We want to be absolutely sure that the class is laid
9949 out. We're going to search something inside it. */
9950 *type_found = type = TREE_TYPE (decl);
9951 layout_class (type);
9952 from_type = 1;
9954 /* Fix them all the way down, if any are left. */
9955 if (q)
9957 list = TREE_CHAIN (q);
9958 while (list)
9960 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9961 list = TREE_CHAIN (list);
9965 else
9967 if (from_super || from_cast)
9968 parse_error_context
9969 ((from_cast ? qual_wfl : wfl),
9970 "No variable %qs defined in class %qs",
9971 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9972 lang_printable_name (type, 0));
9973 else
9974 parse_error_context
9975 (qual_wfl, "Undefined variable or class name: %qs",
9976 IDENTIFIER_POINTER (name));
9977 return 1;
9981 /* We have a type name. It's been already resolved when the
9982 expression was qualified. */
9983 else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
9985 decl = QUAL_RESOLUTION (q);
9987 /* Sneak preview. If next we see a `new', we're facing a
9988 qualification which resulted in a type being selected
9989 instead of a field. Report the error. */
9990 if(TREE_CHAIN (q)
9991 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9993 parse_error_context (qual_wfl, "Undefined variable %qs",
9994 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9995 return 1;
9998 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10000 check_deprecation (qual_wfl, decl);
10002 type = TREE_TYPE (decl);
10003 from_type = 1;
10005 /* We resolve an expression name */
10006 else
10008 tree field_decl = NULL_TREE;
10010 /* If there exists an early resolution, use it. That occurs
10011 only once and we know that there are more things to
10012 come. Don't do that when processing something after SUPER
10013 (we need more thing to be put in place below */
10014 if (!from_super && QUAL_RESOLUTION (q))
10016 decl = QUAL_RESOLUTION (q);
10017 if (!type)
10019 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
10021 if (current_this)
10022 *where_found = current_this;
10023 else
10025 static_ref_err (qual_wfl, DECL_NAME (decl),
10026 current_class);
10027 return 1;
10029 if (outer_field_access_p (current_class, decl))
10030 decl = build_outer_field_access (qual_wfl, decl);
10032 else
10034 *where_found = TREE_TYPE (decl);
10035 if (TREE_CODE (*where_found) == POINTER_TYPE)
10036 *where_found = TREE_TYPE (*where_found);
10041 /* Report and error if we're using a numerical literal as a
10042 qualifier. It can only be an INTEGER_CST. */
10043 else if (TREE_CODE (qual_wfl) == INTEGER_CST)
10045 parse_error_context
10046 (wfl, "Can't use type %qs as a qualifier",
10047 lang_printable_name (TREE_TYPE (qual_wfl), 0));
10048 return 1;
10051 /* We have to search for a field, knowing the type of its
10052 container. The flag FROM_TYPE indicates that we resolved
10053 the last member of the expression as a type name, which
10054 means that for the resolution of this field, we'll look
10055 for other errors than if it was resolved as a member of
10056 an other field. */
10057 else
10059 int is_static;
10060 tree field_decl_type; /* For layout */
10062 if (!from_type && !JREFERENCE_TYPE_P (type))
10064 parse_error_context
10065 (qual_wfl, "Attempt to reference field %qs in %<%s %s%>",
10066 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10067 lang_printable_name (type, 0),
10068 IDENTIFIER_POINTER (DECL_NAME (decl)));
10069 return 1;
10072 field_decl = lookup_field_wrapper (type,
10073 EXPR_WFL_NODE (qual_wfl));
10075 /* Maybe what we're trying to access to is an inner
10076 class, only if decl is a TYPE_DECL. */
10077 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
10079 tree ptr, inner_decl;
10081 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
10082 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
10083 if (inner_decl)
10085 check_inner_class_access (inner_decl, decl, qual_wfl);
10086 type = TREE_TYPE (inner_decl);
10087 decl = inner_decl;
10088 from_type = 1;
10089 continue;
10093 if (field_decl == NULL_TREE)
10095 parse_error_context
10096 (qual_wfl, "No variable %qs defined in type %qs",
10097 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10098 GET_TYPE_NAME (type));
10099 return 1;
10101 if (field_decl == error_mark_node)
10102 return 1;
10104 /* Layout the type of field_decl, since we may need
10105 it. Don't do primitive types or loaded classes. The
10106 situation of non primitive arrays may not handled
10107 properly here. FIXME */
10108 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10109 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
10110 else
10111 field_decl_type = TREE_TYPE (field_decl);
10112 if (!JPRIMITIVE_TYPE_P (field_decl_type)
10113 && !CLASS_LOADED_P (field_decl_type)
10114 && !TYPE_ARRAY_P (field_decl_type))
10115 resolve_and_layout (field_decl_type, NULL_TREE);
10117 /* Check on accessibility here */
10118 if (not_accessible_p (current_class, field_decl,
10119 *type_found, from_super))
10120 return not_accessible_field_error (qual_wfl,field_decl);
10121 check_deprecation (qual_wfl, field_decl);
10123 /* There are things to check when fields are accessed
10124 from type. There are no restrictions on a static
10125 declaration of the field when it is accessed from an
10126 interface */
10127 is_static = FIELD_STATIC (field_decl);
10128 if (!from_super && from_type
10129 && !TYPE_INTERFACE_P (type)
10130 && !is_static
10131 && (current_function_decl
10132 && METHOD_STATIC (current_function_decl)))
10134 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
10135 return 1;
10137 from_cast = from_super = 0;
10139 /* It's an access from a type but it isn't static, we
10140 make it relative to `this'. */
10141 if (!is_static && from_type)
10142 decl = current_this;
10144 /* If we need to generate something to get a proper
10145 handle on what this field is accessed from, do it
10146 now. */
10147 if (!is_static)
10149 decl = maybe_access_field (decl, *where_found, *type_found);
10150 if (decl == error_mark_node)
10151 return 1;
10154 /* We want to keep the location were found it, and the type
10155 we found. */
10156 *where_found = decl;
10157 *type_found = type;
10159 /* Generate the correct expression for field access from
10160 qualified this */
10161 if (from_qualified_this)
10163 field_decl = build_outer_field_access (qual_wfl, field_decl);
10164 from_qualified_this = 0;
10167 /* This is the decl found and eventually the next one to
10168 search from */
10169 decl = field_decl;
10171 from_type = 0;
10172 type = QUAL_DECL_TYPE (decl);
10174 /* Sneak preview. If decl is qualified by a `new', report
10175 the error here to be accurate on the peculiar construct */
10176 if (TREE_CHAIN (q)
10177 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
10178 && !JREFERENCE_TYPE_P (type))
10180 parse_error_context (qual_wfl, "Attempt to reference field %<new%> in a %qs",
10181 lang_printable_name (type, 0));
10182 return 1;
10185 /* `q' might have changed due to a after package resolution
10186 re-qualification */
10187 if (!q)
10188 break;
10190 *found_decl = decl;
10191 return 0;
10194 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
10195 can't be accessed from REFERENCE (a record type). If MEMBER
10196 features a protected access, we then use WHERE which, if non null,
10197 holds the type of MEMBER's access that is checked against
10198 6.6.2.1. This function should be used when decl is a field or a
10199 method. */
10201 static int
10202 not_accessible_p (tree reference, tree member, tree where, int from_super)
10204 int access_flag = get_access_flags_from_decl (member);
10205 bool is_static = false;
10207 if (TREE_CODE (member) == FIELD_DECL ||
10208 TREE_CODE (member) == VAR_DECL)
10209 is_static = FIELD_STATIC (member);
10210 else
10211 is_static = METHOD_STATIC (member);
10213 /* Access always granted for members declared public */
10214 if (access_flag & ACC_PUBLIC)
10215 return 0;
10217 /* Check access on protected members */
10218 if (access_flag & ACC_PROTECTED)
10220 /* Access granted if it occurs from within the package
10221 containing the class in which the protected member is
10222 declared */
10223 if (class_in_current_package (DECL_CONTEXT (member)))
10224 return 0;
10226 /* If accessed with the form `super.member', then access is granted */
10227 if (from_super)
10228 return 0;
10230 /* If WHERE is active, access was made through a qualifier. For
10231 non-static members, access is granted if the type of the qualifier
10232 is or is a sublass of the type the access is made from (6.6.2.1.) */
10233 if (where && !is_static)
10235 while (reference)
10237 if (inherits_from_p (where, reference))
10238 return 0;
10239 if (INNER_CLASS_TYPE_P (reference))
10240 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10241 else
10242 break;
10244 return 1;
10247 /* Otherwise, access is granted if occurring from within the class
10248 where member is declared, or a subclass of it. */
10249 while (reference)
10251 if (inherits_from_p (reference, DECL_CONTEXT (member)))
10252 return 0;
10253 if (INNER_CLASS_TYPE_P (reference))
10254 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10255 else
10256 break;
10258 return 1;
10261 /* Check access on private members. Access is granted only if it
10262 occurs from within the class in which it is declared -- that does
10263 it for innerclasses too. */
10264 if (access_flag & ACC_PRIVATE)
10266 if (reference == DECL_CONTEXT (member) ||
10267 common_enclosing_context_p (DECL_CONTEXT (member), reference))
10268 return 0;
10269 return 1;
10272 /* Default access is permitted only when occurring from within the
10273 package in which the context (MEMBER) is declared. */
10274 return !class_in_current_package (DECL_CONTEXT (member));
10277 /* Test deprecated decl access. */
10278 static void
10279 check_deprecation (tree wfl, tree decl)
10281 const char *file;
10282 tree elt;
10284 if (! warn_deprecated)
10285 return;
10287 /* We want to look at the element type of arrays here, so we strip
10288 all surrounding array types. */
10289 if (TYPE_ARRAY_P (TREE_TYPE (decl)))
10291 elt = TREE_TYPE (decl);
10292 while (TYPE_ARRAY_P (elt))
10293 elt = TYPE_ARRAY_ELEMENT (elt);
10294 /* We'll end up with a pointer type, so we use TREE_TYPE to go
10295 to the record. */
10296 decl = TYPE_NAME (TREE_TYPE (elt));
10298 file = DECL_SOURCE_FILE (decl);
10300 /* Complain if the field is deprecated and the file it was defined
10301 in isn't compiled at the same time the file which contains its
10302 use is */
10303 if (DECL_DEPRECATED (decl)
10304 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10306 const char *the;
10307 switch (TREE_CODE (decl))
10309 case FUNCTION_DECL:
10310 the = "method";
10311 break;
10312 case FIELD_DECL:
10313 case VAR_DECL:
10314 the = "field";
10315 break;
10316 case TYPE_DECL:
10317 parse_warning_context (wfl, "The class %qs has been deprecated",
10318 IDENTIFIER_POINTER (DECL_NAME (decl)));
10319 return;
10320 default:
10321 abort ();
10323 /* Don't issue a message if the context as been deprecated as a
10324 whole. */
10325 if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10326 parse_warning_context
10327 (wfl, "The %s %qs in class %qs has been deprecated",
10328 the, lang_printable_name (decl, 0),
10329 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10333 /* Returns 1 if class was declared in the current package, 0 otherwise */
10335 static GTY(()) tree cicp_cache;
10336 static int
10337 class_in_current_package (tree class)
10339 int qualified_flag;
10340 tree left;
10342 if (cicp_cache == class)
10343 return 1;
10345 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10347 /* If the current package is empty and the name of CLASS is
10348 qualified, class isn't in the current package. If there is a
10349 current package and the name of the CLASS is not qualified, class
10350 isn't in the current package */
10351 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10352 return 0;
10354 /* If there is not package and the name of CLASS isn't qualified,
10355 they belong to the same unnamed package */
10356 if (!ctxp->package && !qualified_flag)
10357 return 1;
10359 /* Compare the left part of the name of CLASS with the package name */
10360 split_qualified_name (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10361 if (ctxp->package == left)
10363 cicp_cache = class;
10364 return 1;
10366 return 0;
10369 /* This function may generate code to access DECL from WHERE. This is
10370 done only if certain conditions meet. */
10372 static tree
10373 maybe_access_field (tree decl, tree where, tree type)
10375 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10376 && !FIELD_STATIC (decl))
10377 decl = build_field_ref (where ? where : current_this,
10378 (type ? type : DECL_CONTEXT (decl)),
10379 DECL_NAME (decl));
10380 return decl;
10383 /* Build a method invocation, by patching PATCH. If non NULL
10384 and according to the situation, PRIMARY and WHERE may be
10385 used. IS_STATIC is set to 1 if the invoked function is static. */
10387 static tree
10388 patch_method_invocation (tree patch, tree primary, tree where, int from_super,
10389 int *is_static, tree *ret_decl)
10391 tree wfl = TREE_OPERAND (patch, 0);
10392 tree args = TREE_OPERAND (patch, 1);
10393 tree name = EXPR_WFL_NODE (wfl);
10394 tree list;
10395 int is_static_flag = 0;
10396 int is_super_init = 0;
10397 tree this_arg = NULL_TREE;
10398 int is_array_clone_call = 0;
10400 /* Should be overridden if everything goes well. Otherwise, if
10401 something fails, it should keep this value. It stop the
10402 evaluation of a bogus assignment. See java_complete_tree,
10403 MODIFY_EXPR: for the reasons why we sometimes want to keep on
10404 evaluating an assignment */
10405 TREE_TYPE (patch) = error_mark_node;
10407 /* Since lookup functions are messing with line numbers, save the
10408 context now. */
10409 java_parser_context_save_global ();
10411 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10413 /* Resolution of qualified name, excluding constructors */
10414 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10416 tree identifier, identifier_wfl, type, resolved;
10417 /* Extract the last IDENTIFIER of the qualified
10418 expression. This is a wfl and we will use it's location
10419 data during error report. */
10420 identifier_wfl = cut_identifier_in_qualified (wfl);
10421 identifier = EXPR_WFL_NODE (identifier_wfl);
10423 /* Given the context, IDENTIFIER is syntactically qualified
10424 as a MethodName. We need to qualify what's before */
10425 qualify_ambiguous_name (wfl);
10426 resolved = resolve_field_access (wfl, NULL, NULL);
10428 if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10429 && FIELD_FINAL (resolved)
10430 && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10431 && !flag_emit_class_files)
10432 resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10434 if (resolved == error_mark_node)
10435 PATCH_METHOD_RETURN_ERROR ();
10437 type = GET_SKIP_TYPE (resolved);
10438 resolve_and_layout (type, NULL_TREE);
10440 if (JPRIMITIVE_TYPE_P (type))
10442 parse_error_context
10443 (identifier_wfl,
10444 "Can't invoke a method on primitive type %qs",
10445 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10446 PATCH_METHOD_RETURN_ERROR ();
10449 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10450 args = nreverse (args);
10452 /* We're resolving a call from a type */
10453 if (TREE_CODE (resolved) == TYPE_DECL)
10455 if (CLASS_INTERFACE (resolved))
10457 parse_error_context
10458 (identifier_wfl,
10459 "Can't make static reference to method %qs in interface %qs",
10460 IDENTIFIER_POINTER (identifier),
10461 IDENTIFIER_POINTER (name));
10462 PATCH_METHOD_RETURN_ERROR ();
10464 if (list && !METHOD_STATIC (list))
10466 char *fct_name = xstrdup (lang_printable_name (list, 2));
10467 parse_error_context
10468 (identifier_wfl,
10469 "Can't make static reference to method %<%s %s%> in class %qs",
10470 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10471 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10472 free (fct_name);
10473 PATCH_METHOD_RETURN_ERROR ();
10476 else
10477 this_arg = primary = resolved;
10479 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10480 is_array_clone_call = 1;
10482 /* IDENTIFIER_WFL will be used to report any problem further */
10483 wfl = identifier_wfl;
10485 /* Resolution of simple names, names generated after a primary: or
10486 constructors */
10487 else
10489 tree class_to_search = NULL_TREE;
10490 int lc; /* Looking for Constructor */
10492 /* We search constructor in their target class */
10493 if (CALL_CONSTRUCTOR_P (patch))
10495 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10496 class_to_search = EXPR_WFL_NODE (wfl);
10497 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10498 this_identifier_node)
10499 class_to_search = NULL_TREE;
10500 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10501 super_identifier_node)
10503 is_super_init = 1;
10504 if (CLASSTYPE_SUPER (current_class))
10505 class_to_search =
10506 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10507 else
10509 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10510 PATCH_METHOD_RETURN_ERROR ();
10514 /* Class to search is NULL if we're searching the current one */
10515 if (class_to_search)
10517 class_to_search = resolve_and_layout (class_to_search, wfl);
10519 if (!class_to_search)
10521 parse_error_context
10522 (wfl, "Class %qs not found in type declaration",
10523 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10524 PATCH_METHOD_RETURN_ERROR ();
10527 /* Can't instantiate an abstract class, but we can
10528 invoke it's constructor. It's use within the `new'
10529 context is denied here. */
10530 if (CLASS_ABSTRACT (class_to_search)
10531 && TREE_CODE (patch) == NEW_CLASS_EXPR)
10533 parse_error_context
10534 (wfl, "Class %qs is an abstract class. It can't be instantiated",
10535 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10536 PATCH_METHOD_RETURN_ERROR ();
10539 class_to_search = TREE_TYPE (class_to_search);
10541 else
10542 class_to_search = current_class;
10543 lc = 1;
10545 /* This is a regular search in the local class, unless an
10546 alternate class is specified. */
10547 else
10549 if (where != NULL_TREE)
10550 class_to_search = where;
10551 else if (QUALIFIED_P (name))
10552 class_to_search = current_class;
10553 else
10555 class_to_search = current_class;
10557 for (;;)
10559 if (has_method (class_to_search, name))
10560 break;
10561 if (! INNER_CLASS_TYPE_P (class_to_search))
10563 parse_error_context (wfl,
10564 "No method named %qs in scope",
10565 IDENTIFIER_POINTER (name));
10566 PATCH_METHOD_RETURN_ERROR ();
10568 class_to_search
10569 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10572 lc = 0;
10575 /* NAME is a simple identifier or comes from a primary. Search
10576 in the class whose declaration contain the method being
10577 invoked. */
10578 resolve_and_layout (class_to_search, NULL_TREE);
10580 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10581 /* Don't continue if no method were found, as the next statement
10582 can't be executed then. */
10583 if (!list)
10584 PATCH_METHOD_RETURN_ERROR ();
10586 if (TYPE_ARRAY_P (class_to_search)
10587 && DECL_NAME (list) == get_identifier ("clone"))
10588 is_array_clone_call = 1;
10590 /* Check for static reference if non static methods */
10591 if (check_for_static_method_reference (wfl, patch, list,
10592 class_to_search, primary))
10593 PATCH_METHOD_RETURN_ERROR ();
10595 /* Check for inner classes creation from illegal contexts */
10596 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10597 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10598 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10599 && !DECL_INIT_P (current_function_decl))
10601 parse_error_context
10602 (wfl, "No enclosing instance for inner class %qs is in scope%s",
10603 lang_printable_name (class_to_search, 0),
10604 (!current_this ? "" :
10605 "; an explicit one must be provided when creating this inner class"));
10606 PATCH_METHOD_RETURN_ERROR ();
10609 /* Non static methods are called with the current object extra
10610 argument. If patch a `new TYPE()', the argument is the value
10611 returned by the object allocator. If method is resolved as a
10612 primary, use the primary otherwise use the current THIS. */
10613 args = nreverse (args);
10614 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10616 this_arg = primary ? primary : current_this;
10618 /* If we're using an access method, things are different.
10619 There are two family of cases:
10621 1) We're not generating bytecodes:
10623 - LIST is non static. It's invocation is transformed from
10624 x(a1,...,an) into this$<n>.x(a1,....an).
10625 - LIST is static. It's invocation is transformed from
10626 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10628 2) We're generating bytecodes:
10630 - LIST is non static. It's invocation is transformed from
10631 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10632 - LIST is static. It's invocation is transformed from
10633 x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10635 Of course, this$<n> can be arbitrarily complex, ranging from
10636 this$0 (the immediate outer context) to
10637 access$0(access$0(...(this$0))).
10639 maybe_use_access_method returns a nonzero value if the
10640 this_arg has to be moved into the (then generated) stub
10641 argument list. In the meantime, the selected function
10642 might have be replaced by a generated stub. */
10643 if (!primary &&
10644 maybe_use_access_method (is_super_init, &list, &this_arg))
10646 args = tree_cons (NULL_TREE, this_arg, args);
10647 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10652 /* Merge point of all resolution schemes. If we have nothing, this
10653 is an error, already signaled */
10654 if (!list)
10655 PATCH_METHOD_RETURN_ERROR ();
10657 /* Check accessibility, position the is_static flag, build and
10658 return the call */
10659 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10660 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10661 NULL_TREE), from_super)
10662 /* Calls to clone() on array types are permitted as a special-case. */
10663 && !is_array_clone_call)
10665 const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10666 const char *const access =
10667 accessibility_string (get_access_flags_from_decl (list));
10668 const char *const klass =
10669 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10670 const char *const refklass =
10671 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10672 const char *const what = (DECL_CONSTRUCTOR_P (list)
10673 ? "constructor" : "method");
10674 parse_error_context (wfl,
10675 "Can't access %s %s %<%s.%s%> from %qs",
10676 access, what, klass, fct_name, refklass);
10677 PATCH_METHOD_RETURN_ERROR ();
10680 /* Deprecation check: check whether the method being invoked or the
10681 instance-being-created's type are deprecated. */
10682 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10683 check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10684 check_deprecation (wfl, list);
10686 /* If invoking a innerclass constructor, there are hidden parameters
10687 to pass */
10688 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10689 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10691 /* And make sure we add the accessed local variables to be saved
10692 in field aliases. */
10693 args = build_alias_initializer_parameter_list
10694 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10696 /* Secretly pass the current_this/primary as a second argument */
10697 if (primary || current_this)
10699 tree extra_arg;
10700 tree this_type = (current_this ?
10701 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10702 /* Method's (list) enclosing context */
10703 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10704 /* If we have a primary, use it. */
10705 if (primary)
10706 extra_arg = primary;
10707 /* The current `this' is an inner class but isn't a direct
10708 enclosing context for the inner class we're trying to
10709 create. Build an access to the proper enclosing context
10710 and use it. */
10711 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10712 && this_type != TREE_TYPE (mec))
10715 extra_arg = build_access_to_thisn (current_class,
10716 TREE_TYPE (mec), 0);
10717 extra_arg = java_complete_tree (extra_arg);
10719 /* Otherwise, just use the current `this' as an enclosing
10720 context. */
10721 else
10722 extra_arg = current_this;
10723 args = tree_cons (NULL_TREE, extra_arg, args);
10725 else
10726 args = tree_cons (NULL_TREE, integer_zero_node, args);
10729 /* This handles the situation where a constructor invocation needs
10730 to have an enclosing context passed as a second parameter (the
10731 constructor is one of an inner class). */
10732 if ((is_super_init ||
10733 (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10734 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10736 tree dest = TYPE_NAME (DECL_CONTEXT (list));
10737 tree extra_arg =
10738 build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10739 extra_arg = java_complete_tree (extra_arg);
10740 args = tree_cons (NULL_TREE, extra_arg, args);
10743 is_static_flag = METHOD_STATIC (list);
10744 if (! is_static_flag && this_arg != NULL_TREE)
10745 args = tree_cons (NULL_TREE, this_arg, args);
10747 /* In the context of an explicit constructor invocation, we can't
10748 invoke any method relying on `this'. Exceptions are: we're
10749 invoking a static function, primary exists and is not the current
10750 this, we're creating a new object. */
10751 if (ctxp->explicit_constructor_p
10752 && !is_static_flag
10753 && (!primary || primary == current_this)
10754 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10756 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
10757 PATCH_METHOD_RETURN_ERROR ();
10759 java_parser_context_restore_global ();
10760 if (is_static)
10761 *is_static = is_static_flag;
10762 /* Sometimes, we want the decl of the selected method. Such as for
10763 EH checking */
10764 if (ret_decl)
10765 *ret_decl = list;
10766 patch = patch_invoke (patch, list, args);
10768 /* Now is a good time to insert the call to finit$ */
10769 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10771 tree finit_parms, finit_call;
10773 /* Prepare to pass hidden parameters to finit$, if any. */
10774 finit_parms = build_alias_initializer_parameter_list
10775 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10777 finit_call =
10778 build_method_invocation (build_wfl_node (finit_identifier_node),
10779 finit_parms);
10781 /* Generate the code used to initialize fields declared with an
10782 initialization statement and build a compound statement along
10783 with the super constructor invocation. */
10784 CAN_COMPLETE_NORMALLY (patch) = 1;
10785 patch = build2 (COMPOUND_EXPR, void_type_node, patch,
10786 java_complete_tree (finit_call));
10788 return patch;
10791 /* Check that we're not trying to do a static reference to a method in
10792 non static method. Return 1 if it's the case, 0 otherwise. */
10794 static int
10795 check_for_static_method_reference (tree wfl, tree node, tree method,
10796 tree where, tree primary)
10798 if (METHOD_STATIC (current_function_decl)
10799 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10801 char *fct_name = xstrdup (lang_printable_name (method, 0));
10802 parse_error_context
10803 (wfl, "Can't make static reference to method %<%s %s%> in class %qs",
10804 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10805 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10806 free (fct_name);
10807 return 1;
10809 return 0;
10812 /* Fix the invocation of *MDECL if necessary in the case of a
10813 invocation from an inner class. *THIS_ARG might be modified
10814 appropriately and an alternative access to *MDECL might be
10815 returned. */
10817 static int
10818 maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10820 tree ctx;
10821 tree md = *mdecl, ta = *this_arg;
10822 int to_return = 0;
10823 int non_static_context = !METHOD_STATIC (md);
10825 if (is_super_init
10826 || DECL_CONTEXT (md) == current_class
10827 || !PURE_INNER_CLASS_TYPE_P (current_class)
10828 || DECL_FINIT_P (md)
10829 || DECL_INSTINIT_P (md))
10830 return 0;
10832 /* If we're calling a method found in an enclosing class, generate
10833 what it takes to retrieve the right this. Don't do that if we're
10834 invoking a static method. Note that if MD's type is unrelated to
10835 CURRENT_CLASS, then the current this can be used. */
10837 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10839 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10840 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10842 ta = build_current_thisn (current_class);
10843 ta = build_wfl_node (ta);
10845 else
10847 tree type = ctx;
10848 while (type)
10850 maybe_build_thisn_access_method (type);
10851 if (inherits_from_p (type, DECL_CONTEXT (md)))
10853 ta = build_access_to_thisn (ctx, type, 0);
10854 break;
10856 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10857 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10860 ta = java_complete_tree (ta);
10863 /* We might have to use an access method to get to MD. We can
10864 break the method access rule as far as we're not generating
10865 bytecode */
10866 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10868 md = build_outer_method_access_method (md);
10869 to_return = 1;
10872 *mdecl = md;
10873 *this_arg = ta;
10875 /* Returning a nonzero value indicates we were doing a non static
10876 method invocation that is now a static invocation. It will have
10877 callee displace `this' to insert it in the regular argument
10878 list. */
10879 return (non_static_context && to_return);
10882 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10883 mode. */
10885 static tree
10886 patch_invoke (tree patch, tree method, tree args)
10888 tree dtable, func;
10889 tree original_call, t, ta;
10890 tree check = NULL_TREE;
10892 /* Last step for args: convert build-in types. If we're dealing with
10893 a new TYPE() type call, the first argument to the constructor
10894 isn't found in the incoming argument list, but delivered by
10895 `new' */
10896 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10897 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10898 t = TREE_CHAIN (t);
10899 for (ta = args; t != end_params_node && ta;
10900 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10901 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10902 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10903 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10905 /* Resolve unresolved returned type issues */
10906 t = TREE_TYPE (TREE_TYPE (method));
10907 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10908 resolve_and_layout (TREE_TYPE (t), NULL);
10910 if (flag_emit_class_files)
10911 func = method;
10912 else
10914 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10916 case INVOKE_VIRTUAL:
10917 dtable = invoke_build_dtable (0, args);
10918 func = build_invokevirtual (dtable, method);
10919 break;
10921 case INVOKE_NONVIRTUAL:
10922 /* If the object for the method call is null, we throw an
10923 exception. We don't do this if the object is the current
10924 method's `this'. In other cases we just rely on an
10925 optimization pass to eliminate redundant checks. */
10926 if (TREE_VALUE (args) != current_this)
10928 /* We use a save_expr here to make sure we only evaluate
10929 the new `self' expression once. */
10930 tree save_arg = save_expr (TREE_VALUE (args));
10931 TREE_VALUE (args) = save_arg;
10932 check = java_check_reference (save_arg, 1);
10934 /* Fall through. */
10936 case INVOKE_SUPER:
10937 case INVOKE_STATIC:
10939 tree signature = build_java_signature (TREE_TYPE (method));
10940 func = build_known_method_ref (method, TREE_TYPE (method),
10941 DECL_CONTEXT (method),
10942 signature, args);
10944 break;
10946 case INVOKE_INTERFACE:
10947 dtable = invoke_build_dtable (1, args);
10948 func = build_invokeinterface (dtable, method);
10949 break;
10951 default:
10952 abort ();
10955 /* Ensure self_type is initialized, (invokestatic). FIXME */
10956 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10959 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10960 TREE_OPERAND (patch, 0) = func;
10961 TREE_OPERAND (patch, 1) = args;
10962 patch = check_for_builtin (method, patch);
10963 original_call = patch;
10965 /* We're processing a `new TYPE ()' form. New is called and its
10966 returned value is the first argument to the constructor. We build
10967 a COMPOUND_EXPR and use saved expression so that the overall NEW
10968 expression value is a pointer to a newly created and initialized
10969 class. */
10970 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10972 tree class = DECL_CONTEXT (method);
10973 tree c1, saved_new, new;
10974 tree alloc_node;
10976 if (flag_emit_class_files)
10978 TREE_TYPE (patch) = build_pointer_type (class);
10979 return patch;
10981 if (!TYPE_SIZE (class))
10982 safe_layout_class (class);
10983 alloc_node =
10984 (class_has_finalize_method (class) ? alloc_object_node
10985 : alloc_no_finalizer_node);
10986 new = build3 (CALL_EXPR, promote_type (class),
10987 build_address_of (alloc_node),
10988 build_tree_list (NULL_TREE, build_class_ref (class)),
10989 NULL_TREE);
10990 saved_new = save_expr (new);
10991 c1 = build_tree_list (NULL_TREE, saved_new);
10992 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10993 TREE_OPERAND (original_call, 1) = c1;
10994 TREE_SET_CODE (original_call, CALL_EXPR);
10995 patch = build2 (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10998 /* If CHECK is set, then we are building a check to see if the object
10999 is NULL. */
11000 if (check != NULL_TREE)
11002 /* We have to call force_evaluation_order now because creating a
11003 COMPOUND_EXPR wraps the arg list in a way that makes it
11004 unrecognizable by force_evaluation_order later. Yuk. */
11005 patch = build2 (COMPOUND_EXPR, TREE_TYPE (patch), check,
11006 force_evaluation_order (patch));
11007 TREE_SIDE_EFFECTS (patch) = 1;
11010 /* In order to be able to modify PATCH later, we SAVE_EXPR it and
11011 put it as the first expression of a COMPOUND_EXPR. The second
11012 expression being an empty statement to be later patched if
11013 necessary. We remember a TREE_LIST (the PURPOSE is the method,
11014 the VALUE is the compound) in a hashtable and return a
11015 COMPOUND_EXPR built so that the result of the evaluation of the
11016 original PATCH node is returned. */
11017 if (STATIC_CLASS_INIT_OPT_P ()
11018 && current_function_decl && METHOD_STATIC (method))
11020 tree list;
11021 tree fndecl = current_function_decl;
11022 /* We have to call force_evaluation_order now because creating a
11023 COMPOUND_EXPR wraps the arg list in a way that makes it
11024 unrecognizable by force_evaluation_order later. Yuk. */
11025 tree save = force_evaluation_order (patch);
11026 tree type = TREE_TYPE (patch);
11028 patch = build2 (COMPOUND_EXPR, type, save, build_java_empty_stmt ());
11029 list = tree_cons (method, patch,
11030 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
11032 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
11034 patch = build2 (COMPOUND_EXPR, type, patch, save);
11037 return patch;
11040 static int
11041 invocation_mode (tree method, int super)
11043 int access = get_access_flags_from_decl (method);
11045 if (super)
11046 return INVOKE_SUPER;
11048 if (access & ACC_STATIC)
11049 return INVOKE_STATIC;
11051 /* We have to look for a constructor before we handle nonvirtual
11052 calls; otherwise the constructor will look nonvirtual. */
11053 if (DECL_CONSTRUCTOR_P (method))
11054 return INVOKE_STATIC;
11056 if (access & ACC_PRIVATE)
11057 return INVOKE_NONVIRTUAL;
11059 /* Binary compatibility: just because it's final today, that doesn't
11060 mean it'll be final tomorrow. */
11061 if (! flag_indirect_dispatch
11062 || DECL_CONTEXT (method) == object_type_node)
11064 if (access & ACC_FINAL)
11065 return INVOKE_NONVIRTUAL;
11067 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
11068 return INVOKE_NONVIRTUAL;
11071 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
11072 return INVOKE_INTERFACE;
11074 return INVOKE_VIRTUAL;
11077 /* Retrieve a refined list of matching methods. It covers the step
11078 15.11.2 (Compile-Time Step 2) */
11080 static tree
11081 lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
11083 tree atl = end_params_node; /* Arg Type List */
11084 tree method, signature, list, node;
11085 const char *candidates; /* Used for error report */
11086 char *dup;
11088 /* Fix the arguments */
11089 for (node = arg_list; node; node = TREE_CHAIN (node))
11091 tree current_arg = TREE_TYPE (TREE_VALUE (node));
11092 /* Non primitive type may have to be resolved */
11093 if (!JPRIMITIVE_TYPE_P (current_arg))
11094 resolve_and_layout (current_arg, NULL_TREE);
11095 /* And promoted */
11096 if (TREE_CODE (current_arg) == RECORD_TYPE)
11097 current_arg = promote_type (current_arg);
11098 atl = tree_cons (NULL_TREE, current_arg, atl);
11101 /* Presto. If we're dealing with an anonymous class and a
11102 constructor call, generate the right constructor now, since we
11103 know the arguments' types. */
11105 if (lc && ANONYMOUS_CLASS_P (class))
11107 tree mdecl = craft_constructor (TYPE_NAME (class), atl);
11108 /* The anonymous class may have already been laid out, so make sure
11109 the new constructor is laid out here. */
11110 layout_class_method (class, CLASSTYPE_SUPER (class), mdecl, NULL_TREE);
11113 /* Find all candidates and then refine the list, searching for the
11114 most specific method. */
11115 list = find_applicable_accessible_methods_list (lc, class, name, atl);
11116 list = find_most_specific_methods_list (list);
11117 if (list && !TREE_CHAIN (list))
11118 return TREE_VALUE (list);
11120 /* Issue an error. List candidates if any. Candidates are listed
11121 only if accessible (non accessible methods may end-up here for
11122 the sake of a better error report). */
11123 candidates = NULL;
11124 if (list)
11126 tree current;
11127 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
11128 for (current = list; current; current = TREE_CHAIN (current))
11130 tree cm = TREE_VALUE (current);
11131 char string [4096];
11132 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
11133 continue;
11134 sprintf
11135 (string, " '%s' in '%s'%s",
11136 get_printable_method_name (cm),
11137 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
11138 (TREE_CHAIN (current) ? "\n" : ""));
11139 obstack_grow (&temporary_obstack, string, strlen (string));
11141 obstack_1grow (&temporary_obstack, '\0');
11142 candidates = obstack_finish (&temporary_obstack);
11144 /* Issue the error message */
11145 method = make_node (FUNCTION_TYPE);
11146 TYPE_ARG_TYPES (method) = atl;
11147 signature = build_java_argument_signature (method);
11148 dup = xstrdup (lang_printable_name (class, 0));
11149 parse_error_context (cl, "Can't find %s %<%s(%s)%> in type %qs%s",
11150 (lc ? "constructor" : "method"),
11151 (lc ? dup : IDENTIFIER_POINTER (name)),
11152 IDENTIFIER_POINTER (signature), dup,
11153 (candidates ? candidates : ""));
11154 free (dup);
11155 return NULL_TREE;
11158 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
11159 when we're looking for a constructor. */
11161 static tree
11162 find_applicable_accessible_methods_list (int lc, tree class, tree name,
11163 tree arglist)
11165 static htab_t searched_classes;
11166 static int search_not_done = 0;
11167 tree list = NULL_TREE, all_list = NULL_TREE;
11168 tree base_binfo;
11169 int i;
11171 /* Check the hash table to determine if this class has been searched
11172 already. */
11173 if (searched_classes)
11175 if (htab_find (searched_classes, class) != NULL)
11176 return NULL;
11178 else
11180 searched_classes = htab_create (10, htab_hash_pointer,
11181 htab_eq_pointer, NULL);
11184 search_not_done++;
11185 *htab_find_slot (searched_classes, class, INSERT) = class;
11187 if (!CLASS_LOADED_P (class))
11189 load_class (class, 1);
11190 safe_layout_class (class);
11193 /* Search interfaces */
11194 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
11195 && CLASS_INTERFACE (TYPE_NAME (class)))
11197 search_applicable_methods_list (lc, TYPE_METHODS (class),
11198 name, arglist, &list, &all_list);
11199 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11201 tree t = BINFO_TYPE (base_binfo);
11202 tree rlist;
11204 rlist = find_applicable_accessible_methods_list (lc, t, name,
11205 arglist);
11206 list = chainon (rlist, list);
11209 /* Search classes */
11210 else
11212 search_applicable_methods_list (lc, TYPE_METHODS (class),
11213 name, arglist, &list, &all_list);
11215 /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11216 that we only search in class. Note that we should have found
11217 something at this point. */
11218 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11220 lc = 1;
11221 if (!list)
11222 abort ();
11225 /* We must search all interfaces of this class */
11226 if (!lc)
11228 for (i = 1;
11229 BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11231 tree t = BINFO_TYPE (base_binfo);
11232 if (t != object_type_node)
11234 tree rlist
11235 = find_applicable_accessible_methods_list (lc, t,
11236 name, arglist);
11237 list = chainon (rlist, list);
11242 /* Search superclass */
11243 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11245 tree rlist;
11246 class = CLASSTYPE_SUPER (class);
11247 rlist = find_applicable_accessible_methods_list (lc, class,
11248 name, arglist);
11249 list = chainon (rlist, list);
11253 search_not_done--;
11255 /* We're done. Reset the searched classes list and finally search
11256 java.lang.Object if it wasn't searched already. */
11257 if (!search_not_done)
11259 if (!lc
11260 && TYPE_METHODS (object_type_node)
11261 && htab_find (searched_classes, object_type_node) == NULL)
11263 search_applicable_methods_list (lc,
11264 TYPE_METHODS (object_type_node),
11265 name, arglist, &list, &all_list);
11267 htab_delete (searched_classes);
11268 searched_classes = NULL;
11271 /* Either return the list obtained or all selected (but
11272 inaccessible) methods for better error report. */
11273 return (!list ? all_list : list);
11276 /* Effectively search for the appropriate method in method */
11278 static void
11279 search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
11280 tree *list, tree *all_list)
11282 for (; method; method = TREE_CHAIN (method))
11284 /* When dealing with constructor, stop here, otherwise search
11285 other classes */
11286 if (lc && !DECL_CONSTRUCTOR_P (method))
11287 continue;
11288 else if (!lc && (DECL_CONSTRUCTOR_P (method)
11289 || (DECL_NAME (method) != name)))
11290 continue;
11292 if (argument_types_convertible (method, arglist))
11294 /* Retain accessible methods only */
11295 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11296 method, NULL_TREE, 0))
11297 *list = tree_cons (NULL_TREE, method, *list);
11298 else
11299 /* Also retain all selected method here */
11300 *all_list = tree_cons (NULL_TREE, method, *list);
11305 /* 15.11.2.2 Choose the Most Specific Method */
11307 static tree
11308 find_most_specific_methods_list (tree list)
11310 int max = 0;
11311 int abstract, candidates;
11312 tree current, new_list = NULL_TREE;
11313 for (current = list; current; current = TREE_CHAIN (current))
11315 tree method;
11316 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11318 for (method = list; method; method = TREE_CHAIN (method))
11320 tree method_v, current_v;
11321 /* Don't test a method against itself */
11322 if (method == current)
11323 continue;
11325 method_v = TREE_VALUE (method);
11326 current_v = TREE_VALUE (current);
11328 /* Compare arguments and location where methods where declared */
11329 if (argument_types_convertible (method_v, current_v))
11331 if (valid_method_invocation_conversion_p
11332 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
11333 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
11334 && enclosing_context_p (DECL_CONTEXT (method_v),
11335 DECL_CONTEXT (current_v))))
11337 int v = (DECL_SPECIFIC_COUNT (current_v) +=
11338 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
11339 max = (v > max ? v : max);
11345 /* Review the list and select the maximally specific methods */
11346 for (current = list, abstract = -1, candidates = -1;
11347 current; current = TREE_CHAIN (current))
11348 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11350 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11351 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11352 candidates++;
11355 /* If we have several and they're all abstract, just pick the
11356 closest one. */
11357 if (candidates > 0 && candidates == abstract)
11359 /* FIXME: merge the throws clauses. There is no convenient way
11360 to do this in gcj right now, since ideally we'd like to
11361 introduce a new METHOD_DECL here, but that is really not
11362 possible. */
11363 new_list = nreverse (new_list);
11364 TREE_CHAIN (new_list) = NULL_TREE;
11365 return new_list;
11368 /* We have several (we couldn't find a most specific), all but one
11369 are abstract, we pick the only non abstract one. */
11370 if (candidates > 0 && (candidates == abstract+1))
11372 for (current = new_list; current; current = TREE_CHAIN (current))
11373 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11375 TREE_CHAIN (current) = NULL_TREE;
11376 new_list = current;
11380 /* If we can't find one, lower expectations and try to gather multiple
11381 maximally specific methods */
11382 while (!new_list && max)
11384 while (--max > 0)
11386 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11387 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11391 return new_list;
11394 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11395 converted by method invocation conversion (5.3) to the type of the
11396 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11397 to change less often than M1. */
11399 static GTY(()) tree m2_arg_value;
11400 static GTY(()) tree m2_arg_cache;
11402 static int
11403 argument_types_convertible (tree m1, tree m2_or_arglist)
11405 tree m1_arg, m2_arg;
11407 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11409 if (m2_arg_value == m2_or_arglist)
11410 m2_arg = m2_arg_cache;
11411 else
11413 /* M2_OR_ARGLIST can be a function DECL or a raw list of
11414 argument types */
11415 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11417 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11418 if (!METHOD_STATIC (m2_or_arglist))
11419 m2_arg = TREE_CHAIN (m2_arg);
11421 else
11422 m2_arg = m2_or_arglist;
11424 m2_arg_value = m2_or_arglist;
11425 m2_arg_cache = m2_arg;
11428 while (m1_arg != end_params_node && m2_arg != end_params_node)
11430 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11431 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11432 TREE_VALUE (m2_arg)))
11433 break;
11434 m1_arg = TREE_CHAIN (m1_arg);
11435 m2_arg = TREE_CHAIN (m2_arg);
11437 return m1_arg == end_params_node && m2_arg == end_params_node;
11440 /* Qualification routines */
11442 /* Given a name x.y.z, look up x locally. If it's found, save the
11443 decl. If it's not found, mark the name as RESOLVE_PACKAGE_NAME_P,
11444 so that we later try and load the appropriate classes. */
11445 static void
11446 qualify_ambiguous_name (tree id)
11448 tree name, decl;
11450 /* We inspect the first item of the qualification list. As a sanity
11451 check, make sure that it is an identfier node. */
11452 tree qual = EXPR_WFL_QUALIFICATION (id);
11453 tree qual_wfl = QUAL_WFL (qual);
11455 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11456 return;
11458 name = EXPR_WFL_NODE (qual_wfl);
11460 /* If we don't have an identifier, or we have a 'this' or 'super',
11461 then field access processing is all we need : there is nothing
11462 for us to do. */
11463 if (!name || TREE_CODE (name) != IDENTIFIER_NODE ||
11464 name == this_identifier_node ||
11465 name == super_identifier_node)
11466 return;
11468 /* If name appears within the scope of a local variable declaration
11469 or parameter declaration, or is a field within an enclosing
11470 class, then it is an expression name. Save the decl and let
11471 resolve_field_access do it's work. */
11472 if ((decl = IDENTIFIER_LOCAL_VALUE (name)) ||
11473 (decl = lookup_field_wrapper (current_class, name)))
11475 QUAL_RESOLUTION (qual) = decl;
11476 return;
11479 /* If name is a known class name (either declared or imported), mark
11480 us as a type name. */
11481 if ((decl = resolve_and_layout (name, NULL_TREE)))
11483 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11484 QUAL_RESOLUTION (qual) = decl;
11487 /* Check here that NAME isn't declared by more than one
11488 type-import-on-demand declaration of the compilation unit
11489 containing NAME. FIXME */
11491 /* We couldn't find a declaration for the name. Assume for now that
11492 we have a qualified class name that needs to be loaded from an
11493 external class file. */
11494 else
11495 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11497 /* Propagate the qualification across other components of the
11498 qualified name */
11499 for (qual = TREE_CHAIN (qual); qual;
11500 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11502 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11503 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11506 /* Store the global qualification for the ambiguous part of ID back
11507 into ID fields */
11508 if (RESOLVE_TYPE_NAME_P (qual_wfl))
11509 RESOLVE_TYPE_NAME_P (id) = 1;
11510 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11511 RESOLVE_PACKAGE_NAME_P (id) = 1;
11514 /* Patch tree nodes in a function body. When a BLOCK is found, push
11515 local variable decls if present.
11516 Same as java_complete_lhs, but does resolve static finals to values. */
11518 static tree
11519 java_complete_tree (tree node)
11521 node = java_complete_lhs (node);
11522 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11523 && DECL_INITIAL (node) != NULL_TREE)
11525 tree value = fold_constant_for_init (node, node);
11526 if (value != NULL_TREE)
11527 return value;
11529 return node;
11532 static tree
11533 java_stabilize_reference (tree node)
11535 if (TREE_CODE (node) == COMPOUND_EXPR)
11537 tree op0 = TREE_OPERAND (node, 0);
11538 tree op1 = TREE_OPERAND (node, 1);
11539 TREE_OPERAND (node, 0) = save_expr (op0);
11540 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11541 return node;
11543 return stabilize_reference (node);
11546 /* Patch tree nodes in a function body. When a BLOCK is found, push
11547 local variable decls if present.
11548 Same as java_complete_tree, but does not resolve static finals to values. */
11550 static tree
11551 java_complete_lhs (tree node)
11553 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11554 int flag;
11556 /* CONVERT_EXPR always has its type set, even though it needs to be
11557 worked out. */
11558 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11559 return node;
11561 /* The switch block implements cases processing container nodes
11562 first. Contained nodes are always written back. Leaves come
11563 next and return a value. */
11564 switch (TREE_CODE (node))
11566 case BLOCK:
11568 /* 1- Block section.
11569 Set the local values on decl names so we can identify them
11570 faster when they're referenced. At that stage, identifiers
11571 are legal so we don't check for declaration errors. */
11572 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11574 DECL_CONTEXT (cn) = current_function_decl;
11575 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11577 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11578 CAN_COMPLETE_NORMALLY (node) = 1;
11579 else
11581 tree stmt = BLOCK_EXPR_BODY (node);
11582 tree *ptr;
11583 int error_seen = 0;
11584 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11586 /* Re-order from (((A; B); C); ...; Z) to
11587 (A; (B; (C ; (...; Z)))).
11588 This makes it easier to scan the statements left-to-right
11589 without using recursion (which might overflow the stack
11590 if the block has many statements. */
11591 for (;;)
11593 tree left = TREE_OPERAND (stmt, 0);
11594 if (TREE_CODE (left) != COMPOUND_EXPR)
11595 break;
11596 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11597 TREE_OPERAND (left, 1) = stmt;
11598 stmt = left;
11600 BLOCK_EXPR_BODY (node) = stmt;
11603 /* Now do the actual complete, without deep recursion for
11604 long blocks. */
11605 ptr = &BLOCK_EXPR_BODY (node);
11606 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11607 && !IS_EMPTY_STMT (TREE_OPERAND (*ptr, 1)))
11609 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11610 tree *next = &TREE_OPERAND (*ptr, 1);
11611 TREE_OPERAND (*ptr, 0) = cur;
11612 if (IS_EMPTY_STMT (cur))
11614 /* Optimization; makes it easier to detect empty bodies.
11615 Most useful for <clinit> with all-constant initializer. */
11616 *ptr = *next;
11617 continue;
11619 if (TREE_CODE (cur) == ERROR_MARK)
11620 error_seen++;
11621 else if (! CAN_COMPLETE_NORMALLY (cur))
11623 wfl_op2 = *next;
11624 for (;;)
11626 if (TREE_CODE (wfl_op2) == BLOCK)
11627 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11628 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11629 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11630 else
11631 break;
11633 if (TREE_CODE (wfl_op2) != CASE_EXPR
11634 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11635 unreachable_stmt_error (*ptr);
11637 if (TREE_TYPE (*ptr) == NULL_TREE)
11638 TREE_TYPE (*ptr) = void_type_node;
11639 ptr = next;
11641 *ptr = java_complete_tree (*ptr);
11643 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11644 return error_mark_node;
11645 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11647 /* Turn local bindings to null */
11648 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11649 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11651 TREE_TYPE (node) = void_type_node;
11652 break;
11654 /* 2- They are expressions but ultimately deal with statements */
11656 case THROW_EXPR:
11657 wfl_op1 = TREE_OPERAND (node, 0);
11658 COMPLETE_CHECK_OP_0 (node);
11659 /* 14.19 A throw statement cannot complete normally. */
11660 CAN_COMPLETE_NORMALLY (node) = 0;
11661 return patch_throw_statement (node, wfl_op1);
11663 case SYNCHRONIZED_EXPR:
11664 wfl_op1 = TREE_OPERAND (node, 0);
11665 return patch_synchronized_statement (node, wfl_op1);
11667 case TRY_EXPR:
11668 return patch_try_statement (node);
11670 case TRY_FINALLY_EXPR:
11671 COMPLETE_CHECK_OP_0 (node);
11672 COMPLETE_CHECK_OP_1 (node);
11673 if (IS_EMPTY_STMT (TREE_OPERAND (node, 0)))
11674 /* Reduce try/finally nodes with an empty try block. */
11675 return TREE_OPERAND (node, 1);
11676 if (IS_EMPTY_STMT (TREE_OPERAND (node, 1)))
11677 /* Likewise for an empty finally block. */
11678 return TREE_OPERAND (node, 0);
11679 CAN_COMPLETE_NORMALLY (node)
11680 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11681 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11682 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11683 return node;
11685 case LABELED_BLOCK_EXPR:
11686 PUSH_LABELED_BLOCK (node);
11687 if (LABELED_BLOCK_BODY (node))
11688 COMPLETE_CHECK_OP_1 (node);
11689 TREE_TYPE (node) = void_type_node;
11690 POP_LABELED_BLOCK ();
11692 if (IS_EMPTY_STMT (LABELED_BLOCK_BODY (node)))
11694 LABELED_BLOCK_BODY (node) = NULL_TREE;
11695 CAN_COMPLETE_NORMALLY (node) = 1;
11697 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11698 CAN_COMPLETE_NORMALLY (node) = 1;
11699 return node;
11701 case EXIT_BLOCK_EXPR:
11702 return patch_bc_statement (node);
11704 case CASE_EXPR:
11705 cn = java_complete_tree (TREE_OPERAND (node, 0));
11706 if (cn == error_mark_node)
11707 return cn;
11709 /* First, the case expression must be constant. Values of final
11710 fields are accepted. */
11711 cn = fold (cn);
11712 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11713 && JDECL_P (TREE_OPERAND (cn, 1))
11714 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11715 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11717 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11718 TREE_OPERAND (cn, 1));
11720 /* Accept final locals too. */
11721 else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn)
11722 && DECL_INITIAL (cn))
11723 cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11725 if (!TREE_CONSTANT (cn))
11727 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11728 parse_error_context (node, "Constant expression required");
11729 return error_mark_node;
11732 nn = ctxp->current_loop;
11734 /* It must be assignable to the type of the switch expression. */
11735 if (!try_builtin_assignconv (NULL_TREE,
11736 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11738 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11739 parse_error_context
11740 (wfl_operator,
11741 "Incompatible type for case. Can't convert %qs to %<int%>",
11742 lang_printable_name (TREE_TYPE (cn), 0));
11743 return error_mark_node;
11746 cn = fold (convert (int_type_node, cn));
11747 TREE_CONSTANT_OVERFLOW (cn) = 0;
11748 CAN_COMPLETE_NORMALLY (cn) = 1;
11750 /* Save the label on a list so that we can later check for
11751 duplicates. */
11752 case_label_list = tree_cons (node, cn, case_label_list);
11754 /* Multiple instance of a case label bearing the same value is
11755 checked later. The case expression is all right so far. */
11756 if (TREE_CODE (cn) == VAR_DECL)
11757 cn = DECL_INITIAL (cn);
11758 TREE_OPERAND (node, 0) = cn;
11759 TREE_TYPE (node) = void_type_node;
11760 CAN_COMPLETE_NORMALLY (node) = 1;
11761 TREE_SIDE_EFFECTS (node) = 1;
11762 break;
11764 case DEFAULT_EXPR:
11765 nn = ctxp->current_loop;
11766 /* Only one default label is allowed per switch statement */
11767 if (SWITCH_HAS_DEFAULT (nn))
11769 #ifdef USE_MAPPED_LOCATION
11770 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
11771 #else
11772 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11773 #endif
11774 parse_error_context (wfl_operator,
11775 "Duplicate case label: %<default%>");
11776 return error_mark_node;
11778 else
11779 SWITCH_HAS_DEFAULT (nn) = 1;
11780 TREE_TYPE (node) = void_type_node;
11781 TREE_SIDE_EFFECTS (node) = 1;
11782 CAN_COMPLETE_NORMALLY (node) = 1;
11783 break;
11785 case SWITCH_EXPR:
11786 case LOOP_EXPR:
11787 PUSH_LOOP (node);
11788 /* Check whether the loop was enclosed in a labeled
11789 statement. If not, create one, insert the loop in it and
11790 return the node */
11791 nn = patch_loop_statement (node);
11793 /* Anyways, walk the body of the loop */
11794 if (TREE_CODE (node) == LOOP_EXPR)
11795 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11796 /* Switch statement: walk the switch expression and the cases */
11797 else
11798 node = patch_switch_statement (node);
11800 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11801 nn = error_mark_node;
11802 else
11804 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11805 /* If we returned something different, that's because we
11806 inserted a label. Pop the label too. */
11807 if (nn != node)
11809 if (CAN_COMPLETE_NORMALLY (node))
11810 CAN_COMPLETE_NORMALLY (nn) = 1;
11811 POP_LABELED_BLOCK ();
11814 POP_LOOP ();
11815 return nn;
11817 case EXIT_EXPR:
11818 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11819 return patch_exit_expr (node);
11821 case COND_EXPR:
11822 /* Condition */
11823 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11824 if (TREE_OPERAND (node, 0) == error_mark_node)
11825 return error_mark_node;
11826 /* then-else branches */
11827 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11828 if (TREE_OPERAND (node, 1) == error_mark_node)
11829 return error_mark_node;
11831 /* This is a special case due to build_assertion(). When
11832 assertions are disabled we build a COND_EXPR in which
11833 Operand 1 is the body of the assertion. If that happens to
11834 be a string concatenation we'll need to patch it here. */
11835 tree patched = patch_string (TREE_OPERAND (node, 1));
11836 if (patched)
11837 TREE_OPERAND (node, 1) = patched;
11839 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11840 if (TREE_OPERAND (node, 2) == error_mark_node)
11841 return error_mark_node;
11842 return patch_if_else_statement (node);
11843 break;
11845 case CONDITIONAL_EXPR:
11846 /* Condition */
11847 wfl_op1 = TREE_OPERAND (node, 0);
11848 COMPLETE_CHECK_OP_0 (node);
11849 wfl_op2 = TREE_OPERAND (node, 1);
11850 COMPLETE_CHECK_OP_1 (node);
11851 wfl_op3 = TREE_OPERAND (node, 2);
11852 COMPLETE_CHECK_OP_2 (node);
11853 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11855 /* 3- Expression section */
11856 case COMPOUND_EXPR:
11857 wfl_op2 = TREE_OPERAND (node, 1);
11858 TREE_OPERAND (node, 0) = nn =
11859 java_complete_tree (TREE_OPERAND (node, 0));
11860 if (IS_EMPTY_STMT (wfl_op2))
11861 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11862 else
11864 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11866 /* An unreachable condition in a do-while statement
11867 is *not* (technically) an unreachable statement. */
11868 nn = wfl_op2;
11869 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11870 nn = EXPR_WFL_NODE (nn);
11871 /* NN can be NULL_TREE exactly when UPDATE is, in
11872 finish_for_loop. */
11873 if (nn != NULL_TREE && TREE_CODE (nn) != EXIT_EXPR)
11875 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11876 if (SUPPRESS_UNREACHABLE_ERROR (nn))
11878 /* Perhaps this warning should have an
11879 associated flag. The code being compiled is
11880 pedantically correct, but useless. */
11881 parse_warning_context (wfl_operator,
11882 "Unreachable statement");
11884 else
11885 parse_error_context (wfl_operator,
11886 "Unreachable statement");
11889 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11890 if (TREE_OPERAND (node, 1) == error_mark_node)
11891 return error_mark_node;
11892 /* Even though we might allow the case where the first
11893 operand doesn't return normally, we still should compute
11894 CAN_COMPLETE_NORMALLY correctly. */
11895 CAN_COMPLETE_NORMALLY (node)
11896 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11897 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11899 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11900 break;
11902 case RETURN_EXPR:
11903 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11904 return patch_return (node);
11906 case EXPR_WITH_FILE_LOCATION:
11907 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11908 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11910 node = resolve_expression_name (node, NULL);
11911 if (node == error_mark_node)
11912 return node;
11913 CAN_COMPLETE_NORMALLY (node) = 1;
11915 else
11917 tree body;
11918 location_t save_location = input_location;
11919 #ifdef USE_MAPPED_LOCATION
11920 input_location = EXPR_LOCATION (node);
11921 if (input_location == UNKNOWN_LOCATION)
11922 input_location = save_location;
11923 #else
11924 input_line = EXPR_WFL_LINENO (node);
11925 #endif
11926 body = java_complete_tree (EXPR_WFL_NODE (node));
11927 input_location = save_location;
11928 EXPR_WFL_NODE (node) = body;
11929 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11930 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11931 if (IS_EMPTY_STMT (body) || TREE_CONSTANT (body))
11933 /* Makes it easier to constant fold, detect empty bodies. */
11934 return body;
11936 if (body == error_mark_node)
11938 /* Its important for the evaluation of assignment that
11939 this mark on the TREE_TYPE is propagated. */
11940 TREE_TYPE (node) = error_mark_node;
11941 return error_mark_node;
11943 else
11944 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11947 break;
11949 case NEW_ARRAY_EXPR:
11950 /* Patch all the dimensions */
11951 flag = 0;
11952 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11954 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11955 tree dim = convert (int_type_node,
11956 java_complete_tree (TREE_VALUE (cn)));
11957 if (dim == error_mark_node)
11959 flag = 1;
11960 continue;
11962 else
11964 TREE_VALUE (cn) = dim;
11965 /* Setup the location of the current dimension, for
11966 later error report. */
11967 #ifdef USE_MAPPED_LOCATION
11968 TREE_PURPOSE (cn) = expr_add_location (NULL_TREE, location, 0);
11969 #else
11970 TREE_PURPOSE (cn) =
11971 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11972 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11973 #endif
11976 /* They complete the array creation expression, if no errors
11977 were found. */
11978 CAN_COMPLETE_NORMALLY (node) = 1;
11979 return (flag ? error_mark_node
11980 : force_evaluation_order (patch_newarray (node)));
11982 case NEW_ANONYMOUS_ARRAY_EXPR:
11983 /* Create the array type if necessary. */
11984 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11986 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11987 if (!(type = resolve_type_during_patch (type)))
11988 return error_mark_node;
11989 type = build_array_from_name (type, NULL_TREE,
11990 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11991 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11993 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11994 ANONYMOUS_ARRAY_INITIALIZER (node));
11995 if (node == error_mark_node)
11996 return error_mark_node;
11997 CAN_COMPLETE_NORMALLY (node) = 1;
11998 return node;
12000 case NEW_CLASS_EXPR:
12001 case CALL_EXPR:
12002 /* Complete function's argument(s) first */
12003 if (complete_function_arguments (node))
12004 return error_mark_node;
12005 else
12007 tree decl, wfl = TREE_OPERAND (node, 0);
12008 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
12009 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
12010 super_identifier_node);
12011 tree arguments;
12012 #ifdef USE_MAPPED_LOCATION
12013 source_location location = EXPR_LOCATION (node);
12014 #else
12015 int location = EXPR_WFL_LINECOL (node);
12016 #endif
12018 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12019 from_super, 0, &decl);
12020 if (node == error_mark_node)
12021 return error_mark_node;
12023 if (TREE_CODE (node) == CALL_EXPR
12024 && TREE_OPERAND (node, 1) != NULL_TREE)
12025 arguments = TREE_VALUE (TREE_OPERAND (node, 1));
12026 else
12027 arguments = NULL_TREE;
12028 check_thrown_exceptions (location, decl, arguments);
12029 /* If we call this(...), register signature and positions */
12030 if (in_this)
12031 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12032 tree_cons (wfl, decl,
12033 DECL_CONSTRUCTOR_CALLS (current_function_decl));
12034 CAN_COMPLETE_NORMALLY (node) = 1;
12035 return force_evaluation_order (node);
12038 case MODIFY_EXPR:
12039 /* Save potential wfls */
12040 wfl_op1 = TREE_OPERAND (node, 0);
12041 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12043 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12044 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12045 && DECL_INITIAL (nn) != NULL_TREE)
12047 tree value;
12049 value = fold_constant_for_init (nn, nn);
12051 /* When we have a primitype type, or a string and we're not
12052 emitting a class file, we actually don't want to generate
12053 anything for the assignment. */
12054 if (value != NULL_TREE &&
12055 (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
12056 (TREE_TYPE (value) == string_ptr_type_node &&
12057 ! flag_emit_class_files)))
12059 /* Prepare node for patch_assignment */
12060 TREE_OPERAND (node, 1) = value;
12061 /* Call patch assignment to verify the assignment */
12062 if (patch_assignment (node, wfl_op1) == error_mark_node)
12063 return error_mark_node;
12064 /* Set DECL_INITIAL properly (a conversion might have
12065 been decided by patch_assignment) and return the
12066 empty statement. */
12067 else
12069 tree patched = patch_string (TREE_OPERAND (node, 1));
12070 if (patched)
12071 DECL_INITIAL (nn) = patched;
12072 else
12073 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12074 DECL_FIELD_FINAL_IUD (nn) = 1;
12075 return build_java_empty_stmt ();
12078 if (! flag_emit_class_files)
12079 DECL_INITIAL (nn) = NULL_TREE;
12081 wfl_op2 = TREE_OPERAND (node, 1);
12083 if (TREE_OPERAND (node, 0) == error_mark_node)
12084 return error_mark_node;
12086 flag = COMPOUND_ASSIGN_P (wfl_op2);
12087 if (flag)
12089 /* This might break when accessing outer field from inner
12090 class. TESTME, FIXME */
12091 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12093 /* Hand stabilize the lhs on both places */
12094 TREE_OPERAND (node, 0) = lvalue;
12095 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12096 (flag_emit_class_files ? lvalue : save_expr (lvalue));
12098 /* 15.25.2.a: Left hand is not an array access. FIXME */
12099 /* Now complete the RHS. We write it back later on. */
12100 nn = java_complete_tree (TREE_OPERAND (node, 1));
12102 if ((cn = patch_string (nn)))
12103 nn = cn;
12105 /* The last part of the rewrite for E1 op= E2 is to have
12106 E1 = (T)(E1 op E2), with T being the type of E1. */
12107 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12108 TREE_TYPE (lvalue), nn));
12110 /* If the assignment is compound and has reference type,
12111 then ensure the LHS has type String and nothing else. */
12112 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12113 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12114 parse_error_context (wfl_op2,
12115 "Incompatible type for %<+=%>. Can't convert %qs to %<java.lang.String%>",
12116 lang_printable_name (TREE_TYPE (lvalue), 0));
12118 /* 15.25.2.b: Left hand is an array access. FIXME */
12121 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12122 function to complete this RHS. Note that a NEW_ARRAY_INIT
12123 might have been already fully expanded if created as a result
12124 of processing an anonymous array initializer. We avoid doing
12125 the operation twice by testing whether the node already bears
12126 a type. */
12127 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12128 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12129 TREE_OPERAND (node, 1));
12130 /* Otherwise we simply complete the RHS */
12131 else
12132 nn = java_complete_tree (TREE_OPERAND (node, 1));
12134 if (nn == error_mark_node)
12135 return error_mark_node;
12137 /* Write back the RHS as we evaluated it. */
12138 TREE_OPERAND (node, 1) = nn;
12140 /* In case we're handling = with a String as a RHS, we need to
12141 produce a String out of the RHS (it might still be a
12142 STRING_CST or a StringBuffer at this stage */
12143 if ((nn = patch_string (TREE_OPERAND (node, 1))))
12144 TREE_OPERAND (node, 1) = nn;
12146 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12147 TREE_OPERAND (node, 1))))
12149 /* We return error_mark_node if outer_field_access_fix
12150 detects we write into a final. */
12151 if (nn == error_mark_node)
12152 return error_mark_node;
12153 node = nn;
12155 else
12157 node = patch_assignment (node, wfl_op1);
12158 if (node == error_mark_node)
12159 return error_mark_node;
12160 /* Reorganize the tree if necessary. */
12161 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12162 || JSTRING_P (TREE_TYPE (node))))
12163 node = java_refold (node);
12166 /* Seek to set DECL_INITIAL to a proper value, since it might have
12167 undergone a conversion in patch_assignment. We do that only when
12168 it's necessary to have DECL_INITIAL properly set. */
12169 nn = TREE_OPERAND (node, 0);
12170 if (TREE_CODE (nn) == VAR_DECL
12171 && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12172 && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12173 && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12174 || TREE_TYPE (nn) == string_ptr_type_node))
12175 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12177 CAN_COMPLETE_NORMALLY (node) = 1;
12178 return node;
12180 case MULT_EXPR:
12181 case PLUS_EXPR:
12182 case MINUS_EXPR:
12183 case LSHIFT_EXPR:
12184 case RSHIFT_EXPR:
12185 case URSHIFT_EXPR:
12186 case BIT_AND_EXPR:
12187 case BIT_XOR_EXPR:
12188 case BIT_IOR_EXPR:
12189 case TRUNC_MOD_EXPR:
12190 case TRUNC_DIV_EXPR:
12191 case RDIV_EXPR:
12192 case TRUTH_ANDIF_EXPR:
12193 case TRUTH_ORIF_EXPR:
12194 case EQ_EXPR:
12195 case NE_EXPR:
12196 case GT_EXPR:
12197 case GE_EXPR:
12198 case LT_EXPR:
12199 case LE_EXPR:
12200 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12201 knows how to handle those cases. */
12202 wfl_op1 = TREE_OPERAND (node, 0);
12203 wfl_op2 = TREE_OPERAND (node, 1);
12205 CAN_COMPLETE_NORMALLY (node) = 1;
12206 /* Don't complete string nodes if dealing with the PLUS operand. */
12207 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12209 nn = java_complete_tree (wfl_op1);
12210 if (nn == error_mark_node)
12211 return error_mark_node;
12213 TREE_OPERAND (node, 0) = nn;
12215 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12217 nn = java_complete_tree (wfl_op2);
12218 if (nn == error_mark_node)
12219 return error_mark_node;
12221 TREE_OPERAND (node, 1) = nn;
12223 return patch_binop (node, wfl_op1, wfl_op2);
12225 case INSTANCEOF_EXPR:
12226 wfl_op1 = TREE_OPERAND (node, 0);
12227 COMPLETE_CHECK_OP_0 (node);
12228 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
12230 case UNARY_PLUS_EXPR:
12231 case NEGATE_EXPR:
12232 case TRUTH_NOT_EXPR:
12233 case BIT_NOT_EXPR:
12234 case PREDECREMENT_EXPR:
12235 case PREINCREMENT_EXPR:
12236 case POSTDECREMENT_EXPR:
12237 case POSTINCREMENT_EXPR:
12238 case CONVERT_EXPR:
12239 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12240 how to handle those cases. */
12241 wfl_op1 = TREE_OPERAND (node, 0);
12242 CAN_COMPLETE_NORMALLY (node) = 1;
12243 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12244 if (TREE_OPERAND (node, 0) == error_mark_node)
12245 return error_mark_node;
12246 node = patch_unaryop (node, wfl_op1);
12247 CAN_COMPLETE_NORMALLY (node) = 1;
12248 break;
12250 case ARRAY_REF:
12251 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12252 how to handle those cases. */
12253 wfl_op1 = TREE_OPERAND (node, 0);
12254 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12255 if (TREE_OPERAND (node, 0) == error_mark_node)
12256 return error_mark_node;
12257 if (!flag_emit_class_files)
12258 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12259 /* The same applies to wfl_op2 */
12260 wfl_op2 = TREE_OPERAND (node, 1);
12261 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12262 if (TREE_OPERAND (node, 1) == error_mark_node)
12263 return error_mark_node;
12264 if (!flag_emit_class_files)
12265 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12266 return patch_array_ref (node);
12268 case RECORD_TYPE:
12269 return node;;
12271 case COMPONENT_REF:
12272 /* The first step in the re-write of qualified name handling. FIXME.
12273 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12274 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12275 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12277 tree name = TREE_OPERAND (node, 1);
12278 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12279 if (field == NULL_TREE)
12281 error ("missing static field %qs", IDENTIFIER_POINTER (name));
12282 return error_mark_node;
12284 if (! FIELD_STATIC (field))
12286 error ("not a static field %qs", IDENTIFIER_POINTER (name));
12287 return error_mark_node;
12289 return field;
12291 else
12292 abort ();
12293 break;
12295 case THIS_EXPR:
12296 /* Can't use THIS in a static environment */
12297 if (!current_this)
12299 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12300 parse_error_context (wfl_operator,
12301 "Keyword %<this%> used outside allowed context");
12302 TREE_TYPE (node) = error_mark_node;
12303 return error_mark_node;
12305 if (ctxp->explicit_constructor_p)
12307 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12308 parse_error_context
12309 (wfl_operator, "Can't reference %<this%> or %<super%> before the superclass constructor has been called");
12310 TREE_TYPE (node) = error_mark_node;
12311 return error_mark_node;
12313 return current_this;
12315 case CLASS_LITERAL:
12316 CAN_COMPLETE_NORMALLY (node) = 1;
12317 node = patch_incomplete_class_ref (node);
12318 if (node == error_mark_node)
12319 return error_mark_node;
12320 break;
12322 default:
12323 CAN_COMPLETE_NORMALLY (node) = 1;
12324 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12325 and it's time to turn it into the appropriate String object */
12326 if ((nn = patch_string (node)))
12327 node = nn;
12328 else
12329 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12331 return node;
12334 /* Complete function call's argument. Return a nonzero value is an
12335 error was found. */
12337 static int
12338 complete_function_arguments (tree node)
12340 int flag = 0;
12341 tree cn;
12343 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12344 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12346 tree wfl = TREE_VALUE (cn), parm, temp;
12347 parm = java_complete_tree (wfl);
12349 if (parm == error_mark_node)
12351 flag = 1;
12352 continue;
12354 /* If we have a string literal that we haven't transformed yet or a
12355 crafted string buffer, as a result of the use of the String
12356 `+' operator. Build `parm.toString()' and expand it. */
12357 if ((temp = patch_string (parm)))
12358 parm = temp;
12360 TREE_VALUE (cn) = parm;
12362 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12363 return flag;
12366 /* Sometimes (for loops and variable initialized during their
12367 declaration), we want to wrap a statement around a WFL and turn it
12368 debugable. */
12370 static tree
12371 build_debugable_stmt (int location, tree stmt)
12373 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12375 #ifdef USE_MAPPED_LOCATION
12376 stmt = expr_add_location (stmt, location, 1);
12377 #else
12378 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12379 EXPR_WFL_LINECOL (stmt) = location;
12380 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12381 #endif
12383 return stmt;
12386 static tree
12387 build_expr_block (tree body, tree decls)
12389 tree node = make_node (BLOCK);
12390 BLOCK_EXPR_DECLS (node) = decls;
12391 BLOCK_EXPR_BODY (node) = body;
12392 if (body)
12393 TREE_TYPE (node) = TREE_TYPE (body);
12394 TREE_SIDE_EFFECTS (node) = 1;
12395 return node;
12398 /* Create a new function block and link it appropriately to current
12399 function block chain */
12401 static tree
12402 enter_block (void)
12404 tree b = build_expr_block (NULL_TREE, NULL_TREE);
12406 /* Link block B supercontext to the previous block. The current
12407 function DECL is used as supercontext when enter_a_block is called
12408 for the first time for a given function. The current function body
12409 (DECL_FUNCTION_BODY) is set to be block B. */
12411 tree fndecl = current_function_decl;
12413 if (!fndecl) {
12414 BLOCK_SUPERCONTEXT (b) = current_static_block;
12415 current_static_block = b;
12418 else if (!DECL_FUNCTION_BODY (fndecl))
12420 BLOCK_SUPERCONTEXT (b) = fndecl;
12421 DECL_FUNCTION_BODY (fndecl) = b;
12423 else
12425 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12426 DECL_FUNCTION_BODY (fndecl) = b;
12428 return b;
12431 /* Exit a block by changing the current function body
12432 (DECL_FUNCTION_BODY) to the current block super context, only if
12433 the block being exited isn't the method's top level one. */
12435 static tree
12436 exit_block (void)
12438 tree b;
12439 if (current_function_decl)
12441 b = DECL_FUNCTION_BODY (current_function_decl);
12442 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12443 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12445 else
12447 b = current_static_block;
12449 if (BLOCK_SUPERCONTEXT (b))
12450 current_static_block = BLOCK_SUPERCONTEXT (b);
12452 return b;
12455 /* Lookup for NAME in the nested function's blocks, all the way up to
12456 the current toplevel one. It complies with Java's local variable
12457 scoping rules. */
12459 static tree
12460 lookup_name_in_blocks (tree name)
12462 tree b = GET_CURRENT_BLOCK (current_function_decl);
12464 while (b != current_function_decl)
12466 tree current;
12468 /* Paranoid sanity check. To be removed */
12469 if (TREE_CODE (b) != BLOCK)
12470 abort ();
12472 for (current = BLOCK_EXPR_DECLS (b); current;
12473 current = TREE_CHAIN (current))
12474 if (DECL_NAME (current) == name)
12475 return current;
12476 b = BLOCK_SUPERCONTEXT (b);
12478 return NULL_TREE;
12481 static void
12482 maybe_absorb_scoping_blocks (void)
12484 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12486 tree b = exit_block ();
12487 java_method_add_stmt (current_function_decl, b);
12488 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", input_line));
12493 /* This section of the source is reserved to build_* functions that
12494 are building incomplete tree nodes and the patch_* functions that
12495 are completing them. */
12497 /* Wrap a non WFL node around a WFL. */
12499 static tree
12500 build_wfl_wrap (tree node, int location)
12502 tree wfl, node_to_insert = node;
12504 /* We want to process THIS . xxx symbolically, to keep it consistent
12505 with the way we're processing SUPER. A THIS from a primary as a
12506 different form than a SUPER. Turn THIS into something symbolic */
12507 if (TREE_CODE (node) == THIS_EXPR)
12508 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12509 else
12510 #ifdef USE_MAPPED_LOCATION
12511 wfl = build_unknown_wfl (NULL_TREE);
12513 SET_EXPR_LOCATION (wfl, location);
12514 #else
12515 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12517 EXPR_WFL_LINECOL (wfl) = location;
12518 #endif
12519 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12520 return wfl;
12523 /* Build a super() constructor invocation. Returns an empty statement if
12524 we're currently dealing with the class java.lang.Object. */
12526 static tree
12527 build_super_invocation (tree mdecl)
12529 if (DECL_CONTEXT (mdecl) == object_type_node)
12530 return build_java_empty_stmt ();
12531 else
12533 tree super_wfl = build_wfl_node (super_identifier_node);
12534 tree a = NULL_TREE, t;
12536 /* This is called after parsing is done, so the parser context
12537 won't be accurate. Set location info from current_class decl. */
12538 tree class_wfl = lookup_cl (TYPE_NAME (current_class));
12539 EXPR_WFL_LINECOL (super_wfl) = EXPR_WFL_LINECOL (class_wfl);
12541 /* If we're dealing with an anonymous class, pass the arguments
12542 of the crafted constructor along. */
12543 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12545 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12546 for (; t != end_params_node; t = TREE_CHAIN (t))
12547 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12549 return build_method_invocation (super_wfl, a);
12553 /* Build a SUPER/THIS qualified method invocation. */
12555 static tree
12556 build_this_super_qualified_invocation (int use_this, tree name, tree args,
12557 int lloc, int rloc)
12559 tree invok;
12560 tree wfl =
12561 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12562 EXPR_WFL_LINECOL (wfl) = lloc;
12563 invok = build_method_invocation (name, args);
12564 return make_qualified_primary (wfl, invok, rloc);
12567 /* Build an incomplete CALL_EXPR node. */
12569 static tree
12570 build_method_invocation (tree name, tree args)
12572 tree call = build3 (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12573 TREE_SIDE_EFFECTS (call) = 1;
12574 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12575 return call;
12578 /* Build an incomplete new xxx(...) node. */
12580 static tree
12581 build_new_invocation (tree name, tree args)
12583 tree call = build3 (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12584 TREE_SIDE_EFFECTS (call) = 1;
12585 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12586 return call;
12589 /* Build an incomplete assignment expression. */
12591 static tree
12592 build_assignment (int op, int op_location, tree lhs, tree rhs)
12594 tree assignment;
12595 /* Build the corresponding binop if we deal with a Compound
12596 Assignment operator. Mark the binop sub-tree as part of a
12597 Compound Assignment expression */
12598 if (op != ASSIGN_TK)
12600 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12601 COMPOUND_ASSIGN_P (rhs) = 1;
12603 assignment = build2 (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12604 TREE_SIDE_EFFECTS (assignment) = 1;
12605 EXPR_WFL_LINECOL (assignment) = op_location;
12606 return assignment;
12609 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12610 the buffer. This is used only for string conversion. */
12611 static char *
12612 string_convert_int_cst (tree node)
12614 /* Long.MIN_VALUE is -9223372036854775808, 20 characters. */
12615 static char buffer[21];
12617 unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12618 unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12619 char *p = buffer + sizeof (buffer);
12620 int neg = 0;
12622 unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12623 << (HOST_BITS_PER_WIDE_INT - 1));
12625 *--p = '\0';
12627 /* If negative, note the fact and negate the value. */
12628 if ((hi & hibit))
12630 lo = ~lo;
12631 hi = ~hi;
12632 if (++lo == 0)
12633 ++hi;
12634 neg = 1;
12637 /* Divide by 10 until there are no bits left. */
12640 unsigned HOST_WIDE_INT acc = 0;
12641 unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12642 unsigned int i;
12644 /* Use long division to compute the result and the remainder. */
12645 for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12647 /* Shift a bit into accumulator. */
12648 acc <<= 1;
12649 if ((hi & hibit))
12650 acc |= 1;
12652 /* Shift the value. */
12653 hi <<= 1;
12654 if ((lo & hibit))
12655 hi |= 1;
12656 lo <<= 1;
12658 /* Shift the correct bit into the result. */
12659 outhi <<= 1;
12660 if ((outlo & hibit))
12661 outhi |= 1;
12662 outlo <<= 1;
12663 if (acc >= 10)
12665 acc -= 10;
12666 outlo |= 1;
12670 /* '0' == 060 in Java, but might not be here (think EBCDIC). */
12671 *--p = '\060' + acc;
12673 hi = outhi;
12674 lo = outlo;
12676 while (hi || lo);
12678 if (neg)
12679 *--p = '\055'; /* '-' == 055 in Java, but might not be here. */
12681 return p;
12684 /* Print an INTEGER_CST node in a static buffer, and return the
12685 buffer. This is used only for error handling. */
12686 char *
12687 print_int_node (tree node)
12689 static char buffer [80];
12690 if (TREE_CONSTANT_OVERFLOW (node))
12691 sprintf (buffer, "<overflow>");
12693 if (TREE_INT_CST_HIGH (node) == 0)
12694 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12695 TREE_INT_CST_LOW (node));
12696 else if (TREE_INT_CST_HIGH (node) == -1
12697 && TREE_INT_CST_LOW (node) != 0)
12698 sprintf (buffer, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
12699 -TREE_INT_CST_LOW (node));
12700 else
12701 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12702 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12704 return buffer;
12708 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12709 context. */
12711 /* 15.25 Assignment operators. */
12713 static tree
12714 patch_assignment (tree node, tree wfl_op1)
12716 tree rhs = TREE_OPERAND (node, 1);
12717 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12718 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12719 int error_found = 0;
12720 int lvalue_from_array = 0;
12721 int is_return = 0;
12723 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12725 /* Lhs can be a named variable */
12726 if (JDECL_P (lvalue))
12728 lhs_type = TREE_TYPE (lvalue);
12730 /* Or Lhs can be an array access. */
12731 else if (TREE_CODE (lvalue) == ARRAY_REF)
12733 lhs_type = TREE_TYPE (lvalue);
12734 lvalue_from_array = 1;
12736 /* Or a field access */
12737 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12738 lhs_type = TREE_TYPE (lvalue);
12739 /* Or a function return slot */
12740 else if (TREE_CODE (lvalue) == RESULT_DECL)
12742 /* If the return type is an integral type, then we create the
12743 RESULT_DECL with a promoted type, but we need to do these
12744 checks against the unpromoted type to ensure type safety. So
12745 here we look at the real type, not the type of the decl we
12746 are modifying. */
12747 lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12748 is_return = 1;
12750 /* Otherwise, we might want to try to write into an optimized static
12751 final, this is an of a different nature, reported further on. */
12752 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12753 && resolve_expression_name (wfl_op1, &llvalue))
12755 lhs_type = TREE_TYPE (lvalue);
12757 else
12759 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12760 error_found = 1;
12763 rhs_type = TREE_TYPE (rhs);
12765 /* 5.1 Try the assignment conversion for builtin type. */
12766 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12768 /* 5.2 If it failed, try a reference conversion */
12769 if (!new_rhs)
12770 new_rhs = try_reference_assignconv (lhs_type, rhs);
12772 /* 15.25.2 If we have a compound assignment, convert RHS into the
12773 type of the LHS */
12774 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12775 new_rhs = convert (lhs_type, rhs);
12777 /* Explicit cast required. This is an error */
12778 if (!new_rhs)
12780 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12781 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12782 tree wfl;
12783 char operation [32]; /* Max size known */
12785 /* If the assignment is part of a declaration, we use the WFL of
12786 the declared variable to point out the error and call it a
12787 declaration problem. If the assignment is a genuine =
12788 operator, we call is a operator `=' problem, otherwise we
12789 call it an assignment problem. In both of these last cases,
12790 we use the WFL of the operator to indicate the error. */
12792 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12794 wfl = wfl_op1;
12795 strcpy (operation, "declaration");
12797 else
12799 wfl = wfl_operator;
12800 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12801 strcpy (operation, "assignment");
12802 else if (is_return)
12803 strcpy (operation, "'return'");
12804 else
12805 strcpy (operation, "'='");
12808 if (!valid_cast_to_p (rhs_type, lhs_type))
12809 parse_error_context
12810 (wfl, "Incompatible type for %s. Can't convert %qs to %qs",
12811 operation, t1, t2);
12812 else
12813 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert %qs to %qs",
12814 operation, t1, t2);
12815 free (t1); free (t2);
12816 error_found = 1;
12819 if (error_found)
12820 return error_mark_node;
12822 /* If we're processing a `return' statement, promote the actual type
12823 to the promoted type. */
12824 if (is_return)
12825 new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12827 /* 10.10: Array Store Exception runtime check */
12828 if (!flag_emit_class_files
12829 && lvalue_from_array
12830 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12832 tree array, store_check, base, index_expr;
12834 /* Save RHS so that it doesn't get re-evaluated by the store check. */
12835 new_rhs = save_expr (new_rhs);
12837 /* Get the INDIRECT_REF. */
12838 array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12839 /* Get the array pointer expr. */
12840 array = TREE_OPERAND (array, 0);
12841 store_check = build_java_arraystore_check (array, new_rhs);
12843 index_expr = TREE_OPERAND (lvalue, 1);
12845 if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12847 /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12848 happen before the store check, so prepare to insert the store
12849 check within the second operand of the existing COMPOUND_EXPR. */
12850 base = index_expr;
12852 else
12853 base = lvalue;
12855 index_expr = TREE_OPERAND (base, 1);
12856 TREE_OPERAND (base, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (index_expr),
12857 store_check, index_expr);
12860 /* Final locals can be used as case values in switch
12861 statement. Prepare them for this eventuality. */
12862 if (TREE_CODE (lvalue) == VAR_DECL
12863 && DECL_FINAL (lvalue)
12864 && TREE_CONSTANT (new_rhs)
12865 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12866 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12869 TREE_CONSTANT (lvalue) = 1;
12870 TREE_INVARIANT (lvalue) = 1;
12871 DECL_INITIAL (lvalue) = new_rhs;
12874 /* Copy the rhs if it's a reference. */
12875 if (! flag_check_references && ! flag_emit_class_files && optimize > 0)
12877 switch (TREE_CODE (new_rhs))
12879 case ARRAY_REF:
12880 case INDIRECT_REF:
12881 case COMPONENT_REF:
12882 /* Transform a = foo.bar
12883 into a = ({int tmp; tmp = foo.bar;}).
12884 We need to ensure that if a read from memory fails
12885 because of a NullPointerException, a destination variable
12886 will remain unchanged. An explicit temporary does what
12887 we need.
12889 If flag_check_references is set, this is unnecessary
12890 because we'll check each reference before doing any
12891 reads. If optimize is not set the result will never be
12892 written to a stack slot that contains the LHS. */
12894 tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"),
12895 TREE_TYPE (new_rhs));
12896 tree block = make_node (BLOCK);
12897 tree assignment
12898 = build2 (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
12899 DECL_CONTEXT (tmp) = current_function_decl;
12900 TREE_TYPE (block) = TREE_TYPE (new_rhs);
12901 BLOCK_VARS (block) = tmp;
12902 BLOCK_EXPR_BODY (block) = assignment;
12903 TREE_SIDE_EFFECTS (block) = 1;
12904 new_rhs = block;
12906 break;
12907 default:
12908 break;
12912 TREE_OPERAND (node, 0) = lvalue;
12913 TREE_OPERAND (node, 1) = new_rhs;
12914 TREE_TYPE (node) = lhs_type;
12915 return node;
12918 /* Check that type SOURCE can be cast into type DEST. If the cast
12919 can't occur at all, return NULL; otherwise, return a possibly
12920 modified rhs. */
12922 static tree
12923 try_reference_assignconv (tree lhs_type, tree rhs)
12925 tree new_rhs = NULL_TREE;
12926 tree rhs_type = TREE_TYPE (rhs);
12928 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12930 /* `null' may be assigned to any reference type */
12931 if (rhs == null_pointer_node)
12932 new_rhs = null_pointer_node;
12933 /* Try the reference assignment conversion */
12934 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12935 new_rhs = rhs;
12936 /* This is a magic assignment that we process differently */
12937 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
12938 new_rhs = rhs;
12940 return new_rhs;
12943 /* Check that RHS can be converted into LHS_TYPE by the assignment
12944 conversion (5.2), for the cases of RHS being a builtin type. Return
12945 NULL_TREE if the conversion fails or if because RHS isn't of a
12946 builtin type. Return a converted RHS if the conversion is possible. */
12948 static tree
12949 try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
12951 tree new_rhs = NULL_TREE;
12952 tree rhs_type = TREE_TYPE (rhs);
12954 /* Handle boolean specially. */
12955 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12956 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12958 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12959 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12960 new_rhs = rhs;
12963 /* 5.1.1 Try Identity Conversion,
12964 5.1.2 Try Widening Primitive Conversion */
12965 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12966 new_rhs = convert (lhs_type, rhs);
12968 /* Try a narrowing primitive conversion (5.1.3):
12969 - expression is a constant expression of type byte, short, char,
12970 or int, AND
12971 - variable is byte, short or char AND
12972 - The value of the expression is representable in the type of the
12973 variable */
12974 else if ((rhs_type == byte_type_node || rhs_type == short_type_node
12975 || rhs_type == char_type_node || rhs_type == int_type_node)
12976 && TREE_CONSTANT (rhs)
12977 && (lhs_type == byte_type_node || lhs_type == char_type_node
12978 || lhs_type == short_type_node))
12980 if (int_fits_type_p (rhs, lhs_type))
12981 new_rhs = convert (lhs_type, rhs);
12982 else if (wfl_op1) /* Might be called with a NULL */
12983 parse_warning_context
12984 (wfl_op1,
12985 "Constant expression %qs too wide for narrowing primitive conversion to %qs",
12986 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12987 /* Reported a warning that will turn into an error further
12988 down, so we don't return */
12991 return new_rhs;
12994 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12995 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12996 0 is the conversion test fails. This implements parts the method
12997 invocation conversion (5.3). */
12999 static int
13000 valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
13002 /* 5.1.1: This is the identity conversion part. */
13003 if (lhs_type == rhs_type)
13004 return 1;
13006 /* Reject non primitive types and boolean conversions. */
13007 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
13008 return 0;
13010 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
13011 than a char can't be converted into a char. Short can't too, but
13012 the < test below takes care of that */
13013 if (lhs_type == char_type_node && rhs_type == byte_type_node)
13014 return 0;
13016 /* Accept all promoted type here. Note, we can't use <= in the test
13017 below, because we still need to bounce out assignments of short
13018 to char and the likes */
13019 if (lhs_type == int_type_node
13020 && (rhs_type == promoted_byte_type_node
13021 || rhs_type == promoted_short_type_node
13022 || rhs_type == promoted_char_type_node
13023 || rhs_type == promoted_boolean_type_node))
13024 return 1;
13026 /* From here, an integral is widened if its precision is smaller
13027 than the precision of the LHS or if the LHS is a floating point
13028 type, or the RHS is a float and the RHS a double. */
13029 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13030 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13031 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13032 || (rhs_type == float_type_node && lhs_type == double_type_node))
13033 return 1;
13035 return 0;
13038 /* Check that something of SOURCE type can be assigned or cast to
13039 something of DEST type at runtime. Return 1 if the operation is
13040 valid, 0 otherwise. If CAST is set to 1, we're treating the case
13041 were SOURCE is cast into DEST, which borrows a lot of the
13042 assignment check. */
13044 static int
13045 valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
13047 /* SOURCE or DEST might be null if not from a declared entity. */
13048 if (!source || !dest)
13049 return 0;
13050 if (JNULLP_TYPE_P (source))
13051 return 1;
13052 if (TREE_CODE (source) == POINTER_TYPE)
13053 source = TREE_TYPE (source);
13054 if (TREE_CODE (dest) == POINTER_TYPE)
13055 dest = TREE_TYPE (dest);
13057 /* If source and dest are being compiled from bytecode, they may need to
13058 be loaded. */
13059 if (CLASS_P (source) && !CLASS_LOADED_P (source))
13061 load_class (source, 1);
13062 safe_layout_class (source);
13064 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13066 load_class (dest, 1);
13067 safe_layout_class (dest);
13070 /* Case where SOURCE is a class type */
13071 if (TYPE_CLASS_P (source))
13073 if (TYPE_CLASS_P (dest))
13074 return (source == dest
13075 || inherits_from_p (source, dest)
13076 || (cast && inherits_from_p (dest, source)));
13077 if (TYPE_INTERFACE_P (dest))
13079 /* If doing a cast and SOURCE is final, the operation is
13080 always correct a compile time (because even if SOURCE
13081 does not implement DEST, a subclass of SOURCE might). */
13082 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13083 return 1;
13084 /* Otherwise, SOURCE must implement DEST */
13085 return interface_of_p (dest, source);
13087 /* DEST is an array, cast permitted if SOURCE is of Object type */
13088 return (cast && source == object_type_node ? 1 : 0);
13090 if (TYPE_INTERFACE_P (source))
13092 if (TYPE_CLASS_P (dest))
13094 /* If not casting, DEST must be the Object type */
13095 if (!cast)
13096 return dest == object_type_node;
13097 /* We're doing a cast. The cast is always valid is class
13098 DEST is not final, otherwise, DEST must implement SOURCE */
13099 else if (!CLASS_FINAL (TYPE_NAME (dest)))
13100 return 1;
13101 else
13102 return interface_of_p (source, dest);
13104 if (TYPE_INTERFACE_P (dest))
13106 /* If doing a cast, then if SOURCE and DEST contain method
13107 with the same signature but different return type, then
13108 this is a (compile time) error */
13109 if (cast)
13111 tree method_source, method_dest;
13112 tree source_type;
13113 tree source_sig;
13114 tree source_name;
13115 for (method_source = TYPE_METHODS (source); method_source;
13116 method_source = TREE_CHAIN (method_source))
13118 source_sig =
13119 build_java_argument_signature (TREE_TYPE (method_source));
13120 source_type = TREE_TYPE (TREE_TYPE (method_source));
13121 source_name = DECL_NAME (method_source);
13122 for (method_dest = TYPE_METHODS (dest);
13123 method_dest; method_dest = TREE_CHAIN (method_dest))
13124 if (source_sig ==
13125 build_java_argument_signature (TREE_TYPE (method_dest))
13126 && source_name == DECL_NAME (method_dest)
13127 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13128 return 0;
13130 return 1;
13132 else
13133 return source == dest || interface_of_p (dest, source);
13135 else
13137 /* Array */
13138 return (cast
13139 && (DECL_NAME (TYPE_NAME (source))
13140 == java_lang_cloneable_identifier_node
13141 || (DECL_NAME (TYPE_NAME (source))
13142 == java_io_serializable_identifier_node)));
13145 if (TYPE_ARRAY_P (source))
13147 if (TYPE_CLASS_P (dest))
13148 return dest == object_type_node;
13149 /* Can't cast an array to an interface unless the interface is
13150 java.lang.Cloneable or java.io.Serializable. */
13151 if (TYPE_INTERFACE_P (dest))
13152 return (DECL_NAME (TYPE_NAME (dest))
13153 == java_lang_cloneable_identifier_node
13154 || (DECL_NAME (TYPE_NAME (dest))
13155 == java_io_serializable_identifier_node));
13156 else /* Arrays */
13158 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13159 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13161 /* In case of severe errors, they turn out null */
13162 if (!dest_element_type || !source_element_type)
13163 return 0;
13164 if (source_element_type == dest_element_type)
13165 return 1;
13166 return valid_ref_assignconv_cast_p (source_element_type,
13167 dest_element_type, cast);
13169 return 0;
13171 return 0;
13174 static int
13175 valid_cast_to_p (tree source, tree dest)
13177 if (TREE_CODE (source) == POINTER_TYPE)
13178 source = TREE_TYPE (source);
13179 if (TREE_CODE (dest) == POINTER_TYPE)
13180 dest = TREE_TYPE (dest);
13182 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13183 return valid_ref_assignconv_cast_p (source, dest, 1);
13185 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13186 return 1;
13188 else if (TREE_CODE (source) == BOOLEAN_TYPE
13189 && TREE_CODE (dest) == BOOLEAN_TYPE)
13190 return 1;
13192 return 0;
13195 static tree
13196 do_unary_numeric_promotion (tree arg)
13198 tree type = TREE_TYPE (arg);
13199 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13200 || TREE_CODE (type) == CHAR_TYPE)
13201 arg = convert (int_type_node, arg);
13202 return arg;
13205 /* Return a nonzero value if SOURCE can be converted into DEST using
13206 the method invocation conversion rule (5.3). */
13207 static int
13208 valid_method_invocation_conversion_p (tree dest, tree source)
13210 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13211 && valid_builtin_assignconv_identity_widening_p (dest, source))
13212 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13213 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13214 && valid_ref_assignconv_cast_p (source, dest, 0)));
13217 /* Build an incomplete binop expression. */
13219 static tree
13220 build_binop (enum tree_code op, int op_location, tree op1, tree op2)
13222 tree binop = build2 (op, NULL_TREE, op1, op2);
13223 TREE_SIDE_EFFECTS (binop) = 1;
13224 /* Store the location of the operator, for better error report. The
13225 string of the operator will be rebuild based on the OP value. */
13226 EXPR_WFL_LINECOL (binop) = op_location;
13227 return binop;
13230 /* Build the string of the operator retained by NODE. If NODE is part
13231 of a compound expression, add an '=' at the end of the string. This
13232 function is called when an error needs to be reported on an
13233 operator. The string is returned as a pointer to a static character
13234 buffer. */
13236 static char *
13237 operator_string (tree node)
13239 #define BUILD_OPERATOR_STRING(S) \
13241 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13242 return buffer; \
13245 static char buffer [10];
13246 switch (TREE_CODE (node))
13248 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13249 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13250 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13251 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13252 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13253 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13254 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13255 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13256 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13257 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13258 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13259 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13260 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13261 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13262 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13263 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13264 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13265 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13266 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13267 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13268 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13269 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13270 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13271 case PREINCREMENT_EXPR: /* Fall through */
13272 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13273 case PREDECREMENT_EXPR: /* Fall through */
13274 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13275 default:
13276 internal_error ("unregistered operator %s",
13277 tree_code_name [TREE_CODE (node)]);
13279 return NULL;
13280 #undef BUILD_OPERATOR_STRING
13283 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13285 static int
13286 java_decl_equiv (tree var_acc1, tree var_acc2)
13288 if (JDECL_P (var_acc1))
13289 return (var_acc1 == var_acc2);
13291 return (TREE_CODE (var_acc1) == COMPONENT_REF
13292 && TREE_CODE (var_acc2) == COMPONENT_REF
13293 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13294 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13295 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13298 /* Return a nonzero value if CODE is one of the operators that can be
13299 used in conjunction with the `=' operator in a compound assignment. */
13301 static int
13302 binop_compound_p (enum tree_code code)
13304 int i;
13305 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13306 if (binop_lookup [i] == code)
13307 break;
13309 return i < BINOP_COMPOUND_CANDIDATES;
13312 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13314 static tree
13315 java_refold (tree t)
13317 tree c, b, ns, decl;
13319 if (TREE_CODE (t) != MODIFY_EXPR)
13320 return t;
13322 c = TREE_OPERAND (t, 1);
13323 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13324 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13325 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13326 return t;
13328 /* Now the left branch of the binary operator. */
13329 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13330 if (! (b && TREE_CODE (b) == NOP_EXPR
13331 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13332 return t;
13334 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13335 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13336 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13337 return t;
13339 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13340 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13341 /* It's got to be the an equivalent decl */
13342 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13344 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13345 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13346 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13347 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13348 /* Change the right part of the BINOP_EXPR */
13349 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13352 return t;
13355 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13356 errors but we modify NODE so that it contains the type computed
13357 according to the expression, when it's fixed. Otherwise, we write
13358 error_mark_node as the type. It allows us to further the analysis
13359 of remaining nodes and detects more errors in certain cases. */
13361 static tree
13362 patch_binop (tree node, tree wfl_op1, tree wfl_op2)
13364 tree op1 = TREE_OPERAND (node, 0);
13365 tree op2 = TREE_OPERAND (node, 1);
13366 tree op1_type = TREE_TYPE (op1);
13367 tree op2_type = TREE_TYPE (op2);
13368 tree prom_type = NULL_TREE, cn;
13369 enum tree_code code = TREE_CODE (node);
13371 /* If 1, tell the routine that we have to return error_mark_node
13372 after checking for the initialization of the RHS */
13373 int error_found = 0;
13375 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13377 /* If either op<n>_type are NULL, this might be early signs of an
13378 error situation, unless it's too early to tell (in case we're
13379 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13380 correctly so the error can be later on reported accurately. */
13381 if (! (code == PLUS_EXPR || code == NE_EXPR
13382 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13384 tree n;
13385 if (! op1_type)
13387 n = java_complete_tree (op1);
13388 op1_type = TREE_TYPE (n);
13390 if (! op2_type)
13392 n = java_complete_tree (op2);
13393 op2_type = TREE_TYPE (n);
13397 switch (code)
13399 /* 15.16 Multiplicative operators */
13400 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13401 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13402 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13403 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13404 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13406 if (!JNUMERIC_TYPE_P (op1_type))
13407 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13408 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13409 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13410 TREE_TYPE (node) = error_mark_node;
13411 error_found = 1;
13412 break;
13414 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13416 /* Detect integral division by zero */
13417 if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13418 && TREE_CODE (prom_type) == INTEGER_TYPE
13419 && (op2 == integer_zero_node || op2 == long_zero_node ||
13420 (TREE_CODE (op2) == INTEGER_CST &&
13421 ! TREE_INT_CST_LOW (op2) && ! TREE_INT_CST_HIGH (op2))))
13423 parse_warning_context
13424 (wfl_operator,
13425 "Evaluating this expression will result in an arithmetic exception being thrown");
13426 TREE_CONSTANT (node) = 0;
13427 TREE_INVARIANT (node) = 0;
13430 /* Change the division operator if necessary */
13431 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13432 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13434 /* Before divisions as is disappear, try to simplify and bail if
13435 applicable, otherwise we won't perform even simple
13436 simplifications like (1-1)/3. We can't do that with floating
13437 point number, folds can't handle them at this stage. */
13438 if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13439 && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13441 TREE_TYPE (node) = prom_type;
13442 node = fold (node);
13443 if (TREE_CODE (node) != code)
13444 return node;
13447 if (TREE_CODE (prom_type) == INTEGER_TYPE
13448 && flag_use_divide_subroutine
13449 && ! flag_emit_class_files
13450 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13451 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13453 /* This one is more complicated. FLOATs are processed by a
13454 function call to soft_fmod. Duplicate the value of the
13455 COMPOUND_ASSIGN_P flag. */
13456 if (code == TRUNC_MOD_EXPR)
13458 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13459 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13460 return mod;
13462 break;
13464 /* 15.17 Additive Operators */
13465 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13467 /* Operation is valid if either one argument is a string
13468 constant, a String object or a StringBuffer crafted for the
13469 purpose of the a previous usage of the String concatenation
13470 operator */
13472 if (TREE_CODE (op1) == STRING_CST
13473 || TREE_CODE (op2) == STRING_CST
13474 || JSTRING_TYPE_P (op1_type)
13475 || JSTRING_TYPE_P (op2_type)
13476 || IS_CRAFTED_STRING_BUFFER_P (op1)
13477 || IS_CRAFTED_STRING_BUFFER_P (op2))
13478 return build_string_concatenation (op1, op2);
13480 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13481 Numeric Types */
13482 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13484 if (!JNUMERIC_TYPE_P (op1_type))
13485 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13486 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13487 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13488 TREE_TYPE (node) = error_mark_node;
13489 error_found = 1;
13490 break;
13492 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13493 break;
13495 /* 15.18 Shift Operators */
13496 case LSHIFT_EXPR:
13497 case RSHIFT_EXPR:
13498 case URSHIFT_EXPR:
13499 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13501 if (!JINTEGRAL_TYPE_P (op1_type))
13502 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13503 else
13505 if (JNUMERIC_TYPE_P (op2_type))
13506 parse_error_context (wfl_operator,
13507 "Incompatible type for %qs. Explicit cast needed to convert shift distance from %qs to integral",
13508 operator_string (node),
13509 lang_printable_name (op2_type, 0));
13510 else
13511 parse_error_context (wfl_operator,
13512 "Incompatible type for %qs. Can't convert shift distance from %qs to integral",
13513 operator_string (node),
13514 lang_printable_name (op2_type, 0));
13516 TREE_TYPE (node) = error_mark_node;
13517 error_found = 1;
13518 break;
13521 /* Unary numeric promotion (5.6.1) is performed on each operand
13522 separately */
13523 op1 = do_unary_numeric_promotion (op1);
13524 op2 = do_unary_numeric_promotion (op2);
13526 /* If the right hand side is of type `long', first cast it to
13527 `int'. */
13528 if (TREE_TYPE (op2) == long_type_node)
13529 op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13531 /* The type of the shift expression is the type of the promoted
13532 type of the left-hand operand */
13533 prom_type = TREE_TYPE (op1);
13535 /* Shift int only up to 0x1f and long up to 0x3f */
13536 if (prom_type == int_type_node)
13537 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13538 build_int_cst (NULL_TREE, 0x1f)));
13539 else
13540 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13541 build_int_cst (NULL_TREE, 0x3f)));
13543 /* The >>> operator is a >> operating on unsigned quantities */
13544 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13546 tree to_return;
13547 tree utype = java_unsigned_type (prom_type);
13548 op1 = convert (utype, op1);
13549 TREE_SET_CODE (node, RSHIFT_EXPR);
13550 TREE_OPERAND (node, 0) = op1;
13551 TREE_OPERAND (node, 1) = op2;
13552 TREE_TYPE (node) = utype;
13553 to_return = convert (prom_type, node);
13554 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13555 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13556 TREE_SIDE_EFFECTS (to_return)
13557 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13558 return to_return;
13560 break;
13562 /* 15.19.1 Type Comparison Operator instanceof */
13563 case INSTANCEOF_EXPR:
13565 TREE_TYPE (node) = boolean_type_node;
13567 /* OP1_TYPE might be NULL when OP1 is a string constant. */
13568 if ((cn = patch_string (op1)))
13570 op1 = cn;
13571 op1_type = TREE_TYPE (op1);
13573 if (op1_type == NULL_TREE)
13574 abort ();
13576 if (!(op2_type = resolve_type_during_patch (op2)))
13577 return error_mark_node;
13579 /* The first operand must be a reference type or the null type */
13580 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13581 error_found = 1; /* Error reported further below */
13583 /* The second operand must be a reference type */
13584 if (!JREFERENCE_TYPE_P (op2_type))
13586 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13587 parse_error_context
13588 (wfl_operator, "Invalid argument %qs for %<instanceof%>",
13589 lang_printable_name (op2_type, 0));
13590 error_found = 1;
13593 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13595 /* If the first operand is null, the result is always false */
13596 if (op1 == null_pointer_node)
13597 return boolean_false_node;
13598 else if (flag_emit_class_files)
13600 TREE_OPERAND (node, 1) = op2_type;
13601 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13602 return node;
13604 /* Otherwise we have to invoke instance of to figure it out */
13605 else
13606 return build_instanceof (op1, op2_type);
13608 /* There is no way the expression operand can be an instance of
13609 the type operand. This is a compile time error. */
13610 else
13612 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13613 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13614 parse_error_context
13615 (wfl_operator, "Impossible for %qs to be instance of %qs",
13616 t1, lang_printable_name (op2_type, 0));
13617 free (t1);
13618 error_found = 1;
13621 break;
13623 /* 15.21 Bitwise and Logical Operators */
13624 case BIT_AND_EXPR:
13625 case BIT_XOR_EXPR:
13626 case BIT_IOR_EXPR:
13627 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13628 /* Binary numeric promotion is performed on both operand and the
13629 expression retain that type */
13630 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13632 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13633 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13634 /* The type of the bitwise operator expression is BOOLEAN */
13635 prom_type = boolean_type_node;
13636 else
13638 if (!JINTEGRAL_TYPE_P (op1_type))
13639 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13640 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13641 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13642 TREE_TYPE (node) = error_mark_node;
13643 error_found = 1;
13644 /* Insert a break here if adding thing before the switch's
13645 break for this case */
13647 break;
13649 /* 15.22 Conditional-And Operator */
13650 case TRUTH_ANDIF_EXPR:
13651 /* 15.23 Conditional-Or Operator */
13652 case TRUTH_ORIF_EXPR:
13653 /* Operands must be of BOOLEAN type */
13654 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13655 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13657 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13658 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13659 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13660 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13661 TREE_TYPE (node) = boolean_type_node;
13662 error_found = 1;
13663 break;
13665 else if (integer_zerop (op1))
13667 return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13669 else if (integer_onep (op1))
13671 return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13673 /* The type of the conditional operators is BOOLEAN */
13674 prom_type = boolean_type_node;
13675 break;
13677 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13678 case LT_EXPR:
13679 case GT_EXPR:
13680 case LE_EXPR:
13681 case GE_EXPR:
13682 /* The type of each of the operands must be a primitive numeric
13683 type */
13684 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13686 if (!JNUMERIC_TYPE_P (op1_type))
13687 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13688 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13689 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13690 TREE_TYPE (node) = boolean_type_node;
13691 error_found = 1;
13692 break;
13694 /* Binary numeric promotion is performed on the operands */
13695 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13696 /* The type of the relation expression is always BOOLEAN */
13697 prom_type = boolean_type_node;
13698 break;
13700 /* 15.20 Equality Operator */
13701 case EQ_EXPR:
13702 case NE_EXPR:
13703 /* It's time for us to patch the strings. */
13704 if ((cn = patch_string (op1)))
13706 op1 = cn;
13707 op1_type = TREE_TYPE (op1);
13709 if ((cn = patch_string (op2)))
13711 op2 = cn;
13712 op2_type = TREE_TYPE (op2);
13715 /* 15.20.1 Numerical Equality Operators == and != */
13716 /* Binary numeric promotion is performed on the operands */
13717 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13718 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13720 /* 15.20.2 Boolean Equality Operators == and != */
13721 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13722 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13723 ; /* Nothing to do here */
13725 /* 15.20.3 Reference Equality Operators == and != */
13726 /* Types have to be either references or the null type. If
13727 they're references, it must be possible to convert either
13728 type to the other by casting conversion. */
13729 else if ((op1 == null_pointer_node && op2 == null_pointer_node)
13730 || (op1 == null_pointer_node && JREFERENCE_TYPE_P (op2_type))
13731 || (JREFERENCE_TYPE_P (op1_type) && op2 == null_pointer_node)
13732 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13733 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13734 || valid_ref_assignconv_cast_p (op2_type,
13735 op1_type, 1))))
13736 ; /* Nothing to do here */
13738 /* Else we have an error figure what can't be converted into
13739 what and report the error */
13740 else
13742 char *t1;
13743 t1 = xstrdup (lang_printable_name (op1_type, 0));
13744 parse_error_context
13745 (wfl_operator,
13746 "Incompatible type for %qs. Can't convert %qs to %qs",
13747 operator_string (node), t1,
13748 lang_printable_name (op2_type, 0));
13749 free (t1);
13750 TREE_TYPE (node) = boolean_type_node;
13751 error_found = 1;
13752 break;
13754 prom_type = boolean_type_node;
13755 break;
13756 default:
13757 abort ();
13760 if (error_found)
13761 return error_mark_node;
13763 TREE_OPERAND (node, 0) = op1;
13764 TREE_OPERAND (node, 1) = op2;
13765 TREE_TYPE (node) = prom_type;
13766 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13768 /* fold does not respect side-effect order as required for Java but not C.
13769 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13770 * bytecode.
13772 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13773 : ! TREE_SIDE_EFFECTS (node))
13774 node = fold (node);
13775 return node;
13778 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13779 zero value, the value of CSTE comes after the valude of STRING */
13781 static tree
13782 do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13784 const char *old = TREE_STRING_POINTER (cste);
13785 int old_len = TREE_STRING_LENGTH (cste);
13786 int len = old_len + string_len;
13787 char *new = alloca (len+1);
13789 if (after)
13791 memcpy (new, string, string_len);
13792 memcpy (&new [string_len], old, old_len);
13794 else
13796 memcpy (new, old, old_len);
13797 memcpy (&new [old_len], string, string_len);
13799 new [len] = '\0';
13800 return build_string (len, new);
13803 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13804 new STRING_CST on success, NULL_TREE on failure. */
13806 static tree
13807 merge_string_cste (tree op1, tree op2, int after)
13809 /* Handle two string constants right away. */
13810 if (TREE_CODE (op2) == STRING_CST)
13811 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13812 TREE_STRING_LENGTH (op2), after);
13814 /* Reasonable integer constant can be treated right away. */
13815 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13817 static const char *const boolean_true = "true";
13818 static const char *const boolean_false = "false";
13819 static const char *const null_pointer = "null";
13820 char ch[4];
13821 const char *string;
13823 if (op2 == boolean_true_node)
13824 string = boolean_true;
13825 else if (op2 == boolean_false_node)
13826 string = boolean_false;
13827 else if (op2 == null_pointer_node
13828 || (integer_zerop (op2)
13829 && TREE_CODE (TREE_TYPE (op2)) == POINTER_TYPE))
13830 /* FIXME: null is not a compile-time constant, so it is only safe to
13831 merge if the overall expression is non-constant. However, this
13832 code always merges without checking the overall expression. */
13833 string = null_pointer;
13834 else if (TREE_TYPE (op2) == char_type_node)
13836 /* Convert the character into UTF-8. */
13837 unsigned int c = (unsigned int) TREE_INT_CST_LOW (op2);
13838 unsigned char *p = (unsigned char *) ch;
13839 if (0x01 <= c && c <= 0x7f)
13840 *p++ = (unsigned char) c;
13841 else if (c < 0x7ff)
13843 *p++ = (unsigned char) (c >> 6 | 0xc0);
13844 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13846 else
13848 *p++ = (unsigned char) (c >> 12 | 0xe0);
13849 *p++ = (unsigned char) (((c >> 6) & 0x3f) | 0x80);
13850 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13852 *p = '\0';
13854 string = ch;
13856 else
13857 string = string_convert_int_cst (op2);
13859 return do_merge_string_cste (op1, string, strlen (string), after);
13861 return NULL_TREE;
13864 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13865 has to be a STRING_CST and the other part must be a STRING_CST or a
13866 INTEGRAL constant. Return a new STRING_CST if the operation
13867 succeed, NULL_TREE otherwise.
13869 If the case we want to optimize for space, we might want to return
13870 NULL_TREE for each invocation of this routine. FIXME */
13872 static tree
13873 string_constant_concatenation (tree op1, tree op2)
13875 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13877 tree string, rest;
13878 int invert;
13880 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13881 rest = (string == op1 ? op2 : op1);
13882 invert = (string == op1 ? 0 : 1 );
13884 /* Walk REST, only if it looks reasonable */
13885 if (TREE_CODE (rest) != STRING_CST
13886 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13887 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13888 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13890 rest = java_complete_tree (rest);
13891 if (rest == error_mark_node)
13892 return error_mark_node;
13893 rest = fold (rest);
13895 return merge_string_cste (string, rest, invert);
13897 return NULL_TREE;
13900 /* Implement the `+' operator. Does static optimization if possible,
13901 otherwise create (if necessary) and append elements to a
13902 StringBuffer. The StringBuffer will be carried around until it is
13903 used for a function call or an assignment. Then toString() will be
13904 called on it to turn it into a String object. */
13906 static tree
13907 build_string_concatenation (tree op1, tree op2)
13909 tree result;
13910 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13912 /* Try to do some static optimization */
13913 if ((result = string_constant_concatenation (op1, op2)))
13914 return result;
13916 /* Discard empty strings on either side of the expression */
13917 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13919 op1 = op2;
13920 op2 = NULL_TREE;
13922 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13923 op2 = NULL_TREE;
13925 /* If operands are string constant, turn then into object references */
13926 if (TREE_CODE (op1) == STRING_CST)
13927 op1 = patch_string_cst (op1);
13928 if (op2 && TREE_CODE (op2) == STRING_CST)
13929 op2 = patch_string_cst (op2);
13931 /* If either one of the constant is null and the other non null
13932 operand is a String constant, return it. */
13933 if ((TREE_CODE (op1) == STRING_CST) && !op2)
13934 return op1;
13936 /* If OP1 isn't already a StringBuffer, create and
13937 initialize a new one */
13938 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13940 /* Two solutions here:
13941 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13942 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13943 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13944 op1 = BUILD_STRING_BUFFER (op1);
13945 else
13947 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13948 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13952 if (op2)
13954 /* OP1 is no longer the last node holding a crafted StringBuffer */
13955 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13956 /* Create a node for `{new...,xxx}.append (op2)' */
13957 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13960 /* Mark the last node holding a crafted StringBuffer */
13961 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13963 TREE_SIDE_EFFECTS (op1) = side_effects;
13964 return op1;
13967 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13968 StringBuffer. If no string were found to be patched, return
13969 NULL. */
13971 static tree
13972 patch_string (tree node)
13974 if (node == error_mark_node)
13975 return error_mark_node;
13976 if (TREE_CODE (node) == STRING_CST)
13977 return patch_string_cst (node);
13978 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13980 int saved = ctxp->explicit_constructor_p;
13981 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13982 tree ret;
13983 /* Temporary disable forbid the use of `this'. */
13984 ctxp->explicit_constructor_p = 0;
13985 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13986 /* String concatenation arguments must be evaluated in order too. */
13987 ret = force_evaluation_order (ret);
13988 /* Restore it at its previous value */
13989 ctxp->explicit_constructor_p = saved;
13990 return ret;
13992 return NULL_TREE;
13995 /* Build the internal representation of a string constant. */
13997 static tree
13998 patch_string_cst (tree node)
14000 int location;
14001 if (! flag_emit_class_files)
14003 node = get_identifier (TREE_STRING_POINTER (node));
14004 location = alloc_name_constant (CONSTANT_String, node);
14005 node = build_ref_from_constant_pool (location);
14007 TREE_CONSTANT (node) = 1;
14008 TREE_INVARIANT (node) = 1;
14010 /* ??? Guessing that the class file code can't handle casts. */
14011 if (! flag_emit_class_files)
14012 node = convert (string_ptr_type_node, node);
14013 else
14014 TREE_TYPE (node) = string_ptr_type_node;
14016 return node;
14019 /* Build an incomplete unary operator expression. */
14021 static tree
14022 build_unaryop (int op_token, int op_location, tree op1)
14024 enum tree_code op;
14025 tree unaryop;
14026 switch (op_token)
14028 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14029 case MINUS_TK: op = NEGATE_EXPR; break;
14030 case NEG_TK: op = TRUTH_NOT_EXPR; break;
14031 case NOT_TK: op = BIT_NOT_EXPR; break;
14032 default: abort ();
14035 unaryop = build1 (op, NULL_TREE, op1);
14036 TREE_SIDE_EFFECTS (unaryop) = 1;
14037 /* Store the location of the operator, for better error report. The
14038 string of the operator will be rebuild based on the OP value. */
14039 EXPR_WFL_LINECOL (unaryop) = op_location;
14040 return unaryop;
14043 /* Special case for the ++/-- operators, since they require an extra
14044 argument to build, which is set to NULL and patched
14045 later. IS_POST_P is 1 if the operator, 0 otherwise. */
14047 static tree
14048 build_incdec (int op_token, int op_location, tree op1, int is_post_p)
14050 static const enum tree_code lookup [2][2] =
14052 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14053 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14055 tree node = build2 (lookup [is_post_p][(op_token - DECR_TK)],
14056 NULL_TREE, op1, NULL_TREE);
14057 TREE_SIDE_EFFECTS (node) = 1;
14058 /* Store the location of the operator, for better error report. The
14059 string of the operator will be rebuild based on the OP value. */
14060 EXPR_WFL_LINECOL (node) = op_location;
14061 return node;
14064 /* Build an incomplete cast operator, based on the use of the
14065 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14066 set. java_complete_tree is trained to walk a CONVERT_EXPR even
14067 though its type is already set. */
14069 static tree
14070 build_cast (int location, tree type, tree exp)
14072 tree node = build1 (CONVERT_EXPR, type, exp);
14073 EXPR_WFL_LINECOL (node) = location;
14074 return node;
14077 /* Build an incomplete class reference operator. */
14078 static tree
14079 build_incomplete_class_ref (int location, tree class_name)
14081 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14082 tree class_decl = GET_CPC ();
14083 tree this_class = TREE_TYPE (class_decl);
14085 /* Generate the synthetic static method `class$'. (Previously we
14086 deferred this, causing different method tables to be emitted
14087 for native code and bytecode.) */
14088 if (!TYPE_DOT_CLASS (this_class)
14089 && !JPRIMITIVE_TYPE_P (class_name)
14090 && !(TREE_CODE (class_name) == VOID_TYPE))
14092 tree cpc_list = GET_CPC_LIST();
14093 tree cpc = cpc_list;
14094 tree target_class;
14096 /* For inner classes, add a 'class$' method to their outermost
14097 context, creating it if necessary. */
14099 while (GET_NEXT_ENCLOSING_CPC(cpc))
14100 cpc = GET_NEXT_ENCLOSING_CPC(cpc);
14101 class_decl = TREE_VALUE (cpc);
14103 target_class = TREE_TYPE (class_decl);
14105 if (CLASS_INTERFACE (TYPE_NAME (target_class)))
14107 /* For interfaces, adding a static 'class$' method directly
14108 is illegal. So create an inner class to contain the new
14109 method. Empirically this matches the behavior of javac. */
14110 tree t, inner;
14111 /* We want the generated inner class inside the outermost class. */
14112 GET_CPC_LIST() = cpc;
14113 t = build_wfl_node (DECL_NAME (TYPE_NAME (object_type_node)));
14114 inner = create_anonymous_class (t);
14115 target_class = TREE_TYPE (inner);
14116 end_class_declaration (1);
14117 GET_CPC_LIST() = cpc_list;
14120 if (TYPE_DOT_CLASS (target_class) == NULL_TREE)
14121 build_dot_class_method (target_class);
14123 if (this_class != target_class)
14124 TYPE_DOT_CLASS (this_class) = TYPE_DOT_CLASS (target_class);
14127 EXPR_WFL_LINECOL (node) = location;
14128 return node;
14131 /* Complete an incomplete class reference operator. */
14132 static tree
14133 patch_incomplete_class_ref (tree node)
14135 tree type = TREE_OPERAND (node, 0);
14136 tree ref_type;
14138 if (!(ref_type = resolve_type_during_patch (type)))
14139 return error_mark_node;
14141 /* If we're not emitting class files and we know ref_type is a
14142 compiled class, build a direct reference. */
14143 if ((! flag_emit_class_files && is_compiled_class (ref_type))
14144 || JPRIMITIVE_TYPE_P (ref_type)
14145 || TREE_CODE (ref_type) == VOID_TYPE)
14147 tree dot = build_class_ref (ref_type);
14148 /* A class referenced by `foo.class' is initialized. */
14149 if (!flag_emit_class_files)
14150 dot = build_class_init (ref_type, dot);
14151 return java_complete_tree (dot);
14154 /* If we're emitting class files and we have to deal with non
14155 primitive types, we invoke the synthetic static method `class$'. */
14156 ref_type = build_dot_class_method_invocation (current_class, ref_type);
14157 return java_complete_tree (ref_type);
14160 /* 15.14 Unary operators. We return error_mark_node in case of error,
14161 but preserve the type of NODE if the type is fixed. */
14163 static tree
14164 patch_unaryop (tree node, tree wfl_op)
14166 tree op = TREE_OPERAND (node, 0);
14167 tree op_type = TREE_TYPE (op);
14168 tree prom_type = NULL_TREE, value, decl;
14169 int outer_field_flag = 0;
14170 int code = TREE_CODE (node);
14171 int error_found = 0;
14173 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14175 switch (code)
14177 /* 15.13.2 Postfix Increment Operator ++ */
14178 case POSTINCREMENT_EXPR:
14179 /* 15.13.3 Postfix Increment Operator -- */
14180 case POSTDECREMENT_EXPR:
14181 /* 15.14.1 Prefix Increment Operator ++ */
14182 case PREINCREMENT_EXPR:
14183 /* 15.14.2 Prefix Decrement Operator -- */
14184 case PREDECREMENT_EXPR:
14185 op = decl = extract_field_decl (op);
14186 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
14187 /* We might be trying to change an outer field accessed using
14188 access method. */
14189 if (outer_field_flag)
14191 /* Retrieve the decl of the field we're trying to access. We
14192 do that by first retrieving the function we would call to
14193 access the field. It has been already verified that this
14194 field isn't final */
14195 if (flag_emit_class_files)
14196 decl = TREE_OPERAND (op, 0);
14197 else
14198 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14199 decl = DECL_FUNCTION_ACCESS_DECL (decl);
14201 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14202 else if (!JDECL_P (decl)
14203 && TREE_CODE (decl) != COMPONENT_REF
14204 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14205 && TREE_CODE (decl) != INDIRECT_REF
14206 && !(TREE_CODE (decl) == COMPOUND_EXPR
14207 && TREE_OPERAND (decl, 1)
14208 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14210 TREE_TYPE (node) = error_mark_node;
14211 error_found = 1;
14214 /* From now on, we know that op if a variable and that it has a
14215 valid wfl. We use wfl_op to locate errors related to the
14216 ++/-- operand. */
14217 if (!JNUMERIC_TYPE_P (op_type))
14219 parse_error_context
14220 (wfl_op, "Invalid argument type %qs to %qs",
14221 lang_printable_name (op_type, 0), operator_string (node));
14222 TREE_TYPE (node) = error_mark_node;
14223 error_found = 1;
14225 else
14227 /* Before the addition, binary numeric promotion is performed on
14228 both operands, if really necessary */
14229 if (JINTEGRAL_TYPE_P (op_type))
14231 value = build_int_cst (op_type, 1);
14232 TREE_TYPE (node) = op_type;
14234 else
14236 value = build_int_cst (NULL_TREE, 1);
14237 TREE_TYPE (node) =
14238 binary_numeric_promotion (op_type,
14239 TREE_TYPE (value), &op, &value);
14242 /* We remember we might be accessing an outer field */
14243 if (outer_field_flag)
14245 /* We re-generate an access to the field */
14246 value = build2 (PLUS_EXPR, TREE_TYPE (op),
14247 build_outer_field_access (wfl_op, decl), value);
14249 /* And we patch the original access$() into a write
14250 with plus_op as a rhs */
14251 return outer_field_access_fix (node, op, value);
14254 /* And write back into the node. */
14255 TREE_OPERAND (node, 0) = op;
14256 TREE_OPERAND (node, 1) = value;
14257 /* Convert the overall back into its original type, if
14258 necessary, and return */
14259 if (JINTEGRAL_TYPE_P (op_type))
14260 return fold (node);
14261 else
14262 return fold (convert (op_type, node));
14264 break;
14266 /* 15.14.3 Unary Plus Operator + */
14267 case UNARY_PLUS_EXPR:
14268 /* 15.14.4 Unary Minus Operator - */
14269 case NEGATE_EXPR:
14270 if (!JNUMERIC_TYPE_P (op_type))
14272 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14273 TREE_TYPE (node) = error_mark_node;
14274 error_found = 1;
14276 /* Unary numeric promotion is performed on operand */
14277 else
14279 op = do_unary_numeric_promotion (op);
14280 prom_type = TREE_TYPE (op);
14281 if (code == UNARY_PLUS_EXPR)
14282 return fold (op);
14284 break;
14286 /* 15.14.5 Bitwise Complement Operator ~ */
14287 case BIT_NOT_EXPR:
14288 if (!JINTEGRAL_TYPE_P (op_type))
14290 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14291 TREE_TYPE (node) = error_mark_node;
14292 error_found = 1;
14294 else
14296 op = do_unary_numeric_promotion (op);
14297 prom_type = TREE_TYPE (op);
14299 break;
14301 /* 15.14.6 Logical Complement Operator ! */
14302 case TRUTH_NOT_EXPR:
14303 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14305 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14306 /* But the type is known. We will report an error if further
14307 attempt of a assignment is made with this rhs */
14308 TREE_TYPE (node) = boolean_type_node;
14309 error_found = 1;
14311 else
14312 prom_type = boolean_type_node;
14313 break;
14315 /* 15.15 Cast Expression */
14316 case CONVERT_EXPR:
14317 value = patch_cast (node, wfl_operator);
14318 if (value == error_mark_node)
14320 /* If this cast is part of an assignment, we tell the code
14321 that deals with it not to complain about a mismatch,
14322 because things have been cast, anyways */
14323 TREE_TYPE (node) = error_mark_node;
14324 error_found = 1;
14326 else
14328 value = fold (value);
14329 return value;
14331 break;
14334 if (error_found)
14335 return error_mark_node;
14337 /* There are cases where node has been replaced by something else
14338 and we don't end up returning here: UNARY_PLUS_EXPR,
14339 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14340 TREE_OPERAND (node, 0) = fold (op);
14341 TREE_TYPE (node) = prom_type;
14342 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14343 return fold (node);
14346 /* Generic type resolution that sometimes takes place during node
14347 patching. Returned the resolved type or generate an error
14348 message. Return the resolved type or NULL_TREE. */
14350 static tree
14351 resolve_type_during_patch (tree type)
14353 if (unresolved_type_p (type, NULL))
14355 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14356 if (!type_decl)
14358 parse_error_context (type,
14359 "Class %qs not found in type declaration",
14360 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14361 return NULL_TREE;
14364 check_deprecation (type, type_decl);
14366 return TREE_TYPE (type_decl);
14368 return type;
14371 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14372 found. Otherwise NODE or something meant to replace it is returned. */
14374 static tree
14375 patch_cast (tree node, tree wfl_op)
14377 tree op = TREE_OPERAND (node, 0);
14378 tree cast_type = TREE_TYPE (node);
14379 tree patched, op_type;
14380 char *t1;
14382 /* Some string patching might be necessary at this stage */
14383 if ((patched = patch_string (op)))
14384 TREE_OPERAND (node, 0) = op = patched;
14385 op_type = TREE_TYPE (op);
14387 /* First resolve OP_TYPE if unresolved */
14388 if (!(cast_type = resolve_type_during_patch (cast_type)))
14389 return error_mark_node;
14391 /* Check on cast that are proven correct at compile time */
14392 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14394 /* Same type */
14395 if (cast_type == op_type)
14396 return node;
14398 /* A narrowing conversion from a floating-point number to an
14399 integral type requires special handling (5.1.3). */
14400 if (JFLOAT_TYPE_P (op_type) && JINTEGRAL_TYPE_P (cast_type))
14401 if (cast_type != long_type_node)
14402 op = convert (integer_type_node, op);
14404 /* Try widening/narrowing conversion. Potentially, things need
14405 to be worked out in gcc so we implement the extreme cases
14406 correctly. fold_convert() needs to be fixed. */
14407 return convert (cast_type, op);
14410 /* It's also valid to cast a boolean into a boolean */
14411 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14412 return node;
14414 /* null can be casted to references */
14415 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14416 return build_null_of_type (cast_type);
14418 /* The remaining legal casts involve conversion between reference
14419 types. Check for their compile time correctness. */
14420 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14421 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14423 TREE_TYPE (node) = promote_type (cast_type);
14424 /* Now, the case can be determined correct at compile time if
14425 OP_TYPE can be converted into CAST_TYPE by assignment
14426 conversion (5.2) */
14428 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14430 TREE_SET_CODE (node, NOP_EXPR);
14431 return node;
14434 if (flag_emit_class_files)
14436 TREE_SET_CODE (node, CONVERT_EXPR);
14437 return node;
14440 /* The cast requires a run-time check */
14441 return build3 (CALL_EXPR, promote_type (cast_type),
14442 build_address_of (soft_checkcast_node),
14443 tree_cons (NULL_TREE, build_class_ref (cast_type),
14444 build_tree_list (NULL_TREE, op)),
14445 NULL_TREE);
14448 /* Any other casts are proven incorrect at compile time */
14449 t1 = xstrdup (lang_printable_name (op_type, 0));
14450 parse_error_context (wfl_op, "Invalid cast from %qs to %qs",
14451 t1, lang_printable_name (cast_type, 0));
14452 free (t1);
14453 return error_mark_node;
14456 /* Build a null constant and give it the type TYPE. */
14458 static tree
14459 build_null_of_type (tree type)
14461 tree node = build_int_cst (promote_type (type), 0);
14462 return node;
14465 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14466 a list of indices. */
14467 static tree
14468 build_array_ref (int location, tree array, tree index)
14470 tree node = build4 (ARRAY_REF, NULL_TREE, array, index,
14471 NULL_TREE, NULL_TREE);
14472 EXPR_WFL_LINECOL (node) = location;
14473 return node;
14476 /* 15.12 Array Access Expression */
14478 static tree
14479 patch_array_ref (tree node)
14481 tree array = TREE_OPERAND (node, 0);
14482 tree array_type = TREE_TYPE (array);
14483 tree index = TREE_OPERAND (node, 1);
14484 tree index_type = TREE_TYPE (index);
14485 int error_found = 0;
14487 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14489 if (TREE_CODE (array_type) == POINTER_TYPE)
14490 array_type = TREE_TYPE (array_type);
14492 /* The array reference must be an array */
14493 if (!TYPE_ARRAY_P (array_type))
14495 parse_error_context
14496 (wfl_operator,
14497 "%<[]%> can only be applied to arrays. It can't be applied to %qs",
14498 lang_printable_name (array_type, 0));
14499 TREE_TYPE (node) = error_mark_node;
14500 error_found = 1;
14503 /* The array index undergoes unary numeric promotion. The promoted
14504 type must be int */
14505 index = do_unary_numeric_promotion (index);
14506 if (TREE_TYPE (index) != int_type_node)
14508 if (valid_cast_to_p (index_type, int_type_node))
14509 parse_error_context (wfl_operator,
14510 "Incompatible type for %<[]%>. Explicit cast needed to convert %qs to %<int%>",
14511 lang_printable_name (index_type, 0));
14512 else
14513 parse_error_context (wfl_operator,
14514 "Incompatible type for %<[]%>. Can't convert %qs to %<int%>",
14515 lang_printable_name (index_type, 0));
14516 TREE_TYPE (node) = error_mark_node;
14517 error_found = 1;
14520 if (error_found)
14521 return error_mark_node;
14523 array_type = TYPE_ARRAY_ELEMENT (array_type);
14525 if (flag_emit_class_files)
14527 TREE_OPERAND (node, 0) = array;
14528 TREE_OPERAND (node, 1) = index;
14530 else
14531 node = build_java_arrayaccess (array, array_type, index);
14532 TREE_TYPE (node) = array_type;
14533 return node;
14536 /* 15.9 Array Creation Expressions */
14538 static tree
14539 build_newarray_node (tree type, tree dims, int extra_dims)
14541 tree node = build3 (NEW_ARRAY_EXPR, NULL_TREE, type,
14542 nreverse (dims),
14543 build_int_cst (NULL_TREE, extra_dims));
14544 return node;
14547 static tree
14548 patch_newarray (tree node)
14550 tree type = TREE_OPERAND (node, 0);
14551 tree dims = TREE_OPERAND (node, 1);
14552 tree cdim, array_type;
14553 int error_found = 0;
14554 int ndims = 0;
14555 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14557 /* Dimension types are verified. It's better for the types to be
14558 verified in order. */
14559 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14561 int dim_error = 0;
14562 tree dim = TREE_VALUE (cdim);
14564 /* Dim might have been saved during its evaluation */
14565 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14567 /* The type of each specified dimension must be an integral type. */
14568 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14569 dim_error = 1;
14571 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14572 promoted type must be int. */
14573 else
14575 dim = do_unary_numeric_promotion (dim);
14576 if (TREE_TYPE (dim) != int_type_node)
14577 dim_error = 1;
14580 /* Report errors on types here */
14581 if (dim_error)
14583 parse_error_context
14584 (TREE_PURPOSE (cdim),
14585 "Incompatible type for dimension in array creation expression. %s convert %qs to %<int%>",
14586 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14587 "Explicit cast needed to" : "Can't"),
14588 lang_printable_name (TREE_TYPE (dim), 0));
14589 error_found = 1;
14592 TREE_PURPOSE (cdim) = NULL_TREE;
14595 /* Resolve array base type if unresolved */
14596 if (!(type = resolve_type_during_patch (type)))
14597 error_found = 1;
14599 if (error_found)
14601 /* We don't want further evaluation of this bogus array creation
14602 operation */
14603 TREE_TYPE (node) = error_mark_node;
14604 return error_mark_node;
14607 /* Set array_type to the actual (promoted) array type of the result. */
14608 if (TREE_CODE (type) == RECORD_TYPE)
14609 type = build_pointer_type (type);
14610 while (--xdims >= 0)
14612 type = promote_type (build_java_array_type (type, -1));
14614 dims = nreverse (dims);
14615 array_type = type;
14616 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14618 type = array_type;
14619 array_type
14620 = build_java_array_type (type,
14621 TREE_CODE (cdim) == INTEGER_CST
14622 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14623 : -1);
14624 array_type = promote_type (array_type);
14626 dims = nreverse (dims);
14628 /* The node is transformed into a function call. Things are done
14629 differently according to the number of dimensions. If the number
14630 of dimension is equal to 1, then the nature of the base type
14631 (primitive or not) matters. */
14632 if (ndims == 1)
14633 return build_new_array (type, TREE_VALUE (dims));
14635 /* Can't reuse what's already written in expr.c because it uses the
14636 JVM stack representation. Provide a build_multianewarray. FIXME */
14637 return build3 (CALL_EXPR, array_type,
14638 build_address_of (soft_multianewarray_node),
14639 tree_cons (NULL_TREE,
14640 build_class_ref (TREE_TYPE (array_type)),
14641 tree_cons (NULL_TREE,
14642 build_int_cst (NULL_TREE, ndims),
14643 dims)),
14644 NULL_TREE);
14647 /* 10.6 Array initializer. */
14649 /* Build a wfl for array element that don't have one, so we can
14650 pin-point errors. */
14652 static tree
14653 maybe_build_array_element_wfl (tree node)
14655 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14657 /* FIXME - old code used "prev_lc.line" and "elc.prev_col */
14658 return build_expr_wfl (NULL_TREE,
14659 #ifdef USE_MAPPED_LOCATION
14660 input_location
14661 #else
14662 ctxp->filename,
14663 ctxp->lexer->token_start.line,
14664 ctxp->lexer->token_start.col
14665 #endif
14668 else
14669 return NULL_TREE;
14672 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14673 identification of initialized arrays easier to detect during walk
14674 and expansion. */
14676 static tree
14677 build_new_array_init (int location, tree values)
14679 tree constructor = build_constructor (NULL_TREE, values);
14680 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14681 EXPR_WFL_LINECOL (to_return) = location;
14682 return to_return;
14685 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14686 occurred. Otherwise return NODE after having set its type
14687 appropriately. */
14689 static tree
14690 patch_new_array_init (tree type, tree node)
14692 int error_seen = 0;
14693 tree current, element_type;
14694 HOST_WIDE_INT length;
14695 int all_constant = 1;
14696 tree init = TREE_OPERAND (node, 0);
14698 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14700 parse_error_context (node,
14701 "Invalid array initializer for non-array type %qs",
14702 lang_printable_name (type, 1));
14703 return error_mark_node;
14705 type = TREE_TYPE (type);
14706 element_type = TYPE_ARRAY_ELEMENT (type);
14708 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14710 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14711 current; length++, current = TREE_CHAIN (current))
14713 tree elt = TREE_VALUE (current);
14714 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14716 error_seen |= array_constructor_check_entry (element_type, current);
14717 elt = TREE_VALUE (current);
14718 /* When compiling to native code, STRING_CST is converted to
14719 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14720 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14721 all_constant = 0;
14723 else
14725 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14726 TREE_PURPOSE (current) = NULL_TREE;
14727 all_constant = 0;
14729 if (elt && TREE_CODE (elt) == TREE_LIST
14730 && TREE_VALUE (elt) == error_mark_node)
14731 error_seen = 1;
14734 if (error_seen)
14735 return error_mark_node;
14737 /* Create a new type. We can't reuse the one we have here by
14738 patching its dimension because it originally is of dimension -1
14739 hence reused by gcc. This would prevent triangular arrays. */
14740 type = build_java_array_type (element_type, length);
14741 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14742 TREE_TYPE (node) = promote_type (type);
14743 TREE_CONSTANT (init) = all_constant;
14744 TREE_INVARIANT (init) = all_constant;
14745 TREE_CONSTANT (node) = all_constant;
14746 TREE_INVARIANT (node) = all_constant;
14747 return node;
14750 /* Verify that one entry of the initializer element list can be
14751 assigned to the array base type. Report 1 if an error occurred, 0
14752 otherwise. */
14754 static int
14755 array_constructor_check_entry (tree type, tree entry)
14757 char *array_type_string = NULL; /* For error reports */
14758 tree value, type_value, new_value, wfl_value, patched;
14759 int error_seen = 0;
14761 new_value = NULL_TREE;
14762 wfl_value = TREE_VALUE (entry);
14764 value = java_complete_tree (TREE_VALUE (entry));
14765 /* patch_string return error_mark_node if arg is error_mark_node */
14766 if ((patched = patch_string (value)))
14767 value = patched;
14768 if (value == error_mark_node)
14769 return 1;
14771 type_value = TREE_TYPE (value);
14773 /* At anytime, try_builtin_assignconv can report a warning on
14774 constant overflow during narrowing. */
14775 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14776 new_value = try_builtin_assignconv (wfl_operator, type, value);
14777 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14778 type_value = promote_type (type);
14780 /* Check and report errors */
14781 if (!new_value)
14783 const char *const msg = (!valid_cast_to_p (type_value, type) ?
14784 "Can't" : "Explicit cast needed to");
14785 if (!array_type_string)
14786 array_type_string = xstrdup (lang_printable_name (type, 1));
14787 parse_error_context
14788 (wfl_operator, "Incompatible type for array. %s convert %qs to %qs",
14789 msg, lang_printable_name (type_value, 1), array_type_string);
14790 error_seen = 1;
14793 if (new_value)
14794 TREE_VALUE (entry) = new_value;
14796 if (array_type_string)
14797 free (array_type_string);
14799 TREE_PURPOSE (entry) = NULL_TREE;
14800 return error_seen;
14803 static tree
14804 build_this (int location)
14806 tree node = build_wfl_node (this_identifier_node);
14807 TREE_SET_CODE (node, THIS_EXPR);
14808 EXPR_WFL_LINECOL (node) = location;
14809 return node;
14812 /* 14.15 The return statement. It builds a modify expression that
14813 assigns the returned value to the RESULT_DECL that hold the value
14814 to be returned. */
14816 static tree
14817 build_return (int location, tree op)
14819 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14820 EXPR_WFL_LINECOL (node) = location;
14821 node = build_debugable_stmt (location, node);
14822 return node;
14825 static tree
14826 patch_return (tree node)
14828 tree return_exp = TREE_OPERAND (node, 0);
14829 tree meth = current_function_decl;
14830 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14831 int error_found = 0;
14833 TREE_TYPE (node) = error_mark_node;
14834 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14836 /* It's invalid to have a return value within a function that is
14837 declared with the keyword void or that is a constructor */
14838 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14839 error_found = 1;
14841 /* It's invalid to use a return statement in a static block */
14842 if (DECL_CLINIT_P (current_function_decl))
14843 error_found = 1;
14845 /* It's invalid to have a no return value within a function that
14846 isn't declared with the keyword `void' */
14847 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14848 error_found = 2;
14850 if (DECL_INSTINIT_P (current_function_decl))
14851 error_found = 1;
14853 if (error_found)
14855 if (DECL_INSTINIT_P (current_function_decl))
14856 parse_error_context (wfl_operator,
14857 "%<return%> inside instance initializer");
14859 else if (DECL_CLINIT_P (current_function_decl))
14860 parse_error_context (wfl_operator,
14861 "%<return%> inside static initializer");
14863 else if (!DECL_CONSTRUCTOR_P (meth))
14865 char *t = xstrdup (lang_printable_name (mtype, 0));
14866 parse_error_context (wfl_operator,
14867 "%<return%> with%s value from %<%s %s%>",
14868 (error_found == 1 ? "" : "out"),
14869 t, lang_printable_name (meth, 2));
14870 free (t);
14872 else
14873 parse_error_context (wfl_operator,
14874 "%<return%> with value from constructor %qs",
14875 lang_printable_name (meth, 2));
14876 return error_mark_node;
14879 /* If we have a return_exp, build a modify expression and expand
14880 it. Note: at that point, the assignment is declared valid, but we
14881 may want to carry some more hacks */
14882 if (return_exp)
14884 tree exp = java_complete_tree (return_exp);
14885 tree modify, patched;
14887 if ((patched = patch_string (exp)))
14888 exp = patched;
14890 modify = build2 (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14891 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14892 modify = java_complete_tree (modify);
14894 if (modify != error_mark_node)
14896 TREE_SIDE_EFFECTS (modify) = 1;
14897 TREE_OPERAND (node, 0) = modify;
14899 else
14900 return error_mark_node;
14902 TREE_TYPE (node) = void_type_node;
14903 TREE_SIDE_EFFECTS (node) = 1;
14904 return node;
14907 /* 14.8 The if Statement */
14909 static tree
14910 build_if_else_statement (int location, tree expression, tree if_body,
14911 tree else_body)
14913 tree node;
14914 if (!else_body)
14915 else_body = build_java_empty_stmt ();
14916 node = build3 (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14917 EXPR_WFL_LINECOL (node) = location;
14918 node = build_debugable_stmt (location, node);
14919 return node;
14922 static tree
14923 patch_if_else_statement (tree node)
14925 tree expression = TREE_OPERAND (node, 0);
14926 int can_complete_normally
14927 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14928 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
14930 TREE_TYPE (node) = error_mark_node;
14931 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14933 /* The type of expression must be boolean */
14934 if (TREE_TYPE (expression) != boolean_type_node
14935 && TREE_TYPE (expression) != promoted_boolean_type_node)
14937 parse_error_context
14938 (wfl_operator,
14939 "Incompatible type for %<if%>. Can't convert %qs to %<boolean%>",
14940 lang_printable_name (TREE_TYPE (expression), 0));
14941 return error_mark_node;
14944 TREE_TYPE (node) = void_type_node;
14945 TREE_SIDE_EFFECTS (node) = 1;
14946 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14947 return node;
14950 /* 14.6 Labeled Statements */
14952 /* Action taken when a labeled statement is parsed. a new
14953 LABELED_BLOCK_EXPR is created. No statement is attached to the
14954 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14956 static tree
14957 build_labeled_block (int location, tree label)
14959 tree label_name ;
14960 tree label_decl, node;
14961 if (label == NULL_TREE || label == continue_identifier_node)
14962 label_name = label;
14963 else
14965 label_name = merge_qualified_name (label_id, label);
14966 /* Issue an error if we try to reuse a label that was previously
14967 declared */
14968 if (IDENTIFIER_LOCAL_VALUE (label_name))
14970 EXPR_WFL_LINECOL (wfl_operator) = location;
14971 parse_error_context (wfl_operator,
14972 "Declaration of %qs shadows a previous label declaration",
14973 IDENTIFIER_POINTER (label));
14974 EXPR_WFL_LINECOL (wfl_operator) =
14975 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14976 parse_error_context (wfl_operator,
14977 "This is the location of the previous declaration of label %qs",
14978 IDENTIFIER_POINTER (label));
14979 java_error_count--;
14983 label_decl = create_label_decl (label_name);
14984 node = build2 (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14985 EXPR_WFL_LINECOL (node) = location;
14986 TREE_SIDE_EFFECTS (node) = 1;
14987 return node;
14990 /* A labeled statement LBE is attached a statement. */
14992 static tree
14993 finish_labeled_statement (tree lbe, /* Labeled block expr */
14994 tree statement)
14996 /* In anyways, tie the loop to its statement */
14997 LABELED_BLOCK_BODY (lbe) = statement;
14998 pop_labeled_block ();
14999 POP_LABELED_BLOCK ();
15000 return lbe;
15003 /* 14.10, 14.11, 14.12 Loop Statements */
15005 /* Create an empty LOOP_EXPR and make it the last in the nested loop
15006 list. */
15008 static tree
15009 build_new_loop (tree loop_body)
15011 tree loop = build1 (LOOP_EXPR, NULL_TREE, loop_body);
15012 TREE_SIDE_EFFECTS (loop) = 1;
15013 PUSH_LOOP (loop);
15014 return loop;
15017 /* Create a loop body according to the following structure:
15018 COMPOUND_EXPR
15019 COMPOUND_EXPR (loop main body)
15020 EXIT_EXPR (this order is for while/for loops.
15021 LABELED_BLOCK_EXPR the order is reversed for do loops)
15022 LABEL_DECL (a continue occurring here branches at the
15023 BODY end of this labeled block)
15024 INCREMENT (if any)
15026 REVERSED, if nonzero, tells that the loop condition expr comes
15027 after the body, like in the do-while loop.
15029 To obtain a loop, the loop body structure described above is
15030 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15032 LABELED_BLOCK_EXPR
15033 LABEL_DECL (use this label to exit the loop)
15034 LOOP_EXPR
15035 <structure described above> */
15037 static tree
15038 build_loop_body (int location, tree condition, int reversed)
15040 tree first, second, body;
15042 condition = build1 (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15043 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15044 condition = build_debugable_stmt (location, condition);
15045 TREE_SIDE_EFFECTS (condition) = 1;
15047 body = build_labeled_block (0, continue_identifier_node);
15048 first = (reversed ? body : condition);
15049 second = (reversed ? condition : body);
15050 return build2 (COMPOUND_EXPR, NULL_TREE,
15051 build2 (COMPOUND_EXPR, NULL_TREE, first, second),
15052 build_java_empty_stmt ());
15055 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15056 their order) on the current loop. Unlink the current loop from the
15057 loop list. */
15059 static tree
15060 finish_loop_body (int location, tree condition, tree body, int reversed)
15062 tree to_return = ctxp->current_loop;
15063 tree loop_body = LOOP_EXPR_BODY (to_return);
15064 if (condition)
15066 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15067 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15068 The real EXIT_EXPR is one operand further. */
15069 EXPR_WFL_LINECOL (cnode) = location;
15070 if (TREE_CODE (cnode) == EXPR_WITH_FILE_LOCATION)
15072 cnode = EXPR_WFL_NODE (cnode);
15073 /* This one is for accurate error reports */
15074 EXPR_WFL_LINECOL (cnode) = location;
15076 TREE_OPERAND (cnode, 0) = condition;
15078 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15079 POP_LOOP ();
15080 return to_return;
15083 /* Tailored version of finish_loop_body for FOR loops, when FOR
15084 loops feature the condition part */
15086 static tree
15087 finish_for_loop (int location, tree condition, tree update, tree body)
15089 /* Put the condition and the loop body in place */
15090 tree loop = finish_loop_body (location, condition, body, 0);
15091 /* LOOP is the current loop which has been now popped of the loop
15092 stack. Mark the update block as reachable and install it. We do
15093 this because the (current interpretation of the) JLS requires
15094 that the update expression be considered reachable even if the
15095 for loop's body doesn't complete normally. */
15096 if (update != NULL_TREE && !IS_EMPTY_STMT (update))
15098 tree up2 = update;
15099 if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15100 up2 = EXPR_WFL_NODE (up2);
15101 /* It is possible for the update expression to be an
15102 EXPR_WFL_NODE wrapping nothing. */
15103 if (up2 != NULL_TREE && !IS_EMPTY_STMT (up2))
15105 /* Try to detect constraint violations. These would be
15106 programming errors somewhere. */
15107 if (! EXPR_P (up2) || TREE_CODE (up2) == LOOP_EXPR)
15108 abort ();
15109 SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15112 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15113 return loop;
15116 /* Try to find the loop a block might be related to. This comprises
15117 the case where the LOOP_EXPR is found as the second operand of a
15118 COMPOUND_EXPR, because the loop happens to have an initialization
15119 part, then expressed as the first operand of the COMPOUND_EXPR. If
15120 the search finds something, 1 is returned. Otherwise, 0 is
15121 returned. The search is assumed to start from a
15122 LABELED_BLOCK_EXPR's block. */
15124 static tree
15125 search_loop (tree statement)
15127 if (TREE_CODE (statement) == LOOP_EXPR)
15128 return statement;
15130 if (TREE_CODE (statement) == BLOCK)
15131 statement = BLOCK_SUBBLOCKS (statement);
15132 else
15133 return NULL_TREE;
15135 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15136 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15137 statement = TREE_OPERAND (statement, 1);
15139 return (TREE_CODE (statement) == LOOP_EXPR
15140 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15143 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15144 returned otherwise. */
15146 static int
15147 labeled_block_contains_loop_p (tree block, tree loop)
15149 if (!block)
15150 return 0;
15152 if (LABELED_BLOCK_BODY (block) == loop)
15153 return 1;
15155 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15156 return 1;
15158 return 0;
15161 /* If the loop isn't surrounded by a labeled statement, create one and
15162 insert LOOP as its body. */
15164 static tree
15165 patch_loop_statement (tree loop)
15167 tree loop_label;
15169 TREE_TYPE (loop) = void_type_node;
15170 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15171 return loop;
15173 loop_label = build_labeled_block (0, NULL_TREE);
15174 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15175 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15176 LABELED_BLOCK_BODY (loop_label) = loop;
15177 PUSH_LABELED_BLOCK (loop_label);
15178 return loop_label;
15181 /* 14.13, 14.14: break and continue Statements */
15183 /* Build a break or a continue statement. a null NAME indicates an
15184 unlabeled break/continue statement. */
15186 static tree
15187 build_bc_statement (int location, int is_break, tree name)
15189 tree break_continue, label_block_expr = NULL_TREE;
15191 if (name)
15193 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15194 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15195 /* Null means that we don't have a target for this named
15196 break/continue. In this case, we make the target to be the
15197 label name, so that the error can be reported accurately in
15198 patch_bc_statement. */
15199 label_block_expr = EXPR_WFL_NODE (name);
15201 /* Unlabeled break/continue will be handled during the
15202 break/continue patch operation */
15203 break_continue = build1 (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr);
15205 IS_BREAK_STMT_P (break_continue) = is_break;
15206 TREE_SIDE_EFFECTS (break_continue) = 1;
15207 EXPR_WFL_LINECOL (break_continue) = location;
15208 break_continue = build_debugable_stmt (location, break_continue);
15209 return break_continue;
15212 /* Verification of a break/continue statement. */
15214 static tree
15215 patch_bc_statement (tree node)
15217 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15218 tree labeled_block = ctxp->current_labeled_block;
15219 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15221 /* Having an identifier here means that the target is unknown. */
15222 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15224 parse_error_context (wfl_operator, "No label definition found for %qs",
15225 IDENTIFIER_POINTER (bc_label));
15226 return error_mark_node;
15228 if (! IS_BREAK_STMT_P (node))
15230 /* It's a continue statement. */
15231 for (;; labeled_block = TREE_CHAIN (labeled_block))
15233 if (labeled_block == NULL_TREE)
15235 if (bc_label == NULL_TREE)
15236 parse_error_context (wfl_operator,
15237 "%<continue%> must be in loop");
15238 else
15239 parse_error_context
15240 (wfl_operator, "continue label %qs does not name a loop",
15241 IDENTIFIER_POINTER (bc_label));
15242 return error_mark_node;
15244 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15245 == continue_identifier_node)
15246 && (bc_label == NULL_TREE
15247 || TREE_CHAIN (labeled_block) == bc_label))
15249 bc_label = labeled_block;
15250 break;
15254 else if (!bc_label)
15256 for (;; labeled_block = TREE_CHAIN (labeled_block))
15258 if (labeled_block == NULL_TREE)
15260 parse_error_context (wfl_operator,
15261 "%<break%> must be in loop or switch");
15262 return error_mark_node;
15264 target_stmt = LABELED_BLOCK_BODY (labeled_block);
15265 if (TREE_CODE (target_stmt) == SWITCH_EXPR
15266 || search_loop (target_stmt))
15268 bc_label = labeled_block;
15269 break;
15274 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15275 CAN_COMPLETE_NORMALLY (bc_label) = 1;
15277 /* Our break/continue don't return values. */
15278 TREE_TYPE (node) = void_type_node;
15279 /* Encapsulate the break within a compound statement so that it's
15280 expanded all the times by expand_expr (and not clobbered
15281 sometimes, like after a if statement) */
15282 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15283 TREE_SIDE_EFFECTS (node) = 1;
15284 return node;
15287 /* Process the exit expression belonging to a loop. Its type must be
15288 boolean. */
15290 static tree
15291 patch_exit_expr (tree node)
15293 tree expression = TREE_OPERAND (node, 0);
15294 TREE_TYPE (node) = error_mark_node;
15295 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15297 /* The type of expression must be boolean */
15298 if (TREE_TYPE (expression) != boolean_type_node)
15300 parse_error_context
15301 (wfl_operator,
15302 "Incompatible type for loop conditional. Can't convert %qs to %<boolean%>",
15303 lang_printable_name (TREE_TYPE (expression), 0));
15304 return error_mark_node;
15306 /* Now we know things are allright, invert the condition, fold and
15307 return */
15308 TREE_OPERAND (node, 0) =
15309 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15311 if (! integer_zerop (TREE_OPERAND (node, 0))
15312 && ctxp->current_loop != NULL_TREE
15313 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15314 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15315 if (! integer_onep (TREE_OPERAND (node, 0)))
15316 CAN_COMPLETE_NORMALLY (node) = 1;
15319 TREE_TYPE (node) = void_type_node;
15320 return node;
15323 /* 14.9 Switch statement */
15325 static tree
15326 patch_switch_statement (tree node)
15328 tree se = TREE_OPERAND (node, 0), se_type;
15329 tree save, iter;
15331 /* Complete the switch expression */
15332 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15333 se_type = TREE_TYPE (se);
15334 /* The type of the switch expression must be char, byte, short or
15335 int */
15336 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15338 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15339 parse_error_context (wfl_operator,
15340 "Incompatible type for %<switch%>. Can't convert %qs to %<int%>",
15341 lang_printable_name (se_type, 0));
15342 /* This is what java_complete_tree will check */
15343 TREE_OPERAND (node, 0) = error_mark_node;
15344 return error_mark_node;
15347 /* Save and restore the outer case label list. */
15348 save = case_label_list;
15349 case_label_list = NULL_TREE;
15351 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15353 /* See if we've found a duplicate label. We can't leave this until
15354 code generation, because in `--syntax-only' and `-C' modes we
15355 don't do ordinary code generation. */
15356 for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15358 HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15359 tree subiter;
15360 for (subiter = TREE_CHAIN (iter);
15361 subiter != NULL_TREE;
15362 subiter = TREE_CHAIN (subiter))
15364 HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15365 if (val == subval)
15367 EXPR_WFL_LINECOL (wfl_operator)
15368 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15369 /* The case_label_list is in reverse order, so print the
15370 outer label first. */
15371 parse_error_context (wfl_operator, "duplicate case label: %<"
15372 HOST_WIDE_INT_PRINT_DEC "%>", subval);
15373 EXPR_WFL_LINECOL (wfl_operator)
15374 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15375 parse_error_context (wfl_operator, "original label is here");
15377 break;
15382 case_label_list = save;
15384 /* Ready to return */
15385 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15387 TREE_TYPE (node) = error_mark_node;
15388 return error_mark_node;
15390 TREE_TYPE (node) = void_type_node;
15391 TREE_SIDE_EFFECTS (node) = 1;
15392 CAN_COMPLETE_NORMALLY (node)
15393 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15394 || ! SWITCH_HAS_DEFAULT (node);
15395 return node;
15398 /* Assertions. */
15400 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15401 might be NULL_TREE. */
15402 static tree
15403 build_assertion (
15404 #ifdef USE_MAPPED_LOCATION
15405 source_location location,
15406 #else
15407 int location,
15408 #endif
15409 tree condition, tree value)
15411 tree node;
15412 tree klass = GET_CPC ();
15414 if (! enable_assertions (klass))
15416 condition = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15417 boolean_false_node, condition);
15418 if (value == NULL_TREE)
15419 value = build_java_empty_stmt ();
15420 return build_if_else_statement (location, condition,
15421 value, NULL_TREE);
15424 if (! CLASS_USES_ASSERTIONS (klass))
15426 tree field, classdollar, id, call;
15427 tree class_type = TREE_TYPE (klass);
15429 field = add_field (class_type,
15430 get_identifier ("$assertionsDisabled"),
15431 boolean_type_node,
15432 ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15433 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15434 FIELD_SYNTHETIC (field) = 1;
15436 classdollar = build_incomplete_class_ref (location, class_type);
15438 /* Call CLASS.desiredAssertionStatus(). */
15439 id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15440 call = build3 (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15441 call = make_qualified_primary (classdollar, call, location);
15442 TREE_SIDE_EFFECTS (call) = 1;
15444 /* Invert to obtain !CLASS.desiredAssertionStatus(). This may
15445 seem odd, but we do it to generate code identical to that of
15446 the JDK. */
15447 call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15448 TREE_SIDE_EFFECTS (call) = 1;
15449 DECL_INITIAL (field) = call;
15451 /* Record the initializer in the initializer statement list. */
15452 call = build2 (MODIFY_EXPR, NULL_TREE, field, call);
15453 TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15454 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15455 MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15457 CLASS_USES_ASSERTIONS (klass) = 1;
15460 if (value != NULL_TREE)
15461 value = tree_cons (NULL_TREE, value, NULL_TREE);
15463 node = build_wfl_node (get_identifier ("java"));
15464 node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15465 location);
15466 node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15467 location);
15469 node = build3 (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15470 TREE_SIDE_EFFECTS (node) = 1;
15471 /* It is too early to use BUILD_THROW. */
15472 node = build1 (THROW_EXPR, NULL_TREE, node);
15473 TREE_SIDE_EFFECTS (node) = 1;
15475 /* We invert the condition; if we just put NODE as the `else' part
15476 then we generate weird-looking bytecode. */
15477 condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15478 /* Check $assertionsDisabled. */
15479 condition
15480 = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15481 build1 (TRUTH_NOT_EXPR, NULL_TREE,
15482 build_wfl_node (get_identifier ("$assertionsDisabled"))),
15483 condition);
15484 node = build_if_else_statement (location, condition, node, NULL_TREE);
15485 return node;
15488 /* 14.18 The try/catch statements */
15490 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15491 catches TYPE and executes CATCH_STMTS. */
15493 static tree
15494 encapsulate_with_try_catch (int location, tree type_or_name, tree try_stmts,
15495 tree catch_stmts)
15497 tree try_block, catch_clause_param, catch_block, catch;
15499 /* First build a try block */
15500 try_block = build_expr_block (try_stmts, NULL_TREE);
15502 /* Build a catch block: we need a catch clause parameter */
15503 if (TREE_CODE (type_or_name) == EXPR_WITH_FILE_LOCATION)
15505 tree catch_type = obtain_incomplete_type (type_or_name);
15506 jdep *dep;
15507 catch_clause_param = build_decl (VAR_DECL, wpv_id, catch_type);
15508 register_incomplete_type (JDEP_VARIABLE, type_or_name,
15509 catch_clause_param, catch_type);
15510 dep = CLASSD_LAST (ctxp->classd_list);
15511 JDEP_GET_PATCH (dep) = &TREE_TYPE (catch_clause_param);
15513 else
15514 catch_clause_param = build_decl (VAR_DECL, wpv_id,
15515 build_pointer_type (type_or_name));
15517 /* And a block */
15518 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15520 /* Initialize the variable and store in the block */
15521 catch = build2 (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15522 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15523 add_stmt_to_block (catch_block, NULL_TREE, catch);
15525 /* Add the catch statements */
15526 add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15528 /* Now we can build a JAVA_CATCH_EXPR */
15529 catch_block = build1 (JAVA_CATCH_EXPR, NULL_TREE, catch_block);
15531 return build_try_statement (location, try_block, catch_block);
15534 static tree
15535 build_try_statement (int location, tree try_block, tree catches)
15537 tree node = build2 (TRY_EXPR, NULL_TREE, try_block, catches);
15538 EXPR_WFL_LINECOL (node) = location;
15539 return node;
15542 static tree
15543 build_try_finally_statement (int location, tree try_block, tree finally)
15545 tree node = build2 (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15546 EXPR_WFL_LINECOL (node) = location;
15547 return node;
15550 static tree
15551 patch_try_statement (tree node)
15553 int error_found = 0;
15554 tree try = TREE_OPERAND (node, 0);
15555 /* Exception handlers are considered in left to right order */
15556 tree catch = nreverse (TREE_OPERAND (node, 1));
15557 tree current, caught_type_list = NULL_TREE;
15559 /* Check catch clauses, if any. Every time we find an error, we try
15560 to process the next catch clause. We process the catch clause before
15561 the try block so that when processing the try block we can check thrown
15562 exceptions against the caught type list. */
15563 for (current = catch; current; current = TREE_CHAIN (current))
15565 tree carg_decl, carg_type;
15566 tree sub_current, catch_block, catch_clause;
15567 int unreachable;
15569 /* At this point, the structure of the catch clause is
15570 JAVA_CATCH_EXPR (catch node)
15571 BLOCK (with the decl of the parameter)
15572 COMPOUND_EXPR
15573 MODIFY_EXPR (assignment of the catch parameter)
15574 BLOCK (catch clause block)
15576 catch_clause = TREE_OPERAND (current, 0);
15577 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15578 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15580 /* Catch clauses can't have more than one parameter declared,
15581 but it's already enforced by the grammar. Make sure that the
15582 only parameter of the clause statement in of class Throwable
15583 or a subclass of Throwable, but that was done earlier. The
15584 catch clause parameter type has also been resolved. */
15586 /* Just make sure that the catch clause parameter type inherits
15587 from java.lang.Throwable */
15588 if (!inherits_from_p (carg_type, throwable_type_node))
15590 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15591 parse_error_context (wfl_operator,
15592 "Can't catch class %qs. Catch clause parameter type must be a subclass of class %<java.lang.Throwable%>",
15593 lang_printable_name (carg_type, 0));
15594 error_found = 1;
15595 continue;
15598 /* Partial check for unreachable catch statement: The catch
15599 clause is reachable iff is no earlier catch block A in
15600 the try statement such that the type of the catch
15601 clause's parameter is the same as or a subclass of the
15602 type of A's parameter */
15603 unreachable = 0;
15604 for (sub_current = catch;
15605 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15607 tree sub_catch_clause, decl;
15608 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15609 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15611 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15613 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15614 parse_error_context
15615 (wfl_operator,
15616 "%<catch%> not reached because of the catch clause at line %d",
15617 EXPR_WFL_LINENO (sub_current));
15618 unreachable = error_found = 1;
15619 break;
15622 /* Complete the catch clause block */
15623 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15624 if (catch_block == error_mark_node)
15626 error_found = 1;
15627 continue;
15629 if (CAN_COMPLETE_NORMALLY (catch_block))
15630 CAN_COMPLETE_NORMALLY (node) = 1;
15631 TREE_OPERAND (current, 0) = catch_block;
15633 if (unreachable)
15634 continue;
15636 /* Things to do here: the exception must be thrown */
15638 /* Link this type to the caught type list */
15639 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15642 PUSH_EXCEPTIONS (caught_type_list);
15643 if ((try = java_complete_tree (try)) == error_mark_node)
15644 error_found = 1;
15645 if (CAN_COMPLETE_NORMALLY (try))
15646 CAN_COMPLETE_NORMALLY (node) = 1;
15647 POP_EXCEPTIONS ();
15649 /* Verification ends here */
15650 if (error_found)
15651 return error_mark_node;
15653 TREE_OPERAND (node, 0) = try;
15654 TREE_OPERAND (node, 1) = catch;
15655 TREE_TYPE (node) = void_type_node;
15656 return node;
15659 /* 14.17 The synchronized Statement */
15661 static tree
15662 patch_synchronized_statement (tree node, tree wfl_op1)
15664 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15665 tree block = TREE_OPERAND (node, 1);
15667 tree tmp, enter, exit, expr_decl, assignment;
15669 if (expr == error_mark_node)
15671 block = java_complete_tree (block);
15672 return expr;
15675 /* We might be trying to synchronize on a STRING_CST */
15676 if ((tmp = patch_string (expr)))
15677 expr = tmp;
15679 /* The TYPE of expr must be a reference type */
15680 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15682 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15683 parse_error_context (wfl_operator, "Incompatible type for %<synchronized%>. Can't convert %qs to %<java.lang.Object%>",
15684 lang_printable_name (TREE_TYPE (expr), 0));
15685 return error_mark_node;
15688 /* Generate a try-finally for the synchronized statement, except
15689 that the handler that catches all throw exception calls
15690 _Jv_MonitorExit and then rethrow the exception.
15691 The synchronized statement is then implemented as:
15694 _Jv_MonitorEnter (expression)
15695 synchronized_block
15696 _Jv_MonitorExit (expression)
15698 CATCH_ALL
15700 e = _Jv_exception_info ();
15701 _Jv_MonitorExit (expression)
15702 Throw (e);
15703 } */
15705 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15706 BUILD_MONITOR_ENTER (enter, expr_decl);
15707 BUILD_MONITOR_EXIT (exit, expr_decl);
15708 CAN_COMPLETE_NORMALLY (enter) = 1;
15709 CAN_COMPLETE_NORMALLY (exit) = 1;
15710 assignment = build2 (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15711 TREE_SIDE_EFFECTS (assignment) = 1;
15712 node = build2 (COMPOUND_EXPR, NULL_TREE,
15713 build2 (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15714 build2 (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15715 node = build_expr_block (node, expr_decl);
15717 return java_complete_tree (node);
15720 /* 14.16 The throw Statement */
15722 static tree
15723 patch_throw_statement (tree node, tree wfl_op1)
15725 tree expr = TREE_OPERAND (node, 0);
15726 tree type = TREE_TYPE (expr);
15727 int unchecked_ok = 0, tryblock_throws_ok = 0;
15729 /* Thrown expression must be assignable to java.lang.Throwable */
15730 if (!try_reference_assignconv (throwable_type_node, expr))
15732 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15733 parse_error_context (wfl_operator,
15734 "Can't throw %qs; it must be a subclass of class %<java.lang.Throwable%>",
15735 lang_printable_name (type, 0));
15736 /* If the thrown expression was a reference, we further the
15737 compile-time check. */
15738 if (!JREFERENCE_TYPE_P (type))
15739 return error_mark_node;
15742 /* At least one of the following must be true */
15744 /* The type of the throw expression is a not checked exception,
15745 i.e. is a unchecked expression. */
15746 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15748 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15749 /* An instance can't throw a checked exception unless that exception
15750 is explicitly declared in the `throws' clause of each
15751 constructor. This doesn't apply to anonymous classes, since they
15752 don't have declared constructors. */
15753 if (!unchecked_ok
15754 && DECL_INSTINIT_P (current_function_decl)
15755 && !ANONYMOUS_CLASS_P (current_class))
15757 tree current;
15758 for (current = TYPE_METHODS (current_class); current;
15759 current = TREE_CHAIN (current))
15760 if (DECL_CONSTRUCTOR_P (current)
15761 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15763 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)",
15764 lang_printable_name (TREE_TYPE (expr), 0));
15765 return error_mark_node;
15769 /* Throw is contained in a try statement and at least one catch
15770 clause can receive the thrown expression or the current method is
15771 declared to throw such an exception. Or, the throw statement is
15772 contained in a method or constructor declaration and the type of
15773 the Expression is assignable to at least one type listed in the
15774 throws clause the declaration. */
15775 if (!unchecked_ok)
15776 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15777 if (!(unchecked_ok || tryblock_throws_ok))
15779 /* If there is a surrounding try block that has no matching
15780 clatch clause, report it first. A surrounding try block exits
15781 only if there is something after the list of checked
15782 exception thrown by the current function (if any). */
15783 if (IN_TRY_BLOCK_P ())
15784 parse_error_context (wfl_operator, "Checked exception %qs can't be caught by any of the catch clause(s) of the surrounding %<try%> block",
15785 lang_printable_name (type, 0));
15786 /* If we have no surrounding try statement and the method doesn't have
15787 any throws, report it now. FIXME */
15789 /* We report that the exception can't be throw from a try block
15790 in all circumstances but when the `throw' is inside a static
15791 block. */
15792 else if (!EXCEPTIONS_P (currently_caught_type_list)
15793 && !tryblock_throws_ok)
15795 if (DECL_CLINIT_P (current_function_decl))
15796 parse_error_context (wfl_operator,
15797 "Checked exception %qs can't be thrown in initializer",
15798 lang_printable_name (type, 0));
15799 else
15800 parse_error_context (wfl_operator,
15801 "Checked exception %qs isn't thrown from a %<try%> block",
15802 lang_printable_name (type, 0));
15804 /* Otherwise, the current method doesn't have the appropriate
15805 throws declaration */
15806 else
15807 parse_error_context (wfl_operator, "Checked exception %qs doesn't match any of current method's %<throws%> declaration(s)",
15808 lang_printable_name (type, 0));
15809 return error_mark_node;
15812 if (! flag_emit_class_files)
15813 BUILD_THROW (node, expr);
15815 return node;
15818 /* Check that exception said to be thrown by method DECL can be
15819 effectively caught from where DECL is invoked. THIS_EXPR is the
15820 expression that computes `this' for the method call. */
15821 static void
15822 check_thrown_exceptions (
15823 #ifdef USE_MAPPED_LOCATION
15824 source_location location,
15825 #else
15827 int location,
15828 #endif
15829 tree decl, tree this_expr)
15831 tree throws;
15832 int is_array_call = 0;
15834 /* Skip check within generated methods, such as access$<n>. */
15835 if (OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (current_function_decl)))
15836 return;
15838 if (this_expr != NULL_TREE
15839 && TREE_CODE (TREE_TYPE (this_expr)) == POINTER_TYPE
15840 && TYPE_ARRAY_P (TREE_TYPE (TREE_TYPE (this_expr))))
15841 is_array_call = 1;
15843 /* For all the unchecked exceptions thrown by DECL. */
15844 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15845 throws = TREE_CHAIN (throws))
15846 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15848 /* Suppress errors about cloning arrays. */
15849 if (is_array_call && DECL_NAME (decl) == get_identifier ("clone"))
15850 continue;
15852 #ifdef USE_MAPPED_LOCATION
15853 SET_EXPR_LOCATION (wfl_operator, location);
15854 #else
15855 EXPR_WFL_LINECOL (wfl_operator) = location;
15856 #endif
15857 if (DECL_FINIT_P (current_function_decl))
15858 parse_error_context
15859 (wfl_operator, "Exception %qs can't be thrown in initializer",
15860 lang_printable_name (TREE_VALUE (throws), 0));
15861 else
15863 parse_error_context
15864 (wfl_operator, "Exception %qs must be caught, or it must be declared in the %<throws%> clause of %qs",
15865 lang_printable_name (TREE_VALUE (throws), 0),
15866 (DECL_INIT_P (current_function_decl) ?
15867 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15868 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15873 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15874 try-catch blocks, OR is listed in the `throws' clause of the
15875 current method. */
15877 static int
15878 check_thrown_exceptions_do (tree exception)
15880 tree list = currently_caught_type_list;
15881 resolve_and_layout (exception, NULL_TREE);
15882 /* First, all the nested try-catch-finally at that stage. The
15883 last element contains `throws' clause exceptions, if any. */
15884 if (IS_UNCHECKED_EXCEPTION_P (exception))
15885 return 1;
15886 while (list)
15888 tree caught;
15889 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15890 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15891 return 1;
15892 list = TREE_CHAIN (list);
15894 return 0;
15897 static void
15898 purge_unchecked_exceptions (tree mdecl)
15900 tree throws = DECL_FUNCTION_THROWS (mdecl);
15901 tree new = NULL_TREE;
15903 while (throws)
15905 tree next = TREE_CHAIN (throws);
15906 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15908 TREE_CHAIN (throws) = new;
15909 new = throws;
15911 throws = next;
15913 /* List is inverted here, but it doesn't matter */
15914 DECL_FUNCTION_THROWS (mdecl) = new;
15917 /* This function goes over all of CLASS_TYPE ctors and checks whether
15918 each of them features at least one unchecked exception in its
15919 `throws' clause. If it's the case, it returns `true', `false'
15920 otherwise. */
15922 static bool
15923 ctors_unchecked_throws_clause_p (tree class_type)
15925 tree current;
15927 for (current = TYPE_METHODS (class_type); current;
15928 current = TREE_CHAIN (current))
15930 bool ctu = false; /* Ctor Throws Unchecked */
15931 if (DECL_CONSTRUCTOR_P (current))
15933 tree throws;
15934 for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
15935 throws = TREE_CHAIN (throws))
15936 if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
15937 ctu = true;
15939 /* We return false as we found one ctor that is unfit. */
15940 if (!ctu && DECL_CONSTRUCTOR_P (current))
15941 return false;
15943 /* All ctors feature at least one unchecked exception in their
15944 `throws' clause. */
15945 return true;
15948 /* 15.24 Conditional Operator ?: */
15950 static tree
15951 patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
15953 tree cond = TREE_OPERAND (node, 0);
15954 tree op1 = TREE_OPERAND (node, 1);
15955 tree op2 = TREE_OPERAND (node, 2);
15956 tree resulting_type = NULL_TREE;
15957 tree t1, t2, patched;
15958 int error_found = 0;
15960 /* The condition and operands of ?: might be StringBuffers crafted
15961 as a result of a string concatenation. Obtain decent ones here. */
15962 if ((patched = patch_string (cond)))
15963 TREE_OPERAND (node, 0) = cond = patched;
15964 if ((patched = patch_string (op1)))
15965 TREE_OPERAND (node, 1) = op1 = patched;
15966 if ((patched = patch_string (op2)))
15967 TREE_OPERAND (node, 2) = op2 = patched;
15969 t1 = TREE_TYPE (op1);
15970 t2 = TREE_TYPE (op2);
15972 /* The first expression must be a boolean */
15973 if (TREE_TYPE (cond) != boolean_type_node)
15975 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15976 parse_error_context (wfl_operator,
15977 "Incompatible type for %<?:%>. Can't convert %qs to %<boolean%>",
15978 lang_printable_name (TREE_TYPE (cond), 0));
15979 error_found = 1;
15982 /* Second and third can be numeric, boolean (i.e. primitive),
15983 references or null. Anything else results in an error */
15984 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15985 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15986 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15987 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15988 error_found = 1;
15990 /* Determine the type of the conditional expression. Same types are
15991 easy to deal with */
15992 else if (t1 == t2)
15993 resulting_type = t1;
15995 /* There are different rules for numeric types */
15996 else if (JNUMERIC_TYPE_P (t1))
15998 /* if byte/short found, the resulting type is short */
15999 if ((t1 == byte_type_node && t2 == short_type_node)
16000 || (t1 == short_type_node && t2 == byte_type_node))
16001 resulting_type = short_type_node;
16003 /* If t1 is a constant int and t2 is of type byte, short or char
16004 and t1's value fits in t2, then the resulting type is t2 */
16005 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
16006 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
16007 resulting_type = t2;
16009 /* If t2 is a constant int and t1 is of type byte, short or char
16010 and t2's value fits in t1, then the resulting type is t1 */
16011 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
16012 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
16013 resulting_type = t1;
16015 /* Otherwise, binary numeric promotion is applied and the
16016 resulting type is the promoted type of operand 1 and 2 */
16017 else
16018 resulting_type = binary_numeric_promotion (t1, t2,
16019 &TREE_OPERAND (node, 1),
16020 &TREE_OPERAND (node, 2));
16023 /* Cases of a reference and a null type */
16024 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
16025 resulting_type = t1;
16027 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
16028 resulting_type = t2;
16030 /* Last case: different reference types. If a type can be converted
16031 into the other one by assignment conversion, the latter
16032 determines the type of the expression */
16033 else if ((resulting_type = try_reference_assignconv (t1, op2)))
16034 resulting_type = promote_type (t1);
16036 else if ((resulting_type = try_reference_assignconv (t2, op1)))
16037 resulting_type = promote_type (t2);
16039 /* If we don't have any resulting type, we're in trouble */
16040 if (!resulting_type)
16042 char *t = xstrdup (lang_printable_name (t1, 0));
16043 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16044 parse_error_context (wfl_operator,
16045 "Incompatible type for %<?:%>. Can't convert %qs to %qs",
16046 t, lang_printable_name (t2, 0));
16047 free (t);
16048 error_found = 1;
16051 if (error_found)
16053 TREE_TYPE (node) = error_mark_node;
16054 return error_mark_node;
16057 TREE_TYPE (node) = resulting_type;
16058 TREE_SET_CODE (node, COND_EXPR);
16059 CAN_COMPLETE_NORMALLY (node) = 1;
16060 return node;
16063 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16065 static tree
16066 maybe_build_class_init_for_field (tree decl, tree expr)
16068 tree clas = DECL_CONTEXT (decl);
16069 if (flag_emit_class_files)
16070 return expr;
16072 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16073 && FIELD_FINAL (decl))
16075 tree init = DECL_INITIAL (decl);
16076 if (init != NULL_TREE)
16077 init = fold_constant_for_init (init, decl);
16078 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16079 return expr;
16082 return build_class_init (clas, expr);
16085 /* Try to constant fold NODE.
16086 If NODE is not a constant expression, return NULL_EXPR.
16087 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16089 static tree
16090 fold_constant_for_init (tree node, tree context)
16092 tree op0, op1, val;
16093 enum tree_code code = TREE_CODE (node);
16095 switch (code)
16097 case INTEGER_CST:
16098 if (node == null_pointer_node)
16099 return NULL_TREE;
16100 case STRING_CST:
16101 case REAL_CST:
16102 return node;
16104 case PLUS_EXPR:
16105 case MINUS_EXPR:
16106 case MULT_EXPR:
16107 case TRUNC_MOD_EXPR:
16108 case RDIV_EXPR:
16109 case LSHIFT_EXPR:
16110 case RSHIFT_EXPR:
16111 case URSHIFT_EXPR:
16112 case BIT_AND_EXPR:
16113 case BIT_XOR_EXPR:
16114 case BIT_IOR_EXPR:
16115 case TRUTH_ANDIF_EXPR:
16116 case TRUTH_ORIF_EXPR:
16117 case EQ_EXPR:
16118 case NE_EXPR:
16119 case GT_EXPR:
16120 case GE_EXPR:
16121 case LT_EXPR:
16122 case LE_EXPR:
16123 op0 = TREE_OPERAND (node, 0);
16124 op1 = TREE_OPERAND (node, 1);
16125 val = fold_constant_for_init (op0, context);
16126 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16127 return NULL_TREE;
16128 TREE_OPERAND (node, 0) = val;
16129 val = fold_constant_for_init (op1, context);
16130 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16131 return NULL_TREE;
16132 TREE_OPERAND (node, 1) = val;
16133 return patch_binop (node, op0, op1);
16135 case UNARY_PLUS_EXPR:
16136 case NEGATE_EXPR:
16137 case TRUTH_NOT_EXPR:
16138 case BIT_NOT_EXPR:
16139 case CONVERT_EXPR:
16140 op0 = TREE_OPERAND (node, 0);
16141 val = fold_constant_for_init (op0, context);
16142 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16143 return NULL_TREE;
16144 TREE_OPERAND (node, 0) = val;
16145 val = patch_unaryop (node, op0);
16146 if (! TREE_CONSTANT (val))
16147 return NULL_TREE;
16148 return val;
16150 break;
16152 case COND_EXPR:
16153 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16154 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16155 return NULL_TREE;
16156 TREE_OPERAND (node, 0) = val;
16157 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16158 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16159 return NULL_TREE;
16160 TREE_OPERAND (node, 1) = val;
16161 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16162 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16163 return NULL_TREE;
16164 TREE_OPERAND (node, 2) = val;
16165 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
16166 : TREE_OPERAND (node, 2);
16168 case VAR_DECL:
16169 case FIELD_DECL:
16170 if (! FIELD_FINAL (node)
16171 || DECL_INITIAL (node) == NULL_TREE)
16172 return NULL_TREE;
16173 val = DECL_INITIAL (node);
16174 /* Guard against infinite recursion. */
16175 DECL_INITIAL (node) = NULL_TREE;
16176 val = fold_constant_for_init (val, node);
16177 if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16178 val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16179 DECL_INITIAL (node) = val;
16180 return val;
16182 case EXPR_WITH_FILE_LOCATION:
16183 /* Compare java_complete_tree and resolve_expression_name. */
16184 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16185 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16187 tree name = EXPR_WFL_NODE (node);
16188 tree decl;
16189 if (PRIMARY_P (node))
16190 return NULL_TREE;
16191 else if (! QUALIFIED_P (name))
16193 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16194 if (decl == NULL_TREE
16195 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16196 return NULL_TREE;
16197 return fold_constant_for_init (decl, decl);
16199 else
16201 tree r = NULL_TREE;
16202 /* Install the proper context for the field resolution. */
16203 tree saved_current_class = current_class;
16204 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
16205 current_class = DECL_CONTEXT (context);
16206 qualify_ambiguous_name (node);
16207 r = resolve_field_access (node, &decl, NULL);
16208 /* Restore prior context. */
16209 current_class = saved_current_class;
16210 if (r != error_mark_node && decl != NULL_TREE)
16211 return fold_constant_for_init (decl, decl);
16212 return NULL_TREE;
16215 else
16217 op0 = TREE_OPERAND (node, 0);
16218 val = fold_constant_for_init (op0, context);
16219 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16220 return NULL_TREE;
16221 TREE_OPERAND (node, 0) = val;
16222 return val;
16225 #ifdef USE_COMPONENT_REF
16226 case IDENTIFIER:
16227 case COMPONENT_REF:
16229 #endif
16231 default:
16232 return NULL_TREE;
16236 #ifdef USE_COMPONENT_REF
16237 /* Context is 'T' for TypeName, 'P' for PackageName,
16238 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16240 tree
16241 resolve_simple_name (tree name, int context)
16245 tree
16246 resolve_qualified_name (tree name, int context)
16249 #endif
16251 void
16252 init_src_parse (void)
16254 /* Sanity check; we've been bit by this before. */
16255 if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16256 abort ();
16261 /* This section deals with the functions that are called when tables
16262 recording class initialization information are traversed. */
16264 /* This function is called for each class that is known definitely
16265 initialized when a given static method was called. This function
16266 augments a compound expression (INFO) storing all assignment to
16267 initialized static class flags if a flag already existed, otherwise
16268 a new one is created. */
16270 static int
16271 emit_test_initialization (void **entry_p, void *info)
16273 tree l = (tree) info;
16274 tree decl, init;
16275 tree key = (tree) *entry_p;
16276 tree *ite;
16277 htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16279 /* If we haven't found a flag and we're dealing with self registered
16280 with current_function_decl, then don't do anything. Self is
16281 always added as definitely initialized but this information is
16282 valid only if used outside the current function. */
16283 if (current_function_decl == TREE_PURPOSE (l)
16284 && java_treetreehash_find (cf_ht, key) == NULL)
16285 return true;
16287 ite = java_treetreehash_new (cf_ht, key);
16289 /* If we don't have a variable, create one and install it. */
16290 if (*ite == NULL)
16292 tree block;
16294 decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16295 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16296 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16297 DECL_CONTEXT (decl) = current_function_decl;
16298 DECL_INITIAL (decl) = boolean_true_node;
16299 /* Don't emit any symbolic debugging info for this decl. */
16300 DECL_IGNORED_P (decl) = 1;
16302 /* The trick is to find the right context for it. */
16303 block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16304 TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16305 BLOCK_EXPR_DECLS (block) = decl;
16306 *ite = decl;
16308 else
16309 decl = *ite;
16311 /* Now simply augment the compound that holds all the assignments
16312 pertaining to this method invocation. */
16313 init = build2 (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16314 TREE_SIDE_EFFECTS (init) = 1;
16315 TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16316 TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16318 return true;
16321 #ifdef __XGETTEXT__
16322 /* Depending on the version of Bison used to compile this grammar,
16323 it may issue generic diagnostics spelled "syntax error" or
16324 "parse error". To prevent this from changing the translation
16325 template randomly, we list all the variants of this particular
16326 diagnostic here. Translators: there is no fine distinction
16327 between diagnostics with "syntax error" in them, and diagnostics
16328 with "parse error" in them. It's okay to give them both the same
16329 translation. */
16330 const char d1[] = N_("syntax error");
16331 const char d2[] = N_("parse error");
16332 const char d3[] = N_("syntax error; also virtual memory exhausted");
16333 const char d4[] = N_("parse error; also virtual memory exhausted");
16334 const char d5[] = N_("syntax error: cannot back up");
16335 const char d6[] = N_("parse error: cannot back up");
16336 #endif
16338 #include "gt-java-parse.h"
16339 #include "gtype-java.h"