Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / java / parse.y
blobcc30c8e97563855b5257ef64fd25dc2e3ae73195
1 /* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
5 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc. */
28 /* This file parses java source code and issues a tree node image
29 suitable for code generation (byte code and targeted CPU assembly
30 language).
32 The grammar conforms to the Java grammar described in "The Java(TM)
33 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
34 1996, ISBN 0-201-63451-1"
36 The following modifications were brought to the original grammar:
38 method_body: added the rule '| block SC_TK'
39 static_initializer: added the rule 'static block SC_TK'.
41 Note: All the extra rules described above should go away when the
42 empty_statement rule will work.
44 statement_nsi: 'nsi' should be read no_short_if.
46 Some rules have been modified to support JDK1.1 inner classes
47 definitions and other extensions. */
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include <dirent.h>
55 #include "tree.h"
56 #include "rtl.h"
57 #include "real.h"
58 #include "obstack.h"
59 #include "toplev.h"
60 #include "pretty-print.h"
61 #include "diagnostic.h"
62 #include "flags.h"
63 #include "java-tree.h"
64 #include "jcf.h"
65 #include "lex.h"
66 #include "parse.h"
67 #include "zipfile.h"
68 #include "convert.h"
69 #include "buffer.h"
70 #include "xref.h"
71 #include "function.h"
72 #include "except.h"
73 #include "ggc.h"
74 #include "debug.h"
75 #include "tree-inline.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 *msgid, ...);
101 #ifdef USE_MAPPED_LOCATION
102 static void issue_warning_error_from_context
103 (source_location, const char *msgid, va_list *);
104 #else
105 static void issue_warning_error_from_context
106 (tree, const char *msgid, 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 void check_inner_class_access (tree, tree, tree);
116 static int check_pkg_class_access (tree, tree, bool, tree);
117 static void register_package (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);
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 #ifdef USE_MAPPED_LOCATION
238 static void check_thrown_exceptions (source_location, tree, tree);
239 #else
240 static void check_thrown_exceptions (int, tree, tree);
241 #endif
242 static int check_thrown_exceptions_do (tree);
243 static void purge_unchecked_exceptions (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 int verify_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, tree);
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 (htab_t, 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_outer_field_access (tree, tree);
326 static tree build_outer_field_access_methods (tree);
327 static tree build_outer_field_access_expr (int, tree, tree,
328 tree, tree);
329 static tree build_outer_method_access_method (tree);
330 static tree build_new_access_id (void);
331 static tree build_outer_field_access_method (tree, tree, tree,
332 tree, tree);
334 static int outer_field_access_p (tree, tree);
335 static int outer_field_expanded_access_p (tree, tree *,
336 tree *, tree *);
337 static tree outer_field_access_fix (tree, tree, tree);
338 static tree build_incomplete_class_ref (int, tree);
339 static tree patch_incomplete_class_ref (tree);
340 static tree create_anonymous_class (tree);
341 static void patch_anonymous_class (tree, tree, tree);
342 static void add_inner_class_fields (tree, tree);
344 static tree build_dot_class_method (tree);
345 static tree build_dot_class_method_invocation (tree, tree);
346 static void create_new_parser_context (int);
347 static tree maybe_build_class_init_for_field (tree, tree);
349 static int emit_test_initialization (void **, void *);
351 static char *string_convert_int_cst (tree);
353 /* Number of error found so far. */
354 int java_error_count;
355 /* Number of warning found so far. */
356 int java_warning_count;
357 /* Tell when not to fold, when doing xrefs */
358 int do_not_fold;
359 /* Cyclic inheritance report, as it can be set by layout_class */
360 const char *cyclic_inheritance_report;
362 /* The current parser context */
363 struct parser_ctxt *ctxp;
365 /* List of things that were analyzed for which code will be generated */
366 struct parser_ctxt *ctxp_for_generation = NULL;
368 /* binop_lookup maps token to tree_code. It is used where binary
369 operations are involved and required by the parser. RDIV_EXPR
370 covers both integral/floating point division. The code is changed
371 once the type of both operator is worked out. */
373 static const enum tree_code binop_lookup[19] =
375 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
376 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
377 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
378 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
379 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
381 #define BINOP_LOOKUP(VALUE) \
382 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
384 /* This is the end index for binary operators that can also be used
385 in compound assignments. */
386 #define BINOP_COMPOUND_CANDIDATES 11
388 /* The "$L" identifier we use to create labels. */
389 static GTY(()) tree label_id;
391 /* The "StringBuffer" identifier used for the String `+' operator. */
392 static GTY(()) tree wfl_string_buffer;
394 /* The "append" identifier used for String `+' operator. */
395 static GTY(()) tree wfl_append;
397 /* The "toString" identifier used for String `+' operator. */
398 static GTY(()) tree wfl_to_string;
400 /* The "java.lang" import qualified name. */
401 static GTY(()) tree java_lang_id;
403 /* The generated `inst$' identifier used for generated enclosing
404 instance/field access functions. */
405 static GTY(()) tree inst_id;
407 /* Context and flag for static blocks */
408 static GTY(()) tree current_static_block;
410 /* The generated `write_parm_value$' identifier. */
411 static GTY(()) tree wpv_id;
413 /* The list of all packages we've seen so far */
414 static GTY(()) tree package_list;
416 /* Hold THIS for the scope of the current method decl. */
417 static GTY(()) tree current_this;
419 /* Hold a list of catch clauses list. The first element of this list is
420 the list of the catch clauses of the currently analyzed try block. */
421 static GTY(()) tree currently_caught_type_list;
423 /* This holds a linked list of all the case labels for the current
424 switch statement. It is only used when checking to see if there
425 are duplicate labels. FIXME: probably this should just be attached
426 to the switch itself; then it could be referenced via
427 `ctxp->current_loop'. */
428 static GTY(()) tree case_label_list;
430 /* Anonymous class counter. Will be reset to 1 every time a non
431 anonymous class gets created. */
432 static int anonymous_class_counter = 1;
434 static GTY(()) tree src_parse_roots[1];
436 /* All classes seen from source code */
437 #define gclass_list src_parse_roots[0]
439 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
440 line and point it out. */
441 /* Should point out the one that don't fit. ASCII/unicode, going
442 backward. FIXME */
444 #define check_modifiers(__message, __value, __mask) do { \
445 if ((__value) & ~(__mask)) \
447 size_t i, remainder = (__value) & ~(__mask); \
448 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++) \
449 if ((1 << i) & remainder) \
450 parse_error_context (ctxp->modifier_ctx [i], (__message), \
451 java_accstring_lookup (1 << i)); \
453 } while (0)
457 %union {
458 tree node;
459 int sub_token;
460 struct {
461 int token;
462 #ifdef USE_MAPPED_LOCATION
463 source_location location;
464 #else
465 int location;
466 #endif
467 } operator;
468 int value;
472 #ifdef USE_MAPPED_LOCATION
473 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
474 SET_EXPR_LOCATION(EXPR, (TOKEN).location)
475 #else
476 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
477 (EXPR_WFL_LINECOL (EXPR) = (TOKEN).location)
478 #endif
480 #include "lex.c"
483 %pure_parser
485 /* Things defined here have to match the order of what's in the
486 binop_lookup table. */
488 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
489 %token LS_TK SRS_TK ZRS_TK
490 %token AND_TK XOR_TK OR_TK
491 %token BOOL_AND_TK BOOL_OR_TK
492 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
494 /* This maps to the same binop_lookup entry than the token above */
496 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
497 %token REM_ASSIGN_TK
498 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
499 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
502 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
504 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
505 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
506 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
507 %token PAD_TK ABSTRACT_TK STRICT_TK
508 %token MODIFIER_TK
510 /* Keep those two in order, too */
511 %token DECR_TK INCR_TK
513 /* From now one, things can be in any order */
515 %token DEFAULT_TK IF_TK THROW_TK
516 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
517 %token THROWS_TK BREAK_TK IMPORT_TK
518 %token ELSE_TK INSTANCEOF_TK RETURN_TK
519 %token VOID_TK CATCH_TK INTERFACE_TK
520 %token CASE_TK EXTENDS_TK FINALLY_TK
521 %token SUPER_TK WHILE_TK CLASS_TK
522 %token SWITCH_TK CONST_TK TRY_TK
523 %token FOR_TK NEW_TK CONTINUE_TK
524 %token GOTO_TK PACKAGE_TK THIS_TK
525 %token ASSERT_TK
527 %token BYTE_TK SHORT_TK INT_TK LONG_TK
528 %token CHAR_TK INTEGRAL_TK
530 %token FLOAT_TK DOUBLE_TK FP_TK
532 %token ID_TK
534 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
536 %token ASSIGN_ANY_TK ASSIGN_TK
537 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
539 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
540 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
542 %type <value> modifiers MODIFIER_TK final synchronized
544 %type <node> super ID_TK identifier
545 %type <node> name simple_name qualified_name
546 %type <node> type_declaration compilation_unit
547 field_declaration method_declaration extends_interfaces
548 interfaces interface_type_list
549 import_declarations package_declaration
550 type_declarations interface_body
551 interface_member_declaration constant_declaration
552 interface_member_declarations interface_type
553 abstract_method_declaration
554 %type <node> class_body_declaration class_member_declaration
555 static_initializer constructor_declaration block
556 %type <node> class_body_declarations constructor_header
557 %type <node> class_or_interface_type class_type class_type_list
558 constructor_declarator explicit_constructor_invocation
559 %type <node> dim_expr dim_exprs this_or_super throws
561 %type <node> variable_declarator_id variable_declarator
562 variable_declarators variable_initializer
563 variable_initializers constructor_body
564 array_initializer
566 %type <node> class_body block_end constructor_block_end
567 %type <node> statement statement_without_trailing_substatement
568 labeled_statement if_then_statement label_decl
569 if_then_else_statement while_statement for_statement
570 statement_nsi labeled_statement_nsi do_statement
571 if_then_else_statement_nsi while_statement_nsi
572 for_statement_nsi statement_expression_list for_init
573 for_update statement_expression expression_statement
574 primary_no_new_array expression primary array_type
575 array_creation_initialized array_creation_uninitialized
576 class_instance_creation_expression field_access
577 method_invocation array_access something_dot_new
578 argument_list postfix_expression while_expression
579 post_increment_expression post_decrement_expression
580 unary_expression_not_plus_minus unary_expression
581 pre_increment_expression pre_decrement_expression
582 cast_expression
583 multiplicative_expression additive_expression
584 shift_expression relational_expression
585 equality_expression and_expression
586 exclusive_or_expression inclusive_or_expression
587 conditional_and_expression conditional_or_expression
588 conditional_expression assignment_expression
589 left_hand_side assignment for_header for_begin
590 constant_expression do_statement_begin empty_statement
591 switch_statement synchronized_statement throw_statement
592 try_statement assert_statement
593 switch_expression switch_block
594 catches catch_clause catch_clause_parameter finally
595 anonymous_class_creation trap_overflow_corner_case
596 %type <node> return_statement break_statement continue_statement
598 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
599 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
600 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
601 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
602 %type <operator> ASSIGN_ANY_TK assignment_operator
603 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
604 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
605 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
606 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
607 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
608 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
609 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
610 %type <operator> NEW_TK ASSERT_TK
612 %type <node> method_body
614 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
615 STRING_LIT_TK NULL_TK VOID_TK
617 %type <node> IF_TK WHILE_TK FOR_TK
619 %type <node> formal_parameter_list formal_parameter
620 method_declarator method_header
622 %type <node> primitive_type reference_type type
623 BOOLEAN_TK INTEGRAL_TK FP_TK
625 /* Added or modified JDK 1.1 rule types */
626 %type <node> type_literals
629 /* 19.2 Production from 2.3: The Syntactic Grammar */
630 goal: compilation_unit
634 /* 19.3 Productions from 3: Lexical structure */
635 literal:
636 INT_LIT_TK
637 | FP_LIT_TK
638 | BOOL_LIT_TK
639 | CHAR_LIT_TK
640 | STRING_LIT_TK
641 | NULL_TK
644 /* 19.4 Productions from 4: Types, Values and Variables */
645 type:
646 primitive_type
647 | reference_type
650 primitive_type:
651 INTEGRAL_TK
652 | FP_TK
653 | BOOLEAN_TK
656 reference_type:
657 class_or_interface_type
658 | array_type
661 class_or_interface_type:
662 name
665 class_type:
666 class_or_interface_type /* Default rule */
669 interface_type:
670 class_or_interface_type
673 array_type:
674 primitive_type dims
676 int osb = pop_current_osb (ctxp);
677 tree t = build_java_array_type (($1), -1);
678 while (--osb)
679 t = build_unresolved_array_type (t);
680 $$ = t;
682 | name dims
684 int osb = pop_current_osb (ctxp);
685 tree t = $1;
686 while (osb--)
687 t = build_unresolved_array_type (t);
688 $$ = t;
692 /* 19.5 Productions from 6: Names */
693 name:
694 simple_name /* Default rule */
695 | qualified_name /* Default rule */
698 simple_name:
699 identifier /* Default rule */
702 qualified_name:
703 name DOT_TK identifier
704 { $$ = make_qualified_name ($1, $3, $2.location); }
707 identifier:
708 ID_TK
711 /* 19.6: Production from 7: Packages */
712 compilation_unit:
713 {$$ = NULL;}
714 | package_declaration
715 | import_declarations
716 | type_declarations
717 | package_declaration import_declarations
718 | package_declaration type_declarations
719 | import_declarations type_declarations
720 | package_declaration import_declarations type_declarations
723 import_declarations:
724 import_declaration
726 $$ = NULL;
728 | import_declarations import_declaration
730 $$ = NULL;
734 type_declarations:
735 type_declaration
736 | type_declarations type_declaration
739 package_declaration:
740 PACKAGE_TK name SC_TK
742 ctxp->package = EXPR_WFL_NODE ($2);
743 register_package (ctxp->package);
745 | PACKAGE_TK error
746 {yyerror ("Missing name"); RECOVER;}
747 | PACKAGE_TK name error
748 {yyerror ("';' expected"); RECOVER;}
751 import_declaration:
752 single_type_import_declaration
753 | type_import_on_demand_declaration
756 single_type_import_declaration:
757 IMPORT_TK name SC_TK
759 tree name = EXPR_WFL_NODE ($2), last_name;
760 int i = IDENTIFIER_LENGTH (name)-1;
761 const char *last = &IDENTIFIER_POINTER (name)[i];
762 while (last != IDENTIFIER_POINTER (name))
764 if (last [0] == '.')
765 break;
766 last--;
768 last_name = get_identifier (++last);
769 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
771 tree err = find_name_in_single_imports (last_name);
772 if (err && err != name)
773 parse_error_context
774 ($2, "Ambiguous class: %qs and %qs",
775 IDENTIFIER_POINTER (name),
776 IDENTIFIER_POINTER (err));
777 else
778 REGISTER_IMPORT ($2, last_name);
780 else
781 REGISTER_IMPORT ($2, last_name);
783 | IMPORT_TK error
784 {yyerror ("Missing name"); RECOVER;}
785 | IMPORT_TK name error
786 {yyerror ("';' expected"); RECOVER;}
789 type_import_on_demand_declaration:
790 IMPORT_TK name DOT_TK MULT_TK SC_TK
792 tree name = EXPR_WFL_NODE ($2);
793 tree it;
794 /* Search for duplicates. */
795 for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
796 if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
797 break;
798 /* Don't import the same thing more than once, just ignore
799 duplicates (7.5.2) */
800 if (! it)
802 read_import_dir ($2);
803 ctxp->import_demand_list =
804 chainon (ctxp->import_demand_list,
805 build_tree_list ($2, NULL_TREE));
808 | IMPORT_TK name DOT_TK error
809 {yyerror ("'*' expected"); RECOVER;}
810 | IMPORT_TK name DOT_TK MULT_TK error
811 {yyerror ("';' expected"); RECOVER;}
814 type_declaration:
815 class_declaration
816 { end_class_declaration (0); }
817 | interface_declaration
818 { end_class_declaration (0); }
819 | empty_statement
820 | error
822 YYERROR_NOW;
823 yyerror ("Class or interface declaration expected");
827 /* 19.7 Shortened from the original:
828 modifiers: modifier | modifiers modifier
829 modifier: any of public... */
830 modifiers:
831 MODIFIER_TK
833 $$ = (1 << $1);
835 | modifiers MODIFIER_TK
837 int acc = (1 << $2);
838 if ($$ & acc)
839 parse_error_context
840 (ctxp->modifier_ctx [$2], "Modifier %qs declared twice",
841 java_accstring_lookup (acc));
842 else
844 $$ |= acc;
849 /* 19.8.1 Production from $8.1: Class Declaration */
850 class_declaration:
851 modifiers CLASS_TK identifier super interfaces
852 { create_class ($1, $3, $4, $5); }
853 class_body
855 | CLASS_TK identifier super interfaces
856 { create_class (0, $2, $3, $4); }
857 class_body
859 | modifiers CLASS_TK error
860 { yyerror ("Missing class name"); RECOVER; }
861 | CLASS_TK error
862 { yyerror ("Missing class name"); RECOVER; }
863 | CLASS_TK identifier error
865 if (!ctxp->class_err) yyerror ("'{' expected");
866 DRECOVER(class1);
868 | modifiers CLASS_TK identifier error
869 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
872 super:
873 { $$ = NULL; }
874 | EXTENDS_TK class_type
875 { $$ = $2; }
876 | EXTENDS_TK class_type error
877 {yyerror ("'{' expected"); ctxp->class_err=1;}
878 | EXTENDS_TK error
879 {yyerror ("Missing super class name"); ctxp->class_err=1;}
882 interfaces:
883 { $$ = NULL_TREE; }
884 | IMPLEMENTS_TK interface_type_list
885 { $$ = $2; }
886 | IMPLEMENTS_TK error
888 ctxp->class_err=1;
889 yyerror ("Missing interface name");
893 interface_type_list:
894 interface_type
896 ctxp->interface_number = 1;
897 $$ = build_tree_list ($1, NULL_TREE);
899 | interface_type_list C_TK interface_type
901 ctxp->interface_number++;
902 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
904 | interface_type_list C_TK error
905 {yyerror ("Missing interface name"); RECOVER;}
908 class_body:
909 OCB_TK CCB_TK
911 /* Store the location of the `}' when doing xrefs */
912 if (flag_emit_xref)
913 DECL_END_SOURCE_LINE (GET_CPC ()) = $2.location;
914 $$ = GET_CPC ();
916 | OCB_TK class_body_declarations CCB_TK
918 /* Store the location of the `}' when doing xrefs */
919 if (flag_emit_xref)
920 DECL_END_SOURCE_LINE (GET_CPC ()) = $3.location;
921 $$ = GET_CPC ();
925 class_body_declarations:
926 class_body_declaration
927 | class_body_declarations class_body_declaration
930 class_body_declaration:
931 class_member_declaration
932 | static_initializer
933 | constructor_declaration
934 | block /* Added, JDK1.1, instance initializer */
936 if (!IS_EMPTY_STMT ($1))
938 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
939 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
944 class_member_declaration:
945 field_declaration
946 | method_declaration
947 | class_declaration /* Added, JDK1.1 inner classes */
948 { end_class_declaration (1); }
949 | interface_declaration /* Added, JDK1.1 inner interfaces */
950 { end_class_declaration (1); }
951 | empty_statement
954 /* 19.8.2 Productions from 8.3: Field Declarations */
955 field_declaration:
956 type variable_declarators SC_TK
957 { register_fields (0, $1, $2); }
958 | modifiers type variable_declarators SC_TK
960 check_modifiers
961 ("Illegal modifier %qs for field declaration",
962 $1, FIELD_MODIFIERS);
963 check_modifiers_consistency ($1);
964 register_fields ($1, $2, $3);
968 variable_declarators:
969 /* Should we use build_decl_list () instead ? FIXME */
970 variable_declarator /* Default rule */
971 | variable_declarators C_TK variable_declarator
972 { $$ = chainon ($1, $3); }
973 | variable_declarators C_TK error
974 {yyerror ("Missing term"); RECOVER;}
977 variable_declarator:
978 variable_declarator_id
979 { $$ = build_tree_list ($1, NULL_TREE); }
980 | variable_declarator_id ASSIGN_TK variable_initializer
982 if (java_error_count)
983 $3 = NULL_TREE;
984 $$ = build_tree_list
985 ($1, build_assignment ($2.token, $2.location, $1, $3));
987 | variable_declarator_id ASSIGN_TK error
989 yyerror ("Missing variable initializer");
990 $$ = build_tree_list ($1, NULL_TREE);
991 RECOVER;
993 | variable_declarator_id ASSIGN_TK variable_initializer error
995 yyerror ("';' expected");
996 $$ = build_tree_list ($1, NULL_TREE);
997 RECOVER;
1001 variable_declarator_id:
1002 identifier
1003 | variable_declarator_id OSB_TK CSB_TK
1004 { $$ = build_unresolved_array_type ($1); }
1005 | identifier error
1006 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
1007 | variable_declarator_id OSB_TK error
1009 yyerror ("']' expected");
1010 DRECOVER(vdi);
1012 | variable_declarator_id CSB_TK error
1013 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1016 variable_initializer:
1017 expression
1018 | array_initializer
1021 /* 19.8.3 Productions from 8.4: Method Declarations */
1022 method_declaration:
1023 method_header
1025 current_function_decl = $1;
1026 if (current_function_decl
1027 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1028 source_start_java_method (current_function_decl);
1029 else
1030 current_function_decl = NULL_TREE;
1032 method_body
1033 { finish_method_declaration ($3); }
1034 | method_header error
1035 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1038 method_header:
1039 type method_declarator throws
1040 { $$ = method_header (0, $1, $2, $3); }
1041 | VOID_TK method_declarator throws
1042 { $$ = method_header (0, void_type_node, $2, $3); }
1043 | modifiers type method_declarator throws
1044 { $$ = method_header ($1, $2, $3, $4); }
1045 | modifiers VOID_TK method_declarator throws
1046 { $$ = method_header ($1, void_type_node, $3, $4); }
1047 | type error
1049 yyerror ("Invalid method declaration, method name required");
1050 RECOVER;
1052 | modifiers type error
1054 yyerror ("Identifier expected");
1055 RECOVER;
1057 | VOID_TK error
1059 yyerror ("Identifier expected");
1060 RECOVER;
1062 | modifiers VOID_TK error
1064 yyerror ("Identifier expected");
1065 RECOVER;
1067 | modifiers error
1069 yyerror ("Invalid method declaration, return type required");
1070 RECOVER;
1074 method_declarator:
1075 identifier OP_TK CP_TK
1077 ctxp->formal_parameter_number = 0;
1078 $$ = method_declarator ($1, NULL_TREE);
1080 | identifier OP_TK formal_parameter_list CP_TK
1081 { $$ = method_declarator ($1, $3); }
1082 | method_declarator OSB_TK CSB_TK
1084 SET_EXPR_LOCATION_FROM_TOKEN (wfl_operator, $2);
1085 TREE_PURPOSE ($1) =
1086 build_unresolved_array_type (TREE_PURPOSE ($1));
1087 parse_warning_context
1088 (wfl_operator,
1089 "Discouraged form of returned type specification");
1091 | identifier OP_TK error
1092 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1093 | method_declarator OSB_TK error
1094 {yyerror ("']' expected"); RECOVER;}
1097 formal_parameter_list:
1098 formal_parameter
1100 ctxp->formal_parameter_number = 1;
1102 | formal_parameter_list C_TK formal_parameter
1104 ctxp->formal_parameter_number += 1;
1105 $$ = chainon ($1, $3);
1107 | formal_parameter_list C_TK error
1108 { yyerror ("Missing formal parameter term"); RECOVER; }
1111 formal_parameter:
1112 type variable_declarator_id
1114 $$ = build_tree_list ($2, $1);
1116 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1118 $$ = build_tree_list ($3, $2);
1119 ARG_FINAL_P ($$) = 1;
1121 | type error
1123 yyerror ("Missing identifier"); RECOVER;
1124 $$ = NULL_TREE;
1126 | final type error
1128 yyerror ("Missing identifier"); RECOVER;
1129 $$ = NULL_TREE;
1133 final:
1134 modifiers
1136 check_modifiers ("Illegal modifier %qs. Only %<final%> was expected here",
1137 $1, ACC_FINAL);
1138 if ($1 != ACC_FINAL)
1139 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1143 throws:
1144 { $$ = NULL_TREE; }
1145 | THROWS_TK class_type_list
1146 { $$ = $2; }
1147 | THROWS_TK error
1148 {yyerror ("Missing class type term"); RECOVER;}
1151 class_type_list:
1152 class_type
1153 { $$ = build_tree_list ($1, $1); }
1154 | class_type_list C_TK class_type
1155 { $$ = tree_cons ($3, $3, $1); }
1156 | class_type_list C_TK error
1157 {yyerror ("Missing class type term"); RECOVER;}
1160 method_body:
1161 block
1162 | SC_TK { $$ = NULL_TREE; }
1165 /* 19.8.4 Productions from 8.5: Static Initializers */
1166 static_initializer:
1167 static block
1169 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1170 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1171 current_static_block = NULL_TREE;
1175 static: /* Test lval.sub_token here */
1176 modifiers
1178 check_modifiers ("Illegal modifier %qs for static initializer", $1, ACC_STATIC);
1179 /* Can't have a static initializer in an innerclass */
1180 if ($1 | ACC_STATIC &&
1181 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1182 parse_error_context
1183 (MODIFIER_WFL (STATIC_TK),
1184 "Can't define static initializer in class %qs. Static initializer can only be defined in top-level classes",
1185 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1186 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1190 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1191 constructor_declaration:
1192 constructor_header
1194 current_function_decl = $1;
1195 source_start_java_method (current_function_decl);
1197 constructor_body
1198 { finish_method_declaration ($3); }
1201 constructor_header:
1202 constructor_declarator throws
1203 { $$ = method_header (0, NULL_TREE, $1, $2); }
1204 | modifiers constructor_declarator throws
1205 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1208 constructor_declarator:
1209 simple_name OP_TK CP_TK
1211 ctxp->formal_parameter_number = 0;
1212 $$ = method_declarator ($1, NULL_TREE);
1214 | simple_name OP_TK formal_parameter_list CP_TK
1215 { $$ = method_declarator ($1, $3); }
1218 constructor_body:
1219 /* Unlike regular method, we always need a complete (empty)
1220 body so we can safely perform all the required code
1221 addition (super invocation and field initialization) */
1222 block_begin constructor_block_end
1224 BLOCK_EXPR_BODY ($2) = build_java_empty_stmt ();
1225 $$ = $2;
1227 | block_begin explicit_constructor_invocation constructor_block_end
1228 { $$ = $3; }
1229 | block_begin block_statements constructor_block_end
1230 { $$ = $3; }
1231 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1232 { $$ = $4; }
1235 constructor_block_end:
1236 block_end
1239 /* Error recovery for that rule moved down expression_statement: rule. */
1240 explicit_constructor_invocation:
1241 this_or_super OP_TK CP_TK SC_TK
1243 $$ = build_method_invocation ($1, NULL_TREE);
1244 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1245 $$ = java_method_add_stmt (current_function_decl, $$);
1247 | this_or_super OP_TK argument_list CP_TK SC_TK
1249 $$ = build_method_invocation ($1, $3);
1250 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1251 $$ = java_method_add_stmt (current_function_decl, $$);
1253 /* Added, JDK1.1 inner classes. Modified because the rule
1254 'primary' couldn't work. */
1255 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1256 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1257 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1258 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1261 this_or_super: /* Added, simplifies error diagnostics */
1262 THIS_TK
1264 tree wfl = build_wfl_node (this_identifier_node);
1265 SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1266 $$ = wfl;
1268 | SUPER_TK
1270 tree wfl = build_wfl_node (super_identifier_node);
1271 SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1272 $$ = wfl;
1276 /* 19.9 Productions from 9: Interfaces */
1277 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1278 interface_declaration:
1279 INTERFACE_TK identifier
1280 { create_interface (0, $2, NULL_TREE); }
1281 interface_body
1282 { ; }
1283 | modifiers INTERFACE_TK identifier
1284 { create_interface ($1, $3, NULL_TREE); }
1285 interface_body
1286 { ; }
1287 | INTERFACE_TK identifier extends_interfaces
1288 { create_interface (0, $2, $3); }
1289 interface_body
1290 { ; }
1291 | modifiers INTERFACE_TK identifier extends_interfaces
1292 { create_interface ($1, $3, $4); }
1293 interface_body
1294 { ; }
1295 | INTERFACE_TK identifier error
1296 { yyerror ("'{' expected"); RECOVER; }
1297 | modifiers INTERFACE_TK identifier error
1298 { yyerror ("'{' expected"); RECOVER; }
1301 extends_interfaces:
1302 EXTENDS_TK interface_type
1304 ctxp->interface_number = 1;
1305 $$ = build_tree_list ($2, NULL_TREE);
1307 | extends_interfaces C_TK interface_type
1309 ctxp->interface_number++;
1310 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1312 | EXTENDS_TK error
1313 {yyerror ("Invalid interface type"); RECOVER;}
1314 | extends_interfaces C_TK error
1315 {yyerror ("Missing term"); RECOVER;}
1318 interface_body:
1319 OCB_TK CCB_TK
1320 { $$ = NULL_TREE; }
1321 | OCB_TK interface_member_declarations CCB_TK
1322 { $$ = NULL_TREE; }
1325 interface_member_declarations:
1326 interface_member_declaration
1327 | interface_member_declarations interface_member_declaration
1330 interface_member_declaration:
1331 constant_declaration
1332 | abstract_method_declaration
1333 | class_declaration /* Added, JDK1.1 inner classes */
1334 { end_class_declaration (1); }
1335 | interface_declaration /* Added, JDK1.1 inner interfaces */
1336 { end_class_declaration (1); }
1339 constant_declaration:
1340 field_declaration
1343 abstract_method_declaration:
1344 method_header SC_TK
1346 check_abstract_method_header ($1);
1347 current_function_decl = NULL_TREE; /* FIXME ? */
1349 | method_header error
1350 {yyerror ("';' expected"); RECOVER;}
1353 /* 19.10 Productions from 10: Arrays */
1354 array_initializer:
1355 OCB_TK CCB_TK
1356 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1357 | OCB_TK C_TK CCB_TK
1358 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1359 | OCB_TK variable_initializers CCB_TK
1360 { $$ = build_new_array_init ($1.location, $2); }
1361 | OCB_TK variable_initializers C_TK CCB_TK
1362 { $$ = build_new_array_init ($1.location, $2); }
1365 variable_initializers:
1366 variable_initializer
1368 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1369 $1, NULL_TREE);
1371 | variable_initializers C_TK variable_initializer
1373 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1375 | variable_initializers C_TK error
1376 {yyerror ("Missing term"); RECOVER;}
1379 /* 19.11 Production from 14: Blocks and Statements */
1380 block:
1381 block_begin block_end
1382 { $$ = $2; }
1383 | block_begin block_statements block_end
1384 { $$ = $3; }
1387 block_begin:
1388 OCB_TK
1389 { enter_block (); }
1392 block_end:
1393 CCB_TK
1395 maybe_absorb_scoping_blocks ();
1396 /* Store the location of the `}' when doing xrefs */
1397 if (current_function_decl && flag_emit_xref)
1398 DECL_END_SOURCE_LINE (current_function_decl) = $1.location;
1399 $$ = exit_block ();
1400 if (!BLOCK_SUBBLOCKS ($$))
1401 BLOCK_SUBBLOCKS ($$) = build_java_empty_stmt ();
1405 block_statements:
1406 block_statement
1407 | block_statements block_statement
1410 block_statement:
1411 local_variable_declaration_statement
1412 | statement
1413 { java_method_add_stmt (current_function_decl, $1); }
1414 | class_declaration /* Added, JDK1.1 local classes */
1416 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1417 end_class_declaration (1);
1421 local_variable_declaration_statement:
1422 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1425 local_variable_declaration:
1426 type variable_declarators
1427 { declare_local_variables (0, $1, $2); }
1428 | final type variable_declarators /* Added, JDK1.1 final locals */
1429 { declare_local_variables ($1, $2, $3); }
1432 statement:
1433 statement_without_trailing_substatement
1434 | labeled_statement
1435 | if_then_statement
1436 | if_then_else_statement
1437 | while_statement
1438 | for_statement
1439 { $$ = exit_block (); }
1442 statement_nsi:
1443 statement_without_trailing_substatement
1444 | labeled_statement_nsi
1445 | if_then_else_statement_nsi
1446 | while_statement_nsi
1447 | for_statement_nsi
1448 { $$ = exit_block (); }
1451 statement_without_trailing_substatement:
1452 block
1453 | empty_statement
1454 | expression_statement
1455 | switch_statement
1456 | do_statement
1457 | break_statement
1458 | continue_statement
1459 | return_statement
1460 | synchronized_statement
1461 | throw_statement
1462 | try_statement
1463 | assert_statement
1466 empty_statement:
1467 SC_TK
1469 if (flag_extraneous_semicolon
1470 && ! current_static_block
1471 && (! current_function_decl ||
1472 /* Verify we're not in a inner class declaration */
1473 (GET_CPC () != TYPE_NAME
1474 (DECL_CONTEXT (current_function_decl)))))
1477 #ifdef USE_MAPPED_LOCATION
1478 SET_EXPR_LOCATION (wfl_operator, input_location);
1479 #else
1480 EXPR_WFL_SET_LINECOL (wfl_operator, input_line, -1);
1481 #endif
1482 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1484 $$ = build_java_empty_stmt ();
1488 label_decl:
1489 identifier REL_CL_TK
1491 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1492 EXPR_WFL_NODE ($1));
1493 pushlevel (2);
1494 push_labeled_block ($$);
1495 PUSH_LABELED_BLOCK ($$);
1499 labeled_statement:
1500 label_decl statement
1501 { $$ = finish_labeled_statement ($1, $2); }
1502 | identifier error
1503 {yyerror ("':' expected"); RECOVER;}
1506 labeled_statement_nsi:
1507 label_decl statement_nsi
1508 { $$ = finish_labeled_statement ($1, $2); }
1511 /* We concentrate here a bunch of error handling rules that we couldn't write
1512 earlier, because expression_statement catches a missing ';'. */
1513 expression_statement:
1514 statement_expression SC_TK
1516 /* We have a statement. Generate a WFL around it so
1517 we can debug it */
1518 #ifdef USE_MAPPED_LOCATION
1519 $$ = expr_add_location ($1, input_location, 1);
1520 #else
1521 $$ = build_expr_wfl ($1, input_filename, input_line, 0);
1522 JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1523 #endif
1524 /* We know we have a statement, so set the debug
1525 info to be eventually generate here. */
1527 | error SC_TK
1529 YYNOT_TWICE yyerror ("Invalid expression statement");
1530 DRECOVER (expr_stmt);
1532 | error OCB_TK
1534 YYNOT_TWICE yyerror ("Invalid expression statement");
1535 DRECOVER (expr_stmt);
1537 | error CCB_TK
1539 YYNOT_TWICE yyerror ("Invalid expression statement");
1540 DRECOVER (expr_stmt);
1542 | this_or_super OP_TK error
1543 {yyerror ("')' expected"); RECOVER;}
1544 | this_or_super OP_TK CP_TK error
1546 parse_ctor_invocation_error ();
1547 RECOVER;
1549 | this_or_super OP_TK argument_list error
1550 {yyerror ("')' expected"); RECOVER;}
1551 | this_or_super OP_TK argument_list CP_TK error
1553 parse_ctor_invocation_error ();
1554 RECOVER;
1556 | name DOT_TK SUPER_TK error
1557 {yyerror ("'(' expected"); RECOVER;}
1558 | name DOT_TK SUPER_TK OP_TK error
1559 {yyerror ("')' expected"); RECOVER;}
1560 | name DOT_TK SUPER_TK OP_TK argument_list error
1561 {yyerror ("')' expected"); RECOVER;}
1562 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1563 {yyerror ("';' expected"); RECOVER;}
1564 | name DOT_TK SUPER_TK OP_TK CP_TK error
1565 {yyerror ("';' expected"); RECOVER;}
1568 statement_expression:
1569 assignment
1570 | pre_increment_expression
1571 | pre_decrement_expression
1572 | post_increment_expression
1573 | post_decrement_expression
1574 | method_invocation
1575 | class_instance_creation_expression
1578 if_then_statement:
1579 IF_TK OP_TK expression CP_TK statement
1581 $$ = build_if_else_statement ($2.location, $3,
1582 $5, NULL_TREE);
1584 | IF_TK error
1585 {yyerror ("'(' expected"); RECOVER;}
1586 | IF_TK OP_TK error
1587 {yyerror ("Missing term"); RECOVER;}
1588 | IF_TK OP_TK expression error
1589 {yyerror ("')' expected"); RECOVER;}
1592 if_then_else_statement:
1593 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1594 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1597 if_then_else_statement_nsi:
1598 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1599 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1602 switch_statement:
1603 switch_expression
1605 enter_block ();
1607 switch_block
1609 /* Make into "proper list" of COMPOUND_EXPRs.
1610 I.e. make the last statement also have its own
1611 COMPOUND_EXPR. */
1612 maybe_absorb_scoping_blocks ();
1613 TREE_OPERAND ($1, 1) = exit_block ();
1614 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1618 switch_expression:
1619 SWITCH_TK OP_TK expression CP_TK
1621 $$ = build3 (SWITCH_EXPR, NULL_TREE, $3,
1622 NULL_TREE, NULL_TREE);
1623 SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
1625 | SWITCH_TK error
1626 {yyerror ("'(' expected"); RECOVER;}
1627 | SWITCH_TK OP_TK error
1628 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1629 | SWITCH_TK OP_TK expression CP_TK error
1630 {yyerror ("'{' expected"); RECOVER;}
1633 /* Default assignment is there to avoid type node on switch_block
1634 node. */
1636 switch_block:
1637 OCB_TK CCB_TK
1638 { $$ = NULL_TREE; }
1639 | OCB_TK switch_labels CCB_TK
1640 { $$ = NULL_TREE; }
1641 | OCB_TK switch_block_statement_groups CCB_TK
1642 { $$ = NULL_TREE; }
1643 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1644 { $$ = NULL_TREE; }
1647 switch_block_statement_groups:
1648 switch_block_statement_group
1649 | switch_block_statement_groups switch_block_statement_group
1652 switch_block_statement_group:
1653 switch_labels block_statements
1656 switch_labels:
1657 switch_label
1658 | switch_labels switch_label
1661 switch_label:
1662 CASE_TK constant_expression REL_CL_TK
1664 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1665 SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1666 java_method_add_stmt (current_function_decl, lab);
1668 | DEFAULT_TK REL_CL_TK
1670 tree lab = make_node (DEFAULT_EXPR);
1671 SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1672 java_method_add_stmt (current_function_decl, lab);
1674 | CASE_TK error
1675 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1676 | CASE_TK constant_expression error
1677 {yyerror ("':' expected"); RECOVER;}
1678 | DEFAULT_TK error
1679 {yyerror ("':' expected"); RECOVER;}
1682 while_expression:
1683 WHILE_TK OP_TK expression CP_TK
1685 tree body = build_loop_body ($2.location, $3, 0);
1686 $$ = build_new_loop (body);
1690 while_statement:
1691 while_expression statement
1692 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1693 | WHILE_TK error
1694 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1695 | WHILE_TK OP_TK error
1696 {yyerror ("Missing term and ')' expected"); RECOVER;}
1697 | WHILE_TK OP_TK expression error
1698 {yyerror ("')' expected"); RECOVER;}
1701 while_statement_nsi:
1702 while_expression statement_nsi
1703 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1706 do_statement_begin:
1707 DO_TK
1709 tree body = build_loop_body (0, NULL_TREE, 1);
1710 $$ = build_new_loop (body);
1712 /* Need error handing here. FIXME */
1715 do_statement:
1716 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1717 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1720 for_statement:
1721 for_begin SC_TK expression SC_TK for_update CP_TK statement
1723 if (CONSTANT_CLASS_P ($3))
1724 $3 = build_wfl_node ($3);
1725 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1727 | for_begin SC_TK SC_TK for_update CP_TK statement
1729 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1730 /* We have not condition, so we get rid of the EXIT_EXPR */
1731 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1732 build_java_empty_stmt ();
1734 | for_begin SC_TK error
1735 {yyerror ("Invalid control expression"); RECOVER;}
1736 | for_begin SC_TK expression SC_TK error
1737 {yyerror ("Invalid update expression"); RECOVER;}
1738 | for_begin SC_TK SC_TK error
1739 {yyerror ("Invalid update expression"); RECOVER;}
1742 for_statement_nsi:
1743 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1744 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1745 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1747 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1748 /* We have not condition, so we get rid of the EXIT_EXPR */
1749 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1750 build_java_empty_stmt ();
1754 for_header:
1755 FOR_TK OP_TK
1757 /* This scope defined for local variable that may be
1758 defined within the scope of the for loop */
1759 enter_block ();
1761 | FOR_TK error
1762 {yyerror ("'(' expected"); DRECOVER(for_1);}
1763 | FOR_TK OP_TK error
1764 {yyerror ("Invalid init statement"); RECOVER;}
1767 for_begin:
1768 for_header for_init
1770 /* We now declare the loop body. The loop is
1771 declared as a for loop. */
1772 tree body = build_loop_body (0, NULL_TREE, 0);
1773 $$ = build_new_loop (body);
1774 FOR_LOOP_P ($$) = 1;
1775 /* The loop is added to the current block the for
1776 statement is defined within */
1777 java_method_add_stmt (current_function_decl, $$);
1780 for_init: /* Can be empty */
1781 { $$ = build_java_empty_stmt (); }
1782 | statement_expression_list
1784 /* Init statement recorded within the previously
1785 defined block scope */
1786 $$ = java_method_add_stmt (current_function_decl, $1);
1788 | local_variable_declaration
1790 /* Local variable are recorded within the previously
1791 defined block scope */
1792 $$ = NULL_TREE;
1794 | statement_expression_list error
1795 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1798 for_update: /* Can be empty */
1799 {$$ = build_java_empty_stmt ();}
1800 | statement_expression_list
1801 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1804 statement_expression_list:
1805 statement_expression
1806 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1807 | statement_expression_list C_TK statement_expression
1808 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1809 | statement_expression_list C_TK error
1810 {yyerror ("Missing term"); RECOVER;}
1813 break_statement:
1814 BREAK_TK SC_TK
1815 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1816 | BREAK_TK identifier SC_TK
1817 { $$ = build_bc_statement ($1.location, 1, $2); }
1818 | BREAK_TK error
1819 {yyerror ("Missing term"); RECOVER;}
1820 | BREAK_TK identifier error
1821 {yyerror ("';' expected"); RECOVER;}
1824 continue_statement:
1825 CONTINUE_TK SC_TK
1826 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1827 | CONTINUE_TK identifier SC_TK
1828 { $$ = build_bc_statement ($1.location, 0, $2); }
1829 | CONTINUE_TK error
1830 {yyerror ("Missing term"); RECOVER;}
1831 | CONTINUE_TK identifier error
1832 {yyerror ("';' expected"); RECOVER;}
1835 return_statement:
1836 RETURN_TK SC_TK
1837 { $$ = build_return ($1.location, NULL_TREE); }
1838 | RETURN_TK expression SC_TK
1839 { $$ = build_return ($1.location, $2); }
1840 | RETURN_TK error
1841 {yyerror ("Missing term"); RECOVER;}
1842 | RETURN_TK expression error
1843 {yyerror ("';' expected"); RECOVER;}
1846 throw_statement:
1847 THROW_TK expression SC_TK
1849 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1850 SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1852 | THROW_TK error
1853 {yyerror ("Missing term"); RECOVER;}
1854 | THROW_TK expression error
1855 {yyerror ("';' expected"); RECOVER;}
1858 assert_statement:
1859 ASSERT_TK expression REL_CL_TK expression SC_TK
1861 $$ = build_assertion ($1.location, $2, $4);
1863 | ASSERT_TK expression SC_TK
1865 $$ = build_assertion ($1.location, $2, NULL_TREE);
1867 | ASSERT_TK error
1868 {yyerror ("Missing term"); RECOVER;}
1869 | ASSERT_TK expression error
1870 {yyerror ("';' expected"); RECOVER;}
1873 synchronized_statement:
1874 synchronized OP_TK expression CP_TK block
1876 $$ = build2 (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1877 EXPR_WFL_LINECOL ($$) =
1878 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1880 | synchronized OP_TK expression CP_TK error
1881 {yyerror ("'{' expected"); RECOVER;}
1882 | synchronized error
1883 {yyerror ("'(' expected"); RECOVER;}
1884 | synchronized OP_TK error CP_TK
1885 {yyerror ("Missing term"); RECOVER;}
1886 | synchronized OP_TK error
1887 {yyerror ("Missing term"); RECOVER;}
1890 synchronized:
1891 modifiers
1893 check_modifiers (
1894 "Illegal modifier %qs. Only %<synchronized%> was expected here",
1895 $1, ACC_SYNCHRONIZED);
1896 if ($1 != ACC_SYNCHRONIZED)
1897 MODIFIER_WFL (SYNCHRONIZED_TK) =
1898 build_wfl_node (NULL_TREE);
1902 try_statement:
1903 TRY_TK block catches
1904 { $$ = build_try_statement ($1.location, $2, $3); }
1905 | TRY_TK block finally
1906 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1907 | TRY_TK block catches finally
1908 { $$ = build_try_finally_statement
1909 ($1.location, build_try_statement ($1.location,
1910 $2, $3), $4);
1912 | TRY_TK error
1913 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1916 catches:
1917 catch_clause
1918 | catches catch_clause
1920 TREE_CHAIN ($2) = $1;
1921 $$ = $2;
1925 catch_clause:
1926 catch_clause_parameter block
1928 java_method_add_stmt (current_function_decl, $2);
1929 exit_block ();
1930 $$ = $1;
1934 catch_clause_parameter:
1935 CATCH_TK OP_TK formal_parameter CP_TK
1937 /* We add a block to define a scope for
1938 formal_parameter (CCBP). The formal parameter is
1939 declared initialized by the appropriate function
1940 call */
1941 tree ccpb;
1942 tree init;
1943 if ($3)
1945 ccpb = enter_block ();
1946 init = build_assignment
1947 (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1948 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1949 declare_local_variables (0, TREE_VALUE ($3),
1950 build_tree_list
1951 (TREE_PURPOSE ($3), init));
1952 $$ = build1 (JAVA_CATCH_EXPR, NULL_TREE, ccpb);
1953 SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1955 else
1957 $$ = error_mark_node;
1960 | CATCH_TK error
1961 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1962 | CATCH_TK OP_TK error
1964 yyerror ("Missing term or ')' expected");
1965 RECOVER; $$ = NULL_TREE;
1967 | CATCH_TK OP_TK error CP_TK /* That's for () */
1968 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1971 finally:
1972 FINALLY_TK block
1973 { $$ = $2; }
1974 | FINALLY_TK error
1975 {yyerror ("'{' expected"); RECOVER; }
1978 /* 19.12 Production from 15: Expressions */
1979 primary:
1980 primary_no_new_array
1981 | array_creation_uninitialized
1982 | array_creation_initialized
1985 primary_no_new_array:
1986 literal
1987 | THIS_TK
1988 { $$ = build_this ($1.location); }
1989 | OP_TK expression CP_TK
1990 {$$ = $2;}
1991 | class_instance_creation_expression
1992 | field_access
1993 | method_invocation
1994 | array_access
1995 | type_literals
1996 /* Added, JDK1.1 inner classes. Documentation is wrong
1997 referring to a 'ClassName' (class_name) rule that doesn't
1998 exist. Used name: instead. */
1999 | name DOT_TK THIS_TK
2001 tree wfl = build_wfl_node (this_identifier_node);
2002 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
2004 | OP_TK expression error
2005 {yyerror ("')' expected"); RECOVER;}
2006 | name DOT_TK error
2007 {yyerror ("'class' or 'this' expected" ); RECOVER;}
2008 | primitive_type DOT_TK error
2009 {yyerror ("'class' expected" ); RECOVER;}
2010 | VOID_TK DOT_TK error
2011 {yyerror ("'class' expected" ); RECOVER;}
2014 type_literals:
2015 name DOT_TK CLASS_TK
2016 { $$ = build_incomplete_class_ref ($2.location, $1); }
2017 | array_type DOT_TK CLASS_TK
2018 { $$ = build_incomplete_class_ref ($2.location, $1); }
2019 | primitive_type DOT_TK CLASS_TK
2020 { $$ = build_incomplete_class_ref ($2.location, $1); }
2021 | VOID_TK DOT_TK CLASS_TK
2023 $$ = build_incomplete_class_ref ($2.location,
2024 void_type_node);
2028 class_instance_creation_expression:
2029 NEW_TK class_type OP_TK argument_list CP_TK
2030 { $$ = build_new_invocation ($2, $4); }
2031 | NEW_TK class_type OP_TK CP_TK
2032 { $$ = build_new_invocation ($2, NULL_TREE); }
2033 | anonymous_class_creation
2034 /* Added, JDK1.1 inner classes, modified to use name or
2035 primary instead of primary solely which couldn't work in
2036 all situations. */
2037 | something_dot_new identifier OP_TK CP_TK
2039 tree ctor = build_new_invocation ($2, NULL_TREE);
2040 $$ = make_qualified_primary ($1, ctor,
2041 EXPR_WFL_LINECOL ($1));
2043 | something_dot_new identifier OP_TK CP_TK class_body
2044 | something_dot_new identifier OP_TK argument_list CP_TK
2046 tree ctor = build_new_invocation ($2, $4);
2047 $$ = make_qualified_primary ($1, ctor,
2048 EXPR_WFL_LINECOL ($1));
2050 | something_dot_new identifier OP_TK argument_list CP_TK class_body
2051 | NEW_TK error SC_TK
2052 {$$ = NULL_TREE; yyerror ("'(' expected"); DRECOVER(new_1);}
2053 | NEW_TK class_type error
2054 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2055 | NEW_TK class_type OP_TK error
2056 {$$ = NULL_TREE; yyerror ("')' or term expected"); RECOVER;}
2057 | NEW_TK class_type OP_TK argument_list error
2058 {$$ = NULL_TREE; yyerror ("')' expected"); RECOVER;}
2059 | something_dot_new error
2061 $$ = NULL_TREE;
2062 YYERROR_NOW;
2063 yyerror ("Identifier expected");
2064 RECOVER;
2066 | something_dot_new identifier error
2067 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2070 /* Created after JDK1.1 rules originally added to
2071 class_instance_creation_expression, but modified to use
2072 'class_type' instead of 'TypeName' (type_name) which is mentioned
2073 in the documentation but doesn't exist. */
2075 anonymous_class_creation:
2076 NEW_TK class_type OP_TK argument_list CP_TK
2077 { create_anonymous_class ($2); }
2078 class_body
2080 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2081 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2083 end_class_declaration (1);
2085 /* Now we can craft the new expression */
2086 $$ = build_new_invocation (id, $4);
2088 /* Note that we can't possibly be here if
2089 `class_type' is an interface (in which case the
2090 anonymous class extends Object and implements
2091 `class_type', hence its constructor can't have
2092 arguments.) */
2094 /* Otherwise, the innerclass must feature a
2095 constructor matching `argument_list'. Anonymous
2096 classes are a bit special: it's impossible to
2097 define constructor for them, hence constructors
2098 must be generated following the hints provided by
2099 the `new' expression. Whether a super constructor
2100 of that nature exists or not is to be verified
2101 later on in verify_constructor_super.
2103 It's during the expansion of a `new' statement
2104 referring to an anonymous class that a ctor will
2105 be generated for the anonymous class, with the
2106 right arguments. */
2109 | NEW_TK class_type OP_TK CP_TK
2110 { create_anonymous_class ($2); }
2111 class_body
2113 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2114 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2116 end_class_declaration (1);
2118 /* Now we can craft the new expression. The
2119 statement doesn't need to be remember so that a
2120 constructor can be generated, since its signature
2121 is already known. */
2122 $$ = build_new_invocation (id, NULL_TREE);
2126 something_dot_new: /* Added, not part of the specs. */
2127 name DOT_TK NEW_TK
2128 { $$ = $1; }
2129 | primary DOT_TK NEW_TK
2130 { $$ = $1; }
2133 argument_list:
2134 expression
2136 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2137 ctxp->formal_parameter_number = 1;
2139 | argument_list C_TK expression
2141 ctxp->formal_parameter_number += 1;
2142 $$ = tree_cons (NULL_TREE, $3, $1);
2144 | argument_list C_TK error
2145 {yyerror ("Missing term"); RECOVER;}
2148 array_creation_uninitialized:
2149 NEW_TK primitive_type dim_exprs
2150 { $$ = build_newarray_node ($2, $3, 0); }
2151 | NEW_TK class_or_interface_type dim_exprs
2152 { $$ = build_newarray_node ($2, $3, 0); }
2153 | NEW_TK primitive_type dim_exprs dims
2154 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2155 | NEW_TK class_or_interface_type dim_exprs dims
2156 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2157 | NEW_TK error CSB_TK
2158 {yyerror ("'[' expected"); DRECOVER ("]");}
2159 | NEW_TK error OSB_TK
2160 {yyerror ("']' expected"); RECOVER;}
2163 array_creation_initialized:
2164 /* Added, JDK1.1 anonymous array. Initial documentation rule
2165 modified */
2166 NEW_TK class_or_interface_type dims array_initializer
2168 char *sig;
2169 int osb = pop_current_osb (ctxp);
2170 while (osb--)
2171 obstack_grow (&temporary_obstack, "[]", 2);
2172 obstack_1grow (&temporary_obstack, '\0');
2173 sig = obstack_finish (&temporary_obstack);
2174 $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2175 $2, get_identifier (sig), $4);
2177 | NEW_TK primitive_type dims array_initializer
2179 int osb = pop_current_osb (ctxp);
2180 tree type = $2;
2181 while (osb--)
2182 type = build_java_array_type (type, -1);
2183 $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2184 build_pointer_type (type), NULL_TREE, $4);
2186 | NEW_TK error CSB_TK
2187 {yyerror ("'[' expected"); DRECOVER ("]");}
2188 | NEW_TK error OSB_TK
2189 {yyerror ("']' expected"); RECOVER;}
2192 dim_exprs:
2193 dim_expr
2194 { $$ = build_tree_list (NULL_TREE, $1); }
2195 | dim_exprs dim_expr
2196 { $$ = tree_cons (NULL_TREE, $2, $$); }
2199 dim_expr:
2200 OSB_TK expression CSB_TK
2202 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2204 $2 = build_wfl_node ($2);
2205 TREE_TYPE ($2) = NULL_TREE;
2207 EXPR_WFL_LINECOL ($2) = $1.location;
2208 $$ = $2;
2210 | OSB_TK expression error
2211 {yyerror ("']' expected"); RECOVER;}
2212 | OSB_TK error
2214 yyerror ("Missing term");
2215 yyerror ("']' expected");
2216 RECOVER;
2220 dims:
2221 OSB_TK CSB_TK
2223 int allocate = 0;
2224 /* If not initialized, allocate memory for the osb
2225 numbers stack */
2226 if (!ctxp->osb_limit)
2228 allocate = ctxp->osb_limit = 32;
2229 ctxp->osb_depth = -1;
2231 /* If capacity overflown, reallocate a bigger chunk */
2232 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2233 allocate = ctxp->osb_limit << 1;
2235 if (allocate)
2237 allocate *= sizeof (int);
2238 if (ctxp->osb_number)
2239 ctxp->osb_number = xrealloc (ctxp->osb_number,
2240 allocate);
2241 else
2242 ctxp->osb_number = xmalloc (allocate);
2244 ctxp->osb_depth++;
2245 CURRENT_OSB (ctxp) = 1;
2247 | dims OSB_TK CSB_TK
2248 { CURRENT_OSB (ctxp)++; }
2249 | dims OSB_TK error
2250 { yyerror ("']' expected"); RECOVER;}
2253 field_access:
2254 primary DOT_TK identifier
2255 { $$ = make_qualified_primary ($1, $3, $2.location); }
2256 /* FIXME - REWRITE TO:
2257 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2258 | SUPER_TK DOT_TK identifier
2260 tree super_wfl = build_wfl_node (super_identifier_node);
2261 SET_EXPR_LOCATION_FROM_TOKEN (super_wfl, $1);
2262 $$ = make_qualified_name (super_wfl, $3, $2.location);
2264 | SUPER_TK error
2265 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2268 method_invocation:
2269 name OP_TK CP_TK
2270 { $$ = build_method_invocation ($1, NULL_TREE); }
2271 | name OP_TK argument_list CP_TK
2272 { $$ = build_method_invocation ($1, $3); }
2273 | primary DOT_TK identifier OP_TK CP_TK
2275 if (TREE_CODE ($1) == THIS_EXPR)
2276 $$ = build_this_super_qualified_invocation
2277 (1, $3, NULL_TREE, 0, $2.location);
2278 else
2280 tree invok = build_method_invocation ($3, NULL_TREE);
2281 $$ = make_qualified_primary ($1, invok, $2.location);
2284 | primary DOT_TK identifier OP_TK argument_list CP_TK
2286 if (TREE_CODE ($1) == THIS_EXPR)
2287 $$ = build_this_super_qualified_invocation
2288 (1, $3, $5, 0, $2.location);
2289 else
2291 tree invok = build_method_invocation ($3, $5);
2292 $$ = make_qualified_primary ($1, invok, $2.location);
2295 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2297 $$ = build_this_super_qualified_invocation
2298 (0, $3, NULL_TREE, $1.location, $2.location);
2300 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2302 $$ = build_this_super_qualified_invocation
2303 (0, $3, $5, $1.location, $2.location);
2305 /* Screws up thing. I let it here until I'm convinced it can
2306 be removed. FIXME
2307 | primary DOT_TK error
2308 {yyerror ("'(' expected"); DRECOVER(bad);} */
2309 | SUPER_TK DOT_TK error CP_TK
2310 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2311 | SUPER_TK DOT_TK error DOT_TK
2312 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2315 array_access:
2316 name OSB_TK expression CSB_TK
2317 { $$ = build_array_ref ($2.location, $1, $3); }
2318 | primary_no_new_array OSB_TK expression CSB_TK
2319 { $$ = build_array_ref ($2.location, $1, $3); }
2320 | array_creation_initialized OSB_TK expression CSB_TK
2321 { $$ = build_array_ref ($2.location, $1, $3); }
2322 | name OSB_TK error
2324 yyerror ("Missing term and ']' expected");
2325 DRECOVER(array_access);
2327 | name OSB_TK expression error
2329 yyerror ("']' expected");
2330 DRECOVER(array_access);
2332 | primary_no_new_array OSB_TK error
2334 yyerror ("Missing term and ']' expected");
2335 DRECOVER(array_access);
2337 | primary_no_new_array OSB_TK expression error
2339 yyerror ("']' expected");
2340 DRECOVER(array_access);
2342 | array_creation_initialized OSB_TK error
2344 yyerror ("Missing term and ']' expected");
2345 DRECOVER(array_access);
2347 | array_creation_initialized OSB_TK expression error
2349 yyerror ("']' expected");
2350 DRECOVER(array_access);
2354 postfix_expression:
2355 primary
2356 | name
2357 | post_increment_expression
2358 | post_decrement_expression
2361 post_increment_expression:
2362 postfix_expression INCR_TK
2363 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2366 post_decrement_expression:
2367 postfix_expression DECR_TK
2368 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2371 trap_overflow_corner_case:
2372 pre_increment_expression
2373 | pre_decrement_expression
2374 | PLUS_TK unary_expression
2375 {$$ = build_unaryop ($1.token, $1.location, $2); }
2376 | unary_expression_not_plus_minus
2377 | PLUS_TK error
2378 {yyerror ("Missing term"); RECOVER}
2381 unary_expression:
2382 trap_overflow_corner_case
2384 if ($1)
2385 error_if_numeric_overflow ($1);
2386 $$ = $1;
2388 | MINUS_TK trap_overflow_corner_case
2389 {$$ = build_unaryop ($1.token, $1.location, $2); }
2390 | MINUS_TK error
2391 {yyerror ("Missing term"); RECOVER}
2394 pre_increment_expression:
2395 INCR_TK unary_expression
2396 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2397 | INCR_TK error
2398 {yyerror ("Missing term"); RECOVER}
2401 pre_decrement_expression:
2402 DECR_TK unary_expression
2403 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2404 | DECR_TK error
2405 {yyerror ("Missing term"); RECOVER}
2408 unary_expression_not_plus_minus:
2409 postfix_expression
2410 | NOT_TK unary_expression
2411 {$$ = build_unaryop ($1.token, $1.location, $2); }
2412 | NEG_TK unary_expression
2413 {$$ = build_unaryop ($1.token, $1.location, $2); }
2414 | cast_expression
2415 | NOT_TK error
2416 {yyerror ("Missing term"); RECOVER}
2417 | NEG_TK error
2418 {yyerror ("Missing term"); RECOVER}
2421 cast_expression: /* Error handling here is potentially weak */
2422 OP_TK primitive_type dims CP_TK unary_expression
2424 tree type = $2;
2425 int osb = pop_current_osb (ctxp);
2426 while (osb--)
2427 type = build_java_array_type (type, -1);
2428 $$ = build_cast ($1.location, type, $5);
2430 | OP_TK primitive_type CP_TK unary_expression
2431 { $$ = build_cast ($1.location, $2, $4); }
2432 | OP_TK expression CP_TK unary_expression_not_plus_minus
2433 { $$ = build_cast ($1.location, $2, $4); }
2434 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2436 const char *ptr;
2437 int osb = pop_current_osb (ctxp);
2438 obstack_grow (&temporary_obstack,
2439 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2440 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2441 while (osb--)
2442 obstack_grow (&temporary_obstack, "[]", 2);
2443 obstack_1grow (&temporary_obstack, '\0');
2444 ptr = obstack_finish (&temporary_obstack);
2445 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2446 $$ = build_cast ($1.location, $2, $5);
2448 | OP_TK primitive_type OSB_TK error
2449 {yyerror ("']' expected, invalid type expression");}
2450 | OP_TK error
2452 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2453 RECOVER;
2455 | OP_TK primitive_type dims CP_TK error
2456 {yyerror ("Missing term"); RECOVER;}
2457 | OP_TK primitive_type CP_TK error
2458 {yyerror ("Missing term"); RECOVER;}
2459 | OP_TK name dims CP_TK error
2460 {yyerror ("Missing term"); RECOVER;}
2463 multiplicative_expression:
2464 unary_expression
2465 | multiplicative_expression MULT_TK unary_expression
2467 $$ = build_binop (BINOP_LOOKUP ($2.token),
2468 $2.location, $1, $3);
2470 | multiplicative_expression DIV_TK unary_expression
2472 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2473 $1, $3);
2475 | multiplicative_expression REM_TK unary_expression
2477 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2478 $1, $3);
2480 | multiplicative_expression MULT_TK error
2481 {yyerror ("Missing term"); RECOVER;}
2482 | multiplicative_expression DIV_TK error
2483 {yyerror ("Missing term"); RECOVER;}
2484 | multiplicative_expression REM_TK error
2485 {yyerror ("Missing term"); RECOVER;}
2488 additive_expression:
2489 multiplicative_expression
2490 | additive_expression PLUS_TK multiplicative_expression
2492 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2493 $1, $3);
2495 | additive_expression MINUS_TK multiplicative_expression
2497 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2498 $1, $3);
2500 | additive_expression PLUS_TK error
2501 {yyerror ("Missing term"); RECOVER;}
2502 | additive_expression MINUS_TK error
2503 {yyerror ("Missing term"); RECOVER;}
2506 shift_expression:
2507 additive_expression
2508 | shift_expression LS_TK additive_expression
2510 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2511 $1, $3);
2513 | shift_expression SRS_TK additive_expression
2515 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2516 $1, $3);
2518 | shift_expression ZRS_TK additive_expression
2520 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2521 $1, $3);
2523 | shift_expression LS_TK error
2524 {yyerror ("Missing term"); RECOVER;}
2525 | shift_expression SRS_TK error
2526 {yyerror ("Missing term"); RECOVER;}
2527 | shift_expression ZRS_TK error
2528 {yyerror ("Missing term"); RECOVER;}
2531 relational_expression:
2532 shift_expression
2533 | relational_expression LT_TK shift_expression
2535 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2536 $1, $3);
2538 | relational_expression GT_TK shift_expression
2540 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2541 $1, $3);
2543 | relational_expression LTE_TK shift_expression
2545 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2546 $1, $3);
2548 | relational_expression GTE_TK shift_expression
2550 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2551 $1, $3);
2553 | relational_expression INSTANCEOF_TK reference_type
2554 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2555 | relational_expression LT_TK error
2556 {yyerror ("Missing term"); RECOVER;}
2557 | relational_expression GT_TK error
2558 {yyerror ("Missing term"); RECOVER;}
2559 | relational_expression LTE_TK error
2560 {yyerror ("Missing term"); RECOVER;}
2561 | relational_expression GTE_TK error
2562 {yyerror ("Missing term"); RECOVER;}
2563 | relational_expression INSTANCEOF_TK error
2564 {yyerror ("Invalid reference type"); RECOVER;}
2567 equality_expression:
2568 relational_expression
2569 | equality_expression EQ_TK relational_expression
2571 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2572 $1, $3);
2574 | equality_expression NEQ_TK relational_expression
2576 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2577 $1, $3);
2579 | equality_expression EQ_TK error
2580 {yyerror ("Missing term"); RECOVER;}
2581 | equality_expression NEQ_TK error
2582 {yyerror ("Missing term"); RECOVER;}
2585 and_expression:
2586 equality_expression
2587 | and_expression AND_TK equality_expression
2589 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2590 $1, $3);
2592 | and_expression AND_TK error
2593 {yyerror ("Missing term"); RECOVER;}
2596 exclusive_or_expression:
2597 and_expression
2598 | exclusive_or_expression XOR_TK and_expression
2600 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2601 $1, $3);
2603 | exclusive_or_expression XOR_TK error
2604 {yyerror ("Missing term"); RECOVER;}
2607 inclusive_or_expression:
2608 exclusive_or_expression
2609 | inclusive_or_expression OR_TK exclusive_or_expression
2611 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2612 $1, $3);
2614 | inclusive_or_expression OR_TK error
2615 {yyerror ("Missing term"); RECOVER;}
2618 conditional_and_expression:
2619 inclusive_or_expression
2620 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2622 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2623 $1, $3);
2625 | conditional_and_expression BOOL_AND_TK error
2626 {yyerror ("Missing term"); RECOVER;}
2629 conditional_or_expression:
2630 conditional_and_expression
2631 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2633 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2634 $1, $3);
2636 | conditional_or_expression BOOL_OR_TK error
2637 {yyerror ("Missing term"); RECOVER;}
2640 conditional_expression: /* Error handling here is weak */
2641 conditional_or_expression
2642 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2644 $$ = build3 (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2645 SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
2647 | conditional_or_expression REL_QM_TK REL_CL_TK error
2649 YYERROR_NOW;
2650 yyerror ("Missing term");
2651 DRECOVER (1);
2653 | conditional_or_expression REL_QM_TK error
2654 {yyerror ("Missing term"); DRECOVER (2);}
2655 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2656 {yyerror ("Missing term"); DRECOVER (3);}
2659 assignment_expression:
2660 conditional_expression
2661 | assignment
2664 assignment:
2665 left_hand_side assignment_operator assignment_expression
2666 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2667 | left_hand_side assignment_operator error
2669 YYNOT_TWICE yyerror ("Missing term");
2670 DRECOVER (assign);
2674 left_hand_side:
2675 name
2676 | field_access
2677 | array_access
2680 assignment_operator:
2681 ASSIGN_ANY_TK
2682 | ASSIGN_TK
2685 expression:
2686 assignment_expression
2689 constant_expression:
2690 expression
2695 /* Helper function to retrieve an OSB count. Should be used when the
2696 `dims:' rule is being used. */
2698 static int
2699 pop_current_osb (struct parser_ctxt *ctxp)
2701 int to_return;
2703 if (ctxp->osb_depth < 0)
2704 abort ();
2706 to_return = CURRENT_OSB (ctxp);
2707 ctxp->osb_depth--;
2709 return to_return;
2714 /* This section of the code deal with save/restoring parser contexts.
2715 Add mode documentation here. FIXME */
2717 /* Helper function. Create a new parser context. With
2718 COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2719 context is copied, otherwise, the new context is zeroed. The newly
2720 created context becomes the current one. */
2722 static void
2723 create_new_parser_context (int copy_from_previous)
2725 struct parser_ctxt *new;
2727 new = ggc_alloc (sizeof (struct parser_ctxt));
2728 if (copy_from_previous)
2730 memcpy (new, ctxp, sizeof (struct parser_ctxt));
2731 /* This flag, indicating the context saves global values,
2732 should only be set by java_parser_context_save_global. */
2733 new->saved_data_ctx = 0;
2735 else
2736 memset (new, 0, sizeof (struct parser_ctxt));
2738 new->next = ctxp;
2739 ctxp = new;
2742 /* Create a new parser context and make it the current one. */
2744 void
2745 java_push_parser_context (void)
2747 create_new_parser_context (0);
2750 void
2751 java_pop_parser_context (int generate)
2753 tree current;
2754 struct parser_ctxt *next;
2756 if (!ctxp)
2757 return;
2759 next = ctxp->next;
2760 if (next)
2762 input_location = ctxp->save_location;
2763 current_class = ctxp->class_type;
2766 /* If the old and new lexers differ, then free the old one. */
2767 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2768 java_destroy_lexer (ctxp->lexer);
2770 /* Set the single import class file flag to 0 for the current list
2771 of imported things */
2772 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2773 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2775 /* If we pushed a context to parse a class intended to be generated,
2776 we keep it so we can remember the class. What we could actually
2777 do is to just update a list of class names. */
2778 if (generate)
2780 ctxp->next = ctxp_for_generation;
2781 ctxp_for_generation = ctxp;
2784 /* And restore those of the previous context */
2785 if ((ctxp = next)) /* Assignment is really meant here */
2786 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2787 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2790 /* Create a parser context for the use of saving some global
2791 variables. */
2793 void
2794 java_parser_context_save_global (void)
2796 if (!ctxp)
2798 java_push_parser_context ();
2799 ctxp->saved_data_ctx = 1;
2802 /* If this context already stores data, create a new one suitable
2803 for data storage. */
2804 else if (ctxp->saved_data)
2806 create_new_parser_context (1);
2807 ctxp->saved_data_ctx = 1;
2810 ctxp->save_location = input_location;
2811 ctxp->class_type = current_class;
2812 ctxp->function_decl = current_function_decl;
2813 ctxp->saved_data = 1;
2816 /* Restore some global variables from the previous context. Make the
2817 previous context the current one. */
2819 void
2820 java_parser_context_restore_global (void)
2822 input_location = ctxp->save_location;
2823 current_class = ctxp->class_type;
2824 if (wfl_operator)
2825 #ifdef USE_MAPPED_LOCATION
2826 SET_EXPR_LOCATION (wfl_operator, ctxp->save_location);
2827 #else
2828 EXPR_WFL_FILENAME_NODE (wfl_operator) = get_identifier (input_filename);
2829 #endif
2830 current_function_decl = ctxp->function_decl;
2831 ctxp->saved_data = 0;
2832 if (ctxp->saved_data_ctx)
2833 java_pop_parser_context (0);
2836 /* Suspend vital data for the current class/function being parsed so
2837 that an other class can be parsed. Used to let local/anonymous
2838 classes be parsed. */
2840 static void
2841 java_parser_context_suspend (void)
2843 /* This makes debugging through java_debug_context easier */
2844 static const char *const name = "<inner buffer context>";
2846 /* Duplicate the previous context, use it to save the globals we're
2847 interested in */
2848 create_new_parser_context (1);
2849 ctxp->function_decl = current_function_decl;
2850 ctxp->class_type = current_class;
2852 /* Then create a new context which inherits all data from the
2853 previous one. This will be the new current context */
2854 create_new_parser_context (1);
2856 /* Help debugging */
2857 ctxp->next->filename = name;
2860 /* Resume vital data for the current class/function being parsed so
2861 that an other class can be parsed. Used to let local/anonymous
2862 classes be parsed. The trick is the data storing file position
2863 informations must be restored to their current value, so parsing
2864 can resume as if no context was ever saved. */
2866 static void
2867 java_parser_context_resume (void)
2869 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2870 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2871 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2873 /* We need to inherit the list of classes to complete/generate */
2874 restored->classd_list = old->classd_list;
2875 restored->class_list = old->class_list;
2877 /* Restore the current class and function from the saver */
2878 current_class = saver->class_type;
2879 current_function_decl = saver->function_decl;
2881 /* Retrieve the restored context */
2882 ctxp = restored;
2884 /* Re-installed the data for the parsing to carry on */
2885 memcpy (&ctxp->marker_begining, &old->marker_begining,
2886 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2889 /* Add a new anchor node to which all statement(s) initializing static
2890 and non static initialized upon declaration field(s) will be
2891 linked. */
2893 static void
2894 java_parser_context_push_initialized_field (void)
2896 tree node;
2898 node = build_tree_list (NULL_TREE, NULL_TREE);
2899 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2900 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2902 node = build_tree_list (NULL_TREE, NULL_TREE);
2903 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2904 CPC_INITIALIZER_LIST (ctxp) = node;
2906 node = build_tree_list (NULL_TREE, NULL_TREE);
2907 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2908 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2911 /* Pop the lists of initialized field. If this lists aren't empty,
2912 remember them so we can use it to create and populate the finit$
2913 or <clinit> functions. */
2915 static void
2916 java_parser_context_pop_initialized_field (void)
2918 tree stmts;
2919 tree class_type = TREE_TYPE (GET_CPC ());
2921 if (CPC_INITIALIZER_LIST (ctxp))
2923 stmts = CPC_INITIALIZER_STMT (ctxp);
2924 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2925 if (stmts && !java_error_count)
2926 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2929 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2931 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2932 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2933 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2934 /* Keep initialization in order to enforce 8.5 */
2935 if (stmts && !java_error_count)
2936 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2939 /* JDK 1.1 instance initializers */
2940 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2942 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2943 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2944 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2945 if (stmts && !java_error_count)
2946 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2950 static tree
2951 reorder_static_initialized (tree list)
2953 /* We have to keep things in order. The alias initializer have to
2954 come first, then the initialized regular field, in reverse to
2955 keep them in lexical order. */
2956 tree marker, previous = NULL_TREE;
2957 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2958 if (TREE_CODE (marker) == TREE_LIST
2959 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2960 break;
2962 /* No static initialized, the list is fine as is */
2963 if (!previous)
2964 list = TREE_CHAIN (marker);
2966 /* No marker? reverse the whole list */
2967 else if (!marker)
2968 list = nreverse (list);
2970 /* Otherwise, reverse what's after the marker and the new reordered
2971 sublist will replace the marker. */
2972 else
2974 TREE_CHAIN (previous) = NULL_TREE;
2975 list = nreverse (list);
2976 list = chainon (TREE_CHAIN (marker), list);
2978 return list;
2981 /* Helper functions to dump the parser context stack. */
2983 #define TAB_CONTEXT(C) \
2984 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2986 static void
2987 java_debug_context_do (int tab)
2989 struct parser_ctxt *copy = ctxp;
2990 while (copy)
2992 TAB_CONTEXT (tab);
2993 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2994 TAB_CONTEXT (tab);
2995 fprintf (stderr, "filename: %s\n", copy->filename);
2996 TAB_CONTEXT (tab);
2997 fprintf (stderr, "package: %s\n",
2998 (copy->package ?
2999 IDENTIFIER_POINTER (copy->package) : "<none>"));
3000 TAB_CONTEXT (tab);
3001 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
3002 TAB_CONTEXT (tab);
3003 fprintf (stderr, "saved data: %d\n", copy->saved_data);
3004 copy = copy->next;
3005 tab += 2;
3009 /* Dump the stacked up parser contexts. Intended to be called from a
3010 debugger. */
3012 void
3013 java_debug_context (void)
3015 java_debug_context_do (0);
3020 /* Flag for the error report routine to issue the error the first time
3021 it's called (overriding the default behavior which is to drop the
3022 first invocation and honor the second one, taking advantage of a
3023 richer context. */
3024 static int force_error = 0;
3026 /* Reporting an constructor invocation error. */
3027 static void
3028 parse_ctor_invocation_error (void)
3030 if (DECL_CONSTRUCTOR_P (current_function_decl))
3031 yyerror ("Constructor invocation must be first thing in a constructor");
3032 else
3033 yyerror ("Only constructors can invoke constructors");
3036 /* Reporting JDK1.1 features not implemented. */
3038 static tree
3039 parse_jdk1_1_error (const char *msg)
3041 sorry (": %qs JDK1.1(TM) feature", msg);
3042 java_error_count++;
3043 return build_java_empty_stmt ();
3046 static int do_warning = 0;
3048 void
3049 yyerror (const char *msgid)
3051 #ifdef USE_MAPPED_LOCATION
3052 static source_location elc;
3053 expanded_location xloc = expand_location (input_location);
3054 int current_line = xloc.line;
3055 #else
3056 static java_lc elc;
3057 int save_lineno;
3058 int current_line = input_line;
3059 #endif
3060 static int prev_lineno;
3061 static const char *prev_msg;
3063 char *remainder, *code_from_source;
3065 if (!force_error && prev_lineno == current_line)
3066 return;
3067 #ifndef USE_MAPPED_LOCATION
3068 current_line = ctxp->lexer->token_start.line;
3069 #endif
3071 /* Save current error location but report latter, when the context is
3072 richer. */
3073 if (ctxp->java_error_flag == 0)
3075 ctxp->java_error_flag = 1;
3076 #ifdef USE_MAPPED_LOCATION
3077 elc = input_location;
3078 #else
3079 elc = ctxp->lexer->token_start;
3080 #endif
3081 /* Do something to use the previous line if we're reaching the
3082 end of the file... */
3083 #ifdef VERBOSE_SKELETON
3084 printf ("* Error detected (%s)\n", (msgid ? msgid : "(null)"));
3085 #endif
3086 return;
3089 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3090 if (!force_error && msgid == prev_msg && prev_lineno == current_line)
3091 return;
3093 ctxp->java_error_flag = 0;
3094 if (do_warning)
3095 java_warning_count++;
3096 else
3097 java_error_count++;
3099 #if 0 /* FIXME */
3100 if (elc.col == 0 && msgid && msgid[1] == ';')
3101 elc = ctxp->prev_line_end;
3102 #endif
3104 prev_msg = msgid;
3106 #ifdef USE_MAPPED_LOCATION
3107 prev_lineno = current_line;
3108 code_from_source = java_get_line_col (xloc.file, current_line, xloc.column);
3109 #else
3110 save_lineno = input_line;
3111 prev_lineno = input_line = current_line;
3112 code_from_source = java_get_line_col (input_filename, current_line,
3113 ctxp->lexer->token_start.col);
3114 #endif
3117 obstack_grow0 (&temporary_obstack,
3118 code_from_source, strlen (code_from_source));
3119 remainder = obstack_finish (&temporary_obstack);
3120 if (do_warning)
3121 warning ("%s.\n%s", msgid, remainder);
3122 else
3123 error ("%s.\n%s", msgid, remainder);
3125 /* This allow us to cheaply avoid an extra 'Invalid expression
3126 statement' error report when errors have been already reported on
3127 the same line. This occurs when we report an error but don't have
3128 a synchronization point other than ';', which
3129 expression_statement is the only one to take care of. */
3130 #ifndef USE_MAPPED_LOCATION
3131 input_line = save_lineno;
3132 #endif
3133 ctxp->prevent_ese = input_line;
3136 static void
3137 issue_warning_error_from_context (
3138 #ifdef USE_MAPPED_LOCATION
3139 source_location cl,
3140 #else
3141 tree cl,
3142 #endif
3143 const char *msgid, va_list *ap)
3145 #ifdef USE_MAPPED_LOCATION
3146 source_location saved_location = input_location;
3147 expanded_location xloc = expand_location (cl);
3148 #else
3149 java_lc save_lc = ctxp->lexer->token_start;
3150 const char *saved = ctxp->filename, *saved_input_filename;
3151 #endif
3152 char buffer [4096];
3153 text_info text;
3155 text.err_no = errno;
3156 text.args_ptr = ap;
3157 text.format_spec = msgid;
3158 pp_format_text (global_dc->printer, &text);
3159 strncpy (buffer, pp_formatted_text (global_dc->printer), sizeof (buffer) - 1);
3160 buffer[sizeof (buffer) - 1] = '\0';
3161 pp_clear_output_area (global_dc->printer);
3163 force_error = 1;
3165 #ifdef USE_MAPPED_LOCATION
3166 if (xloc.file != NULL)
3168 ctxp->filename = xloc.file;
3169 input_location = cl;
3171 #else
3172 ctxp->lexer->token_start.line = EXPR_WFL_LINENO (cl);
3173 ctxp->lexer->token_start.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1
3174 : EXPR_WFL_COLNO (cl) == 0xffe ? -2
3175 : EXPR_WFL_COLNO (cl));
3177 /* We have a CL, that's a good reason for using it if it contains data */
3178 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3179 ctxp->filename = EXPR_WFL_FILENAME (cl);
3180 saved_input_filename = input_filename;
3181 input_filename = ctxp->filename;
3182 #endif
3183 java_error (NULL);
3184 java_error (buffer);
3185 #ifdef USE_MAPPED_LOCATION
3186 input_location = saved_location;
3187 #else
3188 ctxp->filename = saved;
3189 input_filename = saved_input_filename;
3190 ctxp->lexer->token_start = save_lc;
3191 #endif
3192 force_error = 0;
3195 /* Issue an error message at a current source line CL.
3196 FUTURE/FIXME: change cl to be a source_location. */
3198 void
3199 parse_error_context (tree cl, const char *msgid, ...)
3201 va_list ap;
3202 va_start (ap, msgid);
3203 #ifdef USE_MAPPED_LOCATION
3204 issue_warning_error_from_context (EXPR_LOCATION (cl), msgid, &ap);
3205 #else
3206 issue_warning_error_from_context (cl, msgid, &ap);
3207 #endif
3208 va_end (ap);
3211 /* Issue a warning at a current source line CL.
3212 FUTURE/FIXME: change cl to be a source_location. */
3214 static void
3215 parse_warning_context (tree cl, const char *msgid, ...)
3217 va_list ap;
3218 va_start (ap, msgid);
3220 do_warning = 1;
3221 #ifdef USE_MAPPED_LOCATION
3222 issue_warning_error_from_context (EXPR_LOCATION (cl), msgid, &ap);
3223 #else
3224 issue_warning_error_from_context (cl, msgid, &ap);
3225 #endif
3226 do_warning = 0;
3227 va_end (ap);
3230 static tree
3231 find_expr_with_wfl (tree node)
3233 while (node)
3235 enum tree_code_class code;
3236 tree to_return;
3238 switch (TREE_CODE (node))
3240 case BLOCK:
3241 node = BLOCK_EXPR_BODY (node);
3242 continue;
3244 case COMPOUND_EXPR:
3245 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3246 if (to_return)
3247 return to_return;
3248 node = TREE_OPERAND (node, 1);
3249 continue;
3251 case LOOP_EXPR:
3252 node = TREE_OPERAND (node, 0);
3253 continue;
3255 case LABELED_BLOCK_EXPR:
3256 node = LABELED_BLOCK_BODY (node);
3257 continue;
3259 default:
3260 code = TREE_CODE_CLASS (TREE_CODE (node));
3261 if (((code == tcc_unary) || (code == tcc_binary)
3262 || (code == tcc_expression))
3263 && EXPR_WFL_LINECOL (node))
3264 return node;
3265 return NULL_TREE;
3268 return NULL_TREE;
3271 /* Issue a missing return statement error. Uses METHOD to figure the
3272 last line of the method the error occurs in. */
3274 static void
3275 missing_return_error (tree method)
3277 #ifdef USE_MAPPED_LOCATION
3278 SET_EXPR_LOCATION (wfl_operator, DECL_FUNCTION_LAST_LINE (method));
3279 #else
3280 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_FUNCTION_LAST_LINE (method), -2);
3281 #endif
3282 parse_error_context (wfl_operator, "Missing return statement");
3285 /* Issue an unreachable statement error. From NODE, find the next
3286 statement to report appropriately. */
3287 static void
3288 unreachable_stmt_error (tree node)
3290 /* Browse node to find the next expression node that has a WFL. Use
3291 the location to report the error */
3292 if (TREE_CODE (node) == COMPOUND_EXPR)
3293 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3294 else
3295 node = find_expr_with_wfl (node);
3297 if (node)
3299 #ifdef USE_MAPPED_LOCATION
3300 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
3301 #else
3302 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3303 #endif
3304 parse_error_context (wfl_operator, "Unreachable statement");
3306 else
3307 abort ();
3310 static int
3311 not_accessible_field_error (tree wfl, tree decl)
3313 parse_error_context
3314 (wfl, "Can't access %s field %<%s.%s%> from %qs",
3315 accessibility_string (get_access_flags_from_decl (decl)),
3316 GET_TYPE_NAME (DECL_CONTEXT (decl)),
3317 IDENTIFIER_POINTER (DECL_NAME (decl)),
3318 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
3319 return 1;
3323 java_report_errors (void)
3325 if (java_error_count)
3326 fprintf (stderr, "%d error%s",
3327 java_error_count, (java_error_count == 1 ? "" : "s"));
3328 if (java_warning_count)
3329 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3330 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3331 if (java_error_count || java_warning_count)
3332 putc ('\n', stderr);
3333 return java_error_count;
3336 static char *
3337 java_accstring_lookup (int flags)
3339 static char buffer [80];
3340 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3342 /* Access modifier looked-up first for easier report on forbidden
3343 access. */
3344 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3345 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3346 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3347 if (flags & ACC_STATIC) COPY_RETURN ("static");
3348 if (flags & ACC_FINAL) COPY_RETURN ("final");
3349 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3350 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3351 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3352 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3353 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3354 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3356 buffer [0] = '\0';
3357 return buffer;
3358 #undef COPY_RETURN
3361 /* Returns a string denoting the accessibility of a class or a member as
3362 indicated by FLAGS. We need a separate function from
3363 java_accstring_lookup, as the latter can return spurious "static", etc.
3364 if package-private access is defined (in which case none of the
3365 relevant access control bits in FLAGS is set). */
3367 static const char *
3368 accessibility_string (int flags)
3370 if (flags & ACC_PRIVATE) return "private";
3371 if (flags & ACC_PROTECTED) return "protected";
3372 if (flags & ACC_PUBLIC) return "public";
3374 return "package-private";
3377 /* Issuing error messages upon redefinition of classes, interfaces or
3378 variables. */
3380 static void
3381 classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3383 parse_error_context (cl, "%s %qs already defined in %s:%d",
3384 context, IDENTIFIER_POINTER (id),
3385 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3386 /* Here we should point out where its redefined. It's a unicode. FIXME */
3389 static void
3390 variable_redefinition_error (tree context, tree name, tree type, int line)
3392 const char *type_name;
3394 /* Figure a proper name for type. We might haven't resolved it */
3395 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3396 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3397 else
3398 type_name = lang_printable_name (type, 0);
3400 parse_error_context (context,
3401 "Variable %qs is already defined in this method and was declared %<%s %s%> at line %d",
3402 IDENTIFIER_POINTER (name),
3403 type_name, IDENTIFIER_POINTER (name), line);
3406 /* If ANAME is terminated with `[]', it indicates an array. This
3407 function returns the number of `[]' found and if this number is
3408 greater than zero, it extracts the array type name and places it in
3409 the node pointed to by TRIMMED unless TRIMMED is null. */
3411 static int
3412 build_type_name_from_array_name (tree aname, tree *trimmed)
3414 const char *name = IDENTIFIER_POINTER (aname);
3415 int len = IDENTIFIER_LENGTH (aname);
3416 int array_dims;
3418 STRING_STRIP_BRACKETS (name, len, array_dims);
3420 if (array_dims && trimmed)
3421 *trimmed = get_identifier_with_length (name, len);
3423 return array_dims;
3426 static tree
3427 build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3429 int more_dims = 0;
3431 /* Eventually get more dims */
3432 more_dims = build_type_name_from_array_name (name, &name);
3434 /* If we have, then craft a new type for this variable */
3435 if (more_dims)
3437 tree save = type;
3439 /* If we have a pointer, use its type */
3440 if (TREE_CODE (type) == POINTER_TYPE)
3441 type = TREE_TYPE (type);
3443 /* Building the first dimension of a primitive type uses this
3444 function */
3445 if (JPRIMITIVE_TYPE_P (type))
3447 type = build_java_array_type (type, -1);
3448 more_dims--;
3450 /* Otherwise, if we have a WFL for this type, use it (the type
3451 is already an array on an unresolved type, and we just keep
3452 on adding dimensions) */
3453 else if (type_wfl)
3455 type = type_wfl;
3456 more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3457 NULL);
3460 /* Add all the dimensions */
3461 while (more_dims--)
3462 type = build_unresolved_array_type (type);
3464 /* The type may have been incomplete in the first place */
3465 if (type_wfl)
3466 type = obtain_incomplete_type (type);
3469 if (ret_name)
3470 *ret_name = name;
3471 return type;
3474 /* Build something that the type identifier resolver will identify as
3475 being an array to an unresolved type. TYPE_WFL is a WFL on a
3476 identifier. */
3478 static tree
3479 build_unresolved_array_type (tree type_or_wfl)
3481 const char *ptr;
3482 tree wfl;
3484 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3485 just create a array type */
3486 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3487 return build_java_array_type (type_or_wfl, -1);
3489 obstack_grow (&temporary_obstack,
3490 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3491 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3492 obstack_grow0 (&temporary_obstack, "[]", 2);
3493 ptr = obstack_finish (&temporary_obstack);
3494 #ifdef USE_MAPPED_LOCATION
3495 wfl = build_expr_wfl (get_identifier (ptr), EXPR_LOCATION (type_or_wfl));
3496 #else
3497 wfl = build_expr_wfl (get_identifier (ptr),
3498 EXPR_WFL_FILENAME (type_or_wfl),
3499 EXPR_WFL_LINENO (type_or_wfl),
3500 EXPR_WFL_COLNO (type_or_wfl));
3501 #endif
3502 /* Re-install the existing qualifications so that the type can be
3503 resolved properly. */
3504 EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3505 return wfl;
3508 static void
3509 parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3511 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3512 parse_error_context (wfl, "Interface %qs repeated",
3513 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3516 /* Bulk of common class/interface checks. Return 1 if an error was
3517 encountered. TAG is 0 for a class, 1 for an interface. */
3519 static int
3520 check_class_interface_creation (int is_interface, int flags, tree raw_name,
3521 tree qualified_name, tree decl, tree cl)
3523 tree node;
3524 int sca = 0; /* Static class allowed */
3525 int icaf = 0; /* Inner class allowed flags */
3526 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3528 if (!quiet_flag)
3529 fprintf (stderr, " %s%s %s",
3530 (CPC_INNER_P () ? "inner" : ""),
3531 (is_interface ? "interface" : "class"),
3532 IDENTIFIER_POINTER (qualified_name));
3534 /* Scope of an interface/class type name:
3535 - Can't be imported by a single type import
3536 - Can't already exists in the package */
3537 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3538 && (node = find_name_in_single_imports (raw_name))
3539 && !CPC_INNER_P ())
3541 parse_error_context
3542 (cl, "%s name %qs clashes with imported type %qs",
3543 (is_interface ? "Interface" : "Class"),
3544 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3545 return 1;
3547 if (decl && CLASS_COMPLETE_P (decl))
3549 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3550 qualified_name, decl, cl);
3551 return 1;
3554 if (check_inner_class_redefinition (raw_name, cl))
3555 return 1;
3557 /* If public, file name should match class/interface name, except
3558 when dealing with an inner class */
3559 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3561 const char *fname = input_filename;
3562 const char *f;
3564 for (f = fname + strlen (fname);
3565 f != fname && ! IS_DIR_SEPARATOR (*f);
3566 f--)
3568 if (IS_DIR_SEPARATOR (*f))
3569 f++;
3570 if (strncmp (IDENTIFIER_POINTER (raw_name),
3571 f , IDENTIFIER_LENGTH (raw_name)) ||
3572 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3573 parse_error_context
3574 (cl, "Public %s %qs must be defined in a file called %<%s.java%>",
3575 (is_interface ? "interface" : "class"),
3576 IDENTIFIER_POINTER (qualified_name),
3577 IDENTIFIER_POINTER (raw_name));
3580 /* Static classes can be declared only in top level classes. Note:
3581 once static, a inner class is a top level class. */
3582 if (flags & ACC_STATIC)
3584 /* Catch the specific error of declaring an class inner class
3585 with no toplevel enclosing class. Prevent check_modifiers from
3586 complaining a second time */
3587 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3589 parse_error_context (cl, "Inner class %qs can't be static. Static classes can only occur in interfaces and top-level classes",
3590 IDENTIFIER_POINTER (qualified_name));
3591 sca = ACC_STATIC;
3593 /* Else, in the context of a top-level class declaration, let
3594 `check_modifiers' do its job, otherwise, give it a go */
3595 else
3596 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3599 /* Inner classes can be declared private or protected
3600 within their enclosing classes. */
3601 if (CPC_INNER_P ())
3603 /* A class which is local to a block can't be public, private,
3604 protected or static. But it is created final, so allow this
3605 one. */
3606 if (current_function_decl)
3607 icaf = sca = uaaf = ACC_FINAL;
3608 else
3610 check_modifiers_consistency (flags);
3611 icaf = ACC_PROTECTED;
3612 if (! CLASS_INTERFACE (GET_CPC ()))
3613 icaf |= ACC_PRIVATE;
3617 if (is_interface)
3619 if (CPC_INNER_P ())
3620 uaaf = INTERFACE_INNER_MODIFIERS;
3621 else
3622 uaaf = INTERFACE_MODIFIERS;
3624 check_modifiers ("Illegal modifier %qs for interface declaration",
3625 flags, uaaf);
3627 else
3628 check_modifiers ((current_function_decl ?
3629 "Illegal modifier %qs for local class declaration" :
3630 "Illegal modifier %qs for class declaration"),
3631 flags, uaaf|sca|icaf);
3632 return 0;
3635 /* Construct a nested class name. If the final component starts with
3636 a digit, return true. Otherwise return false. */
3637 static int
3638 make_nested_class_name (tree cpc_list)
3640 tree name;
3642 if (!cpc_list)
3643 return 0;
3645 make_nested_class_name (TREE_CHAIN (cpc_list));
3647 /* Pick the qualified name when dealing with the first upmost
3648 enclosing class */
3649 name = (TREE_CHAIN (cpc_list)
3650 ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3651 obstack_grow (&temporary_obstack,
3652 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3653 obstack_1grow (&temporary_obstack, '$');
3655 return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3658 /* Can't redefine a class already defined in an earlier scope. */
3660 static int
3661 check_inner_class_redefinition (tree raw_name, tree cl)
3663 tree scope_list;
3665 for (scope_list = GET_CPC_LIST (); scope_list;
3666 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3667 if (raw_name == GET_CPC_UN_NODE (scope_list))
3669 parse_error_context
3670 (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",
3671 IDENTIFIER_POINTER (raw_name));
3672 return 1;
3674 return 0;
3677 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3678 we remember ENCLOSING and SUPER. */
3680 static tree
3681 resolve_inner_class (htab_t circularity_hash, tree cl, tree *enclosing,
3682 tree *super, tree class_type)
3684 tree local_enclosing = *enclosing;
3685 tree local_super = NULL_TREE;
3687 while (local_enclosing)
3689 tree intermediate, decl;
3691 *htab_find_slot (circularity_hash, local_enclosing, INSERT) =
3692 local_enclosing;
3694 if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3695 return decl;
3697 intermediate = local_enclosing;
3698 /* Explore enclosing contexts. */
3699 while (INNER_CLASS_DECL_P (intermediate))
3701 intermediate = DECL_CONTEXT (intermediate);
3702 if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3703 return decl;
3706 /* Now go to the upper classes, bail out if necessary. We will
3707 analyze the returned SUPER and act accordingly (see
3708 do_resolve_class). */
3709 if (JPRIMITIVE_TYPE_P (TREE_TYPE (local_enclosing))
3710 || TREE_TYPE (local_enclosing) == void_type_node)
3712 parse_error_context (cl, "Qualifier must be a reference");
3713 local_enclosing = NULL_TREE;
3714 break;
3716 local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3717 if (!local_super || local_super == object_type_node)
3718 break;
3720 if (TREE_CODE (local_super) == POINTER_TYPE)
3721 local_super = do_resolve_class (NULL, local_super, NULL, NULL);
3722 else
3723 local_super = TYPE_NAME (local_super);
3725 /* We may not have checked for circular inheritance yet, so do so
3726 here to prevent an infinite loop. */
3727 if (htab_find (circularity_hash, local_super) != NULL)
3729 if (!cl)
3730 cl = lookup_cl (local_enclosing);
3732 parse_error_context
3733 (cl, "Cyclic inheritance involving %s",
3734 IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3735 local_enclosing = NULL_TREE;
3737 else
3738 local_enclosing = local_super;
3741 /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3742 *super = local_super;
3743 *enclosing = local_enclosing;
3745 return NULL_TREE;
3748 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3749 qualified. */
3751 static tree
3752 find_as_inner_class (tree enclosing, tree name, tree cl)
3754 tree qual, to_return;
3755 if (!enclosing)
3756 return NULL_TREE;
3758 name = TYPE_NAME (name);
3760 /* First search: within the scope of `enclosing', search for name */
3761 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3762 qual = EXPR_WFL_QUALIFICATION (cl);
3763 else if (cl)
3764 qual = build_tree_list (cl, NULL_TREE);
3765 else
3766 qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3768 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3769 return to_return;
3771 /* We're dealing with a qualified name. Try to resolve thing until
3772 we get something that is an enclosing class. */
3773 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3775 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3777 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3778 qual = TREE_CHAIN (qual))
3780 acc = merge_qualified_name (acc,
3781 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3782 BUILD_PTR_FROM_NAME (ptr, acc);
3783 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3786 /* A NULL qual and a decl means that the search ended
3787 successfully?!? We have to do something then. FIXME */
3789 if (decl)
3790 enclosing = decl;
3791 else
3792 qual = EXPR_WFL_QUALIFICATION (cl);
3794 /* Otherwise, create a qual for the other part of the resolution. */
3795 else
3796 qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3798 return find_as_inner_class_do (qual, enclosing);
3801 /* We go inside the list of sub classes and try to find a way
3802 through. */
3804 static tree
3805 find_as_inner_class_do (tree qual, tree enclosing)
3807 if (!qual)
3808 return NULL_TREE;
3810 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3812 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3813 tree next_enclosing = NULL_TREE;
3814 tree inner_list;
3816 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3817 inner_list; inner_list = TREE_CHAIN (inner_list))
3819 if (TREE_VALUE (inner_list) == name_to_match)
3821 next_enclosing = TREE_PURPOSE (inner_list);
3822 break;
3825 enclosing = next_enclosing;
3828 return (!qual && enclosing ? enclosing : NULL_TREE);
3831 static void
3832 link_nested_class_to_enclosing (void)
3834 if (GET_ENCLOSING_CPC ())
3836 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3837 DECL_INNER_CLASS_LIST (enclosing) =
3838 tree_cons (GET_CPC (), GET_CPC_UN (),
3839 DECL_INNER_CLASS_LIST (enclosing));
3843 static tree
3844 maybe_make_nested_class_name (tree name)
3846 tree id = NULL_TREE;
3848 if (CPC_INNER_P ())
3850 /* If we're in a function, we must append a number to create the
3851 nested class name. However, we don't do this if the class we
3852 are constructing is anonymous, because in that case we'll
3853 already have a number as the class name. */
3854 if (! make_nested_class_name (GET_CPC_LIST ())
3855 && current_function_decl != NULL_TREE
3856 && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3858 char buf[10];
3859 sprintf (buf, "%d", anonymous_class_counter);
3860 ++anonymous_class_counter;
3861 obstack_grow (&temporary_obstack, buf, strlen (buf));
3862 obstack_1grow (&temporary_obstack, '$');
3864 obstack_grow0 (&temporary_obstack,
3865 IDENTIFIER_POINTER (name),
3866 IDENTIFIER_LENGTH (name));
3867 id = get_identifier (obstack_finish (&temporary_obstack));
3868 if (ctxp->package)
3869 QUALIFIED_P (id) = 1;
3871 return id;
3874 /* If DECL is NULL, create and push a new DECL, record the current
3875 line CL and do other maintenance things. */
3877 static tree
3878 maybe_create_class_interface_decl (tree decl, tree raw_name,
3879 tree qualified_name, tree cl)
3881 if (!decl)
3882 decl = push_class (make_class (), qualified_name);
3884 /* Take care of the file and line business */
3885 #ifdef USE_MAPPED_LOCATION
3886 DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (cl);
3887 #else
3888 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3889 /* If we're emitting xrefs, store the line/col number information */
3890 if (flag_emit_xref)
3891 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3892 else
3893 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3894 #endif
3895 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3896 CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3897 #ifdef USE_MAPPED_LOCATION
3899 tree tmp = maybe_get_identifier (EXPR_FILENAME (cl));
3900 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3901 tmp && IS_A_COMMAND_LINE_FILENAME_P (tmp);
3903 #else
3904 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3905 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3906 #endif
3908 PUSH_CPC (decl, raw_name);
3909 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3911 /* Link the declaration to the already seen ones */
3912 TREE_CHAIN (decl) = ctxp->class_list;
3913 ctxp->class_list = decl;
3915 /* Create a new nodes in the global lists */
3916 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3917 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3919 /* Install a new dependency list element */
3920 create_jdep_list (ctxp);
3922 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3923 IDENTIFIER_POINTER (qualified_name)));
3924 return decl;
3927 static void
3928 add_superinterfaces (tree decl, tree interface_list)
3930 tree node;
3931 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3932 takes care of ensuring that:
3933 - This is an accessible interface type,
3934 - Circularity detection.
3935 parser_add_interface is then called. If present but not defined,
3936 the check operation is delayed until the super interface gets
3937 defined. */
3938 for (node = interface_list; node; node = TREE_CHAIN (node))
3940 tree current = TREE_PURPOSE (node);
3941 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3942 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3944 if (!parser_check_super_interface (idecl, decl, current))
3945 parser_add_interface (decl, idecl, current);
3947 else
3948 register_incomplete_type (JDEP_INTERFACE,
3949 current, decl, NULL_TREE);
3953 /* Create an interface in pass1 and return its decl. Return the
3954 interface's decl in pass 2. */
3956 static tree
3957 create_interface (int flags, tree id, tree super)
3959 tree raw_name = EXPR_WFL_NODE (id);
3960 tree q_name = parser_qualified_classname (raw_name);
3961 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3963 /* Certain syntax errors are making SUPER be like ID. Avoid this
3964 case. */
3965 if (ctxp->class_err && id == super)
3966 super = NULL;
3968 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3970 /* Basic checks: scope, redefinition, modifiers */
3971 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3973 PUSH_ERROR ();
3974 return NULL_TREE;
3977 /* Suspend the current parsing context if we're parsing an inner
3978 interface */
3979 if (CPC_INNER_P ())
3981 java_parser_context_suspend ();
3982 /* Interface members are public. */
3983 if (CLASS_INTERFACE (GET_CPC ()))
3984 flags |= ACC_PUBLIC;
3987 /* Push a new context for (static) initialized upon declaration fields */
3988 java_parser_context_push_initialized_field ();
3990 /* Interface modifiers check
3991 - public/abstract allowed (already done at that point)
3992 - abstract is obsolete (comes first, it's a warning, or should be)
3993 - Can't use twice the same (checked in the modifier rule) */
3994 if ((flags & ACC_ABSTRACT) && flag_redundant)
3995 parse_warning_context
3996 (MODIFIER_WFL (ABSTRACT_TK),
3997 "Redundant use of %<abstract%> modifier. Interface %qs is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3999 /* Create a new decl if DECL is NULL, otherwise fix it */
4000 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
4002 /* Interfaces are always abstract. */
4003 flags |= ACC_ABSTRACT;
4005 /* Inner interfaces are always static. */
4006 if (INNER_CLASS_DECL_P (decl))
4007 flags |= ACC_STATIC;
4009 /* Set super info and mark the class a complete */
4010 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
4011 object_type_node, ctxp->interface_number);
4012 ctxp->interface_number = 0;
4013 CLASS_COMPLETE_P (decl) = 1;
4014 add_superinterfaces (decl, super);
4016 /* Eventually sets the @deprecated tag flag */
4017 CHECK_DEPRECATED (decl);
4019 return decl;
4022 /* Patch anonymous class CLASS, by either extending or implementing
4023 DEP. */
4025 static void
4026 patch_anonymous_class (tree type_decl, tree class_decl, tree wfl)
4028 tree class = TREE_TYPE (class_decl);
4029 tree type = TREE_TYPE (type_decl);
4030 tree binfo = TYPE_BINFO (class);
4032 /* If it's an interface, implement it */
4033 if (CLASS_INTERFACE (type_decl))
4035 if (parser_check_super_interface (type_decl, class_decl, wfl))
4036 return;
4038 if (!VEC_space (tree, BINFO_BASE_BINFOS (binfo), 1))
4040 /* Extend the binfo - by reallocating and copying it. */
4041 tree new_binfo;
4042 tree base_binfo;
4043 int i;
4045 new_binfo = make_tree_binfo ((BINFO_N_BASE_BINFOS (binfo) + 1) * 2);
4046 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4047 BINFO_BASE_APPEND (new_binfo, base_binfo);
4048 CLASS_HAS_SUPER_FLAG (new_binfo) = CLASS_HAS_SUPER_FLAG (binfo);
4049 BINFO_VTABLE (new_binfo) = BINFO_VTABLE (binfo);
4050 TYPE_BINFO (class) = new_binfo;
4053 /* And add the interface */
4054 parser_add_interface (class_decl, type_decl, wfl);
4056 /* Otherwise, it's a type we want to extend */
4057 else
4059 if (parser_check_super (type_decl, class_decl, wfl))
4060 return;
4061 BINFO_TYPE (BINFO_BASE_BINFO (binfo, 0)) = type;
4065 /* Create an anonymous class which extends/implements TYPE_NAME, and return
4066 its decl. */
4068 static tree
4069 create_anonymous_class (tree type_name)
4071 char buffer [80];
4072 tree super = NULL_TREE, itf = NULL_TREE;
4073 tree id, type_decl, class;
4075 /* The unqualified name of the anonymous class. It's just a number. */
4076 sprintf (buffer, "%d", anonymous_class_counter++);
4077 id = build_wfl_node (get_identifier (buffer));
4078 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL (type_name);
4080 /* We know about the type to extend/implement. We go ahead */
4081 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
4083 /* Create a class which either implements on extends the designated
4084 class. The class bears an inaccessible name. */
4085 if (CLASS_INTERFACE (type_decl))
4087 /* It's OK to modify it here. It's been already used and
4088 shouldn't be reused */
4089 ctxp->interface_number = 1;
4090 /* Interfaces should presented as a list of WFLs */
4091 itf = build_tree_list (type_name, NULL_TREE);
4093 else
4094 super = type_name;
4097 class = create_class (ACC_FINAL, id, super, itf);
4099 /* We didn't know anything about the stuff. We register a dependence. */
4100 if (!type_decl)
4101 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
4103 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
4104 return class;
4107 /* Create a class in pass1 and return its decl. Return class
4108 interface's decl in pass 2. */
4110 static tree
4111 create_class (int flags, tree id, tree super, tree interfaces)
4113 tree raw_name = EXPR_WFL_NODE (id);
4114 tree class_id, decl;
4115 tree super_decl_type;
4117 /* Certain syntax errors are making SUPER be like ID. Avoid this
4118 case. */
4119 if (ctxp->class_err && id == super)
4120 super = NULL;
4122 class_id = parser_qualified_classname (raw_name);
4123 decl = IDENTIFIER_CLASS_VALUE (class_id);
4124 EXPR_WFL_NODE (id) = class_id;
4126 /* Basic check: scope, redefinition, modifiers */
4127 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
4129 PUSH_ERROR ();
4130 return NULL_TREE;
4133 /* Suspend the current parsing context if we're parsing an inner
4134 class or an anonymous class. */
4135 if (CPC_INNER_P ())
4137 java_parser_context_suspend ();
4138 /* Interface members are public. */
4139 if (CLASS_INTERFACE (GET_CPC ()))
4140 flags |= ACC_PUBLIC;
4143 /* Push a new context for (static) initialized upon declaration fields */
4144 java_parser_context_push_initialized_field ();
4146 /* Class modifier check:
4147 - Allowed modifier (already done at that point)
4148 - abstract AND final forbidden
4149 - Public classes defined in the correct file */
4150 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4151 parse_error_context
4152 (id, "Class %qs can't be declared both abstract and final",
4153 IDENTIFIER_POINTER (raw_name));
4155 /* Create a new decl if DECL is NULL, otherwise fix it */
4156 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4158 /* If SUPER exists, use it, otherwise use Object */
4159 if (super)
4161 /* java.lang.Object can't extend anything. */
4162 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4164 parse_error_context (id, "%<java.lang.Object%> can't extend anything");
4165 return NULL_TREE;
4168 super_decl_type =
4169 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4171 else if (TREE_TYPE (decl) != object_type_node)
4172 super_decl_type = object_type_node;
4173 /* We're defining java.lang.Object */
4174 else
4175 super_decl_type = NULL_TREE;
4177 /* A class nested in an interface is implicitly static. */
4178 if (INNER_CLASS_DECL_P (decl)
4179 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4181 flags |= ACC_STATIC;
4184 /* Set super info and mark the class as complete. */
4185 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4186 ctxp->interface_number);
4187 ctxp->interface_number = 0;
4188 CLASS_COMPLETE_P (decl) = 1;
4189 add_superinterfaces (decl, interfaces);
4191 /* TYPE_VFIELD' is a compiler-generated field used to point to
4192 virtual function tables. In gcj, every class has a common base
4193 virtual function table in java.lang.object. */
4194 TYPE_VFIELD (TREE_TYPE (decl)) = TYPE_VFIELD (object_type_node);
4196 /* Add the private this$<n> field, Replicate final locals still in
4197 scope as private final fields mangled like val$<local_name>.
4198 This does not occur for top level (static) inner classes. */
4199 if (PURE_INNER_CLASS_DECL_P (decl))
4200 add_inner_class_fields (decl, current_function_decl);
4202 /* If doing xref, store the location at which the inherited class
4203 (if any) was seen. */
4204 if (flag_emit_xref && super)
4205 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
4207 /* Eventually sets the @deprecated tag flag */
4208 CHECK_DEPRECATED (decl);
4210 /* Reset the anonymous class counter when declaring non inner classes */
4211 if (!INNER_CLASS_DECL_P (decl))
4212 anonymous_class_counter = 1;
4214 return decl;
4217 /* End a class declaration: register the statements used to create
4218 finit$ and <clinit>, pop the current class and resume the prior
4219 parser context if necessary. */
4221 static void
4222 end_class_declaration (int resume)
4224 /* If an error occurred, context weren't pushed and won't need to be
4225 popped by a resume. */
4226 int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4228 if (GET_CPC () != error_mark_node)
4229 dump_java_tree (TDI_class, GET_CPC ());
4231 java_parser_context_pop_initialized_field ();
4232 POP_CPC ();
4233 if (resume && no_error_occurred)
4234 java_parser_context_resume ();
4236 /* We're ending a class declaration, this is a good time to reset
4237 the interface cout. Note that might have been already done in
4238 create_interface, but if at that time an inner class was being
4239 dealt with, the interface count was reset in a context created
4240 for the sake of handling inner classes declaration. */
4241 ctxp->interface_number = 0;
4244 static void
4245 add_inner_class_fields (tree class_decl, tree fct_decl)
4247 tree block, marker, f;
4249 f = add_field (TREE_TYPE (class_decl),
4250 build_current_thisn (TREE_TYPE (class_decl)),
4251 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4252 ACC_PRIVATE);
4253 FIELD_THISN (f) = 1;
4255 if (!fct_decl)
4256 return;
4258 for (block = GET_CURRENT_BLOCK (fct_decl);
4259 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4261 tree decl;
4262 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4264 tree name, pname;
4265 tree wfl, init, list;
4267 /* Avoid non final arguments. */
4268 if (!LOCAL_FINAL_P (decl))
4269 continue;
4271 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4272 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4273 wfl = build_wfl_node (name);
4274 init = build_wfl_node (pname);
4275 /* Build an initialization for the field: it will be
4276 initialized by a parameter added to finit$, bearing a
4277 mangled name of the field itself (param$<n>.) The
4278 parameter is provided to finit$ by the constructor
4279 invoking it (hence the constructor will also feature a
4280 hidden parameter, set to the value of the outer context
4281 local at the time the inner class is created.)
4283 Note: we take into account all possible locals that can
4284 be accessed by the inner class. It's actually not trivial
4285 to minimize these aliases down to the ones really
4286 used. One way to do that would be to expand all regular
4287 methods first, then finit$ to get a picture of what's
4288 used. It works with the exception that we would have to
4289 go back on all constructor invoked in regular methods to
4290 have their invocation reworked (to include the right amount
4291 of alias initializer parameters.)
4293 The only real way around, I think, is a first pass to
4294 identify locals really used in the inner class. We leave
4295 the flag FIELD_LOCAL_ALIAS_USED around for that future
4296 use.
4298 On the other hand, it only affect local inner classes,
4299 whose constructors (and finit$ call) will be featuring
4300 unnecessary arguments. It's easy for a developer to keep
4301 this number of parameter down by using the `final'
4302 keyword only when necessary. For the time being, we can
4303 issue a warning on unnecessary finals. FIXME */
4304 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4305 wfl, init);
4307 /* Register the field. The TREE_LIST holding the part
4308 initialized/initializer will be marked ARG_FINAL_P so
4309 that the created field can be marked
4310 FIELD_LOCAL_ALIAS. */
4311 list = build_tree_list (wfl, init);
4312 ARG_FINAL_P (list) = 1;
4313 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4317 if (!CPC_INITIALIZER_STMT (ctxp))
4318 return;
4320 /* If we ever registered an alias field, insert and marker to
4321 remember where the list ends. The second part of the list (the one
4322 featuring initialized fields) so it can be later reversed to
4323 enforce 8.5. The marker will be removed during that operation. */
4324 marker = build_tree_list (NULL_TREE, NULL_TREE);
4325 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4326 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4329 /* Can't use lookup_field () since we don't want to load the class and
4330 can't set the CLASS_LOADED_P flag */
4332 static tree
4333 find_field (tree class, tree name)
4335 tree decl;
4336 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4338 if (DECL_NAME (decl) == name)
4339 return decl;
4341 return NULL_TREE;
4344 /* Wrap around lookup_field that doesn't potentially upset the value
4345 of CLASS */
4347 static tree
4348 lookup_field_wrapper (tree class, tree name)
4350 tree type = class;
4351 tree decl = NULL_TREE;
4352 java_parser_context_save_global ();
4354 /* Last chance: if we're within the context of an inner class, we
4355 might be trying to access a local variable defined in an outer
4356 context. We try to look for it now. */
4357 if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4359 tree new_name;
4360 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4361 decl = lookup_field (&type, new_name);
4362 if (decl && decl != error_mark_node)
4363 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4365 if (!decl || decl == error_mark_node)
4367 type = class;
4368 decl = lookup_field (&type, name);
4371 /* If the field still hasn't been found, try the next enclosing context. */
4372 if (!decl && INNER_CLASS_TYPE_P (class))
4374 tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4375 decl = lookup_field_wrapper (outer_type, name);
4378 java_parser_context_restore_global ();
4379 return decl == error_mark_node ? NULL : decl;
4382 /* Find duplicate field within the same class declarations and report
4383 the error. Returns 1 if a duplicated field was found, 0
4384 otherwise. */
4386 static int
4387 duplicate_declaration_error_p (tree new_field_name, tree new_type, tree cl)
4389 /* This might be modified to work with method decl as well */
4390 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4391 if (decl)
4393 char *t1 = xstrdup (purify_type_name
4394 ((TREE_CODE (new_type) == POINTER_TYPE
4395 && TREE_TYPE (new_type) == NULL_TREE) ?
4396 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4397 lang_printable_name (new_type, 1)));
4398 /* The type may not have been completed by the time we report
4399 the error */
4400 char *t2 = xstrdup (purify_type_name
4401 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4402 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4403 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4404 lang_printable_name (TREE_TYPE (decl), 1)));
4405 parse_error_context
4406 (cl, "Duplicate variable declaration: %<%s %s%> was %<%s %s%> (%s:%d)",
4407 t1, IDENTIFIER_POINTER (new_field_name),
4408 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4409 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4410 free (t1);
4411 free (t2);
4412 return 1;
4414 return 0;
4417 /* Field registration routine. If TYPE doesn't exist, field
4418 declarations are linked to the undefined TYPE dependency list, to
4419 be later resolved in java_complete_class () */
4421 static void
4422 register_fields (int flags, tree type, tree variable_list)
4424 tree current, saved_type;
4425 tree class_type = NULL_TREE;
4426 location_t saved_location = input_location;
4427 int must_chain = 0;
4428 tree wfl = NULL_TREE;
4430 if (GET_CPC ())
4431 class_type = TREE_TYPE (GET_CPC ());
4433 if (!class_type || class_type == error_mark_node)
4434 return;
4436 /* If we're adding fields to interfaces, those fields are public,
4437 static, final */
4438 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4440 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4441 flags, ACC_PUBLIC, "interface field(s)");
4442 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4443 flags, ACC_STATIC, "interface field(s)");
4444 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4445 flags, ACC_FINAL, "interface field(s)");
4446 check_modifiers ("Illegal interface member modifier %qs", flags,
4447 INTERFACE_FIELD_MODIFIERS);
4448 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4451 /* Obtain a suitable type for resolution, if necessary */
4452 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4454 /* If TYPE is fully resolved and we don't have a reference, make one */
4455 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4457 for (current = variable_list, saved_type = type; current;
4458 current = TREE_CHAIN (current), type = saved_type)
4460 tree real_type;
4461 tree field_decl;
4462 tree cl = TREE_PURPOSE (current);
4463 tree init = TREE_VALUE (current);
4464 tree current_name = EXPR_WFL_NODE (cl);
4466 /* Can't declare non-final static fields in inner classes */
4467 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4468 && !(flags & ACC_FINAL))
4469 parse_error_context
4470 (cl, "Field %qs can't be static in inner class %qs unless it is final",
4471 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4472 lang_printable_name (class_type, 0));
4474 /* Process NAME, as it may specify extra dimension(s) for it */
4475 type = build_array_from_name (type, wfl, current_name, &current_name);
4477 /* Type adjustment. We may have just readjusted TYPE because
4478 the variable specified more dimensions. Make sure we have
4479 a reference if we can and don't have one already. Also
4480 change the name if we have an init. */
4481 if (type != saved_type)
4483 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4484 if (init)
4485 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4488 real_type = GET_REAL_TYPE (type);
4489 /* Check for redeclarations */
4490 if (duplicate_declaration_error_p (current_name, real_type, cl))
4491 continue;
4493 /* Set input_line to the line the field was found and create a
4494 declaration for it. Eventually sets the @deprecated tag flag. */
4495 #ifdef USE_MAPPED_LOCATION
4496 input_location = EXPR_LOCATION (cl);
4497 #else
4498 if (flag_emit_xref)
4499 input_line = EXPR_WFL_LINECOL (cl);
4500 else
4501 input_line = EXPR_WFL_LINENO (cl);
4502 #endif
4503 field_decl = add_field (class_type, current_name, real_type, flags);
4504 CHECK_DEPRECATED_NO_RESET (field_decl);
4506 /* If the field denotes a final instance variable, then we
4507 allocate a LANG_DECL_SPECIFIC part to keep track of its
4508 initialization. We also mark whether the field was
4509 initialized upon its declaration. We don't do that if the
4510 created field is an alias to a final local. */
4511 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4513 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4514 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4517 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4518 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4519 hide parameters to this inner class finit$ and
4520 constructors. It also means that the field isn't final per
4521 say. */
4522 if (ARG_FINAL_P (current))
4524 FIELD_LOCAL_ALIAS (field_decl) = 1;
4525 FIELD_FINAL (field_decl) = 0;
4528 /* Check if we must chain. */
4529 if (must_chain)
4530 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4532 /* If we have an initialization value tied to the field */
4533 if (init)
4535 /* The field is declared static */
4536 if (flags & ACC_STATIC)
4538 /* We include the field and its initialization part into
4539 a list used to generate <clinit>. After <clinit> is
4540 walked, field initializations will be processed and
4541 fields initialized with known constants will be taken
4542 out of <clinit> and have their DECL_INITIAL set
4543 appropriately. */
4544 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4545 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4546 if (TREE_OPERAND (init, 1)
4547 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4548 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4550 /* A non-static field declared with an immediate initialization is
4551 to be initialized in <init>, if any. This field is remembered
4552 to be processed at the time of the generation of <init>. */
4553 else
4555 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4556 SET_CPC_INITIALIZER_STMT (ctxp, init);
4558 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4559 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4563 CLEAR_DEPRECATED;
4564 input_location = saved_location;
4567 /* Generate finit$, using the list of initialized fields to populate
4568 its body. finit$'s parameter(s) list is adjusted to include the
4569 one(s) used to initialized the field(s) caching outer context
4570 local(s). */
4572 static tree
4573 generate_finit (tree class_type)
4575 int count = 0;
4576 tree list = TYPE_FINIT_STMT_LIST (class_type);
4577 tree mdecl, current, parms;
4579 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4580 class_type, NULL_TREE,
4581 &count);
4582 CRAFTED_PARAM_LIST_FIXUP (parms);
4583 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4584 finit_identifier_node, parms);
4585 fix_method_argument_names (parms, mdecl);
4586 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4587 mdecl, NULL_TREE);
4588 DECL_FUNCTION_NAP (mdecl) = count;
4589 start_artificial_method_body (mdecl);
4591 for (current = list; current; current = TREE_CHAIN (current))
4592 java_method_add_stmt (mdecl,
4593 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4594 current));
4595 end_artificial_method_body (mdecl);
4596 return mdecl;
4599 /* Generate a function to run the instance initialization code. The
4600 private method is called `instinit$'. Unless we're dealing with an
4601 anonymous class, we determine whether all ctors of CLASS_TYPE
4602 declare a checked exception in their `throws' clause in order to
4603 see whether it's necessary to encapsulate the instance initializer
4604 statements in a try/catch/rethrow sequence. */
4606 static tree
4607 generate_instinit (tree class_type)
4609 tree current;
4610 tree compound = NULL_TREE;
4611 tree parms = tree_cons (this_identifier_node,
4612 build_pointer_type (class_type), end_params_node);
4613 tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4614 void_type_node,
4615 instinit_identifier_node, parms);
4617 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4618 mdecl, NULL_TREE);
4620 /* Gather all the statements in a compound */
4621 for (current = TYPE_II_STMT_LIST (class_type);
4622 current; current = TREE_CHAIN (current))
4623 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4625 /* We need to encapsulate COMPOUND by a try/catch statement to
4626 rethrow exceptions that might occur in the instance initializer.
4627 We do that only if all ctors of CLASS_TYPE are set to catch a
4628 checked exception. This doesn't apply to anonymous classes (since
4629 they don't have declared ctors.) */
4630 if (!ANONYMOUS_CLASS_P (class_type) &&
4631 ctors_unchecked_throws_clause_p (class_type))
4633 compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4634 build1 (THROW_EXPR, NULL_TREE,
4635 build_wfl_node (wpv_id)));
4636 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4637 exception_type_node);
4640 start_artificial_method_body (mdecl);
4641 java_method_add_stmt (mdecl, compound);
4642 end_artificial_method_body (mdecl);
4644 return mdecl;
4647 /* FIXME */
4648 static tree
4649 build_instinit_invocation (tree class_type)
4651 tree to_return = NULL_TREE;
4653 if (TYPE_II_STMT_LIST (class_type))
4655 tree parm = build_tree_list (NULL_TREE,
4656 build_wfl_node (this_identifier_node));
4657 to_return =
4658 build_method_invocation (build_wfl_node (instinit_identifier_node),
4659 parm);
4661 return to_return;
4664 /* Shared across method_declarator and method_header to remember the
4665 patch stage that was reached during the declaration of the method.
4666 A method DECL is built differently is there is no patch
4667 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4668 pending on the currently defined method. */
4670 static int patch_stage;
4672 /* Check the method declaration and add the method to its current
4673 class. If the argument list is known to contain incomplete types,
4674 the method is partially added and the registration will be resume
4675 once the method arguments resolved. If TYPE is NULL, we're dealing
4676 with a constructor. */
4678 static tree
4679 method_header (int flags, tree type, tree mdecl, tree throws)
4681 tree type_wfl = NULL_TREE;
4682 tree meth_name = NULL_TREE;
4683 tree current, orig_arg, this_class = NULL;
4684 tree id, meth;
4685 location_t saved_location;
4686 int constructor_ok = 0, must_chain;
4687 int count;
4689 if (mdecl == error_mark_node)
4690 return error_mark_node;
4691 meth = TREE_VALUE (mdecl);
4692 id = TREE_PURPOSE (mdecl);
4694 check_modifiers_consistency (flags);
4696 if (GET_CPC ())
4697 this_class = TREE_TYPE (GET_CPC ());
4699 if (!this_class || this_class == error_mark_node)
4700 return NULL_TREE;
4702 /* There are some forbidden modifiers for an abstract method and its
4703 class must be abstract as well. */
4704 if (type && (flags & ACC_ABSTRACT))
4706 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4707 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4708 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4709 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4710 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4711 ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4712 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4713 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4714 parse_error_context
4715 (id,
4716 "Class %qs must be declared abstract to define abstract method %qs",
4717 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4718 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4721 /* A native method can't be strictfp. */
4722 if ((flags & ACC_NATIVE) && (flags & ACC_STRICT))
4723 parse_error_context (id, "native method %qs can't be strictfp",
4724 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4725 /* No such thing as a transient or volatile method. */
4726 if ((flags & ACC_TRANSIENT))
4727 parse_error_context (id, "method %qs can't be transient",
4728 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4729 if ((flags & ACC_VOLATILE))
4730 parse_error_context (id, "method %qs can't be volatile",
4731 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4733 /* Things to be checked when declaring a constructor */
4734 if (!type)
4736 int ec = java_error_count;
4737 /* 8.6: Constructor declarations: we might be trying to define a
4738 method without specifying a return type. */
4739 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4740 parse_error_context
4741 (id, "Invalid method declaration, return type required");
4742 /* 8.6.3: Constructor modifiers */
4743 else
4745 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4746 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4747 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4748 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4749 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4750 JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4752 /* If we found error here, we don't consider it's OK to tread
4753 the method definition as a constructor, for the rest of this
4754 function */
4755 if (ec == java_error_count)
4756 constructor_ok = 1;
4759 /* Method declared within the scope of an interface are implicitly
4760 abstract and public. Conflicts with other erroneously provided
4761 modifiers are checked right after. */
4763 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4765 /* If FLAGS isn't set because of a modifier, turn the
4766 corresponding modifier WFL to NULL so we issue a warning on
4767 the obsolete use of the modifier */
4768 if (!(flags & ACC_PUBLIC))
4769 MODIFIER_WFL (PUBLIC_TK) = NULL;
4770 if (!(flags & ACC_ABSTRACT))
4771 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4772 flags |= ACC_PUBLIC;
4773 flags |= ACC_ABSTRACT;
4776 /* Inner class can't declare static methods */
4777 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4779 parse_error_context
4780 (id, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
4781 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4782 lang_printable_name (this_class, 0));
4785 /* Modifiers context reset moved up, so abstract method declaration
4786 modifiers can be later checked. */
4788 /* Set constructor returned type to void and method name to <init>,
4789 unless we found an error identifier the constructor (in which
4790 case we retain the original name) */
4791 if (!type)
4793 type = void_type_node;
4794 if (constructor_ok)
4795 meth_name = init_identifier_node;
4797 else
4798 meth_name = EXPR_WFL_NODE (id);
4800 /* Do the returned type resolution and registration if necessary */
4801 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4803 if (meth_name)
4804 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4805 EXPR_WFL_NODE (id) = meth_name;
4806 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4808 if (must_chain)
4810 patch_stage = JDEP_METHOD_RETURN;
4811 register_incomplete_type (patch_stage, type_wfl, id, type);
4812 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4814 else
4815 TREE_TYPE (meth) = type;
4817 saved_location = input_location;
4818 /* When defining an abstract or interface method, the curly
4819 bracket at level 1 doesn't exist because there is no function
4820 body */
4821 #ifdef USE_MAPPED_LOCATION
4822 input_location = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4823 EXPR_LOCATION (id));
4824 #else
4825 input_line = (ctxp->first_ccb_indent1 ? (int) ctxp->first_ccb_indent1 :
4826 EXPR_WFL_LINENO (id));
4827 #endif
4829 /* Remember the original argument list */
4830 orig_arg = TYPE_ARG_TYPES (meth);
4832 if (patch_stage) /* includes ret type and/or all args */
4834 jdep *jdep;
4835 meth = add_method_1 (this_class, flags, meth_name, meth);
4836 /* Patch for the return type */
4837 if (patch_stage == JDEP_METHOD_RETURN)
4839 jdep = CLASSD_LAST (ctxp->classd_list);
4840 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4842 /* This is the stop JDEP. METH allows the function's signature
4843 to be computed. */
4844 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4846 else
4847 meth = add_method (this_class, flags, meth_name,
4848 build_java_signature (meth));
4850 /* Remember final parameters */
4851 MARK_FINAL_PARMS (meth, orig_arg);
4853 /* Fix the method argument list so we have the argument name
4854 information */
4855 fix_method_argument_names (orig_arg, meth);
4857 /* Register the parameter number and re-install the current line
4858 number */
4859 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4860 input_location = saved_location;
4862 /* Register exception specified by the `throws' keyword for
4863 resolution and set the method decl appropriate field to the list.
4864 Note: the grammar ensures that what we get here are class
4865 types. */
4866 if (throws)
4868 throws = nreverse (throws);
4869 for (current = throws; current; current = TREE_CHAIN (current))
4871 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4872 NULL_TREE, NULL_TREE);
4873 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4874 &TREE_VALUE (current);
4876 DECL_FUNCTION_THROWS (meth) = throws;
4879 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4880 DECL_FUNCTION_WFL (meth) = id;
4882 /* Set the flag if we correctly processed a constructor */
4883 if (constructor_ok)
4885 DECL_CONSTRUCTOR_P (meth) = 1;
4886 /* Compute and store the number of artificial parameters declared
4887 for this constructor */
4888 for (count = 0, current = TYPE_FIELDS (this_class); current;
4889 current = TREE_CHAIN (current))
4890 if (FIELD_LOCAL_ALIAS (current))
4891 count++;
4892 DECL_FUNCTION_NAP (meth) = count;
4895 /* Eventually set the @deprecated tag flag */
4896 CHECK_DEPRECATED (meth);
4898 /* If doing xref, store column and line number information instead
4899 of the line number only. */
4900 if (flag_emit_xref)
4902 #ifdef USE_MAPPED_LOCATION
4903 DECL_SOURCE_LOCATION (meth) = EXPR_LOCATION (id);
4904 #else
4905 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4906 #endif
4909 return meth;
4912 static void
4913 fix_method_argument_names (tree orig_arg, tree meth)
4915 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4916 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4918 TREE_PURPOSE (arg) = this_identifier_node;
4919 arg = TREE_CHAIN (arg);
4921 while (orig_arg != end_params_node)
4923 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4924 orig_arg = TREE_CHAIN (orig_arg);
4925 arg = TREE_CHAIN (arg);
4929 /* Complete the method declaration with METHOD_BODY. */
4931 static void
4932 finish_method_declaration (tree method_body)
4934 int flags;
4936 if (!current_function_decl)
4937 return;
4939 flags = get_access_flags_from_decl (current_function_decl);
4941 /* 8.4.5 Method Body */
4942 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4944 tree name = DECL_NAME (current_function_decl);
4945 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4946 "%s method %qs can't have a body defined",
4947 (METHOD_NATIVE (current_function_decl) ?
4948 "Native" : "Abstract"),
4949 IDENTIFIER_POINTER (name));
4950 method_body = NULL_TREE;
4952 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4954 tree name = DECL_NAME (current_function_decl);
4955 parse_error_context
4956 (DECL_FUNCTION_WFL (current_function_decl),
4957 "Non native and non abstract method %qs must have a body defined",
4958 IDENTIFIER_POINTER (name));
4959 method_body = NULL_TREE;
4962 if (flag_emit_class_files && method_body
4963 && TREE_CODE (method_body) == NOP_EXPR
4964 && TREE_TYPE (current_function_decl)
4965 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4966 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4968 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4969 maybe_absorb_scoping_blocks ();
4970 /* Exit function's body */
4971 exit_block ();
4972 /* Merge last line of the function with first line, directly in the
4973 function decl. It will be used to emit correct debug info. */
4974 if (!flag_emit_xref)
4975 DECL_FUNCTION_LAST_LINE (current_function_decl) = ctxp->last_ccb_indent1;
4977 /* Since function's argument's list are shared, reset the
4978 ARG_FINAL_P parameter that might have been set on some of this
4979 function parameters. */
4980 UNMARK_FINAL_PARMS (current_function_decl);
4982 /* So we don't have an irrelevant function declaration context for
4983 the next static block we'll see. */
4984 current_function_decl = NULL_TREE;
4987 /* Build a an error message for constructor circularity errors. */
4989 static char *
4990 constructor_circularity_msg (tree from, tree to)
4992 static char string [4096];
4993 char *t = xstrdup (lang_printable_name (from, 2));
4994 sprintf (string, "'%s' invokes '%s'", t, lang_printable_name (to, 2));
4995 free (t);
4996 return string;
4999 /* Verify a circular call to METH. Return 1 if an error is found, 0
5000 otherwise. */
5002 static GTY(()) tree vcc_list;
5003 static int
5004 verify_constructor_circularity (tree meth, tree current)
5006 tree c;
5008 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5010 if (TREE_VALUE (c) == meth)
5012 char *t;
5013 if (vcc_list)
5015 tree liste;
5016 vcc_list = nreverse (vcc_list);
5017 for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
5019 parse_error_context
5020 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
5021 constructor_circularity_msg
5022 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
5023 java_error_count--;
5026 t = xstrdup (lang_printable_name (meth, 2));
5027 parse_error_context (TREE_PURPOSE (c),
5028 "%s: recursive invocation of constructor %qs",
5029 constructor_circularity_msg (current, meth), t);
5030 free (t);
5031 vcc_list = NULL_TREE;
5032 return 1;
5035 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5037 vcc_list = tree_cons (c, current, vcc_list);
5038 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
5039 return 1;
5040 vcc_list = TREE_CHAIN (vcc_list);
5042 return 0;
5045 /* Check modifiers that can be declared but exclusively */
5047 static void
5048 check_modifiers_consistency (int flags)
5050 int acc_count = 0;
5051 tree cl = NULL_TREE;
5053 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
5054 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
5055 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
5056 if (acc_count > 1)
5057 parse_error_context
5058 (cl, "Inconsistent member declaration. At most one of %<public%>, %<private%>, or %<protected%> may be specified");
5060 acc_count = 0;
5061 cl = NULL_TREE;
5062 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
5063 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
5064 if (acc_count > 1)
5065 parse_error_context (cl,
5066 "Inconsistent member declaration. At most one of %<final%> or %<volatile%> may be specified");
5069 /* Check the methode header METH for abstract specifics features */
5071 static void
5072 check_abstract_method_header (tree meth)
5074 int flags = get_access_flags_from_decl (meth);
5076 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
5077 ACC_ABSTRACT, "abstract method",
5078 IDENTIFIER_POINTER (DECL_NAME (meth)));
5079 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
5080 ACC_PUBLIC, "abstract method",
5081 IDENTIFIER_POINTER (DECL_NAME (meth)));
5083 check_modifiers ("Illegal modifier %qs for interface method",
5084 flags, INTERFACE_METHOD_MODIFIERS);
5087 /* Create a FUNCTION_TYPE node and start augmenting it with the
5088 declared function arguments. Arguments type that can't be resolved
5089 are left as they are, but the returned node is marked as containing
5090 incomplete types. */
5092 static tree
5093 method_declarator (tree id, tree list)
5095 tree arg_types = NULL_TREE, current, node;
5096 tree meth = make_node (FUNCTION_TYPE);
5097 jdep *jdep;
5099 patch_stage = JDEP_NO_PATCH;
5101 if (GET_CPC () == error_mark_node)
5102 return error_mark_node;
5104 /* If we're dealing with an inner class constructor, we hide the
5105 this$<n> decl in the name field of its parameter declaration. We
5106 also might have to hide the outer context local alias
5107 initializers. Not done when the class is a toplevel class. */
5108 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
5109 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
5111 tree aliases_list, type, thisn;
5112 /* First the aliases, linked to the regular parameters */
5113 aliases_list =
5114 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
5115 TREE_TYPE (GET_CPC ()),
5116 NULL_TREE, NULL);
5117 list = chainon (nreverse (aliases_list), list);
5119 /* Then this$<n> */
5120 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
5121 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
5122 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
5123 list);
5126 for (current = list; current; current = TREE_CHAIN (current))
5128 int must_chain = 0;
5129 tree wfl_name = TREE_PURPOSE (current);
5130 tree type = TREE_VALUE (current);
5131 tree name = EXPR_WFL_NODE (wfl_name);
5132 tree already, arg_node;
5133 tree type_wfl = NULL_TREE;
5134 tree real_type;
5136 /* Obtain a suitable type for resolution, if necessary */
5137 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5139 /* Process NAME, as it may specify extra dimension(s) for it */
5140 type = build_array_from_name (type, type_wfl, name, &name);
5141 EXPR_WFL_NODE (wfl_name) = name;
5143 real_type = GET_REAL_TYPE (type);
5144 if (TREE_CODE (real_type) == RECORD_TYPE)
5146 real_type = promote_type (real_type);
5147 if (TREE_CODE (type) == TREE_LIST)
5148 TREE_PURPOSE (type) = real_type;
5151 /* Check redefinition */
5152 for (already = arg_types; already; already = TREE_CHAIN (already))
5153 if (TREE_PURPOSE (already) == name)
5155 parse_error_context
5156 (wfl_name, "Variable %qs is used more than once in the argument list of method %qs",
5157 IDENTIFIER_POINTER (name),
5158 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5159 break;
5162 /* If we've an incomplete argument type, we know there is a location
5163 to patch when the type get resolved, later. */
5164 jdep = NULL;
5165 if (must_chain)
5167 patch_stage = JDEP_METHOD;
5168 type = register_incomplete_type (patch_stage,
5169 type_wfl, wfl_name, type);
5170 jdep = CLASSD_LAST (ctxp->classd_list);
5171 JDEP_MISC (jdep) = id;
5174 /* The argument node: a name and a (possibly) incomplete type. */
5175 arg_node = build_tree_list (name, real_type);
5176 /* Remember arguments declared final. */
5177 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5179 if (jdep)
5180 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5181 TREE_CHAIN (arg_node) = arg_types;
5182 arg_types = arg_node;
5184 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5185 node = build_tree_list (id, meth);
5186 return node;
5189 static int
5190 unresolved_type_p (tree wfl, tree *returned)
5192 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5194 if (returned)
5196 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5197 if (decl && current_class && (decl == TYPE_NAME (current_class)))
5198 *returned = TREE_TYPE (decl);
5199 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5200 *returned = TREE_TYPE (GET_CPC ());
5201 else
5202 *returned = NULL_TREE;
5204 return 1;
5206 if (returned)
5207 *returned = wfl;
5208 return 0;
5211 /* From NAME, build a qualified identifier node using the
5212 qualification from the current package definition. */
5214 static tree
5215 parser_qualified_classname (tree name)
5217 tree nested_class_name;
5219 if ((nested_class_name = maybe_make_nested_class_name (name)))
5220 return nested_class_name;
5222 if (ctxp->package)
5223 return merge_qualified_name (ctxp->package, name);
5224 else
5225 return name;
5228 /* Called once the type a interface extends is resolved. Returns 0 if
5229 everything is OK. */
5231 static int
5232 parser_check_super_interface (tree super_decl, tree this_decl, tree this_wfl)
5234 tree super_type = TREE_TYPE (super_decl);
5236 /* Has to be an interface */
5237 if (!CLASS_INTERFACE (super_decl))
5239 parse_error_context
5240 (this_wfl, "%s %qs can't implement/extend %s %qs",
5241 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5242 "Interface" : "Class"),
5243 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5244 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5245 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5246 return 1;
5249 /* Check top-level interface access. Inner classes are subject to member
5250 access rules (6.6.1). */
5251 if (! INNER_CLASS_P (super_type)
5252 && check_pkg_class_access (DECL_NAME (super_decl),
5253 NULL_TREE, true, this_decl))
5254 return 1;
5256 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5257 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5258 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5259 return 0;
5262 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5263 0 if everything is OK. */
5265 static int
5266 parser_check_super (tree super_decl, tree this_decl, tree wfl)
5268 tree super_type = TREE_TYPE (super_decl);
5270 /* SUPER should be a CLASS (neither an array nor an interface) */
5271 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5273 parse_error_context
5274 (wfl, "Class %qs can't subclass %s %qs",
5275 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5276 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5277 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5278 return 1;
5281 if (CLASS_FINAL (TYPE_NAME (super_type)))
5283 parse_error_context (wfl, "Can't subclass final classes: %s",
5284 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5285 return 1;
5288 /* Check top-level class scope. Inner classes are subject to member access
5289 rules (6.6.1). */
5290 if (! INNER_CLASS_P (super_type)
5291 && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true, NULL_TREE)))
5292 return 1;
5294 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5295 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5296 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5297 return 0;
5300 /* Create a new dependency list and link it (in a LIFO manner) to the
5301 CTXP list of type dependency list. */
5303 static void
5304 create_jdep_list (struct parser_ctxt *ctxp)
5306 jdeplist *new = xmalloc (sizeof (jdeplist));
5307 new->first = new->last = NULL;
5308 new->next = ctxp->classd_list;
5309 ctxp->classd_list = new;
5312 static jdeplist *
5313 reverse_jdep_list (struct parser_ctxt *ctxp)
5315 jdeplist *prev = NULL, *current, *next;
5316 for (current = ctxp->classd_list; current; current = next)
5318 next = current->next;
5319 current->next = prev;
5320 prev = current;
5322 return prev;
5325 /* Create a fake pointer based on the ID stored in
5326 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5327 registered again. */
5329 static tree
5330 obtain_incomplete_type (tree type_name)
5332 tree ptr = NULL_TREE, name;
5334 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5335 name = EXPR_WFL_NODE (type_name);
5336 else if (INCOMPLETE_TYPE_P (type_name))
5337 name = TYPE_NAME (type_name);
5338 else
5339 abort ();
5341 /* Workaround from build_pointer_type for incomplete types. */
5342 BUILD_PTR_FROM_NAME (ptr, name);
5343 TYPE_MODE (ptr) = ptr_mode;
5344 layout_type (ptr);
5346 return ptr;
5349 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5350 non NULL instead of computing a new fake type based on WFL. The new
5351 dependency is inserted in the current type dependency list, in FIFO
5352 manner. */
5354 static tree
5355 register_incomplete_type (int kind, tree wfl, tree decl, tree ptr)
5357 jdep *new = xmalloc (sizeof (jdep));
5359 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5360 ptr = obtain_incomplete_type (wfl);
5362 JDEP_KIND (new) = kind;
5363 JDEP_DECL (new) = decl;
5364 JDEP_TO_RESOLVE (new) = ptr;
5365 JDEP_WFL (new) = wfl;
5366 JDEP_CHAIN (new) = NULL;
5367 JDEP_MISC (new) = NULL_TREE;
5368 /* For some dependencies, set the enclosing class of the current
5369 class to be the enclosing context */
5370 if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS || kind == JDEP_SUPER)
5371 && GET_ENCLOSING_CPC ())
5372 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5373 else
5374 JDEP_ENCLOSING (new) = GET_CPC ();
5375 JDEP_GET_PATCH (new) = (tree *)NULL;
5377 JDEP_INSERT (ctxp->classd_list, new);
5379 return ptr;
5382 /* This checks for circular references with innerclasses. We start
5383 from SOURCE and should never reach TARGET. Extended/implemented
5384 types in SOURCE have their enclosing context checked not to reach
5385 TARGET. When the last enclosing context of SOURCE is reached, its
5386 extended/implemented types are also checked not to reach TARGET.
5387 In case of error, WFL of the offending type is returned; NULL_TREE
5388 otherwise. */
5390 static tree
5391 check_inner_circular_reference (tree source, tree target)
5393 tree base_binfo;
5394 tree ctx, cl;
5395 int i;
5397 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (source), i, base_binfo); i++)
5399 tree su;
5401 /* We can end up with a NULL_TREE or an incomplete type here if
5402 we encountered previous type resolution errors. It's safe to
5403 simply ignore these cases. */
5404 su = BINFO_TYPE (base_binfo);
5405 if (INCOMPLETE_TYPE_P (su))
5406 continue;
5408 if (inherits_from_p (su, target))
5409 return lookup_cl (TYPE_NAME (su));
5411 for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5413 /* An enclosing context shouldn't be TARGET */
5414 if (ctx == TYPE_NAME (target))
5415 return lookup_cl (TYPE_NAME (su));
5417 /* When we reach the enclosing last context, start a check
5418 on it, with the same target */
5419 if (! DECL_CONTEXT (ctx) &&
5420 (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5421 return cl;
5424 return NULL_TREE;
5427 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5428 offending type if a circularity is detected. NULL_TREE is returned
5429 otherwise. TYPE can be an interface or a class. */
5431 static tree
5432 check_circular_reference (tree type)
5434 tree base_binfo;
5435 int i;
5437 if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
5438 return NULL_TREE;
5440 if (! CLASS_INTERFACE (TYPE_NAME (type)))
5442 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5443 return lookup_cl (TYPE_NAME (type));
5444 return NULL_TREE;
5447 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo); i++)
5449 if (BINFO_TYPE (base_binfo) != object_type_node
5450 && interface_of_p (type, BINFO_TYPE (base_binfo)))
5451 return lookup_cl (TYPE_NAME (BINFO_TYPE (base_binfo)));
5453 return NULL_TREE;
5456 void
5457 java_check_circular_reference (void)
5459 tree current;
5460 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5462 tree type = TREE_TYPE (current);
5463 tree cl;
5465 cl = check_circular_reference (type);
5466 if (! cl)
5467 cl = check_inner_circular_reference (type, type);
5468 if (cl)
5469 parse_error_context (cl, "Cyclic class inheritance%s",
5470 (cyclic_inheritance_report ?
5471 cyclic_inheritance_report : ""));
5475 /* Augment the parameter list PARM with parameters crafted to
5476 initialize outer context locals aliases. Through ARTIFICIAL, a
5477 count is kept of the number of crafted parameters. MODE governs
5478 what eventually gets created: something suitable for a function
5479 creation or a function invocation, either the constructor or
5480 finit$. */
5482 static tree
5483 build_alias_initializer_parameter_list (int mode, tree class_type, tree parm,
5484 int *artificial)
5486 tree field;
5487 tree additional_parms = NULL_TREE;
5489 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5490 if (FIELD_LOCAL_ALIAS (field))
5492 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5493 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5494 tree mangled_id;
5496 switch (mode)
5498 case AIPL_FUNCTION_DECLARATION:
5499 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5500 &buffer [4]);
5501 purpose = build_wfl_node (mangled_id);
5502 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5503 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5504 else
5505 value = TREE_TYPE (field);
5506 break;
5508 case AIPL_FUNCTION_CREATION:
5509 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5510 &buffer [4]);
5511 value = TREE_TYPE (field);
5512 break;
5514 case AIPL_FUNCTION_FINIT_INVOCATION:
5515 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5516 &buffer [4]);
5517 /* Now, this is wrong. purpose should always be the NAME
5518 of something and value its matching value (decl, type,
5519 etc...) FIXME -- but there is a lot to fix. */
5521 /* When invoked for this kind of operation, we already
5522 know whether a field is used or not. */
5523 purpose = TREE_TYPE (field);
5524 value = build_wfl_node (mangled_id);
5525 break;
5527 case AIPL_FUNCTION_CTOR_INVOCATION:
5528 /* There are two case: the constructor invocation happens
5529 outside the local inner, in which case, locales from the outer
5530 context are directly used.
5532 Otherwise, we fold to using the alias directly. */
5533 if (class_type == current_class)
5534 value = field;
5535 else
5537 name = get_identifier (&buffer[4]);
5538 value = IDENTIFIER_LOCAL_VALUE (name);
5540 break;
5542 additional_parms = tree_cons (purpose, value, additional_parms);
5543 if (artificial)
5544 *artificial +=1;
5546 if (additional_parms)
5548 if (ANONYMOUS_CLASS_P (class_type)
5549 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5550 additional_parms = nreverse (additional_parms);
5551 parm = chainon (additional_parms, parm);
5554 return parm;
5557 /* Craft a constructor for CLASS_DECL -- what we should do when none
5558 where found. ARGS is non NULL when a special signature must be
5559 enforced. This is the case for anonymous classes. */
5561 static tree
5562 craft_constructor (tree class_decl, tree args)
5564 tree class_type = TREE_TYPE (class_decl);
5565 tree parm = NULL_TREE;
5566 /* Inherit access flags for the constructor from its enclosing class. */
5567 int valid_ctor_flags = ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE;
5568 int flags = (get_access_flags_from_decl (class_decl) & valid_ctor_flags);
5569 int i = 0, artificial = 0;
5570 tree decl, ctor_name;
5571 char buffer [80];
5573 ctor_name = init_identifier_node;
5575 /* If we're dealing with an inner class constructor, we hide the
5576 this$<n> decl in the name field of its parameter declaration. */
5577 if (PURE_INNER_CLASS_TYPE_P (class_type))
5579 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5580 parm = tree_cons (build_current_thisn (class_type),
5581 build_pointer_type (type), parm);
5583 /* Some more arguments to be hidden here. The values of the local
5584 variables of the outer context that the inner class needs to see. */
5585 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5586 class_type, parm,
5587 &artificial);
5590 /* Then if there are any args to be enforced, enforce them now */
5591 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5593 /* If we see a `void *', we need to change it to Object. */
5594 if (TREE_VALUE (args) == TREE_TYPE (null_pointer_node))
5595 TREE_VALUE (args) = object_ptr_type_node;
5597 sprintf (buffer, "parm%d", i++);
5598 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5601 CRAFTED_PARAM_LIST_FIXUP (parm);
5602 decl = create_artificial_method (class_type, flags, void_type_node,
5603 ctor_name, parm);
5604 fix_method_argument_names (parm, decl);
5605 /* Now, mark the artificial parameters. */
5606 DECL_FUNCTION_NAP (decl) = artificial;
5607 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5608 DECL_INLINE (decl) = 1;
5609 return decl;
5613 /* Fix the constructors. This will be called right after circular
5614 references have been checked. It is necessary to fix constructors
5615 early even if no code generation will take place for that class:
5616 some generated constructor might be required by the class whose
5617 compilation triggered this one to be simply loaded. */
5619 void
5620 java_fix_constructors (void)
5622 tree current;
5624 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5626 tree class_type = TREE_TYPE (current);
5627 int saw_ctor = 0;
5628 tree decl;
5630 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5631 continue;
5633 output_class = current_class = class_type;
5634 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5636 if (DECL_CONSTRUCTOR_P (decl))
5638 fix_constructors (decl);
5639 saw_ctor = 1;
5643 /* Anonymous class constructor can't be generated that early. */
5644 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5645 craft_constructor (current, NULL_TREE);
5649 /* safe_layout_class just makes sure that we can load a class without
5650 disrupting the current_class, input_file, input_line, etc, information
5651 about the class processed currently. */
5653 void
5654 safe_layout_class (tree class)
5656 tree save_current_class = current_class;
5657 location_t save_location = input_location;
5659 layout_class (class);
5661 current_class = save_current_class;
5662 input_location = save_location;
5665 static tree
5666 jdep_resolve_class (jdep *dep)
5668 tree decl;
5670 if (JDEP_RESOLVED_P (dep))
5671 decl = JDEP_RESOLVED_DECL (dep);
5672 else
5674 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5675 JDEP_DECL (dep), JDEP_WFL (dep));
5676 JDEP_RESOLVED (dep, decl);
5677 /* If there is no WFL, that's ok. We generate this warning
5678 elsewhere. */
5679 if (decl && JDEP_WFL (dep) != NULL_TREE)
5680 check_deprecation (JDEP_WFL (dep), decl);
5683 if (!decl)
5684 complete_class_report_errors (dep);
5685 else if (INNER_CLASS_DECL_P (decl))
5687 tree inner = TREE_TYPE (decl);
5688 if (! CLASS_LOADED_P (inner))
5690 safe_layout_class (inner);
5691 if (TYPE_SIZE (inner) == error_mark_node)
5692 TYPE_SIZE (inner) = NULL_TREE;
5694 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5696 return decl;
5699 /* Complete unsatisfied class declaration and their dependencies */
5701 void
5702 java_complete_class (void)
5704 tree cclass;
5705 jdeplist *cclassd;
5706 int error_found;
5707 tree type;
5709 /* Process imports */
5710 process_imports ();
5712 /* Reverse things so we have the right order */
5713 ctxp->class_list = nreverse (ctxp->class_list);
5714 ctxp->classd_list = reverse_jdep_list (ctxp);
5716 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5717 cclass && cclassd;
5718 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5720 jdep *dep;
5722 /* We keep the compilation unit imports in the class so that
5723 they can be used later to resolve type dependencies that
5724 aren't necessary to solve now. */
5725 TYPE_IMPORT_LIST (TREE_TYPE (cclass)) = ctxp->import_list;
5726 TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (cclass)) = ctxp->import_demand_list;
5728 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5730 tree decl;
5731 if (!(decl = jdep_resolve_class (dep)))
5732 continue;
5734 /* Now it's time to patch */
5735 switch (JDEP_KIND (dep))
5737 case JDEP_SUPER:
5738 /* Simply patch super */
5739 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5740 continue;
5741 BINFO_TYPE (BINFO_BASE_BINFO
5742 (TYPE_BINFO (TREE_TYPE (JDEP_DECL (dep))), 0))
5743 = TREE_TYPE (decl);
5744 break;
5746 case JDEP_FIELD:
5748 /* We do part of the job done in add_field */
5749 tree field_decl = JDEP_DECL (dep);
5750 tree field_type = TREE_TYPE (decl);
5751 if (TREE_CODE (field_type) == RECORD_TYPE)
5752 field_type = promote_type (field_type);
5753 TREE_TYPE (field_decl) = field_type;
5754 DECL_ALIGN (field_decl) = 0;
5755 DECL_USER_ALIGN (field_decl) = 0;
5756 layout_decl (field_decl, 0);
5757 SOURCE_FRONTEND_DEBUG
5758 (("Completed field/var decl '%s' with '%s'",
5759 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5760 IDENTIFIER_POINTER (DECL_NAME (decl))));
5761 break;
5763 case JDEP_METHOD: /* We start patching a method */
5764 case JDEP_METHOD_RETURN:
5765 error_found = 0;
5766 while (1)
5768 if (decl)
5770 type = TREE_TYPE(decl);
5771 if (TREE_CODE (type) == RECORD_TYPE)
5772 type = promote_type (type);
5773 JDEP_APPLY_PATCH (dep, type);
5774 SOURCE_FRONTEND_DEBUG
5775 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5776 "Completing fct '%s' with ret type '%s'":
5777 "Completing arg '%s' with type '%s'"),
5778 IDENTIFIER_POINTER (EXPR_WFL_NODE
5779 (JDEP_DECL_WFL (dep))),
5780 IDENTIFIER_POINTER (DECL_NAME (decl))));
5782 else
5783 error_found = 1;
5784 dep = JDEP_CHAIN (dep);
5785 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5786 break;
5787 else
5788 decl = jdep_resolve_class (dep);
5790 if (!error_found)
5792 tree mdecl = JDEP_DECL (dep), signature;
5793 /* Recompute and reset the signature, check first that
5794 all types are now defined. If they're not,
5795 don't build the signature. */
5796 if (check_method_types_complete (mdecl))
5798 signature = build_java_signature (TREE_TYPE (mdecl));
5799 set_java_signature (TREE_TYPE (mdecl), signature);
5802 else
5803 continue;
5804 break;
5806 case JDEP_INTERFACE:
5807 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5808 JDEP_WFL (dep)))
5809 continue;
5810 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5811 break;
5813 case JDEP_PARM:
5814 case JDEP_VARIABLE:
5815 type = TREE_TYPE(decl);
5816 if (TREE_CODE (type) == RECORD_TYPE)
5817 type = promote_type (type);
5818 JDEP_APPLY_PATCH (dep, type);
5819 break;
5821 case JDEP_TYPE:
5822 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5823 SOURCE_FRONTEND_DEBUG
5824 (("Completing a random type dependency on a '%s' node",
5825 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5826 break;
5828 case JDEP_EXCEPTION:
5829 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5830 SOURCE_FRONTEND_DEBUG
5831 (("Completing '%s' 'throws' argument node",
5832 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5833 break;
5835 case JDEP_ANONYMOUS:
5836 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5837 break;
5839 default:
5840 abort ();
5844 return;
5847 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5848 array. */
5850 static tree
5851 resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5853 tree tname = TYPE_NAME (class_type);
5854 tree resolved_type = TREE_TYPE (class_type);
5855 int array_dims = 0;
5856 tree resolved_type_decl;
5858 if (resolved_type != NULL_TREE)
5860 tree resolved_type_decl = TYPE_NAME (resolved_type);
5861 if (resolved_type_decl == NULL_TREE
5862 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5864 resolved_type_decl = build_decl (TYPE_DECL,
5865 TYPE_NAME (class_type),
5866 resolved_type);
5868 return resolved_type_decl;
5871 /* 1- Check to see if we have an array. If true, find what we really
5872 want to resolve */
5873 if ((array_dims = build_type_name_from_array_name (tname,
5874 &TYPE_NAME (class_type))))
5875 WFL_STRIP_BRACKET (cl, cl);
5877 /* 2- Resolve the bare type */
5878 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5879 decl, cl)))
5880 return NULL_TREE;
5881 resolved_type = TREE_TYPE (resolved_type_decl);
5883 /* 3- If we have an array, reconstruct the array down to its nesting */
5884 if (array_dims)
5886 for (; array_dims; array_dims--)
5887 resolved_type = build_java_array_type (resolved_type, -1);
5888 resolved_type_decl = TYPE_NAME (resolved_type);
5890 TREE_TYPE (class_type) = resolved_type;
5891 return resolved_type_decl;
5894 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5895 are used to report error messages; CL must either be NULL_TREE or a
5896 WFL wrapping a class. Do not try to replace TYPE_NAME (class_type)
5897 by a variable, since it is changed by find_in_imports{_on_demand}
5898 and (but it doesn't really matter) qualify_and_find. */
5900 tree
5901 do_resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5903 tree new_class_decl = NULL_TREE, super = NULL_TREE;
5904 tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5905 tree decl_result;
5906 htab_t circularity_hash;
5908 if (QUALIFIED_P (TYPE_NAME (class_type)))
5910 /* If the type name is of the form `Q . Id', then Q is either a
5911 package name or a class name. First we try to find Q as a
5912 class and then treat Id as a member type. If we can't find Q
5913 as a class then we fall through. */
5914 tree q, left, left_type, right;
5915 if (split_qualified_name (&left, &right, TYPE_NAME (class_type)) == 0)
5917 BUILD_PTR_FROM_NAME (left_type, left);
5918 q = do_resolve_class (enclosing, left_type, decl, cl);
5919 if (q)
5921 enclosing = q;
5922 saved_enclosing_type = TREE_TYPE (q);
5923 BUILD_PTR_FROM_NAME (class_type, right);
5928 if (enclosing)
5930 /* This hash table is used to register the classes we're going
5931 through when searching the current class as an inner class, in
5932 order to detect circular references. Remember to free it before
5933 returning the section 0- of this function. */
5934 circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
5935 NULL);
5937 /* 0- Search in the current class as an inner class.
5938 Maybe some code here should be added to load the class or
5939 something, at least if the class isn't an inner class and ended
5940 being loaded from class file. FIXME. */
5941 while (enclosing)
5943 new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5944 &super, class_type);
5945 if (new_class_decl)
5946 break;
5948 /* If we haven't found anything because SUPER reached Object and
5949 ENCLOSING happens to be an innerclass, try the enclosing context. */
5950 if ((!super || super == object_type_node) &&
5951 enclosing && INNER_CLASS_DECL_P (enclosing))
5952 enclosing = DECL_CONTEXT (enclosing);
5953 else
5954 enclosing = NULL_TREE;
5957 htab_delete (circularity_hash);
5959 if (new_class_decl)
5960 return new_class_decl;
5963 /* 1- Check for the type in single imports. This will change
5964 TYPE_NAME() if something relevant is found */
5965 find_in_imports (saved_enclosing_type, class_type);
5967 /* 2- And check for the type in the current compilation unit */
5968 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5970 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
5971 load_class (TYPE_NAME (class_type), 0);
5972 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5975 /* 3- Search according to the current package definition */
5976 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5978 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5979 TYPE_NAME (class_type))))
5980 return new_class_decl;
5983 /* 4- Check the import on demands. Don't allow bar.baz to be
5984 imported from foo.* */
5985 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5986 if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5987 return NULL_TREE;
5989 /* If found in find_in_imports_on_demand, the type has already been
5990 loaded. */
5991 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5992 return new_class_decl;
5994 /* 5- Try with a name qualified with the package name we've seen so far */
5995 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5997 tree package;
5999 /* If there is a current package (ctxp->package), it's the first
6000 element of package_list and we can skip it. */
6001 for (package = (ctxp->package ?
6002 TREE_CHAIN (package_list) : package_list);
6003 package; package = TREE_CHAIN (package))
6004 if ((new_class_decl = qualify_and_find (class_type,
6005 TREE_PURPOSE (package),
6006 TYPE_NAME (class_type))))
6007 return new_class_decl;
6010 /* 5- Check another compilation unit that bears the name of type */
6011 load_class (TYPE_NAME (class_type), 0);
6013 if (!cl)
6014 cl = lookup_cl (decl);
6016 /* If we don't have a value for CL, then we're being called recursively.
6017 We can't check package access just yet, but it will be taken care of
6018 by the caller. */
6019 if (cl)
6021 if (check_pkg_class_access (TYPE_NAME (class_type), cl, true, NULL_TREE))
6022 return NULL_TREE;
6025 /* 6- Last call for a resolution */
6026 decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6028 /* The final lookup might have registered a.b.c into a.b$c If we
6029 failed at the first lookup, progressively change the name if
6030 applicable and use the matching DECL instead. */
6031 if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
6033 char *separator;
6034 tree name = TYPE_NAME (class_type);
6035 char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
6037 strcpy (namebuffer, IDENTIFIER_POINTER (name));
6039 do {
6041 /* Reach the last '.', and if applicable, replace it by a `$' and
6042 see if this exists as a type. */
6043 if ((separator = strrchr (namebuffer, '.')))
6045 *separator = '$';
6046 name = get_identifier (namebuffer);
6047 decl_result = IDENTIFIER_CLASS_VALUE (name);
6049 } while (!decl_result && separator);
6051 return decl_result;
6054 static tree
6055 qualify_and_find (tree class_type, tree package, tree name)
6057 tree new_qualified = merge_qualified_name (package, name);
6058 tree new_class_decl;
6060 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
6061 load_class (new_qualified, 0);
6062 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
6064 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
6065 load_class (TREE_TYPE (new_class_decl), 0);
6066 TYPE_NAME (class_type) = new_qualified;
6067 return IDENTIFIER_CLASS_VALUE (new_qualified);
6069 return NULL_TREE;
6072 /* Resolve NAME and lay it out (if not done and if not the current
6073 parsed class). Return a decl node. This function is meant to be
6074 called when type resolution is necessary during the walk pass. */
6076 static tree
6077 resolve_and_layout (tree something, tree cl)
6079 tree decl, decl_type;
6081 /* Don't do that on the current class */
6082 if (something == current_class)
6083 return TYPE_NAME (current_class);
6085 /* Don't do anything for void and other primitive types */
6086 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6087 return NULL_TREE;
6089 /* Pointer types can be reall pointer types or fake pointers. When
6090 finding a real pointer, recheck for primitive types */
6091 if (TREE_CODE (something) == POINTER_TYPE)
6093 if (TREE_TYPE (something))
6095 something = TREE_TYPE (something);
6096 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6097 return NULL_TREE;
6099 else
6100 something = TYPE_NAME (something);
6103 /* Don't do anything for arrays of primitive types */
6104 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
6105 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
6106 return NULL_TREE;
6108 /* Something might be a WFL */
6109 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
6110 something = EXPR_WFL_NODE (something);
6112 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
6113 TYPE_DECL or a real TYPE */
6114 else if (TREE_CODE (something) != IDENTIFIER_NODE)
6115 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
6116 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
6118 if (!(decl = resolve_no_layout (something, cl)))
6119 return NULL_TREE;
6121 /* Resolve and layout if necessary */
6122 decl_type = TREE_TYPE (decl);
6123 layout_class_methods (decl_type);
6124 /* Check methods */
6125 if (CLASS_FROM_SOURCE_P (decl_type))
6126 java_check_methods (decl);
6127 /* Layout the type if necessary */
6128 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6129 safe_layout_class (decl_type);
6131 return decl;
6134 /* Resolve a class, returns its decl but doesn't perform any
6135 layout. The current parsing context is saved and restored */
6137 static tree
6138 resolve_no_layout (tree name, tree cl)
6140 tree ptr, decl;
6141 BUILD_PTR_FROM_NAME (ptr, name);
6142 java_parser_context_save_global ();
6143 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6144 java_parser_context_restore_global ();
6146 return decl;
6149 /* Called when reporting errors. Skip the '[]'s in a complex array
6150 type description that failed to be resolved. purify_type_name can't
6151 use an identifier tree. */
6153 static const char *
6154 purify_type_name (const char *name)
6156 int len = strlen (name);
6157 int bracket_found;
6159 STRING_STRIP_BRACKETS (name, len, bracket_found);
6160 if (bracket_found)
6162 char *stripped_name = xmemdup (name, len, len+1);
6163 stripped_name [len] = '\0';
6164 return stripped_name;
6166 return name;
6169 /* The type CURRENT refers to can't be found. We print error messages. */
6171 static void
6172 complete_class_report_errors (jdep *dep)
6174 const char *name;
6176 if (!JDEP_WFL (dep))
6177 return;
6179 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6180 switch (JDEP_KIND (dep))
6182 case JDEP_SUPER:
6183 parse_error_context
6184 (JDEP_WFL (dep), "Superclass %qs of class %qs not found",
6185 purify_type_name (name),
6186 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6187 break;
6188 case JDEP_FIELD:
6189 parse_error_context
6190 (JDEP_WFL (dep), "Type %qs not found in declaration of field %qs",
6191 purify_type_name (name),
6192 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6193 break;
6194 case JDEP_METHOD: /* Covers arguments */
6195 parse_error_context
6196 (JDEP_WFL (dep), "Type %qs not found in the declaration of the argument %qs of method %qs",
6197 purify_type_name (name),
6198 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6199 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6200 break;
6201 case JDEP_METHOD_RETURN: /* Covers return type */
6202 parse_error_context
6203 (JDEP_WFL (dep), "Type %qs not found in the declaration of the return type of method %qs",
6204 purify_type_name (name),
6205 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6206 break;
6207 case JDEP_INTERFACE:
6208 parse_error_context
6209 (JDEP_WFL (dep), "Superinterface %qs of %s %qs not found",
6210 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6211 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6212 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6213 break;
6214 case JDEP_VARIABLE:
6215 parse_error_context
6216 (JDEP_WFL (dep), "Type %qs not found in the declaration of the local variable %qs",
6217 purify_type_name (IDENTIFIER_POINTER
6218 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6219 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6220 break;
6221 case JDEP_EXCEPTION: /* As specified by `throws' */
6222 parse_error_context
6223 (JDEP_WFL (dep), "Class %qs not found in %<throws%>",
6224 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6225 break;
6226 default:
6227 /* Fix for -Wall. Just break doing nothing. The error will be
6228 caught later */
6229 break;
6233 /* Return a static string containing the DECL prototype string. If
6234 DECL is a constructor, use the class name instead of the form
6235 <init> */
6237 static const char *
6238 get_printable_method_name (tree decl)
6240 const char *to_return;
6241 tree name = NULL_TREE;
6243 if (DECL_CONSTRUCTOR_P (decl))
6245 name = DECL_NAME (decl);
6246 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6249 to_return = lang_printable_name (decl, 2);
6250 if (DECL_CONSTRUCTOR_P (decl))
6251 DECL_NAME (decl) = name;
6253 return to_return;
6256 /* Track method being redefined inside the same class. As a side
6257 effect, set DECL_NAME to an IDENTIFIER (prior entering this
6258 function it's a FWL, so we can track errors more accurately.) */
6260 static int
6261 check_method_redefinition (tree class, tree method)
6263 tree redef, sig;
6265 /* There's no need to verify <clinit> and finit$ and instinit$ */
6266 if (DECL_CLINIT_P (method)
6267 || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6268 return 0;
6270 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6271 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6273 if (redef == method)
6274 break;
6275 if (DECL_NAME (redef) == DECL_NAME (method)
6276 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6277 && !DECL_ARTIFICIAL (method))
6279 parse_error_context
6280 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration %qs",
6281 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6282 get_printable_method_name (redef));
6283 return 1;
6286 return 0;
6289 /* Return 1 if check went ok, 0 otherwise. */
6290 static int
6291 check_abstract_method_definitions (int do_interface, tree class_decl,
6292 tree type)
6294 tree class = TREE_TYPE (class_decl);
6295 tree method, end_type;
6296 int ok = 1;
6298 end_type = (do_interface ? object_type_node : type);
6299 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6301 tree other_super, other_method, method_sig, method_name;
6302 int found = 0;
6303 int end_type_reached = 0;
6305 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6306 continue;
6308 /* Now verify that somewhere in between TYPE and CLASS,
6309 abstract method METHOD gets a non abstract definition
6310 that is inherited by CLASS. */
6312 method_sig = build_java_signature (TREE_TYPE (method));
6313 method_name = DECL_NAME (method);
6314 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6315 method_name = EXPR_WFL_NODE (method_name);
6317 other_super = class;
6318 do {
6319 if (other_super == end_type)
6320 end_type_reached = 1;
6322 /* Method search */
6323 for (other_method = TYPE_METHODS (other_super); other_method;
6324 other_method = TREE_CHAIN (other_method))
6326 tree s = build_java_signature (TREE_TYPE (other_method));
6327 tree other_name = DECL_NAME (other_method);
6329 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6330 other_name = EXPR_WFL_NODE (other_name);
6331 if (!DECL_CLINIT_P (other_method)
6332 && !DECL_CONSTRUCTOR_P (other_method)
6333 && method_name == other_name
6334 && method_sig == s
6335 && !METHOD_ABSTRACT (other_method))
6337 found = 1;
6338 break;
6341 other_super = CLASSTYPE_SUPER (other_super);
6342 } while (!end_type_reached);
6344 /* Report that abstract METHOD didn't find an implementation
6345 that CLASS can use. */
6346 if (!found)
6348 char *t = xstrdup (lang_printable_name
6349 (TREE_TYPE (TREE_TYPE (method)), 0));
6350 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6352 parse_error_context
6353 (lookup_cl (class_decl),
6354 "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",
6355 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6356 t, lang_printable_name (method, 2),
6357 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6358 "interface" : "class"),
6359 IDENTIFIER_POINTER (ccn),
6360 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6361 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6362 ok = 0;
6363 free (t);
6367 if (ok && do_interface)
6369 /* Check for implemented interfaces. */
6370 int i;
6371 tree base_binfo;
6373 for (i = 1;
6374 ok && BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo);
6375 i++)
6376 ok = check_abstract_method_definitions (1, class_decl,
6377 BINFO_TYPE (base_binfo));
6380 return ok;
6383 /* Check that CLASS_DECL somehow implements all inherited abstract
6384 methods. */
6386 static void
6387 java_check_abstract_method_definitions (tree class_decl)
6389 tree class = TREE_TYPE (class_decl);
6390 tree super, base_binfo;
6391 int i;
6393 if (CLASS_ABSTRACT (class_decl))
6394 return;
6396 /* Check for inherited types */
6397 super = class;
6398 do {
6399 super = CLASSTYPE_SUPER (super);
6400 check_abstract_method_definitions (0, class_decl, super);
6401 } while (super != object_type_node);
6403 /* Check for implemented interfaces. */
6404 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6405 check_abstract_method_definitions (1, class_decl, BINFO_TYPE (base_binfo));
6408 /* Check all the types method DECL uses and return 1 if all of them
6409 are now complete, 0 otherwise. This is used to check whether its
6410 safe to build a method signature or not. */
6412 static int
6413 check_method_types_complete (tree decl)
6415 tree type = TREE_TYPE (decl);
6416 tree args;
6418 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6419 return 0;
6421 args = TYPE_ARG_TYPES (type);
6422 if (TREE_CODE (type) == METHOD_TYPE)
6423 args = TREE_CHAIN (args);
6424 for (; args != end_params_node; args = TREE_CHAIN (args))
6425 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6426 return 0;
6428 return 1;
6431 /* Visible interface to check methods contained in CLASS_DECL */
6433 void
6434 java_check_methods (tree class_decl)
6436 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6437 return;
6439 if (CLASS_INTERFACE (class_decl))
6440 java_check_abstract_methods (class_decl);
6441 else
6442 java_check_regular_methods (class_decl);
6444 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6447 /* Like not_accessible_p, but doesn't refer to the current class at
6448 all. */
6449 static bool
6450 hack_is_accessible_p (tree member, tree from_where)
6452 int flags = get_access_flags_from_decl (member);
6454 if (from_where == DECL_CONTEXT (member)
6455 || (flags & ACC_PUBLIC))
6456 return true;
6458 if ((flags & ACC_PROTECTED))
6460 if (inherits_from_p (from_where, DECL_CONTEXT (member)))
6461 return true;
6464 if ((flags & ACC_PRIVATE))
6465 return false;
6467 /* Package private, or protected. */
6468 return in_same_package (TYPE_NAME (from_where),
6469 TYPE_NAME (DECL_CONTEXT (member)));
6472 /* Check all the methods of CLASS_DECL. Methods are first completed
6473 then checked according to regular method existence rules. If no
6474 constructor for CLASS_DECL were encountered, then build its
6475 declaration. */
6476 static void
6477 java_check_regular_methods (tree class_decl)
6479 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6480 tree method;
6481 tree class = TREE_TYPE (class_decl);
6482 tree found = NULL_TREE;
6483 tree mthrows;
6485 /* It is not necessary to check methods defined in java.lang.Object */
6486 if (class == object_type_node)
6487 return;
6489 if (!TYPE_NVIRTUALS (class))
6490 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6492 /* Should take interfaces into account. FIXME */
6493 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6495 tree sig;
6496 tree method_wfl = DECL_FUNCTION_WFL (method);
6497 int aflags;
6499 /* Check for redefinitions */
6500 if (check_method_redefinition (class, method))
6501 continue;
6503 /* We verify things thrown by the method. They must inherit from
6504 java.lang.Throwable. */
6505 for (mthrows = DECL_FUNCTION_THROWS (method);
6506 mthrows; mthrows = TREE_CHAIN (mthrows))
6508 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6509 parse_error_context
6510 (TREE_PURPOSE (mthrows), "Class %qs in %<throws%> clause must be a subclass of class %<java.lang.Throwable%>",
6511 IDENTIFIER_POINTER
6512 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6515 /* If we see one constructor a mark so we don't generate the
6516 default one. Also skip other verifications: constructors
6517 can't be inherited hence hidden or overridden. */
6518 if (DECL_CONSTRUCTOR_P (method))
6520 saw_constructor = 1;
6521 continue;
6524 sig = build_java_argument_signature (TREE_TYPE (method));
6525 found = lookup_argument_method_generic (class, DECL_NAME (method), sig,
6526 SEARCH_SUPER | SEARCH_INTERFACE);
6528 /* Inner class can't declare static methods */
6529 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6531 char *t = xstrdup (lang_printable_name (class, 0));
6532 parse_error_context
6533 (method_wfl, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
6534 lang_printable_name (method, 2), t);
6535 free (t);
6538 /* Nothing overrides or it's a private method. */
6539 if (!found)
6540 continue;
6541 if (METHOD_PRIVATE (found))
6543 found = NULL_TREE;
6544 continue;
6547 /* If `found' is declared in an interface, make sure the
6548 modifier matches. */
6549 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6550 && clinit_identifier_node != DECL_NAME (found)
6551 && !METHOD_PUBLIC (method))
6553 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6554 parse_error_context (method_wfl, "Class %qs must override %qs with a public method in order to implement interface %qs",
6555 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6556 lang_printable_name (method, 2),
6557 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6560 /* Can't override a method with the same name and different return
6561 types. */
6562 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6564 char *t = xstrdup
6565 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6566 parse_error_context
6567 (method_wfl,
6568 "Method %qs was defined with return type %qs in class %qs",
6569 lang_printable_name (found, 2), t,
6570 IDENTIFIER_POINTER
6571 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6572 free (t);
6575 aflags = get_access_flags_from_decl (found);
6577 /* Can't override final. Can't override static. */
6578 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6580 /* Static *can* override static */
6581 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6582 continue;
6583 parse_error_context
6584 (method_wfl,
6585 "%s methods can't be overridden. Method %qs is %s in class %qs",
6586 (METHOD_FINAL (found) ? "Final" : "Static"),
6587 lang_printable_name (found, 2),
6588 (METHOD_FINAL (found) ? "final" : "static"),
6589 IDENTIFIER_POINTER
6590 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6591 continue;
6594 /* Static method can't override instance method. */
6595 if (METHOD_STATIC (method))
6597 parse_error_context
6598 (method_wfl,
6599 "Instance methods can't be overridden by a static method. Method %qs is an instance method in class %qs",
6600 lang_printable_name (found, 2),
6601 IDENTIFIER_POINTER
6602 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6603 continue;
6606 /* - Overriding/hiding public must be public
6607 - Overriding/hiding protected must be protected or public
6608 - If the overridden or hidden method has default (package)
6609 access, then the overriding or hiding method must not be
6610 private; otherwise, a compile-time error occurs. If
6611 `found' belongs to an interface, things have been already
6612 taken care of. */
6613 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6614 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6615 || (METHOD_PROTECTED (found)
6616 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6617 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6618 && METHOD_PRIVATE (method))))
6620 parse_error_context
6621 (method_wfl,
6622 "Methods can't be overridden to be more private. Method %qs is not %s in class %qs", lang_printable_name (method, 2),
6623 (METHOD_PUBLIC (method) ? "public" :
6624 (METHOD_PRIVATE (method) ? "private" : "protected")),
6625 IDENTIFIER_POINTER (DECL_NAME
6626 (TYPE_NAME (DECL_CONTEXT (found)))));
6627 continue;
6630 /* Check this method against all the other implementations it
6631 overrides. Here we only check the class hierarchy; the rest
6632 of the checking is done later. If this method is just a
6633 Miranda method, we can skip the check. */
6634 if (! METHOD_INVISIBLE (method))
6635 check_concrete_throws_clauses (class, method, DECL_NAME (method), sig);
6638 /* The above throws clause check only looked at superclasses. Now
6639 we must also make sure that all methods declared in interfaces
6640 have compatible throws clauses. FIXME: there are more efficient
6641 ways to organize this checking; we should implement one. */
6642 check_interface_throws_clauses (class, class);
6644 if (!TYPE_NVIRTUALS (class))
6645 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6647 /* Search for inherited abstract method not yet implemented in this
6648 class. */
6649 java_check_abstract_method_definitions (class_decl);
6651 if (!saw_constructor)
6652 abort ();
6655 /* Check to make sure that all the methods in all the interfaces
6656 implemented by CLASS_DECL are compatible with the concrete
6657 implementations available in CHECK_CLASS_DECL. */
6658 static void
6659 check_interface_throws_clauses (tree check_class_decl, tree class_decl)
6661 for (; class_decl != NULL_TREE; class_decl = CLASSTYPE_SUPER (class_decl))
6663 int i;
6665 if (! CLASS_LOADED_P (class_decl))
6667 if (CLASS_FROM_SOURCE_P (class_decl))
6668 safe_layout_class (class_decl);
6669 else
6670 load_class (class_decl, 1);
6673 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (class_decl)) - 1; i > 0; --i)
6675 tree interface
6676 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (class_decl), i));
6677 tree iface_method;
6679 for (iface_method = TYPE_METHODS (interface);
6680 iface_method != NULL_TREE;
6681 iface_method = TREE_CHAIN (iface_method))
6683 tree sig, method;
6685 /* First look for a concrete method implemented or
6686 inherited by this class. No need to search
6687 interfaces here, since we're already looking through
6688 all of them. */
6689 sig = build_java_argument_signature (TREE_TYPE (iface_method));
6690 method
6691 = lookup_argument_method_generic (check_class_decl,
6692 DECL_NAME (iface_method),
6693 sig, SEARCH_VISIBLE);
6694 /* If we don't find an implementation, that is ok. Any
6695 potential errors from that are diagnosed elsewhere.
6696 Also, multiple inheritance with conflicting throws
6697 clauses is fine in the absence of a concrete
6698 implementation. */
6699 if (method != NULL_TREE && !METHOD_ABSTRACT (method)
6700 && !METHOD_INVISIBLE (iface_method))
6702 tree method_wfl = DECL_FUNCTION_WFL (method);
6703 check_throws_clauses (method, method_wfl, iface_method);
6707 /* Now check superinterfaces. */
6708 check_interface_throws_clauses (check_class_decl, interface);
6713 /* Check throws clauses of a method against the clauses of all the
6714 methods it overrides. We do this by searching up the class
6715 hierarchy, examining all matching accessible methods. */
6716 static void
6717 check_concrete_throws_clauses (tree class, tree self_method,
6718 tree name, tree signature)
6720 tree method = lookup_argument_method_generic (class, name, signature,
6721 SEARCH_SUPER | SEARCH_VISIBLE);
6722 while (method != NULL_TREE)
6724 if (! METHOD_INVISIBLE (method) && hack_is_accessible_p (method, class))
6725 check_throws_clauses (self_method, DECL_FUNCTION_WFL (self_method),
6726 method);
6728 method = lookup_argument_method_generic (DECL_CONTEXT (method),
6729 name, signature,
6730 SEARCH_SUPER | SEARCH_VISIBLE);
6734 /* Generate an error if the `throws' clause of METHOD (if any) is
6735 incompatible with the `throws' clause of FOUND (if any). */
6736 static void
6737 check_throws_clauses (tree method, tree method_wfl, tree found)
6739 tree mthrows;
6741 /* Can't check these things with class loaded from bytecode. FIXME */
6742 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6743 return;
6745 for (mthrows = DECL_FUNCTION_THROWS (method);
6746 mthrows; mthrows = TREE_CHAIN (mthrows))
6748 tree fthrows;
6750 /* We don't verify unchecked expressions */
6751 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6752 continue;
6753 /* Checked expression must be compatible */
6754 for (fthrows = DECL_FUNCTION_THROWS (found);
6755 fthrows; fthrows = TREE_CHAIN (fthrows))
6757 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6758 break;
6760 if (!fthrows)
6762 parse_error_context
6763 (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",
6764 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6765 lang_printable_name (found, 2),
6766 IDENTIFIER_POINTER
6767 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6772 /* Check abstract method of interface INTERFACE */
6773 static void
6774 java_check_abstract_methods (tree interface_decl)
6776 int i;
6777 tree method, found;
6778 tree interface = TREE_TYPE (interface_decl);
6779 tree base_binfo;
6781 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6783 /* 2- Check for double definition inside the defining interface */
6784 if (check_method_redefinition (interface, method))
6785 continue;
6787 /* 3- Overriding is OK as far as we preserve the return type. */
6788 found = lookup_java_interface_method2 (interface, method);
6789 if (found)
6791 char *t;
6792 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6793 parse_error_context
6794 (DECL_FUNCTION_WFL (found),
6795 "Method %qs was defined with return type %qs in class %qs",
6796 lang_printable_name (found, 2), t,
6797 IDENTIFIER_POINTER
6798 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6799 free (t);
6800 continue;
6804 /* 4- Inherited methods can't differ by their returned types */
6805 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (interface), i, base_binfo); i++)
6807 tree sub_interface_method, sub_interface;
6809 sub_interface = BINFO_TYPE (base_binfo);
6810 for (sub_interface_method = TYPE_METHODS (sub_interface);
6811 sub_interface_method;
6812 sub_interface_method = TREE_CHAIN (sub_interface_method))
6814 found = lookup_java_interface_method2 (interface,
6815 sub_interface_method);
6816 if (found && (found != sub_interface_method))
6818 parse_error_context
6819 (lookup_cl (sub_interface_method),
6820 "Interface %qs inherits method %qs from interface %qs. This method is redefined with a different return type in interface %qs",
6821 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6822 lang_printable_name (found, 2),
6823 IDENTIFIER_POINTER
6824 (DECL_NAME (TYPE_NAME
6825 (DECL_CONTEXT (sub_interface_method)))),
6826 IDENTIFIER_POINTER
6827 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6833 /* Lookup methods in interfaces using their name and partial
6834 signature. Return a matching method only if their types differ. */
6836 static tree
6837 lookup_java_interface_method2 (tree class, tree method_decl)
6839 int i;
6840 tree base_binfo;
6841 tree to_return;
6843 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6845 if ((BINFO_TYPE (base_binfo) != object_type_node)
6846 && (to_return =
6847 lookup_java_method2 (BINFO_TYPE (base_binfo), method_decl, 1)))
6848 return to_return;
6850 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6852 to_return = lookup_java_interface_method2
6853 (BINFO_TYPE (base_binfo), method_decl);
6854 if (to_return)
6855 return to_return;
6858 return NULL_TREE;
6861 /* Lookup method using their name and partial signature. Return a
6862 matching method only if their types differ. */
6864 static tree
6865 lookup_java_method2 (tree clas, tree method_decl, int do_interface)
6867 tree method, method_signature, method_name, method_type, name;
6869 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6870 name = DECL_NAME (method_decl);
6871 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6872 EXPR_WFL_NODE (name) : name);
6873 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6875 while (clas != NULL_TREE)
6877 for (method = TYPE_METHODS (clas);
6878 method != NULL_TREE; method = TREE_CHAIN (method))
6880 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6881 tree name = DECL_NAME (method);
6882 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6883 EXPR_WFL_NODE (name) : name) == method_name
6884 && method_sig == method_signature
6885 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6886 return method;
6888 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6890 return NULL_TREE;
6893 /* Return the line that matches DECL line number, and try its best to
6894 position the column number. Used during error reports.
6895 FUTURE/FIXME: return source_location instead of node. */
6897 static GTY(()) tree cl_v;
6898 static tree
6899 lookup_cl (tree decl)
6901 #ifndef USE_MAPPED_LOCATION
6902 char *line, *found;
6903 #endif
6905 if (!decl)
6906 return NULL_TREE;
6908 if (cl_v == NULL_TREE)
6910 cl_v = build_unknown_wfl (NULL_TREE);
6913 #ifdef USE_MAPPED_LOCATION
6914 SET_EXPR_LOCATION (cl_v, DECL_SOURCE_LOCATION (decl));
6915 #else
6916 EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6917 EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE (decl), -1);
6919 line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6920 EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6922 found = strstr ((const char *)line,
6923 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6924 if (found)
6925 EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6926 #endif
6928 return cl_v;
6931 /* Look for a simple name in the single-type import list */
6933 static tree
6934 find_name_in_single_imports (tree name)
6936 tree node;
6938 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6939 if (TREE_VALUE (node) == name)
6940 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6942 return NULL_TREE;
6945 /* Process all single-type import. */
6947 static int
6948 process_imports (void)
6950 tree import;
6951 int error_found;
6953 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6955 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6956 char *original_name;
6958 /* Don't load twice something already defined. */
6959 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6960 continue;
6962 original_name = xmemdup (IDENTIFIER_POINTER (to_be_found),
6963 IDENTIFIER_LENGTH (to_be_found),
6964 IDENTIFIER_LENGTH (to_be_found) + 1);
6966 while (1)
6968 tree left;
6970 QUALIFIED_P (to_be_found) = 1;
6971 load_class (to_be_found, 0);
6972 error_found =
6973 check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true, NULL_TREE);
6975 /* We found it, we can bail out */
6976 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6978 check_deprecation (TREE_PURPOSE (import),
6979 IDENTIFIER_CLASS_VALUE (to_be_found));
6980 break;
6983 /* We haven't found it. Maybe we're trying to access an
6984 inner class. The only way for us to know is to try again
6985 after having dropped a qualifier. If we can't break it further,
6986 we have an error. */
6987 if (split_qualified_name (&left, NULL, to_be_found))
6988 break;
6990 to_be_found = left;
6992 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6994 parse_error_context (TREE_PURPOSE (import),
6995 "Class or interface %qs not found in import",
6996 original_name);
6997 error_found = 1;
7000 free (original_name);
7001 if (error_found)
7002 return 1;
7004 return 0;
7007 /* Possibly find and mark a class imported by a single-type import
7008 statement. */
7010 static void
7011 find_in_imports (tree enclosing_type, tree class_type)
7013 tree import = (enclosing_type ? TYPE_IMPORT_LIST (enclosing_type) :
7014 ctxp->import_list);
7015 while (import)
7017 if (TREE_VALUE (import) == TYPE_NAME (class_type))
7019 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
7020 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
7021 return;
7023 import = TREE_CHAIN (import);
7027 static int
7028 note_possible_classname (const char *name, int len)
7030 tree node;
7031 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
7032 len = len - 5;
7033 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
7034 len = len - 6;
7035 else
7036 return 0;
7037 node = ident_subst (name, len, "", '/', '.', "");
7038 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
7039 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
7040 return 1;
7043 /* Read a import directory, gathering potential match for further type
7044 references. Indifferently reads a filesystem or a ZIP archive
7045 directory. */
7047 static void
7048 read_import_dir (tree wfl)
7050 tree package_id = EXPR_WFL_NODE (wfl);
7051 const char *package_name = IDENTIFIER_POINTER (package_id);
7052 int package_length = IDENTIFIER_LENGTH (package_id);
7053 DIR *dirp = NULL;
7054 JCF *saved_jcf = current_jcf;
7056 int found = 0;
7057 int k;
7058 void *entry;
7059 struct buffer filename[1];
7061 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
7062 return;
7063 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
7065 BUFFER_INIT (filename);
7066 buffer_grow (filename, package_length + 100);
7068 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
7070 const char *entry_name = jcf_path_name (entry);
7071 int entry_length = strlen (entry_name);
7072 if (jcf_path_is_zipfile (entry))
7074 ZipFile *zipf;
7075 buffer_grow (filename, entry_length);
7076 memcpy (filename->data, entry_name, entry_length - 1);
7077 filename->data[entry_length-1] = '\0';
7078 zipf = opendir_in_zip ((const char *) filename->data, jcf_path_is_system (entry));
7079 if (zipf == NULL)
7080 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
7081 else
7083 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
7084 BUFFER_RESET (filename);
7085 for (k = 0; k < package_length; k++)
7087 char ch = package_name[k];
7088 *filename->ptr++ = ch == '.' ? '/' : ch;
7090 *filename->ptr++ = '/';
7092 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
7094 const char *current_entry = ZIPDIR_FILENAME (zipd);
7095 int current_entry_len = zipd->filename_length;
7097 if (current_entry_len >= BUFFER_LENGTH (filename)
7098 && strncmp ((const char *) filename->data, current_entry,
7099 BUFFER_LENGTH (filename)) != 0)
7100 continue;
7101 found |= note_possible_classname (current_entry,
7102 current_entry_len);
7106 else
7108 BUFFER_RESET (filename);
7109 buffer_grow (filename, entry_length + package_length + 4);
7110 strcpy ((char *) filename->data, entry_name);
7111 filename->ptr = filename->data + entry_length;
7112 for (k = 0; k < package_length; k++)
7114 char ch = package_name[k];
7115 *filename->ptr++ = ch == '.' ? '/' : ch;
7117 *filename->ptr = '\0';
7119 dirp = opendir ((const char *) filename->data);
7120 if (dirp == NULL)
7121 continue;
7122 *filename->ptr++ = '/';
7123 for (;;)
7125 int len;
7126 const char *d_name;
7127 struct dirent *direntp = readdir (dirp);
7128 if (!direntp)
7129 break;
7130 d_name = direntp->d_name;
7131 len = strlen (direntp->d_name);
7132 buffer_grow (filename, len+1);
7133 strcpy ((char *) filename->ptr, d_name);
7134 found |= note_possible_classname ((const char *) filename->data + entry_length,
7135 package_length+len+1);
7137 if (dirp)
7138 closedir (dirp);
7142 free (filename->data);
7144 /* Here we should have a unified way of retrieving an entry, to be
7145 indexed. */
7146 if (!found)
7148 static int first = 1;
7149 if (first)
7151 error ("Can't find default package %qs. Check the CLASSPATH environment variable and the access to the archives", package_name);
7152 java_error_count++;
7153 first = 0;
7155 else
7156 parse_error_context (wfl, "Package %qs not found in import",
7157 package_name);
7158 current_jcf = saved_jcf;
7159 return;
7161 current_jcf = saved_jcf;
7164 /* Possibly find a type in the import on demands specified
7165 types. Returns 1 if an error occurred, 0 otherwise. Run through the
7166 entire list, to detected potential double definitions. */
7168 static int
7169 find_in_imports_on_demand (tree enclosing_type, tree class_type)
7171 tree class_type_name = TYPE_NAME (class_type);
7172 tree import = (enclosing_type ? TYPE_IMPORT_DEMAND_LIST (enclosing_type) :
7173 ctxp->import_demand_list);
7174 tree cl = NULL_TREE;
7175 int seen_once = -1; /* -1 when not set, 1 if seen once, >1 otherwise. */
7176 int to_return = -1; /* -1 when not set, 0 or 1 otherwise */
7177 tree node;
7179 for (; import; import = TREE_CHAIN (import))
7181 location_t saved_location = input_location;
7182 int access_check;
7183 const char *id_name;
7184 tree decl, type_name_copy;
7186 obstack_grow (&temporary_obstack,
7187 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
7188 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
7189 obstack_1grow (&temporary_obstack, '.');
7190 obstack_grow0 (&temporary_obstack,
7191 IDENTIFIER_POINTER (class_type_name),
7192 IDENTIFIER_LENGTH (class_type_name));
7193 id_name = obstack_finish (&temporary_obstack);
7195 if (! (node = maybe_get_identifier (id_name)))
7196 continue;
7198 /* Setup input_line so that it refers to the line of the import (in
7199 case we parse a class file and encounter errors */
7200 #ifdef USE_MAPPED_LOCATION
7201 input_location = EXPR_LOCATION (TREE_PURPOSE (import));
7202 #else
7203 input_line = EXPR_WFL_LINENO (TREE_PURPOSE (import));
7204 #endif
7206 type_name_copy = TYPE_NAME (class_type);
7207 TYPE_NAME (class_type) = node;
7208 QUALIFIED_P (node) = 1;
7209 decl = IDENTIFIER_CLASS_VALUE (node);
7210 access_check = -1;
7211 /* If there is no DECL set for the class or if the class isn't
7212 loaded and not seen in source yet, then load */
7213 if (!decl || ! CLASS_LOADED_P (TREE_TYPE (decl)))
7215 load_class (node, 0);
7216 decl = IDENTIFIER_CLASS_VALUE (node);
7218 if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7219 access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7220 false, NULL_TREE);
7221 else
7222 /* 6.6.1: Inner classes are subject to member access rules. */
7223 access_check = 0;
7225 input_location = saved_location;
7227 /* If the loaded class is not accessible or couldn't be loaded,
7228 we restore the original TYPE_NAME and process the next
7229 import. */
7230 if (access_check || !decl)
7232 TYPE_NAME (class_type) = type_name_copy;
7233 continue;
7236 /* If the loaded class is accessible, we keep a tab on it to
7237 detect and report multiple inclusions. */
7238 if (IS_A_CLASSFILE_NAME (node))
7240 if (seen_once < 0)
7242 cl = TREE_PURPOSE (import);
7243 seen_once = 1;
7245 else if (seen_once >= 0)
7247 tree location = (cl ? cl : TREE_PURPOSE (import));
7248 tree package = (cl ? EXPR_WFL_NODE (cl) :
7249 EXPR_WFL_NODE (TREE_PURPOSE (import)));
7250 seen_once++;
7251 parse_error_context
7252 (location,
7253 "Type %qs also potentially defined in package %qs",
7254 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7255 IDENTIFIER_POINTER (package));
7258 to_return = access_check;
7261 if (seen_once == 1)
7262 return to_return;
7263 else
7264 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7267 /* Add package NAME to the list of packages encountered so far. To
7268 speed up class lookup in do_resolve_class, we make sure a
7269 particular package is added only once. */
7271 static void
7272 register_package (tree name)
7274 static htab_t pht;
7275 void **e;
7277 if (pht == NULL)
7278 pht = htab_create (50, htab_hash_pointer, htab_eq_pointer, NULL);
7280 e = htab_find_slot (pht, name, INSERT);
7281 if (*e == NULL)
7283 package_list = chainon (package_list, build_tree_list (name, NULL));
7284 *e = name;
7288 static tree
7289 resolve_package (tree pkg, tree *next, tree *type_name)
7291 tree current;
7292 tree decl = NULL_TREE;
7293 *type_name = NULL_TREE;
7295 /* The trick is to determine when the package name stops and were
7296 the name of something contained in the package starts. Then we
7297 return a fully qualified name of what we want to get. */
7299 *next = EXPR_WFL_QUALIFICATION (pkg);
7301 /* Try to progressively construct a type name */
7302 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7303 for (current = EXPR_WFL_QUALIFICATION (pkg);
7304 current; current = TREE_CHAIN (current))
7306 /* If we don't have what we're expecting, exit now. TYPE_NAME
7307 will be null and the error caught later. */
7308 if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7309 break;
7310 *type_name =
7311 merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7312 if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7314 /* resolve_package should be used in a loop, hence we
7315 point at this one to naturally process the next one at
7316 the next iteration. */
7317 *next = current;
7318 break;
7321 return decl;
7325 /* Check accessibility of inner classes according to member access rules.
7326 DECL is the inner class, ENCLOSING_DECL is the class from which the
7327 access is being attempted. */
7329 static void
7330 check_inner_class_access (tree decl, tree enclosing_decl, tree cl)
7332 const char *access;
7333 tree enclosing_decl_type;
7335 /* We don't issue an error message when CL is null. CL can be null
7336 as a result of processing a JDEP crafted by source_start_java_method
7337 for the purpose of patching its parm decl. But the error would
7338 have been already trapped when fixing the method's signature.
7339 DECL can also be NULL in case of earlier errors. */
7340 if (!decl || !cl)
7341 return;
7343 enclosing_decl_type = TREE_TYPE (enclosing_decl);
7345 if (CLASS_PRIVATE (decl))
7347 /* Access is permitted only within the body of the top-level
7348 class in which DECL is declared. */
7349 tree top_level = decl;
7350 while (DECL_CONTEXT (top_level))
7351 top_level = DECL_CONTEXT (top_level);
7352 while (DECL_CONTEXT (enclosing_decl))
7353 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7354 if (top_level == enclosing_decl)
7355 return;
7356 access = "private";
7358 else if (CLASS_PROTECTED (decl))
7360 tree decl_context;
7361 /* Access is permitted from within the same package... */
7362 if (in_same_package (decl, enclosing_decl))
7363 return;
7365 /* ... or from within the body of a subtype of the context in which
7366 DECL is declared. */
7367 decl_context = DECL_CONTEXT (decl);
7368 while (enclosing_decl)
7370 if (CLASS_INTERFACE (decl))
7372 if (interface_of_p (TREE_TYPE (decl_context),
7373 enclosing_decl_type))
7374 return;
7376 else
7378 /* Eww. The order of the arguments is different!! */
7379 if (inherits_from_p (enclosing_decl_type,
7380 TREE_TYPE (decl_context)))
7381 return;
7383 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7385 access = "protected";
7387 else if (! CLASS_PUBLIC (decl))
7389 /* Access is permitted only from within the same package as DECL. */
7390 if (in_same_package (decl, enclosing_decl))
7391 return;
7392 access = "non-public";
7394 else
7395 /* Class is public. */
7396 return;
7398 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7399 (CLASS_INTERFACE (decl) ? "interface" : "class"),
7400 lang_printable_name (decl, 2), access);
7403 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7404 foreign package, it must be PUBLIC. Return 0 if no access
7405 violations were found, 1 otherwise. If VERBOSE is true and an error
7406 was found, it is reported and accounted for. If CL is NULL then
7407 look it up with THIS_DECL. */
7409 static int
7410 check_pkg_class_access (tree class_name, tree cl, bool verbose, tree this_decl)
7412 tree type;
7414 if (!IDENTIFIER_CLASS_VALUE (class_name))
7415 return 0;
7417 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7418 return 0;
7420 if (!CLASS_PUBLIC (TYPE_NAME (type)))
7422 /* Access to a private class within the same package is
7423 allowed. */
7424 tree l, r;
7425 split_qualified_name (&l, &r, class_name);
7426 if (!QUALIFIED_P (class_name) && !ctxp->package)
7427 /* Both in the empty package. */
7428 return 0;
7429 if (l == ctxp->package)
7430 /* Both in the same package. */
7431 return 0;
7433 if (verbose)
7434 parse_error_context
7435 (cl == NULL ? lookup_cl (this_decl): cl,
7436 "Can't access %s %qs. Only public classes and interfaces in other packages can be accessed",
7437 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7438 IDENTIFIER_POINTER (class_name));
7439 return 1;
7441 return 0;
7444 /* Local variable declaration. */
7446 static void
7447 declare_local_variables (int modifier, tree type, tree vlist)
7449 tree decl, current, saved_type;
7450 tree type_wfl = NULL_TREE;
7451 int must_chain = 0;
7452 int final_p = 0;
7454 /* Push a new block if statements were seen between the last time we
7455 pushed a block and now. Keep a count of blocks to close */
7456 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7458 tree b = enter_block ();
7459 BLOCK_IS_IMPLICIT (b) = 1;
7462 if (modifier)
7464 size_t i;
7465 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7466 if (1 << i & modifier)
7467 break;
7468 if (modifier == ACC_FINAL)
7469 final_p = 1;
7470 else
7472 parse_error_context
7473 (ctxp->modifier_ctx [i],
7474 "Only %<final%> is allowed as a local variables modifier");
7475 return;
7479 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7480 hold the TYPE value if a new incomplete has to be created (as
7481 opposed to being found already existing and reused). */
7482 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7484 /* If TYPE is fully resolved and we don't have a reference, make one */
7485 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7487 /* Go through all the declared variables */
7488 for (current = vlist, saved_type = type; current;
7489 current = TREE_CHAIN (current), type = saved_type)
7491 tree other, real_type;
7492 tree wfl = TREE_PURPOSE (current);
7493 tree name = EXPR_WFL_NODE (wfl);
7494 tree init = TREE_VALUE (current);
7496 /* Process NAME, as it may specify extra dimension(s) for it */
7497 type = build_array_from_name (type, type_wfl, name, &name);
7499 /* Variable redefinition check */
7500 if ((other = lookup_name_in_blocks (name)))
7502 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7503 DECL_SOURCE_LINE (other));
7504 continue;
7507 /* Type adjustment. We may have just readjusted TYPE because
7508 the variable specified more dimensions. Make sure we have
7509 a reference if we can and don't have one already. */
7510 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7512 real_type = GET_REAL_TYPE (type);
7513 /* Never layout this decl. This will be done when its scope
7514 will be entered */
7515 decl = build_decl (VAR_DECL, name, real_type);
7516 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7517 DECL_FINAL (decl) = final_p;
7518 BLOCK_CHAIN_DECL (decl);
7520 /* If doing xreferencing, replace the line number with the WFL
7521 compound value */
7522 #ifdef USE_MAPPED_LOCATION
7523 if (flag_emit_xref)
7524 DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (wfl);
7525 #else
7526 if (flag_emit_xref)
7527 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7528 #endif
7530 /* Don't try to use an INIT statement when an error was found */
7531 if (init && java_error_count)
7532 init = NULL_TREE;
7534 /* Remember it if this is an initialized-upon-declaration final
7535 variable. */
7536 if (init && final_p)
7538 DECL_LOCAL_FINAL_IUD (decl) = 1;
7541 /* Add the initialization function to the current function's code */
7542 if (init)
7544 /* Name might have been readjusted */
7545 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7546 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7547 java_method_add_stmt (current_function_decl,
7548 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7549 init));
7552 /* Setup dependency the type of the decl */
7553 if (must_chain)
7555 jdep *dep;
7556 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7557 dep = CLASSD_LAST (ctxp->classd_list);
7558 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7561 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7564 /* Called during parsing. Build decls from argument list. */
7566 static void
7567 source_start_java_method (tree fndecl)
7569 tree tem;
7570 tree parm_decl;
7571 int i;
7573 if (!fndecl)
7574 return;
7576 current_function_decl = fndecl;
7578 /* New scope for the function */
7579 enter_block ();
7580 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7581 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7583 tree type = TREE_VALUE (tem);
7584 tree name = TREE_PURPOSE (tem);
7586 /* If type is incomplete. Create an incomplete decl and ask for
7587 the decl to be patched later */
7588 if (INCOMPLETE_TYPE_P (type))
7590 jdep *jdep;
7591 tree real_type = GET_REAL_TYPE (type);
7592 parm_decl = build_decl (PARM_DECL, name, real_type);
7593 type = obtain_incomplete_type (type);
7594 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7595 jdep = CLASSD_LAST (ctxp->classd_list);
7596 JDEP_MISC (jdep) = name;
7597 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7599 else
7600 parm_decl = build_decl (PARM_DECL, name, type);
7602 /* Remember if a local variable was declared final (via its
7603 TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7604 if (ARG_FINAL_P (tem))
7606 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7607 DECL_FINAL (parm_decl) = 1;
7610 BLOCK_CHAIN_DECL (parm_decl);
7612 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7613 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7614 nreverse (tem);
7615 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7616 DECL_MAX_LOCALS (current_function_decl) = i;
7619 /* Called during parsing. Creates an artificial method declaration. */
7621 static tree
7622 create_artificial_method (tree class, int flags, tree type,
7623 tree name, tree args)
7625 tree mdecl;
7626 location_t save_location = input_location;
7628 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (class));
7629 mdecl = make_node (FUNCTION_TYPE);
7630 TREE_TYPE (mdecl) = type;
7631 TYPE_ARG_TYPES (mdecl) = args;
7632 /* We used to compute the signature of MDECL here and then use
7633 add_method(), but that failed because our caller might modify
7634 the type of the returned method, which trashes the cache in
7635 get_type_from_signature(). */
7636 mdecl = add_method_1 (class, flags, name, mdecl);
7637 input_location = save_location;
7638 DECL_ARTIFICIAL (mdecl) = 1;
7639 return mdecl;
7642 /* Starts the body if an artificial method. */
7644 static void
7645 start_artificial_method_body (tree mdecl)
7647 #ifdef USE_MAPPED_LOCATION
7648 DECL_SOURCE_LOCATION (mdecl) = ctxp->file_start_location;
7649 DECL_FUNCTION_LAST_LINE (mdecl) = ctxp->file_start_location;
7650 #else
7651 DECL_SOURCE_LINE (mdecl) = 1;
7652 DECL_FUNCTION_LAST_LINE (mdecl) = 1;
7653 #endif
7654 source_start_java_method (mdecl);
7655 enter_block ();
7658 static void
7659 end_artificial_method_body (tree mdecl)
7661 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7662 It has to be evaluated first. (if mdecl is current_function_decl,
7663 we have an undefined behavior if no temporary variable is used.) */
7664 tree b = exit_block ();
7665 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7666 exit_block ();
7669 /* Dump a tree of some kind. This is a convenience wrapper for the
7670 dump_* functions in tree-dump.c. */
7671 static void
7672 dump_java_tree (enum tree_dump_index phase, tree t)
7674 FILE *stream;
7675 int flags;
7677 stream = dump_begin (phase, &flags);
7678 flags |= TDF_SLIM;
7679 if (stream)
7681 dump_node (t, flags, stream);
7682 dump_end (phase, stream);
7686 /* Terminate a function and expand its body. */
7688 static void
7689 source_end_java_method (void)
7691 tree fndecl = current_function_decl;
7693 if (!fndecl)
7694 return;
7696 java_parser_context_save_global ();
7697 #ifdef USE_MAPPED_LOCATION
7698 input_location = ctxp->last_ccb_indent1;
7699 #else
7700 input_line = ctxp->last_ccb_indent1;
7701 #endif
7703 /* Turn function bodies with only a NOP expr null, so they don't get
7704 generated at all and we won't get warnings when using the -W
7705 -Wall flags. */
7706 if (IS_EMPTY_STMT (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))))
7707 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7709 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7710 && ! flag_emit_class_files
7711 && ! flag_emit_xref)
7712 finish_method (fndecl);
7714 current_function_decl = NULL_TREE;
7715 java_parser_context_restore_global ();
7716 current_function_decl = NULL_TREE;
7719 /* Record EXPR in the current function block. Complements compound
7720 expression second operand if necessary. */
7722 tree
7723 java_method_add_stmt (tree fndecl, tree expr)
7725 if (!GET_CURRENT_BLOCK (fndecl))
7726 return NULL_TREE;
7727 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7730 static tree
7731 add_stmt_to_block (tree b, tree type, tree stmt)
7733 tree body = BLOCK_EXPR_BODY (b), c;
7735 if (java_error_count)
7736 return body;
7738 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7739 return body;
7741 BLOCK_EXPR_BODY (b) = c;
7742 TREE_SIDE_EFFECTS (c) = 1;
7743 return c;
7746 /* Lays out the methods for the classes seen so far. */
7748 void
7749 java_layout_seen_class_methods (void)
7751 tree previous_list = all_class_list;
7752 tree end = NULL_TREE;
7753 tree current;
7755 while (1)
7757 for (current = previous_list;
7758 current != end; current = TREE_CHAIN (current))
7760 tree decl = TREE_VALUE (current);
7761 tree cls = TREE_TYPE (decl);
7763 input_location = DECL_SOURCE_LOCATION (decl);
7765 if (! CLASS_LOADED_P (cls))
7766 load_class (cls, 0);
7768 layout_class_methods (cls);
7771 /* Note that new classes might have been added while laying out
7772 methods, changing the value of all_class_list. */
7774 if (previous_list != all_class_list)
7776 end = previous_list;
7777 previous_list = all_class_list;
7779 else
7780 break;
7784 static GTY(()) tree stop_reordering;
7785 void
7786 java_reorder_fields (void)
7788 tree current;
7790 for (current = gclass_list; current; current = TREE_CHAIN (current))
7792 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7794 if (current_class == stop_reordering)
7795 break;
7797 /* Reverse the fields, but leave the dummy field in front.
7798 Fields are already ordered for Object and Class */
7799 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7800 && current_class != class_type_node)
7802 /* If the dummy field is there, reverse the right fields and
7803 just layout the type for proper fields offset */
7804 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7806 tree fields = TYPE_FIELDS (current_class);
7807 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7808 TYPE_SIZE (current_class) = NULL_TREE;
7810 /* We don't have a dummy field, we need to layout the class,
7811 after having reversed the fields */
7812 else
7814 TYPE_FIELDS (current_class) =
7815 nreverse (TYPE_FIELDS (current_class));
7816 TYPE_SIZE (current_class) = NULL_TREE;
7820 /* There are cases were gclass_list will be empty. */
7821 if (gclass_list)
7822 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7825 /* Layout the methods of all classes loaded in one way or another.
7826 Check methods of source parsed classes. Then reorder the
7827 fields and layout the classes or the type of all source parsed
7828 classes */
7830 void
7831 java_layout_classes (void)
7833 tree current;
7834 int save_error_count = java_error_count;
7836 /* Layout the methods of all classes seen so far */
7837 java_layout_seen_class_methods ();
7838 java_parse_abort_on_error ();
7839 all_class_list = NULL_TREE;
7841 /* Then check the methods of all parsed classes */
7842 for (current = gclass_list; current; current = TREE_CHAIN (current))
7843 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7844 java_check_methods (TREE_VALUE (current));
7845 java_parse_abort_on_error ();
7847 for (current = gclass_list; current; current = TREE_CHAIN (current))
7849 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7850 layout_class (current_class);
7852 /* Error reported by the caller */
7853 if (java_error_count)
7854 return;
7857 /* We might have reloaded classes durign the process of laying out
7858 classes for code generation. We must layout the methods of those
7859 late additions, as constructor checks might use them */
7860 java_layout_seen_class_methods ();
7861 java_parse_abort_on_error ();
7864 /* Expand methods in the current set of classes remembered for
7865 generation. */
7867 static void
7868 java_complete_expand_classes (void)
7870 tree current;
7872 do_not_fold = flag_emit_xref;
7874 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7875 if (!INNER_CLASS_DECL_P (current))
7876 java_complete_expand_class (current);
7879 /* Expand the methods found in OUTER, starting first by OUTER's inner
7880 classes, if any. */
7882 static void
7883 java_complete_expand_class (tree outer)
7885 tree inner_list;
7887 /* We need to go after all inner classes and start expanding them,
7888 starting with most nested ones. We have to do that because nested
7889 classes might add functions to outer classes */
7891 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7892 inner_list; inner_list = TREE_CHAIN (inner_list))
7893 java_complete_expand_class (TREE_PURPOSE (inner_list));
7895 java_complete_expand_methods (outer);
7898 /* Expand methods registered in CLASS_DECL. The general idea is that
7899 we expand regular methods first. This allows us get an estimate on
7900 how outer context local alias fields are really used so we can add
7901 to the constructor just enough code to initialize them properly (it
7902 also lets us generate finit$ correctly.) Then we expand the
7903 constructors and then <clinit>. */
7905 static void
7906 java_complete_expand_methods (tree class_decl)
7908 tree clinit, decl, first_decl;
7910 output_class = current_class = TREE_TYPE (class_decl);
7912 /* Pre-expand <clinit> to figure whether we really need it or
7913 not. If we do need it, we pre-expand the static fields so they're
7914 ready to be used somewhere else. <clinit> will be fully expanded
7915 after we processed the constructors. */
7916 first_decl = TYPE_METHODS (current_class);
7917 clinit = maybe_generate_pre_expand_clinit (current_class);
7919 /* Then generate finit$ (if we need to) because constructors will
7920 try to use it.*/
7921 if (TYPE_FINIT_STMT_LIST (current_class))
7922 java_complete_expand_method (generate_finit (current_class));
7924 /* Then generate instinit$ (if we need to) because constructors will
7925 try to use it. */
7926 if (TYPE_II_STMT_LIST (current_class))
7927 java_complete_expand_method (generate_instinit (current_class));
7929 /* Now do the constructors */
7930 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7932 if (!DECL_CONSTRUCTOR_P (decl))
7933 continue;
7934 java_complete_expand_method (decl);
7937 /* First, do the ordinary methods. */
7938 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7940 /* Ctors aren't part of this batch. */
7941 if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7942 continue;
7944 /* Skip abstract or native methods -- but do handle native
7945 methods when generating JNI stubs. */
7946 if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7948 DECL_FUNCTION_BODY (decl) = NULL_TREE;
7949 continue;
7952 if (METHOD_NATIVE (decl))
7954 tree body;
7955 current_function_decl = decl;
7956 body = build_jni_stub (decl);
7957 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7960 java_complete_expand_method (decl);
7963 /* If there is indeed a <clinit>, fully expand it now */
7964 if (clinit)
7966 /* Prevent the use of `this' inside <clinit> */
7967 ctxp->explicit_constructor_p = 1;
7968 java_complete_expand_method (clinit);
7969 ctxp->explicit_constructor_p = 0;
7972 /* We might have generated a class$ that we now want to expand */
7973 if (TYPE_DOT_CLASS (current_class))
7974 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7976 /* Now verify constructor circularity (stop after the first one we
7977 prove wrong.) */
7978 if (!CLASS_INTERFACE (class_decl))
7979 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7980 if (DECL_CONSTRUCTOR_P (decl)
7981 && verify_constructor_circularity (decl, decl))
7982 break;
7985 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7986 safely used in some other methods/constructors. */
7988 static tree
7989 maybe_generate_pre_expand_clinit (tree class_type)
7991 tree current, mdecl;
7993 if (!TYPE_CLINIT_STMT_LIST (class_type))
7994 return NULL_TREE;
7996 /* Go through all static fields and pre expand them */
7997 for (current = TYPE_FIELDS (class_type); current;
7998 current = TREE_CHAIN (current))
7999 if (FIELD_STATIC (current))
8000 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
8002 /* Then build the <clinit> method */
8003 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
8004 clinit_identifier_node, end_params_node);
8005 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
8006 mdecl, NULL_TREE);
8007 start_artificial_method_body (mdecl);
8009 /* We process the list of assignment we produced as the result of
8010 the declaration of initialized static field and add them as
8011 statement to the <clinit> method. */
8012 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
8013 current = TREE_CHAIN (current))
8015 tree stmt = current;
8016 /* We build the assignment expression that will initialize the
8017 field to its value. There are strict rules on static
8018 initializers (8.5). FIXME */
8019 if (TREE_CODE (stmt) != BLOCK && !IS_EMPTY_STMT (stmt))
8020 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
8021 java_method_add_stmt (mdecl, stmt);
8024 end_artificial_method_body (mdecl);
8026 /* Now we want to place <clinit> as the last method (because we need
8027 it at least for interface so that it doesn't interfere with the
8028 dispatch table based lookup. */
8029 if (TREE_CHAIN (TYPE_METHODS (class_type)))
8031 current = TREE_CHAIN (TYPE_METHODS (class_type));
8032 TYPE_METHODS (class_type) = current;
8034 while (TREE_CHAIN (current))
8035 current = TREE_CHAIN (current);
8037 TREE_CHAIN (current) = mdecl;
8038 TREE_CHAIN (mdecl) = NULL_TREE;
8041 return mdecl;
8044 /* Analyzes a method body and look for something that isn't a
8045 MODIFY_EXPR with a constant value. */
8047 static int
8048 analyze_clinit_body (tree this_class, tree bbody)
8050 while (bbody)
8051 switch (TREE_CODE (bbody))
8053 case BLOCK:
8054 bbody = BLOCK_EXPR_BODY (bbody);
8055 break;
8057 case EXPR_WITH_FILE_LOCATION:
8058 bbody = EXPR_WFL_NODE (bbody);
8059 break;
8061 case COMPOUND_EXPR:
8062 if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
8063 return 1;
8064 bbody = TREE_OPERAND (bbody, 1);
8065 break;
8067 case MODIFY_EXPR:
8068 /* If we're generating to class file and we're dealing with an
8069 array initialization, we return 1 to keep <clinit> */
8070 if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
8071 && flag_emit_class_files)
8072 return 1;
8074 /* There are a few cases where we're required to keep
8075 <clinit>:
8076 - If this is an assignment whose operand is not constant,
8077 - If this is an assignment to a non-initialized field,
8078 - If this field is not a member of the current class.
8080 return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
8081 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
8082 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
8084 default:
8085 return 1;
8087 return 0;
8091 /* See whether we could get rid of <clinit>. Criteria are: all static
8092 final fields have constant initial values and the body of <clinit>
8093 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
8095 static int
8096 maybe_yank_clinit (tree mdecl)
8098 tree type, current;
8099 tree fbody, bbody;
8101 if (!DECL_CLINIT_P (mdecl))
8102 return 0;
8104 /* If the body isn't empty, then we keep <clinit>. Note that if
8105 we're emitting classfiles, this isn't enough not to rule it
8106 out. */
8107 fbody = DECL_FUNCTION_BODY (mdecl);
8108 bbody = BLOCK_EXPR_BODY (fbody);
8109 if (bbody && bbody != error_mark_node)
8110 bbody = BLOCK_EXPR_BODY (bbody);
8111 else
8112 return 0;
8113 if (bbody && ! flag_emit_class_files && !IS_EMPTY_STMT (bbody))
8114 return 0;
8116 type = DECL_CONTEXT (mdecl);
8117 current = TYPE_FIELDS (type);
8119 for (current = (current ? TREE_CHAIN (current) : current);
8120 current; current = TREE_CHAIN (current))
8122 tree f_init;
8124 /* We're not interested in non-static fields. */
8125 if (!FIELD_STATIC (current))
8126 continue;
8128 /* Nor in fields without initializers. */
8129 f_init = DECL_INITIAL (current);
8130 if (f_init == NULL_TREE)
8131 continue;
8133 /* Anything that isn't String or a basic type is ruled out -- or
8134 if we know how to deal with it (when doing things natively) we
8135 should generated an empty <clinit> so that SUID are computed
8136 correctly. */
8137 if (! JSTRING_TYPE_P (TREE_TYPE (current))
8138 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
8139 return 0;
8141 if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
8142 return 0;
8145 /* Now we analyze the method body and look for something that
8146 isn't a MODIFY_EXPR */
8147 if (!IS_EMPTY_STMT (bbody) && analyze_clinit_body (type, bbody))
8148 return 0;
8150 /* Get rid of <clinit> in the class' list of methods */
8151 if (TYPE_METHODS (type) == mdecl)
8152 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
8153 else
8154 for (current = TYPE_METHODS (type); current;
8155 current = TREE_CHAIN (current))
8156 if (TREE_CHAIN (current) == mdecl)
8158 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
8159 break;
8162 return 1;
8165 /* Install the argument from MDECL. Suitable to completion and
8166 expansion of mdecl's body. */
8168 void
8169 start_complete_expand_method (tree mdecl)
8171 tree tem;
8173 pushlevel (1); /* Prepare for a parameter push */
8174 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
8175 DECL_ARGUMENTS (mdecl) = tem;
8177 for (; tem; tem = TREE_CHAIN (tem))
8179 /* TREE_CHAIN (tem) will change after pushdecl. */
8180 tree next = TREE_CHAIN (tem);
8181 tree type = TREE_TYPE (tem);
8182 if (targetm.calls.promote_prototypes (type)
8183 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8184 && INTEGRAL_TYPE_P (type))
8185 type = integer_type_node;
8186 DECL_ARG_TYPE (tem) = type;
8187 layout_decl (tem, 0);
8188 pushdecl (tem);
8189 /* Re-install the next so that the list is kept and the loop
8190 advances. */
8191 TREE_CHAIN (tem) = next;
8193 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8194 input_location = DECL_SOURCE_LOCATION (mdecl);
8195 build_result_decl (mdecl);
8199 /* Complete and expand a method. */
8201 static void
8202 java_complete_expand_method (tree mdecl)
8204 tree fbody, block_body, exception_copy;
8206 current_function_decl = mdecl;
8207 /* Fix constructors before expanding them */
8208 if (DECL_CONSTRUCTOR_P (mdecl))
8209 fix_constructors (mdecl);
8211 /* Expand functions that have a body */
8212 if (!DECL_FUNCTION_BODY (mdecl))
8213 return;
8215 fbody = DECL_FUNCTION_BODY (mdecl);
8216 block_body = BLOCK_EXPR_BODY (fbody);
8217 exception_copy = NULL_TREE;
8219 current_function_decl = mdecl;
8221 if (! quiet_flag)
8222 fprintf (stderr, " [%s.",
8223 lang_printable_name (DECL_CONTEXT (mdecl), 0));
8224 announce_function (mdecl);
8225 if (! quiet_flag)
8226 fprintf (stderr, "]");
8228 /* Prepare the function for tree completion */
8229 start_complete_expand_method (mdecl);
8231 /* Install the current this */
8232 current_this = (!METHOD_STATIC (mdecl) ?
8233 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8235 /* Purge the `throws' list of unchecked exceptions (we save a copy
8236 of the list and re-install it later.) */
8237 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8238 purge_unchecked_exceptions (mdecl);
8240 /* Install exceptions thrown with `throws' */
8241 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8243 if (block_body != NULL_TREE)
8245 block_body = java_complete_tree (block_body);
8247 /* Before we check initialization, attached all class initialization
8248 variable to the block_body */
8249 htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8250 attach_init_test_initialization_flags, block_body);
8252 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
8254 check_for_initialization (block_body, mdecl);
8256 /* Go through all the flags marking the initialization of
8257 static variables and see whether they're definitively
8258 assigned, in which case the type is remembered as
8259 definitively initialized in MDECL. */
8260 if (STATIC_CLASS_INIT_OPT_P ())
8262 /* Always register the context as properly initialized in
8263 MDECL. This used with caution helps removing extra
8264 initialization of self. */
8265 if (METHOD_STATIC (mdecl))
8267 *(htab_find_slot
8268 (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8269 DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8273 ctxp->explicit_constructor_p = 0;
8276 BLOCK_EXPR_BODY (fbody) = block_body;
8278 /* If we saw a return but couldn't evaluate it properly, we'll have
8279 an error_mark_node here. */
8280 if (block_body != error_mark_node
8281 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8282 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
8283 && !flag_emit_xref)
8284 missing_return_error (current_function_decl);
8286 /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8287 maybe_yank_clinit (mdecl);
8289 /* Pop the current level, with special measures if we found errors. */
8290 if (java_error_count)
8291 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8292 poplevel (1, 0, 1);
8294 /* Pop the exceptions and sanity check */
8295 POP_EXCEPTIONS();
8296 if (currently_caught_type_list)
8297 abort ();
8299 /* Restore the copy of the list of exceptions if emitting xrefs. */
8300 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8303 /* For with each class for which there's code to generate. */
8305 static void
8306 java_expand_method_bodies (tree class)
8308 tree decl;
8309 for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8311 tree block;
8313 if (! DECL_FUNCTION_BODY (decl))
8314 continue;
8316 current_function_decl = decl;
8318 block = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8320 /* Save the function body for gimplify and inlining. */
8321 DECL_SAVED_TREE (decl) = block;
8323 /* It's time to assign the variable flagging static class
8324 initialization based on which classes invoked static methods
8325 are definitely initializing. This should be flagged. */
8326 if (STATIC_CLASS_INIT_OPT_P ())
8328 tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8329 for (; list != NULL_TREE; list = TREE_CHAIN (list))
8331 /* Executed for each statement calling a static function.
8332 LIST is a TREE_LIST whose PURPOSE is the called function
8333 and VALUE is a compound whose second operand can be patched
8334 with static class initialization flag assignments. */
8336 tree called_method = TREE_PURPOSE (list);
8337 tree compound = TREE_VALUE (list);
8338 tree assignment_compound_list
8339 = build_tree_list (called_method, NULL);
8341 /* For each class definitely initialized in
8342 CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8343 assignment to the class initialization flag. */
8344 htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8345 emit_test_initialization,
8346 assignment_compound_list);
8348 if (TREE_VALUE (assignment_compound_list))
8349 TREE_OPERAND (compound, 1)
8350 = TREE_VALUE (assignment_compound_list);
8354 /* Expand the function body. */
8355 source_end_java_method ();
8361 /* This section of the code deals with accessing enclosing context
8362 fields either directly by using the relevant access to this$<n> or
8363 by invoking an access method crafted for that purpose. */
8365 /* Build the necessary access from an inner class to an outer
8366 class. This routine could be optimized to cache previous result
8367 (decl, current_class and returned access). When an access method
8368 needs to be generated, it always takes the form of a read. It might
8369 be later turned into a write by calling outer_field_access_fix. */
8371 static tree
8372 build_outer_field_access (tree id, tree decl)
8374 tree access = NULL_TREE;
8375 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8376 tree decl_ctx = DECL_CONTEXT (decl);
8378 /* If the immediate enclosing context of the current class is the
8379 field decl's class or inherits from it; build the access as
8380 `this$<n>.<field>'. Note that we will break the `private' barrier
8381 if we're not emitting bytecodes. */
8382 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8383 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8385 tree thisn = build_current_thisn (current_class);
8386 access = make_qualified_primary (build_wfl_node (thisn),
8387 id, EXPR_WFL_LINECOL (id));
8389 /* Otherwise, generate access methods to outer this and access the
8390 field (either using an access method or by direct access.) */
8391 else
8393 int lc = EXPR_WFL_LINECOL (id);
8395 /* Now we chain the required number of calls to the access$0 to
8396 get a hold to the enclosing instance we need, and then we
8397 build the field access. */
8398 access = build_access_to_thisn (current_class, decl_ctx, lc);
8400 /* If the field is private and we're generating bytecode, then
8401 we generate an access method */
8402 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8404 tree name = build_outer_field_access_methods (decl);
8405 access = build_outer_field_access_expr (lc, decl_ctx,
8406 name, access, NULL_TREE);
8408 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8409 Once again we break the `private' access rule from a foreign
8410 class. */
8411 else
8412 access = make_qualified_primary (access, id, lc);
8414 return resolve_expression_name (access, NULL);
8417 /* Return a nonzero value if NODE describes an outer field inner
8418 access. */
8420 static int
8421 outer_field_access_p (tree type, tree decl)
8423 if (!INNER_CLASS_TYPE_P (type)
8424 || TREE_CODE (decl) != FIELD_DECL
8425 || DECL_CONTEXT (decl) == type)
8426 return 0;
8428 /* If the inner class extends the declaration context of the field
8429 we're trying to access, then this isn't an outer field access */
8430 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8431 return 0;
8433 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8434 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8436 if (type == DECL_CONTEXT (decl))
8437 return 1;
8439 if (!DECL_CONTEXT (TYPE_NAME (type)))
8441 /* Before we give up, see whether the field is inherited from
8442 the enclosing context we're considering. */
8443 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8444 return 1;
8445 break;
8449 return 0;
8452 /* Return a nonzero value if NODE represents an outer field inner
8453 access that was been already expanded. As a side effect, it returns
8454 the name of the field being accessed and the argument passed to the
8455 access function, suitable for a regeneration of the access method
8456 call if necessary. */
8458 static int
8459 outer_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8460 tree *arg)
8462 int identified = 0;
8464 if (TREE_CODE (node) != CALL_EXPR)
8465 return 0;
8467 /* Well, gcj generates slightly different tree nodes when compiling
8468 to native or bytecodes. It's the case for function calls. */
8470 if (flag_emit_class_files
8471 && TREE_CODE (node) == CALL_EXPR
8472 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8473 identified = 1;
8474 else if (!flag_emit_class_files)
8476 node = TREE_OPERAND (node, 0);
8478 if (node && TREE_OPERAND (node, 0)
8479 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8481 node = TREE_OPERAND (node, 0);
8482 if (TREE_OPERAND (node, 0)
8483 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8484 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8485 (DECL_NAME (TREE_OPERAND (node, 0)))))
8486 identified = 1;
8490 if (identified && name && arg_type && arg)
8492 tree argument = TREE_OPERAND (node, 1);
8493 *name = DECL_NAME (TREE_OPERAND (node, 0));
8494 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8495 *arg = TREE_VALUE (argument);
8497 return identified;
8500 /* Detect in NODE an outer field read access from an inner class and
8501 transform it into a write with RHS as an argument. This function is
8502 called from the java_complete_lhs when an assignment to a LHS can
8503 be identified. */
8505 static tree
8506 outer_field_access_fix (tree wfl, tree node, tree rhs)
8508 tree name, arg_type, arg;
8510 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8512 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8513 arg_type, name, arg, rhs);
8514 return java_complete_tree (node);
8516 return NULL_TREE;
8519 /* Construct the expression that calls an access method:
8520 <type>.access$<n>(<arg1> [, <arg2>]);
8522 ARG2 can be NULL and will be omitted in that case. It will denote a
8523 read access. */
8525 static tree
8526 build_outer_field_access_expr (int lc, tree type, tree access_method_name,
8527 tree arg1, tree arg2)
8529 tree args, cn, access;
8531 args = arg1 ? arg1 :
8532 build_wfl_node (build_current_thisn (current_class));
8533 args = build_tree_list (NULL_TREE, args);
8535 if (arg2)
8536 args = tree_cons (NULL_TREE, arg2, args);
8538 access = build_method_invocation (build_wfl_node (access_method_name), args);
8539 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8540 return make_qualified_primary (cn, access, lc);
8543 static tree
8544 build_new_access_id (void)
8546 static int access_n_counter = 1;
8547 char buffer [128];
8549 sprintf (buffer, "access$%d", access_n_counter++);
8550 return get_identifier (buffer);
8553 /* Create the static access functions for the outer field DECL. We define a
8554 read:
8555 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8556 return inst$.field;
8558 and a write access:
8559 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8560 TREE_TYPE (<field>) value$) {
8561 return inst$.field = value$;
8563 We should have a usage flags on the DECL so we can lazily turn the ones
8564 we're using for code generation. FIXME.
8567 static tree
8568 build_outer_field_access_methods (tree decl)
8570 tree id, args, stmt, mdecl;
8572 if (FIELD_INNER_ACCESS_P (decl))
8573 return FIELD_INNER_ACCESS (decl);
8575 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8577 /* Create the identifier and a function named after it. */
8578 id = build_new_access_id ();
8580 /* The identifier is marked as bearing the name of a generated write
8581 access function for outer field accessed from inner classes. */
8582 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8584 /* Create the read access */
8585 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8586 TREE_CHAIN (args) = end_params_node;
8587 stmt = make_qualified_primary (build_wfl_node (inst_id),
8588 build_wfl_node (DECL_NAME (decl)), 0);
8589 stmt = build_return (0, stmt);
8590 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8591 TREE_TYPE (decl), id, args, stmt);
8592 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8594 /* Create the write access method. No write access for final variable */
8595 if (!FIELD_FINAL (decl))
8597 args = build_tree_list (inst_id,
8598 build_pointer_type (DECL_CONTEXT (decl)));
8599 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8600 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8601 stmt = make_qualified_primary (build_wfl_node (inst_id),
8602 build_wfl_node (DECL_NAME (decl)), 0);
8603 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8604 build_wfl_node (wpv_id)));
8605 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8606 TREE_TYPE (decl), id,
8607 args, stmt);
8609 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8611 /* Return the access name */
8612 return FIELD_INNER_ACCESS (decl) = id;
8615 /* Build an field access method NAME. */
8617 static tree
8618 build_outer_field_access_method (tree class, tree type, tree name,
8619 tree args, tree body)
8621 tree saved_current_function_decl, mdecl;
8623 /* Create the method */
8624 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8625 fix_method_argument_names (args, mdecl);
8626 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8628 /* Attach the method body. */
8629 saved_current_function_decl = current_function_decl;
8630 start_artificial_method_body (mdecl);
8631 java_method_add_stmt (mdecl, body);
8632 end_artificial_method_body (mdecl);
8633 current_function_decl = saved_current_function_decl;
8635 return mdecl;
8639 /* This section deals with building access function necessary for
8640 certain kinds of method invocation from inner classes. */
8642 static tree
8643 build_outer_method_access_method (tree decl)
8645 tree saved_current_function_decl, mdecl;
8646 tree args = NULL_TREE, call_args = NULL_TREE;
8647 tree carg, id, body, class;
8648 char buffer [80];
8649 int parm_id_count = 0;
8651 /* Test this abort with an access to a private field */
8652 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8653 abort ();
8655 /* Check the cache first */
8656 if (DECL_FUNCTION_INNER_ACCESS (decl))
8657 return DECL_FUNCTION_INNER_ACCESS (decl);
8659 class = DECL_CONTEXT (decl);
8661 /* Obtain an access identifier and mark it */
8662 id = build_new_access_id ();
8663 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8665 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8666 /* Create the arguments, as much as the original */
8667 for (; carg && carg != end_params_node;
8668 carg = TREE_CHAIN (carg))
8670 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8671 args = chainon (args, build_tree_list (get_identifier (buffer),
8672 TREE_VALUE (carg)));
8674 args = chainon (args, end_params_node);
8676 /* Create the method */
8677 mdecl = create_artificial_method (class, ACC_STATIC,
8678 TREE_TYPE (TREE_TYPE (decl)), id, args);
8679 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8680 /* There is a potential bug here. We should be able to use
8681 fix_method_argument_names, but then arg names get mixed up and
8682 eventually a constructor will have its this$0 altered and the
8683 outer context won't be assignment properly. The testcase is
8684 stub.java FIXME */
8685 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8687 /* Attach the method body. */
8688 saved_current_function_decl = current_function_decl;
8689 start_artificial_method_body (mdecl);
8691 /* The actual method invocation uses the same args. When invoking a
8692 static methods that way, we don't want to skip the first
8693 argument. */
8694 carg = args;
8695 if (!METHOD_STATIC (decl))
8696 carg = TREE_CHAIN (carg);
8697 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8698 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8699 call_args);
8701 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8702 call_args);
8703 if (!METHOD_STATIC (decl))
8704 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8705 body, 0);
8706 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8707 body = build_return (0, body);
8708 java_method_add_stmt (mdecl,body);
8709 end_artificial_method_body (mdecl);
8710 current_function_decl = saved_current_function_decl;
8712 /* Back tag the access function so it know what it accesses */
8713 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8715 /* Tag the current method so it knows it has an access generated */
8716 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8720 /* This section of the code deals with building expressions to access
8721 the enclosing instance of an inner class. The enclosing instance is
8722 kept in a generated field called this$<n>, with <n> being the
8723 inner class nesting level (starting from 0.) */
8725 /* Build an access to a given this$<n>, always chaining access call to
8726 others. Access methods to this$<n> are build on the fly if
8727 necessary. This CAN'T be used to solely access this$<n-1> from
8728 this$<n> (which alway yield to special cases and optimization, see
8729 for example build_outer_field_access). */
8731 static tree
8732 build_access_to_thisn (tree from, tree to, int lc)
8734 tree access = NULL_TREE;
8736 while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8738 if (!access)
8740 access = build_current_thisn (from);
8741 access = build_wfl_node (access);
8743 else
8745 tree access0_wfl, cn;
8747 maybe_build_thisn_access_method (from);
8748 access0_wfl = build_wfl_node (access0_identifier_node);
8749 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8750 EXPR_WFL_LINECOL (access0_wfl) = lc;
8751 access = build_tree_list (NULL_TREE, access);
8752 access = build_method_invocation (access0_wfl, access);
8753 access = make_qualified_primary (cn, access, lc);
8756 /* If FROM isn't an inner class, that's fine, we've done enough.
8757 What we're looking for can be accessed from there. */
8758 from = DECL_CONTEXT (TYPE_NAME (from));
8759 if (!from)
8760 break;
8761 from = TREE_TYPE (from);
8763 return access;
8766 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8767 is returned if nothing needs to be generated. Otherwise, the method
8768 generated and a method decl is returned.
8770 NOTE: These generated methods should be declared in a class file
8771 attribute so that they can't be referred to directly. */
8773 static tree
8774 maybe_build_thisn_access_method (tree type)
8776 tree mdecl, args, stmt, rtype;
8777 tree saved_current_function_decl;
8779 /* If TYPE is a top-level class, no access method is required.
8780 If there already is such an access method, bail out. */
8781 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8782 return NULL_TREE;
8784 /* We generate the method. The method looks like:
8785 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8787 args = build_tree_list (inst_id, build_pointer_type (type));
8788 TREE_CHAIN (args) = end_params_node;
8789 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8790 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8791 access0_identifier_node, args);
8792 fix_method_argument_names (args, mdecl);
8793 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8794 stmt = build_current_thisn (type);
8795 stmt = make_qualified_primary (build_wfl_node (inst_id),
8796 build_wfl_node (stmt), 0);
8797 stmt = build_return (0, stmt);
8799 saved_current_function_decl = current_function_decl;
8800 start_artificial_method_body (mdecl);
8801 java_method_add_stmt (mdecl, stmt);
8802 end_artificial_method_body (mdecl);
8803 current_function_decl = saved_current_function_decl;
8805 CLASS_ACCESS0_GENERATED_P (type) = 1;
8807 return mdecl;
8810 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8811 the first level of innerclassing. this$1 for the next one, etc...
8812 This function can be invoked with TYPE to NULL, available and then
8813 has to count the parser context. */
8815 static GTY(()) tree saved_thisn;
8816 static GTY(()) tree saved_type;
8818 static tree
8819 build_current_thisn (tree type)
8821 static int saved_i = -1;
8822 static int saved_type_i = 0;
8823 tree decl;
8824 char buffer [24];
8825 int i = 0;
8827 if (type)
8829 if (type == saved_type)
8830 i = saved_type_i;
8831 else
8833 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8834 decl; decl = DECL_CONTEXT (decl), i++)
8837 saved_type = type;
8838 saved_type_i = i;
8841 else
8842 i = list_length (GET_CPC_LIST ())-2;
8844 if (i == saved_i)
8845 return saved_thisn;
8847 sprintf (buffer, "this$%d", i);
8848 saved_i = i;
8849 saved_thisn = get_identifier (buffer);
8850 return saved_thisn;
8853 /* Return the assignment to the hidden enclosing context `this$<n>'
8854 by the second incoming parameter to the innerclass constructor. The
8855 form used is `this.this$<n> = this$<n>;'. */
8857 static tree
8858 build_thisn_assign (void)
8860 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8862 tree thisn = build_current_thisn (current_class);
8863 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8864 build_wfl_node (thisn), 0);
8865 tree rhs = build_wfl_node (thisn);
8866 #ifdef USE_MAPPED_LOCATION
8867 SET_EXPR_LOCATION (lhs, input_location);
8868 #else
8869 EXPR_WFL_SET_LINECOL (lhs, input_line, 0);
8870 #endif
8871 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8873 return NULL_TREE;
8877 /* Building the synthetic `class$' used to implement the `.class' 1.1
8878 extension for non primitive types. This method looks like:
8880 static Class class$(String type) throws NoClassDefFoundError
8882 try {return (java.lang.Class.forName (String));}
8883 catch (ClassNotFoundException e) {
8884 throw new NoClassDefFoundError(e.getMessage());}
8885 } */
8887 static GTY(()) tree get_message_wfl;
8888 static GTY(()) tree type_parm_wfl;
8890 static tree
8891 build_dot_class_method (tree class)
8893 #define BWF(S) build_wfl_node (get_identifier ((S)))
8894 #ifdef USE_MAPPED_LOCATION
8895 #define MQN(X,Y) make_qualified_name ((X), (Y), UNKNOWN_LOCATION)
8896 #else
8897 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8898 #endif
8899 tree args, tmp, saved_current_function_decl, mdecl, qual_name;
8900 tree stmt, throw_stmt;
8902 if (!get_message_wfl)
8904 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8905 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8908 /* Build the arguments */
8909 args = build_tree_list (get_identifier ("type$"),
8910 build_pointer_type (string_type_node));
8911 TREE_CHAIN (args) = end_params_node;
8913 /* Build the qualified name java.lang.Class.forName */
8914 tmp = MQN (MQN (MQN (BWF ("java"),
8915 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8917 /* Create the "class$" function */
8918 mdecl = create_artificial_method (class, ACC_STATIC,
8919 build_pointer_type (class_type_node),
8920 classdollar_identifier_node, args);
8921 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8922 BWF ("NoClassDefFoundError"));
8923 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE, qual_name);
8924 register_incomplete_type (JDEP_EXCEPTION, qual_name, NULL_TREE, NULL_TREE);
8925 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
8926 &TREE_VALUE (DECL_FUNCTION_THROWS (mdecl));
8928 /* We start by building the try block. We need to build:
8929 return (java.lang.Class.forName (type)); */
8930 stmt = build_method_invocation (tmp,
8931 build_tree_list (NULL_TREE, type_parm_wfl));
8932 stmt = build_return (0, stmt);
8934 /* Now onto the catch block. We start by building the expression
8935 throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8936 #ifdef USE_MAPPED_LOCATION
8937 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8938 get_message_wfl, UNKNOWN_LOCATION);
8939 #else
8940 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8941 get_message_wfl, 0);
8942 #endif
8943 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8945 /* Build new NoClassDefFoundError (_.getMessage) */
8946 throw_stmt = build_new_invocation
8947 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8948 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8950 /* Build the throw, (it's too early to use BUILD_THROW) */
8951 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8953 /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8954 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8955 BWF ("ClassNotFoundException"));
8956 stmt = encapsulate_with_try_catch (0, qual_name, stmt, throw_stmt);
8958 fix_method_argument_names (args, mdecl);
8959 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8960 saved_current_function_decl = current_function_decl;
8961 start_artificial_method_body (mdecl);
8962 java_method_add_stmt (mdecl, stmt);
8963 end_artificial_method_body (mdecl);
8964 current_function_decl = saved_current_function_decl;
8965 TYPE_DOT_CLASS (class) = mdecl;
8967 return mdecl;
8970 static tree
8971 build_dot_class_method_invocation (tree this_class, tree type)
8973 tree dot_class_method = TYPE_DOT_CLASS (this_class);
8974 tree sig_id, s, t;
8976 if (TYPE_ARRAY_P (type))
8977 sig_id = build_java_signature (type);
8978 else
8979 sig_id = DECL_NAME (TYPE_NAME (type));
8981 /* Ensure that the proper name separator is used */
8982 sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8983 IDENTIFIER_LENGTH (sig_id));
8985 s = build_string (IDENTIFIER_LENGTH (sig_id),
8986 IDENTIFIER_POINTER (sig_id));
8987 t = build_method_invocation (build_wfl_node (DECL_NAME (dot_class_method)),
8988 build_tree_list (NULL_TREE, s));
8989 if (DECL_CONTEXT (dot_class_method) != this_class)
8991 tree class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (dot_class_method)));
8992 t = make_qualified_primary (build_wfl_node (class_name), t, 0);
8994 return t;
8997 /* This section of the code deals with constructor. */
8999 /* Craft a body for default constructor. Patch existing constructor
9000 bodies with call to super() and field initialization statements if
9001 necessary. */
9003 static void
9004 fix_constructors (tree mdecl)
9006 tree iii; /* Instance Initializer Invocation */
9007 tree *bodyp = &DECL_FUNCTION_BODY (mdecl);
9008 tree thisn_assign, compound = NULL_TREE;
9009 tree class_type = DECL_CONTEXT (mdecl);
9011 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
9012 return;
9013 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
9015 if (!*bodyp)
9017 /* It is an error for the compiler to generate a default
9018 constructor if the superclass doesn't have a constructor that
9019 takes no argument, or the same args for an anonymous class */
9020 if (verify_constructor_super (mdecl))
9022 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
9023 tree save = DECL_NAME (mdecl);
9024 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
9025 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
9026 parse_error_context
9027 (lookup_cl (TYPE_NAME (class_type)),
9028 "No constructor matching %qs found in class %qs",
9029 lang_printable_name (mdecl, 2), n);
9030 DECL_NAME (mdecl) = save;
9033 /* The constructor body must be crafted by hand. It's the
9034 constructor we defined when we realize we didn't have the
9035 CLASSNAME() constructor */
9036 start_artificial_method_body (mdecl);
9038 /* Insert an assignment to the this$<n> hidden field, if
9039 necessary */
9040 if ((thisn_assign = build_thisn_assign ()))
9041 java_method_add_stmt (mdecl, thisn_assign);
9043 /* We don't generate a super constructor invocation if we're
9044 compiling java.lang.Object. build_super_invocation takes care
9045 of that. */
9046 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
9048 /* FIXME */
9049 if ((iii = build_instinit_invocation (class_type)))
9050 java_method_add_stmt (mdecl, iii);
9052 end_artificial_method_body (mdecl);
9054 /* Search for an explicit constructor invocation */
9055 else
9057 int found = 0;
9058 int invokes_this = 0;
9059 tree main_block = BLOCK_EXPR_BODY (*bodyp);
9061 while (*bodyp)
9063 tree body = *bodyp;
9064 switch (TREE_CODE (body))
9066 case CALL_EXPR:
9067 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
9068 if (CALL_THIS_CONSTRUCTOR_P (body))
9069 invokes_this = 1;
9070 break;
9071 case COMPOUND_EXPR:
9072 case EXPR_WITH_FILE_LOCATION:
9073 bodyp = &TREE_OPERAND (body, 0);
9074 continue;
9075 case BLOCK:
9076 bodyp = &BLOCK_EXPR_BODY (body);
9077 continue;
9078 default:
9079 break;
9081 break;
9084 /* Generate the assignment to this$<n>, if necessary */
9085 if ((thisn_assign = build_thisn_assign ()))
9086 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
9088 /* The constructor is missing an invocation of super() */
9089 if (!found)
9090 compound = add_stmt_to_compound (compound, NULL_TREE,
9091 build_super_invocation (mdecl));
9092 /* Explicit super() invocation should take place before the
9093 instance initializer blocks. */
9094 else
9096 compound = add_stmt_to_compound (compound, NULL_TREE, *bodyp);
9097 *bodyp = build_java_empty_stmt ();
9100 DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
9102 /* Insert the instance initializer block right after. */
9103 if (!invokes_this && (iii = build_instinit_invocation (class_type)))
9104 compound = add_stmt_to_compound (compound, NULL_TREE, iii);
9106 /* Fix the constructor main block if we're adding extra stmts */
9107 if (compound)
9109 compound = add_stmt_to_compound (compound, NULL_TREE,
9110 BLOCK_EXPR_BODY (main_block));
9111 BLOCK_EXPR_BODY (main_block) = compound;
9116 /* Browse constructors in the super class, searching for a constructor
9117 that doesn't take any argument. Return 0 if one is found, 1
9118 otherwise. If the current class is an anonymous inner class, look
9119 for something that has the same signature. */
9121 static int
9122 verify_constructor_super (tree mdecl)
9124 tree class = CLASSTYPE_SUPER (current_class);
9125 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
9126 tree sdecl;
9128 if (!class)
9129 return 0;
9131 if (ANONYMOUS_CLASS_P (current_class))
9133 tree mdecl_arg_type;
9134 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
9135 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9136 if (DECL_CONSTRUCTOR_P (sdecl))
9138 tree m_arg_type;
9139 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9140 if (super_inner)
9141 arg_type = TREE_CHAIN (arg_type);
9142 for (m_arg_type = mdecl_arg_type;
9143 (arg_type != end_params_node
9144 && m_arg_type != end_params_node);
9145 arg_type = TREE_CHAIN (arg_type),
9146 m_arg_type = TREE_CHAIN (m_arg_type))
9147 if (!valid_method_invocation_conversion_p
9148 (TREE_VALUE (arg_type),
9149 TREE_VALUE (m_arg_type)))
9150 break;
9152 if (arg_type == end_params_node && m_arg_type == end_params_node)
9153 return 0;
9156 else
9158 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9160 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9161 if (super_inner)
9162 arg = TREE_CHAIN (arg);
9163 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
9164 return 0;
9167 return 1;
9170 /* Generate code for all context remembered for code generation. */
9172 static GTY(()) tree reversed_class_list;
9173 void
9174 java_expand_classes (void)
9176 int save_error_count = 0;
9177 static struct parser_ctxt *cur_ctxp = NULL;
9178 location_t save_location;
9180 java_parse_abort_on_error ();
9181 if (!(ctxp = ctxp_for_generation))
9182 return;
9183 java_layout_classes ();
9184 java_parse_abort_on_error ();
9185 save_location = input_location;
9187 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9189 tree current;
9190 for (current = cur_ctxp->class_list;
9191 current;
9192 current = TREE_CHAIN (current))
9193 gen_indirect_dispatch_tables (TREE_TYPE (current));
9196 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9198 ctxp = cur_ctxp;
9199 input_location = ctxp->file_start_location;
9200 lang_init_source (2); /* Error msgs have method prototypes */
9201 java_complete_expand_classes (); /* Complete and expand classes */
9202 java_parse_abort_on_error ();
9204 input_location = save_location;
9206 /* Find anonymous classes and expand their constructor. This extra pass is
9207 necessary because the constructor itself is only generated when the
9208 method in which it is defined is expanded. */
9209 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9211 tree current;
9212 ctxp = cur_ctxp;
9213 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9215 output_class = current_class = TREE_TYPE (current);
9216 if (ANONYMOUS_CLASS_P (current_class))
9218 tree d;
9219 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9221 if (DECL_CONSTRUCTOR_P (d))
9223 java_complete_expand_method (d);
9224 break; /* There is only one constructor. */
9231 /* Expanding the constructors of anonymous classes generates access
9232 methods. Scan all the methods looking for null DECL_RESULTs --
9233 this will be the case if a method hasn't been expanded. */
9234 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9236 tree current;
9237 ctxp = cur_ctxp;
9238 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9240 tree d;
9241 output_class = current_class = TREE_TYPE (current);
9242 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9244 if (DECL_RESULT (d) == NULL_TREE)
9245 java_complete_expand_method (d);
9250 /* ??? Instead of all this we could iterate around the list of
9251 classes until there were no more un-expanded methods. It would
9252 take a little longer -- one pass over the whole list of methods
9253 -- but it would be simpler. Like this: */
9254 #if 0
9256 int something_changed;
9260 something_changed = 0;
9261 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9263 tree current;
9264 ctxp = cur_ctxp;
9265 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9267 tree d;
9268 output_class = current_class = TREE_TYPE (current);
9269 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9271 if (DECL_RESULT (d) == NULL_TREE)
9273 something_changed = 1;
9274 java_complete_expand_method (d);
9280 while (something_changed);
9282 #endif
9284 /* If we've found error at that stage, don't try to generate
9285 anything, unless we're emitting xrefs or checking the syntax only
9286 (but not using -fsyntax-only for the purpose of generating
9287 bytecode. */
9288 if (java_error_count && !flag_emit_xref
9289 && (!flag_syntax_only && !flag_emit_class_files))
9290 return;
9292 /* Now things are stable, go for generation of the class data. */
9294 /* We pessimistically marked all methods and fields external until
9295 we knew what set of classes we were planning to compile. Now mark
9296 those that will be generated locally as not external. */
9297 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9299 tree current;
9300 ctxp = cur_ctxp;
9301 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9302 java_mark_class_local (TREE_TYPE (current));
9305 /* Compile the classes. */
9306 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9308 tree current;
9309 reversed_class_list = NULL;
9311 ctxp = cur_ctxp;
9313 /* We write out the classes in reverse order. This ensures that
9314 inner classes are written before their containing classes,
9315 which is important for parallel builds. Otherwise, the
9316 class file for the outer class may be found, but the class
9317 file for the inner class may not be present. In that
9318 situation, the compiler cannot fall back to the original
9319 source, having already read the outer class, so we must
9320 prevent that situation. */
9321 for (current = ctxp->class_list;
9322 current;
9323 current = TREE_CHAIN (current))
9324 reversed_class_list
9325 = tree_cons (NULL_TREE, current, reversed_class_list);
9327 for (current = reversed_class_list;
9328 current;
9329 current = TREE_CHAIN (current))
9331 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
9332 if (flag_emit_class_files)
9333 write_classfile (current_class);
9334 if (flag_emit_xref)
9335 expand_xref (current_class);
9336 else if (! flag_syntax_only)
9337 java_expand_method_bodies (current_class);
9342 void
9343 java_finish_classes (void)
9345 static struct parser_ctxt *cur_ctxp = NULL;
9346 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9348 tree current;
9349 ctxp = cur_ctxp;
9350 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9352 output_class = current_class = TREE_TYPE (current);
9353 finish_class ();
9358 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9359 a tree list node containing RIGHT. Fore coming RIGHTs will be
9360 chained to this hook. LOCATION contains the location of the
9361 separating `.' operator. */
9363 static tree
9364 make_qualified_primary (tree primary, tree right, int location)
9366 tree wfl;
9368 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9369 wfl = build_wfl_wrap (primary, location);
9370 else
9372 wfl = primary;
9373 /* If wfl wasn't qualified, we build a first anchor */
9374 if (!EXPR_WFL_QUALIFICATION (wfl))
9375 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9378 /* And chain them */
9379 EXPR_WFL_LINECOL (right) = location;
9380 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9381 PRIMARY_P (wfl) = 1;
9382 return wfl;
9385 /* Simple merge of two name separated by a `.' */
9387 static tree
9388 merge_qualified_name (tree left, tree right)
9390 tree node;
9391 if (!left && !right)
9392 return NULL_TREE;
9394 if (!left)
9395 return right;
9397 if (!right)
9398 return left;
9400 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9401 IDENTIFIER_LENGTH (left));
9402 obstack_1grow (&temporary_obstack, '.');
9403 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9404 IDENTIFIER_LENGTH (right));
9405 node = get_identifier (obstack_base (&temporary_obstack));
9406 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9407 QUALIFIED_P (node) = 1;
9408 return node;
9411 /* Merge the two parts of a qualified name into LEFT. Set the
9412 location information of the resulting node to LOCATION, usually
9413 inherited from the location information of the `.' operator. */
9415 static tree
9416 make_qualified_name (tree left, tree right,
9417 #ifdef USE_MAPPED_LOCATION
9418 source_location location
9419 #else
9420 int location
9421 #endif
9424 #ifdef USE_COMPONENT_REF
9425 tree node = build3 (COMPONENT_REF, NULL_TREE, left, right, NULL_TREE);
9426 SET_EXPR_LOCATION (node, location);
9427 return node;
9428 #else
9429 tree left_id = EXPR_WFL_NODE (left);
9430 tree right_id = EXPR_WFL_NODE (right);
9431 tree wfl, merge;
9433 merge = merge_qualified_name (left_id, right_id);
9435 /* Left wasn't qualified and is now qualified */
9436 #ifdef USE_MAPPED_LOCATION
9437 if (!QUALIFIED_P (left_id))
9439 tree wfl = build_expr_wfl (left_id, EXPR_LOCATION (left));
9440 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9443 wfl = build_expr_wfl (right_id, location);
9444 #else
9445 if (!QUALIFIED_P (left_id))
9447 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9448 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9449 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9452 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9453 EXPR_WFL_LINECOL (wfl) = location;
9454 #endif
9455 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9456 EXPR_WFL_NODE (left) = merge;
9457 return left;
9458 #endif
9461 /* Extract the last identifier component of the qualified in WFL. The
9462 last identifier is removed from the linked list */
9464 static tree
9465 cut_identifier_in_qualified (tree wfl)
9467 tree q;
9468 tree previous = NULL_TREE;
9469 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9470 if (!TREE_CHAIN (q))
9472 if (!previous)
9473 /* Operating on a non qualified qualified WFL. */
9474 abort ();
9476 TREE_CHAIN (previous) = NULL_TREE;
9477 return TREE_PURPOSE (q);
9481 /* Resolve the expression name NAME. Return its decl. */
9483 static tree
9484 resolve_expression_name (tree id, tree *orig)
9486 tree name = EXPR_WFL_NODE (id);
9487 tree decl;
9489 /* 6.5.5.1: Simple expression names */
9490 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9492 /* 15.13.1: NAME can appear within the scope of a local variable
9493 declaration */
9494 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9495 return decl;
9497 /* 15.13.1: NAME can appear within a class declaration */
9498 else
9500 decl = lookup_field_wrapper (current_class, name);
9501 if (decl)
9503 tree access = NULL_TREE;
9504 int fs = FIELD_STATIC (decl);
9506 /* If we're accessing an outer scope local alias, make
9507 sure we change the name of the field we're going to
9508 build access to. */
9509 if (FIELD_LOCAL_ALIAS_USED (decl))
9510 name = DECL_NAME (decl);
9512 check_deprecation (id, decl);
9514 /* Instance variable (8.3.1.1) can't appear within
9515 static method, static initializer or initializer for
9516 a static variable. */
9517 if (!fs && METHOD_STATIC (current_function_decl))
9519 static_ref_err (id, name, current_class);
9520 return error_mark_node;
9522 /* Instance variables can't appear as an argument of
9523 an explicit constructor invocation */
9524 if (!fs && ctxp->explicit_constructor_p
9525 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9527 parse_error_context
9528 (id, "Can't reference %qs before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9529 return error_mark_node;
9532 /* If we're processing an inner class and we're trying
9533 to access a field belonging to an outer class, build
9534 the access to the field */
9535 if (!fs && outer_field_access_p (current_class, decl))
9537 if (CLASS_STATIC (TYPE_NAME (current_class)))
9539 static_ref_err (id, DECL_NAME (decl), current_class);
9540 return error_mark_node;
9542 access = build_outer_field_access (id, decl);
9543 if (orig)
9544 *orig = access;
9545 return access;
9548 /* Otherwise build what it takes to access the field */
9549 access = build_field_ref ((fs ? NULL_TREE : current_this),
9550 DECL_CONTEXT (decl), name);
9551 if (fs)
9552 access = maybe_build_class_init_for_field (decl, access);
9553 /* We may be asked to save the real field access node */
9554 if (orig)
9555 *orig = access;
9556 /* Last check: can we access the field? */
9557 if (not_accessible_p (current_class, decl, NULL_TREE, 0))
9559 not_accessible_field_error (id, decl);
9560 return error_mark_node;
9562 /* And we return what we got */
9563 return access;
9565 /* Fall down to error report on undefined variable */
9568 /* 6.5.5.2 Qualified Expression Names */
9569 else
9571 if (orig)
9572 *orig = NULL_TREE;
9573 qualify_ambiguous_name (id);
9574 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9575 /* 15.10.2: Accessing Superclass Members using super */
9576 return resolve_field_access (id, orig, NULL);
9579 /* We've got an error here */
9580 if (INNER_CLASS_TYPE_P (current_class))
9581 parse_error_context (id,
9582 "Local variable %qs can't be accessed from within the inner class %qs unless it is declared final",
9583 IDENTIFIER_POINTER (name),
9584 IDENTIFIER_POINTER (DECL_NAME
9585 (TYPE_NAME (current_class))));
9586 else
9587 parse_error_context (id, "Undefined variable %qs",
9588 IDENTIFIER_POINTER (name));
9590 return error_mark_node;
9593 static void
9594 static_ref_err (tree wfl, tree field_id, tree class_type)
9596 parse_error_context
9597 (wfl,
9598 "Can't make a static reference to nonstatic variable %qs in class %qs",
9599 IDENTIFIER_POINTER (field_id),
9600 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9603 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9604 We return something suitable to generate the field access. We also
9605 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9606 recipient's address can be null. */
9608 static tree
9609 resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9611 int is_static = 0;
9612 tree field_ref;
9613 tree decl = NULL_TREE, where_found, type_found;
9615 if (resolve_qualified_expression_name (qual_wfl, &decl,
9616 &where_found, &type_found))
9617 return error_mark_node;
9619 /* Resolve the LENGTH field of an array here */
9620 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9621 && type_found && TYPE_ARRAY_P (type_found)
9622 && ! flag_emit_class_files && ! flag_emit_xref)
9624 tree length = build_java_array_length_access (where_found);
9625 field_ref = length;
9627 /* In case we're dealing with a static array, we need to
9628 initialize its class before the array length can be fetched.
9629 It's also a good time to create a DECL_RTL for the field if
9630 none already exists, otherwise if the field was declared in a
9631 class found in an external file and hasn't been (and won't
9632 be) accessed for its value, none will be created. */
9633 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9635 build_static_field_ref (where_found);
9636 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9639 /* We might have been trying to resolve field.method(). In which
9640 case, the resolution is over and decl is the answer */
9641 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9642 field_ref = decl;
9643 else if (JDECL_P (decl))
9645 if (!type_found)
9646 type_found = DECL_CONTEXT (decl);
9647 is_static = FIELD_STATIC (decl);
9648 field_ref = build_field_ref ((is_static && !flag_emit_xref?
9649 NULL_TREE : where_found),
9650 type_found, DECL_NAME (decl));
9651 if (field_ref == error_mark_node)
9652 return error_mark_node;
9653 if (is_static)
9654 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9656 /* If we're looking at a static field, we may need to generate a
9657 class initialization for it. This can happen when the access
9658 looks like `field.ref', where `field' is a static field in an
9659 interface we implement. */
9660 if (!flag_emit_class_files
9661 && !flag_emit_xref
9662 && TREE_CODE (where_found) == VAR_DECL
9663 && FIELD_STATIC (where_found))
9665 build_static_field_ref (where_found);
9666 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9669 else
9670 field_ref = decl;
9672 if (field_decl)
9673 *field_decl = decl;
9674 if (field_type)
9675 *field_type = (QUAL_DECL_TYPE (decl) ?
9676 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9677 return field_ref;
9680 /* If NODE is an access to a static field, strip out the class
9681 initialization part and return the field decl, otherwise, return
9682 NODE. */
9684 tree
9685 extract_field_decl (tree node)
9687 if (TREE_CODE (node) == COMPOUND_EXPR)
9689 tree op1 = TREE_OPERAND (node, 1);
9690 if (TREE_CODE (op1) == COMPOUND_EXPR)
9692 tree call = TREE_OPERAND (op1, 0);
9693 if (TREE_CODE (call) == CALL_EXPR
9694 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9695 && (TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9696 == soft_initclass_node))
9697 return TREE_OPERAND (op1, 1);
9699 else if (JDECL_P (op1))
9700 return op1;
9702 return node;
9705 /* 6.5.5.2: Qualified Expression Names */
9707 static int
9708 resolve_qualified_expression_name (tree wfl, tree *found_decl,
9709 tree *where_found, tree *type_found)
9711 int from_type = 0; /* Field search initiated from a type */
9712 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9713 int previous_call_static = 0;
9714 int is_static;
9715 tree decl = NULL_TREE, type = NULL_TREE, q;
9716 /* For certain for of inner class instantiation */
9717 tree saved_current, saved_this;
9718 #define RESTORE_THIS_AND_CURRENT_CLASS \
9719 { current_class = saved_current; current_this = saved_this;}
9721 *type_found = *where_found = NULL_TREE;
9723 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9725 tree qual_wfl = QUAL_WFL (q);
9726 tree ret_decl; /* for EH checking */
9727 #ifdef USE_MAPPED_LOCATION
9728 source_location location; /* for EH checking */
9729 #else
9730 int location; /* for EH checking */
9731 #endif
9733 /* 15.10.1 Field Access Using a Primary */
9734 switch (TREE_CODE (qual_wfl))
9736 case CALL_EXPR:
9737 case NEW_CLASS_EXPR:
9738 /* If the access to the function call is a non static field,
9739 build the code to access it. */
9740 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9742 decl = maybe_access_field (decl, *where_found,
9743 DECL_CONTEXT (decl));
9744 if (decl == error_mark_node)
9745 return 1;
9748 /* And code for the function call */
9749 if (complete_function_arguments (qual_wfl))
9750 return 1;
9752 /* We might have to setup a new current class and a new this
9753 for the search of an inner class, relative to the type of
9754 a expression resolved as `decl'. The current values are
9755 saved and restored shortly after */
9756 saved_current = current_class;
9757 saved_this = current_this;
9758 if (decl
9759 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9760 || from_qualified_this))
9762 /* If we still have `from_qualified_this', we have the form
9763 <T>.this.f() and we need to build <T>.this */
9764 if (from_qualified_this)
9766 decl = build_access_to_thisn (current_class, type, 0);
9767 decl = java_complete_tree (decl);
9768 type = TREE_TYPE (TREE_TYPE (decl));
9770 current_class = type;
9771 current_this = decl;
9772 from_qualified_this = 0;
9775 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9776 CALL_USING_SUPER (qual_wfl) = 1;
9777 #ifdef USE_MAPPED_LOCATION
9778 location = (TREE_CODE (qual_wfl) == CALL_EXPR
9779 ? EXPR_LOCATION (TREE_OPERAND (qual_wfl, 0))
9780 : UNKNOWN_LOCATION);
9781 #else
9782 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9783 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9784 #endif
9785 *where_found = patch_method_invocation (qual_wfl, decl, type,
9786 from_super,
9787 &is_static, &ret_decl);
9788 from_super = 0;
9789 if (*where_found == error_mark_node)
9791 RESTORE_THIS_AND_CURRENT_CLASS;
9792 return 1;
9794 *type_found = type = QUAL_DECL_TYPE (*where_found);
9796 *where_found = force_evaluation_order (*where_found);
9798 /* If we're creating an inner class instance, check for that
9799 an enclosing instance is in scope */
9800 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9801 && INNER_ENCLOSING_SCOPE_CHECK (type))
9803 parse_error_context
9804 (qual_wfl, "No enclosing instance for inner class %qs is in scope%s",
9805 lang_printable_name (type, 0),
9806 (!current_this ? "" :
9807 "; an explicit one must be provided when creating this inner class"));
9808 RESTORE_THIS_AND_CURRENT_CLASS;
9809 return 1;
9812 /* In case we had to change then to resolve a inner class
9813 instantiation using a primary qualified by a `new' */
9814 RESTORE_THIS_AND_CURRENT_CLASS;
9816 #ifdef USE_MAPPED_LOCATION
9817 if (location != UNKNOWN_LOCATION)
9818 #else
9819 if (location)
9820 #endif
9822 tree arguments = NULL_TREE;
9823 if (TREE_CODE (qual_wfl) == CALL_EXPR
9824 && TREE_OPERAND (qual_wfl, 1) != NULL_TREE)
9825 arguments = TREE_VALUE (TREE_OPERAND (qual_wfl, 1));
9826 check_thrown_exceptions (location, ret_decl, arguments);
9829 /* If the previous call was static and this one is too,
9830 build a compound expression to hold the two (because in
9831 that case, previous function calls aren't transported as
9832 forcoming function's argument. */
9833 if (previous_call_static && is_static)
9835 /* We must set CAN_COMPLETE_NORMALLY for the first call
9836 since it is done nowhere else. */
9837 CAN_COMPLETE_NORMALLY (decl) = 1;
9838 decl = build2 (COMPOUND_EXPR, TREE_TYPE (*where_found),
9839 decl, *where_found);
9840 TREE_SIDE_EFFECTS (decl) = 1;
9842 else
9844 previous_call_static = is_static;
9845 decl = *where_found;
9847 from_type = 0;
9848 continue;
9850 case NEW_ARRAY_EXPR:
9851 case NEW_ANONYMOUS_ARRAY_EXPR:
9852 *where_found = decl = java_complete_tree (qual_wfl);
9853 if (decl == error_mark_node)
9854 return 1;
9855 *type_found = type = QUAL_DECL_TYPE (decl);
9856 continue;
9858 case CONVERT_EXPR:
9859 *where_found = decl = java_complete_tree (qual_wfl);
9860 if (decl == error_mark_node)
9861 return 1;
9862 *type_found = type = QUAL_DECL_TYPE (decl);
9863 from_cast = 1;
9864 continue;
9866 case CONDITIONAL_EXPR:
9867 case STRING_CST:
9868 case MODIFY_EXPR:
9869 *where_found = decl = java_complete_tree (qual_wfl);
9870 if (decl == error_mark_node)
9871 return 1;
9872 *type_found = type = QUAL_DECL_TYPE (decl);
9873 continue;
9875 case ARRAY_REF:
9876 /* If the access to the function call is a non static field,
9877 build the code to access it. */
9878 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9880 decl = maybe_access_field (decl, *where_found, type);
9881 if (decl == error_mark_node)
9882 return 1;
9884 /* And code for the array reference expression */
9885 decl = java_complete_tree (qual_wfl);
9886 if (decl == error_mark_node)
9887 return 1;
9888 type = QUAL_DECL_TYPE (decl);
9889 continue;
9891 case PLUS_EXPR:
9892 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9893 return 1;
9894 if ((type = patch_string (decl)))
9895 decl = type;
9896 *where_found = QUAL_RESOLUTION (q) = decl;
9897 *type_found = type = TREE_TYPE (decl);
9898 break;
9900 case CLASS_LITERAL:
9901 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9902 return 1;
9903 *where_found = QUAL_RESOLUTION (q) = decl;
9904 *type_found = type = TREE_TYPE (decl);
9905 break;
9907 default:
9908 /* Fix for -Wall Just go to the next statement. Don't
9909 continue */
9910 break;
9913 /* If we fall here, we weren't processing a (static) function call. */
9914 previous_call_static = 0;
9916 /* It can be the keyword THIS */
9917 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9918 && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9920 if (!current_this)
9922 parse_error_context
9923 (wfl, "Keyword %<this%> used outside allowed context");
9924 return 1;
9926 if (ctxp->explicit_constructor_p
9927 && type == current_class)
9929 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
9930 return 1;
9932 /* We have to generate code for intermediate access */
9933 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9935 *where_found = decl = current_this;
9936 *type_found = type = QUAL_DECL_TYPE (decl);
9938 /* We're trying to access the this from somewhere else. Make sure
9939 it's allowed before doing so. */
9940 else
9942 if (!enclosing_context_p (type, current_class))
9944 char *p = xstrdup (lang_printable_name (type, 0));
9945 parse_error_context (qual_wfl, "Can't use variable %<%s.this%>: type %qs isn't an outer type of type %qs",
9946 p, p,
9947 lang_printable_name (current_class, 0));
9948 free (p);
9949 return 1;
9951 from_qualified_this = 1;
9952 /* If there's nothing else after that, we need to
9953 produce something now, otherwise, the section of the
9954 code that needs to produce <T>.this will generate
9955 what is necessary. */
9956 if (!TREE_CHAIN (q))
9958 decl = build_access_to_thisn (current_class, type, 0);
9959 *where_found = decl = java_complete_tree (decl);
9960 *type_found = type = TREE_TYPE (decl);
9964 from_type = 0;
9965 continue;
9968 /* 15.10.2 Accessing Superclass Members using SUPER */
9969 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9970 && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9972 tree node;
9973 /* Check on the restricted use of SUPER */
9974 if (METHOD_STATIC (current_function_decl)
9975 || current_class == object_type_node)
9977 parse_error_context
9978 (wfl, "Keyword %<super%> used outside allowed context");
9979 return 1;
9981 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9982 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9983 CLASSTYPE_SUPER (current_class),
9984 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9985 *where_found = decl = java_complete_tree (node);
9986 if (decl == error_mark_node)
9987 return 1;
9988 *type_found = type = QUAL_DECL_TYPE (decl);
9989 from_super = from_type = 1;
9990 continue;
9993 /* 15.13.1: Can't search for field name in packages, so we
9994 assume a variable/class name was meant. */
9995 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9997 tree name;
9998 if ((decl = resolve_package (wfl, &q, &name)))
10000 tree list;
10001 *where_found = decl;
10003 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10005 /* We want to be absolutely sure that the class is laid
10006 out. We're going to search something inside it. */
10007 *type_found = type = TREE_TYPE (decl);
10008 layout_class (type);
10009 from_type = 1;
10011 /* Fix them all the way down, if any are left. */
10012 if (q)
10014 list = TREE_CHAIN (q);
10015 while (list)
10017 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
10018 list = TREE_CHAIN (list);
10022 else
10024 if (from_super || from_cast)
10025 parse_error_context
10026 ((from_cast ? qual_wfl : wfl),
10027 "No variable %qs defined in class %qs",
10028 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10029 lang_printable_name (type, 0));
10030 else
10031 parse_error_context
10032 (qual_wfl, "Undefined variable or class name: %qs",
10033 IDENTIFIER_POINTER (name));
10034 return 1;
10038 /* We have a type name. It's been already resolved when the
10039 expression was qualified. */
10040 else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
10042 decl = QUAL_RESOLUTION (q);
10044 /* Sneak preview. If next we see a `new', we're facing a
10045 qualification which resulted in a type being selected
10046 instead of a field. Report the error. */
10047 if(TREE_CHAIN (q)
10048 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
10050 parse_error_context (qual_wfl, "Undefined variable %qs",
10051 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10052 return 1;
10055 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10057 check_deprecation (qual_wfl, decl);
10059 type = TREE_TYPE (decl);
10060 from_type = 1;
10062 /* We resolve an expression name */
10063 else
10065 tree field_decl = NULL_TREE;
10067 /* If there exists an early resolution, use it. That occurs
10068 only once and we know that there are more things to
10069 come. Don't do that when processing something after SUPER
10070 (we need more thing to be put in place below */
10071 if (!from_super && QUAL_RESOLUTION (q))
10073 decl = QUAL_RESOLUTION (q);
10074 if (!type)
10076 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
10078 if (current_this)
10079 *where_found = current_this;
10080 else
10082 static_ref_err (qual_wfl, DECL_NAME (decl),
10083 current_class);
10084 return 1;
10086 if (outer_field_access_p (current_class, decl))
10087 decl = build_outer_field_access (qual_wfl, decl);
10089 else
10091 *where_found = TREE_TYPE (decl);
10092 if (TREE_CODE (*where_found) == POINTER_TYPE)
10093 *where_found = TREE_TYPE (*where_found);
10098 /* Report and error if we're using a numerical literal as a
10099 qualifier. It can only be an INTEGER_CST. */
10100 else if (TREE_CODE (qual_wfl) == INTEGER_CST)
10102 parse_error_context
10103 (wfl, "Can't use type %qs as a qualifier",
10104 lang_printable_name (TREE_TYPE (qual_wfl), 0));
10105 return 1;
10108 /* We have to search for a field, knowing the type of its
10109 container. The flag FROM_TYPE indicates that we resolved
10110 the last member of the expression as a type name, which
10111 means that for the resolution of this field, we'll look
10112 for other errors than if it was resolved as a member of
10113 an other field. */
10114 else
10116 int is_static;
10117 tree field_decl_type; /* For layout */
10119 if (!from_type && !JREFERENCE_TYPE_P (type))
10121 parse_error_context
10122 (qual_wfl, "Attempt to reference field %qs in %<%s %s%>",
10123 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10124 lang_printable_name (type, 0),
10125 IDENTIFIER_POINTER (DECL_NAME (decl)));
10126 return 1;
10129 field_decl = lookup_field_wrapper (type,
10130 EXPR_WFL_NODE (qual_wfl));
10132 /* Maybe what we're trying to access to is an inner
10133 class, only if decl is a TYPE_DECL. */
10134 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
10136 tree ptr, inner_decl;
10138 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
10139 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
10140 if (inner_decl)
10142 check_inner_class_access (inner_decl, decl, qual_wfl);
10143 type = TREE_TYPE (inner_decl);
10144 decl = inner_decl;
10145 from_type = 1;
10146 continue;
10150 if (field_decl == NULL_TREE)
10152 parse_error_context
10153 (qual_wfl, "No variable %qs defined in type %qs",
10154 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10155 GET_TYPE_NAME (type));
10156 return 1;
10158 if (field_decl == error_mark_node)
10159 return 1;
10161 /* Layout the type of field_decl, since we may need
10162 it. Don't do primitive types or loaded classes. The
10163 situation of non primitive arrays may not handled
10164 properly here. FIXME */
10165 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10166 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
10167 else
10168 field_decl_type = TREE_TYPE (field_decl);
10169 if (!JPRIMITIVE_TYPE_P (field_decl_type)
10170 && !CLASS_LOADED_P (field_decl_type)
10171 && !TYPE_ARRAY_P (field_decl_type))
10172 resolve_and_layout (field_decl_type, NULL_TREE);
10174 /* Check on accessibility here */
10175 if (not_accessible_p (current_class, field_decl,
10176 *type_found, from_super))
10177 return not_accessible_field_error (qual_wfl,field_decl);
10178 check_deprecation (qual_wfl, field_decl);
10180 /* There are things to check when fields are accessed
10181 from type. There are no restrictions on a static
10182 declaration of the field when it is accessed from an
10183 interface */
10184 is_static = FIELD_STATIC (field_decl);
10185 if (!from_super && from_type
10186 && !TYPE_INTERFACE_P (type)
10187 && !is_static
10188 && (current_function_decl
10189 && METHOD_STATIC (current_function_decl)))
10191 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
10192 return 1;
10194 from_cast = from_super = 0;
10196 /* It's an access from a type but it isn't static, we
10197 make it relative to `this'. */
10198 if (!is_static && from_type)
10199 decl = current_this;
10201 /* If we need to generate something to get a proper
10202 handle on what this field is accessed from, do it
10203 now. */
10204 if (!is_static)
10206 decl = maybe_access_field (decl, *where_found, *type_found);
10207 if (decl == error_mark_node)
10208 return 1;
10211 /* We want to keep the location were found it, and the type
10212 we found. */
10213 *where_found = decl;
10214 *type_found = type;
10216 /* Generate the correct expression for field access from
10217 qualified this */
10218 if (from_qualified_this)
10220 field_decl = build_outer_field_access (qual_wfl, field_decl);
10221 from_qualified_this = 0;
10224 /* This is the decl found and eventually the next one to
10225 search from */
10226 decl = field_decl;
10228 from_type = 0;
10229 type = QUAL_DECL_TYPE (decl);
10231 /* Sneak preview. If decl is qualified by a `new', report
10232 the error here to be accurate on the peculiar construct */
10233 if (TREE_CHAIN (q)
10234 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
10235 && !JREFERENCE_TYPE_P (type))
10237 parse_error_context (qual_wfl, "Attempt to reference field %<new%> in a %qs",
10238 lang_printable_name (type, 0));
10239 return 1;
10242 /* `q' might have changed due to a after package resolution
10243 re-qualification */
10244 if (!q)
10245 break;
10247 *found_decl = decl;
10248 return 0;
10251 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
10252 can't be accessed from REFERENCE (a record type). If MEMBER
10253 features a protected access, we then use WHERE which, if non null,
10254 holds the type of MEMBER's access that is checked against
10255 6.6.2.1. This function should be used when decl is a field or a
10256 method. */
10258 static int
10259 not_accessible_p (tree reference, tree member, tree where, int from_super)
10261 int access_flag = get_access_flags_from_decl (member);
10262 bool is_static = false;
10264 if (TREE_CODE (member) == FIELD_DECL ||
10265 TREE_CODE (member) == VAR_DECL)
10266 is_static = FIELD_STATIC (member);
10267 else
10268 is_static = METHOD_STATIC (member);
10270 /* Access always granted for members declared public */
10271 if (access_flag & ACC_PUBLIC)
10272 return 0;
10274 /* Check access on protected members */
10275 if (access_flag & ACC_PROTECTED)
10277 /* Access granted if it occurs from within the package
10278 containing the class in which the protected member is
10279 declared */
10280 if (class_in_current_package (DECL_CONTEXT (member)))
10281 return 0;
10283 /* If accessed with the form `super.member', then access is granted */
10284 if (from_super)
10285 return 0;
10287 /* If WHERE is active, access was made through a qualifier. For
10288 non-static members, access is granted if the type of the qualifier
10289 is or is a sublass of the type the access is made from (6.6.2.1.) */
10290 if (where && !is_static)
10292 while (reference)
10294 if (inherits_from_p (where, reference))
10295 return 0;
10296 if (INNER_CLASS_TYPE_P (reference))
10297 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10298 else
10299 break;
10301 return 1;
10304 /* Otherwise, access is granted if occurring from within the class
10305 where member is declared, or a subclass of it. */
10306 while (reference)
10308 if (inherits_from_p (reference, DECL_CONTEXT (member)))
10309 return 0;
10310 if (INNER_CLASS_TYPE_P (reference))
10311 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10312 else
10313 break;
10315 return 1;
10318 /* Check access on private members. Access is granted only if it
10319 occurs from within the class in which it is declared -- that does
10320 it for innerclasses too. */
10321 if (access_flag & ACC_PRIVATE)
10323 if (reference == DECL_CONTEXT (member) ||
10324 common_enclosing_context_p (DECL_CONTEXT (member), reference))
10325 return 0;
10326 return 1;
10329 /* Default access is permitted only when occurring from within the
10330 package in which the context (MEMBER) is declared. */
10331 return !class_in_current_package (DECL_CONTEXT (member));
10334 /* Test deprecated decl access. */
10335 static void
10336 check_deprecation (tree wfl, tree decl)
10338 const char *file;
10339 tree elt;
10341 if (! warn_deprecated)
10342 return;
10344 /* We want to look at the element type of arrays here, so we strip
10345 all surrounding array types. */
10346 if (TYPE_ARRAY_P (TREE_TYPE (decl)))
10348 elt = TREE_TYPE (decl);
10349 while (TYPE_ARRAY_P (elt))
10350 elt = TYPE_ARRAY_ELEMENT (elt);
10351 /* We'll end up with a pointer type, so we use TREE_TYPE to go
10352 to the record. */
10353 decl = TYPE_NAME (TREE_TYPE (elt));
10355 file = DECL_SOURCE_FILE (decl);
10357 /* Complain if the field is deprecated and the file it was defined
10358 in isn't compiled at the same time the file which contains its
10359 use is */
10360 if (DECL_DEPRECATED (decl)
10361 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10363 const char *the;
10364 switch (TREE_CODE (decl))
10366 case FUNCTION_DECL:
10367 the = "method";
10368 break;
10369 case FIELD_DECL:
10370 case VAR_DECL:
10371 the = "field";
10372 break;
10373 case TYPE_DECL:
10374 parse_warning_context (wfl, "The class %qs has been deprecated",
10375 IDENTIFIER_POINTER (DECL_NAME (decl)));
10376 return;
10377 default:
10378 abort ();
10380 /* Don't issue a message if the context as been deprecated as a
10381 whole. */
10382 if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10383 parse_warning_context
10384 (wfl, "The %s %qs in class %qs has been deprecated",
10385 the, lang_printable_name (decl, 0),
10386 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10390 /* Returns 1 if class was declared in the current package, 0 otherwise */
10392 static GTY(()) tree cicp_cache;
10393 static int
10394 class_in_current_package (tree class)
10396 int qualified_flag;
10397 tree left;
10399 if (cicp_cache == class)
10400 return 1;
10402 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10404 /* If the current package is empty and the name of CLASS is
10405 qualified, class isn't in the current package. If there is a
10406 current package and the name of the CLASS is not qualified, class
10407 isn't in the current package */
10408 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10409 return 0;
10411 /* If there is not package and the name of CLASS isn't qualified,
10412 they belong to the same unnamed package */
10413 if (!ctxp->package && !qualified_flag)
10414 return 1;
10416 /* Compare the left part of the name of CLASS with the package name */
10417 split_qualified_name (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10418 if (ctxp->package == left)
10420 cicp_cache = class;
10421 return 1;
10423 return 0;
10426 /* This function may generate code to access DECL from WHERE. This is
10427 done only if certain conditions meet. */
10429 static tree
10430 maybe_access_field (tree decl, tree where, tree type)
10432 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10433 && !FIELD_STATIC (decl))
10434 decl = build_field_ref (where ? where : current_this,
10435 (type ? type : DECL_CONTEXT (decl)),
10436 DECL_NAME (decl));
10437 return decl;
10440 /* Build a method invocation, by patching PATCH. If non NULL
10441 and according to the situation, PRIMARY and WHERE may be
10442 used. IS_STATIC is set to 1 if the invoked function is static. */
10444 static tree
10445 patch_method_invocation (tree patch, tree primary, tree where, int from_super,
10446 int *is_static, tree *ret_decl)
10448 tree wfl = TREE_OPERAND (patch, 0);
10449 tree args = TREE_OPERAND (patch, 1);
10450 tree name = EXPR_WFL_NODE (wfl);
10451 tree list;
10452 int is_static_flag = 0;
10453 int is_super_init = 0;
10454 tree this_arg = NULL_TREE;
10455 int is_array_clone_call = 0;
10457 /* Should be overridden if everything goes well. Otherwise, if
10458 something fails, it should keep this value. It stop the
10459 evaluation of a bogus assignment. See java_complete_tree,
10460 MODIFY_EXPR: for the reasons why we sometimes want to keep on
10461 evaluating an assignment */
10462 TREE_TYPE (patch) = error_mark_node;
10464 /* Since lookup functions are messing with line numbers, save the
10465 context now. */
10466 java_parser_context_save_global ();
10468 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10470 /* Resolution of qualified name, excluding constructors */
10471 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10473 tree identifier, identifier_wfl, type, resolved;
10474 /* Extract the last IDENTIFIER of the qualified
10475 expression. This is a wfl and we will use it's location
10476 data during error report. */
10477 identifier_wfl = cut_identifier_in_qualified (wfl);
10478 identifier = EXPR_WFL_NODE (identifier_wfl);
10480 /* Given the context, IDENTIFIER is syntactically qualified
10481 as a MethodName. We need to qualify what's before */
10482 qualify_ambiguous_name (wfl);
10483 resolved = resolve_field_access (wfl, NULL, NULL);
10485 if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10486 && FIELD_FINAL (resolved)
10487 && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10488 && !flag_emit_class_files && !flag_emit_xref)
10489 resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10491 if (resolved == error_mark_node)
10492 PATCH_METHOD_RETURN_ERROR ();
10494 type = GET_SKIP_TYPE (resolved);
10495 resolve_and_layout (type, NULL_TREE);
10497 if (JPRIMITIVE_TYPE_P (type))
10499 parse_error_context
10500 (identifier_wfl,
10501 "Can't invoke a method on primitive type %qs",
10502 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10503 PATCH_METHOD_RETURN_ERROR ();
10506 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10507 args = nreverse (args);
10509 /* We're resolving a call from a type */
10510 if (TREE_CODE (resolved) == TYPE_DECL)
10512 if (CLASS_INTERFACE (resolved))
10514 parse_error_context
10515 (identifier_wfl,
10516 "Can't make static reference to method %qs in interface %qs",
10517 IDENTIFIER_POINTER (identifier),
10518 IDENTIFIER_POINTER (name));
10519 PATCH_METHOD_RETURN_ERROR ();
10521 if (list && !METHOD_STATIC (list))
10523 char *fct_name = xstrdup (lang_printable_name (list, 2));
10524 parse_error_context
10525 (identifier_wfl,
10526 "Can't make static reference to method %<%s %s%> in class %qs",
10527 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10528 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10529 free (fct_name);
10530 PATCH_METHOD_RETURN_ERROR ();
10533 else
10534 this_arg = primary = resolved;
10536 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10537 is_array_clone_call = 1;
10539 /* IDENTIFIER_WFL will be used to report any problem further */
10540 wfl = identifier_wfl;
10542 /* Resolution of simple names, names generated after a primary: or
10543 constructors */
10544 else
10546 tree class_to_search = NULL_TREE;
10547 int lc; /* Looking for Constructor */
10549 /* We search constructor in their target class */
10550 if (CALL_CONSTRUCTOR_P (patch))
10552 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10553 class_to_search = EXPR_WFL_NODE (wfl);
10554 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10555 this_identifier_node)
10556 class_to_search = NULL_TREE;
10557 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10558 super_identifier_node)
10560 is_super_init = 1;
10561 if (CLASSTYPE_SUPER (current_class))
10562 class_to_search =
10563 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10564 else
10566 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10567 PATCH_METHOD_RETURN_ERROR ();
10571 /* Class to search is NULL if we're searching the current one */
10572 if (class_to_search)
10574 class_to_search = resolve_and_layout (class_to_search, wfl);
10576 if (!class_to_search)
10578 parse_error_context
10579 (wfl, "Class %qs not found in type declaration",
10580 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10581 PATCH_METHOD_RETURN_ERROR ();
10584 /* Can't instantiate an abstract class, but we can
10585 invoke it's constructor. It's use within the `new'
10586 context is denied here. */
10587 if (CLASS_ABSTRACT (class_to_search)
10588 && TREE_CODE (patch) == NEW_CLASS_EXPR)
10590 parse_error_context
10591 (wfl, "Class %qs is an abstract class. It can't be instantiated",
10592 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10593 PATCH_METHOD_RETURN_ERROR ();
10596 class_to_search = TREE_TYPE (class_to_search);
10598 else
10599 class_to_search = current_class;
10600 lc = 1;
10602 /* This is a regular search in the local class, unless an
10603 alternate class is specified. */
10604 else
10606 if (where != NULL_TREE)
10607 class_to_search = where;
10608 else if (QUALIFIED_P (name))
10609 class_to_search = current_class;
10610 else
10612 class_to_search = current_class;
10614 for (;;)
10616 if (has_method (class_to_search, name))
10617 break;
10618 if (! INNER_CLASS_TYPE_P (class_to_search))
10620 parse_error_context (wfl,
10621 "No method named %qs in scope",
10622 IDENTIFIER_POINTER (name));
10623 PATCH_METHOD_RETURN_ERROR ();
10625 class_to_search
10626 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10629 lc = 0;
10632 /* NAME is a simple identifier or comes from a primary. Search
10633 in the class whose declaration contain the method being
10634 invoked. */
10635 resolve_and_layout (class_to_search, NULL_TREE);
10637 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10638 /* Don't continue if no method were found, as the next statement
10639 can't be executed then. */
10640 if (!list)
10641 PATCH_METHOD_RETURN_ERROR ();
10643 if (TYPE_ARRAY_P (class_to_search)
10644 && DECL_NAME (list) == get_identifier ("clone"))
10645 is_array_clone_call = 1;
10647 /* Check for static reference if non static methods */
10648 if (check_for_static_method_reference (wfl, patch, list,
10649 class_to_search, primary))
10650 PATCH_METHOD_RETURN_ERROR ();
10652 /* Check for inner classes creation from illegal contexts */
10653 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10654 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10655 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10656 && !DECL_INIT_P (current_function_decl))
10658 parse_error_context
10659 (wfl, "No enclosing instance for inner class %qs is in scope%s",
10660 lang_printable_name (class_to_search, 0),
10661 (!current_this ? "" :
10662 "; an explicit one must be provided when creating this inner class"));
10663 PATCH_METHOD_RETURN_ERROR ();
10666 /* Non static methods are called with the current object extra
10667 argument. If patch a `new TYPE()', the argument is the value
10668 returned by the object allocator. If method is resolved as a
10669 primary, use the primary otherwise use the current THIS. */
10670 args = nreverse (args);
10671 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10673 this_arg = primary ? primary : current_this;
10675 /* If we're using an access method, things are different.
10676 There are two family of cases:
10678 1) We're not generating bytecodes:
10680 - LIST is non static. It's invocation is transformed from
10681 x(a1,...,an) into this$<n>.x(a1,....an).
10682 - LIST is static. It's invocation is transformed from
10683 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10685 2) We're generating bytecodes:
10687 - LIST is non static. It's invocation is transformed from
10688 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10689 - LIST is static. It's invocation is transformed from
10690 x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10692 Of course, this$<n> can be arbitrarily complex, ranging from
10693 this$0 (the immediate outer context) to
10694 access$0(access$0(...(this$0))).
10696 maybe_use_access_method returns a nonzero value if the
10697 this_arg has to be moved into the (then generated) stub
10698 argument list. In the meantime, the selected function
10699 might have be replaced by a generated stub. */
10700 if (!primary &&
10701 maybe_use_access_method (is_super_init, &list, &this_arg))
10703 args = tree_cons (NULL_TREE, this_arg, args);
10704 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10709 /* Merge point of all resolution schemes. If we have nothing, this
10710 is an error, already signaled */
10711 if (!list)
10712 PATCH_METHOD_RETURN_ERROR ();
10714 /* Check accessibility, position the is_static flag, build and
10715 return the call */
10716 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10717 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10718 NULL_TREE), from_super)
10719 /* Calls to clone() on array types are permitted as a special-case. */
10720 && !is_array_clone_call)
10722 const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10723 const char *const access =
10724 accessibility_string (get_access_flags_from_decl (list));
10725 const char *const klass =
10726 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10727 const char *const refklass =
10728 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10729 const char *const what = (DECL_CONSTRUCTOR_P (list)
10730 ? "constructor" : "method");
10731 parse_error_context (wfl,
10732 "Can't access %s %s %<%s.%s%> from %qs",
10733 access, what, klass, fct_name, refklass);
10734 PATCH_METHOD_RETURN_ERROR ();
10737 /* Deprecation check: check whether the method being invoked or the
10738 instance-being-created's type are deprecated. */
10739 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10740 check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10741 check_deprecation (wfl, list);
10743 /* If invoking a innerclass constructor, there are hidden parameters
10744 to pass */
10745 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10746 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10748 /* And make sure we add the accessed local variables to be saved
10749 in field aliases. */
10750 args = build_alias_initializer_parameter_list
10751 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10753 /* Secretly pass the current_this/primary as a second argument */
10754 if (primary || current_this)
10756 tree extra_arg;
10757 tree this_type = (current_this ?
10758 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10759 /* Method's (list) enclosing context */
10760 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10761 /* If we have a primary, use it. */
10762 if (primary)
10763 extra_arg = primary;
10764 /* The current `this' is an inner class but isn't a direct
10765 enclosing context for the inner class we're trying to
10766 create. Build an access to the proper enclosing context
10767 and use it. */
10768 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10769 && this_type != TREE_TYPE (mec))
10772 extra_arg = build_access_to_thisn (current_class,
10773 TREE_TYPE (mec), 0);
10774 extra_arg = java_complete_tree (extra_arg);
10776 /* Otherwise, just use the current `this' as an enclosing
10777 context. */
10778 else
10779 extra_arg = current_this;
10780 args = tree_cons (NULL_TREE, extra_arg, args);
10782 else
10783 args = tree_cons (NULL_TREE, integer_zero_node, args);
10786 /* This handles the situation where a constructor invocation needs
10787 to have an enclosing context passed as a second parameter (the
10788 constructor is one of an inner class). */
10789 if ((is_super_init ||
10790 (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10791 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10793 tree dest = TYPE_NAME (DECL_CONTEXT (list));
10794 tree extra_arg =
10795 build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10796 extra_arg = java_complete_tree (extra_arg);
10797 args = tree_cons (NULL_TREE, extra_arg, args);
10800 is_static_flag = METHOD_STATIC (list);
10801 if (! is_static_flag && this_arg != NULL_TREE)
10802 args = tree_cons (NULL_TREE, this_arg, args);
10804 /* In the context of an explicit constructor invocation, we can't
10805 invoke any method relying on `this'. Exceptions are: we're
10806 invoking a static function, primary exists and is not the current
10807 this, we're creating a new object. */
10808 if (ctxp->explicit_constructor_p
10809 && !is_static_flag
10810 && (!primary || primary == current_this)
10811 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10813 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
10814 PATCH_METHOD_RETURN_ERROR ();
10816 java_parser_context_restore_global ();
10817 if (is_static)
10818 *is_static = is_static_flag;
10819 /* Sometimes, we want the decl of the selected method. Such as for
10820 EH checking */
10821 if (ret_decl)
10822 *ret_decl = list;
10823 patch = patch_invoke (patch, list, args);
10825 /* Now is a good time to insert the call to finit$ */
10826 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10828 tree finit_parms, finit_call;
10830 /* Prepare to pass hidden parameters to finit$, if any. */
10831 finit_parms = build_alias_initializer_parameter_list
10832 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10834 finit_call =
10835 build_method_invocation (build_wfl_node (finit_identifier_node),
10836 finit_parms);
10838 /* Generate the code used to initialize fields declared with an
10839 initialization statement and build a compound statement along
10840 with the super constructor invocation. */
10841 CAN_COMPLETE_NORMALLY (patch) = 1;
10842 patch = build2 (COMPOUND_EXPR, void_type_node, patch,
10843 java_complete_tree (finit_call));
10845 return patch;
10848 /* Check that we're not trying to do a static reference to a method in
10849 non static method. Return 1 if it's the case, 0 otherwise. */
10851 static int
10852 check_for_static_method_reference (tree wfl, tree node, tree method,
10853 tree where, tree primary)
10855 if (METHOD_STATIC (current_function_decl)
10856 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10858 char *fct_name = xstrdup (lang_printable_name (method, 0));
10859 parse_error_context
10860 (wfl, "Can't make static reference to method %<%s %s%> in class %qs",
10861 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10862 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10863 free (fct_name);
10864 return 1;
10866 return 0;
10869 /* Fix the invocation of *MDECL if necessary in the case of a
10870 invocation from an inner class. *THIS_ARG might be modified
10871 appropriately and an alternative access to *MDECL might be
10872 returned. */
10874 static int
10875 maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10877 tree ctx;
10878 tree md = *mdecl, ta = *this_arg;
10879 int to_return = 0;
10880 int non_static_context = !METHOD_STATIC (md);
10882 if (is_super_init
10883 || DECL_CONTEXT (md) == current_class
10884 || !PURE_INNER_CLASS_TYPE_P (current_class)
10885 || DECL_FINIT_P (md)
10886 || DECL_INSTINIT_P (md))
10887 return 0;
10889 /* If we're calling a method found in an enclosing class, generate
10890 what it takes to retrieve the right this. Don't do that if we're
10891 invoking a static method. Note that if MD's type is unrelated to
10892 CURRENT_CLASS, then the current this can be used. */
10894 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10896 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10897 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10899 ta = build_current_thisn (current_class);
10900 ta = build_wfl_node (ta);
10902 else
10904 tree type = ctx;
10905 while (type)
10907 maybe_build_thisn_access_method (type);
10908 if (inherits_from_p (type, DECL_CONTEXT (md)))
10910 ta = build_access_to_thisn (ctx, type, 0);
10911 break;
10913 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10914 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10917 ta = java_complete_tree (ta);
10920 /* We might have to use an access method to get to MD. We can
10921 break the method access rule as far as we're not generating
10922 bytecode */
10923 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10925 md = build_outer_method_access_method (md);
10926 to_return = 1;
10929 *mdecl = md;
10930 *this_arg = ta;
10932 /* Returning a nonzero value indicates we were doing a non static
10933 method invocation that is now a static invocation. It will have
10934 callee displace `this' to insert it in the regular argument
10935 list. */
10936 return (non_static_context && to_return);
10939 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10940 mode. */
10942 static tree
10943 patch_invoke (tree patch, tree method, tree args)
10945 tree dtable, func;
10946 tree original_call, t, ta;
10947 tree check = NULL_TREE;
10949 /* Last step for args: convert build-in types. If we're dealing with
10950 a new TYPE() type call, the first argument to the constructor
10951 isn't found in the incoming argument list, but delivered by
10952 `new' */
10953 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10954 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10955 t = TREE_CHAIN (t);
10956 for (ta = args; t != end_params_node && ta;
10957 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10958 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10959 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10960 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10962 /* Resolve unresolved returned type issues */
10963 t = TREE_TYPE (TREE_TYPE (method));
10964 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10965 resolve_and_layout (TREE_TYPE (t), NULL);
10967 if (flag_emit_class_files || flag_emit_xref)
10968 func = method;
10969 else
10971 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10973 case INVOKE_VIRTUAL:
10974 dtable = invoke_build_dtable (0, args);
10975 func = build_invokevirtual (dtable, method);
10976 break;
10978 case INVOKE_NONVIRTUAL:
10979 /* If the object for the method call is null, we throw an
10980 exception. We don't do this if the object is the current
10981 method's `this'. In other cases we just rely on an
10982 optimization pass to eliminate redundant checks. */
10983 if (TREE_VALUE (args) != current_this)
10985 /* We use a save_expr here to make sure we only evaluate
10986 the new `self' expression once. */
10987 tree save_arg = save_expr (TREE_VALUE (args));
10988 TREE_VALUE (args) = save_arg;
10989 check = java_check_reference (save_arg, 1);
10991 /* Fall through. */
10993 case INVOKE_SUPER:
10994 case INVOKE_STATIC:
10996 tree signature = build_java_signature (TREE_TYPE (method));
10997 func = build_known_method_ref (method, TREE_TYPE (method),
10998 DECL_CONTEXT (method),
10999 signature, args);
11001 break;
11003 case INVOKE_INTERFACE:
11004 dtable = invoke_build_dtable (1, args);
11005 func = build_invokeinterface (dtable, method);
11006 break;
11008 default:
11009 abort ();
11012 /* Ensure self_type is initialized, (invokestatic). FIXME */
11013 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
11016 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
11017 TREE_OPERAND (patch, 0) = func;
11018 TREE_OPERAND (patch, 1) = args;
11019 patch = check_for_builtin (method, patch);
11020 original_call = patch;
11022 /* We're processing a `new TYPE ()' form. New is called and its
11023 returned value is the first argument to the constructor. We build
11024 a COMPOUND_EXPR and use saved expression so that the overall NEW
11025 expression value is a pointer to a newly created and initialized
11026 class. */
11027 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
11029 tree class = DECL_CONTEXT (method);
11030 tree c1, saved_new, new;
11031 tree alloc_node;
11033 if (flag_emit_class_files || flag_emit_xref)
11035 TREE_TYPE (patch) = build_pointer_type (class);
11036 return patch;
11038 if (!TYPE_SIZE (class))
11039 safe_layout_class (class);
11040 alloc_node =
11041 (class_has_finalize_method (class) ? alloc_object_node
11042 : alloc_no_finalizer_node);
11043 new = build3 (CALL_EXPR, promote_type (class),
11044 build_address_of (alloc_node),
11045 build_tree_list (NULL_TREE, build_class_ref (class)),
11046 NULL_TREE);
11047 saved_new = save_expr (new);
11048 c1 = build_tree_list (NULL_TREE, saved_new);
11049 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
11050 TREE_OPERAND (original_call, 1) = c1;
11051 TREE_SET_CODE (original_call, CALL_EXPR);
11052 patch = build2 (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
11055 /* If CHECK is set, then we are building a check to see if the object
11056 is NULL. */
11057 if (check != NULL_TREE)
11059 /* We have to call force_evaluation_order now because creating a
11060 COMPOUND_EXPR wraps the arg list in a way that makes it
11061 unrecognizable by force_evaluation_order later. Yuk. */
11062 patch = build2 (COMPOUND_EXPR, TREE_TYPE (patch), check,
11063 force_evaluation_order (patch));
11064 TREE_SIDE_EFFECTS (patch) = 1;
11067 /* In order to be able to modify PATCH later, we SAVE_EXPR it and
11068 put it as the first expression of a COMPOUND_EXPR. The second
11069 expression being an empty statement to be later patched if
11070 necessary. We remember a TREE_LIST (the PURPOSE is the method,
11071 the VALUE is the compound) in a hashtable and return a
11072 COMPOUND_EXPR built so that the result of the evaluation of the
11073 original PATCH node is returned. */
11074 if (STATIC_CLASS_INIT_OPT_P ()
11075 && current_function_decl && METHOD_STATIC (method))
11077 tree list;
11078 tree fndecl = current_function_decl;
11079 /* We have to call force_evaluation_order now because creating a
11080 COMPOUND_EXPR wraps the arg list in a way that makes it
11081 unrecognizable by force_evaluation_order later. Yuk. */
11082 tree save = force_evaluation_order (patch);
11083 tree type = TREE_TYPE (patch);
11085 patch = build2 (COMPOUND_EXPR, type, save, build_java_empty_stmt ());
11086 list = tree_cons (method, patch,
11087 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
11089 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
11091 patch = build2 (COMPOUND_EXPR, type, patch, save);
11094 return patch;
11097 static int
11098 invocation_mode (tree method, int super)
11100 int access = get_access_flags_from_decl (method);
11102 if (super)
11103 return INVOKE_SUPER;
11105 if (access & ACC_STATIC)
11106 return INVOKE_STATIC;
11108 /* We have to look for a constructor before we handle nonvirtual
11109 calls; otherwise the constructor will look nonvirtual. */
11110 if (DECL_CONSTRUCTOR_P (method))
11111 return INVOKE_STATIC;
11113 if (access & ACC_PRIVATE)
11114 return INVOKE_NONVIRTUAL;
11116 /* Binary compatibility: just because it's final today, that doesn't
11117 mean it'll be final tomorrow. */
11118 if (! flag_indirect_dispatch
11119 || DECL_CONTEXT (method) == object_type_node)
11121 if (access & ACC_FINAL)
11122 return INVOKE_NONVIRTUAL;
11124 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
11125 return INVOKE_NONVIRTUAL;
11128 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
11129 return INVOKE_INTERFACE;
11131 return INVOKE_VIRTUAL;
11134 /* Retrieve a refined list of matching methods. It covers the step
11135 15.11.2 (Compile-Time Step 2) */
11137 static tree
11138 lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
11140 tree atl = end_params_node; /* Arg Type List */
11141 tree method, signature, list, node;
11142 const char *candidates; /* Used for error report */
11143 char *dup;
11145 /* Fix the arguments */
11146 for (node = arg_list; node; node = TREE_CHAIN (node))
11148 tree current_arg = TREE_TYPE (TREE_VALUE (node));
11149 /* Non primitive type may have to be resolved */
11150 if (!JPRIMITIVE_TYPE_P (current_arg))
11151 resolve_and_layout (current_arg, NULL_TREE);
11152 /* And promoted */
11153 if (TREE_CODE (current_arg) == RECORD_TYPE)
11154 current_arg = promote_type (current_arg);
11155 atl = tree_cons (NULL_TREE, current_arg, atl);
11158 /* Presto. If we're dealing with an anonymous class and a
11159 constructor call, generate the right constructor now, since we
11160 know the arguments' types. */
11162 if (lc && ANONYMOUS_CLASS_P (class))
11164 tree mdecl = craft_constructor (TYPE_NAME (class), atl);
11165 /* The anonymous class may have already been laid out, so make sure
11166 the new constructor is laid out here. */
11167 layout_class_method (class, CLASSTYPE_SUPER (class), mdecl, NULL_TREE);
11170 /* Find all candidates and then refine the list, searching for the
11171 most specific method. */
11172 list = find_applicable_accessible_methods_list (lc, class, name, atl);
11173 list = find_most_specific_methods_list (list);
11174 if (list && !TREE_CHAIN (list))
11175 return TREE_VALUE (list);
11177 /* Issue an error. List candidates if any. Candidates are listed
11178 only if accessible (non accessible methods may end-up here for
11179 the sake of a better error report). */
11180 candidates = NULL;
11181 if (list)
11183 tree current;
11184 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
11185 for (current = list; current; current = TREE_CHAIN (current))
11187 tree cm = TREE_VALUE (current);
11188 char string [4096];
11189 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
11190 continue;
11191 sprintf
11192 (string, " '%s' in '%s'%s",
11193 get_printable_method_name (cm),
11194 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
11195 (TREE_CHAIN (current) ? "\n" : ""));
11196 obstack_grow (&temporary_obstack, string, strlen (string));
11198 obstack_1grow (&temporary_obstack, '\0');
11199 candidates = obstack_finish (&temporary_obstack);
11201 /* Issue the error message */
11202 method = make_node (FUNCTION_TYPE);
11203 TYPE_ARG_TYPES (method) = atl;
11204 signature = build_java_argument_signature (method);
11205 dup = xstrdup (lang_printable_name (class, 0));
11206 parse_error_context (cl, "Can't find %s %<%s(%s)%> in type %qs%s",
11207 (lc ? "constructor" : "method"),
11208 (lc ? dup : IDENTIFIER_POINTER (name)),
11209 IDENTIFIER_POINTER (signature), dup,
11210 (candidates ? candidates : ""));
11211 free (dup);
11212 return NULL_TREE;
11215 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
11216 when we're looking for a constructor. */
11218 static tree
11219 find_applicable_accessible_methods_list (int lc, tree class, tree name,
11220 tree arglist)
11222 static htab_t searched_classes;
11223 static int search_not_done = 0;
11224 tree list = NULL_TREE, all_list = NULL_TREE;
11225 tree base_binfo;
11226 int i;
11228 /* Check the hash table to determine if this class has been searched
11229 already. */
11230 if (searched_classes)
11232 if (htab_find (searched_classes, class) != NULL)
11233 return NULL;
11235 else
11237 searched_classes = htab_create (10, htab_hash_pointer,
11238 htab_eq_pointer, NULL);
11241 search_not_done++;
11242 *htab_find_slot (searched_classes, class, INSERT) = class;
11244 if (!CLASS_LOADED_P (class))
11246 load_class (class, 1);
11247 safe_layout_class (class);
11250 /* Search interfaces */
11251 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
11252 && CLASS_INTERFACE (TYPE_NAME (class)))
11254 search_applicable_methods_list (lc, TYPE_METHODS (class),
11255 name, arglist, &list, &all_list);
11256 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11258 tree t = BINFO_TYPE (base_binfo);
11259 tree rlist;
11261 rlist = find_applicable_accessible_methods_list (lc, t, name,
11262 arglist);
11263 list = chainon (rlist, list);
11266 /* Search classes */
11267 else
11269 search_applicable_methods_list (lc, TYPE_METHODS (class),
11270 name, arglist, &list, &all_list);
11272 /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11273 that we only search in class. Note that we should have found
11274 something at this point. */
11275 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11277 lc = 1;
11278 if (!list)
11279 abort ();
11282 /* We must search all interfaces of this class */
11283 if (!lc)
11285 for (i = 1;
11286 BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11288 tree t = BINFO_TYPE (base_binfo);
11289 if (t != object_type_node)
11291 tree rlist
11292 = find_applicable_accessible_methods_list (lc, t,
11293 name, arglist);
11294 list = chainon (rlist, list);
11299 /* Search superclass */
11300 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11302 tree rlist;
11303 class = CLASSTYPE_SUPER (class);
11304 rlist = find_applicable_accessible_methods_list (lc, class,
11305 name, arglist);
11306 list = chainon (rlist, list);
11310 search_not_done--;
11312 /* We're done. Reset the searched classes list and finally search
11313 java.lang.Object if it wasn't searched already. */
11314 if (!search_not_done)
11316 if (!lc
11317 && TYPE_METHODS (object_type_node)
11318 && htab_find (searched_classes, object_type_node) == NULL)
11320 search_applicable_methods_list (lc,
11321 TYPE_METHODS (object_type_node),
11322 name, arglist, &list, &all_list);
11324 htab_delete (searched_classes);
11325 searched_classes = NULL;
11328 /* Either return the list obtained or all selected (but
11329 inaccessible) methods for better error report. */
11330 return (!list ? all_list : list);
11333 /* Effectively search for the appropriate method in method */
11335 static void
11336 search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
11337 tree *list, tree *all_list)
11339 for (; method; method = TREE_CHAIN (method))
11341 /* When dealing with constructor, stop here, otherwise search
11342 other classes */
11343 if (lc && !DECL_CONSTRUCTOR_P (method))
11344 continue;
11345 else if (!lc && (DECL_CONSTRUCTOR_P (method)
11346 || (DECL_NAME (method) != name)))
11347 continue;
11349 if (argument_types_convertible (method, arglist))
11351 /* Retain accessible methods only */
11352 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11353 method, NULL_TREE, 0))
11354 *list = tree_cons (NULL_TREE, method, *list);
11355 else
11356 /* Also retain all selected method here */
11357 *all_list = tree_cons (NULL_TREE, method, *list);
11362 /* 15.11.2.2 Choose the Most Specific Method */
11364 static tree
11365 find_most_specific_methods_list (tree list)
11367 int max = 0;
11368 int abstract, candidates;
11369 tree current, new_list = NULL_TREE;
11370 for (current = list; current; current = TREE_CHAIN (current))
11372 tree method;
11373 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11375 for (method = list; method; method = TREE_CHAIN (method))
11377 tree method_v, current_v;
11378 /* Don't test a method against itself */
11379 if (method == current)
11380 continue;
11382 method_v = TREE_VALUE (method);
11383 current_v = TREE_VALUE (current);
11385 /* Compare arguments and location where methods where declared */
11386 if (argument_types_convertible (method_v, current_v))
11388 if (valid_method_invocation_conversion_p
11389 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
11390 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
11391 && enclosing_context_p (DECL_CONTEXT (method_v),
11392 DECL_CONTEXT (current_v))))
11394 int v = (DECL_SPECIFIC_COUNT (current_v) +=
11395 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
11396 max = (v > max ? v : max);
11402 /* Review the list and select the maximally specific methods */
11403 for (current = list, abstract = -1, candidates = -1;
11404 current; current = TREE_CHAIN (current))
11405 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11407 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11408 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11409 candidates++;
11412 /* If we have several and they're all abstract, just pick the
11413 closest one. */
11414 if (candidates > 0 && candidates == abstract)
11416 /* FIXME: merge the throws clauses. There is no convenient way
11417 to do this in gcj right now, since ideally we'd like to
11418 introduce a new METHOD_DECL here, but that is really not
11419 possible. */
11420 new_list = nreverse (new_list);
11421 TREE_CHAIN (new_list) = NULL_TREE;
11422 return new_list;
11425 /* We have several (we couldn't find a most specific), all but one
11426 are abstract, we pick the only non abstract one. */
11427 if (candidates > 0 && (candidates == abstract+1))
11429 for (current = new_list; current; current = TREE_CHAIN (current))
11430 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11432 TREE_CHAIN (current) = NULL_TREE;
11433 new_list = current;
11437 /* If we can't find one, lower expectations and try to gather multiple
11438 maximally specific methods */
11439 while (!new_list && max)
11441 while (--max > 0)
11443 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11444 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11448 return new_list;
11451 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11452 converted by method invocation conversion (5.3) to the type of the
11453 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11454 to change less often than M1. */
11456 static GTY(()) tree m2_arg_value;
11457 static GTY(()) tree m2_arg_cache;
11459 static int
11460 argument_types_convertible (tree m1, tree m2_or_arglist)
11462 tree m1_arg, m2_arg;
11464 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11466 if (m2_arg_value == m2_or_arglist)
11467 m2_arg = m2_arg_cache;
11468 else
11470 /* M2_OR_ARGLIST can be a function DECL or a raw list of
11471 argument types */
11472 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11474 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11475 if (!METHOD_STATIC (m2_or_arglist))
11476 m2_arg = TREE_CHAIN (m2_arg);
11478 else
11479 m2_arg = m2_or_arglist;
11481 m2_arg_value = m2_or_arglist;
11482 m2_arg_cache = m2_arg;
11485 while (m1_arg != end_params_node && m2_arg != end_params_node)
11487 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11488 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11489 TREE_VALUE (m2_arg)))
11490 break;
11491 m1_arg = TREE_CHAIN (m1_arg);
11492 m2_arg = TREE_CHAIN (m2_arg);
11494 return m1_arg == end_params_node && m2_arg == end_params_node;
11497 /* Qualification routines */
11499 /* Given a name x.y.z, look up x locally. If it's found, save the
11500 decl. If it's not found, mark the name as RESOLVE_PACKAGE_NAME_P,
11501 so that we later try and load the appropriate classes. */
11502 static void
11503 qualify_ambiguous_name (tree id)
11505 tree name, decl;
11507 /* We inspect the first item of the qualification list. As a sanity
11508 check, make sure that it is an identfier node. */
11509 tree qual = EXPR_WFL_QUALIFICATION (id);
11510 tree qual_wfl = QUAL_WFL (qual);
11512 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11513 return;
11515 name = EXPR_WFL_NODE (qual_wfl);
11517 /* If we don't have an identifier, or we have a 'this' or 'super',
11518 then field access processing is all we need : there is nothing
11519 for us to do. */
11520 if (!name || TREE_CODE (name) != IDENTIFIER_NODE ||
11521 name == this_identifier_node ||
11522 name == super_identifier_node)
11523 return;
11525 /* If name appears within the scope of a local variable declaration
11526 or parameter declaration, or is a field within an enclosing
11527 class, then it is an expression name. Save the decl and let
11528 resolve_field_access do it's work. */
11529 if ((decl = IDENTIFIER_LOCAL_VALUE (name)) ||
11530 (decl = lookup_field_wrapper (current_class, name)))
11532 QUAL_RESOLUTION (qual) = decl;
11533 return;
11536 /* If name is a known class name (either declared or imported), mark
11537 us as a type name. */
11538 if ((decl = resolve_and_layout (name, NULL_TREE)))
11540 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11541 QUAL_RESOLUTION (qual) = decl;
11544 /* Check here that NAME isn't declared by more than one
11545 type-import-on-demand declaration of the compilation unit
11546 containing NAME. FIXME */
11548 /* We couldn't find a declaration for the name. Assume for now that
11549 we have a qualified class name that needs to be loaded from an
11550 external class file. */
11551 else
11552 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11554 /* Propagate the qualification across other components of the
11555 qualified name */
11556 for (qual = TREE_CHAIN (qual); qual;
11557 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11559 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11560 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11563 /* Store the global qualification for the ambiguous part of ID back
11564 into ID fields */
11565 if (RESOLVE_TYPE_NAME_P (qual_wfl))
11566 RESOLVE_TYPE_NAME_P (id) = 1;
11567 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11568 RESOLVE_PACKAGE_NAME_P (id) = 1;
11571 /* Patch tree nodes in a function body. When a BLOCK is found, push
11572 local variable decls if present.
11573 Same as java_complete_lhs, but does resolve static finals to values. */
11575 static tree
11576 java_complete_tree (tree node)
11578 node = java_complete_lhs (node);
11579 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11580 && DECL_INITIAL (node) != NULL_TREE
11581 && !flag_emit_xref)
11583 tree value = fold_constant_for_init (node, node);
11584 if (value != NULL_TREE)
11585 return value;
11587 return node;
11590 static tree
11591 java_stabilize_reference (tree node)
11593 if (TREE_CODE (node) == COMPOUND_EXPR)
11595 tree op0 = TREE_OPERAND (node, 0);
11596 tree op1 = TREE_OPERAND (node, 1);
11597 TREE_OPERAND (node, 0) = save_expr (op0);
11598 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11599 return node;
11601 return stabilize_reference (node);
11604 /* Patch tree nodes in a function body. When a BLOCK is found, push
11605 local variable decls if present.
11606 Same as java_complete_tree, but does not resolve static finals to values. */
11608 static tree
11609 java_complete_lhs (tree node)
11611 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11612 int flag;
11614 /* CONVERT_EXPR always has its type set, even though it needs to be
11615 worked out. */
11616 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11617 return node;
11619 /* The switch block implements cases processing container nodes
11620 first. Contained nodes are always written back. Leaves come
11621 next and return a value. */
11622 switch (TREE_CODE (node))
11624 case BLOCK:
11626 /* 1- Block section.
11627 Set the local values on decl names so we can identify them
11628 faster when they're referenced. At that stage, identifiers
11629 are legal so we don't check for declaration errors. */
11630 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11632 DECL_CONTEXT (cn) = current_function_decl;
11633 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11635 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11636 CAN_COMPLETE_NORMALLY (node) = 1;
11637 else
11639 tree stmt = BLOCK_EXPR_BODY (node);
11640 tree *ptr;
11641 int error_seen = 0;
11642 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11644 /* Re-order from (((A; B); C); ...; Z) to
11645 (A; (B; (C ; (...; Z)))).
11646 This makes it easier to scan the statements left-to-right
11647 without using recursion (which might overflow the stack
11648 if the block has many statements. */
11649 for (;;)
11651 tree left = TREE_OPERAND (stmt, 0);
11652 if (TREE_CODE (left) != COMPOUND_EXPR)
11653 break;
11654 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11655 TREE_OPERAND (left, 1) = stmt;
11656 stmt = left;
11658 BLOCK_EXPR_BODY (node) = stmt;
11661 /* Now do the actual complete, without deep recursion for
11662 long blocks. */
11663 ptr = &BLOCK_EXPR_BODY (node);
11664 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11665 && !IS_EMPTY_STMT (TREE_OPERAND (*ptr, 1)))
11667 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11668 tree *next = &TREE_OPERAND (*ptr, 1);
11669 TREE_OPERAND (*ptr, 0) = cur;
11670 if (IS_EMPTY_STMT (cur))
11672 /* Optimization; makes it easier to detect empty bodies.
11673 Most useful for <clinit> with all-constant initializer. */
11674 *ptr = *next;
11675 continue;
11677 if (TREE_CODE (cur) == ERROR_MARK)
11678 error_seen++;
11679 else if (! CAN_COMPLETE_NORMALLY (cur))
11681 wfl_op2 = *next;
11682 for (;;)
11684 if (TREE_CODE (wfl_op2) == BLOCK)
11685 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11686 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11687 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11688 else
11689 break;
11691 if (TREE_CODE (wfl_op2) != CASE_EXPR
11692 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11693 unreachable_stmt_error (*ptr);
11695 if (TREE_TYPE (*ptr) == NULL_TREE)
11696 TREE_TYPE (*ptr) = void_type_node;
11697 ptr = next;
11699 *ptr = java_complete_tree (*ptr);
11701 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11702 return error_mark_node;
11703 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11705 /* Turn local bindings to null */
11706 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11707 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11709 TREE_TYPE (node) = void_type_node;
11710 break;
11712 /* 2- They are expressions but ultimately deal with statements */
11714 case THROW_EXPR:
11715 wfl_op1 = TREE_OPERAND (node, 0);
11716 COMPLETE_CHECK_OP_0 (node);
11717 /* 14.19 A throw statement cannot complete normally. */
11718 CAN_COMPLETE_NORMALLY (node) = 0;
11719 return patch_throw_statement (node, wfl_op1);
11721 case SYNCHRONIZED_EXPR:
11722 wfl_op1 = TREE_OPERAND (node, 0);
11723 return patch_synchronized_statement (node, wfl_op1);
11725 case TRY_EXPR:
11726 return patch_try_statement (node);
11728 case TRY_FINALLY_EXPR:
11729 COMPLETE_CHECK_OP_0 (node);
11730 COMPLETE_CHECK_OP_1 (node);
11731 if (IS_EMPTY_STMT (TREE_OPERAND (node, 0)))
11732 /* Reduce try/finally nodes with an empty try block. */
11733 return TREE_OPERAND (node, 1);
11734 if (IS_EMPTY_STMT (TREE_OPERAND (node, 1)))
11735 /* Likewise for an empty finally block. */
11736 return TREE_OPERAND (node, 0);
11737 CAN_COMPLETE_NORMALLY (node)
11738 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11739 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11740 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11741 return node;
11743 case LABELED_BLOCK_EXPR:
11744 PUSH_LABELED_BLOCK (node);
11745 if (LABELED_BLOCK_BODY (node))
11746 COMPLETE_CHECK_OP_1 (node);
11747 TREE_TYPE (node) = void_type_node;
11748 POP_LABELED_BLOCK ();
11750 if (IS_EMPTY_STMT (LABELED_BLOCK_BODY (node)))
11752 LABELED_BLOCK_BODY (node) = NULL_TREE;
11753 CAN_COMPLETE_NORMALLY (node) = 1;
11755 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11756 CAN_COMPLETE_NORMALLY (node) = 1;
11757 return node;
11759 case EXIT_BLOCK_EXPR:
11760 return patch_bc_statement (node);
11762 case CASE_EXPR:
11763 cn = java_complete_tree (TREE_OPERAND (node, 0));
11764 if (cn == error_mark_node)
11765 return cn;
11767 /* First, the case expression must be constant. Values of final
11768 fields are accepted. */
11769 cn = fold (cn);
11770 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11771 && JDECL_P (TREE_OPERAND (cn, 1))
11772 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11773 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11775 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11776 TREE_OPERAND (cn, 1));
11778 /* Accept final locals too. */
11779 else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn)
11780 && DECL_INITIAL (cn))
11781 cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11783 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11785 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11786 parse_error_context (node, "Constant expression required");
11787 return error_mark_node;
11790 nn = ctxp->current_loop;
11792 /* It must be assignable to the type of the switch expression. */
11793 if (!try_builtin_assignconv (NULL_TREE,
11794 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11796 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11797 parse_error_context
11798 (wfl_operator,
11799 "Incompatible type for case. Can't convert %qs to %<int%>",
11800 lang_printable_name (TREE_TYPE (cn), 0));
11801 return error_mark_node;
11804 cn = fold (convert (int_type_node, cn));
11805 TREE_CONSTANT_OVERFLOW (cn) = 0;
11806 CAN_COMPLETE_NORMALLY (cn) = 1;
11808 /* Save the label on a list so that we can later check for
11809 duplicates. */
11810 case_label_list = tree_cons (node, cn, case_label_list);
11812 /* Multiple instance of a case label bearing the same value is
11813 checked later. The case expression is all right so far. */
11814 if (TREE_CODE (cn) == VAR_DECL)
11815 cn = DECL_INITIAL (cn);
11816 TREE_OPERAND (node, 0) = cn;
11817 TREE_TYPE (node) = void_type_node;
11818 CAN_COMPLETE_NORMALLY (node) = 1;
11819 TREE_SIDE_EFFECTS (node) = 1;
11820 break;
11822 case DEFAULT_EXPR:
11823 nn = ctxp->current_loop;
11824 /* Only one default label is allowed per switch statement */
11825 if (SWITCH_HAS_DEFAULT (nn))
11827 #ifdef USE_MAPPED_LOCATION
11828 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
11829 #else
11830 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11831 #endif
11832 parse_error_context (wfl_operator,
11833 "Duplicate case label: %<default%>");
11834 return error_mark_node;
11836 else
11837 SWITCH_HAS_DEFAULT (nn) = 1;
11838 TREE_TYPE (node) = void_type_node;
11839 TREE_SIDE_EFFECTS (node) = 1;
11840 CAN_COMPLETE_NORMALLY (node) = 1;
11841 break;
11843 case SWITCH_EXPR:
11844 case LOOP_EXPR:
11845 PUSH_LOOP (node);
11846 /* Check whether the loop was enclosed in a labeled
11847 statement. If not, create one, insert the loop in it and
11848 return the node */
11849 nn = patch_loop_statement (node);
11851 /* Anyways, walk the body of the loop */
11852 if (TREE_CODE (node) == LOOP_EXPR)
11853 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11854 /* Switch statement: walk the switch expression and the cases */
11855 else
11856 node = patch_switch_statement (node);
11858 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11859 nn = error_mark_node;
11860 else
11862 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11863 /* If we returned something different, that's because we
11864 inserted a label. Pop the label too. */
11865 if (nn != node)
11867 if (CAN_COMPLETE_NORMALLY (node))
11868 CAN_COMPLETE_NORMALLY (nn) = 1;
11869 POP_LABELED_BLOCK ();
11872 POP_LOOP ();
11873 return nn;
11875 case EXIT_EXPR:
11876 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11877 return patch_exit_expr (node);
11879 case COND_EXPR:
11880 /* Condition */
11881 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11882 if (TREE_OPERAND (node, 0) == error_mark_node)
11883 return error_mark_node;
11884 /* then-else branches */
11885 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11886 if (TREE_OPERAND (node, 1) == error_mark_node)
11887 return error_mark_node;
11889 /* This is a special case due to build_assertion(). When
11890 assertions are disabled we build a COND_EXPR in which
11891 Operand 1 is the body of the assertion. If that happens to
11892 be a string concatenation we'll need to patch it here. */
11893 tree patched = patch_string (TREE_OPERAND (node, 1));
11894 if (patched)
11895 TREE_OPERAND (node, 1) = patched;
11897 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11898 if (TREE_OPERAND (node, 2) == error_mark_node)
11899 return error_mark_node;
11900 return patch_if_else_statement (node);
11901 break;
11903 case CONDITIONAL_EXPR:
11904 /* Condition */
11905 wfl_op1 = TREE_OPERAND (node, 0);
11906 COMPLETE_CHECK_OP_0 (node);
11907 wfl_op2 = TREE_OPERAND (node, 1);
11908 COMPLETE_CHECK_OP_1 (node);
11909 wfl_op3 = TREE_OPERAND (node, 2);
11910 COMPLETE_CHECK_OP_2 (node);
11911 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11913 /* 3- Expression section */
11914 case COMPOUND_EXPR:
11915 wfl_op2 = TREE_OPERAND (node, 1);
11916 TREE_OPERAND (node, 0) = nn =
11917 java_complete_tree (TREE_OPERAND (node, 0));
11918 if (IS_EMPTY_STMT (wfl_op2))
11919 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11920 else
11922 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11924 /* An unreachable condition in a do-while statement
11925 is *not* (technically) an unreachable statement. */
11926 nn = wfl_op2;
11927 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11928 nn = EXPR_WFL_NODE (nn);
11929 /* NN can be NULL_TREE exactly when UPDATE is, in
11930 finish_for_loop. */
11931 if (nn != NULL_TREE && TREE_CODE (nn) != EXIT_EXPR)
11933 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11934 if (SUPPRESS_UNREACHABLE_ERROR (nn))
11936 /* Perhaps this warning should have an
11937 associated flag. The code being compiled is
11938 pedantically correct, but useless. */
11939 parse_warning_context (wfl_operator,
11940 "Unreachable statement");
11942 else
11943 parse_error_context (wfl_operator,
11944 "Unreachable statement");
11947 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11948 if (TREE_OPERAND (node, 1) == error_mark_node)
11949 return error_mark_node;
11950 /* Even though we might allow the case where the first
11951 operand doesn't return normally, we still should compute
11952 CAN_COMPLETE_NORMALLY correctly. */
11953 CAN_COMPLETE_NORMALLY (node)
11954 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11955 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11957 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11958 break;
11960 case RETURN_EXPR:
11961 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11962 return patch_return (node);
11964 case EXPR_WITH_FILE_LOCATION:
11965 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11966 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11968 tree wfl = node;
11969 node = resolve_expression_name (node, NULL);
11970 if (node == error_mark_node)
11971 return node;
11972 /* Keep line number information somewhere were it doesn't
11973 disrupt the completion process. */
11974 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11976 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11977 TREE_OPERAND (node, 1) = wfl;
11979 CAN_COMPLETE_NORMALLY (node) = 1;
11981 else
11983 tree body;
11984 location_t save_location = input_location;
11985 #ifdef USE_MAPPED_LOCATION
11986 input_location = EXPR_LOCATION (node);
11987 if (input_location == UNKNOWN_LOCATION)
11988 input_location = save_location;
11989 #else
11990 input_line = EXPR_WFL_LINENO (node);
11991 #endif
11992 body = java_complete_tree (EXPR_WFL_NODE (node));
11993 input_location = save_location;
11994 EXPR_WFL_NODE (node) = body;
11995 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11996 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11997 if (IS_EMPTY_STMT (body) || TREE_CONSTANT (body))
11999 /* Makes it easier to constant fold, detect empty bodies. */
12000 return body;
12002 if (body == error_mark_node)
12004 /* Its important for the evaluation of assignment that
12005 this mark on the TREE_TYPE is propagated. */
12006 TREE_TYPE (node) = error_mark_node;
12007 return error_mark_node;
12009 else
12010 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
12013 break;
12015 case NEW_ARRAY_EXPR:
12016 /* Patch all the dimensions */
12017 flag = 0;
12018 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12020 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
12021 tree dim = convert (int_type_node,
12022 java_complete_tree (TREE_VALUE (cn)));
12023 if (dim == error_mark_node)
12025 flag = 1;
12026 continue;
12028 else
12030 TREE_VALUE (cn) = dim;
12031 /* Setup the location of the current dimension, for
12032 later error report. */
12033 #ifdef USE_MAPPED_LOCATION
12034 TREE_PURPOSE (cn) = expr_add_location (NULL_TREE, location, 0);
12035 #else
12036 TREE_PURPOSE (cn) =
12037 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
12038 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
12039 #endif
12042 /* They complete the array creation expression, if no errors
12043 were found. */
12044 CAN_COMPLETE_NORMALLY (node) = 1;
12045 return (flag ? error_mark_node
12046 : force_evaluation_order (patch_newarray (node)));
12048 case NEW_ANONYMOUS_ARRAY_EXPR:
12049 /* Create the array type if necessary. */
12050 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
12052 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
12053 if (!(type = resolve_type_during_patch (type)))
12054 return error_mark_node;
12055 type = build_array_from_name (type, NULL_TREE,
12056 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
12057 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
12059 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
12060 ANONYMOUS_ARRAY_INITIALIZER (node));
12061 if (node == error_mark_node)
12062 return error_mark_node;
12063 CAN_COMPLETE_NORMALLY (node) = 1;
12064 return node;
12066 case NEW_CLASS_EXPR:
12067 case CALL_EXPR:
12068 /* Complete function's argument(s) first */
12069 if (complete_function_arguments (node))
12070 return error_mark_node;
12071 else
12073 tree decl, wfl = TREE_OPERAND (node, 0);
12074 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
12075 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
12076 super_identifier_node);
12077 tree arguments;
12078 #ifdef USE_MAPPED_LOCATION
12079 source_location location = EXPR_LOCATION (node);
12080 #else
12081 int location = EXPR_WFL_LINECOL (node);
12082 #endif
12084 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12085 from_super, 0, &decl);
12086 if (node == error_mark_node)
12087 return error_mark_node;
12089 if (TREE_CODE (node) == CALL_EXPR
12090 && TREE_OPERAND (node, 1) != NULL_TREE)
12091 arguments = TREE_VALUE (TREE_OPERAND (node, 1));
12092 else
12093 arguments = NULL_TREE;
12094 check_thrown_exceptions (location, decl, arguments);
12095 /* If we call this(...), register signature and positions */
12096 if (in_this)
12097 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12098 tree_cons (wfl, decl,
12099 DECL_CONSTRUCTOR_CALLS (current_function_decl));
12100 CAN_COMPLETE_NORMALLY (node) = 1;
12101 return force_evaluation_order (node);
12104 case MODIFY_EXPR:
12105 /* Save potential wfls */
12106 wfl_op1 = TREE_OPERAND (node, 0);
12107 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12109 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12110 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12111 && DECL_INITIAL (nn) != NULL_TREE)
12113 tree value;
12115 value = fold_constant_for_init (nn, nn);
12117 /* When we have a primitype type, or a string and we're not
12118 emitting a class file, we actually don't want to generate
12119 anything for the assignment. */
12120 if (value != NULL_TREE &&
12121 (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
12122 (TREE_TYPE (value) == string_ptr_type_node &&
12123 ! flag_emit_class_files)))
12125 /* Prepare node for patch_assignment */
12126 TREE_OPERAND (node, 1) = value;
12127 /* Call patch assignment to verify the assignment */
12128 if (patch_assignment (node, wfl_op1) == error_mark_node)
12129 return error_mark_node;
12130 /* Set DECL_INITIAL properly (a conversion might have
12131 been decided by patch_assignment) and return the
12132 empty statement. */
12133 else
12135 tree patched = patch_string (TREE_OPERAND (node, 1));
12136 if (patched)
12137 DECL_INITIAL (nn) = patched;
12138 else
12139 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12140 DECL_FIELD_FINAL_IUD (nn) = 1;
12141 return build_java_empty_stmt ();
12144 if (! flag_emit_class_files)
12145 DECL_INITIAL (nn) = NULL_TREE;
12147 wfl_op2 = TREE_OPERAND (node, 1);
12149 if (TREE_OPERAND (node, 0) == error_mark_node)
12150 return error_mark_node;
12152 flag = COMPOUND_ASSIGN_P (wfl_op2);
12153 if (flag)
12155 /* This might break when accessing outer field from inner
12156 class. TESTME, FIXME */
12157 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12159 /* Hand stabilize the lhs on both places */
12160 TREE_OPERAND (node, 0) = lvalue;
12161 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12162 (flag_emit_class_files ? lvalue : save_expr (lvalue));
12164 /* 15.25.2.a: Left hand is not an array access. FIXME */
12165 /* Now complete the RHS. We write it back later on. */
12166 nn = java_complete_tree (TREE_OPERAND (node, 1));
12168 if ((cn = patch_string (nn)))
12169 nn = cn;
12171 /* The last part of the rewrite for E1 op= E2 is to have
12172 E1 = (T)(E1 op E2), with T being the type of E1. */
12173 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12174 TREE_TYPE (lvalue), nn));
12176 /* If the assignment is compound and has reference type,
12177 then ensure the LHS has type String and nothing else. */
12178 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12179 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12180 parse_error_context (wfl_op2,
12181 "Incompatible type for %<+=%>. Can't convert %qs to %<java.lang.String%>",
12182 lang_printable_name (TREE_TYPE (lvalue), 0));
12184 /* 15.25.2.b: Left hand is an array access. FIXME */
12187 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12188 function to complete this RHS. Note that a NEW_ARRAY_INIT
12189 might have been already fully expanded if created as a result
12190 of processing an anonymous array initializer. We avoid doing
12191 the operation twice by testing whether the node already bears
12192 a type. */
12193 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12194 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12195 TREE_OPERAND (node, 1));
12196 /* Otherwise we simply complete the RHS */
12197 else
12198 nn = java_complete_tree (TREE_OPERAND (node, 1));
12200 if (nn == error_mark_node)
12201 return error_mark_node;
12203 /* Write back the RHS as we evaluated it. */
12204 TREE_OPERAND (node, 1) = nn;
12206 /* In case we're handling = with a String as a RHS, we need to
12207 produce a String out of the RHS (it might still be a
12208 STRING_CST or a StringBuffer at this stage */
12209 if ((nn = patch_string (TREE_OPERAND (node, 1))))
12210 TREE_OPERAND (node, 1) = nn;
12212 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12213 TREE_OPERAND (node, 1))))
12215 /* We return error_mark_node if outer_field_access_fix
12216 detects we write into a final. */
12217 if (nn == error_mark_node)
12218 return error_mark_node;
12219 node = nn;
12221 else
12223 node = patch_assignment (node, wfl_op1);
12224 if (node == error_mark_node)
12225 return error_mark_node;
12226 /* Reorganize the tree if necessary. */
12227 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12228 || JSTRING_P (TREE_TYPE (node))))
12229 node = java_refold (node);
12232 /* Seek to set DECL_INITIAL to a proper value, since it might have
12233 undergone a conversion in patch_assignment. We do that only when
12234 it's necessary to have DECL_INITIAL properly set. */
12235 nn = TREE_OPERAND (node, 0);
12236 if (TREE_CODE (nn) == VAR_DECL
12237 && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12238 && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12239 && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12240 || TREE_TYPE (nn) == string_ptr_type_node))
12241 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12243 CAN_COMPLETE_NORMALLY (node) = 1;
12244 return node;
12246 case MULT_EXPR:
12247 case PLUS_EXPR:
12248 case MINUS_EXPR:
12249 case LSHIFT_EXPR:
12250 case RSHIFT_EXPR:
12251 case URSHIFT_EXPR:
12252 case BIT_AND_EXPR:
12253 case BIT_XOR_EXPR:
12254 case BIT_IOR_EXPR:
12255 case TRUNC_MOD_EXPR:
12256 case TRUNC_DIV_EXPR:
12257 case RDIV_EXPR:
12258 case TRUTH_ANDIF_EXPR:
12259 case TRUTH_ORIF_EXPR:
12260 case EQ_EXPR:
12261 case NE_EXPR:
12262 case GT_EXPR:
12263 case GE_EXPR:
12264 case LT_EXPR:
12265 case LE_EXPR:
12266 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12267 knows how to handle those cases. */
12268 wfl_op1 = TREE_OPERAND (node, 0);
12269 wfl_op2 = TREE_OPERAND (node, 1);
12271 CAN_COMPLETE_NORMALLY (node) = 1;
12272 /* Don't complete string nodes if dealing with the PLUS operand. */
12273 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12275 nn = java_complete_tree (wfl_op1);
12276 if (nn == error_mark_node)
12277 return error_mark_node;
12279 TREE_OPERAND (node, 0) = nn;
12281 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12283 nn = java_complete_tree (wfl_op2);
12284 if (nn == error_mark_node)
12285 return error_mark_node;
12287 TREE_OPERAND (node, 1) = nn;
12289 return patch_binop (node, wfl_op1, wfl_op2);
12291 case INSTANCEOF_EXPR:
12292 wfl_op1 = TREE_OPERAND (node, 0);
12293 COMPLETE_CHECK_OP_0 (node);
12294 if (flag_emit_xref)
12296 TREE_TYPE (node) = boolean_type_node;
12297 return node;
12299 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
12301 case UNARY_PLUS_EXPR:
12302 case NEGATE_EXPR:
12303 case TRUTH_NOT_EXPR:
12304 case BIT_NOT_EXPR:
12305 case PREDECREMENT_EXPR:
12306 case PREINCREMENT_EXPR:
12307 case POSTDECREMENT_EXPR:
12308 case POSTINCREMENT_EXPR:
12309 case CONVERT_EXPR:
12310 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12311 how to handle those cases. */
12312 wfl_op1 = TREE_OPERAND (node, 0);
12313 CAN_COMPLETE_NORMALLY (node) = 1;
12314 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12315 if (TREE_OPERAND (node, 0) == error_mark_node)
12316 return error_mark_node;
12317 node = patch_unaryop (node, wfl_op1);
12318 CAN_COMPLETE_NORMALLY (node) = 1;
12319 break;
12321 case ARRAY_REF:
12322 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12323 how to handle those cases. */
12324 wfl_op1 = TREE_OPERAND (node, 0);
12325 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12326 if (TREE_OPERAND (node, 0) == error_mark_node)
12327 return error_mark_node;
12328 if (!flag_emit_class_files && !flag_emit_xref)
12329 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12330 /* The same applies to wfl_op2 */
12331 wfl_op2 = TREE_OPERAND (node, 1);
12332 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12333 if (TREE_OPERAND (node, 1) == error_mark_node)
12334 return error_mark_node;
12335 if (!flag_emit_class_files && !flag_emit_xref)
12336 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12337 return patch_array_ref (node);
12339 case RECORD_TYPE:
12340 return node;;
12342 case COMPONENT_REF:
12343 /* The first step in the re-write of qualified name handling. FIXME.
12344 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12345 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12346 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12348 tree name = TREE_OPERAND (node, 1);
12349 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12350 if (field == NULL_TREE)
12352 error ("missing static field %qs", IDENTIFIER_POINTER (name));
12353 return error_mark_node;
12355 if (! FIELD_STATIC (field))
12357 error ("not a static field %qs", IDENTIFIER_POINTER (name));
12358 return error_mark_node;
12360 return field;
12362 else
12363 abort ();
12364 break;
12366 case THIS_EXPR:
12367 /* Can't use THIS in a static environment */
12368 if (!current_this)
12370 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12371 parse_error_context (wfl_operator,
12372 "Keyword %<this%> used outside allowed context");
12373 TREE_TYPE (node) = error_mark_node;
12374 return error_mark_node;
12376 if (ctxp->explicit_constructor_p)
12378 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12379 parse_error_context
12380 (wfl_operator, "Can't reference %<this%> or %<super%> before the superclass constructor has been called");
12381 TREE_TYPE (node) = error_mark_node;
12382 return error_mark_node;
12384 return current_this;
12386 case CLASS_LITERAL:
12387 CAN_COMPLETE_NORMALLY (node) = 1;
12388 node = patch_incomplete_class_ref (node);
12389 if (node == error_mark_node)
12390 return error_mark_node;
12391 break;
12393 default:
12394 CAN_COMPLETE_NORMALLY (node) = 1;
12395 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12396 and it's time to turn it into the appropriate String object */
12397 if ((nn = patch_string (node)))
12398 node = nn;
12399 else
12400 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12402 return node;
12405 /* Complete function call's argument. Return a nonzero value is an
12406 error was found. */
12408 static int
12409 complete_function_arguments (tree node)
12411 int flag = 0;
12412 tree cn;
12414 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12415 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12417 tree wfl = TREE_VALUE (cn), parm, temp;
12418 parm = java_complete_tree (wfl);
12420 if (parm == error_mark_node)
12422 flag = 1;
12423 continue;
12425 /* If we have a string literal that we haven't transformed yet or a
12426 crafted string buffer, as a result of the use of the String
12427 `+' operator. Build `parm.toString()' and expand it. */
12428 if ((temp = patch_string (parm)))
12429 parm = temp;
12431 TREE_VALUE (cn) = parm;
12433 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12434 return flag;
12437 /* Sometimes (for loops and variable initialized during their
12438 declaration), we want to wrap a statement around a WFL and turn it
12439 debugable. */
12441 static tree
12442 build_debugable_stmt (int location, tree stmt)
12444 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12446 #ifdef USE_MAPPED_LOCATION
12447 stmt = expr_add_location (stmt, location, 1);
12448 #else
12449 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12450 EXPR_WFL_LINECOL (stmt) = location;
12451 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12452 #endif
12454 return stmt;
12457 static tree
12458 build_expr_block (tree body, tree decls)
12460 tree node = make_node (BLOCK);
12461 BLOCK_EXPR_DECLS (node) = decls;
12462 BLOCK_EXPR_BODY (node) = body;
12463 if (body)
12464 TREE_TYPE (node) = TREE_TYPE (body);
12465 TREE_SIDE_EFFECTS (node) = 1;
12466 return node;
12469 /* Create a new function block and link it appropriately to current
12470 function block chain */
12472 static tree
12473 enter_block (void)
12475 tree b = build_expr_block (NULL_TREE, NULL_TREE);
12477 /* Link block B supercontext to the previous block. The current
12478 function DECL is used as supercontext when enter_a_block is called
12479 for the first time for a given function. The current function body
12480 (DECL_FUNCTION_BODY) is set to be block B. */
12482 tree fndecl = current_function_decl;
12484 if (!fndecl) {
12485 BLOCK_SUPERCONTEXT (b) = current_static_block;
12486 current_static_block = b;
12489 else if (!DECL_FUNCTION_BODY (fndecl))
12491 BLOCK_SUPERCONTEXT (b) = fndecl;
12492 DECL_FUNCTION_BODY (fndecl) = b;
12494 else
12496 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12497 DECL_FUNCTION_BODY (fndecl) = b;
12499 return b;
12502 /* Exit a block by changing the current function body
12503 (DECL_FUNCTION_BODY) to the current block super context, only if
12504 the block being exited isn't the method's top level one. */
12506 static tree
12507 exit_block (void)
12509 tree b;
12510 if (current_function_decl)
12512 b = DECL_FUNCTION_BODY (current_function_decl);
12513 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12514 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12516 else
12518 b = current_static_block;
12520 if (BLOCK_SUPERCONTEXT (b))
12521 current_static_block = BLOCK_SUPERCONTEXT (b);
12523 return b;
12526 /* Lookup for NAME in the nested function's blocks, all the way up to
12527 the current toplevel one. It complies with Java's local variable
12528 scoping rules. */
12530 static tree
12531 lookup_name_in_blocks (tree name)
12533 tree b = GET_CURRENT_BLOCK (current_function_decl);
12535 while (b != current_function_decl)
12537 tree current;
12539 /* Paranoid sanity check. To be removed */
12540 if (TREE_CODE (b) != BLOCK)
12541 abort ();
12543 for (current = BLOCK_EXPR_DECLS (b); current;
12544 current = TREE_CHAIN (current))
12545 if (DECL_NAME (current) == name)
12546 return current;
12547 b = BLOCK_SUPERCONTEXT (b);
12549 return NULL_TREE;
12552 static void
12553 maybe_absorb_scoping_blocks (void)
12555 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12557 tree b = exit_block ();
12558 java_method_add_stmt (current_function_decl, b);
12559 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", input_line));
12564 /* This section of the source is reserved to build_* functions that
12565 are building incomplete tree nodes and the patch_* functions that
12566 are completing them. */
12568 /* Wrap a non WFL node around a WFL. */
12570 static tree
12571 build_wfl_wrap (tree node, int location)
12573 tree wfl, node_to_insert = node;
12575 /* We want to process THIS . xxx symbolically, to keep it consistent
12576 with the way we're processing SUPER. A THIS from a primary as a
12577 different form than a SUPER. Turn THIS into something symbolic */
12578 if (TREE_CODE (node) == THIS_EXPR)
12579 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12580 else
12581 #ifdef USE_MAPPED_LOCATION
12582 wfl = build_unknown_wfl (NULL_TREE);
12584 SET_EXPR_LOCATION (wfl, location);
12585 #else
12586 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12588 EXPR_WFL_LINECOL (wfl) = location;
12589 #endif
12590 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12591 return wfl;
12594 /* Build a super() constructor invocation. Returns an empty statement if
12595 we're currently dealing with the class java.lang.Object. */
12597 static tree
12598 build_super_invocation (tree mdecl)
12600 if (DECL_CONTEXT (mdecl) == object_type_node)
12601 return build_java_empty_stmt ();
12602 else
12604 tree super_wfl = build_wfl_node (super_identifier_node);
12605 tree a = NULL_TREE, t;
12607 /* This is called after parsing is done, so the parser context
12608 won't be accurate. Set location info from current_class decl. */
12609 tree class_wfl = lookup_cl (TYPE_NAME (current_class));
12610 EXPR_WFL_LINECOL (super_wfl) = EXPR_WFL_LINECOL (class_wfl);
12612 /* If we're dealing with an anonymous class, pass the arguments
12613 of the crafted constructor along. */
12614 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12616 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12617 for (; t != end_params_node; t = TREE_CHAIN (t))
12618 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12620 return build_method_invocation (super_wfl, a);
12624 /* Build a SUPER/THIS qualified method invocation. */
12626 static tree
12627 build_this_super_qualified_invocation (int use_this, tree name, tree args,
12628 int lloc, int rloc)
12630 tree invok;
12631 tree wfl =
12632 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12633 EXPR_WFL_LINECOL (wfl) = lloc;
12634 invok = build_method_invocation (name, args);
12635 return make_qualified_primary (wfl, invok, rloc);
12638 /* Build an incomplete CALL_EXPR node. */
12640 static tree
12641 build_method_invocation (tree name, tree args)
12643 tree call = build3 (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12644 TREE_SIDE_EFFECTS (call) = 1;
12645 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12646 return call;
12649 /* Build an incomplete new xxx(...) node. */
12651 static tree
12652 build_new_invocation (tree name, tree args)
12654 tree call = build3 (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12655 TREE_SIDE_EFFECTS (call) = 1;
12656 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12657 return call;
12660 /* Build an incomplete assignment expression. */
12662 static tree
12663 build_assignment (int op, int op_location, tree lhs, tree rhs)
12665 tree assignment;
12666 /* Build the corresponding binop if we deal with a Compound
12667 Assignment operator. Mark the binop sub-tree as part of a
12668 Compound Assignment expression */
12669 if (op != ASSIGN_TK)
12671 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12672 COMPOUND_ASSIGN_P (rhs) = 1;
12674 assignment = build2 (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12675 TREE_SIDE_EFFECTS (assignment) = 1;
12676 EXPR_WFL_LINECOL (assignment) = op_location;
12677 return assignment;
12680 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12681 the buffer. This is used only for string conversion. */
12682 static char *
12683 string_convert_int_cst (tree node)
12685 /* Long.MIN_VALUE is -9223372036854775808, 20 characters. */
12686 static char buffer[21];
12688 unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12689 unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12690 char *p = buffer + sizeof (buffer);
12691 int neg = 0;
12693 unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12694 << (HOST_BITS_PER_WIDE_INT - 1));
12696 *--p = '\0';
12698 /* If negative, note the fact and negate the value. */
12699 if ((hi & hibit))
12701 lo = ~lo;
12702 hi = ~hi;
12703 if (++lo == 0)
12704 ++hi;
12705 neg = 1;
12708 /* Divide by 10 until there are no bits left. */
12711 unsigned HOST_WIDE_INT acc = 0;
12712 unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12713 unsigned int i;
12715 /* Use long division to compute the result and the remainder. */
12716 for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12718 /* Shift a bit into accumulator. */
12719 acc <<= 1;
12720 if ((hi & hibit))
12721 acc |= 1;
12723 /* Shift the value. */
12724 hi <<= 1;
12725 if ((lo & hibit))
12726 hi |= 1;
12727 lo <<= 1;
12729 /* Shift the correct bit into the result. */
12730 outhi <<= 1;
12731 if ((outlo & hibit))
12732 outhi |= 1;
12733 outlo <<= 1;
12734 if (acc >= 10)
12736 acc -= 10;
12737 outlo |= 1;
12741 /* '0' == 060 in Java, but might not be here (think EBCDIC). */
12742 *--p = '\060' + acc;
12744 hi = outhi;
12745 lo = outlo;
12747 while (hi || lo);
12749 if (neg)
12750 *--p = '\055'; /* '-' == 055 in Java, but might not be here. */
12752 return p;
12755 /* Print an INTEGER_CST node in a static buffer, and return the
12756 buffer. This is used only for error handling. */
12757 char *
12758 print_int_node (tree node)
12760 static char buffer [80];
12761 if (TREE_CONSTANT_OVERFLOW (node))
12762 sprintf (buffer, "<overflow>");
12764 if (TREE_INT_CST_HIGH (node) == 0)
12765 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12766 TREE_INT_CST_LOW (node));
12767 else if (TREE_INT_CST_HIGH (node) == -1
12768 && TREE_INT_CST_LOW (node) != 0)
12769 sprintf (buffer, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
12770 -TREE_INT_CST_LOW (node));
12771 else
12772 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12773 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12775 return buffer;
12779 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12780 context. */
12782 /* 15.25 Assignment operators. */
12784 static tree
12785 patch_assignment (tree node, tree wfl_op1)
12787 tree rhs = TREE_OPERAND (node, 1);
12788 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12789 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12790 int error_found = 0;
12791 int lvalue_from_array = 0;
12792 int is_return = 0;
12794 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12796 /* Lhs can be a named variable */
12797 if (JDECL_P (lvalue))
12799 lhs_type = TREE_TYPE (lvalue);
12801 /* Or Lhs can be an array access. */
12802 else if (TREE_CODE (lvalue) == ARRAY_REF)
12804 lhs_type = TREE_TYPE (lvalue);
12805 lvalue_from_array = 1;
12807 /* Or a field access */
12808 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12809 lhs_type = TREE_TYPE (lvalue);
12810 /* Or a function return slot */
12811 else if (TREE_CODE (lvalue) == RESULT_DECL)
12813 /* If the return type is an integral type, then we create the
12814 RESULT_DECL with a promoted type, but we need to do these
12815 checks against the unpromoted type to ensure type safety. So
12816 here we look at the real type, not the type of the decl we
12817 are modifying. */
12818 lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12819 is_return = 1;
12821 /* Otherwise, we might want to try to write into an optimized static
12822 final, this is an of a different nature, reported further on. */
12823 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12824 && resolve_expression_name (wfl_op1, &llvalue))
12826 lhs_type = TREE_TYPE (lvalue);
12828 else
12830 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12831 error_found = 1;
12834 rhs_type = TREE_TYPE (rhs);
12836 /* 5.1 Try the assignment conversion for builtin type. */
12837 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12839 /* 5.2 If it failed, try a reference conversion */
12840 if (!new_rhs)
12841 new_rhs = try_reference_assignconv (lhs_type, rhs);
12843 /* 15.25.2 If we have a compound assignment, convert RHS into the
12844 type of the LHS */
12845 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12846 new_rhs = convert (lhs_type, rhs);
12848 /* Explicit cast required. This is an error */
12849 if (!new_rhs)
12851 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12852 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12853 tree wfl;
12854 char operation [32]; /* Max size known */
12856 /* If the assignment is part of a declaration, we use the WFL of
12857 the declared variable to point out the error and call it a
12858 declaration problem. If the assignment is a genuine =
12859 operator, we call is a operator `=' problem, otherwise we
12860 call it an assignment problem. In both of these last cases,
12861 we use the WFL of the operator to indicate the error. */
12863 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12865 wfl = wfl_op1;
12866 strcpy (operation, "declaration");
12868 else
12870 wfl = wfl_operator;
12871 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12872 strcpy (operation, "assignment");
12873 else if (is_return)
12874 strcpy (operation, "'return'");
12875 else
12876 strcpy (operation, "'='");
12879 if (!valid_cast_to_p (rhs_type, lhs_type))
12880 parse_error_context
12881 (wfl, "Incompatible type for %s. Can't convert %qs to %qs",
12882 operation, t1, t2);
12883 else
12884 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert %qs to %qs",
12885 operation, t1, t2);
12886 free (t1); free (t2);
12887 error_found = 1;
12890 if (error_found)
12891 return error_mark_node;
12893 /* If we're processing a `return' statement, promote the actual type
12894 to the promoted type. */
12895 if (is_return)
12896 new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12898 /* 10.10: Array Store Exception runtime check */
12899 if (!flag_emit_class_files
12900 && !flag_emit_xref
12901 && lvalue_from_array
12902 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12904 tree array, store_check, base, index_expr;
12906 /* Save RHS so that it doesn't get re-evaluated by the store check. */
12907 new_rhs = save_expr (new_rhs);
12909 /* Get the INDIRECT_REF. */
12910 array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12911 /* Get the array pointer expr. */
12912 array = TREE_OPERAND (array, 0);
12913 store_check = build_java_arraystore_check (array, new_rhs);
12915 index_expr = TREE_OPERAND (lvalue, 1);
12917 if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12919 /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12920 happen before the store check, so prepare to insert the store
12921 check within the second operand of the existing COMPOUND_EXPR. */
12922 base = index_expr;
12924 else
12925 base = lvalue;
12927 index_expr = TREE_OPERAND (base, 1);
12928 TREE_OPERAND (base, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (index_expr),
12929 store_check, index_expr);
12932 /* Final locals can be used as case values in switch
12933 statement. Prepare them for this eventuality. */
12934 if (TREE_CODE (lvalue) == VAR_DECL
12935 && DECL_FINAL (lvalue)
12936 && TREE_CONSTANT (new_rhs)
12937 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12938 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12941 TREE_CONSTANT (lvalue) = 1;
12942 TREE_INVARIANT (lvalue) = 1;
12943 DECL_INITIAL (lvalue) = new_rhs;
12946 /* Copy the rhs if it's a reference. */
12947 if (! flag_check_references && ! flag_emit_class_files && optimize > 0)
12949 switch (TREE_CODE (new_rhs))
12951 case ARRAY_REF:
12952 case INDIRECT_REF:
12953 case COMPONENT_REF:
12954 /* Transform a = foo.bar
12955 into a = ({int tmp; tmp = foo.bar;}).
12956 We need to ensure that if a read from memory fails
12957 because of a NullPointerException, a destination variable
12958 will remain unchanged. An explicit temporary does what
12959 we need.
12961 If flag_check_references is set, this is unnecessary
12962 because we'll check each reference before doing any
12963 reads. If optimize is not set the result will never be
12964 written to a stack slot that contains the LHS. */
12966 tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"),
12967 TREE_TYPE (new_rhs));
12968 tree block = make_node (BLOCK);
12969 tree assignment
12970 = build2 (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
12971 DECL_CONTEXT (tmp) = current_function_decl;
12972 TREE_TYPE (block) = TREE_TYPE (new_rhs);
12973 BLOCK_VARS (block) = tmp;
12974 BLOCK_EXPR_BODY (block) = assignment;
12975 TREE_SIDE_EFFECTS (block) = 1;
12976 new_rhs = block;
12978 break;
12979 default:
12980 break;
12984 TREE_OPERAND (node, 0) = lvalue;
12985 TREE_OPERAND (node, 1) = new_rhs;
12986 TREE_TYPE (node) = lhs_type;
12987 return node;
12990 /* Check that type SOURCE can be cast into type DEST. If the cast
12991 can't occur at all, return NULL; otherwise, return a possibly
12992 modified rhs. */
12994 static tree
12995 try_reference_assignconv (tree lhs_type, tree rhs)
12997 tree new_rhs = NULL_TREE;
12998 tree rhs_type = TREE_TYPE (rhs);
13000 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
13002 /* `null' may be assigned to any reference type */
13003 if (rhs == null_pointer_node)
13004 new_rhs = null_pointer_node;
13005 /* Try the reference assignment conversion */
13006 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
13007 new_rhs = rhs;
13008 /* This is a magic assignment that we process differently */
13009 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
13010 new_rhs = rhs;
13012 return new_rhs;
13015 /* Check that RHS can be converted into LHS_TYPE by the assignment
13016 conversion (5.2), for the cases of RHS being a builtin type. Return
13017 NULL_TREE if the conversion fails or if because RHS isn't of a
13018 builtin type. Return a converted RHS if the conversion is possible. */
13020 static tree
13021 try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
13023 tree new_rhs = NULL_TREE;
13024 tree rhs_type = TREE_TYPE (rhs);
13026 /* Handle boolean specially. */
13027 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13028 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13030 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13031 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13032 new_rhs = rhs;
13035 /* 5.1.1 Try Identity Conversion,
13036 5.1.2 Try Widening Primitive Conversion */
13037 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
13038 new_rhs = convert (lhs_type, rhs);
13040 /* Try a narrowing primitive conversion (5.1.3):
13041 - expression is a constant expression of type byte, short, char,
13042 or int, AND
13043 - variable is byte, short or char AND
13044 - The value of the expression is representable in the type of the
13045 variable */
13046 else if ((rhs_type == byte_type_node || rhs_type == short_type_node
13047 || rhs_type == char_type_node || rhs_type == int_type_node)
13048 && TREE_CONSTANT (rhs)
13049 && (lhs_type == byte_type_node || lhs_type == char_type_node
13050 || lhs_type == short_type_node))
13052 if (int_fits_type_p (rhs, lhs_type))
13053 new_rhs = convert (lhs_type, rhs);
13054 else if (wfl_op1) /* Might be called with a NULL */
13055 parse_warning_context
13056 (wfl_op1,
13057 "Constant expression %qs too wide for narrowing primitive conversion to %qs",
13058 print_int_node (rhs), lang_printable_name (lhs_type, 0));
13059 /* Reported a warning that will turn into an error further
13060 down, so we don't return */
13063 return new_rhs;
13066 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
13067 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
13068 0 is the conversion test fails. This implements parts the method
13069 invocation conversion (5.3). */
13071 static int
13072 valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
13074 /* 5.1.1: This is the identity conversion part. */
13075 if (lhs_type == rhs_type)
13076 return 1;
13078 /* Reject non primitive types and boolean conversions. */
13079 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
13080 return 0;
13082 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
13083 than a char can't be converted into a char. Short can't too, but
13084 the < test below takes care of that */
13085 if (lhs_type == char_type_node && rhs_type == byte_type_node)
13086 return 0;
13088 /* Accept all promoted type here. Note, we can't use <= in the test
13089 below, because we still need to bounce out assignments of short
13090 to char and the likes */
13091 if (lhs_type == int_type_node
13092 && (rhs_type == promoted_byte_type_node
13093 || rhs_type == promoted_short_type_node
13094 || rhs_type == promoted_char_type_node
13095 || rhs_type == promoted_boolean_type_node))
13096 return 1;
13098 /* From here, an integral is widened if its precision is smaller
13099 than the precision of the LHS or if the LHS is a floating point
13100 type, or the RHS is a float and the RHS a double. */
13101 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13102 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13103 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13104 || (rhs_type == float_type_node && lhs_type == double_type_node))
13105 return 1;
13107 return 0;
13110 /* Check that something of SOURCE type can be assigned or cast to
13111 something of DEST type at runtime. Return 1 if the operation is
13112 valid, 0 otherwise. If CAST is set to 1, we're treating the case
13113 were SOURCE is cast into DEST, which borrows a lot of the
13114 assignment check. */
13116 static int
13117 valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
13119 /* SOURCE or DEST might be null if not from a declared entity. */
13120 if (!source || !dest)
13121 return 0;
13122 if (JNULLP_TYPE_P (source))
13123 return 1;
13124 if (TREE_CODE (source) == POINTER_TYPE)
13125 source = TREE_TYPE (source);
13126 if (TREE_CODE (dest) == POINTER_TYPE)
13127 dest = TREE_TYPE (dest);
13129 /* If source and dest are being compiled from bytecode, they may need to
13130 be loaded. */
13131 if (CLASS_P (source) && !CLASS_LOADED_P (source))
13133 load_class (source, 1);
13134 safe_layout_class (source);
13136 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13138 load_class (dest, 1);
13139 safe_layout_class (dest);
13142 /* Case where SOURCE is a class type */
13143 if (TYPE_CLASS_P (source))
13145 if (TYPE_CLASS_P (dest))
13146 return (source == dest
13147 || inherits_from_p (source, dest)
13148 || (cast && inherits_from_p (dest, source)));
13149 if (TYPE_INTERFACE_P (dest))
13151 /* If doing a cast and SOURCE is final, the operation is
13152 always correct a compile time (because even if SOURCE
13153 does not implement DEST, a subclass of SOURCE might). */
13154 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13155 return 1;
13156 /* Otherwise, SOURCE must implement DEST */
13157 return interface_of_p (dest, source);
13159 /* DEST is an array, cast permitted if SOURCE is of Object type */
13160 return (cast && source == object_type_node ? 1 : 0);
13162 if (TYPE_INTERFACE_P (source))
13164 if (TYPE_CLASS_P (dest))
13166 /* If not casting, DEST must be the Object type */
13167 if (!cast)
13168 return dest == object_type_node;
13169 /* We're doing a cast. The cast is always valid is class
13170 DEST is not final, otherwise, DEST must implement SOURCE */
13171 else if (!CLASS_FINAL (TYPE_NAME (dest)))
13172 return 1;
13173 else
13174 return interface_of_p (source, dest);
13176 if (TYPE_INTERFACE_P (dest))
13178 /* If doing a cast, then if SOURCE and DEST contain method
13179 with the same signature but different return type, then
13180 this is a (compile time) error */
13181 if (cast)
13183 tree method_source, method_dest;
13184 tree source_type;
13185 tree source_sig;
13186 tree source_name;
13187 for (method_source = TYPE_METHODS (source); method_source;
13188 method_source = TREE_CHAIN (method_source))
13190 source_sig =
13191 build_java_argument_signature (TREE_TYPE (method_source));
13192 source_type = TREE_TYPE (TREE_TYPE (method_source));
13193 source_name = DECL_NAME (method_source);
13194 for (method_dest = TYPE_METHODS (dest);
13195 method_dest; method_dest = TREE_CHAIN (method_dest))
13196 if (source_sig ==
13197 build_java_argument_signature (TREE_TYPE (method_dest))
13198 && source_name == DECL_NAME (method_dest)
13199 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13200 return 0;
13202 return 1;
13204 else
13205 return source == dest || interface_of_p (dest, source);
13207 else
13209 /* Array */
13210 return (cast
13211 && (DECL_NAME (TYPE_NAME (source))
13212 == java_lang_cloneable_identifier_node
13213 || (DECL_NAME (TYPE_NAME (source))
13214 == java_io_serializable_identifier_node)));
13217 if (TYPE_ARRAY_P (source))
13219 if (TYPE_CLASS_P (dest))
13220 return dest == object_type_node;
13221 /* Can't cast an array to an interface unless the interface is
13222 java.lang.Cloneable or java.io.Serializable. */
13223 if (TYPE_INTERFACE_P (dest))
13224 return (DECL_NAME (TYPE_NAME (dest))
13225 == java_lang_cloneable_identifier_node
13226 || (DECL_NAME (TYPE_NAME (dest))
13227 == java_io_serializable_identifier_node));
13228 else /* Arrays */
13230 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13231 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13233 /* In case of severe errors, they turn out null */
13234 if (!dest_element_type || !source_element_type)
13235 return 0;
13236 if (source_element_type == dest_element_type)
13237 return 1;
13238 return valid_ref_assignconv_cast_p (source_element_type,
13239 dest_element_type, cast);
13241 return 0;
13243 return 0;
13246 static int
13247 valid_cast_to_p (tree source, tree dest)
13249 if (TREE_CODE (source) == POINTER_TYPE)
13250 source = TREE_TYPE (source);
13251 if (TREE_CODE (dest) == POINTER_TYPE)
13252 dest = TREE_TYPE (dest);
13254 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13255 return valid_ref_assignconv_cast_p (source, dest, 1);
13257 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13258 return 1;
13260 else if (TREE_CODE (source) == BOOLEAN_TYPE
13261 && TREE_CODE (dest) == BOOLEAN_TYPE)
13262 return 1;
13264 return 0;
13267 static tree
13268 do_unary_numeric_promotion (tree arg)
13270 tree type = TREE_TYPE (arg);
13271 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13272 || TREE_CODE (type) == CHAR_TYPE)
13273 arg = convert (int_type_node, arg);
13274 return arg;
13277 /* Return a nonzero value if SOURCE can be converted into DEST using
13278 the method invocation conversion rule (5.3). */
13279 static int
13280 valid_method_invocation_conversion_p (tree dest, tree source)
13282 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13283 && valid_builtin_assignconv_identity_widening_p (dest, source))
13284 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13285 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13286 && valid_ref_assignconv_cast_p (source, dest, 0)));
13289 /* Build an incomplete binop expression. */
13291 static tree
13292 build_binop (enum tree_code op, int op_location, tree op1, tree op2)
13294 tree binop = build2 (op, NULL_TREE, op1, op2);
13295 TREE_SIDE_EFFECTS (binop) = 1;
13296 /* Store the location of the operator, for better error report. The
13297 string of the operator will be rebuild based on the OP value. */
13298 EXPR_WFL_LINECOL (binop) = op_location;
13299 return binop;
13302 /* Build the string of the operator retained by NODE. If NODE is part
13303 of a compound expression, add an '=' at the end of the string. This
13304 function is called when an error needs to be reported on an
13305 operator. The string is returned as a pointer to a static character
13306 buffer. */
13308 static char *
13309 operator_string (tree node)
13311 #define BUILD_OPERATOR_STRING(S) \
13313 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13314 return buffer; \
13317 static char buffer [10];
13318 switch (TREE_CODE (node))
13320 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13321 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13322 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13323 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13324 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13325 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13326 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13327 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13328 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13329 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13330 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13331 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13332 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13333 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13334 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13335 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13336 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13337 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13338 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13339 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13340 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13341 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13342 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13343 case PREINCREMENT_EXPR: /* Fall through */
13344 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13345 case PREDECREMENT_EXPR: /* Fall through */
13346 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13347 default:
13348 internal_error ("unregistered operator %s",
13349 tree_code_name [TREE_CODE (node)]);
13351 return NULL;
13352 #undef BUILD_OPERATOR_STRING
13355 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13357 static int
13358 java_decl_equiv (tree var_acc1, tree var_acc2)
13360 if (JDECL_P (var_acc1))
13361 return (var_acc1 == var_acc2);
13363 return (TREE_CODE (var_acc1) == COMPONENT_REF
13364 && TREE_CODE (var_acc2) == COMPONENT_REF
13365 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13366 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13367 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13370 /* Return a nonzero value if CODE is one of the operators that can be
13371 used in conjunction with the `=' operator in a compound assignment. */
13373 static int
13374 binop_compound_p (enum tree_code code)
13376 int i;
13377 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13378 if (binop_lookup [i] == code)
13379 break;
13381 return i < BINOP_COMPOUND_CANDIDATES;
13384 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13386 static tree
13387 java_refold (tree t)
13389 tree c, b, ns, decl;
13391 if (TREE_CODE (t) != MODIFY_EXPR)
13392 return t;
13394 c = TREE_OPERAND (t, 1);
13395 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13396 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13397 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13398 return t;
13400 /* Now the left branch of the binary operator. */
13401 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13402 if (! (b && TREE_CODE (b) == NOP_EXPR
13403 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13404 return t;
13406 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13407 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13408 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13409 return t;
13411 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13412 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13413 /* It's got to be the an equivalent decl */
13414 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13416 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13417 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13418 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13419 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13420 /* Change the right part of the BINOP_EXPR */
13421 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13424 return t;
13427 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13428 errors but we modify NODE so that it contains the type computed
13429 according to the expression, when it's fixed. Otherwise, we write
13430 error_mark_node as the type. It allows us to further the analysis
13431 of remaining nodes and detects more errors in certain cases. */
13433 static tree
13434 patch_binop (tree node, tree wfl_op1, tree wfl_op2)
13436 tree op1 = TREE_OPERAND (node, 0);
13437 tree op2 = TREE_OPERAND (node, 1);
13438 tree op1_type = TREE_TYPE (op1);
13439 tree op2_type = TREE_TYPE (op2);
13440 tree prom_type = NULL_TREE, cn;
13441 enum tree_code code = TREE_CODE (node);
13443 /* If 1, tell the routine that we have to return error_mark_node
13444 after checking for the initialization of the RHS */
13445 int error_found = 0;
13447 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13449 /* If either op<n>_type are NULL, this might be early signs of an
13450 error situation, unless it's too early to tell (in case we're
13451 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13452 correctly so the error can be later on reported accurately. */
13453 if (! (code == PLUS_EXPR || code == NE_EXPR
13454 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13456 tree n;
13457 if (! op1_type)
13459 n = java_complete_tree (op1);
13460 op1_type = TREE_TYPE (n);
13462 if (! op2_type)
13464 n = java_complete_tree (op2);
13465 op2_type = TREE_TYPE (n);
13469 switch (code)
13471 /* 15.16 Multiplicative operators */
13472 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13473 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13474 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13475 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13476 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13478 if (!JNUMERIC_TYPE_P (op1_type))
13479 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13480 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13481 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13482 TREE_TYPE (node) = error_mark_node;
13483 error_found = 1;
13484 break;
13486 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13488 /* Detect integral division by zero */
13489 if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13490 && TREE_CODE (prom_type) == INTEGER_TYPE
13491 && (op2 == integer_zero_node || op2 == long_zero_node ||
13492 (TREE_CODE (op2) == INTEGER_CST &&
13493 ! TREE_INT_CST_LOW (op2) && ! TREE_INT_CST_HIGH (op2))))
13495 parse_warning_context
13496 (wfl_operator,
13497 "Evaluating this expression will result in an arithmetic exception being thrown");
13498 TREE_CONSTANT (node) = 0;
13499 TREE_INVARIANT (node) = 0;
13502 /* Change the division operator if necessary */
13503 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13504 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13506 /* Before divisions as is disappear, try to simplify and bail if
13507 applicable, otherwise we won't perform even simple
13508 simplifications like (1-1)/3. We can't do that with floating
13509 point number, folds can't handle them at this stage. */
13510 if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13511 && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13513 TREE_TYPE (node) = prom_type;
13514 node = fold (node);
13515 if (TREE_CODE (node) != code)
13516 return node;
13519 if (TREE_CODE (prom_type) == INTEGER_TYPE
13520 && flag_use_divide_subroutine
13521 && ! flag_emit_class_files
13522 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13523 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13525 /* This one is more complicated. FLOATs are processed by a
13526 function call to soft_fmod. Duplicate the value of the
13527 COMPOUND_ASSIGN_P flag. */
13528 if (code == TRUNC_MOD_EXPR)
13530 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13531 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13532 return mod;
13534 break;
13536 /* 15.17 Additive Operators */
13537 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13539 /* Operation is valid if either one argument is a string
13540 constant, a String object or a StringBuffer crafted for the
13541 purpose of the a previous usage of the String concatenation
13542 operator */
13544 if (TREE_CODE (op1) == STRING_CST
13545 || TREE_CODE (op2) == STRING_CST
13546 || JSTRING_TYPE_P (op1_type)
13547 || JSTRING_TYPE_P (op2_type)
13548 || IS_CRAFTED_STRING_BUFFER_P (op1)
13549 || IS_CRAFTED_STRING_BUFFER_P (op2))
13550 return build_string_concatenation (op1, op2);
13552 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13553 Numeric Types */
13554 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13556 if (!JNUMERIC_TYPE_P (op1_type))
13557 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13558 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13559 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13560 TREE_TYPE (node) = error_mark_node;
13561 error_found = 1;
13562 break;
13564 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13565 break;
13567 /* 15.18 Shift Operators */
13568 case LSHIFT_EXPR:
13569 case RSHIFT_EXPR:
13570 case URSHIFT_EXPR:
13571 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13573 if (!JINTEGRAL_TYPE_P (op1_type))
13574 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13575 else
13577 if (JNUMERIC_TYPE_P (op2_type))
13578 parse_error_context (wfl_operator,
13579 "Incompatible type for %qs. Explicit cast needed to convert shift distance from %qs to integral",
13580 operator_string (node),
13581 lang_printable_name (op2_type, 0));
13582 else
13583 parse_error_context (wfl_operator,
13584 "Incompatible type for %qs. Can't convert shift distance from %qs to integral",
13585 operator_string (node),
13586 lang_printable_name (op2_type, 0));
13588 TREE_TYPE (node) = error_mark_node;
13589 error_found = 1;
13590 break;
13593 /* Unary numeric promotion (5.6.1) is performed on each operand
13594 separately */
13595 op1 = do_unary_numeric_promotion (op1);
13596 op2 = do_unary_numeric_promotion (op2);
13598 /* If the right hand side is of type `long', first cast it to
13599 `int'. */
13600 if (TREE_TYPE (op2) == long_type_node)
13601 op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13603 /* The type of the shift expression is the type of the promoted
13604 type of the left-hand operand */
13605 prom_type = TREE_TYPE (op1);
13607 /* Shift int only up to 0x1f and long up to 0x3f */
13608 if (prom_type == int_type_node)
13609 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13610 build_int_cst (NULL_TREE, 0x1f)));
13611 else
13612 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13613 build_int_cst (NULL_TREE, 0x3f)));
13615 /* The >>> operator is a >> operating on unsigned quantities */
13616 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13618 tree to_return;
13619 tree utype = java_unsigned_type (prom_type);
13620 op1 = convert (utype, op1);
13621 TREE_SET_CODE (node, RSHIFT_EXPR);
13622 TREE_OPERAND (node, 0) = op1;
13623 TREE_OPERAND (node, 1) = op2;
13624 TREE_TYPE (node) = utype;
13625 to_return = convert (prom_type, node);
13626 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13627 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13628 TREE_SIDE_EFFECTS (to_return)
13629 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13630 return to_return;
13632 break;
13634 /* 15.19.1 Type Comparison Operator instanceof */
13635 case INSTANCEOF_EXPR:
13637 TREE_TYPE (node) = boolean_type_node;
13639 /* OP1_TYPE might be NULL when OP1 is a string constant. */
13640 if ((cn = patch_string (op1)))
13642 op1 = cn;
13643 op1_type = TREE_TYPE (op1);
13645 if (op1_type == NULL_TREE)
13646 abort ();
13648 if (!(op2_type = resolve_type_during_patch (op2)))
13649 return error_mark_node;
13651 /* The first operand must be a reference type or the null type */
13652 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13653 error_found = 1; /* Error reported further below */
13655 /* The second operand must be a reference type */
13656 if (!JREFERENCE_TYPE_P (op2_type))
13658 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13659 parse_error_context
13660 (wfl_operator, "Invalid argument %qs for %<instanceof%>",
13661 lang_printable_name (op2_type, 0));
13662 error_found = 1;
13665 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13667 /* If the first operand is null, the result is always false */
13668 if (op1 == null_pointer_node)
13669 return boolean_false_node;
13670 else if (flag_emit_class_files)
13672 TREE_OPERAND (node, 1) = op2_type;
13673 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13674 return node;
13676 /* Otherwise we have to invoke instance of to figure it out */
13677 else
13678 return build_instanceof (op1, op2_type);
13680 /* There is no way the expression operand can be an instance of
13681 the type operand. This is a compile time error. */
13682 else
13684 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13685 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13686 parse_error_context
13687 (wfl_operator, "Impossible for %qs to be instance of %qs",
13688 t1, lang_printable_name (op2_type, 0));
13689 free (t1);
13690 error_found = 1;
13693 break;
13695 /* 15.21 Bitwise and Logical Operators */
13696 case BIT_AND_EXPR:
13697 case BIT_XOR_EXPR:
13698 case BIT_IOR_EXPR:
13699 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13700 /* Binary numeric promotion is performed on both operand and the
13701 expression retain that type */
13702 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13704 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13705 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13706 /* The type of the bitwise operator expression is BOOLEAN */
13707 prom_type = boolean_type_node;
13708 else
13710 if (!JINTEGRAL_TYPE_P (op1_type))
13711 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13712 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13713 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13714 TREE_TYPE (node) = error_mark_node;
13715 error_found = 1;
13716 /* Insert a break here if adding thing before the switch's
13717 break for this case */
13719 break;
13721 /* 15.22 Conditional-And Operator */
13722 case TRUTH_ANDIF_EXPR:
13723 /* 15.23 Conditional-Or Operator */
13724 case TRUTH_ORIF_EXPR:
13725 /* Operands must be of BOOLEAN type */
13726 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13727 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13729 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13730 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13731 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13732 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13733 TREE_TYPE (node) = boolean_type_node;
13734 error_found = 1;
13735 break;
13737 else if (integer_zerop (op1))
13739 return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13741 else if (integer_onep (op1))
13743 return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13745 /* The type of the conditional operators is BOOLEAN */
13746 prom_type = boolean_type_node;
13747 break;
13749 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13750 case LT_EXPR:
13751 case GT_EXPR:
13752 case LE_EXPR:
13753 case GE_EXPR:
13754 /* The type of each of the operands must be a primitive numeric
13755 type */
13756 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13758 if (!JNUMERIC_TYPE_P (op1_type))
13759 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13760 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13761 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13762 TREE_TYPE (node) = boolean_type_node;
13763 error_found = 1;
13764 break;
13766 /* Binary numeric promotion is performed on the operands */
13767 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13768 /* The type of the relation expression is always BOOLEAN */
13769 prom_type = boolean_type_node;
13770 break;
13772 /* 15.20 Equality Operator */
13773 case EQ_EXPR:
13774 case NE_EXPR:
13775 /* It's time for us to patch the strings. */
13776 if ((cn = patch_string (op1)))
13778 op1 = cn;
13779 op1_type = TREE_TYPE (op1);
13781 if ((cn = patch_string (op2)))
13783 op2 = cn;
13784 op2_type = TREE_TYPE (op2);
13787 /* 15.20.1 Numerical Equality Operators == and != */
13788 /* Binary numeric promotion is performed on the operands */
13789 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13790 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13792 /* 15.20.2 Boolean Equality Operators == and != */
13793 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13794 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13795 ; /* Nothing to do here */
13797 /* 15.20.3 Reference Equality Operators == and != */
13798 /* Types have to be either references or the null type. If
13799 they're references, it must be possible to convert either
13800 type to the other by casting conversion. */
13801 else if ((op1 == null_pointer_node && op2 == null_pointer_node)
13802 || (op1 == null_pointer_node && JREFERENCE_TYPE_P (op2_type))
13803 || (JREFERENCE_TYPE_P (op1_type) && op2 == null_pointer_node)
13804 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13805 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13806 || valid_ref_assignconv_cast_p (op2_type,
13807 op1_type, 1))))
13808 ; /* Nothing to do here */
13810 /* Else we have an error figure what can't be converted into
13811 what and report the error */
13812 else
13814 char *t1;
13815 t1 = xstrdup (lang_printable_name (op1_type, 0));
13816 parse_error_context
13817 (wfl_operator,
13818 "Incompatible type for %qs. Can't convert %qs to %qs",
13819 operator_string (node), t1,
13820 lang_printable_name (op2_type, 0));
13821 free (t1);
13822 TREE_TYPE (node) = boolean_type_node;
13823 error_found = 1;
13824 break;
13826 prom_type = boolean_type_node;
13827 break;
13828 default:
13829 abort ();
13832 if (error_found)
13833 return error_mark_node;
13835 TREE_OPERAND (node, 0) = op1;
13836 TREE_OPERAND (node, 1) = op2;
13837 TREE_TYPE (node) = prom_type;
13838 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13840 if (flag_emit_xref)
13841 return node;
13843 /* fold does not respect side-effect order as required for Java but not C.
13844 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13845 * bytecode.
13847 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13848 : ! TREE_SIDE_EFFECTS (node))
13849 node = fold (node);
13850 return node;
13853 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13854 zero value, the value of CSTE comes after the valude of STRING */
13856 static tree
13857 do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13859 const char *old = TREE_STRING_POINTER (cste);
13860 int old_len = TREE_STRING_LENGTH (cste);
13861 int len = old_len + string_len;
13862 char *new = alloca (len+1);
13864 if (after)
13866 memcpy (new, string, string_len);
13867 memcpy (&new [string_len], old, old_len);
13869 else
13871 memcpy (new, old, old_len);
13872 memcpy (&new [old_len], string, string_len);
13874 new [len] = '\0';
13875 return build_string (len, new);
13878 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13879 new STRING_CST on success, NULL_TREE on failure. */
13881 static tree
13882 merge_string_cste (tree op1, tree op2, int after)
13884 /* Handle two string constants right away. */
13885 if (TREE_CODE (op2) == STRING_CST)
13886 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13887 TREE_STRING_LENGTH (op2), after);
13889 /* Reasonable integer constant can be treated right away. */
13890 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13892 static const char *const boolean_true = "true";
13893 static const char *const boolean_false = "false";
13894 static const char *const null_pointer = "null";
13895 char ch[4];
13896 const char *string;
13898 if (op2 == boolean_true_node)
13899 string = boolean_true;
13900 else if (op2 == boolean_false_node)
13901 string = boolean_false;
13902 else if (op2 == null_pointer_node
13903 || (integer_zerop (op2)
13904 && TREE_CODE (TREE_TYPE (op2)) == POINTER_TYPE))
13905 /* FIXME: null is not a compile-time constant, so it is only safe to
13906 merge if the overall expression is non-constant. However, this
13907 code always merges without checking the overall expression. */
13908 string = null_pointer;
13909 else if (TREE_TYPE (op2) == char_type_node)
13911 /* Convert the character into UTF-8. */
13912 unsigned int c = (unsigned int) TREE_INT_CST_LOW (op2);
13913 unsigned char *p = (unsigned char *) ch;
13914 if (0x01 <= c && c <= 0x7f)
13915 *p++ = (unsigned char) c;
13916 else if (c < 0x7ff)
13918 *p++ = (unsigned char) (c >> 6 | 0xc0);
13919 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13921 else
13923 *p++ = (unsigned char) (c >> 12 | 0xe0);
13924 *p++ = (unsigned char) (((c >> 6) & 0x3f) | 0x80);
13925 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13927 *p = '\0';
13929 string = ch;
13931 else
13932 string = string_convert_int_cst (op2);
13934 return do_merge_string_cste (op1, string, strlen (string), after);
13936 return NULL_TREE;
13939 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13940 has to be a STRING_CST and the other part must be a STRING_CST or a
13941 INTEGRAL constant. Return a new STRING_CST if the operation
13942 succeed, NULL_TREE otherwise.
13944 If the case we want to optimize for space, we might want to return
13945 NULL_TREE for each invocation of this routine. FIXME */
13947 static tree
13948 string_constant_concatenation (tree op1, tree op2)
13950 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13952 tree string, rest;
13953 int invert;
13955 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13956 rest = (string == op1 ? op2 : op1);
13957 invert = (string == op1 ? 0 : 1 );
13959 /* Walk REST, only if it looks reasonable */
13960 if (TREE_CODE (rest) != STRING_CST
13961 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13962 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13963 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13965 rest = java_complete_tree (rest);
13966 if (rest == error_mark_node)
13967 return error_mark_node;
13968 rest = fold (rest);
13970 return merge_string_cste (string, rest, invert);
13972 return NULL_TREE;
13975 /* Implement the `+' operator. Does static optimization if possible,
13976 otherwise create (if necessary) and append elements to a
13977 StringBuffer. The StringBuffer will be carried around until it is
13978 used for a function call or an assignment. Then toString() will be
13979 called on it to turn it into a String object. */
13981 static tree
13982 build_string_concatenation (tree op1, tree op2)
13984 tree result;
13985 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13987 if (flag_emit_xref)
13988 return build2 (PLUS_EXPR, string_type_node, op1, op2);
13990 /* Try to do some static optimization */
13991 if ((result = string_constant_concatenation (op1, op2)))
13992 return result;
13994 /* Discard empty strings on either side of the expression */
13995 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13997 op1 = op2;
13998 op2 = NULL_TREE;
14000 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
14001 op2 = NULL_TREE;
14003 /* If operands are string constant, turn then into object references */
14004 if (TREE_CODE (op1) == STRING_CST)
14005 op1 = patch_string_cst (op1);
14006 if (op2 && TREE_CODE (op2) == STRING_CST)
14007 op2 = patch_string_cst (op2);
14009 /* If either one of the constant is null and the other non null
14010 operand is a String constant, return it. */
14011 if ((TREE_CODE (op1) == STRING_CST) && !op2)
14012 return op1;
14014 /* If OP1 isn't already a StringBuffer, create and
14015 initialize a new one */
14016 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
14018 /* Two solutions here:
14019 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
14020 2) OP1 is something else, we call new StringBuffer().append(OP1). */
14021 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
14022 op1 = BUILD_STRING_BUFFER (op1);
14023 else
14025 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
14026 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
14030 if (op2)
14032 /* OP1 is no longer the last node holding a crafted StringBuffer */
14033 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
14034 /* Create a node for `{new...,xxx}.append (op2)' */
14035 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
14038 /* Mark the last node holding a crafted StringBuffer */
14039 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
14041 TREE_SIDE_EFFECTS (op1) = side_effects;
14042 return op1;
14045 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
14046 StringBuffer. If no string were found to be patched, return
14047 NULL. */
14049 static tree
14050 patch_string (tree node)
14052 if (node == error_mark_node)
14053 return error_mark_node;
14054 if (TREE_CODE (node) == STRING_CST)
14055 return patch_string_cst (node);
14056 else if (IS_CRAFTED_STRING_BUFFER_P (node))
14058 int saved = ctxp->explicit_constructor_p;
14059 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
14060 tree ret;
14061 /* Temporary disable forbid the use of `this'. */
14062 ctxp->explicit_constructor_p = 0;
14063 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
14064 /* String concatenation arguments must be evaluated in order too. */
14065 ret = force_evaluation_order (ret);
14066 /* Restore it at its previous value */
14067 ctxp->explicit_constructor_p = saved;
14068 return ret;
14070 return NULL_TREE;
14073 /* Build the internal representation of a string constant. */
14075 static tree
14076 patch_string_cst (tree node)
14078 int location;
14079 if (! flag_emit_class_files)
14081 node = get_identifier (TREE_STRING_POINTER (node));
14082 location = alloc_name_constant (CONSTANT_String, node);
14083 node = build_ref_from_constant_pool (location);
14085 TREE_CONSTANT (node) = 1;
14086 TREE_INVARIANT (node) = 1;
14088 /* ??? Guessing that the class file code can't handle casts. */
14089 if (! flag_emit_class_files)
14090 node = convert (string_ptr_type_node, node);
14091 else
14092 TREE_TYPE (node) = string_ptr_type_node;
14094 return node;
14097 /* Build an incomplete unary operator expression. */
14099 static tree
14100 build_unaryop (int op_token, int op_location, tree op1)
14102 enum tree_code op;
14103 tree unaryop;
14104 switch (op_token)
14106 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14107 case MINUS_TK: op = NEGATE_EXPR; break;
14108 case NEG_TK: op = TRUTH_NOT_EXPR; break;
14109 case NOT_TK: op = BIT_NOT_EXPR; break;
14110 default: abort ();
14113 unaryop = build1 (op, NULL_TREE, op1);
14114 TREE_SIDE_EFFECTS (unaryop) = 1;
14115 /* Store the location of the operator, for better error report. The
14116 string of the operator will be rebuild based on the OP value. */
14117 EXPR_WFL_LINECOL (unaryop) = op_location;
14118 return unaryop;
14121 /* Special case for the ++/-- operators, since they require an extra
14122 argument to build, which is set to NULL and patched
14123 later. IS_POST_P is 1 if the operator, 0 otherwise. */
14125 static tree
14126 build_incdec (int op_token, int op_location, tree op1, int is_post_p)
14128 static const enum tree_code lookup [2][2] =
14130 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14131 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14133 tree node = build2 (lookup [is_post_p][(op_token - DECR_TK)],
14134 NULL_TREE, op1, NULL_TREE);
14135 TREE_SIDE_EFFECTS (node) = 1;
14136 /* Store the location of the operator, for better error report. The
14137 string of the operator will be rebuild based on the OP value. */
14138 EXPR_WFL_LINECOL (node) = op_location;
14139 return node;
14142 /* Build an incomplete cast operator, based on the use of the
14143 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14144 set. java_complete_tree is trained to walk a CONVERT_EXPR even
14145 though its type is already set. */
14147 static tree
14148 build_cast (int location, tree type, tree exp)
14150 tree node = build1 (CONVERT_EXPR, type, exp);
14151 EXPR_WFL_LINECOL (node) = location;
14152 return node;
14155 /* Build an incomplete class reference operator. */
14156 static tree
14157 build_incomplete_class_ref (int location, tree class_name)
14159 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14160 tree class_decl = GET_CPC ();
14161 tree this_class = TREE_TYPE (class_decl);
14163 /* Generate the synthetic static method `class$'. (Previously we
14164 deferred this, causing different method tables to be emitted
14165 for native code and bytecode.) */
14166 if (!TYPE_DOT_CLASS (this_class)
14167 && !JPRIMITIVE_TYPE_P (class_name)
14168 && !(TREE_CODE (class_name) == VOID_TYPE))
14170 tree cpc_list = GET_CPC_LIST();
14171 tree cpc = cpc_list;
14172 tree target_class;
14174 /* For inner classes, add a 'class$' method to their outermost
14175 context, creating it if necessary. */
14177 while (GET_NEXT_ENCLOSING_CPC(cpc))
14178 cpc = GET_NEXT_ENCLOSING_CPC(cpc);
14179 class_decl = TREE_VALUE (cpc);
14181 target_class = TREE_TYPE (class_decl);
14183 if (CLASS_INTERFACE (TYPE_NAME (target_class)))
14185 /* For interfaces, adding a static 'class$' method directly
14186 is illegal. So create an inner class to contain the new
14187 method. Empirically this matches the behavior of javac. */
14188 tree t, inner;
14189 /* We want the generated inner class inside the outermost class. */
14190 GET_CPC_LIST() = cpc;
14191 t = build_wfl_node (DECL_NAME (TYPE_NAME (object_type_node)));
14192 inner = create_anonymous_class (t);
14193 target_class = TREE_TYPE (inner);
14194 end_class_declaration (1);
14195 GET_CPC_LIST() = cpc_list;
14198 if (TYPE_DOT_CLASS (target_class) == NULL_TREE)
14199 build_dot_class_method (target_class);
14201 if (this_class != target_class)
14202 TYPE_DOT_CLASS (this_class) = TYPE_DOT_CLASS (target_class);
14205 EXPR_WFL_LINECOL (node) = location;
14206 return node;
14209 /* Complete an incomplete class reference operator. */
14210 static tree
14211 patch_incomplete_class_ref (tree node)
14213 tree type = TREE_OPERAND (node, 0);
14214 tree ref_type;
14216 if (!(ref_type = resolve_type_during_patch (type)))
14217 return error_mark_node;
14219 /* If we're not emitting class files and we know ref_type is a
14220 compiled class, build a direct reference. */
14221 if ((! flag_emit_class_files && is_compiled_class (ref_type))
14222 || JPRIMITIVE_TYPE_P (ref_type)
14223 || TREE_CODE (ref_type) == VOID_TYPE)
14225 tree dot = build_class_ref (ref_type);
14226 /* A class referenced by `foo.class' is initialized. */
14227 if (!flag_emit_class_files)
14228 dot = build_class_init (ref_type, dot);
14229 return java_complete_tree (dot);
14232 /* If we're emitting class files and we have to deal with non
14233 primitive types, we invoke the synthetic static method `class$'. */
14234 ref_type = build_dot_class_method_invocation (current_class, ref_type);
14235 return java_complete_tree (ref_type);
14238 /* 15.14 Unary operators. We return error_mark_node in case of error,
14239 but preserve the type of NODE if the type is fixed. */
14241 static tree
14242 patch_unaryop (tree node, tree wfl_op)
14244 tree op = TREE_OPERAND (node, 0);
14245 tree op_type = TREE_TYPE (op);
14246 tree prom_type = NULL_TREE, value, decl;
14247 int outer_field_flag = 0;
14248 int code = TREE_CODE (node);
14249 int error_found = 0;
14251 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14253 switch (code)
14255 /* 15.13.2 Postfix Increment Operator ++ */
14256 case POSTINCREMENT_EXPR:
14257 /* 15.13.3 Postfix Increment Operator -- */
14258 case POSTDECREMENT_EXPR:
14259 /* 15.14.1 Prefix Increment Operator ++ */
14260 case PREINCREMENT_EXPR:
14261 /* 15.14.2 Prefix Decrement Operator -- */
14262 case PREDECREMENT_EXPR:
14263 op = decl = extract_field_decl (op);
14264 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
14265 /* We might be trying to change an outer field accessed using
14266 access method. */
14267 if (outer_field_flag)
14269 /* Retrieve the decl of the field we're trying to access. We
14270 do that by first retrieving the function we would call to
14271 access the field. It has been already verified that this
14272 field isn't final */
14273 if (flag_emit_class_files)
14274 decl = TREE_OPERAND (op, 0);
14275 else
14276 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14277 decl = DECL_FUNCTION_ACCESS_DECL (decl);
14279 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14280 else if (!JDECL_P (decl)
14281 && TREE_CODE (decl) != COMPONENT_REF
14282 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14283 && TREE_CODE (decl) != INDIRECT_REF
14284 && !(TREE_CODE (decl) == COMPOUND_EXPR
14285 && TREE_OPERAND (decl, 1)
14286 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14288 TREE_TYPE (node) = error_mark_node;
14289 error_found = 1;
14292 /* From now on, we know that op if a variable and that it has a
14293 valid wfl. We use wfl_op to locate errors related to the
14294 ++/-- operand. */
14295 if (!JNUMERIC_TYPE_P (op_type))
14297 parse_error_context
14298 (wfl_op, "Invalid argument type %qs to %qs",
14299 lang_printable_name (op_type, 0), operator_string (node));
14300 TREE_TYPE (node) = error_mark_node;
14301 error_found = 1;
14303 else
14305 /* Before the addition, binary numeric promotion is performed on
14306 both operands, if really necessary */
14307 if (JINTEGRAL_TYPE_P (op_type))
14309 value = build_int_cst (op_type, 1);
14310 TREE_TYPE (node) = op_type;
14312 else
14314 value = build_int_cst (NULL_TREE, 1);
14315 TREE_TYPE (node) =
14316 binary_numeric_promotion (op_type,
14317 TREE_TYPE (value), &op, &value);
14320 /* We remember we might be accessing an outer field */
14321 if (outer_field_flag)
14323 /* We re-generate an access to the field */
14324 value = build2 (PLUS_EXPR, TREE_TYPE (op),
14325 build_outer_field_access (wfl_op, decl), value);
14327 /* And we patch the original access$() into a write
14328 with plus_op as a rhs */
14329 return outer_field_access_fix (node, op, value);
14332 /* And write back into the node. */
14333 TREE_OPERAND (node, 0) = op;
14334 TREE_OPERAND (node, 1) = value;
14335 /* Convert the overall back into its original type, if
14336 necessary, and return */
14337 if (JINTEGRAL_TYPE_P (op_type))
14338 return fold (node);
14339 else
14340 return fold (convert (op_type, node));
14342 break;
14344 /* 15.14.3 Unary Plus Operator + */
14345 case UNARY_PLUS_EXPR:
14346 /* 15.14.4 Unary Minus Operator - */
14347 case NEGATE_EXPR:
14348 if (!JNUMERIC_TYPE_P (op_type))
14350 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14351 TREE_TYPE (node) = error_mark_node;
14352 error_found = 1;
14354 /* Unary numeric promotion is performed on operand */
14355 else
14357 op = do_unary_numeric_promotion (op);
14358 prom_type = TREE_TYPE (op);
14359 if (code == UNARY_PLUS_EXPR)
14360 return fold (op);
14362 break;
14364 /* 15.14.5 Bitwise Complement Operator ~ */
14365 case BIT_NOT_EXPR:
14366 if (!JINTEGRAL_TYPE_P (op_type))
14368 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14369 TREE_TYPE (node) = error_mark_node;
14370 error_found = 1;
14372 else
14374 op = do_unary_numeric_promotion (op);
14375 prom_type = TREE_TYPE (op);
14377 break;
14379 /* 15.14.6 Logical Complement Operator ! */
14380 case TRUTH_NOT_EXPR:
14381 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14383 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14384 /* But the type is known. We will report an error if further
14385 attempt of a assignment is made with this rhs */
14386 TREE_TYPE (node) = boolean_type_node;
14387 error_found = 1;
14389 else
14390 prom_type = boolean_type_node;
14391 break;
14393 /* 15.15 Cast Expression */
14394 case CONVERT_EXPR:
14395 value = patch_cast (node, wfl_operator);
14396 if (value == error_mark_node)
14398 /* If this cast is part of an assignment, we tell the code
14399 that deals with it not to complain about a mismatch,
14400 because things have been cast, anyways */
14401 TREE_TYPE (node) = error_mark_node;
14402 error_found = 1;
14404 else
14406 value = fold (value);
14407 return value;
14409 break;
14412 if (error_found)
14413 return error_mark_node;
14415 /* There are cases where node has been replaced by something else
14416 and we don't end up returning here: UNARY_PLUS_EXPR,
14417 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14418 TREE_OPERAND (node, 0) = fold (op);
14419 TREE_TYPE (node) = prom_type;
14420 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14421 return fold (node);
14424 /* Generic type resolution that sometimes takes place during node
14425 patching. Returned the resolved type or generate an error
14426 message. Return the resolved type or NULL_TREE. */
14428 static tree
14429 resolve_type_during_patch (tree type)
14431 if (unresolved_type_p (type, NULL))
14433 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14434 if (!type_decl)
14436 parse_error_context (type,
14437 "Class %qs not found in type declaration",
14438 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14439 return NULL_TREE;
14442 check_deprecation (type, type_decl);
14444 return TREE_TYPE (type_decl);
14446 return type;
14449 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14450 found. Otherwise NODE or something meant to replace it is returned. */
14452 static tree
14453 patch_cast (tree node, tree wfl_op)
14455 tree op = TREE_OPERAND (node, 0);
14456 tree cast_type = TREE_TYPE (node);
14457 tree patched, op_type;
14458 char *t1;
14460 /* Some string patching might be necessary at this stage */
14461 if ((patched = patch_string (op)))
14462 TREE_OPERAND (node, 0) = op = patched;
14463 op_type = TREE_TYPE (op);
14465 /* First resolve OP_TYPE if unresolved */
14466 if (!(cast_type = resolve_type_during_patch (cast_type)))
14467 return error_mark_node;
14469 /* Check on cast that are proven correct at compile time */
14470 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14472 /* Same type */
14473 if (cast_type == op_type)
14474 return node;
14476 /* A narrowing conversion from a floating-point number to an
14477 integral type requires special handling (5.1.3). */
14478 if (JFLOAT_TYPE_P (op_type) && JINTEGRAL_TYPE_P (cast_type))
14479 if (cast_type != long_type_node)
14480 op = convert (integer_type_node, op);
14482 /* Try widening/narrowing conversion. Potentially, things need
14483 to be worked out in gcc so we implement the extreme cases
14484 correctly. fold_convert() needs to be fixed. */
14485 return convert (cast_type, op);
14488 /* It's also valid to cast a boolean into a boolean */
14489 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14490 return node;
14492 /* null can be casted to references */
14493 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14494 return build_null_of_type (cast_type);
14496 /* The remaining legal casts involve conversion between reference
14497 types. Check for their compile time correctness. */
14498 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14499 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14501 TREE_TYPE (node) = promote_type (cast_type);
14502 /* Now, the case can be determined correct at compile time if
14503 OP_TYPE can be converted into CAST_TYPE by assignment
14504 conversion (5.2) */
14506 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14508 TREE_SET_CODE (node, NOP_EXPR);
14509 return node;
14512 if (flag_emit_class_files)
14514 TREE_SET_CODE (node, CONVERT_EXPR);
14515 return node;
14518 /* The cast requires a run-time check */
14519 return build3 (CALL_EXPR, promote_type (cast_type),
14520 build_address_of (soft_checkcast_node),
14521 tree_cons (NULL_TREE, build_class_ref (cast_type),
14522 build_tree_list (NULL_TREE, op)),
14523 NULL_TREE);
14526 /* Any other casts are proven incorrect at compile time */
14527 t1 = xstrdup (lang_printable_name (op_type, 0));
14528 parse_error_context (wfl_op, "Invalid cast from %qs to %qs",
14529 t1, lang_printable_name (cast_type, 0));
14530 free (t1);
14531 return error_mark_node;
14534 /* Build a null constant and give it the type TYPE. */
14536 static tree
14537 build_null_of_type (tree type)
14539 tree node = build_int_cst (promote_type (type), 0);
14540 return node;
14543 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14544 a list of indices. */
14545 static tree
14546 build_array_ref (int location, tree array, tree index)
14548 tree node = build4 (ARRAY_REF, NULL_TREE, array, index,
14549 NULL_TREE, NULL_TREE);
14550 EXPR_WFL_LINECOL (node) = location;
14551 return node;
14554 /* 15.12 Array Access Expression */
14556 static tree
14557 patch_array_ref (tree node)
14559 tree array = TREE_OPERAND (node, 0);
14560 tree array_type = TREE_TYPE (array);
14561 tree index = TREE_OPERAND (node, 1);
14562 tree index_type = TREE_TYPE (index);
14563 int error_found = 0;
14565 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14567 if (TREE_CODE (array_type) == POINTER_TYPE)
14568 array_type = TREE_TYPE (array_type);
14570 /* The array reference must be an array */
14571 if (!TYPE_ARRAY_P (array_type))
14573 parse_error_context
14574 (wfl_operator,
14575 "%<[]%> can only be applied to arrays. It can't be applied to %qs",
14576 lang_printable_name (array_type, 0));
14577 TREE_TYPE (node) = error_mark_node;
14578 error_found = 1;
14581 /* The array index undergoes unary numeric promotion. The promoted
14582 type must be int */
14583 index = do_unary_numeric_promotion (index);
14584 if (TREE_TYPE (index) != int_type_node)
14586 if (valid_cast_to_p (index_type, int_type_node))
14587 parse_error_context (wfl_operator,
14588 "Incompatible type for %<[]%>. Explicit cast needed to convert %qs to %<int%>",
14589 lang_printable_name (index_type, 0));
14590 else
14591 parse_error_context (wfl_operator,
14592 "Incompatible type for %<[]%>. Can't convert %qs to %<int%>",
14593 lang_printable_name (index_type, 0));
14594 TREE_TYPE (node) = error_mark_node;
14595 error_found = 1;
14598 if (error_found)
14599 return error_mark_node;
14601 array_type = TYPE_ARRAY_ELEMENT (array_type);
14603 if (flag_emit_class_files || flag_emit_xref)
14605 TREE_OPERAND (node, 0) = array;
14606 TREE_OPERAND (node, 1) = index;
14608 else
14609 node = build_java_arrayaccess (array, array_type, index);
14610 TREE_TYPE (node) = array_type;
14611 return node;
14614 /* 15.9 Array Creation Expressions */
14616 static tree
14617 build_newarray_node (tree type, tree dims, int extra_dims)
14619 tree node = build3 (NEW_ARRAY_EXPR, NULL_TREE, type,
14620 nreverse (dims),
14621 build_int_cst (NULL_TREE, extra_dims));
14622 return node;
14625 static tree
14626 patch_newarray (tree node)
14628 tree type = TREE_OPERAND (node, 0);
14629 tree dims = TREE_OPERAND (node, 1);
14630 tree cdim, array_type;
14631 int error_found = 0;
14632 int ndims = 0;
14633 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14635 /* Dimension types are verified. It's better for the types to be
14636 verified in order. */
14637 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14639 int dim_error = 0;
14640 tree dim = TREE_VALUE (cdim);
14642 /* Dim might have been saved during its evaluation */
14643 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14645 /* The type of each specified dimension must be an integral type. */
14646 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14647 dim_error = 1;
14649 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14650 promoted type must be int. */
14651 else
14653 dim = do_unary_numeric_promotion (dim);
14654 if (TREE_TYPE (dim) != int_type_node)
14655 dim_error = 1;
14658 /* Report errors on types here */
14659 if (dim_error)
14661 parse_error_context
14662 (TREE_PURPOSE (cdim),
14663 "Incompatible type for dimension in array creation expression. %s convert %qs to %<int%>",
14664 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14665 "Explicit cast needed to" : "Can't"),
14666 lang_printable_name (TREE_TYPE (dim), 0));
14667 error_found = 1;
14670 TREE_PURPOSE (cdim) = NULL_TREE;
14673 /* Resolve array base type if unresolved */
14674 if (!(type = resolve_type_during_patch (type)))
14675 error_found = 1;
14677 if (error_found)
14679 /* We don't want further evaluation of this bogus array creation
14680 operation */
14681 TREE_TYPE (node) = error_mark_node;
14682 return error_mark_node;
14685 /* Set array_type to the actual (promoted) array type of the result. */
14686 if (TREE_CODE (type) == RECORD_TYPE)
14687 type = build_pointer_type (type);
14688 while (--xdims >= 0)
14690 type = promote_type (build_java_array_type (type, -1));
14692 dims = nreverse (dims);
14693 array_type = type;
14694 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14696 type = array_type;
14697 array_type
14698 = build_java_array_type (type,
14699 TREE_CODE (cdim) == INTEGER_CST
14700 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14701 : -1);
14702 array_type = promote_type (array_type);
14704 dims = nreverse (dims);
14706 /* The node is transformed into a function call. Things are done
14707 differently according to the number of dimensions. If the number
14708 of dimension is equal to 1, then the nature of the base type
14709 (primitive or not) matters. */
14710 if (ndims == 1)
14711 return build_new_array (type, TREE_VALUE (dims));
14713 /* Can't reuse what's already written in expr.c because it uses the
14714 JVM stack representation. Provide a build_multianewarray. FIXME */
14715 return build3 (CALL_EXPR, array_type,
14716 build_address_of (soft_multianewarray_node),
14717 tree_cons (NULL_TREE,
14718 build_class_ref (TREE_TYPE (array_type)),
14719 tree_cons (NULL_TREE,
14720 build_int_cst (NULL_TREE, ndims),
14721 dims)),
14722 NULL_TREE);
14725 /* 10.6 Array initializer. */
14727 /* Build a wfl for array element that don't have one, so we can
14728 pin-point errors. */
14730 static tree
14731 maybe_build_array_element_wfl (tree node)
14733 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14735 /* FIXME - old code used "prev_lc.line" and "elc.prev_col */
14736 return build_expr_wfl (NULL_TREE,
14737 #ifdef USE_MAPPED_LOCATION
14738 input_location
14739 #else
14740 ctxp->filename,
14741 ctxp->lexer->token_start.line,
14742 ctxp->lexer->token_start.col
14743 #endif
14746 else
14747 return NULL_TREE;
14750 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14751 identification of initialized arrays easier to detect during walk
14752 and expansion. */
14754 static tree
14755 build_new_array_init (int location, tree values)
14757 tree constructor = build_constructor (NULL_TREE, values);
14758 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14759 EXPR_WFL_LINECOL (to_return) = location;
14760 return to_return;
14763 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14764 occurred. Otherwise return NODE after having set its type
14765 appropriately. */
14767 static tree
14768 patch_new_array_init (tree type, tree node)
14770 int error_seen = 0;
14771 tree current, element_type;
14772 HOST_WIDE_INT length;
14773 int all_constant = 1;
14774 tree init = TREE_OPERAND (node, 0);
14776 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14778 parse_error_context (node,
14779 "Invalid array initializer for non-array type %qs",
14780 lang_printable_name (type, 1));
14781 return error_mark_node;
14783 type = TREE_TYPE (type);
14784 element_type = TYPE_ARRAY_ELEMENT (type);
14786 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14788 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14789 current; length++, current = TREE_CHAIN (current))
14791 tree elt = TREE_VALUE (current);
14792 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14794 error_seen |= array_constructor_check_entry (element_type, current);
14795 elt = TREE_VALUE (current);
14796 /* When compiling to native code, STRING_CST is converted to
14797 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14798 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14799 all_constant = 0;
14801 else
14803 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14804 TREE_PURPOSE (current) = NULL_TREE;
14805 all_constant = 0;
14807 if (elt && TREE_CODE (elt) == TREE_LIST
14808 && TREE_VALUE (elt) == error_mark_node)
14809 error_seen = 1;
14812 if (error_seen)
14813 return error_mark_node;
14815 /* Create a new type. We can't reuse the one we have here by
14816 patching its dimension because it originally is of dimension -1
14817 hence reused by gcc. This would prevent triangular arrays. */
14818 type = build_java_array_type (element_type, length);
14819 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14820 TREE_TYPE (node) = promote_type (type);
14821 TREE_CONSTANT (init) = all_constant;
14822 TREE_INVARIANT (init) = all_constant;
14823 TREE_CONSTANT (node) = all_constant;
14824 TREE_INVARIANT (node) = all_constant;
14825 return node;
14828 /* Verify that one entry of the initializer element list can be
14829 assigned to the array base type. Report 1 if an error occurred, 0
14830 otherwise. */
14832 static int
14833 array_constructor_check_entry (tree type, tree entry)
14835 char *array_type_string = NULL; /* For error reports */
14836 tree value, type_value, new_value, wfl_value, patched;
14837 int error_seen = 0;
14839 new_value = NULL_TREE;
14840 wfl_value = TREE_VALUE (entry);
14842 value = java_complete_tree (TREE_VALUE (entry));
14843 /* patch_string return error_mark_node if arg is error_mark_node */
14844 if ((patched = patch_string (value)))
14845 value = patched;
14846 if (value == error_mark_node)
14847 return 1;
14849 type_value = TREE_TYPE (value);
14851 /* At anytime, try_builtin_assignconv can report a warning on
14852 constant overflow during narrowing. */
14853 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14854 new_value = try_builtin_assignconv (wfl_operator, type, value);
14855 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14856 type_value = promote_type (type);
14858 /* Check and report errors */
14859 if (!new_value)
14861 const char *const msg = (!valid_cast_to_p (type_value, type) ?
14862 "Can't" : "Explicit cast needed to");
14863 if (!array_type_string)
14864 array_type_string = xstrdup (lang_printable_name (type, 1));
14865 parse_error_context
14866 (wfl_operator, "Incompatible type for array. %s convert %qs to %qs",
14867 msg, lang_printable_name (type_value, 1), array_type_string);
14868 error_seen = 1;
14871 if (new_value)
14872 TREE_VALUE (entry) = new_value;
14874 if (array_type_string)
14875 free (array_type_string);
14877 TREE_PURPOSE (entry) = NULL_TREE;
14878 return error_seen;
14881 static tree
14882 build_this (int location)
14884 tree node = build_wfl_node (this_identifier_node);
14885 TREE_SET_CODE (node, THIS_EXPR);
14886 EXPR_WFL_LINECOL (node) = location;
14887 return node;
14890 /* 14.15 The return statement. It builds a modify expression that
14891 assigns the returned value to the RESULT_DECL that hold the value
14892 to be returned. */
14894 static tree
14895 build_return (int location, tree op)
14897 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14898 EXPR_WFL_LINECOL (node) = location;
14899 node = build_debugable_stmt (location, node);
14900 return node;
14903 static tree
14904 patch_return (tree node)
14906 tree return_exp = TREE_OPERAND (node, 0);
14907 tree meth = current_function_decl;
14908 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14909 int error_found = 0;
14911 TREE_TYPE (node) = error_mark_node;
14912 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14914 /* It's invalid to have a return value within a function that is
14915 declared with the keyword void or that is a constructor */
14916 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14917 error_found = 1;
14919 /* It's invalid to use a return statement in a static block */
14920 if (DECL_CLINIT_P (current_function_decl))
14921 error_found = 1;
14923 /* It's invalid to have a no return value within a function that
14924 isn't declared with the keyword `void' */
14925 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14926 error_found = 2;
14928 if (DECL_INSTINIT_P (current_function_decl))
14929 error_found = 1;
14931 if (error_found)
14933 if (DECL_INSTINIT_P (current_function_decl))
14934 parse_error_context (wfl_operator,
14935 "%<return%> inside instance initializer");
14937 else if (DECL_CLINIT_P (current_function_decl))
14938 parse_error_context (wfl_operator,
14939 "%<return%> inside static initializer");
14941 else if (!DECL_CONSTRUCTOR_P (meth))
14943 char *t = xstrdup (lang_printable_name (mtype, 0));
14944 parse_error_context (wfl_operator,
14945 "%<return%> with%s value from %<%s %s%>",
14946 (error_found == 1 ? "" : "out"),
14947 t, lang_printable_name (meth, 2));
14948 free (t);
14950 else
14951 parse_error_context (wfl_operator,
14952 "%<return%> with value from constructor %qs",
14953 lang_printable_name (meth, 2));
14954 return error_mark_node;
14957 /* If we have a return_exp, build a modify expression and expand
14958 it. Note: at that point, the assignment is declared valid, but we
14959 may want to carry some more hacks */
14960 if (return_exp)
14962 tree exp = java_complete_tree (return_exp);
14963 tree modify, patched;
14965 if ((patched = patch_string (exp)))
14966 exp = patched;
14968 modify = build2 (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14969 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14970 modify = java_complete_tree (modify);
14972 if (modify != error_mark_node)
14974 TREE_SIDE_EFFECTS (modify) = 1;
14975 TREE_OPERAND (node, 0) = modify;
14977 else
14978 return error_mark_node;
14980 TREE_TYPE (node) = void_type_node;
14981 TREE_SIDE_EFFECTS (node) = 1;
14982 return node;
14985 /* 14.8 The if Statement */
14987 static tree
14988 build_if_else_statement (int location, tree expression, tree if_body,
14989 tree else_body)
14991 tree node;
14992 if (!else_body)
14993 else_body = build_java_empty_stmt ();
14994 node = build3 (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14995 EXPR_WFL_LINECOL (node) = location;
14996 node = build_debugable_stmt (location, node);
14997 return node;
15000 static tree
15001 patch_if_else_statement (tree node)
15003 tree expression = TREE_OPERAND (node, 0);
15004 int can_complete_normally
15005 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15006 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
15008 TREE_TYPE (node) = error_mark_node;
15009 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15011 /* The type of expression must be boolean */
15012 if (TREE_TYPE (expression) != boolean_type_node
15013 && TREE_TYPE (expression) != promoted_boolean_type_node)
15015 parse_error_context
15016 (wfl_operator,
15017 "Incompatible type for %<if%>. Can't convert %qs to %<boolean%>",
15018 lang_printable_name (TREE_TYPE (expression), 0));
15019 return error_mark_node;
15022 TREE_TYPE (node) = void_type_node;
15023 TREE_SIDE_EFFECTS (node) = 1;
15024 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
15025 return node;
15028 /* 14.6 Labeled Statements */
15030 /* Action taken when a labeled statement is parsed. a new
15031 LABELED_BLOCK_EXPR is created. No statement is attached to the
15032 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
15034 static tree
15035 build_labeled_block (int location, tree label)
15037 tree label_name ;
15038 tree label_decl, node;
15039 if (label == NULL_TREE || label == continue_identifier_node)
15040 label_name = label;
15041 else
15043 label_name = merge_qualified_name (label_id, label);
15044 /* Issue an error if we try to reuse a label that was previously
15045 declared */
15046 if (IDENTIFIER_LOCAL_VALUE (label_name))
15048 EXPR_WFL_LINECOL (wfl_operator) = location;
15049 parse_error_context (wfl_operator,
15050 "Declaration of %qs shadows a previous label declaration",
15051 IDENTIFIER_POINTER (label));
15052 EXPR_WFL_LINECOL (wfl_operator) =
15053 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
15054 parse_error_context (wfl_operator,
15055 "This is the location of the previous declaration of label %qs",
15056 IDENTIFIER_POINTER (label));
15057 java_error_count--;
15061 label_decl = create_label_decl (label_name);
15062 node = build2 (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
15063 EXPR_WFL_LINECOL (node) = location;
15064 TREE_SIDE_EFFECTS (node) = 1;
15065 return node;
15068 /* A labeled statement LBE is attached a statement. */
15070 static tree
15071 finish_labeled_statement (tree lbe, /* Labeled block expr */
15072 tree statement)
15074 /* In anyways, tie the loop to its statement */
15075 LABELED_BLOCK_BODY (lbe) = statement;
15076 pop_labeled_block ();
15077 POP_LABELED_BLOCK ();
15078 return lbe;
15081 /* 14.10, 14.11, 14.12 Loop Statements */
15083 /* Create an empty LOOP_EXPR and make it the last in the nested loop
15084 list. */
15086 static tree
15087 build_new_loop (tree loop_body)
15089 tree loop = build1 (LOOP_EXPR, NULL_TREE, loop_body);
15090 TREE_SIDE_EFFECTS (loop) = 1;
15091 PUSH_LOOP (loop);
15092 return loop;
15095 /* Create a loop body according to the following structure:
15096 COMPOUND_EXPR
15097 COMPOUND_EXPR (loop main body)
15098 EXIT_EXPR (this order is for while/for loops.
15099 LABELED_BLOCK_EXPR the order is reversed for do loops)
15100 LABEL_DECL (a continue occurring here branches at the
15101 BODY end of this labeled block)
15102 INCREMENT (if any)
15104 REVERSED, if nonzero, tells that the loop condition expr comes
15105 after the body, like in the do-while loop.
15107 To obtain a loop, the loop body structure described above is
15108 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15110 LABELED_BLOCK_EXPR
15111 LABEL_DECL (use this label to exit the loop)
15112 LOOP_EXPR
15113 <structure described above> */
15115 static tree
15116 build_loop_body (int location, tree condition, int reversed)
15118 tree first, second, body;
15120 condition = build1 (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15121 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15122 condition = build_debugable_stmt (location, condition);
15123 TREE_SIDE_EFFECTS (condition) = 1;
15125 body = build_labeled_block (0, continue_identifier_node);
15126 first = (reversed ? body : condition);
15127 second = (reversed ? condition : body);
15128 return build2 (COMPOUND_EXPR, NULL_TREE,
15129 build2 (COMPOUND_EXPR, NULL_TREE, first, second),
15130 build_java_empty_stmt ());
15133 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15134 their order) on the current loop. Unlink the current loop from the
15135 loop list. */
15137 static tree
15138 finish_loop_body (int location, tree condition, tree body, int reversed)
15140 tree to_return = ctxp->current_loop;
15141 tree loop_body = LOOP_EXPR_BODY (to_return);
15142 if (condition)
15144 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15145 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15146 The real EXIT_EXPR is one operand further. */
15147 EXPR_WFL_LINECOL (cnode) = location;
15148 if (TREE_CODE (cnode) == EXPR_WITH_FILE_LOCATION)
15150 cnode = EXPR_WFL_NODE (cnode);
15151 /* This one is for accurate error reports */
15152 EXPR_WFL_LINECOL (cnode) = location;
15154 TREE_OPERAND (cnode, 0) = condition;
15156 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15157 POP_LOOP ();
15158 return to_return;
15161 /* Tailored version of finish_loop_body for FOR loops, when FOR
15162 loops feature the condition part */
15164 static tree
15165 finish_for_loop (int location, tree condition, tree update, tree body)
15167 /* Put the condition and the loop body in place */
15168 tree loop = finish_loop_body (location, condition, body, 0);
15169 /* LOOP is the current loop which has been now popped of the loop
15170 stack. Mark the update block as reachable and install it. We do
15171 this because the (current interpretation of the) JLS requires
15172 that the update expression be considered reachable even if the
15173 for loop's body doesn't complete normally. */
15174 if (update != NULL_TREE && !IS_EMPTY_STMT (update))
15176 tree up2 = update;
15177 if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15178 up2 = EXPR_WFL_NODE (up2);
15179 /* It is possible for the update expression to be an
15180 EXPR_WFL_NODE wrapping nothing. */
15181 if (up2 != NULL_TREE && !IS_EMPTY_STMT (up2))
15183 /* Try to detect constraint violations. These would be
15184 programming errors somewhere. */
15185 if (! EXPR_P (up2) || TREE_CODE (up2) == LOOP_EXPR)
15186 abort ();
15187 SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15190 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15191 return loop;
15194 /* Try to find the loop a block might be related to. This comprises
15195 the case where the LOOP_EXPR is found as the second operand of a
15196 COMPOUND_EXPR, because the loop happens to have an initialization
15197 part, then expressed as the first operand of the COMPOUND_EXPR. If
15198 the search finds something, 1 is returned. Otherwise, 0 is
15199 returned. The search is assumed to start from a
15200 LABELED_BLOCK_EXPR's block. */
15202 static tree
15203 search_loop (tree statement)
15205 if (TREE_CODE (statement) == LOOP_EXPR)
15206 return statement;
15208 if (TREE_CODE (statement) == BLOCK)
15209 statement = BLOCK_SUBBLOCKS (statement);
15210 else
15211 return NULL_TREE;
15213 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15214 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15215 statement = TREE_OPERAND (statement, 1);
15217 return (TREE_CODE (statement) == LOOP_EXPR
15218 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15221 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15222 returned otherwise. */
15224 static int
15225 labeled_block_contains_loop_p (tree block, tree loop)
15227 if (!block)
15228 return 0;
15230 if (LABELED_BLOCK_BODY (block) == loop)
15231 return 1;
15233 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15234 return 1;
15236 return 0;
15239 /* If the loop isn't surrounded by a labeled statement, create one and
15240 insert LOOP as its body. */
15242 static tree
15243 patch_loop_statement (tree loop)
15245 tree loop_label;
15247 TREE_TYPE (loop) = void_type_node;
15248 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15249 return loop;
15251 loop_label = build_labeled_block (0, NULL_TREE);
15252 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15253 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15254 LABELED_BLOCK_BODY (loop_label) = loop;
15255 PUSH_LABELED_BLOCK (loop_label);
15256 return loop_label;
15259 /* 14.13, 14.14: break and continue Statements */
15261 /* Build a break or a continue statement. a null NAME indicates an
15262 unlabeled break/continue statement. */
15264 static tree
15265 build_bc_statement (int location, int is_break, tree name)
15267 tree break_continue, label_block_expr = NULL_TREE;
15269 if (name)
15271 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15272 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15273 /* Null means that we don't have a target for this named
15274 break/continue. In this case, we make the target to be the
15275 label name, so that the error can be reported accurately in
15276 patch_bc_statement. */
15277 label_block_expr = EXPR_WFL_NODE (name);
15279 /* Unlabeled break/continue will be handled during the
15280 break/continue patch operation */
15281 break_continue = build1 (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr);
15283 IS_BREAK_STMT_P (break_continue) = is_break;
15284 TREE_SIDE_EFFECTS (break_continue) = 1;
15285 EXPR_WFL_LINECOL (break_continue) = location;
15286 break_continue = build_debugable_stmt (location, break_continue);
15287 return break_continue;
15290 /* Verification of a break/continue statement. */
15292 static tree
15293 patch_bc_statement (tree node)
15295 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15296 tree labeled_block = ctxp->current_labeled_block;
15297 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15299 /* Having an identifier here means that the target is unknown. */
15300 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15302 parse_error_context (wfl_operator, "No label definition found for %qs",
15303 IDENTIFIER_POINTER (bc_label));
15304 return error_mark_node;
15306 if (! IS_BREAK_STMT_P (node))
15308 /* It's a continue statement. */
15309 for (;; labeled_block = TREE_CHAIN (labeled_block))
15311 if (labeled_block == NULL_TREE)
15313 if (bc_label == NULL_TREE)
15314 parse_error_context (wfl_operator,
15315 "%<continue%> must be in loop");
15316 else
15317 parse_error_context
15318 (wfl_operator, "continue label %qs does not name a loop",
15319 IDENTIFIER_POINTER (bc_label));
15320 return error_mark_node;
15322 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15323 == continue_identifier_node)
15324 && (bc_label == NULL_TREE
15325 || TREE_CHAIN (labeled_block) == bc_label))
15327 bc_label = labeled_block;
15328 break;
15332 else if (!bc_label)
15334 for (;; labeled_block = TREE_CHAIN (labeled_block))
15336 if (labeled_block == NULL_TREE)
15338 parse_error_context (wfl_operator,
15339 "%<break%> must be in loop or switch");
15340 return error_mark_node;
15342 target_stmt = LABELED_BLOCK_BODY (labeled_block);
15343 if (TREE_CODE (target_stmt) == SWITCH_EXPR
15344 || search_loop (target_stmt))
15346 bc_label = labeled_block;
15347 break;
15352 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15353 CAN_COMPLETE_NORMALLY (bc_label) = 1;
15355 /* Our break/continue don't return values. */
15356 TREE_TYPE (node) = void_type_node;
15357 /* Encapsulate the break within a compound statement so that it's
15358 expanded all the times by expand_expr (and not clobbered
15359 sometimes, like after a if statement) */
15360 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15361 TREE_SIDE_EFFECTS (node) = 1;
15362 return node;
15365 /* Process the exit expression belonging to a loop. Its type must be
15366 boolean. */
15368 static tree
15369 patch_exit_expr (tree node)
15371 tree expression = TREE_OPERAND (node, 0);
15372 TREE_TYPE (node) = error_mark_node;
15373 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15375 /* The type of expression must be boolean */
15376 if (TREE_TYPE (expression) != boolean_type_node)
15378 parse_error_context
15379 (wfl_operator,
15380 "Incompatible type for loop conditional. Can't convert %qs to %<boolean%>",
15381 lang_printable_name (TREE_TYPE (expression), 0));
15382 return error_mark_node;
15384 /* Now we know things are allright, invert the condition, fold and
15385 return */
15386 TREE_OPERAND (node, 0) =
15387 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15389 if (! integer_zerop (TREE_OPERAND (node, 0))
15390 && ctxp->current_loop != NULL_TREE
15391 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15392 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15393 if (! integer_onep (TREE_OPERAND (node, 0)))
15394 CAN_COMPLETE_NORMALLY (node) = 1;
15397 TREE_TYPE (node) = void_type_node;
15398 return node;
15401 /* 14.9 Switch statement */
15403 static tree
15404 patch_switch_statement (tree node)
15406 tree se = TREE_OPERAND (node, 0), se_type;
15407 tree save, iter;
15409 /* Complete the switch expression */
15410 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15411 se_type = TREE_TYPE (se);
15412 /* The type of the switch expression must be char, byte, short or
15413 int */
15414 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15416 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15417 parse_error_context (wfl_operator,
15418 "Incompatible type for %<switch%>. Can't convert %qs to %<int%>",
15419 lang_printable_name (se_type, 0));
15420 /* This is what java_complete_tree will check */
15421 TREE_OPERAND (node, 0) = error_mark_node;
15422 return error_mark_node;
15425 /* Save and restore the outer case label list. */
15426 save = case_label_list;
15427 case_label_list = NULL_TREE;
15429 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15431 /* See if we've found a duplicate label. We can't leave this until
15432 code generation, because in `--syntax-only' and `-C' modes we
15433 don't do ordinary code generation. */
15434 for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15436 HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15437 tree subiter;
15438 for (subiter = TREE_CHAIN (iter);
15439 subiter != NULL_TREE;
15440 subiter = TREE_CHAIN (subiter))
15442 HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15443 if (val == subval)
15445 EXPR_WFL_LINECOL (wfl_operator)
15446 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15447 /* The case_label_list is in reverse order, so print the
15448 outer label first. */
15449 parse_error_context (wfl_operator, "duplicate case label: %<"
15450 HOST_WIDE_INT_PRINT_DEC "%>", subval);
15451 EXPR_WFL_LINECOL (wfl_operator)
15452 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15453 parse_error_context (wfl_operator, "original label is here");
15455 break;
15460 case_label_list = save;
15462 /* Ready to return */
15463 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15465 TREE_TYPE (node) = error_mark_node;
15466 return error_mark_node;
15468 TREE_TYPE (node) = void_type_node;
15469 TREE_SIDE_EFFECTS (node) = 1;
15470 CAN_COMPLETE_NORMALLY (node)
15471 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15472 || ! SWITCH_HAS_DEFAULT (node);
15473 return node;
15476 /* Assertions. */
15478 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15479 might be NULL_TREE. */
15480 static tree
15481 build_assertion (
15482 #ifdef USE_MAPPED_LOCATION
15483 source_location location,
15484 #else
15485 int location,
15486 #endif
15487 tree condition, tree value)
15489 tree node;
15490 tree klass = GET_CPC ();
15492 if (! enable_assertions (klass))
15494 condition = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15495 boolean_false_node, condition);
15496 if (value == NULL_TREE)
15497 value = build_java_empty_stmt ();
15498 return build_if_else_statement (location, condition,
15499 value, NULL_TREE);
15502 if (! CLASS_USES_ASSERTIONS (klass))
15504 tree field, classdollar, id, call;
15505 tree class_type = TREE_TYPE (klass);
15507 field = add_field (class_type,
15508 get_identifier ("$assertionsDisabled"),
15509 boolean_type_node,
15510 ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15511 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15512 FIELD_SYNTHETIC (field) = 1;
15514 classdollar = build_incomplete_class_ref (location, class_type);
15516 /* Call CLASS.desiredAssertionStatus(). */
15517 id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15518 call = build3 (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15519 call = make_qualified_primary (classdollar, call, location);
15520 TREE_SIDE_EFFECTS (call) = 1;
15522 /* Invert to obtain !CLASS.desiredAssertionStatus(). This may
15523 seem odd, but we do it to generate code identical to that of
15524 the JDK. */
15525 call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15526 TREE_SIDE_EFFECTS (call) = 1;
15527 DECL_INITIAL (field) = call;
15529 /* Record the initializer in the initializer statement list. */
15530 call = build2 (MODIFY_EXPR, NULL_TREE, field, call);
15531 TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15532 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15533 MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15535 CLASS_USES_ASSERTIONS (klass) = 1;
15538 if (value != NULL_TREE)
15539 value = tree_cons (NULL_TREE, value, NULL_TREE);
15541 node = build_wfl_node (get_identifier ("java"));
15542 node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15543 location);
15544 node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15545 location);
15547 node = build3 (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15548 TREE_SIDE_EFFECTS (node) = 1;
15549 /* It is too early to use BUILD_THROW. */
15550 node = build1 (THROW_EXPR, NULL_TREE, node);
15551 TREE_SIDE_EFFECTS (node) = 1;
15553 /* We invert the condition; if we just put NODE as the `else' part
15554 then we generate weird-looking bytecode. */
15555 condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15556 /* Check $assertionsDisabled. */
15557 condition
15558 = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15559 build1 (TRUTH_NOT_EXPR, NULL_TREE,
15560 build_wfl_node (get_identifier ("$assertionsDisabled"))),
15561 condition);
15562 node = build_if_else_statement (location, condition, node, NULL_TREE);
15563 return node;
15566 /* 14.18 The try/catch statements */
15568 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15569 catches TYPE and executes CATCH_STMTS. */
15571 static tree
15572 encapsulate_with_try_catch (int location, tree type_or_name, tree try_stmts,
15573 tree catch_stmts)
15575 tree try_block, catch_clause_param, catch_block, catch;
15577 /* First build a try block */
15578 try_block = build_expr_block (try_stmts, NULL_TREE);
15580 /* Build a catch block: we need a catch clause parameter */
15581 if (TREE_CODE (type_or_name) == EXPR_WITH_FILE_LOCATION)
15583 tree catch_type = obtain_incomplete_type (type_or_name);
15584 jdep *dep;
15585 catch_clause_param = build_decl (VAR_DECL, wpv_id, catch_type);
15586 register_incomplete_type (JDEP_VARIABLE, type_or_name,
15587 catch_clause_param, catch_type);
15588 dep = CLASSD_LAST (ctxp->classd_list);
15589 JDEP_GET_PATCH (dep) = &TREE_TYPE (catch_clause_param);
15591 else
15592 catch_clause_param = build_decl (VAR_DECL, wpv_id,
15593 build_pointer_type (type_or_name));
15595 /* And a block */
15596 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15598 /* Initialize the variable and store in the block */
15599 catch = build2 (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15600 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15601 add_stmt_to_block (catch_block, NULL_TREE, catch);
15603 /* Add the catch statements */
15604 add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15606 /* Now we can build a JAVA_CATCH_EXPR */
15607 catch_block = build1 (JAVA_CATCH_EXPR, NULL_TREE, catch_block);
15609 return build_try_statement (location, try_block, catch_block);
15612 static tree
15613 build_try_statement (int location, tree try_block, tree catches)
15615 tree node = build2 (TRY_EXPR, NULL_TREE, try_block, catches);
15616 EXPR_WFL_LINECOL (node) = location;
15617 return node;
15620 static tree
15621 build_try_finally_statement (int location, tree try_block, tree finally)
15623 tree node = build2 (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15624 EXPR_WFL_LINECOL (node) = location;
15625 return node;
15628 static tree
15629 patch_try_statement (tree node)
15631 int error_found = 0;
15632 tree try = TREE_OPERAND (node, 0);
15633 /* Exception handlers are considered in left to right order */
15634 tree catch = nreverse (TREE_OPERAND (node, 1));
15635 tree current, caught_type_list = NULL_TREE;
15637 /* Check catch clauses, if any. Every time we find an error, we try
15638 to process the next catch clause. We process the catch clause before
15639 the try block so that when processing the try block we can check thrown
15640 exceptions against the caught type list. */
15641 for (current = catch; current; current = TREE_CHAIN (current))
15643 tree carg_decl, carg_type;
15644 tree sub_current, catch_block, catch_clause;
15645 int unreachable;
15647 /* At this point, the structure of the catch clause is
15648 JAVA_CATCH_EXPR (catch node)
15649 BLOCK (with the decl of the parameter)
15650 COMPOUND_EXPR
15651 MODIFY_EXPR (assignment of the catch parameter)
15652 BLOCK (catch clause block)
15654 catch_clause = TREE_OPERAND (current, 0);
15655 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15656 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15658 /* Catch clauses can't have more than one parameter declared,
15659 but it's already enforced by the grammar. Make sure that the
15660 only parameter of the clause statement in of class Throwable
15661 or a subclass of Throwable, but that was done earlier. The
15662 catch clause parameter type has also been resolved. */
15664 /* Just make sure that the catch clause parameter type inherits
15665 from java.lang.Throwable */
15666 if (!inherits_from_p (carg_type, throwable_type_node))
15668 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15669 parse_error_context (wfl_operator,
15670 "Can't catch class %qs. Catch clause parameter type must be a subclass of class %<java.lang.Throwable%>",
15671 lang_printable_name (carg_type, 0));
15672 error_found = 1;
15673 continue;
15676 /* Partial check for unreachable catch statement: The catch
15677 clause is reachable iff is no earlier catch block A in
15678 the try statement such that the type of the catch
15679 clause's parameter is the same as or a subclass of the
15680 type of A's parameter */
15681 unreachable = 0;
15682 for (sub_current = catch;
15683 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15685 tree sub_catch_clause, decl;
15686 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15687 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15689 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15691 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15692 parse_error_context
15693 (wfl_operator,
15694 "%<catch%> not reached because of the catch clause at line %d",
15695 EXPR_WFL_LINENO (sub_current));
15696 unreachable = error_found = 1;
15697 break;
15700 /* Complete the catch clause block */
15701 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15702 if (catch_block == error_mark_node)
15704 error_found = 1;
15705 continue;
15707 if (CAN_COMPLETE_NORMALLY (catch_block))
15708 CAN_COMPLETE_NORMALLY (node) = 1;
15709 TREE_OPERAND (current, 0) = catch_block;
15711 if (unreachable)
15712 continue;
15714 /* Things to do here: the exception must be thrown */
15716 /* Link this type to the caught type list */
15717 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15720 PUSH_EXCEPTIONS (caught_type_list);
15721 if ((try = java_complete_tree (try)) == error_mark_node)
15722 error_found = 1;
15723 if (CAN_COMPLETE_NORMALLY (try))
15724 CAN_COMPLETE_NORMALLY (node) = 1;
15725 POP_EXCEPTIONS ();
15727 /* Verification ends here */
15728 if (error_found)
15729 return error_mark_node;
15731 TREE_OPERAND (node, 0) = try;
15732 TREE_OPERAND (node, 1) = catch;
15733 TREE_TYPE (node) = void_type_node;
15734 return node;
15737 /* 14.17 The synchronized Statement */
15739 static tree
15740 patch_synchronized_statement (tree node, tree wfl_op1)
15742 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15743 tree block = TREE_OPERAND (node, 1);
15745 tree tmp, enter, exit, expr_decl, assignment;
15747 if (expr == error_mark_node)
15749 block = java_complete_tree (block);
15750 return expr;
15753 /* We might be trying to synchronize on a STRING_CST */
15754 if ((tmp = patch_string (expr)))
15755 expr = tmp;
15757 /* The TYPE of expr must be a reference type */
15758 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15760 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15761 parse_error_context (wfl_operator, "Incompatible type for %<synchronized%>. Can't convert %qs to %<java.lang.Object%>",
15762 lang_printable_name (TREE_TYPE (expr), 0));
15763 return error_mark_node;
15766 if (flag_emit_xref)
15768 TREE_OPERAND (node, 0) = expr;
15769 TREE_OPERAND (node, 1) = java_complete_tree (block);
15770 CAN_COMPLETE_NORMALLY (node) = 1;
15771 return node;
15774 /* Generate a try-finally for the synchronized statement, except
15775 that the handler that catches all throw exception calls
15776 _Jv_MonitorExit and then rethrow the exception.
15777 The synchronized statement is then implemented as:
15780 _Jv_MonitorEnter (expression)
15781 synchronized_block
15782 _Jv_MonitorExit (expression)
15784 CATCH_ALL
15786 e = _Jv_exception_info ();
15787 _Jv_MonitorExit (expression)
15788 Throw (e);
15789 } */
15791 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15792 BUILD_MONITOR_ENTER (enter, expr_decl);
15793 BUILD_MONITOR_EXIT (exit, expr_decl);
15794 CAN_COMPLETE_NORMALLY (enter) = 1;
15795 CAN_COMPLETE_NORMALLY (exit) = 1;
15796 assignment = build2 (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15797 TREE_SIDE_EFFECTS (assignment) = 1;
15798 node = build2 (COMPOUND_EXPR, NULL_TREE,
15799 build2 (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15800 build2 (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15801 node = build_expr_block (node, expr_decl);
15803 return java_complete_tree (node);
15806 /* 14.16 The throw Statement */
15808 static tree
15809 patch_throw_statement (tree node, tree wfl_op1)
15811 tree expr = TREE_OPERAND (node, 0);
15812 tree type = TREE_TYPE (expr);
15813 int unchecked_ok = 0, tryblock_throws_ok = 0;
15815 /* Thrown expression must be assignable to java.lang.Throwable */
15816 if (!try_reference_assignconv (throwable_type_node, expr))
15818 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15819 parse_error_context (wfl_operator,
15820 "Can't throw %qs; it must be a subclass of class %<java.lang.Throwable%>",
15821 lang_printable_name (type, 0));
15822 /* If the thrown expression was a reference, we further the
15823 compile-time check. */
15824 if (!JREFERENCE_TYPE_P (type))
15825 return error_mark_node;
15828 /* At least one of the following must be true */
15830 /* The type of the throw expression is a not checked exception,
15831 i.e. is a unchecked expression. */
15832 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15834 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15835 /* An instance can't throw a checked exception unless that exception
15836 is explicitly declared in the `throws' clause of each
15837 constructor. This doesn't apply to anonymous classes, since they
15838 don't have declared constructors. */
15839 if (!unchecked_ok
15840 && DECL_INSTINIT_P (current_function_decl)
15841 && !ANONYMOUS_CLASS_P (current_class))
15843 tree current;
15844 for (current = TYPE_METHODS (current_class); current;
15845 current = TREE_CHAIN (current))
15846 if (DECL_CONSTRUCTOR_P (current)
15847 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15849 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)",
15850 lang_printable_name (TREE_TYPE (expr), 0));
15851 return error_mark_node;
15855 /* Throw is contained in a try statement and at least one catch
15856 clause can receive the thrown expression or the current method is
15857 declared to throw such an exception. Or, the throw statement is
15858 contained in a method or constructor declaration and the type of
15859 the Expression is assignable to at least one type listed in the
15860 throws clause the declaration. */
15861 if (!unchecked_ok)
15862 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15863 if (!(unchecked_ok || tryblock_throws_ok))
15865 /* If there is a surrounding try block that has no matching
15866 clatch clause, report it first. A surrounding try block exits
15867 only if there is something after the list of checked
15868 exception thrown by the current function (if any). */
15869 if (IN_TRY_BLOCK_P ())
15870 parse_error_context (wfl_operator, "Checked exception %qs can't be caught by any of the catch clause(s) of the surrounding %<try%> block",
15871 lang_printable_name (type, 0));
15872 /* If we have no surrounding try statement and the method doesn't have
15873 any throws, report it now. FIXME */
15875 /* We report that the exception can't be throw from a try block
15876 in all circumstances but when the `throw' is inside a static
15877 block. */
15878 else if (!EXCEPTIONS_P (currently_caught_type_list)
15879 && !tryblock_throws_ok)
15881 if (DECL_CLINIT_P (current_function_decl))
15882 parse_error_context (wfl_operator,
15883 "Checked exception %qs can't be thrown in initializer",
15884 lang_printable_name (type, 0));
15885 else
15886 parse_error_context (wfl_operator,
15887 "Checked exception %qs isn't thrown from a %<try%> block",
15888 lang_printable_name (type, 0));
15890 /* Otherwise, the current method doesn't have the appropriate
15891 throws declaration */
15892 else
15893 parse_error_context (wfl_operator, "Checked exception %qs doesn't match any of current method's %<throws%> declaration(s)",
15894 lang_printable_name (type, 0));
15895 return error_mark_node;
15898 if (! flag_emit_class_files && ! flag_emit_xref)
15899 BUILD_THROW (node, expr);
15901 /* If doing xrefs, keep the location where the `throw' was seen. */
15902 if (flag_emit_xref)
15903 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15904 return node;
15907 /* Check that exception said to be thrown by method DECL can be
15908 effectively caught from where DECL is invoked. THIS_EXPR is the
15909 expression that computes `this' for the method call. */
15910 static void
15911 check_thrown_exceptions (
15912 #ifdef USE_MAPPED_LOCATION
15913 source_location location,
15914 #else
15916 int location,
15917 #endif
15918 tree decl, tree this_expr)
15920 tree throws;
15921 int is_array_call = 0;
15923 /* Skip check within generated methods, such as access$<n>. */
15924 if (OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (current_function_decl)))
15925 return;
15927 if (this_expr != NULL_TREE
15928 && TREE_CODE (TREE_TYPE (this_expr)) == POINTER_TYPE
15929 && TYPE_ARRAY_P (TREE_TYPE (TREE_TYPE (this_expr))))
15930 is_array_call = 1;
15932 /* For all the unchecked exceptions thrown by DECL. */
15933 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15934 throws = TREE_CHAIN (throws))
15935 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15937 /* Suppress errors about cloning arrays. */
15938 if (is_array_call && DECL_NAME (decl) == get_identifier ("clone"))
15939 continue;
15941 #ifdef USE_MAPPED_LOCATION
15942 SET_EXPR_LOCATION (wfl_operator, location);
15943 #else
15944 EXPR_WFL_LINECOL (wfl_operator) = location;
15945 #endif
15946 if (DECL_FINIT_P (current_function_decl))
15947 parse_error_context
15948 (wfl_operator, "Exception %qs can't be thrown in initializer",
15949 lang_printable_name (TREE_VALUE (throws), 0));
15950 else
15952 parse_error_context
15953 (wfl_operator, "Exception %qs must be caught, or it must be declared in the %<throws%> clause of %qs",
15954 lang_printable_name (TREE_VALUE (throws), 0),
15955 (DECL_INIT_P (current_function_decl) ?
15956 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15957 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15962 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15963 try-catch blocks, OR is listed in the `throws' clause of the
15964 current method. */
15966 static int
15967 check_thrown_exceptions_do (tree exception)
15969 tree list = currently_caught_type_list;
15970 resolve_and_layout (exception, NULL_TREE);
15971 /* First, all the nested try-catch-finally at that stage. The
15972 last element contains `throws' clause exceptions, if any. */
15973 if (IS_UNCHECKED_EXCEPTION_P (exception))
15974 return 1;
15975 while (list)
15977 tree caught;
15978 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15979 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15980 return 1;
15981 list = TREE_CHAIN (list);
15983 return 0;
15986 static void
15987 purge_unchecked_exceptions (tree mdecl)
15989 tree throws = DECL_FUNCTION_THROWS (mdecl);
15990 tree new = NULL_TREE;
15992 while (throws)
15994 tree next = TREE_CHAIN (throws);
15995 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15997 TREE_CHAIN (throws) = new;
15998 new = throws;
16000 throws = next;
16002 /* List is inverted here, but it doesn't matter */
16003 DECL_FUNCTION_THROWS (mdecl) = new;
16006 /* This function goes over all of CLASS_TYPE ctors and checks whether
16007 each of them features at least one unchecked exception in its
16008 `throws' clause. If it's the case, it returns `true', `false'
16009 otherwise. */
16011 static bool
16012 ctors_unchecked_throws_clause_p (tree class_type)
16014 tree current;
16016 for (current = TYPE_METHODS (class_type); current;
16017 current = TREE_CHAIN (current))
16019 bool ctu = false; /* Ctor Throws Unchecked */
16020 if (DECL_CONSTRUCTOR_P (current))
16022 tree throws;
16023 for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
16024 throws = TREE_CHAIN (throws))
16025 if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
16026 ctu = true;
16028 /* We return false as we found one ctor that is unfit. */
16029 if (!ctu && DECL_CONSTRUCTOR_P (current))
16030 return false;
16032 /* All ctors feature at least one unchecked exception in their
16033 `throws' clause. */
16034 return true;
16037 /* 15.24 Conditional Operator ?: */
16039 static tree
16040 patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
16042 tree cond = TREE_OPERAND (node, 0);
16043 tree op1 = TREE_OPERAND (node, 1);
16044 tree op2 = TREE_OPERAND (node, 2);
16045 tree resulting_type = NULL_TREE;
16046 tree t1, t2, patched;
16047 int error_found = 0;
16049 /* The condition and operands of ?: might be StringBuffers crafted
16050 as a result of a string concatenation. Obtain decent ones here. */
16051 if ((patched = patch_string (cond)))
16052 TREE_OPERAND (node, 0) = cond = patched;
16053 if ((patched = patch_string (op1)))
16054 TREE_OPERAND (node, 1) = op1 = patched;
16055 if ((patched = patch_string (op2)))
16056 TREE_OPERAND (node, 2) = op2 = patched;
16058 t1 = TREE_TYPE (op1);
16059 t2 = TREE_TYPE (op2);
16061 /* The first expression must be a boolean */
16062 if (TREE_TYPE (cond) != boolean_type_node)
16064 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
16065 parse_error_context (wfl_operator,
16066 "Incompatible type for %<?:%>. Can't convert %qs to %<boolean%>",
16067 lang_printable_name (TREE_TYPE (cond), 0));
16068 error_found = 1;
16071 /* Second and third can be numeric, boolean (i.e. primitive),
16072 references or null. Anything else results in an error */
16073 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
16074 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
16075 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
16076 || (t1 == boolean_type_node && t2 == boolean_type_node)))
16077 error_found = 1;
16079 /* Determine the type of the conditional expression. Same types are
16080 easy to deal with */
16081 else if (t1 == t2)
16082 resulting_type = t1;
16084 /* There are different rules for numeric types */
16085 else if (JNUMERIC_TYPE_P (t1))
16087 /* if byte/short found, the resulting type is short */
16088 if ((t1 == byte_type_node && t2 == short_type_node)
16089 || (t1 == short_type_node && t2 == byte_type_node))
16090 resulting_type = short_type_node;
16092 /* If t1 is a constant int and t2 is of type byte, short or char
16093 and t1's value fits in t2, then the resulting type is t2 */
16094 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
16095 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
16096 resulting_type = t2;
16098 /* If t2 is a constant int and t1 is of type byte, short or char
16099 and t2's value fits in t1, then the resulting type is t1 */
16100 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
16101 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
16102 resulting_type = t1;
16104 /* Otherwise, binary numeric promotion is applied and the
16105 resulting type is the promoted type of operand 1 and 2 */
16106 else
16107 resulting_type = binary_numeric_promotion (t1, t2,
16108 &TREE_OPERAND (node, 1),
16109 &TREE_OPERAND (node, 2));
16112 /* Cases of a reference and a null type */
16113 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
16114 resulting_type = t1;
16116 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
16117 resulting_type = t2;
16119 /* Last case: different reference types. If a type can be converted
16120 into the other one by assignment conversion, the latter
16121 determines the type of the expression */
16122 else if ((resulting_type = try_reference_assignconv (t1, op2)))
16123 resulting_type = promote_type (t1);
16125 else if ((resulting_type = try_reference_assignconv (t2, op1)))
16126 resulting_type = promote_type (t2);
16128 /* If we don't have any resulting type, we're in trouble */
16129 if (!resulting_type)
16131 char *t = xstrdup (lang_printable_name (t1, 0));
16132 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16133 parse_error_context (wfl_operator,
16134 "Incompatible type for %<?:%>. Can't convert %qs to %qs",
16135 t, lang_printable_name (t2, 0));
16136 free (t);
16137 error_found = 1;
16140 if (error_found)
16142 TREE_TYPE (node) = error_mark_node;
16143 return error_mark_node;
16146 TREE_TYPE (node) = resulting_type;
16147 TREE_SET_CODE (node, COND_EXPR);
16148 CAN_COMPLETE_NORMALLY (node) = 1;
16149 return node;
16152 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16154 static tree
16155 maybe_build_class_init_for_field (tree decl, tree expr)
16157 tree clas = DECL_CONTEXT (decl);
16158 if (flag_emit_class_files || flag_emit_xref)
16159 return expr;
16161 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16162 && FIELD_FINAL (decl))
16164 tree init = DECL_INITIAL (decl);
16165 if (init != NULL_TREE)
16166 init = fold_constant_for_init (init, decl);
16167 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16168 return expr;
16171 return build_class_init (clas, expr);
16174 /* Try to constant fold NODE.
16175 If NODE is not a constant expression, return NULL_EXPR.
16176 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16178 static tree
16179 fold_constant_for_init (tree node, tree context)
16181 tree op0, op1, val;
16182 enum tree_code code = TREE_CODE (node);
16184 switch (code)
16186 case INTEGER_CST:
16187 if (node == null_pointer_node)
16188 return NULL_TREE;
16189 case STRING_CST:
16190 case REAL_CST:
16191 return node;
16193 case PLUS_EXPR:
16194 case MINUS_EXPR:
16195 case MULT_EXPR:
16196 case TRUNC_MOD_EXPR:
16197 case RDIV_EXPR:
16198 case LSHIFT_EXPR:
16199 case RSHIFT_EXPR:
16200 case URSHIFT_EXPR:
16201 case BIT_AND_EXPR:
16202 case BIT_XOR_EXPR:
16203 case BIT_IOR_EXPR:
16204 case TRUTH_ANDIF_EXPR:
16205 case TRUTH_ORIF_EXPR:
16206 case EQ_EXPR:
16207 case NE_EXPR:
16208 case GT_EXPR:
16209 case GE_EXPR:
16210 case LT_EXPR:
16211 case LE_EXPR:
16212 op0 = TREE_OPERAND (node, 0);
16213 op1 = TREE_OPERAND (node, 1);
16214 val = fold_constant_for_init (op0, context);
16215 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16216 return NULL_TREE;
16217 TREE_OPERAND (node, 0) = val;
16218 val = fold_constant_for_init (op1, context);
16219 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16220 return NULL_TREE;
16221 TREE_OPERAND (node, 1) = val;
16222 return patch_binop (node, op0, op1);
16224 case UNARY_PLUS_EXPR:
16225 case NEGATE_EXPR:
16226 case TRUTH_NOT_EXPR:
16227 case BIT_NOT_EXPR:
16228 case CONVERT_EXPR:
16229 op0 = TREE_OPERAND (node, 0);
16230 val = fold_constant_for_init (op0, context);
16231 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16232 return NULL_TREE;
16233 TREE_OPERAND (node, 0) = val;
16234 val = patch_unaryop (node, op0);
16235 if (! TREE_CONSTANT (val))
16236 return NULL_TREE;
16237 return val;
16239 break;
16241 case COND_EXPR:
16242 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16243 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16244 return NULL_TREE;
16245 TREE_OPERAND (node, 0) = val;
16246 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16247 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16248 return NULL_TREE;
16249 TREE_OPERAND (node, 1) = val;
16250 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16251 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16252 return NULL_TREE;
16253 TREE_OPERAND (node, 2) = val;
16254 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
16255 : TREE_OPERAND (node, 2);
16257 case VAR_DECL:
16258 case FIELD_DECL:
16259 if (! FIELD_FINAL (node)
16260 || DECL_INITIAL (node) == NULL_TREE)
16261 return NULL_TREE;
16262 val = DECL_INITIAL (node);
16263 /* Guard against infinite recursion. */
16264 DECL_INITIAL (node) = NULL_TREE;
16265 val = fold_constant_for_init (val, node);
16266 if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16267 val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16268 DECL_INITIAL (node) = val;
16269 return val;
16271 case EXPR_WITH_FILE_LOCATION:
16272 /* Compare java_complete_tree and resolve_expression_name. */
16273 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16274 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16276 tree name = EXPR_WFL_NODE (node);
16277 tree decl;
16278 if (PRIMARY_P (node))
16279 return NULL_TREE;
16280 else if (! QUALIFIED_P (name))
16282 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16283 if (decl == NULL_TREE
16284 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16285 return NULL_TREE;
16286 return fold_constant_for_init (decl, decl);
16288 else
16290 tree r = NULL_TREE;
16291 /* Install the proper context for the field resolution. */
16292 tree saved_current_class = current_class;
16293 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
16294 current_class = DECL_CONTEXT (context);
16295 qualify_ambiguous_name (node);
16296 r = resolve_field_access (node, &decl, NULL);
16297 /* Restore prior context. */
16298 current_class = saved_current_class;
16299 if (r != error_mark_node && decl != NULL_TREE)
16300 return fold_constant_for_init (decl, decl);
16301 return NULL_TREE;
16304 else
16306 op0 = TREE_OPERAND (node, 0);
16307 val = fold_constant_for_init (op0, context);
16308 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16309 return NULL_TREE;
16310 TREE_OPERAND (node, 0) = val;
16311 return val;
16314 #ifdef USE_COMPONENT_REF
16315 case IDENTIFIER:
16316 case COMPONENT_REF:
16318 #endif
16320 default:
16321 return NULL_TREE;
16325 #ifdef USE_COMPONENT_REF
16326 /* Context is 'T' for TypeName, 'P' for PackageName,
16327 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16329 tree
16330 resolve_simple_name (tree name, int context)
16334 tree
16335 resolve_qualified_name (tree name, int context)
16338 #endif
16340 void
16341 init_src_parse (void)
16343 /* Sanity check; we've been bit by this before. */
16344 if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16345 abort ();
16350 /* This section deals with the functions that are called when tables
16351 recording class initialization information are traversed. */
16353 /* This function is called for each class that is known definitely
16354 initialized when a given static method was called. This function
16355 augments a compound expression (INFO) storing all assignment to
16356 initialized static class flags if a flag already existed, otherwise
16357 a new one is created. */
16359 static int
16360 emit_test_initialization (void **entry_p, void *info)
16362 tree l = (tree) info;
16363 tree decl, init;
16364 tree key = (tree) *entry_p;
16365 tree *ite;
16366 htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16368 /* If we haven't found a flag and we're dealing with self registered
16369 with current_function_decl, then don't do anything. Self is
16370 always added as definitely initialized but this information is
16371 valid only if used outside the current function. */
16372 if (current_function_decl == TREE_PURPOSE (l)
16373 && java_treetreehash_find (cf_ht, key) == NULL)
16374 return true;
16376 ite = java_treetreehash_new (cf_ht, key);
16378 /* If we don't have a variable, create one and install it. */
16379 if (*ite == NULL)
16381 tree block;
16383 decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16384 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16385 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16386 DECL_CONTEXT (decl) = current_function_decl;
16387 DECL_INITIAL (decl) = boolean_true_node;
16388 /* Don't emit any symbolic debugging info for this decl. */
16389 DECL_IGNORED_P (decl) = 1;
16391 /* The trick is to find the right context for it. */
16392 block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16393 TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16394 BLOCK_EXPR_DECLS (block) = decl;
16395 *ite = decl;
16397 else
16398 decl = *ite;
16400 /* Now simply augment the compound that holds all the assignments
16401 pertaining to this method invocation. */
16402 init = build2 (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16403 TREE_SIDE_EFFECTS (init) = 1;
16404 TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16405 TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16407 return true;
16410 #ifdef __XGETTEXT__
16411 /* Depending on the version of Bison used to compile this grammar,
16412 it may issue generic diagnostics spelled "syntax error" or
16413 "parse error". To prevent this from changing the translation
16414 template randomly, we list all the variants of this particular
16415 diagnostic here. Translators: there is no fine distinction
16416 between diagnostics with "syntax error" in them, and diagnostics
16417 with "parse error" in them. It's okay to give them both the same
16418 translation. */
16419 const char d1[] = N_("syntax error");
16420 const char d2[] = N_("parse error");
16421 const char d3[] = N_("syntax error; also virtual memory exhausted");
16422 const char d4[] = N_("parse error; also virtual memory exhausted");
16423 const char d5[] = N_("syntax error: cannot back up");
16424 const char d6[] = N_("parse error: cannot back up");
16425 #endif
16427 #include "gt-java-parse.h"
16428 #include "gtype-java.h"