* class.c (build_class_ref): Wrap the primary class type in a
[official-gcc.git] / gcc / java / parse.y
blob82c156e751751e35067903e7433a8cfe9901277e
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, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, 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 "tree-dump.h"
76 #include "cgraph.h"
77 #include "target.h"
79 /* Local function prototypes */
80 static char *java_accstring_lookup (int);
81 static const char *accessibility_string (int);
82 static void classitf_redefinition_error (const char *,tree, tree, tree);
83 static void variable_redefinition_error (tree, tree, tree, int);
84 static tree create_class (int, tree, tree, tree);
85 static tree create_interface (int, tree, tree);
86 static void end_class_declaration (int);
87 static tree find_field (tree, tree);
88 static tree lookup_field_wrapper (tree, tree);
89 static int duplicate_declaration_error_p (tree, tree, tree);
90 static void register_fields (int, tree, tree);
91 static tree parser_qualified_classname (tree);
92 static int parser_check_super (tree, tree, tree);
93 static int parser_check_super_interface (tree, tree, tree);
94 static void check_modifiers_consistency (int);
95 static tree lookup_cl (tree);
96 static tree lookup_java_method2 (tree, tree, int);
97 static tree method_header (int, tree, tree, tree);
98 static void fix_method_argument_names (tree ,tree);
99 static tree method_declarator (tree, tree);
100 static void parse_warning_context (tree cl, const char *gmsgid, ...) ATTRIBUTE_GCC_DIAG(2,3);
101 #ifdef USE_MAPPED_LOCATION
102 static void issue_warning_error_from_context
103 (source_location, const char *gmsgid, va_list *);
104 #else
105 static void issue_warning_error_from_context
106 (tree, const char *gmsgid, va_list *);
107 #endif
108 static void parse_ctor_invocation_error (void);
109 static tree parse_jdk1_1_error (const char *);
110 static void complete_class_report_errors (jdep *);
111 static int process_imports (void);
112 static void read_import_dir (tree);
113 static int find_in_imports_on_demand (tree, tree);
114 static void find_in_imports (tree, tree);
115 static bool inner_class_accessible (tree, tree);
116 static void check_inner_class_access (tree, tree, tree);
117 static int check_pkg_class_access (tree, tree, bool, tree);
118 static tree resolve_package (tree, tree *, tree *);
119 static tree resolve_class (tree, tree, tree, tree);
120 static void declare_local_variables (int, tree, tree);
121 static void dump_java_tree (enum tree_dump_index, tree);
122 static void source_start_java_method (tree);
123 static void source_end_java_method (void);
124 static tree find_name_in_single_imports (tree);
125 static void check_abstract_method_header (tree);
126 static tree lookup_java_interface_method2 (tree, tree);
127 static tree resolve_expression_name (tree, tree *);
128 static tree maybe_create_class_interface_decl (tree, tree, tree, tree);
129 static int check_class_interface_creation (int, int, tree, tree, tree, tree);
130 static tree patch_method_invocation (tree, tree, tree, int, int *, tree *);
131 static tree resolve_and_layout (tree, tree);
132 static tree qualify_and_find (tree, tree, tree);
133 static tree resolve_no_layout (tree, tree);
134 static int invocation_mode (tree, int);
135 static tree find_applicable_accessible_methods_list (int, tree, tree, tree);
136 static void search_applicable_methods_list (int, tree, tree, tree, tree *, tree *);
137 static tree find_most_specific_methods_list (tree);
138 static int argument_types_convertible (tree, tree);
139 static tree patch_invoke (tree, tree, tree);
140 static int maybe_use_access_method (int, tree *, tree *);
141 static tree lookup_method_invoke (int, tree, tree, tree, tree);
142 static tree register_incomplete_type (int, tree, tree, tree);
143 static tree check_inner_circular_reference (tree, tree);
144 static tree check_circular_reference (tree);
145 static tree obtain_incomplete_type (tree);
146 static tree java_complete_lhs (tree);
147 static tree java_complete_tree (tree);
148 static tree maybe_generate_pre_expand_clinit (tree);
149 static int analyze_clinit_body (tree, tree);
150 static int maybe_yank_clinit (tree);
151 static void start_complete_expand_method (tree);
152 static void java_complete_expand_method (tree);
153 static void java_expand_method_bodies (tree);
154 static int unresolved_type_p (tree, tree *);
155 static void create_jdep_list (struct parser_ctxt *);
156 static tree build_expr_block (tree, tree);
157 static tree enter_block (void);
158 static tree exit_block (void);
159 static tree lookup_name_in_blocks (tree);
160 static void maybe_absorb_scoping_blocks (void);
161 static tree build_method_invocation (tree, tree);
162 static tree build_new_invocation (tree, tree);
163 static tree build_assignment (int, int, tree, tree);
164 static tree build_binop (enum tree_code, int, tree, tree);
165 static tree patch_assignment (tree, tree);
166 static tree patch_binop (tree, tree, tree, int);
167 static tree build_unaryop (int, int, tree);
168 static tree build_incdec (int, int, tree, int);
169 static tree patch_unaryop (tree, tree);
170 static tree build_cast (int, tree, tree);
171 static tree build_null_of_type (tree);
172 static tree patch_cast (tree, tree);
173 static int valid_ref_assignconv_cast_p (tree, tree, int);
174 static int valid_builtin_assignconv_identity_widening_p (tree, tree);
175 static int valid_cast_to_p (tree, tree);
176 static int valid_method_invocation_conversion_p (tree, tree);
177 static tree try_builtin_assignconv (tree, tree, tree);
178 static tree try_reference_assignconv (tree, tree);
179 static tree build_unresolved_array_type (tree);
180 static int build_type_name_from_array_name (tree, tree *);
181 static tree build_array_from_name (tree, tree, tree, tree *);
182 static tree build_array_ref (int, tree, tree);
183 static tree patch_array_ref (tree);
184 #ifdef USE_MAPPED_LOCATION
185 static tree make_qualified_name (tree, tree, source_location);
186 #else
187 static tree make_qualified_name (tree, tree, int);
188 #endif
189 static tree merge_qualified_name (tree, tree);
190 static tree make_qualified_primary (tree, tree, int);
191 static int resolve_qualified_expression_name (tree, tree *, tree *, tree *);
192 static void qualify_ambiguous_name (tree);
193 static tree resolve_field_access (tree, tree *, tree *);
194 static tree build_newarray_node (tree, tree, int);
195 static tree patch_newarray (tree);
196 static tree resolve_type_during_patch (tree);
197 static tree build_this (int);
198 static tree build_wfl_wrap (tree, int);
199 static tree build_return (int, tree);
200 static tree patch_return (tree);
201 static tree maybe_access_field (tree, tree, tree);
202 static int complete_function_arguments (tree);
203 static int check_for_static_method_reference (tree, tree, tree, tree, tree);
204 static int not_accessible_p (tree, tree, tree, int);
205 static void check_deprecation (tree, tree);
206 static int class_in_current_package (tree);
207 static tree build_if_else_statement (int, tree, tree, tree);
208 static tree patch_if_else_statement (tree);
209 static tree add_stmt_to_block (tree, tree, tree);
210 static tree patch_exit_expr (tree);
211 static tree build_labeled_block (int, tree);
212 static tree finish_labeled_statement (tree, tree);
213 static tree build_bc_statement (int, int, tree);
214 static tree patch_bc_statement (tree);
215 static tree patch_loop_statement (tree);
216 static tree build_new_loop (tree);
217 static tree build_loop_body (int, tree, int);
218 static tree finish_loop_body (int, tree, tree, int);
219 static tree build_debugable_stmt (int, tree);
220 static tree finish_for_loop (int, tree, tree, tree);
221 static tree patch_switch_statement (tree);
222 static tree string_constant_concatenation (tree, tree);
223 static tree build_string_concatenation (tree, tree);
224 static tree patch_string_cst (tree);
225 static tree patch_string (tree);
226 static tree encapsulate_with_try_catch (int, tree, tree, tree);
227 #ifdef USE_MAPPED_LOCATION
228 static tree build_assertion (source_location, tree, tree);
229 #else
230 static tree build_assertion (int, tree, tree);
231 #endif
232 static tree build_try_statement (int, tree, tree);
233 static tree build_try_finally_statement (int, tree, tree);
234 static tree patch_try_statement (tree);
235 static tree patch_synchronized_statement (tree, tree);
236 static tree patch_throw_statement (tree, tree);
237 static void add_exception_to_throws (tree, tree);
238 #ifdef USE_MAPPED_LOCATION
239 static void check_thrown_exceptions (source_location, tree, tree);
240 #else
241 static void check_thrown_exceptions (int, tree, tree);
242 #endif
243 static int check_thrown_exceptions_do (tree);
244 static bool ctors_unchecked_throws_clause_p (tree);
245 static void check_concrete_throws_clauses (tree, tree, tree, tree);
246 static void check_throws_clauses (tree, tree, tree);
247 static void finish_method_declaration (tree);
248 static tree build_super_invocation (tree);
249 static int verify_constructor_circularity (tree, tree);
250 static char *constructor_circularity_msg (tree, tree);
251 static tree build_this_super_qualified_invocation (int, tree, tree, int, int);
252 static const char *get_printable_method_name (tree);
253 static tree patch_conditional_expr (tree, tree, tree);
254 static tree generate_finit (tree);
255 static tree generate_instinit (tree);
256 static tree build_instinit_invocation (tree);
257 static void fix_constructors (tree);
258 static tree build_alias_initializer_parameter_list (int, tree, tree, int *);
259 static tree craft_constructor (tree, tree);
260 static tree get_constructor_super (tree);
261 static tree create_artificial_method (tree, int, tree, tree, tree);
262 static void start_artificial_method_body (tree);
263 static void end_artificial_method_body (tree);
264 static int check_method_redefinition (tree, tree);
265 static int check_method_types_complete (tree);
266 static bool hack_is_accessible_p (tree, tree);
267 static void java_check_regular_methods (tree);
268 static void check_interface_throws_clauses (tree, tree);
269 static void java_check_abstract_methods (tree);
270 static void unreachable_stmt_error (tree);
271 static int not_accessible_field_error (tree, tree);
272 static tree find_expr_with_wfl (tree);
273 static void missing_return_error (tree);
274 static tree build_new_array_init (int, tree);
275 static tree patch_new_array_init (tree, tree);
276 static tree maybe_build_array_element_wfl (tree);
277 static int array_constructor_check_entry (tree, constructor_elt *);
278 static const char *purify_type_name (const char *);
279 static tree fold_constant_for_init (tree, tree);
280 static jdeplist *reverse_jdep_list (struct parser_ctxt *);
281 static void static_ref_err (tree, tree, tree);
282 static void parser_add_interface (tree, tree, tree);
283 static void add_superinterfaces (tree, tree);
284 static tree jdep_resolve_class (jdep *);
285 static int note_possible_classname (const char *, int);
286 static void java_complete_expand_classes (void);
287 static void java_complete_expand_class (tree);
288 static void java_complete_expand_methods (tree);
289 static tree cut_identifier_in_qualified (tree);
290 static tree java_stabilize_reference (tree);
291 static tree do_unary_numeric_promotion (tree);
292 static char * operator_string (tree);
293 static tree do_merge_string_cste (tree, const char *, int, int);
294 static tree merge_string_cste (tree, tree, int);
295 static tree java_refold (tree);
296 static int java_decl_equiv (tree, tree);
297 static int binop_compound_p (enum tree_code);
298 static tree search_loop (tree);
299 static int labeled_block_contains_loop_p (tree, tree);
300 static int check_abstract_method_definitions (int, tree, tree);
301 static void java_check_abstract_method_definitions (tree);
302 static void java_debug_context_do (int);
303 static void java_parser_context_push_initialized_field (void);
304 static void java_parser_context_pop_initialized_field (void);
305 static tree reorder_static_initialized (tree);
306 static void java_parser_context_suspend (void);
307 static void java_parser_context_resume (void);
308 static int pop_current_osb (struct parser_ctxt *);
310 /* JDK 1.1 work. FIXME */
312 static tree maybe_make_nested_class_name (tree);
313 static int make_nested_class_name (tree);
314 static void link_nested_class_to_enclosing (void);
315 static tree resolve_inner_class (tree, tree, tree, tree);
316 static tree find_as_inner_class (tree, tree, tree);
317 static tree find_as_inner_class_do (tree, tree);
318 static int check_inner_class_redefinition (tree, tree);
320 static tree build_thisn_assign (void);
321 static tree build_current_thisn (tree);
322 static tree build_access_to_thisn (tree, tree, int);
323 static tree maybe_build_thisn_access_method (tree);
325 static tree build_nested_field_access (tree, tree);
326 static tree build_nested_field_access_methods (tree);
327 static tree build_nested_field_access_method (tree, tree, tree, tree, tree);
328 static tree build_nested_field_access_expr (int, tree, tree, tree, tree);
329 static tree build_outer_method_access_method (tree);
330 static tree build_new_access_id (void);
332 static int nested_field_access_p (tree, tree);
333 static int nested_field_expanded_access_p (tree, tree *, tree *, tree *);
334 static tree nested_field_access_fix (tree, tree, tree);
336 static tree build_incomplete_class_ref (int, tree);
337 static tree patch_incomplete_class_ref (tree);
338 static tree create_anonymous_class (tree);
339 static void patch_anonymous_class (tree, tree, tree);
340 static void add_inner_class_fields (tree, tree);
342 static tree build_dot_class_method (tree);
343 static tree build_dot_class_method_invocation (tree, tree);
344 static void create_new_parser_context (int);
345 static tree maybe_build_class_init_for_field (tree, tree);
347 static int emit_test_initialization (void **, void *);
349 static char *string_convert_int_cst (tree);
351 /* Number of error found so far. */
352 int java_error_count;
353 /* Number of warning found so far. */
354 int java_warning_count;
355 /* Cyclic inheritance report, as it can be set by layout_class */
356 const char *cyclic_inheritance_report;
358 /* The current parser context */
359 struct parser_ctxt *ctxp;
361 /* List of things that were analyzed for which code will be generated */
362 struct parser_ctxt *ctxp_for_generation = NULL;
363 struct parser_ctxt *ctxp_for_generation_last = 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 /* Hold THIS for the scope of the current method decl. */
411 static GTY(()) tree current_this;
413 /* Hold a list of catch clauses list. The first element of this list is
414 the list of the catch clauses of the currently analyzed try block. */
415 static GTY(()) tree currently_caught_type_list;
417 /* This holds a linked list of all the case labels for the current
418 switch statement. It is only used when checking to see if there
419 are duplicate labels. FIXME: probably this should just be attached
420 to the switch itself; then it could be referenced via
421 `ctxp->current_loop'. */
422 static GTY(()) tree case_label_list;
424 /* Anonymous class counter. Will be reset to 1 every time a non
425 anonymous class gets created. */
426 static int anonymous_class_counter = 1;
428 static GTY(()) tree src_parse_roots[1];
430 /* All classes seen from source code */
431 #define gclass_list src_parse_roots[0]
433 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
434 line and point it out. */
435 /* Should point out the one that don't fit. ASCII/unicode, going
436 backward. FIXME */
438 #define check_modifiers(__message, __value, __mask) do { \
439 if ((__value) & ~(__mask)) \
441 size_t i, remainder = (__value) & ~(__mask); \
442 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++) \
443 if ((1 << i) & remainder) \
444 parse_error_context (ctxp->modifier_ctx [i], (__message), \
445 java_accstring_lookup (1 << i)); \
447 } while (0)
451 %union {
452 tree node;
453 int sub_token;
454 struct {
455 int token;
456 #ifdef USE_MAPPED_LOCATION
457 source_location location;
458 #else
459 int location;
460 #endif
461 } operator;
462 int value;
466 #ifdef USE_MAPPED_LOCATION
467 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
468 SET_EXPR_LOCATION(EXPR, (TOKEN).location)
469 #else
470 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
471 (EXPR_WFL_LINECOL (EXPR) = (TOKEN).location)
472 #endif
474 #include "lex.c"
477 %pure_parser
479 /* Things defined here have to match the order of what's in the
480 binop_lookup table. */
482 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
483 %token LS_TK SRS_TK ZRS_TK
484 %token AND_TK XOR_TK OR_TK
485 %token BOOL_AND_TK BOOL_OR_TK
486 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
488 /* This maps to the same binop_lookup entry than the token above */
490 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
491 %token REM_ASSIGN_TK
492 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
493 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
496 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
498 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
499 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
500 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
501 %token PAD_TK ABSTRACT_TK STRICT_TK
502 %token MODIFIER_TK
504 /* Keep those two in order, too */
505 %token DECR_TK INCR_TK
507 /* From now one, things can be in any order */
509 %token DEFAULT_TK IF_TK THROW_TK
510 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
511 %token THROWS_TK BREAK_TK IMPORT_TK
512 %token ELSE_TK INSTANCEOF_TK RETURN_TK
513 %token VOID_TK CATCH_TK INTERFACE_TK
514 %token CASE_TK EXTENDS_TK FINALLY_TK
515 %token SUPER_TK WHILE_TK CLASS_TK
516 %token SWITCH_TK CONST_TK TRY_TK
517 %token FOR_TK NEW_TK CONTINUE_TK
518 %token GOTO_TK PACKAGE_TK THIS_TK
519 %token ASSERT_TK
521 %token BYTE_TK SHORT_TK INT_TK LONG_TK
522 %token CHAR_TK INTEGRAL_TK
524 %token FLOAT_TK DOUBLE_TK FP_TK
526 %token ID_TK
528 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
530 %token ASSIGN_ANY_TK ASSIGN_TK
531 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
533 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
534 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
536 %type <value> modifiers MODIFIER_TK final synchronized
538 %type <node> super ID_TK identifier
539 %type <node> name simple_name qualified_name
540 %type <node> type_declaration compilation_unit
541 field_declaration method_declaration extends_interfaces
542 interfaces interface_type_list
543 import_declarations package_declaration
544 type_declarations interface_body
545 interface_member_declaration constant_declaration
546 interface_member_declarations interface_type
547 abstract_method_declaration
548 %type <node> class_body_declaration class_member_declaration
549 static_initializer constructor_declaration block
550 %type <node> class_body_declarations constructor_header
551 %type <node> class_or_interface_type class_type class_type_list
552 constructor_declarator explicit_constructor_invocation
553 %type <node> dim_expr dim_exprs this_or_super throws
555 %type <node> variable_declarator_id variable_declarator
556 variable_declarators variable_initializer
557 variable_initializers constructor_body
558 array_initializer
560 %type <node> class_body block_end constructor_block_end
561 %type <node> statement statement_without_trailing_substatement
562 labeled_statement if_then_statement label_decl
563 if_then_else_statement while_statement for_statement
564 statement_nsi labeled_statement_nsi do_statement
565 if_then_else_statement_nsi while_statement_nsi
566 for_statement_nsi statement_expression_list for_init
567 for_update statement_expression expression_statement
568 primary_no_new_array expression primary array_type
569 array_creation_initialized array_creation_uninitialized
570 class_instance_creation_expression field_access
571 method_invocation array_access something_dot_new
572 argument_list postfix_expression while_expression
573 post_increment_expression post_decrement_expression
574 unary_expression_not_plus_minus unary_expression
575 pre_increment_expression pre_decrement_expression
576 cast_expression
577 multiplicative_expression additive_expression
578 shift_expression relational_expression
579 equality_expression and_expression
580 exclusive_or_expression inclusive_or_expression
581 conditional_and_expression conditional_or_expression
582 conditional_expression assignment_expression
583 left_hand_side assignment for_header for_begin
584 constant_expression do_statement_begin empty_statement
585 switch_statement synchronized_statement throw_statement
586 try_statement assert_statement
587 switch_expression switch_block
588 catches catch_clause catch_clause_parameter finally
589 anonymous_class_creation trap_overflow_corner_case
590 %type <node> return_statement break_statement continue_statement
592 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
593 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
594 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
595 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
596 %type <operator> ASSIGN_ANY_TK assignment_operator
597 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
598 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
599 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
600 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
601 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
602 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
603 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
604 %type <operator> NEW_TK ASSERT_TK
606 %type <node> method_body
608 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
609 STRING_LIT_TK NULL_TK VOID_TK
611 %type <node> IF_TK WHILE_TK FOR_TK
613 %type <node> formal_parameter_list formal_parameter
614 method_declarator method_header
616 %type <node> primitive_type reference_type type
617 BOOLEAN_TK INTEGRAL_TK FP_TK
619 /* Added or modified JDK 1.1 rule types */
620 %type <node> type_literals
623 /* 19.2 Production from 2.3: The Syntactic Grammar */
624 goal: compilation_unit
628 /* 19.3 Productions from 3: Lexical structure */
629 literal:
630 INT_LIT_TK
631 | FP_LIT_TK
632 | BOOL_LIT_TK
633 | CHAR_LIT_TK
634 | STRING_LIT_TK
635 | NULL_TK
638 /* 19.4 Productions from 4: Types, Values and Variables */
639 type:
640 primitive_type
641 | reference_type
644 primitive_type:
645 INTEGRAL_TK
646 | FP_TK
647 | BOOLEAN_TK
650 reference_type:
651 class_or_interface_type
652 | array_type
655 class_or_interface_type:
656 name
659 class_type:
660 class_or_interface_type /* Default rule */
663 interface_type:
664 class_or_interface_type
667 array_type:
668 primitive_type dims
670 int osb = pop_current_osb (ctxp);
671 tree t = build_java_array_type (($1), -1);
672 while (--osb)
673 t = build_unresolved_array_type (t);
674 $$ = t;
676 | name dims
678 int osb = pop_current_osb (ctxp);
679 tree t = $1;
680 while (osb--)
681 t = build_unresolved_array_type (t);
682 $$ = t;
686 /* 19.5 Productions from 6: Names */
687 name:
688 simple_name /* Default rule */
689 | qualified_name /* Default rule */
692 simple_name:
693 identifier /* Default rule */
696 qualified_name:
697 name DOT_TK identifier
698 { $$ = make_qualified_name ($1, $3, $2.location); }
701 identifier:
702 ID_TK
705 /* 19.6: Production from 7: Packages */
706 compilation_unit:
707 {$$ = NULL;}
708 | package_declaration
709 | import_declarations
710 | type_declarations
711 | package_declaration import_declarations
712 | package_declaration type_declarations
713 | import_declarations type_declarations
714 | package_declaration import_declarations type_declarations
717 import_declarations:
718 import_declaration
720 $$ = NULL;
722 | import_declarations import_declaration
724 $$ = NULL;
728 type_declarations:
729 type_declaration
730 | type_declarations type_declaration
733 package_declaration:
734 PACKAGE_TK name SC_TK
736 ctxp->package = EXPR_WFL_NODE ($2);
738 | PACKAGE_TK error
739 {yyerror ("Missing name"); RECOVER;}
740 | PACKAGE_TK name error
741 {yyerror ("';' expected"); RECOVER;}
744 import_declaration:
745 single_type_import_declaration
746 | type_import_on_demand_declaration
749 single_type_import_declaration:
750 IMPORT_TK name SC_TK
752 tree name = EXPR_WFL_NODE ($2), last_name;
753 int i = IDENTIFIER_LENGTH (name)-1;
754 const char *last = &IDENTIFIER_POINTER (name)[i];
755 while (last != IDENTIFIER_POINTER (name))
757 if (last [0] == '.')
758 break;
759 last--;
761 last_name = get_identifier (++last);
762 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
764 tree err = find_name_in_single_imports (last_name);
765 if (err && err != name)
766 parse_error_context
767 ($2, "Ambiguous class: %qs and %qs",
768 IDENTIFIER_POINTER (name),
769 IDENTIFIER_POINTER (err));
770 else
771 REGISTER_IMPORT ($2, last_name);
773 else
774 REGISTER_IMPORT ($2, last_name);
776 | IMPORT_TK error
777 {yyerror ("Missing name"); RECOVER;}
778 | IMPORT_TK name error
779 {yyerror ("';' expected"); RECOVER;}
782 type_import_on_demand_declaration:
783 IMPORT_TK name DOT_TK MULT_TK SC_TK
785 tree name = EXPR_WFL_NODE ($2);
786 tree it;
787 /* Search for duplicates. */
788 for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
789 if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
790 break;
791 /* Don't import the same thing more than once, just ignore
792 duplicates (7.5.2) */
793 if (! it)
795 read_import_dir ($2);
796 ctxp->import_demand_list =
797 chainon (ctxp->import_demand_list,
798 build_tree_list ($2, NULL_TREE));
801 | IMPORT_TK name DOT_TK error
802 {yyerror ("'*' expected"); RECOVER;}
803 | IMPORT_TK name DOT_TK MULT_TK error
804 {yyerror ("';' expected"); RECOVER;}
807 type_declaration:
808 class_declaration
809 { end_class_declaration (0); }
810 | interface_declaration
811 { end_class_declaration (0); }
812 | empty_statement
813 | error
815 YYERROR_NOW;
816 yyerror ("Class or interface declaration expected");
820 /* 19.7 Shortened from the original:
821 modifiers: modifier | modifiers modifier
822 modifier: any of public... */
823 modifiers:
824 MODIFIER_TK
826 $$ = (1 << $1);
828 | modifiers MODIFIER_TK
830 int acc = (1 << $2);
831 if ($$ & acc)
832 parse_error_context
833 (ctxp->modifier_ctx [$2], "Modifier %qs declared twice",
834 java_accstring_lookup (acc));
835 else
837 $$ |= acc;
842 /* 19.8.1 Production from $8.1: Class Declaration */
843 class_declaration:
844 modifiers CLASS_TK identifier super interfaces
845 { create_class ($1, $3, $4, $5); }
846 class_body
848 | CLASS_TK identifier super interfaces
849 { create_class (0, $2, $3, $4); }
850 class_body
852 | modifiers CLASS_TK error
853 { yyerror ("Missing class name"); RECOVER; }
854 | CLASS_TK error
855 { yyerror ("Missing class name"); RECOVER; }
856 | CLASS_TK identifier error
858 if (!ctxp->class_err) yyerror ("'{' expected");
859 DRECOVER(class1);
861 | modifiers CLASS_TK identifier error
862 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
865 super:
866 { $$ = NULL; }
867 | EXTENDS_TK class_type
868 { $$ = $2; }
869 | EXTENDS_TK class_type error
870 {yyerror ("'{' expected"); ctxp->class_err=1;}
871 | EXTENDS_TK error
872 {yyerror ("Missing super class name"); ctxp->class_err=1;}
875 interfaces:
876 { $$ = NULL_TREE; }
877 | IMPLEMENTS_TK interface_type_list
878 { $$ = $2; }
879 | IMPLEMENTS_TK error
881 ctxp->class_err=1;
882 yyerror ("Missing interface name");
886 interface_type_list:
887 interface_type
889 ctxp->interface_number = 1;
890 $$ = build_tree_list ($1, NULL_TREE);
892 | interface_type_list C_TK interface_type
894 ctxp->interface_number++;
895 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
897 | interface_type_list C_TK error
898 {yyerror ("Missing interface name"); RECOVER;}
901 class_body:
902 OCB_TK CCB_TK
904 $$ = GET_CPC ();
906 | OCB_TK class_body_declarations CCB_TK
908 $$ = GET_CPC ();
912 class_body_declarations:
913 class_body_declaration
914 | class_body_declarations class_body_declaration
917 class_body_declaration:
918 class_member_declaration
919 | static_initializer
920 | constructor_declaration
921 | block /* Added, JDK1.1, instance initializer */
923 if (!IS_EMPTY_STMT ($1))
925 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
926 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
931 class_member_declaration:
932 field_declaration
933 | method_declaration
934 | class_declaration /* Added, JDK1.1 inner classes */
935 { end_class_declaration (1); }
936 | interface_declaration /* Added, JDK1.1 inner interfaces */
937 { end_class_declaration (1); }
938 | empty_statement
941 /* 19.8.2 Productions from 8.3: Field Declarations */
942 field_declaration:
943 type variable_declarators SC_TK
944 { register_fields (0, $1, $2); }
945 | modifiers type variable_declarators SC_TK
947 check_modifiers
948 ("Illegal modifier %qs for field declaration",
949 $1, FIELD_MODIFIERS);
950 check_modifiers_consistency ($1);
951 register_fields ($1, $2, $3);
955 variable_declarators:
956 /* Should we use build_decl_list () instead ? FIXME */
957 variable_declarator /* Default rule */
958 | variable_declarators C_TK variable_declarator
959 { $$ = chainon ($1, $3); }
960 | variable_declarators C_TK error
961 {yyerror ("Missing term"); RECOVER;}
964 variable_declarator:
965 variable_declarator_id
966 { $$ = build_tree_list ($1, NULL_TREE); }
967 | variable_declarator_id ASSIGN_TK variable_initializer
969 if (java_error_count)
970 $3 = NULL_TREE;
971 $$ = build_tree_list
972 ($1, build_assignment ($2.token, $2.location, $1, $3));
974 | variable_declarator_id ASSIGN_TK error
976 yyerror ("Missing variable initializer");
977 $$ = build_tree_list ($1, NULL_TREE);
978 RECOVER;
980 | variable_declarator_id ASSIGN_TK variable_initializer error
982 yyerror ("';' expected");
983 $$ = build_tree_list ($1, NULL_TREE);
984 RECOVER;
988 variable_declarator_id:
989 identifier
990 | variable_declarator_id OSB_TK CSB_TK
991 { $$ = build_unresolved_array_type ($1); }
992 | identifier error
993 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
994 | variable_declarator_id OSB_TK error
996 yyerror ("']' expected");
997 DRECOVER(vdi);
999 | variable_declarator_id CSB_TK error
1000 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1003 variable_initializer:
1004 expression
1005 | array_initializer
1008 /* 19.8.3 Productions from 8.4: Method Declarations */
1009 method_declaration:
1010 method_header
1012 current_function_decl = $1;
1013 if (current_function_decl
1014 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1015 source_start_java_method (current_function_decl);
1016 else
1017 current_function_decl = NULL_TREE;
1019 method_body
1020 { finish_method_declaration ($3); }
1021 | method_header error
1022 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1025 method_header:
1026 type method_declarator throws
1027 { $$ = method_header (0, $1, $2, $3); }
1028 | VOID_TK method_declarator throws
1029 { $$ = method_header (0, void_type_node, $2, $3); }
1030 | modifiers type method_declarator throws
1031 { $$ = method_header ($1, $2, $3, $4); }
1032 | modifiers VOID_TK method_declarator throws
1033 { $$ = method_header ($1, void_type_node, $3, $4); }
1034 | type error
1036 yyerror ("Invalid method declaration, method name required");
1037 RECOVER;
1039 | modifiers type error
1041 yyerror ("Identifier expected");
1042 RECOVER;
1044 | VOID_TK error
1046 yyerror ("Identifier expected");
1047 RECOVER;
1049 | modifiers VOID_TK error
1051 yyerror ("Identifier expected");
1052 RECOVER;
1054 | modifiers error
1056 yyerror ("Invalid method declaration, return type required");
1057 RECOVER;
1061 method_declarator:
1062 identifier OP_TK CP_TK
1064 ctxp->formal_parameter_number = 0;
1065 $$ = method_declarator ($1, NULL_TREE);
1067 | identifier OP_TK formal_parameter_list CP_TK
1068 { $$ = method_declarator ($1, $3); }
1069 | method_declarator OSB_TK CSB_TK
1071 SET_EXPR_LOCATION_FROM_TOKEN (wfl_operator, $2);
1072 TREE_PURPOSE ($1) =
1073 build_unresolved_array_type (TREE_PURPOSE ($1));
1074 parse_warning_context
1075 (wfl_operator,
1076 "Discouraged form of returned type specification");
1078 | identifier OP_TK error
1079 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1080 | method_declarator OSB_TK error
1081 {yyerror ("']' expected"); RECOVER;}
1084 formal_parameter_list:
1085 formal_parameter
1087 ctxp->formal_parameter_number = 1;
1089 | formal_parameter_list C_TK formal_parameter
1091 ctxp->formal_parameter_number += 1;
1092 $$ = chainon ($1, $3);
1094 | formal_parameter_list C_TK error
1095 { yyerror ("Missing formal parameter term"); RECOVER; }
1098 formal_parameter:
1099 type variable_declarator_id
1101 $$ = build_tree_list ($2, $1);
1103 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1105 $$ = build_tree_list ($3, $2);
1106 ARG_FINAL_P ($$) = 1;
1108 | type error
1110 yyerror ("Missing identifier"); RECOVER;
1111 $$ = NULL_TREE;
1113 | final type error
1115 yyerror ("Missing identifier"); RECOVER;
1116 $$ = NULL_TREE;
1120 final:
1121 modifiers
1123 check_modifiers ("Illegal modifier %qs. Only %<final%> was expected here",
1124 $1, ACC_FINAL);
1125 if ($1 != ACC_FINAL)
1126 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1130 throws:
1131 { $$ = NULL_TREE; }
1132 | THROWS_TK class_type_list
1133 { $$ = $2; }
1134 | THROWS_TK error
1135 {yyerror ("Missing class type term"); RECOVER;}
1138 class_type_list:
1139 class_type
1140 { $$ = build_tree_list ($1, $1); }
1141 | class_type_list C_TK class_type
1142 { $$ = tree_cons ($3, $3, $1); }
1143 | class_type_list C_TK error
1144 {yyerror ("Missing class type term"); RECOVER;}
1147 method_body:
1148 block
1149 | SC_TK { $$ = NULL_TREE; }
1152 /* 19.8.4 Productions from 8.5: Static Initializers */
1153 static_initializer:
1154 static block
1156 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1157 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1158 current_static_block = NULL_TREE;
1162 static: /* Test lval.sub_token here */
1163 modifiers
1165 check_modifiers ("Illegal modifier %qs for static initializer", $1, ACC_STATIC);
1166 /* Can't have a static initializer in an innerclass */
1167 if ($1 | ACC_STATIC &&
1168 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1169 parse_error_context
1170 (MODIFIER_WFL (STATIC_TK),
1171 "Can't define static initializer in class %qs. Static initializer can only be defined in top-level classes",
1172 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1173 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1177 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1178 constructor_declaration:
1179 constructor_header
1181 current_function_decl = $1;
1182 source_start_java_method (current_function_decl);
1184 constructor_body
1185 { finish_method_declaration ($3); }
1188 constructor_header:
1189 constructor_declarator throws
1190 { $$ = method_header (0, NULL_TREE, $1, $2); }
1191 | modifiers constructor_declarator throws
1192 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1195 constructor_declarator:
1196 simple_name OP_TK CP_TK
1198 ctxp->formal_parameter_number = 0;
1199 $$ = method_declarator ($1, NULL_TREE);
1201 | simple_name OP_TK formal_parameter_list CP_TK
1202 { $$ = method_declarator ($1, $3); }
1205 constructor_body:
1206 /* Unlike regular method, we always need a complete (empty)
1207 body so we can safely perform all the required code
1208 addition (super invocation and field initialization) */
1209 block_begin constructor_block_end
1211 BLOCK_EXPR_BODY ($2) = build_java_empty_stmt ();
1212 $$ = $2;
1214 | block_begin explicit_constructor_invocation constructor_block_end
1215 { $$ = $3; }
1216 | block_begin block_statements constructor_block_end
1217 { $$ = $3; }
1218 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1219 { $$ = $4; }
1222 constructor_block_end:
1223 block_end
1226 /* Error recovery for that rule moved down expression_statement: rule. */
1227 explicit_constructor_invocation:
1228 this_or_super OP_TK CP_TK SC_TK
1230 $$ = build_method_invocation ($1, NULL_TREE);
1231 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1232 $$ = java_method_add_stmt (current_function_decl, $$);
1234 | this_or_super OP_TK argument_list CP_TK SC_TK
1236 $$ = build_method_invocation ($1, $3);
1237 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1238 $$ = java_method_add_stmt (current_function_decl, $$);
1240 /* Added, JDK1.1 inner classes. Modified because the rule
1241 'primary' couldn't work. */
1242 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1243 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1244 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1245 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1248 this_or_super: /* Added, simplifies error diagnostics */
1249 THIS_TK
1251 tree wfl = build_wfl_node (this_identifier_node);
1252 SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1253 $$ = wfl;
1255 | SUPER_TK
1257 tree wfl = build_wfl_node (super_identifier_node);
1258 SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1259 $$ = wfl;
1263 /* 19.9 Productions from 9: Interfaces */
1264 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1265 interface_declaration:
1266 INTERFACE_TK identifier
1267 { create_interface (0, $2, NULL_TREE); }
1268 interface_body
1269 { ; }
1270 | modifiers INTERFACE_TK identifier
1271 { create_interface ($1, $3, NULL_TREE); }
1272 interface_body
1273 { ; }
1274 | INTERFACE_TK identifier extends_interfaces
1275 { create_interface (0, $2, $3); }
1276 interface_body
1277 { ; }
1278 | modifiers INTERFACE_TK identifier extends_interfaces
1279 { create_interface ($1, $3, $4); }
1280 interface_body
1281 { ; }
1282 | INTERFACE_TK identifier error
1283 { yyerror ("'{' expected"); RECOVER; }
1284 | modifiers INTERFACE_TK identifier error
1285 { yyerror ("'{' expected"); RECOVER; }
1288 extends_interfaces:
1289 EXTENDS_TK interface_type
1291 ctxp->interface_number = 1;
1292 $$ = build_tree_list ($2, NULL_TREE);
1294 | extends_interfaces C_TK interface_type
1296 ctxp->interface_number++;
1297 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1299 | EXTENDS_TK error
1300 {yyerror ("Invalid interface type"); RECOVER;}
1301 | extends_interfaces C_TK error
1302 {yyerror ("Missing term"); RECOVER;}
1305 interface_body:
1306 OCB_TK CCB_TK
1307 { $$ = NULL_TREE; }
1308 | OCB_TK interface_member_declarations CCB_TK
1309 { $$ = NULL_TREE; }
1312 interface_member_declarations:
1313 interface_member_declaration
1314 | interface_member_declarations interface_member_declaration
1317 interface_member_declaration:
1318 constant_declaration
1319 | abstract_method_declaration
1320 | class_declaration /* Added, JDK1.1 inner classes */
1321 { end_class_declaration (1); }
1322 | interface_declaration /* Added, JDK1.1 inner interfaces */
1323 { end_class_declaration (1); }
1324 | empty_statement
1327 constant_declaration:
1328 field_declaration
1331 abstract_method_declaration:
1332 method_header SC_TK
1334 check_abstract_method_header ($1);
1335 current_function_decl = NULL_TREE; /* FIXME ? */
1337 | method_header error
1338 {yyerror ("';' expected"); RECOVER;}
1341 /* 19.10 Productions from 10: Arrays */
1342 array_initializer:
1343 OCB_TK CCB_TK
1344 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1345 | OCB_TK C_TK CCB_TK
1346 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1347 | OCB_TK variable_initializers CCB_TK
1348 { $$ = build_new_array_init ($1.location, $2); }
1349 | OCB_TK variable_initializers C_TK CCB_TK
1350 { $$ = build_new_array_init ($1.location, $2); }
1353 variable_initializers:
1354 variable_initializer
1356 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1357 $1, NULL_TREE);
1359 | variable_initializers C_TK variable_initializer
1361 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1363 | variable_initializers C_TK error
1364 {yyerror ("Missing term"); RECOVER;}
1367 /* 19.11 Production from 14: Blocks and Statements */
1368 block:
1369 block_begin block_end
1370 { $$ = $2; }
1371 | block_begin block_statements block_end
1372 { $$ = $3; }
1375 block_begin:
1376 OCB_TK
1377 { enter_block (); }
1380 block_end:
1381 CCB_TK
1383 maybe_absorb_scoping_blocks ();
1384 $$ = exit_block ();
1385 if (!BLOCK_SUBBLOCKS ($$))
1386 BLOCK_SUBBLOCKS ($$) = build_java_empty_stmt ();
1390 block_statements:
1391 block_statement
1392 | block_statements block_statement
1395 block_statement:
1396 local_variable_declaration_statement
1397 | statement
1398 { java_method_add_stmt (current_function_decl, $1); }
1399 | class_declaration /* Added, JDK1.1 local classes */
1401 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1402 end_class_declaration (1);
1406 local_variable_declaration_statement:
1407 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1410 local_variable_declaration:
1411 type variable_declarators
1412 { declare_local_variables (0, $1, $2); }
1413 | final type variable_declarators /* Added, JDK1.1 final locals */
1414 { declare_local_variables ($1, $2, $3); }
1417 statement:
1418 statement_without_trailing_substatement
1419 | labeled_statement
1420 | if_then_statement
1421 | if_then_else_statement
1422 | while_statement
1423 | for_statement
1424 { $$ = exit_block (); }
1427 statement_nsi:
1428 statement_without_trailing_substatement
1429 | labeled_statement_nsi
1430 | if_then_else_statement_nsi
1431 | while_statement_nsi
1432 | for_statement_nsi
1433 { $$ = exit_block (); }
1436 statement_without_trailing_substatement:
1437 block
1438 | empty_statement
1439 | expression_statement
1440 | switch_statement
1441 | do_statement
1442 | break_statement
1443 | continue_statement
1444 | return_statement
1445 | synchronized_statement
1446 | throw_statement
1447 | try_statement
1448 | assert_statement
1451 empty_statement:
1452 SC_TK
1454 if (flag_extraneous_semicolon
1455 && ! current_static_block
1456 && (! current_function_decl ||
1457 /* Verify we're not in a inner class declaration */
1458 (GET_CPC () != TYPE_NAME
1459 (DECL_CONTEXT (current_function_decl)))))
1462 #ifdef USE_MAPPED_LOCATION
1463 SET_EXPR_LOCATION (wfl_operator, input_location);
1464 #else
1465 EXPR_WFL_SET_LINECOL (wfl_operator, input_line, -1);
1466 #endif
1467 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1469 $$ = build_java_empty_stmt ();
1473 label_decl:
1474 identifier REL_CL_TK
1476 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1477 EXPR_WFL_NODE ($1));
1478 pushlevel (2);
1479 push_labeled_block ($$);
1480 PUSH_LABELED_BLOCK ($$);
1484 labeled_statement:
1485 label_decl statement
1486 { $$ = finish_labeled_statement ($1, $2); }
1487 | identifier error
1488 {yyerror ("':' expected"); RECOVER;}
1491 labeled_statement_nsi:
1492 label_decl statement_nsi
1493 { $$ = finish_labeled_statement ($1, $2); }
1496 /* We concentrate here a bunch of error handling rules that we couldn't write
1497 earlier, because expression_statement catches a missing ';'. */
1498 expression_statement:
1499 statement_expression SC_TK
1501 /* We have a statement. Generate a WFL around it so
1502 we can debug it */
1503 #ifdef USE_MAPPED_LOCATION
1504 $$ = expr_add_location ($1, input_location, 1);
1505 #else
1506 $$ = build_expr_wfl ($1, input_filename, input_line, 0);
1507 JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1508 #endif
1509 /* We know we have a statement, so set the debug
1510 info to be eventually generate here. */
1512 | error SC_TK
1514 YYNOT_TWICE yyerror ("Invalid expression statement");
1515 DRECOVER (expr_stmt);
1517 | error OCB_TK
1519 YYNOT_TWICE yyerror ("Invalid expression statement");
1520 DRECOVER (expr_stmt);
1522 | error CCB_TK
1524 YYNOT_TWICE yyerror ("Invalid expression statement");
1525 DRECOVER (expr_stmt);
1527 | this_or_super OP_TK error
1528 {yyerror ("')' expected"); RECOVER;}
1529 | this_or_super OP_TK CP_TK error
1531 parse_ctor_invocation_error ();
1532 RECOVER;
1534 | this_or_super OP_TK argument_list error
1535 {yyerror ("')' expected"); RECOVER;}
1536 | this_or_super OP_TK argument_list CP_TK error
1538 parse_ctor_invocation_error ();
1539 RECOVER;
1541 | name DOT_TK SUPER_TK error
1542 {yyerror ("'(' expected"); RECOVER;}
1543 | name DOT_TK SUPER_TK OP_TK error
1544 {yyerror ("')' expected"); RECOVER;}
1545 | name DOT_TK SUPER_TK OP_TK argument_list error
1546 {yyerror ("')' expected"); RECOVER;}
1547 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1548 {yyerror ("';' expected"); RECOVER;}
1549 | name DOT_TK SUPER_TK OP_TK CP_TK error
1550 {yyerror ("';' expected"); RECOVER;}
1553 statement_expression:
1554 assignment
1555 | pre_increment_expression
1556 | pre_decrement_expression
1557 | post_increment_expression
1558 | post_decrement_expression
1559 | method_invocation
1560 | class_instance_creation_expression
1563 if_then_statement:
1564 IF_TK OP_TK expression CP_TK statement
1566 $$ = build_if_else_statement ($2.location, $3,
1567 $5, NULL_TREE);
1569 | IF_TK error
1570 {yyerror ("'(' expected"); RECOVER;}
1571 | IF_TK OP_TK error
1572 {yyerror ("Missing term"); RECOVER;}
1573 | IF_TK OP_TK expression error
1574 {yyerror ("')' expected"); RECOVER;}
1577 if_then_else_statement:
1578 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1579 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1582 if_then_else_statement_nsi:
1583 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1584 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1587 switch_statement:
1588 switch_expression
1590 enter_block ();
1592 switch_block
1594 /* Make into "proper list" of COMPOUND_EXPRs.
1595 I.e. make the last statement also have its own
1596 COMPOUND_EXPR. */
1597 maybe_absorb_scoping_blocks ();
1598 TREE_OPERAND ($1, 1) = exit_block ();
1599 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1603 switch_expression:
1604 SWITCH_TK OP_TK expression CP_TK
1606 $$ = build3 (SWITCH_EXPR, NULL_TREE, $3,
1607 NULL_TREE, NULL_TREE);
1608 SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
1610 | SWITCH_TK error
1611 {yyerror ("'(' expected"); RECOVER;}
1612 | SWITCH_TK OP_TK error
1613 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1614 | SWITCH_TK OP_TK expression CP_TK error
1615 {yyerror ("'{' expected"); RECOVER;}
1618 /* Default assignment is there to avoid type node on switch_block
1619 node. */
1621 switch_block:
1622 OCB_TK CCB_TK
1623 { $$ = NULL_TREE; }
1624 | OCB_TK switch_labels CCB_TK
1625 { $$ = NULL_TREE; }
1626 | OCB_TK switch_block_statement_groups CCB_TK
1627 { $$ = NULL_TREE; }
1628 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1629 { $$ = NULL_TREE; }
1632 switch_block_statement_groups:
1633 switch_block_statement_group
1634 | switch_block_statement_groups switch_block_statement_group
1637 switch_block_statement_group:
1638 switch_labels block_statements
1641 switch_labels:
1642 switch_label
1643 | switch_labels switch_label
1646 switch_label:
1647 CASE_TK constant_expression REL_CL_TK
1649 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1650 SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1651 java_method_add_stmt (current_function_decl, lab);
1653 | DEFAULT_TK REL_CL_TK
1655 tree lab = make_node (DEFAULT_EXPR);
1656 SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1657 java_method_add_stmt (current_function_decl, lab);
1659 | CASE_TK error
1660 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1661 | CASE_TK constant_expression error
1662 {yyerror ("':' expected"); RECOVER;}
1663 | DEFAULT_TK error
1664 {yyerror ("':' expected"); RECOVER;}
1667 while_expression:
1668 WHILE_TK OP_TK expression CP_TK
1670 tree body = build_loop_body ($2.location, $3, 0);
1671 $$ = build_new_loop (body);
1675 while_statement:
1676 while_expression statement
1677 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1678 | WHILE_TK error
1679 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1680 | WHILE_TK OP_TK error
1681 {yyerror ("Missing term and ')' expected"); RECOVER;}
1682 | WHILE_TK OP_TK expression error
1683 {yyerror ("')' expected"); RECOVER;}
1686 while_statement_nsi:
1687 while_expression statement_nsi
1688 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1691 do_statement_begin:
1692 DO_TK
1694 tree body = build_loop_body (0, NULL_TREE, 1);
1695 $$ = build_new_loop (body);
1697 /* Need error handing here. FIXME */
1700 do_statement:
1701 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1702 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1705 for_statement:
1706 for_begin SC_TK expression SC_TK for_update CP_TK statement
1708 if (CONSTANT_CLASS_P ($3))
1709 $3 = build_wfl_node ($3);
1710 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1712 | for_begin SC_TK SC_TK for_update CP_TK statement
1714 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1715 /* We have not condition, so we get rid of the EXIT_EXPR */
1716 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1717 build_java_empty_stmt ();
1719 | for_begin SC_TK error
1720 {yyerror ("Invalid control expression"); RECOVER;}
1721 | for_begin SC_TK expression SC_TK error
1722 {yyerror ("Invalid update expression"); RECOVER;}
1723 | for_begin SC_TK SC_TK error
1724 {yyerror ("Invalid update expression"); RECOVER;}
1727 for_statement_nsi:
1728 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1729 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1730 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1732 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1733 /* We have not condition, so we get rid of the EXIT_EXPR */
1734 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1735 build_java_empty_stmt ();
1739 for_header:
1740 FOR_TK OP_TK
1742 /* This scope defined for local variable that may be
1743 defined within the scope of the for loop */
1744 enter_block ();
1746 | FOR_TK error
1747 {yyerror ("'(' expected"); DRECOVER(for_1);}
1748 | FOR_TK OP_TK error
1749 {yyerror ("Invalid init statement"); RECOVER;}
1752 for_begin:
1753 for_header for_init
1755 /* We now declare the loop body. The loop is
1756 declared as a for loop. */
1757 tree body = build_loop_body (0, NULL_TREE, 0);
1758 $$ = build_new_loop (body);
1759 FOR_LOOP_P ($$) = 1;
1760 /* The loop is added to the current block the for
1761 statement is defined within */
1762 java_method_add_stmt (current_function_decl, $$);
1765 for_init: /* Can be empty */
1766 { $$ = build_java_empty_stmt (); }
1767 | statement_expression_list
1769 /* Init statement recorded within the previously
1770 defined block scope */
1771 $$ = java_method_add_stmt (current_function_decl, $1);
1773 | local_variable_declaration
1775 /* Local variable are recorded within the previously
1776 defined block scope */
1777 $$ = NULL_TREE;
1779 | statement_expression_list error
1780 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1783 for_update: /* Can be empty */
1784 {$$ = build_java_empty_stmt ();}
1785 | statement_expression_list
1786 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1789 statement_expression_list:
1790 statement_expression
1791 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1792 | statement_expression_list C_TK statement_expression
1793 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1794 | statement_expression_list C_TK error
1795 {yyerror ("Missing term"); RECOVER;}
1798 break_statement:
1799 BREAK_TK SC_TK
1800 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1801 | BREAK_TK identifier SC_TK
1802 { $$ = build_bc_statement ($1.location, 1, $2); }
1803 | BREAK_TK error
1804 {yyerror ("Missing term"); RECOVER;}
1805 | BREAK_TK identifier error
1806 {yyerror ("';' expected"); RECOVER;}
1809 continue_statement:
1810 CONTINUE_TK SC_TK
1811 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1812 | CONTINUE_TK identifier SC_TK
1813 { $$ = build_bc_statement ($1.location, 0, $2); }
1814 | CONTINUE_TK error
1815 {yyerror ("Missing term"); RECOVER;}
1816 | CONTINUE_TK identifier error
1817 {yyerror ("';' expected"); RECOVER;}
1820 return_statement:
1821 RETURN_TK SC_TK
1822 { $$ = build_return ($1.location, NULL_TREE); }
1823 | RETURN_TK expression SC_TK
1824 { $$ = build_return ($1.location, $2); }
1825 | RETURN_TK error
1826 {yyerror ("Missing term"); RECOVER;}
1827 | RETURN_TK expression error
1828 {yyerror ("';' expected"); RECOVER;}
1831 throw_statement:
1832 THROW_TK expression SC_TK
1834 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1835 SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1837 | THROW_TK error
1838 {yyerror ("Missing term"); RECOVER;}
1839 | THROW_TK expression error
1840 {yyerror ("';' expected"); RECOVER;}
1843 assert_statement:
1844 ASSERT_TK expression REL_CL_TK expression SC_TK
1846 $$ = build_assertion ($1.location, $2, $4);
1848 | ASSERT_TK expression SC_TK
1850 $$ = build_assertion ($1.location, $2, NULL_TREE);
1852 | ASSERT_TK error
1853 {yyerror ("Missing term"); RECOVER;}
1854 | ASSERT_TK expression error
1855 {yyerror ("';' expected"); RECOVER;}
1858 synchronized_statement:
1859 synchronized OP_TK expression CP_TK block
1861 $$ = build2 (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1862 EXPR_WFL_LINECOL ($$) =
1863 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1865 | synchronized OP_TK expression CP_TK error
1866 {yyerror ("'{' expected"); RECOVER;}
1867 | synchronized error
1868 {yyerror ("'(' expected"); RECOVER;}
1869 | synchronized OP_TK error CP_TK
1870 {yyerror ("Missing term"); RECOVER;}
1871 | synchronized OP_TK error
1872 {yyerror ("Missing term"); RECOVER;}
1875 synchronized:
1876 modifiers
1878 check_modifiers (
1879 "Illegal modifier %qs. Only %<synchronized%> was expected here",
1880 $1, ACC_SYNCHRONIZED);
1881 if ($1 != ACC_SYNCHRONIZED)
1882 MODIFIER_WFL (SYNCHRONIZED_TK) =
1883 build_wfl_node (NULL_TREE);
1887 try_statement:
1888 TRY_TK block catches
1889 { $$ = build_try_statement ($1.location, $2, $3); }
1890 | TRY_TK block finally
1891 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1892 | TRY_TK block catches finally
1893 { $$ = build_try_finally_statement
1894 ($1.location, build_try_statement ($1.location,
1895 $2, $3), $4);
1897 | TRY_TK error
1898 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1901 catches:
1902 catch_clause
1903 | catches catch_clause
1905 TREE_CHAIN ($2) = $1;
1906 $$ = $2;
1910 catch_clause:
1911 catch_clause_parameter block
1913 java_method_add_stmt (current_function_decl, $2);
1914 exit_block ();
1915 $$ = $1;
1919 catch_clause_parameter:
1920 CATCH_TK OP_TK formal_parameter CP_TK
1922 /* We add a block to define a scope for
1923 formal_parameter (CCBP). The formal parameter is
1924 declared initialized by the appropriate function
1925 call */
1926 tree ccpb;
1927 tree init;
1928 if ($3)
1930 ccpb = enter_block ();
1931 init = build_assignment
1932 (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1933 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1934 declare_local_variables (0, TREE_VALUE ($3),
1935 build_tree_list
1936 (TREE_PURPOSE ($3), init));
1937 $$ = build1 (JAVA_CATCH_EXPR, NULL_TREE, ccpb);
1938 SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1940 else
1942 $$ = error_mark_node;
1945 | CATCH_TK error
1946 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1947 | CATCH_TK OP_TK error
1949 yyerror ("Missing term or ')' expected");
1950 RECOVER; $$ = NULL_TREE;
1952 | CATCH_TK OP_TK error CP_TK /* That's for () */
1953 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1956 finally:
1957 FINALLY_TK block
1958 { $$ = $2; }
1959 | FINALLY_TK error
1960 {yyerror ("'{' expected"); RECOVER; }
1963 /* 19.12 Production from 15: Expressions */
1964 primary:
1965 primary_no_new_array
1966 | array_creation_uninitialized
1967 | array_creation_initialized
1970 primary_no_new_array:
1971 literal
1972 | THIS_TK
1973 { $$ = build_this ($1.location); }
1974 | OP_TK expression CP_TK
1975 {$$ = $2;}
1976 | class_instance_creation_expression
1977 | field_access
1978 | method_invocation
1979 | array_access
1980 | type_literals
1981 /* Added, JDK1.1 inner classes. Documentation is wrong
1982 referring to a 'ClassName' (class_name) rule that doesn't
1983 exist. Used name: instead. */
1984 | name DOT_TK THIS_TK
1986 tree wfl = build_wfl_node (this_identifier_node);
1987 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1989 | OP_TK expression error
1990 {yyerror ("')' expected"); RECOVER;}
1991 | name DOT_TK error
1992 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1993 | primitive_type DOT_TK error
1994 {yyerror ("'class' expected" ); RECOVER;}
1995 | VOID_TK DOT_TK error
1996 {yyerror ("'class' expected" ); RECOVER;}
1999 type_literals:
2000 name DOT_TK CLASS_TK
2001 { $$ = build_incomplete_class_ref ($2.location, $1); }
2002 | array_type DOT_TK CLASS_TK
2003 { $$ = build_incomplete_class_ref ($2.location, $1); }
2004 | primitive_type DOT_TK CLASS_TK
2005 { $$ = build_incomplete_class_ref ($2.location, $1); }
2006 | VOID_TK DOT_TK CLASS_TK
2008 $$ = build_incomplete_class_ref ($2.location,
2009 void_type_node);
2013 class_instance_creation_expression:
2014 NEW_TK class_type OP_TK argument_list CP_TK
2015 { $$ = build_new_invocation ($2, $4); }
2016 | NEW_TK class_type OP_TK CP_TK
2017 { $$ = build_new_invocation ($2, NULL_TREE); }
2018 | anonymous_class_creation
2019 /* Added, JDK1.1 inner classes, modified to use name or
2020 primary instead of primary solely which couldn't work in
2021 all situations. */
2022 | something_dot_new identifier OP_TK CP_TK
2024 tree ctor = build_new_invocation ($2, NULL_TREE);
2025 $$ = make_qualified_primary ($1, ctor,
2026 EXPR_WFL_LINECOL ($1));
2028 | something_dot_new identifier OP_TK CP_TK class_body
2029 | something_dot_new identifier OP_TK argument_list CP_TK
2031 tree ctor = build_new_invocation ($2, $4);
2032 $$ = make_qualified_primary ($1, ctor,
2033 EXPR_WFL_LINECOL ($1));
2035 | something_dot_new identifier OP_TK argument_list CP_TK class_body
2036 | NEW_TK error SC_TK
2037 {$$ = NULL_TREE; yyerror ("'(' expected"); DRECOVER(new_1);}
2038 | NEW_TK class_type error
2039 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2040 | NEW_TK class_type OP_TK error
2041 {$$ = NULL_TREE; yyerror ("')' or term expected"); RECOVER;}
2042 | NEW_TK class_type OP_TK argument_list error
2043 {$$ = NULL_TREE; yyerror ("')' expected"); RECOVER;}
2044 | something_dot_new error
2046 $$ = NULL_TREE;
2047 YYERROR_NOW;
2048 yyerror ("Identifier expected");
2049 RECOVER;
2051 | something_dot_new identifier error
2052 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2055 /* Created after JDK1.1 rules originally added to
2056 class_instance_creation_expression, but modified to use
2057 'class_type' instead of 'TypeName' (type_name) which is mentioned
2058 in the documentation but doesn't exist. */
2060 anonymous_class_creation:
2061 NEW_TK class_type OP_TK argument_list CP_TK
2062 { create_anonymous_class ($2); }
2063 class_body
2065 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2066 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2068 end_class_declaration (1);
2070 /* Now we can craft the new expression */
2071 $$ = build_new_invocation (id, $4);
2073 /* Note that we can't possibly be here if
2074 `class_type' is an interface (in which case the
2075 anonymous class extends Object and implements
2076 `class_type', hence its constructor can't have
2077 arguments.) */
2079 /* Otherwise, the innerclass must feature a
2080 constructor matching `argument_list'. Anonymous
2081 classes are a bit special: it's impossible to
2082 define constructor for them, hence constructors
2083 must be generated following the hints provided by
2084 the `new' expression. Whether a super constructor
2085 of that nature exists or not is to be verified
2086 later on in get_constructor_super.
2088 It's during the expansion of a `new' statement
2089 referring to an anonymous class that a ctor will
2090 be generated for the anonymous class, with the
2091 right arguments. */
2094 | NEW_TK class_type OP_TK CP_TK
2095 { create_anonymous_class ($2); }
2096 class_body
2098 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2099 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2101 end_class_declaration (1);
2103 /* Now we can craft the new expression. The
2104 statement doesn't need to be remember so that a
2105 constructor can be generated, since its signature
2106 is already known. */
2107 $$ = build_new_invocation (id, NULL_TREE);
2111 something_dot_new: /* Added, not part of the specs. */
2112 name DOT_TK NEW_TK
2113 { $$ = $1; }
2114 | primary DOT_TK NEW_TK
2115 { $$ = $1; }
2118 argument_list:
2119 expression
2121 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2122 ctxp->formal_parameter_number = 1;
2124 | argument_list C_TK expression
2126 ctxp->formal_parameter_number += 1;
2127 $$ = tree_cons (NULL_TREE, $3, $1);
2129 | argument_list C_TK error
2130 {yyerror ("Missing term"); RECOVER;}
2133 array_creation_uninitialized:
2134 NEW_TK primitive_type dim_exprs
2135 { $$ = build_newarray_node ($2, $3, 0); }
2136 | NEW_TK class_or_interface_type dim_exprs
2137 { $$ = build_newarray_node ($2, $3, 0); }
2138 | NEW_TK primitive_type dim_exprs dims
2139 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2140 | NEW_TK class_or_interface_type dim_exprs dims
2141 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2142 | NEW_TK error CSB_TK
2143 {yyerror ("'[' expected"); DRECOVER ("]");}
2144 | NEW_TK error OSB_TK
2145 {yyerror ("']' expected"); RECOVER;}
2148 array_creation_initialized:
2149 /* Added, JDK1.1 anonymous array. Initial documentation rule
2150 modified */
2151 NEW_TK class_or_interface_type dims array_initializer
2153 char *sig;
2154 int osb = pop_current_osb (ctxp);
2155 while (osb--)
2156 obstack_grow (&temporary_obstack, "[]", 2);
2157 obstack_1grow (&temporary_obstack, '\0');
2158 sig = obstack_finish (&temporary_obstack);
2159 $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2160 $2, get_identifier (sig), $4);
2162 | NEW_TK primitive_type dims array_initializer
2164 int osb = pop_current_osb (ctxp);
2165 tree type = $2;
2166 while (osb--)
2167 type = build_java_array_type (type, -1);
2168 $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2169 build_pointer_type (type), NULL_TREE, $4);
2171 | NEW_TK error CSB_TK
2172 {yyerror ("'[' expected"); DRECOVER ("]");}
2173 | NEW_TK error OSB_TK
2174 {yyerror ("']' expected"); RECOVER;}
2177 dim_exprs:
2178 dim_expr
2179 { $$ = build_tree_list (NULL_TREE, $1); }
2180 | dim_exprs dim_expr
2181 { $$ = tree_cons (NULL_TREE, $2, $$); }
2184 dim_expr:
2185 OSB_TK expression CSB_TK
2187 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2189 $2 = build_wfl_node ($2);
2190 TREE_TYPE ($2) = NULL_TREE;
2192 EXPR_WFL_LINECOL ($2) = $1.location;
2193 $$ = $2;
2195 | OSB_TK expression error
2196 {yyerror ("']' expected"); RECOVER;}
2197 | OSB_TK error
2199 yyerror ("Missing term");
2200 yyerror ("']' expected");
2201 RECOVER;
2205 dims:
2206 OSB_TK CSB_TK
2208 int allocate = 0;
2209 /* If not initialized, allocate memory for the osb
2210 numbers stack */
2211 if (!ctxp->osb_limit)
2213 allocate = ctxp->osb_limit = 32;
2214 ctxp->osb_depth = -1;
2216 /* If capacity overflown, reallocate a bigger chunk */
2217 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2218 allocate = ctxp->osb_limit << 1;
2220 if (allocate)
2222 allocate *= sizeof (int);
2223 if (ctxp->osb_number)
2224 ctxp->osb_number = xrealloc (ctxp->osb_number,
2225 allocate);
2226 else
2227 ctxp->osb_number = xmalloc (allocate);
2229 ctxp->osb_depth++;
2230 CURRENT_OSB (ctxp) = 1;
2232 | dims OSB_TK CSB_TK
2233 { CURRENT_OSB (ctxp)++; }
2234 | dims OSB_TK error
2235 { yyerror ("']' expected"); RECOVER;}
2238 field_access:
2239 primary DOT_TK identifier
2240 { $$ = make_qualified_primary ($1, $3, $2.location); }
2241 /* FIXME - REWRITE TO:
2242 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2243 | SUPER_TK DOT_TK identifier
2245 tree super_wfl = build_wfl_node (super_identifier_node);
2246 SET_EXPR_LOCATION_FROM_TOKEN (super_wfl, $1);
2247 $$ = make_qualified_name (super_wfl, $3, $2.location);
2249 | SUPER_TK error
2250 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2253 method_invocation:
2254 name OP_TK CP_TK
2255 { $$ = build_method_invocation ($1, NULL_TREE); }
2256 | name OP_TK argument_list CP_TK
2257 { $$ = build_method_invocation ($1, $3); }
2258 | primary DOT_TK identifier OP_TK CP_TK
2260 if (TREE_CODE ($1) == THIS_EXPR)
2261 $$ = build_this_super_qualified_invocation
2262 (1, $3, NULL_TREE, 0, $2.location);
2263 else
2265 tree invok = build_method_invocation ($3, NULL_TREE);
2266 $$ = make_qualified_primary ($1, invok, $2.location);
2269 | primary DOT_TK identifier OP_TK argument_list CP_TK
2271 if (TREE_CODE ($1) == THIS_EXPR)
2272 $$ = build_this_super_qualified_invocation
2273 (1, $3, $5, 0, $2.location);
2274 else
2276 tree invok = build_method_invocation ($3, $5);
2277 $$ = make_qualified_primary ($1, invok, $2.location);
2280 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2282 $$ = build_this_super_qualified_invocation
2283 (0, $3, NULL_TREE, $1.location, $2.location);
2285 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2287 $$ = build_this_super_qualified_invocation
2288 (0, $3, $5, $1.location, $2.location);
2290 /* Screws up thing. I let it here until I'm convinced it can
2291 be removed. FIXME
2292 | primary DOT_TK error
2293 {yyerror ("'(' expected"); DRECOVER(bad);} */
2294 | SUPER_TK DOT_TK error CP_TK
2295 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2296 | SUPER_TK DOT_TK error DOT_TK
2297 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2300 array_access:
2301 name OSB_TK expression CSB_TK
2302 { $$ = build_array_ref ($2.location, $1, $3); }
2303 | primary_no_new_array OSB_TK expression CSB_TK
2304 { $$ = build_array_ref ($2.location, $1, $3); }
2305 | array_creation_initialized OSB_TK expression CSB_TK
2306 { $$ = build_array_ref ($2.location, $1, $3); }
2307 | name OSB_TK error
2309 yyerror ("Missing term and ']' expected");
2310 DRECOVER(array_access);
2312 | name OSB_TK expression error
2314 yyerror ("']' expected");
2315 DRECOVER(array_access);
2317 | primary_no_new_array OSB_TK error
2319 yyerror ("Missing term and ']' expected");
2320 DRECOVER(array_access);
2322 | primary_no_new_array OSB_TK expression error
2324 yyerror ("']' expected");
2325 DRECOVER(array_access);
2327 | array_creation_initialized OSB_TK error
2329 yyerror ("Missing term and ']' expected");
2330 DRECOVER(array_access);
2332 | array_creation_initialized OSB_TK expression error
2334 yyerror ("']' expected");
2335 DRECOVER(array_access);
2339 postfix_expression:
2340 primary
2341 | name
2342 | post_increment_expression
2343 | post_decrement_expression
2346 post_increment_expression:
2347 postfix_expression INCR_TK
2348 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2351 post_decrement_expression:
2352 postfix_expression DECR_TK
2353 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2356 trap_overflow_corner_case:
2357 pre_increment_expression
2358 | pre_decrement_expression
2359 | PLUS_TK unary_expression
2360 {$$ = build_unaryop ($1.token, $1.location, $2); }
2361 | unary_expression_not_plus_minus
2362 | PLUS_TK error
2363 {yyerror ("Missing term"); RECOVER}
2366 unary_expression:
2367 trap_overflow_corner_case
2369 if ($1)
2370 error_if_numeric_overflow ($1);
2371 $$ = $1;
2373 | MINUS_TK trap_overflow_corner_case
2374 {$$ = build_unaryop ($1.token, $1.location, $2); }
2375 | MINUS_TK error
2376 {yyerror ("Missing term"); RECOVER}
2379 pre_increment_expression:
2380 INCR_TK unary_expression
2381 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2382 | INCR_TK error
2383 {yyerror ("Missing term"); RECOVER}
2386 pre_decrement_expression:
2387 DECR_TK unary_expression
2388 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2389 | DECR_TK error
2390 {yyerror ("Missing term"); RECOVER}
2393 unary_expression_not_plus_minus:
2394 postfix_expression
2395 | NOT_TK unary_expression
2396 {$$ = build_unaryop ($1.token, $1.location, $2); }
2397 | NEG_TK unary_expression
2398 {$$ = build_unaryop ($1.token, $1.location, $2); }
2399 | cast_expression
2400 | NOT_TK error
2401 {yyerror ("Missing term"); RECOVER}
2402 | NEG_TK error
2403 {yyerror ("Missing term"); RECOVER}
2406 cast_expression: /* Error handling here is potentially weak */
2407 OP_TK primitive_type dims CP_TK unary_expression
2409 tree type = $2;
2410 int osb = pop_current_osb (ctxp);
2411 while (osb--)
2412 type = build_java_array_type (type, -1);
2413 $$ = build_cast ($1.location, type, $5);
2415 | OP_TK primitive_type CP_TK unary_expression
2416 { $$ = build_cast ($1.location, $2, $4); }
2417 | OP_TK expression CP_TK unary_expression_not_plus_minus
2418 { $$ = build_cast ($1.location, $2, $4); }
2419 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2421 const char *ptr;
2422 int osb = pop_current_osb (ctxp);
2423 obstack_grow (&temporary_obstack,
2424 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2425 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2426 while (osb--)
2427 obstack_grow (&temporary_obstack, "[]", 2);
2428 obstack_1grow (&temporary_obstack, '\0');
2429 ptr = obstack_finish (&temporary_obstack);
2430 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2431 $$ = build_cast ($1.location, $2, $5);
2433 | OP_TK primitive_type OSB_TK error
2434 {yyerror ("']' expected, invalid type expression");}
2435 | OP_TK error
2437 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2438 RECOVER;
2440 | OP_TK primitive_type dims CP_TK error
2441 {yyerror ("Missing term"); RECOVER;}
2442 | OP_TK primitive_type CP_TK error
2443 {yyerror ("Missing term"); RECOVER;}
2444 | OP_TK name dims CP_TK error
2445 {yyerror ("Missing term"); RECOVER;}
2448 multiplicative_expression:
2449 unary_expression
2450 | multiplicative_expression MULT_TK unary_expression
2452 $$ = build_binop (BINOP_LOOKUP ($2.token),
2453 $2.location, $1, $3);
2455 | multiplicative_expression DIV_TK unary_expression
2457 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2458 $1, $3);
2460 | multiplicative_expression REM_TK unary_expression
2462 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2463 $1, $3);
2465 | multiplicative_expression MULT_TK error
2466 {yyerror ("Missing term"); RECOVER;}
2467 | multiplicative_expression DIV_TK error
2468 {yyerror ("Missing term"); RECOVER;}
2469 | multiplicative_expression REM_TK error
2470 {yyerror ("Missing term"); RECOVER;}
2473 additive_expression:
2474 multiplicative_expression
2475 | additive_expression PLUS_TK multiplicative_expression
2477 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2478 $1, $3);
2480 | additive_expression MINUS_TK multiplicative_expression
2482 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2483 $1, $3);
2485 | additive_expression PLUS_TK error
2486 {yyerror ("Missing term"); RECOVER;}
2487 | additive_expression MINUS_TK error
2488 {yyerror ("Missing term"); RECOVER;}
2491 shift_expression:
2492 additive_expression
2493 | shift_expression LS_TK additive_expression
2495 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2496 $1, $3);
2498 | shift_expression SRS_TK additive_expression
2500 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2501 $1, $3);
2503 | shift_expression ZRS_TK additive_expression
2505 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2506 $1, $3);
2508 | shift_expression LS_TK error
2509 {yyerror ("Missing term"); RECOVER;}
2510 | shift_expression SRS_TK error
2511 {yyerror ("Missing term"); RECOVER;}
2512 | shift_expression ZRS_TK error
2513 {yyerror ("Missing term"); RECOVER;}
2516 relational_expression:
2517 shift_expression
2518 | relational_expression LT_TK shift_expression
2520 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2521 $1, $3);
2523 | relational_expression GT_TK shift_expression
2525 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2526 $1, $3);
2528 | relational_expression LTE_TK shift_expression
2530 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2531 $1, $3);
2533 | relational_expression GTE_TK shift_expression
2535 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2536 $1, $3);
2538 | relational_expression INSTANCEOF_TK reference_type
2539 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2540 | relational_expression LT_TK error
2541 {yyerror ("Missing term"); RECOVER;}
2542 | relational_expression GT_TK error
2543 {yyerror ("Missing term"); RECOVER;}
2544 | relational_expression LTE_TK error
2545 {yyerror ("Missing term"); RECOVER;}
2546 | relational_expression GTE_TK error
2547 {yyerror ("Missing term"); RECOVER;}
2548 | relational_expression INSTANCEOF_TK error
2549 {yyerror ("Invalid reference type"); RECOVER;}
2552 equality_expression:
2553 relational_expression
2554 | equality_expression EQ_TK relational_expression
2556 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2557 $1, $3);
2559 | equality_expression NEQ_TK relational_expression
2561 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2562 $1, $3);
2564 | equality_expression EQ_TK error
2565 {yyerror ("Missing term"); RECOVER;}
2566 | equality_expression NEQ_TK error
2567 {yyerror ("Missing term"); RECOVER;}
2570 and_expression:
2571 equality_expression
2572 | and_expression AND_TK equality_expression
2574 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2575 $1, $3);
2577 | and_expression AND_TK error
2578 {yyerror ("Missing term"); RECOVER;}
2581 exclusive_or_expression:
2582 and_expression
2583 | exclusive_or_expression XOR_TK and_expression
2585 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2586 $1, $3);
2588 | exclusive_or_expression XOR_TK error
2589 {yyerror ("Missing term"); RECOVER;}
2592 inclusive_or_expression:
2593 exclusive_or_expression
2594 | inclusive_or_expression OR_TK exclusive_or_expression
2596 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2597 $1, $3);
2599 | inclusive_or_expression OR_TK error
2600 {yyerror ("Missing term"); RECOVER;}
2603 conditional_and_expression:
2604 inclusive_or_expression
2605 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2607 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2608 $1, $3);
2610 | conditional_and_expression BOOL_AND_TK error
2611 {yyerror ("Missing term"); RECOVER;}
2614 conditional_or_expression:
2615 conditional_and_expression
2616 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2618 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2619 $1, $3);
2621 | conditional_or_expression BOOL_OR_TK error
2622 {yyerror ("Missing term"); RECOVER;}
2625 conditional_expression: /* Error handling here is weak */
2626 conditional_or_expression
2627 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2629 $$ = build3 (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2630 SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
2632 | conditional_or_expression REL_QM_TK REL_CL_TK error
2634 YYERROR_NOW;
2635 yyerror ("Missing term");
2636 DRECOVER (1);
2638 | conditional_or_expression REL_QM_TK error
2639 {yyerror ("Missing term"); DRECOVER (2);}
2640 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2641 {yyerror ("Missing term"); DRECOVER (3);}
2644 assignment_expression:
2645 conditional_expression
2646 | assignment
2649 assignment:
2650 left_hand_side assignment_operator assignment_expression
2651 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2652 | left_hand_side assignment_operator error
2654 YYNOT_TWICE yyerror ("Missing term");
2655 DRECOVER (assign);
2659 left_hand_side:
2660 name
2661 | field_access
2662 | array_access
2665 assignment_operator:
2666 ASSIGN_ANY_TK
2667 | ASSIGN_TK
2670 expression:
2671 assignment_expression
2674 constant_expression:
2675 expression
2680 /* Helper function to retrieve an OSB count. Should be used when the
2681 `dims:' rule is being used. */
2683 static int
2684 pop_current_osb (struct parser_ctxt *ctxp)
2686 int to_return;
2688 if (ctxp->osb_depth < 0)
2689 abort ();
2691 to_return = CURRENT_OSB (ctxp);
2692 ctxp->osb_depth--;
2694 return to_return;
2699 /* This section of the code deal with save/restoring parser contexts.
2700 Add mode documentation here. FIXME */
2702 /* Helper function. Create a new parser context. With
2703 COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2704 context is copied, otherwise, the new context is zeroed. The newly
2705 created context becomes the current one. */
2707 static void
2708 create_new_parser_context (int copy_from_previous)
2710 struct parser_ctxt *new;
2712 new = ggc_alloc (sizeof (struct parser_ctxt));
2713 if (copy_from_previous)
2715 memcpy (new, ctxp, sizeof (struct parser_ctxt));
2716 /* This flag, indicating the context saves global values,
2717 should only be set by java_parser_context_save_global. */
2718 new->saved_data_ctx = 0;
2720 else
2721 memset (new, 0, sizeof (struct parser_ctxt));
2723 new->next = ctxp;
2724 ctxp = new;
2727 /* Create a new parser context and make it the current one. */
2729 void
2730 java_push_parser_context (void)
2732 create_new_parser_context (0);
2735 void
2736 java_pop_parser_context (int generate)
2738 tree current;
2739 struct parser_ctxt *next;
2741 if (!ctxp)
2742 return;
2744 next = ctxp->next;
2745 if (next)
2747 input_location = ctxp->save_location;
2748 current_class = ctxp->class_type;
2751 /* If the old and new lexers differ, then free the old one. */
2752 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2753 java_destroy_lexer (ctxp->lexer);
2755 /* Set the single import class file flag to 0 for the current list
2756 of imported things */
2757 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2758 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2760 /* If we pushed a context to parse a class intended to be generated,
2761 we keep it so we can remember the class. What we could actually
2762 do is to just update a list of class names. */
2763 if (generate)
2765 if (ctxp_for_generation_last == NULL)
2766 ctxp_for_generation = ctxp;
2767 else
2768 ctxp_for_generation_last->next = ctxp;
2769 ctxp->next = NULL;
2770 ctxp_for_generation_last = ctxp;
2773 /* And restore those of the previous context */
2774 if ((ctxp = next)) /* Assignment is really meant here */
2775 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2776 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2779 /* Create a parser context for the use of saving some global
2780 variables. */
2782 void
2783 java_parser_context_save_global (void)
2785 if (!ctxp)
2787 java_push_parser_context ();
2788 ctxp->saved_data_ctx = 1;
2791 /* If this context already stores data, create a new one suitable
2792 for data storage. */
2793 else if (ctxp->saved_data)
2795 create_new_parser_context (1);
2796 ctxp->saved_data_ctx = 1;
2799 ctxp->save_location = input_location;
2800 ctxp->class_type = current_class;
2801 ctxp->function_decl = current_function_decl;
2802 ctxp->saved_data = 1;
2805 /* Restore some global variables from the previous context. Make the
2806 previous context the current one. */
2808 void
2809 java_parser_context_restore_global (void)
2811 input_location = ctxp->save_location;
2812 current_class = ctxp->class_type;
2813 if (wfl_operator)
2814 #ifdef USE_MAPPED_LOCATION
2815 SET_EXPR_LOCATION (wfl_operator, ctxp->save_location);
2816 #else
2817 EXPR_WFL_FILENAME_NODE (wfl_operator) = get_identifier (input_filename);
2818 #endif
2819 current_function_decl = ctxp->function_decl;
2820 ctxp->saved_data = 0;
2821 if (ctxp->saved_data_ctx)
2822 java_pop_parser_context (0);
2825 /* Suspend vital data for the current class/function being parsed so
2826 that an other class can be parsed. Used to let local/anonymous
2827 classes be parsed. */
2829 static void
2830 java_parser_context_suspend (void)
2832 /* This makes debugging through java_debug_context easier */
2833 static const char *const name = "<inner buffer context>";
2835 /* Duplicate the previous context, use it to save the globals we're
2836 interested in */
2837 create_new_parser_context (1);
2838 ctxp->function_decl = current_function_decl;
2839 ctxp->class_type = current_class;
2841 /* Then create a new context which inherits all data from the
2842 previous one. This will be the new current context */
2843 create_new_parser_context (1);
2845 /* Help debugging */
2846 ctxp->next->filename = name;
2849 /* Resume vital data for the current class/function being parsed so
2850 that an other class can be parsed. Used to let local/anonymous
2851 classes be parsed. The trick is the data storing file position
2852 informations must be restored to their current value, so parsing
2853 can resume as if no context was ever saved. */
2855 static void
2856 java_parser_context_resume (void)
2858 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2859 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2860 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2862 /* We need to inherit the list of classes to complete/generate */
2863 restored->classd_list = old->classd_list;
2864 restored->class_list = old->class_list;
2866 /* Restore the current class and function from the saver */
2867 current_class = saver->class_type;
2868 current_function_decl = saver->function_decl;
2870 /* Retrieve the restored context */
2871 ctxp = restored;
2873 /* Re-installed the data for the parsing to carry on */
2874 memcpy (&ctxp->marker_begining, &old->marker_begining,
2875 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2878 /* Add a new anchor node to which all statement(s) initializing static
2879 and non static initialized upon declaration field(s) will be
2880 linked. */
2882 static void
2883 java_parser_context_push_initialized_field (void)
2885 tree node;
2887 node = build_tree_list (NULL_TREE, NULL_TREE);
2888 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2889 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2891 node = build_tree_list (NULL_TREE, NULL_TREE);
2892 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2893 CPC_INITIALIZER_LIST (ctxp) = node;
2895 node = build_tree_list (NULL_TREE, NULL_TREE);
2896 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2897 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2900 /* Pop the lists of initialized field. If this lists aren't empty,
2901 remember them so we can use it to create and populate the finit$
2902 or <clinit> functions. */
2904 static void
2905 java_parser_context_pop_initialized_field (void)
2907 tree stmts;
2908 tree class_type = TREE_TYPE (GET_CPC ());
2910 if (CPC_INITIALIZER_LIST (ctxp))
2912 stmts = CPC_INITIALIZER_STMT (ctxp);
2913 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2914 if (stmts && !java_error_count)
2915 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2918 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2920 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2921 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2922 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2923 /* Keep initialization in order to enforce 8.5 */
2924 if (stmts && !java_error_count)
2925 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2928 /* JDK 1.1 instance initializers */
2929 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2931 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2932 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2933 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2934 if (stmts && !java_error_count)
2935 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2939 static tree
2940 reorder_static_initialized (tree list)
2942 /* We have to keep things in order. The alias initializer have to
2943 come first, then the initialized regular field, in reverse to
2944 keep them in lexical order. */
2945 tree marker, previous = NULL_TREE;
2946 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2947 if (TREE_CODE (marker) == TREE_LIST
2948 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2949 break;
2951 /* No static initialized, the list is fine as is */
2952 if (!previous)
2953 list = TREE_CHAIN (marker);
2955 /* No marker? reverse the whole list */
2956 else if (!marker)
2957 list = nreverse (list);
2959 /* Otherwise, reverse what's after the marker and the new reordered
2960 sublist will replace the marker. */
2961 else
2963 TREE_CHAIN (previous) = NULL_TREE;
2964 list = nreverse (list);
2965 list = chainon (TREE_CHAIN (marker), list);
2967 return list;
2970 /* Helper functions to dump the parser context stack. */
2972 #define TAB_CONTEXT(C) \
2973 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2975 static void
2976 java_debug_context_do (int tab)
2978 struct parser_ctxt *copy = ctxp;
2979 while (copy)
2981 TAB_CONTEXT (tab);
2982 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2983 TAB_CONTEXT (tab);
2984 fprintf (stderr, "filename: %s\n", copy->filename);
2985 TAB_CONTEXT (tab);
2986 fprintf (stderr, "package: %s\n",
2987 (copy->package ?
2988 IDENTIFIER_POINTER (copy->package) : "<none>"));
2989 TAB_CONTEXT (tab);
2990 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2991 TAB_CONTEXT (tab);
2992 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2993 copy = copy->next;
2994 tab += 2;
2998 /* Dump the stacked up parser contexts. Intended to be called from a
2999 debugger. */
3001 void
3002 java_debug_context (void)
3004 java_debug_context_do (0);
3009 /* Flag for the error report routine to issue the error the first time
3010 it's called (overriding the default behavior which is to drop the
3011 first invocation and honor the second one, taking advantage of a
3012 richer context. */
3013 static int force_error = 0;
3015 /* Reporting an constructor invocation error. */
3016 static void
3017 parse_ctor_invocation_error (void)
3019 if (DECL_CONSTRUCTOR_P (current_function_decl))
3020 yyerror ("Constructor invocation must be first thing in a constructor");
3021 else
3022 yyerror ("Only constructors can invoke constructors");
3025 /* Reporting JDK1.1 features not implemented. */
3027 static tree
3028 parse_jdk1_1_error (const char *msg)
3030 sorry (": %qs JDK1.1(TM) feature", msg);
3031 java_error_count++;
3032 return build_java_empty_stmt ();
3035 static int do_warning = 0;
3037 void
3038 yyerror (const char *msgid)
3040 #ifdef USE_MAPPED_LOCATION
3041 static source_location elc;
3042 expanded_location xloc = expand_location (input_location);
3043 int current_line = xloc.line;
3044 #else
3045 static java_lc elc;
3046 int save_lineno;
3047 int current_line = input_line;
3048 #endif
3049 static int prev_lineno;
3050 static const char *prev_msg;
3052 char *remainder, *code_from_source;
3054 if (!force_error && prev_lineno == current_line)
3055 return;
3056 #ifndef USE_MAPPED_LOCATION
3057 current_line = ctxp->lexer->token_start.line;
3058 #endif
3060 /* Save current error location but report latter, when the context is
3061 richer. */
3062 if (ctxp->java_error_flag == 0)
3064 ctxp->java_error_flag = 1;
3065 #ifdef USE_MAPPED_LOCATION
3066 elc = input_location;
3067 #else
3068 elc = ctxp->lexer->token_start;
3069 #endif
3070 /* Do something to use the previous line if we're reaching the
3071 end of the file... */
3072 #ifdef VERBOSE_SKELETON
3073 printf ("* Error detected (%s)\n", (msgid ? msgid : "(null)"));
3074 #endif
3075 return;
3078 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3079 if (!force_error && msgid == prev_msg && prev_lineno == current_line)
3080 return;
3082 ctxp->java_error_flag = 0;
3083 if (do_warning)
3084 java_warning_count++;
3085 else
3086 java_error_count++;
3088 #if 0 /* FIXME */
3089 if (elc.col == 0 && msgid && msgid[1] == ';')
3090 elc = ctxp->prev_line_end;
3091 #endif
3093 prev_msg = msgid;
3095 #ifdef USE_MAPPED_LOCATION
3096 prev_lineno = current_line;
3097 code_from_source = java_get_line_col (xloc.file, current_line, xloc.column);
3098 #else
3099 save_lineno = input_line;
3100 prev_lineno = input_line = current_line;
3101 code_from_source = java_get_line_col (input_filename, current_line,
3102 ctxp->lexer->token_start.col);
3103 #endif
3106 obstack_grow0 (&temporary_obstack,
3107 code_from_source, strlen (code_from_source));
3108 remainder = obstack_finish (&temporary_obstack);
3109 if (do_warning)
3110 warning (0, "%s.\n%s", msgid, remainder);
3111 else
3112 error ("%s.\n%s", msgid, remainder);
3114 /* This allow us to cheaply avoid an extra 'Invalid expression
3115 statement' error report when errors have been already reported on
3116 the same line. This occurs when we report an error but don't have
3117 a synchronization point other than ';', which
3118 expression_statement is the only one to take care of. */
3119 #ifndef USE_MAPPED_LOCATION
3120 input_line = save_lineno;
3121 #endif
3122 ctxp->prevent_ese = input_line;
3125 static void
3126 issue_warning_error_from_context (
3127 #ifdef USE_MAPPED_LOCATION
3128 source_location cl,
3129 #else
3130 tree cl,
3131 #endif
3132 const char *gmsgid, va_list *ap)
3134 #ifdef USE_MAPPED_LOCATION
3135 source_location saved_location = input_location;
3136 expanded_location xloc = expand_location (cl);
3137 #else
3138 java_lc save_lc = ctxp->lexer->token_start;
3139 const char *saved = ctxp->filename, *saved_input_filename;
3140 #endif
3141 char buffer [4096];
3142 text_info text;
3144 text.err_no = errno;
3145 text.args_ptr = ap;
3146 text.format_spec = gmsgid;
3147 pp_format (global_dc->printer, &text);
3148 pp_output_formatted_text (global_dc->printer);
3149 strncpy (buffer, pp_formatted_text (global_dc->printer), sizeof (buffer) - 1);
3150 buffer[sizeof (buffer) - 1] = '\0';
3151 pp_clear_output_area (global_dc->printer);
3153 force_error = 1;
3155 #ifdef USE_MAPPED_LOCATION
3156 if (xloc.file != NULL)
3158 ctxp->filename = xloc.file;
3159 input_location = cl;
3161 #else
3162 ctxp->lexer->token_start.line = EXPR_WFL_LINENO (cl);
3163 ctxp->lexer->token_start.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1
3164 : EXPR_WFL_COLNO (cl) == 0xffe ? -2
3165 : EXPR_WFL_COLNO (cl));
3167 /* We have a CL, that's a good reason for using it if it contains data */
3168 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3169 ctxp->filename = EXPR_WFL_FILENAME (cl);
3170 saved_input_filename = input_filename;
3171 input_filename = ctxp->filename;
3172 #endif
3173 java_error (NULL);
3174 java_error (buffer);
3175 #ifdef USE_MAPPED_LOCATION
3176 input_location = saved_location;
3177 #else
3178 ctxp->filename = saved;
3179 input_filename = saved_input_filename;
3180 ctxp->lexer->token_start = save_lc;
3181 #endif
3182 force_error = 0;
3185 /* Issue an error message at a current source line CL.
3186 FUTURE/FIXME: change cl to be a source_location. */
3188 void
3189 parse_error_context (tree cl, const char *gmsgid, ...)
3191 va_list ap;
3192 va_start (ap, gmsgid);
3193 #ifdef USE_MAPPED_LOCATION
3194 issue_warning_error_from_context (EXPR_LOCATION (cl), gmsgid, &ap);
3195 #else
3196 issue_warning_error_from_context (cl, gmsgid, &ap);
3197 #endif
3198 va_end (ap);
3201 /* Issue a warning at a current source line CL.
3202 FUTURE/FIXME: change cl to be a source_location. */
3204 static void
3205 parse_warning_context (tree cl, const char *gmsgid, ...)
3207 va_list ap;
3208 va_start (ap, gmsgid);
3210 do_warning = 1;
3211 #ifdef USE_MAPPED_LOCATION
3212 issue_warning_error_from_context (EXPR_LOCATION (cl), gmsgid, &ap);
3213 #else
3214 issue_warning_error_from_context (cl, gmsgid, &ap);
3215 #endif
3216 do_warning = 0;
3217 va_end (ap);
3220 static tree
3221 find_expr_with_wfl (tree node)
3223 while (node)
3225 enum tree_code_class code;
3226 tree to_return;
3228 switch (TREE_CODE (node))
3230 case BLOCK:
3231 node = BLOCK_EXPR_BODY (node);
3232 continue;
3234 case COMPOUND_EXPR:
3235 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3236 if (to_return)
3237 return to_return;
3238 node = TREE_OPERAND (node, 1);
3239 continue;
3241 case LOOP_EXPR:
3242 node = TREE_OPERAND (node, 0);
3243 continue;
3245 case LABELED_BLOCK_EXPR:
3246 node = LABELED_BLOCK_BODY (node);
3247 continue;
3249 default:
3250 code = TREE_CODE_CLASS (TREE_CODE (node));
3251 if (((code == tcc_unary) || (code == tcc_binary)
3252 || (code == tcc_expression))
3253 && EXPR_WFL_LINECOL (node))
3254 return node;
3255 return NULL_TREE;
3258 return NULL_TREE;
3261 /* Issue a missing return statement error. Uses METHOD to figure the
3262 last line of the method the error occurs in. */
3264 static void
3265 missing_return_error (tree method)
3267 #ifdef USE_MAPPED_LOCATION
3268 SET_EXPR_LOCATION (wfl_operator, DECL_FUNCTION_LAST_LINE (method));
3269 #else
3270 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_FUNCTION_LAST_LINE (method), -2);
3271 #endif
3272 parse_error_context (wfl_operator, "Missing return statement");
3275 /* Issue an unreachable statement error. From NODE, find the next
3276 statement to report appropriately. */
3277 static void
3278 unreachable_stmt_error (tree node)
3280 /* Browse node to find the next expression node that has a WFL. Use
3281 the location to report the error */
3282 if (TREE_CODE (node) == COMPOUND_EXPR)
3283 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3284 else
3285 node = find_expr_with_wfl (node);
3287 if (node)
3289 #ifdef USE_MAPPED_LOCATION
3290 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
3291 #else
3292 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3293 #endif
3294 parse_error_context (wfl_operator, "Unreachable statement");
3296 else
3297 abort ();
3300 static int
3301 not_accessible_field_error (tree wfl, tree decl)
3303 parse_error_context
3304 (wfl, "Can't access %s field %<%s.%s%> from %qs",
3305 accessibility_string (get_access_flags_from_decl (decl)),
3306 GET_TYPE_NAME (DECL_CONTEXT (decl)),
3307 IDENTIFIER_POINTER (DECL_NAME (decl)),
3308 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
3309 return 1;
3313 java_report_errors (void)
3315 if (java_error_count)
3316 fprintf (stderr, "%d error%s",
3317 java_error_count, (java_error_count == 1 ? "" : "s"));
3318 if (java_warning_count)
3319 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3320 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3321 if (java_error_count || java_warning_count)
3322 putc ('\n', stderr);
3323 return java_error_count;
3326 static char *
3327 java_accstring_lookup (int flags)
3329 static char buffer [80];
3330 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3332 /* Access modifier looked-up first for easier report on forbidden
3333 access. */
3334 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3335 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3336 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3337 if (flags & ACC_STATIC) COPY_RETURN ("static");
3338 if (flags & ACC_FINAL) COPY_RETURN ("final");
3339 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3340 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3341 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3342 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3343 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3344 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3346 buffer [0] = '\0';
3347 return buffer;
3348 #undef COPY_RETURN
3351 /* Returns a string denoting the accessibility of a class or a member as
3352 indicated by FLAGS. We need a separate function from
3353 java_accstring_lookup, as the latter can return spurious "static", etc.
3354 if package-private access is defined (in which case none of the
3355 relevant access control bits in FLAGS is set). */
3357 static const char *
3358 accessibility_string (int flags)
3360 if (flags & ACC_PRIVATE) return "private";
3361 if (flags & ACC_PROTECTED) return "protected";
3362 if (flags & ACC_PUBLIC) return "public";
3364 return "package-private";
3367 /* Issuing error messages upon redefinition of classes, interfaces or
3368 variables. */
3370 static void
3371 classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3373 parse_error_context (cl, "%s %qs already defined in %s:%d",
3374 context, IDENTIFIER_POINTER (id),
3375 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3376 /* Here we should point out where its redefined. It's a unicode. FIXME */
3379 static void
3380 variable_redefinition_error (tree context, tree name, tree type, int line)
3382 const char *type_name;
3384 /* Figure a proper name for type. We might haven't resolved it */
3385 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3386 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3387 else
3388 type_name = lang_printable_name (type, 0);
3390 parse_error_context (context,
3391 "Variable %qs is already defined in this method and was declared %<%s %s%> at line %d",
3392 IDENTIFIER_POINTER (name),
3393 type_name, IDENTIFIER_POINTER (name), line);
3396 /* If ANAME is terminated with `[]', it indicates an array. This
3397 function returns the number of `[]' found and if this number is
3398 greater than zero, it extracts the array type name and places it in
3399 the node pointed to by TRIMMED unless TRIMMED is null. */
3401 static int
3402 build_type_name_from_array_name (tree aname, tree *trimmed)
3404 const char *name = IDENTIFIER_POINTER (aname);
3405 int len = IDENTIFIER_LENGTH (aname);
3406 int array_dims;
3408 STRING_STRIP_BRACKETS (name, len, array_dims);
3410 if (array_dims && trimmed)
3411 *trimmed = get_identifier_with_length (name, len);
3413 return array_dims;
3416 static tree
3417 build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3419 int more_dims = 0;
3421 /* Eventually get more dims */
3422 more_dims = build_type_name_from_array_name (name, &name);
3424 /* If we have, then craft a new type for this variable */
3425 if (more_dims)
3427 tree save = type;
3429 /* If we have a pointer, use its type */
3430 if (TREE_CODE (type) == POINTER_TYPE)
3431 type = TREE_TYPE (type);
3433 /* Building the first dimension of a primitive type uses this
3434 function */
3435 if (JPRIMITIVE_TYPE_P (type))
3437 type = build_java_array_type (type, -1);
3438 more_dims--;
3440 /* Otherwise, if we have a WFL for this type, use it (the type
3441 is already an array on an unresolved type, and we just keep
3442 on adding dimensions) */
3443 else if (type_wfl)
3445 type = type_wfl;
3446 more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3447 NULL);
3450 /* Add all the dimensions */
3451 while (more_dims--)
3452 type = build_unresolved_array_type (type);
3454 /* The type may have been incomplete in the first place */
3455 if (type_wfl)
3456 type = obtain_incomplete_type (type);
3459 if (ret_name)
3460 *ret_name = name;
3461 return type;
3464 /* Build something that the type identifier resolver will identify as
3465 being an array to an unresolved type. TYPE_WFL is a WFL on a
3466 identifier. */
3468 static tree
3469 build_unresolved_array_type (tree type_or_wfl)
3471 const char *ptr;
3472 tree wfl;
3474 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3475 just create a array type */
3476 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3477 return build_java_array_type (type_or_wfl, -1);
3479 obstack_grow (&temporary_obstack,
3480 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3481 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3482 obstack_grow0 (&temporary_obstack, "[]", 2);
3483 ptr = obstack_finish (&temporary_obstack);
3484 #ifdef USE_MAPPED_LOCATION
3485 wfl = build_expr_wfl (get_identifier (ptr), EXPR_LOCATION (type_or_wfl));
3486 #else
3487 wfl = build_expr_wfl (get_identifier (ptr),
3488 EXPR_WFL_FILENAME (type_or_wfl),
3489 EXPR_WFL_LINENO (type_or_wfl),
3490 EXPR_WFL_COLNO (type_or_wfl));
3491 #endif
3492 /* Re-install the existing qualifications so that the type can be
3493 resolved properly. */
3494 EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3495 return wfl;
3498 static void
3499 parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3501 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3502 parse_error_context (wfl, "Interface %qs repeated",
3503 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3506 /* Bulk of common class/interface checks. Return 1 if an error was
3507 encountered. TAG is 0 for a class, 1 for an interface. */
3509 static int
3510 check_class_interface_creation (int is_interface, int flags, tree raw_name,
3511 tree qualified_name, tree decl, tree cl)
3513 tree node;
3514 int sca = 0; /* Static class allowed */
3515 int icaf = 0; /* Inner class allowed flags */
3516 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3518 if (!quiet_flag)
3519 fprintf (stderr, " %s%s %s",
3520 (CPC_INNER_P () ? "inner" : ""),
3521 (is_interface ? "interface" : "class"),
3522 IDENTIFIER_POINTER (qualified_name));
3524 /* Scope of an interface/class type name:
3525 - Can't be imported by a single type import
3526 - Can't already exists in the package */
3527 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3528 && (node = find_name_in_single_imports (raw_name))
3529 && !CPC_INNER_P ())
3531 parse_error_context
3532 (cl, "%s name %qs clashes with imported type %qs",
3533 (is_interface ? "Interface" : "Class"),
3534 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3535 return 1;
3537 if (decl && CLASS_COMPLETE_P (decl))
3539 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3540 qualified_name, decl, cl);
3541 return 1;
3544 if (check_inner_class_redefinition (raw_name, cl))
3545 return 1;
3547 /* If public, file name should match class/interface name, except
3548 when dealing with an inner class */
3549 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3551 const char *fname = input_filename;
3552 const char *f;
3554 for (f = fname + strlen (fname);
3555 f != fname && ! IS_DIR_SEPARATOR (*f);
3556 f--)
3558 if (IS_DIR_SEPARATOR (*f))
3559 f++;
3560 if (strncmp (IDENTIFIER_POINTER (raw_name),
3561 f , IDENTIFIER_LENGTH (raw_name)) ||
3562 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3563 parse_error_context
3564 (cl, "Public %s %qs must be defined in a file called %<%s.java%>",
3565 (is_interface ? "interface" : "class"),
3566 IDENTIFIER_POINTER (qualified_name),
3567 IDENTIFIER_POINTER (raw_name));
3570 /* Static classes can be declared only in top level classes. Note:
3571 once static, a inner class is a top level class. */
3572 if (flags & ACC_STATIC)
3574 /* Catch the specific error of declaring an class inner class
3575 with no toplevel enclosing class. Prevent check_modifiers from
3576 complaining a second time */
3577 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3579 parse_error_context (cl, "Inner class %qs can't be static. Static classes can only occur in interfaces and top-level classes",
3580 IDENTIFIER_POINTER (qualified_name));
3581 sca = ACC_STATIC;
3583 /* Else, in the context of a top-level class declaration, let
3584 `check_modifiers' do its job, otherwise, give it a go */
3585 else
3586 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3589 /* Inner classes can be declared private or protected
3590 within their enclosing classes. */
3591 if (CPC_INNER_P ())
3593 /* A class which is local to a block can't be public, private,
3594 protected or static. But it is created final, so allow this
3595 one. */
3596 if (current_function_decl)
3597 icaf = sca = uaaf = ACC_FINAL;
3598 else
3600 check_modifiers_consistency (flags);
3601 icaf = ACC_PROTECTED;
3602 if (! CLASS_INTERFACE (GET_CPC ()))
3603 icaf |= ACC_PRIVATE;
3607 if (is_interface)
3609 if (CPC_INNER_P ())
3610 uaaf = INTERFACE_INNER_MODIFIERS;
3611 else
3612 uaaf = INTERFACE_MODIFIERS;
3614 check_modifiers ("Illegal modifier %qs for interface declaration",
3615 flags, uaaf);
3617 else
3618 check_modifiers ((current_function_decl ?
3619 "Illegal modifier %qs for local class declaration" :
3620 "Illegal modifier %qs for class declaration"),
3621 flags, uaaf|sca|icaf);
3622 return 0;
3625 /* Construct a nested class name. If the final component starts with
3626 a digit, return true. Otherwise return false. */
3627 static int
3628 make_nested_class_name (tree cpc_list)
3630 tree name;
3632 if (!cpc_list)
3633 return 0;
3635 make_nested_class_name (TREE_CHAIN (cpc_list));
3637 /* Pick the qualified name when dealing with the first upmost
3638 enclosing class */
3639 name = (TREE_CHAIN (cpc_list)
3640 ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3641 obstack_grow (&temporary_obstack,
3642 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3643 obstack_1grow (&temporary_obstack, '$');
3645 return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3648 /* Can't redefine a class already defined in an earlier scope. */
3650 static int
3651 check_inner_class_redefinition (tree raw_name, tree cl)
3653 tree scope_list;
3655 for (scope_list = GET_CPC_LIST (); scope_list;
3656 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3657 if (raw_name == GET_CPC_UN_NODE (scope_list))
3659 parse_error_context
3660 (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",
3661 IDENTIFIER_POINTER (raw_name));
3662 return 1;
3664 return 0;
3667 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. May return an
3668 invisible/non-accessible matching decl when an accessible one could not be
3669 found, in order to give a better error message when accessibility is
3670 checked later. */
3672 static tree
3673 resolve_inner_class (tree context, tree cl, tree enclosing, tree class_type)
3675 tree local_super = NULL_TREE;
3676 tree candidate = NULL_TREE;
3678 /* This hash table is used to register the classes we're going
3679 through when searching the current class as an inner class, in
3680 order to detect circular references. */
3681 htab_t circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
3682 NULL);
3684 while (enclosing)
3686 tree decl;
3688 *htab_find_slot (circularity_hash, enclosing, INSERT) = enclosing;
3690 if ((decl = find_as_inner_class (enclosing, class_type, cl)))
3692 if (inner_class_accessible (decl, context))
3694 candidate = decl;
3695 break;
3697 else
3698 if (candidate == NULL_TREE)
3699 candidate = decl;
3702 /* Now go to the upper classes, bail out if necessary. We will
3703 analyze the returned SUPER and act accordingly (see
3704 do_resolve_class). */
3705 if (JPRIMITIVE_TYPE_P (TREE_TYPE (enclosing))
3706 || TREE_TYPE (enclosing) == void_type_node)
3708 parse_error_context (cl, "Qualifier must be a reference");
3709 enclosing = NULL_TREE;
3710 break;
3712 local_super = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
3713 if (!local_super || local_super == object_type_node)
3714 break;
3716 if (TREE_CODE (local_super) == POINTER_TYPE)
3717 local_super = do_resolve_class (NULL, NULL, local_super, NULL, NULL);
3718 else
3719 local_super = TYPE_NAME (local_super);
3721 /* We may not have checked for circular inheritance yet, so do so
3722 here to prevent an infinite loop. */
3723 if (htab_find (circularity_hash, local_super) != NULL)
3725 if (!cl)
3726 cl = lookup_cl (enclosing);
3728 parse_error_context
3729 (cl, "Cyclic inheritance involving %s",
3730 IDENTIFIER_POINTER (DECL_NAME (enclosing)));
3731 enclosing = NULL_TREE;
3733 else
3734 enclosing = local_super;
3737 htab_delete (circularity_hash);
3739 /* We failed, but we might have found a matching class that wasn't
3740 accessible. Return that to get a better error message. */
3741 return candidate;
3744 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3745 qualified. */
3747 static tree
3748 find_as_inner_class (tree enclosing, tree name, tree cl)
3750 tree qual, to_return;
3751 if (!enclosing)
3752 return NULL_TREE;
3754 name = TYPE_NAME (name);
3756 /* First search: within the scope of `enclosing', search for name */
3757 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3758 qual = EXPR_WFL_QUALIFICATION (cl);
3759 else if (cl)
3760 qual = build_tree_list (cl, NULL_TREE);
3761 else
3762 qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3764 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3765 return to_return;
3767 /* We're dealing with a qualified name. Try to resolve thing until
3768 we get something that is an enclosing class. */
3769 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3771 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3773 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3774 qual = TREE_CHAIN (qual))
3776 acc = merge_qualified_name (acc,
3777 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3778 BUILD_PTR_FROM_NAME (ptr, acc);
3779 decl = do_resolve_class (NULL_TREE, NULL_TREE, ptr, NULL_TREE, cl);
3782 /* A NULL qual and a decl means that the search ended
3783 successfully?!? We have to do something then. FIXME */
3785 if (decl)
3786 enclosing = decl;
3787 else
3788 qual = EXPR_WFL_QUALIFICATION (cl);
3790 /* Otherwise, create a qual for the other part of the resolution. */
3791 else
3792 qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3794 return find_as_inner_class_do (qual, enclosing);
3797 /* We go inside the list of sub classes and try to find a way
3798 through. */
3800 static tree
3801 find_as_inner_class_do (tree qual, tree enclosing)
3803 if (!qual)
3804 return NULL_TREE;
3806 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3808 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3809 tree next_enclosing = NULL_TREE;
3810 tree inner_list;
3812 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3813 inner_list; inner_list = TREE_CHAIN (inner_list))
3815 if (TREE_VALUE (inner_list) == name_to_match)
3817 next_enclosing = TREE_PURPOSE (inner_list);
3818 break;
3821 enclosing = next_enclosing;
3824 return (!qual && enclosing ? enclosing : NULL_TREE);
3827 static void
3828 link_nested_class_to_enclosing (void)
3830 if (GET_ENCLOSING_CPC ())
3832 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3833 DECL_INNER_CLASS_LIST (enclosing) =
3834 tree_cons (GET_CPC (), GET_CPC_UN (),
3835 DECL_INNER_CLASS_LIST (enclosing));
3839 static tree
3840 maybe_make_nested_class_name (tree name)
3842 tree id = NULL_TREE;
3844 if (CPC_INNER_P ())
3846 /* If we're in a function, we must append a number to create the
3847 nested class name. However, we don't do this if the class we
3848 are constructing is anonymous, because in that case we'll
3849 already have a number as the class name. */
3850 if (! make_nested_class_name (GET_CPC_LIST ())
3851 && current_function_decl != NULL_TREE
3852 && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3854 char buf[10];
3855 sprintf (buf, "%d", anonymous_class_counter);
3856 ++anonymous_class_counter;
3857 obstack_grow (&temporary_obstack, buf, strlen (buf));
3858 obstack_1grow (&temporary_obstack, '$');
3860 obstack_grow0 (&temporary_obstack,
3861 IDENTIFIER_POINTER (name),
3862 IDENTIFIER_LENGTH (name));
3863 id = get_identifier (obstack_finish (&temporary_obstack));
3864 if (ctxp->package)
3865 QUALIFIED_P (id) = 1;
3867 return id;
3870 /* If DECL is NULL, create and push a new DECL, record the current
3871 line CL and do other maintenance things. */
3873 static tree
3874 maybe_create_class_interface_decl (tree decl, tree raw_name,
3875 tree qualified_name, tree cl)
3877 if (!decl)
3878 decl = push_class (make_class (), qualified_name);
3880 /* Take care of the file and line business */
3881 #ifdef USE_MAPPED_LOCATION
3882 DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (cl);
3883 #else
3884 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3885 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3886 #endif
3887 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3888 CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3889 #ifdef USE_MAPPED_LOCATION
3891 tree tmp = maybe_get_identifier (EXPR_FILENAME (cl));
3892 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3893 tmp && IS_A_COMMAND_LINE_FILENAME_P (tmp);
3895 #else
3896 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3897 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3898 #endif
3900 PUSH_CPC (decl, raw_name);
3901 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3903 /* Link the declaration to the already seen ones */
3904 TREE_CHAIN (decl) = ctxp->class_list;
3905 ctxp->class_list = decl;
3907 /* Create a new nodes in the global lists */
3908 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3909 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3911 /* Install a new dependency list element */
3912 create_jdep_list (ctxp);
3914 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3915 IDENTIFIER_POINTER (qualified_name)));
3916 return decl;
3919 static void
3920 add_superinterfaces (tree decl, tree interface_list)
3922 tree node;
3923 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3924 takes care of ensuring that:
3925 - This is an accessible interface type,
3926 - Circularity detection.
3927 parser_add_interface is then called. If present but not defined,
3928 the check operation is delayed until the super interface gets
3929 defined. */
3930 for (node = interface_list; node; node = TREE_CHAIN (node))
3932 tree current = TREE_PURPOSE (node);
3933 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3934 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3936 if (!parser_check_super_interface (idecl, decl, current))
3937 parser_add_interface (decl, idecl, current);
3939 else
3940 register_incomplete_type (JDEP_INTERFACE,
3941 current, decl, NULL_TREE);
3945 /* Create an interface in pass1 and return its decl. Return the
3946 interface's decl in pass 2. */
3948 static tree
3949 create_interface (int flags, tree id, tree super)
3951 tree raw_name = EXPR_WFL_NODE (id);
3952 tree q_name = parser_qualified_classname (raw_name);
3953 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3955 /* Certain syntax errors are making SUPER be like ID. Avoid this
3956 case. */
3957 if (ctxp->class_err && id == super)
3958 super = NULL;
3960 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3962 /* Basic checks: scope, redefinition, modifiers */
3963 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3965 PUSH_ERROR ();
3966 return NULL_TREE;
3969 /* Suspend the current parsing context if we're parsing an inner
3970 interface */
3971 if (CPC_INNER_P ())
3973 java_parser_context_suspend ();
3974 /* Interface members are public. */
3975 if (CLASS_INTERFACE (GET_CPC ()))
3976 flags |= ACC_PUBLIC;
3979 /* Push a new context for (static) initialized upon declaration fields */
3980 java_parser_context_push_initialized_field ();
3982 /* Interface modifiers check
3983 - public/abstract allowed (already done at that point)
3984 - abstract is obsolete (comes first, it's a warning, or should be)
3985 - Can't use twice the same (checked in the modifier rule) */
3986 if ((flags & ACC_ABSTRACT) && flag_redundant)
3987 parse_warning_context
3988 (MODIFIER_WFL (ABSTRACT_TK),
3989 "Redundant use of %<abstract%> modifier. Interface %qs is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3991 /* Create a new decl if DECL is NULL, otherwise fix it */
3992 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3994 /* Interfaces are always abstract. */
3995 flags |= ACC_ABSTRACT;
3997 /* Inner interfaces are always static. */
3998 if (INNER_CLASS_DECL_P (decl))
3999 flags |= ACC_STATIC;
4001 /* Set super info and mark the class a complete */
4002 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
4003 object_type_node, ctxp->interface_number);
4004 ctxp->interface_number = 0;
4005 CLASS_COMPLETE_P (decl) = 1;
4006 add_superinterfaces (decl, super);
4008 /* Eventually sets the @deprecated tag flag */
4009 CHECK_DEPRECATED (decl);
4011 return decl;
4014 /* Patch anonymous class CLASS, by either extending or implementing
4015 DEP. */
4017 static void
4018 patch_anonymous_class (tree type_decl, tree class_decl, tree wfl)
4020 tree class = TREE_TYPE (class_decl);
4021 tree type = TREE_TYPE (type_decl);
4022 tree binfo = TYPE_BINFO (class);
4024 /* If it's an interface, implement it */
4025 if (CLASS_INTERFACE (type_decl))
4027 if (parser_check_super_interface (type_decl, class_decl, wfl))
4028 return;
4030 if (!VEC_space (tree, BINFO_BASE_BINFOS (binfo), 1))
4032 /* Extend the binfo - by reallocating and copying it. */
4033 tree new_binfo;
4034 tree base_binfo;
4035 int i;
4037 new_binfo = make_tree_binfo ((BINFO_N_BASE_BINFOS (binfo) + 1) * 2);
4038 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4039 BINFO_BASE_APPEND (new_binfo, base_binfo);
4040 CLASS_HAS_SUPER_FLAG (new_binfo) = CLASS_HAS_SUPER_FLAG (binfo);
4041 BINFO_VTABLE (new_binfo) = BINFO_VTABLE (binfo);
4042 TYPE_BINFO (class) = new_binfo;
4045 /* And add the interface */
4046 parser_add_interface (class_decl, type_decl, wfl);
4048 /* Otherwise, it's a type we want to extend */
4049 else
4051 if (parser_check_super (type_decl, class_decl, wfl))
4052 return;
4053 BINFO_TYPE (BINFO_BASE_BINFO (binfo, 0)) = type;
4057 /* Create an anonymous class which extends/implements TYPE_NAME, and return
4058 its decl. */
4060 static tree
4061 create_anonymous_class (tree type_name)
4063 char buffer [80];
4064 tree super = NULL_TREE, itf = NULL_TREE;
4065 tree id, type_decl, class;
4067 /* The unqualified name of the anonymous class. It's just a number. */
4068 sprintf (buffer, "%d", anonymous_class_counter++);
4069 id = build_wfl_node (get_identifier (buffer));
4070 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL (type_name);
4072 /* We know about the type to extend/implement. We go ahead */
4073 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
4075 /* Create a class which either implements on extends the designated
4076 class. The class bears an inaccessible name. */
4077 if (CLASS_INTERFACE (type_decl))
4079 /* It's OK to modify it here. It's been already used and
4080 shouldn't be reused */
4081 ctxp->interface_number = 1;
4082 /* Interfaces should presented as a list of WFLs */
4083 itf = build_tree_list (type_name, NULL_TREE);
4085 else
4086 super = type_name;
4089 class = create_class (ACC_FINAL, id, super, itf);
4091 /* We didn't know anything about the stuff. We register a dependence. */
4092 if (!type_decl)
4093 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
4095 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
4096 return class;
4099 /* Create a class in pass1 and return its decl. Return class
4100 interface's decl in pass 2. */
4102 static tree
4103 create_class (int flags, tree id, tree super, tree interfaces)
4105 tree raw_name = EXPR_WFL_NODE (id);
4106 tree class_id, decl;
4107 tree super_decl_type;
4109 /* Certain syntax errors are making SUPER be like ID. Avoid this
4110 case. */
4111 if (ctxp->class_err && id == super)
4112 super = NULL;
4114 class_id = parser_qualified_classname (raw_name);
4115 decl = IDENTIFIER_CLASS_VALUE (class_id);
4116 EXPR_WFL_NODE (id) = class_id;
4118 /* Basic check: scope, redefinition, modifiers */
4119 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
4121 PUSH_ERROR ();
4122 return NULL_TREE;
4125 /* Suspend the current parsing context if we're parsing an inner
4126 class or an anonymous class. */
4127 if (CPC_INNER_P ())
4129 java_parser_context_suspend ();
4130 /* Interface members are public. */
4131 if (CLASS_INTERFACE (GET_CPC ()))
4132 flags |= ACC_PUBLIC;
4135 /* Push a new context for (static) initialized upon declaration fields */
4136 java_parser_context_push_initialized_field ();
4138 /* Class modifier check:
4139 - Allowed modifier (already done at that point)
4140 - abstract AND final forbidden
4141 - Public classes defined in the correct file */
4142 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4143 parse_error_context
4144 (id, "Class %qs can't be declared both abstract and final",
4145 IDENTIFIER_POINTER (raw_name));
4147 /* Create a new decl if DECL is NULL, otherwise fix it */
4148 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4150 /* If SUPER exists, use it, otherwise use Object */
4151 if (super)
4153 /* java.lang.Object can't extend anything. */
4154 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4156 parse_error_context (id, "%<java.lang.Object%> can't extend anything");
4157 return NULL_TREE;
4160 super_decl_type =
4161 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4163 else if (TREE_TYPE (decl) != object_type_node)
4164 super_decl_type = object_type_node;
4165 /* We're defining java.lang.Object */
4166 else
4167 super_decl_type = NULL_TREE;
4169 /* A class nested in an interface is implicitly static. */
4170 if (INNER_CLASS_DECL_P (decl)
4171 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4173 flags |= ACC_STATIC;
4176 /* Set super info and mark the class as complete. */
4177 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4178 ctxp->interface_number);
4179 ctxp->interface_number = 0;
4180 CLASS_COMPLETE_P (decl) = 1;
4181 add_superinterfaces (decl, interfaces);
4183 /* TYPE_VFIELD' is a compiler-generated field used to point to
4184 virtual function tables. In gcj, every class has a common base
4185 virtual function table in java.lang.object. */
4186 TYPE_VFIELD (TREE_TYPE (decl)) = TYPE_VFIELD (object_type_node);
4188 /* We keep the compilation unit imports in the class so that
4189 they can be used later to resolve type dependencies that
4190 aren't necessary to solve now. */
4191 TYPE_IMPORT_LIST (TREE_TYPE (decl)) = ctxp->import_list;
4192 TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (decl)) = ctxp->import_demand_list;
4194 /* Add the private this$<n> field, Replicate final locals still in
4195 scope as private final fields mangled like val$<local_name>.
4196 This does not occur for top level (static) inner classes. */
4197 if (PURE_INNER_CLASS_DECL_P (decl))
4198 add_inner_class_fields (decl, current_function_decl);
4200 /* Eventually sets the @deprecated tag flag */
4201 CHECK_DEPRECATED (decl);
4203 /* Reset the anonymous class counter when declaring non inner classes */
4204 if (!INNER_CLASS_DECL_P (decl))
4205 anonymous_class_counter = 1;
4207 return decl;
4210 /* End a class declaration: register the statements used to create
4211 finit$ and <clinit>, pop the current class and resume the prior
4212 parser context if necessary. */
4214 static void
4215 end_class_declaration (int resume)
4217 /* If an error occurred, context weren't pushed and won't need to be
4218 popped by a resume. */
4219 int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4221 if (GET_CPC () != error_mark_node)
4222 dump_java_tree (TDI_class, GET_CPC ());
4224 java_parser_context_pop_initialized_field ();
4225 POP_CPC ();
4226 if (resume && no_error_occurred)
4227 java_parser_context_resume ();
4229 /* We're ending a class declaration, this is a good time to reset
4230 the interface cout. Note that might have been already done in
4231 create_interface, but if at that time an inner class was being
4232 dealt with, the interface count was reset in a context created
4233 for the sake of handling inner classes declaration. */
4234 ctxp->interface_number = 0;
4237 static void
4238 add_inner_class_fields (tree class_decl, tree fct_decl)
4240 tree block, marker, f;
4242 f = add_field (TREE_TYPE (class_decl),
4243 build_current_thisn (TREE_TYPE (class_decl)),
4244 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4245 ACC_PRIVATE);
4246 FIELD_THISN (f) = 1;
4248 if (!fct_decl)
4249 return;
4251 for (block = GET_CURRENT_BLOCK (fct_decl);
4252 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4254 tree decl;
4255 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4257 tree name, pname;
4258 tree wfl, init, list;
4260 /* Avoid non final arguments. */
4261 if (!LOCAL_FINAL_P (decl))
4262 continue;
4264 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4265 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4266 wfl = build_wfl_node (name);
4267 init = build_wfl_node (pname);
4268 /* Build an initialization for the field: it will be
4269 initialized by a parameter added to finit$, bearing a
4270 mangled name of the field itself (param$<n>.) The
4271 parameter is provided to finit$ by the constructor
4272 invoking it (hence the constructor will also feature a
4273 hidden parameter, set to the value of the outer context
4274 local at the time the inner class is created.)
4276 Note: we take into account all possible locals that can
4277 be accessed by the inner class. It's actually not trivial
4278 to minimize these aliases down to the ones really
4279 used. One way to do that would be to expand all regular
4280 methods first, then finit$ to get a picture of what's
4281 used. It works with the exception that we would have to
4282 go back on all constructor invoked in regular methods to
4283 have their invocation reworked (to include the right amount
4284 of alias initializer parameters.)
4286 The only real way around, I think, is a first pass to
4287 identify locals really used in the inner class. We leave
4288 the flag FIELD_LOCAL_ALIAS_USED around for that future
4289 use.
4291 On the other hand, it only affect local inner classes,
4292 whose constructors (and finit$ call) will be featuring
4293 unnecessary arguments. It's easy for a developer to keep
4294 this number of parameter down by using the `final'
4295 keyword only when necessary. For the time being, we can
4296 issue a warning on unnecessary finals. FIXME */
4297 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4298 wfl, init);
4300 /* Register the field. The TREE_LIST holding the part
4301 initialized/initializer will be marked ARG_FINAL_P so
4302 that the created field can be marked
4303 FIELD_LOCAL_ALIAS. */
4304 list = build_tree_list (wfl, init);
4305 ARG_FINAL_P (list) = 1;
4306 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4310 if (!CPC_INITIALIZER_STMT (ctxp))
4311 return;
4313 /* If we ever registered an alias field, insert and marker to
4314 remember where the list ends. The second part of the list (the one
4315 featuring initialized fields) so it can be later reversed to
4316 enforce 8.5. The marker will be removed during that operation. */
4317 marker = build_tree_list (NULL_TREE, NULL_TREE);
4318 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4319 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4322 /* Can't use lookup_field () since we don't want to load the class and
4323 can't set the CLASS_LOADED_P flag */
4325 static tree
4326 find_field (tree class, tree name)
4328 tree decl;
4329 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4331 if (DECL_NAME (decl) == name)
4332 return decl;
4334 return NULL_TREE;
4337 /* Wrap around lookup_field that doesn't potentially upset the value
4338 of CLASS */
4340 static tree
4341 lookup_field_wrapper (tree class, tree name)
4343 tree type = class;
4344 tree decl = NULL_TREE;
4345 java_parser_context_save_global ();
4347 /* Last chance: if we're within the context of an inner class, we
4348 might be trying to access a local variable defined in an outer
4349 context. We try to look for it now. */
4350 if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4352 tree new_name;
4353 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4354 decl = lookup_field (&type, new_name);
4355 if (decl && decl != error_mark_node)
4356 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4358 if (!decl || decl == error_mark_node)
4360 type = class;
4361 decl = lookup_field (&type, name);
4364 /* If the field still hasn't been found, try the next enclosing context. */
4365 if (!decl && INNER_CLASS_TYPE_P (class))
4367 tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4368 decl = lookup_field_wrapper (outer_type, name);
4371 java_parser_context_restore_global ();
4372 return decl == error_mark_node ? NULL : decl;
4375 /* Find duplicate field within the same class declarations and report
4376 the error. Returns 1 if a duplicated field was found, 0
4377 otherwise. */
4379 static int
4380 duplicate_declaration_error_p (tree new_field_name, tree new_type, tree cl)
4382 /* This might be modified to work with method decl as well */
4383 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4384 if (decl)
4386 char *t1 = xstrdup (purify_type_name
4387 ((TREE_CODE (new_type) == POINTER_TYPE
4388 && TREE_TYPE (new_type) == NULL_TREE) ?
4389 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4390 lang_printable_name (new_type, 1)));
4391 /* The type may not have been completed by the time we report
4392 the error */
4393 char *t2 = xstrdup (purify_type_name
4394 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4395 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4396 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4397 lang_printable_name (TREE_TYPE (decl), 1)));
4398 parse_error_context
4399 (cl, "Duplicate variable declaration: %<%s %s%> was %<%s %s%> (%s:%d)",
4400 t1, IDENTIFIER_POINTER (new_field_name),
4401 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4402 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4403 free (t1);
4404 free (t2);
4405 return 1;
4407 return 0;
4410 /* Field registration routine. If TYPE doesn't exist, field
4411 declarations are linked to the undefined TYPE dependency list, to
4412 be later resolved in java_complete_class () */
4414 static void
4415 register_fields (int flags, tree type, tree variable_list)
4417 tree current, saved_type;
4418 tree class_type = NULL_TREE;
4419 location_t saved_location = input_location;
4420 int must_chain = 0;
4421 tree wfl = NULL_TREE;
4423 if (GET_CPC ())
4424 class_type = TREE_TYPE (GET_CPC ());
4426 if (!class_type || class_type == error_mark_node)
4427 return;
4429 /* If we're adding fields to interfaces, those fields are public,
4430 static, final */
4431 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4433 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4434 flags, ACC_PUBLIC, "interface field(s)");
4435 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4436 flags, ACC_STATIC, "interface field(s)");
4437 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4438 flags, ACC_FINAL, "interface field(s)");
4439 check_modifiers ("Illegal interface member modifier %qs", flags,
4440 INTERFACE_FIELD_MODIFIERS);
4441 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4444 /* Obtain a suitable type for resolution, if necessary */
4445 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4447 /* If TYPE is fully resolved and we don't have a reference, make one */
4448 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4450 for (current = variable_list, saved_type = type; current;
4451 current = TREE_CHAIN (current), type = saved_type)
4453 tree real_type;
4454 tree field_decl;
4455 tree cl = TREE_PURPOSE (current);
4456 tree init = TREE_VALUE (current);
4457 tree current_name = EXPR_WFL_NODE (cl);
4459 /* Can't declare non-final static fields in inner classes */
4460 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4461 && !(flags & ACC_FINAL))
4462 parse_error_context
4463 (cl, "Field %qs can't be static in inner class %qs unless it is final",
4464 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4465 lang_printable_name (class_type, 0));
4467 /* Process NAME, as it may specify extra dimension(s) for it */
4468 type = build_array_from_name (type, wfl, current_name, &current_name);
4470 /* Type adjustment. We may have just readjusted TYPE because
4471 the variable specified more dimensions. Make sure we have
4472 a reference if we can and don't have one already. Also
4473 change the name if we have an init. */
4474 if (type != saved_type)
4476 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4477 if (init)
4478 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4481 real_type = GET_REAL_TYPE (type);
4482 /* Check for redeclarations */
4483 if (duplicate_declaration_error_p (current_name, real_type, cl))
4484 continue;
4486 /* Set input_line to the line the field was found and create a
4487 declaration for it. Eventually sets the @deprecated tag flag. */
4488 #ifdef USE_MAPPED_LOCATION
4489 input_location = EXPR_LOCATION (cl);
4490 #else
4491 input_line = EXPR_WFL_LINENO (cl);
4492 #endif
4493 field_decl = add_field (class_type, current_name, real_type, flags);
4494 CHECK_DEPRECATED_NO_RESET (field_decl);
4496 /* If the field denotes a final instance variable, then we
4497 allocate a LANG_DECL_SPECIFIC part to keep track of its
4498 initialization. We also mark whether the field was
4499 initialized upon its declaration. We don't do that if the
4500 created field is an alias to a final local. */
4501 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4503 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4504 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4507 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4508 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4509 hide parameters to this inner class finit$ and
4510 constructors. It also means that the field isn't final per
4511 say. */
4512 if (ARG_FINAL_P (current))
4514 FIELD_LOCAL_ALIAS (field_decl) = 1;
4515 FIELD_FINAL (field_decl) = 0;
4518 /* Check if we must chain. */
4519 if (must_chain)
4520 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4522 /* If we have an initialization value tied to the field */
4523 if (init)
4525 /* The field is declared static */
4526 if (flags & ACC_STATIC)
4528 /* We include the field and its initialization part into
4529 a list used to generate <clinit>. After <clinit> is
4530 walked, field initializations will be processed and
4531 fields initialized with known constants will be taken
4532 out of <clinit> and have their DECL_INITIAL set
4533 appropriately. */
4534 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4535 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4536 if (TREE_OPERAND (init, 1)
4537 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4538 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4540 /* A non-static field declared with an immediate initialization is
4541 to be initialized in <init>, if any. This field is remembered
4542 to be processed at the time of the generation of <init>. */
4543 else
4545 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4546 SET_CPC_INITIALIZER_STMT (ctxp, init);
4548 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4549 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4553 CLEAR_DEPRECATED;
4554 input_location = saved_location;
4557 /* Generate finit$, using the list of initialized fields to populate
4558 its body. finit$'s parameter(s) list is adjusted to include the
4559 one(s) used to initialized the field(s) caching outer context
4560 local(s). */
4562 static tree
4563 generate_finit (tree class_type)
4565 int count = 0;
4566 tree list = TYPE_FINIT_STMT_LIST (class_type);
4567 tree mdecl, current, parms;
4569 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4570 class_type, NULL_TREE,
4571 &count);
4572 CRAFTED_PARAM_LIST_FIXUP (parms);
4573 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4574 finit_identifier_node, parms);
4575 fix_method_argument_names (parms, mdecl);
4576 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4577 mdecl, NULL_TREE);
4578 DECL_FUNCTION_NAP (mdecl) = count;
4579 start_artificial_method_body (mdecl);
4581 for (current = list; current; current = TREE_CHAIN (current))
4582 java_method_add_stmt (mdecl,
4583 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4584 current));
4585 end_artificial_method_body (mdecl);
4586 return mdecl;
4589 /* Generate a function to run the instance initialization code. The
4590 private method is called `instinit$'. Unless we're dealing with an
4591 anonymous class, we determine whether all ctors of CLASS_TYPE
4592 declare a checked exception in their `throws' clause in order to
4593 see whether it's necessary to encapsulate the instance initializer
4594 statements in a try/catch/rethrow sequence. */
4596 static tree
4597 generate_instinit (tree class_type)
4599 tree current;
4600 tree compound = NULL_TREE;
4601 tree parms = tree_cons (this_identifier_node,
4602 build_pointer_type (class_type), end_params_node);
4603 tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4604 void_type_node,
4605 instinit_identifier_node, parms);
4607 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4608 mdecl, NULL_TREE);
4610 /* Gather all the statements in a compound */
4611 for (current = TYPE_II_STMT_LIST (class_type);
4612 current; current = TREE_CHAIN (current))
4613 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4615 /* We need to encapsulate COMPOUND by a try/catch statement to
4616 rethrow exceptions that might occur in the instance initializer.
4617 We do that only if all ctors of CLASS_TYPE are set to catch a
4618 checked exception. This doesn't apply to anonymous classes (since
4619 they don't have declared ctors.) */
4620 if (!ANONYMOUS_CLASS_P (class_type) &&
4621 ctors_unchecked_throws_clause_p (class_type))
4623 compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4624 build1 (THROW_EXPR, NULL_TREE,
4625 build_wfl_node (wpv_id)));
4626 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4627 exception_type_node);
4630 start_artificial_method_body (mdecl);
4631 java_method_add_stmt (mdecl, compound);
4632 end_artificial_method_body (mdecl);
4634 return mdecl;
4637 /* FIXME */
4638 static tree
4639 build_instinit_invocation (tree class_type)
4641 tree to_return = NULL_TREE;
4643 if (TYPE_II_STMT_LIST (class_type))
4645 tree parm = build_tree_list (NULL_TREE,
4646 build_wfl_node (this_identifier_node));
4647 to_return =
4648 build_method_invocation (build_wfl_node (instinit_identifier_node),
4649 parm);
4651 return to_return;
4654 /* Shared across method_declarator and method_header to remember the
4655 patch stage that was reached during the declaration of the method.
4656 A method DECL is built differently is there is no patch
4657 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4658 pending on the currently defined method. */
4660 static int patch_stage;
4662 /* Check the method declaration and add the method to its current
4663 class. If the argument list is known to contain incomplete types,
4664 the method is partially added and the registration will be resume
4665 once the method arguments resolved. If TYPE is NULL, we're dealing
4666 with a constructor. */
4668 static tree
4669 method_header (int flags, tree type, tree mdecl, tree throws)
4671 tree type_wfl = NULL_TREE;
4672 tree meth_name = NULL_TREE;
4673 tree current, orig_arg, this_class = NULL;
4674 tree id, meth;
4675 location_t saved_location;
4676 int constructor_ok = 0, must_chain;
4677 int count;
4679 if (mdecl == error_mark_node)
4680 return error_mark_node;
4681 meth = TREE_VALUE (mdecl);
4682 id = TREE_PURPOSE (mdecl);
4684 check_modifiers_consistency (flags);
4686 if (GET_CPC ())
4687 this_class = TREE_TYPE (GET_CPC ());
4689 if (!this_class || this_class == error_mark_node)
4690 return NULL_TREE;
4692 /* There are some forbidden modifiers for an abstract method and its
4693 class must be abstract as well. */
4694 if (type && (flags & ACC_ABSTRACT))
4696 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4697 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4698 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4699 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4700 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4701 ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4702 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4703 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4704 parse_error_context
4705 (id,
4706 "Class %qs must be declared abstract to define abstract method %qs",
4707 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4708 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4711 /* A native method can't be strictfp. */
4712 if ((flags & ACC_NATIVE) && (flags & ACC_STRICT))
4713 parse_error_context (id, "native method %qs can't be strictfp",
4714 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4715 /* No such thing as a transient or volatile method. */
4716 if ((flags & ACC_TRANSIENT))
4717 parse_error_context (id, "method %qs can't be transient",
4718 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4719 if ((flags & ACC_VOLATILE))
4720 parse_error_context (id, "method %qs can't be volatile",
4721 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4723 /* Things to be checked when declaring a constructor */
4724 if (!type)
4726 int ec = java_error_count;
4727 /* 8.6: Constructor declarations: we might be trying to define a
4728 method without specifying a return type. */
4729 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4730 parse_error_context
4731 (id, "Invalid method declaration, return type required");
4732 /* 8.6.3: Constructor modifiers */
4733 else
4735 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4736 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4737 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4738 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4739 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4740 JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4742 /* If we found error here, we don't consider it's OK to tread
4743 the method definition as a constructor, for the rest of this
4744 function */
4745 if (ec == java_error_count)
4746 constructor_ok = 1;
4749 /* Method declared within the scope of an interface are implicitly
4750 abstract and public. Conflicts with other erroneously provided
4751 modifiers are checked right after. */
4753 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4755 /* If FLAGS isn't set because of a modifier, turn the
4756 corresponding modifier WFL to NULL so we issue a warning on
4757 the obsolete use of the modifier */
4758 if (!(flags & ACC_PUBLIC))
4759 MODIFIER_WFL (PUBLIC_TK) = NULL;
4760 if (!(flags & ACC_ABSTRACT))
4761 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4762 flags |= ACC_PUBLIC;
4763 flags |= ACC_ABSTRACT;
4766 /* Inner class can't declare static methods */
4767 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4769 parse_error_context
4770 (id, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
4771 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4772 lang_printable_name (this_class, 0));
4775 /* Modifiers context reset moved up, so abstract method declaration
4776 modifiers can be later checked. */
4778 /* Set constructor returned type to void and method name to <init>,
4779 unless we found an error identifier the constructor (in which
4780 case we retain the original name) */
4781 if (!type)
4783 type = void_type_node;
4784 if (constructor_ok)
4785 meth_name = init_identifier_node;
4787 else
4788 meth_name = EXPR_WFL_NODE (id);
4790 /* Do the returned type resolution and registration if necessary */
4791 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4793 if (meth_name)
4794 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4795 EXPR_WFL_NODE (id) = meth_name;
4796 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4798 if (must_chain)
4800 patch_stage = JDEP_METHOD_RETURN;
4801 register_incomplete_type (patch_stage, type_wfl, id, type);
4802 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4804 else
4805 TREE_TYPE (meth) = type;
4807 saved_location = input_location;
4808 /* When defining an abstract or interface method, the curly
4809 bracket at level 1 doesn't exist because there is no function
4810 body */
4811 #ifdef USE_MAPPED_LOCATION
4812 input_location = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4813 EXPR_LOCATION (id));
4814 #else
4815 input_line = (ctxp->first_ccb_indent1 ? (int) ctxp->first_ccb_indent1 :
4816 EXPR_WFL_LINENO (id));
4817 #endif
4819 /* Remember the original argument list */
4820 orig_arg = TYPE_ARG_TYPES (meth);
4822 if (patch_stage) /* includes ret type and/or all args */
4824 jdep *jdep;
4825 meth = add_method_1 (this_class, flags, meth_name, meth);
4826 /* Patch for the return type */
4827 if (patch_stage == JDEP_METHOD_RETURN)
4829 jdep = CLASSD_LAST (ctxp->classd_list);
4830 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4832 /* This is the stop JDEP. METH allows the function's signature
4833 to be computed. */
4834 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4836 else
4837 meth = add_method (this_class, flags, meth_name,
4838 build_java_signature (meth));
4840 /* Remember final parameters */
4841 MARK_FINAL_PARMS (meth, orig_arg);
4843 /* Fix the method argument list so we have the argument name
4844 information */
4845 fix_method_argument_names (orig_arg, meth);
4847 /* Register the parameter number and re-install the current line
4848 number */
4849 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4850 input_location = saved_location;
4852 /* Register exception specified by the `throws' keyword for
4853 resolution and set the method decl appropriate field to the list.
4854 Note: the grammar ensures that what we get here are class
4855 types. */
4856 if (throws)
4858 throws = nreverse (throws);
4859 for (current = throws; current; current = TREE_CHAIN (current))
4861 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4862 NULL_TREE, NULL_TREE);
4863 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4864 &TREE_VALUE (current);
4866 DECL_FUNCTION_THROWS (meth) = throws;
4869 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4870 DECL_FUNCTION_WFL (meth) = id;
4872 /* Set the flag if we correctly processed a constructor */
4873 if (constructor_ok)
4875 DECL_CONSTRUCTOR_P (meth) = 1;
4876 /* Compute and store the number of artificial parameters declared
4877 for this constructor */
4878 for (count = 0, current = TYPE_FIELDS (this_class); current;
4879 current = TREE_CHAIN (current))
4880 if (FIELD_LOCAL_ALIAS (current))
4881 count++;
4882 DECL_FUNCTION_NAP (meth) = count;
4885 /* Eventually set the @deprecated tag flag */
4886 CHECK_DEPRECATED (meth);
4888 return meth;
4891 static void
4892 fix_method_argument_names (tree orig_arg, tree meth)
4894 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4895 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4897 TREE_PURPOSE (arg) = this_identifier_node;
4898 arg = TREE_CHAIN (arg);
4900 while (orig_arg != end_params_node)
4902 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4903 orig_arg = TREE_CHAIN (orig_arg);
4904 arg = TREE_CHAIN (arg);
4908 /* Complete the method declaration with METHOD_BODY. */
4910 static void
4911 finish_method_declaration (tree method_body)
4913 int flags;
4915 if (!current_function_decl)
4916 return;
4918 flags = get_access_flags_from_decl (current_function_decl);
4920 /* 8.4.5 Method Body */
4921 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4923 tree name = DECL_NAME (current_function_decl);
4924 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4925 "%s method %qs can't have a body defined",
4926 (METHOD_NATIVE (current_function_decl) ?
4927 "Native" : "Abstract"),
4928 IDENTIFIER_POINTER (name));
4929 method_body = NULL_TREE;
4931 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4933 tree name = DECL_NAME (current_function_decl);
4934 parse_error_context
4935 (DECL_FUNCTION_WFL (current_function_decl),
4936 "Non native and non abstract method %qs must have a body defined",
4937 IDENTIFIER_POINTER (name));
4938 method_body = NULL_TREE;
4941 if (flag_emit_class_files && method_body
4942 && TREE_CODE (method_body) == NOP_EXPR
4943 && TREE_TYPE (current_function_decl)
4944 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4945 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4947 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4948 maybe_absorb_scoping_blocks ();
4949 /* Exit function's body */
4950 exit_block ();
4951 /* Merge last line of the function with first line, directly in the
4952 function decl. It will be used to emit correct debug info. */
4953 DECL_FUNCTION_LAST_LINE (current_function_decl) = ctxp->last_ccb_indent1;
4955 /* Since function's argument's list are shared, reset the
4956 ARG_FINAL_P parameter that might have been set on some of this
4957 function parameters. */
4958 UNMARK_FINAL_PARMS (current_function_decl);
4960 /* So we don't have an irrelevant function declaration context for
4961 the next static block we'll see. */
4962 current_function_decl = NULL_TREE;
4965 /* Build a an error message for constructor circularity errors. */
4967 static char *
4968 constructor_circularity_msg (tree from, tree to)
4970 static char string [4096];
4971 char *t = xstrdup (lang_printable_name (from, 2));
4972 sprintf (string, "'%s' invokes '%s'", t, lang_printable_name (to, 2));
4973 free (t);
4974 return string;
4977 /* Verify a circular call to METH. Return 1 if an error is found, 0
4978 otherwise. */
4980 static GTY(()) tree vcc_list;
4981 static int
4982 verify_constructor_circularity (tree meth, tree current)
4984 tree c;
4986 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4988 if (TREE_VALUE (c) == meth)
4990 char *t;
4991 if (vcc_list)
4993 tree liste;
4994 vcc_list = nreverse (vcc_list);
4995 for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
4997 parse_error_context
4998 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4999 constructor_circularity_msg
5000 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
5001 java_error_count--;
5004 t = xstrdup (lang_printable_name (meth, 2));
5005 parse_error_context (TREE_PURPOSE (c),
5006 "%s: recursive invocation of constructor %qs",
5007 constructor_circularity_msg (current, meth), t);
5008 free (t);
5009 vcc_list = NULL_TREE;
5010 return 1;
5013 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5015 vcc_list = tree_cons (c, current, vcc_list);
5016 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
5017 return 1;
5018 vcc_list = TREE_CHAIN (vcc_list);
5020 return 0;
5023 /* Check modifiers that can be declared but exclusively */
5025 static void
5026 check_modifiers_consistency (int flags)
5028 int acc_count = 0;
5029 tree cl = NULL_TREE;
5031 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
5032 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
5033 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
5034 if (acc_count > 1)
5035 parse_error_context
5036 (cl, "Inconsistent member declaration. At most one of %<public%>, %<private%>, or %<protected%> may be specified");
5038 acc_count = 0;
5039 cl = NULL_TREE;
5040 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
5041 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
5042 if (acc_count > 1)
5043 parse_error_context (cl,
5044 "Inconsistent member declaration. At most one of %<final%> or %<volatile%> may be specified");
5047 /* Check the methode header METH for abstract specifics features */
5049 static void
5050 check_abstract_method_header (tree meth)
5052 int flags = get_access_flags_from_decl (meth);
5054 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
5055 ACC_ABSTRACT, "abstract method",
5056 IDENTIFIER_POINTER (DECL_NAME (meth)));
5057 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
5058 ACC_PUBLIC, "abstract method",
5059 IDENTIFIER_POINTER (DECL_NAME (meth)));
5061 check_modifiers ("Illegal modifier %qs for interface method",
5062 flags, INTERFACE_METHOD_MODIFIERS);
5065 /* Create a FUNCTION_TYPE node and start augmenting it with the
5066 declared function arguments. Arguments type that can't be resolved
5067 are left as they are, but the returned node is marked as containing
5068 incomplete types. */
5070 static tree
5071 method_declarator (tree id, tree list)
5073 tree arg_types = NULL_TREE, current, node;
5074 tree meth = make_node (FUNCTION_TYPE);
5075 jdep *jdep;
5077 patch_stage = JDEP_NO_PATCH;
5079 if (GET_CPC () == error_mark_node)
5080 return error_mark_node;
5082 /* If we're dealing with an inner class constructor, we hide the
5083 this$<n> decl in the name field of its parameter declaration. We
5084 also might have to hide the outer context local alias
5085 initializers. Not done when the class is a toplevel class. */
5086 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
5087 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
5089 tree aliases_list, type, thisn;
5090 /* First the aliases, linked to the regular parameters */
5091 aliases_list =
5092 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
5093 TREE_TYPE (GET_CPC ()),
5094 NULL_TREE, NULL);
5095 list = chainon (nreverse (aliases_list), list);
5097 /* Then this$<n> */
5098 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
5099 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
5100 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
5101 list);
5104 for (current = list; current; current = TREE_CHAIN (current))
5106 int must_chain = 0;
5107 tree wfl_name = TREE_PURPOSE (current);
5108 tree type = TREE_VALUE (current);
5109 tree name = EXPR_WFL_NODE (wfl_name);
5110 tree already, arg_node;
5111 tree type_wfl = NULL_TREE;
5112 tree real_type;
5114 /* Obtain a suitable type for resolution, if necessary */
5115 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5117 /* Process NAME, as it may specify extra dimension(s) for it */
5118 type = build_array_from_name (type, type_wfl, name, &name);
5119 EXPR_WFL_NODE (wfl_name) = name;
5121 real_type = GET_REAL_TYPE (type);
5122 if (TREE_CODE (real_type) == RECORD_TYPE)
5124 real_type = promote_type (real_type);
5125 if (TREE_CODE (type) == TREE_LIST)
5126 TREE_PURPOSE (type) = real_type;
5129 /* Check redefinition */
5130 for (already = arg_types; already; already = TREE_CHAIN (already))
5131 if (TREE_PURPOSE (already) == name)
5133 parse_error_context
5134 (wfl_name, "Variable %qs is used more than once in the argument list of method %qs",
5135 IDENTIFIER_POINTER (name),
5136 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5137 break;
5140 /* If we've an incomplete argument type, we know there is a location
5141 to patch when the type get resolved, later. */
5142 jdep = NULL;
5143 if (must_chain)
5145 patch_stage = JDEP_METHOD;
5146 type = register_incomplete_type (patch_stage,
5147 type_wfl, wfl_name, type);
5148 jdep = CLASSD_LAST (ctxp->classd_list);
5149 JDEP_MISC (jdep) = id;
5152 /* The argument node: a name and a (possibly) incomplete type. */
5153 arg_node = build_tree_list (name, real_type);
5154 /* Remember arguments declared final. */
5155 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5157 if (jdep)
5158 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5159 TREE_CHAIN (arg_node) = arg_types;
5160 arg_types = arg_node;
5162 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5163 node = build_tree_list (id, meth);
5164 return node;
5167 static int
5168 unresolved_type_p (tree wfl, tree *returned)
5170 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5172 if (returned)
5174 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5175 if (decl && current_class && (decl == TYPE_NAME (current_class)))
5176 *returned = TREE_TYPE (decl);
5177 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5178 *returned = TREE_TYPE (GET_CPC ());
5179 else
5180 *returned = NULL_TREE;
5182 return 1;
5184 if (returned)
5185 *returned = wfl;
5186 return 0;
5189 /* From NAME, build a qualified identifier node using the
5190 qualification from the current package definition. */
5192 static tree
5193 parser_qualified_classname (tree name)
5195 tree nested_class_name;
5197 if ((nested_class_name = maybe_make_nested_class_name (name)))
5198 return nested_class_name;
5200 if (ctxp->package)
5201 return merge_qualified_name (ctxp->package, name);
5202 else
5203 return name;
5206 /* Called once the type a interface extends is resolved. Returns 0 if
5207 everything is OK. */
5209 static int
5210 parser_check_super_interface (tree super_decl, tree this_decl, tree this_wfl)
5212 tree super_type = TREE_TYPE (super_decl);
5214 /* Has to be an interface */
5215 if (!CLASS_INTERFACE (super_decl))
5217 parse_error_context
5218 (this_wfl, "%s %qs can't implement/extend %s %qs",
5219 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5220 "Interface" : "Class"),
5221 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5222 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5223 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5224 return 1;
5227 /* Check top-level interface access. Inner classes are subject to member
5228 access rules (6.6.1). */
5229 if (! INNER_CLASS_P (super_type)
5230 && check_pkg_class_access (DECL_NAME (super_decl),
5231 NULL_TREE, true, this_decl))
5232 return 1;
5234 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5235 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5236 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5237 return 0;
5240 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5241 0 if everything is OK. */
5243 static int
5244 parser_check_super (tree super_decl, tree this_decl, tree wfl)
5246 tree super_type = TREE_TYPE (super_decl);
5248 /* SUPER should be a CLASS (neither an array nor an interface) */
5249 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5251 parse_error_context
5252 (wfl, "Class %qs can't subclass %s %qs",
5253 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5254 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5255 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5256 return 1;
5259 if (CLASS_FINAL (TYPE_NAME (super_type)))
5261 parse_error_context (wfl, "Can't subclass final classes: %s",
5262 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5263 return 1;
5266 /* Check top-level class scope. Inner classes are subject to member access
5267 rules (6.6.1). */
5268 if (! INNER_CLASS_P (super_type)
5269 && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true, NULL_TREE)))
5270 return 1;
5272 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5273 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5274 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5275 return 0;
5278 /* Create a new dependency list and link it (in a LIFO manner) to the
5279 CTXP list of type dependency list. */
5281 static void
5282 create_jdep_list (struct parser_ctxt *ctxp)
5284 jdeplist *new = xmalloc (sizeof (jdeplist));
5285 new->first = new->last = NULL;
5286 new->next = ctxp->classd_list;
5287 ctxp->classd_list = new;
5290 static jdeplist *
5291 reverse_jdep_list (struct parser_ctxt *ctxp)
5293 jdeplist *prev = NULL, *current, *next;
5294 for (current = ctxp->classd_list; current; current = next)
5296 next = current->next;
5297 current->next = prev;
5298 prev = current;
5300 return prev;
5303 /* Create a fake pointer based on the ID stored in
5304 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5305 registered again. */
5307 static tree
5308 obtain_incomplete_type (tree type_name)
5310 tree ptr = NULL_TREE, name;
5312 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5313 name = EXPR_WFL_NODE (type_name);
5314 else if (INCOMPLETE_TYPE_P (type_name))
5315 name = TYPE_NAME (type_name);
5316 else
5317 abort ();
5319 /* Workaround from build_pointer_type for incomplete types. */
5320 BUILD_PTR_FROM_NAME (ptr, name);
5321 TYPE_MODE (ptr) = ptr_mode;
5322 layout_type (ptr);
5324 return ptr;
5327 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5328 non NULL instead of computing a new fake type based on WFL. The new
5329 dependency is inserted in the current type dependency list, in FIFO
5330 manner. */
5332 static tree
5333 register_incomplete_type (int kind, tree wfl, tree decl, tree ptr)
5335 jdep *new = xmalloc (sizeof (jdep));
5337 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5338 ptr = obtain_incomplete_type (wfl);
5340 JDEP_KIND (new) = kind;
5341 JDEP_DECL (new) = decl;
5342 JDEP_TO_RESOLVE (new) = ptr;
5343 JDEP_WFL (new) = wfl;
5344 JDEP_CHAIN (new) = NULL;
5345 JDEP_MISC (new) = NULL_TREE;
5346 /* For some dependencies, set the enclosing class of the current
5347 class to be the enclosing context */
5348 if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS || kind == JDEP_SUPER)
5349 && GET_ENCLOSING_CPC ())
5350 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5351 else
5352 JDEP_ENCLOSING (new) = GET_CPC ();
5353 JDEP_GET_PATCH (new) = (tree *)NULL;
5355 JDEP_INSERT (ctxp->classd_list, new);
5357 return ptr;
5360 /* This checks for circular references with innerclasses. We start
5361 from SOURCE and should never reach TARGET. Extended/implemented
5362 types in SOURCE have their enclosing context checked not to reach
5363 TARGET. When the last enclosing context of SOURCE is reached, its
5364 extended/implemented types are also checked not to reach TARGET.
5365 In case of error, WFL of the offending type is returned; NULL_TREE
5366 otherwise. */
5368 static tree
5369 check_inner_circular_reference (tree source, tree target)
5371 tree base_binfo;
5372 tree ctx, cl;
5373 int i;
5375 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (source), i, base_binfo); i++)
5377 tree su;
5379 /* We can end up with a NULL_TREE or an incomplete type here if
5380 we encountered previous type resolution errors. It's safe to
5381 simply ignore these cases. */
5382 su = BINFO_TYPE (base_binfo);
5383 if (INCOMPLETE_TYPE_P (su))
5384 continue;
5386 if (inherits_from_p (su, target))
5387 return lookup_cl (TYPE_NAME (su));
5389 for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5391 /* An enclosing context shouldn't be TARGET */
5392 if (ctx == TYPE_NAME (target))
5393 return lookup_cl (TYPE_NAME (su));
5395 /* When we reach the enclosing last context, start a check
5396 on it, with the same target */
5397 if (! DECL_CONTEXT (ctx) &&
5398 (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5399 return cl;
5402 return NULL_TREE;
5405 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5406 offending type if a circularity is detected. NULL_TREE is returned
5407 otherwise. TYPE can be an interface or a class. */
5409 static tree
5410 check_circular_reference (tree type)
5412 tree base_binfo;
5413 int i;
5415 if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
5416 return NULL_TREE;
5418 if (! CLASS_INTERFACE (TYPE_NAME (type)))
5420 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5421 return lookup_cl (TYPE_NAME (type));
5422 return NULL_TREE;
5425 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo); i++)
5427 if (BINFO_TYPE (base_binfo) != object_type_node
5428 && interface_of_p (type, BINFO_TYPE (base_binfo)))
5429 return lookup_cl (TYPE_NAME (BINFO_TYPE (base_binfo)));
5431 return NULL_TREE;
5434 void
5435 java_check_circular_reference (void)
5437 tree current;
5438 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5440 tree type = TREE_TYPE (current);
5441 tree cl;
5443 cl = check_circular_reference (type);
5444 if (! cl)
5445 cl = check_inner_circular_reference (type, type);
5446 if (cl)
5447 parse_error_context (cl, "Cyclic class inheritance%s",
5448 (cyclic_inheritance_report ?
5449 cyclic_inheritance_report : ""));
5453 /* Augment the parameter list PARM with parameters crafted to
5454 initialize outer context locals aliases. Through ARTIFICIAL, a
5455 count is kept of the number of crafted parameters. MODE governs
5456 what eventually gets created: something suitable for a function
5457 creation or a function invocation, either the constructor or
5458 finit$. */
5460 static tree
5461 build_alias_initializer_parameter_list (int mode, tree class_type, tree parm,
5462 int *artificial)
5464 tree field;
5465 tree additional_parms = NULL_TREE;
5467 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5468 if (FIELD_LOCAL_ALIAS (field))
5470 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5471 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5472 tree mangled_id;
5474 switch (mode)
5476 case AIPL_FUNCTION_DECLARATION:
5477 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5478 &buffer [4]);
5479 purpose = build_wfl_node (mangled_id);
5480 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5481 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5482 else
5483 value = TREE_TYPE (field);
5484 break;
5486 case AIPL_FUNCTION_CREATION:
5487 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5488 &buffer [4]);
5489 value = TREE_TYPE (field);
5490 break;
5492 case AIPL_FUNCTION_FINIT_INVOCATION:
5493 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5494 &buffer [4]);
5495 /* Now, this is wrong. purpose should always be the NAME
5496 of something and value its matching value (decl, type,
5497 etc...) FIXME -- but there is a lot to fix. */
5499 /* When invoked for this kind of operation, we already
5500 know whether a field is used or not. */
5501 purpose = TREE_TYPE (field);
5502 value = build_wfl_node (mangled_id);
5503 break;
5505 case AIPL_FUNCTION_CTOR_INVOCATION:
5506 /* There are two case: the constructor invocation happens
5507 outside the local inner, in which case, locales from the outer
5508 context are directly used.
5510 Otherwise, we fold to using the alias directly. */
5511 if (class_type == current_class)
5512 value = field;
5513 else
5515 name = get_identifier (&buffer[4]);
5516 value = IDENTIFIER_LOCAL_VALUE (name);
5518 break;
5520 additional_parms = tree_cons (purpose, value, additional_parms);
5521 if (artificial)
5522 *artificial +=1;
5524 if (additional_parms)
5526 if (ANONYMOUS_CLASS_P (class_type)
5527 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5528 additional_parms = nreverse (additional_parms);
5529 parm = chainon (additional_parms, parm);
5532 return parm;
5535 /* Craft a constructor for CLASS_DECL -- what we should do when none
5536 where found. ARGS is non NULL when a special signature must be
5537 enforced. This is the case for anonymous classes. */
5539 static tree
5540 craft_constructor (tree class_decl, tree args)
5542 tree class_type = TREE_TYPE (class_decl);
5543 tree parm = NULL_TREE;
5544 /* Inherit access flags for the constructor from its enclosing class. */
5545 int valid_ctor_flags = ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE;
5546 int flags = (get_access_flags_from_decl (class_decl) & valid_ctor_flags);
5547 int i = 0, artificial = 0;
5548 tree decl, ctor_name;
5549 char buffer [80];
5551 ctor_name = init_identifier_node;
5553 /* If we're dealing with an inner class constructor, we hide the
5554 this$<n> decl in the name field of its parameter declaration. */
5555 if (PURE_INNER_CLASS_TYPE_P (class_type))
5557 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5558 parm = tree_cons (build_current_thisn (class_type),
5559 build_pointer_type (type), parm);
5561 /* Some more arguments to be hidden here. The values of the local
5562 variables of the outer context that the inner class needs to see. */
5563 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5564 class_type, parm,
5565 &artificial);
5568 /* Then if there are any args to be enforced, enforce them now */
5569 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5571 /* If we see a `void *', we need to change it to Object. */
5572 if (TREE_VALUE (args) == TREE_TYPE (null_pointer_node))
5573 TREE_VALUE (args) = object_ptr_type_node;
5575 sprintf (buffer, "parm%d", i++);
5576 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5579 CRAFTED_PARAM_LIST_FIXUP (parm);
5580 decl = create_artificial_method (class_type, flags, void_type_node,
5581 ctor_name, parm);
5582 fix_method_argument_names (parm, decl);
5583 /* Now, mark the artificial parameters. */
5584 DECL_FUNCTION_NAP (decl) = artificial;
5585 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5586 DECL_INLINE (decl) = 1;
5587 return decl;
5591 /* Fix the constructors. This will be called right after circular
5592 references have been checked. It is necessary to fix constructors
5593 early even if no code generation will take place for that class:
5594 some generated constructor might be required by the class whose
5595 compilation triggered this one to be simply loaded. */
5597 void
5598 java_fix_constructors (void)
5600 tree current;
5602 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5604 tree class_type = TREE_TYPE (current);
5605 int saw_ctor = 0;
5606 tree decl;
5608 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5609 continue;
5611 output_class = current_class = class_type;
5612 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5614 if (DECL_CONSTRUCTOR_P (decl))
5616 fix_constructors (decl);
5617 saw_ctor = 1;
5621 /* Anonymous class constructor can't be generated that early. */
5622 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5623 craft_constructor (current, NULL_TREE);
5627 /* safe_layout_class just makes sure that we can load a class without
5628 disrupting the current_class, input_file, input_line, etc, information
5629 about the class processed currently. */
5631 void
5632 safe_layout_class (tree class)
5634 tree save_current_class = current_class;
5635 location_t save_location = input_location;
5637 layout_class (class);
5639 current_class = save_current_class;
5640 input_location = save_location;
5643 static tree
5644 jdep_resolve_class (jdep *dep)
5646 tree decl;
5648 if (JDEP_RESOLVED_P (dep))
5649 decl = JDEP_RESOLVED_DECL (dep);
5650 else
5652 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5653 JDEP_DECL (dep), JDEP_WFL (dep));
5654 JDEP_RESOLVED (dep, decl);
5655 /* If there is no WFL, that's ok. We generate this warning
5656 elsewhere. */
5657 if (decl && JDEP_WFL (dep) != NULL_TREE)
5658 check_deprecation (JDEP_WFL (dep), decl);
5661 if (!decl)
5662 complete_class_report_errors (dep);
5663 else if (INNER_CLASS_DECL_P (decl))
5665 tree inner = TREE_TYPE (decl);
5666 if (! CLASS_LOADED_P (inner))
5668 safe_layout_class (inner);
5669 if (TYPE_SIZE (inner) == error_mark_node)
5670 TYPE_SIZE (inner) = NULL_TREE;
5672 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5674 return decl;
5677 /* Complete unsatisfied class declaration and their dependencies */
5679 void
5680 java_complete_class (void)
5682 tree cclass;
5683 jdeplist *cclassd;
5684 int error_found;
5685 tree type;
5687 /* Process imports */
5688 process_imports ();
5690 /* Reverse things so we have the right order */
5691 ctxp->class_list = nreverse (ctxp->class_list);
5692 ctxp->classd_list = reverse_jdep_list (ctxp);
5694 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5695 cclass && cclassd;
5696 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5698 jdep *dep;
5700 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5702 tree decl;
5703 if (!(decl = jdep_resolve_class (dep)))
5704 continue;
5706 /* Now it's time to patch */
5707 switch (JDEP_KIND (dep))
5709 case JDEP_SUPER:
5710 /* Simply patch super */
5711 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5712 continue;
5713 BINFO_TYPE (BINFO_BASE_BINFO
5714 (TYPE_BINFO (TREE_TYPE (JDEP_DECL (dep))), 0))
5715 = TREE_TYPE (decl);
5716 break;
5718 case JDEP_FIELD:
5720 /* We do part of the job done in add_field */
5721 tree field_decl = JDEP_DECL (dep);
5722 tree field_type = TREE_TYPE (decl);
5723 if (TREE_CODE (field_type) == RECORD_TYPE)
5724 field_type = promote_type (field_type);
5725 TREE_TYPE (field_decl) = field_type;
5726 DECL_ALIGN (field_decl) = 0;
5727 DECL_USER_ALIGN (field_decl) = 0;
5728 layout_decl (field_decl, 0);
5729 SOURCE_FRONTEND_DEBUG
5730 (("Completed field/var decl '%s' with '%s'",
5731 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5732 IDENTIFIER_POINTER (DECL_NAME (decl))));
5733 break;
5735 case JDEP_METHOD: /* We start patching a method */
5736 case JDEP_METHOD_RETURN:
5737 error_found = 0;
5738 while (1)
5740 if (decl)
5742 type = TREE_TYPE(decl);
5743 if (TREE_CODE (type) == RECORD_TYPE)
5744 type = promote_type (type);
5745 JDEP_APPLY_PATCH (dep, type);
5746 SOURCE_FRONTEND_DEBUG
5747 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5748 "Completing fct '%s' with ret type '%s'":
5749 "Completing arg '%s' with type '%s'"),
5750 IDENTIFIER_POINTER (EXPR_WFL_NODE
5751 (JDEP_DECL_WFL (dep))),
5752 IDENTIFIER_POINTER (DECL_NAME (decl))));
5754 else
5755 error_found = 1;
5756 dep = JDEP_CHAIN (dep);
5757 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5758 break;
5759 else
5760 decl = jdep_resolve_class (dep);
5762 if (!error_found)
5764 tree mdecl = JDEP_DECL (dep), signature;
5765 /* Recompute and reset the signature, check first that
5766 all types are now defined. If they're not,
5767 don't build the signature. */
5768 if (check_method_types_complete (mdecl))
5770 signature = build_java_signature (TREE_TYPE (mdecl));
5771 set_java_signature (TREE_TYPE (mdecl), signature);
5774 else
5775 continue;
5776 break;
5778 case JDEP_INTERFACE:
5779 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5780 JDEP_WFL (dep)))
5781 continue;
5782 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5783 break;
5785 case JDEP_PARM:
5786 case JDEP_VARIABLE:
5787 type = TREE_TYPE(decl);
5788 if (TREE_CODE (type) == RECORD_TYPE)
5789 type = promote_type (type);
5790 JDEP_APPLY_PATCH (dep, type);
5791 break;
5793 case JDEP_TYPE:
5794 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5795 SOURCE_FRONTEND_DEBUG
5796 (("Completing a random type dependency on a '%s' node",
5797 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5798 break;
5800 case JDEP_EXCEPTION:
5801 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5802 SOURCE_FRONTEND_DEBUG
5803 (("Completing '%s' 'throws' argument node",
5804 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5805 break;
5807 case JDEP_ANONYMOUS:
5808 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5809 break;
5811 default:
5812 abort ();
5816 return;
5819 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5820 array. */
5822 static tree
5823 resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5825 tree tname = TYPE_NAME (class_type);
5826 tree resolved_type = TREE_TYPE (class_type);
5827 int array_dims = 0;
5828 tree resolved_type_decl;
5830 if (resolved_type != NULL_TREE)
5832 tree resolved_type_decl = TYPE_NAME (resolved_type);
5833 if (resolved_type_decl == NULL_TREE
5834 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5836 resolved_type_decl = build_decl (TYPE_DECL,
5837 TYPE_NAME (class_type),
5838 resolved_type);
5840 return resolved_type_decl;
5843 /* 1- Check to see if we have an array. If true, find what we really
5844 want to resolve */
5845 if ((array_dims = build_type_name_from_array_name (tname,
5846 &TYPE_NAME (class_type))))
5847 WFL_STRIP_BRACKET (cl, cl);
5849 /* 2- Resolve the bare type */
5850 if (!(resolved_type_decl = do_resolve_class (enclosing, NULL_TREE, class_type,
5851 decl, cl)))
5852 return NULL_TREE;
5853 resolved_type = TREE_TYPE (resolved_type_decl);
5855 /* 3- If we have an array, reconstruct the array down to its nesting */
5856 if (array_dims)
5858 for (; array_dims; array_dims--)
5859 resolved_type = build_java_array_type (resolved_type, -1);
5860 resolved_type_decl = TYPE_NAME (resolved_type);
5862 TREE_TYPE (class_type) = resolved_type;
5863 return resolved_type_decl;
5866 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5867 are used to report error messages; CL must either be NULL_TREE or a
5868 WFL wrapping a class. Do not try to replace TYPE_NAME (class_type)
5869 by a variable, since it is changed by find_in_imports{_on_demand}
5870 and (but it doesn't really matter) qualify_and_find. */
5872 tree
5873 do_resolve_class (tree enclosing, tree import_type, tree class_type, tree decl,
5874 tree cl)
5876 tree new_class_decl = NULL_TREE;
5877 tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5878 tree candidate = NULL_TREE;
5879 tree decl_result;
5881 if (QUALIFIED_P (TYPE_NAME (class_type)))
5883 /* If the type name is of the form `Q . Id', then Q is either a
5884 package name or a class name. First we try to find Q as a
5885 class and then treat Id as a member type. If we can't find Q
5886 as a class then we fall through. */
5887 tree q, left, left_type, right;
5888 if (split_qualified_name (&left, &right, TYPE_NAME (class_type)) == 0)
5890 BUILD_PTR_FROM_NAME (left_type, left);
5891 q = do_resolve_class (enclosing, import_type, left_type, decl, cl);
5892 if (q)
5894 enclosing = q;
5895 saved_enclosing_type = TREE_TYPE (q);
5896 BUILD_PTR_FROM_NAME (class_type, right);
5901 if (enclosing)
5903 tree context = enclosing;
5905 /* 0- Search in the current class as an inner class.
5906 Maybe some code here should be added to load the class or
5907 something, at least if the class isn't an inner class and ended
5908 being loaded from class file. FIXME. */
5909 while (enclosing)
5911 new_class_decl = resolve_inner_class (context, cl, enclosing, class_type);
5913 if (new_class_decl)
5915 if (inner_class_accessible (new_class_decl, context))
5916 break;
5917 else
5918 if (candidate == NULL_TREE)
5919 candidate = new_class_decl;
5920 new_class_decl = NULL_TREE;
5923 /* Now that we've looked through all superclasses, try the enclosing
5924 context. */
5925 enclosing = DECL_CONTEXT (enclosing);
5928 if (new_class_decl)
5929 return new_class_decl;
5932 /* 1- Check for the type in single imports. Look at enclosing classes and,
5933 if we're laying out a superclass, at the import list for the subclass.
5934 This will change TYPE_NAME() if something relevant is found. */
5935 if (import_type && TYPE_IMPORT_LIST (import_type))
5936 find_in_imports (import_type, class_type);
5937 find_in_imports (saved_enclosing_type, class_type);
5939 /* 2- And check for the type in the current compilation unit */
5940 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5942 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
5943 load_class (TYPE_NAME (class_type), 0);
5944 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5947 /* 3- Search according to the current package definition */
5948 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5950 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5951 TYPE_NAME (class_type))))
5952 return new_class_decl;
5955 /* 4- Check the import on demands. Don't allow bar.baz to be
5956 imported from foo.* */
5957 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5959 if (import_type
5960 && TYPE_IMPORT_DEMAND_LIST (import_type)
5961 && find_in_imports_on_demand (import_type, class_type))
5962 return NULL_TREE;
5963 if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5964 return NULL_TREE;
5967 /* If found in find_in_imports_on_demand, the type has already been
5968 loaded. */
5969 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5970 return new_class_decl;
5972 /* 5- Check another compilation unit that bears the name of type */
5973 load_class (TYPE_NAME (class_type), 0);
5975 if (!cl)
5976 cl = lookup_cl (decl);
5978 /* If we don't have a value for CL, then we're being called recursively.
5979 We can't check package access just yet, but it will be taken care of
5980 by the caller. */
5981 if (cl)
5983 if (check_pkg_class_access (TYPE_NAME (class_type), cl, true, NULL_TREE))
5984 return NULL_TREE;
5987 /* 6- Last call for a resolution */
5988 decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5990 /* The final lookup might have registered a.b.c into a.b$c If we
5991 failed at the first lookup, progressively change the name if
5992 applicable and use the matching DECL instead. */
5993 if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
5995 char *separator;
5996 tree name = TYPE_NAME (class_type);
5997 char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
5999 strcpy (namebuffer, IDENTIFIER_POINTER (name));
6001 do {
6003 /* Reach the last '.', and if applicable, replace it by a `$' and
6004 see if this exists as a type. */
6005 if ((separator = strrchr (namebuffer, '.')))
6007 *separator = '$';
6008 name = get_identifier (namebuffer);
6009 decl_result = IDENTIFIER_CLASS_VALUE (name);
6011 } while (!decl_result && separator);
6013 if (decl_result)
6014 return decl_result;
6015 else
6016 return candidate;
6019 static tree
6020 qualify_and_find (tree class_type, tree package, tree name)
6022 tree new_qualified = merge_qualified_name (package, name);
6023 tree new_class_decl;
6025 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
6026 load_class (new_qualified, 0);
6027 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
6029 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
6030 load_class (TREE_TYPE (new_class_decl), 0);
6031 TYPE_NAME (class_type) = new_qualified;
6032 return IDENTIFIER_CLASS_VALUE (new_qualified);
6034 return NULL_TREE;
6037 /* Resolve NAME and lay it out (if not done and if not the current
6038 parsed class). Return a decl node. This function is meant to be
6039 called when type resolution is necessary during the walk pass. */
6041 static tree
6042 resolve_and_layout (tree something, tree cl)
6044 tree decl, decl_type;
6046 /* Don't do that on the current class */
6047 if (something == current_class)
6048 return TYPE_NAME (current_class);
6050 /* Don't do anything for void and other primitive types */
6051 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6052 return NULL_TREE;
6054 /* Pointer types can be reall pointer types or fake pointers. When
6055 finding a real pointer, recheck for primitive types */
6056 if (TREE_CODE (something) == POINTER_TYPE)
6058 if (TREE_TYPE (something))
6060 something = TREE_TYPE (something);
6061 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6062 return NULL_TREE;
6064 else
6065 something = TYPE_NAME (something);
6068 /* Don't do anything for arrays of primitive types */
6069 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
6070 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
6071 return NULL_TREE;
6073 /* Something might be a WFL */
6074 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
6075 something = EXPR_WFL_NODE (something);
6077 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
6078 TYPE_DECL or a real TYPE */
6079 else if (TREE_CODE (something) != IDENTIFIER_NODE)
6080 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
6081 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
6083 if (!(decl = resolve_no_layout (something, cl)))
6084 return NULL_TREE;
6086 /* Resolve and layout if necessary */
6087 decl_type = TREE_TYPE (decl);
6088 layout_class_methods (decl_type);
6089 /* Check methods */
6090 if (CLASS_FROM_SOURCE_P (decl_type))
6091 java_check_methods (decl);
6092 /* Layout the type if necessary */
6093 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6094 safe_layout_class (decl_type);
6096 return decl;
6099 /* Resolve a class, returns its decl but doesn't perform any
6100 layout. The current parsing context is saved and restored */
6102 static tree
6103 resolve_no_layout (tree name, tree cl)
6105 tree ptr, decl;
6106 BUILD_PTR_FROM_NAME (ptr, name);
6107 java_parser_context_save_global ();
6108 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6109 java_parser_context_restore_global ();
6111 return decl;
6114 /* Called when reporting errors. Skip the '[]'s in a complex array
6115 type description that failed to be resolved. purify_type_name can't
6116 use an identifier tree. */
6118 static const char *
6119 purify_type_name (const char *name)
6121 int len = strlen (name);
6122 int bracket_found;
6124 STRING_STRIP_BRACKETS (name, len, bracket_found);
6125 if (bracket_found)
6127 char *stripped_name = xmemdup (name, len, len+1);
6128 stripped_name [len] = '\0';
6129 return stripped_name;
6131 return name;
6134 /* The type CURRENT refers to can't be found. We print error messages. */
6136 static void
6137 complete_class_report_errors (jdep *dep)
6139 const char *name;
6141 if (!JDEP_WFL (dep))
6142 return;
6144 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6145 switch (JDEP_KIND (dep))
6147 case JDEP_SUPER:
6148 parse_error_context
6149 (JDEP_WFL (dep), "Superclass %qs of class %qs not found",
6150 purify_type_name (name),
6151 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6152 break;
6153 case JDEP_FIELD:
6154 parse_error_context
6155 (JDEP_WFL (dep), "Type %qs not found in declaration of field %qs",
6156 purify_type_name (name),
6157 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6158 break;
6159 case JDEP_METHOD: /* Covers arguments */
6160 parse_error_context
6161 (JDEP_WFL (dep), "Type %qs not found in the declaration of the argument %qs of method %qs",
6162 purify_type_name (name),
6163 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6164 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6165 break;
6166 case JDEP_METHOD_RETURN: /* Covers return type */
6167 parse_error_context
6168 (JDEP_WFL (dep), "Type %qs not found in the declaration of the return type of method %qs",
6169 purify_type_name (name),
6170 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6171 break;
6172 case JDEP_INTERFACE:
6173 parse_error_context
6174 (JDEP_WFL (dep), "Superinterface %qs of %s %qs not found",
6175 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6176 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6177 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6178 break;
6179 case JDEP_VARIABLE:
6180 parse_error_context
6181 (JDEP_WFL (dep), "Type %qs not found in the declaration of the local variable %qs",
6182 purify_type_name (IDENTIFIER_POINTER
6183 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6184 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6185 break;
6186 case JDEP_EXCEPTION: /* As specified by `throws' */
6187 parse_error_context
6188 (JDEP_WFL (dep), "Class %qs not found in %<throws%>",
6189 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6190 break;
6191 default:
6192 /* Fix for -Wall. Just break doing nothing. The error will be
6193 caught later */
6194 break;
6198 /* Return a static string containing the DECL prototype string. If
6199 DECL is a constructor, use the class name instead of the form
6200 <init> */
6202 static const char *
6203 get_printable_method_name (tree decl)
6205 const char *to_return;
6206 tree name = NULL_TREE;
6208 if (DECL_CONSTRUCTOR_P (decl))
6210 name = DECL_NAME (decl);
6211 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6214 to_return = lang_printable_name (decl, 2);
6215 if (DECL_CONSTRUCTOR_P (decl))
6216 DECL_NAME (decl) = name;
6218 return to_return;
6221 /* Track method being redefined inside the same class. As a side
6222 effect, set DECL_NAME to an IDENTIFIER (prior entering this
6223 function it's a FWL, so we can track errors more accurately.) */
6225 static int
6226 check_method_redefinition (tree class, tree method)
6228 tree redef, sig;
6230 /* There's no need to verify <clinit> and finit$ and instinit$ */
6231 if (DECL_CLINIT_P (method)
6232 || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6233 return 0;
6235 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6236 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6238 if (redef == method)
6239 break;
6240 if (DECL_NAME (redef) == DECL_NAME (method)
6241 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6242 && !DECL_ARTIFICIAL (method))
6244 parse_error_context
6245 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration %qs",
6246 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6247 get_printable_method_name (redef));
6248 return 1;
6251 return 0;
6254 /* Return 1 if check went ok, 0 otherwise. */
6255 static int
6256 check_abstract_method_definitions (int do_interface, tree class_decl,
6257 tree type)
6259 tree class = TREE_TYPE (class_decl);
6260 tree method, end_type;
6261 int ok = 1;
6263 end_type = (do_interface ? object_type_node : type);
6264 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6266 tree other_super, other_method, method_sig, method_name;
6267 int found = 0;
6268 int end_type_reached = 0;
6270 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6271 continue;
6273 /* Now verify that somewhere in between TYPE and CLASS,
6274 abstract method METHOD gets a non abstract definition
6275 that is inherited by CLASS. */
6277 method_sig = build_java_signature (TREE_TYPE (method));
6278 method_name = DECL_NAME (method);
6279 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6280 method_name = EXPR_WFL_NODE (method_name);
6282 other_super = class;
6283 do {
6284 if (other_super == end_type)
6285 end_type_reached = 1;
6287 /* Method search */
6288 for (other_method = TYPE_METHODS (other_super); other_method;
6289 other_method = TREE_CHAIN (other_method))
6291 tree s = build_java_signature (TREE_TYPE (other_method));
6292 tree other_name = DECL_NAME (other_method);
6294 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6295 other_name = EXPR_WFL_NODE (other_name);
6296 if (!DECL_CLINIT_P (other_method)
6297 && !DECL_CONSTRUCTOR_P (other_method)
6298 && method_name == other_name
6299 && method_sig == s
6300 && !METHOD_ABSTRACT (other_method))
6302 found = 1;
6303 break;
6306 other_super = CLASSTYPE_SUPER (other_super);
6307 } while (!end_type_reached);
6309 /* Report that abstract METHOD didn't find an implementation
6310 that CLASS can use. */
6311 if (!found)
6313 char *t = xstrdup (lang_printable_name
6314 (TREE_TYPE (TREE_TYPE (method)), 0));
6315 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6317 parse_error_context
6318 (lookup_cl (class_decl),
6319 "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",
6320 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6321 t, lang_printable_name (method, 2),
6322 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6323 "interface" : "class"),
6324 IDENTIFIER_POINTER (ccn),
6325 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6326 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6327 ok = 0;
6328 free (t);
6332 if (ok && do_interface)
6334 /* Check for implemented interfaces. */
6335 int i;
6336 tree base_binfo;
6338 for (i = 1;
6339 ok && BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo);
6340 i++)
6341 ok = check_abstract_method_definitions (1, class_decl,
6342 BINFO_TYPE (base_binfo));
6345 return ok;
6348 /* Check that CLASS_DECL somehow implements all inherited abstract
6349 methods. */
6351 static void
6352 java_check_abstract_method_definitions (tree class_decl)
6354 tree class = TREE_TYPE (class_decl);
6355 tree super, base_binfo;
6356 int i;
6358 if (CLASS_ABSTRACT (class_decl))
6359 return;
6361 /* Check for inherited types */
6362 super = class;
6363 do {
6364 super = CLASSTYPE_SUPER (super);
6365 check_abstract_method_definitions (0, class_decl, super);
6366 } while (super != object_type_node);
6368 /* Check for implemented interfaces. */
6369 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6370 check_abstract_method_definitions (1, class_decl, BINFO_TYPE (base_binfo));
6373 /* Check all the types method DECL uses and return 1 if all of them
6374 are now complete, 0 otherwise. This is used to check whether its
6375 safe to build a method signature or not. */
6377 static int
6378 check_method_types_complete (tree decl)
6380 tree type = TREE_TYPE (decl);
6381 tree args;
6383 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6384 return 0;
6386 args = TYPE_ARG_TYPES (type);
6387 if (TREE_CODE (type) == METHOD_TYPE)
6388 args = TREE_CHAIN (args);
6389 for (; args != end_params_node; args = TREE_CHAIN (args))
6390 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6391 return 0;
6393 return 1;
6396 /* Visible interface to check methods contained in CLASS_DECL */
6398 void
6399 java_check_methods (tree class_decl)
6401 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6402 return;
6404 if (CLASS_INTERFACE (class_decl))
6405 java_check_abstract_methods (class_decl);
6406 else
6407 java_check_regular_methods (class_decl);
6409 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6412 /* Like not_accessible_p, but doesn't refer to the current class at
6413 all. */
6414 static bool
6415 hack_is_accessible_p (tree member, tree from_where)
6417 int flags = get_access_flags_from_decl (member);
6419 if (from_where == DECL_CONTEXT (member)
6420 || (flags & ACC_PUBLIC))
6421 return true;
6423 if ((flags & ACC_PROTECTED))
6425 if (inherits_from_p (from_where, DECL_CONTEXT (member)))
6426 return true;
6429 if ((flags & ACC_PRIVATE))
6430 return false;
6432 /* Package private, or protected. */
6433 return in_same_package (TYPE_NAME (from_where),
6434 TYPE_NAME (DECL_CONTEXT (member)));
6437 /* Check all the methods of CLASS_DECL. Methods are first completed
6438 then checked according to regular method existence rules. If no
6439 constructor for CLASS_DECL were encountered, then build its
6440 declaration. */
6441 static void
6442 java_check_regular_methods (tree class_decl)
6444 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6445 tree method;
6446 tree class = TREE_TYPE (class_decl);
6447 tree found = NULL_TREE;
6448 tree mthrows;
6450 /* It is not necessary to check methods defined in java.lang.Object */
6451 if (class == object_type_node)
6452 return;
6454 if (!TYPE_NVIRTUALS (class))
6455 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6457 /* Should take interfaces into account. FIXME */
6458 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6460 tree sig;
6461 tree method_wfl = DECL_FUNCTION_WFL (method);
6462 int aflags;
6464 /* Check for redefinitions */
6465 if (check_method_redefinition (class, method))
6466 continue;
6468 /* We verify things thrown by the method. They must inherit from
6469 java.lang.Throwable. */
6470 for (mthrows = DECL_FUNCTION_THROWS (method);
6471 mthrows; mthrows = TREE_CHAIN (mthrows))
6473 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6474 parse_error_context
6475 (TREE_PURPOSE (mthrows), "Class %qs in %<throws%> clause must be a subclass of class %<java.lang.Throwable%>",
6476 IDENTIFIER_POINTER
6477 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6480 /* If we see one constructor a mark so we don't generate the
6481 default one. Also skip other verifications: constructors
6482 can't be inherited hence hidden or overridden. */
6483 if (DECL_CONSTRUCTOR_P (method))
6485 saw_constructor = 1;
6486 continue;
6489 sig = build_java_argument_signature (TREE_TYPE (method));
6490 found = lookup_argument_method_generic (class, DECL_NAME (method), sig,
6491 SEARCH_SUPER | SEARCH_INTERFACE);
6493 /* Inner class can't declare static methods */
6494 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6496 char *t = xstrdup (lang_printable_name (class, 0));
6497 parse_error_context
6498 (method_wfl, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
6499 lang_printable_name (method, 2), t);
6500 free (t);
6503 /* Nothing overrides or it's a private method. */
6504 if (!found)
6505 continue;
6506 if (METHOD_PRIVATE (found))
6508 found = NULL_TREE;
6509 continue;
6512 /* If `found' is declared in an interface, make sure the
6513 modifier matches. */
6514 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6515 && clinit_identifier_node != DECL_NAME (found)
6516 && !METHOD_PUBLIC (method))
6518 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6519 parse_error_context (method_wfl, "Class %qs must override %qs with a public method in order to implement interface %qs",
6520 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6521 lang_printable_name (method, 2),
6522 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6525 /* Can't override a method with the same name and different return
6526 types. */
6527 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6529 char *t = xstrdup
6530 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6531 parse_error_context
6532 (method_wfl,
6533 "Method %qs was defined with return type %qs in class %qs",
6534 lang_printable_name (found, 2), t,
6535 IDENTIFIER_POINTER
6536 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6537 free (t);
6540 aflags = get_access_flags_from_decl (found);
6542 /* Can't override final. Can't override static. */
6543 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6545 /* Static *can* override static */
6546 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6547 continue;
6548 parse_error_context
6549 (method_wfl,
6550 "%s methods can't be overridden. Method %qs is %s in class %qs",
6551 (METHOD_FINAL (found) ? "Final" : "Static"),
6552 lang_printable_name (found, 2),
6553 (METHOD_FINAL (found) ? "final" : "static"),
6554 IDENTIFIER_POINTER
6555 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6556 continue;
6559 /* Static method can't override instance method. */
6560 if (METHOD_STATIC (method))
6562 parse_error_context
6563 (method_wfl,
6564 "Instance methods can't be overridden by a static method. Method %qs is an instance method in class %qs",
6565 lang_printable_name (found, 2),
6566 IDENTIFIER_POINTER
6567 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6568 continue;
6571 /* - Overriding/hiding public must be public
6572 - Overriding/hiding protected must be protected or public
6573 - If the overridden or hidden method has default (package)
6574 access, then the overriding or hiding method must not be
6575 private; otherwise, a compile-time error occurs. If
6576 `found' belongs to an interface, things have been already
6577 taken care of. */
6578 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6579 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6580 || (METHOD_PROTECTED (found)
6581 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6582 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6583 && METHOD_PRIVATE (method))))
6585 parse_error_context
6586 (method_wfl,
6587 "Methods can't be overridden to be more private. Method %qs is not %s in class %qs", lang_printable_name (method, 2),
6588 (METHOD_PUBLIC (method) ? "public" :
6589 (METHOD_PRIVATE (method) ? "private" : "protected")),
6590 IDENTIFIER_POINTER (DECL_NAME
6591 (TYPE_NAME (DECL_CONTEXT (found)))));
6592 continue;
6595 /* Check this method against all the other implementations it
6596 overrides. Here we only check the class hierarchy; the rest
6597 of the checking is done later. If this method is just a
6598 Miranda method, we can skip the check. */
6599 if (! METHOD_INVISIBLE (method))
6600 check_concrete_throws_clauses (class, method, DECL_NAME (method), sig);
6603 /* The above throws clause check only looked at superclasses. Now
6604 we must also make sure that all methods declared in interfaces
6605 have compatible throws clauses. FIXME: there are more efficient
6606 ways to organize this checking; we should implement one. */
6607 check_interface_throws_clauses (class, class);
6609 if (!TYPE_NVIRTUALS (class))
6610 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6612 /* Search for inherited abstract method not yet implemented in this
6613 class. */
6614 java_check_abstract_method_definitions (class_decl);
6616 if (!saw_constructor)
6617 abort ();
6620 /* Check to make sure that all the methods in all the interfaces
6621 implemented by CLASS_DECL are compatible with the concrete
6622 implementations available in CHECK_CLASS_DECL. */
6623 static void
6624 check_interface_throws_clauses (tree check_class_decl, tree class_decl)
6626 for (; class_decl != NULL_TREE; class_decl = CLASSTYPE_SUPER (class_decl))
6628 int i;
6630 if (! CLASS_LOADED_P (class_decl))
6632 if (CLASS_FROM_SOURCE_P (class_decl))
6633 safe_layout_class (class_decl);
6634 else
6635 load_class (class_decl, 1);
6638 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (class_decl)) - 1; i > 0; --i)
6640 tree interface
6641 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (class_decl), i));
6642 tree iface_method;
6644 for (iface_method = TYPE_METHODS (interface);
6645 iface_method != NULL_TREE;
6646 iface_method = TREE_CHAIN (iface_method))
6648 tree sig, method;
6650 /* First look for a concrete method implemented or
6651 inherited by this class. No need to search
6652 interfaces here, since we're already looking through
6653 all of them. */
6654 sig = build_java_argument_signature (TREE_TYPE (iface_method));
6655 method
6656 = lookup_argument_method_generic (check_class_decl,
6657 DECL_NAME (iface_method),
6658 sig, SEARCH_VISIBLE);
6659 /* If we don't find an implementation, that is ok. Any
6660 potential errors from that are diagnosed elsewhere.
6661 Also, multiple inheritance with conflicting throws
6662 clauses is fine in the absence of a concrete
6663 implementation. */
6664 if (method != NULL_TREE && !METHOD_ABSTRACT (method)
6665 && !METHOD_INVISIBLE (iface_method))
6667 tree method_wfl = DECL_FUNCTION_WFL (method);
6668 check_throws_clauses (method, method_wfl, iface_method);
6672 /* Now check superinterfaces. */
6673 check_interface_throws_clauses (check_class_decl, interface);
6678 /* Check throws clauses of a method against the clauses of all the
6679 methods it overrides. We do this by searching up the class
6680 hierarchy, examining all matching accessible methods. */
6681 static void
6682 check_concrete_throws_clauses (tree class, tree self_method,
6683 tree name, tree signature)
6685 tree method = lookup_argument_method_generic (class, name, signature,
6686 SEARCH_SUPER | SEARCH_VISIBLE);
6687 while (method != NULL_TREE)
6689 if (! METHOD_INVISIBLE (method) && hack_is_accessible_p (method, class))
6690 check_throws_clauses (self_method, DECL_FUNCTION_WFL (self_method),
6691 method);
6693 method = lookup_argument_method_generic (DECL_CONTEXT (method),
6694 name, signature,
6695 SEARCH_SUPER | SEARCH_VISIBLE);
6699 /* Generate an error if the `throws' clause of METHOD (if any) is
6700 incompatible with the `throws' clause of FOUND (if any). */
6701 static void
6702 check_throws_clauses (tree method, tree method_wfl, tree found)
6704 tree mthrows;
6706 for (mthrows = DECL_FUNCTION_THROWS (method);
6707 mthrows; mthrows = TREE_CHAIN (mthrows))
6709 tree fthrows;
6711 /* We don't verify unchecked expressions */
6712 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6713 continue;
6714 /* Checked expression must be compatible */
6715 for (fthrows = DECL_FUNCTION_THROWS (found);
6716 fthrows; fthrows = TREE_CHAIN (fthrows))
6718 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6719 break;
6721 if (!fthrows)
6723 parse_error_context
6724 (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",
6725 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6726 lang_printable_name (found, 2),
6727 IDENTIFIER_POINTER
6728 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6733 /* Check abstract method of interface INTERFACE */
6734 static void
6735 java_check_abstract_methods (tree interface_decl)
6737 int i;
6738 tree method, found;
6739 tree interface = TREE_TYPE (interface_decl);
6740 tree base_binfo;
6742 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6744 /* 2- Check for double definition inside the defining interface */
6745 if (check_method_redefinition (interface, method))
6746 continue;
6748 /* 3- Overriding is OK as far as we preserve the return type. */
6749 found = lookup_java_interface_method2 (interface, method);
6750 if (found)
6752 char *t;
6753 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6754 parse_error_context
6755 (DECL_FUNCTION_WFL (found),
6756 "Method %qs was defined with return type %qs in class %qs",
6757 lang_printable_name (found, 2), t,
6758 IDENTIFIER_POINTER
6759 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6760 free (t);
6761 continue;
6765 /* 4- Inherited methods can't differ by their returned types */
6766 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (interface), i, base_binfo); i++)
6768 tree sub_interface_method, sub_interface;
6770 sub_interface = BINFO_TYPE (base_binfo);
6771 for (sub_interface_method = TYPE_METHODS (sub_interface);
6772 sub_interface_method;
6773 sub_interface_method = TREE_CHAIN (sub_interface_method))
6775 found = lookup_java_interface_method2 (interface,
6776 sub_interface_method);
6777 if (found && (found != sub_interface_method))
6779 parse_error_context
6780 (lookup_cl (sub_interface_method),
6781 "Interface %qs inherits method %qs from interface %qs. This method is redefined with a different return type in interface %qs",
6782 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6783 lang_printable_name (found, 2),
6784 IDENTIFIER_POINTER
6785 (DECL_NAME (TYPE_NAME
6786 (DECL_CONTEXT (sub_interface_method)))),
6787 IDENTIFIER_POINTER
6788 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6794 /* Lookup methods in interfaces using their name and partial
6795 signature. Return a matching method only if their types differ. */
6797 static tree
6798 lookup_java_interface_method2 (tree class, tree method_decl)
6800 int i;
6801 tree base_binfo;
6802 tree to_return;
6804 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6806 if ((BINFO_TYPE (base_binfo) != object_type_node)
6807 && (to_return =
6808 lookup_java_method2 (BINFO_TYPE (base_binfo), method_decl, 1)))
6809 return to_return;
6811 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6813 to_return = lookup_java_interface_method2
6814 (BINFO_TYPE (base_binfo), method_decl);
6815 if (to_return)
6816 return to_return;
6819 return NULL_TREE;
6822 /* Lookup method using their name and partial signature. Return a
6823 matching method only if their types differ. */
6825 static tree
6826 lookup_java_method2 (tree clas, tree method_decl, int do_interface)
6828 tree method, method_signature, method_name, method_type, name;
6830 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6831 name = DECL_NAME (method_decl);
6832 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6833 EXPR_WFL_NODE (name) : name);
6834 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6836 while (clas != NULL_TREE)
6838 for (method = TYPE_METHODS (clas);
6839 method != NULL_TREE; method = TREE_CHAIN (method))
6841 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6842 tree name = DECL_NAME (method);
6843 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6844 EXPR_WFL_NODE (name) : name) == method_name
6845 && method_sig == method_signature
6846 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6847 return method;
6849 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6851 return NULL_TREE;
6854 /* Return the line that matches DECL line number, and try its best to
6855 position the column number. Used during error reports.
6856 FUTURE/FIXME: return source_location instead of node. */
6858 static GTY(()) tree cl_v;
6859 static tree
6860 lookup_cl (tree decl)
6862 #ifndef USE_MAPPED_LOCATION
6863 char *line, *found;
6864 #endif
6866 if (!decl)
6867 return NULL_TREE;
6869 if (cl_v == NULL_TREE)
6871 cl_v = build_unknown_wfl (NULL_TREE);
6874 #ifdef USE_MAPPED_LOCATION
6875 SET_EXPR_LOCATION (cl_v, DECL_SOURCE_LOCATION (decl));
6876 #else
6877 EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6878 EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE (decl), -1);
6880 line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6881 EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6883 found = strstr ((const char *)line,
6884 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6885 if (found)
6886 EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6887 #endif
6889 return cl_v;
6892 /* Look for a simple name in the single-type import list */
6894 static tree
6895 find_name_in_single_imports (tree name)
6897 tree node;
6899 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6900 if (TREE_VALUE (node) == name)
6901 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6903 return NULL_TREE;
6906 /* Process all single-type import. */
6908 static int
6909 process_imports (void)
6911 tree import;
6912 int error_found;
6914 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6916 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6917 char *original_name;
6919 /* Don't load twice something already defined. */
6920 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6921 continue;
6923 original_name = xmemdup (IDENTIFIER_POINTER (to_be_found),
6924 IDENTIFIER_LENGTH (to_be_found),
6925 IDENTIFIER_LENGTH (to_be_found) + 1);
6927 while (1)
6929 tree left;
6931 QUALIFIED_P (to_be_found) = 1;
6932 load_class (to_be_found, 0);
6933 error_found =
6934 check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true, NULL_TREE);
6936 /* We found it, we can bail out */
6937 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6939 check_deprecation (TREE_PURPOSE (import),
6940 IDENTIFIER_CLASS_VALUE (to_be_found));
6941 break;
6944 /* We haven't found it. Maybe we're trying to access an
6945 inner class. The only way for us to know is to try again
6946 after having dropped a qualifier. If we can't break it further,
6947 we have an error. */
6948 if (split_qualified_name (&left, NULL, to_be_found))
6949 break;
6951 to_be_found = left;
6953 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6955 parse_error_context (TREE_PURPOSE (import),
6956 "Class or interface %qs not found in import",
6957 original_name);
6958 error_found = 1;
6961 free (original_name);
6962 if (error_found)
6963 return 1;
6965 return 0;
6968 /* Possibly find and mark a class imported by a single-type import
6969 statement. */
6971 static void
6972 find_in_imports (tree enclosing_type, tree class_type)
6974 tree import;
6975 if (enclosing_type && TYPE_IMPORT_LIST (enclosing_type))
6976 import = TYPE_IMPORT_LIST (enclosing_type);
6977 else
6978 import = ctxp->import_list;
6980 while (import)
6982 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6984 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6985 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6986 return;
6988 import = TREE_CHAIN (import);
6992 static int
6993 note_possible_classname (const char *name, int len)
6995 tree node;
6996 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6997 len = len - 5;
6998 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6999 len = len - 6;
7000 else
7001 return 0;
7002 node = ident_subst (name, len, "", '/', '.', "");
7003 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
7004 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
7005 return 1;
7008 /* Read a import directory, gathering potential match for further type
7009 references. Indifferently reads a filesystem or a ZIP archive
7010 directory. */
7012 static void
7013 read_import_dir (tree wfl)
7015 tree package_id = EXPR_WFL_NODE (wfl);
7016 const char *package_name = IDENTIFIER_POINTER (package_id);
7017 int package_length = IDENTIFIER_LENGTH (package_id);
7018 DIR *dirp = NULL;
7019 JCF *saved_jcf = current_jcf;
7021 int found = 0;
7022 int k;
7023 void *entry;
7024 struct buffer filename[1];
7026 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
7027 return;
7028 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
7030 BUFFER_INIT (filename);
7031 buffer_grow (filename, package_length + 100);
7033 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
7035 const char *entry_name = jcf_path_name (entry);
7036 int entry_length = strlen (entry_name);
7037 if (jcf_path_is_zipfile (entry))
7039 ZipFile *zipf;
7040 buffer_grow (filename, entry_length);
7041 memcpy (filename->data, entry_name, entry_length - 1);
7042 filename->data[entry_length-1] = '\0';
7043 zipf = opendir_in_zip ((const char *) filename->data, jcf_path_is_system (entry));
7044 if (zipf == NULL)
7045 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
7046 else
7048 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
7049 BUFFER_RESET (filename);
7050 for (k = 0; k < package_length; k++)
7052 char ch = package_name[k];
7053 *filename->ptr++ = ch == '.' ? '/' : ch;
7055 *filename->ptr++ = '/';
7057 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
7059 const char *current_entry = ZIPDIR_FILENAME (zipd);
7060 int current_entry_len = zipd->filename_length;
7062 if (current_entry_len >= BUFFER_LENGTH (filename)
7063 && strncmp ((const char *) filename->data, current_entry,
7064 BUFFER_LENGTH (filename)) != 0)
7065 continue;
7066 found |= note_possible_classname (current_entry,
7067 current_entry_len);
7071 else
7073 BUFFER_RESET (filename);
7074 buffer_grow (filename, entry_length + package_length + 4);
7075 strcpy ((char *) filename->data, entry_name);
7076 filename->ptr = filename->data + entry_length;
7077 for (k = 0; k < package_length; k++)
7079 char ch = package_name[k];
7080 *filename->ptr++ = ch == '.' ? '/' : ch;
7082 *filename->ptr = '\0';
7084 dirp = opendir ((const char *) filename->data);
7085 if (dirp == NULL)
7086 continue;
7087 *filename->ptr++ = '/';
7088 for (;;)
7090 int len;
7091 const char *d_name;
7092 struct dirent *direntp = readdir (dirp);
7093 if (!direntp)
7094 break;
7095 d_name = direntp->d_name;
7096 len = strlen (direntp->d_name);
7097 buffer_grow (filename, len+1);
7098 strcpy ((char *) filename->ptr, d_name);
7099 found |= note_possible_classname ((const char *) filename->data + entry_length,
7100 package_length+len+1);
7102 if (dirp)
7103 closedir (dirp);
7107 free (filename->data);
7109 /* Here we should have a unified way of retrieving an entry, to be
7110 indexed. */
7111 if (!found)
7113 static int first = 1;
7114 if (first)
7116 error ("Can't find default package %qs. Check the CLASSPATH environment variable and the access to the archives", package_name);
7117 java_error_count++;
7118 first = 0;
7120 else
7121 parse_error_context (wfl, "Package %qs not found in import",
7122 package_name);
7123 current_jcf = saved_jcf;
7124 return;
7126 current_jcf = saved_jcf;
7129 /* Possibly find a type in the import on demands specified
7130 types. Returns 1 if an error occurred, 0 otherwise. Run through the
7131 entire list, to detected potential double definitions. */
7133 static int
7134 find_in_imports_on_demand (tree enclosing_type, tree class_type)
7136 tree class_type_name = TYPE_NAME (class_type);
7137 tree cl = NULL_TREE;
7138 int seen_once = -1; /* -1 when not set, 1 if seen once, >1 otherwise. */
7139 int to_return = -1; /* -1 when not set, 0 or 1 otherwise */
7140 tree node;
7141 tree import;
7143 if (enclosing_type && TYPE_IMPORT_DEMAND_LIST (enclosing_type))
7144 import = TYPE_IMPORT_DEMAND_LIST (enclosing_type);
7145 else
7146 import = ctxp->import_demand_list;
7148 for (; import; import = TREE_CHAIN (import))
7150 location_t saved_location = input_location;
7151 int access_check;
7152 const char *id_name;
7153 tree decl, type_name_copy;
7155 obstack_grow (&temporary_obstack,
7156 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
7157 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
7158 obstack_1grow (&temporary_obstack, '.');
7159 obstack_grow0 (&temporary_obstack,
7160 IDENTIFIER_POINTER (class_type_name),
7161 IDENTIFIER_LENGTH (class_type_name));
7162 id_name = obstack_finish (&temporary_obstack);
7164 if (! (node = maybe_get_identifier (id_name)))
7165 continue;
7167 /* Setup input_line so that it refers to the line of the import (in
7168 case we parse a class file and encounter errors */
7169 #ifdef USE_MAPPED_LOCATION
7170 input_location = EXPR_LOCATION (TREE_PURPOSE (import));
7171 #else
7172 input_line = EXPR_WFL_LINENO (TREE_PURPOSE (import));
7173 #endif
7175 type_name_copy = TYPE_NAME (class_type);
7176 TYPE_NAME (class_type) = node;
7177 QUALIFIED_P (node) = 1;
7178 decl = IDENTIFIER_CLASS_VALUE (node);
7179 access_check = -1;
7180 /* If there is no DECL set for the class or if the class isn't
7181 loaded and not seen in source yet, then load */
7182 if (!decl || ! CLASS_LOADED_P (TREE_TYPE (decl)))
7184 load_class (node, 0);
7185 decl = IDENTIFIER_CLASS_VALUE (node);
7187 if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7188 access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7189 false, NULL_TREE);
7190 else
7191 /* 6.6.1: Inner classes are subject to member access rules. */
7192 access_check = 0;
7194 input_location = saved_location;
7196 /* If the loaded class is not accessible or couldn't be loaded,
7197 we restore the original TYPE_NAME and process the next
7198 import. */
7199 if (access_check || !decl)
7201 TYPE_NAME (class_type) = type_name_copy;
7202 continue;
7205 /* If the loaded class is accessible, we keep a tab on it to
7206 detect and report multiple inclusions. */
7207 if (IS_A_CLASSFILE_NAME (node))
7209 if (seen_once < 0)
7211 cl = TREE_PURPOSE (import);
7212 seen_once = 1;
7214 else if (seen_once >= 0)
7216 tree location = (cl ? cl : TREE_PURPOSE (import));
7217 tree package = (cl ? EXPR_WFL_NODE (cl) :
7218 EXPR_WFL_NODE (TREE_PURPOSE (import)));
7219 seen_once++;
7220 parse_error_context
7221 (location,
7222 "Type %qs also potentially defined in package %qs",
7223 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7224 IDENTIFIER_POINTER (package));
7227 to_return = access_check;
7230 if (seen_once == 1)
7231 return to_return;
7232 else
7233 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7236 static tree
7237 resolve_package (tree pkg, tree *next, tree *type_name)
7239 tree current;
7240 tree decl = NULL_TREE;
7241 *type_name = NULL_TREE;
7243 /* The trick is to determine when the package name stops and were
7244 the name of something contained in the package starts. Then we
7245 return a fully qualified name of what we want to get. */
7247 *next = EXPR_WFL_QUALIFICATION (pkg);
7249 /* Try to progressively construct a type name */
7250 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7251 for (current = EXPR_WFL_QUALIFICATION (pkg);
7252 current; current = TREE_CHAIN (current))
7254 /* If we don't have what we're expecting, exit now. TYPE_NAME
7255 will be null and the error caught later. */
7256 if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7257 break;
7258 *type_name =
7259 merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7260 if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7262 /* resolve_package should be used in a loop, hence we
7263 point at this one to naturally process the next one at
7264 the next iteration. */
7265 *next = current;
7266 break;
7269 return decl;
7272 /* Check accessibility of inner class DECL, from the context ENCLOSING_DECL,
7273 according to member access rules. */
7275 static bool
7276 inner_class_accessible (tree decl, tree enclosing_decl)
7278 tree enclosing_decl_type;
7280 enclosing_decl_type = TREE_TYPE (enclosing_decl);
7282 if (CLASS_PRIVATE (decl))
7284 /* Access is permitted only within the body of the top-level
7285 class in which DECL is declared. */
7286 tree top_level = decl;
7287 while (DECL_CONTEXT (top_level))
7288 top_level = DECL_CONTEXT (top_level);
7289 while (DECL_CONTEXT (enclosing_decl))
7290 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7291 if (top_level == enclosing_decl)
7292 return true;
7294 else if (CLASS_PROTECTED (decl))
7296 tree decl_context;
7297 /* Access is permitted from within the same package... */
7298 if (in_same_package (decl, enclosing_decl))
7299 return true;
7301 /* ... or from within the body of a subtype of the context in which
7302 DECL is declared. */
7303 decl_context = DECL_CONTEXT (decl);
7304 while (enclosing_decl)
7306 if (CLASS_INTERFACE (decl))
7308 if (interface_of_p (TREE_TYPE (decl_context),
7309 enclosing_decl_type))
7310 return true;
7312 else
7314 /* Eww. The order of the arguments is different!! */
7315 if (inherits_from_p (enclosing_decl_type,
7316 TREE_TYPE (decl_context)))
7317 return true;
7319 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7322 else if (! CLASS_PUBLIC (decl))
7324 /* Access is permitted only from within the same package as DECL. */
7325 if (in_same_package (decl, enclosing_decl))
7326 return true;
7328 else
7329 /* Class is public. */
7330 return true;
7332 return false;
7335 /* Check accessibility of inner classes according to member access rules.
7336 DECL is the inner class, ENCLOSING_DECL is the class from which the
7337 access is being attempted. */
7339 static void
7340 check_inner_class_access (tree decl, tree enclosing_decl, tree cl)
7342 const char *access;
7344 /* We don't issue an error message when CL is null. CL can be null
7345 as a result of processing a JDEP crafted by source_start_java_method
7346 for the purpose of patching its parm decl. But the error would
7347 have been already trapped when fixing the method's signature.
7348 DECL can also be NULL in case of earlier errors. */
7349 if (!decl || !cl)
7350 return;
7352 if (inner_class_accessible (decl, enclosing_decl))
7353 return;
7355 if (CLASS_PRIVATE (decl))
7356 access = "private";
7357 else if (CLASS_PROTECTED (decl))
7358 access = "protected";
7359 else
7360 access = "non-public";
7362 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7363 (CLASS_INTERFACE (decl) ? "interface" : "class"),
7364 lang_printable_name (decl, 2), access);
7367 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7368 foreign package, it must be PUBLIC. Return 0 if no access
7369 violations were found, 1 otherwise. If VERBOSE is true and an error
7370 was found, it is reported and accounted for. If CL is NULL then
7371 look it up with THIS_DECL. */
7373 static int
7374 check_pkg_class_access (tree class_name, tree cl, bool verbose, tree this_decl)
7376 tree type;
7378 if (!IDENTIFIER_CLASS_VALUE (class_name))
7379 return 0;
7381 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7382 return 0;
7384 if (!CLASS_PUBLIC (TYPE_NAME (type)))
7386 /* Access to a private class within the same package is
7387 allowed. */
7388 tree l, r;
7389 split_qualified_name (&l, &r, class_name);
7390 if (!QUALIFIED_P (class_name) && !ctxp->package)
7391 /* Both in the empty package. */
7392 return 0;
7393 if (l == ctxp->package)
7394 /* Both in the same package. */
7395 return 0;
7397 if (verbose)
7398 parse_error_context
7399 (cl == NULL ? lookup_cl (this_decl): cl,
7400 "Can't access %s %qs. Only public classes and interfaces in other packages can be accessed",
7401 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7402 IDENTIFIER_POINTER (class_name));
7403 return 1;
7405 return 0;
7408 /* Local variable declaration. */
7410 static void
7411 declare_local_variables (int modifier, tree type, tree vlist)
7413 tree decl, current, saved_type;
7414 tree type_wfl = NULL_TREE;
7415 int must_chain = 0;
7416 int final_p = 0;
7418 /* Push a new block if statements were seen between the last time we
7419 pushed a block and now. Keep a count of blocks to close */
7420 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7422 tree b = enter_block ();
7423 BLOCK_IS_IMPLICIT (b) = 1;
7426 if (modifier)
7428 size_t i;
7429 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7430 if (1 << i & modifier)
7431 break;
7432 if (modifier == ACC_FINAL)
7433 final_p = 1;
7434 else
7436 parse_error_context
7437 (ctxp->modifier_ctx [i],
7438 "Only %<final%> is allowed as a local variables modifier");
7439 return;
7443 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7444 hold the TYPE value if a new incomplete has to be created (as
7445 opposed to being found already existing and reused). */
7446 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7448 /* If TYPE is fully resolved and we don't have a reference, make one */
7449 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7451 /* Go through all the declared variables */
7452 for (current = vlist, saved_type = type; current;
7453 current = TREE_CHAIN (current), type = saved_type)
7455 tree other, real_type;
7456 tree wfl = TREE_PURPOSE (current);
7457 tree name = EXPR_WFL_NODE (wfl);
7458 tree init = TREE_VALUE (current);
7460 /* Process NAME, as it may specify extra dimension(s) for it */
7461 type = build_array_from_name (type, type_wfl, name, &name);
7463 /* Variable redefinition check */
7464 if ((other = lookup_name_in_blocks (name)))
7466 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7467 DECL_SOURCE_LINE (other));
7468 continue;
7471 /* Type adjustment. We may have just readjusted TYPE because
7472 the variable specified more dimensions. Make sure we have
7473 a reference if we can and don't have one already. */
7474 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7476 real_type = GET_REAL_TYPE (type);
7477 /* Never layout this decl. This will be done when its scope
7478 will be entered */
7479 decl = build_decl (VAR_DECL, name, real_type);
7480 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7481 DECL_FINAL (decl) = final_p;
7482 BLOCK_CHAIN_DECL (decl);
7484 /* Don't try to use an INIT statement when an error was found */
7485 if (init && java_error_count)
7486 init = NULL_TREE;
7488 /* Remember it if this is an initialized-upon-declaration final
7489 variable. */
7490 if (init && final_p)
7492 DECL_LOCAL_FINAL_IUD (decl) = 1;
7495 /* Add the initialization function to the current function's code */
7496 if (init)
7498 /* Name might have been readjusted */
7499 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7500 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7501 java_method_add_stmt (current_function_decl,
7502 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7503 init));
7506 /* Setup dependency the type of the decl */
7507 if (must_chain)
7509 jdep *dep;
7510 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7511 dep = CLASSD_LAST (ctxp->classd_list);
7512 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7515 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7518 /* Called during parsing. Build decls from argument list. */
7520 static void
7521 source_start_java_method (tree fndecl)
7523 tree tem;
7524 tree parm_decl;
7525 int i;
7527 if (!fndecl)
7528 return;
7530 current_function_decl = fndecl;
7532 /* New scope for the function */
7533 enter_block ();
7534 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7535 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7537 tree type = TREE_VALUE (tem);
7538 tree name = TREE_PURPOSE (tem);
7540 /* If type is incomplete. Create an incomplete decl and ask for
7541 the decl to be patched later */
7542 if (INCOMPLETE_TYPE_P (type))
7544 jdep *jdep;
7545 tree real_type = GET_REAL_TYPE (type);
7546 parm_decl = build_decl (PARM_DECL, name, real_type);
7547 type = obtain_incomplete_type (type);
7548 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7549 jdep = CLASSD_LAST (ctxp->classd_list);
7550 JDEP_MISC (jdep) = name;
7551 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7553 else
7554 parm_decl = build_decl (PARM_DECL, name, type);
7556 /* Remember if a local variable was declared final (via its
7557 TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7558 if (ARG_FINAL_P (tem))
7560 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7561 DECL_FINAL (parm_decl) = 1;
7564 BLOCK_CHAIN_DECL (parm_decl);
7566 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7567 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7568 nreverse (tem);
7569 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7570 DECL_MAX_LOCALS (current_function_decl) = i;
7573 /* Called during parsing. Creates an artificial method declaration. */
7575 static tree
7576 create_artificial_method (tree class, int flags, tree type,
7577 tree name, tree args)
7579 tree mdecl;
7580 location_t save_location = input_location;
7582 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (class));
7583 mdecl = make_node (FUNCTION_TYPE);
7584 TREE_TYPE (mdecl) = type;
7585 TYPE_ARG_TYPES (mdecl) = args;
7586 /* We used to compute the signature of MDECL here and then use
7587 add_method(), but that failed because our caller might modify
7588 the type of the returned method, which trashes the cache in
7589 get_type_from_signature(). */
7590 mdecl = add_method_1 (class, flags, name, mdecl);
7591 input_location = save_location;
7592 DECL_ARTIFICIAL (mdecl) = 1;
7593 return mdecl;
7596 /* Starts the body if an artificial method. */
7598 static void
7599 start_artificial_method_body (tree mdecl)
7601 #ifdef USE_MAPPED_LOCATION
7602 DECL_SOURCE_LOCATION (mdecl) = ctxp->file_start_location;
7603 DECL_FUNCTION_LAST_LINE (mdecl) = ctxp->file_start_location;
7604 #else
7605 DECL_SOURCE_LINE (mdecl) = 1;
7606 DECL_FUNCTION_LAST_LINE (mdecl) = 1;
7607 #endif
7608 source_start_java_method (mdecl);
7609 enter_block ();
7612 static void
7613 end_artificial_method_body (tree mdecl)
7615 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7616 It has to be evaluated first. (if mdecl is current_function_decl,
7617 we have an undefined behavior if no temporary variable is used.) */
7618 tree b = exit_block ();
7619 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7620 exit_block ();
7623 /* Dump a tree of some kind. This is a convenience wrapper for the
7624 dump_* functions in tree-dump.c. */
7625 static void
7626 dump_java_tree (enum tree_dump_index phase, tree t)
7628 FILE *stream;
7629 int flags;
7631 stream = dump_begin (phase, &flags);
7632 flags |= TDF_SLIM;
7633 if (stream)
7635 dump_node (t, flags, stream);
7636 dump_end (phase, stream);
7640 /* Terminate a function and expand its body. */
7642 static void
7643 source_end_java_method (void)
7645 tree fndecl = current_function_decl;
7647 if (!fndecl)
7648 return;
7650 java_parser_context_save_global ();
7651 #ifdef USE_MAPPED_LOCATION
7652 input_location = ctxp->last_ccb_indent1;
7653 #else
7654 input_line = ctxp->last_ccb_indent1;
7655 #endif
7657 /* Turn function bodies with only a NOP expr null, so they don't get
7658 generated at all and we won't get warnings when using the -W
7659 -Wall flags. */
7660 if (IS_EMPTY_STMT (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))))
7661 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7663 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7664 && ! flag_emit_class_files)
7665 finish_method (fndecl);
7667 current_function_decl = NULL_TREE;
7668 java_parser_context_restore_global ();
7669 current_function_decl = NULL_TREE;
7672 /* Record EXPR in the current function block. Complements compound
7673 expression second operand if necessary. */
7675 tree
7676 java_method_add_stmt (tree fndecl, tree expr)
7678 if (!GET_CURRENT_BLOCK (fndecl))
7679 return NULL_TREE;
7680 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7683 static tree
7684 add_stmt_to_block (tree b, tree type, tree stmt)
7686 tree body = BLOCK_EXPR_BODY (b), c;
7688 if (java_error_count)
7689 return body;
7691 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7692 return body;
7694 BLOCK_EXPR_BODY (b) = c;
7695 TREE_SIDE_EFFECTS (c) = 1;
7696 return c;
7699 /* Lays out the methods for the classes seen so far. */
7701 void
7702 java_layout_seen_class_methods (void)
7704 tree previous_list = all_class_list;
7705 tree end = NULL_TREE;
7706 tree current;
7708 while (1)
7710 for (current = previous_list;
7711 current != end; current = TREE_CHAIN (current))
7713 tree decl = TREE_VALUE (current);
7714 tree cls = TREE_TYPE (decl);
7716 input_location = DECL_SOURCE_LOCATION (decl);
7718 if (! CLASS_LOADED_P (cls))
7719 load_class (cls, 0);
7721 layout_class_methods (cls);
7724 /* Note that new classes might have been added while laying out
7725 methods, changing the value of all_class_list. */
7727 if (previous_list != all_class_list)
7729 end = previous_list;
7730 previous_list = all_class_list;
7732 else
7733 break;
7737 static GTY(()) tree stop_reordering;
7738 void
7739 java_reorder_fields (void)
7741 tree current;
7743 for (current = gclass_list; current; current = TREE_CHAIN (current))
7745 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7747 if (current_class == stop_reordering)
7748 break;
7750 /* Reverse the fields, but leave the dummy field in front.
7751 Fields are already ordered for Object and Class */
7752 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7753 && current_class != class_type_node)
7755 /* If the dummy field is there, reverse the right fields and
7756 just layout the type for proper fields offset */
7757 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7759 tree fields = TYPE_FIELDS (current_class);
7760 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7761 TYPE_SIZE (current_class) = NULL_TREE;
7763 /* We don't have a dummy field, we need to layout the class,
7764 after having reversed the fields */
7765 else
7767 TYPE_FIELDS (current_class) =
7768 nreverse (TYPE_FIELDS (current_class));
7769 TYPE_SIZE (current_class) = NULL_TREE;
7773 /* There are cases were gclass_list will be empty. */
7774 if (gclass_list)
7775 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7778 /* Layout the methods of all classes loaded in one way or another.
7779 Check methods of source parsed classes. Then reorder the
7780 fields and layout the classes or the type of all source parsed
7781 classes */
7783 void
7784 java_layout_classes (void)
7786 tree current;
7787 int save_error_count = java_error_count;
7789 /* Layout the methods of all classes seen so far */
7790 java_layout_seen_class_methods ();
7791 java_parse_abort_on_error ();
7792 all_class_list = NULL_TREE;
7794 /* Then check the methods of all parsed classes */
7795 for (current = gclass_list; current; current = TREE_CHAIN (current))
7796 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7797 java_check_methods (TREE_VALUE (current));
7798 java_parse_abort_on_error ();
7800 for (current = gclass_list; current; current = TREE_CHAIN (current))
7802 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7803 layout_class (current_class);
7805 /* Error reported by the caller */
7806 if (java_error_count)
7807 return;
7810 /* We might have reloaded classes durign the process of laying out
7811 classes for code generation. We must layout the methods of those
7812 late additions, as constructor checks might use them */
7813 java_layout_seen_class_methods ();
7814 java_parse_abort_on_error ();
7817 /* Expand methods in the current set of classes remembered for
7818 generation. */
7820 static void
7821 java_complete_expand_classes (void)
7823 tree current;
7825 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7826 if (!INNER_CLASS_DECL_P (current))
7827 java_complete_expand_class (current);
7830 /* Expand the methods found in OUTER, starting first by OUTER's inner
7831 classes, if any. */
7833 static void
7834 java_complete_expand_class (tree outer)
7836 tree inner_list;
7838 /* We need to go after all inner classes and start expanding them,
7839 starting with most nested ones. We have to do that because nested
7840 classes might add functions to outer classes */
7842 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7843 inner_list; inner_list = TREE_CHAIN (inner_list))
7844 java_complete_expand_class (TREE_PURPOSE (inner_list));
7846 java_complete_expand_methods (outer);
7849 /* Expand methods registered in CLASS_DECL. The general idea is that
7850 we expand regular methods first. This allows us get an estimate on
7851 how outer context local alias fields are really used so we can add
7852 to the constructor just enough code to initialize them properly (it
7853 also lets us generate finit$ correctly.) Then we expand the
7854 constructors and then <clinit>. */
7856 static void
7857 java_complete_expand_methods (tree class_decl)
7859 tree clinit, decl, first_decl;
7861 output_class = current_class = TREE_TYPE (class_decl);
7863 /* Pre-expand <clinit> to figure whether we really need it or
7864 not. If we do need it, we pre-expand the static fields so they're
7865 ready to be used somewhere else. <clinit> will be fully expanded
7866 after we processed the constructors. */
7867 first_decl = TYPE_METHODS (current_class);
7868 clinit = maybe_generate_pre_expand_clinit (current_class);
7870 /* Then generate finit$ (if we need to) because constructors will
7871 try to use it.*/
7872 if (TYPE_FINIT_STMT_LIST (current_class))
7873 java_complete_expand_method (generate_finit (current_class));
7875 /* Then generate instinit$ (if we need to) because constructors will
7876 try to use it. */
7877 if (TYPE_II_STMT_LIST (current_class))
7878 java_complete_expand_method (generate_instinit (current_class));
7880 /* Now do the constructors */
7881 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7883 if (!DECL_CONSTRUCTOR_P (decl))
7884 continue;
7885 java_complete_expand_method (decl);
7888 /* First, do the ordinary methods. */
7889 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7891 /* Ctors aren't part of this batch. */
7892 if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7893 continue;
7895 /* Skip abstract or native methods -- but do handle native
7896 methods when generating JNI stubs. */
7897 if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7899 DECL_FUNCTION_BODY (decl) = NULL_TREE;
7900 continue;
7903 if (METHOD_NATIVE (decl))
7905 tree body;
7906 current_function_decl = decl;
7907 body = build_jni_stub (decl);
7908 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7911 java_complete_expand_method (decl);
7914 /* If there is indeed a <clinit>, fully expand it now */
7915 if (clinit)
7917 /* Prevent the use of `this' inside <clinit> */
7918 ctxp->explicit_constructor_p = 1;
7919 java_complete_expand_method (clinit);
7920 ctxp->explicit_constructor_p = 0;
7923 /* We might have generated a class$ that we now want to expand */
7924 if (TYPE_DOT_CLASS (current_class))
7925 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7927 /* Now verify constructor circularity (stop after the first one we
7928 prove wrong.) */
7929 if (!CLASS_INTERFACE (class_decl))
7930 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7931 if (DECL_CONSTRUCTOR_P (decl)
7932 && verify_constructor_circularity (decl, decl))
7933 break;
7936 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7937 safely used in some other methods/constructors. */
7939 static tree
7940 maybe_generate_pre_expand_clinit (tree class_type)
7942 tree current, mdecl;
7944 if (!TYPE_CLINIT_STMT_LIST (class_type))
7945 return NULL_TREE;
7947 /* Go through all static fields and pre expand them */
7948 for (current = TYPE_FIELDS (class_type); current;
7949 current = TREE_CHAIN (current))
7950 if (FIELD_STATIC (current))
7951 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7953 /* Then build the <clinit> method */
7954 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7955 clinit_identifier_node, end_params_node);
7956 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7957 mdecl, NULL_TREE);
7958 start_artificial_method_body (mdecl);
7960 /* We process the list of assignment we produced as the result of
7961 the declaration of initialized static field and add them as
7962 statement to the <clinit> method. */
7963 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7964 current = TREE_CHAIN (current))
7966 tree stmt = current;
7967 /* We build the assignment expression that will initialize the
7968 field to its value. There are strict rules on static
7969 initializers (8.5). FIXME */
7970 if (TREE_CODE (stmt) != BLOCK && !IS_EMPTY_STMT (stmt))
7971 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7972 java_method_add_stmt (mdecl, stmt);
7975 end_artificial_method_body (mdecl);
7977 /* Now we want to place <clinit> as the last method (because we need
7978 it at least for interface so that it doesn't interfere with the
7979 dispatch table based lookup. */
7980 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7982 current = TREE_CHAIN (TYPE_METHODS (class_type));
7983 TYPE_METHODS (class_type) = current;
7985 while (TREE_CHAIN (current))
7986 current = TREE_CHAIN (current);
7988 TREE_CHAIN (current) = mdecl;
7989 TREE_CHAIN (mdecl) = NULL_TREE;
7992 return mdecl;
7995 /* Analyzes a method body and look for something that isn't a
7996 MODIFY_EXPR with a constant value. */
7998 static int
7999 analyze_clinit_body (tree this_class, tree bbody)
8001 while (bbody)
8002 switch (TREE_CODE (bbody))
8004 case BLOCK:
8005 bbody = BLOCK_EXPR_BODY (bbody);
8006 break;
8008 case EXPR_WITH_FILE_LOCATION:
8009 bbody = EXPR_WFL_NODE (bbody);
8010 break;
8012 case COMPOUND_EXPR:
8013 if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
8014 return 1;
8015 bbody = TREE_OPERAND (bbody, 1);
8016 break;
8018 case MODIFY_EXPR:
8019 /* If we're generating to class file and we're dealing with an
8020 array initialization, we return 1 to keep <clinit> */
8021 if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
8022 && flag_emit_class_files)
8023 return 1;
8025 /* There are a few cases where we're required to keep
8026 <clinit>:
8027 - If this is an assignment whose operand is not constant,
8028 - If this is an assignment to a non-initialized field,
8029 - If this field is not a member of the current class.
8031 return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
8032 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
8033 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
8035 default:
8036 return 1;
8038 return 0;
8042 /* See whether we could get rid of <clinit>. Criteria are: all static
8043 final fields have constant initial values and the body of <clinit>
8044 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
8046 static int
8047 maybe_yank_clinit (tree mdecl)
8049 tree type, current;
8050 tree fbody, bbody;
8052 if (!DECL_CLINIT_P (mdecl))
8053 return 0;
8055 /* If the body isn't empty, then we keep <clinit>. Note that if
8056 we're emitting classfiles, this isn't enough not to rule it
8057 out. */
8058 fbody = DECL_FUNCTION_BODY (mdecl);
8059 bbody = BLOCK_EXPR_BODY (fbody);
8060 if (bbody && bbody != error_mark_node)
8061 bbody = BLOCK_EXPR_BODY (bbody);
8062 else
8063 return 0;
8064 if (bbody && ! flag_emit_class_files && !IS_EMPTY_STMT (bbody))
8065 return 0;
8067 type = DECL_CONTEXT (mdecl);
8068 current = TYPE_FIELDS (type);
8070 for (current = (current ? TREE_CHAIN (current) : current);
8071 current; current = TREE_CHAIN (current))
8073 tree f_init;
8075 /* We're not interested in non-static fields. */
8076 if (!FIELD_STATIC (current))
8077 continue;
8079 /* Nor in fields without initializers. */
8080 f_init = DECL_INITIAL (current);
8081 if (f_init == NULL_TREE)
8082 continue;
8084 /* Anything that isn't String or a basic type is ruled out -- or
8085 if we know how to deal with it (when doing things natively) we
8086 should generated an empty <clinit> so that SUID are computed
8087 correctly. */
8088 if (! JSTRING_TYPE_P (TREE_TYPE (current))
8089 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
8090 return 0;
8092 if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
8093 return 0;
8096 /* Now we analyze the method body and look for something that
8097 isn't a MODIFY_EXPR */
8098 if (bbody && !IS_EMPTY_STMT (bbody) && analyze_clinit_body (type, bbody))
8099 return 0;
8101 /* Get rid of <clinit> in the class' list of methods */
8102 if (TYPE_METHODS (type) == mdecl)
8103 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
8104 else
8105 for (current = TYPE_METHODS (type); current;
8106 current = TREE_CHAIN (current))
8107 if (TREE_CHAIN (current) == mdecl)
8109 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
8110 break;
8113 return 1;
8116 /* Install the argument from MDECL. Suitable to completion and
8117 expansion of mdecl's body. */
8119 void
8120 start_complete_expand_method (tree mdecl)
8122 tree tem;
8124 pushlevel (1); /* Prepare for a parameter push */
8125 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
8126 DECL_ARGUMENTS (mdecl) = tem;
8128 for (; tem; tem = TREE_CHAIN (tem))
8130 /* TREE_CHAIN (tem) will change after pushdecl. */
8131 tree next = TREE_CHAIN (tem);
8132 tree type = TREE_TYPE (tem);
8133 if (targetm.calls.promote_prototypes (type)
8134 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8135 && INTEGRAL_TYPE_P (type))
8136 type = integer_type_node;
8137 DECL_ARG_TYPE (tem) = type;
8138 layout_decl (tem, 0);
8139 pushdecl (tem);
8140 /* Re-install the next so that the list is kept and the loop
8141 advances. */
8142 TREE_CHAIN (tem) = next;
8144 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8145 input_location = DECL_SOURCE_LOCATION (mdecl);
8146 build_result_decl (mdecl);
8150 /* Complete and expand a method. */
8152 static void
8153 java_complete_expand_method (tree mdecl)
8155 tree fbody, block_body, exception_copy;
8157 current_function_decl = mdecl;
8158 /* Fix constructors before expanding them */
8159 if (DECL_CONSTRUCTOR_P (mdecl))
8160 fix_constructors (mdecl);
8162 /* Expand functions that have a body */
8163 if (!DECL_FUNCTION_BODY (mdecl))
8164 return;
8166 fbody = DECL_FUNCTION_BODY (mdecl);
8167 block_body = BLOCK_EXPR_BODY (fbody);
8168 exception_copy = NULL_TREE;
8170 current_function_decl = mdecl;
8172 if (! quiet_flag)
8173 fprintf (stderr, " [%s.",
8174 lang_printable_name (DECL_CONTEXT (mdecl), 0));
8175 announce_function (mdecl);
8176 if (! quiet_flag)
8177 fprintf (stderr, "]");
8179 /* Prepare the function for tree completion */
8180 start_complete_expand_method (mdecl);
8182 /* Install the current this */
8183 current_this = (!METHOD_STATIC (mdecl) ?
8184 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8186 /* Install exceptions thrown with `throws' */
8187 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8189 if (block_body != NULL_TREE)
8191 block_body = java_complete_tree (block_body);
8193 /* Before we check initialization, attached all class initialization
8194 variable to the block_body */
8195 htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8196 attach_init_test_initialization_flags, block_body);
8198 if (! METHOD_NATIVE (mdecl))
8200 check_for_initialization (block_body, mdecl);
8202 /* Go through all the flags marking the initialization of
8203 static variables and see whether they're definitively
8204 assigned, in which case the type is remembered as
8205 definitively initialized in MDECL. */
8206 if (STATIC_CLASS_INIT_OPT_P ())
8208 /* Always register the context as properly initialized in
8209 MDECL. This used with caution helps removing extra
8210 initialization of self. */
8211 if (METHOD_STATIC (mdecl))
8213 *(htab_find_slot
8214 (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8215 DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8219 ctxp->explicit_constructor_p = 0;
8222 BLOCK_EXPR_BODY (fbody) = block_body;
8224 /* If we saw a return but couldn't evaluate it properly, we'll have
8225 an error_mark_node here. */
8226 if (block_body != error_mark_node
8227 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8228 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE)
8229 missing_return_error (current_function_decl);
8231 /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8232 maybe_yank_clinit (mdecl);
8234 /* Pop the current level, with special measures if we found errors. */
8235 if (java_error_count)
8236 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8237 poplevel (1, 0, 1);
8239 /* Pop the exceptions and sanity check */
8240 POP_EXCEPTIONS();
8241 if (currently_caught_type_list)
8242 abort ();
8245 /* For with each class for which there's code to generate. */
8247 static void
8248 java_expand_method_bodies (tree class)
8250 tree decl;
8251 for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8253 tree block;
8255 if (! DECL_FUNCTION_BODY (decl))
8256 continue;
8258 current_function_decl = decl;
8260 block = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8262 /* Save the function body for gimplify and inlining. */
8263 DECL_SAVED_TREE (decl) = block;
8265 /* It's time to assign the variable flagging static class
8266 initialization based on which classes invoked static methods
8267 are definitely initializing. This should be flagged. */
8268 if (STATIC_CLASS_INIT_OPT_P ())
8270 tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8271 for (; list != NULL_TREE; list = TREE_CHAIN (list))
8273 /* Executed for each statement calling a static function.
8274 LIST is a TREE_LIST whose PURPOSE is the called function
8275 and VALUE is a compound whose second operand can be patched
8276 with static class initialization flag assignments. */
8278 tree called_method = TREE_PURPOSE (list);
8279 tree compound = TREE_VALUE (list);
8280 tree assignment_compound_list
8281 = build_tree_list (called_method, NULL);
8283 /* For each class definitely initialized in
8284 CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8285 assignment to the class initialization flag. */
8286 htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8287 emit_test_initialization,
8288 assignment_compound_list);
8290 if (TREE_VALUE (assignment_compound_list))
8291 TREE_OPERAND (compound, 1)
8292 = TREE_VALUE (assignment_compound_list);
8296 /* Expand the function body. */
8297 source_end_java_method ();
8303 /* This section of the code deals with accessing enclosing context
8304 fields either directly by using the relevant access to this$<n> or
8305 by invoking an access method crafted for that purpose. */
8307 /* Build the necessary access across nested class boundaries.
8308 This routine could be optimized to cache previous result
8309 (decl, current_class and returned access). When an access method
8310 needs to be generated, it always takes the form of a read. It might
8311 be later turned into a write by calling nested_field_access_fix. */
8313 static tree
8314 build_nested_field_access (tree id, tree decl)
8316 tree access = NULL_TREE;
8317 tree ctx = NULL_TREE;
8318 tree decl_ctx = DECL_CONTEXT (decl);
8319 bool is_static = FIELD_STATIC (decl);
8321 if (DECL_CONTEXT (TYPE_NAME (current_class)))
8322 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8324 /* For non-static fields, if the immediate enclosing context of the
8325 current class is the field decl's class or inherits from it,
8326 build the access as `this$<n>.<field>'. Note that we will break
8327 the `private' barrier if we're not emitting bytecodes. */
8328 if (!is_static
8329 && ctx
8330 && (ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8331 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files))
8333 tree thisn = build_current_thisn (current_class);
8334 access = make_qualified_primary (build_wfl_node (thisn),
8335 id, EXPR_WFL_LINECOL (id));
8337 /* Otherwise, generate and use accessor methods for the field as
8338 needed. */
8339 else
8341 int lc = EXPR_WFL_LINECOL (id);
8343 /* Now we chain the required number of calls to the access$0 to
8344 get a hold to the enclosing instance we need for a non-static
8345 field, and then we build the field access. */
8346 if (!is_static)
8347 access = build_access_to_thisn (current_class, decl_ctx, lc);
8349 /* If the field is private and we're generating bytecode, then
8350 we generate an access method. */
8351 if (FIELD_PRIVATE (decl) && flag_emit_class_files)
8353 tree name = build_nested_field_access_methods (decl);
8354 access = build_nested_field_access_expr (lc, decl_ctx,
8355 name, access, NULL_TREE);
8357 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'
8358 for non-static fields.
8359 Once again we break the `private' access rule from a foreign
8360 class. */
8361 else if (is_static)
8363 tree class_name = DECL_NAME (TYPE_NAME (decl_ctx));
8364 access
8365 = make_qualified_primary (build_wfl_node (class_name), id, lc);
8367 else
8368 access = make_qualified_primary (access, id, lc);
8371 return resolve_expression_name (access, NULL);
8374 /* Return a nonzero value if DECL describes a field access across nested
8375 class boundaries. That is, DECL is in a class that either encloses,
8376 is enclosed by or shares a common enclosing class with, the class
8377 TYPE. */
8379 static int
8380 nested_field_access_p (tree type, tree decl)
8382 bool is_static = false;
8383 tree decl_type = DECL_CONTEXT (decl);
8384 tree type_root, decl_type_root;
8386 if (decl_type == type
8387 || (TREE_CODE (decl) != FIELD_DECL && TREE_CODE (decl) != VAR_DECL))
8388 return 0;
8390 if (!INNER_CLASS_TYPE_P (type)
8391 && !(TREE_CODE (decl_type) == RECORD_TYPE
8392 && INNER_CLASS_TYPE_P (decl_type)))
8393 return 0;
8395 is_static = FIELD_STATIC (decl);
8397 /* If TYPE extends the declaration context of the non-static
8398 field we're trying to access, then this isn't a nested field
8399 access we need to worry about. */
8400 if (!is_static && inherits_from_p (type, decl_type))
8401 return 0;
8403 for (type_root = type;
8404 DECL_CONTEXT (TYPE_NAME (type_root));
8405 type_root = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type_root))))
8407 if (type_root == decl_type)
8408 return 1;
8411 if (TREE_CODE (decl_type) == RECORD_TYPE
8412 && INNER_CLASS_TYPE_P (decl_type))
8414 for (decl_type_root = decl_type;
8415 DECL_CONTEXT (TYPE_NAME (decl_type_root));
8416 decl_type_root
8417 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (decl_type_root))))
8419 if (decl_type_root == type)
8420 return 1;
8423 else
8424 decl_type_root = decl_type;
8426 if (type_root == decl_type_root)
8427 return 1;
8429 /* Before we give up, see whether it is a non-static field
8430 inherited from the enclosing context we are considering. */
8431 if (!DECL_CONTEXT (TYPE_NAME (type_root))
8432 && !is_static
8433 && inherits_from_p (type_root, decl_type))
8434 return 1;
8436 return 0;
8439 /* Return a nonzero value if NODE represents a cross-nested-class
8440 access that has already been expanded. As a side effect, it returns
8441 the name of the field being accessed and the argument passed to the
8442 access function, suitable for a regeneration of the access method
8443 call if necessary. */
8445 static int
8446 nested_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8447 tree *arg)
8449 int identified = 0;
8451 if (TREE_CODE (node) != CALL_EXPR)
8452 return 0;
8454 /* Well, GCJ generates slightly different tree nodes when compiling
8455 to native or bytecodes. It's the case for function calls. */
8457 if (flag_emit_class_files
8458 && TREE_CODE (node) == CALL_EXPR
8459 && NESTED_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8460 identified = 1;
8461 else if (!flag_emit_class_files)
8463 node = TREE_OPERAND (node, 0);
8465 if (node && TREE_OPERAND (node, 0)
8466 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8468 node = TREE_OPERAND (node, 0);
8469 if (TREE_OPERAND (node, 0)
8470 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8471 && (NESTED_FIELD_ACCESS_IDENTIFIER_P
8472 (DECL_NAME (TREE_OPERAND (node, 0)))))
8473 identified = 1;
8477 if (identified && name && arg_type && arg)
8479 tree argument = TREE_OPERAND (node, 1);
8480 *name = DECL_NAME (TREE_OPERAND (node, 0));
8482 /* The accessors for static fields do not take in a this$<n> argument,
8483 so we take the class name from the accessor's context instead. */
8484 if (argument)
8486 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8487 *arg = TREE_VALUE (argument);
8489 else
8491 *arg_type = DECL_CONTEXT (TREE_OPERAND (node, 0));
8492 *arg = NULL_TREE;
8495 return identified;
8498 /* Detect in NODE cross-nested-class field read access and
8499 transform it into a write with RHS as an argument. This function
8500 is called from the java_complete_lhs when an assignment to a LHS can
8501 be identified. */
8503 static tree
8504 nested_field_access_fix (tree wfl, tree node, tree rhs)
8506 tree name, arg_type, arg;
8508 if (nested_field_expanded_access_p (node, &name, &arg_type, &arg))
8510 node = build_nested_field_access_expr (EXPR_WFL_LINECOL (wfl),
8511 arg_type, name, arg, rhs);
8512 return java_complete_tree (node);
8514 return NULL_TREE;
8517 /* Construct the expression that calls an access method:
8518 <type>.access$<n>(<arg1> [, <arg2>]);
8520 ARG2 can be NULL and will be omitted in that case. It will denote a
8521 read access. */
8523 static tree
8524 build_nested_field_access_expr (int lc, tree type, tree access_method_name,
8525 tree arg1, tree arg2)
8527 tree args, cn, access;
8529 if (arg1)
8530 args = build_tree_list (NULL_TREE, arg1);
8531 else
8532 args = NULL_TREE;
8534 if (arg2)
8536 if (args)
8537 args = tree_cons (NULL_TREE, arg2, args);
8538 else
8539 args = build_tree_list (NULL_TREE, arg2);
8542 access
8543 = build_method_invocation (build_wfl_node (access_method_name), args);
8544 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8546 return make_qualified_primary (cn, access, lc);
8549 /* Build the name of a synthetic accessor used to access class members
8550 across nested class boundaries. */
8552 static tree
8553 build_new_access_id (void)
8555 static int access_n_counter = 1;
8556 char buffer [128];
8558 sprintf (buffer, "access$%d", access_n_counter++);
8559 return get_identifier (buffer);
8562 /* Create the static access functions for the cross-nested-class field DECL.
8563 We define a read:
8564 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8565 return inst$.field;
8567 and a write access:
8568 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8569 TREE_TYPE (<field>) value$) {
8570 return inst$.field = value$;
8572 For static fields, these methods are generated without the instance
8573 parameter.
8574 We should have a usage flag on the DECL so we can lazily turn the ones
8575 we're using for code generation. FIXME.
8578 static tree
8579 build_nested_field_access_methods (tree decl)
8581 tree id, args, stmt, mdecl, class_name = NULL_TREE;
8582 bool is_static = FIELD_STATIC (decl);
8584 if (FIELD_NESTED_ACCESS_P (decl))
8585 return FIELD_NESTED_ACCESS (decl);
8587 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8589 /* Create the identifier and a function named after it. */
8590 id = build_new_access_id ();
8592 /* The identifier is marked as bearing the name of a generated write
8593 access function for outer field accessed from inner classes. */
8594 NESTED_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8596 /* Create the read access. */
8597 if (!is_static)
8599 args = build_tree_list (inst_id,
8600 build_pointer_type (DECL_CONTEXT (decl)));
8601 TREE_CHAIN (args) = end_params_node;
8602 stmt = make_qualified_primary (build_wfl_node (inst_id),
8603 build_wfl_node (DECL_NAME (decl)), 0);
8605 else
8607 args = end_params_node;
8608 class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
8609 stmt = make_qualified_primary (build_wfl_node (class_name),
8610 build_wfl_node (DECL_NAME (decl)), 0);
8612 stmt = build_return (0, stmt);
8613 mdecl = build_nested_field_access_method (DECL_CONTEXT (decl),
8614 TREE_TYPE (decl), id, args, stmt);
8615 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8617 /* Create the write access method. No write access for final variable */
8618 if (!FIELD_FINAL (decl))
8620 if (!is_static)
8622 args = build_tree_list (inst_id,
8623 build_pointer_type (DECL_CONTEXT (decl)));
8624 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8625 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8626 stmt = make_qualified_primary (build_wfl_node (inst_id),
8627 build_wfl_node (DECL_NAME (decl)),
8630 else
8632 args = build_tree_list (wpv_id, TREE_TYPE (decl));
8633 TREE_CHAIN (args) = end_params_node;
8634 stmt = make_qualified_primary (build_wfl_node (class_name),
8635 build_wfl_node (DECL_NAME (decl)),
8638 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8639 build_wfl_node (wpv_id)));
8640 mdecl = build_nested_field_access_method (DECL_CONTEXT (decl),
8641 TREE_TYPE (decl), id,
8642 args, stmt);
8644 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8646 /* Return the access name */
8647 return FIELD_NESTED_ACCESS (decl) = id;
8650 /* Build a field access method NAME. */
8652 static tree
8653 build_nested_field_access_method (tree class, tree type, tree name,
8654 tree args, tree body)
8656 tree saved_current_function_decl, mdecl;
8658 /* Create the method */
8659 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8660 fix_method_argument_names (args, mdecl);
8661 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8663 /* Attach the method body. */
8664 saved_current_function_decl = current_function_decl;
8665 start_artificial_method_body (mdecl);
8666 java_method_add_stmt (mdecl, body);
8667 end_artificial_method_body (mdecl);
8668 current_function_decl = saved_current_function_decl;
8670 return mdecl;
8674 /* This section deals with building access function necessary for
8675 certain kinds of method invocation from inner classes. */
8677 static tree
8678 build_outer_method_access_method (tree decl)
8680 tree saved_current_function_decl, mdecl;
8681 tree args = NULL_TREE, call_args = NULL_TREE;
8682 tree carg, id, body, class;
8683 char buffer [80];
8684 int parm_id_count = 0;
8686 /* Test this abort with an access to a private field */
8687 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8688 abort ();
8690 /* Check the cache first */
8691 if (DECL_FUNCTION_INNER_ACCESS (decl))
8692 return DECL_FUNCTION_INNER_ACCESS (decl);
8694 class = DECL_CONTEXT (decl);
8696 /* Obtain an access identifier and mark it */
8697 id = build_new_access_id ();
8698 NESTED_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8700 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8701 /* Create the arguments, as much as the original */
8702 for (; carg && carg != end_params_node;
8703 carg = TREE_CHAIN (carg))
8705 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8706 args = chainon (args, build_tree_list (get_identifier (buffer),
8707 TREE_VALUE (carg)));
8709 args = chainon (args, end_params_node);
8711 /* Create the method */
8712 mdecl = create_artificial_method (class, ACC_STATIC,
8713 TREE_TYPE (TREE_TYPE (decl)), id, args);
8714 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8715 /* There is a potential bug here. We should be able to use
8716 fix_method_argument_names, but then arg names get mixed up and
8717 eventually a constructor will have its this$0 altered and the
8718 outer context won't be assignment properly. The testcase is
8719 stub.java FIXME */
8720 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8722 /* Attach the method body. */
8723 saved_current_function_decl = current_function_decl;
8724 start_artificial_method_body (mdecl);
8726 /* The actual method invocation uses the same args. When invoking a
8727 static methods that way, we don't want to skip the first
8728 argument. */
8729 carg = args;
8730 if (!METHOD_STATIC (decl))
8731 carg = TREE_CHAIN (carg);
8732 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8733 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8734 call_args);
8736 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8737 call_args);
8738 if (!METHOD_STATIC (decl))
8739 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8740 body, 0);
8741 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8742 body = build_return (0, body);
8743 java_method_add_stmt (mdecl,body);
8744 end_artificial_method_body (mdecl);
8745 current_function_decl = saved_current_function_decl;
8747 /* Back tag the access function so it know what it accesses */
8748 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8750 /* Tag the current method so it knows it has an access generated */
8751 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8755 /* This section of the code deals with building expressions to access
8756 the enclosing instance of an inner class. The enclosing instance is
8757 kept in a generated field called this$<n>, with <n> being the
8758 inner class nesting level (starting from 0.) */
8760 /* Build an access to a given this$<n>, always chaining access call to
8761 others. Access methods to this$<n> are build on the fly if
8762 necessary. This CAN'T be used to solely access this$<n-1> from
8763 this$<n> (which alway yield to special cases and optimization, see
8764 for example build_nested_field_access). */
8766 static tree
8767 build_access_to_thisn (tree from, tree to, int lc)
8769 tree access = NULL_TREE;
8771 while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8773 if (!access)
8775 access = build_current_thisn (from);
8776 access = build_wfl_node (access);
8778 else
8780 tree access0_wfl, cn;
8782 maybe_build_thisn_access_method (from);
8783 access0_wfl = build_wfl_node (access0_identifier_node);
8784 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8785 EXPR_WFL_LINECOL (access0_wfl) = lc;
8786 access = build_tree_list (NULL_TREE, access);
8787 access = build_method_invocation (access0_wfl, access);
8788 access = make_qualified_primary (cn, access, lc);
8791 /* If FROM isn't an inner class, that's fine, we've done enough.
8792 What we're looking for can be accessed from there. */
8793 from = DECL_CONTEXT (TYPE_NAME (from));
8794 if (!from)
8795 break;
8796 from = TREE_TYPE (from);
8798 return access;
8801 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8802 is returned if nothing needs to be generated. Otherwise, the method
8803 generated and a method decl is returned.
8805 NOTE: These generated methods should be declared in a class file
8806 attribute so that they can't be referred to directly. */
8808 static tree
8809 maybe_build_thisn_access_method (tree type)
8811 tree mdecl, args, stmt, rtype;
8812 tree saved_current_function_decl;
8814 /* If TYPE is a top-level class, no access method is required.
8815 If there already is such an access method, bail out. */
8816 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8817 return NULL_TREE;
8819 /* We generate the method. The method looks like:
8820 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8822 args = build_tree_list (inst_id, build_pointer_type (type));
8823 TREE_CHAIN (args) = end_params_node;
8824 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8825 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8826 access0_identifier_node, args);
8827 fix_method_argument_names (args, mdecl);
8828 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8829 stmt = build_current_thisn (type);
8830 stmt = make_qualified_primary (build_wfl_node (inst_id),
8831 build_wfl_node (stmt), 0);
8832 stmt = build_return (0, stmt);
8834 saved_current_function_decl = current_function_decl;
8835 start_artificial_method_body (mdecl);
8836 java_method_add_stmt (mdecl, stmt);
8837 end_artificial_method_body (mdecl);
8838 current_function_decl = saved_current_function_decl;
8840 CLASS_ACCESS0_GENERATED_P (type) = 1;
8842 return mdecl;
8845 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8846 the first level of innerclassing. this$1 for the next one, etc...
8847 This function can be invoked with TYPE to NULL, available and then
8848 has to count the parser context. */
8850 static GTY(()) tree saved_thisn;
8851 static GTY(()) tree saved_type;
8853 static tree
8854 build_current_thisn (tree type)
8856 static int saved_i = -1;
8857 static int saved_type_i = 0;
8858 tree decl;
8859 char buffer [24];
8860 int i = 0;
8862 if (type)
8864 if (type == saved_type)
8865 i = saved_type_i;
8866 else
8868 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8869 decl; decl = DECL_CONTEXT (decl), i++)
8872 saved_type = type;
8873 saved_type_i = i;
8876 else
8877 i = list_length (GET_CPC_LIST ())-2;
8879 if (i == saved_i)
8880 return saved_thisn;
8882 sprintf (buffer, "this$%d", i);
8883 saved_i = i;
8884 saved_thisn = get_identifier (buffer);
8885 return saved_thisn;
8888 /* Return the assignment to the hidden enclosing context `this$<n>'
8889 by the second incoming parameter to the innerclass constructor. The
8890 form used is `this.this$<n> = this$<n>;'. */
8892 static tree
8893 build_thisn_assign (void)
8895 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8897 tree thisn = build_current_thisn (current_class);
8898 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8899 build_wfl_node (thisn), 0);
8900 tree rhs = build_wfl_node (thisn);
8901 #ifdef USE_MAPPED_LOCATION
8902 SET_EXPR_LOCATION (lhs, input_location);
8903 #else
8904 EXPR_WFL_SET_LINECOL (lhs, input_line, 0);
8905 #endif
8906 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8908 return NULL_TREE;
8912 /* Building the synthetic `class$' used to implement the `.class' 1.1
8913 extension for non primitive types. This method looks like:
8915 static Class class$(String type) throws NoClassDefFoundError
8917 try {return (java.lang.Class.forName (String));}
8918 catch (ClassNotFoundException e) {
8919 throw new NoClassDefFoundError(e.getMessage());}
8920 } */
8922 static GTY(()) tree get_message_wfl;
8923 static GTY(()) tree type_parm_wfl;
8925 static tree
8926 build_dot_class_method (tree class)
8928 #define BWF(S) build_wfl_node (get_identifier ((S)))
8929 #ifdef USE_MAPPED_LOCATION
8930 #define MQN(X,Y) make_qualified_name ((X), (Y), UNKNOWN_LOCATION)
8931 #else
8932 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8933 #endif
8934 tree args, tmp, saved_current_function_decl, mdecl, qual_name;
8935 tree stmt, throw_stmt;
8937 if (!get_message_wfl)
8939 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8940 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8943 /* Build the arguments */
8944 args = build_tree_list (get_identifier ("type$"),
8945 build_pointer_type (string_type_node));
8946 TREE_CHAIN (args) = end_params_node;
8948 /* Build the qualified name java.lang.Class.forName */
8949 tmp = MQN (MQN (MQN (BWF ("java"),
8950 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8952 /* Create the "class$" function */
8953 mdecl = create_artificial_method (class, ACC_STATIC,
8954 build_pointer_type (class_type_node),
8955 classdollar_identifier_node, args);
8956 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8957 BWF ("NoClassDefFoundError"));
8958 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE, qual_name);
8959 register_incomplete_type (JDEP_EXCEPTION, qual_name, NULL_TREE, NULL_TREE);
8960 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
8961 &TREE_VALUE (DECL_FUNCTION_THROWS (mdecl));
8963 /* We start by building the try block. We need to build:
8964 return (java.lang.Class.forName (type)); */
8965 stmt = build_method_invocation (tmp,
8966 build_tree_list (NULL_TREE, type_parm_wfl));
8967 stmt = build_return (0, stmt);
8969 /* Now onto the catch block. We start by building the expression
8970 throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8971 #ifdef USE_MAPPED_LOCATION
8972 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8973 get_message_wfl, UNKNOWN_LOCATION);
8974 #else
8975 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8976 get_message_wfl, 0);
8977 #endif
8978 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8980 /* Build new NoClassDefFoundError (_.getMessage) */
8981 throw_stmt = build_new_invocation
8982 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8983 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8985 /* Build the throw, (it's too early to use BUILD_THROW) */
8986 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8988 /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8989 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8990 BWF ("ClassNotFoundException"));
8991 stmt = encapsulate_with_try_catch (0, qual_name, stmt, throw_stmt);
8993 fix_method_argument_names (args, mdecl);
8994 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8995 saved_current_function_decl = current_function_decl;
8996 start_artificial_method_body (mdecl);
8997 java_method_add_stmt (mdecl, stmt);
8998 end_artificial_method_body (mdecl);
8999 current_function_decl = saved_current_function_decl;
9000 TYPE_DOT_CLASS (class) = mdecl;
9002 return mdecl;
9005 static tree
9006 build_dot_class_method_invocation (tree this_class, tree type)
9008 tree dot_class_method = TYPE_DOT_CLASS (this_class);
9009 tree sig_id, s, t;
9011 if (TYPE_ARRAY_P (type))
9012 sig_id = build_java_signature (type);
9013 else
9014 sig_id = DECL_NAME (TYPE_NAME (type));
9016 /* Ensure that the proper name separator is used */
9017 sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
9018 IDENTIFIER_LENGTH (sig_id));
9020 s = build_string (IDENTIFIER_LENGTH (sig_id),
9021 IDENTIFIER_POINTER (sig_id));
9022 t = build_method_invocation (build_wfl_node (DECL_NAME (dot_class_method)),
9023 build_tree_list (NULL_TREE, s));
9024 if (DECL_CONTEXT (dot_class_method) != this_class)
9026 tree class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (dot_class_method)));
9027 t = make_qualified_primary (build_wfl_node (class_name), t, 0);
9029 return t;
9032 /* This section of the code deals with constructor. */
9034 /* Craft a body for default constructor. Patch existing constructor
9035 bodies with call to super() and field initialization statements if
9036 necessary. */
9038 static void
9039 fix_constructors (tree mdecl)
9041 tree iii; /* Instance Initializer Invocation */
9042 tree *bodyp = &DECL_FUNCTION_BODY (mdecl);
9043 tree thisn_assign, compound = NULL_TREE;
9044 tree class_type = DECL_CONTEXT (mdecl);
9046 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
9047 return;
9048 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
9050 if (!*bodyp)
9052 /* It is an error for the compiler to generate a default
9053 constructor if the superclass doesn't have a constructor that
9054 takes no argument, or the same args for an anonymous class */
9055 tree sdecl = get_constructor_super (mdecl);
9056 if (sdecl == NULL_TREE)
9058 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
9059 tree save = DECL_NAME (mdecl);
9060 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
9061 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
9062 parse_error_context
9063 (lookup_cl (TYPE_NAME (class_type)),
9064 "No constructor matching %qs found in class %qs",
9065 lang_printable_name (mdecl, 2), n);
9066 DECL_NAME (mdecl) = save;
9069 if (ANONYMOUS_CLASS_P (class_type))
9071 /* Copy throws clause from the super constructor. */
9072 tree throws = DECL_FUNCTION_THROWS (sdecl);
9073 DECL_FUNCTION_THROWS (mdecl) = copy_list (throws);
9076 /* The constructor body must be crafted by hand. It's the
9077 constructor we defined when we realize we didn't have the
9078 CLASSNAME() constructor */
9079 start_artificial_method_body (mdecl);
9081 /* Insert an assignment to the this$<n> hidden field, if
9082 necessary */
9083 if ((thisn_assign = build_thisn_assign ()))
9084 java_method_add_stmt (mdecl, thisn_assign);
9086 /* We don't generate a super constructor invocation if we're
9087 compiling java.lang.Object. build_super_invocation takes care
9088 of that. */
9089 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
9091 /* FIXME */
9092 if ((iii = build_instinit_invocation (class_type)))
9093 java_method_add_stmt (mdecl, iii);
9095 end_artificial_method_body (mdecl);
9097 /* Search for an explicit constructor invocation */
9098 else
9100 int found = 0;
9101 int invokes_this = 0;
9102 tree main_block = BLOCK_EXPR_BODY (*bodyp);
9104 while (*bodyp)
9106 tree body = *bodyp;
9107 switch (TREE_CODE (body))
9109 case CALL_EXPR:
9110 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
9111 if (CALL_THIS_CONSTRUCTOR_P (body))
9112 invokes_this = 1;
9113 break;
9114 case COMPOUND_EXPR:
9115 case EXPR_WITH_FILE_LOCATION:
9116 bodyp = &TREE_OPERAND (body, 0);
9117 continue;
9118 case BLOCK:
9119 bodyp = &BLOCK_EXPR_BODY (body);
9120 continue;
9121 default:
9122 break;
9124 break;
9127 /* Generate the assignment to this$<n>, if necessary */
9128 if ((thisn_assign = build_thisn_assign ()))
9129 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
9131 /* The constructor is missing an invocation of super() */
9132 if (!found)
9133 compound = add_stmt_to_compound (compound, NULL_TREE,
9134 build_super_invocation (mdecl));
9135 /* Explicit super() invocation should take place before the
9136 instance initializer blocks. */
9137 else
9139 compound = add_stmt_to_compound (compound, NULL_TREE, *bodyp);
9140 *bodyp = build_java_empty_stmt ();
9143 DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
9145 /* Insert the instance initializer block right after. */
9146 if (!invokes_this && (iii = build_instinit_invocation (class_type)))
9147 compound = add_stmt_to_compound (compound, NULL_TREE, iii);
9149 /* Fix the constructor main block if we're adding extra stmts */
9150 if (compound)
9152 compound = add_stmt_to_compound (compound, NULL_TREE,
9153 BLOCK_EXPR_BODY (main_block));
9154 BLOCK_EXPR_BODY (main_block) = compound;
9159 /* Browse constructors in the super class, searching for a constructor
9160 that doesn't take any argument. Return the constructor if one is found,
9161 NULL_TREE otherwise. If the current class is an anonymous inner class,
9162 look for something that has the same signature. */
9163 static tree
9164 get_constructor_super (tree mdecl)
9166 tree class = CLASSTYPE_SUPER (current_class);
9167 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
9168 tree sdecl;
9170 if (!class)
9171 return NULL_TREE;
9173 if (ANONYMOUS_CLASS_P (current_class))
9175 tree mdecl_arg_type;
9176 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
9177 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9178 if (DECL_CONSTRUCTOR_P (sdecl))
9180 tree m_arg_type;
9181 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9182 if (super_inner)
9183 arg_type = TREE_CHAIN (arg_type);
9184 for (m_arg_type = mdecl_arg_type;
9185 (arg_type != end_params_node
9186 && m_arg_type != end_params_node);
9187 arg_type = TREE_CHAIN (arg_type),
9188 m_arg_type = TREE_CHAIN (m_arg_type))
9189 if (!valid_method_invocation_conversion_p
9190 (TREE_VALUE (arg_type),
9191 TREE_VALUE (m_arg_type)))
9192 break;
9194 if (arg_type == end_params_node && m_arg_type == end_params_node)
9195 return sdecl;
9198 else
9200 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9202 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9203 if (super_inner)
9204 arg = TREE_CHAIN (arg);
9205 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
9206 return sdecl;
9209 return NULL_TREE;
9212 /* Generate code for all context remembered for code generation. */
9214 static GTY(()) tree reversed_class_list;
9215 void
9216 java_expand_classes (void)
9218 int save_error_count = 0;
9219 static struct parser_ctxt *cur_ctxp = NULL;
9220 location_t save_location;
9222 java_parse_abort_on_error ();
9223 if (!(ctxp = ctxp_for_generation))
9224 return;
9225 java_layout_classes ();
9226 java_parse_abort_on_error ();
9227 save_location = input_location;
9229 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9231 tree current;
9232 for (current = cur_ctxp->class_list;
9233 current;
9234 current = TREE_CHAIN (current))
9235 gen_indirect_dispatch_tables (TREE_TYPE (current));
9238 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9240 ctxp = cur_ctxp;
9241 input_location = ctxp->file_start_location;
9242 lang_init_source (2); /* Error msgs have method prototypes */
9243 java_complete_expand_classes (); /* Complete and expand classes */
9244 java_parse_abort_on_error ();
9246 input_location = save_location;
9248 /* Find anonymous classes and expand their constructor. This extra pass is
9249 necessary because the constructor itself is only generated when the
9250 method in which it is defined is expanded. */
9251 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9253 tree current;
9254 ctxp = cur_ctxp;
9255 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9257 output_class = current_class = TREE_TYPE (current);
9258 if (ANONYMOUS_CLASS_P (current_class))
9260 tree d;
9261 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9263 if (DECL_CONSTRUCTOR_P (d))
9265 java_complete_expand_method (d);
9266 break; /* There is only one constructor. */
9273 /* Expanding the constructors of anonymous classes generates access
9274 methods. Scan all the methods looking for null DECL_RESULTs --
9275 this will be the case if a method hasn't been expanded. */
9276 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9278 tree current;
9279 ctxp = cur_ctxp;
9280 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9282 tree d;
9283 output_class = current_class = TREE_TYPE (current);
9284 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9286 if (DECL_RESULT (d) == NULL_TREE)
9287 java_complete_expand_method (d);
9292 /* ??? Instead of all this we could iterate around the list of
9293 classes until there were no more un-expanded methods. It would
9294 take a little longer -- one pass over the whole list of methods
9295 -- but it would be simpler. Like this: */
9296 #if 0
9298 int something_changed;
9302 something_changed = 0;
9303 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9305 tree current;
9306 ctxp = cur_ctxp;
9307 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9309 tree d;
9310 output_class = current_class = TREE_TYPE (current);
9311 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9313 if (DECL_RESULT (d) == NULL_TREE)
9315 something_changed = 1;
9316 java_complete_expand_method (d);
9322 while (something_changed);
9324 #endif
9326 /* If we've found error at that stage, don't try to generate
9327 anything, unless we're checking the syntax only
9328 (but not using -fsyntax-only for the purpose of generating
9329 bytecode). */
9330 if (java_error_count
9331 && (!flag_syntax_only && !flag_emit_class_files))
9332 return;
9334 /* Now things are stable, go for generation of the class data. */
9336 /* We pessimistically marked all methods and fields external until
9337 we knew what set of classes we were planning to compile. Now mark
9338 those that will be generated locally as not external. */
9339 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9341 tree current;
9342 ctxp = cur_ctxp;
9343 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9344 java_mark_class_local (TREE_TYPE (current));
9347 /* Compile the classes. */
9348 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9350 tree current;
9351 reversed_class_list = NULL;
9353 ctxp = cur_ctxp;
9355 /* We write out the classes in reverse order. This ensures that
9356 inner classes are written before their containing classes,
9357 which is important for parallel builds. Otherwise, the
9358 class file for the outer class may be found, but the class
9359 file for the inner class may not be present. In that
9360 situation, the compiler cannot fall back to the original
9361 source, having already read the outer class, so we must
9362 prevent that situation. */
9363 for (current = ctxp->class_list;
9364 current;
9365 current = TREE_CHAIN (current))
9366 reversed_class_list
9367 = tree_cons (NULL_TREE, current, reversed_class_list);
9369 for (current = reversed_class_list;
9370 current;
9371 current = TREE_CHAIN (current))
9373 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
9374 if (flag_emit_class_files)
9375 write_classfile (current_class);
9376 else if (! flag_syntax_only)
9377 java_expand_method_bodies (current_class);
9382 void
9383 java_finish_classes (void)
9385 static struct parser_ctxt *cur_ctxp = NULL;
9386 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9388 tree current;
9389 ctxp = cur_ctxp;
9390 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9392 output_class = current_class = TREE_TYPE (current);
9393 finish_class ();
9398 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9399 a tree list node containing RIGHT. Fore coming RIGHTs will be
9400 chained to this hook. LOCATION contains the location of the
9401 separating `.' operator. */
9403 static tree
9404 make_qualified_primary (tree primary, tree right, int location)
9406 tree wfl;
9408 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9409 wfl = build_wfl_wrap (primary, location);
9410 else
9412 wfl = primary;
9413 /* If wfl wasn't qualified, we build a first anchor */
9414 if (!EXPR_WFL_QUALIFICATION (wfl))
9415 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9418 /* And chain them */
9419 EXPR_WFL_LINECOL (right) = location;
9420 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9421 PRIMARY_P (wfl) = 1;
9422 return wfl;
9425 /* Simple merge of two name separated by a `.' */
9427 static tree
9428 merge_qualified_name (tree left, tree right)
9430 tree node;
9431 if (!left && !right)
9432 return NULL_TREE;
9434 if (!left)
9435 return right;
9437 if (!right)
9438 return left;
9440 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9441 IDENTIFIER_LENGTH (left));
9442 obstack_1grow (&temporary_obstack, '.');
9443 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9444 IDENTIFIER_LENGTH (right));
9445 node = get_identifier (obstack_base (&temporary_obstack));
9446 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9447 QUALIFIED_P (node) = 1;
9448 return node;
9451 /* Merge the two parts of a qualified name into LEFT. Set the
9452 location information of the resulting node to LOCATION, usually
9453 inherited from the location information of the `.' operator. */
9455 static tree
9456 make_qualified_name (tree left, tree right,
9457 #ifdef USE_MAPPED_LOCATION
9458 source_location location
9459 #else
9460 int location
9461 #endif
9464 #ifdef USE_COMPONENT_REF
9465 tree node = build3 (COMPONENT_REF, NULL_TREE, left, right, NULL_TREE);
9466 SET_EXPR_LOCATION (node, location);
9467 return node;
9468 #else
9469 tree left_id = EXPR_WFL_NODE (left);
9470 tree right_id = EXPR_WFL_NODE (right);
9471 tree wfl, merge;
9473 merge = merge_qualified_name (left_id, right_id);
9475 /* Left wasn't qualified and is now qualified */
9476 #ifdef USE_MAPPED_LOCATION
9477 if (!QUALIFIED_P (left_id))
9479 tree wfl = build_expr_wfl (left_id, EXPR_LOCATION (left));
9480 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9483 wfl = build_expr_wfl (right_id, location);
9484 #else
9485 if (!QUALIFIED_P (left_id))
9487 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9488 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9489 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9492 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9493 EXPR_WFL_LINECOL (wfl) = location;
9494 #endif
9495 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9496 EXPR_WFL_NODE (left) = merge;
9497 return left;
9498 #endif
9501 /* Extract the last identifier component of the qualified in WFL. The
9502 last identifier is removed from the linked list */
9504 static tree
9505 cut_identifier_in_qualified (tree wfl)
9507 tree q;
9508 tree previous = NULL_TREE;
9509 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9510 if (!TREE_CHAIN (q))
9512 if (!previous)
9513 /* Operating on a non qualified qualified WFL. */
9514 abort ();
9516 TREE_CHAIN (previous) = NULL_TREE;
9517 return TREE_PURPOSE (q);
9521 /* Resolve the expression name NAME. Return its decl. */
9523 static tree
9524 resolve_expression_name (tree id, tree *orig)
9526 tree name = EXPR_WFL_NODE (id);
9527 tree decl;
9529 /* 6.5.5.1: Simple expression names */
9530 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9532 /* 15.13.1: NAME can appear within the scope of a local variable
9533 declaration */
9534 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9535 return decl;
9537 /* 15.13.1: NAME can appear within a class declaration */
9538 else
9540 decl = lookup_field_wrapper (current_class, name);
9541 if (decl)
9543 tree access = NULL_TREE;
9544 int fs = FIELD_STATIC (decl);
9546 /* If we're accessing an outer scope local alias, make
9547 sure we change the name of the field we're going to
9548 build access to. */
9549 if (FIELD_LOCAL_ALIAS_USED (decl))
9550 name = DECL_NAME (decl);
9552 check_deprecation (id, decl);
9554 /* Instance variable (8.3.1.1) can't appear within
9555 static method, static initializer or initializer for
9556 a static variable. */
9557 if (!fs && METHOD_STATIC (current_function_decl))
9559 static_ref_err (id, name, current_class);
9560 return error_mark_node;
9562 /* Instance variables can't appear as an argument of
9563 an explicit constructor invocation */
9564 if (!fs && ctxp->explicit_constructor_p
9565 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9567 parse_error_context
9568 (id, "Can't reference %qs before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9569 return error_mark_node;
9572 /* If we're processing an inner class and we're trying
9573 to access a field belonging to an outer class, build
9574 the access to the field. */
9575 if (nested_field_access_p (current_class, decl))
9577 if (!fs && CLASS_STATIC (TYPE_NAME (current_class)))
9579 static_ref_err (id, DECL_NAME (decl), current_class);
9580 return error_mark_node;
9582 access = build_nested_field_access (id, decl);
9583 if (orig)
9584 *orig = access;
9585 return access;
9588 /* Otherwise build what it takes to access the field */
9589 access = build_field_ref ((fs ? NULL_TREE : current_this),
9590 DECL_CONTEXT (decl), name);
9591 if (fs)
9592 access = maybe_build_class_init_for_field (decl, access);
9593 /* We may be asked to save the real field access node */
9594 if (orig)
9595 *orig = access;
9596 /* Last check: can we access the field? */
9597 if (not_accessible_p (current_class, decl, NULL_TREE, 0))
9599 not_accessible_field_error (id, decl);
9600 return error_mark_node;
9602 /* And we return what we got */
9603 return access;
9605 /* Fall down to error report on undefined variable */
9608 /* 6.5.5.2 Qualified Expression Names */
9609 else
9611 if (orig)
9612 *orig = NULL_TREE;
9613 qualify_ambiguous_name (id);
9614 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9615 /* 15.10.2: Accessing Superclass Members using super */
9616 return resolve_field_access (id, orig, NULL);
9619 /* We've got an error here */
9620 if (INNER_CLASS_TYPE_P (current_class))
9621 parse_error_context (id,
9622 "Local variable %qs can't be accessed from within the inner class %qs unless it is declared final",
9623 IDENTIFIER_POINTER (name),
9624 IDENTIFIER_POINTER (DECL_NAME
9625 (TYPE_NAME (current_class))));
9626 else
9627 parse_error_context (id, "Undefined variable %qs",
9628 IDENTIFIER_POINTER (name));
9630 return error_mark_node;
9633 static void
9634 static_ref_err (tree wfl, tree field_id, tree class_type)
9636 parse_error_context
9637 (wfl,
9638 "Can't make a static reference to nonstatic variable %qs in class %qs",
9639 IDENTIFIER_POINTER (field_id),
9640 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9643 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9644 We return something suitable to generate the field access. We also
9645 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9646 recipient's address can be null. */
9648 static tree
9649 resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9651 int is_static = 0;
9652 tree field_ref;
9653 tree decl = NULL_TREE, where_found, type_found;
9655 if (resolve_qualified_expression_name (qual_wfl, &decl,
9656 &where_found, &type_found))
9657 return error_mark_node;
9659 /* Resolve the LENGTH field of an array here */
9660 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9661 && type_found && TYPE_ARRAY_P (type_found)
9662 && ! flag_emit_class_files)
9664 tree length = build_java_array_length_access (where_found);
9665 field_ref = length;
9667 /* In case we're dealing with a static array, we need to
9668 initialize its class before the array length can be fetched. */
9669 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9671 build_static_field_ref (where_found);
9672 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9675 /* We might have been trying to resolve field.method(). In which
9676 case, the resolution is over and decl is the answer */
9677 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9678 field_ref = decl;
9679 else if (JDECL_P (decl))
9681 if (!type_found)
9682 type_found = DECL_CONTEXT (decl);
9683 is_static = FIELD_STATIC (decl);
9684 field_ref = build_field_ref ((is_static ?
9685 NULL_TREE : where_found),
9686 type_found, DECL_NAME (decl));
9687 if (field_ref == error_mark_node)
9688 return error_mark_node;
9689 if (is_static)
9690 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9692 /* If we're looking at a static field, we may need to generate a
9693 class initialization for it. This can happen when the access
9694 looks like `field.ref', where `field' is a static field in an
9695 interface we implement. */
9696 if (!flag_emit_class_files
9697 && TREE_CODE (where_found) == VAR_DECL
9698 && FIELD_STATIC (where_found))
9700 build_static_field_ref (where_found);
9701 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9704 else
9705 field_ref = decl;
9707 if (field_decl)
9708 *field_decl = decl;
9709 if (field_type)
9710 *field_type = (QUAL_DECL_TYPE (decl) ?
9711 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9712 return field_ref;
9715 /* If NODE is an access to a static field, strip out the class
9716 initialization part and return the field decl, otherwise, return
9717 NODE. */
9719 tree
9720 extract_field_decl (tree node)
9722 if (TREE_CODE (node) == COMPOUND_EXPR)
9724 tree op1 = TREE_OPERAND (node, 1);
9725 if (TREE_CODE (op1) == COMPOUND_EXPR)
9727 tree call = TREE_OPERAND (op1, 0);
9728 if (TREE_CODE (call) == CALL_EXPR
9729 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9730 && (TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9731 == soft_initclass_node))
9732 return TREE_OPERAND (op1, 1);
9734 else if (JDECL_P (op1))
9735 return op1;
9737 return node;
9740 /* 6.5.5.2: Qualified Expression Names */
9742 static int
9743 resolve_qualified_expression_name (tree wfl, tree *found_decl,
9744 tree *where_found, tree *type_found)
9746 int from_type = 0; /* Field search initiated from a type */
9747 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9748 int previous_call_static = 0;
9749 int is_static;
9750 tree decl = NULL_TREE, type = NULL_TREE, q;
9751 /* For certain for of inner class instantiation */
9752 tree saved_current, saved_this;
9753 #define RESTORE_THIS_AND_CURRENT_CLASS \
9754 { current_class = saved_current; current_this = saved_this;}
9756 *type_found = *where_found = NULL_TREE;
9758 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9760 tree qual_wfl = QUAL_WFL (q);
9761 tree ret_decl; /* for EH checking */
9762 #ifdef USE_MAPPED_LOCATION
9763 source_location location; /* for EH checking */
9764 #else
9765 int location; /* for EH checking */
9766 #endif
9768 /* 15.10.1 Field Access Using a Primary */
9769 switch (TREE_CODE (qual_wfl))
9771 case CALL_EXPR:
9772 case NEW_CLASS_EXPR:
9773 /* If the access to the function call is a non static field,
9774 build the code to access it. */
9775 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9777 decl = maybe_access_field (decl, *where_found,
9778 DECL_CONTEXT (decl));
9779 if (decl == error_mark_node)
9780 return 1;
9783 /* And code for the function call */
9784 if (complete_function_arguments (qual_wfl))
9785 return 1;
9787 /* We might have to setup a new current class and a new this
9788 for the search of an inner class, relative to the type of
9789 a expression resolved as `decl'. The current values are
9790 saved and restored shortly after */
9791 saved_current = current_class;
9792 saved_this = current_this;
9793 if (decl
9794 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9795 || from_qualified_this))
9797 /* If we still have `from_qualified_this', we have the form
9798 <T>.this.f() and we need to build <T>.this */
9799 if (from_qualified_this)
9801 decl = build_access_to_thisn (current_class, type, 0);
9802 decl = java_complete_tree (decl);
9803 type = TREE_TYPE (TREE_TYPE (decl));
9805 current_class = type;
9806 current_this = decl;
9807 from_qualified_this = 0;
9810 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9811 CALL_USING_SUPER (qual_wfl) = 1;
9812 #ifdef USE_MAPPED_LOCATION
9813 location = (TREE_CODE (qual_wfl) == CALL_EXPR
9814 ? EXPR_LOCATION (TREE_OPERAND (qual_wfl, 0))
9815 : UNKNOWN_LOCATION);
9816 #else
9817 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9818 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9819 #endif
9820 *where_found = patch_method_invocation (qual_wfl, decl, type,
9821 from_super,
9822 &is_static, &ret_decl);
9823 from_super = 0;
9824 if (*where_found == error_mark_node)
9826 RESTORE_THIS_AND_CURRENT_CLASS;
9827 return 1;
9829 *type_found = type = QUAL_DECL_TYPE (*where_found);
9831 *where_found = force_evaluation_order (*where_found);
9833 /* If we're creating an inner class instance, check for that
9834 an enclosing instance is in scope */
9835 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9836 && INNER_ENCLOSING_SCOPE_CHECK (type))
9838 parse_error_context
9839 (qual_wfl, "No enclosing instance for inner class %qs is in scope%s",
9840 lang_printable_name (type, 0),
9841 (!current_this ? "" :
9842 "; an explicit one must be provided when creating this inner class"));
9843 RESTORE_THIS_AND_CURRENT_CLASS;
9844 return 1;
9847 /* In case we had to change then to resolve a inner class
9848 instantiation using a primary qualified by a `new' */
9849 RESTORE_THIS_AND_CURRENT_CLASS;
9851 #ifdef USE_MAPPED_LOCATION
9852 if (location != UNKNOWN_LOCATION)
9853 #else
9854 if (location)
9855 #endif
9857 tree arguments = NULL_TREE;
9858 if (TREE_CODE (qual_wfl) == CALL_EXPR
9859 && TREE_OPERAND (qual_wfl, 1) != NULL_TREE)
9860 arguments = TREE_VALUE (TREE_OPERAND (qual_wfl, 1));
9861 check_thrown_exceptions (location, ret_decl, arguments);
9864 /* If the previous call was static and this one is too,
9865 build a compound expression to hold the two (because in
9866 that case, previous function calls aren't transported as
9867 forcoming function's argument. */
9868 if (previous_call_static && is_static)
9870 /* We must set CAN_COMPLETE_NORMALLY for the first call
9871 since it is done nowhere else. */
9872 CAN_COMPLETE_NORMALLY (decl) = 1;
9873 decl = build2 (COMPOUND_EXPR, TREE_TYPE (*where_found),
9874 decl, *where_found);
9875 TREE_SIDE_EFFECTS (decl) = 1;
9877 else
9879 previous_call_static = is_static;
9880 decl = *where_found;
9882 from_type = 0;
9883 continue;
9885 case NEW_ARRAY_EXPR:
9886 case NEW_ANONYMOUS_ARRAY_EXPR:
9887 *where_found = decl = java_complete_tree (qual_wfl);
9888 if (decl == error_mark_node)
9889 return 1;
9890 *type_found = type = QUAL_DECL_TYPE (decl);
9891 continue;
9893 case CONVERT_EXPR:
9894 *where_found = decl = java_complete_tree (qual_wfl);
9895 if (decl == error_mark_node)
9896 return 1;
9897 *type_found = type = QUAL_DECL_TYPE (decl);
9898 from_cast = 1;
9899 continue;
9901 case CONDITIONAL_EXPR:
9902 case STRING_CST:
9903 case MODIFY_EXPR:
9904 *where_found = decl = java_complete_tree (qual_wfl);
9905 if (decl == error_mark_node)
9906 return 1;
9907 *type_found = type = QUAL_DECL_TYPE (decl);
9908 continue;
9910 case ARRAY_REF:
9911 /* If the access to the function call is a non static field,
9912 build the code to access it. */
9913 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9915 decl = maybe_access_field (decl, *where_found, type);
9916 if (decl == error_mark_node)
9917 return 1;
9919 /* And code for the array reference expression */
9920 decl = java_complete_tree (qual_wfl);
9921 if (decl == error_mark_node)
9922 return 1;
9923 type = QUAL_DECL_TYPE (decl);
9924 continue;
9926 case PLUS_EXPR:
9927 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9928 return 1;
9929 if ((type = patch_string (decl)))
9930 decl = type;
9931 *where_found = QUAL_RESOLUTION (q) = decl;
9932 *type_found = type = TREE_TYPE (decl);
9933 break;
9935 case CLASS_LITERAL:
9936 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9937 return 1;
9938 *where_found = QUAL_RESOLUTION (q) = decl;
9939 *type_found = type = TREE_TYPE (decl);
9940 break;
9942 default:
9943 /* Fix for -Wall Just go to the next statement. Don't
9944 continue */
9945 break;
9948 /* If we fall here, we weren't processing a (static) function call. */
9949 previous_call_static = 0;
9951 /* It can be the keyword THIS */
9952 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9953 && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9955 if (!current_this)
9957 parse_error_context
9958 (wfl, "Keyword %<this%> used outside allowed context");
9959 return 1;
9961 if (ctxp->explicit_constructor_p
9962 && type == current_class)
9964 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
9965 return 1;
9967 /* We have to generate code for intermediate access */
9968 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9970 *where_found = decl = current_this;
9971 *type_found = type = QUAL_DECL_TYPE (decl);
9973 /* We're trying to access the this from somewhere else. Make sure
9974 it's allowed before doing so. */
9975 else
9977 if (!enclosing_context_p (type, current_class))
9979 char *p = xstrdup (lang_printable_name (type, 0));
9980 parse_error_context (qual_wfl, "Can't use variable %<%s.this%>: type %qs isn't an outer type of type %qs",
9981 p, p,
9982 lang_printable_name (current_class, 0));
9983 free (p);
9984 return 1;
9986 from_qualified_this = 1;
9987 /* If there's nothing else after that, we need to
9988 produce something now, otherwise, the section of the
9989 code that needs to produce <T>.this will generate
9990 what is necessary. */
9991 if (!TREE_CHAIN (q))
9993 decl = build_access_to_thisn (current_class, type, 0);
9994 *where_found = decl = java_complete_tree (decl);
9995 *type_found = type = TREE_TYPE (decl);
9999 from_type = 0;
10000 continue;
10003 /* 15.10.2 Accessing Superclass Members using SUPER */
10004 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
10005 && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
10007 tree node;
10008 /* Check on the restricted use of SUPER */
10009 if (METHOD_STATIC (current_function_decl)
10010 || current_class == object_type_node)
10012 parse_error_context
10013 (wfl, "Keyword %<super%> used outside allowed context");
10014 return 1;
10016 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
10017 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
10018 CLASSTYPE_SUPER (current_class),
10019 build_this (EXPR_WFL_LINECOL (qual_wfl)));
10020 *where_found = decl = java_complete_tree (node);
10021 if (decl == error_mark_node)
10022 return 1;
10023 *type_found = type = QUAL_DECL_TYPE (decl);
10024 from_super = from_type = 1;
10025 continue;
10028 /* 15.13.1: Can't search for field name in packages, so we
10029 assume a variable/class name was meant. */
10030 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10032 tree name;
10033 if ((decl = resolve_package (wfl, &q, &name)))
10035 tree list;
10036 *where_found = decl;
10038 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10040 /* We want to be absolutely sure that the class is laid
10041 out. We're going to search something inside it. */
10042 *type_found = type = TREE_TYPE (decl);
10043 layout_class (type);
10044 from_type = 1;
10046 /* Fix them all the way down, if any are left. */
10047 if (q)
10049 list = TREE_CHAIN (q);
10050 while (list)
10052 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
10053 list = TREE_CHAIN (list);
10057 else
10059 if (from_super || from_cast)
10060 parse_error_context
10061 ((from_cast ? qual_wfl : wfl),
10062 "No variable %qs defined in class %qs",
10063 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10064 lang_printable_name (type, 0));
10065 else
10066 parse_error_context
10067 (qual_wfl, "Undefined variable or class name: %qs",
10068 IDENTIFIER_POINTER (name));
10069 return 1;
10073 /* We have a type name. It's been already resolved when the
10074 expression was qualified. */
10075 else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
10077 decl = QUAL_RESOLUTION (q);
10079 /* Sneak preview. If next we see a `new', we're facing a
10080 qualification which resulted in a type being selected
10081 instead of a field. Report the error. */
10082 if(TREE_CHAIN (q)
10083 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
10085 parse_error_context (qual_wfl, "Undefined variable %qs",
10086 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10087 return 1;
10090 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10092 check_deprecation (qual_wfl, decl);
10094 type = TREE_TYPE (decl);
10095 from_type = 1;
10097 /* We resolve an expression name */
10098 else
10100 tree field_decl = NULL_TREE;
10102 /* If there exists an early resolution, use it. That occurs
10103 only once and we know that there are more things to
10104 come. Don't do that when processing something after SUPER
10105 (we need more thing to be put in place below */
10106 if (!from_super && QUAL_RESOLUTION (q))
10108 decl = QUAL_RESOLUTION (q);
10109 if (!type)
10111 if (TREE_CODE (decl) == FIELD_DECL
10112 || TREE_CODE (decl) == VAR_DECL)
10114 if (TREE_CODE (decl) == FIELD_DECL
10115 && !FIELD_STATIC (decl))
10117 if (current_this)
10118 *where_found = current_this;
10119 else
10121 static_ref_err (qual_wfl, DECL_NAME (decl),
10122 current_class);
10123 return 1;
10126 else
10128 *where_found = TREE_TYPE (decl);
10129 if (TREE_CODE (*where_found) == POINTER_TYPE)
10130 *where_found = TREE_TYPE (*where_found);
10132 if (nested_field_access_p (current_class, decl))
10133 decl = build_nested_field_access (qual_wfl, decl);
10135 else
10137 *where_found = TREE_TYPE (decl);
10138 if (TREE_CODE (*where_found) == POINTER_TYPE)
10139 *where_found = TREE_TYPE (*where_found);
10144 /* Report and error if we're using a numerical literal as a
10145 qualifier. It can only be an INTEGER_CST. */
10146 else if (TREE_CODE (qual_wfl) == INTEGER_CST)
10148 parse_error_context
10149 (wfl, "Can't use type %qs as a qualifier",
10150 lang_printable_name (TREE_TYPE (qual_wfl), 0));
10151 return 1;
10154 /* We have to search for a field, knowing the type of its
10155 container. The flag FROM_TYPE indicates that we resolved
10156 the last member of the expression as a type name, which
10157 means that for the resolution of this field, we'll look
10158 for other errors than if it was resolved as a member of
10159 an other field. */
10160 else
10162 int is_static;
10163 tree field_decl_type; /* For layout */
10165 if (!from_type && !JREFERENCE_TYPE_P (type))
10167 parse_error_context
10168 (qual_wfl, "Attempt to reference field %qs in %<%s %s%>",
10169 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10170 lang_printable_name (type, 0),
10171 IDENTIFIER_POINTER (DECL_NAME (decl)));
10172 return 1;
10175 field_decl = lookup_field_wrapper (type,
10176 EXPR_WFL_NODE (qual_wfl));
10178 /* Maybe what we're trying to access to is an inner
10179 class, only if decl is a TYPE_DECL. */
10180 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
10182 tree ptr, inner_decl;
10184 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
10185 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
10186 if (inner_decl)
10188 check_inner_class_access (inner_decl, decl, qual_wfl);
10189 type = TREE_TYPE (inner_decl);
10190 decl = inner_decl;
10191 from_type = 1;
10192 continue;
10196 if (field_decl == NULL_TREE)
10198 parse_error_context
10199 (qual_wfl, "No variable %qs defined in type %qs",
10200 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10201 GET_TYPE_NAME (type));
10202 return 1;
10204 if (field_decl == error_mark_node)
10205 return 1;
10207 /* Layout the type of field_decl, since we may need
10208 it. Don't do primitive types or loaded classes. The
10209 situation of non primitive arrays may not handled
10210 properly here. FIXME */
10211 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10212 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
10213 else
10214 field_decl_type = TREE_TYPE (field_decl);
10215 if (!JPRIMITIVE_TYPE_P (field_decl_type)
10216 && !CLASS_LOADED_P (field_decl_type)
10217 && !TYPE_ARRAY_P (field_decl_type))
10218 resolve_and_layout (field_decl_type, NULL_TREE);
10220 /* Check on accessibility here */
10221 if (not_accessible_p (current_class, field_decl,
10222 *type_found, from_super))
10223 return not_accessible_field_error (qual_wfl,field_decl);
10224 check_deprecation (qual_wfl, field_decl);
10226 /* There are things to check when fields are accessed
10227 from type. There are no restrictions on a static
10228 declaration of the field when it is accessed from an
10229 interface */
10230 is_static = FIELD_STATIC (field_decl);
10231 if (!from_super && from_type
10232 && !TYPE_INTERFACE_P (type)
10233 && !is_static
10234 && (current_function_decl
10235 && METHOD_STATIC (current_function_decl)))
10237 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
10238 return 1;
10240 from_cast = from_super = 0;
10242 /* If it's an access from a type but isn't static, we
10243 make it relative to `this'. */
10244 if (!is_static && from_type)
10245 decl = current_this;
10247 /* If we need to generate something to get a proper
10248 handle on what this field is accessed from, do it
10249 now. */
10250 if (!is_static)
10252 decl = maybe_access_field (decl, *where_found, *type_found);
10253 if (decl == error_mark_node)
10254 return 1;
10257 /* We want to keep the location where we found it, and the
10258 type we found. */
10259 *where_found = decl;
10260 *type_found = type;
10262 /* Generate the correct expression for field access from
10263 qualified this */
10264 if (from_qualified_this)
10266 field_decl
10267 = build_nested_field_access (qual_wfl, field_decl);
10268 from_qualified_this = 0;
10271 /* If needed, generate accessors for static field access. */
10272 if (is_static
10273 && FIELD_PRIVATE (field_decl)
10274 && flag_emit_class_files
10275 && nested_field_access_p (current_class, field_decl))
10276 field_decl = build_nested_field_access (qual_wfl, field_decl);
10278 /* This is the decl found and eventually the next one to
10279 search from */
10280 decl = field_decl;
10282 from_type = 0;
10283 type = QUAL_DECL_TYPE (decl);
10285 /* Sneak preview. If decl is qualified by a `new', report
10286 the error here to be accurate on the peculiar construct */
10287 if (TREE_CHAIN (q)
10288 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
10289 && !JREFERENCE_TYPE_P (type))
10291 parse_error_context (qual_wfl, "Attempt to reference field %<new%> in a %qs",
10292 lang_printable_name (type, 0));
10293 return 1;
10296 /* `q' might have changed due to a after package resolution
10297 re-qualification */
10298 if (!q)
10299 break;
10301 *found_decl = decl;
10302 return 0;
10305 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
10306 can't be accessed from REFERENCE (a record type). If MEMBER
10307 features a protected access, we then use WHERE which, if non null,
10308 holds the type of MEMBER's access that is checked against
10309 6.6.2.1. This function should be used when decl is a field or a
10310 method. */
10312 static int
10313 not_accessible_p (tree reference, tree member, tree where, int from_super)
10315 int access_flag = get_access_flags_from_decl (member);
10316 bool is_static = false;
10318 if (TREE_CODE (member) == FIELD_DECL ||
10319 TREE_CODE (member) == VAR_DECL)
10320 is_static = FIELD_STATIC (member);
10321 else
10322 is_static = METHOD_STATIC (member);
10324 /* Access always granted for members declared public */
10325 if (access_flag & ACC_PUBLIC)
10326 return 0;
10328 /* Check access on protected members */
10329 if (access_flag & ACC_PROTECTED)
10331 /* Access granted if it occurs from within the package
10332 containing the class in which the protected member is
10333 declared */
10334 if (class_in_current_package (DECL_CONTEXT (member)))
10335 return 0;
10337 /* If accessed with the form `super.member', then access is granted */
10338 if (from_super)
10339 return 0;
10341 /* If WHERE is active, access was made through a qualifier. For
10342 non-static members, access is granted if the type of the qualifier
10343 is or is a sublass of the type the access is made from (6.6.2.1.) */
10344 if (where && !is_static)
10346 while (reference)
10348 if (inherits_from_p (where, reference))
10349 return 0;
10350 if (INNER_CLASS_TYPE_P (reference))
10351 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10352 else
10353 break;
10355 return 1;
10358 /* Otherwise, access is granted if occurring from within the class
10359 where member is declared, or a subclass of it. */
10360 while (reference)
10362 if (inherits_from_p (reference, DECL_CONTEXT (member)))
10363 return 0;
10364 if (INNER_CLASS_TYPE_P (reference))
10365 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10366 else
10367 break;
10369 return 1;
10372 /* Check access on private members. Access is granted only if it
10373 occurs from within the class in which it is declared -- that does
10374 it for innerclasses too. */
10375 if (access_flag & ACC_PRIVATE)
10377 if (reference == DECL_CONTEXT (member) ||
10378 common_enclosing_context_p (DECL_CONTEXT (member), reference))
10379 return 0;
10380 return 1;
10383 /* Default access is permitted only when occurring from within the
10384 package in which the context (MEMBER) is declared. */
10385 return !class_in_current_package (DECL_CONTEXT (member));
10388 /* Test deprecated decl access. */
10389 static void
10390 check_deprecation (tree wfl, tree decl)
10392 const char *file;
10393 tree elt;
10395 if (! warn_deprecated)
10396 return;
10398 /* We want to look at the element type of arrays here, so we strip
10399 all surrounding array types. */
10400 if (TYPE_ARRAY_P (TREE_TYPE (decl)))
10402 elt = TREE_TYPE (decl);
10403 while (TYPE_ARRAY_P (elt))
10404 elt = TYPE_ARRAY_ELEMENT (elt);
10405 /* We'll end up with a pointer type, so we use TREE_TYPE to go
10406 to the record. */
10407 decl = TYPE_NAME (TREE_TYPE (elt));
10409 file = DECL_SOURCE_FILE (decl);
10411 /* Complain if the field is deprecated and the file it was defined
10412 in isn't compiled at the same time the file which contains its
10413 use is */
10414 if (DECL_DEPRECATED (decl)
10415 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10417 const char *the;
10418 switch (TREE_CODE (decl))
10420 case FUNCTION_DECL:
10421 the = "method";
10422 break;
10423 case FIELD_DECL:
10424 case VAR_DECL:
10425 the = "field";
10426 break;
10427 case TYPE_DECL:
10428 parse_warning_context (wfl, "The class %qs has been deprecated",
10429 IDENTIFIER_POINTER (DECL_NAME (decl)));
10430 return;
10431 default:
10432 abort ();
10434 /* Don't issue a message if the context as been deprecated as a
10435 whole. */
10436 if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10437 parse_warning_context
10438 (wfl, "The %s %qs in class %qs has been deprecated",
10439 the, lang_printable_name (decl, 0),
10440 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10444 /* Returns 1 if class was declared in the current package, 0 otherwise */
10446 static GTY(()) tree cicp_cache;
10447 static int
10448 class_in_current_package (tree class)
10450 int qualified_flag;
10451 tree left;
10453 if (cicp_cache == class)
10454 return 1;
10456 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10458 /* If the current package is empty and the name of CLASS is
10459 qualified, class isn't in the current package. If there is a
10460 current package and the name of the CLASS is not qualified, class
10461 isn't in the current package */
10462 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10463 return 0;
10465 /* If there is not package and the name of CLASS isn't qualified,
10466 they belong to the same unnamed package */
10467 if (!ctxp->package && !qualified_flag)
10468 return 1;
10470 /* Compare the left part of the name of CLASS with the package name */
10471 split_qualified_name (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10472 if (ctxp->package == left)
10474 cicp_cache = class;
10475 return 1;
10477 return 0;
10480 /* This function may generate code to access DECL from WHERE. This is
10481 done only if certain conditions meet. */
10483 static tree
10484 maybe_access_field (tree decl, tree where, tree type)
10486 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10487 && !FIELD_STATIC (decl))
10488 decl = build_field_ref (where ? where : current_this,
10489 (type ? type : DECL_CONTEXT (decl)),
10490 DECL_NAME (decl));
10491 return decl;
10494 /* Build a method invocation, by patching PATCH. If non NULL
10495 and according to the situation, PRIMARY and WHERE may be
10496 used. IS_STATIC is set to 1 if the invoked function is static. */
10498 static tree
10499 patch_method_invocation (tree patch, tree primary, tree where, int from_super,
10500 int *is_static, tree *ret_decl)
10502 tree wfl = TREE_OPERAND (patch, 0);
10503 tree args = TREE_OPERAND (patch, 1);
10504 tree name = EXPR_WFL_NODE (wfl);
10505 tree list;
10506 int is_static_flag = 0;
10507 int is_super_init = 0;
10508 tree this_arg = NULL_TREE;
10509 int is_array_clone_call = 0;
10511 /* Should be overridden if everything goes well. Otherwise, if
10512 something fails, it should keep this value. It stop the
10513 evaluation of a bogus assignment. See java_complete_tree,
10514 MODIFY_EXPR: for the reasons why we sometimes want to keep on
10515 evaluating an assignment */
10516 TREE_TYPE (patch) = error_mark_node;
10518 /* Since lookup functions are messing with line numbers, save the
10519 context now. */
10520 java_parser_context_save_global ();
10522 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10524 /* Resolution of qualified name, excluding constructors */
10525 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10527 tree identifier, identifier_wfl, type, resolved;
10528 /* Extract the last IDENTIFIER of the qualified
10529 expression. This is a wfl and we will use it's location
10530 data during error report. */
10531 identifier_wfl = cut_identifier_in_qualified (wfl);
10532 identifier = EXPR_WFL_NODE (identifier_wfl);
10534 /* Given the context, IDENTIFIER is syntactically qualified
10535 as a MethodName. We need to qualify what's before */
10536 qualify_ambiguous_name (wfl);
10537 resolved = resolve_field_access (wfl, NULL, NULL);
10539 if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10540 && FIELD_FINAL (resolved)
10541 && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10542 && !flag_emit_class_files)
10543 resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10545 if (resolved == error_mark_node)
10546 PATCH_METHOD_RETURN_ERROR ();
10548 type = GET_SKIP_TYPE (resolved);
10549 resolve_and_layout (type, NULL_TREE);
10551 if (JPRIMITIVE_TYPE_P (type))
10553 parse_error_context
10554 (identifier_wfl,
10555 "Can't invoke a method on primitive type %qs",
10556 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10557 PATCH_METHOD_RETURN_ERROR ();
10560 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10561 args = nreverse (args);
10563 /* We're resolving a call from a type */
10564 if (TREE_CODE (resolved) == TYPE_DECL)
10566 if (CLASS_INTERFACE (resolved))
10568 parse_error_context
10569 (identifier_wfl,
10570 "Can't make static reference to method %qs in interface %qs",
10571 IDENTIFIER_POINTER (identifier),
10572 IDENTIFIER_POINTER (name));
10573 PATCH_METHOD_RETURN_ERROR ();
10575 if (list && !METHOD_STATIC (list))
10577 char *fct_name = xstrdup (lang_printable_name (list, 2));
10578 parse_error_context
10579 (identifier_wfl,
10580 "Can't make static reference to method %<%s %s%> in class %qs",
10581 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10582 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10583 free (fct_name);
10584 PATCH_METHOD_RETURN_ERROR ();
10587 else
10588 this_arg = primary = resolved;
10590 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10591 is_array_clone_call = 1;
10593 /* IDENTIFIER_WFL will be used to report any problem further */
10594 wfl = identifier_wfl;
10596 /* Resolution of simple names, names generated after a primary: or
10597 constructors */
10598 else
10600 tree class_to_search = NULL_TREE;
10601 int lc; /* Looking for Constructor */
10603 /* We search constructor in their target class */
10604 if (CALL_CONSTRUCTOR_P (patch))
10606 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10607 class_to_search = EXPR_WFL_NODE (wfl);
10608 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10609 this_identifier_node)
10610 class_to_search = NULL_TREE;
10611 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10612 super_identifier_node)
10614 is_super_init = 1;
10615 if (CLASSTYPE_SUPER (current_class))
10616 class_to_search =
10617 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10618 else
10620 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10621 PATCH_METHOD_RETURN_ERROR ();
10625 /* Class to search is NULL if we're searching the current one */
10626 if (class_to_search)
10628 class_to_search = resolve_and_layout (class_to_search, wfl);
10630 if (!class_to_search)
10632 parse_error_context
10633 (wfl, "Class %qs not found in type declaration",
10634 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10635 PATCH_METHOD_RETURN_ERROR ();
10638 /* Can't instantiate an abstract class, but we can
10639 invoke it's constructor. It's use within the `new'
10640 context is denied here. */
10641 if (CLASS_ABSTRACT (class_to_search)
10642 && TREE_CODE (patch) == NEW_CLASS_EXPR)
10644 parse_error_context
10645 (wfl, "Class %qs is an abstract class. It can't be instantiated",
10646 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10647 PATCH_METHOD_RETURN_ERROR ();
10650 class_to_search = TREE_TYPE (class_to_search);
10652 else
10653 class_to_search = current_class;
10654 lc = 1;
10656 /* This is a regular search in the local class, unless an
10657 alternate class is specified. */
10658 else
10660 if (where != NULL_TREE)
10661 class_to_search = where;
10662 else if (QUALIFIED_P (name))
10663 class_to_search = current_class;
10664 else
10666 class_to_search = current_class;
10668 for (;;)
10670 if (has_method (class_to_search, name))
10671 break;
10672 if (! INNER_CLASS_TYPE_P (class_to_search))
10674 parse_error_context (wfl,
10675 "No method named %qs in scope",
10676 IDENTIFIER_POINTER (name));
10677 PATCH_METHOD_RETURN_ERROR ();
10679 class_to_search
10680 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10683 lc = 0;
10686 /* NAME is a simple identifier or comes from a primary. Search
10687 in the class whose declaration contain the method being
10688 invoked. */
10689 resolve_and_layout (class_to_search, NULL_TREE);
10691 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10692 /* Don't continue if no method were found, as the next statement
10693 can't be executed then. */
10694 if (!list)
10695 PATCH_METHOD_RETURN_ERROR ();
10697 if (TYPE_ARRAY_P (class_to_search)
10698 && DECL_NAME (list) == get_identifier ("clone"))
10699 is_array_clone_call = 1;
10701 /* Check for static reference if non static methods */
10702 if (check_for_static_method_reference (wfl, patch, list,
10703 class_to_search, primary))
10704 PATCH_METHOD_RETURN_ERROR ();
10706 /* Check for inner classes creation from illegal contexts */
10707 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10708 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10709 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10710 && !DECL_INIT_P (current_function_decl))
10712 parse_error_context
10713 (wfl, "No enclosing instance for inner class %qs is in scope%s",
10714 lang_printable_name (class_to_search, 0),
10715 (!current_this ? "" :
10716 "; an explicit one must be provided when creating this inner class"));
10717 PATCH_METHOD_RETURN_ERROR ();
10720 /* Non static methods are called with the current object extra
10721 argument. If patch a `new TYPE()', the argument is the value
10722 returned by the object allocator. If method is resolved as a
10723 primary, use the primary otherwise use the current THIS. */
10724 args = nreverse (args);
10725 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10727 this_arg = primary ? primary : current_this;
10729 /* If we're using an access method, things are different.
10730 There are two family of cases:
10732 1) We're not generating bytecodes:
10734 - LIST is non static. It's invocation is transformed from
10735 x(a1,...,an) into this$<n>.x(a1,....an).
10736 - LIST is static. It's invocation is transformed from
10737 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10739 2) We're generating bytecodes:
10741 - LIST is non static. It's invocation is transformed from
10742 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10743 - LIST is static. It's invocation is transformed from
10744 x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10746 Of course, this$<n> can be arbitrarily complex, ranging from
10747 this$0 (the immediate outer context) to
10748 access$0(access$0(...(this$0))).
10750 maybe_use_access_method returns a nonzero value if the
10751 this_arg has to be moved into the (then generated) stub
10752 argument list. In the meantime, the selected function
10753 might have be replaced by a generated stub. */
10754 if (!primary &&
10755 maybe_use_access_method (is_super_init, &list, &this_arg))
10757 args = tree_cons (NULL_TREE, this_arg, args);
10758 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10763 /* Merge point of all resolution schemes. If we have nothing, this
10764 is an error, already signaled */
10765 if (!list)
10766 PATCH_METHOD_RETURN_ERROR ();
10768 /* Check accessibility, position the is_static flag, build and
10769 return the call */
10770 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10771 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10772 NULL_TREE), from_super)
10773 /* Calls to clone() on array types are permitted as a special-case. */
10774 && !is_array_clone_call)
10776 const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10777 const char *const access =
10778 accessibility_string (get_access_flags_from_decl (list));
10779 const char *const klass =
10780 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10781 const char *const refklass =
10782 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10783 const char *const what = (DECL_CONSTRUCTOR_P (list)
10784 ? "constructor" : "method");
10785 parse_error_context (wfl,
10786 "Can't access %s %s %<%s.%s%> from %qs",
10787 access, what, klass, fct_name, refklass);
10788 PATCH_METHOD_RETURN_ERROR ();
10791 /* Deprecation check: check whether the method being invoked or the
10792 instance-being-created's type are deprecated. */
10793 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10794 check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10795 check_deprecation (wfl, list);
10797 /* If invoking a innerclass constructor, there are hidden parameters
10798 to pass */
10799 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10800 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10802 /* And make sure we add the accessed local variables to be saved
10803 in field aliases. */
10804 args = build_alias_initializer_parameter_list
10805 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10807 /* Secretly pass the current_this/primary as a second argument */
10808 if (primary || current_this)
10810 tree extra_arg;
10811 tree this_type = (current_this ?
10812 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10813 /* Method's (list) enclosing context */
10814 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10815 /* If we have a primary, use it. */
10816 if (primary)
10817 extra_arg = primary;
10818 /* The current `this' is an inner class but isn't a direct
10819 enclosing context for the inner class we're trying to
10820 create. Build an access to the proper enclosing context
10821 and use it. */
10822 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10823 && this_type != TREE_TYPE (mec))
10826 extra_arg = build_access_to_thisn (current_class,
10827 TREE_TYPE (mec), 0);
10828 extra_arg = java_complete_tree (extra_arg);
10830 /* Otherwise, just use the current `this' as an enclosing
10831 context. */
10832 else
10833 extra_arg = current_this;
10834 args = tree_cons (NULL_TREE, extra_arg, args);
10836 else
10837 args = tree_cons (NULL_TREE, integer_zero_node, args);
10840 /* This handles the situation where a constructor invocation needs
10841 to have an enclosing context passed as a second parameter (the
10842 constructor is one of an inner class). */
10843 if ((is_super_init ||
10844 (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10845 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10847 tree dest = TYPE_NAME (DECL_CONTEXT (list));
10848 tree extra_arg =
10849 build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10850 extra_arg = java_complete_tree (extra_arg);
10851 args = tree_cons (NULL_TREE, extra_arg, args);
10854 is_static_flag = METHOD_STATIC (list);
10855 if (! is_static_flag && this_arg != NULL_TREE)
10856 args = tree_cons (NULL_TREE, this_arg, args);
10858 /* In the context of an explicit constructor invocation, we can't
10859 invoke any method relying on `this'. Exceptions are: we're
10860 invoking a static function, primary exists and is not the current
10861 this, we're creating a new object. */
10862 if (ctxp->explicit_constructor_p
10863 && !is_static_flag
10864 && (!primary || primary == current_this)
10865 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10867 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
10868 PATCH_METHOD_RETURN_ERROR ();
10870 java_parser_context_restore_global ();
10871 if (is_static)
10872 *is_static = is_static_flag;
10873 /* Sometimes, we want the decl of the selected method. Such as for
10874 EH checking */
10875 if (ret_decl)
10876 *ret_decl = list;
10877 patch = patch_invoke (patch, list, args);
10879 /* Now is a good time to insert the call to finit$ */
10880 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10882 tree finit_parms, finit_call;
10884 /* Prepare to pass hidden parameters to finit$, if any. */
10885 finit_parms = build_alias_initializer_parameter_list
10886 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10888 finit_call =
10889 build_method_invocation (build_wfl_node (finit_identifier_node),
10890 finit_parms);
10892 /* Generate the code used to initialize fields declared with an
10893 initialization statement and build a compound statement along
10894 with the super constructor invocation. */
10895 CAN_COMPLETE_NORMALLY (patch) = 1;
10896 patch = build2 (COMPOUND_EXPR, void_type_node, patch,
10897 java_complete_tree (finit_call));
10899 return patch;
10902 /* Check that we're not trying to do a static reference to a method in
10903 non static method. Return 1 if it's the case, 0 otherwise. */
10905 static int
10906 check_for_static_method_reference (tree wfl, tree node, tree method,
10907 tree where, tree primary)
10909 if (METHOD_STATIC (current_function_decl)
10910 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10912 char *fct_name = xstrdup (lang_printable_name (method, 0));
10913 parse_error_context
10914 (wfl, "Can't make static reference to method %<%s %s%> in class %qs",
10915 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10916 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10917 free (fct_name);
10918 return 1;
10920 return 0;
10923 /* Fix the invocation of *MDECL if necessary in the case of a
10924 invocation from an inner class. *THIS_ARG might be modified
10925 appropriately and an alternative access to *MDECL might be
10926 returned. */
10928 static int
10929 maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10931 tree ctx;
10932 tree md = *mdecl, ta = *this_arg;
10933 int to_return = 0;
10934 int non_static_context = !METHOD_STATIC (md);
10936 if (is_super_init
10937 || DECL_CONTEXT (md) == current_class
10938 || !PURE_INNER_CLASS_TYPE_P (current_class)
10939 || DECL_FINIT_P (md)
10940 || DECL_INSTINIT_P (md))
10941 return 0;
10943 /* If we're calling a method found in an enclosing class, generate
10944 what it takes to retrieve the right this. Don't do that if we're
10945 invoking a static method. Note that if MD's type is unrelated to
10946 CURRENT_CLASS, then the current this can be used. */
10948 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10950 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10951 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10953 ta = build_current_thisn (current_class);
10954 ta = build_wfl_node (ta);
10956 else
10958 tree type = ctx;
10959 while (type)
10961 maybe_build_thisn_access_method (type);
10962 if (inherits_from_p (type, DECL_CONTEXT (md)))
10964 ta = build_access_to_thisn (ctx, type, 0);
10965 break;
10967 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10968 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10971 ta = java_complete_tree (ta);
10974 /* We might have to use an access method to get to MD. We can
10975 break the method access rule as far as we're not generating
10976 bytecode */
10977 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10979 md = build_outer_method_access_method (md);
10980 to_return = 1;
10983 *mdecl = md;
10984 *this_arg = ta;
10986 /* Returning a nonzero value indicates we were doing a non static
10987 method invocation that is now a static invocation. It will have
10988 callee displace `this' to insert it in the regular argument
10989 list. */
10990 return (non_static_context && to_return);
10993 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10994 mode. */
10996 static tree
10997 patch_invoke (tree patch, tree method, tree args)
10999 tree dtable, func;
11000 tree original_call, t, ta;
11001 tree check = NULL_TREE;
11003 /* Last step for args: convert build-in types. If we're dealing with
11004 a new TYPE() type call, the first argument to the constructor
11005 isn't found in the incoming argument list, but delivered by
11006 `new' */
11007 t = TYPE_ARG_TYPES (TREE_TYPE (method));
11008 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
11009 t = TREE_CHAIN (t);
11010 for (ta = args; t != end_params_node && ta;
11011 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
11012 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
11013 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
11014 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
11016 /* Resolve unresolved returned type issues */
11017 t = TREE_TYPE (TREE_TYPE (method));
11018 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
11019 resolve_and_layout (TREE_TYPE (t), NULL);
11021 if (flag_emit_class_files)
11022 func = method;
11023 else
11025 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
11027 case INVOKE_VIRTUAL:
11028 dtable = invoke_build_dtable (0, args);
11029 func = build_invokevirtual (dtable, method);
11030 break;
11032 case INVOKE_NONVIRTUAL:
11033 /* If the object for the method call is null, we throw an
11034 exception. We don't do this if the object is the current
11035 method's `this'. In other cases we just rely on an
11036 optimization pass to eliminate redundant checks. */
11037 if (TREE_VALUE (args) != current_this)
11039 /* We use a save_expr here to make sure we only evaluate
11040 the new `self' expression once. */
11041 tree save_arg = save_expr (TREE_VALUE (args));
11042 TREE_VALUE (args) = save_arg;
11043 check = java_check_reference (save_arg, 1);
11045 /* Fall through. */
11047 case INVOKE_SUPER:
11048 case INVOKE_STATIC:
11050 tree signature = build_java_signature (TREE_TYPE (method));
11051 func = build_known_method_ref (method, TREE_TYPE (method),
11052 DECL_CONTEXT (method),
11053 signature, args);
11055 break;
11057 case INVOKE_INTERFACE:
11058 dtable = invoke_build_dtable (1, args);
11059 func = build_invokeinterface (dtable, method);
11060 break;
11062 default:
11063 abort ();
11066 /* Ensure self_type is initialized, (invokestatic). FIXME */
11067 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
11070 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
11071 TREE_OPERAND (patch, 0) = func;
11072 TREE_OPERAND (patch, 1) = args;
11073 patch = check_for_builtin (method, patch);
11074 original_call = patch;
11076 /* We're processing a `new TYPE ()' form. New is called and its
11077 returned value is the first argument to the constructor. We build
11078 a COMPOUND_EXPR and use saved expression so that the overall NEW
11079 expression value is a pointer to a newly created and initialized
11080 class. */
11081 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
11083 tree class = DECL_CONTEXT (method);
11084 tree c1, saved_new, new;
11085 tree alloc_node;
11087 if (flag_emit_class_files)
11089 TREE_TYPE (patch) = build_pointer_type (class);
11090 return patch;
11092 if (!TYPE_SIZE (class))
11093 safe_layout_class (class);
11094 alloc_node =
11095 (class_has_finalize_method (class) ? alloc_object_node
11096 : alloc_no_finalizer_node);
11097 new = build3 (CALL_EXPR, promote_type (class),
11098 build_address_of (alloc_node),
11099 build_tree_list (NULL_TREE, build_class_ref (class)),
11100 NULL_TREE);
11101 saved_new = save_expr (new);
11102 c1 = build_tree_list (NULL_TREE, saved_new);
11103 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
11104 TREE_OPERAND (original_call, 1) = c1;
11105 TREE_SET_CODE (original_call, CALL_EXPR);
11106 patch = build2 (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
11109 /* If CHECK is set, then we are building a check to see if the object
11110 is NULL. */
11111 if (check != NULL_TREE)
11113 /* We have to call force_evaluation_order now because creating a
11114 COMPOUND_EXPR wraps the arg list in a way that makes it
11115 unrecognizable by force_evaluation_order later. Yuk. */
11116 patch = build2 (COMPOUND_EXPR, TREE_TYPE (patch), check,
11117 force_evaluation_order (patch));
11118 TREE_SIDE_EFFECTS (patch) = 1;
11121 /* In order to be able to modify PATCH later, we SAVE_EXPR it and
11122 put it as the first expression of a COMPOUND_EXPR. The second
11123 expression being an empty statement to be later patched if
11124 necessary. We remember a TREE_LIST (the PURPOSE is the method,
11125 the VALUE is the compound) in a hashtable and return a
11126 COMPOUND_EXPR built so that the result of the evaluation of the
11127 original PATCH node is returned. */
11128 if (STATIC_CLASS_INIT_OPT_P ()
11129 && current_function_decl && METHOD_STATIC (method))
11131 tree list;
11132 tree fndecl = current_function_decl;
11133 /* We have to call force_evaluation_order now because creating a
11134 COMPOUND_EXPR wraps the arg list in a way that makes it
11135 unrecognizable by force_evaluation_order later. Yuk. */
11136 tree save = force_evaluation_order (patch);
11137 tree type = TREE_TYPE (patch);
11139 patch = build2 (COMPOUND_EXPR, type, save, build_java_empty_stmt ());
11140 list = tree_cons (method, patch,
11141 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
11143 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
11145 patch = build2 (COMPOUND_EXPR, type, patch, save);
11148 return patch;
11151 static int
11152 invocation_mode (tree method, int super)
11154 int access = get_access_flags_from_decl (method);
11156 if (super)
11157 return INVOKE_SUPER;
11159 if (access & ACC_STATIC)
11160 return INVOKE_STATIC;
11162 /* We have to look for a constructor before we handle nonvirtual
11163 calls; otherwise the constructor will look nonvirtual. */
11164 if (DECL_CONSTRUCTOR_P (method))
11165 return INVOKE_STATIC;
11167 if (access & ACC_PRIVATE)
11168 return INVOKE_NONVIRTUAL;
11170 /* Binary compatibility: just because it's final today, that doesn't
11171 mean it'll be final tomorrow. */
11172 if (! flag_indirect_dispatch
11173 || DECL_CONTEXT (method) == object_type_node)
11175 if (access & ACC_FINAL)
11176 return INVOKE_NONVIRTUAL;
11178 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
11179 return INVOKE_NONVIRTUAL;
11182 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
11183 return INVOKE_INTERFACE;
11185 return INVOKE_VIRTUAL;
11188 /* Retrieve a refined list of matching methods. It covers the step
11189 15.11.2 (Compile-Time Step 2) */
11191 static tree
11192 lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
11194 tree atl = end_params_node; /* Arg Type List */
11195 tree method, signature, list, node;
11196 const char *candidates; /* Used for error report */
11197 char *dup;
11199 /* Fix the arguments */
11200 for (node = arg_list; node; node = TREE_CHAIN (node))
11202 tree current_arg = TREE_TYPE (TREE_VALUE (node));
11203 /* Non primitive type may have to be resolved */
11204 if (!JPRIMITIVE_TYPE_P (current_arg))
11205 resolve_and_layout (current_arg, NULL_TREE);
11206 /* And promoted */
11207 if (TREE_CODE (current_arg) == RECORD_TYPE)
11208 current_arg = promote_type (current_arg);
11209 atl = tree_cons (NULL_TREE, current_arg, atl);
11212 /* Presto. If we're dealing with an anonymous class and a
11213 constructor call, generate the right constructor now, since we
11214 know the arguments' types. */
11216 if (lc && ANONYMOUS_CLASS_P (class))
11218 tree mdecl = craft_constructor (TYPE_NAME (class), atl);
11219 /* The anonymous class may have already been laid out, so make sure
11220 the new constructor is laid out here. */
11221 layout_class_method (class, CLASSTYPE_SUPER (class), mdecl, NULL_TREE);
11224 /* Find all candidates and then refine the list, searching for the
11225 most specific method. */
11226 list = find_applicable_accessible_methods_list (lc, class, name, atl);
11227 list = find_most_specific_methods_list (list);
11228 if (list && !TREE_CHAIN (list))
11229 return TREE_VALUE (list);
11231 /* Issue an error. List candidates if any. Candidates are listed
11232 only if accessible (non accessible methods may end-up here for
11233 the sake of a better error report). */
11234 candidates = NULL;
11235 if (list)
11237 tree current;
11238 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
11239 for (current = list; current; current = TREE_CHAIN (current))
11241 tree cm = TREE_VALUE (current);
11242 char string [4096];
11243 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
11244 continue;
11245 sprintf
11246 (string, " '%s' in '%s'%s",
11247 get_printable_method_name (cm),
11248 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
11249 (TREE_CHAIN (current) ? "\n" : ""));
11250 obstack_grow (&temporary_obstack, string, strlen (string));
11252 obstack_1grow (&temporary_obstack, '\0');
11253 candidates = obstack_finish (&temporary_obstack);
11255 /* Issue the error message */
11256 method = make_node (FUNCTION_TYPE);
11257 TYPE_ARG_TYPES (method) = atl;
11258 signature = build_java_argument_signature (method);
11259 dup = xstrdup (lang_printable_name (class, 0));
11260 parse_error_context (cl, "Can't find %s %<%s(%s)%> in type %qs%s",
11261 (lc ? "constructor" : "method"),
11262 (lc ? dup : IDENTIFIER_POINTER (name)),
11263 IDENTIFIER_POINTER (signature), dup,
11264 (candidates ? candidates : ""));
11265 free (dup);
11266 return NULL_TREE;
11269 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
11270 when we're looking for a constructor. */
11272 static tree
11273 find_applicable_accessible_methods_list (int lc, tree class, tree name,
11274 tree arglist)
11276 static htab_t searched_classes;
11277 static int search_not_done = 0;
11278 tree list = NULL_TREE, all_list = NULL_TREE;
11279 tree base_binfo;
11280 int i;
11282 /* Check the hash table to determine if this class has been searched
11283 already. */
11284 if (searched_classes)
11286 if (htab_find (searched_classes, class) != NULL)
11287 return NULL;
11289 else
11291 searched_classes = htab_create (10, htab_hash_pointer,
11292 htab_eq_pointer, NULL);
11295 search_not_done++;
11296 *htab_find_slot (searched_classes, class, INSERT) = class;
11298 if (!CLASS_LOADED_P (class))
11300 load_class (class, 1);
11301 safe_layout_class (class);
11304 /* Search interfaces */
11305 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
11306 && CLASS_INTERFACE (TYPE_NAME (class)))
11308 search_applicable_methods_list (lc, TYPE_METHODS (class),
11309 name, arglist, &list, &all_list);
11310 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11312 tree t = BINFO_TYPE (base_binfo);
11313 tree rlist;
11315 rlist = find_applicable_accessible_methods_list (lc, t, name,
11316 arglist);
11317 list = chainon (rlist, list);
11320 /* Search classes */
11321 else
11323 search_applicable_methods_list (lc, TYPE_METHODS (class),
11324 name, arglist, &list, &all_list);
11326 /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11327 that we only search in class. Note that we should have found
11328 something at this point. */
11329 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11331 lc = 1;
11332 if (!list)
11333 abort ();
11336 /* We must search all interfaces of this class */
11337 if (!lc)
11339 for (i = 1;
11340 BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11342 tree t = BINFO_TYPE (base_binfo);
11343 if (t != object_type_node)
11345 tree rlist
11346 = find_applicable_accessible_methods_list (lc, t,
11347 name, arglist);
11348 list = chainon (rlist, list);
11353 /* Search superclass */
11354 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11356 tree rlist;
11357 class = CLASSTYPE_SUPER (class);
11358 rlist = find_applicable_accessible_methods_list (lc, class,
11359 name, arglist);
11360 list = chainon (rlist, list);
11364 search_not_done--;
11366 /* We're done. Reset the searched classes list and finally search
11367 java.lang.Object if it wasn't searched already. */
11368 if (!search_not_done)
11370 if (!lc
11371 && TYPE_METHODS (object_type_node)
11372 && htab_find (searched_classes, object_type_node) == NULL)
11374 search_applicable_methods_list (lc,
11375 TYPE_METHODS (object_type_node),
11376 name, arglist, &list, &all_list);
11378 htab_delete (searched_classes);
11379 searched_classes = NULL;
11382 /* Either return the list obtained or all selected (but
11383 inaccessible) methods for better error report. */
11384 return (!list ? all_list : list);
11387 /* Effectively search for the appropriate method in method */
11389 static void
11390 search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
11391 tree *list, tree *all_list)
11393 for (; method; method = TREE_CHAIN (method))
11395 /* When dealing with constructor, stop here, otherwise search
11396 other classes */
11397 if (lc && !DECL_CONSTRUCTOR_P (method))
11398 continue;
11399 else if (!lc && (DECL_CONSTRUCTOR_P (method)
11400 || (DECL_NAME (method) != name)))
11401 continue;
11403 if (argument_types_convertible (method, arglist))
11405 /* Retain accessible methods only */
11406 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11407 method, NULL_TREE, 0))
11408 *list = tree_cons (NULL_TREE, method, *list);
11409 else
11410 /* Also retain all selected method here */
11411 *all_list = tree_cons (NULL_TREE, method, *list);
11416 /* 15.11.2.2 Choose the Most Specific Method */
11418 static tree
11419 find_most_specific_methods_list (tree list)
11421 int max = 0;
11422 int abstract, candidates;
11423 tree current, new_list = NULL_TREE;
11424 for (current = list; current; current = TREE_CHAIN (current))
11426 tree method;
11427 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11429 for (method = list; method; method = TREE_CHAIN (method))
11431 tree method_v, current_v;
11432 /* Don't test a method against itself */
11433 if (method == current)
11434 continue;
11436 method_v = TREE_VALUE (method);
11437 current_v = TREE_VALUE (current);
11439 /* Compare arguments and location where methods where declared */
11440 if (argument_types_convertible (method_v, current_v))
11442 if (valid_method_invocation_conversion_p
11443 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v)))
11445 int v = (DECL_SPECIFIC_COUNT (current_v) += 1);
11446 max = (v > max ? v : max);
11452 /* Review the list and select the maximally specific methods */
11453 for (current = list, abstract = -1, candidates = -1;
11454 current; current = TREE_CHAIN (current))
11455 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11457 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11458 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11459 candidates++;
11462 /* If we have several and they're all abstract, just pick the
11463 closest one. */
11464 if (candidates > 0 && candidates == abstract)
11466 /* FIXME: merge the throws clauses. There is no convenient way
11467 to do this in gcj right now, since ideally we'd like to
11468 introduce a new METHOD_DECL here, but that is really not
11469 possible. */
11470 new_list = nreverse (new_list);
11471 TREE_CHAIN (new_list) = NULL_TREE;
11472 return new_list;
11475 /* We have several (we couldn't find a most specific), all but one
11476 are abstract, we pick the only non abstract one. */
11477 if (candidates > 0 && (candidates == abstract+1))
11479 for (current = new_list; current; current = TREE_CHAIN (current))
11480 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11482 TREE_CHAIN (current) = NULL_TREE;
11483 new_list = current;
11487 /* If we can't find one, lower expectations and try to gather multiple
11488 maximally specific methods */
11489 while (!new_list && max)
11491 while (--max > 0)
11493 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11494 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11498 return new_list;
11501 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11502 converted by method invocation conversion (5.3) to the type of the
11503 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11504 to change less often than M1. */
11506 static GTY(()) tree m2_arg_value;
11507 static GTY(()) tree m2_arg_cache;
11509 static int
11510 argument_types_convertible (tree m1, tree m2_or_arglist)
11512 tree m1_arg, m2_arg;
11514 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11516 if (m2_arg_value == m2_or_arglist)
11517 m2_arg = m2_arg_cache;
11518 else
11520 /* M2_OR_ARGLIST can be a function DECL or a raw list of
11521 argument types */
11522 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11524 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11525 if (!METHOD_STATIC (m2_or_arglist))
11526 m2_arg = TREE_CHAIN (m2_arg);
11528 else
11529 m2_arg = m2_or_arglist;
11531 m2_arg_value = m2_or_arglist;
11532 m2_arg_cache = m2_arg;
11535 while (m1_arg != end_params_node && m2_arg != end_params_node)
11537 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11538 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11539 TREE_VALUE (m2_arg)))
11540 break;
11541 m1_arg = TREE_CHAIN (m1_arg);
11542 m2_arg = TREE_CHAIN (m2_arg);
11544 return m1_arg == end_params_node && m2_arg == end_params_node;
11547 /* Qualification routines */
11549 /* Given a name x.y.z, look up x locally. If it's found, save the
11550 decl. If it's not found, mark the name as RESOLVE_PACKAGE_NAME_P,
11551 so that we later try and load the appropriate classes. */
11552 static void
11553 qualify_ambiguous_name (tree id)
11555 tree name, decl;
11557 /* We inspect the first item of the qualification list. As a sanity
11558 check, make sure that it is an identfier node. */
11559 tree qual = EXPR_WFL_QUALIFICATION (id);
11560 tree qual_wfl = QUAL_WFL (qual);
11562 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11563 return;
11565 name = EXPR_WFL_NODE (qual_wfl);
11567 /* If we don't have an identifier, or we have a 'this' or 'super',
11568 then field access processing is all we need : there is nothing
11569 for us to do. */
11570 if (!name || TREE_CODE (name) != IDENTIFIER_NODE ||
11571 name == this_identifier_node ||
11572 name == super_identifier_node)
11573 return;
11575 /* If name appears within the scope of a local variable declaration
11576 or parameter declaration, or is a field within an enclosing
11577 class, then it is an expression name. Save the decl and let
11578 resolve_field_access do it's work. */
11579 if ((decl = IDENTIFIER_LOCAL_VALUE (name)) ||
11580 (decl = lookup_field_wrapper (current_class, name)))
11582 QUAL_RESOLUTION (qual) = decl;
11583 return;
11586 /* If name is a known class name (either declared or imported), mark
11587 us as a type name. */
11588 if ((decl = resolve_and_layout (name, NULL_TREE)))
11590 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11591 QUAL_RESOLUTION (qual) = decl;
11594 /* Check here that NAME isn't declared by more than one
11595 type-import-on-demand declaration of the compilation unit
11596 containing NAME. FIXME */
11598 /* We couldn't find a declaration for the name. Assume for now that
11599 we have a qualified class name that needs to be loaded from an
11600 external class file. */
11601 else
11602 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11604 /* Propagate the qualification across other components of the
11605 qualified name */
11606 for (qual = TREE_CHAIN (qual); qual;
11607 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11609 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11610 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11613 /* Store the global qualification for the ambiguous part of ID back
11614 into ID fields */
11615 if (RESOLVE_TYPE_NAME_P (qual_wfl))
11616 RESOLVE_TYPE_NAME_P (id) = 1;
11617 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11618 RESOLVE_PACKAGE_NAME_P (id) = 1;
11621 /* Patch tree nodes in a function body. When a BLOCK is found, push
11622 local variable decls if present.
11623 Same as java_complete_lhs, but does resolve static finals to values. */
11625 static tree
11626 java_complete_tree (tree node)
11628 node = java_complete_lhs (node);
11629 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11630 && DECL_INITIAL (node) != NULL_TREE)
11632 tree value = fold_constant_for_init (node, node);
11633 if (value != NULL_TREE)
11634 return value;
11636 return node;
11639 static tree
11640 java_stabilize_reference (tree node)
11642 if (TREE_CODE (node) == COMPOUND_EXPR)
11644 tree op0 = TREE_OPERAND (node, 0);
11645 tree op1 = TREE_OPERAND (node, 1);
11646 TREE_OPERAND (node, 0) = save_expr (op0);
11647 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11648 return node;
11650 return stabilize_reference (node);
11653 /* Patch tree nodes in a function body. When a BLOCK is found, push
11654 local variable decls if present.
11655 Same as java_complete_tree, but does not resolve static finals to values. */
11657 static tree
11658 java_complete_lhs (tree node)
11660 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11661 int flag;
11663 /* CONVERT_EXPR always has its type set, even though it needs to be
11664 worked out. */
11665 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11666 return node;
11668 /* The switch block implements cases processing container nodes
11669 first. Contained nodes are always written back. Leaves come
11670 next and return a value. */
11671 switch (TREE_CODE (node))
11673 case BLOCK:
11675 /* 1- Block section.
11676 Set the local values on decl names so we can identify them
11677 faster when they're referenced. At that stage, identifiers
11678 are legal so we don't check for declaration errors. */
11679 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11681 DECL_CONTEXT (cn) = current_function_decl;
11682 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11684 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11685 CAN_COMPLETE_NORMALLY (node) = 1;
11686 else
11688 tree stmt = BLOCK_EXPR_BODY (node);
11689 tree *ptr;
11690 int error_seen = 0;
11691 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11693 /* Re-order from (((A; B); C); ...; Z) to
11694 (A; (B; (C ; (...; Z)))).
11695 This makes it easier to scan the statements left-to-right
11696 without using recursion (which might overflow the stack
11697 if the block has many statements. */
11698 for (;;)
11700 tree left = TREE_OPERAND (stmt, 0);
11701 if (TREE_CODE (left) != COMPOUND_EXPR)
11702 break;
11703 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11704 TREE_OPERAND (left, 1) = stmt;
11705 stmt = left;
11707 BLOCK_EXPR_BODY (node) = stmt;
11710 /* Now do the actual complete, without deep recursion for
11711 long blocks. */
11712 ptr = &BLOCK_EXPR_BODY (node);
11713 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11714 && !IS_EMPTY_STMT (TREE_OPERAND (*ptr, 1)))
11716 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11717 tree *next = &TREE_OPERAND (*ptr, 1);
11718 TREE_OPERAND (*ptr, 0) = cur;
11719 if (IS_EMPTY_STMT (cur))
11721 /* Optimization; makes it easier to detect empty bodies.
11722 Most useful for <clinit> with all-constant initializer. */
11723 *ptr = *next;
11724 continue;
11726 if (TREE_CODE (cur) == ERROR_MARK)
11727 error_seen++;
11728 else if (! CAN_COMPLETE_NORMALLY (cur))
11730 wfl_op2 = *next;
11731 for (;;)
11733 if (TREE_CODE (wfl_op2) == BLOCK)
11734 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11735 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11736 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11737 else
11738 break;
11740 if (TREE_CODE (wfl_op2) != CASE_EXPR
11741 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11742 unreachable_stmt_error (*ptr);
11744 if (TREE_TYPE (*ptr) == NULL_TREE)
11745 TREE_TYPE (*ptr) = void_type_node;
11746 ptr = next;
11748 *ptr = java_complete_tree (*ptr);
11750 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11751 return error_mark_node;
11752 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11754 /* Turn local bindings to null */
11755 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11756 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11758 TREE_TYPE (node) = void_type_node;
11759 break;
11761 /* 2- They are expressions but ultimately deal with statements */
11763 case THROW_EXPR:
11764 wfl_op1 = TREE_OPERAND (node, 0);
11765 COMPLETE_CHECK_OP_0 (node);
11766 /* 14.19 A throw statement cannot complete normally. */
11767 CAN_COMPLETE_NORMALLY (node) = 0;
11768 return patch_throw_statement (node, wfl_op1);
11770 case SYNCHRONIZED_EXPR:
11771 wfl_op1 = TREE_OPERAND (node, 0);
11772 return patch_synchronized_statement (node, wfl_op1);
11774 case TRY_EXPR:
11775 return patch_try_statement (node);
11777 case TRY_FINALLY_EXPR:
11778 COMPLETE_CHECK_OP_0 (node);
11779 COMPLETE_CHECK_OP_1 (node);
11780 if (IS_EMPTY_STMT (TREE_OPERAND (node, 0)))
11781 /* Reduce try/finally nodes with an empty try block. */
11782 return TREE_OPERAND (node, 1);
11783 if (IS_EMPTY_STMT (TREE_OPERAND (node, 1)))
11784 /* Likewise for an empty finally block. */
11785 return TREE_OPERAND (node, 0);
11786 CAN_COMPLETE_NORMALLY (node)
11787 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11788 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11789 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11790 return node;
11792 case LABELED_BLOCK_EXPR:
11793 PUSH_LABELED_BLOCK (node);
11794 if (LABELED_BLOCK_BODY (node))
11795 COMPLETE_CHECK_OP_1 (node);
11796 TREE_TYPE (node) = void_type_node;
11797 POP_LABELED_BLOCK ();
11799 if (IS_EMPTY_STMT (LABELED_BLOCK_BODY (node)))
11801 LABELED_BLOCK_BODY (node) = NULL_TREE;
11802 CAN_COMPLETE_NORMALLY (node) = 1;
11804 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11805 CAN_COMPLETE_NORMALLY (node) = 1;
11806 return node;
11808 case EXIT_BLOCK_EXPR:
11809 return patch_bc_statement (node);
11811 case CASE_EXPR:
11812 cn = java_complete_tree (TREE_OPERAND (node, 0));
11813 if (cn == error_mark_node)
11814 return cn;
11816 /* First, the case expression must be constant. Values of final
11817 fields are accepted. */
11818 nn = fold_constant_for_init (cn, NULL_TREE);
11819 if (nn != NULL_TREE)
11820 cn = nn;
11822 cn = fold (cn);
11823 if ((TREE_CODE (cn) == COMPOUND_EXPR
11824 || TREE_CODE (cn) == COMPONENT_REF)
11825 && JDECL_P (TREE_OPERAND (cn, 1))
11826 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11827 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11829 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11830 TREE_OPERAND (cn, 1));
11832 /* Accept final locals too. */
11833 else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn)
11834 && DECL_INITIAL (cn))
11835 cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11837 if (!TREE_CONSTANT (cn))
11839 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11840 parse_error_context (node, "Constant expression required");
11841 return error_mark_node;
11844 nn = ctxp->current_loop;
11846 /* It must be assignable to the type of the switch expression. */
11847 if (!try_builtin_assignconv (NULL_TREE,
11848 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11850 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11851 parse_error_context
11852 (wfl_operator,
11853 "Incompatible type for case. Can't convert %qs to %<int%>",
11854 lang_printable_name (TREE_TYPE (cn), 0));
11855 return error_mark_node;
11858 cn = fold (convert (int_type_node, cn));
11859 TREE_CONSTANT_OVERFLOW (cn) = 0;
11860 CAN_COMPLETE_NORMALLY (cn) = 1;
11862 /* Save the label on a list so that we can later check for
11863 duplicates. */
11864 case_label_list = tree_cons (node, cn, case_label_list);
11866 /* Multiple instance of a case label bearing the same value is
11867 checked later. The case expression is all right so far. */
11868 if (TREE_CODE (cn) == VAR_DECL)
11869 cn = DECL_INITIAL (cn);
11870 TREE_OPERAND (node, 0) = cn;
11871 TREE_TYPE (node) = void_type_node;
11872 CAN_COMPLETE_NORMALLY (node) = 1;
11873 TREE_SIDE_EFFECTS (node) = 1;
11874 break;
11876 case DEFAULT_EXPR:
11877 nn = ctxp->current_loop;
11878 /* Only one default label is allowed per switch statement */
11879 if (SWITCH_HAS_DEFAULT (nn))
11881 #ifdef USE_MAPPED_LOCATION
11882 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
11883 #else
11884 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11885 #endif
11886 parse_error_context (wfl_operator,
11887 "Duplicate case label: %<default%>");
11888 return error_mark_node;
11890 else
11891 SWITCH_HAS_DEFAULT (nn) = 1;
11892 TREE_TYPE (node) = void_type_node;
11893 TREE_SIDE_EFFECTS (node) = 1;
11894 CAN_COMPLETE_NORMALLY (node) = 1;
11895 break;
11897 case SWITCH_EXPR:
11898 case LOOP_EXPR:
11899 PUSH_LOOP (node);
11900 /* Check whether the loop was enclosed in a labeled
11901 statement. If not, create one, insert the loop in it and
11902 return the node */
11903 nn = patch_loop_statement (node);
11905 /* Anyways, walk the body of the loop */
11906 if (TREE_CODE (node) == LOOP_EXPR)
11907 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11908 /* Switch statement: walk the switch expression and the cases */
11909 else
11910 node = patch_switch_statement (node);
11912 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11913 nn = error_mark_node;
11914 else
11916 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11917 /* If we returned something different, that's because we
11918 inserted a label. Pop the label too. */
11919 if (nn != node)
11921 if (CAN_COMPLETE_NORMALLY (node))
11922 CAN_COMPLETE_NORMALLY (nn) = 1;
11923 POP_LABELED_BLOCK ();
11926 POP_LOOP ();
11927 return nn;
11929 case EXIT_EXPR:
11930 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11931 return patch_exit_expr (node);
11933 case COND_EXPR:
11934 /* Condition */
11935 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11936 if (TREE_OPERAND (node, 0) == error_mark_node)
11937 return error_mark_node;
11938 /* then-else branches */
11939 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11940 if (TREE_OPERAND (node, 1) == error_mark_node)
11941 return error_mark_node;
11943 /* This is a special case due to build_assertion(). When
11944 assertions are disabled we build a COND_EXPR in which
11945 Operand 1 is the body of the assertion. If that happens to
11946 be a string concatenation we'll need to patch it here. */
11947 tree patched = patch_string (TREE_OPERAND (node, 1));
11948 if (patched)
11949 TREE_OPERAND (node, 1) = patched;
11951 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11952 if (TREE_OPERAND (node, 2) == error_mark_node)
11953 return error_mark_node;
11954 return patch_if_else_statement (node);
11955 break;
11957 case CONDITIONAL_EXPR:
11958 /* Condition */
11959 wfl_op1 = TREE_OPERAND (node, 0);
11960 COMPLETE_CHECK_OP_0 (node);
11961 wfl_op2 = TREE_OPERAND (node, 1);
11962 COMPLETE_CHECK_OP_1 (node);
11963 wfl_op3 = TREE_OPERAND (node, 2);
11964 COMPLETE_CHECK_OP_2 (node);
11965 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11967 /* 3- Expression section */
11968 case COMPOUND_EXPR:
11969 wfl_op2 = TREE_OPERAND (node, 1);
11970 TREE_OPERAND (node, 0) = nn =
11971 java_complete_tree (TREE_OPERAND (node, 0));
11972 if (IS_EMPTY_STMT (wfl_op2))
11973 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11974 else
11976 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11978 /* An unreachable condition in a do-while statement
11979 is *not* (technically) an unreachable statement. */
11980 nn = wfl_op2;
11981 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11982 nn = EXPR_WFL_NODE (nn);
11983 /* NN can be NULL_TREE exactly when UPDATE is, in
11984 finish_for_loop. */
11985 if (nn != NULL_TREE && TREE_CODE (nn) != EXIT_EXPR)
11987 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11988 if (SUPPRESS_UNREACHABLE_ERROR (nn))
11990 /* Perhaps this warning should have an
11991 associated flag. The code being compiled is
11992 pedantically correct, but useless. */
11993 parse_warning_context (wfl_operator,
11994 "Unreachable statement");
11996 else
11997 parse_error_context (wfl_operator,
11998 "Unreachable statement");
12001 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
12002 if (TREE_OPERAND (node, 1) == error_mark_node)
12003 return error_mark_node;
12004 /* Even though we might allow the case where the first
12005 operand doesn't return normally, we still should compute
12006 CAN_COMPLETE_NORMALLY correctly. */
12007 CAN_COMPLETE_NORMALLY (node)
12008 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
12009 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
12011 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
12012 break;
12014 case RETURN_EXPR:
12015 /* CAN_COMPLETE_NORMALLY (node) = 0; */
12016 return patch_return (node);
12018 case EXPR_WITH_FILE_LOCATION:
12019 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
12020 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
12022 node = resolve_expression_name (node, NULL);
12023 if (node == error_mark_node)
12024 return node;
12025 CAN_COMPLETE_NORMALLY (node) = 1;
12027 else
12029 tree body;
12030 location_t save_location = input_location;
12031 #ifdef USE_MAPPED_LOCATION
12032 input_location = EXPR_LOCATION (node);
12033 if (input_location == UNKNOWN_LOCATION)
12034 input_location = save_location;
12035 #else
12036 input_line = EXPR_WFL_LINENO (node);
12037 #endif
12038 body = java_complete_tree (EXPR_WFL_NODE (node));
12039 input_location = save_location;
12040 EXPR_WFL_NODE (node) = body;
12041 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
12042 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
12043 if (IS_EMPTY_STMT (body) || TREE_CONSTANT (body))
12045 /* Makes it easier to constant fold, detect empty bodies. */
12046 return body;
12048 if (body == error_mark_node)
12050 /* Its important for the evaluation of assignment that
12051 this mark on the TREE_TYPE is propagated. */
12052 TREE_TYPE (node) = error_mark_node;
12053 return error_mark_node;
12055 else
12056 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
12059 break;
12061 case NEW_ARRAY_EXPR:
12062 /* Patch all the dimensions */
12063 flag = 0;
12064 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12066 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
12067 tree dim = convert (int_type_node,
12068 java_complete_tree (TREE_VALUE (cn)));
12069 if (dim == error_mark_node)
12071 flag = 1;
12072 continue;
12074 else
12076 TREE_VALUE (cn) = dim;
12077 /* Setup the location of the current dimension, for
12078 later error report. */
12079 #ifdef USE_MAPPED_LOCATION
12080 TREE_PURPOSE (cn) = expr_add_location (NULL_TREE, location, 0);
12081 #else
12082 TREE_PURPOSE (cn) =
12083 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
12084 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
12085 #endif
12088 /* They complete the array creation expression, if no errors
12089 were found. */
12090 CAN_COMPLETE_NORMALLY (node) = 1;
12091 return (flag ? error_mark_node
12092 : force_evaluation_order (patch_newarray (node)));
12094 case NEW_ANONYMOUS_ARRAY_EXPR:
12095 /* Create the array type if necessary. */
12096 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
12098 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
12099 if (!(type = resolve_type_during_patch (type)))
12100 return error_mark_node;
12101 type = build_array_from_name (type, NULL_TREE,
12102 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
12103 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
12105 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
12106 ANONYMOUS_ARRAY_INITIALIZER (node));
12107 if (node == error_mark_node)
12108 return error_mark_node;
12109 CAN_COMPLETE_NORMALLY (node) = 1;
12110 return node;
12112 case NEW_CLASS_EXPR:
12113 case CALL_EXPR:
12114 /* Complete function's argument(s) first */
12115 if (complete_function_arguments (node))
12116 return error_mark_node;
12117 else
12119 tree decl, wfl = TREE_OPERAND (node, 0);
12120 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
12121 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
12122 super_identifier_node);
12123 tree arguments;
12124 #ifdef USE_MAPPED_LOCATION
12125 source_location location = EXPR_LOCATION (node);
12126 #else
12127 int location = EXPR_WFL_LINECOL (node);
12128 #endif
12130 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12131 from_super, 0, &decl);
12132 if (node == error_mark_node)
12133 return error_mark_node;
12135 if (TREE_CODE (node) == CALL_EXPR
12136 && TREE_OPERAND (node, 1) != NULL_TREE)
12137 arguments = TREE_VALUE (TREE_OPERAND (node, 1));
12138 else
12139 arguments = NULL_TREE;
12140 check_thrown_exceptions (location, decl, arguments);
12141 /* If we call this(...), register signature and positions */
12142 if (in_this)
12143 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12144 tree_cons (wfl, decl,
12145 DECL_CONSTRUCTOR_CALLS (current_function_decl));
12146 CAN_COMPLETE_NORMALLY (node) = 1;
12147 return force_evaluation_order (node);
12150 case MODIFY_EXPR:
12151 /* Save potential wfls */
12152 wfl_op1 = TREE_OPERAND (node, 0);
12153 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12155 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12156 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12157 && DECL_INITIAL (nn) != NULL_TREE)
12159 tree value;
12161 value = fold_constant_for_init (nn, nn);
12163 /* When we have a primitype type, or a string and we're not
12164 emitting a class file, we actually don't want to generate
12165 anything for the assignment. */
12166 if (value != NULL_TREE &&
12167 (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
12168 (TREE_TYPE (value) == string_ptr_type_node &&
12169 ! flag_emit_class_files)))
12171 /* Prepare node for patch_assignment */
12172 TREE_OPERAND (node, 1) = value;
12173 /* Call patch assignment to verify the assignment */
12174 if (patch_assignment (node, wfl_op1) == error_mark_node)
12175 return error_mark_node;
12176 /* Set DECL_INITIAL properly (a conversion might have
12177 been decided by patch_assignment) and return the
12178 empty statement. */
12179 else
12181 tree patched = patch_string (TREE_OPERAND (node, 1));
12182 if (patched)
12183 DECL_INITIAL (nn) = patched;
12184 else
12185 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12186 DECL_FIELD_FINAL_IUD (nn) = 1;
12187 return build_java_empty_stmt ();
12190 if (! flag_emit_class_files)
12191 DECL_INITIAL (nn) = NULL_TREE;
12193 wfl_op2 = TREE_OPERAND (node, 1);
12195 if (TREE_OPERAND (node, 0) == error_mark_node)
12196 return error_mark_node;
12198 flag = COMPOUND_ASSIGN_P (wfl_op2);
12199 if (flag)
12201 /* This might break when accessing outer field from inner
12202 class. TESTME, FIXME */
12203 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12205 /* Hand stabilize the lhs on both places */
12206 TREE_OPERAND (node, 0) = lvalue;
12207 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12208 (flag_emit_class_files ? lvalue : save_expr (lvalue));
12210 /* 15.25.2.a: Left hand is not an array access. FIXME */
12211 /* Now complete the RHS. We write it back later on. */
12212 nn = java_complete_tree (TREE_OPERAND (node, 1));
12214 if ((cn = patch_string (nn)))
12215 nn = cn;
12217 /* The last part of the rewrite for E1 op= E2 is to have
12218 E1 = (T)(E1 op E2), with T being the type of E1. */
12219 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12220 TREE_TYPE (lvalue), nn));
12222 /* If the assignment is compound and has reference type,
12223 then ensure the LHS has type String and nothing else. */
12224 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12225 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12226 parse_error_context (wfl_op2,
12227 "Incompatible type for %<+=%>. Can't convert %qs to %<java.lang.String%>",
12228 lang_printable_name (TREE_TYPE (lvalue), 0));
12230 /* 15.25.2.b: Left hand is an array access. FIXME */
12233 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12234 function to complete this RHS. Note that a NEW_ARRAY_INIT
12235 might have been already fully expanded if created as a result
12236 of processing an anonymous array initializer. We avoid doing
12237 the operation twice by testing whether the node already bears
12238 a type. */
12239 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12240 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12241 TREE_OPERAND (node, 1));
12242 /* Otherwise we simply complete the RHS */
12243 else
12244 nn = java_complete_tree (TREE_OPERAND (node, 1));
12246 if (nn == error_mark_node)
12247 return error_mark_node;
12249 /* Write back the RHS as we evaluated it. */
12250 TREE_OPERAND (node, 1) = nn;
12252 /* In case we're handling = with a String as a RHS, we need to
12253 produce a String out of the RHS (it might still be a
12254 STRING_CST or a StringBuffer at this stage */
12255 if ((nn = patch_string (TREE_OPERAND (node, 1))))
12256 TREE_OPERAND (node, 1) = nn;
12258 if ((nn = nested_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12259 TREE_OPERAND (node, 1))))
12261 /* We return error_mark_node if nested_field_access_fix
12262 detects we write into a final. */
12263 if (nn == error_mark_node)
12264 return error_mark_node;
12265 node = nn;
12267 else
12269 node = patch_assignment (node, wfl_op1);
12270 if (node == error_mark_node)
12271 return error_mark_node;
12272 /* Reorganize the tree if necessary. */
12273 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12274 || JSTRING_P (TREE_TYPE (node))))
12275 node = java_refold (node);
12278 /* Seek to set DECL_INITIAL to a proper value, since it might have
12279 undergone a conversion in patch_assignment. We do that only when
12280 it's necessary to have DECL_INITIAL properly set. */
12281 nn = TREE_OPERAND (node, 0);
12282 if (TREE_CODE (nn) == VAR_DECL
12283 && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12284 && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12285 && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12286 || TREE_TYPE (nn) == string_ptr_type_node))
12287 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12289 CAN_COMPLETE_NORMALLY (node) = 1;
12290 return node;
12292 case MULT_EXPR:
12293 case PLUS_EXPR:
12294 case MINUS_EXPR:
12295 case LSHIFT_EXPR:
12296 case RSHIFT_EXPR:
12297 case URSHIFT_EXPR:
12298 case BIT_AND_EXPR:
12299 case BIT_XOR_EXPR:
12300 case BIT_IOR_EXPR:
12301 case TRUNC_MOD_EXPR:
12302 case TRUNC_DIV_EXPR:
12303 case RDIV_EXPR:
12304 case TRUTH_ANDIF_EXPR:
12305 case TRUTH_ORIF_EXPR:
12306 case EQ_EXPR:
12307 case NE_EXPR:
12308 case GT_EXPR:
12309 case GE_EXPR:
12310 case LT_EXPR:
12311 case LE_EXPR:
12312 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12313 knows how to handle those cases. */
12314 wfl_op1 = TREE_OPERAND (node, 0);
12315 wfl_op2 = TREE_OPERAND (node, 1);
12317 CAN_COMPLETE_NORMALLY (node) = 1;
12318 /* Don't complete string nodes if dealing with the PLUS operand. */
12319 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12321 nn = java_complete_tree (wfl_op1);
12322 if (nn == error_mark_node)
12323 return error_mark_node;
12325 TREE_OPERAND (node, 0) = nn;
12327 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12329 nn = java_complete_tree (wfl_op2);
12330 if (nn == error_mark_node)
12331 return error_mark_node;
12333 TREE_OPERAND (node, 1) = nn;
12335 return patch_binop (node, wfl_op1, wfl_op2, 0);
12337 case INSTANCEOF_EXPR:
12338 wfl_op1 = TREE_OPERAND (node, 0);
12339 COMPLETE_CHECK_OP_0 (node);
12340 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1), 0);
12342 case UNARY_PLUS_EXPR:
12343 case NEGATE_EXPR:
12344 case TRUTH_NOT_EXPR:
12345 case BIT_NOT_EXPR:
12346 case PREDECREMENT_EXPR:
12347 case PREINCREMENT_EXPR:
12348 case POSTDECREMENT_EXPR:
12349 case POSTINCREMENT_EXPR:
12350 case CONVERT_EXPR:
12351 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12352 how to handle those cases. */
12353 wfl_op1 = TREE_OPERAND (node, 0);
12354 CAN_COMPLETE_NORMALLY (node) = 1;
12355 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12356 if (TREE_OPERAND (node, 0) == error_mark_node)
12357 return error_mark_node;
12358 node = patch_unaryop (node, wfl_op1);
12359 CAN_COMPLETE_NORMALLY (node) = 1;
12360 break;
12362 case ARRAY_REF:
12363 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12364 how to handle those cases. */
12365 wfl_op1 = TREE_OPERAND (node, 0);
12366 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12367 if (TREE_OPERAND (node, 0) == error_mark_node)
12368 return error_mark_node;
12369 if (!flag_emit_class_files)
12370 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12371 /* The same applies to wfl_op2 */
12372 wfl_op2 = TREE_OPERAND (node, 1);
12373 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12374 if (TREE_OPERAND (node, 1) == error_mark_node)
12375 return error_mark_node;
12376 if (!flag_emit_class_files)
12377 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12378 return patch_array_ref (node);
12380 case RECORD_TYPE:
12381 return node;;
12383 case COMPONENT_REF:
12384 /* The first step in the re-write of qualified name handling. FIXME.
12385 So far, this is only to support PRIMTYPE.class ->
12386 PRIMCLASS.TYPE. */
12388 tree prim_class = TREE_OPERAND (node, 0);
12389 tree name = TREE_OPERAND (node, 1);
12390 tree field;
12392 gcc_assert (TREE_CODE (prim_class) == NOP_EXPR);
12393 prim_class = java_complete_tree (TREE_TYPE (prim_class));
12394 gcc_assert (TREE_CODE (prim_class) == RECORD_TYPE);
12395 field = lookup_field_wrapper (prim_class, name);
12397 if (field == NULL_TREE)
12399 error ("missing static field %qs", IDENTIFIER_POINTER (name));
12400 return error_mark_node;
12402 if (! FIELD_STATIC (field))
12404 error ("not a static field %qs", IDENTIFIER_POINTER (name));
12405 return error_mark_node;
12407 return field;
12409 break;
12411 case THIS_EXPR:
12412 /* Can't use THIS in a static environment */
12413 if (!current_this)
12415 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12416 parse_error_context (wfl_operator,
12417 "Keyword %<this%> used outside allowed context");
12418 TREE_TYPE (node) = error_mark_node;
12419 return error_mark_node;
12421 if (ctxp->explicit_constructor_p)
12423 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12424 parse_error_context
12425 (wfl_operator, "Can't reference %<this%> or %<super%> before the superclass constructor has been called");
12426 TREE_TYPE (node) = error_mark_node;
12427 return error_mark_node;
12429 return current_this;
12431 case CLASS_LITERAL:
12432 CAN_COMPLETE_NORMALLY (node) = 1;
12433 node = patch_incomplete_class_ref (node);
12434 if (node == error_mark_node)
12435 return error_mark_node;
12436 break;
12438 default:
12439 CAN_COMPLETE_NORMALLY (node) = 1;
12440 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12441 and it's time to turn it into the appropriate String object */
12442 if ((nn = patch_string (node)))
12443 node = nn;
12444 else
12445 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12447 return node;
12450 /* Complete function call's argument. Return a nonzero value is an
12451 error was found. */
12453 static int
12454 complete_function_arguments (tree node)
12456 int flag = 0;
12457 tree cn;
12459 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12460 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12462 tree wfl = TREE_VALUE (cn), parm, temp;
12463 parm = java_complete_tree (wfl);
12465 if (parm == error_mark_node)
12467 flag = 1;
12468 continue;
12470 /* If we have a string literal that we haven't transformed yet or a
12471 crafted string buffer, as a result of the use of the String
12472 `+' operator. Build `parm.toString()' and expand it. */
12473 if ((temp = patch_string (parm)))
12474 parm = temp;
12476 TREE_VALUE (cn) = parm;
12478 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12479 return flag;
12482 /* Sometimes (for loops and variable initialized during their
12483 declaration), we want to wrap a statement around a WFL and turn it
12484 debugable. */
12486 static tree
12487 build_debugable_stmt (int location, tree stmt)
12489 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12491 #ifdef USE_MAPPED_LOCATION
12492 stmt = expr_add_location (stmt, location, 1);
12493 #else
12494 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12495 EXPR_WFL_LINECOL (stmt) = location;
12496 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12497 #endif
12499 return stmt;
12502 static tree
12503 build_expr_block (tree body, tree decls)
12505 tree node = make_node (BLOCK);
12506 BLOCK_EXPR_DECLS (node) = decls;
12507 BLOCK_EXPR_BODY (node) = body;
12508 if (body)
12509 TREE_TYPE (node) = TREE_TYPE (body);
12510 TREE_SIDE_EFFECTS (node) = 1;
12511 return node;
12514 /* Create a new function block and link it appropriately to current
12515 function block chain */
12517 static tree
12518 enter_block (void)
12520 tree b = build_expr_block (NULL_TREE, NULL_TREE);
12522 /* Link block B supercontext to the previous block. The current
12523 function DECL is used as supercontext when enter_a_block is called
12524 for the first time for a given function. The current function body
12525 (DECL_FUNCTION_BODY) is set to be block B. */
12527 tree fndecl = current_function_decl;
12529 if (!fndecl) {
12530 BLOCK_SUPERCONTEXT (b) = current_static_block;
12531 current_static_block = b;
12534 else if (!DECL_FUNCTION_BODY (fndecl))
12536 BLOCK_SUPERCONTEXT (b) = fndecl;
12537 DECL_FUNCTION_BODY (fndecl) = b;
12539 else
12541 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12542 DECL_FUNCTION_BODY (fndecl) = b;
12544 return b;
12547 /* Exit a block by changing the current function body
12548 (DECL_FUNCTION_BODY) to the current block super context, only if
12549 the block being exited isn't the method's top level one. */
12551 static tree
12552 exit_block (void)
12554 tree b;
12555 if (current_function_decl)
12557 b = DECL_FUNCTION_BODY (current_function_decl);
12558 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12559 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12561 else
12563 b = current_static_block;
12565 if (BLOCK_SUPERCONTEXT (b))
12566 current_static_block = BLOCK_SUPERCONTEXT (b);
12568 return b;
12571 /* Lookup for NAME in the nested function's blocks, all the way up to
12572 the current toplevel one. It complies with Java's local variable
12573 scoping rules. */
12575 static tree
12576 lookup_name_in_blocks (tree name)
12578 tree b = GET_CURRENT_BLOCK (current_function_decl);
12580 while (b != current_function_decl)
12582 tree current;
12584 /* Paranoid sanity check. To be removed */
12585 if (TREE_CODE (b) != BLOCK)
12586 abort ();
12588 for (current = BLOCK_EXPR_DECLS (b); current;
12589 current = TREE_CHAIN (current))
12590 if (DECL_NAME (current) == name)
12591 return current;
12592 b = BLOCK_SUPERCONTEXT (b);
12594 return NULL_TREE;
12597 static void
12598 maybe_absorb_scoping_blocks (void)
12600 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12602 tree b = exit_block ();
12603 java_method_add_stmt (current_function_decl, b);
12604 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", input_line));
12609 /* This section of the source is reserved to build_* functions that
12610 are building incomplete tree nodes and the patch_* functions that
12611 are completing them. */
12613 /* Wrap a non WFL node around a WFL. */
12615 static tree
12616 build_wfl_wrap (tree node, int location)
12618 tree wfl, node_to_insert = node;
12620 /* We want to process THIS . xxx symbolically, to keep it consistent
12621 with the way we're processing SUPER. A THIS from a primary as a
12622 different form than a SUPER. Turn THIS into something symbolic */
12623 if (TREE_CODE (node) == THIS_EXPR)
12624 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12625 else
12626 #ifdef USE_MAPPED_LOCATION
12627 wfl = build_unknown_wfl (NULL_TREE);
12629 SET_EXPR_LOCATION (wfl, location);
12630 #else
12631 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12633 EXPR_WFL_LINECOL (wfl) = location;
12634 #endif
12635 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12636 return wfl;
12639 /* Build a super() constructor invocation. Returns an empty statement if
12640 we're currently dealing with the class java.lang.Object. */
12642 static tree
12643 build_super_invocation (tree mdecl)
12645 if (DECL_CONTEXT (mdecl) == object_type_node)
12646 return build_java_empty_stmt ();
12647 else
12649 tree super_wfl = build_wfl_node (super_identifier_node);
12650 tree a = NULL_TREE, t;
12652 /* This is called after parsing is done, so the parser context
12653 won't be accurate. Set location info from current_class decl. */
12654 tree class_wfl = lookup_cl (TYPE_NAME (current_class));
12655 EXPR_WFL_LINECOL (super_wfl) = EXPR_WFL_LINECOL (class_wfl);
12657 /* If we're dealing with an anonymous class, pass the arguments
12658 of the crafted constructor along. */
12659 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12661 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12662 for (; t != end_params_node; t = TREE_CHAIN (t))
12663 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12665 return build_method_invocation (super_wfl, a);
12669 /* Build a SUPER/THIS qualified method invocation. */
12671 static tree
12672 build_this_super_qualified_invocation (int use_this, tree name, tree args,
12673 int lloc, int rloc)
12675 tree invok;
12676 tree wfl =
12677 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12678 EXPR_WFL_LINECOL (wfl) = lloc;
12679 invok = build_method_invocation (name, args);
12680 return make_qualified_primary (wfl, invok, rloc);
12683 /* Build an incomplete CALL_EXPR node. */
12685 static tree
12686 build_method_invocation (tree name, tree args)
12688 tree call = build3 (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12689 TREE_SIDE_EFFECTS (call) = 1;
12690 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12691 return call;
12694 /* Build an incomplete new xxx(...) node. */
12696 static tree
12697 build_new_invocation (tree name, tree args)
12699 tree call = build3 (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12700 TREE_SIDE_EFFECTS (call) = 1;
12701 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12702 return call;
12705 /* Build an incomplete assignment expression. */
12707 static tree
12708 build_assignment (int op, int op_location, tree lhs, tree rhs)
12710 tree assignment;
12711 /* Build the corresponding binop if we deal with a Compound
12712 Assignment operator. Mark the binop sub-tree as part of a
12713 Compound Assignment expression */
12714 if (op != ASSIGN_TK)
12716 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12717 COMPOUND_ASSIGN_P (rhs) = 1;
12719 assignment = build2 (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12720 TREE_SIDE_EFFECTS (assignment) = 1;
12721 EXPR_WFL_LINECOL (assignment) = op_location;
12722 return assignment;
12725 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12726 the buffer. This is used only for string conversion. */
12727 static char *
12728 string_convert_int_cst (tree node)
12730 /* Long.MIN_VALUE is -9223372036854775808, 20 characters. */
12731 static char buffer[21];
12733 unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12734 unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12735 char *p = buffer + sizeof (buffer);
12736 int neg = 0;
12738 unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12739 << (HOST_BITS_PER_WIDE_INT - 1));
12741 *--p = '\0';
12743 /* If negative, note the fact and negate the value. */
12744 if ((hi & hibit))
12746 lo = ~lo;
12747 hi = ~hi;
12748 if (++lo == 0)
12749 ++hi;
12750 neg = 1;
12753 /* Divide by 10 until there are no bits left. */
12756 unsigned HOST_WIDE_INT acc = 0;
12757 unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12758 unsigned int i;
12760 /* Use long division to compute the result and the remainder. */
12761 for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12763 /* Shift a bit into accumulator. */
12764 acc <<= 1;
12765 if ((hi & hibit))
12766 acc |= 1;
12768 /* Shift the value. */
12769 hi <<= 1;
12770 if ((lo & hibit))
12771 hi |= 1;
12772 lo <<= 1;
12774 /* Shift the correct bit into the result. */
12775 outhi <<= 1;
12776 if ((outlo & hibit))
12777 outhi |= 1;
12778 outlo <<= 1;
12779 if (acc >= 10)
12781 acc -= 10;
12782 outlo |= 1;
12786 /* '0' == 060 in Java, but might not be here (think EBCDIC). */
12787 *--p = '\060' + acc;
12789 hi = outhi;
12790 lo = outlo;
12792 while (hi || lo);
12794 if (neg)
12795 *--p = '\055'; /* '-' == 055 in Java, but might not be here. */
12797 return p;
12800 /* Print an INTEGER_CST node in a static buffer, and return the
12801 buffer. This is used only for error handling. */
12802 char *
12803 print_int_node (tree node)
12805 static char buffer [80];
12806 if (TREE_CONSTANT_OVERFLOW (node))
12807 sprintf (buffer, "<overflow>");
12809 if (TREE_INT_CST_HIGH (node) == 0)
12810 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12811 TREE_INT_CST_LOW (node));
12812 else if (TREE_INT_CST_HIGH (node) == -1
12813 && TREE_INT_CST_LOW (node) != 0)
12814 sprintf (buffer, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
12815 -TREE_INT_CST_LOW (node));
12816 else
12817 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12818 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12820 return buffer;
12824 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12825 context. */
12827 /* 15.25 Assignment operators. */
12829 static tree
12830 patch_assignment (tree node, tree wfl_op1)
12832 tree rhs = TREE_OPERAND (node, 1);
12833 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12834 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12835 int error_found = 0;
12836 int lvalue_from_array = 0;
12837 int is_return = 0;
12839 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12841 /* Lhs can be a named variable */
12842 if (JDECL_P (lvalue))
12844 lhs_type = TREE_TYPE (lvalue);
12846 /* Or Lhs can be an array access. */
12847 else if (TREE_CODE (lvalue) == ARRAY_REF)
12849 lhs_type = TREE_TYPE (lvalue);
12850 lvalue_from_array = 1;
12852 /* Or a field access */
12853 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12854 lhs_type = TREE_TYPE (lvalue);
12855 /* Or a function return slot */
12856 else if (TREE_CODE (lvalue) == RESULT_DECL)
12858 /* If the return type is an integral type, then we create the
12859 RESULT_DECL with a promoted type, but we need to do these
12860 checks against the unpromoted type to ensure type safety. So
12861 here we look at the real type, not the type of the decl we
12862 are modifying. */
12863 lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12864 is_return = 1;
12866 /* Otherwise, we might want to try to write into an optimized static
12867 final, this is an of a different nature, reported further on. */
12868 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12869 && resolve_expression_name (wfl_op1, &llvalue))
12871 lhs_type = TREE_TYPE (lvalue);
12873 else
12875 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12876 error_found = 1;
12879 rhs_type = TREE_TYPE (rhs);
12881 /* 5.1 Try the assignment conversion for builtin type. */
12882 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12884 /* 5.2 If it failed, try a reference conversion */
12885 if (!new_rhs)
12886 new_rhs = try_reference_assignconv (lhs_type, rhs);
12888 /* 15.25.2 If we have a compound assignment, convert RHS into the
12889 type of the LHS */
12890 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12891 new_rhs = convert (lhs_type, rhs);
12893 /* Explicit cast required. This is an error */
12894 if (!new_rhs)
12896 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12897 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12898 tree wfl;
12899 char operation [32]; /* Max size known */
12901 /* If the assignment is part of a declaration, we use the WFL of
12902 the declared variable to point out the error and call it a
12903 declaration problem. If the assignment is a genuine =
12904 operator, we call is a operator `=' problem, otherwise we
12905 call it an assignment problem. In both of these last cases,
12906 we use the WFL of the operator to indicate the error. */
12908 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12910 wfl = wfl_op1;
12911 strcpy (operation, "declaration");
12913 else
12915 wfl = wfl_operator;
12916 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12917 strcpy (operation, "assignment");
12918 else if (is_return)
12919 strcpy (operation, "'return'");
12920 else
12921 strcpy (operation, "'='");
12924 if (!valid_cast_to_p (rhs_type, lhs_type))
12925 parse_error_context
12926 (wfl, "Incompatible type for %s. Can't convert %qs to %qs",
12927 operation, t1, t2);
12928 else
12929 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert %qs to %qs",
12930 operation, t1, t2);
12931 free (t1); free (t2);
12932 error_found = 1;
12935 if (error_found)
12936 return error_mark_node;
12938 /* If we're processing a `return' statement, promote the actual type
12939 to the promoted type. */
12940 if (is_return)
12941 new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12943 /* 10.10: Array Store Exception runtime check */
12944 if (!flag_emit_class_files
12945 && lvalue_from_array
12946 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12948 tree array, store_check, base, index_expr;
12950 /* Save RHS so that it doesn't get re-evaluated by the store check. */
12951 new_rhs = save_expr (new_rhs);
12953 /* Get the INDIRECT_REF. */
12954 array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12955 /* Get the array pointer expr. */
12956 array = TREE_OPERAND (array, 0);
12957 store_check = build_java_arraystore_check (array, new_rhs);
12959 index_expr = TREE_OPERAND (lvalue, 1);
12961 if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12963 /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12964 happen before the store check, so prepare to insert the store
12965 check within the second operand of the existing COMPOUND_EXPR. */
12966 base = index_expr;
12968 else
12969 base = lvalue;
12971 index_expr = TREE_OPERAND (base, 1);
12972 TREE_OPERAND (base, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (index_expr),
12973 store_check, index_expr);
12976 /* Final locals can be used as case values in switch
12977 statement. Prepare them for this eventuality. */
12978 if (TREE_CODE (lvalue) == VAR_DECL
12979 && DECL_FINAL (lvalue)
12980 && TREE_CONSTANT (new_rhs)
12981 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12982 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12985 TREE_CONSTANT (lvalue) = 1;
12986 TREE_INVARIANT (lvalue) = 1;
12987 DECL_INITIAL (lvalue) = new_rhs;
12990 /* Copy the rhs if it's a reference. */
12991 if (! flag_check_references && ! flag_emit_class_files && optimize > 0)
12993 switch (TREE_CODE (new_rhs))
12995 case ARRAY_REF:
12996 case INDIRECT_REF:
12997 case COMPONENT_REF:
12998 /* Transform a = foo.bar
12999 into a = ({int tmp; tmp = foo.bar;}).
13000 We need to ensure that if a read from memory fails
13001 because of a NullPointerException, a destination variable
13002 will remain unchanged. An explicit temporary does what
13003 we need.
13005 If flag_check_references is set, this is unnecessary
13006 because we'll check each reference before doing any
13007 reads. If optimize is not set the result will never be
13008 written to a stack slot that contains the LHS. */
13010 tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"),
13011 TREE_TYPE (new_rhs));
13012 tree block = make_node (BLOCK);
13013 tree assignment
13014 = build2 (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
13015 DECL_CONTEXT (tmp) = current_function_decl;
13016 TREE_TYPE (block) = TREE_TYPE (new_rhs);
13017 BLOCK_VARS (block) = tmp;
13018 BLOCK_EXPR_BODY (block) = assignment;
13019 TREE_SIDE_EFFECTS (block) = 1;
13020 new_rhs = block;
13022 break;
13023 default:
13024 break;
13028 TREE_OPERAND (node, 0) = lvalue;
13029 TREE_OPERAND (node, 1) = new_rhs;
13030 TREE_TYPE (node) = lhs_type;
13031 return node;
13034 /* Check that type SOURCE can be cast into type DEST. If the cast
13035 can't occur at all, return NULL; otherwise, return a possibly
13036 modified rhs. */
13038 static tree
13039 try_reference_assignconv (tree lhs_type, tree rhs)
13041 tree new_rhs = NULL_TREE;
13042 tree rhs_type = TREE_TYPE (rhs);
13044 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
13046 /* `null' may be assigned to any reference type */
13047 if (rhs == null_pointer_node)
13048 new_rhs = null_pointer_node;
13049 /* Try the reference assignment conversion */
13050 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
13051 new_rhs = rhs;
13052 /* This is a magic assignment that we process differently */
13053 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
13054 new_rhs = rhs;
13056 return new_rhs;
13059 /* Check that RHS can be converted into LHS_TYPE by the assignment
13060 conversion (5.2), for the cases of RHS being a builtin type. Return
13061 NULL_TREE if the conversion fails or if because RHS isn't of a
13062 builtin type. Return a converted RHS if the conversion is possible. */
13064 static tree
13065 try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
13067 tree new_rhs = NULL_TREE;
13068 tree rhs_type = TREE_TYPE (rhs);
13070 /* Handle boolean specially. */
13071 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13072 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13074 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13075 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13076 new_rhs = rhs;
13079 /* 5.1.1 Try Identity Conversion,
13080 5.1.2 Try Widening Primitive Conversion */
13081 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
13082 new_rhs = convert (lhs_type, rhs);
13084 /* Try a narrowing primitive conversion (5.1.3):
13085 - expression is a constant expression of type byte, short, char,
13086 or int, AND
13087 - variable is byte, short or char AND
13088 - The value of the expression is representable in the type of the
13089 variable */
13090 else if ((rhs_type == byte_type_node || rhs_type == short_type_node
13091 || rhs_type == char_type_node || rhs_type == int_type_node)
13092 && TREE_CONSTANT (rhs)
13093 && (lhs_type == byte_type_node || lhs_type == char_type_node
13094 || lhs_type == short_type_node))
13096 if (int_fits_type_p (rhs, lhs_type))
13097 new_rhs = convert (lhs_type, rhs);
13098 else if (wfl_op1) /* Might be called with a NULL */
13099 parse_warning_context
13100 (wfl_op1,
13101 "Constant expression %qs too wide for narrowing primitive conversion to %qs",
13102 print_int_node (rhs), lang_printable_name (lhs_type, 0));
13103 /* Reported a warning that will turn into an error further
13104 down, so we don't return */
13107 return new_rhs;
13110 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
13111 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
13112 0 is the conversion test fails. This implements parts the method
13113 invocation conversion (5.3). */
13115 static int
13116 valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
13118 /* 5.1.1: This is the identity conversion part. */
13119 if (lhs_type == rhs_type)
13120 return 1;
13122 /* Reject non primitive types and boolean conversions. */
13123 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
13124 return 0;
13126 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
13127 than a char can't be converted into a char. Short can't too, but
13128 the < test below takes care of that */
13129 if (lhs_type == char_type_node && rhs_type == byte_type_node)
13130 return 0;
13132 /* Accept all promoted type here. Note, we can't use <= in the test
13133 below, because we still need to bounce out assignments of short
13134 to char and the likes */
13135 if (lhs_type == int_type_node
13136 && (rhs_type == promoted_byte_type_node
13137 || rhs_type == promoted_short_type_node
13138 || rhs_type == promoted_char_type_node
13139 || rhs_type == promoted_boolean_type_node))
13140 return 1;
13142 /* From here, an integral is widened if its precision is smaller
13143 than the precision of the LHS or if the LHS is a floating point
13144 type, or the RHS is a float and the RHS a double. */
13145 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13146 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13147 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13148 || (rhs_type == float_type_node && lhs_type == double_type_node))
13149 return 1;
13151 return 0;
13154 /* Check that something of SOURCE type can be assigned or cast to
13155 something of DEST type at runtime. Return 1 if the operation is
13156 valid, 0 otherwise. If CAST is set to 1, we're treating the case
13157 were SOURCE is cast into DEST, which borrows a lot of the
13158 assignment check. */
13160 static int
13161 valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
13163 /* SOURCE or DEST might be null if not from a declared entity. */
13164 if (!source || !dest)
13165 return 0;
13166 if (JNULLP_TYPE_P (source))
13167 return 1;
13168 if (TREE_CODE (source) == POINTER_TYPE)
13169 source = TREE_TYPE (source);
13170 if (TREE_CODE (dest) == POINTER_TYPE)
13171 dest = TREE_TYPE (dest);
13173 /* If source and dest are being compiled from bytecode, they may need to
13174 be loaded. */
13175 if (CLASS_P (source) && !CLASS_LOADED_P (source))
13177 load_class (source, 1);
13178 safe_layout_class (source);
13180 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13182 load_class (dest, 1);
13183 safe_layout_class (dest);
13186 /* Case where SOURCE is a class type */
13187 if (TYPE_CLASS_P (source))
13189 if (TYPE_CLASS_P (dest))
13190 return (source == dest
13191 || inherits_from_p (source, dest)
13192 || (cast && inherits_from_p (dest, source)));
13193 if (TYPE_INTERFACE_P (dest))
13195 /* If doing a cast and SOURCE is final, the operation is
13196 always correct a compile time (because even if SOURCE
13197 does not implement DEST, a subclass of SOURCE might). */
13198 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13199 return 1;
13200 /* Otherwise, SOURCE must implement DEST */
13201 return interface_of_p (dest, source);
13203 /* DEST is an array, cast permitted if SOURCE is of Object type */
13204 return (cast && source == object_type_node ? 1 : 0);
13206 if (TYPE_INTERFACE_P (source))
13208 if (TYPE_CLASS_P (dest))
13210 /* If not casting, DEST must be the Object type */
13211 if (!cast)
13212 return dest == object_type_node;
13213 /* We're doing a cast. The cast is always valid is class
13214 DEST is not final, otherwise, DEST must implement SOURCE */
13215 else if (!CLASS_FINAL (TYPE_NAME (dest)))
13216 return 1;
13217 else
13218 return interface_of_p (source, dest);
13220 if (TYPE_INTERFACE_P (dest))
13222 /* If doing a cast, then if SOURCE and DEST contain method
13223 with the same signature but different return type, then
13224 this is a (compile time) error */
13225 if (cast)
13227 tree method_source, method_dest;
13228 tree source_type;
13229 tree source_sig;
13230 tree source_name;
13231 for (method_source = TYPE_METHODS (source); method_source;
13232 method_source = TREE_CHAIN (method_source))
13234 source_sig =
13235 build_java_argument_signature (TREE_TYPE (method_source));
13236 source_type = TREE_TYPE (TREE_TYPE (method_source));
13237 source_name = DECL_NAME (method_source);
13238 for (method_dest = TYPE_METHODS (dest);
13239 method_dest; method_dest = TREE_CHAIN (method_dest))
13240 if (source_sig ==
13241 build_java_argument_signature (TREE_TYPE (method_dest))
13242 && source_name == DECL_NAME (method_dest)
13243 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13244 return 0;
13246 return 1;
13248 else
13249 return source == dest || interface_of_p (dest, source);
13251 else
13253 /* Array */
13254 return (cast
13255 && (DECL_NAME (TYPE_NAME (source))
13256 == java_lang_cloneable_identifier_node
13257 || (DECL_NAME (TYPE_NAME (source))
13258 == java_io_serializable_identifier_node)));
13261 if (TYPE_ARRAY_P (source))
13263 if (TYPE_CLASS_P (dest))
13264 return dest == object_type_node;
13265 /* Can't cast an array to an interface unless the interface is
13266 java.lang.Cloneable or java.io.Serializable. */
13267 if (TYPE_INTERFACE_P (dest))
13268 return (DECL_NAME (TYPE_NAME (dest))
13269 == java_lang_cloneable_identifier_node
13270 || (DECL_NAME (TYPE_NAME (dest))
13271 == java_io_serializable_identifier_node));
13272 else /* Arrays */
13274 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13275 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13277 /* In case of severe errors, they turn out null */
13278 if (!dest_element_type || !source_element_type)
13279 return 0;
13280 if (source_element_type == dest_element_type)
13281 return 1;
13282 return valid_ref_assignconv_cast_p (source_element_type,
13283 dest_element_type, cast);
13285 return 0;
13287 return 0;
13290 static int
13291 valid_cast_to_p (tree source, tree dest)
13293 if (TREE_CODE (source) == POINTER_TYPE)
13294 source = TREE_TYPE (source);
13295 if (TREE_CODE (dest) == POINTER_TYPE)
13296 dest = TREE_TYPE (dest);
13298 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13299 return valid_ref_assignconv_cast_p (source, dest, 1);
13301 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13302 return 1;
13304 else if (TREE_CODE (source) == BOOLEAN_TYPE
13305 && TREE_CODE (dest) == BOOLEAN_TYPE)
13306 return 1;
13308 return 0;
13311 static tree
13312 do_unary_numeric_promotion (tree arg)
13314 tree type = TREE_TYPE (arg);
13315 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13316 || TREE_CODE (type) == CHAR_TYPE)
13317 arg = convert (int_type_node, arg);
13318 return arg;
13321 /* Return a nonzero value if SOURCE can be converted into DEST using
13322 the method invocation conversion rule (5.3). */
13323 static int
13324 valid_method_invocation_conversion_p (tree dest, tree source)
13326 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13327 && valid_builtin_assignconv_identity_widening_p (dest, source))
13328 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13329 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13330 && valid_ref_assignconv_cast_p (source, dest, 0)));
13333 /* Build an incomplete binop expression. */
13335 static tree
13336 build_binop (enum tree_code op, int op_location, tree op1, tree op2)
13338 tree binop = build2 (op, NULL_TREE, op1, op2);
13339 TREE_SIDE_EFFECTS (binop) = 1;
13340 /* Store the location of the operator, for better error report. The
13341 string of the operator will be rebuild based on the OP value. */
13342 EXPR_WFL_LINECOL (binop) = op_location;
13343 return binop;
13346 /* Build the string of the operator retained by NODE. If NODE is part
13347 of a compound expression, add an '=' at the end of the string. This
13348 function is called when an error needs to be reported on an
13349 operator. The string is returned as a pointer to a static character
13350 buffer. */
13352 static char *
13353 operator_string (tree node)
13355 #define BUILD_OPERATOR_STRING(S) \
13357 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13358 return buffer; \
13361 static char buffer [10];
13362 switch (TREE_CODE (node))
13364 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13365 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13366 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13367 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13368 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13369 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13370 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13371 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13372 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13373 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13374 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13375 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13376 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13377 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13378 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13379 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13380 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13381 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13382 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13383 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13384 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13385 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13386 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13387 case PREINCREMENT_EXPR: /* Fall through */
13388 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13389 case PREDECREMENT_EXPR: /* Fall through */
13390 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13391 default:
13392 internal_error ("unregistered operator %s",
13393 tree_code_name [TREE_CODE (node)]);
13395 return NULL;
13396 #undef BUILD_OPERATOR_STRING
13399 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13401 static int
13402 java_decl_equiv (tree var_acc1, tree var_acc2)
13404 if (JDECL_P (var_acc1))
13405 return (var_acc1 == var_acc2);
13407 return (TREE_CODE (var_acc1) == COMPONENT_REF
13408 && TREE_CODE (var_acc2) == COMPONENT_REF
13409 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13410 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13411 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13414 /* Return a nonzero value if CODE is one of the operators that can be
13415 used in conjunction with the `=' operator in a compound assignment. */
13417 static int
13418 binop_compound_p (enum tree_code code)
13420 int i;
13421 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13422 if (binop_lookup [i] == code)
13423 break;
13425 return i < BINOP_COMPOUND_CANDIDATES;
13428 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13430 static tree
13431 java_refold (tree t)
13433 tree c, b, ns, decl;
13435 if (TREE_CODE (t) != MODIFY_EXPR)
13436 return t;
13438 c = TREE_OPERAND (t, 1);
13439 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13440 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13441 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13442 return t;
13444 /* Now the left branch of the binary operator. */
13445 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13446 if (! (b && TREE_CODE (b) == NOP_EXPR
13447 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13448 return t;
13450 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13451 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13452 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13453 return t;
13455 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13456 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13457 /* It's got to be the an equivalent decl */
13458 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13460 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13461 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13462 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13463 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13464 /* Change the right part of the BINOP_EXPR */
13465 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13468 return t;
13471 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13472 errors but we modify NODE so that it contains the type computed
13473 according to the expression, when it's fixed. Otherwise, we write
13474 error_mark_node as the type. It allows us to further the analysis
13475 of remaining nodes and detects more errors in certain cases. */
13477 static tree
13478 patch_binop (tree node, tree wfl_op1, tree wfl_op2, int folding)
13480 tree op1 = TREE_OPERAND (node, 0);
13481 tree op2 = TREE_OPERAND (node, 1);
13482 tree op1_type = TREE_TYPE (op1);
13483 tree op2_type = TREE_TYPE (op2);
13484 tree prom_type = NULL_TREE, cn;
13485 enum tree_code code = TREE_CODE (node);
13487 /* If 1, tell the routine that we have to return error_mark_node
13488 after checking for the initialization of the RHS */
13489 int error_found = 0;
13491 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13493 /* If either op<n>_type are NULL, this might be early signs of an
13494 error situation, unless it's too early to tell (in case we're
13495 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13496 correctly so the error can be later on reported accurately. */
13497 if (! (code == PLUS_EXPR || code == NE_EXPR
13498 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13500 tree n;
13501 if (! op1_type)
13503 n = java_complete_tree (op1);
13504 op1_type = TREE_TYPE (n);
13506 if (! op2_type)
13508 n = java_complete_tree (op2);
13509 op2_type = TREE_TYPE (n);
13513 switch (code)
13515 /* 15.16 Multiplicative operators */
13516 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13517 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13518 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13519 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13520 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13522 if (!JNUMERIC_TYPE_P (op1_type))
13523 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13524 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13525 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13526 TREE_TYPE (node) = error_mark_node;
13527 error_found = 1;
13528 break;
13530 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13532 /* Detect integral division by zero */
13533 if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13534 && TREE_CODE (prom_type) == INTEGER_TYPE
13535 && (op2 == integer_zero_node || op2 == long_zero_node ||
13536 (TREE_CODE (op2) == INTEGER_CST &&
13537 ! TREE_INT_CST_LOW (op2) && ! TREE_INT_CST_HIGH (op2))))
13539 parse_warning_context
13540 (wfl_operator,
13541 "Evaluating this expression will result in an arithmetic exception being thrown");
13542 TREE_CONSTANT (node) = 0;
13543 TREE_INVARIANT (node) = 0;
13546 /* Change the division operator if necessary */
13547 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13548 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13550 /* Before divisions as is disappear, try to simplify and bail if
13551 applicable, otherwise we won't perform even simple
13552 simplifications like (1-1)/3. We can't do that with floating
13553 point number, folds can't handle them at this stage. */
13554 if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13555 && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13557 TREE_TYPE (node) = prom_type;
13558 node = fold (node);
13559 if (TREE_CODE (node) != code)
13560 return node;
13563 if (TREE_CODE (prom_type) == INTEGER_TYPE
13564 && flag_use_divide_subroutine
13565 && ! flag_emit_class_files
13566 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13567 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13569 /* This one is more complicated. FLOATs are processed by a
13570 function call to soft_fmod. Duplicate the value of the
13571 COMPOUND_ASSIGN_P flag. */
13572 if (code == TRUNC_MOD_EXPR)
13574 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13575 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13576 return mod;
13578 break;
13580 /* 15.17 Additive Operators */
13581 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13583 /* Operation is valid if either one argument is a string
13584 constant, a String object or a StringBuffer crafted for the
13585 purpose of the a previous usage of the String concatenation
13586 operator */
13588 if (TREE_CODE (op1) == STRING_CST
13589 || TREE_CODE (op2) == STRING_CST
13590 || JSTRING_TYPE_P (op1_type)
13591 || JSTRING_TYPE_P (op2_type)
13592 || IS_CRAFTED_STRING_BUFFER_P (op1)
13593 || IS_CRAFTED_STRING_BUFFER_P (op2))
13594 return build_string_concatenation (op1, op2);
13596 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13597 Numeric Types */
13598 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13600 if (!JNUMERIC_TYPE_P (op1_type))
13601 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13602 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13603 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13604 TREE_TYPE (node) = error_mark_node;
13605 error_found = 1;
13606 break;
13608 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13609 break;
13611 /* 15.18 Shift Operators */
13612 case LSHIFT_EXPR:
13613 case RSHIFT_EXPR:
13614 case URSHIFT_EXPR:
13615 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13617 if (!JINTEGRAL_TYPE_P (op1_type))
13618 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13619 else
13621 if (JNUMERIC_TYPE_P (op2_type))
13622 parse_error_context (wfl_operator,
13623 "Incompatible type for %qs. Explicit cast needed to convert shift distance from %qs to integral",
13624 operator_string (node),
13625 lang_printable_name (op2_type, 0));
13626 else
13627 parse_error_context (wfl_operator,
13628 "Incompatible type for %qs. Can't convert shift distance from %qs to integral",
13629 operator_string (node),
13630 lang_printable_name (op2_type, 0));
13632 TREE_TYPE (node) = error_mark_node;
13633 error_found = 1;
13634 break;
13637 /* Unary numeric promotion (5.6.1) is performed on each operand
13638 separately */
13639 op1 = do_unary_numeric_promotion (op1);
13640 op2 = do_unary_numeric_promotion (op2);
13642 /* If the right hand side is of type `long', first cast it to
13643 `int'. */
13644 if (TREE_TYPE (op2) == long_type_node)
13645 op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13647 /* The type of the shift expression is the type of the promoted
13648 type of the left-hand operand */
13649 prom_type = TREE_TYPE (op1);
13651 /* Shift int only up to 0x1f and long up to 0x3f */
13652 if (prom_type == int_type_node)
13653 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13654 build_int_cst (NULL_TREE, 0x1f)));
13655 else
13656 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13657 build_int_cst (NULL_TREE, 0x3f)));
13659 /* The >>> operator is a >> operating on unsigned quantities */
13660 if (code == URSHIFT_EXPR && (folding || ! flag_emit_class_files))
13662 tree to_return;
13663 tree utype = java_unsigned_type (prom_type);
13664 op1 = convert (utype, op1);
13666 to_return = fold_build2 (RSHIFT_EXPR, utype, op1, op2);
13667 to_return = convert (prom_type, to_return);
13668 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13669 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13670 TREE_SIDE_EFFECTS (to_return)
13671 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13672 return to_return;
13674 break;
13676 /* 15.19.1 Type Comparison Operator instanceof */
13677 case INSTANCEOF_EXPR:
13679 TREE_TYPE (node) = boolean_type_node;
13681 /* OP1_TYPE might be NULL when OP1 is a string constant. */
13682 if ((cn = patch_string (op1)))
13684 op1 = cn;
13685 op1_type = TREE_TYPE (op1);
13687 if (op1_type == NULL_TREE)
13688 abort ();
13690 if (!(op2_type = resolve_type_during_patch (op2)))
13691 return error_mark_node;
13693 /* The first operand must be a reference type or the null type */
13694 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13695 error_found = 1; /* Error reported further below */
13697 /* The second operand must be a reference type */
13698 if (!JREFERENCE_TYPE_P (op2_type))
13700 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13701 parse_error_context
13702 (wfl_operator, "Invalid argument %qs for %<instanceof%>",
13703 lang_printable_name (op2_type, 0));
13704 error_found = 1;
13707 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13709 /* If the first operand is null, the result is always false */
13710 if (op1 == null_pointer_node)
13711 return boolean_false_node;
13712 else if (flag_emit_class_files)
13714 TREE_OPERAND (node, 1) = op2_type;
13715 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13716 return node;
13718 /* Otherwise we have to invoke instance of to figure it out */
13719 else
13720 return build_instanceof (op1, op2_type);
13722 /* There is no way the expression operand can be an instance of
13723 the type operand. This is a compile time error. */
13724 else
13726 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13727 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13728 parse_error_context
13729 (wfl_operator, "Impossible for %qs to be instance of %qs",
13730 t1, lang_printable_name (op2_type, 0));
13731 free (t1);
13732 error_found = 1;
13735 break;
13737 /* 15.21 Bitwise and Logical Operators */
13738 case BIT_AND_EXPR:
13739 case BIT_XOR_EXPR:
13740 case BIT_IOR_EXPR:
13741 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13742 /* Binary numeric promotion is performed on both operand and the
13743 expression retain that type */
13744 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13746 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13747 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13748 /* The type of the bitwise operator expression is BOOLEAN */
13749 prom_type = boolean_type_node;
13750 else
13752 if (!JINTEGRAL_TYPE_P (op1_type))
13753 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13754 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13755 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13756 TREE_TYPE (node) = error_mark_node;
13757 error_found = 1;
13758 /* Insert a break here if adding thing before the switch's
13759 break for this case */
13761 break;
13763 /* 15.22 Conditional-And Operator */
13764 case TRUTH_ANDIF_EXPR:
13765 /* 15.23 Conditional-Or Operator */
13766 case TRUTH_ORIF_EXPR:
13767 /* Operands must be of BOOLEAN type */
13768 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13769 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13771 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13772 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13773 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13774 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13775 TREE_TYPE (node) = boolean_type_node;
13776 error_found = 1;
13777 break;
13779 else if (integer_zerop (op1))
13781 return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13783 else if (integer_onep (op1))
13785 return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13787 /* The type of the conditional operators is BOOLEAN */
13788 prom_type = boolean_type_node;
13789 break;
13791 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13792 case LT_EXPR:
13793 case GT_EXPR:
13794 case LE_EXPR:
13795 case GE_EXPR:
13796 /* The type of each of the operands must be a primitive numeric
13797 type */
13798 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13800 if (!JNUMERIC_TYPE_P (op1_type))
13801 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13802 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13803 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13804 TREE_TYPE (node) = boolean_type_node;
13805 error_found = 1;
13806 break;
13808 /* Binary numeric promotion is performed on the operands */
13809 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13810 /* The type of the relation expression is always BOOLEAN */
13811 prom_type = boolean_type_node;
13812 break;
13814 /* 15.20 Equality Operator */
13815 case EQ_EXPR:
13816 case NE_EXPR:
13817 /* It's time for us to patch the strings. */
13818 if ((cn = patch_string (op1)))
13820 op1 = cn;
13821 op1_type = TREE_TYPE (op1);
13823 if ((cn = patch_string (op2)))
13825 op2 = cn;
13826 op2_type = TREE_TYPE (op2);
13829 /* 15.20.1 Numerical Equality Operators == and != */
13830 /* Binary numeric promotion is performed on the operands */
13831 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13832 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13834 /* 15.20.2 Boolean Equality Operators == and != */
13835 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13836 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13837 ; /* Nothing to do here */
13839 /* 15.20.3 Reference Equality Operators == and != */
13840 /* Types have to be either references or the null type. If
13841 they're references, it must be possible to convert either
13842 type to the other by casting conversion. */
13843 else if ((op1 == null_pointer_node && op2 == null_pointer_node)
13844 || (op1 == null_pointer_node && JREFERENCE_TYPE_P (op2_type))
13845 || (JREFERENCE_TYPE_P (op1_type) && op2 == null_pointer_node)
13846 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13847 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13848 || valid_ref_assignconv_cast_p (op2_type,
13849 op1_type, 1))))
13850 ; /* Nothing to do here */
13852 /* Else we have an error figure what can't be converted into
13853 what and report the error */
13854 else
13856 char *t1;
13857 t1 = xstrdup (lang_printable_name (op1_type, 0));
13858 parse_error_context
13859 (wfl_operator,
13860 "Incompatible type for %qs. Can't convert %qs to %qs",
13861 operator_string (node), t1,
13862 lang_printable_name (op2_type, 0));
13863 free (t1);
13864 TREE_TYPE (node) = boolean_type_node;
13865 error_found = 1;
13866 break;
13868 prom_type = boolean_type_node;
13869 break;
13870 default:
13871 abort ();
13874 if (error_found)
13875 return error_mark_node;
13877 TREE_OPERAND (node, 0) = op1;
13878 TREE_OPERAND (node, 1) = op2;
13879 TREE_TYPE (node) = prom_type;
13880 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13882 /* fold does not respect side-effect order as required for Java but not C.
13883 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13884 * bytecode.
13886 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13887 : ! TREE_SIDE_EFFECTS (node))
13888 node = fold (node);
13889 return node;
13892 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13893 zero value, the value of CSTE comes after the valude of STRING */
13895 static tree
13896 do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13898 const char *old = TREE_STRING_POINTER (cste);
13899 int old_len = TREE_STRING_LENGTH (cste);
13900 int len = old_len + string_len;
13901 char *new = alloca (len+1);
13903 if (after)
13905 memcpy (new, string, string_len);
13906 memcpy (&new [string_len], old, old_len);
13908 else
13910 memcpy (new, old, old_len);
13911 memcpy (&new [old_len], string, string_len);
13913 new [len] = '\0';
13914 return build_string (len, new);
13917 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13918 new STRING_CST on success, NULL_TREE on failure. */
13920 static tree
13921 merge_string_cste (tree op1, tree op2, int after)
13923 /* Handle two string constants right away. */
13924 if (TREE_CODE (op2) == STRING_CST)
13925 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13926 TREE_STRING_LENGTH (op2), after);
13928 /* Reasonable integer constant can be treated right away. */
13929 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13931 static const char *const boolean_true = "true";
13932 static const char *const boolean_false = "false";
13933 static const char *const null_pointer = "null";
13934 char ch[4];
13935 const char *string;
13937 if (op2 == boolean_true_node)
13938 string = boolean_true;
13939 else if (op2 == boolean_false_node)
13940 string = boolean_false;
13941 else if (op2 == null_pointer_node
13942 || (integer_zerop (op2)
13943 && TREE_CODE (TREE_TYPE (op2)) == POINTER_TYPE))
13944 /* FIXME: null is not a compile-time constant, so it is only safe to
13945 merge if the overall expression is non-constant. However, this
13946 code always merges without checking the overall expression. */
13947 string = null_pointer;
13948 else if (TREE_TYPE (op2) == char_type_node)
13950 /* Convert the character into UTF-8. */
13951 unsigned int c = (unsigned int) TREE_INT_CST_LOW (op2);
13952 unsigned char *p = (unsigned char *) ch;
13953 if (0x01 <= c && c <= 0x7f)
13954 *p++ = (unsigned char) c;
13955 else if (c < 0x7ff)
13957 *p++ = (unsigned char) (c >> 6 | 0xc0);
13958 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13960 else
13962 *p++ = (unsigned char) (c >> 12 | 0xe0);
13963 *p++ = (unsigned char) (((c >> 6) & 0x3f) | 0x80);
13964 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13966 *p = '\0';
13968 string = ch;
13970 else
13971 string = string_convert_int_cst (op2);
13973 return do_merge_string_cste (op1, string, strlen (string), after);
13975 return NULL_TREE;
13978 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13979 has to be a STRING_CST and the other part must be a STRING_CST or a
13980 INTEGRAL constant. Return a new STRING_CST if the operation
13981 succeed, NULL_TREE otherwise.
13983 If the case we want to optimize for space, we might want to return
13984 NULL_TREE for each invocation of this routine. FIXME */
13986 static tree
13987 string_constant_concatenation (tree op1, tree op2)
13989 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13991 tree string, rest;
13992 int invert;
13994 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13995 rest = (string == op1 ? op2 : op1);
13996 invert = (string == op1 ? 0 : 1 );
13998 /* Walk REST, only if it looks reasonable */
13999 if (TREE_CODE (rest) != STRING_CST
14000 && !IS_CRAFTED_STRING_BUFFER_P (rest)
14001 && !JSTRING_TYPE_P (TREE_TYPE (rest))
14002 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
14004 rest = java_complete_tree (rest);
14005 if (rest == error_mark_node)
14006 return error_mark_node;
14007 rest = fold (rest);
14009 return merge_string_cste (string, rest, invert);
14011 return NULL_TREE;
14014 /* Implement the `+' operator. Does static optimization if possible,
14015 otherwise create (if necessary) and append elements to a
14016 StringBuffer. The StringBuffer will be carried around until it is
14017 used for a function call or an assignment. Then toString() will be
14018 called on it to turn it into a String object. */
14020 static tree
14021 build_string_concatenation (tree op1, tree op2)
14023 tree result;
14024 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
14026 /* Try to do some static optimization */
14027 if ((result = string_constant_concatenation (op1, op2)))
14028 return result;
14030 /* Discard empty strings on either side of the expression */
14031 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
14033 op1 = op2;
14034 op2 = NULL_TREE;
14036 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
14037 op2 = NULL_TREE;
14039 /* If operands are string constant, turn then into object references */
14040 if (TREE_CODE (op1) == STRING_CST)
14041 op1 = patch_string_cst (op1);
14042 if (op2 && TREE_CODE (op2) == STRING_CST)
14043 op2 = patch_string_cst (op2);
14045 /* If either one of the constant is null and the other non null
14046 operand is a String constant, return it. */
14047 if ((TREE_CODE (op1) == STRING_CST) && !op2)
14048 return op1;
14050 /* If OP1 isn't already a StringBuffer, create and
14051 initialize a new one */
14052 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
14054 /* Two solutions here:
14055 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
14056 2) OP1 is something else, we call new StringBuffer().append(OP1). */
14057 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
14058 op1 = BUILD_STRING_BUFFER (op1);
14059 else
14061 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
14062 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
14066 if (op2)
14068 /* OP1 is no longer the last node holding a crafted StringBuffer */
14069 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
14070 /* Create a node for `{new...,xxx}.append (op2)' */
14071 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
14074 /* Mark the last node holding a crafted StringBuffer */
14075 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
14077 TREE_SIDE_EFFECTS (op1) = side_effects;
14078 return op1;
14081 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
14082 StringBuffer. If no string were found to be patched, return
14083 NULL. */
14085 static tree
14086 patch_string (tree node)
14088 if (node == error_mark_node)
14089 return error_mark_node;
14090 if (TREE_CODE (node) == STRING_CST)
14091 return patch_string_cst (node);
14092 else if (IS_CRAFTED_STRING_BUFFER_P (node))
14094 int saved = ctxp->explicit_constructor_p;
14095 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
14096 tree ret;
14097 /* Temporary disable forbid the use of `this'. */
14098 ctxp->explicit_constructor_p = 0;
14099 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
14100 /* String concatenation arguments must be evaluated in order too. */
14101 ret = force_evaluation_order (ret);
14102 /* Restore it at its previous value */
14103 ctxp->explicit_constructor_p = saved;
14104 return ret;
14106 return NULL_TREE;
14109 /* Build the internal representation of a string constant. */
14111 static tree
14112 patch_string_cst (tree node)
14114 int location;
14115 if (! flag_emit_class_files)
14117 node = get_identifier (TREE_STRING_POINTER (node));
14118 location = alloc_name_constant (CONSTANT_String, node);
14119 node = build_ref_from_constant_pool (location);
14121 TREE_CONSTANT (node) = 1;
14122 TREE_INVARIANT (node) = 1;
14124 /* ??? Guessing that the class file code can't handle casts. */
14125 if (! flag_emit_class_files)
14126 node = convert (string_ptr_type_node, node);
14127 else
14128 TREE_TYPE (node) = string_ptr_type_node;
14130 return node;
14133 /* Build an incomplete unary operator expression. */
14135 static tree
14136 build_unaryop (int op_token, int op_location, tree op1)
14138 enum tree_code op;
14139 tree unaryop;
14140 switch (op_token)
14142 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14143 case MINUS_TK: op = NEGATE_EXPR; break;
14144 case NEG_TK: op = TRUTH_NOT_EXPR; break;
14145 case NOT_TK: op = BIT_NOT_EXPR; break;
14146 default: abort ();
14149 unaryop = build1 (op, NULL_TREE, op1);
14150 TREE_SIDE_EFFECTS (unaryop) = 1;
14151 /* Store the location of the operator, for better error report. The
14152 string of the operator will be rebuild based on the OP value. */
14153 EXPR_WFL_LINECOL (unaryop) = op_location;
14154 return unaryop;
14157 /* Special case for the ++/-- operators, since they require an extra
14158 argument to build, which is set to NULL and patched
14159 later. IS_POST_P is 1 if the operator, 0 otherwise. */
14161 static tree
14162 build_incdec (int op_token, int op_location, tree op1, int is_post_p)
14164 static const enum tree_code lookup [2][2] =
14166 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14167 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14169 tree node = build2 (lookup [is_post_p][(op_token - DECR_TK)],
14170 NULL_TREE, op1, NULL_TREE);
14171 TREE_SIDE_EFFECTS (node) = 1;
14172 /* Store the location of the operator, for better error report. The
14173 string of the operator will be rebuild based on the OP value. */
14174 EXPR_WFL_LINECOL (node) = op_location;
14175 return node;
14178 /* Build an incomplete cast operator, based on the use of the
14179 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14180 set. java_complete_tree is trained to walk a CONVERT_EXPR even
14181 though its type is already set. */
14183 static tree
14184 build_cast (int location, tree type, tree exp)
14186 tree node = build1 (CONVERT_EXPR, type, exp);
14187 EXPR_WFL_LINECOL (node) = location;
14188 return node;
14191 /* Build an incomplete class reference operator. */
14192 static tree
14193 build_incomplete_class_ref (int location, tree class_name)
14195 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14196 tree class_decl = GET_CPC ();
14197 tree this_class = TREE_TYPE (class_decl);
14199 /* Generate the synthetic static method `class$'. (Previously we
14200 deferred this, causing different method tables to be emitted
14201 for native code and bytecode.) */
14202 if (!TYPE_DOT_CLASS (this_class)
14203 && !JPRIMITIVE_TYPE_P (class_name)
14204 && !(TREE_CODE (class_name) == VOID_TYPE))
14206 tree cpc_list = GET_CPC_LIST();
14207 tree cpc = cpc_list;
14208 tree target_class;
14210 /* For inner classes, add a 'class$' method to their outermost
14211 context, creating it if necessary. */
14213 while (GET_NEXT_ENCLOSING_CPC(cpc))
14214 cpc = GET_NEXT_ENCLOSING_CPC(cpc);
14215 class_decl = TREE_VALUE (cpc);
14217 target_class = TREE_TYPE (class_decl);
14219 if (CLASS_INTERFACE (TYPE_NAME (target_class)))
14221 /* For interfaces, adding a static 'class$' method directly
14222 is illegal. So create an inner class to contain the new
14223 method. Empirically this matches the behavior of javac. */
14224 tree t, inner;
14225 /* We want the generated inner class inside the outermost class. */
14226 GET_CPC_LIST() = cpc;
14227 t = build_wfl_node (DECL_NAME (TYPE_NAME (object_type_node)));
14228 inner = create_anonymous_class (t);
14229 target_class = TREE_TYPE (inner);
14230 end_class_declaration (1);
14231 GET_CPC_LIST() = cpc_list;
14234 if (TYPE_DOT_CLASS (target_class) == NULL_TREE)
14235 build_dot_class_method (target_class);
14237 if (this_class != target_class)
14238 TYPE_DOT_CLASS (this_class) = TYPE_DOT_CLASS (target_class);
14241 EXPR_WFL_LINECOL (node) = location;
14242 return node;
14245 /* Complete an incomplete class reference operator. */
14246 static tree
14247 patch_incomplete_class_ref (tree node)
14249 tree type = TREE_OPERAND (node, 0);
14250 tree ref_type;
14252 if (!(ref_type = resolve_type_during_patch (type)))
14253 return error_mark_node;
14255 /* If we're not emitting class files and we know ref_type is a
14256 compiled class, build a direct reference. */
14257 if ((! flag_emit_class_files && is_compiled_class (ref_type))
14258 || JPRIMITIVE_TYPE_P (ref_type)
14259 || TREE_CODE (ref_type) == VOID_TYPE)
14261 tree dot = build_class_ref (ref_type);
14262 /* A class referenced by `foo.class' is initialized. */
14263 if (!flag_emit_class_files)
14264 dot = build_class_init (ref_type, dot);
14265 return java_complete_tree (dot);
14268 /* If we're emitting class files and we have to deal with non
14269 primitive types, we invoke the synthetic static method `class$'. */
14270 ref_type = build_dot_class_method_invocation (current_class, ref_type);
14271 return java_complete_tree (ref_type);
14274 /* 15.14 Unary operators. We return error_mark_node in case of error,
14275 but preserve the type of NODE if the type is fixed. */
14277 static tree
14278 patch_unaryop (tree node, tree wfl_op)
14280 tree op = TREE_OPERAND (node, 0);
14281 tree op_type = TREE_TYPE (op);
14282 tree prom_type = NULL_TREE, value, decl;
14283 int nested_field_flag = 0;
14284 int code = TREE_CODE (node);
14285 int error_found = 0;
14287 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14289 switch (code)
14291 /* 15.13.2 Postfix Increment Operator ++ */
14292 case POSTINCREMENT_EXPR:
14293 /* 15.13.3 Postfix Increment Operator -- */
14294 case POSTDECREMENT_EXPR:
14295 /* 15.14.1 Prefix Increment Operator ++ */
14296 case PREINCREMENT_EXPR:
14297 /* 15.14.2 Prefix Decrement Operator -- */
14298 case PREDECREMENT_EXPR:
14299 op = decl = extract_field_decl (op);
14300 nested_field_flag
14301 = nested_field_expanded_access_p (op, NULL, NULL, NULL);
14302 /* We might be trying to change an outer field accessed using
14303 access method. */
14304 if (nested_field_flag)
14306 /* Retrieve the decl of the field we're trying to access. We
14307 do that by first retrieving the function we would call to
14308 access the field. It has been already verified that this
14309 field isn't final */
14310 if (flag_emit_class_files)
14311 decl = TREE_OPERAND (op, 0);
14312 else
14313 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14314 decl = DECL_FUNCTION_ACCESS_DECL (decl);
14316 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14317 else if (!JDECL_P (decl)
14318 && TREE_CODE (decl) != COMPONENT_REF
14319 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14320 && TREE_CODE (decl) != INDIRECT_REF
14321 && !(TREE_CODE (decl) == COMPOUND_EXPR
14322 && TREE_OPERAND (decl, 1)
14323 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14325 TREE_TYPE (node) = error_mark_node;
14326 error_found = 1;
14329 /* From now on, we know that op if a variable and that it has a
14330 valid wfl. We use wfl_op to locate errors related to the
14331 ++/-- operand. */
14332 if (!JNUMERIC_TYPE_P (op_type))
14334 parse_error_context
14335 (wfl_op, "Invalid argument type %qs to %qs",
14336 lang_printable_name (op_type, 0), operator_string (node));
14337 TREE_TYPE (node) = error_mark_node;
14338 error_found = 1;
14340 else
14342 /* Before the addition, binary numeric promotion is performed on
14343 both operands, if really necessary */
14344 if (JINTEGRAL_TYPE_P (op_type))
14346 value = build_int_cst (op_type, 1);
14347 TREE_TYPE (node) = op_type;
14349 else
14351 value = build_int_cst (NULL_TREE, 1);
14352 TREE_TYPE (node) =
14353 binary_numeric_promotion (op_type,
14354 TREE_TYPE (value), &op, &value);
14357 /* We remember we might be accessing an outer field */
14358 if (nested_field_flag)
14360 /* We re-generate an access to the field */
14361 value = build2 (PLUS_EXPR, TREE_TYPE (op),
14362 build_nested_field_access (wfl_op, decl), value);
14364 /* And we patch the original access$() into a write
14365 with plus_op as a rhs */
14366 return nested_field_access_fix (node, op, value);
14369 /* And write back into the node. */
14370 TREE_OPERAND (node, 0) = op;
14371 TREE_OPERAND (node, 1) = value;
14372 /* Convert the overall back into its original type, if
14373 necessary, and return */
14374 if (JINTEGRAL_TYPE_P (op_type))
14375 return fold (node);
14376 else
14377 return fold (convert (op_type, node));
14379 break;
14381 /* 15.14.3 Unary Plus Operator + */
14382 case UNARY_PLUS_EXPR:
14383 /* 15.14.4 Unary Minus Operator - */
14384 case NEGATE_EXPR:
14385 if (!JNUMERIC_TYPE_P (op_type))
14387 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14388 TREE_TYPE (node) = error_mark_node;
14389 error_found = 1;
14391 /* Unary numeric promotion is performed on operand */
14392 else
14394 op = do_unary_numeric_promotion (op);
14395 prom_type = TREE_TYPE (op);
14396 if (code == UNARY_PLUS_EXPR)
14397 return fold (op);
14399 break;
14401 /* 15.14.5 Bitwise Complement Operator ~ */
14402 case BIT_NOT_EXPR:
14403 if (!JINTEGRAL_TYPE_P (op_type))
14405 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14406 TREE_TYPE (node) = error_mark_node;
14407 error_found = 1;
14409 else
14411 op = do_unary_numeric_promotion (op);
14412 prom_type = TREE_TYPE (op);
14414 break;
14416 /* 15.14.6 Logical Complement Operator ! */
14417 case TRUTH_NOT_EXPR:
14418 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14420 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14421 /* But the type is known. We will report an error if further
14422 attempt of a assignment is made with this rhs */
14423 TREE_TYPE (node) = boolean_type_node;
14424 error_found = 1;
14426 else
14427 prom_type = boolean_type_node;
14428 break;
14430 /* 15.15 Cast Expression */
14431 case CONVERT_EXPR:
14432 value = patch_cast (node, wfl_operator);
14433 if (value == error_mark_node)
14435 /* If this cast is part of an assignment, we tell the code
14436 that deals with it not to complain about a mismatch,
14437 because things have been cast, anyways */
14438 TREE_TYPE (node) = error_mark_node;
14439 error_found = 1;
14441 else
14443 value = fold (value);
14444 return value;
14446 break;
14448 case NOP_EXPR:
14449 /* This can only happen when the type is already known. */
14450 gcc_assert (TREE_TYPE (node) != NULL_TREE);
14451 prom_type = TREE_TYPE (node);
14452 break;
14455 if (error_found)
14456 return error_mark_node;
14458 /* There are cases where node has been replaced by something else
14459 and we don't end up returning here: UNARY_PLUS_EXPR,
14460 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14461 TREE_OPERAND (node, 0) = fold (op);
14462 TREE_TYPE (node) = prom_type;
14463 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14464 return fold (node);
14467 /* Generic type resolution that sometimes takes place during node
14468 patching. Returned the resolved type or generate an error
14469 message. Return the resolved type or NULL_TREE. */
14471 static tree
14472 resolve_type_during_patch (tree type)
14474 if (unresolved_type_p (type, NULL))
14476 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14477 if (!type_decl)
14479 parse_error_context (type,
14480 "Class %qs not found in type declaration",
14481 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14482 return NULL_TREE;
14485 check_deprecation (type, type_decl);
14487 return TREE_TYPE (type_decl);
14489 return type;
14492 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14493 found. Otherwise NODE or something meant to replace it is returned. */
14495 static tree
14496 patch_cast (tree node, tree wfl_op)
14498 tree op = TREE_OPERAND (node, 0);
14499 tree cast_type = TREE_TYPE (node);
14500 tree patched, op_type;
14501 char *t1;
14503 /* Some string patching might be necessary at this stage */
14504 if ((patched = patch_string (op)))
14505 TREE_OPERAND (node, 0) = op = patched;
14506 op_type = TREE_TYPE (op);
14508 /* First resolve OP_TYPE if unresolved */
14509 if (!(cast_type = resolve_type_during_patch (cast_type)))
14510 return error_mark_node;
14512 /* Check on cast that are proven correct at compile time */
14513 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14515 /* Same type */
14516 if (cast_type == op_type)
14517 return node;
14519 /* A narrowing conversion from a floating-point number to an
14520 integral type requires special handling (5.1.3). */
14521 if (JFLOAT_TYPE_P (op_type) && JINTEGRAL_TYPE_P (cast_type))
14522 if (cast_type != long_type_node)
14523 op = convert (integer_type_node, op);
14525 /* Try widening/narrowing conversion. Potentially, things need
14526 to be worked out in gcc so we implement the extreme cases
14527 correctly. fold_convert() needs to be fixed. */
14528 return convert (cast_type, op);
14531 /* It's also valid to cast a boolean into a boolean */
14532 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14533 return node;
14535 /* null can be casted to references */
14536 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14537 return build_null_of_type (cast_type);
14539 /* The remaining legal casts involve conversion between reference
14540 types. Check for their compile time correctness. */
14541 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14542 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14544 TREE_TYPE (node) = promote_type (cast_type);
14545 /* Now, the case can be determined correct at compile time if
14546 OP_TYPE can be converted into CAST_TYPE by assignment
14547 conversion (5.2) */
14549 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14551 TREE_SET_CODE (node, NOP_EXPR);
14552 return node;
14555 if (flag_emit_class_files)
14557 TREE_SET_CODE (node, CONVERT_EXPR);
14558 return node;
14561 /* The cast requires a run-time check */
14562 return build3 (CALL_EXPR, promote_type (cast_type),
14563 build_address_of (soft_checkcast_node),
14564 tree_cons (NULL_TREE, build_class_ref (cast_type),
14565 build_tree_list (NULL_TREE, op)),
14566 NULL_TREE);
14569 /* Any other casts are proven incorrect at compile time */
14570 t1 = xstrdup (lang_printable_name (op_type, 0));
14571 parse_error_context (wfl_op, "Invalid cast from %qs to %qs",
14572 t1, lang_printable_name (cast_type, 0));
14573 free (t1);
14574 return error_mark_node;
14577 /* Build a null constant and give it the type TYPE. */
14579 static tree
14580 build_null_of_type (tree type)
14582 tree node = build_int_cst (promote_type (type), 0);
14583 return node;
14586 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14587 a list of indices. */
14588 static tree
14589 build_array_ref (int location, tree array, tree index)
14591 tree node = build4 (ARRAY_REF, NULL_TREE, array, index,
14592 NULL_TREE, NULL_TREE);
14593 EXPR_WFL_LINECOL (node) = location;
14594 return node;
14597 /* 15.12 Array Access Expression */
14599 static tree
14600 patch_array_ref (tree node)
14602 tree array = TREE_OPERAND (node, 0);
14603 tree array_type = TREE_TYPE (array);
14604 tree index = TREE_OPERAND (node, 1);
14605 tree index_type = TREE_TYPE (index);
14606 int error_found = 0;
14608 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14610 if (TREE_CODE (array_type) == POINTER_TYPE)
14611 array_type = TREE_TYPE (array_type);
14613 /* The array reference must be an array */
14614 if (!TYPE_ARRAY_P (array_type))
14616 parse_error_context
14617 (wfl_operator,
14618 "%<[]%> can only be applied to arrays. It can't be applied to %qs",
14619 lang_printable_name (array_type, 0));
14620 TREE_TYPE (node) = error_mark_node;
14621 error_found = 1;
14624 /* The array index undergoes unary numeric promotion. The promoted
14625 type must be int */
14626 index = do_unary_numeric_promotion (index);
14627 if (TREE_TYPE (index) != int_type_node)
14629 if (valid_cast_to_p (index_type, int_type_node))
14630 parse_error_context (wfl_operator,
14631 "Incompatible type for %<[]%>. Explicit cast needed to convert %qs to %<int%>",
14632 lang_printable_name (index_type, 0));
14633 else
14634 parse_error_context (wfl_operator,
14635 "Incompatible type for %<[]%>. Can't convert %qs to %<int%>",
14636 lang_printable_name (index_type, 0));
14637 TREE_TYPE (node) = error_mark_node;
14638 error_found = 1;
14641 if (error_found)
14642 return error_mark_node;
14644 array_type = TYPE_ARRAY_ELEMENT (array_type);
14646 if (flag_emit_class_files)
14648 TREE_OPERAND (node, 0) = array;
14649 TREE_OPERAND (node, 1) = index;
14651 else
14652 node = build_java_arrayaccess (array, array_type, index);
14653 TREE_TYPE (node) = array_type;
14654 return node;
14657 /* 15.9 Array Creation Expressions */
14659 static tree
14660 build_newarray_node (tree type, tree dims, int extra_dims)
14662 tree node = build3 (NEW_ARRAY_EXPR, NULL_TREE, type,
14663 nreverse (dims),
14664 build_int_cst (NULL_TREE, extra_dims));
14665 return node;
14668 static tree
14669 patch_newarray (tree node)
14671 tree type = TREE_OPERAND (node, 0);
14672 tree dims = TREE_OPERAND (node, 1);
14673 tree cdim, array_type;
14674 int error_found = 0;
14675 int ndims = 0;
14676 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14678 /* Dimension types are verified. It's better for the types to be
14679 verified in order. */
14680 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14682 int dim_error = 0;
14683 tree dim = TREE_VALUE (cdim);
14685 /* Dim might have been saved during its evaluation */
14686 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14688 /* The type of each specified dimension must be an integral type. */
14689 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14690 dim_error = 1;
14692 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14693 promoted type must be int. */
14694 else
14696 dim = do_unary_numeric_promotion (dim);
14697 if (TREE_TYPE (dim) != int_type_node)
14698 dim_error = 1;
14701 /* Report errors on types here */
14702 if (dim_error)
14704 parse_error_context
14705 (TREE_PURPOSE (cdim),
14706 "Incompatible type for dimension in array creation expression. %s convert %qs to %<int%>",
14707 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14708 "Explicit cast needed to" : "Can't"),
14709 lang_printable_name (TREE_TYPE (dim), 0));
14710 error_found = 1;
14713 TREE_PURPOSE (cdim) = NULL_TREE;
14716 /* Resolve array base type if unresolved */
14717 if (!(type = resolve_type_during_patch (type)))
14718 error_found = 1;
14720 if (error_found)
14722 /* We don't want further evaluation of this bogus array creation
14723 operation */
14724 TREE_TYPE (node) = error_mark_node;
14725 return error_mark_node;
14728 /* Set array_type to the actual (promoted) array type of the result. */
14729 if (TREE_CODE (type) == RECORD_TYPE)
14730 type = build_pointer_type (type);
14731 while (--xdims >= 0)
14733 type = promote_type (build_java_array_type (type, -1));
14735 dims = nreverse (dims);
14736 array_type = type;
14737 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14739 type = array_type;
14740 array_type
14741 = build_java_array_type (type,
14742 TREE_CODE (cdim) == INTEGER_CST
14743 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14744 : -1);
14745 array_type = promote_type (array_type);
14747 dims = nreverse (dims);
14749 /* The node is transformed into a function call. Things are done
14750 differently according to the number of dimensions. If the number
14751 of dimension is equal to 1, then the nature of the base type
14752 (primitive or not) matters. */
14753 if (ndims == 1)
14754 return build_new_array (type, TREE_VALUE (dims));
14756 /* Can't reuse what's already written in expr.c because it uses the
14757 JVM stack representation. Provide a build_multianewarray. FIXME */
14758 return build3 (CALL_EXPR, array_type,
14759 build_address_of (soft_multianewarray_node),
14760 tree_cons (NULL_TREE,
14761 build_class_ref (TREE_TYPE (array_type)),
14762 tree_cons (NULL_TREE,
14763 build_int_cst (NULL_TREE, ndims),
14764 dims)),
14765 NULL_TREE);
14768 /* 10.6 Array initializer. */
14770 /* Build a wfl for array element that don't have one, so we can
14771 pin-point errors. */
14773 static tree
14774 maybe_build_array_element_wfl (tree node)
14776 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14778 /* FIXME - old code used "prev_lc.line" and "elc.prev_col */
14779 return build_expr_wfl (NULL_TREE,
14780 #ifdef USE_MAPPED_LOCATION
14781 input_location
14782 #else
14783 ctxp->filename,
14784 ctxp->lexer->token_start.line,
14785 ctxp->lexer->token_start.col
14786 #endif
14789 else
14790 return NULL_TREE;
14793 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14794 identification of initialized arrays easier to detect during walk
14795 and expansion. */
14797 static tree
14798 build_new_array_init (int location, tree values)
14800 tree constructor = build_constructor_from_list (NULL_TREE,
14801 nreverse (values));
14802 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14803 EXPR_WFL_LINECOL (to_return) = location;
14804 return to_return;
14807 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14808 occurred. Otherwise return NODE after having set its type
14809 appropriately. */
14811 static tree
14812 patch_new_array_init (tree type, tree node)
14814 int error_seen = 0;
14815 tree element_type;
14816 unsigned HOST_WIDE_INT length;
14817 constructor_elt *current;
14818 int all_constant = 1;
14819 tree init = TREE_OPERAND (node, 0);
14821 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14823 parse_error_context (node,
14824 "Invalid array initializer for non-array type %qs",
14825 lang_printable_name (type, 1));
14826 return error_mark_node;
14828 type = TREE_TYPE (type);
14829 element_type = TYPE_ARRAY_ELEMENT (type);
14831 for (length = 0;
14832 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init),
14833 length, current);
14834 length++)
14836 tree elt = current->value;
14837 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14839 error_seen |= array_constructor_check_entry (element_type, current);
14840 elt = current->value;
14841 /* When compiling to native code, STRING_CST is converted to
14842 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14843 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14844 all_constant = 0;
14846 else
14848 current->value = patch_new_array_init (element_type, elt);
14849 current->index = NULL_TREE;
14850 all_constant = 0;
14852 if (elt && TREE_CODE (elt) == TREE_LIST
14853 && TREE_VALUE (elt) == error_mark_node)
14854 error_seen = 1;
14857 if (error_seen)
14858 return error_mark_node;
14860 /* Create a new type. We can't reuse the one we have here by
14861 patching its dimension because it originally is of dimension -1
14862 hence reused by gcc. This would prevent triangular arrays. */
14863 type = build_java_array_type (element_type, length);
14864 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14865 TREE_TYPE (node) = promote_type (type);
14866 TREE_CONSTANT (init) = all_constant;
14867 TREE_INVARIANT (init) = all_constant;
14868 TREE_CONSTANT (node) = all_constant;
14869 TREE_INVARIANT (node) = all_constant;
14870 return node;
14873 /* Verify that one entry of the initializer element list can be
14874 assigned to the array base type. Report 1 if an error occurred, 0
14875 otherwise. */
14877 static int
14878 array_constructor_check_entry (tree type, constructor_elt *entry)
14880 char *array_type_string = NULL; /* For error reports */
14881 tree value, type_value, new_value, wfl_value, patched;
14882 int error_seen = 0;
14884 new_value = NULL_TREE;
14885 wfl_value = entry->value;
14887 value = java_complete_tree (entry->value);
14888 /* patch_string return error_mark_node if arg is error_mark_node */
14889 if ((patched = patch_string (value)))
14890 value = patched;
14891 if (value == error_mark_node)
14892 return 1;
14894 type_value = TREE_TYPE (value);
14896 /* At anytime, try_builtin_assignconv can report a warning on
14897 constant overflow during narrowing. */
14898 SET_WFL_OPERATOR (wfl_operator, entry->index, wfl_value);
14899 new_value = try_builtin_assignconv (wfl_operator, type, value);
14900 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14901 type_value = promote_type (type);
14903 /* Check and report errors */
14904 if (!new_value)
14906 const char *const msg = (!valid_cast_to_p (type_value, type) ?
14907 "Can't" : "Explicit cast needed to");
14908 if (!array_type_string)
14909 array_type_string = xstrdup (lang_printable_name (type, 1));
14910 parse_error_context
14911 (wfl_operator, "Incompatible type for array. %s convert %qs to %qs",
14912 msg, lang_printable_name (type_value, 1), array_type_string);
14913 error_seen = 1;
14916 if (new_value)
14917 entry->value = new_value;
14919 if (array_type_string)
14920 free (array_type_string);
14922 entry->index = NULL_TREE;
14923 return error_seen;
14926 static tree
14927 build_this (int location)
14929 tree node = build_wfl_node (this_identifier_node);
14930 TREE_SET_CODE (node, THIS_EXPR);
14931 EXPR_WFL_LINECOL (node) = location;
14932 return node;
14935 /* 14.15 The return statement. It builds a modify expression that
14936 assigns the returned value to the RESULT_DECL that hold the value
14937 to be returned. */
14939 static tree
14940 build_return (int location, tree op)
14942 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14943 EXPR_WFL_LINECOL (node) = location;
14944 node = build_debugable_stmt (location, node);
14945 return node;
14948 static tree
14949 patch_return (tree node)
14951 tree return_exp = TREE_OPERAND (node, 0);
14952 tree meth = current_function_decl;
14953 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14954 int error_found = 0;
14956 TREE_TYPE (node) = error_mark_node;
14957 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14959 /* It's invalid to have a return value within a function that is
14960 declared with the keyword void or that is a constructor */
14961 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14962 error_found = 1;
14964 /* It's invalid to use a return statement in a static block */
14965 if (DECL_CLINIT_P (current_function_decl))
14966 error_found = 1;
14968 /* It's invalid to have a no return value within a function that
14969 isn't declared with the keyword `void' */
14970 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14971 error_found = 2;
14973 if (DECL_INSTINIT_P (current_function_decl))
14974 error_found = 1;
14976 if (error_found)
14978 if (DECL_INSTINIT_P (current_function_decl))
14979 parse_error_context (wfl_operator,
14980 "%<return%> inside instance initializer");
14982 else if (DECL_CLINIT_P (current_function_decl))
14983 parse_error_context (wfl_operator,
14984 "%<return%> inside static initializer");
14986 else if (!DECL_CONSTRUCTOR_P (meth))
14988 char *t = xstrdup (lang_printable_name (mtype, 0));
14989 parse_error_context (wfl_operator,
14990 "%<return%> with%s value from %<%s %s%>",
14991 (error_found == 1 ? "" : "out"),
14992 t, lang_printable_name (meth, 2));
14993 free (t);
14995 else
14996 parse_error_context (wfl_operator,
14997 "%<return%> with value from constructor %qs",
14998 lang_printable_name (meth, 2));
14999 return error_mark_node;
15002 /* If we have a return_exp, build a modify expression and expand
15003 it. Note: at that point, the assignment is declared valid, but we
15004 may want to carry some more hacks */
15005 if (return_exp)
15007 tree exp = java_complete_tree (return_exp);
15008 tree modify, patched;
15010 if ((patched = patch_string (exp)))
15011 exp = patched;
15013 modify = build2 (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
15014 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
15015 modify = java_complete_tree (modify);
15017 if (modify != error_mark_node)
15019 TREE_SIDE_EFFECTS (modify) = 1;
15020 TREE_OPERAND (node, 0) = modify;
15022 else
15023 return error_mark_node;
15025 TREE_TYPE (node) = void_type_node;
15026 TREE_SIDE_EFFECTS (node) = 1;
15027 return node;
15030 /* 14.8 The if Statement */
15032 static tree
15033 build_if_else_statement (int location, tree expression, tree if_body,
15034 tree else_body)
15036 tree node;
15037 if (!else_body)
15038 else_body = build_java_empty_stmt ();
15039 node = build3 (COND_EXPR, NULL_TREE, expression, if_body, else_body);
15040 EXPR_WFL_LINECOL (node) = location;
15041 node = build_debugable_stmt (location, node);
15042 return node;
15045 static tree
15046 patch_if_else_statement (tree node)
15048 tree expression = TREE_OPERAND (node, 0);
15049 int can_complete_normally
15050 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15051 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
15053 TREE_TYPE (node) = error_mark_node;
15054 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15056 /* The type of expression must be boolean */
15057 if (TREE_TYPE (expression) != boolean_type_node
15058 && TREE_TYPE (expression) != promoted_boolean_type_node)
15060 parse_error_context
15061 (wfl_operator,
15062 "Incompatible type for %<if%>. Can't convert %qs to %<boolean%>",
15063 lang_printable_name (TREE_TYPE (expression), 0));
15064 return error_mark_node;
15067 TREE_TYPE (node) = void_type_node;
15068 TREE_SIDE_EFFECTS (node) = 1;
15069 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
15070 return node;
15073 /* 14.6 Labeled Statements */
15075 /* Action taken when a labeled statement is parsed. a new
15076 LABELED_BLOCK_EXPR is created. No statement is attached to the
15077 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
15079 static tree
15080 build_labeled_block (int location, tree label)
15082 tree label_name ;
15083 tree label_decl, node;
15084 if (label == NULL_TREE || label == continue_identifier_node)
15085 label_name = label;
15086 else
15088 label_name = merge_qualified_name (label_id, label);
15089 /* Issue an error if we try to reuse a label that was previously
15090 declared */
15091 if (IDENTIFIER_LOCAL_VALUE (label_name))
15093 EXPR_WFL_LINECOL (wfl_operator) = location;
15094 parse_error_context (wfl_operator,
15095 "Declaration of %qs shadows a previous label declaration",
15096 IDENTIFIER_POINTER (label));
15097 EXPR_WFL_LINECOL (wfl_operator) =
15098 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
15099 parse_error_context (wfl_operator,
15100 "This is the location of the previous declaration of label %qs",
15101 IDENTIFIER_POINTER (label));
15102 java_error_count--;
15106 label_decl = create_label_decl (label_name);
15107 node = build2 (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
15108 EXPR_WFL_LINECOL (node) = location;
15109 TREE_SIDE_EFFECTS (node) = 1;
15110 return node;
15113 /* A labeled statement LBE is attached a statement. */
15115 static tree
15116 finish_labeled_statement (tree lbe, /* Labeled block expr */
15117 tree statement)
15119 /* In anyways, tie the loop to its statement */
15120 LABELED_BLOCK_BODY (lbe) = statement;
15121 pop_labeled_block ();
15122 POP_LABELED_BLOCK ();
15123 return lbe;
15126 /* 14.10, 14.11, 14.12 Loop Statements */
15128 /* Create an empty LOOP_EXPR and make it the last in the nested loop
15129 list. */
15131 static tree
15132 build_new_loop (tree loop_body)
15134 tree loop = build1 (LOOP_EXPR, NULL_TREE, loop_body);
15135 TREE_SIDE_EFFECTS (loop) = 1;
15136 PUSH_LOOP (loop);
15137 return loop;
15140 /* Create a loop body according to the following structure:
15141 COMPOUND_EXPR
15142 COMPOUND_EXPR (loop main body)
15143 EXIT_EXPR (this order is for while/for loops.
15144 LABELED_BLOCK_EXPR the order is reversed for do loops)
15145 LABEL_DECL (a continue occurring here branches at the
15146 BODY end of this labeled block)
15147 INCREMENT (if any)
15149 REVERSED, if nonzero, tells that the loop condition expr comes
15150 after the body, like in the do-while loop.
15152 To obtain a loop, the loop body structure described above is
15153 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15155 LABELED_BLOCK_EXPR
15156 LABEL_DECL (use this label to exit the loop)
15157 LOOP_EXPR
15158 <structure described above> */
15160 static tree
15161 build_loop_body (int location, tree condition, int reversed)
15163 tree first, second, body;
15165 condition = build1 (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15166 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15167 condition = build_debugable_stmt (location, condition);
15168 TREE_SIDE_EFFECTS (condition) = 1;
15170 body = build_labeled_block (0, continue_identifier_node);
15171 first = (reversed ? body : condition);
15172 second = (reversed ? condition : body);
15173 return build2 (COMPOUND_EXPR, NULL_TREE,
15174 build2 (COMPOUND_EXPR, NULL_TREE, first, second),
15175 build_java_empty_stmt ());
15178 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15179 their order) on the current loop. Unlink the current loop from the
15180 loop list. */
15182 static tree
15183 finish_loop_body (int location, tree condition, tree body, int reversed)
15185 tree to_return = ctxp->current_loop;
15186 tree loop_body = LOOP_EXPR_BODY (to_return);
15187 if (condition)
15189 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15190 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15191 The real EXIT_EXPR is one operand further. */
15192 EXPR_WFL_LINECOL (cnode) = location;
15193 if (TREE_CODE (cnode) == EXPR_WITH_FILE_LOCATION)
15195 cnode = EXPR_WFL_NODE (cnode);
15196 /* This one is for accurate error reports */
15197 EXPR_WFL_LINECOL (cnode) = location;
15199 TREE_OPERAND (cnode, 0) = condition;
15201 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15202 POP_LOOP ();
15203 return to_return;
15206 /* Tailored version of finish_loop_body for FOR loops, when FOR
15207 loops feature the condition part */
15209 static tree
15210 finish_for_loop (int location, tree condition, tree update, tree body)
15212 /* Put the condition and the loop body in place */
15213 tree loop = finish_loop_body (location, condition, body, 0);
15214 /* LOOP is the current loop which has been now popped of the loop
15215 stack. Mark the update block as reachable and install it. We do
15216 this because the (current interpretation of the) JLS requires
15217 that the update expression be considered reachable even if the
15218 for loop's body doesn't complete normally. */
15219 if (update != NULL_TREE && !IS_EMPTY_STMT (update))
15221 tree up2 = update;
15222 if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15223 up2 = EXPR_WFL_NODE (up2);
15224 /* It is possible for the update expression to be an
15225 EXPR_WFL_NODE wrapping nothing. */
15226 if (up2 != NULL_TREE && !IS_EMPTY_STMT (up2))
15228 /* Try to detect constraint violations. These would be
15229 programming errors somewhere. */
15230 if (! EXPR_P (up2) || TREE_CODE (up2) == LOOP_EXPR)
15231 abort ();
15232 SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15235 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15236 return loop;
15239 /* Try to find the loop a block might be related to. This comprises
15240 the case where the LOOP_EXPR is found as the second operand of a
15241 COMPOUND_EXPR, because the loop happens to have an initialization
15242 part, then expressed as the first operand of the COMPOUND_EXPR. If
15243 the search finds something, 1 is returned. Otherwise, 0 is
15244 returned. The search is assumed to start from a
15245 LABELED_BLOCK_EXPR's block. */
15247 static tree
15248 search_loop (tree statement)
15250 if (TREE_CODE (statement) == LOOP_EXPR)
15251 return statement;
15253 if (TREE_CODE (statement) == BLOCK)
15254 statement = BLOCK_SUBBLOCKS (statement);
15255 else
15256 return NULL_TREE;
15258 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15259 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15260 statement = TREE_OPERAND (statement, 1);
15262 return (TREE_CODE (statement) == LOOP_EXPR
15263 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15266 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15267 returned otherwise. */
15269 static int
15270 labeled_block_contains_loop_p (tree block, tree loop)
15272 if (!block)
15273 return 0;
15275 if (LABELED_BLOCK_BODY (block) == loop)
15276 return 1;
15278 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15279 return 1;
15281 return 0;
15284 /* If the loop isn't surrounded by a labeled statement, create one and
15285 insert LOOP as its body. */
15287 static tree
15288 patch_loop_statement (tree loop)
15290 tree loop_label;
15292 TREE_TYPE (loop) = void_type_node;
15293 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15294 return loop;
15296 loop_label = build_labeled_block (0, NULL_TREE);
15297 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15298 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15299 LABELED_BLOCK_BODY (loop_label) = loop;
15300 PUSH_LABELED_BLOCK (loop_label);
15301 return loop_label;
15304 /* 14.13, 14.14: break and continue Statements */
15306 /* Build a break or a continue statement. a null NAME indicates an
15307 unlabeled break/continue statement. */
15309 static tree
15310 build_bc_statement (int location, int is_break, tree name)
15312 tree break_continue, label_block_expr = NULL_TREE;
15314 if (name)
15316 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15317 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15318 /* Null means that we don't have a target for this named
15319 break/continue. In this case, we make the target to be the
15320 label name, so that the error can be reported accurately in
15321 patch_bc_statement. */
15322 label_block_expr = EXPR_WFL_NODE (name);
15324 /* Unlabeled break/continue will be handled during the
15325 break/continue patch operation */
15326 break_continue = build1 (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr);
15328 IS_BREAK_STMT_P (break_continue) = is_break;
15329 TREE_SIDE_EFFECTS (break_continue) = 1;
15330 EXPR_WFL_LINECOL (break_continue) = location;
15331 break_continue = build_debugable_stmt (location, break_continue);
15332 return break_continue;
15335 /* Verification of a break/continue statement. */
15337 static tree
15338 patch_bc_statement (tree node)
15340 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15341 tree labeled_block = ctxp->current_labeled_block;
15342 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15344 /* Having an identifier here means that the target is unknown. */
15345 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15347 parse_error_context (wfl_operator, "No label definition found for %qs",
15348 IDENTIFIER_POINTER (bc_label));
15349 return error_mark_node;
15351 if (! IS_BREAK_STMT_P (node))
15353 /* It's a continue statement. */
15354 for (;; labeled_block = TREE_CHAIN (labeled_block))
15356 if (labeled_block == NULL_TREE)
15358 if (bc_label == NULL_TREE)
15359 parse_error_context (wfl_operator,
15360 "%<continue%> must be in loop");
15361 else
15362 parse_error_context
15363 (wfl_operator, "continue label %qs does not name a loop",
15364 IDENTIFIER_POINTER (bc_label));
15365 return error_mark_node;
15367 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15368 == continue_identifier_node)
15369 && (bc_label == NULL_TREE
15370 || TREE_CHAIN (labeled_block) == bc_label))
15372 bc_label = labeled_block;
15373 break;
15377 else if (!bc_label)
15379 for (;; labeled_block = TREE_CHAIN (labeled_block))
15381 if (labeled_block == NULL_TREE)
15383 parse_error_context (wfl_operator,
15384 "%<break%> must be in loop or switch");
15385 return error_mark_node;
15387 target_stmt = LABELED_BLOCK_BODY (labeled_block);
15388 if (TREE_CODE (target_stmt) == SWITCH_EXPR
15389 || search_loop (target_stmt))
15391 bc_label = labeled_block;
15392 break;
15397 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15398 CAN_COMPLETE_NORMALLY (bc_label) = 1;
15400 /* Our break/continue don't return values. */
15401 TREE_TYPE (node) = void_type_node;
15402 /* Encapsulate the break within a compound statement so that it's
15403 expanded all the times by expand_expr (and not clobbered
15404 sometimes, like after a if statement) */
15405 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15406 TREE_SIDE_EFFECTS (node) = 1;
15407 return node;
15410 /* Process the exit expression belonging to a loop. Its type must be
15411 boolean. */
15413 static tree
15414 patch_exit_expr (tree node)
15416 tree expression = TREE_OPERAND (node, 0);
15417 TREE_TYPE (node) = error_mark_node;
15418 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15420 /* The type of expression must be boolean */
15421 if (TREE_TYPE (expression) != boolean_type_node)
15423 parse_error_context
15424 (wfl_operator,
15425 "Incompatible type for loop conditional. Can't convert %qs to %<boolean%>",
15426 lang_printable_name (TREE_TYPE (expression), 0));
15427 return error_mark_node;
15429 /* Now we know things are allright, invert the condition, fold and
15430 return */
15431 TREE_OPERAND (node, 0) =
15432 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15434 if (! integer_zerop (TREE_OPERAND (node, 0))
15435 && ctxp->current_loop != NULL_TREE
15436 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15437 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15438 if (! integer_onep (TREE_OPERAND (node, 0)))
15439 CAN_COMPLETE_NORMALLY (node) = 1;
15442 TREE_TYPE (node) = void_type_node;
15443 return node;
15446 /* 14.9 Switch statement */
15448 static tree
15449 patch_switch_statement (tree node)
15451 tree se = TREE_OPERAND (node, 0), se_type;
15452 tree save, iter;
15454 /* Complete the switch expression */
15455 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15456 se_type = TREE_TYPE (se);
15457 /* The type of the switch expression must be char, byte, short or
15458 int */
15459 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15461 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15462 parse_error_context (wfl_operator,
15463 "Incompatible type for %<switch%>. Can't convert %qs to %<int%>",
15464 lang_printable_name (se_type, 0));
15465 /* This is what java_complete_tree will check */
15466 TREE_OPERAND (node, 0) = error_mark_node;
15467 return error_mark_node;
15470 /* Save and restore the outer case label list. */
15471 save = case_label_list;
15472 case_label_list = NULL_TREE;
15474 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15476 /* See if we've found a duplicate label. We can't leave this until
15477 code generation, because in `--syntax-only' and `-C' modes we
15478 don't do ordinary code generation. */
15479 for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15481 HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15482 tree subiter;
15483 for (subiter = TREE_CHAIN (iter);
15484 subiter != NULL_TREE;
15485 subiter = TREE_CHAIN (subiter))
15487 HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15488 if (val == subval)
15490 EXPR_WFL_LINECOL (wfl_operator)
15491 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15492 /* The case_label_list is in reverse order, so print the
15493 outer label first. */
15494 parse_error_context (wfl_operator, "duplicate case label: %<"
15495 HOST_WIDE_INT_PRINT_DEC "%>", subval);
15496 EXPR_WFL_LINECOL (wfl_operator)
15497 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15498 parse_error_context (wfl_operator, "original label is here");
15500 break;
15505 case_label_list = save;
15507 /* Ready to return */
15508 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15510 TREE_TYPE (node) = error_mark_node;
15511 return error_mark_node;
15513 TREE_TYPE (node) = void_type_node;
15514 TREE_SIDE_EFFECTS (node) = 1;
15515 CAN_COMPLETE_NORMALLY (node)
15516 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15517 || ! SWITCH_HAS_DEFAULT (node);
15518 return node;
15521 /* Assertions. */
15523 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15524 might be NULL_TREE. */
15525 static tree
15526 build_assertion (
15527 #ifdef USE_MAPPED_LOCATION
15528 source_location location,
15529 #else
15530 int location,
15531 #endif
15532 tree condition, tree value)
15534 tree node;
15535 tree klass = GET_CPC ();
15537 if (! enable_assertions (klass))
15539 condition = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15540 boolean_false_node, condition);
15541 if (value == NULL_TREE)
15542 value = build_java_empty_stmt ();
15543 return build_if_else_statement (location, condition,
15544 value, NULL_TREE);
15547 if (! CLASS_USES_ASSERTIONS (klass))
15549 tree field, classdollar, id, call;
15550 tree class_type = TREE_TYPE (klass);
15552 field = add_field (class_type,
15553 get_identifier ("$assertionsDisabled"),
15554 boolean_type_node,
15555 ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15556 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15557 FIELD_SYNTHETIC (field) = 1;
15559 classdollar = build_incomplete_class_ref (location, class_type);
15561 /* Call CLASS.desiredAssertionStatus(). */
15562 id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15563 call = build3 (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15564 call = make_qualified_primary (classdollar, call, location);
15565 TREE_SIDE_EFFECTS (call) = 1;
15567 /* Invert to obtain !CLASS.desiredAssertionStatus(). This may
15568 seem odd, but we do it to generate code identical to that of
15569 the JDK. */
15570 call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15571 TREE_SIDE_EFFECTS (call) = 1;
15572 DECL_INITIAL (field) = call;
15574 /* Record the initializer in the initializer statement list. */
15575 call = build2 (MODIFY_EXPR, NULL_TREE, field, call);
15576 TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15577 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15578 MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15580 CLASS_USES_ASSERTIONS (klass) = 1;
15583 if (value != NULL_TREE)
15584 value = tree_cons (NULL_TREE, value, NULL_TREE);
15586 node = build_wfl_node (get_identifier ("java"));
15587 node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15588 location);
15589 node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15590 location);
15592 node = build3 (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15593 TREE_SIDE_EFFECTS (node) = 1;
15594 /* It is too early to use BUILD_THROW. */
15595 node = build1 (THROW_EXPR, NULL_TREE, node);
15596 TREE_SIDE_EFFECTS (node) = 1;
15598 /* We invert the condition; if we just put NODE as the `else' part
15599 then we generate weird-looking bytecode. */
15600 condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15601 /* Check $assertionsDisabled. */
15602 condition
15603 = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15604 build1 (TRUTH_NOT_EXPR, NULL_TREE,
15605 build_wfl_node (get_identifier ("$assertionsDisabled"))),
15606 condition);
15607 node = build_if_else_statement (location, condition, node, NULL_TREE);
15608 return node;
15611 /* 14.18 The try/catch statements */
15613 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15614 catches TYPE and executes CATCH_STMTS. */
15616 static tree
15617 encapsulate_with_try_catch (int location, tree type_or_name, tree try_stmts,
15618 tree catch_stmts)
15620 tree try_block, catch_clause_param, catch_block, catch;
15622 /* First build a try block */
15623 try_block = build_expr_block (try_stmts, NULL_TREE);
15625 /* Build a catch block: we need a catch clause parameter */
15626 if (TREE_CODE (type_or_name) == EXPR_WITH_FILE_LOCATION)
15628 tree catch_type = obtain_incomplete_type (type_or_name);
15629 jdep *dep;
15630 catch_clause_param = build_decl (VAR_DECL, wpv_id, catch_type);
15631 register_incomplete_type (JDEP_VARIABLE, type_or_name,
15632 catch_clause_param, catch_type);
15633 dep = CLASSD_LAST (ctxp->classd_list);
15634 JDEP_GET_PATCH (dep) = &TREE_TYPE (catch_clause_param);
15636 else
15637 catch_clause_param = build_decl (VAR_DECL, wpv_id,
15638 build_pointer_type (type_or_name));
15640 /* And a block */
15641 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15643 /* Initialize the variable and store in the block */
15644 catch = build2 (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15645 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15646 add_stmt_to_block (catch_block, NULL_TREE, catch);
15648 /* Add the catch statements */
15649 add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15651 /* Now we can build a JAVA_CATCH_EXPR */
15652 catch_block = build1 (JAVA_CATCH_EXPR, NULL_TREE, catch_block);
15654 return build_try_statement (location, try_block, catch_block);
15657 static tree
15658 build_try_statement (int location, tree try_block, tree catches)
15660 tree node = build2 (TRY_EXPR, NULL_TREE, try_block, catches);
15661 EXPR_WFL_LINECOL (node) = location;
15662 return node;
15665 static tree
15666 build_try_finally_statement (int location, tree try_block, tree finally)
15668 tree node = build2 (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15669 EXPR_WFL_LINECOL (node) = location;
15670 return node;
15673 static tree
15674 patch_try_statement (tree node)
15676 int error_found = 0;
15677 tree try = TREE_OPERAND (node, 0);
15678 /* Exception handlers are considered in left to right order */
15679 tree catch = nreverse (TREE_OPERAND (node, 1));
15680 tree current, caught_type_list = NULL_TREE;
15682 /* Check catch clauses, if any. Every time we find an error, we try
15683 to process the next catch clause. We process the catch clause before
15684 the try block so that when processing the try block we can check thrown
15685 exceptions against the caught type list. */
15686 for (current = catch; current; current = TREE_CHAIN (current))
15688 tree carg_decl, carg_type;
15689 tree sub_current, catch_block, catch_clause;
15690 int unreachable;
15692 /* At this point, the structure of the catch clause is
15693 JAVA_CATCH_EXPR (catch node)
15694 BLOCK (with the decl of the parameter)
15695 COMPOUND_EXPR
15696 MODIFY_EXPR (assignment of the catch parameter)
15697 BLOCK (catch clause block)
15699 catch_clause = TREE_OPERAND (current, 0);
15700 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15701 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15703 /* Catch clauses can't have more than one parameter declared,
15704 but it's already enforced by the grammar. Make sure that the
15705 only parameter of the clause statement in of class Throwable
15706 or a subclass of Throwable, but that was done earlier. The
15707 catch clause parameter type has also been resolved. */
15709 /* Just make sure that the catch clause parameter type inherits
15710 from java.lang.Throwable */
15711 if (!inherits_from_p (carg_type, throwable_type_node))
15713 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15714 parse_error_context (wfl_operator,
15715 "Can't catch class %qs. Catch clause parameter type must be a subclass of class %<java.lang.Throwable%>",
15716 lang_printable_name (carg_type, 0));
15717 error_found = 1;
15718 continue;
15721 /* Partial check for unreachable catch statement: The catch
15722 clause is reachable iff is no earlier catch block A in
15723 the try statement such that the type of the catch
15724 clause's parameter is the same as or a subclass of the
15725 type of A's parameter */
15726 unreachable = 0;
15727 for (sub_current = catch;
15728 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15730 tree sub_catch_clause, decl;
15731 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15732 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15734 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15736 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15737 parse_error_context
15738 (wfl_operator,
15739 "%<catch%> not reached because of the catch clause at line %d",
15740 EXPR_WFL_LINENO (sub_current));
15741 unreachable = error_found = 1;
15742 break;
15745 /* Complete the catch clause block */
15746 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15747 if (catch_block == error_mark_node)
15749 error_found = 1;
15750 continue;
15752 if (CAN_COMPLETE_NORMALLY (catch_block))
15753 CAN_COMPLETE_NORMALLY (node) = 1;
15754 TREE_OPERAND (current, 0) = catch_block;
15756 if (unreachable)
15757 continue;
15759 /* Things to do here: the exception must be thrown */
15761 /* Link this type to the caught type list */
15762 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15765 PUSH_EXCEPTIONS (caught_type_list);
15766 if ((try = java_complete_tree (try)) == error_mark_node)
15767 error_found = 1;
15768 if (CAN_COMPLETE_NORMALLY (try))
15769 CAN_COMPLETE_NORMALLY (node) = 1;
15770 POP_EXCEPTIONS ();
15772 /* Verification ends here */
15773 if (error_found)
15774 return error_mark_node;
15776 TREE_OPERAND (node, 0) = try;
15777 TREE_OPERAND (node, 1) = catch;
15778 TREE_TYPE (node) = void_type_node;
15779 return node;
15782 /* 14.17 The synchronized Statement */
15784 static tree
15785 patch_synchronized_statement (tree node, tree wfl_op1)
15787 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15788 tree block = TREE_OPERAND (node, 1);
15790 tree tmp, enter, exit, expr_decl, assignment;
15792 if (expr == error_mark_node)
15794 block = java_complete_tree (block);
15795 return expr;
15798 /* We might be trying to synchronize on a STRING_CST */
15799 if ((tmp = patch_string (expr)))
15800 expr = tmp;
15802 /* The TYPE of expr must be a reference type */
15803 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15805 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15806 parse_error_context (wfl_operator, "Incompatible type for %<synchronized%>. Can't convert %qs to %<java.lang.Object%>",
15807 lang_printable_name (TREE_TYPE (expr), 0));
15808 return error_mark_node;
15811 /* Generate a try-finally for the synchronized statement, except
15812 that the handler that catches all throw exception calls
15813 _Jv_MonitorExit and then rethrow the exception.
15814 The synchronized statement is then implemented as:
15817 _Jv_MonitorEnter (expression)
15818 synchronized_block
15819 _Jv_MonitorExit (expression)
15821 CATCH_ALL
15823 e = _Jv_exception_info ();
15824 _Jv_MonitorExit (expression)
15825 Throw (e);
15826 } */
15828 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15829 BUILD_MONITOR_ENTER (enter, expr_decl);
15830 BUILD_MONITOR_EXIT (exit, expr_decl);
15831 CAN_COMPLETE_NORMALLY (enter) = 1;
15832 CAN_COMPLETE_NORMALLY (exit) = 1;
15833 assignment = build2 (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15834 TREE_SIDE_EFFECTS (assignment) = 1;
15835 node = build2 (COMPOUND_EXPR, NULL_TREE,
15836 build2 (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15837 build2 (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15838 node = build_expr_block (node, expr_decl);
15840 return java_complete_tree (node);
15843 /* 14.16 The throw Statement */
15845 static tree
15846 patch_throw_statement (tree node, tree wfl_op1)
15848 tree expr = TREE_OPERAND (node, 0);
15849 tree type = TREE_TYPE (expr);
15850 int unchecked_ok = 0, tryblock_throws_ok = 0;
15852 /* Thrown expression must be assignable to java.lang.Throwable */
15853 if (!try_reference_assignconv (throwable_type_node, expr))
15855 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15856 parse_error_context (wfl_operator,
15857 "Can't throw %qs; it must be a subclass of class %<java.lang.Throwable%>",
15858 lang_printable_name (type, 0));
15859 /* If the thrown expression was a reference, we further the
15860 compile-time check. */
15861 if (!JREFERENCE_TYPE_P (type))
15862 return error_mark_node;
15865 /* At least one of the following must be true */
15867 /* The type of the throw expression is a not checked exception,
15868 i.e. is a unchecked expression. */
15869 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15871 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15872 /* An instance can't throw a checked exception unless that exception
15873 is explicitly declared in the `throws' clause of each
15874 constructor. This doesn't apply to anonymous classes, since they
15875 don't have declared constructors. */
15876 if (!unchecked_ok
15877 && DECL_INSTINIT_P (current_function_decl)
15878 && !ANONYMOUS_CLASS_P (current_class))
15880 tree current;
15881 for (current = TYPE_METHODS (current_class); current;
15882 current = TREE_CHAIN (current))
15883 if (DECL_CONSTRUCTOR_P (current)
15884 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15886 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)",
15887 lang_printable_name (TREE_TYPE (expr), 0));
15888 return error_mark_node;
15892 /* Throw is contained in a try statement and at least one catch
15893 clause can receive the thrown expression or the current method is
15894 declared to throw such an exception. Or, the throw statement is
15895 contained in a method or constructor declaration and the type of
15896 the Expression is assignable to at least one type listed in the
15897 throws clause the declaration. */
15898 if (!unchecked_ok)
15899 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15900 if (!(unchecked_ok || tryblock_throws_ok))
15902 /* If there is a surrounding try block that has no matching
15903 clatch clause, report it first. A surrounding try block exits
15904 only if there is something after the list of checked
15905 exception thrown by the current function (if any). */
15906 if (IN_TRY_BLOCK_P ())
15907 parse_error_context (wfl_operator, "Checked exception %qs can't be caught by any of the catch clause(s) of the surrounding %<try%> block",
15908 lang_printable_name (type, 0));
15909 /* If we have no surrounding try statement and the method doesn't have
15910 any throws, report it now. FIXME */
15912 /* We report that the exception can't be throw from a try block
15913 in all circumstances but when the `throw' is inside a static
15914 block. */
15915 else if (!EXCEPTIONS_P (currently_caught_type_list)
15916 && !tryblock_throws_ok)
15918 if (DECL_CLINIT_P (current_function_decl))
15919 parse_error_context (wfl_operator,
15920 "Checked exception %qs can't be thrown in initializer",
15921 lang_printable_name (type, 0));
15922 else
15923 parse_error_context (wfl_operator,
15924 "Checked exception %qs isn't thrown from a %<try%> block",
15925 lang_printable_name (type, 0));
15927 /* Otherwise, the current method doesn't have the appropriate
15928 throws declaration */
15929 else
15930 parse_error_context (wfl_operator, "Checked exception %qs doesn't match any of current method's %<throws%> declaration(s)",
15931 lang_printable_name (type, 0));
15932 return error_mark_node;
15935 if (! flag_emit_class_files)
15936 BUILD_THROW (node, expr);
15938 return node;
15941 /* Add EXCEPTION to the throws clause of MDECL. If MDECL already throws
15942 a super-class of EXCEPTION, keep the superclass instead. If MDECL already
15943 throws a sub-class of EXCEPTION, replace the sub-class with EXCEPTION. */
15944 static void
15945 add_exception_to_throws (tree mdecl, tree exception)
15947 tree mthrows;
15949 /* Ignore unchecked exceptions. */
15950 if (IS_UNCHECKED_EXCEPTION_P (exception))
15951 return;
15953 for (mthrows = DECL_FUNCTION_THROWS (mdecl);
15954 mthrows; mthrows = TREE_CHAIN (mthrows))
15956 if (inherits_from_p (exception, TREE_VALUE (mthrows)))
15957 return;
15958 if (inherits_from_p (TREE_VALUE (mthrows), exception))
15960 TREE_VALUE (mthrows) = exception;
15961 return;
15965 mthrows = DECL_FUNCTION_THROWS (mdecl);
15966 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (mthrows, exception);
15969 /* Check that exception said to be thrown by method DECL can be
15970 effectively caught from where DECL is invoked. THIS_EXPR is the
15971 expression that computes `this' for the method call. */
15972 static void
15973 check_thrown_exceptions (
15974 #ifdef USE_MAPPED_LOCATION
15975 source_location location,
15976 #else
15978 int location,
15979 #endif
15980 tree decl, tree this_expr)
15982 tree throws;
15983 int is_array_call = 0;
15985 /* Skip check within generated methods, such as access$<n>. */
15986 if (NESTED_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (current_function_decl)))
15987 return;
15989 if (this_expr != NULL_TREE
15990 && TREE_CODE (TREE_TYPE (this_expr)) == POINTER_TYPE
15991 && TYPE_ARRAY_P (TREE_TYPE (TREE_TYPE (this_expr))))
15992 is_array_call = 1;
15994 /* For all the unchecked exceptions thrown by DECL. */
15995 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15996 throws = TREE_CHAIN (throws))
15997 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15999 /* Suppress errors about cloning arrays. */
16000 if (is_array_call && DECL_NAME (decl) == get_identifier ("clone"))
16001 continue;
16003 #ifdef USE_MAPPED_LOCATION
16004 SET_EXPR_LOCATION (wfl_operator, location);
16005 #else
16006 EXPR_WFL_LINECOL (wfl_operator) = location;
16007 #endif
16008 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (current_function_decl))
16009 && (DECL_FINIT_P (current_function_decl)
16010 || DECL_INIT_P (current_function_decl)
16011 || DECL_CONSTRUCTOR_P (current_function_decl)))
16013 /* Add "throws" to the initializer's exception list */
16014 tree exception = TREE_VALUE (throws);
16015 add_exception_to_throws (current_function_decl, exception);
16017 else if (DECL_FINIT_P (current_function_decl))
16019 parse_error_context
16020 (wfl_operator, "Exception %qs can't be thrown in initializer",
16021 lang_printable_name (TREE_VALUE (throws), 0));
16023 else
16025 parse_error_context
16026 (wfl_operator, "Exception %qs must be caught, or it must be declared in the %<throws%> clause of %qs",
16027 lang_printable_name (TREE_VALUE (throws), 0),
16028 (DECL_INIT_P (current_function_decl) ?
16029 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
16030 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
16035 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
16036 try-catch blocks, OR is listed in the `throws' clause of the
16037 current method. */
16039 static int
16040 check_thrown_exceptions_do (tree exception)
16042 tree list = currently_caught_type_list;
16043 resolve_and_layout (exception, NULL_TREE);
16044 /* First, all the nested try-catch-finally at that stage. The
16045 last element contains `throws' clause exceptions, if any. */
16046 if (IS_UNCHECKED_EXCEPTION_P (exception))
16047 return 1;
16048 while (list)
16050 tree caught;
16051 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
16052 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
16053 return 1;
16054 list = TREE_CHAIN (list);
16056 return 0;
16059 /* This function goes over all of CLASS_TYPE ctors and checks whether
16060 each of them features at least one unchecked exception in its
16061 `throws' clause. If it's the case, it returns `true', `false'
16062 otherwise. */
16064 static bool
16065 ctors_unchecked_throws_clause_p (tree class_type)
16067 tree current;
16069 for (current = TYPE_METHODS (class_type); current;
16070 current = TREE_CHAIN (current))
16072 bool ctu = false; /* Ctor Throws Unchecked */
16073 if (DECL_CONSTRUCTOR_P (current))
16075 tree throws;
16076 for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
16077 throws = TREE_CHAIN (throws))
16078 if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
16079 ctu = true;
16081 /* We return false as we found one ctor that is unfit. */
16082 if (!ctu && DECL_CONSTRUCTOR_P (current))
16083 return false;
16085 /* All ctors feature at least one unchecked exception in their
16086 `throws' clause. */
16087 return true;
16090 /* 15.24 Conditional Operator ?: */
16092 static tree
16093 patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
16095 tree cond = TREE_OPERAND (node, 0);
16096 tree op1 = TREE_OPERAND (node, 1);
16097 tree op2 = TREE_OPERAND (node, 2);
16098 tree resulting_type = NULL_TREE;
16099 tree t1, t2, patched;
16100 int error_found = 0;
16102 /* The condition and operands of ?: might be StringBuffers crafted
16103 as a result of a string concatenation. Obtain decent ones here. */
16104 if ((patched = patch_string (cond)))
16105 TREE_OPERAND (node, 0) = cond = patched;
16106 if ((patched = patch_string (op1)))
16107 TREE_OPERAND (node, 1) = op1 = patched;
16108 if ((patched = patch_string (op2)))
16109 TREE_OPERAND (node, 2) = op2 = patched;
16111 t1 = TREE_TYPE (op1);
16112 t2 = TREE_TYPE (op2);
16114 /* The first expression must be a boolean */
16115 if (TREE_TYPE (cond) != boolean_type_node)
16117 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
16118 parse_error_context (wfl_operator,
16119 "Incompatible type for %<?:%>. Can't convert %qs to %<boolean%>",
16120 lang_printable_name (TREE_TYPE (cond), 0));
16121 error_found = 1;
16124 /* Second and third can be numeric, boolean (i.e. primitive),
16125 references or null. Anything else results in an error */
16126 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
16127 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
16128 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
16129 || (t1 == boolean_type_node && t2 == boolean_type_node)))
16130 error_found = 1;
16132 /* Determine the type of the conditional expression. Same types are
16133 easy to deal with */
16134 else if (t1 == t2)
16135 resulting_type = t1;
16137 /* There are different rules for numeric types */
16138 else if (JNUMERIC_TYPE_P (t1))
16140 /* if byte/short found, the resulting type is short */
16141 if ((t1 == byte_type_node && t2 == short_type_node)
16142 || (t1 == short_type_node && t2 == byte_type_node))
16143 resulting_type = short_type_node;
16145 /* If t1 is a constant int and t2 is of type byte, short or char
16146 and t1's value fits in t2, then the resulting type is t2 */
16147 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
16148 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
16149 resulting_type = t2;
16151 /* If t2 is a constant int and t1 is of type byte, short or char
16152 and t2's value fits in t1, then the resulting type is t1 */
16153 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
16154 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
16155 resulting_type = t1;
16157 /* Otherwise, binary numeric promotion is applied and the
16158 resulting type is the promoted type of operand 1 and 2 */
16159 else
16160 resulting_type = binary_numeric_promotion (t1, t2,
16161 &TREE_OPERAND (node, 1),
16162 &TREE_OPERAND (node, 2));
16165 /* Cases of a reference and a null type */
16166 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
16167 resulting_type = t1;
16169 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
16170 resulting_type = t2;
16172 /* Last case: different reference types. If a type can be converted
16173 into the other one by assignment conversion, the latter
16174 determines the type of the expression */
16175 else if ((resulting_type = try_reference_assignconv (t1, op2)))
16176 resulting_type = promote_type (t1);
16178 else if ((resulting_type = try_reference_assignconv (t2, op1)))
16179 resulting_type = promote_type (t2);
16181 /* If we don't have any resulting type, we're in trouble */
16182 if (!resulting_type)
16184 char *t = xstrdup (lang_printable_name (t1, 0));
16185 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16186 parse_error_context (wfl_operator,
16187 "Incompatible type for %<?:%>. Can't convert %qs to %qs",
16188 t, lang_printable_name (t2, 0));
16189 free (t);
16190 error_found = 1;
16193 if (error_found)
16195 TREE_TYPE (node) = error_mark_node;
16196 return error_mark_node;
16199 TREE_TYPE (node) = resulting_type;
16200 TREE_SET_CODE (node, COND_EXPR);
16201 CAN_COMPLETE_NORMALLY (node) = 1;
16202 return node;
16205 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16207 static tree
16208 maybe_build_class_init_for_field (tree decl, tree expr)
16210 tree clas = DECL_CONTEXT (decl);
16211 if (flag_emit_class_files)
16212 return expr;
16214 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16215 && FIELD_FINAL (decl))
16217 tree init = DECL_INITIAL (decl);
16218 if (init != NULL_TREE)
16219 init = fold_constant_for_init (init, decl);
16220 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16221 return expr;
16224 return build_class_init (clas, expr);
16227 /* Try to constant fold NODE.
16228 If NODE is not a constant expression, return NULL_EXPR.
16229 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16231 static tree
16232 fold_constant_for_init (tree node, tree context)
16234 tree op0, op1, val;
16235 enum tree_code code = TREE_CODE (node);
16237 switch (code)
16239 case INTEGER_CST:
16240 if (node == null_pointer_node)
16241 return NULL_TREE;
16242 case STRING_CST:
16243 case REAL_CST:
16244 return node;
16246 case PLUS_EXPR:
16247 case MINUS_EXPR:
16248 case MULT_EXPR:
16249 case TRUNC_MOD_EXPR:
16250 case RDIV_EXPR:
16251 case LSHIFT_EXPR:
16252 case RSHIFT_EXPR:
16253 case URSHIFT_EXPR:
16254 case BIT_AND_EXPR:
16255 case BIT_XOR_EXPR:
16256 case BIT_IOR_EXPR:
16257 case TRUTH_ANDIF_EXPR:
16258 case TRUTH_ORIF_EXPR:
16259 case EQ_EXPR:
16260 case NE_EXPR:
16261 case GT_EXPR:
16262 case GE_EXPR:
16263 case LT_EXPR:
16264 case LE_EXPR:
16265 op0 = TREE_OPERAND (node, 0);
16266 op1 = TREE_OPERAND (node, 1);
16267 val = fold_constant_for_init (op0, context);
16268 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16269 return NULL_TREE;
16270 TREE_OPERAND (node, 0) = val;
16271 val = fold_constant_for_init (op1, context);
16272 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16273 return NULL_TREE;
16274 TREE_OPERAND (node, 1) = val;
16275 return patch_binop (node, op0, op1, 1);
16277 case UNARY_PLUS_EXPR:
16278 case NEGATE_EXPR:
16279 case TRUTH_NOT_EXPR:
16280 case BIT_NOT_EXPR:
16281 case CONVERT_EXPR:
16282 case NOP_EXPR:
16283 op0 = TREE_OPERAND (node, 0);
16284 val = fold_constant_for_init (op0, context);
16285 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16286 return NULL_TREE;
16287 TREE_OPERAND (node, 0) = val;
16288 val = patch_unaryop (node, op0);
16289 if (! TREE_CONSTANT (val))
16290 return NULL_TREE;
16291 return val;
16293 break;
16295 case COND_EXPR:
16296 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16297 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16298 return NULL_TREE;
16299 TREE_OPERAND (node, 0) = val;
16300 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16301 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16302 return NULL_TREE;
16303 TREE_OPERAND (node, 1) = val;
16304 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16305 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16306 return NULL_TREE;
16307 TREE_OPERAND (node, 2) = val;
16308 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 2)
16309 : TREE_OPERAND (node, 1);
16311 case VAR_DECL:
16312 case FIELD_DECL:
16313 if (! FIELD_FINAL (node)
16314 || DECL_INITIAL (node) == NULL_TREE)
16315 return NULL_TREE;
16316 val = DECL_INITIAL (node);
16317 /* Guard against infinite recursion. */
16318 DECL_INITIAL (node) = NULL_TREE;
16319 val = fold_constant_for_init (val, node);
16320 if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16321 val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16322 DECL_INITIAL (node) = val;
16323 return val;
16325 case EXPR_WITH_FILE_LOCATION:
16326 /* Compare java_complete_tree and resolve_expression_name. */
16327 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16328 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16330 tree name = EXPR_WFL_NODE (node);
16331 tree decl;
16332 if (PRIMARY_P (node))
16333 return NULL_TREE;
16334 else if (! QUALIFIED_P (name))
16336 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16337 if (decl == NULL_TREE
16338 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16339 return NULL_TREE;
16340 return fold_constant_for_init (decl, decl);
16342 else
16344 tree r = NULL_TREE;
16345 /* Install the proper context for the field resolution. */
16346 tree saved_current_class = current_class;
16347 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
16348 current_class = DECL_CONTEXT (context);
16349 qualify_ambiguous_name (node);
16350 r = resolve_field_access (node, &decl, NULL);
16351 /* Restore prior context. */
16352 current_class = saved_current_class;
16353 if (r != error_mark_node && decl != NULL_TREE)
16354 return fold_constant_for_init (decl, decl);
16355 return NULL_TREE;
16358 else
16360 op0 = TREE_OPERAND (node, 0);
16361 val = fold_constant_for_init (op0, context);
16362 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16363 return NULL_TREE;
16364 TREE_OPERAND (node, 0) = val;
16365 return val;
16368 #ifdef USE_COMPONENT_REF
16369 case IDENTIFIER:
16370 case COMPONENT_REF:
16372 #endif
16374 default:
16375 return NULL_TREE;
16379 #ifdef USE_COMPONENT_REF
16380 /* Context is 'T' for TypeName, 'P' for PackageName,
16381 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16383 tree
16384 resolve_simple_name (tree name, int context)
16388 tree
16389 resolve_qualified_name (tree name, int context)
16392 #endif
16394 void
16395 init_src_parse (void)
16397 /* Sanity check; we've been bit by this before. */
16398 if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16399 abort ();
16404 /* This section deals with the functions that are called when tables
16405 recording class initialization information are traversed. */
16407 /* This function is called for each class that is known definitely
16408 initialized when a given static method was called. This function
16409 augments a compound expression (INFO) storing all assignment to
16410 initialized static class flags if a flag already existed, otherwise
16411 a new one is created. */
16413 static int
16414 emit_test_initialization (void **entry_p, void *info)
16416 tree l = (tree) info;
16417 tree decl, init;
16418 tree key = (tree) *entry_p;
16419 tree *ite;
16420 htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16422 /* If we haven't found a flag and we're dealing with self registered
16423 with current_function_decl, then don't do anything. Self is
16424 always added as definitely initialized but this information is
16425 valid only if used outside the current function. */
16426 if (current_function_decl == TREE_PURPOSE (l)
16427 && java_treetreehash_find (cf_ht, key) == NULL)
16428 return true;
16430 ite = java_treetreehash_new (cf_ht, key);
16432 /* If we don't have a variable, create one and install it. */
16433 if (*ite == NULL)
16435 tree block;
16437 decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16438 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16439 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16440 DECL_CONTEXT (decl) = current_function_decl;
16441 DECL_INITIAL (decl) = boolean_true_node;
16442 /* Don't emit any symbolic debugging info for this decl. */
16443 DECL_IGNORED_P (decl) = 1;
16445 /* The trick is to find the right context for it. */
16446 block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16447 TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16448 BLOCK_EXPR_DECLS (block) = decl;
16449 *ite = decl;
16451 else
16452 decl = *ite;
16454 /* Now simply augment the compound that holds all the assignments
16455 pertaining to this method invocation. */
16456 init = build2 (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16457 TREE_SIDE_EFFECTS (init) = 1;
16458 TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16459 TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16461 return true;
16464 #ifdef __XGETTEXT__
16465 /* Depending on the version of Bison used to compile this grammar,
16466 it may issue generic diagnostics spelled "syntax error" or
16467 "parse error". To prevent this from changing the translation
16468 template randomly, we list all the variants of this particular
16469 diagnostic here. Translators: there is no fine distinction
16470 between diagnostics with "syntax error" in them, and diagnostics
16471 with "parse error" in them. It's okay to give them both the same
16472 translation. */
16473 const char d1[] = N_("syntax error");
16474 const char d2[] = N_("parse error");
16475 const char d3[] = N_("syntax error; also virtual memory exhausted");
16476 const char d4[] = N_("parse error; also virtual memory exhausted");
16477 const char d5[] = N_("syntax error: cannot back up");
16478 const char d6[] = N_("parse error: cannot back up");
16479 #endif
16481 #include "gt-java-parse.h"
16482 #include "gtype-java.h"