* check-init.c, decl.c, expr.c, gcj.texi, java-tree.h,
[official-gcc.git] / gcc / java / parse.y
blob11c273863f4740ccc81d9a64580b321e107c8b23
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_nested_method_access_method (tree);
330 static tree build_new_access_id (void);
332 static int nested_member_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
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 (found, 0),
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 member 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_member_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
8388 && TREE_CODE (decl) != VAR_DECL
8389 && TREE_CODE (decl) != FUNCTION_DECL))
8390 return 0;
8392 if (!INNER_CLASS_TYPE_P (type)
8393 && !(TREE_CODE (decl_type) == RECORD_TYPE
8394 && INNER_CLASS_TYPE_P (decl_type)))
8395 return 0;
8397 is_static = (TREE_CODE (decl) == FUNCTION_DECL)
8398 ? METHOD_STATIC (decl)
8399 : FIELD_STATIC (decl);
8401 /* If TYPE extends the declaration context of the non-static
8402 member we're trying to access, then this isn't a nested member
8403 access we need to worry about. */
8404 if (!is_static && inherits_from_p (type, decl_type))
8405 return 0;
8407 for (type_root = type;
8408 DECL_CONTEXT (TYPE_NAME (type_root));
8409 type_root = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type_root))))
8411 if (type_root == decl_type)
8412 return 1;
8415 if (TREE_CODE (decl_type) == RECORD_TYPE
8416 && INNER_CLASS_TYPE_P (decl_type))
8418 for (decl_type_root = decl_type;
8419 DECL_CONTEXT (TYPE_NAME (decl_type_root));
8420 decl_type_root
8421 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (decl_type_root))))
8423 if (decl_type_root == type)
8424 return 1;
8427 else
8428 decl_type_root = decl_type;
8430 if (type_root == decl_type_root)
8431 return 1;
8433 /* Before we give up, see whether it is a non-static field
8434 inherited from the enclosing context we are considering. */
8435 if (!DECL_CONTEXT (TYPE_NAME (type_root))
8436 && !is_static
8437 && inherits_from_p (type_root, decl_type))
8438 return 1;
8440 return 0;
8443 /* Return a nonzero value if NODE represents a cross-nested-class
8444 access that has already been expanded. As a side effect, it returns
8445 the name of the field being accessed and the argument passed to the
8446 access function, suitable for a regeneration of the access method
8447 call if necessary. */
8449 static int
8450 nested_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8451 tree *arg)
8453 int identified = 0;
8455 if (TREE_CODE (node) != CALL_EXPR)
8456 return 0;
8458 /* Well, GCJ generates slightly different tree nodes when compiling
8459 to native or bytecodes. It's the case for function calls. */
8461 if (flag_emit_class_files
8462 && TREE_CODE (node) == CALL_EXPR
8463 && NESTED_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8464 identified = 1;
8465 else if (!flag_emit_class_files)
8467 node = TREE_OPERAND (node, 0);
8469 if (node && TREE_OPERAND (node, 0)
8470 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8472 node = TREE_OPERAND (node, 0);
8473 if (TREE_OPERAND (node, 0)
8474 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8475 && (NESTED_FIELD_ACCESS_IDENTIFIER_P
8476 (DECL_NAME (TREE_OPERAND (node, 0)))))
8477 identified = 1;
8481 if (identified && name && arg_type && arg)
8483 tree argument = TREE_OPERAND (node, 1);
8484 *name = DECL_NAME (TREE_OPERAND (node, 0));
8486 /* The accessors for static fields do not take in a this$<n> argument,
8487 so we take the class name from the accessor's context instead. */
8488 if (argument)
8490 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8491 *arg = TREE_VALUE (argument);
8493 else
8495 *arg_type = DECL_CONTEXT (TREE_OPERAND (node, 0));
8496 *arg = NULL_TREE;
8499 return identified;
8502 /* Detect in NODE cross-nested-class field read access and
8503 transform it into a write with RHS as an argument. This function
8504 is called from the java_complete_lhs when an assignment to a LHS can
8505 be identified. */
8507 static tree
8508 nested_field_access_fix (tree wfl, tree node, tree rhs)
8510 tree name, arg_type, arg;
8512 if (nested_field_expanded_access_p (node, &name, &arg_type, &arg))
8514 node = build_nested_field_access_expr (EXPR_WFL_LINECOL (wfl),
8515 arg_type, name, arg, rhs);
8516 return java_complete_tree (node);
8518 return NULL_TREE;
8521 /* Construct the expression that calls an access method:
8522 <type>.access$<n>(<arg1> [, <arg2>]);
8524 ARG2 can be NULL and will be omitted in that case. It will denote a
8525 read access. */
8527 static tree
8528 build_nested_field_access_expr (int lc, tree type, tree access_method_name,
8529 tree arg1, tree arg2)
8531 tree args, cn, access;
8533 if (arg1)
8534 args = build_tree_list (NULL_TREE, arg1);
8535 else
8536 args = NULL_TREE;
8538 if (arg2)
8540 if (args)
8541 args = tree_cons (NULL_TREE, arg2, args);
8542 else
8543 args = build_tree_list (NULL_TREE, arg2);
8546 access
8547 = build_method_invocation (build_wfl_node (access_method_name), args);
8548 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8550 return make_qualified_primary (cn, access, lc);
8553 /* Build the name of a synthetic accessor used to access class members
8554 across nested class boundaries. */
8556 static tree
8557 build_new_access_id (void)
8559 static int access_n_counter = 1;
8560 char buffer [128];
8562 sprintf (buffer, "access$%d", access_n_counter++);
8563 return get_identifier (buffer);
8566 /* Create the static access functions for the cross-nested-class field DECL.
8567 We define a read:
8568 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8569 return inst$.field;
8571 and a write access:
8572 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8573 TREE_TYPE (<field>) value$) {
8574 return inst$.field = value$;
8576 For static fields, these methods are generated without the instance
8577 parameter.
8578 We should have a usage flag on the DECL so we can lazily turn the ones
8579 we're using for code generation. FIXME.
8582 static tree
8583 build_nested_field_access_methods (tree decl)
8585 tree id, args, stmt, mdecl, class_name = NULL_TREE;
8586 bool is_static = FIELD_STATIC (decl);
8588 if (FIELD_NESTED_ACCESS_P (decl))
8589 return FIELD_NESTED_ACCESS (decl);
8591 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8593 /* Create the identifier and a function named after it. */
8594 id = build_new_access_id ();
8596 /* The identifier is marked as bearing the name of a generated write
8597 access function for outer field accessed from inner classes. */
8598 NESTED_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8600 /* Create the read access. */
8601 if (!is_static)
8603 args = build_tree_list (inst_id,
8604 build_pointer_type (DECL_CONTEXT (decl)));
8605 TREE_CHAIN (args) = end_params_node;
8606 stmt = make_qualified_primary (build_wfl_node (inst_id),
8607 build_wfl_node (DECL_NAME (decl)), 0);
8609 else
8611 args = end_params_node;
8612 class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
8613 stmt = make_qualified_primary (build_wfl_node (class_name),
8614 build_wfl_node (DECL_NAME (decl)), 0);
8616 stmt = build_return (0, stmt);
8617 mdecl = build_nested_field_access_method (DECL_CONTEXT (decl),
8618 TREE_TYPE (decl), id, args, stmt);
8619 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8621 /* Create the write access method. No write access for final variable */
8622 if (!FIELD_FINAL (decl))
8624 if (!is_static)
8626 args = build_tree_list (inst_id,
8627 build_pointer_type (DECL_CONTEXT (decl)));
8628 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8629 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8630 stmt = make_qualified_primary (build_wfl_node (inst_id),
8631 build_wfl_node (DECL_NAME (decl)),
8634 else
8636 args = build_tree_list (wpv_id, TREE_TYPE (decl));
8637 TREE_CHAIN (args) = end_params_node;
8638 stmt = make_qualified_primary (build_wfl_node (class_name),
8639 build_wfl_node (DECL_NAME (decl)),
8642 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8643 build_wfl_node (wpv_id)));
8644 mdecl = build_nested_field_access_method (DECL_CONTEXT (decl),
8645 TREE_TYPE (decl), id,
8646 args, stmt);
8648 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8650 /* Return the access name */
8651 return FIELD_NESTED_ACCESS (decl) = id;
8654 /* Build a field access method NAME. */
8656 static tree
8657 build_nested_field_access_method (tree class, tree type, tree name,
8658 tree args, tree body)
8660 tree saved_current_function_decl, mdecl;
8662 /* Create the method */
8663 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8664 fix_method_argument_names (args, mdecl);
8665 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8667 /* Attach the method body. */
8668 saved_current_function_decl = current_function_decl;
8669 start_artificial_method_body (mdecl);
8670 java_method_add_stmt (mdecl, body);
8671 end_artificial_method_body (mdecl);
8672 current_function_decl = saved_current_function_decl;
8674 return mdecl;
8678 /* This section deals with building access function necessary for
8679 certain kinds of method invocation across nested class boundaries. */
8681 static tree
8682 build_nested_method_access_method (tree decl)
8684 tree saved_current_function_decl, mdecl;
8685 tree args = NULL_TREE, call_args = NULL_TREE;
8686 tree carg, id, body, class;
8687 char buffer [80];
8688 int parm_id_count = 0;
8690 /* Test this abort with an access to a private field */
8691 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8692 abort ();
8694 /* Check the cache first */
8695 if (DECL_FUNCTION_INNER_ACCESS (decl))
8696 return DECL_FUNCTION_INNER_ACCESS (decl);
8698 class = DECL_CONTEXT (decl);
8700 /* Obtain an access identifier and mark it */
8701 id = build_new_access_id ();
8702 NESTED_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8704 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8705 /* Create the arguments, as much as the original */
8706 for (; carg && carg != end_params_node;
8707 carg = TREE_CHAIN (carg))
8709 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8710 args = chainon (args, build_tree_list (get_identifier (buffer),
8711 TREE_VALUE (carg)));
8713 args = chainon (args, end_params_node);
8715 /* Create the method */
8716 mdecl = create_artificial_method (class, ACC_STATIC,
8717 TREE_TYPE (TREE_TYPE (decl)), id, args);
8718 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8719 /* There is a potential bug here. We should be able to use
8720 fix_method_argument_names, but then arg names get mixed up and
8721 eventually a constructor will have its this$0 altered and the
8722 outer context won't be assignment properly. The testcase is
8723 stub.java FIXME */
8724 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8726 /* Attach the method body. */
8727 saved_current_function_decl = current_function_decl;
8728 start_artificial_method_body (mdecl);
8730 /* The actual method invocation uses the same args. When invoking a
8731 static methods that way, we don't want to skip the first argument. */
8732 carg = args;
8733 if (!METHOD_STATIC (decl))
8734 carg = TREE_CHAIN (carg);
8735 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8736 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8737 call_args);
8739 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8740 call_args);
8741 if (!METHOD_STATIC (decl))
8742 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8743 body, 0);
8744 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8745 body = build_return (0, body);
8746 java_method_add_stmt (mdecl,body);
8747 end_artificial_method_body (mdecl);
8748 current_function_decl = saved_current_function_decl;
8750 /* Back tag the access function so it know what it accesses. */
8751 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8753 /* Tag the current method so it knows it has an access generated. */
8754 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8758 /* This section of the code deals with building expressions to access
8759 the enclosing instance of an inner class. The enclosing instance is
8760 kept in a generated field called this$<n>, with <n> being the
8761 inner class nesting level (starting from 0.) */
8763 /* Build an access to a given this$<n>, always chaining access call to
8764 others. Access methods to this$<n> are build on the fly if
8765 necessary. This CAN'T be used to solely access this$<n-1> from
8766 this$<n> (which alway yield to special cases and optimization, see
8767 for example build_nested_field_access). */
8769 static tree
8770 build_access_to_thisn (tree from, tree to, int lc)
8772 tree access = NULL_TREE;
8774 while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8776 if (!access)
8778 access = build_current_thisn (from);
8779 access = build_wfl_node (access);
8781 else
8783 tree access0_wfl, cn;
8785 maybe_build_thisn_access_method (from);
8786 access0_wfl = build_wfl_node (access0_identifier_node);
8787 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8788 EXPR_WFL_LINECOL (access0_wfl) = lc;
8789 access = build_tree_list (NULL_TREE, access);
8790 access = build_method_invocation (access0_wfl, access);
8791 access = make_qualified_primary (cn, access, lc);
8794 /* If FROM isn't an inner class, that's fine, we've done enough.
8795 What we're looking for can be accessed from there. */
8796 from = DECL_CONTEXT (TYPE_NAME (from));
8797 if (!from)
8798 break;
8799 from = TREE_TYPE (from);
8801 return access;
8804 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8805 is returned if nothing needs to be generated. Otherwise, the method
8806 generated and a method decl is returned.
8808 NOTE: These generated methods should be declared in a class file
8809 attribute so that they can't be referred to directly. */
8811 static tree
8812 maybe_build_thisn_access_method (tree type)
8814 tree mdecl, args, stmt, rtype;
8815 tree saved_current_function_decl;
8817 /* If TYPE is a top-level class, no access method is required.
8818 If there already is such an access method, bail out. */
8819 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8820 return NULL_TREE;
8822 /* We generate the method. The method looks like:
8823 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8825 args = build_tree_list (inst_id, build_pointer_type (type));
8826 TREE_CHAIN (args) = end_params_node;
8827 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8828 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8829 access0_identifier_node, args);
8830 fix_method_argument_names (args, mdecl);
8831 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8832 stmt = build_current_thisn (type);
8833 stmt = make_qualified_primary (build_wfl_node (inst_id),
8834 build_wfl_node (stmt), 0);
8835 stmt = build_return (0, stmt);
8837 saved_current_function_decl = current_function_decl;
8838 start_artificial_method_body (mdecl);
8839 java_method_add_stmt (mdecl, stmt);
8840 end_artificial_method_body (mdecl);
8841 current_function_decl = saved_current_function_decl;
8843 CLASS_ACCESS0_GENERATED_P (type) = 1;
8845 return mdecl;
8848 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8849 the first level of innerclassing. this$1 for the next one, etc...
8850 This function can be invoked with TYPE to NULL, available and then
8851 has to count the parser context. */
8853 static GTY(()) tree saved_thisn;
8854 static GTY(()) tree saved_type;
8856 static tree
8857 build_current_thisn (tree type)
8859 static int saved_i = -1;
8860 static int saved_type_i = 0;
8861 tree decl;
8862 char buffer [24];
8863 int i = 0;
8865 if (type)
8867 if (type == saved_type)
8868 i = saved_type_i;
8869 else
8871 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8872 decl; decl = DECL_CONTEXT (decl), i++)
8875 saved_type = type;
8876 saved_type_i = i;
8879 else
8880 i = list_length (GET_CPC_LIST ())-2;
8882 if (i == saved_i)
8883 return saved_thisn;
8885 sprintf (buffer, "this$%d", i);
8886 saved_i = i;
8887 saved_thisn = get_identifier (buffer);
8888 return saved_thisn;
8891 /* Return the assignment to the hidden enclosing context `this$<n>'
8892 by the second incoming parameter to the innerclass constructor. The
8893 form used is `this.this$<n> = this$<n>;'. */
8895 static tree
8896 build_thisn_assign (void)
8898 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8900 tree thisn = build_current_thisn (current_class);
8901 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8902 build_wfl_node (thisn), 0);
8903 tree rhs = build_wfl_node (thisn);
8904 #ifdef USE_MAPPED_LOCATION
8905 SET_EXPR_LOCATION (lhs, input_location);
8906 #else
8907 EXPR_WFL_SET_LINECOL (lhs, input_line, 0);
8908 #endif
8909 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8911 return NULL_TREE;
8915 /* Building the synthetic `class$' used to implement the `.class' 1.1
8916 extension for non primitive types. This method looks like:
8918 static Class class$(String type) throws NoClassDefFoundError
8920 try {return (java.lang.Class.forName (String));}
8921 catch (ClassNotFoundException e) {
8922 throw new NoClassDefFoundError(e.getMessage());}
8923 } */
8925 static GTY(()) tree get_message_wfl;
8926 static GTY(()) tree type_parm_wfl;
8928 static tree
8929 build_dot_class_method (tree class)
8931 #define BWF(S) build_wfl_node (get_identifier ((S)))
8932 #ifdef USE_MAPPED_LOCATION
8933 #define MQN(X,Y) make_qualified_name ((X), (Y), UNKNOWN_LOCATION)
8934 #else
8935 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8936 #endif
8937 tree args, tmp, saved_current_function_decl, mdecl, qual_name;
8938 tree stmt, throw_stmt;
8940 if (!get_message_wfl)
8942 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8943 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8946 /* Build the arguments */
8947 args = build_tree_list (get_identifier ("type$"),
8948 build_pointer_type (string_type_node));
8949 TREE_CHAIN (args) = end_params_node;
8951 /* Build the qualified name java.lang.Class.forName */
8952 tmp = MQN (MQN (MQN (BWF ("java"),
8953 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8955 /* Create the "class$" function */
8956 mdecl = create_artificial_method (class, ACC_STATIC,
8957 build_pointer_type (class_type_node),
8958 classdollar_identifier_node, args);
8959 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8960 BWF ("NoClassDefFoundError"));
8961 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE, qual_name);
8962 register_incomplete_type (JDEP_EXCEPTION, qual_name, NULL_TREE, NULL_TREE);
8963 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
8964 &TREE_VALUE (DECL_FUNCTION_THROWS (mdecl));
8966 /* We start by building the try block. We need to build:
8967 return (java.lang.Class.forName (type)); */
8968 stmt = build_method_invocation (tmp,
8969 build_tree_list (NULL_TREE, type_parm_wfl));
8970 stmt = build_return (0, stmt);
8972 /* Now onto the catch block. We start by building the expression
8973 throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8974 #ifdef USE_MAPPED_LOCATION
8975 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8976 get_message_wfl, UNKNOWN_LOCATION);
8977 #else
8978 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8979 get_message_wfl, 0);
8980 #endif
8981 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8983 /* Build new NoClassDefFoundError (_.getMessage) */
8984 throw_stmt = build_new_invocation
8985 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8986 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8988 /* Build the throw, (it's too early to use BUILD_THROW) */
8989 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8991 /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8992 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8993 BWF ("ClassNotFoundException"));
8994 stmt = encapsulate_with_try_catch (0, qual_name, stmt, throw_stmt);
8996 fix_method_argument_names (args, mdecl);
8997 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8998 saved_current_function_decl = current_function_decl;
8999 start_artificial_method_body (mdecl);
9000 java_method_add_stmt (mdecl, stmt);
9001 end_artificial_method_body (mdecl);
9002 current_function_decl = saved_current_function_decl;
9003 TYPE_DOT_CLASS (class) = mdecl;
9005 return mdecl;
9008 static tree
9009 build_dot_class_method_invocation (tree this_class, tree type)
9011 tree dot_class_method = TYPE_DOT_CLASS (this_class);
9012 tree sig_id, s, t;
9014 if (TYPE_ARRAY_P (type))
9015 sig_id = build_java_signature (type);
9016 else
9017 sig_id = DECL_NAME (TYPE_NAME (type));
9019 /* Ensure that the proper name separator is used */
9020 sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
9021 IDENTIFIER_LENGTH (sig_id));
9023 s = build_string (IDENTIFIER_LENGTH (sig_id),
9024 IDENTIFIER_POINTER (sig_id));
9025 t = build_method_invocation (build_wfl_node (DECL_NAME (dot_class_method)),
9026 build_tree_list (NULL_TREE, s));
9027 if (DECL_CONTEXT (dot_class_method) != this_class)
9029 tree class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (dot_class_method)));
9030 t = make_qualified_primary (build_wfl_node (class_name), t, 0);
9032 return t;
9035 /* This section of the code deals with constructor. */
9037 /* Craft a body for default constructor. Patch existing constructor
9038 bodies with call to super() and field initialization statements if
9039 necessary. */
9041 static void
9042 fix_constructors (tree mdecl)
9044 tree iii; /* Instance Initializer Invocation */
9045 tree *bodyp = &DECL_FUNCTION_BODY (mdecl);
9046 tree thisn_assign, compound = NULL_TREE;
9047 tree class_type = DECL_CONTEXT (mdecl);
9049 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
9050 return;
9051 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
9053 if (!*bodyp)
9055 /* It is an error for the compiler to generate a default
9056 constructor if the superclass doesn't have a constructor that
9057 takes no argument, or the same args for an anonymous class */
9058 tree sdecl = get_constructor_super (mdecl);
9059 if (sdecl == NULL_TREE)
9061 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
9062 tree save = DECL_NAME (mdecl);
9063 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
9064 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
9065 parse_error_context
9066 (lookup_cl (TYPE_NAME (class_type)),
9067 "No constructor matching %qs found in class %qs",
9068 lang_printable_name (mdecl, 2), n);
9069 DECL_NAME (mdecl) = save;
9072 if (ANONYMOUS_CLASS_P (class_type))
9074 /* Copy throws clause from the super constructor. */
9075 tree throws = DECL_FUNCTION_THROWS (sdecl);
9076 DECL_FUNCTION_THROWS (mdecl) = copy_list (throws);
9079 /* The constructor body must be crafted by hand. It's the
9080 constructor we defined when we realize we didn't have the
9081 CLASSNAME() constructor */
9082 start_artificial_method_body (mdecl);
9084 /* Insert an assignment to the this$<n> hidden field, if
9085 necessary */
9086 if ((thisn_assign = build_thisn_assign ()))
9087 java_method_add_stmt (mdecl, thisn_assign);
9089 /* We don't generate a super constructor invocation if we're
9090 compiling java.lang.Object. build_super_invocation takes care
9091 of that. */
9092 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
9094 /* FIXME */
9095 if ((iii = build_instinit_invocation (class_type)))
9096 java_method_add_stmt (mdecl, iii);
9098 end_artificial_method_body (mdecl);
9100 /* Search for an explicit constructor invocation */
9101 else
9103 int found = 0;
9104 int invokes_this = 0;
9105 tree main_block = BLOCK_EXPR_BODY (*bodyp);
9107 while (*bodyp)
9109 tree body = *bodyp;
9110 switch (TREE_CODE (body))
9112 case CALL_EXPR:
9113 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
9114 if (CALL_THIS_CONSTRUCTOR_P (body))
9115 invokes_this = 1;
9116 break;
9117 case COMPOUND_EXPR:
9118 case EXPR_WITH_FILE_LOCATION:
9119 bodyp = &TREE_OPERAND (body, 0);
9120 continue;
9121 case BLOCK:
9122 bodyp = &BLOCK_EXPR_BODY (body);
9123 continue;
9124 default:
9125 break;
9127 break;
9130 /* Generate the assignment to this$<n>, if necessary */
9131 if ((thisn_assign = build_thisn_assign ()))
9132 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
9134 /* The constructor is missing an invocation of super() */
9135 if (!found)
9136 compound = add_stmt_to_compound (compound, NULL_TREE,
9137 build_super_invocation (mdecl));
9138 /* Explicit super() invocation should take place before the
9139 instance initializer blocks. */
9140 else
9142 compound = add_stmt_to_compound (compound, NULL_TREE, *bodyp);
9143 *bodyp = build_java_empty_stmt ();
9146 DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
9148 /* Insert the instance initializer block right after. */
9149 if (!invokes_this && (iii = build_instinit_invocation (class_type)))
9150 compound = add_stmt_to_compound (compound, NULL_TREE, iii);
9152 /* Fix the constructor main block if we're adding extra stmts */
9153 if (compound)
9155 compound = add_stmt_to_compound (compound, NULL_TREE,
9156 BLOCK_EXPR_BODY (main_block));
9157 BLOCK_EXPR_BODY (main_block) = compound;
9162 /* Browse constructors in the super class, searching for a constructor
9163 that doesn't take any argument. Return the constructor if one is found,
9164 NULL_TREE otherwise. If the current class is an anonymous inner class,
9165 look for something that has the same signature. */
9166 static tree
9167 get_constructor_super (tree mdecl)
9169 tree class = CLASSTYPE_SUPER (current_class);
9170 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
9171 tree sdecl;
9173 if (!class)
9174 return NULL_TREE;
9176 if (ANONYMOUS_CLASS_P (current_class))
9178 tree mdecl_arg_type;
9179 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
9180 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9181 if (DECL_CONSTRUCTOR_P (sdecl))
9183 tree m_arg_type;
9184 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9185 if (super_inner)
9186 arg_type = TREE_CHAIN (arg_type);
9187 for (m_arg_type = mdecl_arg_type;
9188 (arg_type != end_params_node
9189 && m_arg_type != end_params_node);
9190 arg_type = TREE_CHAIN (arg_type),
9191 m_arg_type = TREE_CHAIN (m_arg_type))
9192 if (!valid_method_invocation_conversion_p
9193 (TREE_VALUE (arg_type),
9194 TREE_VALUE (m_arg_type)))
9195 break;
9197 if (arg_type == end_params_node && m_arg_type == end_params_node)
9198 return sdecl;
9201 else
9203 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9205 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9206 if (super_inner)
9207 arg = TREE_CHAIN (arg);
9208 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
9209 return sdecl;
9212 return NULL_TREE;
9215 /* Generate code for all context remembered for code generation. */
9217 static GTY(()) tree reversed_class_list;
9218 void
9219 java_expand_classes (void)
9221 int save_error_count = 0;
9222 static struct parser_ctxt *cur_ctxp = NULL;
9223 location_t save_location;
9225 java_parse_abort_on_error ();
9226 if (!(ctxp = ctxp_for_generation))
9227 return;
9228 java_layout_classes ();
9229 java_parse_abort_on_error ();
9230 save_location = input_location;
9232 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9234 tree current;
9235 for (current = cur_ctxp->class_list;
9236 current;
9237 current = TREE_CHAIN (current))
9238 gen_indirect_dispatch_tables (TREE_TYPE (current));
9241 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9243 ctxp = cur_ctxp;
9244 input_location = ctxp->file_start_location;
9245 lang_init_source (2); /* Error msgs have method prototypes */
9246 java_complete_expand_classes (); /* Complete and expand classes */
9247 java_parse_abort_on_error ();
9249 input_location = save_location;
9251 /* Find anonymous classes and expand their constructor. This extra pass is
9252 necessary because the constructor itself is only generated when the
9253 method in which it is defined is expanded. */
9254 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9256 tree current;
9257 ctxp = cur_ctxp;
9258 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9260 output_class = current_class = TREE_TYPE (current);
9261 if (ANONYMOUS_CLASS_P (current_class))
9263 tree d;
9264 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9266 if (DECL_CONSTRUCTOR_P (d))
9268 java_complete_expand_method (d);
9269 break; /* There is only one constructor. */
9276 /* Expanding the constructors of anonymous classes generates access
9277 methods. Scan all the methods looking for null DECL_RESULTs --
9278 this will be the case if a method hasn't been expanded. */
9279 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9281 tree current;
9282 ctxp = cur_ctxp;
9283 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9285 tree d;
9286 output_class = current_class = TREE_TYPE (current);
9287 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9289 if (DECL_RESULT (d) == NULL_TREE)
9290 java_complete_expand_method (d);
9295 /* ??? Instead of all this we could iterate around the list of
9296 classes until there were no more un-expanded methods. It would
9297 take a little longer -- one pass over the whole list of methods
9298 -- but it would be simpler. Like this: */
9299 #if 0
9301 int something_changed;
9305 something_changed = 0;
9306 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9308 tree current;
9309 ctxp = cur_ctxp;
9310 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9312 tree d;
9313 output_class = current_class = TREE_TYPE (current);
9314 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9316 if (DECL_RESULT (d) == NULL_TREE)
9318 something_changed = 1;
9319 java_complete_expand_method (d);
9325 while (something_changed);
9327 #endif
9329 /* If we've found error at that stage, don't try to generate
9330 anything, unless we're checking the syntax only
9331 (but not using -fsyntax-only for the purpose of generating
9332 bytecode). */
9333 if (java_error_count
9334 && (!flag_syntax_only && !flag_emit_class_files))
9335 return;
9337 /* Now things are stable, go for generation of the class data. */
9339 /* We pessimistically marked all methods and fields external until
9340 we knew what set of classes we were planning to compile. Now mark
9341 those that will be generated locally as not external. */
9342 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9344 tree current;
9345 ctxp = cur_ctxp;
9346 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9347 java_mark_class_local (TREE_TYPE (current));
9350 /* Compile the classes. */
9351 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9353 tree current;
9354 reversed_class_list = NULL;
9356 ctxp = cur_ctxp;
9358 /* We write out the classes in reverse order. This ensures that
9359 inner classes are written before their containing classes,
9360 which is important for parallel builds. Otherwise, the
9361 class file for the outer class may be found, but the class
9362 file for the inner class may not be present. In that
9363 situation, the compiler cannot fall back to the original
9364 source, having already read the outer class, so we must
9365 prevent that situation. */
9366 for (current = ctxp->class_list;
9367 current;
9368 current = TREE_CHAIN (current))
9369 reversed_class_list
9370 = tree_cons (NULL_TREE, current, reversed_class_list);
9372 for (current = reversed_class_list;
9373 current;
9374 current = TREE_CHAIN (current))
9376 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
9377 if (flag_emit_class_files)
9378 write_classfile (current_class);
9379 else if (! flag_syntax_only)
9380 java_expand_method_bodies (current_class);
9385 void
9386 java_finish_classes (void)
9388 static struct parser_ctxt *cur_ctxp = NULL;
9389 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9391 tree current;
9392 ctxp = cur_ctxp;
9393 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9395 output_class = current_class = TREE_TYPE (current);
9396 finish_class ();
9401 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9402 a tree list node containing RIGHT. Fore coming RIGHTs will be
9403 chained to this hook. LOCATION contains the location of the
9404 separating `.' operator. */
9406 static tree
9407 make_qualified_primary (tree primary, tree right, int location)
9409 tree wfl;
9411 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9412 wfl = build_wfl_wrap (primary, location);
9413 else
9415 wfl = primary;
9416 /* If wfl wasn't qualified, we build a first anchor */
9417 if (!EXPR_WFL_QUALIFICATION (wfl))
9418 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9421 /* And chain them */
9422 EXPR_WFL_LINECOL (right) = location;
9423 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9424 PRIMARY_P (wfl) = 1;
9425 return wfl;
9428 /* Simple merge of two name separated by a `.' */
9430 static tree
9431 merge_qualified_name (tree left, tree right)
9433 tree node;
9434 if (!left && !right)
9435 return NULL_TREE;
9437 if (!left)
9438 return right;
9440 if (!right)
9441 return left;
9443 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9444 IDENTIFIER_LENGTH (left));
9445 obstack_1grow (&temporary_obstack, '.');
9446 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9447 IDENTIFIER_LENGTH (right));
9448 node = get_identifier (obstack_base (&temporary_obstack));
9449 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9450 QUALIFIED_P (node) = 1;
9451 return node;
9454 /* Merge the two parts of a qualified name into LEFT. Set the
9455 location information of the resulting node to LOCATION, usually
9456 inherited from the location information of the `.' operator. */
9458 static tree
9459 make_qualified_name (tree left, tree right,
9460 #ifdef USE_MAPPED_LOCATION
9461 source_location location
9462 #else
9463 int location
9464 #endif
9467 #ifdef USE_COMPONENT_REF
9468 tree node = build3 (COMPONENT_REF, NULL_TREE, left, right, NULL_TREE);
9469 SET_EXPR_LOCATION (node, location);
9470 return node;
9471 #else
9472 tree left_id = EXPR_WFL_NODE (left);
9473 tree right_id = EXPR_WFL_NODE (right);
9474 tree wfl, merge;
9476 merge = merge_qualified_name (left_id, right_id);
9478 /* Left wasn't qualified and is now qualified */
9479 #ifdef USE_MAPPED_LOCATION
9480 if (!QUALIFIED_P (left_id))
9482 tree wfl = build_expr_wfl (left_id, EXPR_LOCATION (left));
9483 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9486 wfl = build_expr_wfl (right_id, location);
9487 #else
9488 if (!QUALIFIED_P (left_id))
9490 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9491 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9492 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9495 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9496 EXPR_WFL_LINECOL (wfl) = location;
9497 #endif
9498 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9499 EXPR_WFL_NODE (left) = merge;
9500 return left;
9501 #endif
9504 /* Extract the last identifier component of the qualified in WFL. The
9505 last identifier is removed from the linked list */
9507 static tree
9508 cut_identifier_in_qualified (tree wfl)
9510 tree q;
9511 tree previous = NULL_TREE;
9512 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9513 if (!TREE_CHAIN (q))
9515 if (!previous)
9516 /* Operating on a non qualified qualified WFL. */
9517 abort ();
9519 TREE_CHAIN (previous) = NULL_TREE;
9520 return TREE_PURPOSE (q);
9524 /* Resolve the expression name NAME. Return its decl. */
9526 static tree
9527 resolve_expression_name (tree id, tree *orig)
9529 tree name = EXPR_WFL_NODE (id);
9530 tree decl;
9532 /* 6.5.5.1: Simple expression names */
9533 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9535 /* 15.13.1: NAME can appear within the scope of a local variable
9536 declaration */
9537 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9538 return decl;
9540 /* 15.13.1: NAME can appear within a class declaration */
9541 else
9543 decl = lookup_field_wrapper (current_class, name);
9544 if (decl)
9546 tree access = NULL_TREE;
9547 int fs = FIELD_STATIC (decl);
9549 /* If we're accessing an outer scope local alias, make
9550 sure we change the name of the field we're going to
9551 build access to. */
9552 if (FIELD_LOCAL_ALIAS_USED (decl))
9553 name = DECL_NAME (decl);
9555 check_deprecation (id, decl);
9557 /* Instance variable (8.3.1.1) can't appear within
9558 static method, static initializer or initializer for
9559 a static variable. */
9560 if (!fs && METHOD_STATIC (current_function_decl))
9562 static_ref_err (id, name, current_class);
9563 return error_mark_node;
9565 /* Instance variables can't appear as an argument of
9566 an explicit constructor invocation */
9567 if (!fs && ctxp->explicit_constructor_p
9568 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9570 parse_error_context
9571 (id, "Can't reference %qs before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9572 return error_mark_node;
9575 /* If we're processing an inner class and we're trying
9576 to access a field belonging to an outer class, build
9577 the access to the field. */
9578 if (nested_member_access_p (current_class, decl))
9580 if (!fs && CLASS_STATIC (TYPE_NAME (current_class)))
9582 static_ref_err (id, DECL_NAME (decl), current_class);
9583 return error_mark_node;
9585 access = build_nested_field_access (id, decl);
9586 if (orig)
9587 *orig = access;
9588 return access;
9591 /* Otherwise build what it takes to access the field */
9592 access = build_field_ref ((fs ? NULL_TREE : current_this),
9593 DECL_CONTEXT (decl), name);
9594 if (fs)
9595 access = maybe_build_class_init_for_field (decl, access);
9596 /* We may be asked to save the real field access node */
9597 if (orig)
9598 *orig = access;
9599 /* Last check: can we access the field? */
9600 if (not_accessible_p (current_class, decl, NULL_TREE, 0))
9602 not_accessible_field_error (id, decl);
9603 return error_mark_node;
9605 /* And we return what we got */
9606 return access;
9608 /* Fall down to error report on undefined variable */
9611 /* 6.5.5.2 Qualified Expression Names */
9612 else
9614 if (orig)
9615 *orig = NULL_TREE;
9616 qualify_ambiguous_name (id);
9617 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9618 /* 15.10.2: Accessing Superclass Members using super */
9619 return resolve_field_access (id, orig, NULL);
9622 /* We've got an error here */
9623 if (INNER_CLASS_TYPE_P (current_class))
9624 parse_error_context (id,
9625 "Local variable %qs can't be accessed from within the inner class %qs unless it is declared final",
9626 IDENTIFIER_POINTER (name),
9627 IDENTIFIER_POINTER (DECL_NAME
9628 (TYPE_NAME (current_class))));
9629 else
9630 parse_error_context (id, "Undefined variable %qs",
9631 IDENTIFIER_POINTER (name));
9633 return error_mark_node;
9636 static void
9637 static_ref_err (tree wfl, tree field_id, tree class_type)
9639 parse_error_context
9640 (wfl,
9641 "Can't make a static reference to nonstatic variable %qs in class %qs",
9642 IDENTIFIER_POINTER (field_id),
9643 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9646 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9647 We return something suitable to generate the field access. We also
9648 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9649 recipient's address can be null. */
9651 static tree
9652 resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9654 int is_static = 0;
9655 tree field_ref;
9656 tree decl = NULL_TREE, where_found, type_found;
9658 if (resolve_qualified_expression_name (qual_wfl, &decl,
9659 &where_found, &type_found))
9660 return error_mark_node;
9662 /* Resolve the LENGTH field of an array here */
9663 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9664 && type_found && TYPE_ARRAY_P (type_found)
9665 && ! flag_emit_class_files)
9667 tree length = build_java_array_length_access (where_found);
9668 field_ref = length;
9670 /* In case we're dealing with a static array, we need to
9671 initialize its class before the array length can be fetched. */
9672 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9674 build_static_field_ref (where_found);
9675 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9678 /* We might have been trying to resolve field.method(). In which
9679 case, the resolution is over and decl is the answer */
9680 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9681 field_ref = decl;
9682 else if (JDECL_P (decl))
9684 if (!type_found)
9685 type_found = DECL_CONTEXT (decl);
9686 is_static = FIELD_STATIC (decl);
9687 field_ref = build_field_ref ((is_static ?
9688 NULL_TREE : where_found),
9689 type_found, DECL_NAME (decl));
9690 if (field_ref == error_mark_node)
9691 return error_mark_node;
9692 if (is_static)
9693 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9695 /* If we're looking at a static field, we may need to generate a
9696 class initialization for it. This can happen when the access
9697 looks like `field.ref', where `field' is a static field in an
9698 interface we implement. */
9699 if (!flag_emit_class_files
9700 && TREE_CODE (where_found) == VAR_DECL
9701 && FIELD_STATIC (where_found))
9703 build_static_field_ref (where_found);
9704 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9707 else
9708 field_ref = decl;
9710 if (field_decl)
9711 *field_decl = decl;
9712 if (field_type)
9713 *field_type = (QUAL_DECL_TYPE (decl) ?
9714 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9715 return field_ref;
9718 /* If NODE is an access to a static field, strip out the class
9719 initialization part and return the field decl, otherwise, return
9720 NODE. */
9722 tree
9723 extract_field_decl (tree node)
9725 if (TREE_CODE (node) == COMPOUND_EXPR)
9727 tree op1 = TREE_OPERAND (node, 1);
9728 if (TREE_CODE (op1) == COMPOUND_EXPR)
9730 tree call = TREE_OPERAND (op1, 0);
9731 if (TREE_CODE (call) == CALL_EXPR
9732 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9733 && (TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9734 == soft_initclass_node))
9735 return TREE_OPERAND (op1, 1);
9737 else if (JDECL_P (op1))
9738 return op1;
9740 return node;
9743 /* 6.5.5.2: Qualified Expression Names */
9745 static int
9746 resolve_qualified_expression_name (tree wfl, tree *found_decl,
9747 tree *where_found, tree *type_found)
9749 int from_type = 0; /* Field search initiated from a type */
9750 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9751 int previous_call_static = 0;
9752 int is_static;
9753 tree decl = NULL_TREE, type = NULL_TREE, q;
9754 /* For certain for of inner class instantiation */
9755 tree saved_current, saved_this;
9756 #define RESTORE_THIS_AND_CURRENT_CLASS \
9757 { current_class = saved_current; current_this = saved_this;}
9759 *type_found = *where_found = NULL_TREE;
9761 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9763 tree qual_wfl = QUAL_WFL (q);
9764 tree ret_decl; /* for EH checking */
9765 #ifdef USE_MAPPED_LOCATION
9766 source_location location; /* for EH checking */
9767 #else
9768 int location; /* for EH checking */
9769 #endif
9771 /* 15.10.1 Field Access Using a Primary */
9772 switch (TREE_CODE (qual_wfl))
9774 case CALL_EXPR:
9775 case NEW_CLASS_EXPR:
9776 /* If the access to the function call is a non static field,
9777 build the code to access it. */
9778 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9780 decl = maybe_access_field (decl, *where_found,
9781 DECL_CONTEXT (decl));
9782 if (decl == error_mark_node)
9783 return 1;
9786 /* And code for the function call */
9787 if (complete_function_arguments (qual_wfl))
9788 return 1;
9790 /* We might have to setup a new current class and a new this
9791 for the search of an inner class, relative to the type of
9792 a expression resolved as `decl'. The current values are
9793 saved and restored shortly after */
9794 saved_current = current_class;
9795 saved_this = current_this;
9796 if (decl
9797 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9798 || from_qualified_this))
9800 /* If we still have `from_qualified_this', we have the form
9801 <T>.this.f() and we need to build <T>.this */
9802 if (from_qualified_this)
9804 decl = build_access_to_thisn (current_class, type, 0);
9805 decl = java_complete_tree (decl);
9806 type = TREE_TYPE (TREE_TYPE (decl));
9808 current_class = type;
9809 current_this = decl;
9810 from_qualified_this = 0;
9813 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9814 CALL_USING_SUPER (qual_wfl) = 1;
9815 #ifdef USE_MAPPED_LOCATION
9816 location = (TREE_CODE (qual_wfl) == CALL_EXPR
9817 ? EXPR_LOCATION (TREE_OPERAND (qual_wfl, 0))
9818 : UNKNOWN_LOCATION);
9819 #else
9820 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9821 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9822 #endif
9823 *where_found = patch_method_invocation (qual_wfl, decl, type,
9824 from_super,
9825 &is_static, &ret_decl);
9826 from_super = 0;
9827 if (*where_found == error_mark_node)
9829 RESTORE_THIS_AND_CURRENT_CLASS;
9830 return 1;
9832 *type_found = type = QUAL_DECL_TYPE (*where_found);
9834 *where_found = force_evaluation_order (*where_found);
9836 /* If we're creating an inner class instance, check for that
9837 an enclosing instance is in scope */
9838 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9839 && INNER_ENCLOSING_SCOPE_CHECK (type))
9841 parse_error_context
9842 (qual_wfl, "No enclosing instance for inner class %qs is in scope%s",
9843 lang_printable_name (type, 0),
9844 (!current_this ? "" :
9845 "; an explicit one must be provided when creating this inner class"));
9846 RESTORE_THIS_AND_CURRENT_CLASS;
9847 return 1;
9850 /* In case we had to change then to resolve a inner class
9851 instantiation using a primary qualified by a `new' */
9852 RESTORE_THIS_AND_CURRENT_CLASS;
9854 #ifdef USE_MAPPED_LOCATION
9855 if (location != UNKNOWN_LOCATION)
9856 #else
9857 if (location)
9858 #endif
9860 tree arguments = NULL_TREE;
9861 if (TREE_CODE (qual_wfl) == CALL_EXPR
9862 && TREE_OPERAND (qual_wfl, 1) != NULL_TREE)
9863 arguments = TREE_VALUE (TREE_OPERAND (qual_wfl, 1));
9864 check_thrown_exceptions (location, ret_decl, arguments);
9867 /* If the previous call was static and this one is too,
9868 build a compound expression to hold the two (because in
9869 that case, previous function calls aren't transported as
9870 forcoming function's argument. */
9871 if (previous_call_static && is_static)
9873 /* We must set CAN_COMPLETE_NORMALLY for the first call
9874 since it is done nowhere else. */
9875 CAN_COMPLETE_NORMALLY (decl) = 1;
9876 decl = build2 (COMPOUND_EXPR, TREE_TYPE (*where_found),
9877 decl, *where_found);
9878 TREE_SIDE_EFFECTS (decl) = 1;
9880 else
9882 previous_call_static = is_static;
9883 decl = *where_found;
9885 from_type = 0;
9886 continue;
9888 case NEW_ARRAY_EXPR:
9889 case NEW_ANONYMOUS_ARRAY_EXPR:
9890 *where_found = decl = java_complete_tree (qual_wfl);
9891 if (decl == error_mark_node)
9892 return 1;
9893 *type_found = type = QUAL_DECL_TYPE (decl);
9894 continue;
9896 case CONVERT_EXPR:
9897 *where_found = decl = java_complete_tree (qual_wfl);
9898 if (decl == error_mark_node)
9899 return 1;
9900 *type_found = type = QUAL_DECL_TYPE (decl);
9901 from_cast = 1;
9902 continue;
9904 case CONDITIONAL_EXPR:
9905 case STRING_CST:
9906 case MODIFY_EXPR:
9907 *where_found = decl = java_complete_tree (qual_wfl);
9908 if (decl == error_mark_node)
9909 return 1;
9910 *type_found = type = QUAL_DECL_TYPE (decl);
9911 continue;
9913 case ARRAY_REF:
9914 /* If the access to the function call is a non static field,
9915 build the code to access it. */
9916 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9918 decl = maybe_access_field (decl, *where_found, type);
9919 if (decl == error_mark_node)
9920 return 1;
9922 /* And code for the array reference expression */
9923 decl = java_complete_tree (qual_wfl);
9924 if (decl == error_mark_node)
9925 return 1;
9926 type = QUAL_DECL_TYPE (decl);
9927 continue;
9929 case PLUS_EXPR:
9930 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9931 return 1;
9932 if ((type = patch_string (decl)))
9933 decl = type;
9934 *where_found = QUAL_RESOLUTION (q) = decl;
9935 *type_found = type = TREE_TYPE (decl);
9936 break;
9938 case CLASS_LITERAL:
9939 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9940 return 1;
9941 *where_found = QUAL_RESOLUTION (q) = decl;
9942 *type_found = type = TREE_TYPE (decl);
9943 break;
9945 default:
9946 /* Fix for -Wall Just go to the next statement. Don't
9947 continue */
9948 break;
9951 /* If we fall here, we weren't processing a (static) function call. */
9952 previous_call_static = 0;
9954 /* It can be the keyword THIS */
9955 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9956 && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9958 if (!current_this)
9960 parse_error_context
9961 (wfl, "Keyword %<this%> used outside allowed context");
9962 return 1;
9964 if (ctxp->explicit_constructor_p
9965 && type == current_class)
9967 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
9968 return 1;
9970 /* We have to generate code for intermediate access */
9971 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9973 *where_found = decl = current_this;
9974 *type_found = type = QUAL_DECL_TYPE (decl);
9976 /* We're trying to access the this from somewhere else. Make sure
9977 it's allowed before doing so. */
9978 else
9980 if (!enclosing_context_p (type, current_class))
9982 char *p = xstrdup (lang_printable_name (type, 0));
9983 parse_error_context (qual_wfl, "Can't use variable %<%s.this%>: type %qs isn't an outer type of type %qs",
9984 p, p,
9985 lang_printable_name (current_class, 0));
9986 free (p);
9987 return 1;
9989 from_qualified_this = 1;
9990 /* If there's nothing else after that, we need to
9991 produce something now, otherwise, the section of the
9992 code that needs to produce <T>.this will generate
9993 what is necessary. */
9994 if (!TREE_CHAIN (q))
9996 decl = build_access_to_thisn (current_class, type, 0);
9997 *where_found = decl = java_complete_tree (decl);
9998 *type_found = type = TREE_TYPE (decl);
10002 from_type = 0;
10003 continue;
10006 /* 15.10.2 Accessing Superclass Members using SUPER */
10007 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
10008 && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
10010 tree node;
10011 /* Check on the restricted use of SUPER */
10012 if (METHOD_STATIC (current_function_decl)
10013 || current_class == object_type_node)
10015 parse_error_context
10016 (wfl, "Keyword %<super%> used outside allowed context");
10017 return 1;
10019 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
10020 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
10021 CLASSTYPE_SUPER (current_class),
10022 build_this (EXPR_WFL_LINECOL (qual_wfl)));
10023 *where_found = decl = java_complete_tree (node);
10024 if (decl == error_mark_node)
10025 return 1;
10026 *type_found = type = QUAL_DECL_TYPE (decl);
10027 from_super = from_type = 1;
10028 continue;
10031 /* 15.13.1: Can't search for field name in packages, so we
10032 assume a variable/class name was meant. */
10033 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10035 tree name;
10036 if ((decl = resolve_package (wfl, &q, &name)))
10038 tree list;
10039 *where_found = decl;
10041 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10043 /* We want to be absolutely sure that the class is laid
10044 out. We're going to search something inside it. */
10045 *type_found = type = TREE_TYPE (decl);
10046 layout_class (type);
10047 from_type = 1;
10049 /* Fix them all the way down, if any are left. */
10050 if (q)
10052 list = TREE_CHAIN (q);
10053 while (list)
10055 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
10056 list = TREE_CHAIN (list);
10060 else
10062 if (from_super || from_cast)
10063 parse_error_context
10064 ((from_cast ? qual_wfl : wfl),
10065 "No variable %qs defined in class %qs",
10066 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10067 lang_printable_name (type, 0));
10068 else
10069 parse_error_context
10070 (qual_wfl, "Undefined variable or class name: %qs",
10071 IDENTIFIER_POINTER (name));
10072 return 1;
10076 /* We have a type name. It's been already resolved when the
10077 expression was qualified. */
10078 else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
10080 decl = QUAL_RESOLUTION (q);
10082 /* Sneak preview. If next we see a `new', we're facing a
10083 qualification which resulted in a type being selected
10084 instead of a field. Report the error. */
10085 if(TREE_CHAIN (q)
10086 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
10088 parse_error_context (qual_wfl, "Undefined variable %qs",
10089 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10090 return 1;
10093 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10095 check_deprecation (qual_wfl, decl);
10097 type = TREE_TYPE (decl);
10098 from_type = 1;
10100 /* We resolve an expression name */
10101 else
10103 tree field_decl = NULL_TREE;
10105 /* If there exists an early resolution, use it. That occurs
10106 only once and we know that there are more things to
10107 come. Don't do that when processing something after SUPER
10108 (we need more thing to be put in place below */
10109 if (!from_super && QUAL_RESOLUTION (q))
10111 decl = QUAL_RESOLUTION (q);
10112 if (!type)
10114 if (TREE_CODE (decl) == FIELD_DECL
10115 || TREE_CODE (decl) == VAR_DECL)
10117 if (TREE_CODE (decl) == FIELD_DECL
10118 && !FIELD_STATIC (decl))
10120 if (current_this)
10121 *where_found = current_this;
10122 else
10124 static_ref_err (qual_wfl, DECL_NAME (decl),
10125 current_class);
10126 return 1;
10129 else
10131 *where_found = TREE_TYPE (decl);
10132 if (TREE_CODE (*where_found) == POINTER_TYPE)
10133 *where_found = TREE_TYPE (*where_found);
10135 if (nested_member_access_p (current_class, decl))
10136 decl = build_nested_field_access (qual_wfl, decl);
10138 else
10140 *where_found = TREE_TYPE (decl);
10141 if (TREE_CODE (*where_found) == POINTER_TYPE)
10142 *where_found = TREE_TYPE (*where_found);
10147 /* Report and error if we're using a numerical literal as a
10148 qualifier. It can only be an INTEGER_CST. */
10149 else if (TREE_CODE (qual_wfl) == INTEGER_CST)
10151 parse_error_context
10152 (wfl, "Can't use type %qs as a qualifier",
10153 lang_printable_name (TREE_TYPE (qual_wfl), 0));
10154 return 1;
10157 /* We have to search for a field, knowing the type of its
10158 container. The flag FROM_TYPE indicates that we resolved
10159 the last member of the expression as a type name, which
10160 means that for the resolution of this field, we'll look
10161 for other errors than if it was resolved as a member of
10162 an other field. */
10163 else
10165 int is_static;
10166 tree field_decl_type; /* For layout */
10168 if (!from_type && !JREFERENCE_TYPE_P (type))
10170 parse_error_context
10171 (qual_wfl, "Attempt to reference field %qs in %<%s %s%>",
10172 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10173 lang_printable_name (type, 0),
10174 IDENTIFIER_POINTER (DECL_NAME (decl)));
10175 return 1;
10178 field_decl = lookup_field_wrapper (type,
10179 EXPR_WFL_NODE (qual_wfl));
10181 /* Maybe what we're trying to access to is an inner
10182 class, only if decl is a TYPE_DECL. */
10183 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
10185 tree ptr, inner_decl;
10187 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
10188 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
10189 if (inner_decl)
10191 check_inner_class_access (inner_decl, decl, qual_wfl);
10192 type = TREE_TYPE (inner_decl);
10193 decl = inner_decl;
10194 from_type = 1;
10195 continue;
10199 if (field_decl == NULL_TREE)
10201 parse_error_context
10202 (qual_wfl, "No variable %qs defined in type %qs",
10203 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10204 GET_TYPE_NAME (type));
10205 return 1;
10207 if (field_decl == error_mark_node)
10208 return 1;
10210 /* Layout the type of field_decl, since we may need
10211 it. Don't do primitive types or loaded classes. The
10212 situation of non primitive arrays may not handled
10213 properly here. FIXME */
10214 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10215 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
10216 else
10217 field_decl_type = TREE_TYPE (field_decl);
10218 if (!JPRIMITIVE_TYPE_P (field_decl_type)
10219 && !CLASS_LOADED_P (field_decl_type)
10220 && !TYPE_ARRAY_P (field_decl_type))
10221 resolve_and_layout (field_decl_type, NULL_TREE);
10223 /* Check on accessibility here */
10224 if (not_accessible_p (current_class, field_decl,
10225 *type_found, from_super))
10226 return not_accessible_field_error (qual_wfl,field_decl);
10227 check_deprecation (qual_wfl, field_decl);
10229 /* There are things to check when fields are accessed
10230 from type. There are no restrictions on a static
10231 declaration of the field when it is accessed from an
10232 interface */
10233 is_static = FIELD_STATIC (field_decl);
10234 if (!from_super && from_type
10235 && !TYPE_INTERFACE_P (type)
10236 && !is_static
10237 && (current_function_decl
10238 && METHOD_STATIC (current_function_decl)))
10240 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
10241 return 1;
10243 from_cast = from_super = 0;
10245 /* If it's an access from a type but isn't static, we
10246 make it relative to `this'. */
10247 if (!is_static && from_type)
10248 decl = current_this;
10250 /* If we need to generate something to get a proper
10251 handle on what this field is accessed from, do it
10252 now. */
10253 if (!is_static)
10255 decl = maybe_access_field (decl, *where_found, *type_found);
10256 if (decl == error_mark_node)
10257 return 1;
10260 /* We want to keep the location where we found it, and the
10261 type we found. */
10262 *where_found = decl;
10263 *type_found = type;
10265 /* Generate the correct expression for field access from
10266 qualified this */
10267 if (from_qualified_this)
10269 field_decl
10270 = build_nested_field_access (qual_wfl, field_decl);
10271 from_qualified_this = 0;
10274 /* If needed, generate accessors for static field access. */
10275 if (is_static
10276 && FIELD_PRIVATE (field_decl)
10277 && flag_emit_class_files
10278 && nested_member_access_p (current_class, field_decl))
10279 field_decl = build_nested_field_access (qual_wfl, field_decl);
10281 /* This is the decl found and eventually the next one to
10282 search from */
10283 decl = field_decl;
10285 from_type = 0;
10286 type = QUAL_DECL_TYPE (decl);
10288 /* Sneak preview. If decl is qualified by a `new', report
10289 the error here to be accurate on the peculiar construct */
10290 if (TREE_CHAIN (q)
10291 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
10292 && !JREFERENCE_TYPE_P (type))
10294 parse_error_context (qual_wfl, "Attempt to reference field %<new%> in a %qs",
10295 lang_printable_name (type, 0));
10296 return 1;
10299 /* `q' might have changed due to a after package resolution
10300 re-qualification */
10301 if (!q)
10302 break;
10304 *found_decl = decl;
10305 return 0;
10308 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
10309 can't be accessed from REFERENCE (a record type). If MEMBER
10310 features a protected access, we then use WHERE which, if non null,
10311 holds the type of MEMBER's access that is checked against
10312 6.6.2.1. This function should be used when decl is a field or a
10313 method. */
10315 static int
10316 not_accessible_p (tree reference, tree member, tree where, int from_super)
10318 int access_flag = get_access_flags_from_decl (member);
10319 bool is_static = false;
10321 if (TREE_CODE (member) == FIELD_DECL ||
10322 TREE_CODE (member) == VAR_DECL)
10323 is_static = FIELD_STATIC (member);
10324 else
10325 is_static = METHOD_STATIC (member);
10327 /* Access always granted for members declared public */
10328 if (access_flag & ACC_PUBLIC)
10329 return 0;
10331 /* Check access on protected members */
10332 if (access_flag & ACC_PROTECTED)
10334 /* Access granted if it occurs from within the package
10335 containing the class in which the protected member is
10336 declared */
10337 if (class_in_current_package (DECL_CONTEXT (member)))
10338 return 0;
10340 /* If accessed with the form `super.member', then access is granted */
10341 if (from_super)
10342 return 0;
10344 /* If WHERE is active, access was made through a qualifier. For
10345 non-static members, access is granted if the type of the qualifier
10346 is or is a sublass of the type the access is made from (6.6.2.1.) */
10347 if (where && !is_static)
10349 while (reference)
10351 if (inherits_from_p (where, reference))
10352 return 0;
10353 if (INNER_CLASS_TYPE_P (reference))
10354 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10355 else
10356 break;
10358 return 1;
10361 /* Otherwise, access is granted if occurring from within the class
10362 where member is declared, or a subclass of it. */
10363 while (reference)
10365 if (inherits_from_p (reference, DECL_CONTEXT (member)))
10366 return 0;
10367 if (INNER_CLASS_TYPE_P (reference))
10368 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10369 else
10370 break;
10372 return 1;
10375 /* Check access on private members. Access is granted only if it
10376 occurs from within the class in which it is declared -- that does
10377 it for innerclasses too. */
10378 if (access_flag & ACC_PRIVATE)
10380 if (reference == DECL_CONTEXT (member) ||
10381 common_enclosing_context_p (DECL_CONTEXT (member), reference))
10382 return 0;
10383 return 1;
10386 /* Default access is permitted only when occurring from within the
10387 package in which the context (MEMBER) is declared. */
10388 return !class_in_current_package (DECL_CONTEXT (member));
10391 /* Test deprecated decl access. */
10392 static void
10393 check_deprecation (tree wfl, tree decl)
10395 const char *file;
10396 tree elt;
10398 if (! warn_deprecated)
10399 return;
10401 /* We want to look at the element type of arrays here, so we strip
10402 all surrounding array types. */
10403 if (TYPE_ARRAY_P (TREE_TYPE (decl)))
10405 elt = TREE_TYPE (decl);
10406 while (TYPE_ARRAY_P (elt))
10407 elt = TYPE_ARRAY_ELEMENT (elt);
10408 /* We'll end up with a pointer type, so we use TREE_TYPE to go
10409 to the record. */
10410 decl = TYPE_NAME (TREE_TYPE (elt));
10412 file = DECL_SOURCE_FILE (decl);
10414 /* Complain if the field is deprecated and the file it was defined
10415 in isn't compiled at the same time the file which contains its
10416 use is */
10417 if (DECL_DEPRECATED (decl)
10418 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10420 const char *the;
10421 switch (TREE_CODE (decl))
10423 case FUNCTION_DECL:
10424 the = "method";
10425 break;
10426 case FIELD_DECL:
10427 case VAR_DECL:
10428 the = "field";
10429 break;
10430 case TYPE_DECL:
10431 parse_warning_context (wfl, "The class %qs has been deprecated",
10432 IDENTIFIER_POINTER (DECL_NAME (decl)));
10433 return;
10434 default:
10435 abort ();
10437 /* Don't issue a message if the context as been deprecated as a
10438 whole. */
10439 if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10440 parse_warning_context
10441 (wfl, "The %s %qs in class %qs has been deprecated",
10442 the, lang_printable_name (decl, 0),
10443 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10447 /* Returns 1 if class was declared in the current package, 0 otherwise */
10449 static GTY(()) tree cicp_cache;
10450 static int
10451 class_in_current_package (tree class)
10453 int qualified_flag;
10454 tree left;
10456 if (cicp_cache == class)
10457 return 1;
10459 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10461 /* If the current package is empty and the name of CLASS is
10462 qualified, class isn't in the current package. If there is a
10463 current package and the name of the CLASS is not qualified, class
10464 isn't in the current package */
10465 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10466 return 0;
10468 /* If there is not package and the name of CLASS isn't qualified,
10469 they belong to the same unnamed package */
10470 if (!ctxp->package && !qualified_flag)
10471 return 1;
10473 /* Compare the left part of the name of CLASS with the package name */
10474 split_qualified_name (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10475 if (ctxp->package == left)
10477 cicp_cache = class;
10478 return 1;
10480 return 0;
10483 /* This function may generate code to access DECL from WHERE. This is
10484 done only if certain conditions meet. */
10486 static tree
10487 maybe_access_field (tree decl, tree where, tree type)
10489 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10490 && !FIELD_STATIC (decl))
10491 decl = build_field_ref (where ? where : current_this,
10492 (type ? type : DECL_CONTEXT (decl)),
10493 DECL_NAME (decl));
10494 return decl;
10497 /* Build a method invocation, by patching PATCH. If non NULL
10498 and according to the situation, PRIMARY and WHERE may be
10499 used. IS_STATIC is set to 1 if the invoked function is static. */
10501 static tree
10502 patch_method_invocation (tree patch, tree primary, tree where, int from_super,
10503 int *is_static, tree *ret_decl)
10505 tree wfl = TREE_OPERAND (patch, 0);
10506 tree args = TREE_OPERAND (patch, 1);
10507 tree name = EXPR_WFL_NODE (wfl);
10508 tree list;
10509 int is_static_flag = 0;
10510 int is_super_init = 0;
10511 tree this_arg = NULL_TREE;
10512 int is_array_clone_call = 0;
10514 /* Should be overridden if everything goes well. Otherwise, if
10515 something fails, it should keep this value. It stop the
10516 evaluation of a bogus assignment. See java_complete_tree,
10517 MODIFY_EXPR: for the reasons why we sometimes want to keep on
10518 evaluating an assignment */
10519 TREE_TYPE (patch) = error_mark_node;
10521 /* Since lookup functions are messing with line numbers, save the
10522 context now. */
10523 java_parser_context_save_global ();
10525 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10527 /* Resolution of qualified name, excluding constructors */
10528 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10530 tree identifier, identifier_wfl, type, resolved;
10531 /* Extract the last IDENTIFIER of the qualified
10532 expression. This is a wfl and we will use it's location
10533 data during error report. */
10534 identifier_wfl = cut_identifier_in_qualified (wfl);
10535 identifier = EXPR_WFL_NODE (identifier_wfl);
10537 /* Given the context, IDENTIFIER is syntactically qualified
10538 as a MethodName. We need to qualify what's before */
10539 qualify_ambiguous_name (wfl);
10540 resolved = resolve_field_access (wfl, NULL, NULL);
10542 if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10543 && FIELD_FINAL (resolved)
10544 && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10545 && !flag_emit_class_files)
10546 resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10548 if (resolved == error_mark_node)
10549 PATCH_METHOD_RETURN_ERROR ();
10551 type = GET_SKIP_TYPE (resolved);
10552 resolve_and_layout (type, NULL_TREE);
10554 if (JPRIMITIVE_TYPE_P (type))
10556 parse_error_context
10557 (identifier_wfl,
10558 "Can't invoke a method on primitive type %qs",
10559 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10560 PATCH_METHOD_RETURN_ERROR ();
10563 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10564 args = nreverse (args);
10566 /* We're resolving a call from a type */
10567 if (TREE_CODE (resolved) == TYPE_DECL)
10569 if (CLASS_INTERFACE (resolved))
10571 parse_error_context
10572 (identifier_wfl,
10573 "Can't make static reference to method %qs in interface %qs",
10574 IDENTIFIER_POINTER (identifier),
10575 IDENTIFIER_POINTER (name));
10576 PATCH_METHOD_RETURN_ERROR ();
10578 if (list)
10580 if (METHOD_STATIC (list))
10581 maybe_use_access_method (0, &list, NULL);
10582 else
10584 char *fct_name = xstrdup (lang_printable_name (list, 2));
10585 parse_error_context
10586 (identifier_wfl,
10587 "Can't make static reference to method %<%s %s%> in class %qs",
10588 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10589 fct_name,
10590 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10591 free (fct_name);
10592 PATCH_METHOD_RETURN_ERROR ();
10596 else
10597 this_arg = primary = resolved;
10599 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10600 is_array_clone_call = 1;
10602 /* IDENTIFIER_WFL will be used to report any problem further */
10603 wfl = identifier_wfl;
10605 /* Resolution of simple names, names generated after a primary: or
10606 constructors */
10607 else
10609 tree class_to_search = NULL_TREE;
10610 int lc; /* Looking for Constructor */
10612 /* We search constructor in their target class */
10613 if (CALL_CONSTRUCTOR_P (patch))
10615 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10616 class_to_search = EXPR_WFL_NODE (wfl);
10617 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10618 this_identifier_node)
10619 class_to_search = NULL_TREE;
10620 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10621 super_identifier_node)
10623 is_super_init = 1;
10624 if (CLASSTYPE_SUPER (current_class))
10625 class_to_search =
10626 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10627 else
10629 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10630 PATCH_METHOD_RETURN_ERROR ();
10634 /* Class to search is NULL if we're searching the current one */
10635 if (class_to_search)
10637 class_to_search = resolve_and_layout (class_to_search, wfl);
10639 if (!class_to_search)
10641 parse_error_context
10642 (wfl, "Class %qs not found in type declaration",
10643 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10644 PATCH_METHOD_RETURN_ERROR ();
10647 /* Can't instantiate an abstract class, but we can
10648 invoke it's constructor. It's use within the `new'
10649 context is denied here. */
10650 if (CLASS_ABSTRACT (class_to_search)
10651 && TREE_CODE (patch) == NEW_CLASS_EXPR)
10653 parse_error_context
10654 (wfl, "Class %qs is an abstract class. It can't be instantiated",
10655 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10656 PATCH_METHOD_RETURN_ERROR ();
10659 class_to_search = TREE_TYPE (class_to_search);
10661 else
10662 class_to_search = current_class;
10663 lc = 1;
10665 /* This is a regular search in the local class, unless an
10666 alternate class is specified. */
10667 else
10669 if (where != NULL_TREE)
10670 class_to_search = where;
10671 else if (QUALIFIED_P (name))
10672 class_to_search = current_class;
10673 else
10675 class_to_search = current_class;
10677 for (;;)
10679 if (has_method (class_to_search, name))
10680 break;
10681 if (! INNER_CLASS_TYPE_P (class_to_search))
10683 parse_error_context (wfl,
10684 "No method named %qs in scope",
10685 IDENTIFIER_POINTER (name));
10686 PATCH_METHOD_RETURN_ERROR ();
10688 class_to_search
10689 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10692 lc = 0;
10695 /* NAME is a simple identifier or comes from a primary. Search
10696 in the class whose declaration contain the method being
10697 invoked. */
10698 resolve_and_layout (class_to_search, NULL_TREE);
10700 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10701 /* Don't continue if no method were found, as the next statement
10702 can't be executed then. */
10703 if (!list)
10704 PATCH_METHOD_RETURN_ERROR ();
10706 if (TYPE_ARRAY_P (class_to_search)
10707 && DECL_NAME (list) == get_identifier ("clone"))
10708 is_array_clone_call = 1;
10710 /* Check for static reference of non static methods. */
10711 if (check_for_static_method_reference (wfl, patch, list,
10712 class_to_search, primary))
10713 PATCH_METHOD_RETURN_ERROR ();
10715 /* Check for inner classes creation from illegal contexts */
10716 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10717 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10718 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10719 && !DECL_INIT_P (current_function_decl))
10721 parse_error_context
10722 (wfl, "No enclosing instance for inner class %qs is in scope%s",
10723 lang_printable_name (class_to_search, 0),
10724 (!current_this ? "" :
10725 "; an explicit one must be provided when creating this inner class"));
10726 PATCH_METHOD_RETURN_ERROR ();
10729 /* Non static methods are called with the current object extra
10730 argument. If PATCH is a `new TYPE()', the argument is the value
10731 returned by the object allocator. If method is resolved as a
10732 primary, use the primary otherwise use the current THIS. */
10733 args = nreverse (args);
10734 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10736 this_arg = primary ? primary : current_this;
10738 /* If we're using an access method, things are different.
10739 There are two family of cases:
10741 1) We're not generating bytecodes:
10743 - LIST is non-static. Its invocation is transformed from
10744 x(a1,...,an) into this$<n>.x(a1,....an).
10745 - LIST is static. Its invocation is transformed from
10746 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10748 2) We're generating bytecodes:
10750 - LIST is non-static. Its invocation is transformed from
10751 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10752 - LIST is static. Its invocation is transformed from
10753 x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10755 Of course, this$<n> can be arbitrarily complex, ranging from
10756 this$0 (the immediate outer context) to
10757 access$0(access$0(...(this$0))).
10759 maybe_use_access_method returns a nonzero value if the
10760 this_arg has to be moved into the (then generated) stub
10761 argument list. In the meantime, the selected function
10762 might have been replaced by a generated stub. */
10763 if (METHOD_STATIC (list))
10764 maybe_use_access_method (0, &list, NULL);
10765 else if (!primary &&
10766 maybe_use_access_method (is_super_init, &list, &this_arg))
10768 args = tree_cons (NULL_TREE, this_arg, args);
10769 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10774 /* Merge point of all resolution schemes. If we have nothing, this
10775 is an error, already signaled */
10776 if (!list)
10777 PATCH_METHOD_RETURN_ERROR ();
10779 /* Check accessibility, position the is_static flag, build and
10780 return the call */
10781 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10782 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10783 NULL_TREE), from_super)
10784 /* Calls to clone() on array types are permitted as a special-case. */
10785 && !is_array_clone_call)
10787 const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10788 const char *const access =
10789 accessibility_string (get_access_flags_from_decl (list));
10790 const char *const klass =
10791 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10792 const char *const refklass =
10793 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10794 const char *const what = (DECL_CONSTRUCTOR_P (list)
10795 ? "constructor" : "method");
10796 parse_error_context (wfl,
10797 "Can't access %s %s %<%s.%s%> from %qs",
10798 access, what, klass, fct_name, refklass);
10799 PATCH_METHOD_RETURN_ERROR ();
10802 /* Deprecation check: check whether the method being invoked or the
10803 instance-being-created's type are deprecated. */
10804 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10805 check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10806 check_deprecation (wfl, list);
10808 /* If invoking a innerclass constructor, there are hidden parameters
10809 to pass */
10810 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10811 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10813 /* And make sure we add the accessed local variables to be saved
10814 in field aliases. */
10815 args = build_alias_initializer_parameter_list
10816 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10818 /* Secretly pass the current_this/primary as a second argument */
10819 if (primary || current_this)
10821 tree extra_arg;
10822 tree this_type = (current_this ?
10823 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10824 /* Method's (list) enclosing context */
10825 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10826 /* If we have a primary, use it. */
10827 if (primary)
10828 extra_arg = primary;
10829 /* The current `this' is an inner class but isn't a direct
10830 enclosing context for the inner class we're trying to
10831 create. Build an access to the proper enclosing context
10832 and use it. */
10833 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10834 && this_type != TREE_TYPE (mec))
10837 extra_arg = build_access_to_thisn (current_class,
10838 TREE_TYPE (mec), 0);
10839 extra_arg = java_complete_tree (extra_arg);
10841 /* Otherwise, just use the current `this' as an enclosing
10842 context. */
10843 else
10844 extra_arg = current_this;
10845 args = tree_cons (NULL_TREE, extra_arg, args);
10847 else
10848 args = tree_cons (NULL_TREE, integer_zero_node, args);
10851 /* This handles the situation where a constructor invocation needs
10852 to have an enclosing context passed as a second parameter (the
10853 constructor is one of an inner class). */
10854 if ((is_super_init ||
10855 (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10856 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10858 tree dest = TYPE_NAME (DECL_CONTEXT (list));
10859 tree extra_arg =
10860 build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10861 extra_arg = java_complete_tree (extra_arg);
10862 args = tree_cons (NULL_TREE, extra_arg, args);
10865 is_static_flag = METHOD_STATIC (list);
10866 if (! is_static_flag && this_arg != NULL_TREE)
10867 args = tree_cons (NULL_TREE, this_arg, args);
10869 /* In the context of an explicit constructor invocation, we can't
10870 invoke any method relying on `this'. Exceptions are: we're
10871 invoking a static function, primary exists and is not the current
10872 this, we're creating a new object. */
10873 if (ctxp->explicit_constructor_p
10874 && !is_static_flag
10875 && (!primary || primary == current_this)
10876 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10878 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
10879 PATCH_METHOD_RETURN_ERROR ();
10881 java_parser_context_restore_global ();
10882 if (is_static)
10883 *is_static = is_static_flag;
10884 /* Sometimes, we want the decl of the selected method. Such as for
10885 EH checking */
10886 if (ret_decl)
10887 *ret_decl = list;
10888 patch = patch_invoke (patch, list, args);
10890 /* Now is a good time to insert the call to finit$ */
10891 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10893 tree finit_parms, finit_call;
10895 /* Prepare to pass hidden parameters to finit$, if any. */
10896 finit_parms = build_alias_initializer_parameter_list
10897 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10899 finit_call =
10900 build_method_invocation (build_wfl_node (finit_identifier_node),
10901 finit_parms);
10903 /* Generate the code used to initialize fields declared with an
10904 initialization statement and build a compound statement along
10905 with the super constructor invocation. */
10906 CAN_COMPLETE_NORMALLY (patch) = 1;
10907 patch = build2 (COMPOUND_EXPR, void_type_node, patch,
10908 java_complete_tree (finit_call));
10910 return patch;
10913 /* Check that we're not trying to do a static reference to a method in
10914 non static method. Return 1 if it's the case, 0 otherwise. */
10916 static int
10917 check_for_static_method_reference (tree wfl, tree node, tree method,
10918 tree where, tree primary)
10920 if (METHOD_STATIC (current_function_decl)
10921 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10923 char *fct_name = xstrdup (lang_printable_name (method, 0));
10924 parse_error_context
10925 (wfl, "Can't make static reference to method %<%s %s%> in class %qs",
10926 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10927 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10928 free (fct_name);
10929 return 1;
10931 return 0;
10934 /* Fix the invocation of *MDECL if necessary in the case of an
10935 invocation across a nested class. *THIS_ARG might be modified
10936 appropriately and an alternative access to *MDECL might be
10937 returned. */
10939 static int
10940 maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10942 tree ctx;
10943 tree md = *mdecl, ta = NULL_TREE;
10944 int to_return = 0;
10945 int non_static_context = !METHOD_STATIC (md);
10947 if (is_super_init
10948 || DECL_FINIT_P (md)
10949 || DECL_INSTINIT_P (md)
10950 || !nested_member_access_p (current_class, md))
10951 return 0;
10953 /* If we're calling a method found in an enclosing class, generate
10954 what it takes to retrieve the right `this'. Don't do that if we're
10955 invoking a static method. Note that if MD's type is unrelated to
10956 CURRENT_CLASS, then the current this can be used. */
10958 if (non_static_context
10959 && !inherits_from_p (current_class, DECL_CONTEXT (md))
10960 && DECL_CONTEXT (TYPE_NAME (current_class)))
10962 ta = *this_arg;
10963 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10964 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10966 ta = build_current_thisn (current_class);
10967 ta = build_wfl_node (ta);
10969 else
10971 tree type = ctx;
10972 while (type)
10974 maybe_build_thisn_access_method (type);
10975 if (inherits_from_p (type, DECL_CONTEXT (md)))
10977 ta = build_access_to_thisn (ctx, type, 0);
10978 break;
10980 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10981 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10984 ta = java_complete_tree (ta);
10987 /* We might have to use an access method to get to MD. We can
10988 break the method access rule as long as we're not generating
10989 bytecode. */
10990 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10992 md = build_nested_method_access_method (md);
10993 to_return = 1;
10996 *mdecl = md;
10997 if (this_arg)
10998 *this_arg = ta;
11000 /* Returning a nonzero value indicates we were doing a non static
11001 method invocation that is now a static invocation. It will have
11002 callee displace `this' to insert it in the regular argument
11003 list. */
11004 return (non_static_context && to_return);
11007 /* Patch an invoke expression METHOD and ARGS, based on its invocation
11008 mode. */
11010 static tree
11011 patch_invoke (tree patch, tree method, tree args)
11013 tree dtable, func;
11014 tree original_call, t, ta;
11015 tree check = NULL_TREE;
11017 /* Last step for args: convert build-in types. If we're dealing with
11018 a new TYPE() type call, the first argument to the constructor
11019 isn't found in the incoming argument list, but delivered by
11020 `new' */
11021 t = TYPE_ARG_TYPES (TREE_TYPE (method));
11022 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
11023 t = TREE_CHAIN (t);
11024 for (ta = args; t != end_params_node && ta;
11025 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
11026 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
11027 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
11028 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
11030 /* Resolve unresolved returned type issues */
11031 t = TREE_TYPE (TREE_TYPE (method));
11032 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
11033 resolve_and_layout (TREE_TYPE (t), NULL);
11035 if (flag_emit_class_files)
11036 func = method;
11037 else
11039 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
11041 case INVOKE_VIRTUAL:
11042 dtable = invoke_build_dtable (0, args);
11043 func = build_invokevirtual (dtable, method);
11044 break;
11046 case INVOKE_NONVIRTUAL:
11047 /* If the object for the method call is null, we throw an
11048 exception. We don't do this if the object is the current
11049 method's `this'. In other cases we just rely on an
11050 optimization pass to eliminate redundant checks. */
11051 if (TREE_VALUE (args) != current_this)
11053 /* We use a save_expr here to make sure we only evaluate
11054 the new `self' expression once. */
11055 tree save_arg = save_expr (TREE_VALUE (args));
11056 TREE_VALUE (args) = save_arg;
11057 check = java_check_reference (save_arg, 1);
11059 /* Fall through. */
11061 case INVOKE_SUPER:
11062 case INVOKE_STATIC:
11064 tree signature = build_java_signature (TREE_TYPE (method));
11065 func = build_known_method_ref (method, TREE_TYPE (method),
11066 DECL_CONTEXT (method),
11067 signature, args);
11069 break;
11071 case INVOKE_INTERFACE:
11072 dtable = invoke_build_dtable (1, args);
11073 func = build_invokeinterface (dtable, method);
11074 break;
11076 default:
11077 abort ();
11080 /* Ensure self_type is initialized, (invokestatic). FIXME */
11081 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
11084 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
11085 TREE_OPERAND (patch, 0) = func;
11086 TREE_OPERAND (patch, 1) = args;
11087 patch = check_for_builtin (method, patch);
11088 original_call = patch;
11090 /* We're processing a `new TYPE ()' form. New is called and its
11091 returned value is the first argument to the constructor. We build
11092 a COMPOUND_EXPR and use saved expression so that the overall NEW
11093 expression value is a pointer to a newly created and initialized
11094 class. */
11095 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
11097 tree class = DECL_CONTEXT (method);
11098 tree c1, saved_new, new;
11099 tree alloc_node;
11101 if (flag_emit_class_files)
11103 TREE_TYPE (patch) = build_pointer_type (class);
11104 return patch;
11106 if (!TYPE_SIZE (class))
11107 safe_layout_class (class);
11108 alloc_node =
11109 (class_has_finalize_method (class) ? alloc_object_node
11110 : alloc_no_finalizer_node);
11111 new = build3 (CALL_EXPR, promote_type (class),
11112 build_address_of (alloc_node),
11113 build_tree_list (NULL_TREE, build_class_ref (class)),
11114 NULL_TREE);
11115 saved_new = save_expr (new);
11116 c1 = build_tree_list (NULL_TREE, saved_new);
11117 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
11118 TREE_OPERAND (original_call, 1) = c1;
11119 TREE_SET_CODE (original_call, CALL_EXPR);
11120 patch = build2 (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
11123 /* If CHECK is set, then we are building a check to see if the object
11124 is NULL. */
11125 if (check != NULL_TREE)
11127 /* We have to call force_evaluation_order now because creating a
11128 COMPOUND_EXPR wraps the arg list in a way that makes it
11129 unrecognizable by force_evaluation_order later. Yuk. */
11130 patch = build2 (COMPOUND_EXPR, TREE_TYPE (patch), check,
11131 force_evaluation_order (patch));
11132 TREE_SIDE_EFFECTS (patch) = 1;
11135 /* In order to be able to modify PATCH later, we SAVE_EXPR it and
11136 put it as the first expression of a COMPOUND_EXPR. The second
11137 expression being an empty statement to be later patched if
11138 necessary. We remember a TREE_LIST (the PURPOSE is the method,
11139 the VALUE is the compound) in a hashtable and return a
11140 COMPOUND_EXPR built so that the result of the evaluation of the
11141 original PATCH node is returned. */
11142 if (STATIC_CLASS_INIT_OPT_P ()
11143 && current_function_decl && METHOD_STATIC (method))
11145 tree list;
11146 tree fndecl = current_function_decl;
11147 /* We have to call force_evaluation_order now because creating a
11148 COMPOUND_EXPR wraps the arg list in a way that makes it
11149 unrecognizable by force_evaluation_order later. Yuk. */
11150 tree save = force_evaluation_order (patch);
11151 tree type = TREE_TYPE (patch);
11153 patch = build2 (COMPOUND_EXPR, type, save, build_java_empty_stmt ());
11154 list = tree_cons (method, patch,
11155 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
11157 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
11159 patch = build2 (COMPOUND_EXPR, type, patch, save);
11162 return patch;
11165 static int
11166 invocation_mode (tree method, int super)
11168 int access = get_access_flags_from_decl (method);
11170 if (super)
11171 return INVOKE_SUPER;
11173 if (access & ACC_STATIC)
11174 return INVOKE_STATIC;
11176 /* We have to look for a constructor before we handle nonvirtual
11177 calls; otherwise the constructor will look nonvirtual. */
11178 if (DECL_CONSTRUCTOR_P (method))
11179 return INVOKE_STATIC;
11181 if (access & ACC_PRIVATE)
11182 return INVOKE_NONVIRTUAL;
11184 /* Binary compatibility: just because it's final today, that doesn't
11185 mean it'll be final tomorrow. */
11186 if (! flag_indirect_dispatch
11187 || DECL_CONTEXT (method) == object_type_node)
11189 if (access & ACC_FINAL)
11190 return INVOKE_NONVIRTUAL;
11192 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
11193 return INVOKE_NONVIRTUAL;
11196 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
11197 return INVOKE_INTERFACE;
11199 return INVOKE_VIRTUAL;
11202 /* Retrieve a refined list of matching methods. It covers the step
11203 15.11.2 (Compile-Time Step 2) */
11205 static tree
11206 lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
11208 tree atl = end_params_node; /* Arg Type List */
11209 tree method, signature, list, node;
11210 const char *candidates; /* Used for error report */
11211 char *dup;
11213 /* Fix the arguments */
11214 for (node = arg_list; node; node = TREE_CHAIN (node))
11216 tree current_arg = TREE_TYPE (TREE_VALUE (node));
11217 /* Non primitive type may have to be resolved */
11218 if (!JPRIMITIVE_TYPE_P (current_arg))
11219 resolve_and_layout (current_arg, NULL_TREE);
11220 /* And promoted */
11221 if (TREE_CODE (current_arg) == RECORD_TYPE)
11222 current_arg = promote_type (current_arg);
11223 atl = tree_cons (NULL_TREE, current_arg, atl);
11226 /* Presto. If we're dealing with an anonymous class and a
11227 constructor call, generate the right constructor now, since we
11228 know the arguments' types. */
11230 if (lc && ANONYMOUS_CLASS_P (class))
11232 tree mdecl = craft_constructor (TYPE_NAME (class), atl);
11233 /* The anonymous class may have already been laid out, so make sure
11234 the new constructor is laid out here. */
11235 layout_class_method (class, CLASSTYPE_SUPER (class), mdecl, NULL_TREE);
11238 /* Find all candidates and then refine the list, searching for the
11239 most specific method. */
11240 list = find_applicable_accessible_methods_list (lc, class, name, atl);
11241 list = find_most_specific_methods_list (list);
11242 if (list && !TREE_CHAIN (list))
11243 return TREE_VALUE (list);
11245 /* Issue an error. List candidates if any. Candidates are listed
11246 only if accessible (non accessible methods may end-up here for
11247 the sake of a better error report). */
11248 candidates = NULL;
11249 if (list)
11251 tree current;
11252 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
11253 for (current = list; current; current = TREE_CHAIN (current))
11255 tree cm = TREE_VALUE (current);
11256 char string [4096];
11257 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
11258 continue;
11259 sprintf
11260 (string, " '%s' in '%s'%s",
11261 get_printable_method_name (cm),
11262 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
11263 (TREE_CHAIN (current) ? "\n" : ""));
11264 obstack_grow (&temporary_obstack, string, strlen (string));
11266 obstack_1grow (&temporary_obstack, '\0');
11267 candidates = obstack_finish (&temporary_obstack);
11269 /* Issue the error message */
11270 method = make_node (FUNCTION_TYPE);
11271 TYPE_ARG_TYPES (method) = atl;
11272 signature = build_java_argument_signature (method);
11273 dup = xstrdup (lang_printable_name (class, 0));
11274 parse_error_context (cl, "Can't find %s %<%s(%s)%> in type %qs%s",
11275 (lc ? "constructor" : "method"),
11276 (lc ? dup : IDENTIFIER_POINTER (name)),
11277 IDENTIFIER_POINTER (signature), dup,
11278 (candidates ? candidates : ""));
11279 free (dup);
11280 return NULL_TREE;
11283 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
11284 when we're looking for a constructor. */
11286 static tree
11287 find_applicable_accessible_methods_list (int lc, tree class, tree name,
11288 tree arglist)
11290 static htab_t searched_classes;
11291 static int search_not_done = 0;
11292 tree list = NULL_TREE, all_list = NULL_TREE;
11293 tree base_binfo;
11294 int i;
11296 /* Check the hash table to determine if this class has been searched
11297 already. */
11298 if (searched_classes)
11300 if (htab_find (searched_classes, class) != NULL)
11301 return NULL;
11303 else
11305 searched_classes = htab_create (10, htab_hash_pointer,
11306 htab_eq_pointer, NULL);
11309 search_not_done++;
11310 *htab_find_slot (searched_classes, class, INSERT) = class;
11312 if (!CLASS_LOADED_P (class))
11314 load_class (class, 1);
11315 safe_layout_class (class);
11318 /* Search interfaces */
11319 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
11320 && CLASS_INTERFACE (TYPE_NAME (class)))
11322 search_applicable_methods_list (lc, TYPE_METHODS (class),
11323 name, arglist, &list, &all_list);
11324 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11326 tree t = BINFO_TYPE (base_binfo);
11327 tree rlist;
11329 rlist = find_applicable_accessible_methods_list (lc, t, name,
11330 arglist);
11331 list = chainon (rlist, list);
11334 /* Search classes */
11335 else
11337 search_applicable_methods_list (lc, TYPE_METHODS (class),
11338 name, arglist, &list, &all_list);
11340 /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11341 that we only search in class. Note that we should have found
11342 something at this point. */
11343 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11345 lc = 1;
11346 if (!list)
11347 abort ();
11350 /* We must search all interfaces of this class */
11351 if (!lc)
11353 for (i = 1;
11354 BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11356 tree t = BINFO_TYPE (base_binfo);
11357 if (t != object_type_node)
11359 tree rlist
11360 = find_applicable_accessible_methods_list (lc, t,
11361 name, arglist);
11362 list = chainon (rlist, list);
11367 /* Search superclass */
11368 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11370 tree rlist;
11371 class = CLASSTYPE_SUPER (class);
11372 rlist = find_applicable_accessible_methods_list (lc, class,
11373 name, arglist);
11374 list = chainon (rlist, list);
11378 search_not_done--;
11380 /* We're done. Reset the searched classes list and finally search
11381 java.lang.Object if it wasn't searched already. */
11382 if (!search_not_done)
11384 if (!lc
11385 && TYPE_METHODS (object_type_node)
11386 && htab_find (searched_classes, object_type_node) == NULL)
11388 search_applicable_methods_list (lc,
11389 TYPE_METHODS (object_type_node),
11390 name, arglist, &list, &all_list);
11392 htab_delete (searched_classes);
11393 searched_classes = NULL;
11396 /* Either return the list obtained or all selected (but
11397 inaccessible) methods for better error report. */
11398 return (!list ? all_list : list);
11401 /* Effectively search for the appropriate method in method */
11403 static void
11404 search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
11405 tree *list, tree *all_list)
11407 for (; method; method = TREE_CHAIN (method))
11409 /* When dealing with constructor, stop here, otherwise search
11410 other classes */
11411 if (lc && !DECL_CONSTRUCTOR_P (method))
11412 continue;
11413 else if (!lc && (DECL_CONSTRUCTOR_P (method)
11414 || (DECL_NAME (method) != name)))
11415 continue;
11417 if (argument_types_convertible (method, arglist))
11419 /* Retain accessible methods only */
11420 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11421 method, NULL_TREE, 0))
11422 *list = tree_cons (NULL_TREE, method, *list);
11423 else
11424 /* Also retain all selected method here */
11425 *all_list = tree_cons (NULL_TREE, method, *list);
11430 /* 15.11.2.2 Choose the Most Specific Method */
11432 static tree
11433 find_most_specific_methods_list (tree list)
11435 int max = 0;
11436 int abstract, candidates;
11437 tree current, new_list = NULL_TREE;
11438 for (current = list; current; current = TREE_CHAIN (current))
11440 tree method;
11441 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11443 for (method = list; method; method = TREE_CHAIN (method))
11445 tree method_v, current_v;
11446 /* Don't test a method against itself */
11447 if (method == current)
11448 continue;
11450 method_v = TREE_VALUE (method);
11451 current_v = TREE_VALUE (current);
11453 /* Compare arguments and location where methods where declared */
11454 if (argument_types_convertible (method_v, current_v))
11456 if (valid_method_invocation_conversion_p
11457 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v)))
11459 int v = (DECL_SPECIFIC_COUNT (current_v) += 1);
11460 max = (v > max ? v : max);
11466 /* Review the list and select the maximally specific methods */
11467 for (current = list, abstract = -1, candidates = -1;
11468 current; current = TREE_CHAIN (current))
11469 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11471 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11472 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11473 candidates++;
11476 /* If we have several and they're all abstract, just pick the
11477 closest one. */
11478 if (candidates > 0 && candidates == abstract)
11480 /* FIXME: merge the throws clauses. There is no convenient way
11481 to do this in gcj right now, since ideally we'd like to
11482 introduce a new METHOD_DECL here, but that is really not
11483 possible. */
11484 new_list = nreverse (new_list);
11485 TREE_CHAIN (new_list) = NULL_TREE;
11486 return new_list;
11489 /* We have several (we couldn't find a most specific), all but one
11490 are abstract, we pick the only non abstract one. */
11491 if (candidates > 0 && (candidates == abstract+1))
11493 for (current = new_list; current; current = TREE_CHAIN (current))
11494 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11496 TREE_CHAIN (current) = NULL_TREE;
11497 new_list = current;
11501 /* If we can't find one, lower expectations and try to gather multiple
11502 maximally specific methods */
11503 while (!new_list && max)
11505 while (--max > 0)
11507 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11508 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11512 return new_list;
11515 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11516 converted by method invocation conversion (5.3) to the type of the
11517 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11518 to change less often than M1. */
11520 static GTY(()) tree m2_arg_value;
11521 static GTY(()) tree m2_arg_cache;
11523 static int
11524 argument_types_convertible (tree m1, tree m2_or_arglist)
11526 tree m1_arg, m2_arg;
11528 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11530 if (m2_arg_value == m2_or_arglist)
11531 m2_arg = m2_arg_cache;
11532 else
11534 /* M2_OR_ARGLIST can be a function DECL or a raw list of
11535 argument types */
11536 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11538 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11539 if (!METHOD_STATIC (m2_or_arglist))
11540 m2_arg = TREE_CHAIN (m2_arg);
11542 else
11543 m2_arg = m2_or_arglist;
11545 m2_arg_value = m2_or_arglist;
11546 m2_arg_cache = m2_arg;
11549 while (m1_arg != end_params_node && m2_arg != end_params_node)
11551 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11552 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11553 TREE_VALUE (m2_arg)))
11554 break;
11555 m1_arg = TREE_CHAIN (m1_arg);
11556 m2_arg = TREE_CHAIN (m2_arg);
11558 return m1_arg == end_params_node && m2_arg == end_params_node;
11561 /* Qualification routines */
11563 /* Given a name x.y.z, look up x locally. If it's found, save the
11564 decl. If it's not found, mark the name as RESOLVE_PACKAGE_NAME_P,
11565 so that we later try and load the appropriate classes. */
11566 static void
11567 qualify_ambiguous_name (tree id)
11569 tree name, decl;
11571 /* We inspect the first item of the qualification list. As a sanity
11572 check, make sure that it is an identfier node. */
11573 tree qual = EXPR_WFL_QUALIFICATION (id);
11574 tree qual_wfl = QUAL_WFL (qual);
11576 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11577 return;
11579 name = EXPR_WFL_NODE (qual_wfl);
11581 /* If we don't have an identifier, or we have a 'this' or 'super',
11582 then field access processing is all we need : there is nothing
11583 for us to do. */
11584 if (!name || TREE_CODE (name) != IDENTIFIER_NODE ||
11585 name == this_identifier_node ||
11586 name == super_identifier_node)
11587 return;
11589 /* If name appears within the scope of a local variable declaration
11590 or parameter declaration, or is a field within an enclosing
11591 class, then it is an expression name. Save the decl and let
11592 resolve_field_access do it's work. */
11593 if ((decl = IDENTIFIER_LOCAL_VALUE (name)) ||
11594 (decl = lookup_field_wrapper (current_class, name)))
11596 QUAL_RESOLUTION (qual) = decl;
11597 return;
11600 /* If name is a known class name (either declared or imported), mark
11601 us as a type name. */
11602 if ((decl = resolve_and_layout (name, NULL_TREE)))
11604 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11605 QUAL_RESOLUTION (qual) = decl;
11608 /* Check here that NAME isn't declared by more than one
11609 type-import-on-demand declaration of the compilation unit
11610 containing NAME. FIXME */
11612 /* We couldn't find a declaration for the name. Assume for now that
11613 we have a qualified class name that needs to be loaded from an
11614 external class file. */
11615 else
11616 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11618 /* Propagate the qualification across other components of the
11619 qualified name */
11620 for (qual = TREE_CHAIN (qual); qual;
11621 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11623 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11624 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11627 /* Store the global qualification for the ambiguous part of ID back
11628 into ID fields */
11629 if (RESOLVE_TYPE_NAME_P (qual_wfl))
11630 RESOLVE_TYPE_NAME_P (id) = 1;
11631 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11632 RESOLVE_PACKAGE_NAME_P (id) = 1;
11635 /* Patch tree nodes in a function body. When a BLOCK is found, push
11636 local variable decls if present.
11637 Same as java_complete_lhs, but does resolve static finals to values. */
11639 static tree
11640 java_complete_tree (tree node)
11642 node = java_complete_lhs (node);
11643 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11644 && DECL_INITIAL (node) != NULL_TREE)
11646 tree value = fold_constant_for_init (node, node);
11647 if (value != NULL_TREE)
11648 return value;
11650 return node;
11653 static tree
11654 java_stabilize_reference (tree node)
11656 if (TREE_CODE (node) == COMPOUND_EXPR)
11658 tree op0 = TREE_OPERAND (node, 0);
11659 tree op1 = TREE_OPERAND (node, 1);
11660 TREE_OPERAND (node, 0) = save_expr (op0);
11661 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11662 return node;
11664 return stabilize_reference (node);
11667 /* Patch tree nodes in a function body. When a BLOCK is found, push
11668 local variable decls if present.
11669 Same as java_complete_tree, but does not resolve static finals to values. */
11671 static tree
11672 java_complete_lhs (tree node)
11674 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11675 int flag;
11677 /* CONVERT_EXPR always has its type set, even though it needs to be
11678 worked out. */
11679 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11680 return node;
11682 /* The switch block implements cases processing container nodes
11683 first. Contained nodes are always written back. Leaves come
11684 next and return a value. */
11685 switch (TREE_CODE (node))
11687 case BLOCK:
11689 /* 1- Block section.
11690 Set the local values on decl names so we can identify them
11691 faster when they're referenced. At that stage, identifiers
11692 are legal so we don't check for declaration errors. */
11693 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11695 DECL_CONTEXT (cn) = current_function_decl;
11696 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11698 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11699 CAN_COMPLETE_NORMALLY (node) = 1;
11700 else
11702 tree stmt = BLOCK_EXPR_BODY (node);
11703 tree *ptr;
11704 int error_seen = 0;
11705 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11707 /* Re-order from (((A; B); C); ...; Z) to
11708 (A; (B; (C ; (...; Z)))).
11709 This makes it easier to scan the statements left-to-right
11710 without using recursion (which might overflow the stack
11711 if the block has many statements. */
11712 for (;;)
11714 tree left = TREE_OPERAND (stmt, 0);
11715 if (TREE_CODE (left) != COMPOUND_EXPR)
11716 break;
11717 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11718 TREE_OPERAND (left, 1) = stmt;
11719 stmt = left;
11721 BLOCK_EXPR_BODY (node) = stmt;
11724 /* Now do the actual complete, without deep recursion for
11725 long blocks. */
11726 ptr = &BLOCK_EXPR_BODY (node);
11727 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11728 && !IS_EMPTY_STMT (TREE_OPERAND (*ptr, 1)))
11730 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11731 tree *next = &TREE_OPERAND (*ptr, 1);
11732 TREE_OPERAND (*ptr, 0) = cur;
11733 if (IS_EMPTY_STMT (cur))
11735 /* Optimization; makes it easier to detect empty bodies.
11736 Most useful for <clinit> with all-constant initializer. */
11737 *ptr = *next;
11738 continue;
11740 if (TREE_CODE (cur) == ERROR_MARK)
11741 error_seen++;
11742 else if (! CAN_COMPLETE_NORMALLY (cur))
11744 wfl_op2 = *next;
11745 for (;;)
11747 if (TREE_CODE (wfl_op2) == BLOCK)
11748 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11749 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11750 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11751 else
11752 break;
11754 if (TREE_CODE (wfl_op2) != CASE_EXPR
11755 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11756 unreachable_stmt_error (*ptr);
11758 if (TREE_TYPE (*ptr) == NULL_TREE)
11759 TREE_TYPE (*ptr) = void_type_node;
11760 ptr = next;
11762 *ptr = java_complete_tree (*ptr);
11764 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11765 return error_mark_node;
11766 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11768 /* Turn local bindings to null */
11769 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11770 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11772 TREE_TYPE (node) = void_type_node;
11773 break;
11775 /* 2- They are expressions but ultimately deal with statements */
11777 case THROW_EXPR:
11778 wfl_op1 = TREE_OPERAND (node, 0);
11779 COMPLETE_CHECK_OP_0 (node);
11780 /* 14.19 A throw statement cannot complete normally. */
11781 CAN_COMPLETE_NORMALLY (node) = 0;
11782 return patch_throw_statement (node, wfl_op1);
11784 case SYNCHRONIZED_EXPR:
11785 wfl_op1 = TREE_OPERAND (node, 0);
11786 return patch_synchronized_statement (node, wfl_op1);
11788 case TRY_EXPR:
11789 return patch_try_statement (node);
11791 case TRY_FINALLY_EXPR:
11792 COMPLETE_CHECK_OP_0 (node);
11793 COMPLETE_CHECK_OP_1 (node);
11794 if (IS_EMPTY_STMT (TREE_OPERAND (node, 0)))
11795 /* Reduce try/finally nodes with an empty try block. */
11796 return TREE_OPERAND (node, 1);
11797 if (IS_EMPTY_STMT (TREE_OPERAND (node, 1)))
11798 /* Likewise for an empty finally block. */
11799 return TREE_OPERAND (node, 0);
11800 CAN_COMPLETE_NORMALLY (node)
11801 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11802 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11803 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11804 return node;
11806 case LABELED_BLOCK_EXPR:
11807 PUSH_LABELED_BLOCK (node);
11808 if (LABELED_BLOCK_BODY (node))
11809 COMPLETE_CHECK_OP_1 (node);
11810 TREE_TYPE (node) = void_type_node;
11811 POP_LABELED_BLOCK ();
11813 if (IS_EMPTY_STMT (LABELED_BLOCK_BODY (node)))
11815 LABELED_BLOCK_BODY (node) = NULL_TREE;
11816 CAN_COMPLETE_NORMALLY (node) = 1;
11818 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11819 CAN_COMPLETE_NORMALLY (node) = 1;
11820 return node;
11822 case EXIT_BLOCK_EXPR:
11823 return patch_bc_statement (node);
11825 case CASE_EXPR:
11826 cn = java_complete_tree (TREE_OPERAND (node, 0));
11827 if (cn == error_mark_node)
11828 return cn;
11830 /* First, the case expression must be constant. Values of final
11831 fields are accepted. */
11832 nn = fold_constant_for_init (cn, NULL_TREE);
11833 if (nn != NULL_TREE)
11834 cn = nn;
11836 cn = fold (cn);
11837 if ((TREE_CODE (cn) == COMPOUND_EXPR
11838 || TREE_CODE (cn) == COMPONENT_REF)
11839 && JDECL_P (TREE_OPERAND (cn, 1))
11840 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11841 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11843 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11844 TREE_OPERAND (cn, 1));
11846 /* Accept final locals too. */
11847 else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn)
11848 && DECL_INITIAL (cn))
11849 cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11851 if (!TREE_CONSTANT (cn))
11853 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11854 parse_error_context (node, "Constant expression required");
11855 return error_mark_node;
11858 nn = ctxp->current_loop;
11860 /* It must be assignable to the type of the switch expression. */
11861 if (!try_builtin_assignconv (NULL_TREE,
11862 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11864 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11865 parse_error_context
11866 (wfl_operator,
11867 "Incompatible type for case. Can't convert %qs to %<int%>",
11868 lang_printable_name (TREE_TYPE (cn), 0));
11869 return error_mark_node;
11872 cn = fold (convert (int_type_node, cn));
11873 TREE_CONSTANT_OVERFLOW (cn) = 0;
11874 CAN_COMPLETE_NORMALLY (cn) = 1;
11876 /* Save the label on a list so that we can later check for
11877 duplicates. */
11878 case_label_list = tree_cons (node, cn, case_label_list);
11880 /* Multiple instance of a case label bearing the same value is
11881 checked later. The case expression is all right so far. */
11882 if (TREE_CODE (cn) == VAR_DECL)
11883 cn = DECL_INITIAL (cn);
11884 TREE_OPERAND (node, 0) = cn;
11885 TREE_TYPE (node) = void_type_node;
11886 CAN_COMPLETE_NORMALLY (node) = 1;
11887 TREE_SIDE_EFFECTS (node) = 1;
11888 break;
11890 case DEFAULT_EXPR:
11891 nn = ctxp->current_loop;
11892 /* Only one default label is allowed per switch statement */
11893 if (SWITCH_HAS_DEFAULT (nn))
11895 #ifdef USE_MAPPED_LOCATION
11896 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
11897 #else
11898 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11899 #endif
11900 parse_error_context (wfl_operator,
11901 "Duplicate case label: %<default%>");
11902 return error_mark_node;
11904 else
11905 SWITCH_HAS_DEFAULT (nn) = 1;
11906 TREE_TYPE (node) = void_type_node;
11907 TREE_SIDE_EFFECTS (node) = 1;
11908 CAN_COMPLETE_NORMALLY (node) = 1;
11909 break;
11911 case SWITCH_EXPR:
11912 case LOOP_EXPR:
11913 PUSH_LOOP (node);
11914 /* Check whether the loop was enclosed in a labeled
11915 statement. If not, create one, insert the loop in it and
11916 return the node */
11917 nn = patch_loop_statement (node);
11919 /* Anyways, walk the body of the loop */
11920 if (TREE_CODE (node) == LOOP_EXPR)
11921 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11922 /* Switch statement: walk the switch expression and the cases */
11923 else
11924 node = patch_switch_statement (node);
11926 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11927 nn = error_mark_node;
11928 else
11930 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11931 /* If we returned something different, that's because we
11932 inserted a label. Pop the label too. */
11933 if (nn != node)
11935 if (CAN_COMPLETE_NORMALLY (node))
11936 CAN_COMPLETE_NORMALLY (nn) = 1;
11937 POP_LABELED_BLOCK ();
11940 POP_LOOP ();
11941 return nn;
11943 case EXIT_EXPR:
11944 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11945 return patch_exit_expr (node);
11947 case COND_EXPR:
11948 /* Condition */
11949 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11950 if (TREE_OPERAND (node, 0) == error_mark_node)
11951 return error_mark_node;
11952 /* then-else branches */
11953 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11954 if (TREE_OPERAND (node, 1) == error_mark_node)
11955 return error_mark_node;
11957 /* This is a special case due to build_assertion(). When
11958 assertions are disabled we build a COND_EXPR in which
11959 Operand 1 is the body of the assertion. If that happens to
11960 be a string concatenation we'll need to patch it here. */
11961 tree patched = patch_string (TREE_OPERAND (node, 1));
11962 if (patched)
11963 TREE_OPERAND (node, 1) = patched;
11965 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11966 if (TREE_OPERAND (node, 2) == error_mark_node)
11967 return error_mark_node;
11968 return patch_if_else_statement (node);
11969 break;
11971 case CONDITIONAL_EXPR:
11972 /* Condition */
11973 wfl_op1 = TREE_OPERAND (node, 0);
11974 COMPLETE_CHECK_OP_0 (node);
11975 wfl_op2 = TREE_OPERAND (node, 1);
11976 COMPLETE_CHECK_OP_1 (node);
11977 wfl_op3 = TREE_OPERAND (node, 2);
11978 COMPLETE_CHECK_OP_2 (node);
11979 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11981 /* 3- Expression section */
11982 case COMPOUND_EXPR:
11983 wfl_op2 = TREE_OPERAND (node, 1);
11984 TREE_OPERAND (node, 0) = nn =
11985 java_complete_tree (TREE_OPERAND (node, 0));
11986 if (IS_EMPTY_STMT (wfl_op2))
11987 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11988 else
11990 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11992 /* An unreachable condition in a do-while statement
11993 is *not* (technically) an unreachable statement. */
11994 nn = wfl_op2;
11995 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11996 nn = EXPR_WFL_NODE (nn);
11997 /* NN can be NULL_TREE exactly when UPDATE is, in
11998 finish_for_loop. */
11999 if (nn != NULL_TREE && TREE_CODE (nn) != EXIT_EXPR)
12001 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12002 if (SUPPRESS_UNREACHABLE_ERROR (nn))
12004 /* Perhaps this warning should have an
12005 associated flag. The code being compiled is
12006 pedantically correct, but useless. */
12007 parse_warning_context (wfl_operator,
12008 "Unreachable statement");
12010 else
12011 parse_error_context (wfl_operator,
12012 "Unreachable statement");
12015 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
12016 if (TREE_OPERAND (node, 1) == error_mark_node)
12017 return error_mark_node;
12018 /* Even though we might allow the case where the first
12019 operand doesn't return normally, we still should compute
12020 CAN_COMPLETE_NORMALLY correctly. */
12021 CAN_COMPLETE_NORMALLY (node)
12022 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
12023 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
12025 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
12026 break;
12028 case RETURN_EXPR:
12029 /* CAN_COMPLETE_NORMALLY (node) = 0; */
12030 return patch_return (node);
12032 case EXPR_WITH_FILE_LOCATION:
12033 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
12034 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
12036 node = resolve_expression_name (node, NULL);
12037 if (node == error_mark_node)
12038 return node;
12039 CAN_COMPLETE_NORMALLY (node) = 1;
12041 else
12043 tree body;
12044 location_t save_location = input_location;
12045 #ifdef USE_MAPPED_LOCATION
12046 input_location = EXPR_LOCATION (node);
12047 if (input_location == UNKNOWN_LOCATION)
12048 input_location = save_location;
12049 #else
12050 input_line = EXPR_WFL_LINENO (node);
12051 #endif
12052 body = java_complete_tree (EXPR_WFL_NODE (node));
12053 input_location = save_location;
12054 EXPR_WFL_NODE (node) = body;
12055 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
12056 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
12057 if (IS_EMPTY_STMT (body) || TREE_CONSTANT (body))
12059 /* Makes it easier to constant fold, detect empty bodies. */
12060 return body;
12062 if (body == error_mark_node)
12064 /* Its important for the evaluation of assignment that
12065 this mark on the TREE_TYPE is propagated. */
12066 TREE_TYPE (node) = error_mark_node;
12067 return error_mark_node;
12069 else
12070 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
12073 break;
12075 case NEW_ARRAY_EXPR:
12076 /* Patch all the dimensions */
12077 flag = 0;
12078 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12080 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
12081 tree dim = convert (int_type_node,
12082 java_complete_tree (TREE_VALUE (cn)));
12083 if (dim == error_mark_node)
12085 flag = 1;
12086 continue;
12088 else
12090 TREE_VALUE (cn) = dim;
12091 /* Setup the location of the current dimension, for
12092 later error report. */
12093 #ifdef USE_MAPPED_LOCATION
12094 TREE_PURPOSE (cn) = expr_add_location (NULL_TREE, location, 0);
12095 #else
12096 TREE_PURPOSE (cn) =
12097 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
12098 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
12099 #endif
12102 /* They complete the array creation expression, if no errors
12103 were found. */
12104 CAN_COMPLETE_NORMALLY (node) = 1;
12105 return (flag ? error_mark_node
12106 : force_evaluation_order (patch_newarray (node)));
12108 case NEW_ANONYMOUS_ARRAY_EXPR:
12109 /* Create the array type if necessary. */
12110 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
12112 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
12113 if (!(type = resolve_type_during_patch (type)))
12114 return error_mark_node;
12115 type = build_array_from_name (type, NULL_TREE,
12116 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
12117 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
12119 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
12120 ANONYMOUS_ARRAY_INITIALIZER (node));
12121 if (node == error_mark_node)
12122 return error_mark_node;
12123 CAN_COMPLETE_NORMALLY (node) = 1;
12124 return node;
12126 case NEW_CLASS_EXPR:
12127 case CALL_EXPR:
12128 /* Complete function's argument(s) first */
12129 if (complete_function_arguments (node))
12130 return error_mark_node;
12131 else
12133 tree decl, wfl = TREE_OPERAND (node, 0);
12134 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
12135 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
12136 super_identifier_node);
12137 tree arguments;
12138 #ifdef USE_MAPPED_LOCATION
12139 source_location location = EXPR_LOCATION (node);
12140 #else
12141 int location = EXPR_WFL_LINECOL (node);
12142 #endif
12144 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12145 from_super, 0, &decl);
12146 if (node == error_mark_node)
12147 return error_mark_node;
12149 if (TREE_CODE (node) == CALL_EXPR
12150 && TREE_OPERAND (node, 1) != NULL_TREE)
12151 arguments = TREE_VALUE (TREE_OPERAND (node, 1));
12152 else
12153 arguments = NULL_TREE;
12154 check_thrown_exceptions (location, decl, arguments);
12155 /* If we call this(...), register signature and positions */
12156 if (in_this)
12157 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12158 tree_cons (wfl, decl,
12159 DECL_CONSTRUCTOR_CALLS (current_function_decl));
12160 CAN_COMPLETE_NORMALLY (node) = 1;
12161 return force_evaluation_order (node);
12164 case MODIFY_EXPR:
12165 /* Save potential wfls */
12166 wfl_op1 = TREE_OPERAND (node, 0);
12167 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12169 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12170 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12171 && DECL_INITIAL (nn) != NULL_TREE)
12173 tree value;
12175 value = fold_constant_for_init (nn, nn);
12177 /* When we have a primitype type, or a string and we're not
12178 emitting a class file, we actually don't want to generate
12179 anything for the assignment. */
12180 if (value != NULL_TREE &&
12181 (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
12182 (TREE_TYPE (value) == string_ptr_type_node &&
12183 ! flag_emit_class_files)))
12185 /* Prepare node for patch_assignment */
12186 TREE_OPERAND (node, 1) = value;
12187 /* Call patch assignment to verify the assignment */
12188 if (patch_assignment (node, wfl_op1) == error_mark_node)
12189 return error_mark_node;
12190 /* Set DECL_INITIAL properly (a conversion might have
12191 been decided by patch_assignment) and return the
12192 empty statement. */
12193 else
12195 tree patched = patch_string (TREE_OPERAND (node, 1));
12196 if (patched)
12197 DECL_INITIAL (nn) = patched;
12198 else
12199 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12200 DECL_FIELD_FINAL_IUD (nn) = 1;
12201 return build_java_empty_stmt ();
12204 if (! flag_emit_class_files)
12205 DECL_INITIAL (nn) = NULL_TREE;
12207 wfl_op2 = TREE_OPERAND (node, 1);
12209 if (TREE_OPERAND (node, 0) == error_mark_node)
12210 return error_mark_node;
12212 flag = COMPOUND_ASSIGN_P (wfl_op2);
12213 if (flag)
12215 /* This might break when accessing outer field from inner
12216 class. TESTME, FIXME */
12217 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12219 /* Hand stabilize the lhs on both places */
12220 TREE_OPERAND (node, 0) = lvalue;
12221 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12222 (flag_emit_class_files ? lvalue : save_expr (lvalue));
12224 /* 15.25.2.a: Left hand is not an array access. FIXME */
12225 /* Now complete the RHS. We write it back later on. */
12226 nn = java_complete_tree (TREE_OPERAND (node, 1));
12228 if ((cn = patch_string (nn)))
12229 nn = cn;
12231 /* The last part of the rewrite for E1 op= E2 is to have
12232 E1 = (T)(E1 op E2), with T being the type of E1. */
12233 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12234 TREE_TYPE (lvalue), nn));
12236 /* If the assignment is compound and has reference type,
12237 then ensure the LHS has type String and nothing else. */
12238 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12239 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12240 parse_error_context (wfl_op2,
12241 "Incompatible type for %<+=%>. Can't convert %qs to %<java.lang.String%>",
12242 lang_printable_name (TREE_TYPE (lvalue), 0));
12244 /* 15.25.2.b: Left hand is an array access. FIXME */
12247 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12248 function to complete this RHS. Note that a NEW_ARRAY_INIT
12249 might have been already fully expanded if created as a result
12250 of processing an anonymous array initializer. We avoid doing
12251 the operation twice by testing whether the node already bears
12252 a type. */
12253 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12254 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12255 TREE_OPERAND (node, 1));
12256 /* Otherwise we simply complete the RHS */
12257 else
12258 nn = java_complete_tree (TREE_OPERAND (node, 1));
12260 if (nn == error_mark_node)
12261 return error_mark_node;
12263 /* Write back the RHS as we evaluated it. */
12264 TREE_OPERAND (node, 1) = nn;
12266 /* In case we're handling = with a String as a RHS, we need to
12267 produce a String out of the RHS (it might still be a
12268 STRING_CST or a StringBuffer at this stage */
12269 if ((nn = patch_string (TREE_OPERAND (node, 1))))
12270 TREE_OPERAND (node, 1) = nn;
12272 if ((nn = nested_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12273 TREE_OPERAND (node, 1))))
12275 /* We return error_mark_node if nested_field_access_fix
12276 detects we write into a final. */
12277 if (nn == error_mark_node)
12278 return error_mark_node;
12279 node = nn;
12281 else
12283 node = patch_assignment (node, wfl_op1);
12284 if (node == error_mark_node)
12285 return error_mark_node;
12286 /* Reorganize the tree if necessary. */
12287 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12288 || JSTRING_P (TREE_TYPE (node))))
12289 node = java_refold (node);
12292 /* Seek to set DECL_INITIAL to a proper value, since it might have
12293 undergone a conversion in patch_assignment. We do that only when
12294 it's necessary to have DECL_INITIAL properly set. */
12295 nn = TREE_OPERAND (node, 0);
12296 if (TREE_CODE (nn) == VAR_DECL
12297 && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12298 && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12299 && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12300 || TREE_TYPE (nn) == string_ptr_type_node))
12301 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12303 CAN_COMPLETE_NORMALLY (node) = 1;
12304 return node;
12306 case MULT_EXPR:
12307 case PLUS_EXPR:
12308 case MINUS_EXPR:
12309 case LSHIFT_EXPR:
12310 case RSHIFT_EXPR:
12311 case URSHIFT_EXPR:
12312 case BIT_AND_EXPR:
12313 case BIT_XOR_EXPR:
12314 case BIT_IOR_EXPR:
12315 case TRUNC_MOD_EXPR:
12316 case TRUNC_DIV_EXPR:
12317 case RDIV_EXPR:
12318 case TRUTH_ANDIF_EXPR:
12319 case TRUTH_ORIF_EXPR:
12320 case EQ_EXPR:
12321 case NE_EXPR:
12322 case GT_EXPR:
12323 case GE_EXPR:
12324 case LT_EXPR:
12325 case LE_EXPR:
12326 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12327 knows how to handle those cases. */
12328 wfl_op1 = TREE_OPERAND (node, 0);
12329 wfl_op2 = TREE_OPERAND (node, 1);
12331 CAN_COMPLETE_NORMALLY (node) = 1;
12332 /* Don't complete string nodes if dealing with the PLUS operand. */
12333 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12335 nn = java_complete_tree (wfl_op1);
12336 if (nn == error_mark_node)
12337 return error_mark_node;
12339 TREE_OPERAND (node, 0) = nn;
12341 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12343 nn = java_complete_tree (wfl_op2);
12344 if (nn == error_mark_node)
12345 return error_mark_node;
12347 TREE_OPERAND (node, 1) = nn;
12349 return patch_binop (node, wfl_op1, wfl_op2, 0);
12351 case INSTANCEOF_EXPR:
12352 wfl_op1 = TREE_OPERAND (node, 0);
12353 COMPLETE_CHECK_OP_0 (node);
12354 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1), 0);
12356 case UNARY_PLUS_EXPR:
12357 case NEGATE_EXPR:
12358 case TRUTH_NOT_EXPR:
12359 case BIT_NOT_EXPR:
12360 case PREDECREMENT_EXPR:
12361 case PREINCREMENT_EXPR:
12362 case POSTDECREMENT_EXPR:
12363 case POSTINCREMENT_EXPR:
12364 case CONVERT_EXPR:
12365 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12366 how to handle those cases. */
12367 wfl_op1 = TREE_OPERAND (node, 0);
12368 CAN_COMPLETE_NORMALLY (node) = 1;
12369 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12370 if (TREE_OPERAND (node, 0) == error_mark_node)
12371 return error_mark_node;
12372 node = patch_unaryop (node, wfl_op1);
12373 CAN_COMPLETE_NORMALLY (node) = 1;
12374 break;
12376 case ARRAY_REF:
12377 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12378 how to handle those cases. */
12379 wfl_op1 = TREE_OPERAND (node, 0);
12380 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12381 if (TREE_OPERAND (node, 0) == error_mark_node)
12382 return error_mark_node;
12383 if (!flag_emit_class_files)
12384 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12385 /* The same applies to wfl_op2 */
12386 wfl_op2 = TREE_OPERAND (node, 1);
12387 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12388 if (TREE_OPERAND (node, 1) == error_mark_node)
12389 return error_mark_node;
12390 if (!flag_emit_class_files)
12391 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12392 return patch_array_ref (node);
12394 case RECORD_TYPE:
12395 return node;;
12397 case COMPONENT_REF:
12398 /* The first step in the re-write of qualified name handling. FIXME.
12399 So far, this is only to support PRIMTYPE.class ->
12400 PRIMCLASS.TYPE. */
12402 tree prim_class = TREE_OPERAND (node, 0);
12403 tree name = TREE_OPERAND (node, 1);
12404 tree field;
12406 gcc_assert (TREE_CODE (prim_class) == NOP_EXPR);
12407 prim_class = java_complete_tree (TREE_TYPE (prim_class));
12408 gcc_assert (TREE_CODE (prim_class) == RECORD_TYPE);
12409 field = lookup_field_wrapper (prim_class, name);
12411 if (field == NULL_TREE)
12413 error ("missing static field %qs", IDENTIFIER_POINTER (name));
12414 return error_mark_node;
12416 if (! FIELD_STATIC (field))
12418 error ("not a static field %qs", IDENTIFIER_POINTER (name));
12419 return error_mark_node;
12421 return field;
12423 break;
12425 case THIS_EXPR:
12426 /* Can't use THIS in a static environment */
12427 if (!current_this)
12429 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12430 parse_error_context (wfl_operator,
12431 "Keyword %<this%> used outside allowed context");
12432 TREE_TYPE (node) = error_mark_node;
12433 return error_mark_node;
12435 if (ctxp->explicit_constructor_p)
12437 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12438 parse_error_context
12439 (wfl_operator, "Can't reference %<this%> or %<super%> before the superclass constructor has been called");
12440 TREE_TYPE (node) = error_mark_node;
12441 return error_mark_node;
12443 return current_this;
12445 case CLASS_LITERAL:
12446 CAN_COMPLETE_NORMALLY (node) = 1;
12447 node = patch_incomplete_class_ref (node);
12448 if (node == error_mark_node)
12449 return error_mark_node;
12450 break;
12452 default:
12453 CAN_COMPLETE_NORMALLY (node) = 1;
12454 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12455 and it's time to turn it into the appropriate String object */
12456 if ((nn = patch_string (node)))
12457 node = nn;
12458 else
12459 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12461 return node;
12464 /* Complete function call's argument. Return a nonzero value is an
12465 error was found. */
12467 static int
12468 complete_function_arguments (tree node)
12470 int flag = 0;
12471 tree cn;
12473 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12474 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12476 tree wfl = TREE_VALUE (cn), parm, temp;
12477 parm = java_complete_tree (wfl);
12479 if (parm == error_mark_node)
12481 flag = 1;
12482 continue;
12484 /* If we have a string literal that we haven't transformed yet or a
12485 crafted string buffer, as a result of the use of the String
12486 `+' operator. Build `parm.toString()' and expand it. */
12487 if ((temp = patch_string (parm)))
12488 parm = temp;
12490 TREE_VALUE (cn) = parm;
12492 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12493 return flag;
12496 /* Sometimes (for loops and variable initialized during their
12497 declaration), we want to wrap a statement around a WFL and turn it
12498 debugable. */
12500 static tree
12501 build_debugable_stmt (int location, tree stmt)
12503 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12505 #ifdef USE_MAPPED_LOCATION
12506 stmt = expr_add_location (stmt, location, 1);
12507 #else
12508 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12509 EXPR_WFL_LINECOL (stmt) = location;
12510 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12511 #endif
12513 return stmt;
12516 static tree
12517 build_expr_block (tree body, tree decls)
12519 tree node = make_node (BLOCK);
12520 BLOCK_EXPR_DECLS (node) = decls;
12521 BLOCK_EXPR_BODY (node) = body;
12522 if (body)
12523 TREE_TYPE (node) = TREE_TYPE (body);
12524 TREE_SIDE_EFFECTS (node) = 1;
12525 return node;
12528 /* Create a new function block and link it appropriately to current
12529 function block chain */
12531 static tree
12532 enter_block (void)
12534 tree b = build_expr_block (NULL_TREE, NULL_TREE);
12536 /* Link block B supercontext to the previous block. The current
12537 function DECL is used as supercontext when enter_a_block is called
12538 for the first time for a given function. The current function body
12539 (DECL_FUNCTION_BODY) is set to be block B. */
12541 tree fndecl = current_function_decl;
12543 if (!fndecl) {
12544 BLOCK_SUPERCONTEXT (b) = current_static_block;
12545 current_static_block = b;
12548 else if (!DECL_FUNCTION_BODY (fndecl))
12550 BLOCK_SUPERCONTEXT (b) = fndecl;
12551 DECL_FUNCTION_BODY (fndecl) = b;
12553 else
12555 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12556 DECL_FUNCTION_BODY (fndecl) = b;
12558 return b;
12561 /* Exit a block by changing the current function body
12562 (DECL_FUNCTION_BODY) to the current block super context, only if
12563 the block being exited isn't the method's top level one. */
12565 static tree
12566 exit_block (void)
12568 tree b;
12569 if (current_function_decl)
12571 b = DECL_FUNCTION_BODY (current_function_decl);
12572 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12573 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12575 else
12577 b = current_static_block;
12579 if (BLOCK_SUPERCONTEXT (b))
12580 current_static_block = BLOCK_SUPERCONTEXT (b);
12582 return b;
12585 /* Lookup for NAME in the nested function's blocks, all the way up to
12586 the current toplevel one. It complies with Java's local variable
12587 scoping rules. */
12589 static tree
12590 lookup_name_in_blocks (tree name)
12592 tree b = GET_CURRENT_BLOCK (current_function_decl);
12594 while (b != current_function_decl)
12596 tree current;
12598 /* Paranoid sanity check. To be removed */
12599 if (TREE_CODE (b) != BLOCK)
12600 abort ();
12602 for (current = BLOCK_EXPR_DECLS (b); current;
12603 current = TREE_CHAIN (current))
12604 if (DECL_NAME (current) == name)
12605 return current;
12606 b = BLOCK_SUPERCONTEXT (b);
12608 return NULL_TREE;
12611 static void
12612 maybe_absorb_scoping_blocks (void)
12614 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12616 tree b = exit_block ();
12617 java_method_add_stmt (current_function_decl, b);
12618 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", input_line));
12623 /* This section of the source is reserved to build_* functions that
12624 are building incomplete tree nodes and the patch_* functions that
12625 are completing them. */
12627 /* Wrap a non WFL node around a WFL. */
12629 static tree
12630 build_wfl_wrap (tree node, int location)
12632 tree wfl, node_to_insert = node;
12634 /* We want to process THIS . xxx symbolically, to keep it consistent
12635 with the way we're processing SUPER. A THIS from a primary as a
12636 different form than a SUPER. Turn THIS into something symbolic */
12637 if (TREE_CODE (node) == THIS_EXPR)
12638 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12639 else
12640 #ifdef USE_MAPPED_LOCATION
12641 wfl = build_unknown_wfl (NULL_TREE);
12643 SET_EXPR_LOCATION (wfl, location);
12644 #else
12645 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12647 EXPR_WFL_LINECOL (wfl) = location;
12648 #endif
12649 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12650 return wfl;
12653 /* Build a super() constructor invocation. Returns an empty statement if
12654 we're currently dealing with the class java.lang.Object. */
12656 static tree
12657 build_super_invocation (tree mdecl)
12659 if (DECL_CONTEXT (mdecl) == object_type_node)
12660 return build_java_empty_stmt ();
12661 else
12663 tree super_wfl = build_wfl_node (super_identifier_node);
12664 tree a = NULL_TREE, t;
12666 /* This is called after parsing is done, so the parser context
12667 won't be accurate. Set location info from current_class decl. */
12668 tree class_wfl = lookup_cl (TYPE_NAME (current_class));
12669 EXPR_WFL_LINECOL (super_wfl) = EXPR_WFL_LINECOL (class_wfl);
12671 /* If we're dealing with an anonymous class, pass the arguments
12672 of the crafted constructor along. */
12673 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12675 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12676 for (; t != end_params_node; t = TREE_CHAIN (t))
12677 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12679 return build_method_invocation (super_wfl, a);
12683 /* Build a SUPER/THIS qualified method invocation. */
12685 static tree
12686 build_this_super_qualified_invocation (int use_this, tree name, tree args,
12687 int lloc, int rloc)
12689 tree invok;
12690 tree wfl =
12691 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12692 EXPR_WFL_LINECOL (wfl) = lloc;
12693 invok = build_method_invocation (name, args);
12694 return make_qualified_primary (wfl, invok, rloc);
12697 /* Build an incomplete CALL_EXPR node. */
12699 static tree
12700 build_method_invocation (tree name, tree args)
12702 tree call = build3 (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12703 TREE_SIDE_EFFECTS (call) = 1;
12704 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12705 return call;
12708 /* Build an incomplete new xxx(...) node. */
12710 static tree
12711 build_new_invocation (tree name, tree args)
12713 tree call = build3 (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12714 TREE_SIDE_EFFECTS (call) = 1;
12715 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12716 return call;
12719 /* Build an incomplete assignment expression. */
12721 static tree
12722 build_assignment (int op, int op_location, tree lhs, tree rhs)
12724 tree assignment;
12725 /* Build the corresponding binop if we deal with a Compound
12726 Assignment operator. Mark the binop sub-tree as part of a
12727 Compound Assignment expression */
12728 if (op != ASSIGN_TK)
12730 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12731 COMPOUND_ASSIGN_P (rhs) = 1;
12733 assignment = build2 (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12734 TREE_SIDE_EFFECTS (assignment) = 1;
12735 EXPR_WFL_LINECOL (assignment) = op_location;
12736 return assignment;
12739 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12740 the buffer. This is used only for string conversion. */
12741 static char *
12742 string_convert_int_cst (tree node)
12744 /* Long.MIN_VALUE is -9223372036854775808, 20 characters. */
12745 static char buffer[21];
12747 unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12748 unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12749 char *p = buffer + sizeof (buffer);
12750 int neg = 0;
12752 unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12753 << (HOST_BITS_PER_WIDE_INT - 1));
12755 *--p = '\0';
12757 /* If negative, note the fact and negate the value. */
12758 if ((hi & hibit))
12760 lo = ~lo;
12761 hi = ~hi;
12762 if (++lo == 0)
12763 ++hi;
12764 neg = 1;
12767 /* Divide by 10 until there are no bits left. */
12770 unsigned HOST_WIDE_INT acc = 0;
12771 unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12772 unsigned int i;
12774 /* Use long division to compute the result and the remainder. */
12775 for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12777 /* Shift a bit into accumulator. */
12778 acc <<= 1;
12779 if ((hi & hibit))
12780 acc |= 1;
12782 /* Shift the value. */
12783 hi <<= 1;
12784 if ((lo & hibit))
12785 hi |= 1;
12786 lo <<= 1;
12788 /* Shift the correct bit into the result. */
12789 outhi <<= 1;
12790 if ((outlo & hibit))
12791 outhi |= 1;
12792 outlo <<= 1;
12793 if (acc >= 10)
12795 acc -= 10;
12796 outlo |= 1;
12800 /* '0' == 060 in Java, but might not be here (think EBCDIC). */
12801 *--p = '\060' + acc;
12803 hi = outhi;
12804 lo = outlo;
12806 while (hi || lo);
12808 if (neg)
12809 *--p = '\055'; /* '-' == 055 in Java, but might not be here. */
12811 return p;
12814 /* Print an INTEGER_CST node in a static buffer, and return the
12815 buffer. This is used only for error handling. */
12816 char *
12817 print_int_node (tree node)
12819 static char buffer [80];
12820 if (TREE_CONSTANT_OVERFLOW (node))
12821 sprintf (buffer, "<overflow>");
12823 if (TREE_INT_CST_HIGH (node) == 0)
12824 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12825 TREE_INT_CST_LOW (node));
12826 else if (TREE_INT_CST_HIGH (node) == -1
12827 && TREE_INT_CST_LOW (node) != 0)
12828 sprintf (buffer, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
12829 -TREE_INT_CST_LOW (node));
12830 else
12831 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12832 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12834 return buffer;
12838 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12839 context. */
12841 /* 15.25 Assignment operators. */
12843 static tree
12844 patch_assignment (tree node, tree wfl_op1)
12846 tree rhs = TREE_OPERAND (node, 1);
12847 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12848 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12849 int error_found = 0;
12850 int lvalue_from_array = 0;
12851 int is_return = 0;
12853 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12855 /* Lhs can be a named variable */
12856 if (JDECL_P (lvalue))
12858 lhs_type = TREE_TYPE (lvalue);
12860 /* Or Lhs can be an array access. */
12861 else if (TREE_CODE (lvalue) == ARRAY_REF)
12863 lhs_type = TREE_TYPE (lvalue);
12864 lvalue_from_array = 1;
12866 /* Or a field access */
12867 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12868 lhs_type = TREE_TYPE (lvalue);
12869 /* Or a function return slot */
12870 else if (TREE_CODE (lvalue) == RESULT_DECL)
12872 /* If the return type is an integral type, then we create the
12873 RESULT_DECL with a promoted type, but we need to do these
12874 checks against the unpromoted type to ensure type safety. So
12875 here we look at the real type, not the type of the decl we
12876 are modifying. */
12877 lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12878 is_return = 1;
12880 /* Otherwise, we might want to try to write into an optimized static
12881 final, this is an of a different nature, reported further on. */
12882 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12883 && resolve_expression_name (wfl_op1, &llvalue))
12885 lhs_type = TREE_TYPE (lvalue);
12887 else
12889 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12890 error_found = 1;
12893 rhs_type = TREE_TYPE (rhs);
12895 /* 5.1 Try the assignment conversion for builtin type. */
12896 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12898 /* 5.2 If it failed, try a reference conversion */
12899 if (!new_rhs)
12900 new_rhs = try_reference_assignconv (lhs_type, rhs);
12902 /* 15.25.2 If we have a compound assignment, convert RHS into the
12903 type of the LHS */
12904 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12905 new_rhs = convert (lhs_type, rhs);
12907 /* Explicit cast required. This is an error */
12908 if (!new_rhs)
12910 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12911 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12912 tree wfl;
12913 char operation [32]; /* Max size known */
12915 /* If the assignment is part of a declaration, we use the WFL of
12916 the declared variable to point out the error and call it a
12917 declaration problem. If the assignment is a genuine =
12918 operator, we call is a operator `=' problem, otherwise we
12919 call it an assignment problem. In both of these last cases,
12920 we use the WFL of the operator to indicate the error. */
12922 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12924 wfl = wfl_op1;
12925 strcpy (operation, "declaration");
12927 else
12929 wfl = wfl_operator;
12930 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12931 strcpy (operation, "assignment");
12932 else if (is_return)
12933 strcpy (operation, "'return'");
12934 else
12935 strcpy (operation, "'='");
12938 if (!valid_cast_to_p (rhs_type, lhs_type))
12939 parse_error_context
12940 (wfl, "Incompatible type for %s. Can't convert %qs to %qs",
12941 operation, t1, t2);
12942 else
12943 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert %qs to %qs",
12944 operation, t1, t2);
12945 free (t1); free (t2);
12946 error_found = 1;
12949 if (error_found)
12950 return error_mark_node;
12952 /* If we're processing a `return' statement, promote the actual type
12953 to the promoted type. */
12954 if (is_return)
12955 new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12957 /* 10.10: Array Store Exception runtime check */
12958 if (!flag_emit_class_files
12959 && lvalue_from_array
12960 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12962 tree array, store_check, base, index_expr;
12964 /* Save RHS so that it doesn't get re-evaluated by the store check. */
12965 new_rhs = save_expr (new_rhs);
12967 /* Get the INDIRECT_REF. */
12968 array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12969 /* Get the array pointer expr. */
12970 array = TREE_OPERAND (array, 0);
12971 store_check = build_java_arraystore_check (array, new_rhs);
12973 index_expr = TREE_OPERAND (lvalue, 1);
12975 if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12977 /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12978 happen before the store check, so prepare to insert the store
12979 check within the second operand of the existing COMPOUND_EXPR. */
12980 base = index_expr;
12982 else
12983 base = lvalue;
12985 index_expr = TREE_OPERAND (base, 1);
12986 TREE_OPERAND (base, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (index_expr),
12987 store_check, index_expr);
12990 /* Final locals can be used as case values in switch
12991 statement. Prepare them for this eventuality. */
12992 if (TREE_CODE (lvalue) == VAR_DECL
12993 && DECL_FINAL (lvalue)
12994 && TREE_CONSTANT (new_rhs)
12995 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12996 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12999 TREE_CONSTANT (lvalue) = 1;
13000 TREE_INVARIANT (lvalue) = 1;
13001 DECL_INITIAL (lvalue) = new_rhs;
13004 /* Copy the rhs if it's a reference. */
13005 if (! flag_check_references && ! flag_emit_class_files && optimize > 0)
13007 switch (TREE_CODE (new_rhs))
13009 case ARRAY_REF:
13010 case INDIRECT_REF:
13011 case COMPONENT_REF:
13012 /* Transform a = foo.bar
13013 into a = ({int tmp; tmp = foo.bar;}).
13014 We need to ensure that if a read from memory fails
13015 because of a NullPointerException, a destination variable
13016 will remain unchanged. An explicit temporary does what
13017 we need.
13019 If flag_check_references is set, this is unnecessary
13020 because we'll check each reference before doing any
13021 reads. If optimize is not set the result will never be
13022 written to a stack slot that contains the LHS. */
13024 tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"),
13025 TREE_TYPE (new_rhs));
13026 tree block = make_node (BLOCK);
13027 tree assignment
13028 = build2 (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
13029 DECL_CONTEXT (tmp) = current_function_decl;
13030 TREE_TYPE (block) = TREE_TYPE (new_rhs);
13031 BLOCK_VARS (block) = tmp;
13032 BLOCK_EXPR_BODY (block) = assignment;
13033 TREE_SIDE_EFFECTS (block) = 1;
13034 new_rhs = block;
13036 break;
13037 default:
13038 break;
13042 TREE_OPERAND (node, 0) = lvalue;
13043 TREE_OPERAND (node, 1) = new_rhs;
13044 TREE_TYPE (node) = lhs_type;
13045 return node;
13048 /* Check that type SOURCE can be cast into type DEST. If the cast
13049 can't occur at all, return NULL; otherwise, return a possibly
13050 modified rhs. */
13052 static tree
13053 try_reference_assignconv (tree lhs_type, tree rhs)
13055 tree new_rhs = NULL_TREE;
13056 tree rhs_type = TREE_TYPE (rhs);
13058 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
13060 /* `null' may be assigned to any reference type */
13061 if (rhs == null_pointer_node)
13062 new_rhs = null_pointer_node;
13063 /* Try the reference assignment conversion */
13064 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
13065 new_rhs = rhs;
13066 /* This is a magic assignment that we process differently */
13067 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
13068 new_rhs = rhs;
13070 return new_rhs;
13073 /* Check that RHS can be converted into LHS_TYPE by the assignment
13074 conversion (5.2), for the cases of RHS being a builtin type. Return
13075 NULL_TREE if the conversion fails or if because RHS isn't of a
13076 builtin type. Return a converted RHS if the conversion is possible. */
13078 static tree
13079 try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
13081 tree new_rhs = NULL_TREE;
13082 tree rhs_type = TREE_TYPE (rhs);
13084 /* Handle boolean specially. */
13085 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13086 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13088 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13089 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13090 new_rhs = rhs;
13093 /* 5.1.1 Try Identity Conversion,
13094 5.1.2 Try Widening Primitive Conversion */
13095 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
13096 new_rhs = convert (lhs_type, rhs);
13098 /* Try a narrowing primitive conversion (5.1.3):
13099 - expression is a constant expression of type byte, short, char,
13100 or int, AND
13101 - variable is byte, short or char AND
13102 - The value of the expression is representable in the type of the
13103 variable */
13104 else if ((rhs_type == byte_type_node || rhs_type == short_type_node
13105 || rhs_type == char_type_node || rhs_type == int_type_node)
13106 && TREE_CONSTANT (rhs)
13107 && (lhs_type == byte_type_node || lhs_type == char_type_node
13108 || lhs_type == short_type_node))
13110 if (int_fits_type_p (rhs, lhs_type))
13111 new_rhs = convert (lhs_type, rhs);
13112 else if (wfl_op1) /* Might be called with a NULL */
13113 parse_warning_context
13114 (wfl_op1,
13115 "Constant expression %qs too wide for narrowing primitive conversion to %qs",
13116 print_int_node (rhs), lang_printable_name (lhs_type, 0));
13117 /* Reported a warning that will turn into an error further
13118 down, so we don't return */
13121 return new_rhs;
13124 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
13125 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
13126 0 is the conversion test fails. This implements parts the method
13127 invocation conversion (5.3). */
13129 static int
13130 valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
13132 /* 5.1.1: This is the identity conversion part. */
13133 if (lhs_type == rhs_type)
13134 return 1;
13136 /* Reject non primitive types and boolean conversions. */
13137 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
13138 return 0;
13140 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
13141 than a char can't be converted into a char. Short can't too, but
13142 the < test below takes care of that */
13143 if (lhs_type == char_type_node && rhs_type == byte_type_node)
13144 return 0;
13146 /* Accept all promoted type here. Note, we can't use <= in the test
13147 below, because we still need to bounce out assignments of short
13148 to char and the likes */
13149 if (lhs_type == int_type_node
13150 && (rhs_type == promoted_byte_type_node
13151 || rhs_type == promoted_short_type_node
13152 || rhs_type == promoted_char_type_node
13153 || rhs_type == promoted_boolean_type_node))
13154 return 1;
13156 /* From here, an integral is widened if its precision is smaller
13157 than the precision of the LHS or if the LHS is a floating point
13158 type, or the RHS is a float and the RHS a double. */
13159 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13160 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13161 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13162 || (rhs_type == float_type_node && lhs_type == double_type_node))
13163 return 1;
13165 return 0;
13168 /* Check that something of SOURCE type can be assigned or cast to
13169 something of DEST type at runtime. Return 1 if the operation is
13170 valid, 0 otherwise. If CAST is set to 1, we're treating the case
13171 were SOURCE is cast into DEST, which borrows a lot of the
13172 assignment check. */
13174 static int
13175 valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
13177 /* SOURCE or DEST might be null if not from a declared entity. */
13178 if (!source || !dest)
13179 return 0;
13180 if (JNULLP_TYPE_P (source))
13181 return 1;
13182 if (TREE_CODE (source) == POINTER_TYPE)
13183 source = TREE_TYPE (source);
13184 if (TREE_CODE (dest) == POINTER_TYPE)
13185 dest = TREE_TYPE (dest);
13187 /* If source and dest are being compiled from bytecode, they may need to
13188 be loaded. */
13189 if (CLASS_P (source) && !CLASS_LOADED_P (source))
13191 load_class (source, 1);
13192 safe_layout_class (source);
13194 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13196 load_class (dest, 1);
13197 safe_layout_class (dest);
13200 /* Case where SOURCE is a class type */
13201 if (TYPE_CLASS_P (source))
13203 if (TYPE_CLASS_P (dest))
13204 return (source == dest
13205 || inherits_from_p (source, dest)
13206 || (cast && inherits_from_p (dest, source)));
13207 if (TYPE_INTERFACE_P (dest))
13209 /* If doing a cast and SOURCE is final, the operation is
13210 always correct a compile time (because even if SOURCE
13211 does not implement DEST, a subclass of SOURCE might). */
13212 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13213 return 1;
13214 /* Otherwise, SOURCE must implement DEST */
13215 return interface_of_p (dest, source);
13217 /* DEST is an array, cast permitted if SOURCE is of Object type */
13218 return (cast && source == object_type_node ? 1 : 0);
13220 if (TYPE_INTERFACE_P (source))
13222 if (TYPE_CLASS_P (dest))
13224 /* If not casting, DEST must be the Object type */
13225 if (!cast)
13226 return dest == object_type_node;
13227 /* We're doing a cast. The cast is always valid is class
13228 DEST is not final, otherwise, DEST must implement SOURCE */
13229 else if (!CLASS_FINAL (TYPE_NAME (dest)))
13230 return 1;
13231 else
13232 return interface_of_p (source, dest);
13234 if (TYPE_INTERFACE_P (dest))
13236 /* If doing a cast, then if SOURCE and DEST contain method
13237 with the same signature but different return type, then
13238 this is a (compile time) error */
13239 if (cast)
13241 tree method_source, method_dest;
13242 tree source_type;
13243 tree source_sig;
13244 tree source_name;
13245 for (method_source = TYPE_METHODS (source); method_source;
13246 method_source = TREE_CHAIN (method_source))
13248 source_sig =
13249 build_java_argument_signature (TREE_TYPE (method_source));
13250 source_type = TREE_TYPE (TREE_TYPE (method_source));
13251 source_name = DECL_NAME (method_source);
13252 for (method_dest = TYPE_METHODS (dest);
13253 method_dest; method_dest = TREE_CHAIN (method_dest))
13254 if (source_sig ==
13255 build_java_argument_signature (TREE_TYPE (method_dest))
13256 && source_name == DECL_NAME (method_dest)
13257 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13258 return 0;
13260 return 1;
13262 else
13263 return source == dest || interface_of_p (dest, source);
13265 else
13267 /* Array */
13268 return (cast
13269 && (DECL_NAME (TYPE_NAME (source))
13270 == java_lang_cloneable_identifier_node
13271 || (DECL_NAME (TYPE_NAME (source))
13272 == java_io_serializable_identifier_node)));
13275 if (TYPE_ARRAY_P (source))
13277 if (TYPE_CLASS_P (dest))
13278 return dest == object_type_node;
13279 /* Can't cast an array to an interface unless the interface is
13280 java.lang.Cloneable or java.io.Serializable. */
13281 if (TYPE_INTERFACE_P (dest))
13282 return (DECL_NAME (TYPE_NAME (dest))
13283 == java_lang_cloneable_identifier_node
13284 || (DECL_NAME (TYPE_NAME (dest))
13285 == java_io_serializable_identifier_node));
13286 else /* Arrays */
13288 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13289 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13291 /* In case of severe errors, they turn out null */
13292 if (!dest_element_type || !source_element_type)
13293 return 0;
13294 if (source_element_type == dest_element_type)
13295 return 1;
13296 return valid_ref_assignconv_cast_p (source_element_type,
13297 dest_element_type, cast);
13299 return 0;
13301 return 0;
13304 static int
13305 valid_cast_to_p (tree source, tree dest)
13307 if (TREE_CODE (source) == POINTER_TYPE)
13308 source = TREE_TYPE (source);
13309 if (TREE_CODE (dest) == POINTER_TYPE)
13310 dest = TREE_TYPE (dest);
13312 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13313 return valid_ref_assignconv_cast_p (source, dest, 1);
13315 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13316 return 1;
13318 else if (TREE_CODE (source) == BOOLEAN_TYPE
13319 && TREE_CODE (dest) == BOOLEAN_TYPE)
13320 return 1;
13322 return 0;
13325 static tree
13326 do_unary_numeric_promotion (tree arg)
13328 tree type = TREE_TYPE (arg);
13329 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13330 || TREE_CODE (type) == CHAR_TYPE)
13331 arg = convert (int_type_node, arg);
13332 return arg;
13335 /* Return a nonzero value if SOURCE can be converted into DEST using
13336 the method invocation conversion rule (5.3). */
13337 static int
13338 valid_method_invocation_conversion_p (tree dest, tree source)
13340 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13341 && valid_builtin_assignconv_identity_widening_p (dest, source))
13342 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13343 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13344 && valid_ref_assignconv_cast_p (source, dest, 0)));
13347 /* Build an incomplete binop expression. */
13349 static tree
13350 build_binop (enum tree_code op, int op_location, tree op1, tree op2)
13352 tree binop = build2 (op, NULL_TREE, op1, op2);
13353 TREE_SIDE_EFFECTS (binop) = 1;
13354 /* Store the location of the operator, for better error report. The
13355 string of the operator will be rebuild based on the OP value. */
13356 EXPR_WFL_LINECOL (binop) = op_location;
13357 return binop;
13360 /* Build the string of the operator retained by NODE. If NODE is part
13361 of a compound expression, add an '=' at the end of the string. This
13362 function is called when an error needs to be reported on an
13363 operator. The string is returned as a pointer to a static character
13364 buffer. */
13366 static char *
13367 operator_string (tree node)
13369 #define BUILD_OPERATOR_STRING(S) \
13371 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13372 return buffer; \
13375 static char buffer [10];
13376 switch (TREE_CODE (node))
13378 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13379 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13380 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13381 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13382 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13383 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13384 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13385 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13386 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13387 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13388 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13389 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13390 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13391 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13392 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13393 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13394 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13395 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13396 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13397 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13398 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13399 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13400 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13401 case PREINCREMENT_EXPR: /* Fall through */
13402 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13403 case PREDECREMENT_EXPR: /* Fall through */
13404 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13405 default:
13406 internal_error ("unregistered operator %s",
13407 tree_code_name [TREE_CODE (node)]);
13409 return NULL;
13410 #undef BUILD_OPERATOR_STRING
13413 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13415 static int
13416 java_decl_equiv (tree var_acc1, tree var_acc2)
13418 if (JDECL_P (var_acc1))
13419 return (var_acc1 == var_acc2);
13421 return (TREE_CODE (var_acc1) == COMPONENT_REF
13422 && TREE_CODE (var_acc2) == COMPONENT_REF
13423 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13424 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13425 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13428 /* Return a nonzero value if CODE is one of the operators that can be
13429 used in conjunction with the `=' operator in a compound assignment. */
13431 static int
13432 binop_compound_p (enum tree_code code)
13434 int i;
13435 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13436 if (binop_lookup [i] == code)
13437 break;
13439 return i < BINOP_COMPOUND_CANDIDATES;
13442 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13444 static tree
13445 java_refold (tree t)
13447 tree c, b, ns, decl;
13449 if (TREE_CODE (t) != MODIFY_EXPR)
13450 return t;
13452 c = TREE_OPERAND (t, 1);
13453 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13454 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13455 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13456 return t;
13458 /* Now the left branch of the binary operator. */
13459 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13460 if (! (b && TREE_CODE (b) == NOP_EXPR
13461 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13462 return t;
13464 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13465 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13466 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13467 return t;
13469 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13470 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13471 /* It's got to be the an equivalent decl */
13472 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13474 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13475 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13476 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13477 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13478 /* Change the right part of the BINOP_EXPR */
13479 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13482 return t;
13485 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13486 errors but we modify NODE so that it contains the type computed
13487 according to the expression, when it's fixed. Otherwise, we write
13488 error_mark_node as the type. It allows us to further the analysis
13489 of remaining nodes and detects more errors in certain cases. */
13491 static tree
13492 patch_binop (tree node, tree wfl_op1, tree wfl_op2, int folding)
13494 tree op1 = TREE_OPERAND (node, 0);
13495 tree op2 = TREE_OPERAND (node, 1);
13496 tree op1_type = TREE_TYPE (op1);
13497 tree op2_type = TREE_TYPE (op2);
13498 tree prom_type = NULL_TREE, cn;
13499 enum tree_code code = TREE_CODE (node);
13501 /* If 1, tell the routine that we have to return error_mark_node
13502 after checking for the initialization of the RHS */
13503 int error_found = 0;
13505 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13507 /* If either op<n>_type are NULL, this might be early signs of an
13508 error situation, unless it's too early to tell (in case we're
13509 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13510 correctly so the error can be later on reported accurately. */
13511 if (! (code == PLUS_EXPR || code == NE_EXPR
13512 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13514 tree n;
13515 if (! op1_type)
13517 n = java_complete_tree (op1);
13518 op1_type = TREE_TYPE (n);
13520 if (! op2_type)
13522 n = java_complete_tree (op2);
13523 op2_type = TREE_TYPE (n);
13527 switch (code)
13529 /* 15.16 Multiplicative operators */
13530 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13531 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13532 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13533 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13534 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13536 if (!JNUMERIC_TYPE_P (op1_type))
13537 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13538 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13539 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13540 TREE_TYPE (node) = error_mark_node;
13541 error_found = 1;
13542 break;
13544 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13546 /* Detect integral division by zero */
13547 if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13548 && TREE_CODE (prom_type) == INTEGER_TYPE
13549 && (op2 == integer_zero_node || op2 == long_zero_node ||
13550 (TREE_CODE (op2) == INTEGER_CST &&
13551 ! TREE_INT_CST_LOW (op2) && ! TREE_INT_CST_HIGH (op2))))
13553 parse_warning_context
13554 (wfl_operator,
13555 "Evaluating this expression will result in an arithmetic exception being thrown");
13556 TREE_CONSTANT (node) = 0;
13557 TREE_INVARIANT (node) = 0;
13560 /* Change the division operator if necessary */
13561 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13562 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13564 /* Before divisions as is disappear, try to simplify and bail if
13565 applicable, otherwise we won't perform even simple
13566 simplifications like (1-1)/3. We can't do that with floating
13567 point number, folds can't handle them at this stage. */
13568 if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13569 && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13571 TREE_TYPE (node) = prom_type;
13572 node = fold (node);
13573 if (TREE_CODE (node) != code)
13574 return node;
13577 if (TREE_CODE (prom_type) == INTEGER_TYPE
13578 && flag_use_divide_subroutine
13579 && ! flag_emit_class_files
13580 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13581 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13583 /* This one is more complicated. FLOATs are processed by a
13584 function call to soft_fmod. Duplicate the value of the
13585 COMPOUND_ASSIGN_P flag. */
13586 if (code == TRUNC_MOD_EXPR)
13588 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13589 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13590 return mod;
13592 break;
13594 /* 15.17 Additive Operators */
13595 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13597 /* Operation is valid if either one argument is a string
13598 constant, a String object or a StringBuffer crafted for the
13599 purpose of the a previous usage of the String concatenation
13600 operator */
13602 if (TREE_CODE (op1) == STRING_CST
13603 || TREE_CODE (op2) == STRING_CST
13604 || JSTRING_TYPE_P (op1_type)
13605 || JSTRING_TYPE_P (op2_type)
13606 || IS_CRAFTED_STRING_BUFFER_P (op1)
13607 || IS_CRAFTED_STRING_BUFFER_P (op2))
13608 return build_string_concatenation (op1, op2);
13610 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13611 Numeric Types */
13612 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13614 if (!JNUMERIC_TYPE_P (op1_type))
13615 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13616 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13617 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13618 TREE_TYPE (node) = error_mark_node;
13619 error_found = 1;
13620 break;
13622 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13623 break;
13625 /* 15.18 Shift Operators */
13626 case LSHIFT_EXPR:
13627 case RSHIFT_EXPR:
13628 case URSHIFT_EXPR:
13629 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13631 if (!JINTEGRAL_TYPE_P (op1_type))
13632 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13633 else
13635 if (JNUMERIC_TYPE_P (op2_type))
13636 parse_error_context (wfl_operator,
13637 "Incompatible type for %qs. Explicit cast needed to convert shift distance from %qs to integral",
13638 operator_string (node),
13639 lang_printable_name (op2_type, 0));
13640 else
13641 parse_error_context (wfl_operator,
13642 "Incompatible type for %qs. Can't convert shift distance from %qs to integral",
13643 operator_string (node),
13644 lang_printable_name (op2_type, 0));
13646 TREE_TYPE (node) = error_mark_node;
13647 error_found = 1;
13648 break;
13651 /* Unary numeric promotion (5.6.1) is performed on each operand
13652 separately */
13653 op1 = do_unary_numeric_promotion (op1);
13654 op2 = do_unary_numeric_promotion (op2);
13656 /* If the right hand side is of type `long', first cast it to
13657 `int'. */
13658 if (TREE_TYPE (op2) == long_type_node)
13659 op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13661 /* The type of the shift expression is the type of the promoted
13662 type of the left-hand operand */
13663 prom_type = TREE_TYPE (op1);
13665 /* Shift int only up to 0x1f and long up to 0x3f */
13666 if (prom_type == int_type_node)
13667 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13668 build_int_cst (NULL_TREE, 0x1f)));
13669 else
13670 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13671 build_int_cst (NULL_TREE, 0x3f)));
13673 /* The >>> operator is a >> operating on unsigned quantities */
13674 if (code == URSHIFT_EXPR && (folding || ! flag_emit_class_files))
13676 tree to_return;
13677 tree utype = java_unsigned_type (prom_type);
13678 op1 = convert (utype, op1);
13680 to_return = fold_build2 (RSHIFT_EXPR, utype, op1, op2);
13681 to_return = convert (prom_type, to_return);
13682 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13683 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13684 TREE_SIDE_EFFECTS (to_return)
13685 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13686 return to_return;
13688 break;
13690 /* 15.19.1 Type Comparison Operator instanceof */
13691 case INSTANCEOF_EXPR:
13693 TREE_TYPE (node) = boolean_type_node;
13695 /* OP1_TYPE might be NULL when OP1 is a string constant. */
13696 if ((cn = patch_string (op1)))
13698 op1 = cn;
13699 op1_type = TREE_TYPE (op1);
13701 if (op1_type == NULL_TREE)
13702 abort ();
13704 if (!(op2_type = resolve_type_during_patch (op2)))
13705 return error_mark_node;
13707 /* The first operand must be a reference type or the null type */
13708 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13709 error_found = 1; /* Error reported further below */
13711 /* The second operand must be a reference type */
13712 if (!JREFERENCE_TYPE_P (op2_type))
13714 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13715 parse_error_context
13716 (wfl_operator, "Invalid argument %qs for %<instanceof%>",
13717 lang_printable_name (op2_type, 0));
13718 error_found = 1;
13721 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13723 /* If the first operand is null, the result is always false */
13724 if (op1 == null_pointer_node)
13725 return boolean_false_node;
13726 else if (flag_emit_class_files)
13728 TREE_OPERAND (node, 1) = op2_type;
13729 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13730 return node;
13732 /* Otherwise we have to invoke instance of to figure it out */
13733 else
13734 return build_instanceof (op1, op2_type);
13736 /* There is no way the expression operand can be an instance of
13737 the type operand. This is a compile time error. */
13738 else
13740 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13741 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13742 parse_error_context
13743 (wfl_operator, "Impossible for %qs to be instance of %qs",
13744 t1, lang_printable_name (op2_type, 0));
13745 free (t1);
13746 error_found = 1;
13749 break;
13751 /* 15.21 Bitwise and Logical Operators */
13752 case BIT_AND_EXPR:
13753 case BIT_XOR_EXPR:
13754 case BIT_IOR_EXPR:
13755 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13756 /* Binary numeric promotion is performed on both operand and the
13757 expression retain that type */
13758 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13760 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13761 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13762 /* The type of the bitwise operator expression is BOOLEAN */
13763 prom_type = boolean_type_node;
13764 else
13766 if (!JINTEGRAL_TYPE_P (op1_type))
13767 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13768 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13769 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13770 TREE_TYPE (node) = error_mark_node;
13771 error_found = 1;
13772 /* Insert a break here if adding thing before the switch's
13773 break for this case */
13775 break;
13777 /* 15.22 Conditional-And Operator */
13778 case TRUTH_ANDIF_EXPR:
13779 /* 15.23 Conditional-Or Operator */
13780 case TRUTH_ORIF_EXPR:
13781 /* Operands must be of BOOLEAN type */
13782 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13783 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13785 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13786 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13787 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13788 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13789 TREE_TYPE (node) = boolean_type_node;
13790 error_found = 1;
13791 break;
13793 else if (integer_zerop (op1))
13795 return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13797 else if (integer_onep (op1))
13799 return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13801 /* The type of the conditional operators is BOOLEAN */
13802 prom_type = boolean_type_node;
13803 break;
13805 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13806 case LT_EXPR:
13807 case GT_EXPR:
13808 case LE_EXPR:
13809 case GE_EXPR:
13810 /* The type of each of the operands must be a primitive numeric
13811 type */
13812 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13814 if (!JNUMERIC_TYPE_P (op1_type))
13815 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13816 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13817 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13818 TREE_TYPE (node) = boolean_type_node;
13819 error_found = 1;
13820 break;
13822 /* Binary numeric promotion is performed on the operands */
13823 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13824 /* The type of the relation expression is always BOOLEAN */
13825 prom_type = boolean_type_node;
13826 break;
13828 /* 15.20 Equality Operator */
13829 case EQ_EXPR:
13830 case NE_EXPR:
13831 /* It's time for us to patch the strings. */
13832 if ((cn = patch_string (op1)))
13834 op1 = cn;
13835 op1_type = TREE_TYPE (op1);
13837 if ((cn = patch_string (op2)))
13839 op2 = cn;
13840 op2_type = TREE_TYPE (op2);
13843 /* 15.20.1 Numerical Equality Operators == and != */
13844 /* Binary numeric promotion is performed on the operands */
13845 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13846 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13848 /* 15.20.2 Boolean Equality Operators == and != */
13849 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13850 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13851 ; /* Nothing to do here */
13853 /* 15.20.3 Reference Equality Operators == and != */
13854 /* Types have to be either references or the null type. If
13855 they're references, it must be possible to convert either
13856 type to the other by casting conversion. */
13857 else if ((op1 == null_pointer_node && op2 == null_pointer_node)
13858 || (op1 == null_pointer_node && JREFERENCE_TYPE_P (op2_type))
13859 || (JREFERENCE_TYPE_P (op1_type) && op2 == null_pointer_node)
13860 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13861 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13862 || valid_ref_assignconv_cast_p (op2_type,
13863 op1_type, 1))))
13864 ; /* Nothing to do here */
13866 /* Else we have an error figure what can't be converted into
13867 what and report the error */
13868 else
13870 char *t1;
13871 t1 = xstrdup (lang_printable_name (op1_type, 0));
13872 parse_error_context
13873 (wfl_operator,
13874 "Incompatible type for %qs. Can't convert %qs to %qs",
13875 operator_string (node), t1,
13876 lang_printable_name (op2_type, 0));
13877 free (t1);
13878 TREE_TYPE (node) = boolean_type_node;
13879 error_found = 1;
13880 break;
13882 prom_type = boolean_type_node;
13883 break;
13884 default:
13885 abort ();
13888 if (error_found)
13889 return error_mark_node;
13891 TREE_OPERAND (node, 0) = op1;
13892 TREE_OPERAND (node, 1) = op2;
13893 TREE_TYPE (node) = prom_type;
13894 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13896 /* fold does not respect side-effect order as required for Java but not C.
13897 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13898 * bytecode.
13900 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13901 : ! TREE_SIDE_EFFECTS (node))
13902 node = fold (node);
13903 return node;
13906 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13907 zero value, the value of CSTE comes after the valude of STRING */
13909 static tree
13910 do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13912 const char *old = TREE_STRING_POINTER (cste);
13913 int old_len = TREE_STRING_LENGTH (cste);
13914 int len = old_len + string_len;
13915 char *new = alloca (len+1);
13917 if (after)
13919 memcpy (new, string, string_len);
13920 memcpy (&new [string_len], old, old_len);
13922 else
13924 memcpy (new, old, old_len);
13925 memcpy (&new [old_len], string, string_len);
13927 new [len] = '\0';
13928 return build_string (len, new);
13931 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13932 new STRING_CST on success, NULL_TREE on failure. */
13934 static tree
13935 merge_string_cste (tree op1, tree op2, int after)
13937 /* Handle two string constants right away. */
13938 if (TREE_CODE (op2) == STRING_CST)
13939 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13940 TREE_STRING_LENGTH (op2), after);
13942 /* Reasonable integer constant can be treated right away. */
13943 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13945 static const char *const boolean_true = "true";
13946 static const char *const boolean_false = "false";
13947 static const char *const null_pointer = "null";
13948 char ch[4];
13949 const char *string;
13951 if (op2 == boolean_true_node)
13952 string = boolean_true;
13953 else if (op2 == boolean_false_node)
13954 string = boolean_false;
13955 else if (op2 == null_pointer_node
13956 || (integer_zerop (op2)
13957 && TREE_CODE (TREE_TYPE (op2)) == POINTER_TYPE))
13958 /* FIXME: null is not a compile-time constant, so it is only safe to
13959 merge if the overall expression is non-constant. However, this
13960 code always merges without checking the overall expression. */
13961 string = null_pointer;
13962 else if (TREE_TYPE (op2) == char_type_node)
13964 /* Convert the character into UTF-8. */
13965 unsigned int c = (unsigned int) TREE_INT_CST_LOW (op2);
13966 unsigned char *p = (unsigned char *) ch;
13967 if (0x01 <= c && c <= 0x7f)
13968 *p++ = (unsigned char) c;
13969 else if (c < 0x7ff)
13971 *p++ = (unsigned char) (c >> 6 | 0xc0);
13972 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13974 else
13976 *p++ = (unsigned char) (c >> 12 | 0xe0);
13977 *p++ = (unsigned char) (((c >> 6) & 0x3f) | 0x80);
13978 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13980 *p = '\0';
13982 string = ch;
13984 else
13985 string = string_convert_int_cst (op2);
13987 return do_merge_string_cste (op1, string, strlen (string), after);
13989 return NULL_TREE;
13992 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13993 has to be a STRING_CST and the other part must be a STRING_CST or a
13994 INTEGRAL constant. Return a new STRING_CST if the operation
13995 succeed, NULL_TREE otherwise.
13997 If the case we want to optimize for space, we might want to return
13998 NULL_TREE for each invocation of this routine. FIXME */
14000 static tree
14001 string_constant_concatenation (tree op1, tree op2)
14003 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
14005 tree string, rest;
14006 int invert;
14008 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
14009 rest = (string == op1 ? op2 : op1);
14010 invert = (string == op1 ? 0 : 1 );
14012 /* Walk REST, only if it looks reasonable */
14013 if (TREE_CODE (rest) != STRING_CST
14014 && !IS_CRAFTED_STRING_BUFFER_P (rest)
14015 && !JSTRING_TYPE_P (TREE_TYPE (rest))
14016 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
14018 rest = java_complete_tree (rest);
14019 if (rest == error_mark_node)
14020 return error_mark_node;
14021 rest = fold (rest);
14023 return merge_string_cste (string, rest, invert);
14025 return NULL_TREE;
14028 /* Implement the `+' operator. Does static optimization if possible,
14029 otherwise create (if necessary) and append elements to a
14030 StringBuffer. The StringBuffer will be carried around until it is
14031 used for a function call or an assignment. Then toString() will be
14032 called on it to turn it into a String object. */
14034 static tree
14035 build_string_concatenation (tree op1, tree op2)
14037 tree result;
14038 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
14040 /* Try to do some static optimization */
14041 if ((result = string_constant_concatenation (op1, op2)))
14042 return result;
14044 /* Discard empty strings on either side of the expression */
14045 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
14047 op1 = op2;
14048 op2 = NULL_TREE;
14050 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
14051 op2 = NULL_TREE;
14053 /* If operands are string constant, turn then into object references */
14054 if (TREE_CODE (op1) == STRING_CST)
14055 op1 = patch_string_cst (op1);
14056 if (op2 && TREE_CODE (op2) == STRING_CST)
14057 op2 = patch_string_cst (op2);
14059 /* If either one of the constant is null and the other non null
14060 operand is a String constant, return it. */
14061 if ((TREE_CODE (op1) == STRING_CST) && !op2)
14062 return op1;
14064 /* If OP1 isn't already a StringBuffer, create and
14065 initialize a new one */
14066 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
14068 /* Two solutions here:
14069 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
14070 2) OP1 is something else, we call new StringBuffer().append(OP1). */
14071 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
14072 op1 = BUILD_STRING_BUFFER (op1);
14073 else
14075 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
14076 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
14080 if (op2)
14082 /* OP1 is no longer the last node holding a crafted StringBuffer */
14083 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
14084 /* Create a node for `{new...,xxx}.append (op2)' */
14085 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
14088 /* Mark the last node holding a crafted StringBuffer */
14089 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
14091 TREE_SIDE_EFFECTS (op1) = side_effects;
14092 return op1;
14095 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
14096 StringBuffer. If no string were found to be patched, return
14097 NULL. */
14099 static tree
14100 patch_string (tree node)
14102 if (node == error_mark_node)
14103 return error_mark_node;
14104 if (TREE_CODE (node) == STRING_CST)
14105 return patch_string_cst (node);
14106 else if (IS_CRAFTED_STRING_BUFFER_P (node))
14108 int saved = ctxp->explicit_constructor_p;
14109 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
14110 tree ret;
14111 /* Temporary disable forbid the use of `this'. */
14112 ctxp->explicit_constructor_p = 0;
14113 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
14114 /* String concatenation arguments must be evaluated in order too. */
14115 ret = force_evaluation_order (ret);
14116 /* Restore it at its previous value */
14117 ctxp->explicit_constructor_p = saved;
14118 return ret;
14120 return NULL_TREE;
14123 /* Build the internal representation of a string constant. */
14125 static tree
14126 patch_string_cst (tree node)
14128 int location;
14129 if (! flag_emit_class_files)
14131 node = get_identifier (TREE_STRING_POINTER (node));
14132 location = alloc_name_constant (CONSTANT_String, node);
14133 node = build_ref_from_constant_pool (location);
14135 TREE_CONSTANT (node) = 1;
14136 TREE_INVARIANT (node) = 1;
14138 /* ??? Guessing that the class file code can't handle casts. */
14139 if (! flag_emit_class_files)
14140 node = convert (string_ptr_type_node, node);
14141 else
14142 TREE_TYPE (node) = string_ptr_type_node;
14144 return node;
14147 /* Build an incomplete unary operator expression. */
14149 static tree
14150 build_unaryop (int op_token, int op_location, tree op1)
14152 enum tree_code op;
14153 tree unaryop;
14154 switch (op_token)
14156 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14157 case MINUS_TK: op = NEGATE_EXPR; break;
14158 case NEG_TK: op = TRUTH_NOT_EXPR; break;
14159 case NOT_TK: op = BIT_NOT_EXPR; break;
14160 default: abort ();
14163 unaryop = build1 (op, NULL_TREE, op1);
14164 TREE_SIDE_EFFECTS (unaryop) = 1;
14165 /* Store the location of the operator, for better error report. The
14166 string of the operator will be rebuild based on the OP value. */
14167 EXPR_WFL_LINECOL (unaryop) = op_location;
14168 return unaryop;
14171 /* Special case for the ++/-- operators, since they require an extra
14172 argument to build, which is set to NULL and patched
14173 later. IS_POST_P is 1 if the operator, 0 otherwise. */
14175 static tree
14176 build_incdec (int op_token, int op_location, tree op1, int is_post_p)
14178 static const enum tree_code lookup [2][2] =
14180 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14181 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14183 tree node = build2 (lookup [is_post_p][(op_token - DECR_TK)],
14184 NULL_TREE, op1, NULL_TREE);
14185 TREE_SIDE_EFFECTS (node) = 1;
14186 /* Store the location of the operator, for better error report. The
14187 string of the operator will be rebuild based on the OP value. */
14188 EXPR_WFL_LINECOL (node) = op_location;
14189 return node;
14192 /* Build an incomplete cast operator, based on the use of the
14193 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14194 set. java_complete_tree is trained to walk a CONVERT_EXPR even
14195 though its type is already set. */
14197 static tree
14198 build_cast (int location, tree type, tree exp)
14200 tree node = build1 (CONVERT_EXPR, type, exp);
14201 EXPR_WFL_LINECOL (node) = location;
14202 return node;
14205 /* Build an incomplete class reference operator. */
14206 static tree
14207 build_incomplete_class_ref (int location, tree class_name)
14209 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14210 tree class_decl = GET_CPC ();
14211 tree this_class = TREE_TYPE (class_decl);
14213 /* Generate the synthetic static method `class$'. (Previously we
14214 deferred this, causing different method tables to be emitted
14215 for native code and bytecode.) */
14216 if (!TYPE_DOT_CLASS (this_class)
14217 && !JPRIMITIVE_TYPE_P (class_name)
14218 && !(TREE_CODE (class_name) == VOID_TYPE))
14220 tree cpc_list = GET_CPC_LIST();
14221 tree cpc = cpc_list;
14222 tree target_class;
14224 /* For inner classes, add a 'class$' method to their outermost
14225 context, creating it if necessary. */
14227 while (GET_NEXT_ENCLOSING_CPC(cpc))
14228 cpc = GET_NEXT_ENCLOSING_CPC(cpc);
14229 class_decl = TREE_VALUE (cpc);
14231 target_class = TREE_TYPE (class_decl);
14233 if (CLASS_INTERFACE (TYPE_NAME (target_class)))
14235 /* For interfaces, adding a static 'class$' method directly
14236 is illegal. So create an inner class to contain the new
14237 method. Empirically this matches the behavior of javac. */
14238 tree t, inner;
14239 /* We want the generated inner class inside the outermost class. */
14240 GET_CPC_LIST() = cpc;
14241 t = build_wfl_node (DECL_NAME (TYPE_NAME (object_type_node)));
14242 inner = create_anonymous_class (t);
14243 target_class = TREE_TYPE (inner);
14244 end_class_declaration (1);
14245 GET_CPC_LIST() = cpc_list;
14248 if (TYPE_DOT_CLASS (target_class) == NULL_TREE)
14249 build_dot_class_method (target_class);
14251 if (this_class != target_class)
14252 TYPE_DOT_CLASS (this_class) = TYPE_DOT_CLASS (target_class);
14255 EXPR_WFL_LINECOL (node) = location;
14256 return node;
14259 /* Complete an incomplete class reference operator. */
14260 static tree
14261 patch_incomplete_class_ref (tree node)
14263 tree type = TREE_OPERAND (node, 0);
14264 tree ref_type;
14266 if (!(ref_type = resolve_type_during_patch (type)))
14267 return error_mark_node;
14269 /* If we're not emitting class files and we know ref_type is a
14270 compiled class, build a direct reference. */
14271 if ((! flag_emit_class_files && is_compiled_class (ref_type))
14272 || JPRIMITIVE_TYPE_P (ref_type)
14273 || TREE_CODE (ref_type) == VOID_TYPE)
14275 tree dot = build_class_ref (ref_type);
14276 /* A class referenced by `foo.class' is initialized. */
14277 if (!flag_emit_class_files)
14278 dot = build_class_init (ref_type, dot);
14279 return java_complete_tree (dot);
14282 /* If we're emitting class files and we have to deal with non
14283 primitive types, we invoke the synthetic static method `class$'. */
14284 ref_type = build_dot_class_method_invocation (current_class, ref_type);
14285 return java_complete_tree (ref_type);
14288 /* 15.14 Unary operators. We return error_mark_node in case of error,
14289 but preserve the type of NODE if the type is fixed. */
14291 static tree
14292 patch_unaryop (tree node, tree wfl_op)
14294 tree op = TREE_OPERAND (node, 0);
14295 tree op_type = TREE_TYPE (op);
14296 tree prom_type = NULL_TREE, value, decl;
14297 int nested_field_flag = 0;
14298 int code = TREE_CODE (node);
14299 int error_found = 0;
14301 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14303 switch (code)
14305 /* 15.13.2 Postfix Increment Operator ++ */
14306 case POSTINCREMENT_EXPR:
14307 /* 15.13.3 Postfix Increment Operator -- */
14308 case POSTDECREMENT_EXPR:
14309 /* 15.14.1 Prefix Increment Operator ++ */
14310 case PREINCREMENT_EXPR:
14311 /* 15.14.2 Prefix Decrement Operator -- */
14312 case PREDECREMENT_EXPR:
14313 op = decl = extract_field_decl (op);
14314 nested_field_flag
14315 = nested_field_expanded_access_p (op, NULL, NULL, NULL);
14316 /* We might be trying to change an outer field accessed using
14317 access method. */
14318 if (nested_field_flag)
14320 /* Retrieve the decl of the field we're trying to access. We
14321 do that by first retrieving the function we would call to
14322 access the field. It has been already verified that this
14323 field isn't final */
14324 if (flag_emit_class_files)
14325 decl = TREE_OPERAND (op, 0);
14326 else
14327 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14328 decl = DECL_FUNCTION_ACCESS_DECL (decl);
14330 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14331 else if (!JDECL_P (decl)
14332 && TREE_CODE (decl) != COMPONENT_REF
14333 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14334 && TREE_CODE (decl) != INDIRECT_REF
14335 && !(TREE_CODE (decl) == COMPOUND_EXPR
14336 && TREE_OPERAND (decl, 1)
14337 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14339 TREE_TYPE (node) = error_mark_node;
14340 error_found = 1;
14343 /* From now on, we know that op if a variable and that it has a
14344 valid wfl. We use wfl_op to locate errors related to the
14345 ++/-- operand. */
14346 if (!JNUMERIC_TYPE_P (op_type))
14348 parse_error_context
14349 (wfl_op, "Invalid argument type %qs to %qs",
14350 lang_printable_name (op_type, 0), operator_string (node));
14351 TREE_TYPE (node) = error_mark_node;
14352 error_found = 1;
14354 else
14356 /* Before the addition, binary numeric promotion is performed on
14357 both operands, if really necessary */
14358 if (JINTEGRAL_TYPE_P (op_type))
14360 value = build_int_cst (op_type, 1);
14361 TREE_TYPE (node) = op_type;
14363 else
14365 value = build_int_cst (NULL_TREE, 1);
14366 TREE_TYPE (node) =
14367 binary_numeric_promotion (op_type,
14368 TREE_TYPE (value), &op, &value);
14371 /* We remember we might be accessing an outer field */
14372 if (nested_field_flag)
14374 /* We re-generate an access to the field */
14375 value = build2 (PLUS_EXPR, TREE_TYPE (op),
14376 build_nested_field_access (wfl_op, decl), value);
14378 /* And we patch the original access$() into a write
14379 with plus_op as a rhs */
14380 return nested_field_access_fix (node, op, value);
14383 /* And write back into the node. */
14384 TREE_OPERAND (node, 0) = op;
14385 TREE_OPERAND (node, 1) = value;
14386 /* Convert the overall back into its original type, if
14387 necessary, and return */
14388 if (JINTEGRAL_TYPE_P (op_type))
14389 return fold (node);
14390 else
14391 return fold (convert (op_type, node));
14393 break;
14395 /* 15.14.3 Unary Plus Operator + */
14396 case UNARY_PLUS_EXPR:
14397 /* 15.14.4 Unary Minus Operator - */
14398 case NEGATE_EXPR:
14399 if (!JNUMERIC_TYPE_P (op_type))
14401 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14402 TREE_TYPE (node) = error_mark_node;
14403 error_found = 1;
14405 /* Unary numeric promotion is performed on operand */
14406 else
14408 op = do_unary_numeric_promotion (op);
14409 prom_type = TREE_TYPE (op);
14410 if (code == UNARY_PLUS_EXPR)
14411 return fold (op);
14413 break;
14415 /* 15.14.5 Bitwise Complement Operator ~ */
14416 case BIT_NOT_EXPR:
14417 if (!JINTEGRAL_TYPE_P (op_type))
14419 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14420 TREE_TYPE (node) = error_mark_node;
14421 error_found = 1;
14423 else
14425 op = do_unary_numeric_promotion (op);
14426 prom_type = TREE_TYPE (op);
14428 break;
14430 /* 15.14.6 Logical Complement Operator ! */
14431 case TRUTH_NOT_EXPR:
14432 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14434 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14435 /* But the type is known. We will report an error if further
14436 attempt of a assignment is made with this rhs */
14437 TREE_TYPE (node) = boolean_type_node;
14438 error_found = 1;
14440 else
14441 prom_type = boolean_type_node;
14442 break;
14444 /* 15.15 Cast Expression */
14445 case CONVERT_EXPR:
14446 value = patch_cast (node, wfl_operator);
14447 if (value == error_mark_node)
14449 /* If this cast is part of an assignment, we tell the code
14450 that deals with it not to complain about a mismatch,
14451 because things have been cast, anyways */
14452 TREE_TYPE (node) = error_mark_node;
14453 error_found = 1;
14455 else
14457 value = fold (value);
14458 return value;
14460 break;
14462 case NOP_EXPR:
14463 /* This can only happen when the type is already known. */
14464 gcc_assert (TREE_TYPE (node) != NULL_TREE);
14465 prom_type = TREE_TYPE (node);
14466 break;
14469 if (error_found)
14470 return error_mark_node;
14472 /* There are cases where node has been replaced by something else
14473 and we don't end up returning here: UNARY_PLUS_EXPR,
14474 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14475 TREE_OPERAND (node, 0) = fold (op);
14476 TREE_TYPE (node) = prom_type;
14477 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14478 return fold (node);
14481 /* Generic type resolution that sometimes takes place during node
14482 patching. Returned the resolved type or generate an error
14483 message. Return the resolved type or NULL_TREE. */
14485 static tree
14486 resolve_type_during_patch (tree type)
14488 if (unresolved_type_p (type, NULL))
14490 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14491 if (!type_decl)
14493 parse_error_context (type,
14494 "Class %qs not found in type declaration",
14495 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14496 return NULL_TREE;
14499 check_deprecation (type, type_decl);
14501 return TREE_TYPE (type_decl);
14503 return type;
14506 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14507 found. Otherwise NODE or something meant to replace it is returned. */
14509 static tree
14510 patch_cast (tree node, tree wfl_op)
14512 tree op = TREE_OPERAND (node, 0);
14513 tree cast_type = TREE_TYPE (node);
14514 tree patched, op_type;
14515 char *t1;
14517 /* Some string patching might be necessary at this stage */
14518 if ((patched = patch_string (op)))
14519 TREE_OPERAND (node, 0) = op = patched;
14520 op_type = TREE_TYPE (op);
14522 /* First resolve OP_TYPE if unresolved */
14523 if (!(cast_type = resolve_type_during_patch (cast_type)))
14524 return error_mark_node;
14526 /* Check on cast that are proven correct at compile time */
14527 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14529 /* Same type */
14530 if (cast_type == op_type)
14531 return node;
14533 /* A narrowing conversion from a floating-point number to an
14534 integral type requires special handling (5.1.3). */
14535 if (JFLOAT_TYPE_P (op_type) && JINTEGRAL_TYPE_P (cast_type))
14536 if (cast_type != long_type_node)
14537 op = convert (integer_type_node, op);
14539 /* Try widening/narrowing conversion. Potentially, things need
14540 to be worked out in gcc so we implement the extreme cases
14541 correctly. fold_convert() needs to be fixed. */
14542 return convert (cast_type, op);
14545 /* It's also valid to cast a boolean into a boolean */
14546 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14547 return node;
14549 /* null can be casted to references */
14550 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14551 return build_null_of_type (cast_type);
14553 /* The remaining legal casts involve conversion between reference
14554 types. Check for their compile time correctness. */
14555 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14556 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14558 TREE_TYPE (node) = promote_type (cast_type);
14559 /* Now, the case can be determined correct at compile time if
14560 OP_TYPE can be converted into CAST_TYPE by assignment
14561 conversion (5.2) */
14563 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14565 TREE_SET_CODE (node, NOP_EXPR);
14566 return node;
14569 if (flag_emit_class_files)
14571 TREE_SET_CODE (node, CONVERT_EXPR);
14572 return node;
14575 /* The cast requires a run-time check */
14576 return build3 (CALL_EXPR, promote_type (cast_type),
14577 build_address_of (soft_checkcast_node),
14578 tree_cons (NULL_TREE, build_class_ref (cast_type),
14579 build_tree_list (NULL_TREE, op)),
14580 NULL_TREE);
14583 /* Any other casts are proven incorrect at compile time */
14584 t1 = xstrdup (lang_printable_name (op_type, 0));
14585 parse_error_context (wfl_op, "Invalid cast from %qs to %qs",
14586 t1, lang_printable_name (cast_type, 0));
14587 free (t1);
14588 return error_mark_node;
14591 /* Build a null constant and give it the type TYPE. */
14593 static tree
14594 build_null_of_type (tree type)
14596 tree node = build_int_cst (promote_type (type), 0);
14597 return node;
14600 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14601 a list of indices. */
14602 static tree
14603 build_array_ref (int location, tree array, tree index)
14605 tree node = build4 (ARRAY_REF, NULL_TREE, array, index,
14606 NULL_TREE, NULL_TREE);
14607 EXPR_WFL_LINECOL (node) = location;
14608 return node;
14611 /* 15.12 Array Access Expression */
14613 static tree
14614 patch_array_ref (tree node)
14616 tree array = TREE_OPERAND (node, 0);
14617 tree array_type = TREE_TYPE (array);
14618 tree index = TREE_OPERAND (node, 1);
14619 tree index_type = TREE_TYPE (index);
14620 int error_found = 0;
14622 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14624 if (TREE_CODE (array_type) == POINTER_TYPE)
14625 array_type = TREE_TYPE (array_type);
14627 /* The array reference must be an array */
14628 if (!TYPE_ARRAY_P (array_type))
14630 parse_error_context
14631 (wfl_operator,
14632 "%<[]%> can only be applied to arrays. It can't be applied to %qs",
14633 lang_printable_name (array_type, 0));
14634 TREE_TYPE (node) = error_mark_node;
14635 error_found = 1;
14638 /* The array index undergoes unary numeric promotion. The promoted
14639 type must be int */
14640 index = do_unary_numeric_promotion (index);
14641 if (TREE_TYPE (index) != int_type_node)
14643 if (valid_cast_to_p (index_type, int_type_node))
14644 parse_error_context (wfl_operator,
14645 "Incompatible type for %<[]%>. Explicit cast needed to convert %qs to %<int%>",
14646 lang_printable_name (index_type, 0));
14647 else
14648 parse_error_context (wfl_operator,
14649 "Incompatible type for %<[]%>. Can't convert %qs to %<int%>",
14650 lang_printable_name (index_type, 0));
14651 TREE_TYPE (node) = error_mark_node;
14652 error_found = 1;
14655 if (error_found)
14656 return error_mark_node;
14658 array_type = TYPE_ARRAY_ELEMENT (array_type);
14660 if (flag_emit_class_files)
14662 TREE_OPERAND (node, 0) = array;
14663 TREE_OPERAND (node, 1) = index;
14665 else
14666 node = build_java_arrayaccess (array, array_type, index);
14667 TREE_TYPE (node) = array_type;
14668 return node;
14671 /* 15.9 Array Creation Expressions */
14673 static tree
14674 build_newarray_node (tree type, tree dims, int extra_dims)
14676 tree node = build3 (NEW_ARRAY_EXPR, NULL_TREE, type,
14677 nreverse (dims),
14678 build_int_cst (NULL_TREE, extra_dims));
14679 return node;
14682 static tree
14683 patch_newarray (tree node)
14685 tree type = TREE_OPERAND (node, 0);
14686 tree dims = TREE_OPERAND (node, 1);
14687 tree cdim, array_type;
14688 int error_found = 0;
14689 int ndims = 0;
14690 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14692 /* Dimension types are verified. It's better for the types to be
14693 verified in order. */
14694 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14696 int dim_error = 0;
14697 tree dim = TREE_VALUE (cdim);
14699 /* Dim might have been saved during its evaluation */
14700 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14702 /* The type of each specified dimension must be an integral type. */
14703 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14704 dim_error = 1;
14706 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14707 promoted type must be int. */
14708 else
14710 dim = do_unary_numeric_promotion (dim);
14711 if (TREE_TYPE (dim) != int_type_node)
14712 dim_error = 1;
14715 /* Report errors on types here */
14716 if (dim_error)
14718 parse_error_context
14719 (TREE_PURPOSE (cdim),
14720 "Incompatible type for dimension in array creation expression. %s convert %qs to %<int%>",
14721 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14722 "Explicit cast needed to" : "Can't"),
14723 lang_printable_name (TREE_TYPE (dim), 0));
14724 error_found = 1;
14727 TREE_PURPOSE (cdim) = NULL_TREE;
14730 /* Resolve array base type if unresolved */
14731 if (!(type = resolve_type_during_patch (type)))
14732 error_found = 1;
14734 if (error_found)
14736 /* We don't want further evaluation of this bogus array creation
14737 operation */
14738 TREE_TYPE (node) = error_mark_node;
14739 return error_mark_node;
14742 /* Set array_type to the actual (promoted) array type of the result. */
14743 if (TREE_CODE (type) == RECORD_TYPE)
14744 type = build_pointer_type (type);
14745 while (--xdims >= 0)
14747 type = promote_type (build_java_array_type (type, -1));
14749 dims = nreverse (dims);
14750 array_type = type;
14751 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14753 type = array_type;
14754 array_type
14755 = build_java_array_type (type,
14756 TREE_CODE (cdim) == INTEGER_CST
14757 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14758 : -1);
14759 array_type = promote_type (array_type);
14761 dims = nreverse (dims);
14763 /* The node is transformed into a function call. Things are done
14764 differently according to the number of dimensions. If the number
14765 of dimension is equal to 1, then the nature of the base type
14766 (primitive or not) matters. */
14767 if (ndims == 1)
14768 return build_new_array (type, TREE_VALUE (dims));
14770 /* Can't reuse what's already written in expr.c because it uses the
14771 JVM stack representation. Provide a build_multianewarray. FIXME */
14772 return build3 (CALL_EXPR, array_type,
14773 build_address_of (soft_multianewarray_node),
14774 tree_cons (NULL_TREE,
14775 build_class_ref (TREE_TYPE (array_type)),
14776 tree_cons (NULL_TREE,
14777 build_int_cst (NULL_TREE, ndims),
14778 dims)),
14779 NULL_TREE);
14782 /* 10.6 Array initializer. */
14784 /* Build a wfl for array element that don't have one, so we can
14785 pin-point errors. */
14787 static tree
14788 maybe_build_array_element_wfl (tree node)
14790 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14792 /* FIXME - old code used "prev_lc.line" and "elc.prev_col */
14793 return build_expr_wfl (NULL_TREE,
14794 #ifdef USE_MAPPED_LOCATION
14795 input_location
14796 #else
14797 ctxp->filename,
14798 ctxp->lexer->token_start.line,
14799 ctxp->lexer->token_start.col
14800 #endif
14803 else
14804 return NULL_TREE;
14807 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14808 identification of initialized arrays easier to detect during walk
14809 and expansion. */
14811 static tree
14812 build_new_array_init (int location, tree values)
14814 tree constructor = build_constructor_from_list (NULL_TREE,
14815 nreverse (values));
14816 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14817 EXPR_WFL_LINECOL (to_return) = location;
14818 return to_return;
14821 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14822 occurred. Otherwise return NODE after having set its type
14823 appropriately. */
14825 static tree
14826 patch_new_array_init (tree type, tree node)
14828 int error_seen = 0;
14829 tree element_type;
14830 unsigned HOST_WIDE_INT length;
14831 constructor_elt *current;
14832 int all_constant = 1;
14833 tree init = TREE_OPERAND (node, 0);
14835 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14837 parse_error_context (node,
14838 "Invalid array initializer for non-array type %qs",
14839 lang_printable_name (type, 1));
14840 return error_mark_node;
14842 type = TREE_TYPE (type);
14843 element_type = TYPE_ARRAY_ELEMENT (type);
14845 for (length = 0;
14846 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init),
14847 length, current);
14848 length++)
14850 tree elt = current->value;
14851 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14853 error_seen |= array_constructor_check_entry (element_type, current);
14854 elt = current->value;
14855 /* When compiling to native code, STRING_CST is converted to
14856 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14857 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14858 all_constant = 0;
14860 else
14862 current->value = patch_new_array_init (element_type, elt);
14863 current->index = NULL_TREE;
14864 all_constant = 0;
14866 if (elt && TREE_CODE (elt) == TREE_LIST
14867 && TREE_VALUE (elt) == error_mark_node)
14868 error_seen = 1;
14871 if (error_seen)
14872 return error_mark_node;
14874 /* Create a new type. We can't reuse the one we have here by
14875 patching its dimension because it originally is of dimension -1
14876 hence reused by gcc. This would prevent triangular arrays. */
14877 type = build_java_array_type (element_type, length);
14878 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14879 TREE_TYPE (node) = promote_type (type);
14880 TREE_CONSTANT (init) = all_constant;
14881 TREE_INVARIANT (init) = all_constant;
14882 TREE_CONSTANT (node) = all_constant;
14883 TREE_INVARIANT (node) = all_constant;
14884 return node;
14887 /* Verify that one entry of the initializer element list can be
14888 assigned to the array base type. Report 1 if an error occurred, 0
14889 otherwise. */
14891 static int
14892 array_constructor_check_entry (tree type, constructor_elt *entry)
14894 char *array_type_string = NULL; /* For error reports */
14895 tree value, type_value, new_value, wfl_value, patched;
14896 int error_seen = 0;
14898 new_value = NULL_TREE;
14899 wfl_value = entry->value;
14901 value = java_complete_tree (entry->value);
14902 /* patch_string return error_mark_node if arg is error_mark_node */
14903 if ((patched = patch_string (value)))
14904 value = patched;
14905 if (value == error_mark_node)
14906 return 1;
14908 type_value = TREE_TYPE (value);
14910 /* At anytime, try_builtin_assignconv can report a warning on
14911 constant overflow during narrowing. */
14912 SET_WFL_OPERATOR (wfl_operator, entry->index, wfl_value);
14913 new_value = try_builtin_assignconv (wfl_operator, type, value);
14914 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14915 type_value = promote_type (type);
14917 /* Check and report errors */
14918 if (!new_value)
14920 const char *const msg = (!valid_cast_to_p (type_value, type) ?
14921 "Can't" : "Explicit cast needed to");
14922 if (!array_type_string)
14923 array_type_string = xstrdup (lang_printable_name (type, 1));
14924 parse_error_context
14925 (wfl_operator, "Incompatible type for array. %s convert %qs to %qs",
14926 msg, lang_printable_name (type_value, 1), array_type_string);
14927 error_seen = 1;
14930 if (new_value)
14931 entry->value = new_value;
14933 if (array_type_string)
14934 free (array_type_string);
14936 entry->index = NULL_TREE;
14937 return error_seen;
14940 static tree
14941 build_this (int location)
14943 tree node = build_wfl_node (this_identifier_node);
14944 TREE_SET_CODE (node, THIS_EXPR);
14945 EXPR_WFL_LINECOL (node) = location;
14946 return node;
14949 /* 14.15 The return statement. It builds a modify expression that
14950 assigns the returned value to the RESULT_DECL that hold the value
14951 to be returned. */
14953 static tree
14954 build_return (int location, tree op)
14956 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14957 EXPR_WFL_LINECOL (node) = location;
14958 node = build_debugable_stmt (location, node);
14959 return node;
14962 static tree
14963 patch_return (tree node)
14965 tree return_exp = TREE_OPERAND (node, 0);
14966 tree meth = current_function_decl;
14967 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14968 int error_found = 0;
14970 TREE_TYPE (node) = error_mark_node;
14971 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14973 /* It's invalid to have a return value within a function that is
14974 declared with the keyword void or that is a constructor */
14975 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14976 error_found = 1;
14978 /* It's invalid to use a return statement in a static block */
14979 if (DECL_CLINIT_P (current_function_decl))
14980 error_found = 1;
14982 /* It's invalid to have a no return value within a function that
14983 isn't declared with the keyword `void' */
14984 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14985 error_found = 2;
14987 if (DECL_INSTINIT_P (current_function_decl))
14988 error_found = 1;
14990 if (error_found)
14992 if (DECL_INSTINIT_P (current_function_decl))
14993 parse_error_context (wfl_operator,
14994 "%<return%> inside instance initializer");
14996 else if (DECL_CLINIT_P (current_function_decl))
14997 parse_error_context (wfl_operator,
14998 "%<return%> inside static initializer");
15000 else if (!DECL_CONSTRUCTOR_P (meth))
15002 char *t = xstrdup (lang_printable_name (mtype, 0));
15003 parse_error_context (wfl_operator,
15004 "%<return%> with%s value from %<%s %s%>",
15005 (error_found == 1 ? "" : "out"),
15006 t, lang_printable_name (meth, 2));
15007 free (t);
15009 else
15010 parse_error_context (wfl_operator,
15011 "%<return%> with value from constructor %qs",
15012 lang_printable_name (meth, 2));
15013 return error_mark_node;
15016 /* If we have a return_exp, build a modify expression and expand
15017 it. Note: at that point, the assignment is declared valid, but we
15018 may want to carry some more hacks */
15019 if (return_exp)
15021 tree exp = java_complete_tree (return_exp);
15022 tree modify, patched;
15024 if ((patched = patch_string (exp)))
15025 exp = patched;
15027 modify = build2 (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
15028 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
15029 modify = java_complete_tree (modify);
15031 if (modify != error_mark_node)
15033 TREE_SIDE_EFFECTS (modify) = 1;
15034 TREE_OPERAND (node, 0) = modify;
15036 else
15037 return error_mark_node;
15039 TREE_TYPE (node) = void_type_node;
15040 TREE_SIDE_EFFECTS (node) = 1;
15041 return node;
15044 /* 14.8 The if Statement */
15046 static tree
15047 build_if_else_statement (int location, tree expression, tree if_body,
15048 tree else_body)
15050 tree node;
15051 if (!else_body)
15052 else_body = build_java_empty_stmt ();
15053 node = build3 (COND_EXPR, NULL_TREE, expression, if_body, else_body);
15054 EXPR_WFL_LINECOL (node) = location;
15055 node = build_debugable_stmt (location, node);
15056 return node;
15059 static tree
15060 patch_if_else_statement (tree node)
15062 tree expression = TREE_OPERAND (node, 0);
15063 int can_complete_normally
15064 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15065 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
15067 TREE_TYPE (node) = error_mark_node;
15068 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15070 /* The type of expression must be boolean */
15071 if (TREE_TYPE (expression) != boolean_type_node
15072 && TREE_TYPE (expression) != promoted_boolean_type_node)
15074 parse_error_context
15075 (wfl_operator,
15076 "Incompatible type for %<if%>. Can't convert %qs to %<boolean%>",
15077 lang_printable_name (TREE_TYPE (expression), 0));
15078 return error_mark_node;
15081 TREE_TYPE (node) = void_type_node;
15082 TREE_SIDE_EFFECTS (node) = 1;
15083 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
15084 return node;
15087 /* 14.6 Labeled Statements */
15089 /* Action taken when a labeled statement is parsed. a new
15090 LABELED_BLOCK_EXPR is created. No statement is attached to the
15091 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
15093 static tree
15094 build_labeled_block (int location, tree label)
15096 tree label_name ;
15097 tree label_decl, node;
15098 if (label == NULL_TREE || label == continue_identifier_node)
15099 label_name = label;
15100 else
15102 label_name = merge_qualified_name (label_id, label);
15103 /* Issue an error if we try to reuse a label that was previously
15104 declared */
15105 if (IDENTIFIER_LOCAL_VALUE (label_name))
15107 EXPR_WFL_LINECOL (wfl_operator) = location;
15108 parse_error_context (wfl_operator,
15109 "Declaration of %qs shadows a previous label declaration",
15110 IDENTIFIER_POINTER (label));
15111 EXPR_WFL_LINECOL (wfl_operator) =
15112 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
15113 parse_error_context (wfl_operator,
15114 "This is the location of the previous declaration of label %qs",
15115 IDENTIFIER_POINTER (label));
15116 java_error_count--;
15120 label_decl = create_label_decl (label_name);
15121 node = build2 (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
15122 EXPR_WFL_LINECOL (node) = location;
15123 TREE_SIDE_EFFECTS (node) = 1;
15124 return node;
15127 /* A labeled statement LBE is attached a statement. */
15129 static tree
15130 finish_labeled_statement (tree lbe, /* Labeled block expr */
15131 tree statement)
15133 /* In anyways, tie the loop to its statement */
15134 LABELED_BLOCK_BODY (lbe) = statement;
15135 pop_labeled_block ();
15136 POP_LABELED_BLOCK ();
15137 return lbe;
15140 /* 14.10, 14.11, 14.12 Loop Statements */
15142 /* Create an empty LOOP_EXPR and make it the last in the nested loop
15143 list. */
15145 static tree
15146 build_new_loop (tree loop_body)
15148 tree loop = build1 (LOOP_EXPR, NULL_TREE, loop_body);
15149 TREE_SIDE_EFFECTS (loop) = 1;
15150 PUSH_LOOP (loop);
15151 return loop;
15154 /* Create a loop body according to the following structure:
15155 COMPOUND_EXPR
15156 COMPOUND_EXPR (loop main body)
15157 EXIT_EXPR (this order is for while/for loops.
15158 LABELED_BLOCK_EXPR the order is reversed for do loops)
15159 LABEL_DECL (a continue occurring here branches at the
15160 BODY end of this labeled block)
15161 INCREMENT (if any)
15163 REVERSED, if nonzero, tells that the loop condition expr comes
15164 after the body, like in the do-while loop.
15166 To obtain a loop, the loop body structure described above is
15167 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15169 LABELED_BLOCK_EXPR
15170 LABEL_DECL (use this label to exit the loop)
15171 LOOP_EXPR
15172 <structure described above> */
15174 static tree
15175 build_loop_body (int location, tree condition, int reversed)
15177 tree first, second, body;
15179 condition = build1 (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15180 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15181 condition = build_debugable_stmt (location, condition);
15182 TREE_SIDE_EFFECTS (condition) = 1;
15184 body = build_labeled_block (0, continue_identifier_node);
15185 first = (reversed ? body : condition);
15186 second = (reversed ? condition : body);
15187 return build2 (COMPOUND_EXPR, NULL_TREE,
15188 build2 (COMPOUND_EXPR, NULL_TREE, first, second),
15189 build_java_empty_stmt ());
15192 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15193 their order) on the current loop. Unlink the current loop from the
15194 loop list. */
15196 static tree
15197 finish_loop_body (int location, tree condition, tree body, int reversed)
15199 tree to_return = ctxp->current_loop;
15200 tree loop_body = LOOP_EXPR_BODY (to_return);
15201 if (condition)
15203 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15204 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15205 The real EXIT_EXPR is one operand further. */
15206 EXPR_WFL_LINECOL (cnode) = location;
15207 if (TREE_CODE (cnode) == EXPR_WITH_FILE_LOCATION)
15209 cnode = EXPR_WFL_NODE (cnode);
15210 /* This one is for accurate error reports */
15211 EXPR_WFL_LINECOL (cnode) = location;
15213 TREE_OPERAND (cnode, 0) = condition;
15215 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15216 POP_LOOP ();
15217 return to_return;
15220 /* Tailored version of finish_loop_body for FOR loops, when FOR
15221 loops feature the condition part */
15223 static tree
15224 finish_for_loop (int location, tree condition, tree update, tree body)
15226 /* Put the condition and the loop body in place */
15227 tree loop = finish_loop_body (location, condition, body, 0);
15228 /* LOOP is the current loop which has been now popped of the loop
15229 stack. Mark the update block as reachable and install it. We do
15230 this because the (current interpretation of the) JLS requires
15231 that the update expression be considered reachable even if the
15232 for loop's body doesn't complete normally. */
15233 if (update != NULL_TREE && !IS_EMPTY_STMT (update))
15235 tree up2 = update;
15236 if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15237 up2 = EXPR_WFL_NODE (up2);
15238 /* It is possible for the update expression to be an
15239 EXPR_WFL_NODE wrapping nothing. */
15240 if (up2 != NULL_TREE && !IS_EMPTY_STMT (up2))
15242 /* Try to detect constraint violations. These would be
15243 programming errors somewhere. */
15244 if (! EXPR_P (up2) || TREE_CODE (up2) == LOOP_EXPR)
15245 abort ();
15246 SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15249 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15250 return loop;
15253 /* Try to find the loop a block might be related to. This comprises
15254 the case where the LOOP_EXPR is found as the second operand of a
15255 COMPOUND_EXPR, because the loop happens to have an initialization
15256 part, then expressed as the first operand of the COMPOUND_EXPR. If
15257 the search finds something, 1 is returned. Otherwise, 0 is
15258 returned. The search is assumed to start from a
15259 LABELED_BLOCK_EXPR's block. */
15261 static tree
15262 search_loop (tree statement)
15264 if (TREE_CODE (statement) == LOOP_EXPR)
15265 return statement;
15267 if (TREE_CODE (statement) == BLOCK)
15268 statement = BLOCK_SUBBLOCKS (statement);
15269 else
15270 return NULL_TREE;
15272 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15273 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15274 statement = TREE_OPERAND (statement, 1);
15276 return (TREE_CODE (statement) == LOOP_EXPR
15277 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15280 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15281 returned otherwise. */
15283 static int
15284 labeled_block_contains_loop_p (tree block, tree loop)
15286 if (!block)
15287 return 0;
15289 if (LABELED_BLOCK_BODY (block) == loop)
15290 return 1;
15292 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15293 return 1;
15295 return 0;
15298 /* If the loop isn't surrounded by a labeled statement, create one and
15299 insert LOOP as its body. */
15301 static tree
15302 patch_loop_statement (tree loop)
15304 tree loop_label;
15306 TREE_TYPE (loop) = void_type_node;
15307 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15308 return loop;
15310 loop_label = build_labeled_block (0, NULL_TREE);
15311 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15312 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15313 LABELED_BLOCK_BODY (loop_label) = loop;
15314 PUSH_LABELED_BLOCK (loop_label);
15315 return loop_label;
15318 /* 14.13, 14.14: break and continue Statements */
15320 /* Build a break or a continue statement. a null NAME indicates an
15321 unlabeled break/continue statement. */
15323 static tree
15324 build_bc_statement (int location, int is_break, tree name)
15326 tree break_continue, label_block_expr = NULL_TREE;
15328 if (name)
15330 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15331 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15332 /* Null means that we don't have a target for this named
15333 break/continue. In this case, we make the target to be the
15334 label name, so that the error can be reported accurately in
15335 patch_bc_statement. */
15336 label_block_expr = EXPR_WFL_NODE (name);
15338 /* Unlabeled break/continue will be handled during the
15339 break/continue patch operation */
15340 break_continue = build1 (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr);
15342 IS_BREAK_STMT_P (break_continue) = is_break;
15343 TREE_SIDE_EFFECTS (break_continue) = 1;
15344 EXPR_WFL_LINECOL (break_continue) = location;
15345 break_continue = build_debugable_stmt (location, break_continue);
15346 return break_continue;
15349 /* Verification of a break/continue statement. */
15351 static tree
15352 patch_bc_statement (tree node)
15354 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15355 tree labeled_block = ctxp->current_labeled_block;
15356 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15358 /* Having an identifier here means that the target is unknown. */
15359 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15361 parse_error_context (wfl_operator, "No label definition found for %qs",
15362 IDENTIFIER_POINTER (bc_label));
15363 return error_mark_node;
15365 if (! IS_BREAK_STMT_P (node))
15367 /* It's a continue statement. */
15368 for (;; labeled_block = TREE_CHAIN (labeled_block))
15370 if (labeled_block == NULL_TREE)
15372 if (bc_label == NULL_TREE)
15373 parse_error_context (wfl_operator,
15374 "%<continue%> must be in loop");
15375 else
15376 parse_error_context
15377 (wfl_operator, "continue label %qs does not name a loop",
15378 IDENTIFIER_POINTER (bc_label));
15379 return error_mark_node;
15381 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15382 == continue_identifier_node)
15383 && (bc_label == NULL_TREE
15384 || TREE_CHAIN (labeled_block) == bc_label))
15386 bc_label = labeled_block;
15387 break;
15391 else if (!bc_label)
15393 for (;; labeled_block = TREE_CHAIN (labeled_block))
15395 if (labeled_block == NULL_TREE)
15397 parse_error_context (wfl_operator,
15398 "%<break%> must be in loop or switch");
15399 return error_mark_node;
15401 target_stmt = LABELED_BLOCK_BODY (labeled_block);
15402 if (TREE_CODE (target_stmt) == SWITCH_EXPR
15403 || search_loop (target_stmt))
15405 bc_label = labeled_block;
15406 break;
15411 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15412 CAN_COMPLETE_NORMALLY (bc_label) = 1;
15414 /* Our break/continue don't return values. */
15415 TREE_TYPE (node) = void_type_node;
15416 /* Encapsulate the break within a compound statement so that it's
15417 expanded all the times by expand_expr (and not clobbered
15418 sometimes, like after a if statement) */
15419 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15420 TREE_SIDE_EFFECTS (node) = 1;
15421 return node;
15424 /* Process the exit expression belonging to a loop. Its type must be
15425 boolean. */
15427 static tree
15428 patch_exit_expr (tree node)
15430 tree expression = TREE_OPERAND (node, 0);
15431 TREE_TYPE (node) = error_mark_node;
15432 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15434 /* The type of expression must be boolean */
15435 if (TREE_TYPE (expression) != boolean_type_node)
15437 parse_error_context
15438 (wfl_operator,
15439 "Incompatible type for loop conditional. Can't convert %qs to %<boolean%>",
15440 lang_printable_name (TREE_TYPE (expression), 0));
15441 return error_mark_node;
15443 /* Now we know things are allright, invert the condition, fold and
15444 return */
15445 TREE_OPERAND (node, 0) =
15446 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15448 if (! integer_zerop (TREE_OPERAND (node, 0))
15449 && ctxp->current_loop != NULL_TREE
15450 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15451 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15452 if (! integer_onep (TREE_OPERAND (node, 0)))
15453 CAN_COMPLETE_NORMALLY (node) = 1;
15456 TREE_TYPE (node) = void_type_node;
15457 return node;
15460 /* 14.9 Switch statement */
15462 static tree
15463 patch_switch_statement (tree node)
15465 tree se = TREE_OPERAND (node, 0), se_type;
15466 tree save, iter;
15468 /* Complete the switch expression */
15469 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15470 se_type = TREE_TYPE (se);
15471 /* The type of the switch expression must be char, byte, short or
15472 int */
15473 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15475 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15476 parse_error_context (wfl_operator,
15477 "Incompatible type for %<switch%>. Can't convert %qs to %<int%>",
15478 lang_printable_name (se_type, 0));
15479 /* This is what java_complete_tree will check */
15480 TREE_OPERAND (node, 0) = error_mark_node;
15481 return error_mark_node;
15484 /* Save and restore the outer case label list. */
15485 save = case_label_list;
15486 case_label_list = NULL_TREE;
15488 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15490 /* See if we've found a duplicate label. We can't leave this until
15491 code generation, because in `--syntax-only' and `-C' modes we
15492 don't do ordinary code generation. */
15493 for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15495 HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15496 tree subiter;
15497 for (subiter = TREE_CHAIN (iter);
15498 subiter != NULL_TREE;
15499 subiter = TREE_CHAIN (subiter))
15501 HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15502 if (val == subval)
15504 EXPR_WFL_LINECOL (wfl_operator)
15505 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15506 /* The case_label_list is in reverse order, so print the
15507 outer label first. */
15508 parse_error_context (wfl_operator, "duplicate case label: %<"
15509 HOST_WIDE_INT_PRINT_DEC "%>", subval);
15510 EXPR_WFL_LINECOL (wfl_operator)
15511 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15512 parse_error_context (wfl_operator, "original label is here");
15514 break;
15519 case_label_list = save;
15521 /* Ready to return */
15522 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15524 TREE_TYPE (node) = error_mark_node;
15525 return error_mark_node;
15527 TREE_TYPE (node) = void_type_node;
15528 TREE_SIDE_EFFECTS (node) = 1;
15529 CAN_COMPLETE_NORMALLY (node)
15530 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15531 || ! SWITCH_HAS_DEFAULT (node);
15532 return node;
15535 /* Assertions. */
15537 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15538 might be NULL_TREE. */
15539 static tree
15540 build_assertion (
15541 #ifdef USE_MAPPED_LOCATION
15542 source_location location,
15543 #else
15544 int location,
15545 #endif
15546 tree condition, tree value)
15548 tree node;
15549 tree klass = GET_CPC ();
15551 if (! enable_assertions (klass))
15553 condition = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15554 boolean_false_node, condition);
15555 if (value == NULL_TREE)
15556 value = build_java_empty_stmt ();
15557 return build_if_else_statement (location, condition,
15558 value, NULL_TREE);
15561 if (! CLASS_USES_ASSERTIONS (klass))
15563 tree field, classdollar, id, call;
15564 tree class_type = TREE_TYPE (klass);
15566 field = add_field (class_type,
15567 get_identifier ("$assertionsDisabled"),
15568 boolean_type_node,
15569 ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15570 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15571 FIELD_SYNTHETIC (field) = 1;
15573 classdollar = build_incomplete_class_ref (location, class_type);
15575 /* Call CLASS.desiredAssertionStatus(). */
15576 id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15577 call = build3 (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15578 call = make_qualified_primary (classdollar, call, location);
15579 TREE_SIDE_EFFECTS (call) = 1;
15581 /* Invert to obtain !CLASS.desiredAssertionStatus(). This may
15582 seem odd, but we do it to generate code identical to that of
15583 the JDK. */
15584 call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15585 TREE_SIDE_EFFECTS (call) = 1;
15586 DECL_INITIAL (field) = call;
15588 /* Record the initializer in the initializer statement list. */
15589 call = build2 (MODIFY_EXPR, NULL_TREE, field, call);
15590 TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15591 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15592 MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15594 CLASS_USES_ASSERTIONS (klass) = 1;
15597 if (value != NULL_TREE)
15598 value = tree_cons (NULL_TREE, value, NULL_TREE);
15600 node = build_wfl_node (get_identifier ("java"));
15601 node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15602 location);
15603 node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15604 location);
15606 node = build3 (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15607 TREE_SIDE_EFFECTS (node) = 1;
15608 /* It is too early to use BUILD_THROW. */
15609 node = build1 (THROW_EXPR, NULL_TREE, node);
15610 TREE_SIDE_EFFECTS (node) = 1;
15612 /* We invert the condition; if we just put NODE as the `else' part
15613 then we generate weird-looking bytecode. */
15614 condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15615 /* Check $assertionsDisabled. */
15616 condition
15617 = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15618 build1 (TRUTH_NOT_EXPR, NULL_TREE,
15619 build_wfl_node (get_identifier ("$assertionsDisabled"))),
15620 condition);
15621 node = build_if_else_statement (location, condition, node, NULL_TREE);
15622 return node;
15625 /* 14.18 The try/catch statements */
15627 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15628 catches TYPE and executes CATCH_STMTS. */
15630 static tree
15631 encapsulate_with_try_catch (int location, tree type_or_name, tree try_stmts,
15632 tree catch_stmts)
15634 tree try_block, catch_clause_param, catch_block, catch;
15636 /* First build a try block */
15637 try_block = build_expr_block (try_stmts, NULL_TREE);
15639 /* Build a catch block: we need a catch clause parameter */
15640 if (TREE_CODE (type_or_name) == EXPR_WITH_FILE_LOCATION)
15642 tree catch_type = obtain_incomplete_type (type_or_name);
15643 jdep *dep;
15644 catch_clause_param = build_decl (VAR_DECL, wpv_id, catch_type);
15645 register_incomplete_type (JDEP_VARIABLE, type_or_name,
15646 catch_clause_param, catch_type);
15647 dep = CLASSD_LAST (ctxp->classd_list);
15648 JDEP_GET_PATCH (dep) = &TREE_TYPE (catch_clause_param);
15650 else
15651 catch_clause_param = build_decl (VAR_DECL, wpv_id,
15652 build_pointer_type (type_or_name));
15654 /* And a block */
15655 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15657 /* Initialize the variable and store in the block */
15658 catch = build2 (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15659 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15660 add_stmt_to_block (catch_block, NULL_TREE, catch);
15662 /* Add the catch statements */
15663 add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15665 /* Now we can build a JAVA_CATCH_EXPR */
15666 catch_block = build1 (JAVA_CATCH_EXPR, NULL_TREE, catch_block);
15668 return build_try_statement (location, try_block, catch_block);
15671 static tree
15672 build_try_statement (int location, tree try_block, tree catches)
15674 tree node = build2 (TRY_EXPR, NULL_TREE, try_block, catches);
15675 EXPR_WFL_LINECOL (node) = location;
15676 return node;
15679 static tree
15680 build_try_finally_statement (int location, tree try_block, tree finally)
15682 tree node = build2 (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15683 EXPR_WFL_LINECOL (node) = location;
15684 return node;
15687 static tree
15688 patch_try_statement (tree node)
15690 int error_found = 0;
15691 tree try = TREE_OPERAND (node, 0);
15692 /* Exception handlers are considered in left to right order */
15693 tree catch = nreverse (TREE_OPERAND (node, 1));
15694 tree current, caught_type_list = NULL_TREE;
15696 /* Check catch clauses, if any. Every time we find an error, we try
15697 to process the next catch clause. We process the catch clause before
15698 the try block so that when processing the try block we can check thrown
15699 exceptions against the caught type list. */
15700 for (current = catch; current; current = TREE_CHAIN (current))
15702 tree carg_decl, carg_type;
15703 tree sub_current, catch_block, catch_clause;
15704 int unreachable;
15706 /* At this point, the structure of the catch clause is
15707 JAVA_CATCH_EXPR (catch node)
15708 BLOCK (with the decl of the parameter)
15709 COMPOUND_EXPR
15710 MODIFY_EXPR (assignment of the catch parameter)
15711 BLOCK (catch clause block)
15713 catch_clause = TREE_OPERAND (current, 0);
15714 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15715 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15717 /* Catch clauses can't have more than one parameter declared,
15718 but it's already enforced by the grammar. Make sure that the
15719 only parameter of the clause statement in of class Throwable
15720 or a subclass of Throwable, but that was done earlier. The
15721 catch clause parameter type has also been resolved. */
15723 /* Just make sure that the catch clause parameter type inherits
15724 from java.lang.Throwable */
15725 if (!inherits_from_p (carg_type, throwable_type_node))
15727 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15728 parse_error_context (wfl_operator,
15729 "Can't catch class %qs. Catch clause parameter type must be a subclass of class %<java.lang.Throwable%>",
15730 lang_printable_name (carg_type, 0));
15731 error_found = 1;
15732 continue;
15735 /* Partial check for unreachable catch statement: The catch
15736 clause is reachable iff is no earlier catch block A in
15737 the try statement such that the type of the catch
15738 clause's parameter is the same as or a subclass of the
15739 type of A's parameter */
15740 unreachable = 0;
15741 for (sub_current = catch;
15742 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15744 tree sub_catch_clause, decl;
15745 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15746 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15748 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15750 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15751 parse_error_context
15752 (wfl_operator,
15753 "%<catch%> not reached because of the catch clause at line %d",
15754 EXPR_WFL_LINENO (sub_current));
15755 unreachable = error_found = 1;
15756 break;
15759 /* Complete the catch clause block */
15760 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15761 if (catch_block == error_mark_node)
15763 error_found = 1;
15764 continue;
15766 if (CAN_COMPLETE_NORMALLY (catch_block))
15767 CAN_COMPLETE_NORMALLY (node) = 1;
15768 TREE_OPERAND (current, 0) = catch_block;
15770 if (unreachable)
15771 continue;
15773 /* Things to do here: the exception must be thrown */
15775 /* Link this type to the caught type list */
15776 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15779 PUSH_EXCEPTIONS (caught_type_list);
15780 if ((try = java_complete_tree (try)) == error_mark_node)
15781 error_found = 1;
15782 if (CAN_COMPLETE_NORMALLY (try))
15783 CAN_COMPLETE_NORMALLY (node) = 1;
15784 POP_EXCEPTIONS ();
15786 /* Verification ends here */
15787 if (error_found)
15788 return error_mark_node;
15790 TREE_OPERAND (node, 0) = try;
15791 TREE_OPERAND (node, 1) = catch;
15792 TREE_TYPE (node) = void_type_node;
15793 return node;
15796 /* 14.17 The synchronized Statement */
15798 static tree
15799 patch_synchronized_statement (tree node, tree wfl_op1)
15801 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15802 tree block = TREE_OPERAND (node, 1);
15804 tree tmp, enter, exit, expr_decl, assignment;
15806 if (expr == error_mark_node)
15808 block = java_complete_tree (block);
15809 return expr;
15812 /* We might be trying to synchronize on a STRING_CST */
15813 if ((tmp = patch_string (expr)))
15814 expr = tmp;
15816 /* The TYPE of expr must be a reference type */
15817 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15819 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15820 parse_error_context (wfl_operator, "Incompatible type for %<synchronized%>. Can't convert %qs to %<java.lang.Object%>",
15821 lang_printable_name (TREE_TYPE (expr), 0));
15822 return error_mark_node;
15825 /* Generate a try-finally for the synchronized statement, except
15826 that the handler that catches all throw exception calls
15827 _Jv_MonitorExit and then rethrow the exception.
15828 The synchronized statement is then implemented as:
15831 _Jv_MonitorEnter (expression)
15832 synchronized_block
15833 _Jv_MonitorExit (expression)
15835 CATCH_ALL
15837 e = _Jv_exception_info ();
15838 _Jv_MonitorExit (expression)
15839 Throw (e);
15840 } */
15842 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15843 BUILD_MONITOR_ENTER (enter, expr_decl);
15844 BUILD_MONITOR_EXIT (exit, expr_decl);
15845 CAN_COMPLETE_NORMALLY (enter) = 1;
15846 CAN_COMPLETE_NORMALLY (exit) = 1;
15847 assignment = build2 (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15848 TREE_SIDE_EFFECTS (assignment) = 1;
15849 node = build2 (COMPOUND_EXPR, NULL_TREE,
15850 build2 (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15851 build2 (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15852 node = build_expr_block (node, expr_decl);
15854 return java_complete_tree (node);
15857 /* 14.16 The throw Statement */
15859 static tree
15860 patch_throw_statement (tree node, tree wfl_op1)
15862 tree expr = TREE_OPERAND (node, 0);
15863 tree type = TREE_TYPE (expr);
15864 int unchecked_ok = 0, tryblock_throws_ok = 0;
15866 /* Thrown expression must be assignable to java.lang.Throwable */
15867 if (!try_reference_assignconv (throwable_type_node, expr))
15869 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15870 parse_error_context (wfl_operator,
15871 "Can't throw %qs; it must be a subclass of class %<java.lang.Throwable%>",
15872 lang_printable_name (type, 0));
15873 /* If the thrown expression was a reference, we further the
15874 compile-time check. */
15875 if (!JREFERENCE_TYPE_P (type))
15876 return error_mark_node;
15879 /* At least one of the following must be true */
15881 /* The type of the throw expression is a not checked exception,
15882 i.e. is a unchecked expression. */
15883 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15885 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15886 /* An instance can't throw a checked exception unless that exception
15887 is explicitly declared in the `throws' clause of each
15888 constructor. This doesn't apply to anonymous classes, since they
15889 don't have declared constructors. */
15890 if (!unchecked_ok
15891 && DECL_INSTINIT_P (current_function_decl)
15892 && !ANONYMOUS_CLASS_P (current_class))
15894 tree current;
15895 for (current = TYPE_METHODS (current_class); current;
15896 current = TREE_CHAIN (current))
15897 if (DECL_CONSTRUCTOR_P (current)
15898 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15900 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)",
15901 lang_printable_name (TREE_TYPE (expr), 0));
15902 return error_mark_node;
15906 /* Throw is contained in a try statement and at least one catch
15907 clause can receive the thrown expression or the current method is
15908 declared to throw such an exception. Or, the throw statement is
15909 contained in a method or constructor declaration and the type of
15910 the Expression is assignable to at least one type listed in the
15911 throws clause the declaration. */
15912 if (!unchecked_ok)
15913 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15914 if (!(unchecked_ok || tryblock_throws_ok))
15916 /* If there is a surrounding try block that has no matching
15917 clatch clause, report it first. A surrounding try block exits
15918 only if there is something after the list of checked
15919 exception thrown by the current function (if any). */
15920 if (IN_TRY_BLOCK_P ())
15921 parse_error_context (wfl_operator, "Checked exception %qs can't be caught by any of the catch clause(s) of the surrounding %<try%> block",
15922 lang_printable_name (type, 0));
15923 /* If we have no surrounding try statement and the method doesn't have
15924 any throws, report it now. FIXME */
15926 /* We report that the exception can't be throw from a try block
15927 in all circumstances but when the `throw' is inside a static
15928 block. */
15929 else if (!EXCEPTIONS_P (currently_caught_type_list)
15930 && !tryblock_throws_ok)
15932 if (DECL_CLINIT_P (current_function_decl))
15933 parse_error_context (wfl_operator,
15934 "Checked exception %qs can't be thrown in initializer",
15935 lang_printable_name (type, 0));
15936 else
15937 parse_error_context (wfl_operator,
15938 "Checked exception %qs isn't thrown from a %<try%> block",
15939 lang_printable_name (type, 0));
15941 /* Otherwise, the current method doesn't have the appropriate
15942 throws declaration */
15943 else
15944 parse_error_context (wfl_operator, "Checked exception %qs doesn't match any of current method's %<throws%> declaration(s)",
15945 lang_printable_name (type, 0));
15946 return error_mark_node;
15949 if (! flag_emit_class_files)
15950 BUILD_THROW (node, expr);
15952 return node;
15955 /* Add EXCEPTION to the throws clause of MDECL. If MDECL already throws
15956 a super-class of EXCEPTION, keep the superclass instead. If MDECL already
15957 throws a sub-class of EXCEPTION, replace the sub-class with EXCEPTION. */
15958 static void
15959 add_exception_to_throws (tree mdecl, tree exception)
15961 tree mthrows;
15963 /* Ignore unchecked exceptions. */
15964 if (IS_UNCHECKED_EXCEPTION_P (exception))
15965 return;
15967 for (mthrows = DECL_FUNCTION_THROWS (mdecl);
15968 mthrows; mthrows = TREE_CHAIN (mthrows))
15970 if (inherits_from_p (exception, TREE_VALUE (mthrows)))
15971 return;
15972 if (inherits_from_p (TREE_VALUE (mthrows), exception))
15974 TREE_VALUE (mthrows) = exception;
15975 return;
15979 mthrows = DECL_FUNCTION_THROWS (mdecl);
15980 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (mthrows, exception);
15983 /* Check that exception said to be thrown by method DECL can be
15984 effectively caught from where DECL is invoked. THIS_EXPR is the
15985 expression that computes `this' for the method call. */
15986 static void
15987 check_thrown_exceptions (
15988 #ifdef USE_MAPPED_LOCATION
15989 source_location location,
15990 #else
15992 int location,
15993 #endif
15994 tree decl, tree this_expr)
15996 tree throws;
15997 int is_array_call = 0;
15999 /* Skip check within generated methods, such as access$<n>. */
16000 if (NESTED_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (current_function_decl)))
16001 return;
16003 if (this_expr != NULL_TREE
16004 && TREE_CODE (TREE_TYPE (this_expr)) == POINTER_TYPE
16005 && TYPE_ARRAY_P (TREE_TYPE (TREE_TYPE (this_expr))))
16006 is_array_call = 1;
16008 /* For all the unchecked exceptions thrown by DECL. */
16009 for (throws = DECL_FUNCTION_THROWS (decl); throws;
16010 throws = TREE_CHAIN (throws))
16011 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
16013 /* Suppress errors about cloning arrays. */
16014 if (is_array_call && DECL_NAME (decl) == get_identifier ("clone"))
16015 continue;
16017 #ifdef USE_MAPPED_LOCATION
16018 SET_EXPR_LOCATION (wfl_operator, location);
16019 #else
16020 EXPR_WFL_LINECOL (wfl_operator) = location;
16021 #endif
16022 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (current_function_decl))
16023 && (DECL_FINIT_P (current_function_decl)
16024 || DECL_INIT_P (current_function_decl)
16025 || DECL_CONSTRUCTOR_P (current_function_decl)))
16027 /* Add "throws" to the initializer's exception list */
16028 tree exception = TREE_VALUE (throws);
16029 add_exception_to_throws (current_function_decl, exception);
16031 else if (DECL_FINIT_P (current_function_decl))
16033 parse_error_context
16034 (wfl_operator, "Exception %qs can't be thrown in initializer",
16035 lang_printable_name (TREE_VALUE (throws), 0));
16037 else
16039 parse_error_context
16040 (wfl_operator, "Exception %qs must be caught, or it must be declared in the %<throws%> clause of %qs",
16041 lang_printable_name (TREE_VALUE (throws), 0),
16042 (DECL_INIT_P (current_function_decl) ?
16043 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
16044 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
16049 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
16050 try-catch blocks, OR is listed in the `throws' clause of the
16051 current method. */
16053 static int
16054 check_thrown_exceptions_do (tree exception)
16056 tree list = currently_caught_type_list;
16057 resolve_and_layout (exception, NULL_TREE);
16058 /* First, all the nested try-catch-finally at that stage. The
16059 last element contains `throws' clause exceptions, if any. */
16060 if (IS_UNCHECKED_EXCEPTION_P (exception))
16061 return 1;
16062 while (list)
16064 tree caught;
16065 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
16066 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
16067 return 1;
16068 list = TREE_CHAIN (list);
16070 return 0;
16073 /* This function goes over all of CLASS_TYPE ctors and checks whether
16074 each of them features at least one unchecked exception in its
16075 `throws' clause. If it's the case, it returns `true', `false'
16076 otherwise. */
16078 static bool
16079 ctors_unchecked_throws_clause_p (tree class_type)
16081 tree current;
16083 for (current = TYPE_METHODS (class_type); current;
16084 current = TREE_CHAIN (current))
16086 bool ctu = false; /* Ctor Throws Unchecked */
16087 if (DECL_CONSTRUCTOR_P (current))
16089 tree throws;
16090 for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
16091 throws = TREE_CHAIN (throws))
16092 if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
16093 ctu = true;
16095 /* We return false as we found one ctor that is unfit. */
16096 if (!ctu && DECL_CONSTRUCTOR_P (current))
16097 return false;
16099 /* All ctors feature at least one unchecked exception in their
16100 `throws' clause. */
16101 return true;
16104 /* 15.24 Conditional Operator ?: */
16106 static tree
16107 patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
16109 tree cond = TREE_OPERAND (node, 0);
16110 tree op1 = TREE_OPERAND (node, 1);
16111 tree op2 = TREE_OPERAND (node, 2);
16112 tree resulting_type = NULL_TREE;
16113 tree t1, t2, patched;
16114 int error_found = 0;
16116 /* The condition and operands of ?: might be StringBuffers crafted
16117 as a result of a string concatenation. Obtain decent ones here. */
16118 if ((patched = patch_string (cond)))
16119 TREE_OPERAND (node, 0) = cond = patched;
16120 if ((patched = patch_string (op1)))
16121 TREE_OPERAND (node, 1) = op1 = patched;
16122 if ((patched = patch_string (op2)))
16123 TREE_OPERAND (node, 2) = op2 = patched;
16125 t1 = TREE_TYPE (op1);
16126 t2 = TREE_TYPE (op2);
16128 /* The first expression must be a boolean */
16129 if (TREE_TYPE (cond) != boolean_type_node)
16131 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
16132 parse_error_context (wfl_operator,
16133 "Incompatible type for %<?:%>. Can't convert %qs to %<boolean%>",
16134 lang_printable_name (TREE_TYPE (cond), 0));
16135 error_found = 1;
16138 /* Second and third can be numeric, boolean (i.e. primitive),
16139 references or null. Anything else results in an error */
16140 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
16141 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
16142 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
16143 || (t1 == boolean_type_node && t2 == boolean_type_node)))
16144 error_found = 1;
16146 /* Determine the type of the conditional expression. Same types are
16147 easy to deal with */
16148 else if (t1 == t2)
16149 resulting_type = t1;
16151 /* There are different rules for numeric types */
16152 else if (JNUMERIC_TYPE_P (t1))
16154 /* if byte/short found, the resulting type is short */
16155 if ((t1 == byte_type_node && t2 == short_type_node)
16156 || (t1 == short_type_node && t2 == byte_type_node))
16157 resulting_type = short_type_node;
16159 /* If t1 is a constant int and t2 is of type byte, short or char
16160 and t1's value fits in t2, then the resulting type is t2 */
16161 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
16162 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
16163 resulting_type = t2;
16165 /* If t2 is a constant int and t1 is of type byte, short or char
16166 and t2's value fits in t1, then the resulting type is t1 */
16167 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
16168 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
16169 resulting_type = t1;
16171 /* Otherwise, binary numeric promotion is applied and the
16172 resulting type is the promoted type of operand 1 and 2 */
16173 else
16174 resulting_type = binary_numeric_promotion (t1, t2,
16175 &TREE_OPERAND (node, 1),
16176 &TREE_OPERAND (node, 2));
16179 /* Cases of a reference and a null type */
16180 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
16181 resulting_type = t1;
16183 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
16184 resulting_type = t2;
16186 /* Last case: different reference types. If a type can be converted
16187 into the other one by assignment conversion, the latter
16188 determines the type of the expression */
16189 else if ((resulting_type = try_reference_assignconv (t1, op2)))
16190 resulting_type = promote_type (t1);
16192 else if ((resulting_type = try_reference_assignconv (t2, op1)))
16193 resulting_type = promote_type (t2);
16195 /* If we don't have any resulting type, we're in trouble */
16196 if (!resulting_type)
16198 char *t = xstrdup (lang_printable_name (t1, 0));
16199 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16200 parse_error_context (wfl_operator,
16201 "Incompatible type for %<?:%>. Can't convert %qs to %qs",
16202 t, lang_printable_name (t2, 0));
16203 free (t);
16204 error_found = 1;
16207 if (error_found)
16209 TREE_TYPE (node) = error_mark_node;
16210 return error_mark_node;
16213 TREE_TYPE (node) = resulting_type;
16214 TREE_SET_CODE (node, COND_EXPR);
16215 CAN_COMPLETE_NORMALLY (node) = 1;
16216 return node;
16219 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16221 static tree
16222 maybe_build_class_init_for_field (tree decl, tree expr)
16224 tree clas = DECL_CONTEXT (decl);
16225 if (flag_emit_class_files)
16226 return expr;
16228 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16229 && FIELD_FINAL (decl))
16231 tree init = DECL_INITIAL (decl);
16232 if (init != NULL_TREE)
16233 init = fold_constant_for_init (init, decl);
16234 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16235 return expr;
16238 return build_class_init (clas, expr);
16241 /* Try to constant fold NODE.
16242 If NODE is not a constant expression, return NULL_EXPR.
16243 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16245 static tree
16246 fold_constant_for_init (tree node, tree context)
16248 tree op0, op1, val;
16249 enum tree_code code = TREE_CODE (node);
16251 switch (code)
16253 case INTEGER_CST:
16254 if (node == null_pointer_node)
16255 return NULL_TREE;
16256 case STRING_CST:
16257 case REAL_CST:
16258 return node;
16260 case PLUS_EXPR:
16261 case MINUS_EXPR:
16262 case MULT_EXPR:
16263 case TRUNC_MOD_EXPR:
16264 case RDIV_EXPR:
16265 case LSHIFT_EXPR:
16266 case RSHIFT_EXPR:
16267 case URSHIFT_EXPR:
16268 case BIT_AND_EXPR:
16269 case BIT_XOR_EXPR:
16270 case BIT_IOR_EXPR:
16271 case TRUTH_ANDIF_EXPR:
16272 case TRUTH_ORIF_EXPR:
16273 case EQ_EXPR:
16274 case NE_EXPR:
16275 case GT_EXPR:
16276 case GE_EXPR:
16277 case LT_EXPR:
16278 case LE_EXPR:
16279 op0 = TREE_OPERAND (node, 0);
16280 op1 = TREE_OPERAND (node, 1);
16281 val = fold_constant_for_init (op0, context);
16282 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16283 return NULL_TREE;
16284 TREE_OPERAND (node, 0) = val;
16285 val = fold_constant_for_init (op1, context);
16286 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16287 return NULL_TREE;
16288 TREE_OPERAND (node, 1) = val;
16289 return patch_binop (node, op0, op1, 1);
16291 case UNARY_PLUS_EXPR:
16292 case NEGATE_EXPR:
16293 case TRUTH_NOT_EXPR:
16294 case BIT_NOT_EXPR:
16295 case CONVERT_EXPR:
16296 case NOP_EXPR:
16297 op0 = TREE_OPERAND (node, 0);
16298 val = fold_constant_for_init (op0, context);
16299 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16300 return NULL_TREE;
16301 TREE_OPERAND (node, 0) = val;
16302 val = patch_unaryop (node, op0);
16303 if (! TREE_CONSTANT (val))
16304 return NULL_TREE;
16305 return val;
16307 break;
16309 case COND_EXPR:
16310 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16311 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16312 return NULL_TREE;
16313 TREE_OPERAND (node, 0) = val;
16314 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16315 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16316 return NULL_TREE;
16317 TREE_OPERAND (node, 1) = val;
16318 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16319 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16320 return NULL_TREE;
16321 TREE_OPERAND (node, 2) = val;
16322 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 2)
16323 : TREE_OPERAND (node, 1);
16325 case VAR_DECL:
16326 case FIELD_DECL:
16327 if (! FIELD_FINAL (node)
16328 || DECL_INITIAL (node) == NULL_TREE)
16329 return NULL_TREE;
16330 val = DECL_INITIAL (node);
16331 /* Guard against infinite recursion. */
16332 DECL_INITIAL (node) = NULL_TREE;
16333 val = fold_constant_for_init (val, node);
16334 if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16335 val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16336 DECL_INITIAL (node) = val;
16337 return val;
16339 case EXPR_WITH_FILE_LOCATION:
16340 /* Compare java_complete_tree and resolve_expression_name. */
16341 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16342 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16344 tree name = EXPR_WFL_NODE (node);
16345 tree decl;
16346 if (PRIMARY_P (node))
16347 return NULL_TREE;
16348 else if (! QUALIFIED_P (name))
16350 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16351 if (decl == NULL_TREE
16352 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16353 return NULL_TREE;
16354 return fold_constant_for_init (decl, decl);
16356 else
16358 tree r = NULL_TREE;
16359 /* Install the proper context for the field resolution. */
16360 tree saved_current_class = current_class;
16361 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
16362 current_class = DECL_CONTEXT (context);
16363 qualify_ambiguous_name (node);
16364 r = resolve_field_access (node, &decl, NULL);
16365 /* Restore prior context. */
16366 current_class = saved_current_class;
16367 if (r != error_mark_node && decl != NULL_TREE)
16368 return fold_constant_for_init (decl, decl);
16369 return NULL_TREE;
16372 else
16374 op0 = TREE_OPERAND (node, 0);
16375 val = fold_constant_for_init (op0, context);
16376 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16377 return NULL_TREE;
16378 TREE_OPERAND (node, 0) = val;
16379 return val;
16382 #ifdef USE_COMPONENT_REF
16383 case IDENTIFIER:
16384 case COMPONENT_REF:
16386 #endif
16388 default:
16389 return NULL_TREE;
16393 #ifdef USE_COMPONENT_REF
16394 /* Context is 'T' for TypeName, 'P' for PackageName,
16395 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16397 tree
16398 resolve_simple_name (tree name, int context)
16402 tree
16403 resolve_qualified_name (tree name, int context)
16406 #endif
16408 void
16409 init_src_parse (void)
16411 /* Sanity check; we've been bit by this before. */
16412 if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16413 abort ();
16418 /* This section deals with the functions that are called when tables
16419 recording class initialization information are traversed. */
16421 /* This function is called for each class that is known definitely
16422 initialized when a given static method was called. This function
16423 augments a compound expression (INFO) storing all assignment to
16424 initialized static class flags if a flag already existed, otherwise
16425 a new one is created. */
16427 static int
16428 emit_test_initialization (void **entry_p, void *info)
16430 tree l = (tree) info;
16431 tree decl, init;
16432 tree key = (tree) *entry_p;
16433 tree *ite;
16434 htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16436 /* If we haven't found a flag and we're dealing with self registered
16437 with current_function_decl, then don't do anything. Self is
16438 always added as definitely initialized but this information is
16439 valid only if used outside the current function. */
16440 if (current_function_decl == TREE_PURPOSE (l)
16441 && java_treetreehash_find (cf_ht, key) == NULL)
16442 return true;
16444 ite = java_treetreehash_new (cf_ht, key);
16446 /* If we don't have a variable, create one and install it. */
16447 if (*ite == NULL)
16449 tree block;
16451 decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16452 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16453 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16454 DECL_CONTEXT (decl) = current_function_decl;
16455 DECL_INITIAL (decl) = boolean_true_node;
16456 /* Don't emit any symbolic debugging info for this decl. */
16457 DECL_IGNORED_P (decl) = 1;
16459 /* The trick is to find the right context for it. */
16460 block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16461 TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16462 BLOCK_EXPR_DECLS (block) = decl;
16463 *ite = decl;
16465 else
16466 decl = *ite;
16468 /* Now simply augment the compound that holds all the assignments
16469 pertaining to this method invocation. */
16470 init = build2 (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16471 TREE_SIDE_EFFECTS (init) = 1;
16472 TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16473 TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16475 return true;
16478 #ifdef __XGETTEXT__
16479 /* Depending on the version of Bison used to compile this grammar,
16480 it may issue generic diagnostics spelled "syntax error" or
16481 "parse error". To prevent this from changing the translation
16482 template randomly, we list all the variants of this particular
16483 diagnostic here. Translators: there is no fine distinction
16484 between diagnostics with "syntax error" in them, and diagnostics
16485 with "parse error" in them. It's okay to give them both the same
16486 translation. */
16487 const char d1[] = N_("syntax error");
16488 const char d2[] = N_("parse error");
16489 const char d3[] = N_("syntax error; also virtual memory exhausted");
16490 const char d4[] = N_("parse error; also virtual memory exhausted");
16491 const char d5[] = N_("syntax error: cannot back up");
16492 const char d6[] = N_("parse error: cannot back up");
16493 #endif
16495 #include "gt-java-parse.h"
16496 #include "gtype-java.h"