PR testsuite/21969
[official-gcc.git] / gcc / java / parse.y
blob0a513b1f92d85d4a71ed83e2af890dff0d690a29
1 /* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
5 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc. */
28 /* This file parses java source code and issues a tree node image
29 suitable for code generation (byte code and targeted CPU assembly
30 language).
32 The grammar conforms to the Java grammar described in "The Java(TM)
33 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
34 1996, ISBN 0-201-63451-1"
36 The following modifications were brought to the original grammar:
38 method_body: added the rule '| block SC_TK'
39 static_initializer: added the rule 'static block SC_TK'.
41 Note: All the extra rules described above should go away when the
42 empty_statement rule will work.
44 statement_nsi: 'nsi' should be read no_short_if.
46 Some rules have been modified to support JDK1.1 inner classes
47 definitions and other extensions. */
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include <dirent.h>
55 #include "tree.h"
56 #include "rtl.h"
57 #include "real.h"
58 #include "obstack.h"
59 #include "toplev.h"
60 #include "pretty-print.h"
61 #include "diagnostic.h"
62 #include "flags.h"
63 #include "java-tree.h"
64 #include "jcf.h"
65 #include "lex.h"
66 #include "parse.h"
67 #include "zipfile.h"
68 #include "convert.h"
69 #include "buffer.h"
70 #include "function.h"
71 #include "except.h"
72 #include "ggc.h"
73 #include "debug.h"
74 #include "tree-inline.h"
75 #include "cgraph.h"
76 #include "target.h"
78 /* Local function prototypes */
79 static char *java_accstring_lookup (int);
80 static const char *accessibility_string (int);
81 static void classitf_redefinition_error (const char *,tree, tree, tree);
82 static void variable_redefinition_error (tree, tree, tree, int);
83 static tree create_class (int, tree, tree, tree);
84 static tree create_interface (int, tree, tree);
85 static void end_class_declaration (int);
86 static tree find_field (tree, tree);
87 static tree lookup_field_wrapper (tree, tree);
88 static int duplicate_declaration_error_p (tree, tree, tree);
89 static void register_fields (int, tree, tree);
90 static tree parser_qualified_classname (tree);
91 static int parser_check_super (tree, tree, tree);
92 static int parser_check_super_interface (tree, tree, tree);
93 static void check_modifiers_consistency (int);
94 static tree lookup_cl (tree);
95 static tree lookup_java_method2 (tree, tree, int);
96 static tree method_header (int, tree, tree, tree);
97 static void fix_method_argument_names (tree ,tree);
98 static tree method_declarator (tree, tree);
99 static void parse_warning_context (tree cl, const char *gmsgid, ...) ATTRIBUTE_GCC_DIAG(2,3);
100 #ifdef USE_MAPPED_LOCATION
101 static void issue_warning_error_from_context
102 (source_location, const char *gmsgid, va_list *);
103 #else
104 static void issue_warning_error_from_context
105 (tree, const char *gmsgid, va_list *);
106 #endif
107 static void parse_ctor_invocation_error (void);
108 static tree parse_jdk1_1_error (const char *);
109 static void complete_class_report_errors (jdep *);
110 static int process_imports (void);
111 static void read_import_dir (tree);
112 static int find_in_imports_on_demand (tree, tree);
113 static void find_in_imports (tree, tree);
114 static void check_inner_class_access (tree, tree, tree);
115 static int check_pkg_class_access (tree, tree, bool, tree);
116 static tree resolve_package (tree, tree *, tree *);
117 static tree resolve_class (tree, tree, tree, tree);
118 static void declare_local_variables (int, tree, tree);
119 static void dump_java_tree (enum tree_dump_index, tree);
120 static void source_start_java_method (tree);
121 static void source_end_java_method (void);
122 static tree find_name_in_single_imports (tree);
123 static void check_abstract_method_header (tree);
124 static tree lookup_java_interface_method2 (tree, tree);
125 static tree resolve_expression_name (tree, tree *);
126 static tree maybe_create_class_interface_decl (tree, tree, tree, tree);
127 static int check_class_interface_creation (int, int, tree, tree, tree, tree);
128 static tree patch_method_invocation (tree, tree, tree, int, int *, tree *);
129 static tree resolve_and_layout (tree, tree);
130 static tree qualify_and_find (tree, tree, tree);
131 static tree resolve_no_layout (tree, tree);
132 static int invocation_mode (tree, int);
133 static tree find_applicable_accessible_methods_list (int, tree, tree, tree);
134 static void search_applicable_methods_list (int, tree, tree, tree, tree *, tree *);
135 static tree find_most_specific_methods_list (tree);
136 static int argument_types_convertible (tree, tree);
137 static tree patch_invoke (tree, tree, tree);
138 static int maybe_use_access_method (int, tree *, tree *);
139 static tree lookup_method_invoke (int, tree, tree, tree, tree);
140 static tree register_incomplete_type (int, tree, tree, tree);
141 static tree check_inner_circular_reference (tree, tree);
142 static tree check_circular_reference (tree);
143 static tree obtain_incomplete_type (tree);
144 static tree java_complete_lhs (tree);
145 static tree java_complete_tree (tree);
146 static tree maybe_generate_pre_expand_clinit (tree);
147 static int analyze_clinit_body (tree, tree);
148 static int maybe_yank_clinit (tree);
149 static void start_complete_expand_method (tree);
150 static void java_complete_expand_method (tree);
151 static void java_expand_method_bodies (tree);
152 static int unresolved_type_p (tree, tree *);
153 static void create_jdep_list (struct parser_ctxt *);
154 static tree build_expr_block (tree, tree);
155 static tree enter_block (void);
156 static tree exit_block (void);
157 static tree lookup_name_in_blocks (tree);
158 static void maybe_absorb_scoping_blocks (void);
159 static tree build_method_invocation (tree, tree);
160 static tree build_new_invocation (tree, tree);
161 static tree build_assignment (int, int, tree, tree);
162 static tree build_binop (enum tree_code, int, tree, tree);
163 static tree patch_assignment (tree, tree);
164 static tree patch_binop (tree, tree, tree, int);
165 static tree build_unaryop (int, int, tree);
166 static tree build_incdec (int, int, tree, int);
167 static tree patch_unaryop (tree, tree);
168 static tree build_cast (int, tree, tree);
169 static tree build_null_of_type (tree);
170 static tree patch_cast (tree, tree);
171 static int valid_ref_assignconv_cast_p (tree, tree, int);
172 static int valid_builtin_assignconv_identity_widening_p (tree, tree);
173 static int valid_cast_to_p (tree, tree);
174 static int valid_method_invocation_conversion_p (tree, tree);
175 static tree try_builtin_assignconv (tree, tree, tree);
176 static tree try_reference_assignconv (tree, tree);
177 static tree build_unresolved_array_type (tree);
178 static int build_type_name_from_array_name (tree, tree *);
179 static tree build_array_from_name (tree, tree, tree, tree *);
180 static tree build_array_ref (int, tree, tree);
181 static tree patch_array_ref (tree);
182 #ifdef USE_MAPPED_LOCATION
183 static tree make_qualified_name (tree, tree, source_location);
184 #else
185 static tree make_qualified_name (tree, tree, int);
186 #endif
187 static tree merge_qualified_name (tree, tree);
188 static tree make_qualified_primary (tree, tree, int);
189 static int resolve_qualified_expression_name (tree, tree *, tree *, tree *);
190 static void qualify_ambiguous_name (tree);
191 static tree resolve_field_access (tree, tree *, tree *);
192 static tree build_newarray_node (tree, tree, int);
193 static tree patch_newarray (tree);
194 static tree resolve_type_during_patch (tree);
195 static tree build_this (int);
196 static tree build_wfl_wrap (tree, int);
197 static tree build_return (int, tree);
198 static tree patch_return (tree);
199 static tree maybe_access_field (tree, tree, tree);
200 static int complete_function_arguments (tree);
201 static int check_for_static_method_reference (tree, tree, tree, tree, tree);
202 static int not_accessible_p (tree, tree, tree, int);
203 static void check_deprecation (tree, tree);
204 static int class_in_current_package (tree);
205 static tree build_if_else_statement (int, tree, tree, tree);
206 static tree patch_if_else_statement (tree);
207 static tree add_stmt_to_block (tree, tree, tree);
208 static tree patch_exit_expr (tree);
209 static tree build_labeled_block (int, tree);
210 static tree finish_labeled_statement (tree, tree);
211 static tree build_bc_statement (int, int, tree);
212 static tree patch_bc_statement (tree);
213 static tree patch_loop_statement (tree);
214 static tree build_new_loop (tree);
215 static tree build_loop_body (int, tree, int);
216 static tree finish_loop_body (int, tree, tree, int);
217 static tree build_debugable_stmt (int, tree);
218 static tree finish_for_loop (int, tree, tree, tree);
219 static tree patch_switch_statement (tree);
220 static tree string_constant_concatenation (tree, tree);
221 static tree build_string_concatenation (tree, tree);
222 static tree patch_string_cst (tree);
223 static tree patch_string (tree);
224 static tree encapsulate_with_try_catch (int, tree, tree, tree);
225 #ifdef USE_MAPPED_LOCATION
226 static tree build_assertion (source_location, tree, tree);
227 #else
228 static tree build_assertion (int, tree, tree);
229 #endif
230 static tree build_try_statement (int, tree, tree);
231 static tree build_try_finally_statement (int, tree, tree);
232 static tree patch_try_statement (tree);
233 static tree patch_synchronized_statement (tree, tree);
234 static tree patch_throw_statement (tree, tree);
235 #ifdef USE_MAPPED_LOCATION
236 static void check_thrown_exceptions (source_location, tree, tree);
237 #else
238 static void check_thrown_exceptions (int, tree, tree);
239 #endif
240 static int check_thrown_exceptions_do (tree);
241 static void purge_unchecked_exceptions (tree);
242 static bool ctors_unchecked_throws_clause_p (tree);
243 static void check_concrete_throws_clauses (tree, tree, tree, tree);
244 static void check_throws_clauses (tree, tree, tree);
245 static void finish_method_declaration (tree);
246 static tree build_super_invocation (tree);
247 static int verify_constructor_circularity (tree, tree);
248 static char *constructor_circularity_msg (tree, tree);
249 static tree build_this_super_qualified_invocation (int, tree, tree, int, int);
250 static const char *get_printable_method_name (tree);
251 static tree patch_conditional_expr (tree, tree, tree);
252 static tree generate_finit (tree);
253 static tree generate_instinit (tree);
254 static tree build_instinit_invocation (tree);
255 static void fix_constructors (tree);
256 static tree build_alias_initializer_parameter_list (int, tree, tree, int *);
257 static tree craft_constructor (tree, tree);
258 static int verify_constructor_super (tree);
259 static tree create_artificial_method (tree, int, tree, tree, tree);
260 static void start_artificial_method_body (tree);
261 static void end_artificial_method_body (tree);
262 static int check_method_redefinition (tree, tree);
263 static int check_method_types_complete (tree);
264 static bool hack_is_accessible_p (tree, tree);
265 static void java_check_regular_methods (tree);
266 static void check_interface_throws_clauses (tree, tree);
267 static void java_check_abstract_methods (tree);
268 static void unreachable_stmt_error (tree);
269 static int not_accessible_field_error (tree, tree);
270 static tree find_expr_with_wfl (tree);
271 static void missing_return_error (tree);
272 static tree build_new_array_init (int, tree);
273 static tree patch_new_array_init (tree, tree);
274 static tree maybe_build_array_element_wfl (tree);
275 static int array_constructor_check_entry (tree, tree);
276 static const char *purify_type_name (const char *);
277 static tree fold_constant_for_init (tree, tree);
278 static jdeplist *reverse_jdep_list (struct parser_ctxt *);
279 static void static_ref_err (tree, tree, tree);
280 static void parser_add_interface (tree, tree, tree);
281 static void add_superinterfaces (tree, tree);
282 static tree jdep_resolve_class (jdep *);
283 static int note_possible_classname (const char *, int);
284 static void java_complete_expand_classes (void);
285 static void java_complete_expand_class (tree);
286 static void java_complete_expand_methods (tree);
287 static tree cut_identifier_in_qualified (tree);
288 static tree java_stabilize_reference (tree);
289 static tree do_unary_numeric_promotion (tree);
290 static char * operator_string (tree);
291 static tree do_merge_string_cste (tree, const char *, int, int);
292 static tree merge_string_cste (tree, tree, int);
293 static tree java_refold (tree);
294 static int java_decl_equiv (tree, tree);
295 static int binop_compound_p (enum tree_code);
296 static tree search_loop (tree);
297 static int labeled_block_contains_loop_p (tree, tree);
298 static int check_abstract_method_definitions (int, tree, tree);
299 static void java_check_abstract_method_definitions (tree);
300 static void java_debug_context_do (int);
301 static void java_parser_context_push_initialized_field (void);
302 static void java_parser_context_pop_initialized_field (void);
303 static tree reorder_static_initialized (tree);
304 static void java_parser_context_suspend (void);
305 static void java_parser_context_resume (void);
306 static int pop_current_osb (struct parser_ctxt *);
308 /* JDK 1.1 work. FIXME */
310 static tree maybe_make_nested_class_name (tree);
311 static int make_nested_class_name (tree);
312 static void link_nested_class_to_enclosing (void);
313 static tree resolve_inner_class (htab_t, tree, tree *, tree *, tree);
314 static tree find_as_inner_class (tree, tree, tree);
315 static tree find_as_inner_class_do (tree, tree);
316 static int check_inner_class_redefinition (tree, tree);
318 static tree build_thisn_assign (void);
319 static tree build_current_thisn (tree);
320 static tree build_access_to_thisn (tree, tree, int);
321 static tree maybe_build_thisn_access_method (tree);
323 static tree build_nested_field_access (tree, tree);
324 static tree build_nested_field_access_methods (tree);
325 static tree build_nested_field_access_method (tree, tree, tree, tree, tree);
326 static tree build_nested_field_access_expr (int, tree, tree, tree, tree);
327 static tree build_outer_method_access_method (tree);
328 static tree build_new_access_id (void);
330 static int nested_field_access_p (tree, tree);
331 static int nested_field_expanded_access_p (tree, tree *, tree *, tree *);
332 static tree nested_field_access_fix (tree, tree, tree);
334 static tree build_incomplete_class_ref (int, tree);
335 static tree patch_incomplete_class_ref (tree);
336 static tree create_anonymous_class (tree);
337 static void patch_anonymous_class (tree, tree, tree);
338 static void add_inner_class_fields (tree, tree);
340 static tree build_dot_class_method (tree);
341 static tree build_dot_class_method_invocation (tree, tree);
342 static void create_new_parser_context (int);
343 static tree maybe_build_class_init_for_field (tree, tree);
345 static int emit_test_initialization (void **, void *);
347 static char *string_convert_int_cst (tree);
349 /* Number of error found so far. */
350 int java_error_count;
351 /* Number of warning found so far. */
352 int java_warning_count;
353 /* Cyclic inheritance report, as it can be set by layout_class */
354 const char *cyclic_inheritance_report;
356 /* The current parser context */
357 struct parser_ctxt *ctxp;
359 /* List of things that were analyzed for which code will be generated */
360 struct parser_ctxt *ctxp_for_generation = NULL;
361 struct parser_ctxt *ctxp_for_generation_last = NULL;
363 /* binop_lookup maps token to tree_code. It is used where binary
364 operations are involved and required by the parser. RDIV_EXPR
365 covers both integral/floating point division. The code is changed
366 once the type of both operator is worked out. */
368 static const enum tree_code binop_lookup[19] =
370 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
371 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
372 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
373 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
374 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
376 #define BINOP_LOOKUP(VALUE) \
377 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
379 /* This is the end index for binary operators that can also be used
380 in compound assignments. */
381 #define BINOP_COMPOUND_CANDIDATES 11
383 /* The "$L" identifier we use to create labels. */
384 static GTY(()) tree label_id;
386 /* The "StringBuffer" identifier used for the String `+' operator. */
387 static GTY(()) tree wfl_string_buffer;
389 /* The "append" identifier used for String `+' operator. */
390 static GTY(()) tree wfl_append;
392 /* The "toString" identifier used for String `+' operator. */
393 static GTY(()) tree wfl_to_string;
395 /* The "java.lang" import qualified name. */
396 static GTY(()) tree java_lang_id;
398 /* The generated `inst$' identifier used for generated enclosing
399 instance/field access functions. */
400 static GTY(()) tree inst_id;
402 /* Context and flag for static blocks */
403 static GTY(()) tree current_static_block;
405 /* The generated `write_parm_value$' identifier. */
406 static GTY(()) tree wpv_id;
408 /* Hold THIS for the scope of the current method decl. */
409 static GTY(()) tree current_this;
411 /* Hold a list of catch clauses list. The first element of this list is
412 the list of the catch clauses of the currently analyzed try block. */
413 static GTY(()) tree currently_caught_type_list;
415 /* This holds a linked list of all the case labels for the current
416 switch statement. It is only used when checking to see if there
417 are duplicate labels. FIXME: probably this should just be attached
418 to the switch itself; then it could be referenced via
419 `ctxp->current_loop'. */
420 static GTY(()) tree case_label_list;
422 /* Anonymous class counter. Will be reset to 1 every time a non
423 anonymous class gets created. */
424 static int anonymous_class_counter = 1;
426 static GTY(()) tree src_parse_roots[1];
428 /* All classes seen from source code */
429 #define gclass_list src_parse_roots[0]
431 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
432 line and point it out. */
433 /* Should point out the one that don't fit. ASCII/unicode, going
434 backward. FIXME */
436 #define check_modifiers(__message, __value, __mask) do { \
437 if ((__value) & ~(__mask)) \
439 size_t i, remainder = (__value) & ~(__mask); \
440 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++) \
441 if ((1 << i) & remainder) \
442 parse_error_context (ctxp->modifier_ctx [i], (__message), \
443 java_accstring_lookup (1 << i)); \
445 } while (0)
449 %union {
450 tree node;
451 int sub_token;
452 struct {
453 int token;
454 #ifdef USE_MAPPED_LOCATION
455 source_location location;
456 #else
457 int location;
458 #endif
459 } operator;
460 int value;
464 #ifdef USE_MAPPED_LOCATION
465 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
466 SET_EXPR_LOCATION(EXPR, (TOKEN).location)
467 #else
468 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
469 (EXPR_WFL_LINECOL (EXPR) = (TOKEN).location)
470 #endif
472 #include "lex.c"
475 %pure_parser
477 /* Things defined here have to match the order of what's in the
478 binop_lookup table. */
480 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
481 %token LS_TK SRS_TK ZRS_TK
482 %token AND_TK XOR_TK OR_TK
483 %token BOOL_AND_TK BOOL_OR_TK
484 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
486 /* This maps to the same binop_lookup entry than the token above */
488 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
489 %token REM_ASSIGN_TK
490 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
491 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
494 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
496 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
497 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
498 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
499 %token PAD_TK ABSTRACT_TK STRICT_TK
500 %token MODIFIER_TK
502 /* Keep those two in order, too */
503 %token DECR_TK INCR_TK
505 /* From now one, things can be in any order */
507 %token DEFAULT_TK IF_TK THROW_TK
508 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
509 %token THROWS_TK BREAK_TK IMPORT_TK
510 %token ELSE_TK INSTANCEOF_TK RETURN_TK
511 %token VOID_TK CATCH_TK INTERFACE_TK
512 %token CASE_TK EXTENDS_TK FINALLY_TK
513 %token SUPER_TK WHILE_TK CLASS_TK
514 %token SWITCH_TK CONST_TK TRY_TK
515 %token FOR_TK NEW_TK CONTINUE_TK
516 %token GOTO_TK PACKAGE_TK THIS_TK
517 %token ASSERT_TK
519 %token BYTE_TK SHORT_TK INT_TK LONG_TK
520 %token CHAR_TK INTEGRAL_TK
522 %token FLOAT_TK DOUBLE_TK FP_TK
524 %token ID_TK
526 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
528 %token ASSIGN_ANY_TK ASSIGN_TK
529 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
531 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
532 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
534 %type <value> modifiers MODIFIER_TK final synchronized
536 %type <node> super ID_TK identifier
537 %type <node> name simple_name qualified_name
538 %type <node> type_declaration compilation_unit
539 field_declaration method_declaration extends_interfaces
540 interfaces interface_type_list
541 import_declarations package_declaration
542 type_declarations interface_body
543 interface_member_declaration constant_declaration
544 interface_member_declarations interface_type
545 abstract_method_declaration
546 %type <node> class_body_declaration class_member_declaration
547 static_initializer constructor_declaration block
548 %type <node> class_body_declarations constructor_header
549 %type <node> class_or_interface_type class_type class_type_list
550 constructor_declarator explicit_constructor_invocation
551 %type <node> dim_expr dim_exprs this_or_super throws
553 %type <node> variable_declarator_id variable_declarator
554 variable_declarators variable_initializer
555 variable_initializers constructor_body
556 array_initializer
558 %type <node> class_body block_end constructor_block_end
559 %type <node> statement statement_without_trailing_substatement
560 labeled_statement if_then_statement label_decl
561 if_then_else_statement while_statement for_statement
562 statement_nsi labeled_statement_nsi do_statement
563 if_then_else_statement_nsi while_statement_nsi
564 for_statement_nsi statement_expression_list for_init
565 for_update statement_expression expression_statement
566 primary_no_new_array expression primary array_type
567 array_creation_initialized array_creation_uninitialized
568 class_instance_creation_expression field_access
569 method_invocation array_access something_dot_new
570 argument_list postfix_expression while_expression
571 post_increment_expression post_decrement_expression
572 unary_expression_not_plus_minus unary_expression
573 pre_increment_expression pre_decrement_expression
574 cast_expression
575 multiplicative_expression additive_expression
576 shift_expression relational_expression
577 equality_expression and_expression
578 exclusive_or_expression inclusive_or_expression
579 conditional_and_expression conditional_or_expression
580 conditional_expression assignment_expression
581 left_hand_side assignment for_header for_begin
582 constant_expression do_statement_begin empty_statement
583 switch_statement synchronized_statement throw_statement
584 try_statement assert_statement
585 switch_expression switch_block
586 catches catch_clause catch_clause_parameter finally
587 anonymous_class_creation trap_overflow_corner_case
588 %type <node> return_statement break_statement continue_statement
590 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
591 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
592 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
593 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
594 %type <operator> ASSIGN_ANY_TK assignment_operator
595 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
596 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
597 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
598 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
599 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
600 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
601 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
602 %type <operator> NEW_TK ASSERT_TK
604 %type <node> method_body
606 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
607 STRING_LIT_TK NULL_TK VOID_TK
609 %type <node> IF_TK WHILE_TK FOR_TK
611 %type <node> formal_parameter_list formal_parameter
612 method_declarator method_header
614 %type <node> primitive_type reference_type type
615 BOOLEAN_TK INTEGRAL_TK FP_TK
617 /* Added or modified JDK 1.1 rule types */
618 %type <node> type_literals
621 /* 19.2 Production from 2.3: The Syntactic Grammar */
622 goal: compilation_unit
626 /* 19.3 Productions from 3: Lexical structure */
627 literal:
628 INT_LIT_TK
629 | FP_LIT_TK
630 | BOOL_LIT_TK
631 | CHAR_LIT_TK
632 | STRING_LIT_TK
633 | NULL_TK
636 /* 19.4 Productions from 4: Types, Values and Variables */
637 type:
638 primitive_type
639 | reference_type
642 primitive_type:
643 INTEGRAL_TK
644 | FP_TK
645 | BOOLEAN_TK
648 reference_type:
649 class_or_interface_type
650 | array_type
653 class_or_interface_type:
654 name
657 class_type:
658 class_or_interface_type /* Default rule */
661 interface_type:
662 class_or_interface_type
665 array_type:
666 primitive_type dims
668 int osb = pop_current_osb (ctxp);
669 tree t = build_java_array_type (($1), -1);
670 while (--osb)
671 t = build_unresolved_array_type (t);
672 $$ = t;
674 | name dims
676 int osb = pop_current_osb (ctxp);
677 tree t = $1;
678 while (osb--)
679 t = build_unresolved_array_type (t);
680 $$ = t;
684 /* 19.5 Productions from 6: Names */
685 name:
686 simple_name /* Default rule */
687 | qualified_name /* Default rule */
690 simple_name:
691 identifier /* Default rule */
694 qualified_name:
695 name DOT_TK identifier
696 { $$ = make_qualified_name ($1, $3, $2.location); }
699 identifier:
700 ID_TK
703 /* 19.6: Production from 7: Packages */
704 compilation_unit:
705 {$$ = NULL;}
706 | package_declaration
707 | import_declarations
708 | type_declarations
709 | package_declaration import_declarations
710 | package_declaration type_declarations
711 | import_declarations type_declarations
712 | package_declaration import_declarations type_declarations
715 import_declarations:
716 import_declaration
718 $$ = NULL;
720 | import_declarations import_declaration
722 $$ = NULL;
726 type_declarations:
727 type_declaration
728 | type_declarations type_declaration
731 package_declaration:
732 PACKAGE_TK name SC_TK
734 ctxp->package = EXPR_WFL_NODE ($2);
736 | PACKAGE_TK error
737 {yyerror ("Missing name"); RECOVER;}
738 | PACKAGE_TK name error
739 {yyerror ("';' expected"); RECOVER;}
742 import_declaration:
743 single_type_import_declaration
744 | type_import_on_demand_declaration
747 single_type_import_declaration:
748 IMPORT_TK name SC_TK
750 tree name = EXPR_WFL_NODE ($2), last_name;
751 int i = IDENTIFIER_LENGTH (name)-1;
752 const char *last = &IDENTIFIER_POINTER (name)[i];
753 while (last != IDENTIFIER_POINTER (name))
755 if (last [0] == '.')
756 break;
757 last--;
759 last_name = get_identifier (++last);
760 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
762 tree err = find_name_in_single_imports (last_name);
763 if (err && err != name)
764 parse_error_context
765 ($2, "Ambiguous class: %qs and %qs",
766 IDENTIFIER_POINTER (name),
767 IDENTIFIER_POINTER (err));
768 else
769 REGISTER_IMPORT ($2, last_name);
771 else
772 REGISTER_IMPORT ($2, last_name);
774 | IMPORT_TK error
775 {yyerror ("Missing name"); RECOVER;}
776 | IMPORT_TK name error
777 {yyerror ("';' expected"); RECOVER;}
780 type_import_on_demand_declaration:
781 IMPORT_TK name DOT_TK MULT_TK SC_TK
783 tree name = EXPR_WFL_NODE ($2);
784 tree it;
785 /* Search for duplicates. */
786 for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
787 if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
788 break;
789 /* Don't import the same thing more than once, just ignore
790 duplicates (7.5.2) */
791 if (! it)
793 read_import_dir ($2);
794 ctxp->import_demand_list =
795 chainon (ctxp->import_demand_list,
796 build_tree_list ($2, NULL_TREE));
799 | IMPORT_TK name DOT_TK error
800 {yyerror ("'*' expected"); RECOVER;}
801 | IMPORT_TK name DOT_TK MULT_TK error
802 {yyerror ("';' expected"); RECOVER;}
805 type_declaration:
806 class_declaration
807 { end_class_declaration (0); }
808 | interface_declaration
809 { end_class_declaration (0); }
810 | empty_statement
811 | error
813 YYERROR_NOW;
814 yyerror ("Class or interface declaration expected");
818 /* 19.7 Shortened from the original:
819 modifiers: modifier | modifiers modifier
820 modifier: any of public... */
821 modifiers:
822 MODIFIER_TK
824 $$ = (1 << $1);
826 | modifiers MODIFIER_TK
828 int acc = (1 << $2);
829 if ($$ & acc)
830 parse_error_context
831 (ctxp->modifier_ctx [$2], "Modifier %qs declared twice",
832 java_accstring_lookup (acc));
833 else
835 $$ |= acc;
840 /* 19.8.1 Production from $8.1: Class Declaration */
841 class_declaration:
842 modifiers CLASS_TK identifier super interfaces
843 { create_class ($1, $3, $4, $5); }
844 class_body
846 | CLASS_TK identifier super interfaces
847 { create_class (0, $2, $3, $4); }
848 class_body
850 | modifiers CLASS_TK error
851 { yyerror ("Missing class name"); RECOVER; }
852 | CLASS_TK error
853 { yyerror ("Missing class name"); RECOVER; }
854 | CLASS_TK identifier error
856 if (!ctxp->class_err) yyerror ("'{' expected");
857 DRECOVER(class1);
859 | modifiers CLASS_TK identifier error
860 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
863 super:
864 { $$ = NULL; }
865 | EXTENDS_TK class_type
866 { $$ = $2; }
867 | EXTENDS_TK class_type error
868 {yyerror ("'{' expected"); ctxp->class_err=1;}
869 | EXTENDS_TK error
870 {yyerror ("Missing super class name"); ctxp->class_err=1;}
873 interfaces:
874 { $$ = NULL_TREE; }
875 | IMPLEMENTS_TK interface_type_list
876 { $$ = $2; }
877 | IMPLEMENTS_TK error
879 ctxp->class_err=1;
880 yyerror ("Missing interface name");
884 interface_type_list:
885 interface_type
887 ctxp->interface_number = 1;
888 $$ = build_tree_list ($1, NULL_TREE);
890 | interface_type_list C_TK interface_type
892 ctxp->interface_number++;
893 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
895 | interface_type_list C_TK error
896 {yyerror ("Missing interface name"); RECOVER;}
899 class_body:
900 OCB_TK CCB_TK
902 $$ = GET_CPC ();
904 | OCB_TK class_body_declarations CCB_TK
906 $$ = GET_CPC ();
910 class_body_declarations:
911 class_body_declaration
912 | class_body_declarations class_body_declaration
915 class_body_declaration:
916 class_member_declaration
917 | static_initializer
918 | constructor_declaration
919 | block /* Added, JDK1.1, instance initializer */
921 if (!IS_EMPTY_STMT ($1))
923 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
924 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
929 class_member_declaration:
930 field_declaration
931 | method_declaration
932 | class_declaration /* Added, JDK1.1 inner classes */
933 { end_class_declaration (1); }
934 | interface_declaration /* Added, JDK1.1 inner interfaces */
935 { end_class_declaration (1); }
936 | empty_statement
939 /* 19.8.2 Productions from 8.3: Field Declarations */
940 field_declaration:
941 type variable_declarators SC_TK
942 { register_fields (0, $1, $2); }
943 | modifiers type variable_declarators SC_TK
945 check_modifiers
946 ("Illegal modifier %qs for field declaration",
947 $1, FIELD_MODIFIERS);
948 check_modifiers_consistency ($1);
949 register_fields ($1, $2, $3);
953 variable_declarators:
954 /* Should we use build_decl_list () instead ? FIXME */
955 variable_declarator /* Default rule */
956 | variable_declarators C_TK variable_declarator
957 { $$ = chainon ($1, $3); }
958 | variable_declarators C_TK error
959 {yyerror ("Missing term"); RECOVER;}
962 variable_declarator:
963 variable_declarator_id
964 { $$ = build_tree_list ($1, NULL_TREE); }
965 | variable_declarator_id ASSIGN_TK variable_initializer
967 if (java_error_count)
968 $3 = NULL_TREE;
969 $$ = build_tree_list
970 ($1, build_assignment ($2.token, $2.location, $1, $3));
972 | variable_declarator_id ASSIGN_TK error
974 yyerror ("Missing variable initializer");
975 $$ = build_tree_list ($1, NULL_TREE);
976 RECOVER;
978 | variable_declarator_id ASSIGN_TK variable_initializer error
980 yyerror ("';' expected");
981 $$ = build_tree_list ($1, NULL_TREE);
982 RECOVER;
986 variable_declarator_id:
987 identifier
988 | variable_declarator_id OSB_TK CSB_TK
989 { $$ = build_unresolved_array_type ($1); }
990 | identifier error
991 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
992 | variable_declarator_id OSB_TK error
994 yyerror ("']' expected");
995 DRECOVER(vdi);
997 | variable_declarator_id CSB_TK error
998 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1001 variable_initializer:
1002 expression
1003 | array_initializer
1006 /* 19.8.3 Productions from 8.4: Method Declarations */
1007 method_declaration:
1008 method_header
1010 current_function_decl = $1;
1011 if (current_function_decl
1012 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1013 source_start_java_method (current_function_decl);
1014 else
1015 current_function_decl = NULL_TREE;
1017 method_body
1018 { finish_method_declaration ($3); }
1019 | method_header error
1020 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1023 method_header:
1024 type method_declarator throws
1025 { $$ = method_header (0, $1, $2, $3); }
1026 | VOID_TK method_declarator throws
1027 { $$ = method_header (0, void_type_node, $2, $3); }
1028 | modifiers type method_declarator throws
1029 { $$ = method_header ($1, $2, $3, $4); }
1030 | modifiers VOID_TK method_declarator throws
1031 { $$ = method_header ($1, void_type_node, $3, $4); }
1032 | type error
1034 yyerror ("Invalid method declaration, method name required");
1035 RECOVER;
1037 | modifiers type error
1039 yyerror ("Identifier expected");
1040 RECOVER;
1042 | VOID_TK error
1044 yyerror ("Identifier expected");
1045 RECOVER;
1047 | modifiers VOID_TK error
1049 yyerror ("Identifier expected");
1050 RECOVER;
1052 | modifiers error
1054 yyerror ("Invalid method declaration, return type required");
1055 RECOVER;
1059 method_declarator:
1060 identifier OP_TK CP_TK
1062 ctxp->formal_parameter_number = 0;
1063 $$ = method_declarator ($1, NULL_TREE);
1065 | identifier OP_TK formal_parameter_list CP_TK
1066 { $$ = method_declarator ($1, $3); }
1067 | method_declarator OSB_TK CSB_TK
1069 SET_EXPR_LOCATION_FROM_TOKEN (wfl_operator, $2);
1070 TREE_PURPOSE ($1) =
1071 build_unresolved_array_type (TREE_PURPOSE ($1));
1072 parse_warning_context
1073 (wfl_operator,
1074 "Discouraged form of returned type specification");
1076 | identifier OP_TK error
1077 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1078 | method_declarator OSB_TK error
1079 {yyerror ("']' expected"); RECOVER;}
1082 formal_parameter_list:
1083 formal_parameter
1085 ctxp->formal_parameter_number = 1;
1087 | formal_parameter_list C_TK formal_parameter
1089 ctxp->formal_parameter_number += 1;
1090 $$ = chainon ($1, $3);
1092 | formal_parameter_list C_TK error
1093 { yyerror ("Missing formal parameter term"); RECOVER; }
1096 formal_parameter:
1097 type variable_declarator_id
1099 $$ = build_tree_list ($2, $1);
1101 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1103 $$ = build_tree_list ($3, $2);
1104 ARG_FINAL_P ($$) = 1;
1106 | type error
1108 yyerror ("Missing identifier"); RECOVER;
1109 $$ = NULL_TREE;
1111 | final type error
1113 yyerror ("Missing identifier"); RECOVER;
1114 $$ = NULL_TREE;
1118 final:
1119 modifiers
1121 check_modifiers ("Illegal modifier %qs. Only %<final%> was expected here",
1122 $1, ACC_FINAL);
1123 if ($1 != ACC_FINAL)
1124 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1128 throws:
1129 { $$ = NULL_TREE; }
1130 | THROWS_TK class_type_list
1131 { $$ = $2; }
1132 | THROWS_TK error
1133 {yyerror ("Missing class type term"); RECOVER;}
1136 class_type_list:
1137 class_type
1138 { $$ = build_tree_list ($1, $1); }
1139 | class_type_list C_TK class_type
1140 { $$ = tree_cons ($3, $3, $1); }
1141 | class_type_list C_TK error
1142 {yyerror ("Missing class type term"); RECOVER;}
1145 method_body:
1146 block
1147 | SC_TK { $$ = NULL_TREE; }
1150 /* 19.8.4 Productions from 8.5: Static Initializers */
1151 static_initializer:
1152 static block
1154 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1155 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1156 current_static_block = NULL_TREE;
1160 static: /* Test lval.sub_token here */
1161 modifiers
1163 check_modifiers ("Illegal modifier %qs for static initializer", $1, ACC_STATIC);
1164 /* Can't have a static initializer in an innerclass */
1165 if ($1 | ACC_STATIC &&
1166 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1167 parse_error_context
1168 (MODIFIER_WFL (STATIC_TK),
1169 "Can't define static initializer in class %qs. Static initializer can only be defined in top-level classes",
1170 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1171 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1175 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1176 constructor_declaration:
1177 constructor_header
1179 current_function_decl = $1;
1180 source_start_java_method (current_function_decl);
1182 constructor_body
1183 { finish_method_declaration ($3); }
1186 constructor_header:
1187 constructor_declarator throws
1188 { $$ = method_header (0, NULL_TREE, $1, $2); }
1189 | modifiers constructor_declarator throws
1190 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1193 constructor_declarator:
1194 simple_name OP_TK CP_TK
1196 ctxp->formal_parameter_number = 0;
1197 $$ = method_declarator ($1, NULL_TREE);
1199 | simple_name OP_TK formal_parameter_list CP_TK
1200 { $$ = method_declarator ($1, $3); }
1203 constructor_body:
1204 /* Unlike regular method, we always need a complete (empty)
1205 body so we can safely perform all the required code
1206 addition (super invocation and field initialization) */
1207 block_begin constructor_block_end
1209 BLOCK_EXPR_BODY ($2) = build_java_empty_stmt ();
1210 $$ = $2;
1212 | block_begin explicit_constructor_invocation constructor_block_end
1213 { $$ = $3; }
1214 | block_begin block_statements constructor_block_end
1215 { $$ = $3; }
1216 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1217 { $$ = $4; }
1220 constructor_block_end:
1221 block_end
1224 /* Error recovery for that rule moved down expression_statement: rule. */
1225 explicit_constructor_invocation:
1226 this_or_super OP_TK CP_TK SC_TK
1228 $$ = build_method_invocation ($1, NULL_TREE);
1229 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1230 $$ = java_method_add_stmt (current_function_decl, $$);
1232 | this_or_super OP_TK argument_list CP_TK SC_TK
1234 $$ = build_method_invocation ($1, $3);
1235 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1236 $$ = java_method_add_stmt (current_function_decl, $$);
1238 /* Added, JDK1.1 inner classes. Modified because the rule
1239 'primary' couldn't work. */
1240 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1241 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1242 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1243 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1246 this_or_super: /* Added, simplifies error diagnostics */
1247 THIS_TK
1249 tree wfl = build_wfl_node (this_identifier_node);
1250 SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1251 $$ = wfl;
1253 | SUPER_TK
1255 tree wfl = build_wfl_node (super_identifier_node);
1256 SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1257 $$ = wfl;
1261 /* 19.9 Productions from 9: Interfaces */
1262 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1263 interface_declaration:
1264 INTERFACE_TK identifier
1265 { create_interface (0, $2, NULL_TREE); }
1266 interface_body
1267 { ; }
1268 | modifiers INTERFACE_TK identifier
1269 { create_interface ($1, $3, NULL_TREE); }
1270 interface_body
1271 { ; }
1272 | INTERFACE_TK identifier extends_interfaces
1273 { create_interface (0, $2, $3); }
1274 interface_body
1275 { ; }
1276 | modifiers INTERFACE_TK identifier extends_interfaces
1277 { create_interface ($1, $3, $4); }
1278 interface_body
1279 { ; }
1280 | INTERFACE_TK identifier error
1281 { yyerror ("'{' expected"); RECOVER; }
1282 | modifiers INTERFACE_TK identifier error
1283 { yyerror ("'{' expected"); RECOVER; }
1286 extends_interfaces:
1287 EXTENDS_TK interface_type
1289 ctxp->interface_number = 1;
1290 $$ = build_tree_list ($2, NULL_TREE);
1292 | extends_interfaces C_TK interface_type
1294 ctxp->interface_number++;
1295 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1297 | EXTENDS_TK error
1298 {yyerror ("Invalid interface type"); RECOVER;}
1299 | extends_interfaces C_TK error
1300 {yyerror ("Missing term"); RECOVER;}
1303 interface_body:
1304 OCB_TK CCB_TK
1305 { $$ = NULL_TREE; }
1306 | OCB_TK interface_member_declarations CCB_TK
1307 { $$ = NULL_TREE; }
1310 interface_member_declarations:
1311 interface_member_declaration
1312 | interface_member_declarations interface_member_declaration
1315 interface_member_declaration:
1316 constant_declaration
1317 | abstract_method_declaration
1318 | class_declaration /* Added, JDK1.1 inner classes */
1319 { end_class_declaration (1); }
1320 | interface_declaration /* Added, JDK1.1 inner interfaces */
1321 { end_class_declaration (1); }
1324 constant_declaration:
1325 field_declaration
1328 abstract_method_declaration:
1329 method_header SC_TK
1331 check_abstract_method_header ($1);
1332 current_function_decl = NULL_TREE; /* FIXME ? */
1334 | method_header error
1335 {yyerror ("';' expected"); RECOVER;}
1338 /* 19.10 Productions from 10: Arrays */
1339 array_initializer:
1340 OCB_TK CCB_TK
1341 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1342 | OCB_TK C_TK CCB_TK
1343 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1344 | OCB_TK variable_initializers CCB_TK
1345 { $$ = build_new_array_init ($1.location, $2); }
1346 | OCB_TK variable_initializers C_TK CCB_TK
1347 { $$ = build_new_array_init ($1.location, $2); }
1350 variable_initializers:
1351 variable_initializer
1353 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1354 $1, NULL_TREE);
1356 | variable_initializers C_TK variable_initializer
1358 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1360 | variable_initializers C_TK error
1361 {yyerror ("Missing term"); RECOVER;}
1364 /* 19.11 Production from 14: Blocks and Statements */
1365 block:
1366 block_begin block_end
1367 { $$ = $2; }
1368 | block_begin block_statements block_end
1369 { $$ = $3; }
1372 block_begin:
1373 OCB_TK
1374 { enter_block (); }
1377 block_end:
1378 CCB_TK
1380 maybe_absorb_scoping_blocks ();
1381 $$ = exit_block ();
1382 if (!BLOCK_SUBBLOCKS ($$))
1383 BLOCK_SUBBLOCKS ($$) = build_java_empty_stmt ();
1387 block_statements:
1388 block_statement
1389 | block_statements block_statement
1392 block_statement:
1393 local_variable_declaration_statement
1394 | statement
1395 { java_method_add_stmt (current_function_decl, $1); }
1396 | class_declaration /* Added, JDK1.1 local classes */
1398 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1399 end_class_declaration (1);
1403 local_variable_declaration_statement:
1404 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1407 local_variable_declaration:
1408 type variable_declarators
1409 { declare_local_variables (0, $1, $2); }
1410 | final type variable_declarators /* Added, JDK1.1 final locals */
1411 { declare_local_variables ($1, $2, $3); }
1414 statement:
1415 statement_without_trailing_substatement
1416 | labeled_statement
1417 | if_then_statement
1418 | if_then_else_statement
1419 | while_statement
1420 | for_statement
1421 { $$ = exit_block (); }
1424 statement_nsi:
1425 statement_without_trailing_substatement
1426 | labeled_statement_nsi
1427 | if_then_else_statement_nsi
1428 | while_statement_nsi
1429 | for_statement_nsi
1430 { $$ = exit_block (); }
1433 statement_without_trailing_substatement:
1434 block
1435 | empty_statement
1436 | expression_statement
1437 | switch_statement
1438 | do_statement
1439 | break_statement
1440 | continue_statement
1441 | return_statement
1442 | synchronized_statement
1443 | throw_statement
1444 | try_statement
1445 | assert_statement
1448 empty_statement:
1449 SC_TK
1451 if (flag_extraneous_semicolon
1452 && ! current_static_block
1453 && (! current_function_decl ||
1454 /* Verify we're not in a inner class declaration */
1455 (GET_CPC () != TYPE_NAME
1456 (DECL_CONTEXT (current_function_decl)))))
1459 #ifdef USE_MAPPED_LOCATION
1460 SET_EXPR_LOCATION (wfl_operator, input_location);
1461 #else
1462 EXPR_WFL_SET_LINECOL (wfl_operator, input_line, -1);
1463 #endif
1464 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1466 $$ = build_java_empty_stmt ();
1470 label_decl:
1471 identifier REL_CL_TK
1473 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1474 EXPR_WFL_NODE ($1));
1475 pushlevel (2);
1476 push_labeled_block ($$);
1477 PUSH_LABELED_BLOCK ($$);
1481 labeled_statement:
1482 label_decl statement
1483 { $$ = finish_labeled_statement ($1, $2); }
1484 | identifier error
1485 {yyerror ("':' expected"); RECOVER;}
1488 labeled_statement_nsi:
1489 label_decl statement_nsi
1490 { $$ = finish_labeled_statement ($1, $2); }
1493 /* We concentrate here a bunch of error handling rules that we couldn't write
1494 earlier, because expression_statement catches a missing ';'. */
1495 expression_statement:
1496 statement_expression SC_TK
1498 /* We have a statement. Generate a WFL around it so
1499 we can debug it */
1500 #ifdef USE_MAPPED_LOCATION
1501 $$ = expr_add_location ($1, input_location, 1);
1502 #else
1503 $$ = build_expr_wfl ($1, input_filename, input_line, 0);
1504 JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1505 #endif
1506 /* We know we have a statement, so set the debug
1507 info to be eventually generate here. */
1509 | error SC_TK
1511 YYNOT_TWICE yyerror ("Invalid expression statement");
1512 DRECOVER (expr_stmt);
1514 | error OCB_TK
1516 YYNOT_TWICE yyerror ("Invalid expression statement");
1517 DRECOVER (expr_stmt);
1519 | error CCB_TK
1521 YYNOT_TWICE yyerror ("Invalid expression statement");
1522 DRECOVER (expr_stmt);
1524 | this_or_super OP_TK error
1525 {yyerror ("')' expected"); RECOVER;}
1526 | this_or_super OP_TK CP_TK error
1528 parse_ctor_invocation_error ();
1529 RECOVER;
1531 | this_or_super OP_TK argument_list error
1532 {yyerror ("')' expected"); RECOVER;}
1533 | this_or_super OP_TK argument_list CP_TK error
1535 parse_ctor_invocation_error ();
1536 RECOVER;
1538 | name DOT_TK SUPER_TK error
1539 {yyerror ("'(' expected"); RECOVER;}
1540 | name DOT_TK SUPER_TK OP_TK error
1541 {yyerror ("')' expected"); RECOVER;}
1542 | name DOT_TK SUPER_TK OP_TK argument_list error
1543 {yyerror ("')' expected"); RECOVER;}
1544 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1545 {yyerror ("';' expected"); RECOVER;}
1546 | name DOT_TK SUPER_TK OP_TK CP_TK error
1547 {yyerror ("';' expected"); RECOVER;}
1550 statement_expression:
1551 assignment
1552 | pre_increment_expression
1553 | pre_decrement_expression
1554 | post_increment_expression
1555 | post_decrement_expression
1556 | method_invocation
1557 | class_instance_creation_expression
1560 if_then_statement:
1561 IF_TK OP_TK expression CP_TK statement
1563 $$ = build_if_else_statement ($2.location, $3,
1564 $5, NULL_TREE);
1566 | IF_TK error
1567 {yyerror ("'(' expected"); RECOVER;}
1568 | IF_TK OP_TK error
1569 {yyerror ("Missing term"); RECOVER;}
1570 | IF_TK OP_TK expression error
1571 {yyerror ("')' expected"); RECOVER;}
1574 if_then_else_statement:
1575 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1576 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1579 if_then_else_statement_nsi:
1580 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1581 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1584 switch_statement:
1585 switch_expression
1587 enter_block ();
1589 switch_block
1591 /* Make into "proper list" of COMPOUND_EXPRs.
1592 I.e. make the last statement also have its own
1593 COMPOUND_EXPR. */
1594 maybe_absorb_scoping_blocks ();
1595 TREE_OPERAND ($1, 1) = exit_block ();
1596 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1600 switch_expression:
1601 SWITCH_TK OP_TK expression CP_TK
1603 $$ = build3 (SWITCH_EXPR, NULL_TREE, $3,
1604 NULL_TREE, NULL_TREE);
1605 SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
1607 | SWITCH_TK error
1608 {yyerror ("'(' expected"); RECOVER;}
1609 | SWITCH_TK OP_TK error
1610 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1611 | SWITCH_TK OP_TK expression CP_TK error
1612 {yyerror ("'{' expected"); RECOVER;}
1615 /* Default assignment is there to avoid type node on switch_block
1616 node. */
1618 switch_block:
1619 OCB_TK CCB_TK
1620 { $$ = NULL_TREE; }
1621 | OCB_TK switch_labels CCB_TK
1622 { $$ = NULL_TREE; }
1623 | OCB_TK switch_block_statement_groups CCB_TK
1624 { $$ = NULL_TREE; }
1625 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1626 { $$ = NULL_TREE; }
1629 switch_block_statement_groups:
1630 switch_block_statement_group
1631 | switch_block_statement_groups switch_block_statement_group
1634 switch_block_statement_group:
1635 switch_labels block_statements
1638 switch_labels:
1639 switch_label
1640 | switch_labels switch_label
1643 switch_label:
1644 CASE_TK constant_expression REL_CL_TK
1646 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1647 SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1648 java_method_add_stmt (current_function_decl, lab);
1650 | DEFAULT_TK REL_CL_TK
1652 tree lab = make_node (DEFAULT_EXPR);
1653 SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1654 java_method_add_stmt (current_function_decl, lab);
1656 | CASE_TK error
1657 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1658 | CASE_TK constant_expression error
1659 {yyerror ("':' expected"); RECOVER;}
1660 | DEFAULT_TK error
1661 {yyerror ("':' expected"); RECOVER;}
1664 while_expression:
1665 WHILE_TK OP_TK expression CP_TK
1667 tree body = build_loop_body ($2.location, $3, 0);
1668 $$ = build_new_loop (body);
1672 while_statement:
1673 while_expression statement
1674 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1675 | WHILE_TK error
1676 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1677 | WHILE_TK OP_TK error
1678 {yyerror ("Missing term and ')' expected"); RECOVER;}
1679 | WHILE_TK OP_TK expression error
1680 {yyerror ("')' expected"); RECOVER;}
1683 while_statement_nsi:
1684 while_expression statement_nsi
1685 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1688 do_statement_begin:
1689 DO_TK
1691 tree body = build_loop_body (0, NULL_TREE, 1);
1692 $$ = build_new_loop (body);
1694 /* Need error handing here. FIXME */
1697 do_statement:
1698 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1699 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1702 for_statement:
1703 for_begin SC_TK expression SC_TK for_update CP_TK statement
1705 if (CONSTANT_CLASS_P ($3))
1706 $3 = build_wfl_node ($3);
1707 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1709 | for_begin SC_TK SC_TK for_update CP_TK statement
1711 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1712 /* We have not condition, so we get rid of the EXIT_EXPR */
1713 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1714 build_java_empty_stmt ();
1716 | for_begin SC_TK error
1717 {yyerror ("Invalid control expression"); RECOVER;}
1718 | for_begin SC_TK expression SC_TK error
1719 {yyerror ("Invalid update expression"); RECOVER;}
1720 | for_begin SC_TK SC_TK error
1721 {yyerror ("Invalid update expression"); RECOVER;}
1724 for_statement_nsi:
1725 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1726 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1727 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
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 ();
1736 for_header:
1737 FOR_TK OP_TK
1739 /* This scope defined for local variable that may be
1740 defined within the scope of the for loop */
1741 enter_block ();
1743 | FOR_TK error
1744 {yyerror ("'(' expected"); DRECOVER(for_1);}
1745 | FOR_TK OP_TK error
1746 {yyerror ("Invalid init statement"); RECOVER;}
1749 for_begin:
1750 for_header for_init
1752 /* We now declare the loop body. The loop is
1753 declared as a for loop. */
1754 tree body = build_loop_body (0, NULL_TREE, 0);
1755 $$ = build_new_loop (body);
1756 FOR_LOOP_P ($$) = 1;
1757 /* The loop is added to the current block the for
1758 statement is defined within */
1759 java_method_add_stmt (current_function_decl, $$);
1762 for_init: /* Can be empty */
1763 { $$ = build_java_empty_stmt (); }
1764 | statement_expression_list
1766 /* Init statement recorded within the previously
1767 defined block scope */
1768 $$ = java_method_add_stmt (current_function_decl, $1);
1770 | local_variable_declaration
1772 /* Local variable are recorded within the previously
1773 defined block scope */
1774 $$ = NULL_TREE;
1776 | statement_expression_list error
1777 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1780 for_update: /* Can be empty */
1781 {$$ = build_java_empty_stmt ();}
1782 | statement_expression_list
1783 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1786 statement_expression_list:
1787 statement_expression
1788 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1789 | statement_expression_list C_TK statement_expression
1790 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1791 | statement_expression_list C_TK error
1792 {yyerror ("Missing term"); RECOVER;}
1795 break_statement:
1796 BREAK_TK SC_TK
1797 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1798 | BREAK_TK identifier SC_TK
1799 { $$ = build_bc_statement ($1.location, 1, $2); }
1800 | BREAK_TK error
1801 {yyerror ("Missing term"); RECOVER;}
1802 | BREAK_TK identifier error
1803 {yyerror ("';' expected"); RECOVER;}
1806 continue_statement:
1807 CONTINUE_TK SC_TK
1808 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1809 | CONTINUE_TK identifier SC_TK
1810 { $$ = build_bc_statement ($1.location, 0, $2); }
1811 | CONTINUE_TK error
1812 {yyerror ("Missing term"); RECOVER;}
1813 | CONTINUE_TK identifier error
1814 {yyerror ("';' expected"); RECOVER;}
1817 return_statement:
1818 RETURN_TK SC_TK
1819 { $$ = build_return ($1.location, NULL_TREE); }
1820 | RETURN_TK expression SC_TK
1821 { $$ = build_return ($1.location, $2); }
1822 | RETURN_TK error
1823 {yyerror ("Missing term"); RECOVER;}
1824 | RETURN_TK expression error
1825 {yyerror ("';' expected"); RECOVER;}
1828 throw_statement:
1829 THROW_TK expression SC_TK
1831 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1832 SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1834 | THROW_TK error
1835 {yyerror ("Missing term"); RECOVER;}
1836 | THROW_TK expression error
1837 {yyerror ("';' expected"); RECOVER;}
1840 assert_statement:
1841 ASSERT_TK expression REL_CL_TK expression SC_TK
1843 $$ = build_assertion ($1.location, $2, $4);
1845 | ASSERT_TK expression SC_TK
1847 $$ = build_assertion ($1.location, $2, NULL_TREE);
1849 | ASSERT_TK error
1850 {yyerror ("Missing term"); RECOVER;}
1851 | ASSERT_TK expression error
1852 {yyerror ("';' expected"); RECOVER;}
1855 synchronized_statement:
1856 synchronized OP_TK expression CP_TK block
1858 $$ = build2 (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1859 EXPR_WFL_LINECOL ($$) =
1860 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1862 | synchronized OP_TK expression CP_TK error
1863 {yyerror ("'{' expected"); RECOVER;}
1864 | synchronized error
1865 {yyerror ("'(' expected"); RECOVER;}
1866 | synchronized OP_TK error CP_TK
1867 {yyerror ("Missing term"); RECOVER;}
1868 | synchronized OP_TK error
1869 {yyerror ("Missing term"); RECOVER;}
1872 synchronized:
1873 modifiers
1875 check_modifiers (
1876 "Illegal modifier %qs. Only %<synchronized%> was expected here",
1877 $1, ACC_SYNCHRONIZED);
1878 if ($1 != ACC_SYNCHRONIZED)
1879 MODIFIER_WFL (SYNCHRONIZED_TK) =
1880 build_wfl_node (NULL_TREE);
1884 try_statement:
1885 TRY_TK block catches
1886 { $$ = build_try_statement ($1.location, $2, $3); }
1887 | TRY_TK block finally
1888 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1889 | TRY_TK block catches finally
1890 { $$ = build_try_finally_statement
1891 ($1.location, build_try_statement ($1.location,
1892 $2, $3), $4);
1894 | TRY_TK error
1895 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1898 catches:
1899 catch_clause
1900 | catches catch_clause
1902 TREE_CHAIN ($2) = $1;
1903 $$ = $2;
1907 catch_clause:
1908 catch_clause_parameter block
1910 java_method_add_stmt (current_function_decl, $2);
1911 exit_block ();
1912 $$ = $1;
1916 catch_clause_parameter:
1917 CATCH_TK OP_TK formal_parameter CP_TK
1919 /* We add a block to define a scope for
1920 formal_parameter (CCBP). The formal parameter is
1921 declared initialized by the appropriate function
1922 call */
1923 tree ccpb;
1924 tree init;
1925 if ($3)
1927 ccpb = enter_block ();
1928 init = build_assignment
1929 (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1930 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1931 declare_local_variables (0, TREE_VALUE ($3),
1932 build_tree_list
1933 (TREE_PURPOSE ($3), init));
1934 $$ = build1 (JAVA_CATCH_EXPR, NULL_TREE, ccpb);
1935 SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1937 else
1939 $$ = error_mark_node;
1942 | CATCH_TK error
1943 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1944 | CATCH_TK OP_TK error
1946 yyerror ("Missing term or ')' expected");
1947 RECOVER; $$ = NULL_TREE;
1949 | CATCH_TK OP_TK error CP_TK /* That's for () */
1950 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1953 finally:
1954 FINALLY_TK block
1955 { $$ = $2; }
1956 | FINALLY_TK error
1957 {yyerror ("'{' expected"); RECOVER; }
1960 /* 19.12 Production from 15: Expressions */
1961 primary:
1962 primary_no_new_array
1963 | array_creation_uninitialized
1964 | array_creation_initialized
1967 primary_no_new_array:
1968 literal
1969 | THIS_TK
1970 { $$ = build_this ($1.location); }
1971 | OP_TK expression CP_TK
1972 {$$ = $2;}
1973 | class_instance_creation_expression
1974 | field_access
1975 | method_invocation
1976 | array_access
1977 | type_literals
1978 /* Added, JDK1.1 inner classes. Documentation is wrong
1979 referring to a 'ClassName' (class_name) rule that doesn't
1980 exist. Used name: instead. */
1981 | name DOT_TK THIS_TK
1983 tree wfl = build_wfl_node (this_identifier_node);
1984 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1986 | OP_TK expression error
1987 {yyerror ("')' expected"); RECOVER;}
1988 | name DOT_TK error
1989 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1990 | primitive_type DOT_TK error
1991 {yyerror ("'class' expected" ); RECOVER;}
1992 | VOID_TK DOT_TK error
1993 {yyerror ("'class' expected" ); RECOVER;}
1996 type_literals:
1997 name DOT_TK CLASS_TK
1998 { $$ = build_incomplete_class_ref ($2.location, $1); }
1999 | array_type DOT_TK CLASS_TK
2000 { $$ = build_incomplete_class_ref ($2.location, $1); }
2001 | primitive_type DOT_TK CLASS_TK
2002 { $$ = build_incomplete_class_ref ($2.location, $1); }
2003 | VOID_TK DOT_TK CLASS_TK
2005 $$ = build_incomplete_class_ref ($2.location,
2006 void_type_node);
2010 class_instance_creation_expression:
2011 NEW_TK class_type OP_TK argument_list CP_TK
2012 { $$ = build_new_invocation ($2, $4); }
2013 | NEW_TK class_type OP_TK CP_TK
2014 { $$ = build_new_invocation ($2, NULL_TREE); }
2015 | anonymous_class_creation
2016 /* Added, JDK1.1 inner classes, modified to use name or
2017 primary instead of primary solely which couldn't work in
2018 all situations. */
2019 | something_dot_new identifier OP_TK CP_TK
2021 tree ctor = build_new_invocation ($2, NULL_TREE);
2022 $$ = make_qualified_primary ($1, ctor,
2023 EXPR_WFL_LINECOL ($1));
2025 | something_dot_new identifier OP_TK CP_TK class_body
2026 | something_dot_new identifier OP_TK argument_list CP_TK
2028 tree ctor = build_new_invocation ($2, $4);
2029 $$ = make_qualified_primary ($1, ctor,
2030 EXPR_WFL_LINECOL ($1));
2032 | something_dot_new identifier OP_TK argument_list CP_TK class_body
2033 | NEW_TK error SC_TK
2034 {$$ = NULL_TREE; yyerror ("'(' expected"); DRECOVER(new_1);}
2035 | NEW_TK class_type error
2036 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2037 | NEW_TK class_type OP_TK error
2038 {$$ = NULL_TREE; yyerror ("')' or term expected"); RECOVER;}
2039 | NEW_TK class_type OP_TK argument_list error
2040 {$$ = NULL_TREE; yyerror ("')' expected"); RECOVER;}
2041 | something_dot_new error
2043 $$ = NULL_TREE;
2044 YYERROR_NOW;
2045 yyerror ("Identifier expected");
2046 RECOVER;
2048 | something_dot_new identifier error
2049 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2052 /* Created after JDK1.1 rules originally added to
2053 class_instance_creation_expression, but modified to use
2054 'class_type' instead of 'TypeName' (type_name) which is mentioned
2055 in the documentation but doesn't exist. */
2057 anonymous_class_creation:
2058 NEW_TK class_type OP_TK argument_list CP_TK
2059 { create_anonymous_class ($2); }
2060 class_body
2062 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2063 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2065 end_class_declaration (1);
2067 /* Now we can craft the new expression */
2068 $$ = build_new_invocation (id, $4);
2070 /* Note that we can't possibly be here if
2071 `class_type' is an interface (in which case the
2072 anonymous class extends Object and implements
2073 `class_type', hence its constructor can't have
2074 arguments.) */
2076 /* Otherwise, the innerclass must feature a
2077 constructor matching `argument_list'. Anonymous
2078 classes are a bit special: it's impossible to
2079 define constructor for them, hence constructors
2080 must be generated following the hints provided by
2081 the `new' expression. Whether a super constructor
2082 of that nature exists or not is to be verified
2083 later on in verify_constructor_super.
2085 It's during the expansion of a `new' statement
2086 referring to an anonymous class that a ctor will
2087 be generated for the anonymous class, with the
2088 right arguments. */
2091 | NEW_TK class_type OP_TK CP_TK
2092 { create_anonymous_class ($2); }
2093 class_body
2095 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2096 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2098 end_class_declaration (1);
2100 /* Now we can craft the new expression. The
2101 statement doesn't need to be remember so that a
2102 constructor can be generated, since its signature
2103 is already known. */
2104 $$ = build_new_invocation (id, NULL_TREE);
2108 something_dot_new: /* Added, not part of the specs. */
2109 name DOT_TK NEW_TK
2110 { $$ = $1; }
2111 | primary DOT_TK NEW_TK
2112 { $$ = $1; }
2115 argument_list:
2116 expression
2118 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2119 ctxp->formal_parameter_number = 1;
2121 | argument_list C_TK expression
2123 ctxp->formal_parameter_number += 1;
2124 $$ = tree_cons (NULL_TREE, $3, $1);
2126 | argument_list C_TK error
2127 {yyerror ("Missing term"); RECOVER;}
2130 array_creation_uninitialized:
2131 NEW_TK primitive_type dim_exprs
2132 { $$ = build_newarray_node ($2, $3, 0); }
2133 | NEW_TK class_or_interface_type dim_exprs
2134 { $$ = build_newarray_node ($2, $3, 0); }
2135 | NEW_TK primitive_type dim_exprs dims
2136 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2137 | NEW_TK class_or_interface_type dim_exprs dims
2138 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2139 | NEW_TK error CSB_TK
2140 {yyerror ("'[' expected"); DRECOVER ("]");}
2141 | NEW_TK error OSB_TK
2142 {yyerror ("']' expected"); RECOVER;}
2145 array_creation_initialized:
2146 /* Added, JDK1.1 anonymous array. Initial documentation rule
2147 modified */
2148 NEW_TK class_or_interface_type dims array_initializer
2150 char *sig;
2151 int osb = pop_current_osb (ctxp);
2152 while (osb--)
2153 obstack_grow (&temporary_obstack, "[]", 2);
2154 obstack_1grow (&temporary_obstack, '\0');
2155 sig = obstack_finish (&temporary_obstack);
2156 $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2157 $2, get_identifier (sig), $4);
2159 | NEW_TK primitive_type dims array_initializer
2161 int osb = pop_current_osb (ctxp);
2162 tree type = $2;
2163 while (osb--)
2164 type = build_java_array_type (type, -1);
2165 $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2166 build_pointer_type (type), NULL_TREE, $4);
2168 | NEW_TK error CSB_TK
2169 {yyerror ("'[' expected"); DRECOVER ("]");}
2170 | NEW_TK error OSB_TK
2171 {yyerror ("']' expected"); RECOVER;}
2174 dim_exprs:
2175 dim_expr
2176 { $$ = build_tree_list (NULL_TREE, $1); }
2177 | dim_exprs dim_expr
2178 { $$ = tree_cons (NULL_TREE, $2, $$); }
2181 dim_expr:
2182 OSB_TK expression CSB_TK
2184 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2186 $2 = build_wfl_node ($2);
2187 TREE_TYPE ($2) = NULL_TREE;
2189 EXPR_WFL_LINECOL ($2) = $1.location;
2190 $$ = $2;
2192 | OSB_TK expression error
2193 {yyerror ("']' expected"); RECOVER;}
2194 | OSB_TK error
2196 yyerror ("Missing term");
2197 yyerror ("']' expected");
2198 RECOVER;
2202 dims:
2203 OSB_TK CSB_TK
2205 int allocate = 0;
2206 /* If not initialized, allocate memory for the osb
2207 numbers stack */
2208 if (!ctxp->osb_limit)
2210 allocate = ctxp->osb_limit = 32;
2211 ctxp->osb_depth = -1;
2213 /* If capacity overflown, reallocate a bigger chunk */
2214 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2215 allocate = ctxp->osb_limit << 1;
2217 if (allocate)
2219 allocate *= sizeof (int);
2220 if (ctxp->osb_number)
2221 ctxp->osb_number = xrealloc (ctxp->osb_number,
2222 allocate);
2223 else
2224 ctxp->osb_number = xmalloc (allocate);
2226 ctxp->osb_depth++;
2227 CURRENT_OSB (ctxp) = 1;
2229 | dims OSB_TK CSB_TK
2230 { CURRENT_OSB (ctxp)++; }
2231 | dims OSB_TK error
2232 { yyerror ("']' expected"); RECOVER;}
2235 field_access:
2236 primary DOT_TK identifier
2237 { $$ = make_qualified_primary ($1, $3, $2.location); }
2238 /* FIXME - REWRITE TO:
2239 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2240 | SUPER_TK DOT_TK identifier
2242 tree super_wfl = build_wfl_node (super_identifier_node);
2243 SET_EXPR_LOCATION_FROM_TOKEN (super_wfl, $1);
2244 $$ = make_qualified_name (super_wfl, $3, $2.location);
2246 | SUPER_TK error
2247 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2250 method_invocation:
2251 name OP_TK CP_TK
2252 { $$ = build_method_invocation ($1, NULL_TREE); }
2253 | name OP_TK argument_list CP_TK
2254 { $$ = build_method_invocation ($1, $3); }
2255 | primary DOT_TK identifier OP_TK CP_TK
2257 if (TREE_CODE ($1) == THIS_EXPR)
2258 $$ = build_this_super_qualified_invocation
2259 (1, $3, NULL_TREE, 0, $2.location);
2260 else
2262 tree invok = build_method_invocation ($3, NULL_TREE);
2263 $$ = make_qualified_primary ($1, invok, $2.location);
2266 | primary DOT_TK identifier OP_TK argument_list CP_TK
2268 if (TREE_CODE ($1) == THIS_EXPR)
2269 $$ = build_this_super_qualified_invocation
2270 (1, $3, $5, 0, $2.location);
2271 else
2273 tree invok = build_method_invocation ($3, $5);
2274 $$ = make_qualified_primary ($1, invok, $2.location);
2277 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2279 $$ = build_this_super_qualified_invocation
2280 (0, $3, NULL_TREE, $1.location, $2.location);
2282 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2284 $$ = build_this_super_qualified_invocation
2285 (0, $3, $5, $1.location, $2.location);
2287 /* Screws up thing. I let it here until I'm convinced it can
2288 be removed. FIXME
2289 | primary DOT_TK error
2290 {yyerror ("'(' expected"); DRECOVER(bad);} */
2291 | SUPER_TK DOT_TK error CP_TK
2292 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2293 | SUPER_TK DOT_TK error DOT_TK
2294 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2297 array_access:
2298 name OSB_TK expression CSB_TK
2299 { $$ = build_array_ref ($2.location, $1, $3); }
2300 | primary_no_new_array OSB_TK expression CSB_TK
2301 { $$ = build_array_ref ($2.location, $1, $3); }
2302 | array_creation_initialized OSB_TK expression CSB_TK
2303 { $$ = build_array_ref ($2.location, $1, $3); }
2304 | name OSB_TK error
2306 yyerror ("Missing term and ']' expected");
2307 DRECOVER(array_access);
2309 | name OSB_TK expression error
2311 yyerror ("']' expected");
2312 DRECOVER(array_access);
2314 | primary_no_new_array OSB_TK error
2316 yyerror ("Missing term and ']' expected");
2317 DRECOVER(array_access);
2319 | primary_no_new_array OSB_TK expression error
2321 yyerror ("']' expected");
2322 DRECOVER(array_access);
2324 | array_creation_initialized OSB_TK error
2326 yyerror ("Missing term and ']' expected");
2327 DRECOVER(array_access);
2329 | array_creation_initialized OSB_TK expression error
2331 yyerror ("']' expected");
2332 DRECOVER(array_access);
2336 postfix_expression:
2337 primary
2338 | name
2339 | post_increment_expression
2340 | post_decrement_expression
2343 post_increment_expression:
2344 postfix_expression INCR_TK
2345 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2348 post_decrement_expression:
2349 postfix_expression DECR_TK
2350 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2353 trap_overflow_corner_case:
2354 pre_increment_expression
2355 | pre_decrement_expression
2356 | PLUS_TK unary_expression
2357 {$$ = build_unaryop ($1.token, $1.location, $2); }
2358 | unary_expression_not_plus_minus
2359 | PLUS_TK error
2360 {yyerror ("Missing term"); RECOVER}
2363 unary_expression:
2364 trap_overflow_corner_case
2366 if ($1)
2367 error_if_numeric_overflow ($1);
2368 $$ = $1;
2370 | MINUS_TK trap_overflow_corner_case
2371 {$$ = build_unaryop ($1.token, $1.location, $2); }
2372 | MINUS_TK error
2373 {yyerror ("Missing term"); RECOVER}
2376 pre_increment_expression:
2377 INCR_TK unary_expression
2378 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2379 | INCR_TK error
2380 {yyerror ("Missing term"); RECOVER}
2383 pre_decrement_expression:
2384 DECR_TK unary_expression
2385 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2386 | DECR_TK error
2387 {yyerror ("Missing term"); RECOVER}
2390 unary_expression_not_plus_minus:
2391 postfix_expression
2392 | NOT_TK unary_expression
2393 {$$ = build_unaryop ($1.token, $1.location, $2); }
2394 | NEG_TK unary_expression
2395 {$$ = build_unaryop ($1.token, $1.location, $2); }
2396 | cast_expression
2397 | NOT_TK error
2398 {yyerror ("Missing term"); RECOVER}
2399 | NEG_TK error
2400 {yyerror ("Missing term"); RECOVER}
2403 cast_expression: /* Error handling here is potentially weak */
2404 OP_TK primitive_type dims CP_TK unary_expression
2406 tree type = $2;
2407 int osb = pop_current_osb (ctxp);
2408 while (osb--)
2409 type = build_java_array_type (type, -1);
2410 $$ = build_cast ($1.location, type, $5);
2412 | OP_TK primitive_type CP_TK unary_expression
2413 { $$ = build_cast ($1.location, $2, $4); }
2414 | OP_TK expression CP_TK unary_expression_not_plus_minus
2415 { $$ = build_cast ($1.location, $2, $4); }
2416 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2418 const char *ptr;
2419 int osb = pop_current_osb (ctxp);
2420 obstack_grow (&temporary_obstack,
2421 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2422 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2423 while (osb--)
2424 obstack_grow (&temporary_obstack, "[]", 2);
2425 obstack_1grow (&temporary_obstack, '\0');
2426 ptr = obstack_finish (&temporary_obstack);
2427 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2428 $$ = build_cast ($1.location, $2, $5);
2430 | OP_TK primitive_type OSB_TK error
2431 {yyerror ("']' expected, invalid type expression");}
2432 | OP_TK error
2434 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2435 RECOVER;
2437 | OP_TK primitive_type dims CP_TK error
2438 {yyerror ("Missing term"); RECOVER;}
2439 | OP_TK primitive_type CP_TK error
2440 {yyerror ("Missing term"); RECOVER;}
2441 | OP_TK name dims CP_TK error
2442 {yyerror ("Missing term"); RECOVER;}
2445 multiplicative_expression:
2446 unary_expression
2447 | multiplicative_expression MULT_TK unary_expression
2449 $$ = build_binop (BINOP_LOOKUP ($2.token),
2450 $2.location, $1, $3);
2452 | multiplicative_expression DIV_TK unary_expression
2454 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2455 $1, $3);
2457 | multiplicative_expression REM_TK unary_expression
2459 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2460 $1, $3);
2462 | multiplicative_expression MULT_TK error
2463 {yyerror ("Missing term"); RECOVER;}
2464 | multiplicative_expression DIV_TK error
2465 {yyerror ("Missing term"); RECOVER;}
2466 | multiplicative_expression REM_TK error
2467 {yyerror ("Missing term"); RECOVER;}
2470 additive_expression:
2471 multiplicative_expression
2472 | additive_expression PLUS_TK multiplicative_expression
2474 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2475 $1, $3);
2477 | additive_expression MINUS_TK multiplicative_expression
2479 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2480 $1, $3);
2482 | additive_expression PLUS_TK error
2483 {yyerror ("Missing term"); RECOVER;}
2484 | additive_expression MINUS_TK error
2485 {yyerror ("Missing term"); RECOVER;}
2488 shift_expression:
2489 additive_expression
2490 | shift_expression LS_TK additive_expression
2492 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2493 $1, $3);
2495 | shift_expression SRS_TK additive_expression
2497 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2498 $1, $3);
2500 | shift_expression ZRS_TK additive_expression
2502 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2503 $1, $3);
2505 | shift_expression LS_TK error
2506 {yyerror ("Missing term"); RECOVER;}
2507 | shift_expression SRS_TK error
2508 {yyerror ("Missing term"); RECOVER;}
2509 | shift_expression ZRS_TK error
2510 {yyerror ("Missing term"); RECOVER;}
2513 relational_expression:
2514 shift_expression
2515 | relational_expression LT_TK shift_expression
2517 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2518 $1, $3);
2520 | relational_expression GT_TK shift_expression
2522 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2523 $1, $3);
2525 | relational_expression LTE_TK shift_expression
2527 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2528 $1, $3);
2530 | relational_expression GTE_TK shift_expression
2532 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2533 $1, $3);
2535 | relational_expression INSTANCEOF_TK reference_type
2536 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2537 | relational_expression LT_TK error
2538 {yyerror ("Missing term"); RECOVER;}
2539 | relational_expression GT_TK error
2540 {yyerror ("Missing term"); RECOVER;}
2541 | relational_expression LTE_TK error
2542 {yyerror ("Missing term"); RECOVER;}
2543 | relational_expression GTE_TK error
2544 {yyerror ("Missing term"); RECOVER;}
2545 | relational_expression INSTANCEOF_TK error
2546 {yyerror ("Invalid reference type"); RECOVER;}
2549 equality_expression:
2550 relational_expression
2551 | equality_expression EQ_TK relational_expression
2553 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2554 $1, $3);
2556 | equality_expression NEQ_TK relational_expression
2558 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2559 $1, $3);
2561 | equality_expression EQ_TK error
2562 {yyerror ("Missing term"); RECOVER;}
2563 | equality_expression NEQ_TK error
2564 {yyerror ("Missing term"); RECOVER;}
2567 and_expression:
2568 equality_expression
2569 | and_expression AND_TK equality_expression
2571 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2572 $1, $3);
2574 | and_expression AND_TK error
2575 {yyerror ("Missing term"); RECOVER;}
2578 exclusive_or_expression:
2579 and_expression
2580 | exclusive_or_expression XOR_TK and_expression
2582 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2583 $1, $3);
2585 | exclusive_or_expression XOR_TK error
2586 {yyerror ("Missing term"); RECOVER;}
2589 inclusive_or_expression:
2590 exclusive_or_expression
2591 | inclusive_or_expression OR_TK exclusive_or_expression
2593 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2594 $1, $3);
2596 | inclusive_or_expression OR_TK error
2597 {yyerror ("Missing term"); RECOVER;}
2600 conditional_and_expression:
2601 inclusive_or_expression
2602 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2604 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2605 $1, $3);
2607 | conditional_and_expression BOOL_AND_TK error
2608 {yyerror ("Missing term"); RECOVER;}
2611 conditional_or_expression:
2612 conditional_and_expression
2613 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2615 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2616 $1, $3);
2618 | conditional_or_expression BOOL_OR_TK error
2619 {yyerror ("Missing term"); RECOVER;}
2622 conditional_expression: /* Error handling here is weak */
2623 conditional_or_expression
2624 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2626 $$ = build3 (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2627 SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
2629 | conditional_or_expression REL_QM_TK REL_CL_TK error
2631 YYERROR_NOW;
2632 yyerror ("Missing term");
2633 DRECOVER (1);
2635 | conditional_or_expression REL_QM_TK error
2636 {yyerror ("Missing term"); DRECOVER (2);}
2637 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2638 {yyerror ("Missing term"); DRECOVER (3);}
2641 assignment_expression:
2642 conditional_expression
2643 | assignment
2646 assignment:
2647 left_hand_side assignment_operator assignment_expression
2648 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2649 | left_hand_side assignment_operator error
2651 YYNOT_TWICE yyerror ("Missing term");
2652 DRECOVER (assign);
2656 left_hand_side:
2657 name
2658 | field_access
2659 | array_access
2662 assignment_operator:
2663 ASSIGN_ANY_TK
2664 | ASSIGN_TK
2667 expression:
2668 assignment_expression
2671 constant_expression:
2672 expression
2677 /* Helper function to retrieve an OSB count. Should be used when the
2678 `dims:' rule is being used. */
2680 static int
2681 pop_current_osb (struct parser_ctxt *ctxp)
2683 int to_return;
2685 if (ctxp->osb_depth < 0)
2686 abort ();
2688 to_return = CURRENT_OSB (ctxp);
2689 ctxp->osb_depth--;
2691 return to_return;
2696 /* This section of the code deal with save/restoring parser contexts.
2697 Add mode documentation here. FIXME */
2699 /* Helper function. Create a new parser context. With
2700 COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2701 context is copied, otherwise, the new context is zeroed. The newly
2702 created context becomes the current one. */
2704 static void
2705 create_new_parser_context (int copy_from_previous)
2707 struct parser_ctxt *new;
2709 new = ggc_alloc (sizeof (struct parser_ctxt));
2710 if (copy_from_previous)
2712 memcpy (new, ctxp, sizeof (struct parser_ctxt));
2713 /* This flag, indicating the context saves global values,
2714 should only be set by java_parser_context_save_global. */
2715 new->saved_data_ctx = 0;
2717 else
2718 memset (new, 0, sizeof (struct parser_ctxt));
2720 new->next = ctxp;
2721 ctxp = new;
2724 /* Create a new parser context and make it the current one. */
2726 void
2727 java_push_parser_context (void)
2729 create_new_parser_context (0);
2732 void
2733 java_pop_parser_context (int generate)
2735 tree current;
2736 struct parser_ctxt *next;
2738 if (!ctxp)
2739 return;
2741 next = ctxp->next;
2742 if (next)
2744 input_location = ctxp->save_location;
2745 current_class = ctxp->class_type;
2748 /* If the old and new lexers differ, then free the old one. */
2749 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2750 java_destroy_lexer (ctxp->lexer);
2752 /* Set the single import class file flag to 0 for the current list
2753 of imported things */
2754 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2755 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2757 /* If we pushed a context to parse a class intended to be generated,
2758 we keep it so we can remember the class. What we could actually
2759 do is to just update a list of class names. */
2760 if (generate)
2762 if (ctxp_for_generation_last == NULL)
2763 ctxp_for_generation = ctxp;
2764 else
2765 ctxp_for_generation_last->next = ctxp;
2766 ctxp->next = NULL;
2767 ctxp_for_generation_last = ctxp;
2770 /* And restore those of the previous context */
2771 if ((ctxp = next)) /* Assignment is really meant here */
2772 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2773 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2776 /* Create a parser context for the use of saving some global
2777 variables. */
2779 void
2780 java_parser_context_save_global (void)
2782 if (!ctxp)
2784 java_push_parser_context ();
2785 ctxp->saved_data_ctx = 1;
2788 /* If this context already stores data, create a new one suitable
2789 for data storage. */
2790 else if (ctxp->saved_data)
2792 create_new_parser_context (1);
2793 ctxp->saved_data_ctx = 1;
2796 ctxp->save_location = input_location;
2797 ctxp->class_type = current_class;
2798 ctxp->function_decl = current_function_decl;
2799 ctxp->saved_data = 1;
2802 /* Restore some global variables from the previous context. Make the
2803 previous context the current one. */
2805 void
2806 java_parser_context_restore_global (void)
2808 input_location = ctxp->save_location;
2809 current_class = ctxp->class_type;
2810 if (wfl_operator)
2811 #ifdef USE_MAPPED_LOCATION
2812 SET_EXPR_LOCATION (wfl_operator, ctxp->save_location);
2813 #else
2814 EXPR_WFL_FILENAME_NODE (wfl_operator) = get_identifier (input_filename);
2815 #endif
2816 current_function_decl = ctxp->function_decl;
2817 ctxp->saved_data = 0;
2818 if (ctxp->saved_data_ctx)
2819 java_pop_parser_context (0);
2822 /* Suspend vital data for the current class/function being parsed so
2823 that an other class can be parsed. Used to let local/anonymous
2824 classes be parsed. */
2826 static void
2827 java_parser_context_suspend (void)
2829 /* This makes debugging through java_debug_context easier */
2830 static const char *const name = "<inner buffer context>";
2832 /* Duplicate the previous context, use it to save the globals we're
2833 interested in */
2834 create_new_parser_context (1);
2835 ctxp->function_decl = current_function_decl;
2836 ctxp->class_type = current_class;
2838 /* Then create a new context which inherits all data from the
2839 previous one. This will be the new current context */
2840 create_new_parser_context (1);
2842 /* Help debugging */
2843 ctxp->next->filename = name;
2846 /* Resume vital data for the current class/function being parsed so
2847 that an other class can be parsed. Used to let local/anonymous
2848 classes be parsed. The trick is the data storing file position
2849 informations must be restored to their current value, so parsing
2850 can resume as if no context was ever saved. */
2852 static void
2853 java_parser_context_resume (void)
2855 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2856 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2857 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2859 /* We need to inherit the list of classes to complete/generate */
2860 restored->classd_list = old->classd_list;
2861 restored->class_list = old->class_list;
2863 /* Restore the current class and function from the saver */
2864 current_class = saver->class_type;
2865 current_function_decl = saver->function_decl;
2867 /* Retrieve the restored context */
2868 ctxp = restored;
2870 /* Re-installed the data for the parsing to carry on */
2871 memcpy (&ctxp->marker_begining, &old->marker_begining,
2872 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2875 /* Add a new anchor node to which all statement(s) initializing static
2876 and non static initialized upon declaration field(s) will be
2877 linked. */
2879 static void
2880 java_parser_context_push_initialized_field (void)
2882 tree node;
2884 node = build_tree_list (NULL_TREE, NULL_TREE);
2885 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2886 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2888 node = build_tree_list (NULL_TREE, NULL_TREE);
2889 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2890 CPC_INITIALIZER_LIST (ctxp) = node;
2892 node = build_tree_list (NULL_TREE, NULL_TREE);
2893 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2894 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2897 /* Pop the lists of initialized field. If this lists aren't empty,
2898 remember them so we can use it to create and populate the finit$
2899 or <clinit> functions. */
2901 static void
2902 java_parser_context_pop_initialized_field (void)
2904 tree stmts;
2905 tree class_type = TREE_TYPE (GET_CPC ());
2907 if (CPC_INITIALIZER_LIST (ctxp))
2909 stmts = CPC_INITIALIZER_STMT (ctxp);
2910 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2911 if (stmts && !java_error_count)
2912 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2915 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2917 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2918 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2919 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2920 /* Keep initialization in order to enforce 8.5 */
2921 if (stmts && !java_error_count)
2922 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2925 /* JDK 1.1 instance initializers */
2926 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2928 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2929 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2930 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2931 if (stmts && !java_error_count)
2932 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2936 static tree
2937 reorder_static_initialized (tree list)
2939 /* We have to keep things in order. The alias initializer have to
2940 come first, then the initialized regular field, in reverse to
2941 keep them in lexical order. */
2942 tree marker, previous = NULL_TREE;
2943 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2944 if (TREE_CODE (marker) == TREE_LIST
2945 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2946 break;
2948 /* No static initialized, the list is fine as is */
2949 if (!previous)
2950 list = TREE_CHAIN (marker);
2952 /* No marker? reverse the whole list */
2953 else if (!marker)
2954 list = nreverse (list);
2956 /* Otherwise, reverse what's after the marker and the new reordered
2957 sublist will replace the marker. */
2958 else
2960 TREE_CHAIN (previous) = NULL_TREE;
2961 list = nreverse (list);
2962 list = chainon (TREE_CHAIN (marker), list);
2964 return list;
2967 /* Helper functions to dump the parser context stack. */
2969 #define TAB_CONTEXT(C) \
2970 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2972 static void
2973 java_debug_context_do (int tab)
2975 struct parser_ctxt *copy = ctxp;
2976 while (copy)
2978 TAB_CONTEXT (tab);
2979 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2980 TAB_CONTEXT (tab);
2981 fprintf (stderr, "filename: %s\n", copy->filename);
2982 TAB_CONTEXT (tab);
2983 fprintf (stderr, "package: %s\n",
2984 (copy->package ?
2985 IDENTIFIER_POINTER (copy->package) : "<none>"));
2986 TAB_CONTEXT (tab);
2987 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2988 TAB_CONTEXT (tab);
2989 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2990 copy = copy->next;
2991 tab += 2;
2995 /* Dump the stacked up parser contexts. Intended to be called from a
2996 debugger. */
2998 void
2999 java_debug_context (void)
3001 java_debug_context_do (0);
3006 /* Flag for the error report routine to issue the error the first time
3007 it's called (overriding the default behavior which is to drop the
3008 first invocation and honor the second one, taking advantage of a
3009 richer context. */
3010 static int force_error = 0;
3012 /* Reporting an constructor invocation error. */
3013 static void
3014 parse_ctor_invocation_error (void)
3016 if (DECL_CONSTRUCTOR_P (current_function_decl))
3017 yyerror ("Constructor invocation must be first thing in a constructor");
3018 else
3019 yyerror ("Only constructors can invoke constructors");
3022 /* Reporting JDK1.1 features not implemented. */
3024 static tree
3025 parse_jdk1_1_error (const char *msg)
3027 sorry (": %qs JDK1.1(TM) feature", msg);
3028 java_error_count++;
3029 return build_java_empty_stmt ();
3032 static int do_warning = 0;
3034 void
3035 yyerror (const char *msgid)
3037 #ifdef USE_MAPPED_LOCATION
3038 static source_location elc;
3039 expanded_location xloc = expand_location (input_location);
3040 int current_line = xloc.line;
3041 #else
3042 static java_lc elc;
3043 int save_lineno;
3044 int current_line = input_line;
3045 #endif
3046 static int prev_lineno;
3047 static const char *prev_msg;
3049 char *remainder, *code_from_source;
3051 if (!force_error && prev_lineno == current_line)
3052 return;
3053 #ifndef USE_MAPPED_LOCATION
3054 current_line = ctxp->lexer->token_start.line;
3055 #endif
3057 /* Save current error location but report latter, when the context is
3058 richer. */
3059 if (ctxp->java_error_flag == 0)
3061 ctxp->java_error_flag = 1;
3062 #ifdef USE_MAPPED_LOCATION
3063 elc = input_location;
3064 #else
3065 elc = ctxp->lexer->token_start;
3066 #endif
3067 /* Do something to use the previous line if we're reaching the
3068 end of the file... */
3069 #ifdef VERBOSE_SKELETON
3070 printf ("* Error detected (%s)\n", (msgid ? msgid : "(null)"));
3071 #endif
3072 return;
3075 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3076 if (!force_error && msgid == prev_msg && prev_lineno == current_line)
3077 return;
3079 ctxp->java_error_flag = 0;
3080 if (do_warning)
3081 java_warning_count++;
3082 else
3083 java_error_count++;
3085 #if 0 /* FIXME */
3086 if (elc.col == 0 && msgid && msgid[1] == ';')
3087 elc = ctxp->prev_line_end;
3088 #endif
3090 prev_msg = msgid;
3092 #ifdef USE_MAPPED_LOCATION
3093 prev_lineno = current_line;
3094 code_from_source = java_get_line_col (xloc.file, current_line, xloc.column);
3095 #else
3096 save_lineno = input_line;
3097 prev_lineno = input_line = current_line;
3098 code_from_source = java_get_line_col (input_filename, current_line,
3099 ctxp->lexer->token_start.col);
3100 #endif
3103 obstack_grow0 (&temporary_obstack,
3104 code_from_source, strlen (code_from_source));
3105 remainder = obstack_finish (&temporary_obstack);
3106 if (do_warning)
3107 warning (0, "%s.\n%s", msgid, remainder);
3108 else
3109 error ("%s.\n%s", msgid, remainder);
3111 /* This allow us to cheaply avoid an extra 'Invalid expression
3112 statement' error report when errors have been already reported on
3113 the same line. This occurs when we report an error but don't have
3114 a synchronization point other than ';', which
3115 expression_statement is the only one to take care of. */
3116 #ifndef USE_MAPPED_LOCATION
3117 input_line = save_lineno;
3118 #endif
3119 ctxp->prevent_ese = input_line;
3122 static void
3123 issue_warning_error_from_context (
3124 #ifdef USE_MAPPED_LOCATION
3125 source_location cl,
3126 #else
3127 tree cl,
3128 #endif
3129 const char *gmsgid, va_list *ap)
3131 #ifdef USE_MAPPED_LOCATION
3132 source_location saved_location = input_location;
3133 expanded_location xloc = expand_location (cl);
3134 #else
3135 java_lc save_lc = ctxp->lexer->token_start;
3136 const char *saved = ctxp->filename, *saved_input_filename;
3137 #endif
3138 char buffer [4096];
3139 text_info text;
3141 text.err_no = errno;
3142 text.args_ptr = ap;
3143 text.format_spec = gmsgid;
3144 pp_format_text (global_dc->printer, &text);
3145 strncpy (buffer, pp_formatted_text (global_dc->printer), sizeof (buffer) - 1);
3146 buffer[sizeof (buffer) - 1] = '\0';
3147 pp_clear_output_area (global_dc->printer);
3149 force_error = 1;
3151 #ifdef USE_MAPPED_LOCATION
3152 if (xloc.file != NULL)
3154 ctxp->filename = xloc.file;
3155 input_location = cl;
3157 #else
3158 ctxp->lexer->token_start.line = EXPR_WFL_LINENO (cl);
3159 ctxp->lexer->token_start.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1
3160 : EXPR_WFL_COLNO (cl) == 0xffe ? -2
3161 : EXPR_WFL_COLNO (cl));
3163 /* We have a CL, that's a good reason for using it if it contains data */
3164 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3165 ctxp->filename = EXPR_WFL_FILENAME (cl);
3166 saved_input_filename = input_filename;
3167 input_filename = ctxp->filename;
3168 #endif
3169 java_error (NULL);
3170 java_error (buffer);
3171 #ifdef USE_MAPPED_LOCATION
3172 input_location = saved_location;
3173 #else
3174 ctxp->filename = saved;
3175 input_filename = saved_input_filename;
3176 ctxp->lexer->token_start = save_lc;
3177 #endif
3178 force_error = 0;
3181 /* Issue an error message at a current source line CL.
3182 FUTURE/FIXME: change cl to be a source_location. */
3184 void
3185 parse_error_context (tree cl, const char *gmsgid, ...)
3187 va_list ap;
3188 va_start (ap, gmsgid);
3189 #ifdef USE_MAPPED_LOCATION
3190 issue_warning_error_from_context (EXPR_LOCATION (cl), gmsgid, &ap);
3191 #else
3192 issue_warning_error_from_context (cl, gmsgid, &ap);
3193 #endif
3194 va_end (ap);
3197 /* Issue a warning at a current source line CL.
3198 FUTURE/FIXME: change cl to be a source_location. */
3200 static void
3201 parse_warning_context (tree cl, const char *gmsgid, ...)
3203 va_list ap;
3204 va_start (ap, gmsgid);
3206 do_warning = 1;
3207 #ifdef USE_MAPPED_LOCATION
3208 issue_warning_error_from_context (EXPR_LOCATION (cl), gmsgid, &ap);
3209 #else
3210 issue_warning_error_from_context (cl, gmsgid, &ap);
3211 #endif
3212 do_warning = 0;
3213 va_end (ap);
3216 static tree
3217 find_expr_with_wfl (tree node)
3219 while (node)
3221 enum tree_code_class code;
3222 tree to_return;
3224 switch (TREE_CODE (node))
3226 case BLOCK:
3227 node = BLOCK_EXPR_BODY (node);
3228 continue;
3230 case COMPOUND_EXPR:
3231 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3232 if (to_return)
3233 return to_return;
3234 node = TREE_OPERAND (node, 1);
3235 continue;
3237 case LOOP_EXPR:
3238 node = TREE_OPERAND (node, 0);
3239 continue;
3241 case LABELED_BLOCK_EXPR:
3242 node = LABELED_BLOCK_BODY (node);
3243 continue;
3245 default:
3246 code = TREE_CODE_CLASS (TREE_CODE (node));
3247 if (((code == tcc_unary) || (code == tcc_binary)
3248 || (code == tcc_expression))
3249 && EXPR_WFL_LINECOL (node))
3250 return node;
3251 return NULL_TREE;
3254 return NULL_TREE;
3257 /* Issue a missing return statement error. Uses METHOD to figure the
3258 last line of the method the error occurs in. */
3260 static void
3261 missing_return_error (tree method)
3263 #ifdef USE_MAPPED_LOCATION
3264 SET_EXPR_LOCATION (wfl_operator, DECL_FUNCTION_LAST_LINE (method));
3265 #else
3266 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_FUNCTION_LAST_LINE (method), -2);
3267 #endif
3268 parse_error_context (wfl_operator, "Missing return statement");
3271 /* Issue an unreachable statement error. From NODE, find the next
3272 statement to report appropriately. */
3273 static void
3274 unreachable_stmt_error (tree node)
3276 /* Browse node to find the next expression node that has a WFL. Use
3277 the location to report the error */
3278 if (TREE_CODE (node) == COMPOUND_EXPR)
3279 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3280 else
3281 node = find_expr_with_wfl (node);
3283 if (node)
3285 #ifdef USE_MAPPED_LOCATION
3286 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
3287 #else
3288 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3289 #endif
3290 parse_error_context (wfl_operator, "Unreachable statement");
3292 else
3293 abort ();
3296 static int
3297 not_accessible_field_error (tree wfl, tree decl)
3299 parse_error_context
3300 (wfl, "Can't access %s field %<%s.%s%> from %qs",
3301 accessibility_string (get_access_flags_from_decl (decl)),
3302 GET_TYPE_NAME (DECL_CONTEXT (decl)),
3303 IDENTIFIER_POINTER (DECL_NAME (decl)),
3304 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
3305 return 1;
3309 java_report_errors (void)
3311 if (java_error_count)
3312 fprintf (stderr, "%d error%s",
3313 java_error_count, (java_error_count == 1 ? "" : "s"));
3314 if (java_warning_count)
3315 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3316 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3317 if (java_error_count || java_warning_count)
3318 putc ('\n', stderr);
3319 return java_error_count;
3322 static char *
3323 java_accstring_lookup (int flags)
3325 static char buffer [80];
3326 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3328 /* Access modifier looked-up first for easier report on forbidden
3329 access. */
3330 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3331 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3332 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3333 if (flags & ACC_STATIC) COPY_RETURN ("static");
3334 if (flags & ACC_FINAL) COPY_RETURN ("final");
3335 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3336 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3337 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3338 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3339 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3340 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3342 buffer [0] = '\0';
3343 return buffer;
3344 #undef COPY_RETURN
3347 /* Returns a string denoting the accessibility of a class or a member as
3348 indicated by FLAGS. We need a separate function from
3349 java_accstring_lookup, as the latter can return spurious "static", etc.
3350 if package-private access is defined (in which case none of the
3351 relevant access control bits in FLAGS is set). */
3353 static const char *
3354 accessibility_string (int flags)
3356 if (flags & ACC_PRIVATE) return "private";
3357 if (flags & ACC_PROTECTED) return "protected";
3358 if (flags & ACC_PUBLIC) return "public";
3360 return "package-private";
3363 /* Issuing error messages upon redefinition of classes, interfaces or
3364 variables. */
3366 static void
3367 classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3369 parse_error_context (cl, "%s %qs already defined in %s:%d",
3370 context, IDENTIFIER_POINTER (id),
3371 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3372 /* Here we should point out where its redefined. It's a unicode. FIXME */
3375 static void
3376 variable_redefinition_error (tree context, tree name, tree type, int line)
3378 const char *type_name;
3380 /* Figure a proper name for type. We might haven't resolved it */
3381 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3382 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3383 else
3384 type_name = lang_printable_name (type, 0);
3386 parse_error_context (context,
3387 "Variable %qs is already defined in this method and was declared %<%s %s%> at line %d",
3388 IDENTIFIER_POINTER (name),
3389 type_name, IDENTIFIER_POINTER (name), line);
3392 /* If ANAME is terminated with `[]', it indicates an array. This
3393 function returns the number of `[]' found and if this number is
3394 greater than zero, it extracts the array type name and places it in
3395 the node pointed to by TRIMMED unless TRIMMED is null. */
3397 static int
3398 build_type_name_from_array_name (tree aname, tree *trimmed)
3400 const char *name = IDENTIFIER_POINTER (aname);
3401 int len = IDENTIFIER_LENGTH (aname);
3402 int array_dims;
3404 STRING_STRIP_BRACKETS (name, len, array_dims);
3406 if (array_dims && trimmed)
3407 *trimmed = get_identifier_with_length (name, len);
3409 return array_dims;
3412 static tree
3413 build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3415 int more_dims = 0;
3417 /* Eventually get more dims */
3418 more_dims = build_type_name_from_array_name (name, &name);
3420 /* If we have, then craft a new type for this variable */
3421 if (more_dims)
3423 tree save = type;
3425 /* If we have a pointer, use its type */
3426 if (TREE_CODE (type) == POINTER_TYPE)
3427 type = TREE_TYPE (type);
3429 /* Building the first dimension of a primitive type uses this
3430 function */
3431 if (JPRIMITIVE_TYPE_P (type))
3433 type = build_java_array_type (type, -1);
3434 more_dims--;
3436 /* Otherwise, if we have a WFL for this type, use it (the type
3437 is already an array on an unresolved type, and we just keep
3438 on adding dimensions) */
3439 else if (type_wfl)
3441 type = type_wfl;
3442 more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3443 NULL);
3446 /* Add all the dimensions */
3447 while (more_dims--)
3448 type = build_unresolved_array_type (type);
3450 /* The type may have been incomplete in the first place */
3451 if (type_wfl)
3452 type = obtain_incomplete_type (type);
3455 if (ret_name)
3456 *ret_name = name;
3457 return type;
3460 /* Build something that the type identifier resolver will identify as
3461 being an array to an unresolved type. TYPE_WFL is a WFL on a
3462 identifier. */
3464 static tree
3465 build_unresolved_array_type (tree type_or_wfl)
3467 const char *ptr;
3468 tree wfl;
3470 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3471 just create a array type */
3472 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3473 return build_java_array_type (type_or_wfl, -1);
3475 obstack_grow (&temporary_obstack,
3476 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3477 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3478 obstack_grow0 (&temporary_obstack, "[]", 2);
3479 ptr = obstack_finish (&temporary_obstack);
3480 #ifdef USE_MAPPED_LOCATION
3481 wfl = build_expr_wfl (get_identifier (ptr), EXPR_LOCATION (type_or_wfl));
3482 #else
3483 wfl = build_expr_wfl (get_identifier (ptr),
3484 EXPR_WFL_FILENAME (type_or_wfl),
3485 EXPR_WFL_LINENO (type_or_wfl),
3486 EXPR_WFL_COLNO (type_or_wfl));
3487 #endif
3488 /* Re-install the existing qualifications so that the type can be
3489 resolved properly. */
3490 EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3491 return wfl;
3494 static void
3495 parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3497 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3498 parse_error_context (wfl, "Interface %qs repeated",
3499 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3502 /* Bulk of common class/interface checks. Return 1 if an error was
3503 encountered. TAG is 0 for a class, 1 for an interface. */
3505 static int
3506 check_class_interface_creation (int is_interface, int flags, tree raw_name,
3507 tree qualified_name, tree decl, tree cl)
3509 tree node;
3510 int sca = 0; /* Static class allowed */
3511 int icaf = 0; /* Inner class allowed flags */
3512 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3514 if (!quiet_flag)
3515 fprintf (stderr, " %s%s %s",
3516 (CPC_INNER_P () ? "inner" : ""),
3517 (is_interface ? "interface" : "class"),
3518 IDENTIFIER_POINTER (qualified_name));
3520 /* Scope of an interface/class type name:
3521 - Can't be imported by a single type import
3522 - Can't already exists in the package */
3523 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3524 && (node = find_name_in_single_imports (raw_name))
3525 && !CPC_INNER_P ())
3527 parse_error_context
3528 (cl, "%s name %qs clashes with imported type %qs",
3529 (is_interface ? "Interface" : "Class"),
3530 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3531 return 1;
3533 if (decl && CLASS_COMPLETE_P (decl))
3535 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3536 qualified_name, decl, cl);
3537 return 1;
3540 if (check_inner_class_redefinition (raw_name, cl))
3541 return 1;
3543 /* If public, file name should match class/interface name, except
3544 when dealing with an inner class */
3545 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3547 const char *fname = input_filename;
3548 const char *f;
3550 for (f = fname + strlen (fname);
3551 f != fname && ! IS_DIR_SEPARATOR (*f);
3552 f--)
3554 if (IS_DIR_SEPARATOR (*f))
3555 f++;
3556 if (strncmp (IDENTIFIER_POINTER (raw_name),
3557 f , IDENTIFIER_LENGTH (raw_name)) ||
3558 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3559 parse_error_context
3560 (cl, "Public %s %qs must be defined in a file called %<%s.java%>",
3561 (is_interface ? "interface" : "class"),
3562 IDENTIFIER_POINTER (qualified_name),
3563 IDENTIFIER_POINTER (raw_name));
3566 /* Static classes can be declared only in top level classes. Note:
3567 once static, a inner class is a top level class. */
3568 if (flags & ACC_STATIC)
3570 /* Catch the specific error of declaring an class inner class
3571 with no toplevel enclosing class. Prevent check_modifiers from
3572 complaining a second time */
3573 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3575 parse_error_context (cl, "Inner class %qs can't be static. Static classes can only occur in interfaces and top-level classes",
3576 IDENTIFIER_POINTER (qualified_name));
3577 sca = ACC_STATIC;
3579 /* Else, in the context of a top-level class declaration, let
3580 `check_modifiers' do its job, otherwise, give it a go */
3581 else
3582 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3585 /* Inner classes can be declared private or protected
3586 within their enclosing classes. */
3587 if (CPC_INNER_P ())
3589 /* A class which is local to a block can't be public, private,
3590 protected or static. But it is created final, so allow this
3591 one. */
3592 if (current_function_decl)
3593 icaf = sca = uaaf = ACC_FINAL;
3594 else
3596 check_modifiers_consistency (flags);
3597 icaf = ACC_PROTECTED;
3598 if (! CLASS_INTERFACE (GET_CPC ()))
3599 icaf |= ACC_PRIVATE;
3603 if (is_interface)
3605 if (CPC_INNER_P ())
3606 uaaf = INTERFACE_INNER_MODIFIERS;
3607 else
3608 uaaf = INTERFACE_MODIFIERS;
3610 check_modifiers ("Illegal modifier %qs for interface declaration",
3611 flags, uaaf);
3613 else
3614 check_modifiers ((current_function_decl ?
3615 "Illegal modifier %qs for local class declaration" :
3616 "Illegal modifier %qs for class declaration"),
3617 flags, uaaf|sca|icaf);
3618 return 0;
3621 /* Construct a nested class name. If the final component starts with
3622 a digit, return true. Otherwise return false. */
3623 static int
3624 make_nested_class_name (tree cpc_list)
3626 tree name;
3628 if (!cpc_list)
3629 return 0;
3631 make_nested_class_name (TREE_CHAIN (cpc_list));
3633 /* Pick the qualified name when dealing with the first upmost
3634 enclosing class */
3635 name = (TREE_CHAIN (cpc_list)
3636 ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3637 obstack_grow (&temporary_obstack,
3638 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3639 obstack_1grow (&temporary_obstack, '$');
3641 return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3644 /* Can't redefine a class already defined in an earlier scope. */
3646 static int
3647 check_inner_class_redefinition (tree raw_name, tree cl)
3649 tree scope_list;
3651 for (scope_list = GET_CPC_LIST (); scope_list;
3652 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3653 if (raw_name == GET_CPC_UN_NODE (scope_list))
3655 parse_error_context
3656 (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",
3657 IDENTIFIER_POINTER (raw_name));
3658 return 1;
3660 return 0;
3663 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3664 we remember ENCLOSING and SUPER. */
3666 static tree
3667 resolve_inner_class (htab_t circularity_hash, tree cl, tree *enclosing,
3668 tree *super, tree class_type)
3670 tree local_enclosing = *enclosing;
3671 tree local_super = NULL_TREE;
3673 while (local_enclosing)
3675 tree intermediate, decl;
3677 *htab_find_slot (circularity_hash, local_enclosing, INSERT) =
3678 local_enclosing;
3680 if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3681 return decl;
3683 intermediate = local_enclosing;
3684 /* Explore enclosing contexts. */
3685 while (INNER_CLASS_DECL_P (intermediate))
3687 intermediate = DECL_CONTEXT (intermediate);
3688 if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3689 return decl;
3692 /* Now go to the upper classes, bail out if necessary. We will
3693 analyze the returned SUPER and act accordingly (see
3694 do_resolve_class). */
3695 if (JPRIMITIVE_TYPE_P (TREE_TYPE (local_enclosing))
3696 || TREE_TYPE (local_enclosing) == void_type_node)
3698 parse_error_context (cl, "Qualifier must be a reference");
3699 local_enclosing = NULL_TREE;
3700 break;
3702 local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3703 if (!local_super || local_super == object_type_node)
3704 break;
3706 if (TREE_CODE (local_super) == POINTER_TYPE)
3707 local_super = do_resolve_class (NULL, NULL, local_super, NULL, NULL);
3708 else
3709 local_super = TYPE_NAME (local_super);
3711 /* We may not have checked for circular inheritance yet, so do so
3712 here to prevent an infinite loop. */
3713 if (htab_find (circularity_hash, local_super) != NULL)
3715 if (!cl)
3716 cl = lookup_cl (local_enclosing);
3718 parse_error_context
3719 (cl, "Cyclic inheritance involving %s",
3720 IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3721 local_enclosing = NULL_TREE;
3723 else
3724 local_enclosing = local_super;
3727 /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3728 *super = local_super;
3729 *enclosing = local_enclosing;
3731 return NULL_TREE;
3734 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3735 qualified. */
3737 static tree
3738 find_as_inner_class (tree enclosing, tree name, tree cl)
3740 tree qual, to_return;
3741 if (!enclosing)
3742 return NULL_TREE;
3744 name = TYPE_NAME (name);
3746 /* First search: within the scope of `enclosing', search for name */
3747 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3748 qual = EXPR_WFL_QUALIFICATION (cl);
3749 else if (cl)
3750 qual = build_tree_list (cl, NULL_TREE);
3751 else
3752 qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3754 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3755 return to_return;
3757 /* We're dealing with a qualified name. Try to resolve thing until
3758 we get something that is an enclosing class. */
3759 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3761 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3763 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3764 qual = TREE_CHAIN (qual))
3766 acc = merge_qualified_name (acc,
3767 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3768 BUILD_PTR_FROM_NAME (ptr, acc);
3769 decl = do_resolve_class (NULL_TREE, NULL_TREE, ptr, NULL_TREE, cl);
3772 /* A NULL qual and a decl means that the search ended
3773 successfully?!? We have to do something then. FIXME */
3775 if (decl)
3776 enclosing = decl;
3777 else
3778 qual = EXPR_WFL_QUALIFICATION (cl);
3780 /* Otherwise, create a qual for the other part of the resolution. */
3781 else
3782 qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3784 return find_as_inner_class_do (qual, enclosing);
3787 /* We go inside the list of sub classes and try to find a way
3788 through. */
3790 static tree
3791 find_as_inner_class_do (tree qual, tree enclosing)
3793 if (!qual)
3794 return NULL_TREE;
3796 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3798 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3799 tree next_enclosing = NULL_TREE;
3800 tree inner_list;
3802 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3803 inner_list; inner_list = TREE_CHAIN (inner_list))
3805 if (TREE_VALUE (inner_list) == name_to_match)
3807 next_enclosing = TREE_PURPOSE (inner_list);
3808 break;
3811 enclosing = next_enclosing;
3814 return (!qual && enclosing ? enclosing : NULL_TREE);
3817 static void
3818 link_nested_class_to_enclosing (void)
3820 if (GET_ENCLOSING_CPC ())
3822 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3823 DECL_INNER_CLASS_LIST (enclosing) =
3824 tree_cons (GET_CPC (), GET_CPC_UN (),
3825 DECL_INNER_CLASS_LIST (enclosing));
3829 static tree
3830 maybe_make_nested_class_name (tree name)
3832 tree id = NULL_TREE;
3834 if (CPC_INNER_P ())
3836 /* If we're in a function, we must append a number to create the
3837 nested class name. However, we don't do this if the class we
3838 are constructing is anonymous, because in that case we'll
3839 already have a number as the class name. */
3840 if (! make_nested_class_name (GET_CPC_LIST ())
3841 && current_function_decl != NULL_TREE
3842 && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3844 char buf[10];
3845 sprintf (buf, "%d", anonymous_class_counter);
3846 ++anonymous_class_counter;
3847 obstack_grow (&temporary_obstack, buf, strlen (buf));
3848 obstack_1grow (&temporary_obstack, '$');
3850 obstack_grow0 (&temporary_obstack,
3851 IDENTIFIER_POINTER (name),
3852 IDENTIFIER_LENGTH (name));
3853 id = get_identifier (obstack_finish (&temporary_obstack));
3854 if (ctxp->package)
3855 QUALIFIED_P (id) = 1;
3857 return id;
3860 /* If DECL is NULL, create and push a new DECL, record the current
3861 line CL and do other maintenance things. */
3863 static tree
3864 maybe_create_class_interface_decl (tree decl, tree raw_name,
3865 tree qualified_name, tree cl)
3867 if (!decl)
3868 decl = push_class (make_class (), qualified_name);
3870 /* Take care of the file and line business */
3871 #ifdef USE_MAPPED_LOCATION
3872 DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (cl);
3873 #else
3874 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3875 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3876 #endif
3877 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3878 CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3879 #ifdef USE_MAPPED_LOCATION
3881 tree tmp = maybe_get_identifier (EXPR_FILENAME (cl));
3882 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3883 tmp && IS_A_COMMAND_LINE_FILENAME_P (tmp);
3885 #else
3886 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3887 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3888 #endif
3890 PUSH_CPC (decl, raw_name);
3891 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3893 /* Link the declaration to the already seen ones */
3894 TREE_CHAIN (decl) = ctxp->class_list;
3895 ctxp->class_list = decl;
3897 /* Create a new nodes in the global lists */
3898 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3899 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3901 /* Install a new dependency list element */
3902 create_jdep_list (ctxp);
3904 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3905 IDENTIFIER_POINTER (qualified_name)));
3906 return decl;
3909 static void
3910 add_superinterfaces (tree decl, tree interface_list)
3912 tree node;
3913 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3914 takes care of ensuring that:
3915 - This is an accessible interface type,
3916 - Circularity detection.
3917 parser_add_interface is then called. If present but not defined,
3918 the check operation is delayed until the super interface gets
3919 defined. */
3920 for (node = interface_list; node; node = TREE_CHAIN (node))
3922 tree current = TREE_PURPOSE (node);
3923 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3924 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3926 if (!parser_check_super_interface (idecl, decl, current))
3927 parser_add_interface (decl, idecl, current);
3929 else
3930 register_incomplete_type (JDEP_INTERFACE,
3931 current, decl, NULL_TREE);
3935 /* Create an interface in pass1 and return its decl. Return the
3936 interface's decl in pass 2. */
3938 static tree
3939 create_interface (int flags, tree id, tree super)
3941 tree raw_name = EXPR_WFL_NODE (id);
3942 tree q_name = parser_qualified_classname (raw_name);
3943 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3945 /* Certain syntax errors are making SUPER be like ID. Avoid this
3946 case. */
3947 if (ctxp->class_err && id == super)
3948 super = NULL;
3950 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3952 /* Basic checks: scope, redefinition, modifiers */
3953 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3955 PUSH_ERROR ();
3956 return NULL_TREE;
3959 /* Suspend the current parsing context if we're parsing an inner
3960 interface */
3961 if (CPC_INNER_P ())
3963 java_parser_context_suspend ();
3964 /* Interface members are public. */
3965 if (CLASS_INTERFACE (GET_CPC ()))
3966 flags |= ACC_PUBLIC;
3969 /* Push a new context for (static) initialized upon declaration fields */
3970 java_parser_context_push_initialized_field ();
3972 /* Interface modifiers check
3973 - public/abstract allowed (already done at that point)
3974 - abstract is obsolete (comes first, it's a warning, or should be)
3975 - Can't use twice the same (checked in the modifier rule) */
3976 if ((flags & ACC_ABSTRACT) && flag_redundant)
3977 parse_warning_context
3978 (MODIFIER_WFL (ABSTRACT_TK),
3979 "Redundant use of %<abstract%> modifier. Interface %qs is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3981 /* Create a new decl if DECL is NULL, otherwise fix it */
3982 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3984 /* Interfaces are always abstract. */
3985 flags |= ACC_ABSTRACT;
3987 /* Inner interfaces are always static. */
3988 if (INNER_CLASS_DECL_P (decl))
3989 flags |= ACC_STATIC;
3991 /* Set super info and mark the class a complete */
3992 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3993 object_type_node, ctxp->interface_number);
3994 ctxp->interface_number = 0;
3995 CLASS_COMPLETE_P (decl) = 1;
3996 add_superinterfaces (decl, super);
3998 /* Eventually sets the @deprecated tag flag */
3999 CHECK_DEPRECATED (decl);
4001 return decl;
4004 /* Patch anonymous class CLASS, by either extending or implementing
4005 DEP. */
4007 static void
4008 patch_anonymous_class (tree type_decl, tree class_decl, tree wfl)
4010 tree class = TREE_TYPE (class_decl);
4011 tree type = TREE_TYPE (type_decl);
4012 tree binfo = TYPE_BINFO (class);
4014 /* If it's an interface, implement it */
4015 if (CLASS_INTERFACE (type_decl))
4017 if (parser_check_super_interface (type_decl, class_decl, wfl))
4018 return;
4020 if (!VEC_space (tree, BINFO_BASE_BINFOS (binfo), 1))
4022 /* Extend the binfo - by reallocating and copying it. */
4023 tree new_binfo;
4024 tree base_binfo;
4025 int i;
4027 new_binfo = make_tree_binfo ((BINFO_N_BASE_BINFOS (binfo) + 1) * 2);
4028 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4029 BINFO_BASE_APPEND (new_binfo, base_binfo);
4030 CLASS_HAS_SUPER_FLAG (new_binfo) = CLASS_HAS_SUPER_FLAG (binfo);
4031 BINFO_VTABLE (new_binfo) = BINFO_VTABLE (binfo);
4032 TYPE_BINFO (class) = new_binfo;
4035 /* And add the interface */
4036 parser_add_interface (class_decl, type_decl, wfl);
4038 /* Otherwise, it's a type we want to extend */
4039 else
4041 if (parser_check_super (type_decl, class_decl, wfl))
4042 return;
4043 BINFO_TYPE (BINFO_BASE_BINFO (binfo, 0)) = type;
4047 /* Create an anonymous class which extends/implements TYPE_NAME, and return
4048 its decl. */
4050 static tree
4051 create_anonymous_class (tree type_name)
4053 char buffer [80];
4054 tree super = NULL_TREE, itf = NULL_TREE;
4055 tree id, type_decl, class;
4057 /* The unqualified name of the anonymous class. It's just a number. */
4058 sprintf (buffer, "%d", anonymous_class_counter++);
4059 id = build_wfl_node (get_identifier (buffer));
4060 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL (type_name);
4062 /* We know about the type to extend/implement. We go ahead */
4063 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
4065 /* Create a class which either implements on extends the designated
4066 class. The class bears an inaccessible name. */
4067 if (CLASS_INTERFACE (type_decl))
4069 /* It's OK to modify it here. It's been already used and
4070 shouldn't be reused */
4071 ctxp->interface_number = 1;
4072 /* Interfaces should presented as a list of WFLs */
4073 itf = build_tree_list (type_name, NULL_TREE);
4075 else
4076 super = type_name;
4079 class = create_class (ACC_FINAL, id, super, itf);
4081 /* We didn't know anything about the stuff. We register a dependence. */
4082 if (!type_decl)
4083 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
4085 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
4086 return class;
4089 /* Create a class in pass1 and return its decl. Return class
4090 interface's decl in pass 2. */
4092 static tree
4093 create_class (int flags, tree id, tree super, tree interfaces)
4095 tree raw_name = EXPR_WFL_NODE (id);
4096 tree class_id, decl;
4097 tree super_decl_type;
4099 /* Certain syntax errors are making SUPER be like ID. Avoid this
4100 case. */
4101 if (ctxp->class_err && id == super)
4102 super = NULL;
4104 class_id = parser_qualified_classname (raw_name);
4105 decl = IDENTIFIER_CLASS_VALUE (class_id);
4106 EXPR_WFL_NODE (id) = class_id;
4108 /* Basic check: scope, redefinition, modifiers */
4109 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
4111 PUSH_ERROR ();
4112 return NULL_TREE;
4115 /* Suspend the current parsing context if we're parsing an inner
4116 class or an anonymous class. */
4117 if (CPC_INNER_P ())
4119 java_parser_context_suspend ();
4120 /* Interface members are public. */
4121 if (CLASS_INTERFACE (GET_CPC ()))
4122 flags |= ACC_PUBLIC;
4125 /* Push a new context for (static) initialized upon declaration fields */
4126 java_parser_context_push_initialized_field ();
4128 /* Class modifier check:
4129 - Allowed modifier (already done at that point)
4130 - abstract AND final forbidden
4131 - Public classes defined in the correct file */
4132 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4133 parse_error_context
4134 (id, "Class %qs can't be declared both abstract and final",
4135 IDENTIFIER_POINTER (raw_name));
4137 /* Create a new decl if DECL is NULL, otherwise fix it */
4138 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4140 /* If SUPER exists, use it, otherwise use Object */
4141 if (super)
4143 /* java.lang.Object can't extend anything. */
4144 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4146 parse_error_context (id, "%<java.lang.Object%> can't extend anything");
4147 return NULL_TREE;
4150 super_decl_type =
4151 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4153 else if (TREE_TYPE (decl) != object_type_node)
4154 super_decl_type = object_type_node;
4155 /* We're defining java.lang.Object */
4156 else
4157 super_decl_type = NULL_TREE;
4159 /* A class nested in an interface is implicitly static. */
4160 if (INNER_CLASS_DECL_P (decl)
4161 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4163 flags |= ACC_STATIC;
4166 /* Set super info and mark the class as complete. */
4167 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4168 ctxp->interface_number);
4169 ctxp->interface_number = 0;
4170 CLASS_COMPLETE_P (decl) = 1;
4171 add_superinterfaces (decl, interfaces);
4173 /* TYPE_VFIELD' is a compiler-generated field used to point to
4174 virtual function tables. In gcj, every class has a common base
4175 virtual function table in java.lang.object. */
4176 TYPE_VFIELD (TREE_TYPE (decl)) = TYPE_VFIELD (object_type_node);
4178 /* We keep the compilation unit imports in the class so that
4179 they can be used later to resolve type dependencies that
4180 aren't necessary to solve now. */
4181 TYPE_IMPORT_LIST (TREE_TYPE (decl)) = ctxp->import_list;
4182 TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (decl)) = ctxp->import_demand_list;
4184 /* Add the private this$<n> field, Replicate final locals still in
4185 scope as private final fields mangled like val$<local_name>.
4186 This does not occur for top level (static) inner classes. */
4187 if (PURE_INNER_CLASS_DECL_P (decl))
4188 add_inner_class_fields (decl, current_function_decl);
4190 /* Eventually sets the @deprecated tag flag */
4191 CHECK_DEPRECATED (decl);
4193 /* Reset the anonymous class counter when declaring non inner classes */
4194 if (!INNER_CLASS_DECL_P (decl))
4195 anonymous_class_counter = 1;
4197 return decl;
4200 /* End a class declaration: register the statements used to create
4201 finit$ and <clinit>, pop the current class and resume the prior
4202 parser context if necessary. */
4204 static void
4205 end_class_declaration (int resume)
4207 /* If an error occurred, context weren't pushed and won't need to be
4208 popped by a resume. */
4209 int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4211 if (GET_CPC () != error_mark_node)
4212 dump_java_tree (TDI_class, GET_CPC ());
4214 java_parser_context_pop_initialized_field ();
4215 POP_CPC ();
4216 if (resume && no_error_occurred)
4217 java_parser_context_resume ();
4219 /* We're ending a class declaration, this is a good time to reset
4220 the interface cout. Note that might have been already done in
4221 create_interface, but if at that time an inner class was being
4222 dealt with, the interface count was reset in a context created
4223 for the sake of handling inner classes declaration. */
4224 ctxp->interface_number = 0;
4227 static void
4228 add_inner_class_fields (tree class_decl, tree fct_decl)
4230 tree block, marker, f;
4232 f = add_field (TREE_TYPE (class_decl),
4233 build_current_thisn (TREE_TYPE (class_decl)),
4234 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4235 ACC_PRIVATE);
4236 FIELD_THISN (f) = 1;
4238 if (!fct_decl)
4239 return;
4241 for (block = GET_CURRENT_BLOCK (fct_decl);
4242 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4244 tree decl;
4245 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4247 tree name, pname;
4248 tree wfl, init, list;
4250 /* Avoid non final arguments. */
4251 if (!LOCAL_FINAL_P (decl))
4252 continue;
4254 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4255 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4256 wfl = build_wfl_node (name);
4257 init = build_wfl_node (pname);
4258 /* Build an initialization for the field: it will be
4259 initialized by a parameter added to finit$, bearing a
4260 mangled name of the field itself (param$<n>.) The
4261 parameter is provided to finit$ by the constructor
4262 invoking it (hence the constructor will also feature a
4263 hidden parameter, set to the value of the outer context
4264 local at the time the inner class is created.)
4266 Note: we take into account all possible locals that can
4267 be accessed by the inner class. It's actually not trivial
4268 to minimize these aliases down to the ones really
4269 used. One way to do that would be to expand all regular
4270 methods first, then finit$ to get a picture of what's
4271 used. It works with the exception that we would have to
4272 go back on all constructor invoked in regular methods to
4273 have their invocation reworked (to include the right amount
4274 of alias initializer parameters.)
4276 The only real way around, I think, is a first pass to
4277 identify locals really used in the inner class. We leave
4278 the flag FIELD_LOCAL_ALIAS_USED around for that future
4279 use.
4281 On the other hand, it only affect local inner classes,
4282 whose constructors (and finit$ call) will be featuring
4283 unnecessary arguments. It's easy for a developer to keep
4284 this number of parameter down by using the `final'
4285 keyword only when necessary. For the time being, we can
4286 issue a warning on unnecessary finals. FIXME */
4287 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4288 wfl, init);
4290 /* Register the field. The TREE_LIST holding the part
4291 initialized/initializer will be marked ARG_FINAL_P so
4292 that the created field can be marked
4293 FIELD_LOCAL_ALIAS. */
4294 list = build_tree_list (wfl, init);
4295 ARG_FINAL_P (list) = 1;
4296 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4300 if (!CPC_INITIALIZER_STMT (ctxp))
4301 return;
4303 /* If we ever registered an alias field, insert and marker to
4304 remember where the list ends. The second part of the list (the one
4305 featuring initialized fields) so it can be later reversed to
4306 enforce 8.5. The marker will be removed during that operation. */
4307 marker = build_tree_list (NULL_TREE, NULL_TREE);
4308 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4309 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4312 /* Can't use lookup_field () since we don't want to load the class and
4313 can't set the CLASS_LOADED_P flag */
4315 static tree
4316 find_field (tree class, tree name)
4318 tree decl;
4319 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4321 if (DECL_NAME (decl) == name)
4322 return decl;
4324 return NULL_TREE;
4327 /* Wrap around lookup_field that doesn't potentially upset the value
4328 of CLASS */
4330 static tree
4331 lookup_field_wrapper (tree class, tree name)
4333 tree type = class;
4334 tree decl = NULL_TREE;
4335 java_parser_context_save_global ();
4337 /* Last chance: if we're within the context of an inner class, we
4338 might be trying to access a local variable defined in an outer
4339 context. We try to look for it now. */
4340 if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4342 tree new_name;
4343 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4344 decl = lookup_field (&type, new_name);
4345 if (decl && decl != error_mark_node)
4346 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4348 if (!decl || decl == error_mark_node)
4350 type = class;
4351 decl = lookup_field (&type, name);
4354 /* If the field still hasn't been found, try the next enclosing context. */
4355 if (!decl && INNER_CLASS_TYPE_P (class))
4357 tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4358 decl = lookup_field_wrapper (outer_type, name);
4361 java_parser_context_restore_global ();
4362 return decl == error_mark_node ? NULL : decl;
4365 /* Find duplicate field within the same class declarations and report
4366 the error. Returns 1 if a duplicated field was found, 0
4367 otherwise. */
4369 static int
4370 duplicate_declaration_error_p (tree new_field_name, tree new_type, tree cl)
4372 /* This might be modified to work with method decl as well */
4373 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4374 if (decl)
4376 char *t1 = xstrdup (purify_type_name
4377 ((TREE_CODE (new_type) == POINTER_TYPE
4378 && TREE_TYPE (new_type) == NULL_TREE) ?
4379 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4380 lang_printable_name (new_type, 1)));
4381 /* The type may not have been completed by the time we report
4382 the error */
4383 char *t2 = xstrdup (purify_type_name
4384 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4385 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4386 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4387 lang_printable_name (TREE_TYPE (decl), 1)));
4388 parse_error_context
4389 (cl, "Duplicate variable declaration: %<%s %s%> was %<%s %s%> (%s:%d)",
4390 t1, IDENTIFIER_POINTER (new_field_name),
4391 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4392 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4393 free (t1);
4394 free (t2);
4395 return 1;
4397 return 0;
4400 /* Field registration routine. If TYPE doesn't exist, field
4401 declarations are linked to the undefined TYPE dependency list, to
4402 be later resolved in java_complete_class () */
4404 static void
4405 register_fields (int flags, tree type, tree variable_list)
4407 tree current, saved_type;
4408 tree class_type = NULL_TREE;
4409 location_t saved_location = input_location;
4410 int must_chain = 0;
4411 tree wfl = NULL_TREE;
4413 if (GET_CPC ())
4414 class_type = TREE_TYPE (GET_CPC ());
4416 if (!class_type || class_type == error_mark_node)
4417 return;
4419 /* If we're adding fields to interfaces, those fields are public,
4420 static, final */
4421 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4423 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4424 flags, ACC_PUBLIC, "interface field(s)");
4425 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4426 flags, ACC_STATIC, "interface field(s)");
4427 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4428 flags, ACC_FINAL, "interface field(s)");
4429 check_modifiers ("Illegal interface member modifier %qs", flags,
4430 INTERFACE_FIELD_MODIFIERS);
4431 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4434 /* Obtain a suitable type for resolution, if necessary */
4435 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4437 /* If TYPE is fully resolved and we don't have a reference, make one */
4438 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4440 for (current = variable_list, saved_type = type; current;
4441 current = TREE_CHAIN (current), type = saved_type)
4443 tree real_type;
4444 tree field_decl;
4445 tree cl = TREE_PURPOSE (current);
4446 tree init = TREE_VALUE (current);
4447 tree current_name = EXPR_WFL_NODE (cl);
4449 /* Can't declare non-final static fields in inner classes */
4450 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4451 && !(flags & ACC_FINAL))
4452 parse_error_context
4453 (cl, "Field %qs can't be static in inner class %qs unless it is final",
4454 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4455 lang_printable_name (class_type, 0));
4457 /* Process NAME, as it may specify extra dimension(s) for it */
4458 type = build_array_from_name (type, wfl, current_name, &current_name);
4460 /* Type adjustment. We may have just readjusted TYPE because
4461 the variable specified more dimensions. Make sure we have
4462 a reference if we can and don't have one already. Also
4463 change the name if we have an init. */
4464 if (type != saved_type)
4466 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4467 if (init)
4468 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4471 real_type = GET_REAL_TYPE (type);
4472 /* Check for redeclarations */
4473 if (duplicate_declaration_error_p (current_name, real_type, cl))
4474 continue;
4476 /* Set input_line to the line the field was found and create a
4477 declaration for it. Eventually sets the @deprecated tag flag. */
4478 #ifdef USE_MAPPED_LOCATION
4479 input_location = EXPR_LOCATION (cl);
4480 #else
4481 input_line = EXPR_WFL_LINENO (cl);
4482 #endif
4483 field_decl = add_field (class_type, current_name, real_type, flags);
4484 CHECK_DEPRECATED_NO_RESET (field_decl);
4486 /* If the field denotes a final instance variable, then we
4487 allocate a LANG_DECL_SPECIFIC part to keep track of its
4488 initialization. We also mark whether the field was
4489 initialized upon its declaration. We don't do that if the
4490 created field is an alias to a final local. */
4491 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4493 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4494 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4497 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4498 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4499 hide parameters to this inner class finit$ and
4500 constructors. It also means that the field isn't final per
4501 say. */
4502 if (ARG_FINAL_P (current))
4504 FIELD_LOCAL_ALIAS (field_decl) = 1;
4505 FIELD_FINAL (field_decl) = 0;
4508 /* Check if we must chain. */
4509 if (must_chain)
4510 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4512 /* If we have an initialization value tied to the field */
4513 if (init)
4515 /* The field is declared static */
4516 if (flags & ACC_STATIC)
4518 /* We include the field and its initialization part into
4519 a list used to generate <clinit>. After <clinit> is
4520 walked, field initializations will be processed and
4521 fields initialized with known constants will be taken
4522 out of <clinit> and have their DECL_INITIAL set
4523 appropriately. */
4524 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4525 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4526 if (TREE_OPERAND (init, 1)
4527 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4528 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4530 /* A non-static field declared with an immediate initialization is
4531 to be initialized in <init>, if any. This field is remembered
4532 to be processed at the time of the generation of <init>. */
4533 else
4535 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4536 SET_CPC_INITIALIZER_STMT (ctxp, init);
4538 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4539 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4543 CLEAR_DEPRECATED;
4544 input_location = saved_location;
4547 /* Generate finit$, using the list of initialized fields to populate
4548 its body. finit$'s parameter(s) list is adjusted to include the
4549 one(s) used to initialized the field(s) caching outer context
4550 local(s). */
4552 static tree
4553 generate_finit (tree class_type)
4555 int count = 0;
4556 tree list = TYPE_FINIT_STMT_LIST (class_type);
4557 tree mdecl, current, parms;
4559 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4560 class_type, NULL_TREE,
4561 &count);
4562 CRAFTED_PARAM_LIST_FIXUP (parms);
4563 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4564 finit_identifier_node, parms);
4565 fix_method_argument_names (parms, mdecl);
4566 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4567 mdecl, NULL_TREE);
4568 DECL_FUNCTION_NAP (mdecl) = count;
4569 start_artificial_method_body (mdecl);
4571 for (current = list; current; current = TREE_CHAIN (current))
4572 java_method_add_stmt (mdecl,
4573 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4574 current));
4575 end_artificial_method_body (mdecl);
4576 return mdecl;
4579 /* Generate a function to run the instance initialization code. The
4580 private method is called `instinit$'. Unless we're dealing with an
4581 anonymous class, we determine whether all ctors of CLASS_TYPE
4582 declare a checked exception in their `throws' clause in order to
4583 see whether it's necessary to encapsulate the instance initializer
4584 statements in a try/catch/rethrow sequence. */
4586 static tree
4587 generate_instinit (tree class_type)
4589 tree current;
4590 tree compound = NULL_TREE;
4591 tree parms = tree_cons (this_identifier_node,
4592 build_pointer_type (class_type), end_params_node);
4593 tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4594 void_type_node,
4595 instinit_identifier_node, parms);
4597 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4598 mdecl, NULL_TREE);
4600 /* Gather all the statements in a compound */
4601 for (current = TYPE_II_STMT_LIST (class_type);
4602 current; current = TREE_CHAIN (current))
4603 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4605 /* We need to encapsulate COMPOUND by a try/catch statement to
4606 rethrow exceptions that might occur in the instance initializer.
4607 We do that only if all ctors of CLASS_TYPE are set to catch a
4608 checked exception. This doesn't apply to anonymous classes (since
4609 they don't have declared ctors.) */
4610 if (!ANONYMOUS_CLASS_P (class_type) &&
4611 ctors_unchecked_throws_clause_p (class_type))
4613 compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4614 build1 (THROW_EXPR, NULL_TREE,
4615 build_wfl_node (wpv_id)));
4616 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4617 exception_type_node);
4620 start_artificial_method_body (mdecl);
4621 java_method_add_stmt (mdecl, compound);
4622 end_artificial_method_body (mdecl);
4624 return mdecl;
4627 /* FIXME */
4628 static tree
4629 build_instinit_invocation (tree class_type)
4631 tree to_return = NULL_TREE;
4633 if (TYPE_II_STMT_LIST (class_type))
4635 tree parm = build_tree_list (NULL_TREE,
4636 build_wfl_node (this_identifier_node));
4637 to_return =
4638 build_method_invocation (build_wfl_node (instinit_identifier_node),
4639 parm);
4641 return to_return;
4644 /* Shared across method_declarator and method_header to remember the
4645 patch stage that was reached during the declaration of the method.
4646 A method DECL is built differently is there is no patch
4647 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4648 pending on the currently defined method. */
4650 static int patch_stage;
4652 /* Check the method declaration and add the method to its current
4653 class. If the argument list is known to contain incomplete types,
4654 the method is partially added and the registration will be resume
4655 once the method arguments resolved. If TYPE is NULL, we're dealing
4656 with a constructor. */
4658 static tree
4659 method_header (int flags, tree type, tree mdecl, tree throws)
4661 tree type_wfl = NULL_TREE;
4662 tree meth_name = NULL_TREE;
4663 tree current, orig_arg, this_class = NULL;
4664 tree id, meth;
4665 location_t saved_location;
4666 int constructor_ok = 0, must_chain;
4667 int count;
4669 if (mdecl == error_mark_node)
4670 return error_mark_node;
4671 meth = TREE_VALUE (mdecl);
4672 id = TREE_PURPOSE (mdecl);
4674 check_modifiers_consistency (flags);
4676 if (GET_CPC ())
4677 this_class = TREE_TYPE (GET_CPC ());
4679 if (!this_class || this_class == error_mark_node)
4680 return NULL_TREE;
4682 /* There are some forbidden modifiers for an abstract method and its
4683 class must be abstract as well. */
4684 if (type && (flags & ACC_ABSTRACT))
4686 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4687 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4688 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4689 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4690 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4691 ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4692 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4693 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4694 parse_error_context
4695 (id,
4696 "Class %qs must be declared abstract to define abstract method %qs",
4697 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4698 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4701 /* A native method can't be strictfp. */
4702 if ((flags & ACC_NATIVE) && (flags & ACC_STRICT))
4703 parse_error_context (id, "native method %qs can't be strictfp",
4704 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4705 /* No such thing as a transient or volatile method. */
4706 if ((flags & ACC_TRANSIENT))
4707 parse_error_context (id, "method %qs can't be transient",
4708 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4709 if ((flags & ACC_VOLATILE))
4710 parse_error_context (id, "method %qs can't be volatile",
4711 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4713 /* Things to be checked when declaring a constructor */
4714 if (!type)
4716 int ec = java_error_count;
4717 /* 8.6: Constructor declarations: we might be trying to define a
4718 method without specifying a return type. */
4719 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4720 parse_error_context
4721 (id, "Invalid method declaration, return type required");
4722 /* 8.6.3: Constructor modifiers */
4723 else
4725 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4726 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4727 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4728 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4729 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4730 JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4732 /* If we found error here, we don't consider it's OK to tread
4733 the method definition as a constructor, for the rest of this
4734 function */
4735 if (ec == java_error_count)
4736 constructor_ok = 1;
4739 /* Method declared within the scope of an interface are implicitly
4740 abstract and public. Conflicts with other erroneously provided
4741 modifiers are checked right after. */
4743 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4745 /* If FLAGS isn't set because of a modifier, turn the
4746 corresponding modifier WFL to NULL so we issue a warning on
4747 the obsolete use of the modifier */
4748 if (!(flags & ACC_PUBLIC))
4749 MODIFIER_WFL (PUBLIC_TK) = NULL;
4750 if (!(flags & ACC_ABSTRACT))
4751 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4752 flags |= ACC_PUBLIC;
4753 flags |= ACC_ABSTRACT;
4756 /* Inner class can't declare static methods */
4757 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4759 parse_error_context
4760 (id, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
4761 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4762 lang_printable_name (this_class, 0));
4765 /* Modifiers context reset moved up, so abstract method declaration
4766 modifiers can be later checked. */
4768 /* Set constructor returned type to void and method name to <init>,
4769 unless we found an error identifier the constructor (in which
4770 case we retain the original name) */
4771 if (!type)
4773 type = void_type_node;
4774 if (constructor_ok)
4775 meth_name = init_identifier_node;
4777 else
4778 meth_name = EXPR_WFL_NODE (id);
4780 /* Do the returned type resolution and registration if necessary */
4781 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4783 if (meth_name)
4784 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4785 EXPR_WFL_NODE (id) = meth_name;
4786 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4788 if (must_chain)
4790 patch_stage = JDEP_METHOD_RETURN;
4791 register_incomplete_type (patch_stage, type_wfl, id, type);
4792 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4794 else
4795 TREE_TYPE (meth) = type;
4797 saved_location = input_location;
4798 /* When defining an abstract or interface method, the curly
4799 bracket at level 1 doesn't exist because there is no function
4800 body */
4801 #ifdef USE_MAPPED_LOCATION
4802 input_location = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4803 EXPR_LOCATION (id));
4804 #else
4805 input_line = (ctxp->first_ccb_indent1 ? (int) ctxp->first_ccb_indent1 :
4806 EXPR_WFL_LINENO (id));
4807 #endif
4809 /* Remember the original argument list */
4810 orig_arg = TYPE_ARG_TYPES (meth);
4812 if (patch_stage) /* includes ret type and/or all args */
4814 jdep *jdep;
4815 meth = add_method_1 (this_class, flags, meth_name, meth);
4816 /* Patch for the return type */
4817 if (patch_stage == JDEP_METHOD_RETURN)
4819 jdep = CLASSD_LAST (ctxp->classd_list);
4820 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4822 /* This is the stop JDEP. METH allows the function's signature
4823 to be computed. */
4824 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4826 else
4827 meth = add_method (this_class, flags, meth_name,
4828 build_java_signature (meth));
4830 /* Remember final parameters */
4831 MARK_FINAL_PARMS (meth, orig_arg);
4833 /* Fix the method argument list so we have the argument name
4834 information */
4835 fix_method_argument_names (orig_arg, meth);
4837 /* Register the parameter number and re-install the current line
4838 number */
4839 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4840 input_location = saved_location;
4842 /* Register exception specified by the `throws' keyword for
4843 resolution and set the method decl appropriate field to the list.
4844 Note: the grammar ensures that what we get here are class
4845 types. */
4846 if (throws)
4848 throws = nreverse (throws);
4849 for (current = throws; current; current = TREE_CHAIN (current))
4851 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4852 NULL_TREE, NULL_TREE);
4853 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4854 &TREE_VALUE (current);
4856 DECL_FUNCTION_THROWS (meth) = throws;
4859 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4860 DECL_FUNCTION_WFL (meth) = id;
4862 /* Set the flag if we correctly processed a constructor */
4863 if (constructor_ok)
4865 DECL_CONSTRUCTOR_P (meth) = 1;
4866 /* Compute and store the number of artificial parameters declared
4867 for this constructor */
4868 for (count = 0, current = TYPE_FIELDS (this_class); current;
4869 current = TREE_CHAIN (current))
4870 if (FIELD_LOCAL_ALIAS (current))
4871 count++;
4872 DECL_FUNCTION_NAP (meth) = count;
4875 /* Eventually set the @deprecated tag flag */
4876 CHECK_DEPRECATED (meth);
4878 return meth;
4881 static void
4882 fix_method_argument_names (tree orig_arg, tree meth)
4884 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4885 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4887 TREE_PURPOSE (arg) = this_identifier_node;
4888 arg = TREE_CHAIN (arg);
4890 while (orig_arg != end_params_node)
4892 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4893 orig_arg = TREE_CHAIN (orig_arg);
4894 arg = TREE_CHAIN (arg);
4898 /* Complete the method declaration with METHOD_BODY. */
4900 static void
4901 finish_method_declaration (tree method_body)
4903 int flags;
4905 if (!current_function_decl)
4906 return;
4908 flags = get_access_flags_from_decl (current_function_decl);
4910 /* 8.4.5 Method Body */
4911 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4913 tree name = DECL_NAME (current_function_decl);
4914 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4915 "%s method %qs can't have a body defined",
4916 (METHOD_NATIVE (current_function_decl) ?
4917 "Native" : "Abstract"),
4918 IDENTIFIER_POINTER (name));
4919 method_body = NULL_TREE;
4921 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4923 tree name = DECL_NAME (current_function_decl);
4924 parse_error_context
4925 (DECL_FUNCTION_WFL (current_function_decl),
4926 "Non native and non abstract method %qs must have a body defined",
4927 IDENTIFIER_POINTER (name));
4928 method_body = NULL_TREE;
4931 if (flag_emit_class_files && method_body
4932 && TREE_CODE (method_body) == NOP_EXPR
4933 && TREE_TYPE (current_function_decl)
4934 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4935 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4937 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4938 maybe_absorb_scoping_blocks ();
4939 /* Exit function's body */
4940 exit_block ();
4941 /* Merge last line of the function with first line, directly in the
4942 function decl. It will be used to emit correct debug info. */
4943 DECL_FUNCTION_LAST_LINE (current_function_decl) = ctxp->last_ccb_indent1;
4945 /* Since function's argument's list are shared, reset the
4946 ARG_FINAL_P parameter that might have been set on some of this
4947 function parameters. */
4948 UNMARK_FINAL_PARMS (current_function_decl);
4950 /* So we don't have an irrelevant function declaration context for
4951 the next static block we'll see. */
4952 current_function_decl = NULL_TREE;
4955 /* Build a an error message for constructor circularity errors. */
4957 static char *
4958 constructor_circularity_msg (tree from, tree to)
4960 static char string [4096];
4961 char *t = xstrdup (lang_printable_name (from, 2));
4962 sprintf (string, "'%s' invokes '%s'", t, lang_printable_name (to, 2));
4963 free (t);
4964 return string;
4967 /* Verify a circular call to METH. Return 1 if an error is found, 0
4968 otherwise. */
4970 static GTY(()) tree vcc_list;
4971 static int
4972 verify_constructor_circularity (tree meth, tree current)
4974 tree c;
4976 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4978 if (TREE_VALUE (c) == meth)
4980 char *t;
4981 if (vcc_list)
4983 tree liste;
4984 vcc_list = nreverse (vcc_list);
4985 for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
4987 parse_error_context
4988 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4989 constructor_circularity_msg
4990 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4991 java_error_count--;
4994 t = xstrdup (lang_printable_name (meth, 2));
4995 parse_error_context (TREE_PURPOSE (c),
4996 "%s: recursive invocation of constructor %qs",
4997 constructor_circularity_msg (current, meth), t);
4998 free (t);
4999 vcc_list = NULL_TREE;
5000 return 1;
5003 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5005 vcc_list = tree_cons (c, current, vcc_list);
5006 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
5007 return 1;
5008 vcc_list = TREE_CHAIN (vcc_list);
5010 return 0;
5013 /* Check modifiers that can be declared but exclusively */
5015 static void
5016 check_modifiers_consistency (int flags)
5018 int acc_count = 0;
5019 tree cl = NULL_TREE;
5021 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
5022 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
5023 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
5024 if (acc_count > 1)
5025 parse_error_context
5026 (cl, "Inconsistent member declaration. At most one of %<public%>, %<private%>, or %<protected%> may be specified");
5028 acc_count = 0;
5029 cl = NULL_TREE;
5030 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
5031 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
5032 if (acc_count > 1)
5033 parse_error_context (cl,
5034 "Inconsistent member declaration. At most one of %<final%> or %<volatile%> may be specified");
5037 /* Check the methode header METH for abstract specifics features */
5039 static void
5040 check_abstract_method_header (tree meth)
5042 int flags = get_access_flags_from_decl (meth);
5044 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
5045 ACC_ABSTRACT, "abstract method",
5046 IDENTIFIER_POINTER (DECL_NAME (meth)));
5047 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
5048 ACC_PUBLIC, "abstract method",
5049 IDENTIFIER_POINTER (DECL_NAME (meth)));
5051 check_modifiers ("Illegal modifier %qs for interface method",
5052 flags, INTERFACE_METHOD_MODIFIERS);
5055 /* Create a FUNCTION_TYPE node and start augmenting it with the
5056 declared function arguments. Arguments type that can't be resolved
5057 are left as they are, but the returned node is marked as containing
5058 incomplete types. */
5060 static tree
5061 method_declarator (tree id, tree list)
5063 tree arg_types = NULL_TREE, current, node;
5064 tree meth = make_node (FUNCTION_TYPE);
5065 jdep *jdep;
5067 patch_stage = JDEP_NO_PATCH;
5069 if (GET_CPC () == error_mark_node)
5070 return error_mark_node;
5072 /* If we're dealing with an inner class constructor, we hide the
5073 this$<n> decl in the name field of its parameter declaration. We
5074 also might have to hide the outer context local alias
5075 initializers. Not done when the class is a toplevel class. */
5076 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
5077 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
5079 tree aliases_list, type, thisn;
5080 /* First the aliases, linked to the regular parameters */
5081 aliases_list =
5082 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
5083 TREE_TYPE (GET_CPC ()),
5084 NULL_TREE, NULL);
5085 list = chainon (nreverse (aliases_list), list);
5087 /* Then this$<n> */
5088 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
5089 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
5090 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
5091 list);
5094 for (current = list; current; current = TREE_CHAIN (current))
5096 int must_chain = 0;
5097 tree wfl_name = TREE_PURPOSE (current);
5098 tree type = TREE_VALUE (current);
5099 tree name = EXPR_WFL_NODE (wfl_name);
5100 tree already, arg_node;
5101 tree type_wfl = NULL_TREE;
5102 tree real_type;
5104 /* Obtain a suitable type for resolution, if necessary */
5105 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5107 /* Process NAME, as it may specify extra dimension(s) for it */
5108 type = build_array_from_name (type, type_wfl, name, &name);
5109 EXPR_WFL_NODE (wfl_name) = name;
5111 real_type = GET_REAL_TYPE (type);
5112 if (TREE_CODE (real_type) == RECORD_TYPE)
5114 real_type = promote_type (real_type);
5115 if (TREE_CODE (type) == TREE_LIST)
5116 TREE_PURPOSE (type) = real_type;
5119 /* Check redefinition */
5120 for (already = arg_types; already; already = TREE_CHAIN (already))
5121 if (TREE_PURPOSE (already) == name)
5123 parse_error_context
5124 (wfl_name, "Variable %qs is used more than once in the argument list of method %qs",
5125 IDENTIFIER_POINTER (name),
5126 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5127 break;
5130 /* If we've an incomplete argument type, we know there is a location
5131 to patch when the type get resolved, later. */
5132 jdep = NULL;
5133 if (must_chain)
5135 patch_stage = JDEP_METHOD;
5136 type = register_incomplete_type (patch_stage,
5137 type_wfl, wfl_name, type);
5138 jdep = CLASSD_LAST (ctxp->classd_list);
5139 JDEP_MISC (jdep) = id;
5142 /* The argument node: a name and a (possibly) incomplete type. */
5143 arg_node = build_tree_list (name, real_type);
5144 /* Remember arguments declared final. */
5145 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5147 if (jdep)
5148 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5149 TREE_CHAIN (arg_node) = arg_types;
5150 arg_types = arg_node;
5152 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5153 node = build_tree_list (id, meth);
5154 return node;
5157 static int
5158 unresolved_type_p (tree wfl, tree *returned)
5160 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5162 if (returned)
5164 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5165 if (decl && current_class && (decl == TYPE_NAME (current_class)))
5166 *returned = TREE_TYPE (decl);
5167 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5168 *returned = TREE_TYPE (GET_CPC ());
5169 else
5170 *returned = NULL_TREE;
5172 return 1;
5174 if (returned)
5175 *returned = wfl;
5176 return 0;
5179 /* From NAME, build a qualified identifier node using the
5180 qualification from the current package definition. */
5182 static tree
5183 parser_qualified_classname (tree name)
5185 tree nested_class_name;
5187 if ((nested_class_name = maybe_make_nested_class_name (name)))
5188 return nested_class_name;
5190 if (ctxp->package)
5191 return merge_qualified_name (ctxp->package, name);
5192 else
5193 return name;
5196 /* Called once the type a interface extends is resolved. Returns 0 if
5197 everything is OK. */
5199 static int
5200 parser_check_super_interface (tree super_decl, tree this_decl, tree this_wfl)
5202 tree super_type = TREE_TYPE (super_decl);
5204 /* Has to be an interface */
5205 if (!CLASS_INTERFACE (super_decl))
5207 parse_error_context
5208 (this_wfl, "%s %qs can't implement/extend %s %qs",
5209 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5210 "Interface" : "Class"),
5211 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5212 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5213 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5214 return 1;
5217 /* Check top-level interface access. Inner classes are subject to member
5218 access rules (6.6.1). */
5219 if (! INNER_CLASS_P (super_type)
5220 && check_pkg_class_access (DECL_NAME (super_decl),
5221 NULL_TREE, true, this_decl))
5222 return 1;
5224 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5225 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5226 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5227 return 0;
5230 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5231 0 if everything is OK. */
5233 static int
5234 parser_check_super (tree super_decl, tree this_decl, tree wfl)
5236 tree super_type = TREE_TYPE (super_decl);
5238 /* SUPER should be a CLASS (neither an array nor an interface) */
5239 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5241 parse_error_context
5242 (wfl, "Class %qs can't subclass %s %qs",
5243 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5244 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5245 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5246 return 1;
5249 if (CLASS_FINAL (TYPE_NAME (super_type)))
5251 parse_error_context (wfl, "Can't subclass final classes: %s",
5252 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5253 return 1;
5256 /* Check top-level class scope. Inner classes are subject to member access
5257 rules (6.6.1). */
5258 if (! INNER_CLASS_P (super_type)
5259 && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true, NULL_TREE)))
5260 return 1;
5262 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5263 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5264 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5265 return 0;
5268 /* Create a new dependency list and link it (in a LIFO manner) to the
5269 CTXP list of type dependency list. */
5271 static void
5272 create_jdep_list (struct parser_ctxt *ctxp)
5274 jdeplist *new = xmalloc (sizeof (jdeplist));
5275 new->first = new->last = NULL;
5276 new->next = ctxp->classd_list;
5277 ctxp->classd_list = new;
5280 static jdeplist *
5281 reverse_jdep_list (struct parser_ctxt *ctxp)
5283 jdeplist *prev = NULL, *current, *next;
5284 for (current = ctxp->classd_list; current; current = next)
5286 next = current->next;
5287 current->next = prev;
5288 prev = current;
5290 return prev;
5293 /* Create a fake pointer based on the ID stored in
5294 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5295 registered again. */
5297 static tree
5298 obtain_incomplete_type (tree type_name)
5300 tree ptr = NULL_TREE, name;
5302 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5303 name = EXPR_WFL_NODE (type_name);
5304 else if (INCOMPLETE_TYPE_P (type_name))
5305 name = TYPE_NAME (type_name);
5306 else
5307 abort ();
5309 /* Workaround from build_pointer_type for incomplete types. */
5310 BUILD_PTR_FROM_NAME (ptr, name);
5311 TYPE_MODE (ptr) = ptr_mode;
5312 layout_type (ptr);
5314 return ptr;
5317 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5318 non NULL instead of computing a new fake type based on WFL. The new
5319 dependency is inserted in the current type dependency list, in FIFO
5320 manner. */
5322 static tree
5323 register_incomplete_type (int kind, tree wfl, tree decl, tree ptr)
5325 jdep *new = xmalloc (sizeof (jdep));
5327 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5328 ptr = obtain_incomplete_type (wfl);
5330 JDEP_KIND (new) = kind;
5331 JDEP_DECL (new) = decl;
5332 JDEP_TO_RESOLVE (new) = ptr;
5333 JDEP_WFL (new) = wfl;
5334 JDEP_CHAIN (new) = NULL;
5335 JDEP_MISC (new) = NULL_TREE;
5336 /* For some dependencies, set the enclosing class of the current
5337 class to be the enclosing context */
5338 if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS || kind == JDEP_SUPER)
5339 && GET_ENCLOSING_CPC ())
5340 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5341 else
5342 JDEP_ENCLOSING (new) = GET_CPC ();
5343 JDEP_GET_PATCH (new) = (tree *)NULL;
5345 JDEP_INSERT (ctxp->classd_list, new);
5347 return ptr;
5350 /* This checks for circular references with innerclasses. We start
5351 from SOURCE and should never reach TARGET. Extended/implemented
5352 types in SOURCE have their enclosing context checked not to reach
5353 TARGET. When the last enclosing context of SOURCE is reached, its
5354 extended/implemented types are also checked not to reach TARGET.
5355 In case of error, WFL of the offending type is returned; NULL_TREE
5356 otherwise. */
5358 static tree
5359 check_inner_circular_reference (tree source, tree target)
5361 tree base_binfo;
5362 tree ctx, cl;
5363 int i;
5365 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (source), i, base_binfo); i++)
5367 tree su;
5369 /* We can end up with a NULL_TREE or an incomplete type here if
5370 we encountered previous type resolution errors. It's safe to
5371 simply ignore these cases. */
5372 su = BINFO_TYPE (base_binfo);
5373 if (INCOMPLETE_TYPE_P (su))
5374 continue;
5376 if (inherits_from_p (su, target))
5377 return lookup_cl (TYPE_NAME (su));
5379 for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5381 /* An enclosing context shouldn't be TARGET */
5382 if (ctx == TYPE_NAME (target))
5383 return lookup_cl (TYPE_NAME (su));
5385 /* When we reach the enclosing last context, start a check
5386 on it, with the same target */
5387 if (! DECL_CONTEXT (ctx) &&
5388 (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5389 return cl;
5392 return NULL_TREE;
5395 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5396 offending type if a circularity is detected. NULL_TREE is returned
5397 otherwise. TYPE can be an interface or a class. */
5399 static tree
5400 check_circular_reference (tree type)
5402 tree base_binfo;
5403 int i;
5405 if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
5406 return NULL_TREE;
5408 if (! CLASS_INTERFACE (TYPE_NAME (type)))
5410 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5411 return lookup_cl (TYPE_NAME (type));
5412 return NULL_TREE;
5415 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo); i++)
5417 if (BINFO_TYPE (base_binfo) != object_type_node
5418 && interface_of_p (type, BINFO_TYPE (base_binfo)))
5419 return lookup_cl (TYPE_NAME (BINFO_TYPE (base_binfo)));
5421 return NULL_TREE;
5424 void
5425 java_check_circular_reference (void)
5427 tree current;
5428 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5430 tree type = TREE_TYPE (current);
5431 tree cl;
5433 cl = check_circular_reference (type);
5434 if (! cl)
5435 cl = check_inner_circular_reference (type, type);
5436 if (cl)
5437 parse_error_context (cl, "Cyclic class inheritance%s",
5438 (cyclic_inheritance_report ?
5439 cyclic_inheritance_report : ""));
5443 /* Augment the parameter list PARM with parameters crafted to
5444 initialize outer context locals aliases. Through ARTIFICIAL, a
5445 count is kept of the number of crafted parameters. MODE governs
5446 what eventually gets created: something suitable for a function
5447 creation or a function invocation, either the constructor or
5448 finit$. */
5450 static tree
5451 build_alias_initializer_parameter_list (int mode, tree class_type, tree parm,
5452 int *artificial)
5454 tree field;
5455 tree additional_parms = NULL_TREE;
5457 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5458 if (FIELD_LOCAL_ALIAS (field))
5460 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5461 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5462 tree mangled_id;
5464 switch (mode)
5466 case AIPL_FUNCTION_DECLARATION:
5467 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5468 &buffer [4]);
5469 purpose = build_wfl_node (mangled_id);
5470 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5471 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5472 else
5473 value = TREE_TYPE (field);
5474 break;
5476 case AIPL_FUNCTION_CREATION:
5477 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5478 &buffer [4]);
5479 value = TREE_TYPE (field);
5480 break;
5482 case AIPL_FUNCTION_FINIT_INVOCATION:
5483 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5484 &buffer [4]);
5485 /* Now, this is wrong. purpose should always be the NAME
5486 of something and value its matching value (decl, type,
5487 etc...) FIXME -- but there is a lot to fix. */
5489 /* When invoked for this kind of operation, we already
5490 know whether a field is used or not. */
5491 purpose = TREE_TYPE (field);
5492 value = build_wfl_node (mangled_id);
5493 break;
5495 case AIPL_FUNCTION_CTOR_INVOCATION:
5496 /* There are two case: the constructor invocation happens
5497 outside the local inner, in which case, locales from the outer
5498 context are directly used.
5500 Otherwise, we fold to using the alias directly. */
5501 if (class_type == current_class)
5502 value = field;
5503 else
5505 name = get_identifier (&buffer[4]);
5506 value = IDENTIFIER_LOCAL_VALUE (name);
5508 break;
5510 additional_parms = tree_cons (purpose, value, additional_parms);
5511 if (artificial)
5512 *artificial +=1;
5514 if (additional_parms)
5516 if (ANONYMOUS_CLASS_P (class_type)
5517 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5518 additional_parms = nreverse (additional_parms);
5519 parm = chainon (additional_parms, parm);
5522 return parm;
5525 /* Craft a constructor for CLASS_DECL -- what we should do when none
5526 where found. ARGS is non NULL when a special signature must be
5527 enforced. This is the case for anonymous classes. */
5529 static tree
5530 craft_constructor (tree class_decl, tree args)
5532 tree class_type = TREE_TYPE (class_decl);
5533 tree parm = NULL_TREE;
5534 /* Inherit access flags for the constructor from its enclosing class. */
5535 int valid_ctor_flags = ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE;
5536 int flags = (get_access_flags_from_decl (class_decl) & valid_ctor_flags);
5537 int i = 0, artificial = 0;
5538 tree decl, ctor_name;
5539 char buffer [80];
5541 ctor_name = init_identifier_node;
5543 /* If we're dealing with an inner class constructor, we hide the
5544 this$<n> decl in the name field of its parameter declaration. */
5545 if (PURE_INNER_CLASS_TYPE_P (class_type))
5547 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5548 parm = tree_cons (build_current_thisn (class_type),
5549 build_pointer_type (type), parm);
5551 /* Some more arguments to be hidden here. The values of the local
5552 variables of the outer context that the inner class needs to see. */
5553 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5554 class_type, parm,
5555 &artificial);
5558 /* Then if there are any args to be enforced, enforce them now */
5559 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5561 /* If we see a `void *', we need to change it to Object. */
5562 if (TREE_VALUE (args) == TREE_TYPE (null_pointer_node))
5563 TREE_VALUE (args) = object_ptr_type_node;
5565 sprintf (buffer, "parm%d", i++);
5566 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5569 CRAFTED_PARAM_LIST_FIXUP (parm);
5570 decl = create_artificial_method (class_type, flags, void_type_node,
5571 ctor_name, parm);
5572 fix_method_argument_names (parm, decl);
5573 /* Now, mark the artificial parameters. */
5574 DECL_FUNCTION_NAP (decl) = artificial;
5575 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5576 DECL_INLINE (decl) = 1;
5577 return decl;
5581 /* Fix the constructors. This will be called right after circular
5582 references have been checked. It is necessary to fix constructors
5583 early even if no code generation will take place for that class:
5584 some generated constructor might be required by the class whose
5585 compilation triggered this one to be simply loaded. */
5587 void
5588 java_fix_constructors (void)
5590 tree current;
5592 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5594 tree class_type = TREE_TYPE (current);
5595 int saw_ctor = 0;
5596 tree decl;
5598 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5599 continue;
5601 output_class = current_class = class_type;
5602 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5604 if (DECL_CONSTRUCTOR_P (decl))
5606 fix_constructors (decl);
5607 saw_ctor = 1;
5611 /* Anonymous class constructor can't be generated that early. */
5612 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5613 craft_constructor (current, NULL_TREE);
5617 /* safe_layout_class just makes sure that we can load a class without
5618 disrupting the current_class, input_file, input_line, etc, information
5619 about the class processed currently. */
5621 void
5622 safe_layout_class (tree class)
5624 tree save_current_class = current_class;
5625 location_t save_location = input_location;
5627 layout_class (class);
5629 current_class = save_current_class;
5630 input_location = save_location;
5633 static tree
5634 jdep_resolve_class (jdep *dep)
5636 tree decl;
5638 if (JDEP_RESOLVED_P (dep))
5639 decl = JDEP_RESOLVED_DECL (dep);
5640 else
5642 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5643 JDEP_DECL (dep), JDEP_WFL (dep));
5644 JDEP_RESOLVED (dep, decl);
5645 /* If there is no WFL, that's ok. We generate this warning
5646 elsewhere. */
5647 if (decl && JDEP_WFL (dep) != NULL_TREE)
5648 check_deprecation (JDEP_WFL (dep), decl);
5651 if (!decl)
5652 complete_class_report_errors (dep);
5653 else if (INNER_CLASS_DECL_P (decl))
5655 tree inner = TREE_TYPE (decl);
5656 if (! CLASS_LOADED_P (inner))
5658 safe_layout_class (inner);
5659 if (TYPE_SIZE (inner) == error_mark_node)
5660 TYPE_SIZE (inner) = NULL_TREE;
5662 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5664 return decl;
5667 /* Complete unsatisfied class declaration and their dependencies */
5669 void
5670 java_complete_class (void)
5672 tree cclass;
5673 jdeplist *cclassd;
5674 int error_found;
5675 tree type;
5677 /* Process imports */
5678 process_imports ();
5680 /* Reverse things so we have the right order */
5681 ctxp->class_list = nreverse (ctxp->class_list);
5682 ctxp->classd_list = reverse_jdep_list (ctxp);
5684 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5685 cclass && cclassd;
5686 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5688 jdep *dep;
5690 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5692 tree decl;
5693 if (!(decl = jdep_resolve_class (dep)))
5694 continue;
5696 /* Now it's time to patch */
5697 switch (JDEP_KIND (dep))
5699 case JDEP_SUPER:
5700 /* Simply patch super */
5701 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5702 continue;
5703 BINFO_TYPE (BINFO_BASE_BINFO
5704 (TYPE_BINFO (TREE_TYPE (JDEP_DECL (dep))), 0))
5705 = TREE_TYPE (decl);
5706 break;
5708 case JDEP_FIELD:
5710 /* We do part of the job done in add_field */
5711 tree field_decl = JDEP_DECL (dep);
5712 tree field_type = TREE_TYPE (decl);
5713 if (TREE_CODE (field_type) == RECORD_TYPE)
5714 field_type = promote_type (field_type);
5715 TREE_TYPE (field_decl) = field_type;
5716 DECL_ALIGN (field_decl) = 0;
5717 DECL_USER_ALIGN (field_decl) = 0;
5718 layout_decl (field_decl, 0);
5719 SOURCE_FRONTEND_DEBUG
5720 (("Completed field/var decl '%s' with '%s'",
5721 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5722 IDENTIFIER_POINTER (DECL_NAME (decl))));
5723 break;
5725 case JDEP_METHOD: /* We start patching a method */
5726 case JDEP_METHOD_RETURN:
5727 error_found = 0;
5728 while (1)
5730 if (decl)
5732 type = TREE_TYPE(decl);
5733 if (TREE_CODE (type) == RECORD_TYPE)
5734 type = promote_type (type);
5735 JDEP_APPLY_PATCH (dep, type);
5736 SOURCE_FRONTEND_DEBUG
5737 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5738 "Completing fct '%s' with ret type '%s'":
5739 "Completing arg '%s' with type '%s'"),
5740 IDENTIFIER_POINTER (EXPR_WFL_NODE
5741 (JDEP_DECL_WFL (dep))),
5742 IDENTIFIER_POINTER (DECL_NAME (decl))));
5744 else
5745 error_found = 1;
5746 dep = JDEP_CHAIN (dep);
5747 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5748 break;
5749 else
5750 decl = jdep_resolve_class (dep);
5752 if (!error_found)
5754 tree mdecl = JDEP_DECL (dep), signature;
5755 /* Recompute and reset the signature, check first that
5756 all types are now defined. If they're not,
5757 don't build the signature. */
5758 if (check_method_types_complete (mdecl))
5760 signature = build_java_signature (TREE_TYPE (mdecl));
5761 set_java_signature (TREE_TYPE (mdecl), signature);
5764 else
5765 continue;
5766 break;
5768 case JDEP_INTERFACE:
5769 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5770 JDEP_WFL (dep)))
5771 continue;
5772 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5773 break;
5775 case JDEP_PARM:
5776 case JDEP_VARIABLE:
5777 type = TREE_TYPE(decl);
5778 if (TREE_CODE (type) == RECORD_TYPE)
5779 type = promote_type (type);
5780 JDEP_APPLY_PATCH (dep, type);
5781 break;
5783 case JDEP_TYPE:
5784 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5785 SOURCE_FRONTEND_DEBUG
5786 (("Completing a random type dependency on a '%s' node",
5787 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5788 break;
5790 case JDEP_EXCEPTION:
5791 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5792 SOURCE_FRONTEND_DEBUG
5793 (("Completing '%s' 'throws' argument node",
5794 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5795 break;
5797 case JDEP_ANONYMOUS:
5798 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5799 break;
5801 default:
5802 abort ();
5806 return;
5809 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5810 array. */
5812 static tree
5813 resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5815 tree tname = TYPE_NAME (class_type);
5816 tree resolved_type = TREE_TYPE (class_type);
5817 int array_dims = 0;
5818 tree resolved_type_decl;
5820 if (resolved_type != NULL_TREE)
5822 tree resolved_type_decl = TYPE_NAME (resolved_type);
5823 if (resolved_type_decl == NULL_TREE
5824 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5826 resolved_type_decl = build_decl (TYPE_DECL,
5827 TYPE_NAME (class_type),
5828 resolved_type);
5830 return resolved_type_decl;
5833 /* 1- Check to see if we have an array. If true, find what we really
5834 want to resolve */
5835 if ((array_dims = build_type_name_from_array_name (tname,
5836 &TYPE_NAME (class_type))))
5837 WFL_STRIP_BRACKET (cl, cl);
5839 /* 2- Resolve the bare type */
5840 if (!(resolved_type_decl = do_resolve_class (enclosing, NULL_TREE, class_type,
5841 decl, cl)))
5842 return NULL_TREE;
5843 resolved_type = TREE_TYPE (resolved_type_decl);
5845 /* 3- If we have an array, reconstruct the array down to its nesting */
5846 if (array_dims)
5848 for (; array_dims; array_dims--)
5849 resolved_type = build_java_array_type (resolved_type, -1);
5850 resolved_type_decl = TYPE_NAME (resolved_type);
5852 TREE_TYPE (class_type) = resolved_type;
5853 return resolved_type_decl;
5856 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5857 are used to report error messages; CL must either be NULL_TREE or a
5858 WFL wrapping a class. Do not try to replace TYPE_NAME (class_type)
5859 by a variable, since it is changed by find_in_imports{_on_demand}
5860 and (but it doesn't really matter) qualify_and_find. */
5862 tree
5863 do_resolve_class (tree enclosing, tree import_type, tree class_type, tree decl,
5864 tree cl)
5866 tree new_class_decl = NULL_TREE, super = NULL_TREE;
5867 tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5868 tree decl_result;
5869 htab_t circularity_hash;
5871 if (QUALIFIED_P (TYPE_NAME (class_type)))
5873 /* If the type name is of the form `Q . Id', then Q is either a
5874 package name or a class name. First we try to find Q as a
5875 class and then treat Id as a member type. If we can't find Q
5876 as a class then we fall through. */
5877 tree q, left, left_type, right;
5878 if (split_qualified_name (&left, &right, TYPE_NAME (class_type)) == 0)
5880 BUILD_PTR_FROM_NAME (left_type, left);
5881 q = do_resolve_class (enclosing, import_type, left_type, decl, cl);
5882 if (q)
5884 enclosing = q;
5885 saved_enclosing_type = TREE_TYPE (q);
5886 BUILD_PTR_FROM_NAME (class_type, right);
5891 if (enclosing)
5893 /* This hash table is used to register the classes we're going
5894 through when searching the current class as an inner class, in
5895 order to detect circular references. Remember to free it before
5896 returning the section 0- of this function. */
5897 circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
5898 NULL);
5900 /* 0- Search in the current class as an inner class.
5901 Maybe some code here should be added to load the class or
5902 something, at least if the class isn't an inner class and ended
5903 being loaded from class file. FIXME. */
5904 while (enclosing)
5906 new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5907 &super, class_type);
5908 if (new_class_decl)
5909 break;
5911 /* If we haven't found anything because SUPER reached Object and
5912 ENCLOSING happens to be an innerclass, try the enclosing context. */
5913 if ((!super || super == object_type_node) &&
5914 enclosing && INNER_CLASS_DECL_P (enclosing))
5915 enclosing = DECL_CONTEXT (enclosing);
5916 else
5917 enclosing = NULL_TREE;
5920 htab_delete (circularity_hash);
5922 if (new_class_decl)
5923 return new_class_decl;
5926 /* 1- Check for the type in single imports. Look at enclosing classes and,
5927 if we're laying out a superclass, at the import list for the subclass.
5928 This will change TYPE_NAME() if something relevant is found. */
5929 if (import_type && TYPE_IMPORT_LIST (import_type))
5930 find_in_imports (import_type, class_type);
5931 find_in_imports (saved_enclosing_type, class_type);
5933 /* 2- And check for the type in the current compilation unit */
5934 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5936 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
5937 load_class (TYPE_NAME (class_type), 0);
5938 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5941 /* 3- Search according to the current package definition */
5942 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5944 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5945 TYPE_NAME (class_type))))
5946 return new_class_decl;
5949 /* 4- Check the import on demands. Don't allow bar.baz to be
5950 imported from foo.* */
5951 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5953 if (import_type
5954 && TYPE_IMPORT_DEMAND_LIST (import_type)
5955 && find_in_imports_on_demand (import_type, class_type))
5956 return NULL_TREE;
5957 if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5958 return NULL_TREE;
5961 /* If found in find_in_imports_on_demand, the type has already been
5962 loaded. */
5963 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5964 return new_class_decl;
5966 /* 5- Check another compilation unit that bears the name of type */
5967 load_class (TYPE_NAME (class_type), 0);
5969 if (!cl)
5970 cl = lookup_cl (decl);
5972 /* If we don't have a value for CL, then we're being called recursively.
5973 We can't check package access just yet, but it will be taken care of
5974 by the caller. */
5975 if (cl)
5977 if (check_pkg_class_access (TYPE_NAME (class_type), cl, true, NULL_TREE))
5978 return NULL_TREE;
5981 /* 6- Last call for a resolution */
5982 decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5984 /* The final lookup might have registered a.b.c into a.b$c If we
5985 failed at the first lookup, progressively change the name if
5986 applicable and use the matching DECL instead. */
5987 if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
5989 char *separator;
5990 tree name = TYPE_NAME (class_type);
5991 char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
5993 strcpy (namebuffer, IDENTIFIER_POINTER (name));
5995 do {
5997 /* Reach the last '.', and if applicable, replace it by a `$' and
5998 see if this exists as a type. */
5999 if ((separator = strrchr (namebuffer, '.')))
6001 *separator = '$';
6002 name = get_identifier (namebuffer);
6003 decl_result = IDENTIFIER_CLASS_VALUE (name);
6005 } while (!decl_result && separator);
6007 return decl_result;
6010 static tree
6011 qualify_and_find (tree class_type, tree package, tree name)
6013 tree new_qualified = merge_qualified_name (package, name);
6014 tree new_class_decl;
6016 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
6017 load_class (new_qualified, 0);
6018 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
6020 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
6021 load_class (TREE_TYPE (new_class_decl), 0);
6022 TYPE_NAME (class_type) = new_qualified;
6023 return IDENTIFIER_CLASS_VALUE (new_qualified);
6025 return NULL_TREE;
6028 /* Resolve NAME and lay it out (if not done and if not the current
6029 parsed class). Return a decl node. This function is meant to be
6030 called when type resolution is necessary during the walk pass. */
6032 static tree
6033 resolve_and_layout (tree something, tree cl)
6035 tree decl, decl_type;
6037 /* Don't do that on the current class */
6038 if (something == current_class)
6039 return TYPE_NAME (current_class);
6041 /* Don't do anything for void and other primitive types */
6042 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6043 return NULL_TREE;
6045 /* Pointer types can be reall pointer types or fake pointers. When
6046 finding a real pointer, recheck for primitive types */
6047 if (TREE_CODE (something) == POINTER_TYPE)
6049 if (TREE_TYPE (something))
6051 something = TREE_TYPE (something);
6052 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6053 return NULL_TREE;
6055 else
6056 something = TYPE_NAME (something);
6059 /* Don't do anything for arrays of primitive types */
6060 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
6061 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
6062 return NULL_TREE;
6064 /* Something might be a WFL */
6065 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
6066 something = EXPR_WFL_NODE (something);
6068 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
6069 TYPE_DECL or a real TYPE */
6070 else if (TREE_CODE (something) != IDENTIFIER_NODE)
6071 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
6072 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
6074 if (!(decl = resolve_no_layout (something, cl)))
6075 return NULL_TREE;
6077 /* Resolve and layout if necessary */
6078 decl_type = TREE_TYPE (decl);
6079 layout_class_methods (decl_type);
6080 /* Check methods */
6081 if (CLASS_FROM_SOURCE_P (decl_type))
6082 java_check_methods (decl);
6083 /* Layout the type if necessary */
6084 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6085 safe_layout_class (decl_type);
6087 return decl;
6090 /* Resolve a class, returns its decl but doesn't perform any
6091 layout. The current parsing context is saved and restored */
6093 static tree
6094 resolve_no_layout (tree name, tree cl)
6096 tree ptr, decl;
6097 BUILD_PTR_FROM_NAME (ptr, name);
6098 java_parser_context_save_global ();
6099 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6100 java_parser_context_restore_global ();
6102 return decl;
6105 /* Called when reporting errors. Skip the '[]'s in a complex array
6106 type description that failed to be resolved. purify_type_name can't
6107 use an identifier tree. */
6109 static const char *
6110 purify_type_name (const char *name)
6112 int len = strlen (name);
6113 int bracket_found;
6115 STRING_STRIP_BRACKETS (name, len, bracket_found);
6116 if (bracket_found)
6118 char *stripped_name = xmemdup (name, len, len+1);
6119 stripped_name [len] = '\0';
6120 return stripped_name;
6122 return name;
6125 /* The type CURRENT refers to can't be found. We print error messages. */
6127 static void
6128 complete_class_report_errors (jdep *dep)
6130 const char *name;
6132 if (!JDEP_WFL (dep))
6133 return;
6135 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6136 switch (JDEP_KIND (dep))
6138 case JDEP_SUPER:
6139 parse_error_context
6140 (JDEP_WFL (dep), "Superclass %qs of class %qs not found",
6141 purify_type_name (name),
6142 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6143 break;
6144 case JDEP_FIELD:
6145 parse_error_context
6146 (JDEP_WFL (dep), "Type %qs not found in declaration of field %qs",
6147 purify_type_name (name),
6148 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6149 break;
6150 case JDEP_METHOD: /* Covers arguments */
6151 parse_error_context
6152 (JDEP_WFL (dep), "Type %qs not found in the declaration of the argument %qs of method %qs",
6153 purify_type_name (name),
6154 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6155 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6156 break;
6157 case JDEP_METHOD_RETURN: /* Covers return type */
6158 parse_error_context
6159 (JDEP_WFL (dep), "Type %qs not found in the declaration of the return type of method %qs",
6160 purify_type_name (name),
6161 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6162 break;
6163 case JDEP_INTERFACE:
6164 parse_error_context
6165 (JDEP_WFL (dep), "Superinterface %qs of %s %qs not found",
6166 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6167 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6168 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6169 break;
6170 case JDEP_VARIABLE:
6171 parse_error_context
6172 (JDEP_WFL (dep), "Type %qs not found in the declaration of the local variable %qs",
6173 purify_type_name (IDENTIFIER_POINTER
6174 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6175 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6176 break;
6177 case JDEP_EXCEPTION: /* As specified by `throws' */
6178 parse_error_context
6179 (JDEP_WFL (dep), "Class %qs not found in %<throws%>",
6180 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6181 break;
6182 default:
6183 /* Fix for -Wall. Just break doing nothing. The error will be
6184 caught later */
6185 break;
6189 /* Return a static string containing the DECL prototype string. If
6190 DECL is a constructor, use the class name instead of the form
6191 <init> */
6193 static const char *
6194 get_printable_method_name (tree decl)
6196 const char *to_return;
6197 tree name = NULL_TREE;
6199 if (DECL_CONSTRUCTOR_P (decl))
6201 name = DECL_NAME (decl);
6202 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6205 to_return = lang_printable_name (decl, 2);
6206 if (DECL_CONSTRUCTOR_P (decl))
6207 DECL_NAME (decl) = name;
6209 return to_return;
6212 /* Track method being redefined inside the same class. As a side
6213 effect, set DECL_NAME to an IDENTIFIER (prior entering this
6214 function it's a FWL, so we can track errors more accurately.) */
6216 static int
6217 check_method_redefinition (tree class, tree method)
6219 tree redef, sig;
6221 /* There's no need to verify <clinit> and finit$ and instinit$ */
6222 if (DECL_CLINIT_P (method)
6223 || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6224 return 0;
6226 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6227 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6229 if (redef == method)
6230 break;
6231 if (DECL_NAME (redef) == DECL_NAME (method)
6232 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6233 && !DECL_ARTIFICIAL (method))
6235 parse_error_context
6236 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration %qs",
6237 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6238 get_printable_method_name (redef));
6239 return 1;
6242 return 0;
6245 /* Return 1 if check went ok, 0 otherwise. */
6246 static int
6247 check_abstract_method_definitions (int do_interface, tree class_decl,
6248 tree type)
6250 tree class = TREE_TYPE (class_decl);
6251 tree method, end_type;
6252 int ok = 1;
6254 end_type = (do_interface ? object_type_node : type);
6255 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6257 tree other_super, other_method, method_sig, method_name;
6258 int found = 0;
6259 int end_type_reached = 0;
6261 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6262 continue;
6264 /* Now verify that somewhere in between TYPE and CLASS,
6265 abstract method METHOD gets a non abstract definition
6266 that is inherited by CLASS. */
6268 method_sig = build_java_signature (TREE_TYPE (method));
6269 method_name = DECL_NAME (method);
6270 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6271 method_name = EXPR_WFL_NODE (method_name);
6273 other_super = class;
6274 do {
6275 if (other_super == end_type)
6276 end_type_reached = 1;
6278 /* Method search */
6279 for (other_method = TYPE_METHODS (other_super); other_method;
6280 other_method = TREE_CHAIN (other_method))
6282 tree s = build_java_signature (TREE_TYPE (other_method));
6283 tree other_name = DECL_NAME (other_method);
6285 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6286 other_name = EXPR_WFL_NODE (other_name);
6287 if (!DECL_CLINIT_P (other_method)
6288 && !DECL_CONSTRUCTOR_P (other_method)
6289 && method_name == other_name
6290 && method_sig == s
6291 && !METHOD_ABSTRACT (other_method))
6293 found = 1;
6294 break;
6297 other_super = CLASSTYPE_SUPER (other_super);
6298 } while (!end_type_reached);
6300 /* Report that abstract METHOD didn't find an implementation
6301 that CLASS can use. */
6302 if (!found)
6304 char *t = xstrdup (lang_printable_name
6305 (TREE_TYPE (TREE_TYPE (method)), 0));
6306 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6308 parse_error_context
6309 (lookup_cl (class_decl),
6310 "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",
6311 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6312 t, lang_printable_name (method, 2),
6313 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6314 "interface" : "class"),
6315 IDENTIFIER_POINTER (ccn),
6316 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6317 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6318 ok = 0;
6319 free (t);
6323 if (ok && do_interface)
6325 /* Check for implemented interfaces. */
6326 int i;
6327 tree base_binfo;
6329 for (i = 1;
6330 ok && BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo);
6331 i++)
6332 ok = check_abstract_method_definitions (1, class_decl,
6333 BINFO_TYPE (base_binfo));
6336 return ok;
6339 /* Check that CLASS_DECL somehow implements all inherited abstract
6340 methods. */
6342 static void
6343 java_check_abstract_method_definitions (tree class_decl)
6345 tree class = TREE_TYPE (class_decl);
6346 tree super, base_binfo;
6347 int i;
6349 if (CLASS_ABSTRACT (class_decl))
6350 return;
6352 /* Check for inherited types */
6353 super = class;
6354 do {
6355 super = CLASSTYPE_SUPER (super);
6356 check_abstract_method_definitions (0, class_decl, super);
6357 } while (super != object_type_node);
6359 /* Check for implemented interfaces. */
6360 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6361 check_abstract_method_definitions (1, class_decl, BINFO_TYPE (base_binfo));
6364 /* Check all the types method DECL uses and return 1 if all of them
6365 are now complete, 0 otherwise. This is used to check whether its
6366 safe to build a method signature or not. */
6368 static int
6369 check_method_types_complete (tree decl)
6371 tree type = TREE_TYPE (decl);
6372 tree args;
6374 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6375 return 0;
6377 args = TYPE_ARG_TYPES (type);
6378 if (TREE_CODE (type) == METHOD_TYPE)
6379 args = TREE_CHAIN (args);
6380 for (; args != end_params_node; args = TREE_CHAIN (args))
6381 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6382 return 0;
6384 return 1;
6387 /* Visible interface to check methods contained in CLASS_DECL */
6389 void
6390 java_check_methods (tree class_decl)
6392 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6393 return;
6395 if (CLASS_INTERFACE (class_decl))
6396 java_check_abstract_methods (class_decl);
6397 else
6398 java_check_regular_methods (class_decl);
6400 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6403 /* Like not_accessible_p, but doesn't refer to the current class at
6404 all. */
6405 static bool
6406 hack_is_accessible_p (tree member, tree from_where)
6408 int flags = get_access_flags_from_decl (member);
6410 if (from_where == DECL_CONTEXT (member)
6411 || (flags & ACC_PUBLIC))
6412 return true;
6414 if ((flags & ACC_PROTECTED))
6416 if (inherits_from_p (from_where, DECL_CONTEXT (member)))
6417 return true;
6420 if ((flags & ACC_PRIVATE))
6421 return false;
6423 /* Package private, or protected. */
6424 return in_same_package (TYPE_NAME (from_where),
6425 TYPE_NAME (DECL_CONTEXT (member)));
6428 /* Check all the methods of CLASS_DECL. Methods are first completed
6429 then checked according to regular method existence rules. If no
6430 constructor for CLASS_DECL were encountered, then build its
6431 declaration. */
6432 static void
6433 java_check_regular_methods (tree class_decl)
6435 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6436 tree method;
6437 tree class = TREE_TYPE (class_decl);
6438 tree found = NULL_TREE;
6439 tree mthrows;
6441 /* It is not necessary to check methods defined in java.lang.Object */
6442 if (class == object_type_node)
6443 return;
6445 if (!TYPE_NVIRTUALS (class))
6446 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6448 /* Should take interfaces into account. FIXME */
6449 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6451 tree sig;
6452 tree method_wfl = DECL_FUNCTION_WFL (method);
6453 int aflags;
6455 /* Check for redefinitions */
6456 if (check_method_redefinition (class, method))
6457 continue;
6459 /* We verify things thrown by the method. They must inherit from
6460 java.lang.Throwable. */
6461 for (mthrows = DECL_FUNCTION_THROWS (method);
6462 mthrows; mthrows = TREE_CHAIN (mthrows))
6464 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6465 parse_error_context
6466 (TREE_PURPOSE (mthrows), "Class %qs in %<throws%> clause must be a subclass of class %<java.lang.Throwable%>",
6467 IDENTIFIER_POINTER
6468 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6471 /* If we see one constructor a mark so we don't generate the
6472 default one. Also skip other verifications: constructors
6473 can't be inherited hence hidden or overridden. */
6474 if (DECL_CONSTRUCTOR_P (method))
6476 saw_constructor = 1;
6477 continue;
6480 sig = build_java_argument_signature (TREE_TYPE (method));
6481 found = lookup_argument_method_generic (class, DECL_NAME (method), sig,
6482 SEARCH_SUPER | SEARCH_INTERFACE);
6484 /* Inner class can't declare static methods */
6485 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6487 char *t = xstrdup (lang_printable_name (class, 0));
6488 parse_error_context
6489 (method_wfl, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
6490 lang_printable_name (method, 2), t);
6491 free (t);
6494 /* Nothing overrides or it's a private method. */
6495 if (!found)
6496 continue;
6497 if (METHOD_PRIVATE (found))
6499 found = NULL_TREE;
6500 continue;
6503 /* If `found' is declared in an interface, make sure the
6504 modifier matches. */
6505 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6506 && clinit_identifier_node != DECL_NAME (found)
6507 && !METHOD_PUBLIC (method))
6509 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6510 parse_error_context (method_wfl, "Class %qs must override %qs with a public method in order to implement interface %qs",
6511 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6512 lang_printable_name (method, 2),
6513 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6516 /* Can't override a method with the same name and different return
6517 types. */
6518 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6520 char *t = xstrdup
6521 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6522 parse_error_context
6523 (method_wfl,
6524 "Method %qs was defined with return type %qs in class %qs",
6525 lang_printable_name (found, 2), t,
6526 IDENTIFIER_POINTER
6527 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6528 free (t);
6531 aflags = get_access_flags_from_decl (found);
6533 /* Can't override final. Can't override static. */
6534 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6536 /* Static *can* override static */
6537 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6538 continue;
6539 parse_error_context
6540 (method_wfl,
6541 "%s methods can't be overridden. Method %qs is %s in class %qs",
6542 (METHOD_FINAL (found) ? "Final" : "Static"),
6543 lang_printable_name (found, 2),
6544 (METHOD_FINAL (found) ? "final" : "static"),
6545 IDENTIFIER_POINTER
6546 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6547 continue;
6550 /* Static method can't override instance method. */
6551 if (METHOD_STATIC (method))
6553 parse_error_context
6554 (method_wfl,
6555 "Instance methods can't be overridden by a static method. Method %qs is an instance method in class %qs",
6556 lang_printable_name (found, 2),
6557 IDENTIFIER_POINTER
6558 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6559 continue;
6562 /* - Overriding/hiding public must be public
6563 - Overriding/hiding protected must be protected or public
6564 - If the overridden or hidden method has default (package)
6565 access, then the overriding or hiding method must not be
6566 private; otherwise, a compile-time error occurs. If
6567 `found' belongs to an interface, things have been already
6568 taken care of. */
6569 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6570 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6571 || (METHOD_PROTECTED (found)
6572 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6573 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6574 && METHOD_PRIVATE (method))))
6576 parse_error_context
6577 (method_wfl,
6578 "Methods can't be overridden to be more private. Method %qs is not %s in class %qs", lang_printable_name (method, 2),
6579 (METHOD_PUBLIC (method) ? "public" :
6580 (METHOD_PRIVATE (method) ? "private" : "protected")),
6581 IDENTIFIER_POINTER (DECL_NAME
6582 (TYPE_NAME (DECL_CONTEXT (found)))));
6583 continue;
6586 /* Check this method against all the other implementations it
6587 overrides. Here we only check the class hierarchy; the rest
6588 of the checking is done later. If this method is just a
6589 Miranda method, we can skip the check. */
6590 if (! METHOD_INVISIBLE (method))
6591 check_concrete_throws_clauses (class, method, DECL_NAME (method), sig);
6594 /* The above throws clause check only looked at superclasses. Now
6595 we must also make sure that all methods declared in interfaces
6596 have compatible throws clauses. FIXME: there are more efficient
6597 ways to organize this checking; we should implement one. */
6598 check_interface_throws_clauses (class, class);
6600 if (!TYPE_NVIRTUALS (class))
6601 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6603 /* Search for inherited abstract method not yet implemented in this
6604 class. */
6605 java_check_abstract_method_definitions (class_decl);
6607 if (!saw_constructor)
6608 abort ();
6611 /* Check to make sure that all the methods in all the interfaces
6612 implemented by CLASS_DECL are compatible with the concrete
6613 implementations available in CHECK_CLASS_DECL. */
6614 static void
6615 check_interface_throws_clauses (tree check_class_decl, tree class_decl)
6617 for (; class_decl != NULL_TREE; class_decl = CLASSTYPE_SUPER (class_decl))
6619 int i;
6621 if (! CLASS_LOADED_P (class_decl))
6623 if (CLASS_FROM_SOURCE_P (class_decl))
6624 safe_layout_class (class_decl);
6625 else
6626 load_class (class_decl, 1);
6629 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (class_decl)) - 1; i > 0; --i)
6631 tree interface
6632 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (class_decl), i));
6633 tree iface_method;
6635 for (iface_method = TYPE_METHODS (interface);
6636 iface_method != NULL_TREE;
6637 iface_method = TREE_CHAIN (iface_method))
6639 tree sig, method;
6641 /* First look for a concrete method implemented or
6642 inherited by this class. No need to search
6643 interfaces here, since we're already looking through
6644 all of them. */
6645 sig = build_java_argument_signature (TREE_TYPE (iface_method));
6646 method
6647 = lookup_argument_method_generic (check_class_decl,
6648 DECL_NAME (iface_method),
6649 sig, SEARCH_VISIBLE);
6650 /* If we don't find an implementation, that is ok. Any
6651 potential errors from that are diagnosed elsewhere.
6652 Also, multiple inheritance with conflicting throws
6653 clauses is fine in the absence of a concrete
6654 implementation. */
6655 if (method != NULL_TREE && !METHOD_ABSTRACT (method)
6656 && !METHOD_INVISIBLE (iface_method))
6658 tree method_wfl = DECL_FUNCTION_WFL (method);
6659 check_throws_clauses (method, method_wfl, iface_method);
6663 /* Now check superinterfaces. */
6664 check_interface_throws_clauses (check_class_decl, interface);
6669 /* Check throws clauses of a method against the clauses of all the
6670 methods it overrides. We do this by searching up the class
6671 hierarchy, examining all matching accessible methods. */
6672 static void
6673 check_concrete_throws_clauses (tree class, tree self_method,
6674 tree name, tree signature)
6676 tree method = lookup_argument_method_generic (class, name, signature,
6677 SEARCH_SUPER | SEARCH_VISIBLE);
6678 while (method != NULL_TREE)
6680 if (! METHOD_INVISIBLE (method) && hack_is_accessible_p (method, class))
6681 check_throws_clauses (self_method, DECL_FUNCTION_WFL (self_method),
6682 method);
6684 method = lookup_argument_method_generic (DECL_CONTEXT (method),
6685 name, signature,
6686 SEARCH_SUPER | SEARCH_VISIBLE);
6690 /* Generate an error if the `throws' clause of METHOD (if any) is
6691 incompatible with the `throws' clause of FOUND (if any). */
6692 static void
6693 check_throws_clauses (tree method, tree method_wfl, tree found)
6695 tree mthrows;
6697 for (mthrows = DECL_FUNCTION_THROWS (method);
6698 mthrows; mthrows = TREE_CHAIN (mthrows))
6700 tree fthrows;
6702 /* We don't verify unchecked expressions */
6703 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6704 continue;
6705 /* Checked expression must be compatible */
6706 for (fthrows = DECL_FUNCTION_THROWS (found);
6707 fthrows; fthrows = TREE_CHAIN (fthrows))
6709 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6710 break;
6712 if (!fthrows)
6714 parse_error_context
6715 (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",
6716 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6717 lang_printable_name (found, 2),
6718 IDENTIFIER_POINTER
6719 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6724 /* Check abstract method of interface INTERFACE */
6725 static void
6726 java_check_abstract_methods (tree interface_decl)
6728 int i;
6729 tree method, found;
6730 tree interface = TREE_TYPE (interface_decl);
6731 tree base_binfo;
6733 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6735 /* 2- Check for double definition inside the defining interface */
6736 if (check_method_redefinition (interface, method))
6737 continue;
6739 /* 3- Overriding is OK as far as we preserve the return type. */
6740 found = lookup_java_interface_method2 (interface, method);
6741 if (found)
6743 char *t;
6744 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6745 parse_error_context
6746 (DECL_FUNCTION_WFL (found),
6747 "Method %qs was defined with return type %qs in class %qs",
6748 lang_printable_name (found, 2), t,
6749 IDENTIFIER_POINTER
6750 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6751 free (t);
6752 continue;
6756 /* 4- Inherited methods can't differ by their returned types */
6757 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (interface), i, base_binfo); i++)
6759 tree sub_interface_method, sub_interface;
6761 sub_interface = BINFO_TYPE (base_binfo);
6762 for (sub_interface_method = TYPE_METHODS (sub_interface);
6763 sub_interface_method;
6764 sub_interface_method = TREE_CHAIN (sub_interface_method))
6766 found = lookup_java_interface_method2 (interface,
6767 sub_interface_method);
6768 if (found && (found != sub_interface_method))
6770 parse_error_context
6771 (lookup_cl (sub_interface_method),
6772 "Interface %qs inherits method %qs from interface %qs. This method is redefined with a different return type in interface %qs",
6773 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6774 lang_printable_name (found, 2),
6775 IDENTIFIER_POINTER
6776 (DECL_NAME (TYPE_NAME
6777 (DECL_CONTEXT (sub_interface_method)))),
6778 IDENTIFIER_POINTER
6779 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6785 /* Lookup methods in interfaces using their name and partial
6786 signature. Return a matching method only if their types differ. */
6788 static tree
6789 lookup_java_interface_method2 (tree class, tree method_decl)
6791 int i;
6792 tree base_binfo;
6793 tree to_return;
6795 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6797 if ((BINFO_TYPE (base_binfo) != object_type_node)
6798 && (to_return =
6799 lookup_java_method2 (BINFO_TYPE (base_binfo), method_decl, 1)))
6800 return to_return;
6802 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6804 to_return = lookup_java_interface_method2
6805 (BINFO_TYPE (base_binfo), method_decl);
6806 if (to_return)
6807 return to_return;
6810 return NULL_TREE;
6813 /* Lookup method using their name and partial signature. Return a
6814 matching method only if their types differ. */
6816 static tree
6817 lookup_java_method2 (tree clas, tree method_decl, int do_interface)
6819 tree method, method_signature, method_name, method_type, name;
6821 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6822 name = DECL_NAME (method_decl);
6823 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6824 EXPR_WFL_NODE (name) : name);
6825 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6827 while (clas != NULL_TREE)
6829 for (method = TYPE_METHODS (clas);
6830 method != NULL_TREE; method = TREE_CHAIN (method))
6832 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6833 tree name = DECL_NAME (method);
6834 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6835 EXPR_WFL_NODE (name) : name) == method_name
6836 && method_sig == method_signature
6837 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6838 return method;
6840 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6842 return NULL_TREE;
6845 /* Return the line that matches DECL line number, and try its best to
6846 position the column number. Used during error reports.
6847 FUTURE/FIXME: return source_location instead of node. */
6849 static GTY(()) tree cl_v;
6850 static tree
6851 lookup_cl (tree decl)
6853 #ifndef USE_MAPPED_LOCATION
6854 char *line, *found;
6855 #endif
6857 if (!decl)
6858 return NULL_TREE;
6860 if (cl_v == NULL_TREE)
6862 cl_v = build_unknown_wfl (NULL_TREE);
6865 #ifdef USE_MAPPED_LOCATION
6866 SET_EXPR_LOCATION (cl_v, DECL_SOURCE_LOCATION (decl));
6867 #else
6868 EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6869 EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE (decl), -1);
6871 line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6872 EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6874 found = strstr ((const char *)line,
6875 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6876 if (found)
6877 EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6878 #endif
6880 return cl_v;
6883 /* Look for a simple name in the single-type import list */
6885 static tree
6886 find_name_in_single_imports (tree name)
6888 tree node;
6890 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6891 if (TREE_VALUE (node) == name)
6892 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6894 return NULL_TREE;
6897 /* Process all single-type import. */
6899 static int
6900 process_imports (void)
6902 tree import;
6903 int error_found;
6905 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6907 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6908 char *original_name;
6910 /* Don't load twice something already defined. */
6911 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6912 continue;
6914 original_name = xmemdup (IDENTIFIER_POINTER (to_be_found),
6915 IDENTIFIER_LENGTH (to_be_found),
6916 IDENTIFIER_LENGTH (to_be_found) + 1);
6918 while (1)
6920 tree left;
6922 QUALIFIED_P (to_be_found) = 1;
6923 load_class (to_be_found, 0);
6924 error_found =
6925 check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true, NULL_TREE);
6927 /* We found it, we can bail out */
6928 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6930 check_deprecation (TREE_PURPOSE (import),
6931 IDENTIFIER_CLASS_VALUE (to_be_found));
6932 break;
6935 /* We haven't found it. Maybe we're trying to access an
6936 inner class. The only way for us to know is to try again
6937 after having dropped a qualifier. If we can't break it further,
6938 we have an error. */
6939 if (split_qualified_name (&left, NULL, to_be_found))
6940 break;
6942 to_be_found = left;
6944 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6946 parse_error_context (TREE_PURPOSE (import),
6947 "Class or interface %qs not found in import",
6948 original_name);
6949 error_found = 1;
6952 free (original_name);
6953 if (error_found)
6954 return 1;
6956 return 0;
6959 /* Possibly find and mark a class imported by a single-type import
6960 statement. */
6962 static void
6963 find_in_imports (tree enclosing_type, tree class_type)
6965 tree import;
6966 if (enclosing_type && TYPE_IMPORT_LIST (enclosing_type))
6967 import = TYPE_IMPORT_LIST (enclosing_type);
6968 else
6969 import = ctxp->import_list;
6971 while (import)
6973 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6975 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6976 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6977 return;
6979 import = TREE_CHAIN (import);
6983 static int
6984 note_possible_classname (const char *name, int len)
6986 tree node;
6987 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6988 len = len - 5;
6989 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6990 len = len - 6;
6991 else
6992 return 0;
6993 node = ident_subst (name, len, "", '/', '.', "");
6994 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6995 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6996 return 1;
6999 /* Read a import directory, gathering potential match for further type
7000 references. Indifferently reads a filesystem or a ZIP archive
7001 directory. */
7003 static void
7004 read_import_dir (tree wfl)
7006 tree package_id = EXPR_WFL_NODE (wfl);
7007 const char *package_name = IDENTIFIER_POINTER (package_id);
7008 int package_length = IDENTIFIER_LENGTH (package_id);
7009 DIR *dirp = NULL;
7010 JCF *saved_jcf = current_jcf;
7012 int found = 0;
7013 int k;
7014 void *entry;
7015 struct buffer filename[1];
7017 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
7018 return;
7019 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
7021 BUFFER_INIT (filename);
7022 buffer_grow (filename, package_length + 100);
7024 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
7026 const char *entry_name = jcf_path_name (entry);
7027 int entry_length = strlen (entry_name);
7028 if (jcf_path_is_zipfile (entry))
7030 ZipFile *zipf;
7031 buffer_grow (filename, entry_length);
7032 memcpy (filename->data, entry_name, entry_length - 1);
7033 filename->data[entry_length-1] = '\0';
7034 zipf = opendir_in_zip ((const char *) filename->data, jcf_path_is_system (entry));
7035 if (zipf == NULL)
7036 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
7037 else
7039 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
7040 BUFFER_RESET (filename);
7041 for (k = 0; k < package_length; k++)
7043 char ch = package_name[k];
7044 *filename->ptr++ = ch == '.' ? '/' : ch;
7046 *filename->ptr++ = '/';
7048 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
7050 const char *current_entry = ZIPDIR_FILENAME (zipd);
7051 int current_entry_len = zipd->filename_length;
7053 if (current_entry_len >= BUFFER_LENGTH (filename)
7054 && strncmp ((const char *) filename->data, current_entry,
7055 BUFFER_LENGTH (filename)) != 0)
7056 continue;
7057 found |= note_possible_classname (current_entry,
7058 current_entry_len);
7062 else
7064 BUFFER_RESET (filename);
7065 buffer_grow (filename, entry_length + package_length + 4);
7066 strcpy ((char *) filename->data, entry_name);
7067 filename->ptr = filename->data + entry_length;
7068 for (k = 0; k < package_length; k++)
7070 char ch = package_name[k];
7071 *filename->ptr++ = ch == '.' ? '/' : ch;
7073 *filename->ptr = '\0';
7075 dirp = opendir ((const char *) filename->data);
7076 if (dirp == NULL)
7077 continue;
7078 *filename->ptr++ = '/';
7079 for (;;)
7081 int len;
7082 const char *d_name;
7083 struct dirent *direntp = readdir (dirp);
7084 if (!direntp)
7085 break;
7086 d_name = direntp->d_name;
7087 len = strlen (direntp->d_name);
7088 buffer_grow (filename, len+1);
7089 strcpy ((char *) filename->ptr, d_name);
7090 found |= note_possible_classname ((const char *) filename->data + entry_length,
7091 package_length+len+1);
7093 if (dirp)
7094 closedir (dirp);
7098 free (filename->data);
7100 /* Here we should have a unified way of retrieving an entry, to be
7101 indexed. */
7102 if (!found)
7104 static int first = 1;
7105 if (first)
7107 error ("Can't find default package %qs. Check the CLASSPATH environment variable and the access to the archives", package_name);
7108 java_error_count++;
7109 first = 0;
7111 else
7112 parse_error_context (wfl, "Package %qs not found in import",
7113 package_name);
7114 current_jcf = saved_jcf;
7115 return;
7117 current_jcf = saved_jcf;
7120 /* Possibly find a type in the import on demands specified
7121 types. Returns 1 if an error occurred, 0 otherwise. Run through the
7122 entire list, to detected potential double definitions. */
7124 static int
7125 find_in_imports_on_demand (tree enclosing_type, tree class_type)
7127 tree class_type_name = TYPE_NAME (class_type);
7128 tree cl = NULL_TREE;
7129 int seen_once = -1; /* -1 when not set, 1 if seen once, >1 otherwise. */
7130 int to_return = -1; /* -1 when not set, 0 or 1 otherwise */
7131 tree node;
7132 tree import;
7134 if (enclosing_type && TYPE_IMPORT_DEMAND_LIST (enclosing_type))
7135 import = TYPE_IMPORT_DEMAND_LIST (enclosing_type);
7136 else
7137 import = ctxp->import_demand_list;
7139 for (; import; import = TREE_CHAIN (import))
7141 location_t saved_location = input_location;
7142 int access_check;
7143 const char *id_name;
7144 tree decl, type_name_copy;
7146 obstack_grow (&temporary_obstack,
7147 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
7148 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
7149 obstack_1grow (&temporary_obstack, '.');
7150 obstack_grow0 (&temporary_obstack,
7151 IDENTIFIER_POINTER (class_type_name),
7152 IDENTIFIER_LENGTH (class_type_name));
7153 id_name = obstack_finish (&temporary_obstack);
7155 if (! (node = maybe_get_identifier (id_name)))
7156 continue;
7158 /* Setup input_line so that it refers to the line of the import (in
7159 case we parse a class file and encounter errors */
7160 #ifdef USE_MAPPED_LOCATION
7161 input_location = EXPR_LOCATION (TREE_PURPOSE (import));
7162 #else
7163 input_line = EXPR_WFL_LINENO (TREE_PURPOSE (import));
7164 #endif
7166 type_name_copy = TYPE_NAME (class_type);
7167 TYPE_NAME (class_type) = node;
7168 QUALIFIED_P (node) = 1;
7169 decl = IDENTIFIER_CLASS_VALUE (node);
7170 access_check = -1;
7171 /* If there is no DECL set for the class or if the class isn't
7172 loaded and not seen in source yet, then load */
7173 if (!decl || ! CLASS_LOADED_P (TREE_TYPE (decl)))
7175 load_class (node, 0);
7176 decl = IDENTIFIER_CLASS_VALUE (node);
7178 if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7179 access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7180 false, NULL_TREE);
7181 else
7182 /* 6.6.1: Inner classes are subject to member access rules. */
7183 access_check = 0;
7185 input_location = saved_location;
7187 /* If the loaded class is not accessible or couldn't be loaded,
7188 we restore the original TYPE_NAME and process the next
7189 import. */
7190 if (access_check || !decl)
7192 TYPE_NAME (class_type) = type_name_copy;
7193 continue;
7196 /* If the loaded class is accessible, we keep a tab on it to
7197 detect and report multiple inclusions. */
7198 if (IS_A_CLASSFILE_NAME (node))
7200 if (seen_once < 0)
7202 cl = TREE_PURPOSE (import);
7203 seen_once = 1;
7205 else if (seen_once >= 0)
7207 tree location = (cl ? cl : TREE_PURPOSE (import));
7208 tree package = (cl ? EXPR_WFL_NODE (cl) :
7209 EXPR_WFL_NODE (TREE_PURPOSE (import)));
7210 seen_once++;
7211 parse_error_context
7212 (location,
7213 "Type %qs also potentially defined in package %qs",
7214 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7215 IDENTIFIER_POINTER (package));
7218 to_return = access_check;
7221 if (seen_once == 1)
7222 return to_return;
7223 else
7224 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7227 static tree
7228 resolve_package (tree pkg, tree *next, tree *type_name)
7230 tree current;
7231 tree decl = NULL_TREE;
7232 *type_name = NULL_TREE;
7234 /* The trick is to determine when the package name stops and were
7235 the name of something contained in the package starts. Then we
7236 return a fully qualified name of what we want to get. */
7238 *next = EXPR_WFL_QUALIFICATION (pkg);
7240 /* Try to progressively construct a type name */
7241 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7242 for (current = EXPR_WFL_QUALIFICATION (pkg);
7243 current; current = TREE_CHAIN (current))
7245 /* If we don't have what we're expecting, exit now. TYPE_NAME
7246 will be null and the error caught later. */
7247 if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7248 break;
7249 *type_name =
7250 merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7251 if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7253 /* resolve_package should be used in a loop, hence we
7254 point at this one to naturally process the next one at
7255 the next iteration. */
7256 *next = current;
7257 break;
7260 return decl;
7264 /* Check accessibility of inner classes according to member access rules.
7265 DECL is the inner class, ENCLOSING_DECL is the class from which the
7266 access is being attempted. */
7268 static void
7269 check_inner_class_access (tree decl, tree enclosing_decl, tree cl)
7271 const char *access;
7272 tree enclosing_decl_type;
7274 /* We don't issue an error message when CL is null. CL can be null
7275 as a result of processing a JDEP crafted by source_start_java_method
7276 for the purpose of patching its parm decl. But the error would
7277 have been already trapped when fixing the method's signature.
7278 DECL can also be NULL in case of earlier errors. */
7279 if (!decl || !cl)
7280 return;
7282 enclosing_decl_type = TREE_TYPE (enclosing_decl);
7284 if (CLASS_PRIVATE (decl))
7286 /* Access is permitted only within the body of the top-level
7287 class in which DECL is declared. */
7288 tree top_level = decl;
7289 while (DECL_CONTEXT (top_level))
7290 top_level = DECL_CONTEXT (top_level);
7291 while (DECL_CONTEXT (enclosing_decl))
7292 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7293 if (top_level == enclosing_decl)
7294 return;
7295 access = "private";
7297 else if (CLASS_PROTECTED (decl))
7299 tree decl_context;
7300 /* Access is permitted from within the same package... */
7301 if (in_same_package (decl, enclosing_decl))
7302 return;
7304 /* ... or from within the body of a subtype of the context in which
7305 DECL is declared. */
7306 decl_context = DECL_CONTEXT (decl);
7307 while (enclosing_decl)
7309 if (CLASS_INTERFACE (decl))
7311 if (interface_of_p (TREE_TYPE (decl_context),
7312 enclosing_decl_type))
7313 return;
7315 else
7317 /* Eww. The order of the arguments is different!! */
7318 if (inherits_from_p (enclosing_decl_type,
7319 TREE_TYPE (decl_context)))
7320 return;
7322 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7324 access = "protected";
7326 else if (! CLASS_PUBLIC (decl))
7328 /* Access is permitted only from within the same package as DECL. */
7329 if (in_same_package (decl, enclosing_decl))
7330 return;
7331 access = "non-public";
7333 else
7334 /* Class is public. */
7335 return;
7337 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7338 (CLASS_INTERFACE (decl) ? "interface" : "class"),
7339 lang_printable_name (decl, 2), access);
7342 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7343 foreign package, it must be PUBLIC. Return 0 if no access
7344 violations were found, 1 otherwise. If VERBOSE is true and an error
7345 was found, it is reported and accounted for. If CL is NULL then
7346 look it up with THIS_DECL. */
7348 static int
7349 check_pkg_class_access (tree class_name, tree cl, bool verbose, tree this_decl)
7351 tree type;
7353 if (!IDENTIFIER_CLASS_VALUE (class_name))
7354 return 0;
7356 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7357 return 0;
7359 if (!CLASS_PUBLIC (TYPE_NAME (type)))
7361 /* Access to a private class within the same package is
7362 allowed. */
7363 tree l, r;
7364 split_qualified_name (&l, &r, class_name);
7365 if (!QUALIFIED_P (class_name) && !ctxp->package)
7366 /* Both in the empty package. */
7367 return 0;
7368 if (l == ctxp->package)
7369 /* Both in the same package. */
7370 return 0;
7372 if (verbose)
7373 parse_error_context
7374 (cl == NULL ? lookup_cl (this_decl): cl,
7375 "Can't access %s %qs. Only public classes and interfaces in other packages can be accessed",
7376 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7377 IDENTIFIER_POINTER (class_name));
7378 return 1;
7380 return 0;
7383 /* Local variable declaration. */
7385 static void
7386 declare_local_variables (int modifier, tree type, tree vlist)
7388 tree decl, current, saved_type;
7389 tree type_wfl = NULL_TREE;
7390 int must_chain = 0;
7391 int final_p = 0;
7393 /* Push a new block if statements were seen between the last time we
7394 pushed a block and now. Keep a count of blocks to close */
7395 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7397 tree b = enter_block ();
7398 BLOCK_IS_IMPLICIT (b) = 1;
7401 if (modifier)
7403 size_t i;
7404 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7405 if (1 << i & modifier)
7406 break;
7407 if (modifier == ACC_FINAL)
7408 final_p = 1;
7409 else
7411 parse_error_context
7412 (ctxp->modifier_ctx [i],
7413 "Only %<final%> is allowed as a local variables modifier");
7414 return;
7418 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7419 hold the TYPE value if a new incomplete has to be created (as
7420 opposed to being found already existing and reused). */
7421 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7423 /* If TYPE is fully resolved and we don't have a reference, make one */
7424 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7426 /* Go through all the declared variables */
7427 for (current = vlist, saved_type = type; current;
7428 current = TREE_CHAIN (current), type = saved_type)
7430 tree other, real_type;
7431 tree wfl = TREE_PURPOSE (current);
7432 tree name = EXPR_WFL_NODE (wfl);
7433 tree init = TREE_VALUE (current);
7435 /* Process NAME, as it may specify extra dimension(s) for it */
7436 type = build_array_from_name (type, type_wfl, name, &name);
7438 /* Variable redefinition check */
7439 if ((other = lookup_name_in_blocks (name)))
7441 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7442 DECL_SOURCE_LINE (other));
7443 continue;
7446 /* Type adjustment. We may have just readjusted TYPE because
7447 the variable specified more dimensions. Make sure we have
7448 a reference if we can and don't have one already. */
7449 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7451 real_type = GET_REAL_TYPE (type);
7452 /* Never layout this decl. This will be done when its scope
7453 will be entered */
7454 decl = build_decl (VAR_DECL, name, real_type);
7455 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7456 DECL_FINAL (decl) = final_p;
7457 BLOCK_CHAIN_DECL (decl);
7459 /* Don't try to use an INIT statement when an error was found */
7460 if (init && java_error_count)
7461 init = NULL_TREE;
7463 /* Remember it if this is an initialized-upon-declaration final
7464 variable. */
7465 if (init && final_p)
7467 DECL_LOCAL_FINAL_IUD (decl) = 1;
7470 /* Add the initialization function to the current function's code */
7471 if (init)
7473 /* Name might have been readjusted */
7474 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7475 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7476 java_method_add_stmt (current_function_decl,
7477 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7478 init));
7481 /* Setup dependency the type of the decl */
7482 if (must_chain)
7484 jdep *dep;
7485 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7486 dep = CLASSD_LAST (ctxp->classd_list);
7487 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7490 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7493 /* Called during parsing. Build decls from argument list. */
7495 static void
7496 source_start_java_method (tree fndecl)
7498 tree tem;
7499 tree parm_decl;
7500 int i;
7502 if (!fndecl)
7503 return;
7505 current_function_decl = fndecl;
7507 /* New scope for the function */
7508 enter_block ();
7509 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7510 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7512 tree type = TREE_VALUE (tem);
7513 tree name = TREE_PURPOSE (tem);
7515 /* If type is incomplete. Create an incomplete decl and ask for
7516 the decl to be patched later */
7517 if (INCOMPLETE_TYPE_P (type))
7519 jdep *jdep;
7520 tree real_type = GET_REAL_TYPE (type);
7521 parm_decl = build_decl (PARM_DECL, name, real_type);
7522 type = obtain_incomplete_type (type);
7523 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7524 jdep = CLASSD_LAST (ctxp->classd_list);
7525 JDEP_MISC (jdep) = name;
7526 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7528 else
7529 parm_decl = build_decl (PARM_DECL, name, type);
7531 /* Remember if a local variable was declared final (via its
7532 TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7533 if (ARG_FINAL_P (tem))
7535 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7536 DECL_FINAL (parm_decl) = 1;
7539 BLOCK_CHAIN_DECL (parm_decl);
7541 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7542 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7543 nreverse (tem);
7544 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7545 DECL_MAX_LOCALS (current_function_decl) = i;
7548 /* Called during parsing. Creates an artificial method declaration. */
7550 static tree
7551 create_artificial_method (tree class, int flags, tree type,
7552 tree name, tree args)
7554 tree mdecl;
7555 location_t save_location = input_location;
7557 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (class));
7558 mdecl = make_node (FUNCTION_TYPE);
7559 TREE_TYPE (mdecl) = type;
7560 TYPE_ARG_TYPES (mdecl) = args;
7561 /* We used to compute the signature of MDECL here and then use
7562 add_method(), but that failed because our caller might modify
7563 the type of the returned method, which trashes the cache in
7564 get_type_from_signature(). */
7565 mdecl = add_method_1 (class, flags, name, mdecl);
7566 input_location = save_location;
7567 DECL_ARTIFICIAL (mdecl) = 1;
7568 return mdecl;
7571 /* Starts the body if an artificial method. */
7573 static void
7574 start_artificial_method_body (tree mdecl)
7576 #ifdef USE_MAPPED_LOCATION
7577 DECL_SOURCE_LOCATION (mdecl) = ctxp->file_start_location;
7578 DECL_FUNCTION_LAST_LINE (mdecl) = ctxp->file_start_location;
7579 #else
7580 DECL_SOURCE_LINE (mdecl) = 1;
7581 DECL_FUNCTION_LAST_LINE (mdecl) = 1;
7582 #endif
7583 source_start_java_method (mdecl);
7584 enter_block ();
7587 static void
7588 end_artificial_method_body (tree mdecl)
7590 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7591 It has to be evaluated first. (if mdecl is current_function_decl,
7592 we have an undefined behavior if no temporary variable is used.) */
7593 tree b = exit_block ();
7594 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7595 exit_block ();
7598 /* Dump a tree of some kind. This is a convenience wrapper for the
7599 dump_* functions in tree-dump.c. */
7600 static void
7601 dump_java_tree (enum tree_dump_index phase, tree t)
7603 FILE *stream;
7604 int flags;
7606 stream = dump_begin (phase, &flags);
7607 flags |= TDF_SLIM;
7608 if (stream)
7610 dump_node (t, flags, stream);
7611 dump_end (phase, stream);
7615 /* Terminate a function and expand its body. */
7617 static void
7618 source_end_java_method (void)
7620 tree fndecl = current_function_decl;
7622 if (!fndecl)
7623 return;
7625 java_parser_context_save_global ();
7626 #ifdef USE_MAPPED_LOCATION
7627 input_location = ctxp->last_ccb_indent1;
7628 #else
7629 input_line = ctxp->last_ccb_indent1;
7630 #endif
7632 /* Turn function bodies with only a NOP expr null, so they don't get
7633 generated at all and we won't get warnings when using the -W
7634 -Wall flags. */
7635 if (IS_EMPTY_STMT (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))))
7636 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7638 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7639 && ! flag_emit_class_files)
7640 finish_method (fndecl);
7642 current_function_decl = NULL_TREE;
7643 java_parser_context_restore_global ();
7644 current_function_decl = NULL_TREE;
7647 /* Record EXPR in the current function block. Complements compound
7648 expression second operand if necessary. */
7650 tree
7651 java_method_add_stmt (tree fndecl, tree expr)
7653 if (!GET_CURRENT_BLOCK (fndecl))
7654 return NULL_TREE;
7655 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7658 static tree
7659 add_stmt_to_block (tree b, tree type, tree stmt)
7661 tree body = BLOCK_EXPR_BODY (b), c;
7663 if (java_error_count)
7664 return body;
7666 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7667 return body;
7669 BLOCK_EXPR_BODY (b) = c;
7670 TREE_SIDE_EFFECTS (c) = 1;
7671 return c;
7674 /* Lays out the methods for the classes seen so far. */
7676 void
7677 java_layout_seen_class_methods (void)
7679 tree previous_list = all_class_list;
7680 tree end = NULL_TREE;
7681 tree current;
7683 while (1)
7685 for (current = previous_list;
7686 current != end; current = TREE_CHAIN (current))
7688 tree decl = TREE_VALUE (current);
7689 tree cls = TREE_TYPE (decl);
7691 input_location = DECL_SOURCE_LOCATION (decl);
7693 if (! CLASS_LOADED_P (cls))
7694 load_class (cls, 0);
7696 layout_class_methods (cls);
7699 /* Note that new classes might have been added while laying out
7700 methods, changing the value of all_class_list. */
7702 if (previous_list != all_class_list)
7704 end = previous_list;
7705 previous_list = all_class_list;
7707 else
7708 break;
7712 static GTY(()) tree stop_reordering;
7713 void
7714 java_reorder_fields (void)
7716 tree current;
7718 for (current = gclass_list; current; current = TREE_CHAIN (current))
7720 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7722 if (current_class == stop_reordering)
7723 break;
7725 /* Reverse the fields, but leave the dummy field in front.
7726 Fields are already ordered for Object and Class */
7727 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7728 && current_class != class_type_node)
7730 /* If the dummy field is there, reverse the right fields and
7731 just layout the type for proper fields offset */
7732 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7734 tree fields = TYPE_FIELDS (current_class);
7735 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7736 TYPE_SIZE (current_class) = NULL_TREE;
7738 /* We don't have a dummy field, we need to layout the class,
7739 after having reversed the fields */
7740 else
7742 TYPE_FIELDS (current_class) =
7743 nreverse (TYPE_FIELDS (current_class));
7744 TYPE_SIZE (current_class) = NULL_TREE;
7748 /* There are cases were gclass_list will be empty. */
7749 if (gclass_list)
7750 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7753 /* Layout the methods of all classes loaded in one way or another.
7754 Check methods of source parsed classes. Then reorder the
7755 fields and layout the classes or the type of all source parsed
7756 classes */
7758 void
7759 java_layout_classes (void)
7761 tree current;
7762 int save_error_count = java_error_count;
7764 /* Layout the methods of all classes seen so far */
7765 java_layout_seen_class_methods ();
7766 java_parse_abort_on_error ();
7767 all_class_list = NULL_TREE;
7769 /* Then check the methods of all parsed classes */
7770 for (current = gclass_list; current; current = TREE_CHAIN (current))
7771 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7772 java_check_methods (TREE_VALUE (current));
7773 java_parse_abort_on_error ();
7775 for (current = gclass_list; current; current = TREE_CHAIN (current))
7777 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7778 layout_class (current_class);
7780 /* Error reported by the caller */
7781 if (java_error_count)
7782 return;
7785 /* We might have reloaded classes durign the process of laying out
7786 classes for code generation. We must layout the methods of those
7787 late additions, as constructor checks might use them */
7788 java_layout_seen_class_methods ();
7789 java_parse_abort_on_error ();
7792 /* Expand methods in the current set of classes remembered for
7793 generation. */
7795 static void
7796 java_complete_expand_classes (void)
7798 tree current;
7800 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7801 if (!INNER_CLASS_DECL_P (current))
7802 java_complete_expand_class (current);
7805 /* Expand the methods found in OUTER, starting first by OUTER's inner
7806 classes, if any. */
7808 static void
7809 java_complete_expand_class (tree outer)
7811 tree inner_list;
7813 /* We need to go after all inner classes and start expanding them,
7814 starting with most nested ones. We have to do that because nested
7815 classes might add functions to outer classes */
7817 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7818 inner_list; inner_list = TREE_CHAIN (inner_list))
7819 java_complete_expand_class (TREE_PURPOSE (inner_list));
7821 java_complete_expand_methods (outer);
7824 /* Expand methods registered in CLASS_DECL. The general idea is that
7825 we expand regular methods first. This allows us get an estimate on
7826 how outer context local alias fields are really used so we can add
7827 to the constructor just enough code to initialize them properly (it
7828 also lets us generate finit$ correctly.) Then we expand the
7829 constructors and then <clinit>. */
7831 static void
7832 java_complete_expand_methods (tree class_decl)
7834 tree clinit, decl, first_decl;
7836 output_class = current_class = TREE_TYPE (class_decl);
7838 /* Pre-expand <clinit> to figure whether we really need it or
7839 not. If we do need it, we pre-expand the static fields so they're
7840 ready to be used somewhere else. <clinit> will be fully expanded
7841 after we processed the constructors. */
7842 first_decl = TYPE_METHODS (current_class);
7843 clinit = maybe_generate_pre_expand_clinit (current_class);
7845 /* Then generate finit$ (if we need to) because constructors will
7846 try to use it.*/
7847 if (TYPE_FINIT_STMT_LIST (current_class))
7848 java_complete_expand_method (generate_finit (current_class));
7850 /* Then generate instinit$ (if we need to) because constructors will
7851 try to use it. */
7852 if (TYPE_II_STMT_LIST (current_class))
7853 java_complete_expand_method (generate_instinit (current_class));
7855 /* Now do the constructors */
7856 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7858 if (!DECL_CONSTRUCTOR_P (decl))
7859 continue;
7860 java_complete_expand_method (decl);
7863 /* First, do the ordinary methods. */
7864 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7866 /* Ctors aren't part of this batch. */
7867 if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7868 continue;
7870 /* Skip abstract or native methods -- but do handle native
7871 methods when generating JNI stubs. */
7872 if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7874 DECL_FUNCTION_BODY (decl) = NULL_TREE;
7875 continue;
7878 if (METHOD_NATIVE (decl))
7880 tree body;
7881 current_function_decl = decl;
7882 body = build_jni_stub (decl);
7883 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7886 java_complete_expand_method (decl);
7889 /* If there is indeed a <clinit>, fully expand it now */
7890 if (clinit)
7892 /* Prevent the use of `this' inside <clinit> */
7893 ctxp->explicit_constructor_p = 1;
7894 java_complete_expand_method (clinit);
7895 ctxp->explicit_constructor_p = 0;
7898 /* We might have generated a class$ that we now want to expand */
7899 if (TYPE_DOT_CLASS (current_class))
7900 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7902 /* Now verify constructor circularity (stop after the first one we
7903 prove wrong.) */
7904 if (!CLASS_INTERFACE (class_decl))
7905 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7906 if (DECL_CONSTRUCTOR_P (decl)
7907 && verify_constructor_circularity (decl, decl))
7908 break;
7911 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7912 safely used in some other methods/constructors. */
7914 static tree
7915 maybe_generate_pre_expand_clinit (tree class_type)
7917 tree current, mdecl;
7919 if (!TYPE_CLINIT_STMT_LIST (class_type))
7920 return NULL_TREE;
7922 /* Go through all static fields and pre expand them */
7923 for (current = TYPE_FIELDS (class_type); current;
7924 current = TREE_CHAIN (current))
7925 if (FIELD_STATIC (current))
7926 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7928 /* Then build the <clinit> method */
7929 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7930 clinit_identifier_node, end_params_node);
7931 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7932 mdecl, NULL_TREE);
7933 start_artificial_method_body (mdecl);
7935 /* We process the list of assignment we produced as the result of
7936 the declaration of initialized static field and add them as
7937 statement to the <clinit> method. */
7938 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7939 current = TREE_CHAIN (current))
7941 tree stmt = current;
7942 /* We build the assignment expression that will initialize the
7943 field to its value. There are strict rules on static
7944 initializers (8.5). FIXME */
7945 if (TREE_CODE (stmt) != BLOCK && !IS_EMPTY_STMT (stmt))
7946 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7947 java_method_add_stmt (mdecl, stmt);
7950 end_artificial_method_body (mdecl);
7952 /* Now we want to place <clinit> as the last method (because we need
7953 it at least for interface so that it doesn't interfere with the
7954 dispatch table based lookup. */
7955 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7957 current = TREE_CHAIN (TYPE_METHODS (class_type));
7958 TYPE_METHODS (class_type) = current;
7960 while (TREE_CHAIN (current))
7961 current = TREE_CHAIN (current);
7963 TREE_CHAIN (current) = mdecl;
7964 TREE_CHAIN (mdecl) = NULL_TREE;
7967 return mdecl;
7970 /* Analyzes a method body and look for something that isn't a
7971 MODIFY_EXPR with a constant value. */
7973 static int
7974 analyze_clinit_body (tree this_class, tree bbody)
7976 while (bbody)
7977 switch (TREE_CODE (bbody))
7979 case BLOCK:
7980 bbody = BLOCK_EXPR_BODY (bbody);
7981 break;
7983 case EXPR_WITH_FILE_LOCATION:
7984 bbody = EXPR_WFL_NODE (bbody);
7985 break;
7987 case COMPOUND_EXPR:
7988 if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
7989 return 1;
7990 bbody = TREE_OPERAND (bbody, 1);
7991 break;
7993 case MODIFY_EXPR:
7994 /* If we're generating to class file and we're dealing with an
7995 array initialization, we return 1 to keep <clinit> */
7996 if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
7997 && flag_emit_class_files)
7998 return 1;
8000 /* There are a few cases where we're required to keep
8001 <clinit>:
8002 - If this is an assignment whose operand is not constant,
8003 - If this is an assignment to a non-initialized field,
8004 - If this field is not a member of the current class.
8006 return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
8007 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
8008 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
8010 default:
8011 return 1;
8013 return 0;
8017 /* See whether we could get rid of <clinit>. Criteria are: all static
8018 final fields have constant initial values and the body of <clinit>
8019 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
8021 static int
8022 maybe_yank_clinit (tree mdecl)
8024 tree type, current;
8025 tree fbody, bbody;
8027 if (!DECL_CLINIT_P (mdecl))
8028 return 0;
8030 /* If the body isn't empty, then we keep <clinit>. Note that if
8031 we're emitting classfiles, this isn't enough not to rule it
8032 out. */
8033 fbody = DECL_FUNCTION_BODY (mdecl);
8034 bbody = BLOCK_EXPR_BODY (fbody);
8035 if (bbody && bbody != error_mark_node)
8036 bbody = BLOCK_EXPR_BODY (bbody);
8037 else
8038 return 0;
8039 if (bbody && ! flag_emit_class_files && !IS_EMPTY_STMT (bbody))
8040 return 0;
8042 type = DECL_CONTEXT (mdecl);
8043 current = TYPE_FIELDS (type);
8045 for (current = (current ? TREE_CHAIN (current) : current);
8046 current; current = TREE_CHAIN (current))
8048 tree f_init;
8050 /* We're not interested in non-static fields. */
8051 if (!FIELD_STATIC (current))
8052 continue;
8054 /* Nor in fields without initializers. */
8055 f_init = DECL_INITIAL (current);
8056 if (f_init == NULL_TREE)
8057 continue;
8059 /* Anything that isn't String or a basic type is ruled out -- or
8060 if we know how to deal with it (when doing things natively) we
8061 should generated an empty <clinit> so that SUID are computed
8062 correctly. */
8063 if (! JSTRING_TYPE_P (TREE_TYPE (current))
8064 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
8065 return 0;
8067 if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
8068 return 0;
8071 /* Now we analyze the method body and look for something that
8072 isn't a MODIFY_EXPR */
8073 if (bbody && !IS_EMPTY_STMT (bbody) && analyze_clinit_body (type, bbody))
8074 return 0;
8076 /* Get rid of <clinit> in the class' list of methods */
8077 if (TYPE_METHODS (type) == mdecl)
8078 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
8079 else
8080 for (current = TYPE_METHODS (type); current;
8081 current = TREE_CHAIN (current))
8082 if (TREE_CHAIN (current) == mdecl)
8084 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
8085 break;
8088 return 1;
8091 /* Install the argument from MDECL. Suitable to completion and
8092 expansion of mdecl's body. */
8094 void
8095 start_complete_expand_method (tree mdecl)
8097 tree tem;
8099 pushlevel (1); /* Prepare for a parameter push */
8100 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
8101 DECL_ARGUMENTS (mdecl) = tem;
8103 for (; tem; tem = TREE_CHAIN (tem))
8105 /* TREE_CHAIN (tem) will change after pushdecl. */
8106 tree next = TREE_CHAIN (tem);
8107 tree type = TREE_TYPE (tem);
8108 if (targetm.calls.promote_prototypes (type)
8109 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8110 && INTEGRAL_TYPE_P (type))
8111 type = integer_type_node;
8112 DECL_ARG_TYPE (tem) = type;
8113 layout_decl (tem, 0);
8114 pushdecl (tem);
8115 /* Re-install the next so that the list is kept and the loop
8116 advances. */
8117 TREE_CHAIN (tem) = next;
8119 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8120 input_location = DECL_SOURCE_LOCATION (mdecl);
8121 build_result_decl (mdecl);
8125 /* Complete and expand a method. */
8127 static void
8128 java_complete_expand_method (tree mdecl)
8130 tree fbody, block_body, exception_copy;
8132 current_function_decl = mdecl;
8133 /* Fix constructors before expanding them */
8134 if (DECL_CONSTRUCTOR_P (mdecl))
8135 fix_constructors (mdecl);
8137 /* Expand functions that have a body */
8138 if (!DECL_FUNCTION_BODY (mdecl))
8139 return;
8141 fbody = DECL_FUNCTION_BODY (mdecl);
8142 block_body = BLOCK_EXPR_BODY (fbody);
8143 exception_copy = NULL_TREE;
8145 current_function_decl = mdecl;
8147 if (! quiet_flag)
8148 fprintf (stderr, " [%s.",
8149 lang_printable_name (DECL_CONTEXT (mdecl), 0));
8150 announce_function (mdecl);
8151 if (! quiet_flag)
8152 fprintf (stderr, "]");
8154 /* Prepare the function for tree completion */
8155 start_complete_expand_method (mdecl);
8157 /* Install the current this */
8158 current_this = (!METHOD_STATIC (mdecl) ?
8159 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8161 /* Purge the `throws' list of unchecked exceptions (we save a copy
8162 of the list and re-install it later.) */
8163 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8164 purge_unchecked_exceptions (mdecl);
8166 /* Install exceptions thrown with `throws' */
8167 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8169 if (block_body != NULL_TREE)
8171 block_body = java_complete_tree (block_body);
8173 /* Before we check initialization, attached all class initialization
8174 variable to the block_body */
8175 htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8176 attach_init_test_initialization_flags, block_body);
8178 if (! METHOD_NATIVE (mdecl))
8180 check_for_initialization (block_body, mdecl);
8182 /* Go through all the flags marking the initialization of
8183 static variables and see whether they're definitively
8184 assigned, in which case the type is remembered as
8185 definitively initialized in MDECL. */
8186 if (STATIC_CLASS_INIT_OPT_P ())
8188 /* Always register the context as properly initialized in
8189 MDECL. This used with caution helps removing extra
8190 initialization of self. */
8191 if (METHOD_STATIC (mdecl))
8193 *(htab_find_slot
8194 (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8195 DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8199 ctxp->explicit_constructor_p = 0;
8202 BLOCK_EXPR_BODY (fbody) = block_body;
8204 /* If we saw a return but couldn't evaluate it properly, we'll have
8205 an error_mark_node here. */
8206 if (block_body != error_mark_node
8207 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8208 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE)
8209 missing_return_error (current_function_decl);
8211 /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8212 maybe_yank_clinit (mdecl);
8214 /* Pop the current level, with special measures if we found errors. */
8215 if (java_error_count)
8216 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8217 poplevel (1, 0, 1);
8219 /* Pop the exceptions and sanity check */
8220 POP_EXCEPTIONS();
8221 if (currently_caught_type_list)
8222 abort ();
8224 /* Restore the copy of the list of exceptions. */
8225 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8228 /* For with each class for which there's code to generate. */
8230 static void
8231 java_expand_method_bodies (tree class)
8233 tree decl;
8234 for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8236 tree block;
8238 if (! DECL_FUNCTION_BODY (decl))
8239 continue;
8241 current_function_decl = decl;
8243 block = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8245 /* Save the function body for gimplify and inlining. */
8246 DECL_SAVED_TREE (decl) = block;
8248 /* It's time to assign the variable flagging static class
8249 initialization based on which classes invoked static methods
8250 are definitely initializing. This should be flagged. */
8251 if (STATIC_CLASS_INIT_OPT_P ())
8253 tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8254 for (; list != NULL_TREE; list = TREE_CHAIN (list))
8256 /* Executed for each statement calling a static function.
8257 LIST is a TREE_LIST whose PURPOSE is the called function
8258 and VALUE is a compound whose second operand can be patched
8259 with static class initialization flag assignments. */
8261 tree called_method = TREE_PURPOSE (list);
8262 tree compound = TREE_VALUE (list);
8263 tree assignment_compound_list
8264 = build_tree_list (called_method, NULL);
8266 /* For each class definitely initialized in
8267 CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8268 assignment to the class initialization flag. */
8269 htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8270 emit_test_initialization,
8271 assignment_compound_list);
8273 if (TREE_VALUE (assignment_compound_list))
8274 TREE_OPERAND (compound, 1)
8275 = TREE_VALUE (assignment_compound_list);
8279 /* Expand the function body. */
8280 source_end_java_method ();
8286 /* This section of the code deals with accessing enclosing context
8287 fields either directly by using the relevant access to this$<n> or
8288 by invoking an access method crafted for that purpose. */
8290 /* Build the necessary access across nested class boundaries.
8291 This routine could be optimized to cache previous result
8292 (decl, current_class and returned access). When an access method
8293 needs to be generated, it always takes the form of a read. It might
8294 be later turned into a write by calling nested_field_access_fix. */
8296 static tree
8297 build_nested_field_access (tree id, tree decl)
8299 tree access = NULL_TREE;
8300 tree ctx = NULL_TREE;
8301 tree decl_ctx = DECL_CONTEXT (decl);
8302 bool is_static = FIELD_STATIC (decl);
8304 if (DECL_CONTEXT (TYPE_NAME (current_class)))
8305 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8307 /* For non-static fields, if the immediate enclosing context of the
8308 current class is the field decl's class or inherits from it,
8309 build the access as `this$<n>.<field>'. Note that we will break
8310 the `private' barrier if we're not emitting bytecodes. */
8311 if (!is_static
8312 && ctx
8313 && (ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8314 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files))
8316 tree thisn = build_current_thisn (current_class);
8317 access = make_qualified_primary (build_wfl_node (thisn),
8318 id, EXPR_WFL_LINECOL (id));
8320 /* Otherwise, generate and use accessor methods for the field as
8321 needed. */
8322 else
8324 int lc = EXPR_WFL_LINECOL (id);
8326 /* Now we chain the required number of calls to the access$0 to
8327 get a hold to the enclosing instance we need for a non-static
8328 field, and then we build the field access. */
8329 if (!is_static)
8330 access = build_access_to_thisn (current_class, decl_ctx, lc);
8332 /* If the field is private and we're generating bytecode, then
8333 we generate an access method. */
8334 if (FIELD_PRIVATE (decl) && flag_emit_class_files)
8336 tree name = build_nested_field_access_methods (decl);
8337 access = build_nested_field_access_expr (lc, decl_ctx,
8338 name, access, NULL_TREE);
8340 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'
8341 for non-static fields.
8342 Once again we break the `private' access rule from a foreign
8343 class. */
8344 else if (is_static)
8346 tree class_name = DECL_NAME (TYPE_NAME (decl_ctx));
8347 access
8348 = make_qualified_primary (build_wfl_node (class_name), id, lc);
8350 else
8351 access = make_qualified_primary (access, id, lc);
8354 return resolve_expression_name (access, NULL);
8357 /* Return a nonzero value if DECL describes a field access across nested
8358 class boundaries. That is, DECL is in a class that either encloses,
8359 is enclosed by or shares a common enclosing class with, the class
8360 TYPE. */
8362 static int
8363 nested_field_access_p (tree type, tree decl)
8365 bool is_static = false;
8366 tree decl_type = DECL_CONTEXT (decl);
8367 tree type_root, decl_type_root;
8369 if (decl_type == type
8370 || (TREE_CODE (decl) != FIELD_DECL && TREE_CODE (decl) != VAR_DECL))
8371 return 0;
8373 if (!INNER_CLASS_TYPE_P (type)
8374 && !(TREE_CODE (decl_type) == RECORD_TYPE
8375 && INNER_CLASS_TYPE_P (decl_type)))
8376 return 0;
8378 is_static = FIELD_STATIC (decl);
8380 /* If TYPE extends the declaration context of the non-static
8381 field we're trying to access, then this isn't a nested field
8382 access we need to worry about. */
8383 if (!is_static && inherits_from_p (type, decl_type))
8384 return 0;
8386 for (type_root = type;
8387 DECL_CONTEXT (TYPE_NAME (type_root));
8388 type_root = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type_root))))
8390 if (type_root == decl_type)
8391 return 1;
8394 if (TREE_CODE (decl_type) == RECORD_TYPE
8395 && INNER_CLASS_TYPE_P (decl_type))
8397 for (decl_type_root = decl_type;
8398 DECL_CONTEXT (TYPE_NAME (decl_type_root));
8399 decl_type_root
8400 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (decl_type_root))))
8402 if (decl_type_root == type)
8403 return 1;
8406 else
8407 decl_type_root = decl_type;
8409 if (type_root == decl_type_root)
8410 return 1;
8412 /* Before we give up, see whether it is a non-static field
8413 inherited from the enclosing context we are considering. */
8414 if (!DECL_CONTEXT (TYPE_NAME (type_root))
8415 && !is_static
8416 && inherits_from_p (type_root, decl_type))
8417 return 1;
8419 return 0;
8422 /* Return a nonzero value if NODE represents a cross-nested-class
8423 access that has already been expanded. As a side effect, it returns
8424 the name of the field being accessed and the argument passed to the
8425 access function, suitable for a regeneration of the access method
8426 call if necessary. */
8428 static int
8429 nested_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8430 tree *arg)
8432 int identified = 0;
8434 if (TREE_CODE (node) != CALL_EXPR)
8435 return 0;
8437 /* Well, GCJ generates slightly different tree nodes when compiling
8438 to native or bytecodes. It's the case for function calls. */
8440 if (flag_emit_class_files
8441 && TREE_CODE (node) == CALL_EXPR
8442 && NESTED_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8443 identified = 1;
8444 else if (!flag_emit_class_files)
8446 node = TREE_OPERAND (node, 0);
8448 if (node && TREE_OPERAND (node, 0)
8449 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8451 node = TREE_OPERAND (node, 0);
8452 if (TREE_OPERAND (node, 0)
8453 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8454 && (NESTED_FIELD_ACCESS_IDENTIFIER_P
8455 (DECL_NAME (TREE_OPERAND (node, 0)))))
8456 identified = 1;
8460 if (identified && name && arg_type && arg)
8462 tree argument = TREE_OPERAND (node, 1);
8463 *name = DECL_NAME (TREE_OPERAND (node, 0));
8465 /* The accessors for static fields do not take in a this$<n> argument,
8466 so we take the class name from the accessor's context instead. */
8467 if (argument)
8469 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8470 *arg = TREE_VALUE (argument);
8472 else
8474 *arg_type = DECL_CONTEXT (TREE_OPERAND (node, 0));
8475 *arg = NULL_TREE;
8478 return identified;
8481 /* Detect in NODE cross-nested-class field read access and
8482 transform it into a write with RHS as an argument. This function
8483 is called from the java_complete_lhs when an assignment to a LHS can
8484 be identified. */
8486 static tree
8487 nested_field_access_fix (tree wfl, tree node, tree rhs)
8489 tree name, arg_type, arg;
8491 if (nested_field_expanded_access_p (node, &name, &arg_type, &arg))
8493 node = build_nested_field_access_expr (EXPR_WFL_LINECOL (wfl),
8494 arg_type, name, arg, rhs);
8495 return java_complete_tree (node);
8497 return NULL_TREE;
8500 /* Construct the expression that calls an access method:
8501 <type>.access$<n>(<arg1> [, <arg2>]);
8503 ARG2 can be NULL and will be omitted in that case. It will denote a
8504 read access. */
8506 static tree
8507 build_nested_field_access_expr (int lc, tree type, tree access_method_name,
8508 tree arg1, tree arg2)
8510 tree args, cn, access;
8512 if (arg1)
8513 args = build_tree_list (NULL_TREE, arg1);
8514 else
8515 args = NULL_TREE;
8517 if (arg2)
8519 if (args)
8520 args = tree_cons (NULL_TREE, arg2, args);
8521 else
8522 args = build_tree_list (NULL_TREE, arg2);
8525 access
8526 = build_method_invocation (build_wfl_node (access_method_name), args);
8527 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8529 return make_qualified_primary (cn, access, lc);
8532 /* Build the name of a synthetic accessor used to access class members
8533 across nested class boundaries. */
8535 static tree
8536 build_new_access_id (void)
8538 static int access_n_counter = 1;
8539 char buffer [128];
8541 sprintf (buffer, "access$%d", access_n_counter++);
8542 return get_identifier (buffer);
8545 /* Create the static access functions for the cross-nested-class field DECL.
8546 We define a read:
8547 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8548 return inst$.field;
8550 and a write access:
8551 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8552 TREE_TYPE (<field>) value$) {
8553 return inst$.field = value$;
8555 For static fields, these methods are generated without the instance
8556 parameter.
8557 We should have a usage flag on the DECL so we can lazily turn the ones
8558 we're using for code generation. FIXME.
8561 static tree
8562 build_nested_field_access_methods (tree decl)
8564 tree id, args, stmt, mdecl, class_name = NULL_TREE;
8565 bool is_static = FIELD_STATIC (decl);
8567 if (FIELD_NESTED_ACCESS_P (decl))
8568 return FIELD_NESTED_ACCESS (decl);
8570 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8572 /* Create the identifier and a function named after it. */
8573 id = build_new_access_id ();
8575 /* The identifier is marked as bearing the name of a generated write
8576 access function for outer field accessed from inner classes. */
8577 NESTED_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8579 /* Create the read access. */
8580 if (!is_static)
8582 args = build_tree_list (inst_id,
8583 build_pointer_type (DECL_CONTEXT (decl)));
8584 TREE_CHAIN (args) = end_params_node;
8585 stmt = make_qualified_primary (build_wfl_node (inst_id),
8586 build_wfl_node (DECL_NAME (decl)), 0);
8588 else
8590 args = end_params_node;
8591 class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
8592 stmt = make_qualified_primary (build_wfl_node (class_name),
8593 build_wfl_node (DECL_NAME (decl)), 0);
8595 stmt = build_return (0, stmt);
8596 mdecl = build_nested_field_access_method (DECL_CONTEXT (decl),
8597 TREE_TYPE (decl), id, args, stmt);
8598 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8600 /* Create the write access method. No write access for final variable */
8601 if (!FIELD_FINAL (decl))
8603 if (!is_static)
8605 args = build_tree_list (inst_id,
8606 build_pointer_type (DECL_CONTEXT (decl)));
8607 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8608 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8609 stmt = make_qualified_primary (build_wfl_node (inst_id),
8610 build_wfl_node (DECL_NAME (decl)),
8613 else
8615 args = build_tree_list (wpv_id, TREE_TYPE (decl));
8616 TREE_CHAIN (args) = end_params_node;
8617 stmt = make_qualified_primary (build_wfl_node (class_name),
8618 build_wfl_node (DECL_NAME (decl)),
8621 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8622 build_wfl_node (wpv_id)));
8623 mdecl = build_nested_field_access_method (DECL_CONTEXT (decl),
8624 TREE_TYPE (decl), id,
8625 args, stmt);
8627 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8629 /* Return the access name */
8630 return FIELD_NESTED_ACCESS (decl) = id;
8633 /* Build a field access method NAME. */
8635 static tree
8636 build_nested_field_access_method (tree class, tree type, tree name,
8637 tree args, tree body)
8639 tree saved_current_function_decl, mdecl;
8641 /* Create the method */
8642 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8643 fix_method_argument_names (args, mdecl);
8644 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8646 /* Attach the method body. */
8647 saved_current_function_decl = current_function_decl;
8648 start_artificial_method_body (mdecl);
8649 java_method_add_stmt (mdecl, body);
8650 end_artificial_method_body (mdecl);
8651 current_function_decl = saved_current_function_decl;
8653 return mdecl;
8657 /* This section deals with building access function necessary for
8658 certain kinds of method invocation from inner classes. */
8660 static tree
8661 build_outer_method_access_method (tree decl)
8663 tree saved_current_function_decl, mdecl;
8664 tree args = NULL_TREE, call_args = NULL_TREE;
8665 tree carg, id, body, class;
8666 char buffer [80];
8667 int parm_id_count = 0;
8669 /* Test this abort with an access to a private field */
8670 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8671 abort ();
8673 /* Check the cache first */
8674 if (DECL_FUNCTION_INNER_ACCESS (decl))
8675 return DECL_FUNCTION_INNER_ACCESS (decl);
8677 class = DECL_CONTEXT (decl);
8679 /* Obtain an access identifier and mark it */
8680 id = build_new_access_id ();
8681 NESTED_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8683 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8684 /* Create the arguments, as much as the original */
8685 for (; carg && carg != end_params_node;
8686 carg = TREE_CHAIN (carg))
8688 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8689 args = chainon (args, build_tree_list (get_identifier (buffer),
8690 TREE_VALUE (carg)));
8692 args = chainon (args, end_params_node);
8694 /* Create the method */
8695 mdecl = create_artificial_method (class, ACC_STATIC,
8696 TREE_TYPE (TREE_TYPE (decl)), id, args);
8697 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8698 /* There is a potential bug here. We should be able to use
8699 fix_method_argument_names, but then arg names get mixed up and
8700 eventually a constructor will have its this$0 altered and the
8701 outer context won't be assignment properly. The testcase is
8702 stub.java FIXME */
8703 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8705 /* Attach the method body. */
8706 saved_current_function_decl = current_function_decl;
8707 start_artificial_method_body (mdecl);
8709 /* The actual method invocation uses the same args. When invoking a
8710 static methods that way, we don't want to skip the first
8711 argument. */
8712 carg = args;
8713 if (!METHOD_STATIC (decl))
8714 carg = TREE_CHAIN (carg);
8715 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8716 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8717 call_args);
8719 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8720 call_args);
8721 if (!METHOD_STATIC (decl))
8722 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8723 body, 0);
8724 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8725 body = build_return (0, body);
8726 java_method_add_stmt (mdecl,body);
8727 end_artificial_method_body (mdecl);
8728 current_function_decl = saved_current_function_decl;
8730 /* Back tag the access function so it know what it accesses */
8731 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8733 /* Tag the current method so it knows it has an access generated */
8734 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8738 /* This section of the code deals with building expressions to access
8739 the enclosing instance of an inner class. The enclosing instance is
8740 kept in a generated field called this$<n>, with <n> being the
8741 inner class nesting level (starting from 0.) */
8743 /* Build an access to a given this$<n>, always chaining access call to
8744 others. Access methods to this$<n> are build on the fly if
8745 necessary. This CAN'T be used to solely access this$<n-1> from
8746 this$<n> (which alway yield to special cases and optimization, see
8747 for example build_nested_field_access). */
8749 static tree
8750 build_access_to_thisn (tree from, tree to, int lc)
8752 tree access = NULL_TREE;
8754 while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8756 if (!access)
8758 access = build_current_thisn (from);
8759 access = build_wfl_node (access);
8761 else
8763 tree access0_wfl, cn;
8765 maybe_build_thisn_access_method (from);
8766 access0_wfl = build_wfl_node (access0_identifier_node);
8767 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8768 EXPR_WFL_LINECOL (access0_wfl) = lc;
8769 access = build_tree_list (NULL_TREE, access);
8770 access = build_method_invocation (access0_wfl, access);
8771 access = make_qualified_primary (cn, access, lc);
8774 /* If FROM isn't an inner class, that's fine, we've done enough.
8775 What we're looking for can be accessed from there. */
8776 from = DECL_CONTEXT (TYPE_NAME (from));
8777 if (!from)
8778 break;
8779 from = TREE_TYPE (from);
8781 return access;
8784 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8785 is returned if nothing needs to be generated. Otherwise, the method
8786 generated and a method decl is returned.
8788 NOTE: These generated methods should be declared in a class file
8789 attribute so that they can't be referred to directly. */
8791 static tree
8792 maybe_build_thisn_access_method (tree type)
8794 tree mdecl, args, stmt, rtype;
8795 tree saved_current_function_decl;
8797 /* If TYPE is a top-level class, no access method is required.
8798 If there already is such an access method, bail out. */
8799 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8800 return NULL_TREE;
8802 /* We generate the method. The method looks like:
8803 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8805 args = build_tree_list (inst_id, build_pointer_type (type));
8806 TREE_CHAIN (args) = end_params_node;
8807 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8808 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8809 access0_identifier_node, args);
8810 fix_method_argument_names (args, mdecl);
8811 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8812 stmt = build_current_thisn (type);
8813 stmt = make_qualified_primary (build_wfl_node (inst_id),
8814 build_wfl_node (stmt), 0);
8815 stmt = build_return (0, stmt);
8817 saved_current_function_decl = current_function_decl;
8818 start_artificial_method_body (mdecl);
8819 java_method_add_stmt (mdecl, stmt);
8820 end_artificial_method_body (mdecl);
8821 current_function_decl = saved_current_function_decl;
8823 CLASS_ACCESS0_GENERATED_P (type) = 1;
8825 return mdecl;
8828 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8829 the first level of innerclassing. this$1 for the next one, etc...
8830 This function can be invoked with TYPE to NULL, available and then
8831 has to count the parser context. */
8833 static GTY(()) tree saved_thisn;
8834 static GTY(()) tree saved_type;
8836 static tree
8837 build_current_thisn (tree type)
8839 static int saved_i = -1;
8840 static int saved_type_i = 0;
8841 tree decl;
8842 char buffer [24];
8843 int i = 0;
8845 if (type)
8847 if (type == saved_type)
8848 i = saved_type_i;
8849 else
8851 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8852 decl; decl = DECL_CONTEXT (decl), i++)
8855 saved_type = type;
8856 saved_type_i = i;
8859 else
8860 i = list_length (GET_CPC_LIST ())-2;
8862 if (i == saved_i)
8863 return saved_thisn;
8865 sprintf (buffer, "this$%d", i);
8866 saved_i = i;
8867 saved_thisn = get_identifier (buffer);
8868 return saved_thisn;
8871 /* Return the assignment to the hidden enclosing context `this$<n>'
8872 by the second incoming parameter to the innerclass constructor. The
8873 form used is `this.this$<n> = this$<n>;'. */
8875 static tree
8876 build_thisn_assign (void)
8878 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8880 tree thisn = build_current_thisn (current_class);
8881 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8882 build_wfl_node (thisn), 0);
8883 tree rhs = build_wfl_node (thisn);
8884 #ifdef USE_MAPPED_LOCATION
8885 SET_EXPR_LOCATION (lhs, input_location);
8886 #else
8887 EXPR_WFL_SET_LINECOL (lhs, input_line, 0);
8888 #endif
8889 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8891 return NULL_TREE;
8895 /* Building the synthetic `class$' used to implement the `.class' 1.1
8896 extension for non primitive types. This method looks like:
8898 static Class class$(String type) throws NoClassDefFoundError
8900 try {return (java.lang.Class.forName (String));}
8901 catch (ClassNotFoundException e) {
8902 throw new NoClassDefFoundError(e.getMessage());}
8903 } */
8905 static GTY(()) tree get_message_wfl;
8906 static GTY(()) tree type_parm_wfl;
8908 static tree
8909 build_dot_class_method (tree class)
8911 #define BWF(S) build_wfl_node (get_identifier ((S)))
8912 #ifdef USE_MAPPED_LOCATION
8913 #define MQN(X,Y) make_qualified_name ((X), (Y), UNKNOWN_LOCATION)
8914 #else
8915 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8916 #endif
8917 tree args, tmp, saved_current_function_decl, mdecl, qual_name;
8918 tree stmt, throw_stmt;
8920 if (!get_message_wfl)
8922 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8923 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8926 /* Build the arguments */
8927 args = build_tree_list (get_identifier ("type$"),
8928 build_pointer_type (string_type_node));
8929 TREE_CHAIN (args) = end_params_node;
8931 /* Build the qualified name java.lang.Class.forName */
8932 tmp = MQN (MQN (MQN (BWF ("java"),
8933 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8935 /* Create the "class$" function */
8936 mdecl = create_artificial_method (class, ACC_STATIC,
8937 build_pointer_type (class_type_node),
8938 classdollar_identifier_node, args);
8939 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8940 BWF ("NoClassDefFoundError"));
8941 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE, qual_name);
8942 register_incomplete_type (JDEP_EXCEPTION, qual_name, NULL_TREE, NULL_TREE);
8943 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
8944 &TREE_VALUE (DECL_FUNCTION_THROWS (mdecl));
8946 /* We start by building the try block. We need to build:
8947 return (java.lang.Class.forName (type)); */
8948 stmt = build_method_invocation (tmp,
8949 build_tree_list (NULL_TREE, type_parm_wfl));
8950 stmt = build_return (0, stmt);
8952 /* Now onto the catch block. We start by building the expression
8953 throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8954 #ifdef USE_MAPPED_LOCATION
8955 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8956 get_message_wfl, UNKNOWN_LOCATION);
8957 #else
8958 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8959 get_message_wfl, 0);
8960 #endif
8961 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8963 /* Build new NoClassDefFoundError (_.getMessage) */
8964 throw_stmt = build_new_invocation
8965 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8966 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8968 /* Build the throw, (it's too early to use BUILD_THROW) */
8969 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8971 /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8972 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8973 BWF ("ClassNotFoundException"));
8974 stmt = encapsulate_with_try_catch (0, qual_name, stmt, throw_stmt);
8976 fix_method_argument_names (args, mdecl);
8977 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8978 saved_current_function_decl = current_function_decl;
8979 start_artificial_method_body (mdecl);
8980 java_method_add_stmt (mdecl, stmt);
8981 end_artificial_method_body (mdecl);
8982 current_function_decl = saved_current_function_decl;
8983 TYPE_DOT_CLASS (class) = mdecl;
8985 return mdecl;
8988 static tree
8989 build_dot_class_method_invocation (tree this_class, tree type)
8991 tree dot_class_method = TYPE_DOT_CLASS (this_class);
8992 tree sig_id, s, t;
8994 if (TYPE_ARRAY_P (type))
8995 sig_id = build_java_signature (type);
8996 else
8997 sig_id = DECL_NAME (TYPE_NAME (type));
8999 /* Ensure that the proper name separator is used */
9000 sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
9001 IDENTIFIER_LENGTH (sig_id));
9003 s = build_string (IDENTIFIER_LENGTH (sig_id),
9004 IDENTIFIER_POINTER (sig_id));
9005 t = build_method_invocation (build_wfl_node (DECL_NAME (dot_class_method)),
9006 build_tree_list (NULL_TREE, s));
9007 if (DECL_CONTEXT (dot_class_method) != this_class)
9009 tree class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (dot_class_method)));
9010 t = make_qualified_primary (build_wfl_node (class_name), t, 0);
9012 return t;
9015 /* This section of the code deals with constructor. */
9017 /* Craft a body for default constructor. Patch existing constructor
9018 bodies with call to super() and field initialization statements if
9019 necessary. */
9021 static void
9022 fix_constructors (tree mdecl)
9024 tree iii; /* Instance Initializer Invocation */
9025 tree *bodyp = &DECL_FUNCTION_BODY (mdecl);
9026 tree thisn_assign, compound = NULL_TREE;
9027 tree class_type = DECL_CONTEXT (mdecl);
9029 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
9030 return;
9031 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
9033 if (!*bodyp)
9035 /* It is an error for the compiler to generate a default
9036 constructor if the superclass doesn't have a constructor that
9037 takes no argument, or the same args for an anonymous class */
9038 if (verify_constructor_super (mdecl))
9040 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
9041 tree save = DECL_NAME (mdecl);
9042 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
9043 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
9044 parse_error_context
9045 (lookup_cl (TYPE_NAME (class_type)),
9046 "No constructor matching %qs found in class %qs",
9047 lang_printable_name (mdecl, 2), n);
9048 DECL_NAME (mdecl) = save;
9051 /* The constructor body must be crafted by hand. It's the
9052 constructor we defined when we realize we didn't have the
9053 CLASSNAME() constructor */
9054 start_artificial_method_body (mdecl);
9056 /* Insert an assignment to the this$<n> hidden field, if
9057 necessary */
9058 if ((thisn_assign = build_thisn_assign ()))
9059 java_method_add_stmt (mdecl, thisn_assign);
9061 /* We don't generate a super constructor invocation if we're
9062 compiling java.lang.Object. build_super_invocation takes care
9063 of that. */
9064 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
9066 /* FIXME */
9067 if ((iii = build_instinit_invocation (class_type)))
9068 java_method_add_stmt (mdecl, iii);
9070 end_artificial_method_body (mdecl);
9072 /* Search for an explicit constructor invocation */
9073 else
9075 int found = 0;
9076 int invokes_this = 0;
9077 tree main_block = BLOCK_EXPR_BODY (*bodyp);
9079 while (*bodyp)
9081 tree body = *bodyp;
9082 switch (TREE_CODE (body))
9084 case CALL_EXPR:
9085 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
9086 if (CALL_THIS_CONSTRUCTOR_P (body))
9087 invokes_this = 1;
9088 break;
9089 case COMPOUND_EXPR:
9090 case EXPR_WITH_FILE_LOCATION:
9091 bodyp = &TREE_OPERAND (body, 0);
9092 continue;
9093 case BLOCK:
9094 bodyp = &BLOCK_EXPR_BODY (body);
9095 continue;
9096 default:
9097 break;
9099 break;
9102 /* Generate the assignment to this$<n>, if necessary */
9103 if ((thisn_assign = build_thisn_assign ()))
9104 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
9106 /* The constructor is missing an invocation of super() */
9107 if (!found)
9108 compound = add_stmt_to_compound (compound, NULL_TREE,
9109 build_super_invocation (mdecl));
9110 /* Explicit super() invocation should take place before the
9111 instance initializer blocks. */
9112 else
9114 compound = add_stmt_to_compound (compound, NULL_TREE, *bodyp);
9115 *bodyp = build_java_empty_stmt ();
9118 DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
9120 /* Insert the instance initializer block right after. */
9121 if (!invokes_this && (iii = build_instinit_invocation (class_type)))
9122 compound = add_stmt_to_compound (compound, NULL_TREE, iii);
9124 /* Fix the constructor main block if we're adding extra stmts */
9125 if (compound)
9127 compound = add_stmt_to_compound (compound, NULL_TREE,
9128 BLOCK_EXPR_BODY (main_block));
9129 BLOCK_EXPR_BODY (main_block) = compound;
9134 /* Browse constructors in the super class, searching for a constructor
9135 that doesn't take any argument. Return 0 if one is found, 1
9136 otherwise. If the current class is an anonymous inner class, look
9137 for something that has the same signature. */
9139 static int
9140 verify_constructor_super (tree mdecl)
9142 tree class = CLASSTYPE_SUPER (current_class);
9143 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
9144 tree sdecl;
9146 if (!class)
9147 return 0;
9149 if (ANONYMOUS_CLASS_P (current_class))
9151 tree mdecl_arg_type;
9152 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
9153 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9154 if (DECL_CONSTRUCTOR_P (sdecl))
9156 tree m_arg_type;
9157 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9158 if (super_inner)
9159 arg_type = TREE_CHAIN (arg_type);
9160 for (m_arg_type = mdecl_arg_type;
9161 (arg_type != end_params_node
9162 && m_arg_type != end_params_node);
9163 arg_type = TREE_CHAIN (arg_type),
9164 m_arg_type = TREE_CHAIN (m_arg_type))
9165 if (!valid_method_invocation_conversion_p
9166 (TREE_VALUE (arg_type),
9167 TREE_VALUE (m_arg_type)))
9168 break;
9170 if (arg_type == end_params_node && m_arg_type == end_params_node)
9171 return 0;
9174 else
9176 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9178 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9179 if (super_inner)
9180 arg = TREE_CHAIN (arg);
9181 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
9182 return 0;
9185 return 1;
9188 /* Generate code for all context remembered for code generation. */
9190 static GTY(()) tree reversed_class_list;
9191 void
9192 java_expand_classes (void)
9194 int save_error_count = 0;
9195 static struct parser_ctxt *cur_ctxp = NULL;
9196 location_t save_location;
9198 java_parse_abort_on_error ();
9199 if (!(ctxp = ctxp_for_generation))
9200 return;
9201 java_layout_classes ();
9202 java_parse_abort_on_error ();
9203 save_location = input_location;
9205 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9207 tree current;
9208 for (current = cur_ctxp->class_list;
9209 current;
9210 current = TREE_CHAIN (current))
9211 gen_indirect_dispatch_tables (TREE_TYPE (current));
9214 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9216 ctxp = cur_ctxp;
9217 input_location = ctxp->file_start_location;
9218 lang_init_source (2); /* Error msgs have method prototypes */
9219 java_complete_expand_classes (); /* Complete and expand classes */
9220 java_parse_abort_on_error ();
9222 input_location = save_location;
9224 /* Find anonymous classes and expand their constructor. This extra pass is
9225 necessary because the constructor itself is only generated when the
9226 method in which it is defined is expanded. */
9227 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9229 tree current;
9230 ctxp = cur_ctxp;
9231 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9233 output_class = current_class = TREE_TYPE (current);
9234 if (ANONYMOUS_CLASS_P (current_class))
9236 tree d;
9237 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9239 if (DECL_CONSTRUCTOR_P (d))
9241 java_complete_expand_method (d);
9242 break; /* There is only one constructor. */
9249 /* Expanding the constructors of anonymous classes generates access
9250 methods. Scan all the methods looking for null DECL_RESULTs --
9251 this will be the case if a method hasn't been expanded. */
9252 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9254 tree current;
9255 ctxp = cur_ctxp;
9256 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9258 tree d;
9259 output_class = current_class = TREE_TYPE (current);
9260 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9262 if (DECL_RESULT (d) == NULL_TREE)
9263 java_complete_expand_method (d);
9268 /* ??? Instead of all this we could iterate around the list of
9269 classes until there were no more un-expanded methods. It would
9270 take a little longer -- one pass over the whole list of methods
9271 -- but it would be simpler. Like this: */
9272 #if 0
9274 int something_changed;
9278 something_changed = 0;
9279 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9281 tree current;
9282 ctxp = cur_ctxp;
9283 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9285 tree d;
9286 output_class = current_class = TREE_TYPE (current);
9287 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9289 if (DECL_RESULT (d) == NULL_TREE)
9291 something_changed = 1;
9292 java_complete_expand_method (d);
9298 while (something_changed);
9300 #endif
9302 /* If we've found error at that stage, don't try to generate
9303 anything, unless we're checking the syntax only
9304 (but not using -fsyntax-only for the purpose of generating
9305 bytecode). */
9306 if (java_error_count
9307 && (!flag_syntax_only && !flag_emit_class_files))
9308 return;
9310 /* Now things are stable, go for generation of the class data. */
9312 /* We pessimistically marked all methods and fields external until
9313 we knew what set of classes we were planning to compile. Now mark
9314 those that will be generated locally as not external. */
9315 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9317 tree current;
9318 ctxp = cur_ctxp;
9319 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9320 java_mark_class_local (TREE_TYPE (current));
9323 /* Compile the classes. */
9324 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9326 tree current;
9327 reversed_class_list = NULL;
9329 ctxp = cur_ctxp;
9331 /* We write out the classes in reverse order. This ensures that
9332 inner classes are written before their containing classes,
9333 which is important for parallel builds. Otherwise, the
9334 class file for the outer class may be found, but the class
9335 file for the inner class may not be present. In that
9336 situation, the compiler cannot fall back to the original
9337 source, having already read the outer class, so we must
9338 prevent that situation. */
9339 for (current = ctxp->class_list;
9340 current;
9341 current = TREE_CHAIN (current))
9342 reversed_class_list
9343 = tree_cons (NULL_TREE, current, reversed_class_list);
9345 for (current = reversed_class_list;
9346 current;
9347 current = TREE_CHAIN (current))
9349 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
9350 if (flag_emit_class_files)
9351 write_classfile (current_class);
9352 else if (! flag_syntax_only)
9353 java_expand_method_bodies (current_class);
9358 void
9359 java_finish_classes (void)
9361 static struct parser_ctxt *cur_ctxp = NULL;
9362 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9364 tree current;
9365 ctxp = cur_ctxp;
9366 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9368 output_class = current_class = TREE_TYPE (current);
9369 finish_class ();
9374 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9375 a tree list node containing RIGHT. Fore coming RIGHTs will be
9376 chained to this hook. LOCATION contains the location of the
9377 separating `.' operator. */
9379 static tree
9380 make_qualified_primary (tree primary, tree right, int location)
9382 tree wfl;
9384 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9385 wfl = build_wfl_wrap (primary, location);
9386 else
9388 wfl = primary;
9389 /* If wfl wasn't qualified, we build a first anchor */
9390 if (!EXPR_WFL_QUALIFICATION (wfl))
9391 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9394 /* And chain them */
9395 EXPR_WFL_LINECOL (right) = location;
9396 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9397 PRIMARY_P (wfl) = 1;
9398 return wfl;
9401 /* Simple merge of two name separated by a `.' */
9403 static tree
9404 merge_qualified_name (tree left, tree right)
9406 tree node;
9407 if (!left && !right)
9408 return NULL_TREE;
9410 if (!left)
9411 return right;
9413 if (!right)
9414 return left;
9416 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9417 IDENTIFIER_LENGTH (left));
9418 obstack_1grow (&temporary_obstack, '.');
9419 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9420 IDENTIFIER_LENGTH (right));
9421 node = get_identifier (obstack_base (&temporary_obstack));
9422 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9423 QUALIFIED_P (node) = 1;
9424 return node;
9427 /* Merge the two parts of a qualified name into LEFT. Set the
9428 location information of the resulting node to LOCATION, usually
9429 inherited from the location information of the `.' operator. */
9431 static tree
9432 make_qualified_name (tree left, tree right,
9433 #ifdef USE_MAPPED_LOCATION
9434 source_location location
9435 #else
9436 int location
9437 #endif
9440 #ifdef USE_COMPONENT_REF
9441 tree node = build3 (COMPONENT_REF, NULL_TREE, left, right, NULL_TREE);
9442 SET_EXPR_LOCATION (node, location);
9443 return node;
9444 #else
9445 tree left_id = EXPR_WFL_NODE (left);
9446 tree right_id = EXPR_WFL_NODE (right);
9447 tree wfl, merge;
9449 merge = merge_qualified_name (left_id, right_id);
9451 /* Left wasn't qualified and is now qualified */
9452 #ifdef USE_MAPPED_LOCATION
9453 if (!QUALIFIED_P (left_id))
9455 tree wfl = build_expr_wfl (left_id, EXPR_LOCATION (left));
9456 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9459 wfl = build_expr_wfl (right_id, location);
9460 #else
9461 if (!QUALIFIED_P (left_id))
9463 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9464 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9465 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9468 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9469 EXPR_WFL_LINECOL (wfl) = location;
9470 #endif
9471 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9472 EXPR_WFL_NODE (left) = merge;
9473 return left;
9474 #endif
9477 /* Extract the last identifier component of the qualified in WFL. The
9478 last identifier is removed from the linked list */
9480 static tree
9481 cut_identifier_in_qualified (tree wfl)
9483 tree q;
9484 tree previous = NULL_TREE;
9485 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9486 if (!TREE_CHAIN (q))
9488 if (!previous)
9489 /* Operating on a non qualified qualified WFL. */
9490 abort ();
9492 TREE_CHAIN (previous) = NULL_TREE;
9493 return TREE_PURPOSE (q);
9497 /* Resolve the expression name NAME. Return its decl. */
9499 static tree
9500 resolve_expression_name (tree id, tree *orig)
9502 tree name = EXPR_WFL_NODE (id);
9503 tree decl;
9505 /* 6.5.5.1: Simple expression names */
9506 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9508 /* 15.13.1: NAME can appear within the scope of a local variable
9509 declaration */
9510 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9511 return decl;
9513 /* 15.13.1: NAME can appear within a class declaration */
9514 else
9516 decl = lookup_field_wrapper (current_class, name);
9517 if (decl)
9519 tree access = NULL_TREE;
9520 int fs = FIELD_STATIC (decl);
9522 /* If we're accessing an outer scope local alias, make
9523 sure we change the name of the field we're going to
9524 build access to. */
9525 if (FIELD_LOCAL_ALIAS_USED (decl))
9526 name = DECL_NAME (decl);
9528 check_deprecation (id, decl);
9530 /* Instance variable (8.3.1.1) can't appear within
9531 static method, static initializer or initializer for
9532 a static variable. */
9533 if (!fs && METHOD_STATIC (current_function_decl))
9535 static_ref_err (id, name, current_class);
9536 return error_mark_node;
9538 /* Instance variables can't appear as an argument of
9539 an explicit constructor invocation */
9540 if (!fs && ctxp->explicit_constructor_p
9541 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9543 parse_error_context
9544 (id, "Can't reference %qs before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9545 return error_mark_node;
9548 /* If we're processing an inner class and we're trying
9549 to access a field belonging to an outer class, build
9550 the access to the field. */
9551 if (nested_field_access_p (current_class, decl))
9553 if (!fs && CLASS_STATIC (TYPE_NAME (current_class)))
9555 static_ref_err (id, DECL_NAME (decl), current_class);
9556 return error_mark_node;
9558 access = build_nested_field_access (id, decl);
9559 if (orig)
9560 *orig = access;
9561 return access;
9564 /* Otherwise build what it takes to access the field */
9565 access = build_field_ref ((fs ? NULL_TREE : current_this),
9566 DECL_CONTEXT (decl), name);
9567 if (fs)
9568 access = maybe_build_class_init_for_field (decl, access);
9569 /* We may be asked to save the real field access node */
9570 if (orig)
9571 *orig = access;
9572 /* Last check: can we access the field? */
9573 if (not_accessible_p (current_class, decl, NULL_TREE, 0))
9575 not_accessible_field_error (id, decl);
9576 return error_mark_node;
9578 /* And we return what we got */
9579 return access;
9581 /* Fall down to error report on undefined variable */
9584 /* 6.5.5.2 Qualified Expression Names */
9585 else
9587 if (orig)
9588 *orig = NULL_TREE;
9589 qualify_ambiguous_name (id);
9590 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9591 /* 15.10.2: Accessing Superclass Members using super */
9592 return resolve_field_access (id, orig, NULL);
9595 /* We've got an error here */
9596 if (INNER_CLASS_TYPE_P (current_class))
9597 parse_error_context (id,
9598 "Local variable %qs can't be accessed from within the inner class %qs unless it is declared final",
9599 IDENTIFIER_POINTER (name),
9600 IDENTIFIER_POINTER (DECL_NAME
9601 (TYPE_NAME (current_class))));
9602 else
9603 parse_error_context (id, "Undefined variable %qs",
9604 IDENTIFIER_POINTER (name));
9606 return error_mark_node;
9609 static void
9610 static_ref_err (tree wfl, tree field_id, tree class_type)
9612 parse_error_context
9613 (wfl,
9614 "Can't make a static reference to nonstatic variable %qs in class %qs",
9615 IDENTIFIER_POINTER (field_id),
9616 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9619 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9620 We return something suitable to generate the field access. We also
9621 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9622 recipient's address can be null. */
9624 static tree
9625 resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9627 int is_static = 0;
9628 tree field_ref;
9629 tree decl = NULL_TREE, where_found, type_found;
9631 if (resolve_qualified_expression_name (qual_wfl, &decl,
9632 &where_found, &type_found))
9633 return error_mark_node;
9635 /* Resolve the LENGTH field of an array here */
9636 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9637 && type_found && TYPE_ARRAY_P (type_found)
9638 && ! flag_emit_class_files)
9640 tree length = build_java_array_length_access (where_found);
9641 field_ref = length;
9643 /* In case we're dealing with a static array, we need to
9644 initialize its class before the array length can be fetched. */
9645 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9647 build_static_field_ref (where_found);
9648 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9651 /* We might have been trying to resolve field.method(). In which
9652 case, the resolution is over and decl is the answer */
9653 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9654 field_ref = decl;
9655 else if (JDECL_P (decl))
9657 if (!type_found)
9658 type_found = DECL_CONTEXT (decl);
9659 is_static = FIELD_STATIC (decl);
9660 field_ref = build_field_ref ((is_static ?
9661 NULL_TREE : where_found),
9662 type_found, DECL_NAME (decl));
9663 if (field_ref == error_mark_node)
9664 return error_mark_node;
9665 if (is_static)
9666 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9668 /* If we're looking at a static field, we may need to generate a
9669 class initialization for it. This can happen when the access
9670 looks like `field.ref', where `field' is a static field in an
9671 interface we implement. */
9672 if (!flag_emit_class_files
9673 && TREE_CODE (where_found) == VAR_DECL
9674 && FIELD_STATIC (where_found))
9676 build_static_field_ref (where_found);
9677 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9680 else
9681 field_ref = decl;
9683 if (field_decl)
9684 *field_decl = decl;
9685 if (field_type)
9686 *field_type = (QUAL_DECL_TYPE (decl) ?
9687 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9688 return field_ref;
9691 /* If NODE is an access to a static field, strip out the class
9692 initialization part and return the field decl, otherwise, return
9693 NODE. */
9695 tree
9696 extract_field_decl (tree node)
9698 if (TREE_CODE (node) == COMPOUND_EXPR)
9700 tree op1 = TREE_OPERAND (node, 1);
9701 if (TREE_CODE (op1) == COMPOUND_EXPR)
9703 tree call = TREE_OPERAND (op1, 0);
9704 if (TREE_CODE (call) == CALL_EXPR
9705 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9706 && (TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9707 == soft_initclass_node))
9708 return TREE_OPERAND (op1, 1);
9710 else if (JDECL_P (op1))
9711 return op1;
9713 return node;
9716 /* 6.5.5.2: Qualified Expression Names */
9718 static int
9719 resolve_qualified_expression_name (tree wfl, tree *found_decl,
9720 tree *where_found, tree *type_found)
9722 int from_type = 0; /* Field search initiated from a type */
9723 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9724 int previous_call_static = 0;
9725 int is_static;
9726 tree decl = NULL_TREE, type = NULL_TREE, q;
9727 /* For certain for of inner class instantiation */
9728 tree saved_current, saved_this;
9729 #define RESTORE_THIS_AND_CURRENT_CLASS \
9730 { current_class = saved_current; current_this = saved_this;}
9732 *type_found = *where_found = NULL_TREE;
9734 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9736 tree qual_wfl = QUAL_WFL (q);
9737 tree ret_decl; /* for EH checking */
9738 #ifdef USE_MAPPED_LOCATION
9739 source_location location; /* for EH checking */
9740 #else
9741 int location; /* for EH checking */
9742 #endif
9744 /* 15.10.1 Field Access Using a Primary */
9745 switch (TREE_CODE (qual_wfl))
9747 case CALL_EXPR:
9748 case NEW_CLASS_EXPR:
9749 /* If the access to the function call is a non static field,
9750 build the code to access it. */
9751 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9753 decl = maybe_access_field (decl, *where_found,
9754 DECL_CONTEXT (decl));
9755 if (decl == error_mark_node)
9756 return 1;
9759 /* And code for the function call */
9760 if (complete_function_arguments (qual_wfl))
9761 return 1;
9763 /* We might have to setup a new current class and a new this
9764 for the search of an inner class, relative to the type of
9765 a expression resolved as `decl'. The current values are
9766 saved and restored shortly after */
9767 saved_current = current_class;
9768 saved_this = current_this;
9769 if (decl
9770 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9771 || from_qualified_this))
9773 /* If we still have `from_qualified_this', we have the form
9774 <T>.this.f() and we need to build <T>.this */
9775 if (from_qualified_this)
9777 decl = build_access_to_thisn (current_class, type, 0);
9778 decl = java_complete_tree (decl);
9779 type = TREE_TYPE (TREE_TYPE (decl));
9781 current_class = type;
9782 current_this = decl;
9783 from_qualified_this = 0;
9786 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9787 CALL_USING_SUPER (qual_wfl) = 1;
9788 #ifdef USE_MAPPED_LOCATION
9789 location = (TREE_CODE (qual_wfl) == CALL_EXPR
9790 ? EXPR_LOCATION (TREE_OPERAND (qual_wfl, 0))
9791 : UNKNOWN_LOCATION);
9792 #else
9793 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9794 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9795 #endif
9796 *where_found = patch_method_invocation (qual_wfl, decl, type,
9797 from_super,
9798 &is_static, &ret_decl);
9799 from_super = 0;
9800 if (*where_found == error_mark_node)
9802 RESTORE_THIS_AND_CURRENT_CLASS;
9803 return 1;
9805 *type_found = type = QUAL_DECL_TYPE (*where_found);
9807 *where_found = force_evaluation_order (*where_found);
9809 /* If we're creating an inner class instance, check for that
9810 an enclosing instance is in scope */
9811 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9812 && INNER_ENCLOSING_SCOPE_CHECK (type))
9814 parse_error_context
9815 (qual_wfl, "No enclosing instance for inner class %qs is in scope%s",
9816 lang_printable_name (type, 0),
9817 (!current_this ? "" :
9818 "; an explicit one must be provided when creating this inner class"));
9819 RESTORE_THIS_AND_CURRENT_CLASS;
9820 return 1;
9823 /* In case we had to change then to resolve a inner class
9824 instantiation using a primary qualified by a `new' */
9825 RESTORE_THIS_AND_CURRENT_CLASS;
9827 #ifdef USE_MAPPED_LOCATION
9828 if (location != UNKNOWN_LOCATION)
9829 #else
9830 if (location)
9831 #endif
9833 tree arguments = NULL_TREE;
9834 if (TREE_CODE (qual_wfl) == CALL_EXPR
9835 && TREE_OPERAND (qual_wfl, 1) != NULL_TREE)
9836 arguments = TREE_VALUE (TREE_OPERAND (qual_wfl, 1));
9837 check_thrown_exceptions (location, ret_decl, arguments);
9840 /* If the previous call was static and this one is too,
9841 build a compound expression to hold the two (because in
9842 that case, previous function calls aren't transported as
9843 forcoming function's argument. */
9844 if (previous_call_static && is_static)
9846 /* We must set CAN_COMPLETE_NORMALLY for the first call
9847 since it is done nowhere else. */
9848 CAN_COMPLETE_NORMALLY (decl) = 1;
9849 decl = build2 (COMPOUND_EXPR, TREE_TYPE (*where_found),
9850 decl, *where_found);
9851 TREE_SIDE_EFFECTS (decl) = 1;
9853 else
9855 previous_call_static = is_static;
9856 decl = *where_found;
9858 from_type = 0;
9859 continue;
9861 case NEW_ARRAY_EXPR:
9862 case NEW_ANONYMOUS_ARRAY_EXPR:
9863 *where_found = decl = java_complete_tree (qual_wfl);
9864 if (decl == error_mark_node)
9865 return 1;
9866 *type_found = type = QUAL_DECL_TYPE (decl);
9867 continue;
9869 case CONVERT_EXPR:
9870 *where_found = decl = java_complete_tree (qual_wfl);
9871 if (decl == error_mark_node)
9872 return 1;
9873 *type_found = type = QUAL_DECL_TYPE (decl);
9874 from_cast = 1;
9875 continue;
9877 case CONDITIONAL_EXPR:
9878 case STRING_CST:
9879 case MODIFY_EXPR:
9880 *where_found = decl = java_complete_tree (qual_wfl);
9881 if (decl == error_mark_node)
9882 return 1;
9883 *type_found = type = QUAL_DECL_TYPE (decl);
9884 continue;
9886 case ARRAY_REF:
9887 /* If the access to the function call is a non static field,
9888 build the code to access it. */
9889 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9891 decl = maybe_access_field (decl, *where_found, type);
9892 if (decl == error_mark_node)
9893 return 1;
9895 /* And code for the array reference expression */
9896 decl = java_complete_tree (qual_wfl);
9897 if (decl == error_mark_node)
9898 return 1;
9899 type = QUAL_DECL_TYPE (decl);
9900 continue;
9902 case PLUS_EXPR:
9903 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9904 return 1;
9905 if ((type = patch_string (decl)))
9906 decl = type;
9907 *where_found = QUAL_RESOLUTION (q) = decl;
9908 *type_found = type = TREE_TYPE (decl);
9909 break;
9911 case CLASS_LITERAL:
9912 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9913 return 1;
9914 *where_found = QUAL_RESOLUTION (q) = decl;
9915 *type_found = type = TREE_TYPE (decl);
9916 break;
9918 default:
9919 /* Fix for -Wall Just go to the next statement. Don't
9920 continue */
9921 break;
9924 /* If we fall here, we weren't processing a (static) function call. */
9925 previous_call_static = 0;
9927 /* It can be the keyword THIS */
9928 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9929 && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9931 if (!current_this)
9933 parse_error_context
9934 (wfl, "Keyword %<this%> used outside allowed context");
9935 return 1;
9937 if (ctxp->explicit_constructor_p
9938 && type == current_class)
9940 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
9941 return 1;
9943 /* We have to generate code for intermediate access */
9944 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9946 *where_found = decl = current_this;
9947 *type_found = type = QUAL_DECL_TYPE (decl);
9949 /* We're trying to access the this from somewhere else. Make sure
9950 it's allowed before doing so. */
9951 else
9953 if (!enclosing_context_p (type, current_class))
9955 char *p = xstrdup (lang_printable_name (type, 0));
9956 parse_error_context (qual_wfl, "Can't use variable %<%s.this%>: type %qs isn't an outer type of type %qs",
9957 p, p,
9958 lang_printable_name (current_class, 0));
9959 free (p);
9960 return 1;
9962 from_qualified_this = 1;
9963 /* If there's nothing else after that, we need to
9964 produce something now, otherwise, the section of the
9965 code that needs to produce <T>.this will generate
9966 what is necessary. */
9967 if (!TREE_CHAIN (q))
9969 decl = build_access_to_thisn (current_class, type, 0);
9970 *where_found = decl = java_complete_tree (decl);
9971 *type_found = type = TREE_TYPE (decl);
9975 from_type = 0;
9976 continue;
9979 /* 15.10.2 Accessing Superclass Members using SUPER */
9980 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9981 && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9983 tree node;
9984 /* Check on the restricted use of SUPER */
9985 if (METHOD_STATIC (current_function_decl)
9986 || current_class == object_type_node)
9988 parse_error_context
9989 (wfl, "Keyword %<super%> used outside allowed context");
9990 return 1;
9992 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9993 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9994 CLASSTYPE_SUPER (current_class),
9995 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9996 *where_found = decl = java_complete_tree (node);
9997 if (decl == error_mark_node)
9998 return 1;
9999 *type_found = type = QUAL_DECL_TYPE (decl);
10000 from_super = from_type = 1;
10001 continue;
10004 /* 15.13.1: Can't search for field name in packages, so we
10005 assume a variable/class name was meant. */
10006 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10008 tree name;
10009 if ((decl = resolve_package (wfl, &q, &name)))
10011 tree list;
10012 *where_found = decl;
10014 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10016 /* We want to be absolutely sure that the class is laid
10017 out. We're going to search something inside it. */
10018 *type_found = type = TREE_TYPE (decl);
10019 layout_class (type);
10020 from_type = 1;
10022 /* Fix them all the way down, if any are left. */
10023 if (q)
10025 list = TREE_CHAIN (q);
10026 while (list)
10028 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
10029 list = TREE_CHAIN (list);
10033 else
10035 if (from_super || from_cast)
10036 parse_error_context
10037 ((from_cast ? qual_wfl : wfl),
10038 "No variable %qs defined in class %qs",
10039 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10040 lang_printable_name (type, 0));
10041 else
10042 parse_error_context
10043 (qual_wfl, "Undefined variable or class name: %qs",
10044 IDENTIFIER_POINTER (name));
10045 return 1;
10049 /* We have a type name. It's been already resolved when the
10050 expression was qualified. */
10051 else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
10053 decl = QUAL_RESOLUTION (q);
10055 /* Sneak preview. If next we see a `new', we're facing a
10056 qualification which resulted in a type being selected
10057 instead of a field. Report the error. */
10058 if(TREE_CHAIN (q)
10059 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
10061 parse_error_context (qual_wfl, "Undefined variable %qs",
10062 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10063 return 1;
10066 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10068 check_deprecation (qual_wfl, decl);
10070 type = TREE_TYPE (decl);
10071 from_type = 1;
10073 /* We resolve an expression name */
10074 else
10076 tree field_decl = NULL_TREE;
10078 /* If there exists an early resolution, use it. That occurs
10079 only once and we know that there are more things to
10080 come. Don't do that when processing something after SUPER
10081 (we need more thing to be put in place below */
10082 if (!from_super && QUAL_RESOLUTION (q))
10084 decl = QUAL_RESOLUTION (q);
10085 if (!type)
10087 if (TREE_CODE (decl) == FIELD_DECL
10088 || TREE_CODE (decl) == VAR_DECL)
10090 if (TREE_CODE (decl) == FIELD_DECL
10091 && !FIELD_STATIC (decl))
10093 if (current_this)
10094 *where_found = current_this;
10095 else
10097 static_ref_err (qual_wfl, DECL_NAME (decl),
10098 current_class);
10099 return 1;
10102 else
10104 *where_found = TREE_TYPE (decl);
10105 if (TREE_CODE (*where_found) == POINTER_TYPE)
10106 *where_found = TREE_TYPE (*where_found);
10108 if (nested_field_access_p (current_class, decl))
10109 decl = build_nested_field_access (qual_wfl, decl);
10111 else
10113 *where_found = TREE_TYPE (decl);
10114 if (TREE_CODE (*where_found) == POINTER_TYPE)
10115 *where_found = TREE_TYPE (*where_found);
10120 /* Report and error if we're using a numerical literal as a
10121 qualifier. It can only be an INTEGER_CST. */
10122 else if (TREE_CODE (qual_wfl) == INTEGER_CST)
10124 parse_error_context
10125 (wfl, "Can't use type %qs as a qualifier",
10126 lang_printable_name (TREE_TYPE (qual_wfl), 0));
10127 return 1;
10130 /* We have to search for a field, knowing the type of its
10131 container. The flag FROM_TYPE indicates that we resolved
10132 the last member of the expression as a type name, which
10133 means that for the resolution of this field, we'll look
10134 for other errors than if it was resolved as a member of
10135 an other field. */
10136 else
10138 int is_static;
10139 tree field_decl_type; /* For layout */
10141 if (!from_type && !JREFERENCE_TYPE_P (type))
10143 parse_error_context
10144 (qual_wfl, "Attempt to reference field %qs in %<%s %s%>",
10145 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10146 lang_printable_name (type, 0),
10147 IDENTIFIER_POINTER (DECL_NAME (decl)));
10148 return 1;
10151 field_decl = lookup_field_wrapper (type,
10152 EXPR_WFL_NODE (qual_wfl));
10154 /* Maybe what we're trying to access to is an inner
10155 class, only if decl is a TYPE_DECL. */
10156 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
10158 tree ptr, inner_decl;
10160 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
10161 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
10162 if (inner_decl)
10164 check_inner_class_access (inner_decl, decl, qual_wfl);
10165 type = TREE_TYPE (inner_decl);
10166 decl = inner_decl;
10167 from_type = 1;
10168 continue;
10172 if (field_decl == NULL_TREE)
10174 parse_error_context
10175 (qual_wfl, "No variable %qs defined in type %qs",
10176 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10177 GET_TYPE_NAME (type));
10178 return 1;
10180 if (field_decl == error_mark_node)
10181 return 1;
10183 /* Layout the type of field_decl, since we may need
10184 it. Don't do primitive types or loaded classes. The
10185 situation of non primitive arrays may not handled
10186 properly here. FIXME */
10187 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10188 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
10189 else
10190 field_decl_type = TREE_TYPE (field_decl);
10191 if (!JPRIMITIVE_TYPE_P (field_decl_type)
10192 && !CLASS_LOADED_P (field_decl_type)
10193 && !TYPE_ARRAY_P (field_decl_type))
10194 resolve_and_layout (field_decl_type, NULL_TREE);
10196 /* Check on accessibility here */
10197 if (not_accessible_p (current_class, field_decl,
10198 *type_found, from_super))
10199 return not_accessible_field_error (qual_wfl,field_decl);
10200 check_deprecation (qual_wfl, field_decl);
10202 /* There are things to check when fields are accessed
10203 from type. There are no restrictions on a static
10204 declaration of the field when it is accessed from an
10205 interface */
10206 is_static = FIELD_STATIC (field_decl);
10207 if (!from_super && from_type
10208 && !TYPE_INTERFACE_P (type)
10209 && !is_static
10210 && (current_function_decl
10211 && METHOD_STATIC (current_function_decl)))
10213 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
10214 return 1;
10216 from_cast = from_super = 0;
10218 /* If it's an access from a type but isn't static, we
10219 make it relative to `this'. */
10220 if (!is_static && from_type)
10221 decl = current_this;
10223 /* If we need to generate something to get a proper
10224 handle on what this field is accessed from, do it
10225 now. */
10226 if (!is_static)
10228 decl = maybe_access_field (decl, *where_found, *type_found);
10229 if (decl == error_mark_node)
10230 return 1;
10233 /* We want to keep the location where we found it, and the
10234 type we found. */
10235 *where_found = decl;
10236 *type_found = type;
10238 /* Generate the correct expression for field access from
10239 qualified this */
10240 if (from_qualified_this)
10242 field_decl
10243 = build_nested_field_access (qual_wfl, field_decl);
10244 from_qualified_this = 0;
10247 /* If needed, generate accessors for static field access. */
10248 if (is_static
10249 && FIELD_PRIVATE (field_decl)
10250 && flag_emit_class_files
10251 && nested_field_access_p (current_class, field_decl))
10252 field_decl = build_nested_field_access (qual_wfl, field_decl);
10254 /* This is the decl found and eventually the next one to
10255 search from */
10256 decl = field_decl;
10258 from_type = 0;
10259 type = QUAL_DECL_TYPE (decl);
10261 /* Sneak preview. If decl is qualified by a `new', report
10262 the error here to be accurate on the peculiar construct */
10263 if (TREE_CHAIN (q)
10264 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
10265 && !JREFERENCE_TYPE_P (type))
10267 parse_error_context (qual_wfl, "Attempt to reference field %<new%> in a %qs",
10268 lang_printable_name (type, 0));
10269 return 1;
10272 /* `q' might have changed due to a after package resolution
10273 re-qualification */
10274 if (!q)
10275 break;
10277 *found_decl = decl;
10278 return 0;
10281 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
10282 can't be accessed from REFERENCE (a record type). If MEMBER
10283 features a protected access, we then use WHERE which, if non null,
10284 holds the type of MEMBER's access that is checked against
10285 6.6.2.1. This function should be used when decl is a field or a
10286 method. */
10288 static int
10289 not_accessible_p (tree reference, tree member, tree where, int from_super)
10291 int access_flag = get_access_flags_from_decl (member);
10292 bool is_static = false;
10294 if (TREE_CODE (member) == FIELD_DECL ||
10295 TREE_CODE (member) == VAR_DECL)
10296 is_static = FIELD_STATIC (member);
10297 else
10298 is_static = METHOD_STATIC (member);
10300 /* Access always granted for members declared public */
10301 if (access_flag & ACC_PUBLIC)
10302 return 0;
10304 /* Check access on protected members */
10305 if (access_flag & ACC_PROTECTED)
10307 /* Access granted if it occurs from within the package
10308 containing the class in which the protected member is
10309 declared */
10310 if (class_in_current_package (DECL_CONTEXT (member)))
10311 return 0;
10313 /* If accessed with the form `super.member', then access is granted */
10314 if (from_super)
10315 return 0;
10317 /* If WHERE is active, access was made through a qualifier. For
10318 non-static members, access is granted if the type of the qualifier
10319 is or is a sublass of the type the access is made from (6.6.2.1.) */
10320 if (where && !is_static)
10322 while (reference)
10324 if (inherits_from_p (where, reference))
10325 return 0;
10326 if (INNER_CLASS_TYPE_P (reference))
10327 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10328 else
10329 break;
10331 return 1;
10334 /* Otherwise, access is granted if occurring from within the class
10335 where member is declared, or a subclass of it. */
10336 while (reference)
10338 if (inherits_from_p (reference, DECL_CONTEXT (member)))
10339 return 0;
10340 if (INNER_CLASS_TYPE_P (reference))
10341 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10342 else
10343 break;
10345 return 1;
10348 /* Check access on private members. Access is granted only if it
10349 occurs from within the class in which it is declared -- that does
10350 it for innerclasses too. */
10351 if (access_flag & ACC_PRIVATE)
10353 if (reference == DECL_CONTEXT (member) ||
10354 common_enclosing_context_p (DECL_CONTEXT (member), reference))
10355 return 0;
10356 return 1;
10359 /* Default access is permitted only when occurring from within the
10360 package in which the context (MEMBER) is declared. */
10361 return !class_in_current_package (DECL_CONTEXT (member));
10364 /* Test deprecated decl access. */
10365 static void
10366 check_deprecation (tree wfl, tree decl)
10368 const char *file;
10369 tree elt;
10371 if (! warn_deprecated)
10372 return;
10374 /* We want to look at the element type of arrays here, so we strip
10375 all surrounding array types. */
10376 if (TYPE_ARRAY_P (TREE_TYPE (decl)))
10378 elt = TREE_TYPE (decl);
10379 while (TYPE_ARRAY_P (elt))
10380 elt = TYPE_ARRAY_ELEMENT (elt);
10381 /* We'll end up with a pointer type, so we use TREE_TYPE to go
10382 to the record. */
10383 decl = TYPE_NAME (TREE_TYPE (elt));
10385 file = DECL_SOURCE_FILE (decl);
10387 /* Complain if the field is deprecated and the file it was defined
10388 in isn't compiled at the same time the file which contains its
10389 use is */
10390 if (DECL_DEPRECATED (decl)
10391 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10393 const char *the;
10394 switch (TREE_CODE (decl))
10396 case FUNCTION_DECL:
10397 the = "method";
10398 break;
10399 case FIELD_DECL:
10400 case VAR_DECL:
10401 the = "field";
10402 break;
10403 case TYPE_DECL:
10404 parse_warning_context (wfl, "The class %qs has been deprecated",
10405 IDENTIFIER_POINTER (DECL_NAME (decl)));
10406 return;
10407 default:
10408 abort ();
10410 /* Don't issue a message if the context as been deprecated as a
10411 whole. */
10412 if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10413 parse_warning_context
10414 (wfl, "The %s %qs in class %qs has been deprecated",
10415 the, lang_printable_name (decl, 0),
10416 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10420 /* Returns 1 if class was declared in the current package, 0 otherwise */
10422 static GTY(()) tree cicp_cache;
10423 static int
10424 class_in_current_package (tree class)
10426 int qualified_flag;
10427 tree left;
10429 if (cicp_cache == class)
10430 return 1;
10432 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10434 /* If the current package is empty and the name of CLASS is
10435 qualified, class isn't in the current package. If there is a
10436 current package and the name of the CLASS is not qualified, class
10437 isn't in the current package */
10438 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10439 return 0;
10441 /* If there is not package and the name of CLASS isn't qualified,
10442 they belong to the same unnamed package */
10443 if (!ctxp->package && !qualified_flag)
10444 return 1;
10446 /* Compare the left part of the name of CLASS with the package name */
10447 split_qualified_name (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10448 if (ctxp->package == left)
10450 cicp_cache = class;
10451 return 1;
10453 return 0;
10456 /* This function may generate code to access DECL from WHERE. This is
10457 done only if certain conditions meet. */
10459 static tree
10460 maybe_access_field (tree decl, tree where, tree type)
10462 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10463 && !FIELD_STATIC (decl))
10464 decl = build_field_ref (where ? where : current_this,
10465 (type ? type : DECL_CONTEXT (decl)),
10466 DECL_NAME (decl));
10467 return decl;
10470 /* Build a method invocation, by patching PATCH. If non NULL
10471 and according to the situation, PRIMARY and WHERE may be
10472 used. IS_STATIC is set to 1 if the invoked function is static. */
10474 static tree
10475 patch_method_invocation (tree patch, tree primary, tree where, int from_super,
10476 int *is_static, tree *ret_decl)
10478 tree wfl = TREE_OPERAND (patch, 0);
10479 tree args = TREE_OPERAND (patch, 1);
10480 tree name = EXPR_WFL_NODE (wfl);
10481 tree list;
10482 int is_static_flag = 0;
10483 int is_super_init = 0;
10484 tree this_arg = NULL_TREE;
10485 int is_array_clone_call = 0;
10487 /* Should be overridden if everything goes well. Otherwise, if
10488 something fails, it should keep this value. It stop the
10489 evaluation of a bogus assignment. See java_complete_tree,
10490 MODIFY_EXPR: for the reasons why we sometimes want to keep on
10491 evaluating an assignment */
10492 TREE_TYPE (patch) = error_mark_node;
10494 /* Since lookup functions are messing with line numbers, save the
10495 context now. */
10496 java_parser_context_save_global ();
10498 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10500 /* Resolution of qualified name, excluding constructors */
10501 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10503 tree identifier, identifier_wfl, type, resolved;
10504 /* Extract the last IDENTIFIER of the qualified
10505 expression. This is a wfl and we will use it's location
10506 data during error report. */
10507 identifier_wfl = cut_identifier_in_qualified (wfl);
10508 identifier = EXPR_WFL_NODE (identifier_wfl);
10510 /* Given the context, IDENTIFIER is syntactically qualified
10511 as a MethodName. We need to qualify what's before */
10512 qualify_ambiguous_name (wfl);
10513 resolved = resolve_field_access (wfl, NULL, NULL);
10515 if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10516 && FIELD_FINAL (resolved)
10517 && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10518 && !flag_emit_class_files)
10519 resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10521 if (resolved == error_mark_node)
10522 PATCH_METHOD_RETURN_ERROR ();
10524 type = GET_SKIP_TYPE (resolved);
10525 resolve_and_layout (type, NULL_TREE);
10527 if (JPRIMITIVE_TYPE_P (type))
10529 parse_error_context
10530 (identifier_wfl,
10531 "Can't invoke a method on primitive type %qs",
10532 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10533 PATCH_METHOD_RETURN_ERROR ();
10536 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10537 args = nreverse (args);
10539 /* We're resolving a call from a type */
10540 if (TREE_CODE (resolved) == TYPE_DECL)
10542 if (CLASS_INTERFACE (resolved))
10544 parse_error_context
10545 (identifier_wfl,
10546 "Can't make static reference to method %qs in interface %qs",
10547 IDENTIFIER_POINTER (identifier),
10548 IDENTIFIER_POINTER (name));
10549 PATCH_METHOD_RETURN_ERROR ();
10551 if (list && !METHOD_STATIC (list))
10553 char *fct_name = xstrdup (lang_printable_name (list, 2));
10554 parse_error_context
10555 (identifier_wfl,
10556 "Can't make static reference to method %<%s %s%> in class %qs",
10557 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10558 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10559 free (fct_name);
10560 PATCH_METHOD_RETURN_ERROR ();
10563 else
10564 this_arg = primary = resolved;
10566 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10567 is_array_clone_call = 1;
10569 /* IDENTIFIER_WFL will be used to report any problem further */
10570 wfl = identifier_wfl;
10572 /* Resolution of simple names, names generated after a primary: or
10573 constructors */
10574 else
10576 tree class_to_search = NULL_TREE;
10577 int lc; /* Looking for Constructor */
10579 /* We search constructor in their target class */
10580 if (CALL_CONSTRUCTOR_P (patch))
10582 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10583 class_to_search = EXPR_WFL_NODE (wfl);
10584 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10585 this_identifier_node)
10586 class_to_search = NULL_TREE;
10587 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10588 super_identifier_node)
10590 is_super_init = 1;
10591 if (CLASSTYPE_SUPER (current_class))
10592 class_to_search =
10593 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10594 else
10596 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10597 PATCH_METHOD_RETURN_ERROR ();
10601 /* Class to search is NULL if we're searching the current one */
10602 if (class_to_search)
10604 class_to_search = resolve_and_layout (class_to_search, wfl);
10606 if (!class_to_search)
10608 parse_error_context
10609 (wfl, "Class %qs not found in type declaration",
10610 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10611 PATCH_METHOD_RETURN_ERROR ();
10614 /* Can't instantiate an abstract class, but we can
10615 invoke it's constructor. It's use within the `new'
10616 context is denied here. */
10617 if (CLASS_ABSTRACT (class_to_search)
10618 && TREE_CODE (patch) == NEW_CLASS_EXPR)
10620 parse_error_context
10621 (wfl, "Class %qs is an abstract class. It can't be instantiated",
10622 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10623 PATCH_METHOD_RETURN_ERROR ();
10626 class_to_search = TREE_TYPE (class_to_search);
10628 else
10629 class_to_search = current_class;
10630 lc = 1;
10632 /* This is a regular search in the local class, unless an
10633 alternate class is specified. */
10634 else
10636 if (where != NULL_TREE)
10637 class_to_search = where;
10638 else if (QUALIFIED_P (name))
10639 class_to_search = current_class;
10640 else
10642 class_to_search = current_class;
10644 for (;;)
10646 if (has_method (class_to_search, name))
10647 break;
10648 if (! INNER_CLASS_TYPE_P (class_to_search))
10650 parse_error_context (wfl,
10651 "No method named %qs in scope",
10652 IDENTIFIER_POINTER (name));
10653 PATCH_METHOD_RETURN_ERROR ();
10655 class_to_search
10656 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10659 lc = 0;
10662 /* NAME is a simple identifier or comes from a primary. Search
10663 in the class whose declaration contain the method being
10664 invoked. */
10665 resolve_and_layout (class_to_search, NULL_TREE);
10667 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10668 /* Don't continue if no method were found, as the next statement
10669 can't be executed then. */
10670 if (!list)
10671 PATCH_METHOD_RETURN_ERROR ();
10673 if (TYPE_ARRAY_P (class_to_search)
10674 && DECL_NAME (list) == get_identifier ("clone"))
10675 is_array_clone_call = 1;
10677 /* Check for static reference if non static methods */
10678 if (check_for_static_method_reference (wfl, patch, list,
10679 class_to_search, primary))
10680 PATCH_METHOD_RETURN_ERROR ();
10682 /* Check for inner classes creation from illegal contexts */
10683 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10684 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10685 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10686 && !DECL_INIT_P (current_function_decl))
10688 parse_error_context
10689 (wfl, "No enclosing instance for inner class %qs is in scope%s",
10690 lang_printable_name (class_to_search, 0),
10691 (!current_this ? "" :
10692 "; an explicit one must be provided when creating this inner class"));
10693 PATCH_METHOD_RETURN_ERROR ();
10696 /* Non static methods are called with the current object extra
10697 argument. If patch a `new TYPE()', the argument is the value
10698 returned by the object allocator. If method is resolved as a
10699 primary, use the primary otherwise use the current THIS. */
10700 args = nreverse (args);
10701 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10703 this_arg = primary ? primary : current_this;
10705 /* If we're using an access method, things are different.
10706 There are two family of cases:
10708 1) We're not generating bytecodes:
10710 - LIST is non static. It's invocation is transformed from
10711 x(a1,...,an) into this$<n>.x(a1,....an).
10712 - LIST is static. It's invocation is transformed from
10713 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10715 2) We're generating bytecodes:
10717 - LIST is non static. It's invocation is transformed from
10718 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10719 - LIST is static. It's invocation is transformed from
10720 x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10722 Of course, this$<n> can be arbitrarily complex, ranging from
10723 this$0 (the immediate outer context) to
10724 access$0(access$0(...(this$0))).
10726 maybe_use_access_method returns a nonzero value if the
10727 this_arg has to be moved into the (then generated) stub
10728 argument list. In the meantime, the selected function
10729 might have be replaced by a generated stub. */
10730 if (!primary &&
10731 maybe_use_access_method (is_super_init, &list, &this_arg))
10733 args = tree_cons (NULL_TREE, this_arg, args);
10734 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10739 /* Merge point of all resolution schemes. If we have nothing, this
10740 is an error, already signaled */
10741 if (!list)
10742 PATCH_METHOD_RETURN_ERROR ();
10744 /* Check accessibility, position the is_static flag, build and
10745 return the call */
10746 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10747 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10748 NULL_TREE), from_super)
10749 /* Calls to clone() on array types are permitted as a special-case. */
10750 && !is_array_clone_call)
10752 const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10753 const char *const access =
10754 accessibility_string (get_access_flags_from_decl (list));
10755 const char *const klass =
10756 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10757 const char *const refklass =
10758 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10759 const char *const what = (DECL_CONSTRUCTOR_P (list)
10760 ? "constructor" : "method");
10761 parse_error_context (wfl,
10762 "Can't access %s %s %<%s.%s%> from %qs",
10763 access, what, klass, fct_name, refklass);
10764 PATCH_METHOD_RETURN_ERROR ();
10767 /* Deprecation check: check whether the method being invoked or the
10768 instance-being-created's type are deprecated. */
10769 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10770 check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10771 check_deprecation (wfl, list);
10773 /* If invoking a innerclass constructor, there are hidden parameters
10774 to pass */
10775 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10776 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10778 /* And make sure we add the accessed local variables to be saved
10779 in field aliases. */
10780 args = build_alias_initializer_parameter_list
10781 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10783 /* Secretly pass the current_this/primary as a second argument */
10784 if (primary || current_this)
10786 tree extra_arg;
10787 tree this_type = (current_this ?
10788 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10789 /* Method's (list) enclosing context */
10790 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10791 /* If we have a primary, use it. */
10792 if (primary)
10793 extra_arg = primary;
10794 /* The current `this' is an inner class but isn't a direct
10795 enclosing context for the inner class we're trying to
10796 create. Build an access to the proper enclosing context
10797 and use it. */
10798 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10799 && this_type != TREE_TYPE (mec))
10802 extra_arg = build_access_to_thisn (current_class,
10803 TREE_TYPE (mec), 0);
10804 extra_arg = java_complete_tree (extra_arg);
10806 /* Otherwise, just use the current `this' as an enclosing
10807 context. */
10808 else
10809 extra_arg = current_this;
10810 args = tree_cons (NULL_TREE, extra_arg, args);
10812 else
10813 args = tree_cons (NULL_TREE, integer_zero_node, args);
10816 /* This handles the situation where a constructor invocation needs
10817 to have an enclosing context passed as a second parameter (the
10818 constructor is one of an inner class). */
10819 if ((is_super_init ||
10820 (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10821 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10823 tree dest = TYPE_NAME (DECL_CONTEXT (list));
10824 tree extra_arg =
10825 build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10826 extra_arg = java_complete_tree (extra_arg);
10827 args = tree_cons (NULL_TREE, extra_arg, args);
10830 is_static_flag = METHOD_STATIC (list);
10831 if (! is_static_flag && this_arg != NULL_TREE)
10832 args = tree_cons (NULL_TREE, this_arg, args);
10834 /* In the context of an explicit constructor invocation, we can't
10835 invoke any method relying on `this'. Exceptions are: we're
10836 invoking a static function, primary exists and is not the current
10837 this, we're creating a new object. */
10838 if (ctxp->explicit_constructor_p
10839 && !is_static_flag
10840 && (!primary || primary == current_this)
10841 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10843 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
10844 PATCH_METHOD_RETURN_ERROR ();
10846 java_parser_context_restore_global ();
10847 if (is_static)
10848 *is_static = is_static_flag;
10849 /* Sometimes, we want the decl of the selected method. Such as for
10850 EH checking */
10851 if (ret_decl)
10852 *ret_decl = list;
10853 patch = patch_invoke (patch, list, args);
10855 /* Now is a good time to insert the call to finit$ */
10856 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10858 tree finit_parms, finit_call;
10860 /* Prepare to pass hidden parameters to finit$, if any. */
10861 finit_parms = build_alias_initializer_parameter_list
10862 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10864 finit_call =
10865 build_method_invocation (build_wfl_node (finit_identifier_node),
10866 finit_parms);
10868 /* Generate the code used to initialize fields declared with an
10869 initialization statement and build a compound statement along
10870 with the super constructor invocation. */
10871 CAN_COMPLETE_NORMALLY (patch) = 1;
10872 patch = build2 (COMPOUND_EXPR, void_type_node, patch,
10873 java_complete_tree (finit_call));
10875 return patch;
10878 /* Check that we're not trying to do a static reference to a method in
10879 non static method. Return 1 if it's the case, 0 otherwise. */
10881 static int
10882 check_for_static_method_reference (tree wfl, tree node, tree method,
10883 tree where, tree primary)
10885 if (METHOD_STATIC (current_function_decl)
10886 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10888 char *fct_name = xstrdup (lang_printable_name (method, 0));
10889 parse_error_context
10890 (wfl, "Can't make static reference to method %<%s %s%> in class %qs",
10891 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10892 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10893 free (fct_name);
10894 return 1;
10896 return 0;
10899 /* Fix the invocation of *MDECL if necessary in the case of a
10900 invocation from an inner class. *THIS_ARG might be modified
10901 appropriately and an alternative access to *MDECL might be
10902 returned. */
10904 static int
10905 maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10907 tree ctx;
10908 tree md = *mdecl, ta = *this_arg;
10909 int to_return = 0;
10910 int non_static_context = !METHOD_STATIC (md);
10912 if (is_super_init
10913 || DECL_CONTEXT (md) == current_class
10914 || !PURE_INNER_CLASS_TYPE_P (current_class)
10915 || DECL_FINIT_P (md)
10916 || DECL_INSTINIT_P (md))
10917 return 0;
10919 /* If we're calling a method found in an enclosing class, generate
10920 what it takes to retrieve the right this. Don't do that if we're
10921 invoking a static method. Note that if MD's type is unrelated to
10922 CURRENT_CLASS, then the current this can be used. */
10924 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10926 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10927 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10929 ta = build_current_thisn (current_class);
10930 ta = build_wfl_node (ta);
10932 else
10934 tree type = ctx;
10935 while (type)
10937 maybe_build_thisn_access_method (type);
10938 if (inherits_from_p (type, DECL_CONTEXT (md)))
10940 ta = build_access_to_thisn (ctx, type, 0);
10941 break;
10943 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10944 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10947 ta = java_complete_tree (ta);
10950 /* We might have to use an access method to get to MD. We can
10951 break the method access rule as far as we're not generating
10952 bytecode */
10953 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10955 md = build_outer_method_access_method (md);
10956 to_return = 1;
10959 *mdecl = md;
10960 *this_arg = ta;
10962 /* Returning a nonzero value indicates we were doing a non static
10963 method invocation that is now a static invocation. It will have
10964 callee displace `this' to insert it in the regular argument
10965 list. */
10966 return (non_static_context && to_return);
10969 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10970 mode. */
10972 static tree
10973 patch_invoke (tree patch, tree method, tree args)
10975 tree dtable, func;
10976 tree original_call, t, ta;
10977 tree check = NULL_TREE;
10979 /* Last step for args: convert build-in types. If we're dealing with
10980 a new TYPE() type call, the first argument to the constructor
10981 isn't found in the incoming argument list, but delivered by
10982 `new' */
10983 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10984 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10985 t = TREE_CHAIN (t);
10986 for (ta = args; t != end_params_node && ta;
10987 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10988 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10989 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10990 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10992 /* Resolve unresolved returned type issues */
10993 t = TREE_TYPE (TREE_TYPE (method));
10994 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10995 resolve_and_layout (TREE_TYPE (t), NULL);
10997 if (flag_emit_class_files)
10998 func = method;
10999 else
11001 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
11003 case INVOKE_VIRTUAL:
11004 dtable = invoke_build_dtable (0, args);
11005 func = build_invokevirtual (dtable, method);
11006 break;
11008 case INVOKE_NONVIRTUAL:
11009 /* If the object for the method call is null, we throw an
11010 exception. We don't do this if the object is the current
11011 method's `this'. In other cases we just rely on an
11012 optimization pass to eliminate redundant checks. */
11013 if (TREE_VALUE (args) != current_this)
11015 /* We use a save_expr here to make sure we only evaluate
11016 the new `self' expression once. */
11017 tree save_arg = save_expr (TREE_VALUE (args));
11018 TREE_VALUE (args) = save_arg;
11019 check = java_check_reference (save_arg, 1);
11021 /* Fall through. */
11023 case INVOKE_SUPER:
11024 case INVOKE_STATIC:
11026 tree signature = build_java_signature (TREE_TYPE (method));
11027 func = build_known_method_ref (method, TREE_TYPE (method),
11028 DECL_CONTEXT (method),
11029 signature, args);
11031 break;
11033 case INVOKE_INTERFACE:
11034 dtable = invoke_build_dtable (1, args);
11035 func = build_invokeinterface (dtable, method);
11036 break;
11038 default:
11039 abort ();
11042 /* Ensure self_type is initialized, (invokestatic). FIXME */
11043 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
11046 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
11047 TREE_OPERAND (patch, 0) = func;
11048 TREE_OPERAND (patch, 1) = args;
11049 patch = check_for_builtin (method, patch);
11050 original_call = patch;
11052 /* We're processing a `new TYPE ()' form. New is called and its
11053 returned value is the first argument to the constructor. We build
11054 a COMPOUND_EXPR and use saved expression so that the overall NEW
11055 expression value is a pointer to a newly created and initialized
11056 class. */
11057 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
11059 tree class = DECL_CONTEXT (method);
11060 tree c1, saved_new, new;
11061 tree alloc_node;
11063 if (flag_emit_class_files)
11065 TREE_TYPE (patch) = build_pointer_type (class);
11066 return patch;
11068 if (!TYPE_SIZE (class))
11069 safe_layout_class (class);
11070 alloc_node =
11071 (class_has_finalize_method (class) ? alloc_object_node
11072 : alloc_no_finalizer_node);
11073 new = build3 (CALL_EXPR, promote_type (class),
11074 build_address_of (alloc_node),
11075 build_tree_list (NULL_TREE, build_class_ref (class)),
11076 NULL_TREE);
11077 saved_new = save_expr (new);
11078 c1 = build_tree_list (NULL_TREE, saved_new);
11079 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
11080 TREE_OPERAND (original_call, 1) = c1;
11081 TREE_SET_CODE (original_call, CALL_EXPR);
11082 patch = build2 (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
11085 /* If CHECK is set, then we are building a check to see if the object
11086 is NULL. */
11087 if (check != NULL_TREE)
11089 /* We have to call force_evaluation_order now because creating a
11090 COMPOUND_EXPR wraps the arg list in a way that makes it
11091 unrecognizable by force_evaluation_order later. Yuk. */
11092 patch = build2 (COMPOUND_EXPR, TREE_TYPE (patch), check,
11093 force_evaluation_order (patch));
11094 TREE_SIDE_EFFECTS (patch) = 1;
11097 /* In order to be able to modify PATCH later, we SAVE_EXPR it and
11098 put it as the first expression of a COMPOUND_EXPR. The second
11099 expression being an empty statement to be later patched if
11100 necessary. We remember a TREE_LIST (the PURPOSE is the method,
11101 the VALUE is the compound) in a hashtable and return a
11102 COMPOUND_EXPR built so that the result of the evaluation of the
11103 original PATCH node is returned. */
11104 if (STATIC_CLASS_INIT_OPT_P ()
11105 && current_function_decl && METHOD_STATIC (method))
11107 tree list;
11108 tree fndecl = current_function_decl;
11109 /* We have to call force_evaluation_order now because creating a
11110 COMPOUND_EXPR wraps the arg list in a way that makes it
11111 unrecognizable by force_evaluation_order later. Yuk. */
11112 tree save = force_evaluation_order (patch);
11113 tree type = TREE_TYPE (patch);
11115 patch = build2 (COMPOUND_EXPR, type, save, build_java_empty_stmt ());
11116 list = tree_cons (method, patch,
11117 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
11119 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
11121 patch = build2 (COMPOUND_EXPR, type, patch, save);
11124 return patch;
11127 static int
11128 invocation_mode (tree method, int super)
11130 int access = get_access_flags_from_decl (method);
11132 if (super)
11133 return INVOKE_SUPER;
11135 if (access & ACC_STATIC)
11136 return INVOKE_STATIC;
11138 /* We have to look for a constructor before we handle nonvirtual
11139 calls; otherwise the constructor will look nonvirtual. */
11140 if (DECL_CONSTRUCTOR_P (method))
11141 return INVOKE_STATIC;
11143 if (access & ACC_PRIVATE)
11144 return INVOKE_NONVIRTUAL;
11146 /* Binary compatibility: just because it's final today, that doesn't
11147 mean it'll be final tomorrow. */
11148 if (! flag_indirect_dispatch
11149 || DECL_CONTEXT (method) == object_type_node)
11151 if (access & ACC_FINAL)
11152 return INVOKE_NONVIRTUAL;
11154 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
11155 return INVOKE_NONVIRTUAL;
11158 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
11159 return INVOKE_INTERFACE;
11161 return INVOKE_VIRTUAL;
11164 /* Retrieve a refined list of matching methods. It covers the step
11165 15.11.2 (Compile-Time Step 2) */
11167 static tree
11168 lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
11170 tree atl = end_params_node; /* Arg Type List */
11171 tree method, signature, list, node;
11172 const char *candidates; /* Used for error report */
11173 char *dup;
11175 /* Fix the arguments */
11176 for (node = arg_list; node; node = TREE_CHAIN (node))
11178 tree current_arg = TREE_TYPE (TREE_VALUE (node));
11179 /* Non primitive type may have to be resolved */
11180 if (!JPRIMITIVE_TYPE_P (current_arg))
11181 resolve_and_layout (current_arg, NULL_TREE);
11182 /* And promoted */
11183 if (TREE_CODE (current_arg) == RECORD_TYPE)
11184 current_arg = promote_type (current_arg);
11185 atl = tree_cons (NULL_TREE, current_arg, atl);
11188 /* Presto. If we're dealing with an anonymous class and a
11189 constructor call, generate the right constructor now, since we
11190 know the arguments' types. */
11192 if (lc && ANONYMOUS_CLASS_P (class))
11194 tree mdecl = craft_constructor (TYPE_NAME (class), atl);
11195 /* The anonymous class may have already been laid out, so make sure
11196 the new constructor is laid out here. */
11197 layout_class_method (class, CLASSTYPE_SUPER (class), mdecl, NULL_TREE);
11200 /* Find all candidates and then refine the list, searching for the
11201 most specific method. */
11202 list = find_applicable_accessible_methods_list (lc, class, name, atl);
11203 list = find_most_specific_methods_list (list);
11204 if (list && !TREE_CHAIN (list))
11205 return TREE_VALUE (list);
11207 /* Issue an error. List candidates if any. Candidates are listed
11208 only if accessible (non accessible methods may end-up here for
11209 the sake of a better error report). */
11210 candidates = NULL;
11211 if (list)
11213 tree current;
11214 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
11215 for (current = list; current; current = TREE_CHAIN (current))
11217 tree cm = TREE_VALUE (current);
11218 char string [4096];
11219 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
11220 continue;
11221 sprintf
11222 (string, " '%s' in '%s'%s",
11223 get_printable_method_name (cm),
11224 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
11225 (TREE_CHAIN (current) ? "\n" : ""));
11226 obstack_grow (&temporary_obstack, string, strlen (string));
11228 obstack_1grow (&temporary_obstack, '\0');
11229 candidates = obstack_finish (&temporary_obstack);
11231 /* Issue the error message */
11232 method = make_node (FUNCTION_TYPE);
11233 TYPE_ARG_TYPES (method) = atl;
11234 signature = build_java_argument_signature (method);
11235 dup = xstrdup (lang_printable_name (class, 0));
11236 parse_error_context (cl, "Can't find %s %<%s(%s)%> in type %qs%s",
11237 (lc ? "constructor" : "method"),
11238 (lc ? dup : IDENTIFIER_POINTER (name)),
11239 IDENTIFIER_POINTER (signature), dup,
11240 (candidates ? candidates : ""));
11241 free (dup);
11242 return NULL_TREE;
11245 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
11246 when we're looking for a constructor. */
11248 static tree
11249 find_applicable_accessible_methods_list (int lc, tree class, tree name,
11250 tree arglist)
11252 static htab_t searched_classes;
11253 static int search_not_done = 0;
11254 tree list = NULL_TREE, all_list = NULL_TREE;
11255 tree base_binfo;
11256 int i;
11258 /* Check the hash table to determine if this class has been searched
11259 already. */
11260 if (searched_classes)
11262 if (htab_find (searched_classes, class) != NULL)
11263 return NULL;
11265 else
11267 searched_classes = htab_create (10, htab_hash_pointer,
11268 htab_eq_pointer, NULL);
11271 search_not_done++;
11272 *htab_find_slot (searched_classes, class, INSERT) = class;
11274 if (!CLASS_LOADED_P (class))
11276 load_class (class, 1);
11277 safe_layout_class (class);
11280 /* Search interfaces */
11281 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
11282 && CLASS_INTERFACE (TYPE_NAME (class)))
11284 search_applicable_methods_list (lc, TYPE_METHODS (class),
11285 name, arglist, &list, &all_list);
11286 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11288 tree t = BINFO_TYPE (base_binfo);
11289 tree rlist;
11291 rlist = find_applicable_accessible_methods_list (lc, t, name,
11292 arglist);
11293 list = chainon (rlist, list);
11296 /* Search classes */
11297 else
11299 search_applicable_methods_list (lc, TYPE_METHODS (class),
11300 name, arglist, &list, &all_list);
11302 /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11303 that we only search in class. Note that we should have found
11304 something at this point. */
11305 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11307 lc = 1;
11308 if (!list)
11309 abort ();
11312 /* We must search all interfaces of this class */
11313 if (!lc)
11315 for (i = 1;
11316 BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11318 tree t = BINFO_TYPE (base_binfo);
11319 if (t != object_type_node)
11321 tree rlist
11322 = find_applicable_accessible_methods_list (lc, t,
11323 name, arglist);
11324 list = chainon (rlist, list);
11329 /* Search superclass */
11330 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11332 tree rlist;
11333 class = CLASSTYPE_SUPER (class);
11334 rlist = find_applicable_accessible_methods_list (lc, class,
11335 name, arglist);
11336 list = chainon (rlist, list);
11340 search_not_done--;
11342 /* We're done. Reset the searched classes list and finally search
11343 java.lang.Object if it wasn't searched already. */
11344 if (!search_not_done)
11346 if (!lc
11347 && TYPE_METHODS (object_type_node)
11348 && htab_find (searched_classes, object_type_node) == NULL)
11350 search_applicable_methods_list (lc,
11351 TYPE_METHODS (object_type_node),
11352 name, arglist, &list, &all_list);
11354 htab_delete (searched_classes);
11355 searched_classes = NULL;
11358 /* Either return the list obtained or all selected (but
11359 inaccessible) methods for better error report. */
11360 return (!list ? all_list : list);
11363 /* Effectively search for the appropriate method in method */
11365 static void
11366 search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
11367 tree *list, tree *all_list)
11369 for (; method; method = TREE_CHAIN (method))
11371 /* When dealing with constructor, stop here, otherwise search
11372 other classes */
11373 if (lc && !DECL_CONSTRUCTOR_P (method))
11374 continue;
11375 else if (!lc && (DECL_CONSTRUCTOR_P (method)
11376 || (DECL_NAME (method) != name)))
11377 continue;
11379 if (argument_types_convertible (method, arglist))
11381 /* Retain accessible methods only */
11382 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11383 method, NULL_TREE, 0))
11384 *list = tree_cons (NULL_TREE, method, *list);
11385 else
11386 /* Also retain all selected method here */
11387 *all_list = tree_cons (NULL_TREE, method, *list);
11392 /* 15.11.2.2 Choose the Most Specific Method */
11394 static tree
11395 find_most_specific_methods_list (tree list)
11397 int max = 0;
11398 int abstract, candidates;
11399 tree current, new_list = NULL_TREE;
11400 for (current = list; current; current = TREE_CHAIN (current))
11402 tree method;
11403 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11405 for (method = list; method; method = TREE_CHAIN (method))
11407 tree method_v, current_v;
11408 /* Don't test a method against itself */
11409 if (method == current)
11410 continue;
11412 method_v = TREE_VALUE (method);
11413 current_v = TREE_VALUE (current);
11415 /* Compare arguments and location where methods where declared */
11416 if (argument_types_convertible (method_v, current_v))
11418 if (valid_method_invocation_conversion_p
11419 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v)))
11421 int v = (DECL_SPECIFIC_COUNT (current_v) += 1);
11422 max = (v > max ? v : max);
11428 /* Review the list and select the maximally specific methods */
11429 for (current = list, abstract = -1, candidates = -1;
11430 current; current = TREE_CHAIN (current))
11431 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11433 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11434 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11435 candidates++;
11438 /* If we have several and they're all abstract, just pick the
11439 closest one. */
11440 if (candidates > 0 && candidates == abstract)
11442 /* FIXME: merge the throws clauses. There is no convenient way
11443 to do this in gcj right now, since ideally we'd like to
11444 introduce a new METHOD_DECL here, but that is really not
11445 possible. */
11446 new_list = nreverse (new_list);
11447 TREE_CHAIN (new_list) = NULL_TREE;
11448 return new_list;
11451 /* We have several (we couldn't find a most specific), all but one
11452 are abstract, we pick the only non abstract one. */
11453 if (candidates > 0 && (candidates == abstract+1))
11455 for (current = new_list; current; current = TREE_CHAIN (current))
11456 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11458 TREE_CHAIN (current) = NULL_TREE;
11459 new_list = current;
11463 /* If we can't find one, lower expectations and try to gather multiple
11464 maximally specific methods */
11465 while (!new_list && max)
11467 while (--max > 0)
11469 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11470 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11474 return new_list;
11477 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11478 converted by method invocation conversion (5.3) to the type of the
11479 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11480 to change less often than M1. */
11482 static GTY(()) tree m2_arg_value;
11483 static GTY(()) tree m2_arg_cache;
11485 static int
11486 argument_types_convertible (tree m1, tree m2_or_arglist)
11488 tree m1_arg, m2_arg;
11490 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11492 if (m2_arg_value == m2_or_arglist)
11493 m2_arg = m2_arg_cache;
11494 else
11496 /* M2_OR_ARGLIST can be a function DECL or a raw list of
11497 argument types */
11498 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11500 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11501 if (!METHOD_STATIC (m2_or_arglist))
11502 m2_arg = TREE_CHAIN (m2_arg);
11504 else
11505 m2_arg = m2_or_arglist;
11507 m2_arg_value = m2_or_arglist;
11508 m2_arg_cache = m2_arg;
11511 while (m1_arg != end_params_node && m2_arg != end_params_node)
11513 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11514 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11515 TREE_VALUE (m2_arg)))
11516 break;
11517 m1_arg = TREE_CHAIN (m1_arg);
11518 m2_arg = TREE_CHAIN (m2_arg);
11520 return m1_arg == end_params_node && m2_arg == end_params_node;
11523 /* Qualification routines */
11525 /* Given a name x.y.z, look up x locally. If it's found, save the
11526 decl. If it's not found, mark the name as RESOLVE_PACKAGE_NAME_P,
11527 so that we later try and load the appropriate classes. */
11528 static void
11529 qualify_ambiguous_name (tree id)
11531 tree name, decl;
11533 /* We inspect the first item of the qualification list. As a sanity
11534 check, make sure that it is an identfier node. */
11535 tree qual = EXPR_WFL_QUALIFICATION (id);
11536 tree qual_wfl = QUAL_WFL (qual);
11538 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11539 return;
11541 name = EXPR_WFL_NODE (qual_wfl);
11543 /* If we don't have an identifier, or we have a 'this' or 'super',
11544 then field access processing is all we need : there is nothing
11545 for us to do. */
11546 if (!name || TREE_CODE (name) != IDENTIFIER_NODE ||
11547 name == this_identifier_node ||
11548 name == super_identifier_node)
11549 return;
11551 /* If name appears within the scope of a local variable declaration
11552 or parameter declaration, or is a field within an enclosing
11553 class, then it is an expression name. Save the decl and let
11554 resolve_field_access do it's work. */
11555 if ((decl = IDENTIFIER_LOCAL_VALUE (name)) ||
11556 (decl = lookup_field_wrapper (current_class, name)))
11558 QUAL_RESOLUTION (qual) = decl;
11559 return;
11562 /* If name is a known class name (either declared or imported), mark
11563 us as a type name. */
11564 if ((decl = resolve_and_layout (name, NULL_TREE)))
11566 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11567 QUAL_RESOLUTION (qual) = decl;
11570 /* Check here that NAME isn't declared by more than one
11571 type-import-on-demand declaration of the compilation unit
11572 containing NAME. FIXME */
11574 /* We couldn't find a declaration for the name. Assume for now that
11575 we have a qualified class name that needs to be loaded from an
11576 external class file. */
11577 else
11578 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11580 /* Propagate the qualification across other components of the
11581 qualified name */
11582 for (qual = TREE_CHAIN (qual); qual;
11583 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11585 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11586 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11589 /* Store the global qualification for the ambiguous part of ID back
11590 into ID fields */
11591 if (RESOLVE_TYPE_NAME_P (qual_wfl))
11592 RESOLVE_TYPE_NAME_P (id) = 1;
11593 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11594 RESOLVE_PACKAGE_NAME_P (id) = 1;
11597 /* Patch tree nodes in a function body. When a BLOCK is found, push
11598 local variable decls if present.
11599 Same as java_complete_lhs, but does resolve static finals to values. */
11601 static tree
11602 java_complete_tree (tree node)
11604 node = java_complete_lhs (node);
11605 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11606 && DECL_INITIAL (node) != NULL_TREE)
11608 tree value = fold_constant_for_init (node, node);
11609 if (value != NULL_TREE)
11610 return value;
11612 return node;
11615 static tree
11616 java_stabilize_reference (tree node)
11618 if (TREE_CODE (node) == COMPOUND_EXPR)
11620 tree op0 = TREE_OPERAND (node, 0);
11621 tree op1 = TREE_OPERAND (node, 1);
11622 TREE_OPERAND (node, 0) = save_expr (op0);
11623 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11624 return node;
11626 return stabilize_reference (node);
11629 /* Patch tree nodes in a function body. When a BLOCK is found, push
11630 local variable decls if present.
11631 Same as java_complete_tree, but does not resolve static finals to values. */
11633 static tree
11634 java_complete_lhs (tree node)
11636 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11637 int flag;
11639 /* CONVERT_EXPR always has its type set, even though it needs to be
11640 worked out. */
11641 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11642 return node;
11644 /* The switch block implements cases processing container nodes
11645 first. Contained nodes are always written back. Leaves come
11646 next and return a value. */
11647 switch (TREE_CODE (node))
11649 case BLOCK:
11651 /* 1- Block section.
11652 Set the local values on decl names so we can identify them
11653 faster when they're referenced. At that stage, identifiers
11654 are legal so we don't check for declaration errors. */
11655 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11657 DECL_CONTEXT (cn) = current_function_decl;
11658 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11660 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11661 CAN_COMPLETE_NORMALLY (node) = 1;
11662 else
11664 tree stmt = BLOCK_EXPR_BODY (node);
11665 tree *ptr;
11666 int error_seen = 0;
11667 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11669 /* Re-order from (((A; B); C); ...; Z) to
11670 (A; (B; (C ; (...; Z)))).
11671 This makes it easier to scan the statements left-to-right
11672 without using recursion (which might overflow the stack
11673 if the block has many statements. */
11674 for (;;)
11676 tree left = TREE_OPERAND (stmt, 0);
11677 if (TREE_CODE (left) != COMPOUND_EXPR)
11678 break;
11679 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11680 TREE_OPERAND (left, 1) = stmt;
11681 stmt = left;
11683 BLOCK_EXPR_BODY (node) = stmt;
11686 /* Now do the actual complete, without deep recursion for
11687 long blocks. */
11688 ptr = &BLOCK_EXPR_BODY (node);
11689 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11690 && !IS_EMPTY_STMT (TREE_OPERAND (*ptr, 1)))
11692 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11693 tree *next = &TREE_OPERAND (*ptr, 1);
11694 TREE_OPERAND (*ptr, 0) = cur;
11695 if (IS_EMPTY_STMT (cur))
11697 /* Optimization; makes it easier to detect empty bodies.
11698 Most useful for <clinit> with all-constant initializer. */
11699 *ptr = *next;
11700 continue;
11702 if (TREE_CODE (cur) == ERROR_MARK)
11703 error_seen++;
11704 else if (! CAN_COMPLETE_NORMALLY (cur))
11706 wfl_op2 = *next;
11707 for (;;)
11709 if (TREE_CODE (wfl_op2) == BLOCK)
11710 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11711 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11712 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11713 else
11714 break;
11716 if (TREE_CODE (wfl_op2) != CASE_EXPR
11717 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11718 unreachable_stmt_error (*ptr);
11720 if (TREE_TYPE (*ptr) == NULL_TREE)
11721 TREE_TYPE (*ptr) = void_type_node;
11722 ptr = next;
11724 *ptr = java_complete_tree (*ptr);
11726 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11727 return error_mark_node;
11728 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11730 /* Turn local bindings to null */
11731 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11732 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11734 TREE_TYPE (node) = void_type_node;
11735 break;
11737 /* 2- They are expressions but ultimately deal with statements */
11739 case THROW_EXPR:
11740 wfl_op1 = TREE_OPERAND (node, 0);
11741 COMPLETE_CHECK_OP_0 (node);
11742 /* 14.19 A throw statement cannot complete normally. */
11743 CAN_COMPLETE_NORMALLY (node) = 0;
11744 return patch_throw_statement (node, wfl_op1);
11746 case SYNCHRONIZED_EXPR:
11747 wfl_op1 = TREE_OPERAND (node, 0);
11748 return patch_synchronized_statement (node, wfl_op1);
11750 case TRY_EXPR:
11751 return patch_try_statement (node);
11753 case TRY_FINALLY_EXPR:
11754 COMPLETE_CHECK_OP_0 (node);
11755 COMPLETE_CHECK_OP_1 (node);
11756 if (IS_EMPTY_STMT (TREE_OPERAND (node, 0)))
11757 /* Reduce try/finally nodes with an empty try block. */
11758 return TREE_OPERAND (node, 1);
11759 if (IS_EMPTY_STMT (TREE_OPERAND (node, 1)))
11760 /* Likewise for an empty finally block. */
11761 return TREE_OPERAND (node, 0);
11762 CAN_COMPLETE_NORMALLY (node)
11763 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11764 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11765 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11766 return node;
11768 case LABELED_BLOCK_EXPR:
11769 PUSH_LABELED_BLOCK (node);
11770 if (LABELED_BLOCK_BODY (node))
11771 COMPLETE_CHECK_OP_1 (node);
11772 TREE_TYPE (node) = void_type_node;
11773 POP_LABELED_BLOCK ();
11775 if (IS_EMPTY_STMT (LABELED_BLOCK_BODY (node)))
11777 LABELED_BLOCK_BODY (node) = NULL_TREE;
11778 CAN_COMPLETE_NORMALLY (node) = 1;
11780 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11781 CAN_COMPLETE_NORMALLY (node) = 1;
11782 return node;
11784 case EXIT_BLOCK_EXPR:
11785 return patch_bc_statement (node);
11787 case CASE_EXPR:
11788 cn = java_complete_tree (TREE_OPERAND (node, 0));
11789 if (cn == error_mark_node)
11790 return cn;
11792 /* First, the case expression must be constant. Values of final
11793 fields are accepted. */
11794 nn = fold_constant_for_init (cn, NULL_TREE);
11795 if (nn != NULL_TREE)
11796 cn = nn;
11798 cn = fold (cn);
11799 if ((TREE_CODE (cn) == COMPOUND_EXPR
11800 || TREE_CODE (cn) == COMPONENT_REF)
11801 && JDECL_P (TREE_OPERAND (cn, 1))
11802 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11803 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11805 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11806 TREE_OPERAND (cn, 1));
11808 /* Accept final locals too. */
11809 else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn)
11810 && DECL_INITIAL (cn))
11811 cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11813 if (!TREE_CONSTANT (cn))
11815 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11816 parse_error_context (node, "Constant expression required");
11817 return error_mark_node;
11820 nn = ctxp->current_loop;
11822 /* It must be assignable to the type of the switch expression. */
11823 if (!try_builtin_assignconv (NULL_TREE,
11824 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11826 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11827 parse_error_context
11828 (wfl_operator,
11829 "Incompatible type for case. Can't convert %qs to %<int%>",
11830 lang_printable_name (TREE_TYPE (cn), 0));
11831 return error_mark_node;
11834 cn = fold (convert (int_type_node, cn));
11835 TREE_CONSTANT_OVERFLOW (cn) = 0;
11836 CAN_COMPLETE_NORMALLY (cn) = 1;
11838 /* Save the label on a list so that we can later check for
11839 duplicates. */
11840 case_label_list = tree_cons (node, cn, case_label_list);
11842 /* Multiple instance of a case label bearing the same value is
11843 checked later. The case expression is all right so far. */
11844 if (TREE_CODE (cn) == VAR_DECL)
11845 cn = DECL_INITIAL (cn);
11846 TREE_OPERAND (node, 0) = cn;
11847 TREE_TYPE (node) = void_type_node;
11848 CAN_COMPLETE_NORMALLY (node) = 1;
11849 TREE_SIDE_EFFECTS (node) = 1;
11850 break;
11852 case DEFAULT_EXPR:
11853 nn = ctxp->current_loop;
11854 /* Only one default label is allowed per switch statement */
11855 if (SWITCH_HAS_DEFAULT (nn))
11857 #ifdef USE_MAPPED_LOCATION
11858 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
11859 #else
11860 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11861 #endif
11862 parse_error_context (wfl_operator,
11863 "Duplicate case label: %<default%>");
11864 return error_mark_node;
11866 else
11867 SWITCH_HAS_DEFAULT (nn) = 1;
11868 TREE_TYPE (node) = void_type_node;
11869 TREE_SIDE_EFFECTS (node) = 1;
11870 CAN_COMPLETE_NORMALLY (node) = 1;
11871 break;
11873 case SWITCH_EXPR:
11874 case LOOP_EXPR:
11875 PUSH_LOOP (node);
11876 /* Check whether the loop was enclosed in a labeled
11877 statement. If not, create one, insert the loop in it and
11878 return the node */
11879 nn = patch_loop_statement (node);
11881 /* Anyways, walk the body of the loop */
11882 if (TREE_CODE (node) == LOOP_EXPR)
11883 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11884 /* Switch statement: walk the switch expression and the cases */
11885 else
11886 node = patch_switch_statement (node);
11888 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11889 nn = error_mark_node;
11890 else
11892 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11893 /* If we returned something different, that's because we
11894 inserted a label. Pop the label too. */
11895 if (nn != node)
11897 if (CAN_COMPLETE_NORMALLY (node))
11898 CAN_COMPLETE_NORMALLY (nn) = 1;
11899 POP_LABELED_BLOCK ();
11902 POP_LOOP ();
11903 return nn;
11905 case EXIT_EXPR:
11906 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11907 return patch_exit_expr (node);
11909 case COND_EXPR:
11910 /* Condition */
11911 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11912 if (TREE_OPERAND (node, 0) == error_mark_node)
11913 return error_mark_node;
11914 /* then-else branches */
11915 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11916 if (TREE_OPERAND (node, 1) == error_mark_node)
11917 return error_mark_node;
11919 /* This is a special case due to build_assertion(). When
11920 assertions are disabled we build a COND_EXPR in which
11921 Operand 1 is the body of the assertion. If that happens to
11922 be a string concatenation we'll need to patch it here. */
11923 tree patched = patch_string (TREE_OPERAND (node, 1));
11924 if (patched)
11925 TREE_OPERAND (node, 1) = patched;
11927 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11928 if (TREE_OPERAND (node, 2) == error_mark_node)
11929 return error_mark_node;
11930 return patch_if_else_statement (node);
11931 break;
11933 case CONDITIONAL_EXPR:
11934 /* Condition */
11935 wfl_op1 = TREE_OPERAND (node, 0);
11936 COMPLETE_CHECK_OP_0 (node);
11937 wfl_op2 = TREE_OPERAND (node, 1);
11938 COMPLETE_CHECK_OP_1 (node);
11939 wfl_op3 = TREE_OPERAND (node, 2);
11940 COMPLETE_CHECK_OP_2 (node);
11941 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11943 /* 3- Expression section */
11944 case COMPOUND_EXPR:
11945 wfl_op2 = TREE_OPERAND (node, 1);
11946 TREE_OPERAND (node, 0) = nn =
11947 java_complete_tree (TREE_OPERAND (node, 0));
11948 if (IS_EMPTY_STMT (wfl_op2))
11949 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11950 else
11952 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11954 /* An unreachable condition in a do-while statement
11955 is *not* (technically) an unreachable statement. */
11956 nn = wfl_op2;
11957 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11958 nn = EXPR_WFL_NODE (nn);
11959 /* NN can be NULL_TREE exactly when UPDATE is, in
11960 finish_for_loop. */
11961 if (nn != NULL_TREE && TREE_CODE (nn) != EXIT_EXPR)
11963 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11964 if (SUPPRESS_UNREACHABLE_ERROR (nn))
11966 /* Perhaps this warning should have an
11967 associated flag. The code being compiled is
11968 pedantically correct, but useless. */
11969 parse_warning_context (wfl_operator,
11970 "Unreachable statement");
11972 else
11973 parse_error_context (wfl_operator,
11974 "Unreachable statement");
11977 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11978 if (TREE_OPERAND (node, 1) == error_mark_node)
11979 return error_mark_node;
11980 /* Even though we might allow the case where the first
11981 operand doesn't return normally, we still should compute
11982 CAN_COMPLETE_NORMALLY correctly. */
11983 CAN_COMPLETE_NORMALLY (node)
11984 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11985 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11987 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11988 break;
11990 case RETURN_EXPR:
11991 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11992 return patch_return (node);
11994 case EXPR_WITH_FILE_LOCATION:
11995 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11996 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11998 node = resolve_expression_name (node, NULL);
11999 if (node == error_mark_node)
12000 return node;
12001 CAN_COMPLETE_NORMALLY (node) = 1;
12003 else
12005 tree body;
12006 location_t save_location = input_location;
12007 #ifdef USE_MAPPED_LOCATION
12008 input_location = EXPR_LOCATION (node);
12009 if (input_location == UNKNOWN_LOCATION)
12010 input_location = save_location;
12011 #else
12012 input_line = EXPR_WFL_LINENO (node);
12013 #endif
12014 body = java_complete_tree (EXPR_WFL_NODE (node));
12015 input_location = save_location;
12016 EXPR_WFL_NODE (node) = body;
12017 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
12018 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
12019 if (IS_EMPTY_STMT (body) || TREE_CONSTANT (body))
12021 /* Makes it easier to constant fold, detect empty bodies. */
12022 return body;
12024 if (body == error_mark_node)
12026 /* Its important for the evaluation of assignment that
12027 this mark on the TREE_TYPE is propagated. */
12028 TREE_TYPE (node) = error_mark_node;
12029 return error_mark_node;
12031 else
12032 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
12035 break;
12037 case NEW_ARRAY_EXPR:
12038 /* Patch all the dimensions */
12039 flag = 0;
12040 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12042 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
12043 tree dim = convert (int_type_node,
12044 java_complete_tree (TREE_VALUE (cn)));
12045 if (dim == error_mark_node)
12047 flag = 1;
12048 continue;
12050 else
12052 TREE_VALUE (cn) = dim;
12053 /* Setup the location of the current dimension, for
12054 later error report. */
12055 #ifdef USE_MAPPED_LOCATION
12056 TREE_PURPOSE (cn) = expr_add_location (NULL_TREE, location, 0);
12057 #else
12058 TREE_PURPOSE (cn) =
12059 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
12060 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
12061 #endif
12064 /* They complete the array creation expression, if no errors
12065 were found. */
12066 CAN_COMPLETE_NORMALLY (node) = 1;
12067 return (flag ? error_mark_node
12068 : force_evaluation_order (patch_newarray (node)));
12070 case NEW_ANONYMOUS_ARRAY_EXPR:
12071 /* Create the array type if necessary. */
12072 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
12074 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
12075 if (!(type = resolve_type_during_patch (type)))
12076 return error_mark_node;
12077 type = build_array_from_name (type, NULL_TREE,
12078 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
12079 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
12081 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
12082 ANONYMOUS_ARRAY_INITIALIZER (node));
12083 if (node == error_mark_node)
12084 return error_mark_node;
12085 CAN_COMPLETE_NORMALLY (node) = 1;
12086 return node;
12088 case NEW_CLASS_EXPR:
12089 case CALL_EXPR:
12090 /* Complete function's argument(s) first */
12091 if (complete_function_arguments (node))
12092 return error_mark_node;
12093 else
12095 tree decl, wfl = TREE_OPERAND (node, 0);
12096 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
12097 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
12098 super_identifier_node);
12099 tree arguments;
12100 #ifdef USE_MAPPED_LOCATION
12101 source_location location = EXPR_LOCATION (node);
12102 #else
12103 int location = EXPR_WFL_LINECOL (node);
12104 #endif
12106 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12107 from_super, 0, &decl);
12108 if (node == error_mark_node)
12109 return error_mark_node;
12111 if (TREE_CODE (node) == CALL_EXPR
12112 && TREE_OPERAND (node, 1) != NULL_TREE)
12113 arguments = TREE_VALUE (TREE_OPERAND (node, 1));
12114 else
12115 arguments = NULL_TREE;
12116 check_thrown_exceptions (location, decl, arguments);
12117 /* If we call this(...), register signature and positions */
12118 if (in_this)
12119 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12120 tree_cons (wfl, decl,
12121 DECL_CONSTRUCTOR_CALLS (current_function_decl));
12122 CAN_COMPLETE_NORMALLY (node) = 1;
12123 return force_evaluation_order (node);
12126 case MODIFY_EXPR:
12127 /* Save potential wfls */
12128 wfl_op1 = TREE_OPERAND (node, 0);
12129 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12131 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12132 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12133 && DECL_INITIAL (nn) != NULL_TREE)
12135 tree value;
12137 value = fold_constant_for_init (nn, nn);
12139 /* When we have a primitype type, or a string and we're not
12140 emitting a class file, we actually don't want to generate
12141 anything for the assignment. */
12142 if (value != NULL_TREE &&
12143 (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
12144 (TREE_TYPE (value) == string_ptr_type_node &&
12145 ! flag_emit_class_files)))
12147 /* Prepare node for patch_assignment */
12148 TREE_OPERAND (node, 1) = value;
12149 /* Call patch assignment to verify the assignment */
12150 if (patch_assignment (node, wfl_op1) == error_mark_node)
12151 return error_mark_node;
12152 /* Set DECL_INITIAL properly (a conversion might have
12153 been decided by patch_assignment) and return the
12154 empty statement. */
12155 else
12157 tree patched = patch_string (TREE_OPERAND (node, 1));
12158 if (patched)
12159 DECL_INITIAL (nn) = patched;
12160 else
12161 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12162 DECL_FIELD_FINAL_IUD (nn) = 1;
12163 return build_java_empty_stmt ();
12166 if (! flag_emit_class_files)
12167 DECL_INITIAL (nn) = NULL_TREE;
12169 wfl_op2 = TREE_OPERAND (node, 1);
12171 if (TREE_OPERAND (node, 0) == error_mark_node)
12172 return error_mark_node;
12174 flag = COMPOUND_ASSIGN_P (wfl_op2);
12175 if (flag)
12177 /* This might break when accessing outer field from inner
12178 class. TESTME, FIXME */
12179 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12181 /* Hand stabilize the lhs on both places */
12182 TREE_OPERAND (node, 0) = lvalue;
12183 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12184 (flag_emit_class_files ? lvalue : save_expr (lvalue));
12186 /* 15.25.2.a: Left hand is not an array access. FIXME */
12187 /* Now complete the RHS. We write it back later on. */
12188 nn = java_complete_tree (TREE_OPERAND (node, 1));
12190 if ((cn = patch_string (nn)))
12191 nn = cn;
12193 /* The last part of the rewrite for E1 op= E2 is to have
12194 E1 = (T)(E1 op E2), with T being the type of E1. */
12195 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12196 TREE_TYPE (lvalue), nn));
12198 /* If the assignment is compound and has reference type,
12199 then ensure the LHS has type String and nothing else. */
12200 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12201 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12202 parse_error_context (wfl_op2,
12203 "Incompatible type for %<+=%>. Can't convert %qs to %<java.lang.String%>",
12204 lang_printable_name (TREE_TYPE (lvalue), 0));
12206 /* 15.25.2.b: Left hand is an array access. FIXME */
12209 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12210 function to complete this RHS. Note that a NEW_ARRAY_INIT
12211 might have been already fully expanded if created as a result
12212 of processing an anonymous array initializer. We avoid doing
12213 the operation twice by testing whether the node already bears
12214 a type. */
12215 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12216 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12217 TREE_OPERAND (node, 1));
12218 /* Otherwise we simply complete the RHS */
12219 else
12220 nn = java_complete_tree (TREE_OPERAND (node, 1));
12222 if (nn == error_mark_node)
12223 return error_mark_node;
12225 /* Write back the RHS as we evaluated it. */
12226 TREE_OPERAND (node, 1) = nn;
12228 /* In case we're handling = with a String as a RHS, we need to
12229 produce a String out of the RHS (it might still be a
12230 STRING_CST or a StringBuffer at this stage */
12231 if ((nn = patch_string (TREE_OPERAND (node, 1))))
12232 TREE_OPERAND (node, 1) = nn;
12234 if ((nn = nested_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12235 TREE_OPERAND (node, 1))))
12237 /* We return error_mark_node if nested_field_access_fix
12238 detects we write into a final. */
12239 if (nn == error_mark_node)
12240 return error_mark_node;
12241 node = nn;
12243 else
12245 node = patch_assignment (node, wfl_op1);
12246 if (node == error_mark_node)
12247 return error_mark_node;
12248 /* Reorganize the tree if necessary. */
12249 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12250 || JSTRING_P (TREE_TYPE (node))))
12251 node = java_refold (node);
12254 /* Seek to set DECL_INITIAL to a proper value, since it might have
12255 undergone a conversion in patch_assignment. We do that only when
12256 it's necessary to have DECL_INITIAL properly set. */
12257 nn = TREE_OPERAND (node, 0);
12258 if (TREE_CODE (nn) == VAR_DECL
12259 && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12260 && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12261 && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12262 || TREE_TYPE (nn) == string_ptr_type_node))
12263 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12265 CAN_COMPLETE_NORMALLY (node) = 1;
12266 return node;
12268 case MULT_EXPR:
12269 case PLUS_EXPR:
12270 case MINUS_EXPR:
12271 case LSHIFT_EXPR:
12272 case RSHIFT_EXPR:
12273 case URSHIFT_EXPR:
12274 case BIT_AND_EXPR:
12275 case BIT_XOR_EXPR:
12276 case BIT_IOR_EXPR:
12277 case TRUNC_MOD_EXPR:
12278 case TRUNC_DIV_EXPR:
12279 case RDIV_EXPR:
12280 case TRUTH_ANDIF_EXPR:
12281 case TRUTH_ORIF_EXPR:
12282 case EQ_EXPR:
12283 case NE_EXPR:
12284 case GT_EXPR:
12285 case GE_EXPR:
12286 case LT_EXPR:
12287 case LE_EXPR:
12288 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12289 knows how to handle those cases. */
12290 wfl_op1 = TREE_OPERAND (node, 0);
12291 wfl_op2 = TREE_OPERAND (node, 1);
12293 CAN_COMPLETE_NORMALLY (node) = 1;
12294 /* Don't complete string nodes if dealing with the PLUS operand. */
12295 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12297 nn = java_complete_tree (wfl_op1);
12298 if (nn == error_mark_node)
12299 return error_mark_node;
12301 TREE_OPERAND (node, 0) = nn;
12303 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12305 nn = java_complete_tree (wfl_op2);
12306 if (nn == error_mark_node)
12307 return error_mark_node;
12309 TREE_OPERAND (node, 1) = nn;
12311 return patch_binop (node, wfl_op1, wfl_op2, 0);
12313 case INSTANCEOF_EXPR:
12314 wfl_op1 = TREE_OPERAND (node, 0);
12315 COMPLETE_CHECK_OP_0 (node);
12316 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1), 0);
12318 case UNARY_PLUS_EXPR:
12319 case NEGATE_EXPR:
12320 case TRUTH_NOT_EXPR:
12321 case BIT_NOT_EXPR:
12322 case PREDECREMENT_EXPR:
12323 case PREINCREMENT_EXPR:
12324 case POSTDECREMENT_EXPR:
12325 case POSTINCREMENT_EXPR:
12326 case CONVERT_EXPR:
12327 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12328 how to handle those cases. */
12329 wfl_op1 = TREE_OPERAND (node, 0);
12330 CAN_COMPLETE_NORMALLY (node) = 1;
12331 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12332 if (TREE_OPERAND (node, 0) == error_mark_node)
12333 return error_mark_node;
12334 node = patch_unaryop (node, wfl_op1);
12335 CAN_COMPLETE_NORMALLY (node) = 1;
12336 break;
12338 case ARRAY_REF:
12339 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12340 how to handle those cases. */
12341 wfl_op1 = TREE_OPERAND (node, 0);
12342 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12343 if (TREE_OPERAND (node, 0) == error_mark_node)
12344 return error_mark_node;
12345 if (!flag_emit_class_files)
12346 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12347 /* The same applies to wfl_op2 */
12348 wfl_op2 = TREE_OPERAND (node, 1);
12349 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12350 if (TREE_OPERAND (node, 1) == error_mark_node)
12351 return error_mark_node;
12352 if (!flag_emit_class_files)
12353 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12354 return patch_array_ref (node);
12356 case RECORD_TYPE:
12357 return node;;
12359 case COMPONENT_REF:
12360 /* The first step in the re-write of qualified name handling. FIXME.
12361 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12362 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12363 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12365 tree name = TREE_OPERAND (node, 1);
12366 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12367 if (field == NULL_TREE)
12369 error ("missing static field %qs", IDENTIFIER_POINTER (name));
12370 return error_mark_node;
12372 if (! FIELD_STATIC (field))
12374 error ("not a static field %qs", IDENTIFIER_POINTER (name));
12375 return error_mark_node;
12377 return field;
12379 else
12380 abort ();
12381 break;
12383 case THIS_EXPR:
12384 /* Can't use THIS in a static environment */
12385 if (!current_this)
12387 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12388 parse_error_context (wfl_operator,
12389 "Keyword %<this%> used outside allowed context");
12390 TREE_TYPE (node) = error_mark_node;
12391 return error_mark_node;
12393 if (ctxp->explicit_constructor_p)
12395 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12396 parse_error_context
12397 (wfl_operator, "Can't reference %<this%> or %<super%> before the superclass constructor has been called");
12398 TREE_TYPE (node) = error_mark_node;
12399 return error_mark_node;
12401 return current_this;
12403 case CLASS_LITERAL:
12404 CAN_COMPLETE_NORMALLY (node) = 1;
12405 node = patch_incomplete_class_ref (node);
12406 if (node == error_mark_node)
12407 return error_mark_node;
12408 break;
12410 default:
12411 CAN_COMPLETE_NORMALLY (node) = 1;
12412 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12413 and it's time to turn it into the appropriate String object */
12414 if ((nn = patch_string (node)))
12415 node = nn;
12416 else
12417 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12419 return node;
12422 /* Complete function call's argument. Return a nonzero value is an
12423 error was found. */
12425 static int
12426 complete_function_arguments (tree node)
12428 int flag = 0;
12429 tree cn;
12431 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12432 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12434 tree wfl = TREE_VALUE (cn), parm, temp;
12435 parm = java_complete_tree (wfl);
12437 if (parm == error_mark_node)
12439 flag = 1;
12440 continue;
12442 /* If we have a string literal that we haven't transformed yet or a
12443 crafted string buffer, as a result of the use of the String
12444 `+' operator. Build `parm.toString()' and expand it. */
12445 if ((temp = patch_string (parm)))
12446 parm = temp;
12448 TREE_VALUE (cn) = parm;
12450 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12451 return flag;
12454 /* Sometimes (for loops and variable initialized during their
12455 declaration), we want to wrap a statement around a WFL and turn it
12456 debugable. */
12458 static tree
12459 build_debugable_stmt (int location, tree stmt)
12461 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12463 #ifdef USE_MAPPED_LOCATION
12464 stmt = expr_add_location (stmt, location, 1);
12465 #else
12466 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12467 EXPR_WFL_LINECOL (stmt) = location;
12468 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12469 #endif
12471 return stmt;
12474 static tree
12475 build_expr_block (tree body, tree decls)
12477 tree node = make_node (BLOCK);
12478 BLOCK_EXPR_DECLS (node) = decls;
12479 BLOCK_EXPR_BODY (node) = body;
12480 if (body)
12481 TREE_TYPE (node) = TREE_TYPE (body);
12482 TREE_SIDE_EFFECTS (node) = 1;
12483 return node;
12486 /* Create a new function block and link it appropriately to current
12487 function block chain */
12489 static tree
12490 enter_block (void)
12492 tree b = build_expr_block (NULL_TREE, NULL_TREE);
12494 /* Link block B supercontext to the previous block. The current
12495 function DECL is used as supercontext when enter_a_block is called
12496 for the first time for a given function. The current function body
12497 (DECL_FUNCTION_BODY) is set to be block B. */
12499 tree fndecl = current_function_decl;
12501 if (!fndecl) {
12502 BLOCK_SUPERCONTEXT (b) = current_static_block;
12503 current_static_block = b;
12506 else if (!DECL_FUNCTION_BODY (fndecl))
12508 BLOCK_SUPERCONTEXT (b) = fndecl;
12509 DECL_FUNCTION_BODY (fndecl) = b;
12511 else
12513 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12514 DECL_FUNCTION_BODY (fndecl) = b;
12516 return b;
12519 /* Exit a block by changing the current function body
12520 (DECL_FUNCTION_BODY) to the current block super context, only if
12521 the block being exited isn't the method's top level one. */
12523 static tree
12524 exit_block (void)
12526 tree b;
12527 if (current_function_decl)
12529 b = DECL_FUNCTION_BODY (current_function_decl);
12530 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12531 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12533 else
12535 b = current_static_block;
12537 if (BLOCK_SUPERCONTEXT (b))
12538 current_static_block = BLOCK_SUPERCONTEXT (b);
12540 return b;
12543 /* Lookup for NAME in the nested function's blocks, all the way up to
12544 the current toplevel one. It complies with Java's local variable
12545 scoping rules. */
12547 static tree
12548 lookup_name_in_blocks (tree name)
12550 tree b = GET_CURRENT_BLOCK (current_function_decl);
12552 while (b != current_function_decl)
12554 tree current;
12556 /* Paranoid sanity check. To be removed */
12557 if (TREE_CODE (b) != BLOCK)
12558 abort ();
12560 for (current = BLOCK_EXPR_DECLS (b); current;
12561 current = TREE_CHAIN (current))
12562 if (DECL_NAME (current) == name)
12563 return current;
12564 b = BLOCK_SUPERCONTEXT (b);
12566 return NULL_TREE;
12569 static void
12570 maybe_absorb_scoping_blocks (void)
12572 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12574 tree b = exit_block ();
12575 java_method_add_stmt (current_function_decl, b);
12576 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", input_line));
12581 /* This section of the source is reserved to build_* functions that
12582 are building incomplete tree nodes and the patch_* functions that
12583 are completing them. */
12585 /* Wrap a non WFL node around a WFL. */
12587 static tree
12588 build_wfl_wrap (tree node, int location)
12590 tree wfl, node_to_insert = node;
12592 /* We want to process THIS . xxx symbolically, to keep it consistent
12593 with the way we're processing SUPER. A THIS from a primary as a
12594 different form than a SUPER. Turn THIS into something symbolic */
12595 if (TREE_CODE (node) == THIS_EXPR)
12596 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12597 else
12598 #ifdef USE_MAPPED_LOCATION
12599 wfl = build_unknown_wfl (NULL_TREE);
12601 SET_EXPR_LOCATION (wfl, location);
12602 #else
12603 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12605 EXPR_WFL_LINECOL (wfl) = location;
12606 #endif
12607 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12608 return wfl;
12611 /* Build a super() constructor invocation. Returns an empty statement if
12612 we're currently dealing with the class java.lang.Object. */
12614 static tree
12615 build_super_invocation (tree mdecl)
12617 if (DECL_CONTEXT (mdecl) == object_type_node)
12618 return build_java_empty_stmt ();
12619 else
12621 tree super_wfl = build_wfl_node (super_identifier_node);
12622 tree a = NULL_TREE, t;
12624 /* This is called after parsing is done, so the parser context
12625 won't be accurate. Set location info from current_class decl. */
12626 tree class_wfl = lookup_cl (TYPE_NAME (current_class));
12627 EXPR_WFL_LINECOL (super_wfl) = EXPR_WFL_LINECOL (class_wfl);
12629 /* If we're dealing with an anonymous class, pass the arguments
12630 of the crafted constructor along. */
12631 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12633 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12634 for (; t != end_params_node; t = TREE_CHAIN (t))
12635 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12637 return build_method_invocation (super_wfl, a);
12641 /* Build a SUPER/THIS qualified method invocation. */
12643 static tree
12644 build_this_super_qualified_invocation (int use_this, tree name, tree args,
12645 int lloc, int rloc)
12647 tree invok;
12648 tree wfl =
12649 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12650 EXPR_WFL_LINECOL (wfl) = lloc;
12651 invok = build_method_invocation (name, args);
12652 return make_qualified_primary (wfl, invok, rloc);
12655 /* Build an incomplete CALL_EXPR node. */
12657 static tree
12658 build_method_invocation (tree name, tree args)
12660 tree call = build3 (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12661 TREE_SIDE_EFFECTS (call) = 1;
12662 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12663 return call;
12666 /* Build an incomplete new xxx(...) node. */
12668 static tree
12669 build_new_invocation (tree name, tree args)
12671 tree call = build3 (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12672 TREE_SIDE_EFFECTS (call) = 1;
12673 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12674 return call;
12677 /* Build an incomplete assignment expression. */
12679 static tree
12680 build_assignment (int op, int op_location, tree lhs, tree rhs)
12682 tree assignment;
12683 /* Build the corresponding binop if we deal with a Compound
12684 Assignment operator. Mark the binop sub-tree as part of a
12685 Compound Assignment expression */
12686 if (op != ASSIGN_TK)
12688 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12689 COMPOUND_ASSIGN_P (rhs) = 1;
12691 assignment = build2 (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12692 TREE_SIDE_EFFECTS (assignment) = 1;
12693 EXPR_WFL_LINECOL (assignment) = op_location;
12694 return assignment;
12697 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12698 the buffer. This is used only for string conversion. */
12699 static char *
12700 string_convert_int_cst (tree node)
12702 /* Long.MIN_VALUE is -9223372036854775808, 20 characters. */
12703 static char buffer[21];
12705 unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12706 unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12707 char *p = buffer + sizeof (buffer);
12708 int neg = 0;
12710 unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12711 << (HOST_BITS_PER_WIDE_INT - 1));
12713 *--p = '\0';
12715 /* If negative, note the fact and negate the value. */
12716 if ((hi & hibit))
12718 lo = ~lo;
12719 hi = ~hi;
12720 if (++lo == 0)
12721 ++hi;
12722 neg = 1;
12725 /* Divide by 10 until there are no bits left. */
12728 unsigned HOST_WIDE_INT acc = 0;
12729 unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12730 unsigned int i;
12732 /* Use long division to compute the result and the remainder. */
12733 for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12735 /* Shift a bit into accumulator. */
12736 acc <<= 1;
12737 if ((hi & hibit))
12738 acc |= 1;
12740 /* Shift the value. */
12741 hi <<= 1;
12742 if ((lo & hibit))
12743 hi |= 1;
12744 lo <<= 1;
12746 /* Shift the correct bit into the result. */
12747 outhi <<= 1;
12748 if ((outlo & hibit))
12749 outhi |= 1;
12750 outlo <<= 1;
12751 if (acc >= 10)
12753 acc -= 10;
12754 outlo |= 1;
12758 /* '0' == 060 in Java, but might not be here (think EBCDIC). */
12759 *--p = '\060' + acc;
12761 hi = outhi;
12762 lo = outlo;
12764 while (hi || lo);
12766 if (neg)
12767 *--p = '\055'; /* '-' == 055 in Java, but might not be here. */
12769 return p;
12772 /* Print an INTEGER_CST node in a static buffer, and return the
12773 buffer. This is used only for error handling. */
12774 char *
12775 print_int_node (tree node)
12777 static char buffer [80];
12778 if (TREE_CONSTANT_OVERFLOW (node))
12779 sprintf (buffer, "<overflow>");
12781 if (TREE_INT_CST_HIGH (node) == 0)
12782 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12783 TREE_INT_CST_LOW (node));
12784 else if (TREE_INT_CST_HIGH (node) == -1
12785 && TREE_INT_CST_LOW (node) != 0)
12786 sprintf (buffer, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
12787 -TREE_INT_CST_LOW (node));
12788 else
12789 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12790 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12792 return buffer;
12796 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12797 context. */
12799 /* 15.25 Assignment operators. */
12801 static tree
12802 patch_assignment (tree node, tree wfl_op1)
12804 tree rhs = TREE_OPERAND (node, 1);
12805 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12806 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12807 int error_found = 0;
12808 int lvalue_from_array = 0;
12809 int is_return = 0;
12811 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12813 /* Lhs can be a named variable */
12814 if (JDECL_P (lvalue))
12816 lhs_type = TREE_TYPE (lvalue);
12818 /* Or Lhs can be an array access. */
12819 else if (TREE_CODE (lvalue) == ARRAY_REF)
12821 lhs_type = TREE_TYPE (lvalue);
12822 lvalue_from_array = 1;
12824 /* Or a field access */
12825 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12826 lhs_type = TREE_TYPE (lvalue);
12827 /* Or a function return slot */
12828 else if (TREE_CODE (lvalue) == RESULT_DECL)
12830 /* If the return type is an integral type, then we create the
12831 RESULT_DECL with a promoted type, but we need to do these
12832 checks against the unpromoted type to ensure type safety. So
12833 here we look at the real type, not the type of the decl we
12834 are modifying. */
12835 lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12836 is_return = 1;
12838 /* Otherwise, we might want to try to write into an optimized static
12839 final, this is an of a different nature, reported further on. */
12840 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12841 && resolve_expression_name (wfl_op1, &llvalue))
12843 lhs_type = TREE_TYPE (lvalue);
12845 else
12847 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12848 error_found = 1;
12851 rhs_type = TREE_TYPE (rhs);
12853 /* 5.1 Try the assignment conversion for builtin type. */
12854 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12856 /* 5.2 If it failed, try a reference conversion */
12857 if (!new_rhs)
12858 new_rhs = try_reference_assignconv (lhs_type, rhs);
12860 /* 15.25.2 If we have a compound assignment, convert RHS into the
12861 type of the LHS */
12862 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12863 new_rhs = convert (lhs_type, rhs);
12865 /* Explicit cast required. This is an error */
12866 if (!new_rhs)
12868 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12869 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12870 tree wfl;
12871 char operation [32]; /* Max size known */
12873 /* If the assignment is part of a declaration, we use the WFL of
12874 the declared variable to point out the error and call it a
12875 declaration problem. If the assignment is a genuine =
12876 operator, we call is a operator `=' problem, otherwise we
12877 call it an assignment problem. In both of these last cases,
12878 we use the WFL of the operator to indicate the error. */
12880 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12882 wfl = wfl_op1;
12883 strcpy (operation, "declaration");
12885 else
12887 wfl = wfl_operator;
12888 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12889 strcpy (operation, "assignment");
12890 else if (is_return)
12891 strcpy (operation, "'return'");
12892 else
12893 strcpy (operation, "'='");
12896 if (!valid_cast_to_p (rhs_type, lhs_type))
12897 parse_error_context
12898 (wfl, "Incompatible type for %s. Can't convert %qs to %qs",
12899 operation, t1, t2);
12900 else
12901 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert %qs to %qs",
12902 operation, t1, t2);
12903 free (t1); free (t2);
12904 error_found = 1;
12907 if (error_found)
12908 return error_mark_node;
12910 /* If we're processing a `return' statement, promote the actual type
12911 to the promoted type. */
12912 if (is_return)
12913 new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12915 /* 10.10: Array Store Exception runtime check */
12916 if (!flag_emit_class_files
12917 && lvalue_from_array
12918 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12920 tree array, store_check, base, index_expr;
12922 /* Save RHS so that it doesn't get re-evaluated by the store check. */
12923 new_rhs = save_expr (new_rhs);
12925 /* Get the INDIRECT_REF. */
12926 array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12927 /* Get the array pointer expr. */
12928 array = TREE_OPERAND (array, 0);
12929 store_check = build_java_arraystore_check (array, new_rhs);
12931 index_expr = TREE_OPERAND (lvalue, 1);
12933 if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12935 /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12936 happen before the store check, so prepare to insert the store
12937 check within the second operand of the existing COMPOUND_EXPR. */
12938 base = index_expr;
12940 else
12941 base = lvalue;
12943 index_expr = TREE_OPERAND (base, 1);
12944 TREE_OPERAND (base, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (index_expr),
12945 store_check, index_expr);
12948 /* Final locals can be used as case values in switch
12949 statement. Prepare them for this eventuality. */
12950 if (TREE_CODE (lvalue) == VAR_DECL
12951 && DECL_FINAL (lvalue)
12952 && TREE_CONSTANT (new_rhs)
12953 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12954 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12957 TREE_CONSTANT (lvalue) = 1;
12958 TREE_INVARIANT (lvalue) = 1;
12959 DECL_INITIAL (lvalue) = new_rhs;
12962 /* Copy the rhs if it's a reference. */
12963 if (! flag_check_references && ! flag_emit_class_files && optimize > 0)
12965 switch (TREE_CODE (new_rhs))
12967 case ARRAY_REF:
12968 case INDIRECT_REF:
12969 case COMPONENT_REF:
12970 /* Transform a = foo.bar
12971 into a = ({int tmp; tmp = foo.bar;}).
12972 We need to ensure that if a read from memory fails
12973 because of a NullPointerException, a destination variable
12974 will remain unchanged. An explicit temporary does what
12975 we need.
12977 If flag_check_references is set, this is unnecessary
12978 because we'll check each reference before doing any
12979 reads. If optimize is not set the result will never be
12980 written to a stack slot that contains the LHS. */
12982 tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"),
12983 TREE_TYPE (new_rhs));
12984 tree block = make_node (BLOCK);
12985 tree assignment
12986 = build2 (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
12987 DECL_CONTEXT (tmp) = current_function_decl;
12988 TREE_TYPE (block) = TREE_TYPE (new_rhs);
12989 BLOCK_VARS (block) = tmp;
12990 BLOCK_EXPR_BODY (block) = assignment;
12991 TREE_SIDE_EFFECTS (block) = 1;
12992 new_rhs = block;
12994 break;
12995 default:
12996 break;
13000 TREE_OPERAND (node, 0) = lvalue;
13001 TREE_OPERAND (node, 1) = new_rhs;
13002 TREE_TYPE (node) = lhs_type;
13003 return node;
13006 /* Check that type SOURCE can be cast into type DEST. If the cast
13007 can't occur at all, return NULL; otherwise, return a possibly
13008 modified rhs. */
13010 static tree
13011 try_reference_assignconv (tree lhs_type, tree rhs)
13013 tree new_rhs = NULL_TREE;
13014 tree rhs_type = TREE_TYPE (rhs);
13016 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
13018 /* `null' may be assigned to any reference type */
13019 if (rhs == null_pointer_node)
13020 new_rhs = null_pointer_node;
13021 /* Try the reference assignment conversion */
13022 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
13023 new_rhs = rhs;
13024 /* This is a magic assignment that we process differently */
13025 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
13026 new_rhs = rhs;
13028 return new_rhs;
13031 /* Check that RHS can be converted into LHS_TYPE by the assignment
13032 conversion (5.2), for the cases of RHS being a builtin type. Return
13033 NULL_TREE if the conversion fails or if because RHS isn't of a
13034 builtin type. Return a converted RHS if the conversion is possible. */
13036 static tree
13037 try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
13039 tree new_rhs = NULL_TREE;
13040 tree rhs_type = TREE_TYPE (rhs);
13042 /* Handle boolean specially. */
13043 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13044 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13046 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13047 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13048 new_rhs = rhs;
13051 /* 5.1.1 Try Identity Conversion,
13052 5.1.2 Try Widening Primitive Conversion */
13053 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
13054 new_rhs = convert (lhs_type, rhs);
13056 /* Try a narrowing primitive conversion (5.1.3):
13057 - expression is a constant expression of type byte, short, char,
13058 or int, AND
13059 - variable is byte, short or char AND
13060 - The value of the expression is representable in the type of the
13061 variable */
13062 else if ((rhs_type == byte_type_node || rhs_type == short_type_node
13063 || rhs_type == char_type_node || rhs_type == int_type_node)
13064 && TREE_CONSTANT (rhs)
13065 && (lhs_type == byte_type_node || lhs_type == char_type_node
13066 || lhs_type == short_type_node))
13068 if (int_fits_type_p (rhs, lhs_type))
13069 new_rhs = convert (lhs_type, rhs);
13070 else if (wfl_op1) /* Might be called with a NULL */
13071 parse_warning_context
13072 (wfl_op1,
13073 "Constant expression %qs too wide for narrowing primitive conversion to %qs",
13074 print_int_node (rhs), lang_printable_name (lhs_type, 0));
13075 /* Reported a warning that will turn into an error further
13076 down, so we don't return */
13079 return new_rhs;
13082 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
13083 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
13084 0 is the conversion test fails. This implements parts the method
13085 invocation conversion (5.3). */
13087 static int
13088 valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
13090 /* 5.1.1: This is the identity conversion part. */
13091 if (lhs_type == rhs_type)
13092 return 1;
13094 /* Reject non primitive types and boolean conversions. */
13095 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
13096 return 0;
13098 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
13099 than a char can't be converted into a char. Short can't too, but
13100 the < test below takes care of that */
13101 if (lhs_type == char_type_node && rhs_type == byte_type_node)
13102 return 0;
13104 /* Accept all promoted type here. Note, we can't use <= in the test
13105 below, because we still need to bounce out assignments of short
13106 to char and the likes */
13107 if (lhs_type == int_type_node
13108 && (rhs_type == promoted_byte_type_node
13109 || rhs_type == promoted_short_type_node
13110 || rhs_type == promoted_char_type_node
13111 || rhs_type == promoted_boolean_type_node))
13112 return 1;
13114 /* From here, an integral is widened if its precision is smaller
13115 than the precision of the LHS or if the LHS is a floating point
13116 type, or the RHS is a float and the RHS a double. */
13117 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13118 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13119 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13120 || (rhs_type == float_type_node && lhs_type == double_type_node))
13121 return 1;
13123 return 0;
13126 /* Check that something of SOURCE type can be assigned or cast to
13127 something of DEST type at runtime. Return 1 if the operation is
13128 valid, 0 otherwise. If CAST is set to 1, we're treating the case
13129 were SOURCE is cast into DEST, which borrows a lot of the
13130 assignment check. */
13132 static int
13133 valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
13135 /* SOURCE or DEST might be null if not from a declared entity. */
13136 if (!source || !dest)
13137 return 0;
13138 if (JNULLP_TYPE_P (source))
13139 return 1;
13140 if (TREE_CODE (source) == POINTER_TYPE)
13141 source = TREE_TYPE (source);
13142 if (TREE_CODE (dest) == POINTER_TYPE)
13143 dest = TREE_TYPE (dest);
13145 /* If source and dest are being compiled from bytecode, they may need to
13146 be loaded. */
13147 if (CLASS_P (source) && !CLASS_LOADED_P (source))
13149 load_class (source, 1);
13150 safe_layout_class (source);
13152 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13154 load_class (dest, 1);
13155 safe_layout_class (dest);
13158 /* Case where SOURCE is a class type */
13159 if (TYPE_CLASS_P (source))
13161 if (TYPE_CLASS_P (dest))
13162 return (source == dest
13163 || inherits_from_p (source, dest)
13164 || (cast && inherits_from_p (dest, source)));
13165 if (TYPE_INTERFACE_P (dest))
13167 /* If doing a cast and SOURCE is final, the operation is
13168 always correct a compile time (because even if SOURCE
13169 does not implement DEST, a subclass of SOURCE might). */
13170 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13171 return 1;
13172 /* Otherwise, SOURCE must implement DEST */
13173 return interface_of_p (dest, source);
13175 /* DEST is an array, cast permitted if SOURCE is of Object type */
13176 return (cast && source == object_type_node ? 1 : 0);
13178 if (TYPE_INTERFACE_P (source))
13180 if (TYPE_CLASS_P (dest))
13182 /* If not casting, DEST must be the Object type */
13183 if (!cast)
13184 return dest == object_type_node;
13185 /* We're doing a cast. The cast is always valid is class
13186 DEST is not final, otherwise, DEST must implement SOURCE */
13187 else if (!CLASS_FINAL (TYPE_NAME (dest)))
13188 return 1;
13189 else
13190 return interface_of_p (source, dest);
13192 if (TYPE_INTERFACE_P (dest))
13194 /* If doing a cast, then if SOURCE and DEST contain method
13195 with the same signature but different return type, then
13196 this is a (compile time) error */
13197 if (cast)
13199 tree method_source, method_dest;
13200 tree source_type;
13201 tree source_sig;
13202 tree source_name;
13203 for (method_source = TYPE_METHODS (source); method_source;
13204 method_source = TREE_CHAIN (method_source))
13206 source_sig =
13207 build_java_argument_signature (TREE_TYPE (method_source));
13208 source_type = TREE_TYPE (TREE_TYPE (method_source));
13209 source_name = DECL_NAME (method_source);
13210 for (method_dest = TYPE_METHODS (dest);
13211 method_dest; method_dest = TREE_CHAIN (method_dest))
13212 if (source_sig ==
13213 build_java_argument_signature (TREE_TYPE (method_dest))
13214 && source_name == DECL_NAME (method_dest)
13215 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13216 return 0;
13218 return 1;
13220 else
13221 return source == dest || interface_of_p (dest, source);
13223 else
13225 /* Array */
13226 return (cast
13227 && (DECL_NAME (TYPE_NAME (source))
13228 == java_lang_cloneable_identifier_node
13229 || (DECL_NAME (TYPE_NAME (source))
13230 == java_io_serializable_identifier_node)));
13233 if (TYPE_ARRAY_P (source))
13235 if (TYPE_CLASS_P (dest))
13236 return dest == object_type_node;
13237 /* Can't cast an array to an interface unless the interface is
13238 java.lang.Cloneable or java.io.Serializable. */
13239 if (TYPE_INTERFACE_P (dest))
13240 return (DECL_NAME (TYPE_NAME (dest))
13241 == java_lang_cloneable_identifier_node
13242 || (DECL_NAME (TYPE_NAME (dest))
13243 == java_io_serializable_identifier_node));
13244 else /* Arrays */
13246 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13247 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13249 /* In case of severe errors, they turn out null */
13250 if (!dest_element_type || !source_element_type)
13251 return 0;
13252 if (source_element_type == dest_element_type)
13253 return 1;
13254 return valid_ref_assignconv_cast_p (source_element_type,
13255 dest_element_type, cast);
13257 return 0;
13259 return 0;
13262 static int
13263 valid_cast_to_p (tree source, tree dest)
13265 if (TREE_CODE (source) == POINTER_TYPE)
13266 source = TREE_TYPE (source);
13267 if (TREE_CODE (dest) == POINTER_TYPE)
13268 dest = TREE_TYPE (dest);
13270 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13271 return valid_ref_assignconv_cast_p (source, dest, 1);
13273 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13274 return 1;
13276 else if (TREE_CODE (source) == BOOLEAN_TYPE
13277 && TREE_CODE (dest) == BOOLEAN_TYPE)
13278 return 1;
13280 return 0;
13283 static tree
13284 do_unary_numeric_promotion (tree arg)
13286 tree type = TREE_TYPE (arg);
13287 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13288 || TREE_CODE (type) == CHAR_TYPE)
13289 arg = convert (int_type_node, arg);
13290 return arg;
13293 /* Return a nonzero value if SOURCE can be converted into DEST using
13294 the method invocation conversion rule (5.3). */
13295 static int
13296 valid_method_invocation_conversion_p (tree dest, tree source)
13298 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13299 && valid_builtin_assignconv_identity_widening_p (dest, source))
13300 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13301 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13302 && valid_ref_assignconv_cast_p (source, dest, 0)));
13305 /* Build an incomplete binop expression. */
13307 static tree
13308 build_binop (enum tree_code op, int op_location, tree op1, tree op2)
13310 tree binop = build2 (op, NULL_TREE, op1, op2);
13311 TREE_SIDE_EFFECTS (binop) = 1;
13312 /* Store the location of the operator, for better error report. The
13313 string of the operator will be rebuild based on the OP value. */
13314 EXPR_WFL_LINECOL (binop) = op_location;
13315 return binop;
13318 /* Build the string of the operator retained by NODE. If NODE is part
13319 of a compound expression, add an '=' at the end of the string. This
13320 function is called when an error needs to be reported on an
13321 operator. The string is returned as a pointer to a static character
13322 buffer. */
13324 static char *
13325 operator_string (tree node)
13327 #define BUILD_OPERATOR_STRING(S) \
13329 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13330 return buffer; \
13333 static char buffer [10];
13334 switch (TREE_CODE (node))
13336 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13337 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13338 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13339 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13340 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13341 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13342 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13343 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13344 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13345 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13346 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13347 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13348 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13349 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13350 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13351 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13352 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13353 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13354 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13355 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13356 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13357 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13358 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13359 case PREINCREMENT_EXPR: /* Fall through */
13360 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13361 case PREDECREMENT_EXPR: /* Fall through */
13362 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13363 default:
13364 internal_error ("unregistered operator %s",
13365 tree_code_name [TREE_CODE (node)]);
13367 return NULL;
13368 #undef BUILD_OPERATOR_STRING
13371 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13373 static int
13374 java_decl_equiv (tree var_acc1, tree var_acc2)
13376 if (JDECL_P (var_acc1))
13377 return (var_acc1 == var_acc2);
13379 return (TREE_CODE (var_acc1) == COMPONENT_REF
13380 && TREE_CODE (var_acc2) == COMPONENT_REF
13381 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13382 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13383 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13386 /* Return a nonzero value if CODE is one of the operators that can be
13387 used in conjunction with the `=' operator in a compound assignment. */
13389 static int
13390 binop_compound_p (enum tree_code code)
13392 int i;
13393 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13394 if (binop_lookup [i] == code)
13395 break;
13397 return i < BINOP_COMPOUND_CANDIDATES;
13400 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13402 static tree
13403 java_refold (tree t)
13405 tree c, b, ns, decl;
13407 if (TREE_CODE (t) != MODIFY_EXPR)
13408 return t;
13410 c = TREE_OPERAND (t, 1);
13411 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13412 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13413 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13414 return t;
13416 /* Now the left branch of the binary operator. */
13417 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13418 if (! (b && TREE_CODE (b) == NOP_EXPR
13419 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13420 return t;
13422 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13423 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13424 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13425 return t;
13427 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13428 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13429 /* It's got to be the an equivalent decl */
13430 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13432 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13433 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13434 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13435 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13436 /* Change the right part of the BINOP_EXPR */
13437 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13440 return t;
13443 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13444 errors but we modify NODE so that it contains the type computed
13445 according to the expression, when it's fixed. Otherwise, we write
13446 error_mark_node as the type. It allows us to further the analysis
13447 of remaining nodes and detects more errors in certain cases. */
13449 static tree
13450 patch_binop (tree node, tree wfl_op1, tree wfl_op2, int folding)
13452 tree op1 = TREE_OPERAND (node, 0);
13453 tree op2 = TREE_OPERAND (node, 1);
13454 tree op1_type = TREE_TYPE (op1);
13455 tree op2_type = TREE_TYPE (op2);
13456 tree prom_type = NULL_TREE, cn;
13457 enum tree_code code = TREE_CODE (node);
13459 /* If 1, tell the routine that we have to return error_mark_node
13460 after checking for the initialization of the RHS */
13461 int error_found = 0;
13463 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13465 /* If either op<n>_type are NULL, this might be early signs of an
13466 error situation, unless it's too early to tell (in case we're
13467 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13468 correctly so the error can be later on reported accurately. */
13469 if (! (code == PLUS_EXPR || code == NE_EXPR
13470 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13472 tree n;
13473 if (! op1_type)
13475 n = java_complete_tree (op1);
13476 op1_type = TREE_TYPE (n);
13478 if (! op2_type)
13480 n = java_complete_tree (op2);
13481 op2_type = TREE_TYPE (n);
13485 switch (code)
13487 /* 15.16 Multiplicative operators */
13488 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13489 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13490 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13491 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13492 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13494 if (!JNUMERIC_TYPE_P (op1_type))
13495 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13496 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13497 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13498 TREE_TYPE (node) = error_mark_node;
13499 error_found = 1;
13500 break;
13502 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13504 /* Detect integral division by zero */
13505 if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13506 && TREE_CODE (prom_type) == INTEGER_TYPE
13507 && (op2 == integer_zero_node || op2 == long_zero_node ||
13508 (TREE_CODE (op2) == INTEGER_CST &&
13509 ! TREE_INT_CST_LOW (op2) && ! TREE_INT_CST_HIGH (op2))))
13511 parse_warning_context
13512 (wfl_operator,
13513 "Evaluating this expression will result in an arithmetic exception being thrown");
13514 TREE_CONSTANT (node) = 0;
13515 TREE_INVARIANT (node) = 0;
13518 /* Change the division operator if necessary */
13519 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13520 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13522 /* Before divisions as is disappear, try to simplify and bail if
13523 applicable, otherwise we won't perform even simple
13524 simplifications like (1-1)/3. We can't do that with floating
13525 point number, folds can't handle them at this stage. */
13526 if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13527 && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13529 TREE_TYPE (node) = prom_type;
13530 node = fold (node);
13531 if (TREE_CODE (node) != code)
13532 return node;
13535 if (TREE_CODE (prom_type) == INTEGER_TYPE
13536 && flag_use_divide_subroutine
13537 && ! flag_emit_class_files
13538 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13539 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13541 /* This one is more complicated. FLOATs are processed by a
13542 function call to soft_fmod. Duplicate the value of the
13543 COMPOUND_ASSIGN_P flag. */
13544 if (code == TRUNC_MOD_EXPR)
13546 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13547 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13548 return mod;
13550 break;
13552 /* 15.17 Additive Operators */
13553 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13555 /* Operation is valid if either one argument is a string
13556 constant, a String object or a StringBuffer crafted for the
13557 purpose of the a previous usage of the String concatenation
13558 operator */
13560 if (TREE_CODE (op1) == STRING_CST
13561 || TREE_CODE (op2) == STRING_CST
13562 || JSTRING_TYPE_P (op1_type)
13563 || JSTRING_TYPE_P (op2_type)
13564 || IS_CRAFTED_STRING_BUFFER_P (op1)
13565 || IS_CRAFTED_STRING_BUFFER_P (op2))
13566 return build_string_concatenation (op1, op2);
13568 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13569 Numeric Types */
13570 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13572 if (!JNUMERIC_TYPE_P (op1_type))
13573 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13574 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13575 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13576 TREE_TYPE (node) = error_mark_node;
13577 error_found = 1;
13578 break;
13580 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13581 break;
13583 /* 15.18 Shift Operators */
13584 case LSHIFT_EXPR:
13585 case RSHIFT_EXPR:
13586 case URSHIFT_EXPR:
13587 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13589 if (!JINTEGRAL_TYPE_P (op1_type))
13590 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13591 else
13593 if (JNUMERIC_TYPE_P (op2_type))
13594 parse_error_context (wfl_operator,
13595 "Incompatible type for %qs. Explicit cast needed to convert shift distance from %qs to integral",
13596 operator_string (node),
13597 lang_printable_name (op2_type, 0));
13598 else
13599 parse_error_context (wfl_operator,
13600 "Incompatible type for %qs. Can't convert shift distance from %qs to integral",
13601 operator_string (node),
13602 lang_printable_name (op2_type, 0));
13604 TREE_TYPE (node) = error_mark_node;
13605 error_found = 1;
13606 break;
13609 /* Unary numeric promotion (5.6.1) is performed on each operand
13610 separately */
13611 op1 = do_unary_numeric_promotion (op1);
13612 op2 = do_unary_numeric_promotion (op2);
13614 /* If the right hand side is of type `long', first cast it to
13615 `int'. */
13616 if (TREE_TYPE (op2) == long_type_node)
13617 op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13619 /* The type of the shift expression is the type of the promoted
13620 type of the left-hand operand */
13621 prom_type = TREE_TYPE (op1);
13623 /* Shift int only up to 0x1f and long up to 0x3f */
13624 if (prom_type == int_type_node)
13625 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13626 build_int_cst (NULL_TREE, 0x1f)));
13627 else
13628 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13629 build_int_cst (NULL_TREE, 0x3f)));
13631 /* The >>> operator is a >> operating on unsigned quantities */
13632 if (code == URSHIFT_EXPR && (folding || ! flag_emit_class_files))
13634 tree to_return;
13635 tree utype = java_unsigned_type (prom_type);
13636 op1 = convert (utype, op1);
13638 to_return = fold_build2 (RSHIFT_EXPR, utype, op1, op2);
13639 to_return = convert (prom_type, to_return);
13640 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13641 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13642 TREE_SIDE_EFFECTS (to_return)
13643 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13644 return to_return;
13646 break;
13648 /* 15.19.1 Type Comparison Operator instanceof */
13649 case INSTANCEOF_EXPR:
13651 TREE_TYPE (node) = boolean_type_node;
13653 /* OP1_TYPE might be NULL when OP1 is a string constant. */
13654 if ((cn = patch_string (op1)))
13656 op1 = cn;
13657 op1_type = TREE_TYPE (op1);
13659 if (op1_type == NULL_TREE)
13660 abort ();
13662 if (!(op2_type = resolve_type_during_patch (op2)))
13663 return error_mark_node;
13665 /* The first operand must be a reference type or the null type */
13666 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13667 error_found = 1; /* Error reported further below */
13669 /* The second operand must be a reference type */
13670 if (!JREFERENCE_TYPE_P (op2_type))
13672 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13673 parse_error_context
13674 (wfl_operator, "Invalid argument %qs for %<instanceof%>",
13675 lang_printable_name (op2_type, 0));
13676 error_found = 1;
13679 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13681 /* If the first operand is null, the result is always false */
13682 if (op1 == null_pointer_node)
13683 return boolean_false_node;
13684 else if (flag_emit_class_files)
13686 TREE_OPERAND (node, 1) = op2_type;
13687 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13688 return node;
13690 /* Otherwise we have to invoke instance of to figure it out */
13691 else
13692 return build_instanceof (op1, op2_type);
13694 /* There is no way the expression operand can be an instance of
13695 the type operand. This is a compile time error. */
13696 else
13698 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13699 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13700 parse_error_context
13701 (wfl_operator, "Impossible for %qs to be instance of %qs",
13702 t1, lang_printable_name (op2_type, 0));
13703 free (t1);
13704 error_found = 1;
13707 break;
13709 /* 15.21 Bitwise and Logical Operators */
13710 case BIT_AND_EXPR:
13711 case BIT_XOR_EXPR:
13712 case BIT_IOR_EXPR:
13713 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13714 /* Binary numeric promotion is performed on both operand and the
13715 expression retain that type */
13716 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13718 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13719 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13720 /* The type of the bitwise operator expression is BOOLEAN */
13721 prom_type = boolean_type_node;
13722 else
13724 if (!JINTEGRAL_TYPE_P (op1_type))
13725 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13726 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13727 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13728 TREE_TYPE (node) = error_mark_node;
13729 error_found = 1;
13730 /* Insert a break here if adding thing before the switch's
13731 break for this case */
13733 break;
13735 /* 15.22 Conditional-And Operator */
13736 case TRUTH_ANDIF_EXPR:
13737 /* 15.23 Conditional-Or Operator */
13738 case TRUTH_ORIF_EXPR:
13739 /* Operands must be of BOOLEAN type */
13740 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13741 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13743 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13744 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13745 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13746 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13747 TREE_TYPE (node) = boolean_type_node;
13748 error_found = 1;
13749 break;
13751 else if (integer_zerop (op1))
13753 return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13755 else if (integer_onep (op1))
13757 return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13759 /* The type of the conditional operators is BOOLEAN */
13760 prom_type = boolean_type_node;
13761 break;
13763 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13764 case LT_EXPR:
13765 case GT_EXPR:
13766 case LE_EXPR:
13767 case GE_EXPR:
13768 /* The type of each of the operands must be a primitive numeric
13769 type */
13770 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13772 if (!JNUMERIC_TYPE_P (op1_type))
13773 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13774 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13775 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13776 TREE_TYPE (node) = boolean_type_node;
13777 error_found = 1;
13778 break;
13780 /* Binary numeric promotion is performed on the operands */
13781 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13782 /* The type of the relation expression is always BOOLEAN */
13783 prom_type = boolean_type_node;
13784 break;
13786 /* 15.20 Equality Operator */
13787 case EQ_EXPR:
13788 case NE_EXPR:
13789 /* It's time for us to patch the strings. */
13790 if ((cn = patch_string (op1)))
13792 op1 = cn;
13793 op1_type = TREE_TYPE (op1);
13795 if ((cn = patch_string (op2)))
13797 op2 = cn;
13798 op2_type = TREE_TYPE (op2);
13801 /* 15.20.1 Numerical Equality Operators == and != */
13802 /* Binary numeric promotion is performed on the operands */
13803 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13804 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13806 /* 15.20.2 Boolean Equality Operators == and != */
13807 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13808 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13809 ; /* Nothing to do here */
13811 /* 15.20.3 Reference Equality Operators == and != */
13812 /* Types have to be either references or the null type. If
13813 they're references, it must be possible to convert either
13814 type to the other by casting conversion. */
13815 else if ((op1 == null_pointer_node && op2 == null_pointer_node)
13816 || (op1 == null_pointer_node && JREFERENCE_TYPE_P (op2_type))
13817 || (JREFERENCE_TYPE_P (op1_type) && op2 == null_pointer_node)
13818 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13819 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13820 || valid_ref_assignconv_cast_p (op2_type,
13821 op1_type, 1))))
13822 ; /* Nothing to do here */
13824 /* Else we have an error figure what can't be converted into
13825 what and report the error */
13826 else
13828 char *t1;
13829 t1 = xstrdup (lang_printable_name (op1_type, 0));
13830 parse_error_context
13831 (wfl_operator,
13832 "Incompatible type for %qs. Can't convert %qs to %qs",
13833 operator_string (node), t1,
13834 lang_printable_name (op2_type, 0));
13835 free (t1);
13836 TREE_TYPE (node) = boolean_type_node;
13837 error_found = 1;
13838 break;
13840 prom_type = boolean_type_node;
13841 break;
13842 default:
13843 abort ();
13846 if (error_found)
13847 return error_mark_node;
13849 TREE_OPERAND (node, 0) = op1;
13850 TREE_OPERAND (node, 1) = op2;
13851 TREE_TYPE (node) = prom_type;
13852 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13854 /* fold does not respect side-effect order as required for Java but not C.
13855 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13856 * bytecode.
13858 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13859 : ! TREE_SIDE_EFFECTS (node))
13860 node = fold (node);
13861 return node;
13864 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13865 zero value, the value of CSTE comes after the valude of STRING */
13867 static tree
13868 do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13870 const char *old = TREE_STRING_POINTER (cste);
13871 int old_len = TREE_STRING_LENGTH (cste);
13872 int len = old_len + string_len;
13873 char *new = alloca (len+1);
13875 if (after)
13877 memcpy (new, string, string_len);
13878 memcpy (&new [string_len], old, old_len);
13880 else
13882 memcpy (new, old, old_len);
13883 memcpy (&new [old_len], string, string_len);
13885 new [len] = '\0';
13886 return build_string (len, new);
13889 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13890 new STRING_CST on success, NULL_TREE on failure. */
13892 static tree
13893 merge_string_cste (tree op1, tree op2, int after)
13895 /* Handle two string constants right away. */
13896 if (TREE_CODE (op2) == STRING_CST)
13897 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13898 TREE_STRING_LENGTH (op2), after);
13900 /* Reasonable integer constant can be treated right away. */
13901 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13903 static const char *const boolean_true = "true";
13904 static const char *const boolean_false = "false";
13905 static const char *const null_pointer = "null";
13906 char ch[4];
13907 const char *string;
13909 if (op2 == boolean_true_node)
13910 string = boolean_true;
13911 else if (op2 == boolean_false_node)
13912 string = boolean_false;
13913 else if (op2 == null_pointer_node
13914 || (integer_zerop (op2)
13915 && TREE_CODE (TREE_TYPE (op2)) == POINTER_TYPE))
13916 /* FIXME: null is not a compile-time constant, so it is only safe to
13917 merge if the overall expression is non-constant. However, this
13918 code always merges without checking the overall expression. */
13919 string = null_pointer;
13920 else if (TREE_TYPE (op2) == char_type_node)
13922 /* Convert the character into UTF-8. */
13923 unsigned int c = (unsigned int) TREE_INT_CST_LOW (op2);
13924 unsigned char *p = (unsigned char *) ch;
13925 if (0x01 <= c && c <= 0x7f)
13926 *p++ = (unsigned char) c;
13927 else if (c < 0x7ff)
13929 *p++ = (unsigned char) (c >> 6 | 0xc0);
13930 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13932 else
13934 *p++ = (unsigned char) (c >> 12 | 0xe0);
13935 *p++ = (unsigned char) (((c >> 6) & 0x3f) | 0x80);
13936 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13938 *p = '\0';
13940 string = ch;
13942 else
13943 string = string_convert_int_cst (op2);
13945 return do_merge_string_cste (op1, string, strlen (string), after);
13947 return NULL_TREE;
13950 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13951 has to be a STRING_CST and the other part must be a STRING_CST or a
13952 INTEGRAL constant. Return a new STRING_CST if the operation
13953 succeed, NULL_TREE otherwise.
13955 If the case we want to optimize for space, we might want to return
13956 NULL_TREE for each invocation of this routine. FIXME */
13958 static tree
13959 string_constant_concatenation (tree op1, tree op2)
13961 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13963 tree string, rest;
13964 int invert;
13966 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13967 rest = (string == op1 ? op2 : op1);
13968 invert = (string == op1 ? 0 : 1 );
13970 /* Walk REST, only if it looks reasonable */
13971 if (TREE_CODE (rest) != STRING_CST
13972 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13973 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13974 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13976 rest = java_complete_tree (rest);
13977 if (rest == error_mark_node)
13978 return error_mark_node;
13979 rest = fold (rest);
13981 return merge_string_cste (string, rest, invert);
13983 return NULL_TREE;
13986 /* Implement the `+' operator. Does static optimization if possible,
13987 otherwise create (if necessary) and append elements to a
13988 StringBuffer. The StringBuffer will be carried around until it is
13989 used for a function call or an assignment. Then toString() will be
13990 called on it to turn it into a String object. */
13992 static tree
13993 build_string_concatenation (tree op1, tree op2)
13995 tree result;
13996 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13998 /* Try to do some static optimization */
13999 if ((result = string_constant_concatenation (op1, op2)))
14000 return result;
14002 /* Discard empty strings on either side of the expression */
14003 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
14005 op1 = op2;
14006 op2 = NULL_TREE;
14008 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
14009 op2 = NULL_TREE;
14011 /* If operands are string constant, turn then into object references */
14012 if (TREE_CODE (op1) == STRING_CST)
14013 op1 = patch_string_cst (op1);
14014 if (op2 && TREE_CODE (op2) == STRING_CST)
14015 op2 = patch_string_cst (op2);
14017 /* If either one of the constant is null and the other non null
14018 operand is a String constant, return it. */
14019 if ((TREE_CODE (op1) == STRING_CST) && !op2)
14020 return op1;
14022 /* If OP1 isn't already a StringBuffer, create and
14023 initialize a new one */
14024 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
14026 /* Two solutions here:
14027 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
14028 2) OP1 is something else, we call new StringBuffer().append(OP1). */
14029 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
14030 op1 = BUILD_STRING_BUFFER (op1);
14031 else
14033 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
14034 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
14038 if (op2)
14040 /* OP1 is no longer the last node holding a crafted StringBuffer */
14041 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
14042 /* Create a node for `{new...,xxx}.append (op2)' */
14043 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
14046 /* Mark the last node holding a crafted StringBuffer */
14047 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
14049 TREE_SIDE_EFFECTS (op1) = side_effects;
14050 return op1;
14053 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
14054 StringBuffer. If no string were found to be patched, return
14055 NULL. */
14057 static tree
14058 patch_string (tree node)
14060 if (node == error_mark_node)
14061 return error_mark_node;
14062 if (TREE_CODE (node) == STRING_CST)
14063 return patch_string_cst (node);
14064 else if (IS_CRAFTED_STRING_BUFFER_P (node))
14066 int saved = ctxp->explicit_constructor_p;
14067 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
14068 tree ret;
14069 /* Temporary disable forbid the use of `this'. */
14070 ctxp->explicit_constructor_p = 0;
14071 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
14072 /* String concatenation arguments must be evaluated in order too. */
14073 ret = force_evaluation_order (ret);
14074 /* Restore it at its previous value */
14075 ctxp->explicit_constructor_p = saved;
14076 return ret;
14078 return NULL_TREE;
14081 /* Build the internal representation of a string constant. */
14083 static tree
14084 patch_string_cst (tree node)
14086 int location;
14087 if (! flag_emit_class_files)
14089 node = get_identifier (TREE_STRING_POINTER (node));
14090 location = alloc_name_constant (CONSTANT_String, node);
14091 node = build_ref_from_constant_pool (location);
14093 TREE_CONSTANT (node) = 1;
14094 TREE_INVARIANT (node) = 1;
14096 /* ??? Guessing that the class file code can't handle casts. */
14097 if (! flag_emit_class_files)
14098 node = convert (string_ptr_type_node, node);
14099 else
14100 TREE_TYPE (node) = string_ptr_type_node;
14102 return node;
14105 /* Build an incomplete unary operator expression. */
14107 static tree
14108 build_unaryop (int op_token, int op_location, tree op1)
14110 enum tree_code op;
14111 tree unaryop;
14112 switch (op_token)
14114 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14115 case MINUS_TK: op = NEGATE_EXPR; break;
14116 case NEG_TK: op = TRUTH_NOT_EXPR; break;
14117 case NOT_TK: op = BIT_NOT_EXPR; break;
14118 default: abort ();
14121 unaryop = build1 (op, NULL_TREE, op1);
14122 TREE_SIDE_EFFECTS (unaryop) = 1;
14123 /* Store the location of the operator, for better error report. The
14124 string of the operator will be rebuild based on the OP value. */
14125 EXPR_WFL_LINECOL (unaryop) = op_location;
14126 return unaryop;
14129 /* Special case for the ++/-- operators, since they require an extra
14130 argument to build, which is set to NULL and patched
14131 later. IS_POST_P is 1 if the operator, 0 otherwise. */
14133 static tree
14134 build_incdec (int op_token, int op_location, tree op1, int is_post_p)
14136 static const enum tree_code lookup [2][2] =
14138 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14139 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14141 tree node = build2 (lookup [is_post_p][(op_token - DECR_TK)],
14142 NULL_TREE, op1, NULL_TREE);
14143 TREE_SIDE_EFFECTS (node) = 1;
14144 /* Store the location of the operator, for better error report. The
14145 string of the operator will be rebuild based on the OP value. */
14146 EXPR_WFL_LINECOL (node) = op_location;
14147 return node;
14150 /* Build an incomplete cast operator, based on the use of the
14151 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14152 set. java_complete_tree is trained to walk a CONVERT_EXPR even
14153 though its type is already set. */
14155 static tree
14156 build_cast (int location, tree type, tree exp)
14158 tree node = build1 (CONVERT_EXPR, type, exp);
14159 EXPR_WFL_LINECOL (node) = location;
14160 return node;
14163 /* Build an incomplete class reference operator. */
14164 static tree
14165 build_incomplete_class_ref (int location, tree class_name)
14167 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14168 tree class_decl = GET_CPC ();
14169 tree this_class = TREE_TYPE (class_decl);
14171 /* Generate the synthetic static method `class$'. (Previously we
14172 deferred this, causing different method tables to be emitted
14173 for native code and bytecode.) */
14174 if (!TYPE_DOT_CLASS (this_class)
14175 && !JPRIMITIVE_TYPE_P (class_name)
14176 && !(TREE_CODE (class_name) == VOID_TYPE))
14178 tree cpc_list = GET_CPC_LIST();
14179 tree cpc = cpc_list;
14180 tree target_class;
14182 /* For inner classes, add a 'class$' method to their outermost
14183 context, creating it if necessary. */
14185 while (GET_NEXT_ENCLOSING_CPC(cpc))
14186 cpc = GET_NEXT_ENCLOSING_CPC(cpc);
14187 class_decl = TREE_VALUE (cpc);
14189 target_class = TREE_TYPE (class_decl);
14191 if (CLASS_INTERFACE (TYPE_NAME (target_class)))
14193 /* For interfaces, adding a static 'class$' method directly
14194 is illegal. So create an inner class to contain the new
14195 method. Empirically this matches the behavior of javac. */
14196 tree t, inner;
14197 /* We want the generated inner class inside the outermost class. */
14198 GET_CPC_LIST() = cpc;
14199 t = build_wfl_node (DECL_NAME (TYPE_NAME (object_type_node)));
14200 inner = create_anonymous_class (t);
14201 target_class = TREE_TYPE (inner);
14202 end_class_declaration (1);
14203 GET_CPC_LIST() = cpc_list;
14206 if (TYPE_DOT_CLASS (target_class) == NULL_TREE)
14207 build_dot_class_method (target_class);
14209 if (this_class != target_class)
14210 TYPE_DOT_CLASS (this_class) = TYPE_DOT_CLASS (target_class);
14213 EXPR_WFL_LINECOL (node) = location;
14214 return node;
14217 /* Complete an incomplete class reference operator. */
14218 static tree
14219 patch_incomplete_class_ref (tree node)
14221 tree type = TREE_OPERAND (node, 0);
14222 tree ref_type;
14224 if (!(ref_type = resolve_type_during_patch (type)))
14225 return error_mark_node;
14227 /* If we're not emitting class files and we know ref_type is a
14228 compiled class, build a direct reference. */
14229 if ((! flag_emit_class_files && is_compiled_class (ref_type))
14230 || JPRIMITIVE_TYPE_P (ref_type)
14231 || TREE_CODE (ref_type) == VOID_TYPE)
14233 tree dot = build_class_ref (ref_type);
14234 /* A class referenced by `foo.class' is initialized. */
14235 if (!flag_emit_class_files)
14236 dot = build_class_init (ref_type, dot);
14237 return java_complete_tree (dot);
14240 /* If we're emitting class files and we have to deal with non
14241 primitive types, we invoke the synthetic static method `class$'. */
14242 ref_type = build_dot_class_method_invocation (current_class, ref_type);
14243 return java_complete_tree (ref_type);
14246 /* 15.14 Unary operators. We return error_mark_node in case of error,
14247 but preserve the type of NODE if the type is fixed. */
14249 static tree
14250 patch_unaryop (tree node, tree wfl_op)
14252 tree op = TREE_OPERAND (node, 0);
14253 tree op_type = TREE_TYPE (op);
14254 tree prom_type = NULL_TREE, value, decl;
14255 int nested_field_flag = 0;
14256 int code = TREE_CODE (node);
14257 int error_found = 0;
14259 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14261 switch (code)
14263 /* 15.13.2 Postfix Increment Operator ++ */
14264 case POSTINCREMENT_EXPR:
14265 /* 15.13.3 Postfix Increment Operator -- */
14266 case POSTDECREMENT_EXPR:
14267 /* 15.14.1 Prefix Increment Operator ++ */
14268 case PREINCREMENT_EXPR:
14269 /* 15.14.2 Prefix Decrement Operator -- */
14270 case PREDECREMENT_EXPR:
14271 op = decl = extract_field_decl (op);
14272 nested_field_flag
14273 = nested_field_expanded_access_p (op, NULL, NULL, NULL);
14274 /* We might be trying to change an outer field accessed using
14275 access method. */
14276 if (nested_field_flag)
14278 /* Retrieve the decl of the field we're trying to access. We
14279 do that by first retrieving the function we would call to
14280 access the field. It has been already verified that this
14281 field isn't final */
14282 if (flag_emit_class_files)
14283 decl = TREE_OPERAND (op, 0);
14284 else
14285 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14286 decl = DECL_FUNCTION_ACCESS_DECL (decl);
14288 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14289 else if (!JDECL_P (decl)
14290 && TREE_CODE (decl) != COMPONENT_REF
14291 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14292 && TREE_CODE (decl) != INDIRECT_REF
14293 && !(TREE_CODE (decl) == COMPOUND_EXPR
14294 && TREE_OPERAND (decl, 1)
14295 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14297 TREE_TYPE (node) = error_mark_node;
14298 error_found = 1;
14301 /* From now on, we know that op if a variable and that it has a
14302 valid wfl. We use wfl_op to locate errors related to the
14303 ++/-- operand. */
14304 if (!JNUMERIC_TYPE_P (op_type))
14306 parse_error_context
14307 (wfl_op, "Invalid argument type %qs to %qs",
14308 lang_printable_name (op_type, 0), operator_string (node));
14309 TREE_TYPE (node) = error_mark_node;
14310 error_found = 1;
14312 else
14314 /* Before the addition, binary numeric promotion is performed on
14315 both operands, if really necessary */
14316 if (JINTEGRAL_TYPE_P (op_type))
14318 value = build_int_cst (op_type, 1);
14319 TREE_TYPE (node) = op_type;
14321 else
14323 value = build_int_cst (NULL_TREE, 1);
14324 TREE_TYPE (node) =
14325 binary_numeric_promotion (op_type,
14326 TREE_TYPE (value), &op, &value);
14329 /* We remember we might be accessing an outer field */
14330 if (nested_field_flag)
14332 /* We re-generate an access to the field */
14333 value = build2 (PLUS_EXPR, TREE_TYPE (op),
14334 build_nested_field_access (wfl_op, decl), value);
14336 /* And we patch the original access$() into a write
14337 with plus_op as a rhs */
14338 return nested_field_access_fix (node, op, value);
14341 /* And write back into the node. */
14342 TREE_OPERAND (node, 0) = op;
14343 TREE_OPERAND (node, 1) = value;
14344 /* Convert the overall back into its original type, if
14345 necessary, and return */
14346 if (JINTEGRAL_TYPE_P (op_type))
14347 return fold (node);
14348 else
14349 return fold (convert (op_type, node));
14351 break;
14353 /* 15.14.3 Unary Plus Operator + */
14354 case UNARY_PLUS_EXPR:
14355 /* 15.14.4 Unary Minus Operator - */
14356 case NEGATE_EXPR:
14357 if (!JNUMERIC_TYPE_P (op_type))
14359 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14360 TREE_TYPE (node) = error_mark_node;
14361 error_found = 1;
14363 /* Unary numeric promotion is performed on operand */
14364 else
14366 op = do_unary_numeric_promotion (op);
14367 prom_type = TREE_TYPE (op);
14368 if (code == UNARY_PLUS_EXPR)
14369 return fold (op);
14371 break;
14373 /* 15.14.5 Bitwise Complement Operator ~ */
14374 case BIT_NOT_EXPR:
14375 if (!JINTEGRAL_TYPE_P (op_type))
14377 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14378 TREE_TYPE (node) = error_mark_node;
14379 error_found = 1;
14381 else
14383 op = do_unary_numeric_promotion (op);
14384 prom_type = TREE_TYPE (op);
14386 break;
14388 /* 15.14.6 Logical Complement Operator ! */
14389 case TRUTH_NOT_EXPR:
14390 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14392 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14393 /* But the type is known. We will report an error if further
14394 attempt of a assignment is made with this rhs */
14395 TREE_TYPE (node) = boolean_type_node;
14396 error_found = 1;
14398 else
14399 prom_type = boolean_type_node;
14400 break;
14402 /* 15.15 Cast Expression */
14403 case CONVERT_EXPR:
14404 value = patch_cast (node, wfl_operator);
14405 if (value == error_mark_node)
14407 /* If this cast is part of an assignment, we tell the code
14408 that deals with it not to complain about a mismatch,
14409 because things have been cast, anyways */
14410 TREE_TYPE (node) = error_mark_node;
14411 error_found = 1;
14413 else
14415 value = fold (value);
14416 return value;
14418 break;
14420 case NOP_EXPR:
14421 /* This can only happen when the type is already known. */
14422 gcc_assert (TREE_TYPE (node) != NULL_TREE);
14423 prom_type = TREE_TYPE (node);
14424 break;
14427 if (error_found)
14428 return error_mark_node;
14430 /* There are cases where node has been replaced by something else
14431 and we don't end up returning here: UNARY_PLUS_EXPR,
14432 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14433 TREE_OPERAND (node, 0) = fold (op);
14434 TREE_TYPE (node) = prom_type;
14435 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14436 return fold (node);
14439 /* Generic type resolution that sometimes takes place during node
14440 patching. Returned the resolved type or generate an error
14441 message. Return the resolved type or NULL_TREE. */
14443 static tree
14444 resolve_type_during_patch (tree type)
14446 if (unresolved_type_p (type, NULL))
14448 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14449 if (!type_decl)
14451 parse_error_context (type,
14452 "Class %qs not found in type declaration",
14453 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14454 return NULL_TREE;
14457 check_deprecation (type, type_decl);
14459 return TREE_TYPE (type_decl);
14461 return type;
14464 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14465 found. Otherwise NODE or something meant to replace it is returned. */
14467 static tree
14468 patch_cast (tree node, tree wfl_op)
14470 tree op = TREE_OPERAND (node, 0);
14471 tree cast_type = TREE_TYPE (node);
14472 tree patched, op_type;
14473 char *t1;
14475 /* Some string patching might be necessary at this stage */
14476 if ((patched = patch_string (op)))
14477 TREE_OPERAND (node, 0) = op = patched;
14478 op_type = TREE_TYPE (op);
14480 /* First resolve OP_TYPE if unresolved */
14481 if (!(cast_type = resolve_type_during_patch (cast_type)))
14482 return error_mark_node;
14484 /* Check on cast that are proven correct at compile time */
14485 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14487 /* Same type */
14488 if (cast_type == op_type)
14489 return node;
14491 /* A narrowing conversion from a floating-point number to an
14492 integral type requires special handling (5.1.3). */
14493 if (JFLOAT_TYPE_P (op_type) && JINTEGRAL_TYPE_P (cast_type))
14494 if (cast_type != long_type_node)
14495 op = convert (integer_type_node, op);
14497 /* Try widening/narrowing conversion. Potentially, things need
14498 to be worked out in gcc so we implement the extreme cases
14499 correctly. fold_convert() needs to be fixed. */
14500 return convert (cast_type, op);
14503 /* It's also valid to cast a boolean into a boolean */
14504 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14505 return node;
14507 /* null can be casted to references */
14508 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14509 return build_null_of_type (cast_type);
14511 /* The remaining legal casts involve conversion between reference
14512 types. Check for their compile time correctness. */
14513 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14514 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14516 TREE_TYPE (node) = promote_type (cast_type);
14517 /* Now, the case can be determined correct at compile time if
14518 OP_TYPE can be converted into CAST_TYPE by assignment
14519 conversion (5.2) */
14521 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14523 TREE_SET_CODE (node, NOP_EXPR);
14524 return node;
14527 if (flag_emit_class_files)
14529 TREE_SET_CODE (node, CONVERT_EXPR);
14530 return node;
14533 /* The cast requires a run-time check */
14534 return build3 (CALL_EXPR, promote_type (cast_type),
14535 build_address_of (soft_checkcast_node),
14536 tree_cons (NULL_TREE, build_class_ref (cast_type),
14537 build_tree_list (NULL_TREE, op)),
14538 NULL_TREE);
14541 /* Any other casts are proven incorrect at compile time */
14542 t1 = xstrdup (lang_printable_name (op_type, 0));
14543 parse_error_context (wfl_op, "Invalid cast from %qs to %qs",
14544 t1, lang_printable_name (cast_type, 0));
14545 free (t1);
14546 return error_mark_node;
14549 /* Build a null constant and give it the type TYPE. */
14551 static tree
14552 build_null_of_type (tree type)
14554 tree node = build_int_cst (promote_type (type), 0);
14555 return node;
14558 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14559 a list of indices. */
14560 static tree
14561 build_array_ref (int location, tree array, tree index)
14563 tree node = build4 (ARRAY_REF, NULL_TREE, array, index,
14564 NULL_TREE, NULL_TREE);
14565 EXPR_WFL_LINECOL (node) = location;
14566 return node;
14569 /* 15.12 Array Access Expression */
14571 static tree
14572 patch_array_ref (tree node)
14574 tree array = TREE_OPERAND (node, 0);
14575 tree array_type = TREE_TYPE (array);
14576 tree index = TREE_OPERAND (node, 1);
14577 tree index_type = TREE_TYPE (index);
14578 int error_found = 0;
14580 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14582 if (TREE_CODE (array_type) == POINTER_TYPE)
14583 array_type = TREE_TYPE (array_type);
14585 /* The array reference must be an array */
14586 if (!TYPE_ARRAY_P (array_type))
14588 parse_error_context
14589 (wfl_operator,
14590 "%<[]%> can only be applied to arrays. It can't be applied to %qs",
14591 lang_printable_name (array_type, 0));
14592 TREE_TYPE (node) = error_mark_node;
14593 error_found = 1;
14596 /* The array index undergoes unary numeric promotion. The promoted
14597 type must be int */
14598 index = do_unary_numeric_promotion (index);
14599 if (TREE_TYPE (index) != int_type_node)
14601 if (valid_cast_to_p (index_type, int_type_node))
14602 parse_error_context (wfl_operator,
14603 "Incompatible type for %<[]%>. Explicit cast needed to convert %qs to %<int%>",
14604 lang_printable_name (index_type, 0));
14605 else
14606 parse_error_context (wfl_operator,
14607 "Incompatible type for %<[]%>. Can't convert %qs to %<int%>",
14608 lang_printable_name (index_type, 0));
14609 TREE_TYPE (node) = error_mark_node;
14610 error_found = 1;
14613 if (error_found)
14614 return error_mark_node;
14616 array_type = TYPE_ARRAY_ELEMENT (array_type);
14618 if (flag_emit_class_files)
14620 TREE_OPERAND (node, 0) = array;
14621 TREE_OPERAND (node, 1) = index;
14623 else
14624 node = build_java_arrayaccess (array, array_type, index);
14625 TREE_TYPE (node) = array_type;
14626 return node;
14629 /* 15.9 Array Creation Expressions */
14631 static tree
14632 build_newarray_node (tree type, tree dims, int extra_dims)
14634 tree node = build3 (NEW_ARRAY_EXPR, NULL_TREE, type,
14635 nreverse (dims),
14636 build_int_cst (NULL_TREE, extra_dims));
14637 return node;
14640 static tree
14641 patch_newarray (tree node)
14643 tree type = TREE_OPERAND (node, 0);
14644 tree dims = TREE_OPERAND (node, 1);
14645 tree cdim, array_type;
14646 int error_found = 0;
14647 int ndims = 0;
14648 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14650 /* Dimension types are verified. It's better for the types to be
14651 verified in order. */
14652 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14654 int dim_error = 0;
14655 tree dim = TREE_VALUE (cdim);
14657 /* Dim might have been saved during its evaluation */
14658 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14660 /* The type of each specified dimension must be an integral type. */
14661 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14662 dim_error = 1;
14664 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14665 promoted type must be int. */
14666 else
14668 dim = do_unary_numeric_promotion (dim);
14669 if (TREE_TYPE (dim) != int_type_node)
14670 dim_error = 1;
14673 /* Report errors on types here */
14674 if (dim_error)
14676 parse_error_context
14677 (TREE_PURPOSE (cdim),
14678 "Incompatible type for dimension in array creation expression. %s convert %qs to %<int%>",
14679 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14680 "Explicit cast needed to" : "Can't"),
14681 lang_printable_name (TREE_TYPE (dim), 0));
14682 error_found = 1;
14685 TREE_PURPOSE (cdim) = NULL_TREE;
14688 /* Resolve array base type if unresolved */
14689 if (!(type = resolve_type_during_patch (type)))
14690 error_found = 1;
14692 if (error_found)
14694 /* We don't want further evaluation of this bogus array creation
14695 operation */
14696 TREE_TYPE (node) = error_mark_node;
14697 return error_mark_node;
14700 /* Set array_type to the actual (promoted) array type of the result. */
14701 if (TREE_CODE (type) == RECORD_TYPE)
14702 type = build_pointer_type (type);
14703 while (--xdims >= 0)
14705 type = promote_type (build_java_array_type (type, -1));
14707 dims = nreverse (dims);
14708 array_type = type;
14709 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14711 type = array_type;
14712 array_type
14713 = build_java_array_type (type,
14714 TREE_CODE (cdim) == INTEGER_CST
14715 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14716 : -1);
14717 array_type = promote_type (array_type);
14719 dims = nreverse (dims);
14721 /* The node is transformed into a function call. Things are done
14722 differently according to the number of dimensions. If the number
14723 of dimension is equal to 1, then the nature of the base type
14724 (primitive or not) matters. */
14725 if (ndims == 1)
14726 return build_new_array (type, TREE_VALUE (dims));
14728 /* Can't reuse what's already written in expr.c because it uses the
14729 JVM stack representation. Provide a build_multianewarray. FIXME */
14730 return build3 (CALL_EXPR, array_type,
14731 build_address_of (soft_multianewarray_node),
14732 tree_cons (NULL_TREE,
14733 build_class_ref (TREE_TYPE (array_type)),
14734 tree_cons (NULL_TREE,
14735 build_int_cst (NULL_TREE, ndims),
14736 dims)),
14737 NULL_TREE);
14740 /* 10.6 Array initializer. */
14742 /* Build a wfl for array element that don't have one, so we can
14743 pin-point errors. */
14745 static tree
14746 maybe_build_array_element_wfl (tree node)
14748 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14750 /* FIXME - old code used "prev_lc.line" and "elc.prev_col */
14751 return build_expr_wfl (NULL_TREE,
14752 #ifdef USE_MAPPED_LOCATION
14753 input_location
14754 #else
14755 ctxp->filename,
14756 ctxp->lexer->token_start.line,
14757 ctxp->lexer->token_start.col
14758 #endif
14761 else
14762 return NULL_TREE;
14765 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14766 identification of initialized arrays easier to detect during walk
14767 and expansion. */
14769 static tree
14770 build_new_array_init (int location, tree values)
14772 tree constructor = build_constructor (NULL_TREE, values);
14773 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14774 EXPR_WFL_LINECOL (to_return) = location;
14775 return to_return;
14778 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14779 occurred. Otherwise return NODE after having set its type
14780 appropriately. */
14782 static tree
14783 patch_new_array_init (tree type, tree node)
14785 int error_seen = 0;
14786 tree current, element_type;
14787 HOST_WIDE_INT length;
14788 int all_constant = 1;
14789 tree init = TREE_OPERAND (node, 0);
14791 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14793 parse_error_context (node,
14794 "Invalid array initializer for non-array type %qs",
14795 lang_printable_name (type, 1));
14796 return error_mark_node;
14798 type = TREE_TYPE (type);
14799 element_type = TYPE_ARRAY_ELEMENT (type);
14801 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14803 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14804 current; length++, current = TREE_CHAIN (current))
14806 tree elt = TREE_VALUE (current);
14807 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14809 error_seen |= array_constructor_check_entry (element_type, current);
14810 elt = TREE_VALUE (current);
14811 /* When compiling to native code, STRING_CST is converted to
14812 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14813 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14814 all_constant = 0;
14816 else
14818 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14819 TREE_PURPOSE (current) = NULL_TREE;
14820 all_constant = 0;
14822 if (elt && TREE_CODE (elt) == TREE_LIST
14823 && TREE_VALUE (elt) == error_mark_node)
14824 error_seen = 1;
14827 if (error_seen)
14828 return error_mark_node;
14830 /* Create a new type. We can't reuse the one we have here by
14831 patching its dimension because it originally is of dimension -1
14832 hence reused by gcc. This would prevent triangular arrays. */
14833 type = build_java_array_type (element_type, length);
14834 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14835 TREE_TYPE (node) = promote_type (type);
14836 TREE_CONSTANT (init) = all_constant;
14837 TREE_INVARIANT (init) = all_constant;
14838 TREE_CONSTANT (node) = all_constant;
14839 TREE_INVARIANT (node) = all_constant;
14840 return node;
14843 /* Verify that one entry of the initializer element list can be
14844 assigned to the array base type. Report 1 if an error occurred, 0
14845 otherwise. */
14847 static int
14848 array_constructor_check_entry (tree type, tree entry)
14850 char *array_type_string = NULL; /* For error reports */
14851 tree value, type_value, new_value, wfl_value, patched;
14852 int error_seen = 0;
14854 new_value = NULL_TREE;
14855 wfl_value = TREE_VALUE (entry);
14857 value = java_complete_tree (TREE_VALUE (entry));
14858 /* patch_string return error_mark_node if arg is error_mark_node */
14859 if ((patched = patch_string (value)))
14860 value = patched;
14861 if (value == error_mark_node)
14862 return 1;
14864 type_value = TREE_TYPE (value);
14866 /* At anytime, try_builtin_assignconv can report a warning on
14867 constant overflow during narrowing. */
14868 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14869 new_value = try_builtin_assignconv (wfl_operator, type, value);
14870 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14871 type_value = promote_type (type);
14873 /* Check and report errors */
14874 if (!new_value)
14876 const char *const msg = (!valid_cast_to_p (type_value, type) ?
14877 "Can't" : "Explicit cast needed to");
14878 if (!array_type_string)
14879 array_type_string = xstrdup (lang_printable_name (type, 1));
14880 parse_error_context
14881 (wfl_operator, "Incompatible type for array. %s convert %qs to %qs",
14882 msg, lang_printable_name (type_value, 1), array_type_string);
14883 error_seen = 1;
14886 if (new_value)
14887 TREE_VALUE (entry) = new_value;
14889 if (array_type_string)
14890 free (array_type_string);
14892 TREE_PURPOSE (entry) = NULL_TREE;
14893 return error_seen;
14896 static tree
14897 build_this (int location)
14899 tree node = build_wfl_node (this_identifier_node);
14900 TREE_SET_CODE (node, THIS_EXPR);
14901 EXPR_WFL_LINECOL (node) = location;
14902 return node;
14905 /* 14.15 The return statement. It builds a modify expression that
14906 assigns the returned value to the RESULT_DECL that hold the value
14907 to be returned. */
14909 static tree
14910 build_return (int location, tree op)
14912 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14913 EXPR_WFL_LINECOL (node) = location;
14914 node = build_debugable_stmt (location, node);
14915 return node;
14918 static tree
14919 patch_return (tree node)
14921 tree return_exp = TREE_OPERAND (node, 0);
14922 tree meth = current_function_decl;
14923 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14924 int error_found = 0;
14926 TREE_TYPE (node) = error_mark_node;
14927 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14929 /* It's invalid to have a return value within a function that is
14930 declared with the keyword void or that is a constructor */
14931 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14932 error_found = 1;
14934 /* It's invalid to use a return statement in a static block */
14935 if (DECL_CLINIT_P (current_function_decl))
14936 error_found = 1;
14938 /* It's invalid to have a no return value within a function that
14939 isn't declared with the keyword `void' */
14940 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14941 error_found = 2;
14943 if (DECL_INSTINIT_P (current_function_decl))
14944 error_found = 1;
14946 if (error_found)
14948 if (DECL_INSTINIT_P (current_function_decl))
14949 parse_error_context (wfl_operator,
14950 "%<return%> inside instance initializer");
14952 else if (DECL_CLINIT_P (current_function_decl))
14953 parse_error_context (wfl_operator,
14954 "%<return%> inside static initializer");
14956 else if (!DECL_CONSTRUCTOR_P (meth))
14958 char *t = xstrdup (lang_printable_name (mtype, 0));
14959 parse_error_context (wfl_operator,
14960 "%<return%> with%s value from %<%s %s%>",
14961 (error_found == 1 ? "" : "out"),
14962 t, lang_printable_name (meth, 2));
14963 free (t);
14965 else
14966 parse_error_context (wfl_operator,
14967 "%<return%> with value from constructor %qs",
14968 lang_printable_name (meth, 2));
14969 return error_mark_node;
14972 /* If we have a return_exp, build a modify expression and expand
14973 it. Note: at that point, the assignment is declared valid, but we
14974 may want to carry some more hacks */
14975 if (return_exp)
14977 tree exp = java_complete_tree (return_exp);
14978 tree modify, patched;
14980 if ((patched = patch_string (exp)))
14981 exp = patched;
14983 modify = build2 (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14984 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14985 modify = java_complete_tree (modify);
14987 if (modify != error_mark_node)
14989 TREE_SIDE_EFFECTS (modify) = 1;
14990 TREE_OPERAND (node, 0) = modify;
14992 else
14993 return error_mark_node;
14995 TREE_TYPE (node) = void_type_node;
14996 TREE_SIDE_EFFECTS (node) = 1;
14997 return node;
15000 /* 14.8 The if Statement */
15002 static tree
15003 build_if_else_statement (int location, tree expression, tree if_body,
15004 tree else_body)
15006 tree node;
15007 if (!else_body)
15008 else_body = build_java_empty_stmt ();
15009 node = build3 (COND_EXPR, NULL_TREE, expression, if_body, else_body);
15010 EXPR_WFL_LINECOL (node) = location;
15011 node = build_debugable_stmt (location, node);
15012 return node;
15015 static tree
15016 patch_if_else_statement (tree node)
15018 tree expression = TREE_OPERAND (node, 0);
15019 int can_complete_normally
15020 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15021 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
15023 TREE_TYPE (node) = error_mark_node;
15024 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15026 /* The type of expression must be boolean */
15027 if (TREE_TYPE (expression) != boolean_type_node
15028 && TREE_TYPE (expression) != promoted_boolean_type_node)
15030 parse_error_context
15031 (wfl_operator,
15032 "Incompatible type for %<if%>. Can't convert %qs to %<boolean%>",
15033 lang_printable_name (TREE_TYPE (expression), 0));
15034 return error_mark_node;
15037 TREE_TYPE (node) = void_type_node;
15038 TREE_SIDE_EFFECTS (node) = 1;
15039 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
15040 return node;
15043 /* 14.6 Labeled Statements */
15045 /* Action taken when a labeled statement is parsed. a new
15046 LABELED_BLOCK_EXPR is created. No statement is attached to the
15047 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
15049 static tree
15050 build_labeled_block (int location, tree label)
15052 tree label_name ;
15053 tree label_decl, node;
15054 if (label == NULL_TREE || label == continue_identifier_node)
15055 label_name = label;
15056 else
15058 label_name = merge_qualified_name (label_id, label);
15059 /* Issue an error if we try to reuse a label that was previously
15060 declared */
15061 if (IDENTIFIER_LOCAL_VALUE (label_name))
15063 EXPR_WFL_LINECOL (wfl_operator) = location;
15064 parse_error_context (wfl_operator,
15065 "Declaration of %qs shadows a previous label declaration",
15066 IDENTIFIER_POINTER (label));
15067 EXPR_WFL_LINECOL (wfl_operator) =
15068 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
15069 parse_error_context (wfl_operator,
15070 "This is the location of the previous declaration of label %qs",
15071 IDENTIFIER_POINTER (label));
15072 java_error_count--;
15076 label_decl = create_label_decl (label_name);
15077 node = build2 (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
15078 EXPR_WFL_LINECOL (node) = location;
15079 TREE_SIDE_EFFECTS (node) = 1;
15080 return node;
15083 /* A labeled statement LBE is attached a statement. */
15085 static tree
15086 finish_labeled_statement (tree lbe, /* Labeled block expr */
15087 tree statement)
15089 /* In anyways, tie the loop to its statement */
15090 LABELED_BLOCK_BODY (lbe) = statement;
15091 pop_labeled_block ();
15092 POP_LABELED_BLOCK ();
15093 return lbe;
15096 /* 14.10, 14.11, 14.12 Loop Statements */
15098 /* Create an empty LOOP_EXPR and make it the last in the nested loop
15099 list. */
15101 static tree
15102 build_new_loop (tree loop_body)
15104 tree loop = build1 (LOOP_EXPR, NULL_TREE, loop_body);
15105 TREE_SIDE_EFFECTS (loop) = 1;
15106 PUSH_LOOP (loop);
15107 return loop;
15110 /* Create a loop body according to the following structure:
15111 COMPOUND_EXPR
15112 COMPOUND_EXPR (loop main body)
15113 EXIT_EXPR (this order is for while/for loops.
15114 LABELED_BLOCK_EXPR the order is reversed for do loops)
15115 LABEL_DECL (a continue occurring here branches at the
15116 BODY end of this labeled block)
15117 INCREMENT (if any)
15119 REVERSED, if nonzero, tells that the loop condition expr comes
15120 after the body, like in the do-while loop.
15122 To obtain a loop, the loop body structure described above is
15123 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15125 LABELED_BLOCK_EXPR
15126 LABEL_DECL (use this label to exit the loop)
15127 LOOP_EXPR
15128 <structure described above> */
15130 static tree
15131 build_loop_body (int location, tree condition, int reversed)
15133 tree first, second, body;
15135 condition = build1 (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15136 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15137 condition = build_debugable_stmt (location, condition);
15138 TREE_SIDE_EFFECTS (condition) = 1;
15140 body = build_labeled_block (0, continue_identifier_node);
15141 first = (reversed ? body : condition);
15142 second = (reversed ? condition : body);
15143 return build2 (COMPOUND_EXPR, NULL_TREE,
15144 build2 (COMPOUND_EXPR, NULL_TREE, first, second),
15145 build_java_empty_stmt ());
15148 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15149 their order) on the current loop. Unlink the current loop from the
15150 loop list. */
15152 static tree
15153 finish_loop_body (int location, tree condition, tree body, int reversed)
15155 tree to_return = ctxp->current_loop;
15156 tree loop_body = LOOP_EXPR_BODY (to_return);
15157 if (condition)
15159 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15160 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15161 The real EXIT_EXPR is one operand further. */
15162 EXPR_WFL_LINECOL (cnode) = location;
15163 if (TREE_CODE (cnode) == EXPR_WITH_FILE_LOCATION)
15165 cnode = EXPR_WFL_NODE (cnode);
15166 /* This one is for accurate error reports */
15167 EXPR_WFL_LINECOL (cnode) = location;
15169 TREE_OPERAND (cnode, 0) = condition;
15171 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15172 POP_LOOP ();
15173 return to_return;
15176 /* Tailored version of finish_loop_body for FOR loops, when FOR
15177 loops feature the condition part */
15179 static tree
15180 finish_for_loop (int location, tree condition, tree update, tree body)
15182 /* Put the condition and the loop body in place */
15183 tree loop = finish_loop_body (location, condition, body, 0);
15184 /* LOOP is the current loop which has been now popped of the loop
15185 stack. Mark the update block as reachable and install it. We do
15186 this because the (current interpretation of the) JLS requires
15187 that the update expression be considered reachable even if the
15188 for loop's body doesn't complete normally. */
15189 if (update != NULL_TREE && !IS_EMPTY_STMT (update))
15191 tree up2 = update;
15192 if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15193 up2 = EXPR_WFL_NODE (up2);
15194 /* It is possible for the update expression to be an
15195 EXPR_WFL_NODE wrapping nothing. */
15196 if (up2 != NULL_TREE && !IS_EMPTY_STMT (up2))
15198 /* Try to detect constraint violations. These would be
15199 programming errors somewhere. */
15200 if (! EXPR_P (up2) || TREE_CODE (up2) == LOOP_EXPR)
15201 abort ();
15202 SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15205 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15206 return loop;
15209 /* Try to find the loop a block might be related to. This comprises
15210 the case where the LOOP_EXPR is found as the second operand of a
15211 COMPOUND_EXPR, because the loop happens to have an initialization
15212 part, then expressed as the first operand of the COMPOUND_EXPR. If
15213 the search finds something, 1 is returned. Otherwise, 0 is
15214 returned. The search is assumed to start from a
15215 LABELED_BLOCK_EXPR's block. */
15217 static tree
15218 search_loop (tree statement)
15220 if (TREE_CODE (statement) == LOOP_EXPR)
15221 return statement;
15223 if (TREE_CODE (statement) == BLOCK)
15224 statement = BLOCK_SUBBLOCKS (statement);
15225 else
15226 return NULL_TREE;
15228 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15229 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15230 statement = TREE_OPERAND (statement, 1);
15232 return (TREE_CODE (statement) == LOOP_EXPR
15233 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15236 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15237 returned otherwise. */
15239 static int
15240 labeled_block_contains_loop_p (tree block, tree loop)
15242 if (!block)
15243 return 0;
15245 if (LABELED_BLOCK_BODY (block) == loop)
15246 return 1;
15248 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15249 return 1;
15251 return 0;
15254 /* If the loop isn't surrounded by a labeled statement, create one and
15255 insert LOOP as its body. */
15257 static tree
15258 patch_loop_statement (tree loop)
15260 tree loop_label;
15262 TREE_TYPE (loop) = void_type_node;
15263 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15264 return loop;
15266 loop_label = build_labeled_block (0, NULL_TREE);
15267 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15268 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15269 LABELED_BLOCK_BODY (loop_label) = loop;
15270 PUSH_LABELED_BLOCK (loop_label);
15271 return loop_label;
15274 /* 14.13, 14.14: break and continue Statements */
15276 /* Build a break or a continue statement. a null NAME indicates an
15277 unlabeled break/continue statement. */
15279 static tree
15280 build_bc_statement (int location, int is_break, tree name)
15282 tree break_continue, label_block_expr = NULL_TREE;
15284 if (name)
15286 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15287 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15288 /* Null means that we don't have a target for this named
15289 break/continue. In this case, we make the target to be the
15290 label name, so that the error can be reported accurately in
15291 patch_bc_statement. */
15292 label_block_expr = EXPR_WFL_NODE (name);
15294 /* Unlabeled break/continue will be handled during the
15295 break/continue patch operation */
15296 break_continue = build1 (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr);
15298 IS_BREAK_STMT_P (break_continue) = is_break;
15299 TREE_SIDE_EFFECTS (break_continue) = 1;
15300 EXPR_WFL_LINECOL (break_continue) = location;
15301 break_continue = build_debugable_stmt (location, break_continue);
15302 return break_continue;
15305 /* Verification of a break/continue statement. */
15307 static tree
15308 patch_bc_statement (tree node)
15310 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15311 tree labeled_block = ctxp->current_labeled_block;
15312 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15314 /* Having an identifier here means that the target is unknown. */
15315 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15317 parse_error_context (wfl_operator, "No label definition found for %qs",
15318 IDENTIFIER_POINTER (bc_label));
15319 return error_mark_node;
15321 if (! IS_BREAK_STMT_P (node))
15323 /* It's a continue statement. */
15324 for (;; labeled_block = TREE_CHAIN (labeled_block))
15326 if (labeled_block == NULL_TREE)
15328 if (bc_label == NULL_TREE)
15329 parse_error_context (wfl_operator,
15330 "%<continue%> must be in loop");
15331 else
15332 parse_error_context
15333 (wfl_operator, "continue label %qs does not name a loop",
15334 IDENTIFIER_POINTER (bc_label));
15335 return error_mark_node;
15337 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15338 == continue_identifier_node)
15339 && (bc_label == NULL_TREE
15340 || TREE_CHAIN (labeled_block) == bc_label))
15342 bc_label = labeled_block;
15343 break;
15347 else if (!bc_label)
15349 for (;; labeled_block = TREE_CHAIN (labeled_block))
15351 if (labeled_block == NULL_TREE)
15353 parse_error_context (wfl_operator,
15354 "%<break%> must be in loop or switch");
15355 return error_mark_node;
15357 target_stmt = LABELED_BLOCK_BODY (labeled_block);
15358 if (TREE_CODE (target_stmt) == SWITCH_EXPR
15359 || search_loop (target_stmt))
15361 bc_label = labeled_block;
15362 break;
15367 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15368 CAN_COMPLETE_NORMALLY (bc_label) = 1;
15370 /* Our break/continue don't return values. */
15371 TREE_TYPE (node) = void_type_node;
15372 /* Encapsulate the break within a compound statement so that it's
15373 expanded all the times by expand_expr (and not clobbered
15374 sometimes, like after a if statement) */
15375 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15376 TREE_SIDE_EFFECTS (node) = 1;
15377 return node;
15380 /* Process the exit expression belonging to a loop. Its type must be
15381 boolean. */
15383 static tree
15384 patch_exit_expr (tree node)
15386 tree expression = TREE_OPERAND (node, 0);
15387 TREE_TYPE (node) = error_mark_node;
15388 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15390 /* The type of expression must be boolean */
15391 if (TREE_TYPE (expression) != boolean_type_node)
15393 parse_error_context
15394 (wfl_operator,
15395 "Incompatible type for loop conditional. Can't convert %qs to %<boolean%>",
15396 lang_printable_name (TREE_TYPE (expression), 0));
15397 return error_mark_node;
15399 /* Now we know things are allright, invert the condition, fold and
15400 return */
15401 TREE_OPERAND (node, 0) =
15402 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15404 if (! integer_zerop (TREE_OPERAND (node, 0))
15405 && ctxp->current_loop != NULL_TREE
15406 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15407 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15408 if (! integer_onep (TREE_OPERAND (node, 0)))
15409 CAN_COMPLETE_NORMALLY (node) = 1;
15412 TREE_TYPE (node) = void_type_node;
15413 return node;
15416 /* 14.9 Switch statement */
15418 static tree
15419 patch_switch_statement (tree node)
15421 tree se = TREE_OPERAND (node, 0), se_type;
15422 tree save, iter;
15424 /* Complete the switch expression */
15425 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15426 se_type = TREE_TYPE (se);
15427 /* The type of the switch expression must be char, byte, short or
15428 int */
15429 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15431 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15432 parse_error_context (wfl_operator,
15433 "Incompatible type for %<switch%>. Can't convert %qs to %<int%>",
15434 lang_printable_name (se_type, 0));
15435 /* This is what java_complete_tree will check */
15436 TREE_OPERAND (node, 0) = error_mark_node;
15437 return error_mark_node;
15440 /* Save and restore the outer case label list. */
15441 save = case_label_list;
15442 case_label_list = NULL_TREE;
15444 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15446 /* See if we've found a duplicate label. We can't leave this until
15447 code generation, because in `--syntax-only' and `-C' modes we
15448 don't do ordinary code generation. */
15449 for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15451 HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15452 tree subiter;
15453 for (subiter = TREE_CHAIN (iter);
15454 subiter != NULL_TREE;
15455 subiter = TREE_CHAIN (subiter))
15457 HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15458 if (val == subval)
15460 EXPR_WFL_LINECOL (wfl_operator)
15461 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15462 /* The case_label_list is in reverse order, so print the
15463 outer label first. */
15464 parse_error_context (wfl_operator, "duplicate case label: %<"
15465 HOST_WIDE_INT_PRINT_DEC "%>", subval);
15466 EXPR_WFL_LINECOL (wfl_operator)
15467 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15468 parse_error_context (wfl_operator, "original label is here");
15470 break;
15475 case_label_list = save;
15477 /* Ready to return */
15478 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15480 TREE_TYPE (node) = error_mark_node;
15481 return error_mark_node;
15483 TREE_TYPE (node) = void_type_node;
15484 TREE_SIDE_EFFECTS (node) = 1;
15485 CAN_COMPLETE_NORMALLY (node)
15486 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15487 || ! SWITCH_HAS_DEFAULT (node);
15488 return node;
15491 /* Assertions. */
15493 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15494 might be NULL_TREE. */
15495 static tree
15496 build_assertion (
15497 #ifdef USE_MAPPED_LOCATION
15498 source_location location,
15499 #else
15500 int location,
15501 #endif
15502 tree condition, tree value)
15504 tree node;
15505 tree klass = GET_CPC ();
15507 if (! enable_assertions (klass))
15509 condition = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15510 boolean_false_node, condition);
15511 if (value == NULL_TREE)
15512 value = build_java_empty_stmt ();
15513 return build_if_else_statement (location, condition,
15514 value, NULL_TREE);
15517 if (! CLASS_USES_ASSERTIONS (klass))
15519 tree field, classdollar, id, call;
15520 tree class_type = TREE_TYPE (klass);
15522 field = add_field (class_type,
15523 get_identifier ("$assertionsDisabled"),
15524 boolean_type_node,
15525 ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15526 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15527 FIELD_SYNTHETIC (field) = 1;
15529 classdollar = build_incomplete_class_ref (location, class_type);
15531 /* Call CLASS.desiredAssertionStatus(). */
15532 id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15533 call = build3 (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15534 call = make_qualified_primary (classdollar, call, location);
15535 TREE_SIDE_EFFECTS (call) = 1;
15537 /* Invert to obtain !CLASS.desiredAssertionStatus(). This may
15538 seem odd, but we do it to generate code identical to that of
15539 the JDK. */
15540 call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15541 TREE_SIDE_EFFECTS (call) = 1;
15542 DECL_INITIAL (field) = call;
15544 /* Record the initializer in the initializer statement list. */
15545 call = build2 (MODIFY_EXPR, NULL_TREE, field, call);
15546 TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15547 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15548 MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15550 CLASS_USES_ASSERTIONS (klass) = 1;
15553 if (value != NULL_TREE)
15554 value = tree_cons (NULL_TREE, value, NULL_TREE);
15556 node = build_wfl_node (get_identifier ("java"));
15557 node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15558 location);
15559 node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15560 location);
15562 node = build3 (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15563 TREE_SIDE_EFFECTS (node) = 1;
15564 /* It is too early to use BUILD_THROW. */
15565 node = build1 (THROW_EXPR, NULL_TREE, node);
15566 TREE_SIDE_EFFECTS (node) = 1;
15568 /* We invert the condition; if we just put NODE as the `else' part
15569 then we generate weird-looking bytecode. */
15570 condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15571 /* Check $assertionsDisabled. */
15572 condition
15573 = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15574 build1 (TRUTH_NOT_EXPR, NULL_TREE,
15575 build_wfl_node (get_identifier ("$assertionsDisabled"))),
15576 condition);
15577 node = build_if_else_statement (location, condition, node, NULL_TREE);
15578 return node;
15581 /* 14.18 The try/catch statements */
15583 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15584 catches TYPE and executes CATCH_STMTS. */
15586 static tree
15587 encapsulate_with_try_catch (int location, tree type_or_name, tree try_stmts,
15588 tree catch_stmts)
15590 tree try_block, catch_clause_param, catch_block, catch;
15592 /* First build a try block */
15593 try_block = build_expr_block (try_stmts, NULL_TREE);
15595 /* Build a catch block: we need a catch clause parameter */
15596 if (TREE_CODE (type_or_name) == EXPR_WITH_FILE_LOCATION)
15598 tree catch_type = obtain_incomplete_type (type_or_name);
15599 jdep *dep;
15600 catch_clause_param = build_decl (VAR_DECL, wpv_id, catch_type);
15601 register_incomplete_type (JDEP_VARIABLE, type_or_name,
15602 catch_clause_param, catch_type);
15603 dep = CLASSD_LAST (ctxp->classd_list);
15604 JDEP_GET_PATCH (dep) = &TREE_TYPE (catch_clause_param);
15606 else
15607 catch_clause_param = build_decl (VAR_DECL, wpv_id,
15608 build_pointer_type (type_or_name));
15610 /* And a block */
15611 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15613 /* Initialize the variable and store in the block */
15614 catch = build2 (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15615 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15616 add_stmt_to_block (catch_block, NULL_TREE, catch);
15618 /* Add the catch statements */
15619 add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15621 /* Now we can build a JAVA_CATCH_EXPR */
15622 catch_block = build1 (JAVA_CATCH_EXPR, NULL_TREE, catch_block);
15624 return build_try_statement (location, try_block, catch_block);
15627 static tree
15628 build_try_statement (int location, tree try_block, tree catches)
15630 tree node = build2 (TRY_EXPR, NULL_TREE, try_block, catches);
15631 EXPR_WFL_LINECOL (node) = location;
15632 return node;
15635 static tree
15636 build_try_finally_statement (int location, tree try_block, tree finally)
15638 tree node = build2 (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15639 EXPR_WFL_LINECOL (node) = location;
15640 return node;
15643 static tree
15644 patch_try_statement (tree node)
15646 int error_found = 0;
15647 tree try = TREE_OPERAND (node, 0);
15648 /* Exception handlers are considered in left to right order */
15649 tree catch = nreverse (TREE_OPERAND (node, 1));
15650 tree current, caught_type_list = NULL_TREE;
15652 /* Check catch clauses, if any. Every time we find an error, we try
15653 to process the next catch clause. We process the catch clause before
15654 the try block so that when processing the try block we can check thrown
15655 exceptions against the caught type list. */
15656 for (current = catch; current; current = TREE_CHAIN (current))
15658 tree carg_decl, carg_type;
15659 tree sub_current, catch_block, catch_clause;
15660 int unreachable;
15662 /* At this point, the structure of the catch clause is
15663 JAVA_CATCH_EXPR (catch node)
15664 BLOCK (with the decl of the parameter)
15665 COMPOUND_EXPR
15666 MODIFY_EXPR (assignment of the catch parameter)
15667 BLOCK (catch clause block)
15669 catch_clause = TREE_OPERAND (current, 0);
15670 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15671 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15673 /* Catch clauses can't have more than one parameter declared,
15674 but it's already enforced by the grammar. Make sure that the
15675 only parameter of the clause statement in of class Throwable
15676 or a subclass of Throwable, but that was done earlier. The
15677 catch clause parameter type has also been resolved. */
15679 /* Just make sure that the catch clause parameter type inherits
15680 from java.lang.Throwable */
15681 if (!inherits_from_p (carg_type, throwable_type_node))
15683 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15684 parse_error_context (wfl_operator,
15685 "Can't catch class %qs. Catch clause parameter type must be a subclass of class %<java.lang.Throwable%>",
15686 lang_printable_name (carg_type, 0));
15687 error_found = 1;
15688 continue;
15691 /* Partial check for unreachable catch statement: The catch
15692 clause is reachable iff is no earlier catch block A in
15693 the try statement such that the type of the catch
15694 clause's parameter is the same as or a subclass of the
15695 type of A's parameter */
15696 unreachable = 0;
15697 for (sub_current = catch;
15698 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15700 tree sub_catch_clause, decl;
15701 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15702 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15704 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15706 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15707 parse_error_context
15708 (wfl_operator,
15709 "%<catch%> not reached because of the catch clause at line %d",
15710 EXPR_WFL_LINENO (sub_current));
15711 unreachable = error_found = 1;
15712 break;
15715 /* Complete the catch clause block */
15716 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15717 if (catch_block == error_mark_node)
15719 error_found = 1;
15720 continue;
15722 if (CAN_COMPLETE_NORMALLY (catch_block))
15723 CAN_COMPLETE_NORMALLY (node) = 1;
15724 TREE_OPERAND (current, 0) = catch_block;
15726 if (unreachable)
15727 continue;
15729 /* Things to do here: the exception must be thrown */
15731 /* Link this type to the caught type list */
15732 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15735 PUSH_EXCEPTIONS (caught_type_list);
15736 if ((try = java_complete_tree (try)) == error_mark_node)
15737 error_found = 1;
15738 if (CAN_COMPLETE_NORMALLY (try))
15739 CAN_COMPLETE_NORMALLY (node) = 1;
15740 POP_EXCEPTIONS ();
15742 /* Verification ends here */
15743 if (error_found)
15744 return error_mark_node;
15746 TREE_OPERAND (node, 0) = try;
15747 TREE_OPERAND (node, 1) = catch;
15748 TREE_TYPE (node) = void_type_node;
15749 return node;
15752 /* 14.17 The synchronized Statement */
15754 static tree
15755 patch_synchronized_statement (tree node, tree wfl_op1)
15757 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15758 tree block = TREE_OPERAND (node, 1);
15760 tree tmp, enter, exit, expr_decl, assignment;
15762 if (expr == error_mark_node)
15764 block = java_complete_tree (block);
15765 return expr;
15768 /* We might be trying to synchronize on a STRING_CST */
15769 if ((tmp = patch_string (expr)))
15770 expr = tmp;
15772 /* The TYPE of expr must be a reference type */
15773 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15775 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15776 parse_error_context (wfl_operator, "Incompatible type for %<synchronized%>. Can't convert %qs to %<java.lang.Object%>",
15777 lang_printable_name (TREE_TYPE (expr), 0));
15778 return error_mark_node;
15781 /* Generate a try-finally for the synchronized statement, except
15782 that the handler that catches all throw exception calls
15783 _Jv_MonitorExit and then rethrow the exception.
15784 The synchronized statement is then implemented as:
15787 _Jv_MonitorEnter (expression)
15788 synchronized_block
15789 _Jv_MonitorExit (expression)
15791 CATCH_ALL
15793 e = _Jv_exception_info ();
15794 _Jv_MonitorExit (expression)
15795 Throw (e);
15796 } */
15798 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15799 BUILD_MONITOR_ENTER (enter, expr_decl);
15800 BUILD_MONITOR_EXIT (exit, expr_decl);
15801 CAN_COMPLETE_NORMALLY (enter) = 1;
15802 CAN_COMPLETE_NORMALLY (exit) = 1;
15803 assignment = build2 (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15804 TREE_SIDE_EFFECTS (assignment) = 1;
15805 node = build2 (COMPOUND_EXPR, NULL_TREE,
15806 build2 (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15807 build2 (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15808 node = build_expr_block (node, expr_decl);
15810 return java_complete_tree (node);
15813 /* 14.16 The throw Statement */
15815 static tree
15816 patch_throw_statement (tree node, tree wfl_op1)
15818 tree expr = TREE_OPERAND (node, 0);
15819 tree type = TREE_TYPE (expr);
15820 int unchecked_ok = 0, tryblock_throws_ok = 0;
15822 /* Thrown expression must be assignable to java.lang.Throwable */
15823 if (!try_reference_assignconv (throwable_type_node, expr))
15825 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15826 parse_error_context (wfl_operator,
15827 "Can't throw %qs; it must be a subclass of class %<java.lang.Throwable%>",
15828 lang_printable_name (type, 0));
15829 /* If the thrown expression was a reference, we further the
15830 compile-time check. */
15831 if (!JREFERENCE_TYPE_P (type))
15832 return error_mark_node;
15835 /* At least one of the following must be true */
15837 /* The type of the throw expression is a not checked exception,
15838 i.e. is a unchecked expression. */
15839 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15841 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15842 /* An instance can't throw a checked exception unless that exception
15843 is explicitly declared in the `throws' clause of each
15844 constructor. This doesn't apply to anonymous classes, since they
15845 don't have declared constructors. */
15846 if (!unchecked_ok
15847 && DECL_INSTINIT_P (current_function_decl)
15848 && !ANONYMOUS_CLASS_P (current_class))
15850 tree current;
15851 for (current = TYPE_METHODS (current_class); current;
15852 current = TREE_CHAIN (current))
15853 if (DECL_CONSTRUCTOR_P (current)
15854 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15856 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)",
15857 lang_printable_name (TREE_TYPE (expr), 0));
15858 return error_mark_node;
15862 /* Throw is contained in a try statement and at least one catch
15863 clause can receive the thrown expression or the current method is
15864 declared to throw such an exception. Or, the throw statement is
15865 contained in a method or constructor declaration and the type of
15866 the Expression is assignable to at least one type listed in the
15867 throws clause the declaration. */
15868 if (!unchecked_ok)
15869 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15870 if (!(unchecked_ok || tryblock_throws_ok))
15872 /* If there is a surrounding try block that has no matching
15873 clatch clause, report it first. A surrounding try block exits
15874 only if there is something after the list of checked
15875 exception thrown by the current function (if any). */
15876 if (IN_TRY_BLOCK_P ())
15877 parse_error_context (wfl_operator, "Checked exception %qs can't be caught by any of the catch clause(s) of the surrounding %<try%> block",
15878 lang_printable_name (type, 0));
15879 /* If we have no surrounding try statement and the method doesn't have
15880 any throws, report it now. FIXME */
15882 /* We report that the exception can't be throw from a try block
15883 in all circumstances but when the `throw' is inside a static
15884 block. */
15885 else if (!EXCEPTIONS_P (currently_caught_type_list)
15886 && !tryblock_throws_ok)
15888 if (DECL_CLINIT_P (current_function_decl))
15889 parse_error_context (wfl_operator,
15890 "Checked exception %qs can't be thrown in initializer",
15891 lang_printable_name (type, 0));
15892 else
15893 parse_error_context (wfl_operator,
15894 "Checked exception %qs isn't thrown from a %<try%> block",
15895 lang_printable_name (type, 0));
15897 /* Otherwise, the current method doesn't have the appropriate
15898 throws declaration */
15899 else
15900 parse_error_context (wfl_operator, "Checked exception %qs doesn't match any of current method's %<throws%> declaration(s)",
15901 lang_printable_name (type, 0));
15902 return error_mark_node;
15905 if (! flag_emit_class_files)
15906 BUILD_THROW (node, expr);
15908 return node;
15911 /* Check that exception said to be thrown by method DECL can be
15912 effectively caught from where DECL is invoked. THIS_EXPR is the
15913 expression that computes `this' for the method call. */
15914 static void
15915 check_thrown_exceptions (
15916 #ifdef USE_MAPPED_LOCATION
15917 source_location location,
15918 #else
15920 int location,
15921 #endif
15922 tree decl, tree this_expr)
15924 tree throws;
15925 int is_array_call = 0;
15927 /* Skip check within generated methods, such as access$<n>. */
15928 if (NESTED_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (current_function_decl)))
15929 return;
15931 if (this_expr != NULL_TREE
15932 && TREE_CODE (TREE_TYPE (this_expr)) == POINTER_TYPE
15933 && TYPE_ARRAY_P (TREE_TYPE (TREE_TYPE (this_expr))))
15934 is_array_call = 1;
15936 /* For all the unchecked exceptions thrown by DECL. */
15937 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15938 throws = TREE_CHAIN (throws))
15939 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15941 /* Suppress errors about cloning arrays. */
15942 if (is_array_call && DECL_NAME (decl) == get_identifier ("clone"))
15943 continue;
15945 #ifdef USE_MAPPED_LOCATION
15946 SET_EXPR_LOCATION (wfl_operator, location);
15947 #else
15948 EXPR_WFL_LINECOL (wfl_operator) = location;
15949 #endif
15950 if (DECL_FINIT_P (current_function_decl))
15951 parse_error_context
15952 (wfl_operator, "Exception %qs can't be thrown in initializer",
15953 lang_printable_name (TREE_VALUE (throws), 0));
15954 else
15956 parse_error_context
15957 (wfl_operator, "Exception %qs must be caught, or it must be declared in the %<throws%> clause of %qs",
15958 lang_printable_name (TREE_VALUE (throws), 0),
15959 (DECL_INIT_P (current_function_decl) ?
15960 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15961 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15966 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15967 try-catch blocks, OR is listed in the `throws' clause of the
15968 current method. */
15970 static int
15971 check_thrown_exceptions_do (tree exception)
15973 tree list = currently_caught_type_list;
15974 resolve_and_layout (exception, NULL_TREE);
15975 /* First, all the nested try-catch-finally at that stage. The
15976 last element contains `throws' clause exceptions, if any. */
15977 if (IS_UNCHECKED_EXCEPTION_P (exception))
15978 return 1;
15979 while (list)
15981 tree caught;
15982 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15983 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15984 return 1;
15985 list = TREE_CHAIN (list);
15987 return 0;
15990 static void
15991 purge_unchecked_exceptions (tree mdecl)
15993 tree throws = DECL_FUNCTION_THROWS (mdecl);
15994 tree new = NULL_TREE;
15996 while (throws)
15998 tree next = TREE_CHAIN (throws);
15999 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
16001 TREE_CHAIN (throws) = new;
16002 new = throws;
16004 throws = next;
16006 /* List is inverted here, but it doesn't matter */
16007 DECL_FUNCTION_THROWS (mdecl) = new;
16010 /* This function goes over all of CLASS_TYPE ctors and checks whether
16011 each of them features at least one unchecked exception in its
16012 `throws' clause. If it's the case, it returns `true', `false'
16013 otherwise. */
16015 static bool
16016 ctors_unchecked_throws_clause_p (tree class_type)
16018 tree current;
16020 for (current = TYPE_METHODS (class_type); current;
16021 current = TREE_CHAIN (current))
16023 bool ctu = false; /* Ctor Throws Unchecked */
16024 if (DECL_CONSTRUCTOR_P (current))
16026 tree throws;
16027 for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
16028 throws = TREE_CHAIN (throws))
16029 if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
16030 ctu = true;
16032 /* We return false as we found one ctor that is unfit. */
16033 if (!ctu && DECL_CONSTRUCTOR_P (current))
16034 return false;
16036 /* All ctors feature at least one unchecked exception in their
16037 `throws' clause. */
16038 return true;
16041 /* 15.24 Conditional Operator ?: */
16043 static tree
16044 patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
16046 tree cond = TREE_OPERAND (node, 0);
16047 tree op1 = TREE_OPERAND (node, 1);
16048 tree op2 = TREE_OPERAND (node, 2);
16049 tree resulting_type = NULL_TREE;
16050 tree t1, t2, patched;
16051 int error_found = 0;
16053 /* The condition and operands of ?: might be StringBuffers crafted
16054 as a result of a string concatenation. Obtain decent ones here. */
16055 if ((patched = patch_string (cond)))
16056 TREE_OPERAND (node, 0) = cond = patched;
16057 if ((patched = patch_string (op1)))
16058 TREE_OPERAND (node, 1) = op1 = patched;
16059 if ((patched = patch_string (op2)))
16060 TREE_OPERAND (node, 2) = op2 = patched;
16062 t1 = TREE_TYPE (op1);
16063 t2 = TREE_TYPE (op2);
16065 /* The first expression must be a boolean */
16066 if (TREE_TYPE (cond) != boolean_type_node)
16068 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
16069 parse_error_context (wfl_operator,
16070 "Incompatible type for %<?:%>. Can't convert %qs to %<boolean%>",
16071 lang_printable_name (TREE_TYPE (cond), 0));
16072 error_found = 1;
16075 /* Second and third can be numeric, boolean (i.e. primitive),
16076 references or null. Anything else results in an error */
16077 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
16078 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
16079 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
16080 || (t1 == boolean_type_node && t2 == boolean_type_node)))
16081 error_found = 1;
16083 /* Determine the type of the conditional expression. Same types are
16084 easy to deal with */
16085 else if (t1 == t2)
16086 resulting_type = t1;
16088 /* There are different rules for numeric types */
16089 else if (JNUMERIC_TYPE_P (t1))
16091 /* if byte/short found, the resulting type is short */
16092 if ((t1 == byte_type_node && t2 == short_type_node)
16093 || (t1 == short_type_node && t2 == byte_type_node))
16094 resulting_type = short_type_node;
16096 /* If t1 is a constant int and t2 is of type byte, short or char
16097 and t1's value fits in t2, then the resulting type is t2 */
16098 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
16099 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
16100 resulting_type = t2;
16102 /* If t2 is a constant int and t1 is of type byte, short or char
16103 and t2's value fits in t1, then the resulting type is t1 */
16104 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
16105 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
16106 resulting_type = t1;
16108 /* Otherwise, binary numeric promotion is applied and the
16109 resulting type is the promoted type of operand 1 and 2 */
16110 else
16111 resulting_type = binary_numeric_promotion (t1, t2,
16112 &TREE_OPERAND (node, 1),
16113 &TREE_OPERAND (node, 2));
16116 /* Cases of a reference and a null type */
16117 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
16118 resulting_type = t1;
16120 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
16121 resulting_type = t2;
16123 /* Last case: different reference types. If a type can be converted
16124 into the other one by assignment conversion, the latter
16125 determines the type of the expression */
16126 else if ((resulting_type = try_reference_assignconv (t1, op2)))
16127 resulting_type = promote_type (t1);
16129 else if ((resulting_type = try_reference_assignconv (t2, op1)))
16130 resulting_type = promote_type (t2);
16132 /* If we don't have any resulting type, we're in trouble */
16133 if (!resulting_type)
16135 char *t = xstrdup (lang_printable_name (t1, 0));
16136 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16137 parse_error_context (wfl_operator,
16138 "Incompatible type for %<?:%>. Can't convert %qs to %qs",
16139 t, lang_printable_name (t2, 0));
16140 free (t);
16141 error_found = 1;
16144 if (error_found)
16146 TREE_TYPE (node) = error_mark_node;
16147 return error_mark_node;
16150 TREE_TYPE (node) = resulting_type;
16151 TREE_SET_CODE (node, COND_EXPR);
16152 CAN_COMPLETE_NORMALLY (node) = 1;
16153 return node;
16156 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16158 static tree
16159 maybe_build_class_init_for_field (tree decl, tree expr)
16161 tree clas = DECL_CONTEXT (decl);
16162 if (flag_emit_class_files)
16163 return expr;
16165 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16166 && FIELD_FINAL (decl))
16168 tree init = DECL_INITIAL (decl);
16169 if (init != NULL_TREE)
16170 init = fold_constant_for_init (init, decl);
16171 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16172 return expr;
16175 return build_class_init (clas, expr);
16178 /* Try to constant fold NODE.
16179 If NODE is not a constant expression, return NULL_EXPR.
16180 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16182 static tree
16183 fold_constant_for_init (tree node, tree context)
16185 tree op0, op1, val;
16186 enum tree_code code = TREE_CODE (node);
16188 switch (code)
16190 case INTEGER_CST:
16191 if (node == null_pointer_node)
16192 return NULL_TREE;
16193 case STRING_CST:
16194 case REAL_CST:
16195 return node;
16197 case PLUS_EXPR:
16198 case MINUS_EXPR:
16199 case MULT_EXPR:
16200 case TRUNC_MOD_EXPR:
16201 case RDIV_EXPR:
16202 case LSHIFT_EXPR:
16203 case RSHIFT_EXPR:
16204 case URSHIFT_EXPR:
16205 case BIT_AND_EXPR:
16206 case BIT_XOR_EXPR:
16207 case BIT_IOR_EXPR:
16208 case TRUTH_ANDIF_EXPR:
16209 case TRUTH_ORIF_EXPR:
16210 case EQ_EXPR:
16211 case NE_EXPR:
16212 case GT_EXPR:
16213 case GE_EXPR:
16214 case LT_EXPR:
16215 case LE_EXPR:
16216 op0 = TREE_OPERAND (node, 0);
16217 op1 = TREE_OPERAND (node, 1);
16218 val = fold_constant_for_init (op0, context);
16219 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16220 return NULL_TREE;
16221 TREE_OPERAND (node, 0) = val;
16222 val = fold_constant_for_init (op1, context);
16223 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16224 return NULL_TREE;
16225 TREE_OPERAND (node, 1) = val;
16226 return patch_binop (node, op0, op1, 1);
16228 case UNARY_PLUS_EXPR:
16229 case NEGATE_EXPR:
16230 case TRUTH_NOT_EXPR:
16231 case BIT_NOT_EXPR:
16232 case CONVERT_EXPR:
16233 case NOP_EXPR:
16234 op0 = TREE_OPERAND (node, 0);
16235 val = fold_constant_for_init (op0, context);
16236 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16237 return NULL_TREE;
16238 TREE_OPERAND (node, 0) = val;
16239 val = patch_unaryop (node, op0);
16240 if (! TREE_CONSTANT (val))
16241 return NULL_TREE;
16242 return val;
16244 break;
16246 case COND_EXPR:
16247 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16248 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16249 return NULL_TREE;
16250 TREE_OPERAND (node, 0) = val;
16251 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16252 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16253 return NULL_TREE;
16254 TREE_OPERAND (node, 1) = val;
16255 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16256 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16257 return NULL_TREE;
16258 TREE_OPERAND (node, 2) = val;
16259 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 2)
16260 : TREE_OPERAND (node, 1);
16262 case VAR_DECL:
16263 case FIELD_DECL:
16264 if (! FIELD_FINAL (node)
16265 || DECL_INITIAL (node) == NULL_TREE)
16266 return NULL_TREE;
16267 val = DECL_INITIAL (node);
16268 /* Guard against infinite recursion. */
16269 DECL_INITIAL (node) = NULL_TREE;
16270 val = fold_constant_for_init (val, node);
16271 if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16272 val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16273 DECL_INITIAL (node) = val;
16274 return val;
16276 case EXPR_WITH_FILE_LOCATION:
16277 /* Compare java_complete_tree and resolve_expression_name. */
16278 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16279 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16281 tree name = EXPR_WFL_NODE (node);
16282 tree decl;
16283 if (PRIMARY_P (node))
16284 return NULL_TREE;
16285 else if (! QUALIFIED_P (name))
16287 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16288 if (decl == NULL_TREE
16289 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16290 return NULL_TREE;
16291 return fold_constant_for_init (decl, decl);
16293 else
16295 tree r = NULL_TREE;
16296 /* Install the proper context for the field resolution. */
16297 tree saved_current_class = current_class;
16298 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
16299 current_class = DECL_CONTEXT (context);
16300 qualify_ambiguous_name (node);
16301 r = resolve_field_access (node, &decl, NULL);
16302 /* Restore prior context. */
16303 current_class = saved_current_class;
16304 if (r != error_mark_node && decl != NULL_TREE)
16305 return fold_constant_for_init (decl, decl);
16306 return NULL_TREE;
16309 else
16311 op0 = TREE_OPERAND (node, 0);
16312 val = fold_constant_for_init (op0, context);
16313 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16314 return NULL_TREE;
16315 TREE_OPERAND (node, 0) = val;
16316 return val;
16319 #ifdef USE_COMPONENT_REF
16320 case IDENTIFIER:
16321 case COMPONENT_REF:
16323 #endif
16325 default:
16326 return NULL_TREE;
16330 #ifdef USE_COMPONENT_REF
16331 /* Context is 'T' for TypeName, 'P' for PackageName,
16332 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16334 tree
16335 resolve_simple_name (tree name, int context)
16339 tree
16340 resolve_qualified_name (tree name, int context)
16343 #endif
16345 void
16346 init_src_parse (void)
16348 /* Sanity check; we've been bit by this before. */
16349 if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16350 abort ();
16355 /* This section deals with the functions that are called when tables
16356 recording class initialization information are traversed. */
16358 /* This function is called for each class that is known definitely
16359 initialized when a given static method was called. This function
16360 augments a compound expression (INFO) storing all assignment to
16361 initialized static class flags if a flag already existed, otherwise
16362 a new one is created. */
16364 static int
16365 emit_test_initialization (void **entry_p, void *info)
16367 tree l = (tree) info;
16368 tree decl, init;
16369 tree key = (tree) *entry_p;
16370 tree *ite;
16371 htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16373 /* If we haven't found a flag and we're dealing with self registered
16374 with current_function_decl, then don't do anything. Self is
16375 always added as definitely initialized but this information is
16376 valid only if used outside the current function. */
16377 if (current_function_decl == TREE_PURPOSE (l)
16378 && java_treetreehash_find (cf_ht, key) == NULL)
16379 return true;
16381 ite = java_treetreehash_new (cf_ht, key);
16383 /* If we don't have a variable, create one and install it. */
16384 if (*ite == NULL)
16386 tree block;
16388 decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16389 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16390 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16391 DECL_CONTEXT (decl) = current_function_decl;
16392 DECL_INITIAL (decl) = boolean_true_node;
16393 /* Don't emit any symbolic debugging info for this decl. */
16394 DECL_IGNORED_P (decl) = 1;
16396 /* The trick is to find the right context for it. */
16397 block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16398 TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16399 BLOCK_EXPR_DECLS (block) = decl;
16400 *ite = decl;
16402 else
16403 decl = *ite;
16405 /* Now simply augment the compound that holds all the assignments
16406 pertaining to this method invocation. */
16407 init = build2 (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16408 TREE_SIDE_EFFECTS (init) = 1;
16409 TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16410 TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16412 return true;
16415 #ifdef __XGETTEXT__
16416 /* Depending on the version of Bison used to compile this grammar,
16417 it may issue generic diagnostics spelled "syntax error" or
16418 "parse error". To prevent this from changing the translation
16419 template randomly, we list all the variants of this particular
16420 diagnostic here. Translators: there is no fine distinction
16421 between diagnostics with "syntax error" in them, and diagnostics
16422 with "parse error" in them. It's okay to give them both the same
16423 translation. */
16424 const char d1[] = N_("syntax error");
16425 const char d2[] = N_("parse error");
16426 const char d3[] = N_("syntax error; also virtual memory exhausted");
16427 const char d4[] = N_("parse error; also virtual memory exhausted");
16428 const char d5[] = N_("syntax error: cannot back up");
16429 const char d6[] = N_("parse error: cannot back up");
16430 #endif
16432 #include "gt-java-parse.h"
16433 #include "gtype-java.h"